You need to sign in or sign up before continuing.
mndUser.c 27.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"
21
#include "mndTopic.h"
S
Shengliang Guan 已提交
22
#include "mndTrans.h"
S
tbase64  
Shengliang Guan 已提交
23
#include "tbase64.h"
S
Shengliang Guan 已提交
24

25
#define USER_VER_NUMBER   2
26
#define USER_RESERVE_SIZE 64
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28 29 30 31
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 已提交
32
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
S
Shengliang Guan 已提交
33 34 35 36 37 38
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 已提交
39
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
40 41
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
42 43

int32_t mndInitUser(SMnode *pMnode) {
S
Shengliang Guan 已提交
44 45 46 47 48 49 50 51 52 53
  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 已提交
54

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

S
Shengliang Guan 已提交
60 61
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
S
Shengliang Guan 已提交
62 63 64 65 66 67 68
  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 已提交
69
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
S
Shengliang Guan 已提交
70 71 72 73
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
74 75
  userObj.sysInfo = 1;
  userObj.enable = 1;
S
Shengliang Guan 已提交
76 77

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

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

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

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

  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 已提交
100
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
101 102 103 104 105 106 107 108 109

  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 已提交
110 111 112 113 114 115 116 117 118 119
}

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

  return 0;
}

120
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
121 122
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
123 124
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
125 126 127
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
                 numOfTopics * TSDB_TOPIC_FNAME_LEN;
S
Shengliang Guan 已提交
128

129
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
130
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
131 132

  int32_t dataPos = 0;
133 134 135 136 137 138
  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)
139 140 141
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
142
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
143 144
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
145
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
146 147 148

  char *db = taosHashIterate(pUser->readDbs, NULL);
  while (db != NULL) {
149
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
150 151 152 153 154
    db = taosHashIterate(pUser->readDbs, db);
  }

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
155
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
156 157 158
    db = taosHashIterate(pUser->writeDbs, db);
  }

159 160 161 162 163 164
  char *topic = taosHashIterate(pUser->topics, NULL);
  while (topic != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
    db = taosHashIterate(pUser->topics, topic);
  }

165 166
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
167 168 169

  terrno = 0;

170
_OVER:
171 172 173 174 175
  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 已提交
176

S
Shengliang Guan 已提交
177
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
178
  return pRaw;
S
Shengliang Guan 已提交
179 180
}

S
Shengliang Guan 已提交
181
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
182 183
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
184
  int8_t sver = 0;
185
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
186

187
  if (sver != 1 && sver != 2) {
S
Shengliang Guan 已提交
188
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
189
    goto _OVER;
S
Shengliang Guan 已提交
190
  }
S
Shengliang Guan 已提交
191

192
  SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
193
  if (pRow == NULL) goto _OVER;
194

S
Shengliang Guan 已提交
195
  SUserObj *pUser = sdbGetRowObj(pRow);
196
  if (pUser == NULL) goto _OVER;
197

S
Shengliang Guan 已提交
198
  int32_t dataPos = 0;
199 200 201 202 203 204
  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)
205 206 207
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
208
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
209 210 211

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
212
  int32_t numOfTopics = 0;
213 214
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
215 216 217 218
  if (sver >= 2) {
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
  }

S
Shengliang Guan 已提交
219 220 221
  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);
222 223
  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;
224 225 226

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
227
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
228 229 230 231 232 233
    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};
234
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
235 236 237 238
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

239 240 241 242 243 244 245 246 247
  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);
    }
  }

248
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
249
  taosInitRWLatch(&pUser->lock);
250 251 252

  terrno = 0;

253
_OVER:
254 255
  if (terrno != 0) {
    mError("user:%s, failed to decode from raw:%p since %s", pUser->user, pRaw, terrstr());
256 257
    taosHashCleanup(pUser->readDbs);
    taosHashCleanup(pUser->writeDbs);
258
    taosHashCleanup(pUser->topics);
wafwerar's avatar
wafwerar 已提交
259
    taosMemoryFreeClear(pRow);
260 261
    return NULL;
  }
S
Shengliang Guan 已提交
262

S
Shengliang Guan 已提交
263
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
264
  return pRow;
S
Shengliang Guan 已提交
265
}
S
Shengliang Guan 已提交
266

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

S
Shengliang Guan 已提交
270 271
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
272
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
273
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
274
    return -1;
S
Shengliang Guan 已提交
275
  }
S
Shengliang Guan 已提交
276 277
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
278

S
Shengliang Guan 已提交
279 280
  return 0;
}
S
Shengliang Guan 已提交
281

S
Shengliang Guan 已提交
282
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
283
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
284 285
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
286
  taosHashCleanup(pUser->topics);
287 288
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
289
  pUser->topics = NULL;
S
Shengliang Guan 已提交
290 291 292
  return 0;
}

S
Shengliang Guan 已提交
293
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
294
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
295
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
296
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
297
  pOld->authVersion = pNew->authVersion;
298 299
  pOld->sysInfo = pNew->sysInfo;
  pOld->enable = pNew->enable;
S
Shengliang Guan 已提交
300
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
301 302
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
303
  TSWAP(pOld->topics, pNew->topics);
S
Shengliang Guan 已提交
304
  taosWUnLockLatch(&pOld->lock);
305

S
Shengliang Guan 已提交
306 307 308
  return 0;
}

309
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
S
Shengliang Guan 已提交
310 311 312
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
S
Shengliang Guan 已提交
313
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
S
Shengliang Guan 已提交
314 315
  }
  return pUser;
S
Shengliang Guan 已提交
316
}
S
Shengliang Guan 已提交
317

S
Shengliang Guan 已提交
318 319 320
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
321 322
}

S
Shengliang Guan 已提交
323
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
324
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
325 326
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
327 328 329
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
330
  userObj.superUser = 0;  // pCreate->superUser;
331 332
  userObj.sysInfo = pCreate->sysInfo;
  userObj.enable = pCreate->enable;
S
Shengliang Guan 已提交
333

334
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
S
Shengliang Guan 已提交
335
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
336
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
337 338
    return -1;
  }
339
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
340

341 342 343
  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 已提交
344
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
345
    return -1;
S
Shengliang Guan 已提交
346
  }
S
Shengliang Guan 已提交
347
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
348

S
Shengliang Guan 已提交
349
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
350
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
351
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
352
    return -1;
S
Shengliang Guan 已提交
353 354
  }

S
Shengliang Guan 已提交
355
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
356
  return 0;
S
Shengliang Guan 已提交
357 358
}

S
Shengliang Guan 已提交
359 360
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
361 362 363 364 365
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
366
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
367
    terrno = TSDB_CODE_INVALID_MSG;
368
    goto _OVER;
S
Shengliang Guan 已提交
369
  }
S
Shengliang Guan 已提交
370

371
  mInfo("user:%s, start to create", createReq.user);
372 373 374
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
375

S
Shengliang Guan 已提交
376
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
377
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
378
    goto _OVER;
S
Shengliang Guan 已提交
379 380
  }

S
Shengliang Guan 已提交
381
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
382
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
383
    goto _OVER;
S
Shengliang Guan 已提交
384 385
  }

S
Shengliang Guan 已提交
386
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
387
  if (pUser != NULL) {
S
Shengliang Guan 已提交
388
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
389
    goto _OVER;
S
Shengliang Guan 已提交
390 391
  }

392
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
393
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
394
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
395
    goto _OVER;
S
Shengliang Guan 已提交
396 397
  }

C
Cary Xu 已提交
398 399 400 401
  if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
    code = terrno;
    goto _OVER;
  }
402

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

406
_OVER:
S
Shengliang Guan 已提交
407
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
408
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
409 410
  }

S
Shengliang Guan 已提交
411 412 413 414
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
415 416
}

S
Shengliang Guan 已提交
417
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
418
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
S
Shengliang Guan 已提交
419
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
420
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
421 422
    return -1;
  }
423
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
424

425 426 427
  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 已提交
428 429 430
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
431
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
432 433 434 435 436 437 438 439 440 441 442

  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 已提交
443
SHashObj *mndDupDbHash(SHashObj *pOld) {
S
Shengliang Guan 已提交
444 445
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
446 447 448 449 450 451 452 453 454 455 456
  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;
    if (taosHashPut(pNew, db, len, db, TSDB_DB_FNAME_LEN) != 0) {
      taosHashCancelIterate(pOld, db);
      taosHashCleanup(pNew);
S
Shengliang Guan 已提交
457
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
458 459 460 461 462 463 464 465
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

S
Shengliang Guan 已提交
466 467
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
468 469
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
470 471 472
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
473
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
474 475
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
476
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
477
    terrno = TSDB_CODE_INVALID_MSG;
478
    goto _OVER;
S
Shengliang Guan 已提交
479
  }
S
Shengliang Guan 已提交
480

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

S
Shengliang Guan 已提交
483
  if (alterReq.user[0] == 0) {
S
Shengliang Guan 已提交
484
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
485 486 487 488 489
    goto _OVER;
  }

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

S
Shengliang Guan 已提交
493
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
494 495
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
496
    goto _OVER;
S
Shengliang Guan 已提交
497 498
  }

499
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
500 501
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
502
    goto _OVER;
S
Shengliang Guan 已提交
503 504
  }

505
  if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
S
Shengliang Guan 已提交
506 507 508
    goto _OVER;
  }

S
Shengliang Guan 已提交
509
  memcpy(&newUser, pUser, sizeof(SUserObj));
S
Shengliang Guan 已提交
510 511
  newUser.authVersion++;
  newUser.updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
512 513

  taosRLockLatch(&pUser->lock);
S
Shengliang Guan 已提交
514 515
  newUser.readDbs = mndDupDbHash(pUser->readDbs);
  newUser.writeDbs = mndDupDbHash(pUser->writeDbs);
516
  newUser.topics = mndDupDbHash(pUser->topics);
S
Shengliang Guan 已提交
517 518
  taosRUnLockLatch(&pUser->lock);

519
  if (newUser.readDbs == NULL || newUser.writeDbs == NULL || newUser.topics == NULL) {
520
    goto _OVER;
S
Shengliang Guan 已提交
521 522 523 524 525
  }

  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
526
    memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
527 528 529
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
530
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
531 532
  }

533 534 535 536 537 538 539 540
  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 已提交
541
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
542 543 544
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
545 546 547 548
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
549
      if (taosHashPut(newUser.readDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
550 551 552 553 554 555 556 557 558 559 560 561
        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 已提交
562
    }
S
Shengliang Guan 已提交
563 564 565
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
566 567 568
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
569 570 571 572
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
573
      if (taosHashPut(newUser.writeDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
574 575 576 577 578 579 580 581 582 583 584 585
        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 已提交
586 587 588
    }
  }

S
Shengliang Guan 已提交
589
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
590 591 592
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
593 594 595 596
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
597
      taosHashRemove(newUser.readDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
598 599 600 601
    } else {
      taosHashClear(newUser.readDbs);
    }
  }
S
Shengliang Guan 已提交
602

S
Shengliang Guan 已提交
603
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
604 605 606
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
607 608 609 610
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
611
      taosHashRemove(newUser.writeDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
612 613 614
    } else {
      taosHashClear(newUser.writeDbs);
    }
S
Shengliang Guan 已提交
615 616
  }

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  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 已提交
637
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
638
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
639

640
_OVER:
S
Shengliang Guan 已提交
641
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
642
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
643 644
  }

S
Shengliang Guan 已提交
645 646
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
647 648
  taosHashCleanup(newUser.writeDbs);
  taosHashCleanup(newUser.readDbs);
649
  taosHashCleanup(newUser.topics);
S
Shengliang Guan 已提交
650 651

  return code;
S
Shengliang Guan 已提交
652 653
}

S
Shengliang Guan 已提交
654
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
655
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
S
Shengliang Guan 已提交
656 657 658 659
  if (pTrans == NULL) {
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
    return -1;
  }
660
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
S
Shengliang Guan 已提交
661

662 663 664
  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 已提交
665 666 667
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
668
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
669 670 671 672 673 674 675 676 677 678 679

  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 已提交
680 681
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
682 683 684 685
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
686
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
687
    terrno = TSDB_CODE_INVALID_MSG;
688
    goto _OVER;
S
Shengliang Guan 已提交
689
  }
S
Shengliang Guan 已提交
690

691
  mInfo("user:%s, start to drop", dropReq.user);
692 693 694
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
695

S
Shengliang Guan 已提交
696
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
697
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
698
    goto _OVER;
S
Shengliang Guan 已提交
699 700
  }

S
Shengliang Guan 已提交
701
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
702 703
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
704
    goto _OVER;
S
Shengliang Guan 已提交
705 706
  }

S
Shengliang Guan 已提交
707
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
708
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
709

710
_OVER:
S
Shengliang Guan 已提交
711
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
712
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
713 714
  }

S
Shengliang Guan 已提交
715 716
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
717 718
}

S
Shengliang Guan 已提交
719 720
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
721 722 723 724 725
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
726
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
727
    terrno = TSDB_CODE_INVALID_MSG;
728
    goto _OVER;
S
Shengliang Guan 已提交
729
  }
S
Shengliang Guan 已提交
730 731 732 733 734 735

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

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

D
dapan 已提交
739 740 741
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
742 743
  }

S
Shengliang Guan 已提交
744
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
745 746 747
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
748
    goto _OVER;
S
Shengliang Guan 已提交
749 750
  }

S
Shengliang Guan 已提交
751
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
752

S
Shengliang Guan 已提交
753 754
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
755 756
  code = 0;

757
_OVER:
758

S
Shengliang Guan 已提交
759
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
760
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
761 762 763 764

  return code;
}

S
Shengliang Guan 已提交
765 766
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
767 768 769 770 771 772 773 774 775 776 777
  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;
778
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
779
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
780
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
781
    colDataAppend(pColInfo, numOfRows, (const char *)name, false);
782

wafwerar's avatar
wafwerar 已提交
783 784
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
785
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
786

787 788 789 790 791 792 793
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->enable, false);

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

wafwerar's avatar
wafwerar 已提交
795 796
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
797
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
798 799 800 801 802

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

803
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
804 805 806 807 808 809
  return numOfRows;
}

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

812 813 814 815
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { return 0; }

static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {}

816 817
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  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 已提交
833
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
834 835 836 837
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
838

D
dapan 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    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;
854

D
dapan 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    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;
878

D
dapan 已提交
879 880 881
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}