mndUser.c 26.1 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"
18
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
19
#include "mndDb.h"
S
Shengliang Guan 已提交
20
#include "mndShow.h"
S
Shengliang Guan 已提交
21
#include "mndTrans.h"
S
tbase64  
Shengliang Guan 已提交
22
#include "tbase64.h"
S
Shengliang Guan 已提交
23

24 25
#define USER_VER_NUMBER   1
#define USER_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26

S
Shengliang Guan 已提交
27 28 29 30
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 已提交
31
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
S
Shengliang Guan 已提交
32 33 34 35 36 37
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 已提交
38
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
39 40

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

S
Shengliang Guan 已提交
52 53 54
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
S
Shengliang Guan 已提交
55
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
S
Shengliang Guan 已提交
56

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

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

  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

82
  mDebug("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
83

84
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  if (pTrans == NULL) {
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to create user:%s", pTrans->id, userObj.user);

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

  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 已提交
106 107 108 109 110 111 112 113 114 115
}

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

  return 0;
}

116
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
117 118
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
119 120
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
121
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN;
S
Shengliang Guan 已提交
122

123
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
124
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
125 126

  int32_t dataPos = 0;
127 128 129 130 131 132
  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)
133 134 135
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
136
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
137 138
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
139 140 141

  char *db = taosHashIterate(pUser->readDbs, NULL);
  while (db != NULL) {
142
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
143 144 145 146 147
    db = taosHashIterate(pUser->readDbs, db);
  }

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
148
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
149 150 151
    db = taosHashIterate(pUser->writeDbs, db);
  }

152 153
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
154 155 156

  terrno = 0;

157
_OVER:
158 159 160 161 162
  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 已提交
163

S
Shengliang Guan 已提交
164
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
165
  return pRaw;
S
Shengliang Guan 已提交
166 167
}

S
Shengliang Guan 已提交
168
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
169 170
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
171
  int8_t sver = 0;
172
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
173

174
  if (sver != USER_VER_NUMBER) {
S
Shengliang Guan 已提交
175
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
176
    goto _OVER;
S
Shengliang Guan 已提交
177
  }
S
Shengliang Guan 已提交
178

179
  SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
180
  if (pRow == NULL) goto _OVER;
181

S
Shengliang Guan 已提交
182
  SUserObj *pUser = sdbGetRowObj(pRow);
183
  if (pUser == NULL) goto _OVER;
184

S
Shengliang Guan 已提交
185
  int32_t dataPos = 0;
186 187 188 189 190 191
  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)
192 193 194
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
195
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
196 197 198

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
199 200
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
S
Shengliang Guan 已提交
201 202 203
  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);
204
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL) goto _OVER;
205 206 207

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
208
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
209 210 211 212 213 214
    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};
215
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
216 217 218 219
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

220
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
221
  taosInitRWLatch(&pUser->lock);
222 223 224

  terrno = 0;

225
_OVER:
226 227
  if (terrno != 0) {
    mError("user:%s, failed to decode from raw:%p since %s", pUser->user, pRaw, terrstr());
228 229
    taosHashCleanup(pUser->readDbs);
    taosHashCleanup(pUser->writeDbs);
wafwerar's avatar
wafwerar 已提交
230
    taosMemoryFreeClear(pRow);
231 232
    return NULL;
  }
S
Shengliang Guan 已提交
233

S
Shengliang Guan 已提交
234
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
235
  return pRow;
S
Shengliang Guan 已提交
236
}
S
Shengliang Guan 已提交
237

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

S
Shengliang Guan 已提交
241 242
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
243
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
244
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
245
    return -1;
S
Shengliang Guan 已提交
246
  }
S
Shengliang Guan 已提交
247 248
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250 251
  return 0;
}
S
Shengliang Guan 已提交
252

S
Shengliang Guan 已提交
253
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
254
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
255 256
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
257 258
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
S
Shengliang Guan 已提交
259 260 261
  return 0;
}

S
Shengliang Guan 已提交
262
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
263
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
264
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
265
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
266
  pOld->authVersion = pNew->authVersion;
267 268
  pOld->sysInfo = pNew->sysInfo;
  pOld->enable = pNew->enable;
S
Shengliang Guan 已提交
269
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
270 271
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
S
Shengliang Guan 已提交
272
  taosWUnLockLatch(&pOld->lock);
273

S
Shengliang Guan 已提交
274 275 276
  return 0;
}

277
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
S
Shengliang Guan 已提交
278 279 280
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
S
Shengliang Guan 已提交
281
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
S
Shengliang Guan 已提交
282 283
  }
  return pUser;
S
Shengliang Guan 已提交
284
}
S
Shengliang Guan 已提交
285

S
Shengliang Guan 已提交
286 287 288
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
289 290
}

S
Shengliang Guan 已提交
291
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
292
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
293 294
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
295 296 297
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
298
  userObj.superUser = 0;  // pCreate->superUser;
299 300
  userObj.sysInfo = pCreate->sysInfo;
  userObj.enable = pCreate->enable;
S
Shengliang Guan 已提交
301

302
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq);
S
Shengliang Guan 已提交
303
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
304
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
305 306
    return -1;
  }
S
Shengliang Guan 已提交
307
  mDebug("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
308

309 310 311
  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 已提交
312
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
313
    return -1;
S
Shengliang Guan 已提交
314
  }
315
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
316

S
Shengliang Guan 已提交
317
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
318
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
319
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
320
    return -1;
S
Shengliang Guan 已提交
321 322
  }

S
Shengliang Guan 已提交
323
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
324
  return 0;
S
Shengliang Guan 已提交
325 326
}

S
Shengliang Guan 已提交
327 328
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
329 330 331 332 333
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
334
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
335
    terrno = TSDB_CODE_INVALID_MSG;
336
    goto _OVER;
S
Shengliang Guan 已提交
337
  }
S
Shengliang Guan 已提交
338

S
Shengliang Guan 已提交
339
  mDebug("user:%s, start to create", createReq.user);
340 341 342
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
343

S
Shengliang Guan 已提交
344
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
345
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
346
    goto _OVER;
S
Shengliang Guan 已提交
347 348
  }

S
Shengliang Guan 已提交
349
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
350
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
351
    goto _OVER;
S
Shengliang Guan 已提交
352 353
  }

S
Shengliang Guan 已提交
354
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
355
  if (pUser != NULL) {
S
Shengliang Guan 已提交
356
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
357
    goto _OVER;
S
Shengliang Guan 已提交
358 359
  }

360
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
361
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
362
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
363
    goto _OVER;
S
Shengliang Guan 已提交
364 365
  }

C
Cary Xu 已提交
366 367 368 369
  if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
    code = terrno;
    goto _OVER;
  }
370

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

374
_OVER:
S
Shengliang Guan 已提交
375
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
376
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
377 378
  }

S
Shengliang Guan 已提交
379 380 381 382
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
383 384
}

S
Shengliang Guan 已提交
385
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
386
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq);
S
Shengliang Guan 已提交
387
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
388
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
389 390
    return -1;
  }
S
Shengliang Guan 已提交
391
  mDebug("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
392

393 394 395
  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 已提交
396 397 398
    mndTransDrop(pTrans);
    return -1;
  }
399
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
400 401 402 403 404 405 406 407 408 409 410

  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 已提交
411
static SHashObj *mndDupDbHash(SHashObj *pOld) {
S
Shengliang Guan 已提交
412 413
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
414 415 416 417 418 419 420 421 422 423 424
  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 已提交
425
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
426 427 428 429 430 431 432 433
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

S
Shengliang Guan 已提交
434 435
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
436 437
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
438 439 440
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
441
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
442 443
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
444
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
445
    terrno = TSDB_CODE_INVALID_MSG;
446
    goto _OVER;
S
Shengliang Guan 已提交
447
  }
S
Shengliang Guan 已提交
448

S
Shengliang Guan 已提交
449
  mDebug("user:%s, start to alter", alterReq.user);
S
Shengliang Guan 已提交
450

S
Shengliang Guan 已提交
451
  if (alterReq.user[0] == 0) {
S
Shengliang Guan 已提交
452
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
453 454 455 456 457
    goto _OVER;
  }

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

S
Shengliang Guan 已提交
461
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
462 463
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
464
    goto _OVER;
S
Shengliang Guan 已提交
465 466
  }

467
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
468 469
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
470
    goto _OVER;
S
Shengliang Guan 已提交
471 472
  }

473
  if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
S
Shengliang Guan 已提交
474 475 476
    goto _OVER;
  }

S
Shengliang Guan 已提交
477
  memcpy(&newUser, pUser, sizeof(SUserObj));
S
Shengliang Guan 已提交
478 479
  newUser.authVersion++;
  newUser.updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
480 481

  taosRLockLatch(&pUser->lock);
S
Shengliang Guan 已提交
482 483
  newUser.readDbs = mndDupDbHash(pUser->readDbs);
  newUser.writeDbs = mndDupDbHash(pUser->writeDbs);
S
Shengliang Guan 已提交
484 485
  taosRUnLockLatch(&pUser->lock);

S
Shengliang Guan 已提交
486
  if (newUser.readDbs == NULL || newUser.writeDbs == NULL) {
487
    goto _OVER;
S
Shengliang Guan 已提交
488 489 490 491 492
  }

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

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
497
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
498 499
  }

500 501 502 503 504 505 506 507
  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 已提交
508
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
509
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
      int32_t len = strlen(alterReq.dbname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname);
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
      if (taosHashPut(newUser.readDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) {
        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 已提交
529
    }
S
Shengliang Guan 已提交
530 531 532
  }

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

S
Shengliang Guan 已提交
556
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
S
Shengliang 已提交
557
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
558 559 560 561 562 563 564 565 566 567 568
      int32_t len = strlen(alterReq.dbname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname);
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
      taosHashRemove(newUser.readDbs, alterReq.dbname, len);
    } else {
      taosHashClear(newUser.readDbs);
    }
  }
S
Shengliang Guan 已提交
569

S
Shengliang Guan 已提交
570
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
S
Shengliang 已提交
571
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
572 573 574 575 576 577 578 579 580 581
      int32_t len = strlen(alterReq.dbname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname);
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
      taosHashRemove(newUser.writeDbs, alterReq.dbname, len);
    } else {
      taosHashClear(newUser.writeDbs);
    }
S
Shengliang Guan 已提交
582 583
  }

S
Shengliang Guan 已提交
584
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
585
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
586

587
_OVER:
S
Shengliang Guan 已提交
588
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
589
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
590 591
  }

S
Shengliang Guan 已提交
592 593
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
594 595
  taosHashCleanup(newUser.writeDbs);
  taosHashCleanup(newUser.readDbs);
S
Shengliang Guan 已提交
596 597

  return code;
S
Shengliang Guan 已提交
598 599
}

S
Shengliang Guan 已提交
600
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
601
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq);
S
Shengliang Guan 已提交
602 603 604 605 606 607
  if (pTrans == NULL) {
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to drop user:%s", pTrans->id, pUser->user);

608 609 610
  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 已提交
611 612 613
    mndTransDrop(pTrans);
    return -1;
  }
614
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
615 616 617 618 619 620 621 622 623 624 625

  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 已提交
626 627
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
628 629 630 631
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
632
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
633
    terrno = TSDB_CODE_INVALID_MSG;
634
    goto _OVER;
S
Shengliang Guan 已提交
635
  }
S
Shengliang Guan 已提交
636

S
Shengliang Guan 已提交
637
  mDebug("user:%s, start to drop", dropReq.user);
638 639 640
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
641

S
Shengliang Guan 已提交
642
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
643
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
644
    goto _OVER;
S
Shengliang Guan 已提交
645 646
  }

S
Shengliang Guan 已提交
647
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
648 649
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
650
    goto _OVER;
S
Shengliang Guan 已提交
651 652
  }

S
Shengliang Guan 已提交
653
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
654
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
655

656
_OVER:
S
Shengliang Guan 已提交
657
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
658
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
659 660
  }

S
Shengliang Guan 已提交
661 662
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
663 664
}

665
static int32_t mndSetUserAuthRsp(SMnode *pMnode, SUserObj *pUser, SGetUserAuthRsp *pRsp) {
D
dapan 已提交
666 667 668
  memcpy(pRsp->user, pUser->user, TSDB_USER_LEN);
  pRsp->superAuth = pUser->superUser;
  pRsp->version = pUser->authVersion;
669
  taosRLockLatch(&pUser->lock);
D
dapan 已提交
670 671
  pRsp->readDbs = mndDupDbHash(pUser->readDbs);
  pRsp->writeDbs = mndDupDbHash(pUser->writeDbs);
672
  taosRUnLockLatch(&pUser->lock);
D
dapan 已提交
673 674 675 676 677
  pRsp->createdDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  if (NULL == pRsp->createdDbs) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
678

D
dapan 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SDbObj *pDb = NULL;
    pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
    if (pIter == NULL) break;

    if (strcmp(pDb->createUser, pUser->user) == 0) {
      int32_t len = strlen(pDb->name) + 1;
      taosHashPut(pRsp->createdDbs, pDb->name, len, pDb->name, len);
    }

    sdbRelease(pSdb, pDb);
  }

  return 0;
}

S
Shengliang Guan 已提交
697 698
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
699 700 701 702 703
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
704
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
705
    terrno = TSDB_CODE_INVALID_MSG;
706
    goto _OVER;
S
Shengliang Guan 已提交
707
  }
S
Shengliang Guan 已提交
708 709 710 711 712 713

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

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

D
dapan 已提交
717 718 719
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
720 721
  }

S
Shengliang Guan 已提交
722
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
723 724 725
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
726
    goto _OVER;
S
Shengliang Guan 已提交
727 728
  }

S
Shengliang Guan 已提交
729
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
730

S
Shengliang Guan 已提交
731 732
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
733 734
  code = 0;

735
_OVER:
736

S
Shengliang Guan 已提交
737
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
738
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
739 740 741 742

  return code;
}

S
Shengliang Guan 已提交
743 744
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
745 746 747 748 749 750 751 752 753 754 755
  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;
756
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
757
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
758
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
759
    colDataAppend(pColInfo, numOfRows, (const char *)name, false);
760

wafwerar's avatar
wafwerar 已提交
761 762
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
763
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
764

765 766 767 768 769 770 771
    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);
772

wafwerar's avatar
wafwerar 已提交
773 774
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
775
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
776 777 778 779 780

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

781
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
782 783 784 785 786 787
  return numOfRows;
}

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

790 791
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  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 已提交
807
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
808 809 810 811
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
812

D
dapan 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    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;
828

D
dapan 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    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;
852

D
dapan 已提交
853 854 855
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}