mndUser.c 51.5 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndUser.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
20
#include "mndShow.h"
X
Xiaoyu Wang 已提交
21
#include "mndStb.h"
22
#include "mndTopic.h"
S
Shengliang Guan 已提交
23
#include "mndTrans.h"
S
tbase64  
Shengliang Guan 已提交
24
#include "tbase64.h"
S
Shengliang Guan 已提交
25

K
kailixu 已提交
26
#define USER_VER_NUMBER   4
27
#define USER_RESERVE_SIZE 64
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29 30 31 32
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
static int32_t  mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
static int32_t  mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
S
Shengliang Guan 已提交
33
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
S
Shengliang Guan 已提交
34 35 36 37 38 39
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
static int32_t  mndProcessCreateUserReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterUserReq(SRpcMsg *pReq);
static int32_t  mndProcessDropUserReq(SRpcMsg *pReq);
static int32_t  mndProcessGetUserAuthReq(SRpcMsg *pReq);
static int32_t  mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
40
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
41 42
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
43 44

int32_t mndInitUser(SMnode *pMnode) {
S
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53 54
  SSdbTable table = {
      .sdbType = SDB_USER,
      .keyType = SDB_KEY_BINARY,
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
      .insertFp = (SdbInsertFp)mndUserActionInsert,
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
  };
S
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56 57 58
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
S
Shengliang Guan 已提交
59
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61 62
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
S
Shengliang Guan 已提交
63 64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
S
Shengliang Guan 已提交
65 66 67 68 69 70 71
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupUser(SMnode *pMnode) {}

static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
72
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
S
Shengliang Guan 已提交
73 74 75 76
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
77 78
  userObj.sysInfo = 1;
  userObj.enable = 1;
S
Shengliang Guan 已提交
79 80

  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
81
    userObj.superUser = 1;
S
Shengliang Guan 已提交
82 83 84 85
  }

  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
86
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
87

88
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
89

90
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
91
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
92
    sdbFreeRaw(pRaw);
93 94 95
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
    return -1;
  }
96
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
97 98 99 100 101 102

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
103
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
104 105 106 107 108 109 110 111 112

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120 121 122
}

static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }

  return 0;
}

123
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
124 125
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
126 127
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
C
cademfly 已提交
128 129
  int32_t numOfReadStbs = taosHashGetSize(pUser->readTbs);
  int32_t numOfWriteStbs = taosHashGetSize(pUser->writeTbs);
130
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
X
Xiaoyu Wang 已提交
131 132 133
  int32_t numOfUseDbs = taosHashGetSize(pUser->useDbs);
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE +
                 (numOfReadDbs + numOfWriteDbs + numOfUseDbs) * TSDB_DB_FNAME_LEN + numOfTopics * TSDB_TOPIC_FNAME_LEN;
S
Shengliang Guan 已提交
134

C
cademfly 已提交
135
  char *stb = taosHashIterate(pUser->readTbs, NULL);
C
cademfly 已提交
136 137 138 139 140 141 142 143 144 145
  while (stb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(stb, &keyLen);
    size += sizeof(int32_t);
    size += keyLen;

    size_t valueLen = 0;
    valueLen = strlen(stb);
    size += sizeof(int32_t);
    size += valueLen;
C
cademfly 已提交
146
    stb = taosHashIterate(pUser->readTbs, stb);
C
cademfly 已提交
147 148
  }

C
cademfly 已提交
149
  stb = taosHashIterate(pUser->writeTbs, NULL);
C
cademfly 已提交
150 151 152 153 154
  while (stb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(stb, &keyLen);
    size += sizeof(int32_t);
    size += keyLen;
X
Xiaoyu Wang 已提交
155

C
cademfly 已提交
156 157 158
    size_t valueLen = 0;
    valueLen = strlen(stb);
    size += sizeof(int32_t);
X
Xiaoyu Wang 已提交
159
    size += valueLen;
C
cademfly 已提交
160
    stb = taosHashIterate(pUser->writeTbs, stb);
C
cademfly 已提交
161
  }
S
Shengliang Guan 已提交
162

163
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
164
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
165 166

  int32_t dataPos = 0;
167 168 169 170 171 172
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
173 174 175
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
176
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
K
kailixu 已提交
177
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
178 179
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
180
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
181 182 183

  char *db = taosHashIterate(pUser->readDbs, NULL);
  while (db != NULL) {
184
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
185 186 187 188 189
    db = taosHashIterate(pUser->readDbs, db);
  }

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
190
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
191 192 193
    db = taosHashIterate(pUser->writeDbs, db);
  }

194 195 196
  char *topic = taosHashIterate(pUser->topics, NULL);
  while (topic != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
197
    topic = taosHashIterate(pUser->topics, topic);
198 199
  }

X
Xiaoyu Wang 已提交
200 201 202 203
  SDB_SET_INT32(pRaw, dataPos, numOfReadStbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteStbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)

C
cademfly 已提交
204
  stb = taosHashIterate(pUser->readTbs, NULL);
C
table  
cademfly 已提交
205
  while (stb != NULL) {
C
cademfly 已提交
206 207 208 209 210 211
    size_t keyLen = 0;
    void  *key = taosHashGetKey(stb, &keyLen);
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);

    size_t valueLen = 0;
X
Xiaoyu Wang 已提交
212
    valueLen = strlen(stb) + 1;
C
cademfly 已提交
213 214
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
C
cademfly 已提交
215
    stb = taosHashIterate(pUser->readTbs, stb);
C
table  
cademfly 已提交
216 217
  }

C
cademfly 已提交
218
  stb = taosHashIterate(pUser->writeTbs, NULL);
C
table  
cademfly 已提交
219
  while (stb != NULL) {
C
cademfly 已提交
220 221 222 223
    size_t keyLen = 0;
    void  *key = taosHashGetKey(stb, &keyLen);
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
X
Xiaoyu Wang 已提交
224

C
cademfly 已提交
225
    size_t valueLen = 0;
X
Xiaoyu Wang 已提交
226
    valueLen = strlen(stb) + 1;
C
cademfly 已提交
227 228
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
C
cademfly 已提交
229
    stb = taosHashIterate(pUser->writeTbs, stb);
C
table  
cademfly 已提交
230 231
  }

X
Xiaoyu Wang 已提交
232 233 234 235 236 237 238 239 240 241 242
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
  while (useDb != NULL) {
    size_t keyLen = 0;
    void  *key = taosHashGetKey(useDb, &keyLen);
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);

    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
    useDb = taosHashIterate(pUser->writeTbs, useDb);
  }

243 244
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
245 246 247

  terrno = 0;

248
_OVER:
249 250 251 252 253
  if (terrno != 0) {
    mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
254

S
Shengliang Guan 已提交
255
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
256
  return pRaw;
S
Shengliang Guan 已提交
257 258
}

S
Shengliang Guan 已提交
259
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
260
  terrno = TSDB_CODE_OUT_OF_MEMORY;
261 262
  SSdbRow  *pRow = NULL;
  SUserObj *pUser = NULL;
263

S
Shengliang Guan 已提交
264
  int8_t sver = 0;
265
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
266

K
kailixu 已提交
267
  if (sver < 1 || sver > USER_VER_NUMBER) {
S
Shengliang Guan 已提交
268
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
269
    goto _OVER;
S
Shengliang Guan 已提交
270
  }
S
Shengliang Guan 已提交
271

272
  pRow = sdbAllocRow(sizeof(SUserObj));
273
  if (pRow == NULL) goto _OVER;
274

275
  pUser = sdbGetRowObj(pRow);
276
  if (pUser == NULL) goto _OVER;
277

S
Shengliang Guan 已提交
278
  int32_t dataPos = 0;
279 280 281 282 283 284
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
285 286 287
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
288
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
K
kailixu 已提交
289
  if (sver >= 4) {
K
kailixu 已提交
290 291
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
  }
292 293 294

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
295
  int32_t numOfTopics = 0;
296 297
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
298 299 300 301
  if (sver >= 2) {
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
  }

S
Shengliang Guan 已提交
302 303 304
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pUser->writeDbs =
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
305 306
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) goto _OVER;
307 308 309

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
310
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
311 312 313 314 315 316
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
317
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
318 319 320 321
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

322 323 324 325 326 327 328 329 330
  if (sver >= 2) {
    for (int32_t i = 0; i < numOfTopics; ++i) {
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
      int32_t len = strlen(topic) + 1;
      taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN);
    }
  }

X
Xiaoyu Wang 已提交
331
  if (sver >= 3) {
X
Xiaoyu Wang 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344
    int32_t numOfReadStbs = 0;
    int32_t numOfWriteStbs = 0;
    int32_t numOfUseDbs = 0;
    SDB_GET_INT32(pRaw, dataPos, &numOfReadStbs, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteStbs, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)

    pUser->readTbs =
        taosHashInit(numOfReadStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
    pUser->writeTbs =
        taosHashInit(numOfWriteStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);

C
table  
cademfly 已提交
345
    for (int32_t i = 0; i < numOfReadStbs; ++i) {
C
cademfly 已提交
346 347 348
      int32_t keyLen = 0;
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);

C
cademfly 已提交
349 350
      char *key = taosMemoryCalloc(keyLen, sizeof(char));
      memset(key, 0, keyLen);
C
cademfly 已提交
351 352 353 354
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);

      int32_t valuelen = 0;
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
C
cademfly 已提交
355
      char *value = taosMemoryCalloc(valuelen, sizeof(char));
X
Xiaoyu Wang 已提交
356
      memset(value, 0, valuelen);
C
cademfly 已提交
357 358
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)

C
cademfly 已提交
359
      taosHashPut(pUser->readTbs, key, keyLen, value, valuelen);
C
cademfly 已提交
360 361 362

      taosMemoryFree(key);
      taosMemoryFree(value);
C
table  
cademfly 已提交
363 364 365
    }

    for (int32_t i = 0; i < numOfWriteStbs; ++i) {
C
cademfly 已提交
366 367 368
      int32_t keyLen = 0;
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);

C
cademfly 已提交
369 370
      char *key = taosMemoryCalloc(keyLen, sizeof(char));
      memset(key, 0, keyLen);
C
cademfly 已提交
371 372 373 374
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);

      int32_t valuelen = 0;
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
C
cademfly 已提交
375
      char *value = taosMemoryCalloc(valuelen, sizeof(char));
X
Xiaoyu Wang 已提交
376
      memset(value, 0, valuelen);
C
cademfly 已提交
377 378
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)

C
cademfly 已提交
379
      taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen);
C
cademfly 已提交
380 381 382

      taosMemoryFree(key);
      taosMemoryFree(value);
C
table  
cademfly 已提交
383
    }
X
Xiaoyu Wang 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397

    for (int32_t i = 0; i < numOfUseDbs; ++i) {
      int32_t keyLen = 0;
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);

      char *key = taosMemoryCalloc(keyLen, sizeof(char));
      memset(key, 0, keyLen);
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);

      int32_t ref = 0;
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);

      taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref));
    }
C
table  
cademfly 已提交
398 399
  }

400
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
401
  taosInitRWLatch(&pUser->lock);
402 403 404

  terrno = 0;

405
_OVER:
406
  if (terrno != 0) {
407 408 409 410 411
    mError("user:%s, failed to decode from raw:%p since %s", pUser == NULL ? "null" : pUser->user, pRaw, terrstr());
    if (pUser != NULL) {
      taosHashCleanup(pUser->readDbs);
      taosHashCleanup(pUser->writeDbs);
      taosHashCleanup(pUser->topics);
C
cademfly 已提交
412 413
      taosHashCleanup(pUser->readTbs);
      taosHashCleanup(pUser->writeTbs);
X
Xiaoyu Wang 已提交
414
      taosHashCleanup(pUser->useDbs);
415
    }
wafwerar's avatar
wafwerar 已提交
416
    taosMemoryFreeClear(pRow);
417 418
    return NULL;
  }
S
Shengliang Guan 已提交
419

S
Shengliang Guan 已提交
420
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
421
  return pRow;
S
Shengliang Guan 已提交
422
}
S
Shengliang Guan 已提交
423

S
Shengliang Guan 已提交
424
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
425
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
S
Shengliang Guan 已提交
426

S
Shengliang Guan 已提交
427 428
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
429
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
430
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
431
    return -1;
S
Shengliang Guan 已提交
432
  }
S
Shengliang Guan 已提交
433 434
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
435

S
Shengliang Guan 已提交
436 437
  return 0;
}
S
Shengliang Guan 已提交
438

C
cademfly 已提交
439 440 441 442 443 444 445 446 447 448 449
SHashObj *mndDupTableHash(SHashObj *pOld) {
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pNew == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  char *tb = taosHashIterate(pOld, NULL);
  while (tb != NULL) {
    size_t keyLen = 0;
X
Xiaoyu Wang 已提交
450
    char  *key = taosHashGetKey(tb, &keyLen);
C
cademfly 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464

    int32_t valueLen = strlen(tb) + 1;
    if (taosHashPut(pNew, key, keyLen, tb, valueLen) != 0) {
      taosHashCancelIterate(pOld, tb);
      taosHashCleanup(pNew);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }
    tb = taosHashIterate(pOld, tb);
  }

  return pNew;
}

X
Xiaoyu Wang 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
SHashObj *mndDupUseDbHash(SHashObj *pOld) {
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pNew == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  int32_t *db = taosHashIterate(pOld, NULL);
  while (db != NULL) {
    size_t keyLen = 0;
    char  *key = taosHashGetKey(db, &keyLen);

    if (taosHashPut(pNew, key, keyLen, db, sizeof(*db)) != 0) {
      taosHashCancelIterate(pOld, db);
      taosHashCleanup(pNew);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

490 491 492 493 494 495 496 497
static int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
  memcpy(pNew, pUser, sizeof(SUserObj));
  pNew->authVersion++;
  pNew->updateTime = taosGetTimestampMs();

  taosRLockLatch(&pUser->lock);
  pNew->readDbs = mndDupDbHash(pUser->readDbs);
  pNew->writeDbs = mndDupDbHash(pUser->writeDbs);
C
cademfly 已提交
498 499
  pNew->readTbs = mndDupTableHash(pUser->readTbs);
  pNew->writeTbs = mndDupTableHash(pUser->writeTbs);
500
  pNew->topics = mndDupTopicHash(pUser->topics);
X
Xiaoyu Wang 已提交
501
  pNew->useDbs = mndDupUseDbHash(pUser->useDbs);
502 503 504 505 506 507 508 509 510
  taosRUnLockLatch(&pUser->lock);

  if (pNew->readDbs == NULL || pNew->writeDbs == NULL || pNew->topics == NULL) {
    return -1;
  }
  return 0;
}

static void mndUserFreeObj(SUserObj *pUser) {
511 512
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
513
  taosHashCleanup(pUser->topics);
C
cademfly 已提交
514 515
  taosHashCleanup(pUser->readTbs);
  taosHashCleanup(pUser->writeTbs);
X
Xiaoyu Wang 已提交
516
  taosHashCleanup(pUser->useDbs);
517 518
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
519
  pUser->topics = NULL;
C
cademfly 已提交
520 521
  pUser->readTbs = NULL;
  pUser->writeTbs = NULL;
X
Xiaoyu Wang 已提交
522
  pUser->useDbs = NULL;
523 524 525 526 527
}

static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
  mndUserFreeObj(pUser);
S
Shengliang Guan 已提交
528 529 530
  return 0;
}

S
Shengliang Guan 已提交
531
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
532
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
533
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
534
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
535
  pOld->authVersion = pNew->authVersion;
K
kailixu 已提交
536
  pOld->passVersion = pNew->passVersion;
537 538
  pOld->sysInfo = pNew->sysInfo;
  pOld->enable = pNew->enable;
S
Shengliang Guan 已提交
539
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
540 541
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
542
  TSWAP(pOld->topics, pNew->topics);
C
cademfly 已提交
543 544
  TSWAP(pOld->readTbs, pNew->readTbs);
  TSWAP(pOld->writeTbs, pNew->writeTbs);
X
Xiaoyu Wang 已提交
545
  TSWAP(pOld->useDbs, pNew->useDbs);
S
Shengliang Guan 已提交
546
  taosWUnLockLatch(&pOld->lock);
547

S
Shengliang Guan 已提交
548 549 550
  return 0;
}

551
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
S
Shengliang Guan 已提交
552 553 554
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
dengyihao's avatar
dengyihao 已提交
555 556 557 558 559
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
      terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    } else {
      terrno = TSDB_CODE_MND_USER_NOT_AVAILABLE;
    }
S
Shengliang Guan 已提交
560 561
  }
  return pUser;
S
Shengliang Guan 已提交
562
}
S
Shengliang Guan 已提交
563

S
Shengliang Guan 已提交
564 565 566
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
567 568
}

S
Shengliang Guan 已提交
569
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
570
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
571 572
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
573 574 575
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
576
  userObj.superUser = 0;  // pCreate->superUser;
577 578
  userObj.sysInfo = pCreate->sysInfo;
  userObj.enable = pCreate->enable;
S
Shengliang Guan 已提交
579

580
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
S
Shengliang Guan 已提交
581
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
582
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
583 584
    return -1;
  }
585
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
586

587 588 589
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
590
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
591
    return -1;
S
Shengliang Guan 已提交
592
  }
S
Shengliang Guan 已提交
593
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
594

S
Shengliang Guan 已提交
595
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
596
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
597
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
598
    return -1;
S
Shengliang Guan 已提交
599 600
  }

S
Shengliang Guan 已提交
601
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
602
  return 0;
S
Shengliang Guan 已提交
603 604
}

S
Shengliang Guan 已提交
605 606
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
607 608 609 610 611
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
612
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
613
    terrno = TSDB_CODE_INVALID_MSG;
614
    goto _OVER;
S
Shengliang Guan 已提交
615
  }
S
Shengliang Guan 已提交
616

617
  mInfo("user:%s, start to create", createReq.user);
618 619 620
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
621

S
Shengliang Guan 已提交
622
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
623
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
624
    goto _OVER;
S
Shengliang Guan 已提交
625 626
  }

S
Shengliang Guan 已提交
627
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
628
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
629
    goto _OVER;
S
Shengliang Guan 已提交
630 631
  }

S
Shengliang Guan 已提交
632
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
633
  if (pUser != NULL) {
S
Shengliang Guan 已提交
634
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
635
    goto _OVER;
S
Shengliang Guan 已提交
636 637
  }

638
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
639
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
640
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
641
    goto _OVER;
S
Shengliang Guan 已提交
642 643
  }

C
Cary Xu 已提交
644 645 646 647
  if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
    code = terrno;
    goto _OVER;
  }
648

S
Shengliang Guan 已提交
649
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
S
Shengliang Guan 已提交
650
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
651

652
_OVER:
S
Shengliang Guan 已提交
653
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
654
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
655 656
  }

S
Shengliang Guan 已提交
657 658 659 660
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
661 662
}

S
Shengliang Guan 已提交
663
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
664
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
S
Shengliang Guan 已提交
665
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
666
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
667 668
    return -1;
  }
669
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
670

671 672 673
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
674 675 676
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
677
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
678 679 680 681 682 683 684 685 686 687 688

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

689
SHashObj *mndDupObjHash(SHashObj *pOld, int32_t dataLen) {
S
Shengliang Guan 已提交
690 691
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
692 693 694 695 696 697 698 699
  if (pNew == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  char *db = taosHashIterate(pOld, NULL);
  while (db != NULL) {
    int32_t len = strlen(db) + 1;
700
    if (taosHashPut(pNew, db, len, db, dataLen) != 0) {
S
Shengliang Guan 已提交
701 702
      taosHashCancelIterate(pOld, db);
      taosHashCleanup(pNew);
S
Shengliang Guan 已提交
703
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
704 705 706 707 708 709 710 711
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

712 713 714 715
SHashObj *mndDupDbHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN); }

SHashObj *mndDupTopicHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN); }

X
Xiaoyu Wang 已提交
716 717
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
                                  SSdb *pSdb) {
X
Xiaoyu Wang 已提交
718 719
  void *pIter = NULL;
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
C
cademfly 已提交
720

C
cademfly 已提交
721 722
  snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
  int32_t len = strlen(tbFName) + 1;
C
cademfly 已提交
723

X
Xiaoyu Wang 已提交
724
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
725
    char *value = taosHashGet(hash, tbFName, len);
X
Xiaoyu Wang 已提交
726
    if (value != NULL) {
727 728 729 730
      terrno = TSDB_CODE_MND_PRIVILEDGE_EXIST;
      return -1;
    }

X
Xiaoyu Wang 已提交
731
    int32_t condLen = alterReq->tagCondLen;
732 733 734
    if (taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen) != 0) {
      return -1;
    }
X
Xiaoyu Wang 已提交
735
  } else {
736 737 738
    if (taosHashPut(hash, tbFName, len, "t", 2) != 0) {
      return -1;
    }
C
cademfly 已提交
739
  }
C
cademfly 已提交
740

X
Xiaoyu Wang 已提交
741 742 743 744 745 746 747 748 749 750
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
  int32_t  ref = 1;
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
  if (NULL != currRef) {
    ref = (*currRef) + 1;
  }
  if (taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)) != 0) {
    return -1;
  }

C
cademfly 已提交
751 752 753
  return 0;
}

X
Xiaoyu Wang 已提交
754 755
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
                                        SSdb *pSdb) {
X
Xiaoyu Wang 已提交
756 757
  void *pIter = NULL;
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
C
cademfly 已提交
758 759
  snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
  int32_t len = strlen(tbFName) + 1;
C
cademfly 已提交
760

C
cademfly 已提交
761 762 763
  if (taosHashRemove(hash, tbFName, len) != 0) {
    return -1;
  }
C
cademfly 已提交
764

X
Xiaoyu Wang 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
  if (NULL == currRef || 1 == *currRef) {
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
      return -1;
    }
    return 0;
  }
  int32_t ref = (*currRef) - 1;
  if (taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)) != 0) {
    return -1;
  }

C
cademfly 已提交
778 779 780
  return 0;
}

S
Shengliang Guan 已提交
781 782
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
783 784
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
785 786 787
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
788
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
789 790
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
791
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
792
    terrno = TSDB_CODE_INVALID_MSG;
793
    goto _OVER;
S
Shengliang Guan 已提交
794
  }
S
Shengliang Guan 已提交
795

796
  mInfo("user:%s, start to alter", alterReq.user);
S
Shengliang Guan 已提交
797

S
Shengliang Guan 已提交
798
  if (alterReq.user[0] == 0) {
S
Shengliang Guan 已提交
799
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
800 801 802 803 804
    goto _OVER;
  }

  if (TSDB_ALTER_USER_PASSWD == alterReq.alterType && alterReq.pass[0] == 0) {
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
805
    goto _OVER;
S
Shengliang Guan 已提交
806 807
  }

S
Shengliang Guan 已提交
808
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
809 810
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
811
    goto _OVER;
S
Shengliang Guan 已提交
812 813
  }

814
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
815 816
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
817
    goto _OVER;
S
Shengliang Guan 已提交
818 819
  }

820
  if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
S
Shengliang Guan 已提交
821 822 823
    goto _OVER;
  }

824
  if (mndUserDupObj(pUser, &newUser) != 0) goto _OVER;
S
Shengliang Guan 已提交
825

K
kailixu 已提交
826
  newUser.passVersion = pUser->passVersion;
S
Shengliang Guan 已提交
827 828 829
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
830
    memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
K
kailixu 已提交
831 832 833
    if (0 != strncmp(pUser->pass, pass, TSDB_PASSWORD_LEN)) {
      ++newUser.passVersion;
    }
S
Shengliang Guan 已提交
834 835 836
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
837
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
838 839
  }

840 841 842 843 844 845 846 847
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
    newUser.enable = alterReq.enable;
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
    newUser.sysInfo = alterReq.sysInfo;
  }

S
Shengliang Guan 已提交
848
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
849 850 851
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
852 853 854 855
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
856
      if (taosHashPut(newUser.readDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
857 858 859 860 861 862 863 864 865 866 867 868
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
    } else {
      while (1) {
        SDbObj *pDb = NULL;
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
        if (pIter == NULL) break;
        int32_t len = strlen(pDb->name) + 1;
        taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
        sdbRelease(pSdb, pDb);
      }
S
Shengliang Guan 已提交
869
    }
S
Shengliang Guan 已提交
870 871 872
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
873 874 875
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
876 877 878 879
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
880
      if (taosHashPut(newUser.writeDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
881 882 883 884 885 886 887 888 889 890 891 892
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
    } else {
      while (1) {
        SDbObj *pDb = NULL;
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
        if (pIter == NULL) break;
        int32_t len = strlen(pDb->name) + 1;
        taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
        sdbRelease(pSdb, pDb);
      }
S
Shengliang Guan 已提交
893 894 895
    }
  }

S
Shengliang Guan 已提交
896
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
897 898 899
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
900 901 902 903
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
904
      taosHashRemove(newUser.readDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
905 906 907 908
    } else {
      taosHashClear(newUser.readDbs);
    }
  }
S
Shengliang Guan 已提交
909

S
Shengliang Guan 已提交
910
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
911 912 913
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
914 915 916 917
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
918
      taosHashRemove(newUser.writeDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
919 920 921
    } else {
      taosHashClear(newUser.writeDbs);
    }
S
Shengliang Guan 已提交
922 923
  }

C
cademfly 已提交
924
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TABLE) {
X
Xiaoyu Wang 已提交
925
    if (mndTablePriviledge(pMnode, newUser.readTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
C
table  
cademfly 已提交
926 927
  }

C
cademfly 已提交
928
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TABLE) {
X
Xiaoyu Wang 已提交
929
    if (mndTablePriviledge(pMnode, newUser.writeTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
C
table  
cademfly 已提交
930 931
  }

X
Xiaoyu Wang 已提交
932 933
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_TABLE) {
    if (mndRemoveTablePriviledge(pMnode, newUser.readTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
C
cademfly 已提交
934 935
  }

X
Xiaoyu Wang 已提交
936 937
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_TABLE) {
    if (mndRemoveTablePriviledge(pMnode, newUser.writeTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
C
cademfly 已提交
938 939
  }

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
    int32_t      len = strlen(alterReq.objname) + 1;
    SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
    if (pTopic == NULL) {
      mndReleaseTopic(pMnode, pTopic);
      goto _OVER;
    }
    taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
  }

  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_SUBSCRIBE_TOPIC) {
    int32_t      len = strlen(alterReq.objname) + 1;
    SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
    if (pTopic == NULL) {
      mndReleaseTopic(pMnode, pTopic);
      goto _OVER;
    }
    taosHashRemove(newUser.topics, alterReq.objname, len);
  }

S
Shengliang Guan 已提交
960
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
961
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
962

963
_OVER:
S
Shengliang Guan 已提交
964
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
965
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
966 967
  }

S
Shengliang Guan 已提交
968 969
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
970
  mndUserFreeObj(&newUser);
S
Shengliang Guan 已提交
971 972

  return code;
S
Shengliang Guan 已提交
973 974
}

S
Shengliang Guan 已提交
975
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
976
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
S
Shengliang Guan 已提交
977 978 979 980
  if (pTrans == NULL) {
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
    return -1;
  }
981
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
S
Shengliang Guan 已提交
982

983 984 985
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
986 987 988
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
989
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
990 991 992 993 994 995 996 997 998 999 1000

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
1001 1002
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1003 1004 1005 1006
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
1007
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1008
    terrno = TSDB_CODE_INVALID_MSG;
1009
    goto _OVER;
S
Shengliang Guan 已提交
1010
  }
S
Shengliang Guan 已提交
1011

1012
  mInfo("user:%s, start to drop", dropReq.user);
1013 1014 1015
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
1016

S
Shengliang Guan 已提交
1017
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
1018
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
1019
    goto _OVER;
S
Shengliang Guan 已提交
1020 1021
  }

S
Shengliang Guan 已提交
1022
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
1023 1024
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
1025
    goto _OVER;
S
Shengliang Guan 已提交
1026 1027
  }

S
Shengliang Guan 已提交
1028
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
1029
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1030

1031
_OVER:
S
Shengliang Guan 已提交
1032
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1033
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
1034 1035
  }

S
Shengliang Guan 已提交
1036 1037
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
1038 1039
}

S
Shengliang Guan 已提交
1040 1041
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1042 1043 1044 1045 1046
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
1047
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
1048
    terrno = TSDB_CODE_INVALID_MSG;
1049
    goto _OVER;
S
Shengliang Guan 已提交
1050
  }
S
Shengliang Guan 已提交
1051 1052 1053 1054 1055 1056

  mTrace("user:%s, start to get auth", authReq.user);

  pUser = mndAcquireUser(pMnode, authReq.user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
1057
    goto _OVER;
S
Shengliang Guan 已提交
1058 1059
  }

D
dapan 已提交
1060 1061 1062
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
1063 1064
  }

S
Shengliang Guan 已提交
1065
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
1066 1067 1068
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1069
    goto _OVER;
S
Shengliang Guan 已提交
1070 1071
  }

S
Shengliang Guan 已提交
1072
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
1073

S
Shengliang Guan 已提交
1074 1075
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
1076 1077
  code = 0;

1078
_OVER:
1079

S
Shengliang Guan 已提交
1080
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1081
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
1082 1083 1084 1085

  return code;
}

S
Shengliang Guan 已提交
1086 1087
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   numOfRows = 0;
  SUserObj *pUser = NULL;
  int32_t   cols = 0;
  char     *pWrite;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
    if (pShow->pIter == NULL) break;

    cols = 0;
1099
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1100
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
1101
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1102
    colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
1103

wafwerar's avatar
wafwerar 已提交
1104 1105
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1106
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
1107

1108 1109
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1110
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->enable, false);
1111 1112 1113

    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1114
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->sysInfo, false);
1115

wafwerar's avatar
wafwerar 已提交
1116 1117
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1118
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
1119 1120 1121 1122 1123

    numOfRows++;
    sdbRelease(pSdb, pUser);
  }

1124
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1125 1126 1127 1128 1129 1130
  return numOfRows;
}

static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
1131
}
D
dapan 已提交
1132

X
Xiaoyu Wang 已提交
1133 1134 1135
static void mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *numOfRows, char *user,
                        SShowObj *pShow) {
  char   *value = taosHashIterate(hash, NULL);
C
cademfly 已提交
1136
  int32_t cols = 0;
X
Xiaoyu Wang 已提交
1137

C
cademfly 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
  while (value != NULL) {
    cols = 0;
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, *numOfRows, (const char *)userName, false);

    char privilege[20] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, *numOfRows, (const char *)privilege, false);
X
Xiaoyu Wang 已提交
1149

C
cademfly 已提交
1150 1151
    size_t keyLen = 0;
    void  *key = taosHashGetKey(value, &keyLen);
C
cademfly 已提交
1152

X
Xiaoyu Wang 已提交
1153
    char dbName[TSDB_DB_NAME_LEN] = {0};
C
cademfly 已提交
1154
    mndExtractShortDbNameFromStbFullName(key, dbName);
X
Xiaoyu Wang 已提交
1155
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1156 1157 1158 1159
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, *numOfRows, (const char *)dbNameContent, false);

X
Xiaoyu Wang 已提交
1160
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
C
cademfly 已提交
1161
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
X
Xiaoyu Wang 已提交
1162
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1163 1164 1165
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataSetVal(pColInfo, *numOfRows, (const char *)tableNameContent, false);
C
cademfly 已提交
1166

X
Xiaoyu Wang 已提交
1167
    if (strcmp("t", value) != 0) {
C
cademfly 已提交
1168 1169
      SNode  *pAst = NULL;
      int32_t sqlLen = 0;
X
Xiaoyu Wang 已提交
1170
      char    sql[TSDB_EXPLAIN_RESULT_ROW_SIZE] = {0};
C
cademfly 已提交
1171

X
Xiaoyu Wang 已提交
1172
      if (nodesStringToNode(value, &pAst) == 0) {
C
cademfly 已提交
1173
        nodesNodeToSQL(pAst, sql, TSDB_EXPLAIN_RESULT_ROW_SIZE, &sqlLen);
C
cademfly 已提交
1174
        nodesDestroyNode(pAst);
X
Xiaoyu Wang 已提交
1175
      } else {
C
cademfly 已提交
1176 1177 1178 1179
        sqlLen = 5;
        sprintf(sql, "error");
      }

X
Xiaoyu Wang 已提交
1180
      char obj[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1181
      STR_WITH_MAXSIZE_TO_VARSTR(obj, sql, pShow->pMeta->pSchemas[cols].bytes);
C
cademfly 已提交
1182 1183 1184

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, *numOfRows, (const char *)obj, false);
X
Xiaoyu Wang 已提交
1185
    } else {
1186
      char condition[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1187 1188 1189 1190
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, *numOfRows, (const char *)condition, false);
    }
X
Xiaoyu Wang 已提交
1191

C
cademfly 已提交
1192 1193 1194 1195 1196
    (*numOfRows)++;
    value = taosHashIterate(hash, value);
  }
}

S
Shengliang Guan 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   numOfRows = 0;
  SUserObj *pUser = NULL;
  int32_t   cols = 0;
  char     *pWrite;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
    if (pShow->pIter == NULL) break;

    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
C
cademfly 已提交
1212 1213 1214
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs >= rows) break;
S
Shengliang Guan 已提交
1215

1216 1217
    if (pUser->superUser) {
      cols = 0;
X
Xiaoyu Wang 已提交
1218
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
1219
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
1220
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1221
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
1222 1223 1224 1225

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1226
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
1227 1228 1229 1230

      char objName[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1231
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
1232

X
Xiaoyu Wang 已提交
1233
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1234 1235 1236 1237
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);

X
Xiaoyu Wang 已提交
1238
      char condition[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1239 1240 1241 1242
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);

1243 1244 1245
      numOfRows++;
    }

S
Shengliang Guan 已提交
1246 1247 1248
    char *db = taosHashIterate(pUser->readDbs, NULL);
    while (db != NULL) {
      cols = 0;
X
Xiaoyu Wang 已提交
1249
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
1250
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
1251
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1252
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
1253 1254 1255 1256

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1257
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
1258 1259 1260 1261 1262 1263 1264

      SName name = {0};
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
      tNameGetDbName(&name, varDataVal(objName));
      varDataSetLen(objName, strlen(varDataVal(objName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1265
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
1266

1267
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1268 1269 1270 1271
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);

1272
      char condition[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1273 1274 1275 1276
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);

S
Shengliang Guan 已提交
1277 1278 1279 1280 1281 1282 1283
      numOfRows++;
      db = taosHashIterate(pUser->readDbs, db);
    }

    db = taosHashIterate(pUser->writeDbs, NULL);
    while (db != NULL) {
      cols = 0;
1284
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
1285
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1286
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1287
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
1288 1289 1290 1291

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1292
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
1293 1294 1295 1296 1297 1298 1299

      SName name = {0};
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
      tNameGetDbName(&name, varDataVal(objName));
      varDataSetLen(objName, strlen(varDataVal(objName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1300
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
1301

1302
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1303 1304 1305 1306
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);

1307
      char condition[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1308 1309 1310 1311
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);

S
Shengliang Guan 已提交
1312 1313 1314 1315
      numOfRows++;
      db = taosHashIterate(pUser->writeDbs, db);
    }

C
cademfly 已提交
1316
    mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pUser->user, pShow);
C
table  
cademfly 已提交
1317

C
cademfly 已提交
1318
    mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pUser->user, pShow);
C
table  
cademfly 已提交
1319

S
Shengliang Guan 已提交
1320 1321 1322
    char *topic = taosHashIterate(pUser->topics, NULL);
    while (topic != NULL) {
      cols = 0;
1323
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
1324
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1325
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1326
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
1327 1328 1329 1330

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1331
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
1332 1333 1334 1335 1336

      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1337
      colDataSetVal(pColInfo, numOfRows, (const char *)topicName, false);
S
Shengliang Guan 已提交
1338

1339
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1340 1341 1342 1343
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);

1344
      char condition[TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE] = {0};
C
cademfly 已提交
1345 1346 1347 1348
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);

S
Shengliang Guan 已提交
1349
      numOfRows++;
1350
      topic = taosHashIterate(pUser->topics, topic);
S
Shengliang Guan 已提交
1351
    }
1352

S
Shengliang Guan 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
    sdbRelease(pSdb, pUser);
  }

  pShow->numOfRows += numOfRows;
  return numOfRows;
}

static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}
1364

1365 1366
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  SUserAuthBatchRsp batchRsp = {0};
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
  if (batchRsp.pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  int32_t code = 0;
  for (int32_t i = 0; i < numOfUses; ++i) {
    SUserObj *pUser = mndAcquireUser(pMnode, pUsers[i].user);
    if (pUser == NULL) {
      mError("user:%s, failed to auth user since %s", pUsers[i].user, terrstr());
      continue;
    }

D
dapan1121 已提交
1382
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
1383 1384 1385 1386
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
1387

D
dapan 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
    SGetUserAuthRsp rsp = {0};
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
    if (code) {
      mndReleaseUser(pMnode, pUser);
      tFreeSGetUserAuthRsp(&rsp);
      goto _OVER;
    }

    taosArrayPush(batchRsp.pArray, &rsp);
    mndReleaseUser(pMnode, pUser);
  }

  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
    *ppRsp = NULL;
    *pRspLen = 0;
1403

D
dapan 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    tFreeSUserAuthBatchRsp(&batchRsp);
    return 0;
  }

  int32_t rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
  void   *pRsp = taosMemoryMalloc(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    tFreeSUserAuthBatchRsp(&batchRsp);
    return -1;
  }
  tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);

  *ppRsp = pRsp;
  *pRspLen = rspLen;

  tFreeSUserAuthBatchRsp(&batchRsp);
  return 0;

_OVER:

  *ppRsp = NULL;
  *pRspLen = 0;
1427

D
dapan 已提交
1428 1429 1430
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}
1431

K
kailixu 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
int32_t mndValidateUserPassInfo(SMnode *pMnode, SUserPassVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
  int32_t           code = 0;
  SUserPassBatchRsp batchRsp = {0};

  for (int32_t i = 0; i < numOfUses; ++i) {
    SUserObj *pUser = mndAcquireUser(pMnode, pUsers[i].user);
    if (pUser == NULL) {
      mError("user:%s, failed to validate user pass since %s", pUsers[i].user, terrstr());
      continue;
    }

    pUsers[i].version = ntohl(pUsers[i].version);
    if (pUser->passVersion <= pUsers[i].version) {
1446
      mTrace("user:%s, not update since mnd passVer %d <= client passVer %d", pUsers[i].user, pUser->passVersion,
K
kailixu 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
             pUsers[i].version);
      mndReleaseUser(pMnode, pUser);
      continue;
    }

    SGetUserPassRsp rsp = {0};
    memcpy(rsp.user, pUser->user, TSDB_USER_LEN);
    rsp.version = pUser->passVersion;

    if (!batchRsp.pArray && !(batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserPassRsp)))) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _OVER;
    }

    taosArrayPush(batchRsp.pArray, &rsp);
    mndReleaseUser(pMnode, pUser);
  }

  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
    goto _OVER;
  }

  int32_t rspLen = tSerializeSUserPassBatchRsp(NULL, 0, &batchRsp);
  if (rspLen < 0) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }
  void   *pRsp = taosMemoryMalloc(rspLen);
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }
  tSerializeSUserPassBatchRsp(pRsp, rspLen, &batchRsp);

  *ppRsp = pRsp;
  *pRspLen = rspLen;

_OVER:
  if (code) {
    *ppRsp = NULL;
    *pRspLen = 0;
  }

  tFreeSUserPassBatchRsp(&batchRsp);
  return code;
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
  int32_t   code = 0;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   len = strlen(db) + 1;
  void     *pIter = NULL;
  SUserObj *pUser = NULL;
  SUserObj  newUser = {0};

  while (1) {
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
    if (pIter == NULL) break;

    code = -1;
    if (mndUserDupObj(pUser, &newUser) != 0) break;

    bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
    bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
    if (inRead || inWrite) {
      (void)taosHashRemove(newUser.readDbs, db, len);
      (void)taosHashRemove(newUser.writeDbs, db, len);

      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
      if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break;
      (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
    }

    mndUserFreeObj(&newUser);
    sdbRelease(pSdb, pUser);
    code = 0;
  }

  if (pUser != NULL) sdbRelease(pSdb, pUser);
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
  mndUserFreeObj(&newUser);
  return code;
}

int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
  int32_t   code = 0;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   len = strlen(topic) + 1;
  void     *pIter = NULL;
  SUserObj *pUser = NULL;
  SUserObj  newUser = {0};

  while (1) {
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1541 1542 1543
    if (pIter == NULL) {
      break;
    }
1544 1545

    code = -1;
1546 1547 1548
    if (mndUserDupObj(pUser, &newUser) != 0) {
      break;
    }
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567

    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
    if (inTopic) {
      (void)taosHashRemove(newUser.topics, topic, len);
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
      if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break;
      (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
    }

    mndUserFreeObj(&newUser);
    sdbRelease(pSdb, pUser);
    code = 0;
  }

  if (pUser != NULL) sdbRelease(pSdb, pUser);
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
  mndUserFreeObj(&newUser);
  return code;
}