mndUser.c 21.3 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 "mndAuth.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 31
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
static SSdbRaw *mndUserActionEncode(SUserObj *pUser);
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
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SNodeMsg *pReq);
static int32_t  mndProcessCreateUserReq(SNodeMsg *pReq);
static int32_t  mndProcessAlterUserReq(SNodeMsg *pReq);
static int32_t  mndProcessDropUserReq(SNodeMsg *pReq);
static int32_t  mndProcessGetUserAuthReq(SNodeMsg *pReq);
38
static int32_t  mndRetrieveUsers(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
39
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
40 41

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

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

S
Shengliang Guan 已提交
58 59
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
S
Shengliang Guan 已提交
60 61 62 63 64 65 66
  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 已提交
67
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
S
Shengliang Guan 已提交
68 69 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;

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

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

S
Shengliang Guan 已提交
81
  mDebug("user:%s, will be created while deploy sdb, raw:%p", userObj.user, pRaw);
S
Shengliang Guan 已提交
82 83 84 85 86 87 88 89 90 91 92
  return sdbWrite(pMnode->pSdb, pRaw);
}

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

  return 0;
}

S
Shengliang Guan 已提交
93
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
94 95
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
96 97
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
98
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN;
S
Shengliang Guan 已提交
99

100
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
101
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
102 103

  int32_t dataPos = 0;
104 105 106 107 108 109 110 111
  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)
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
112 113 114

  char *db = taosHashIterate(pUser->readDbs, NULL);
  while (db != NULL) {
115
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
116 117 118 119 120
    db = taosHashIterate(pUser->readDbs, db);
  }

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
121
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
122 123 124
    db = taosHashIterate(pUser->writeDbs, db);
  }

125 126
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
127 128 129

  terrno = 0;

130
_OVER:
131 132 133 134 135
  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 已提交
136

S
Shengliang Guan 已提交
137
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
138
  return pRaw;
S
Shengliang Guan 已提交
139 140
}

S
Shengliang Guan 已提交
141
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
142 143
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
144
  int8_t sver = 0;
145
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
146

147
  if (sver != USER_VER_NUMBER) {
S
Shengliang Guan 已提交
148
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
149
    goto _OVER;
S
Shengliang Guan 已提交
150
  }
S
Shengliang Guan 已提交
151

152
  SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
153
  if (pRow == NULL) goto _OVER;
154

S
Shengliang Guan 已提交
155
  SUserObj *pUser = sdbGetRowObj(pRow);
156
  if (pUser == NULL) goto _OVER;
157

S
Shengliang Guan 已提交
158
  int32_t dataPos = 0;
159 160 161 162 163 164
  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)
165 166 167

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
168 169
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
S
Shengliang Guan 已提交
170 171 172
  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);
173
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL) goto _OVER;
174 175 176

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
177
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
178 179 180 181 182 183
    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};
184
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
185 186 187 188
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

189
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
190 191 192

  terrno = 0;

193
_OVER:
194 195
  if (terrno != 0) {
    mError("user:%s, failed to decode from raw:%p since %s", pUser->user, pRaw, terrstr());
196 197
    taosHashCleanup(pUser->readDbs);
    taosHashCleanup(pUser->writeDbs);
wafwerar's avatar
wafwerar 已提交
198
    taosMemoryFreeClear(pRow);
199 200
    return NULL;
  }
S
Shengliang Guan 已提交
201

S
Shengliang Guan 已提交
202
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
203
  return pRow;
S
Shengliang Guan 已提交
204
}
S
Shengliang Guan 已提交
205

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

S
Shengliang Guan 已提交
209 210
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
211
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
212
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
213
    return -1;
S
Shengliang Guan 已提交
214
  }
S
Shengliang Guan 已提交
215 216
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218 219
  return 0;
}
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
222
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
223 224
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
225 226
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
S
Shengliang Guan 已提交
227 228 229
  return 0;
}

S
Shengliang Guan 已提交
230
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
231
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
232 233
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
  pOld->updateTime = pNew->updateTime;
234

wafwerar's avatar
wafwerar 已提交
235 236
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
237

S
Shengliang Guan 已提交
238 239 240
  return 0;
}

S
Shengliang Guan 已提交
241
SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) {
S
Shengliang Guan 已提交
242 243 244
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
S
Shengliang Guan 已提交
245
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
S
Shengliang Guan 已提交
246 247
  }
  return pUser;
S
Shengliang Guan 已提交
248
}
S
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250 251 252
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
253 254
}

S
Shengliang Guan 已提交
255
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SNodeMsg *pReq) {
S
Shengliang Guan 已提交
256
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
257 258
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
259 260 261
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
S
Shengliang Guan 已提交
262
  userObj.superUser = pCreate->superUser;
S
Shengliang Guan 已提交
263

S
Shengliang Guan 已提交
264
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_USER, &pReq->rpcMsg);
S
Shengliang Guan 已提交
265
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
266
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
267 268
    return -1;
  }
S
Shengliang Guan 已提交
269
  mDebug("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271
  SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
272
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
S
Shengliang Guan 已提交
273
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
274
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
275
    return -1;
S
Shengliang Guan 已提交
276
  }
S
Shengliang Guan 已提交
277 278
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY);

279 280 281
  char *param = strdup("====> test code to be deleted later <=====");
  mndTransSetCb(pTrans, TEST_TRANS_START_FUNC, TEST_TRANS_STOP_FUNC, param, strlen(param) + 1);

S
Shengliang Guan 已提交
282
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
283
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
284
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
285
    return -1;
S
Shengliang Guan 已提交
286 287
  }

S
Shengliang Guan 已提交
288
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
289
  return 0;
S
Shengliang Guan 已提交
290 291
}

S
Shengliang Guan 已提交
292 293
static int32_t mndProcessCreateUserReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
294 295 296 297 298
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
299 300
  if (tDeserializeSCreateUserReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
301
    goto _OVER;
S
Shengliang Guan 已提交
302
  }
S
Shengliang Guan 已提交
303

S
Shengliang Guan 已提交
304
  mDebug("user:%s, start to create", createReq.user);
S
Shengliang Guan 已提交
305

S
Shengliang Guan 已提交
306
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
307
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
308
    goto _OVER;
S
Shengliang Guan 已提交
309 310
  }

S
Shengliang Guan 已提交
311
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
312
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
313
    goto _OVER;
S
Shengliang Guan 已提交
314 315
  }

S
Shengliang Guan 已提交
316
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
317
  if (pUser != NULL) {
S
Shengliang Guan 已提交
318
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
319
    goto _OVER;
S
Shengliang Guan 已提交
320 321
  }

S
Shengliang Guan 已提交
322
  pOperUser = mndAcquireUser(pMnode, pReq->user);
S
Shengliang Guan 已提交
323
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
324
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
325
    goto _OVER;
S
Shengliang Guan 已提交
326 327
  }

S
Shengliang Guan 已提交
328
  if (mndCheckCreateUserAuth(pOperUser) != 0) {
329
    goto _OVER;
S
Shengliang Guan 已提交
330 331 332
  }

  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
S
Shengliang Guan 已提交
333
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
334

335
_OVER:
S
Shengliang Guan 已提交
336 337
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
338 339
  }

S
Shengliang Guan 已提交
340 341 342 343
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
344 345
}

S
Shengliang Guan 已提交
346
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SNodeMsg *pReq) {
S
Shengliang Guan 已提交
347
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_ALTER_USER, &pReq->rpcMsg);
S
Shengliang Guan 已提交
348
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
349
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
350 351
    return -1;
  }
S
Shengliang Guan 已提交
352
  mDebug("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
353

S
Shengliang Guan 已提交
354
  SSdbRaw *pRedoRaw = mndUserActionEncode(pNew);
S
Shengliang Guan 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRedoRaw, 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 已提交
372
static SHashObj *mndDupDbHash(SHashObj *pOld) {
S
Shengliang Guan 已提交
373 374
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
375 376 377 378 379 380 381 382 383 384 385
  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 已提交
386
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
387 388 389 390 391 392 393 394
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

S
Shengliang Guan 已提交
395 396
static int32_t mndProcessAlterUserReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
397 398 399
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
400
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
401 402
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
403 404
  if (tDeserializeSAlterUserReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
405
    goto _OVER;
S
Shengliang Guan 已提交
406
  }
S
Shengliang Guan 已提交
407

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

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

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

S
Shengliang Guan 已提交
420
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
421 422
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
423
    goto _OVER;
S
Shengliang Guan 已提交
424 425
  }

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

  memcpy(&newUser, pUser, sizeof(SUserObj));
S
Shengliang Guan 已提交
433 434 435
  newUser.readDbs = mndDupDbHash(pUser->readDbs);
  newUser.writeDbs = mndDupDbHash(pUser->writeDbs);
  if (newUser.readDbs == NULL || newUser.writeDbs == NULL) {
436
    goto _OVER;
S
Shengliang Guan 已提交
437 438 439 440 441 442 443 444 445
  }

  int32_t len = strlen(alterReq.dbname) + 1;
  SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname);
  mndReleaseDb(pMnode, pDb);

  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
446
    memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
447
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
448
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
449
  } else if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB) {
S
Shengliang Guan 已提交
450 451
    if (pDb == NULL) {
      terrno = TSDB_CODE_MND_DB_NOT_EXIST;
452
      goto _OVER;
S
Shengliang Guan 已提交
453 454 455
    }
    if (taosHashPut(newUser.readDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
456
      goto _OVER;
S
Shengliang Guan 已提交
457
    }
S
Shengliang Guan 已提交
458
  } else if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB) {
S
Shengliang Guan 已提交
459 460
    if (taosHashRemove(newUser.readDbs, alterReq.dbname, len) != 0) {
      terrno = TSDB_CODE_MND_DB_NOT_EXIST;
461
      goto _OVER;
S
Shengliang Guan 已提交
462
    }
S
Shengliang Guan 已提交
463
  } else if (alterReq.alterType == TSDB_ALTER_USER_CLEAR_READ_DB) {
S
Shengliang Guan 已提交
464 465 466 467
    taosHashClear(newUser.readDbs);
  } else if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB) {
    if (pDb == NULL) {
      terrno = TSDB_CODE_MND_DB_NOT_EXIST;
468
      goto _OVER;
S
Shengliang Guan 已提交
469 470 471
    }
    if (taosHashPut(newUser.writeDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
472
      goto _OVER;
S
Shengliang Guan 已提交
473 474 475 476
    }
  } else if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB) {
    if (taosHashRemove(newUser.writeDbs, alterReq.dbname, len) != 0) {
      terrno = TSDB_CODE_MND_DB_NOT_EXIST;
477
      goto _OVER;
S
Shengliang Guan 已提交
478
    }
S
Shengliang Guan 已提交
479
  } else if (alterReq.alterType == TSDB_ALTER_USER_CLEAR_WRITE_DB) {
S
Shengliang Guan 已提交
480 481 482
    taosHashClear(newUser.writeDbs);
  } else {
    terrno = TSDB_CODE_MND_INVALID_ALTER_OPER;
483
    goto _OVER;
S
Shengliang Guan 已提交
484 485
  }

S
Shengliang Guan 已提交
486
  newUser.updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
487

S
Shengliang Guan 已提交
488
  if (mndCheckAlterUserAuth(pOperUser, pUser, pDb, &alterReq) != 0) {
489
    goto _OVER;
S
Shengliang Guan 已提交
490 491
  }

S
Shengliang Guan 已提交
492
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
493
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
494

495
_OVER:
S
Shengliang Guan 已提交
496 497
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
498 499
  }

S
Shengliang Guan 已提交
500 501
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
502 503
  taosHashCleanup(newUser.writeDbs);
  taosHashCleanup(newUser.readDbs);
S
Shengliang Guan 已提交
504 505

  return code;
S
Shengliang Guan 已提交
506 507
}

S
Shengliang Guan 已提交
508
static int32_t mndDropUser(SMnode *pMnode, SNodeMsg *pReq, SUserObj *pUser) {
S
Shengliang Guan 已提交
509
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_USER, &pReq->rpcMsg);
S
Shengliang Guan 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  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);

  SSdbRaw *pRedoRaw = mndUserActionEncode(pUser);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPED);

  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 已提交
534 535
static int32_t mndProcessDropUserReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
536 537 538 539 540
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SUserObj    *pOperUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
541 542
  if (tDeserializeSDropUserReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
543
    goto _OVER;
S
Shengliang Guan 已提交
544
  }
S
Shengliang Guan 已提交
545

S
Shengliang Guan 已提交
546
  mDebug("user:%s, start to drop", dropReq.user);
S
Shengliang Guan 已提交
547

S
Shengliang Guan 已提交
548
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
549
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
550
    goto _OVER;
S
Shengliang Guan 已提交
551 552
  }

S
Shengliang Guan 已提交
553
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
554 555
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
556
    goto _OVER;
S
Shengliang Guan 已提交
557 558
  }

S
Shengliang Guan 已提交
559
  pOperUser = mndAcquireUser(pMnode, pReq->user);
S
Shengliang Guan 已提交
560 561
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
562
    goto _OVER;
S
Shengliang Guan 已提交
563 564
  }

S
Shengliang Guan 已提交
565
  if (mndCheckDropUserAuth(pOperUser) != 0) {
566
    goto _OVER;
S
Shengliang Guan 已提交
567 568
  }

S
Shengliang Guan 已提交
569 570
  code = mndDropUser(pMnode, pReq, pUser);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
571

572
_OVER:
S
Shengliang Guan 已提交
573 574
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
575 576
  }

S
Shengliang Guan 已提交
577 578 579 580
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
581 582
}

S
Shengliang Guan 已提交
583 584
static int32_t mndProcessGetUserAuthReq(SNodeMsg *pReq) {
  SMnode         *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
585 586 587 588 589
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
590 591
  if (tDeserializeSGetUserAuthReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &authReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
592
    goto _OVER;
S
Shengliang Guan 已提交
593
  }
S
Shengliang Guan 已提交
594 595 596 597 598 599

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

  pUser = mndAcquireUser(pMnode, authReq.user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
600
    goto _OVER;
S
Shengliang Guan 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  }

  memcpy(authRsp.user, pUser->user, TSDB_USER_LEN);
  authRsp.superAuth = pUser->superUser;
  authRsp.readDbs = mndDupDbHash(pUser->readDbs);
  authRsp.writeDbs = mndDupDbHash(pUser->writeDbs);

  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(authRsp.readDbs, pDb->name, len, pDb->name, len);
      taosHashPut(authRsp.writeDbs, pDb->name, len, pDb->name, len);
    }

    sdbRelease(pSdb, pDb);
  }

S
Shengliang Guan 已提交
624
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
625 626 627
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
628
    goto _OVER;
S
Shengliang Guan 已提交
629 630
  }

S
Shengliang Guan 已提交
631
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
632

S
Shengliang Guan 已提交
633 634
  pReq->pRsp = pRsp;
  pReq->rspLen = contLen;
S
Shengliang Guan 已提交
635 636
  code = 0;

637
_OVER:
S
Shengliang Guan 已提交
638
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
639
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
640 641 642 643

  return code;
}

644
static int32_t mndRetrieveUsers(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
645
  SMnode   *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
646 647 648 649 650 651 652 653 654 655 656
  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;
657
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
658 659 660 661

    char name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->bytes[cols]);

662
    colDataAppend(pColInfo, numOfRows, (const char *)name, false);
663

wafwerar's avatar
wafwerar 已提交
664 665
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
666

667 668
    const char *src = pUser->superUser ? "super" : "normal";
    char        b[10 + VARSTR_HEADER_SIZE] = {0};
669
    STR_WITH_SIZE_TO_VARSTR(b, src, strlen(src));
670
    colDataAppend(pColInfo, numOfRows, (const char *)b, false);
671

wafwerar's avatar
wafwerar 已提交
672 673
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
674
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
675 676 677 678 679

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

680
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
681 682 683 684 685 686
  return numOfRows;
}

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