mndUser.c 37.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"
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

K
kailixu 已提交
25
#define USER_VER_NUMBER   3
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
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
S
Shengliang Guan 已提交
64 65 66 67 68 69 70
  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 已提交
71
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
S
Shengliang Guan 已提交
72 73 74 75
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
76 77
  userObj.sysInfo = 1;
  userObj.enable = 1;
S
Shengliang Guan 已提交
78 79

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

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

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

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

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

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

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

  return 0;
}

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

S
Shengliang Guan 已提交
125 126
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
127 128 129
  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 已提交
130

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

  int32_t dataPos = 0;
135 136 137 138 139 140
  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)
141 142 143
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
144
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
K
kailixu 已提交
145
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
146 147
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
148
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
149 150 151

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

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
158
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
159 160 161
    db = taosHashIterate(pUser->writeDbs, db);
  }

162 163 164
  char *topic = taosHashIterate(pUser->topics, NULL);
  while (topic != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
165
    topic = taosHashIterate(pUser->topics, topic);
166 167
  }

168 169
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
170 171 172

  terrno = 0;

173
_OVER:
174 175 176 177 178
  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 已提交
179

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

S
Shengliang Guan 已提交
184
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
185
  terrno = TSDB_CODE_OUT_OF_MEMORY;
186 187
  SSdbRow  *pRow = NULL;
  SUserObj *pUser = NULL;
188

S
Shengliang Guan 已提交
189
  int8_t sver = 0;
190
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
191

K
kailixu 已提交
192
  if (sver < 1 || sver > USER_VER_NUMBER) {
S
Shengliang Guan 已提交
193
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
194
    goto _OVER;
S
Shengliang Guan 已提交
195
  }
S
Shengliang Guan 已提交
196

197
  pRow = sdbAllocRow(sizeof(SUserObj));
198
  if (pRow == NULL) goto _OVER;
199

200
  pUser = sdbGetRowObj(pRow);
201
  if (pUser == NULL) goto _OVER;
202

S
Shengliang Guan 已提交
203
  int32_t dataPos = 0;
204 205 206 207 208 209
  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)
210 211 212
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
213
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
K
kailixu 已提交
214 215 216
  if (sver >= 3) {
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
  }
217 218 219

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
220
  int32_t numOfTopics = 0;
221 222
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
223 224 225 226
  if (sver >= 2) {
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
  }

S
Shengliang Guan 已提交
227 228 229
  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);
230 231
  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;
232 233 234

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
235
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
236 237 238 239 240 241
    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};
242
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
243 244 245 246
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

247 248 249 250 251 252 253 254 255
  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);
    }
  }

256
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
257
  taosInitRWLatch(&pUser->lock);
258 259 260

  terrno = 0;

261
_OVER:
262
  if (terrno != 0) {
263 264 265 266 267 268
    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);
    }
wafwerar's avatar
wafwerar 已提交
269
    taosMemoryFreeClear(pRow);
270 271
    return NULL;
  }
S
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
274
  return pRow;
S
Shengliang Guan 已提交
275
}
S
Shengliang Guan 已提交
276

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

S
Shengliang Guan 已提交
280 281
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
282
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
283
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
284
    return -1;
S
Shengliang Guan 已提交
285
  }
S
Shengliang Guan 已提交
286 287
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
289 290
  return 0;
}
S
Shengliang Guan 已提交
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
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);
  pNew->topics = mndDupTopicHash(pUser->topics);
  taosRUnLockLatch(&pUser->lock);

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

static void mndUserFreeObj(SUserObj *pUser) {
310 311
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
312
  taosHashCleanup(pUser->topics);
313 314
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
315
  pUser->topics = NULL;
316 317 318 319 320
}

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

S
Shengliang Guan 已提交
324
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
325
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
326
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
327
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
328
  pOld->authVersion = pNew->authVersion;
K
kailixu 已提交
329
  pOld->passVersion = pNew->passVersion;
330 331
  pOld->sysInfo = pNew->sysInfo;
  pOld->enable = pNew->enable;
S
Shengliang Guan 已提交
332
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
333 334
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
335
  TSWAP(pOld->topics, pNew->topics);
S
Shengliang Guan 已提交
336
  taosWUnLockLatch(&pOld->lock);
337

S
Shengliang Guan 已提交
338 339 340
  return 0;
}

341
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
S
Shengliang Guan 已提交
342 343 344
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
dengyihao's avatar
dengyihao 已提交
345 346 347 348 349
    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 已提交
350 351
  }
  return pUser;
S
Shengliang Guan 已提交
352
}
S
Shengliang Guan 已提交
353

S
Shengliang Guan 已提交
354 355 356
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
357 358
}

S
Shengliang Guan 已提交
359
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
360
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
361 362
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
363 364 365
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
366
  userObj.superUser = 0;  // pCreate->superUser;
367 368
  userObj.sysInfo = pCreate->sysInfo;
  userObj.enable = pCreate->enable;
S
Shengliang Guan 已提交
369

370
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
S
Shengliang Guan 已提交
371
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
372
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
373 374
    return -1;
  }
375
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
376

377 378 379
  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 已提交
380
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
381
    return -1;
S
Shengliang Guan 已提交
382
  }
S
Shengliang Guan 已提交
383
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
384

S
Shengliang Guan 已提交
385
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
386
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
387
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
388
    return -1;
S
Shengliang Guan 已提交
389 390
  }

S
Shengliang Guan 已提交
391
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
392
  return 0;
S
Shengliang Guan 已提交
393 394
}

S
Shengliang Guan 已提交
395 396
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
397 398 399 400 401
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
402
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
403
    terrno = TSDB_CODE_INVALID_MSG;
404
    goto _OVER;
S
Shengliang Guan 已提交
405
  }
S
Shengliang Guan 已提交
406

407
  mInfo("user:%s, start to create", createReq.user);
408 409 410
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
411

S
Shengliang Guan 已提交
412
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
413
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
414
    goto _OVER;
S
Shengliang Guan 已提交
415 416
  }

S
Shengliang Guan 已提交
417
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
418
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
419
    goto _OVER;
S
Shengliang Guan 已提交
420 421
  }

S
Shengliang Guan 已提交
422
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
423
  if (pUser != NULL) {
S
Shengliang Guan 已提交
424
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
425
    goto _OVER;
S
Shengliang Guan 已提交
426 427
  }

428
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
429
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
430
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
431
    goto _OVER;
S
Shengliang Guan 已提交
432 433
  }

C
Cary Xu 已提交
434 435 436 437
  if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
    code = terrno;
    goto _OVER;
  }
438

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

442
_OVER:
S
Shengliang Guan 已提交
443
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
444
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
445 446
  }

S
Shengliang Guan 已提交
447 448 449 450
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
451 452
}

S
Shengliang Guan 已提交
453
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
454
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
S
Shengliang Guan 已提交
455
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
456
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
457 458
    return -1;
  }
459
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
460

461 462 463
  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 已提交
464 465 466
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
467
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
468 469 470 471 472 473 474 475 476 477 478

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

  mndTransDrop(pTrans);
  return 0;
}

479
SHashObj *mndDupObjHash(SHashObj *pOld, int32_t dataLen) {
S
Shengliang Guan 已提交
480 481
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
482 483 484 485 486 487 488 489
  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;
490
    if (taosHashPut(pNew, db, len, db, dataLen) != 0) {
S
Shengliang Guan 已提交
491 492
      taosHashCancelIterate(pOld, db);
      taosHashCleanup(pNew);
S
Shengliang Guan 已提交
493
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
494 495 496 497 498 499 500 501
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

502 503 504 505
SHashObj *mndDupDbHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN); }

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

S
Shengliang Guan 已提交
506 507
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
508 509
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
510 511 512
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
513
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
514 515
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
516
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
517
    terrno = TSDB_CODE_INVALID_MSG;
518
    goto _OVER;
S
Shengliang Guan 已提交
519
  }
S
Shengliang Guan 已提交
520

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

S
Shengliang Guan 已提交
523
  if (alterReq.user[0] == 0) {
S
Shengliang Guan 已提交
524
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
525 526 527 528 529
    goto _OVER;
  }

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

S
Shengliang Guan 已提交
533
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
534 535
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
536
    goto _OVER;
S
Shengliang Guan 已提交
537 538
  }

539
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
540 541
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
542
    goto _OVER;
S
Shengliang Guan 已提交
543 544
  }

545
  if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
S
Shengliang Guan 已提交
546 547 548
    goto _OVER;
  }

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

K
kailixu 已提交
551
  newUser.passVersion = pUser->passVersion;
S
Shengliang Guan 已提交
552 553 554
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
555
    memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
K
kailixu 已提交
556 557 558
    if (0 != strncmp(pUser->pass, pass, TSDB_PASSWORD_LEN)) {
      ++newUser.passVersion;
    }
S
Shengliang Guan 已提交
559 560 561
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
562
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
563 564
  }

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

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
598 599 600
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
601 602 603 604
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
605
      if (taosHashPut(newUser.writeDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
606 607 608 609 610 611 612 613 614 615 616 617
        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 已提交
618 619 620
    }
  }

S
Shengliang Guan 已提交
621
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
622 623 624
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
625 626 627 628
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
629
      taosHashRemove(newUser.readDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
630 631 632 633
    } else {
      taosHashClear(newUser.readDbs);
    }
  }
S
Shengliang Guan 已提交
634

S
Shengliang Guan 已提交
635
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
636 637 638
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
639 640 641 642
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
643
      taosHashRemove(newUser.writeDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
644 645 646
    } else {
      taosHashClear(newUser.writeDbs);
    }
S
Shengliang Guan 已提交
647 648
  }

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  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 已提交
669
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
670
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
671

672
_OVER:
S
Shengliang Guan 已提交
673
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
674
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
675 676
  }

S
Shengliang Guan 已提交
677 678
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
679
  mndUserFreeObj(&newUser);
S
Shengliang Guan 已提交
680 681

  return code;
S
Shengliang Guan 已提交
682 683
}

S
Shengliang Guan 已提交
684
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
685
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
S
Shengliang Guan 已提交
686 687 688 689
  if (pTrans == NULL) {
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
    return -1;
  }
690
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
S
Shengliang Guan 已提交
691

692 693 694
  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 已提交
695 696 697
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
698
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
699 700 701 702 703 704 705 706 707 708 709

  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 已提交
710 711
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
712 713 714 715
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
716
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
717
    terrno = TSDB_CODE_INVALID_MSG;
718
    goto _OVER;
S
Shengliang Guan 已提交
719
  }
S
Shengliang Guan 已提交
720

721
  mInfo("user:%s, start to drop", dropReq.user);
722 723 724
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
725

S
Shengliang Guan 已提交
726
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
727
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
728
    goto _OVER;
S
Shengliang Guan 已提交
729 730
  }

S
Shengliang Guan 已提交
731
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
732 733
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
734
    goto _OVER;
S
Shengliang Guan 已提交
735 736
  }

S
Shengliang Guan 已提交
737
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
738
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
739

740
_OVER:
S
Shengliang Guan 已提交
741
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
742
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
743 744
  }

S
Shengliang Guan 已提交
745 746
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
747 748
}

S
Shengliang Guan 已提交
749 750
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
751 752 753 754 755
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
756
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
757
    terrno = TSDB_CODE_INVALID_MSG;
758
    goto _OVER;
S
Shengliang Guan 已提交
759
  }
S
Shengliang Guan 已提交
760 761 762 763 764 765

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

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

D
dapan 已提交
769 770 771
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
772 773
  }

S
Shengliang Guan 已提交
774
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
775 776 777
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
778
    goto _OVER;
S
Shengliang Guan 已提交
779 780
  }

S
Shengliang Guan 已提交
781
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
782

S
Shengliang Guan 已提交
783 784
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
785 786
  code = 0;

787
_OVER:
788

S
Shengliang Guan 已提交
789
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
790
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
791 792 793 794

  return code;
}

S
Shengliang Guan 已提交
795 796
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
797 798 799 800 801 802 803 804 805 806 807
  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;
808
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
809
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
810
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
811
    colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
812

wafwerar's avatar
wafwerar 已提交
813 814
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
815
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
816

817 818
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
819
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->enable, false);
820 821 822

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

wafwerar's avatar
wafwerar 已提交
825 826
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
827
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
828 829 830 831 832

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

833
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
834 835 836 837 838 839
  return numOfRows;
}

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

S
Shengliang Guan 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
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);
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics >= rows) break;

859 860
    if (pUser->superUser) {
      cols = 0;
X
Xiaoyu Wang 已提交
861
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
862
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
863
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
864
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
865 866 867 868

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
869
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
870 871 872 873

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

      numOfRows++;
    }

S
Shengliang Guan 已提交
879 880 881
    char *db = taosHashIterate(pUser->readDbs, NULL);
    while (db != NULL) {
      cols = 0;
X
Xiaoyu Wang 已提交
882
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
883
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
884
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
885
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
886 887 888 889

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
890
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
891 892 893 894 895 896 897

      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++);
898
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
899 900 901 902 903 904 905 906

      numOfRows++;
      db = taosHashIterate(pUser->readDbs, db);
    }

    db = taosHashIterate(pUser->writeDbs, NULL);
    while (db != NULL) {
      cols = 0;
907
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
908
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
909
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
910
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
911 912 913 914

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
915
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
916 917 918 919 920 921 922

      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++);
923
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
924 925 926 927 928 929 930 931

      numOfRows++;
      db = taosHashIterate(pUser->writeDbs, db);
    }

    char *topic = taosHashIterate(pUser->topics, NULL);
    while (topic != NULL) {
      cols = 0;
932
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
933
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
934
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
935
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
936 937 938 939

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
940
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
941 942 943 944 945

      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++);
946
      colDataSetVal(pColInfo, numOfRows, (const char *)topicName, false);
S
Shengliang Guan 已提交
947 948

      numOfRows++;
949
      topic = taosHashIterate(pUser->topics, topic);
S
Shengliang Guan 已提交
950
    }
951

S
Shengliang Guan 已提交
952 953 954 955 956 957 958 959 960 961 962
    sdbRelease(pSdb, pUser);
  }

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

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

964 965
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
  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 已提交
981
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
982 983 984 985
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
986

D
dapan 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    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;
1002

D
dapan 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    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;
1026

D
dapan 已提交
1027 1028 1029
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}
1030

K
kailixu 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
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) {
      mDebug("user:%s, not update since mnd passVer %d <= client passVer %d", pUsers[i].user, pUser->passVersion,
             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;
      assert(0);
      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) {
    assert(0);
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }
  void   *pRsp = taosMemoryMalloc(rspLen);
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    assert(0);
    goto _OVER;
  }
  tSerializeSUserPassBatchRsp(pRsp, rspLen, &batchRsp);

  *ppRsp = pRsp;
  *pRspLen = rspLen;

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

  tFreeSUserPassBatchRsp(&batchRsp);
  return code;
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
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);
1144 1145 1146
    if (pIter == NULL) {
      break;
    }
1147 1148

    code = -1;
1149 1150 1151
    if (mndUserDupObj(pUser, &newUser) != 0) {
      break;
    }
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

    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;
}