mndUser.c 24.7 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
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 71 72
  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) {
73
    userObj.superUser = 1;
S
Shengliang Guan 已提交
74 75 76 77 78 79
  }

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

S
Shengliang Guan 已提交
80
  mDebug("user:%s, will be created while deploy sdb, raw:%p", userObj.user, pRaw);
S
Shengliang Guan 已提交
81 82 83 84 85 86 87 88 89 90 91
  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;
}

92
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
93 94
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

  int32_t dataPos = 0;
103 104 105 106 107 108
  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)
109
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
110 111
  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
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
166 167 168

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

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

190
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
191
  taosInitRWLatch(&pUser->lock);
192 193 194

  terrno = 0;

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

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

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

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

S
Shengliang Guan 已提交
220 221
  return 0;
}
S
Shengliang Guan 已提交
222

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

S
Shengliang Guan 已提交
232
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
233
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
234
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
235
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
236
  pOld->authVersion = pNew->authVersion;
S
Shengliang Guan 已提交
237
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
238 239
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
S
Shengliang Guan 已提交
240
  taosWUnLockLatch(&pOld->lock);
241

S
Shengliang Guan 已提交
242 243 244
  return 0;
}

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

S
Shengliang Guan 已提交
254 255 256
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
257 258
}

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

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

275 276 277
  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 已提交
278
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
279
    return -1;
S
Shengliang Guan 已提交
280
  }
281
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
282

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

355 356 357
  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 已提交
358 359 360
    mndTransDrop(pTrans);
    return -1;
  }
361
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
362 363 364 365 366 367 368 369 370 371 372

  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 已提交
373
static SHashObj *mndDupDbHash(SHashObj *pOld) {
S
Shengliang Guan 已提交
374 375
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
376 377 378 379 380 381 382 383 384 385 386
  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 已提交
387
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
388 389 390 391 392 393 394 395
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

S
Shengliang Guan 已提交
396 397
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
398 399
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
400 401 402
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
403
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
404 405
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
406
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
407
    terrno = TSDB_CODE_INVALID_MSG;
408
    goto _OVER;
S
Shengliang Guan 已提交
409
  }
S
Shengliang Guan 已提交
410

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

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

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

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

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

S
Shengliang Guan 已提交
435 436 437 438
  if (mndCheckAlterUserAuth(pOperUser, pUser, &alterReq) != 0) {
    goto _OVER;
  }

S
Shengliang Guan 已提交
439
  memcpy(&newUser, pUser, sizeof(SUserObj));
S
Shengliang Guan 已提交
440 441
  newUser.authVersion++;
  newUser.updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
442 443

  taosRLockLatch(&pUser->lock);
S
Shengliang Guan 已提交
444 445
  newUser.readDbs = mndDupDbHash(pUser->readDbs);
  newUser.writeDbs = mndDupDbHash(pUser->writeDbs);
S
Shengliang Guan 已提交
446 447
  taosRUnLockLatch(&pUser->lock);

S
Shengliang Guan 已提交
448
  if (newUser.readDbs == NULL || newUser.writeDbs == NULL) {
449
    goto _OVER;
S
Shengliang Guan 已提交
450 451 452 453 454
  }

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

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
459
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
460 461 462
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
463
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
      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 已提交
483
    }
S
Shengliang Guan 已提交
484 485 486
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
487
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
      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 已提交
507 508 509
    }
  }

S
Shengliang Guan 已提交
510
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
S
Shengliang 已提交
511
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
512 513 514 515 516 517 518 519 520 521 522
      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 已提交
523

S
Shengliang Guan 已提交
524
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
S
Shengliang 已提交
525
    if (strcmp(alterReq.dbname, "1.*") != 0) {
S
Shengliang Guan 已提交
526 527 528 529 530 531 532 533 534 535
      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 已提交
536 537
  }

S
Shengliang Guan 已提交
538
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
539
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
540

541
_OVER:
S
Shengliang Guan 已提交
542
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
543
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
544 545
  }

S
Shengliang Guan 已提交
546 547
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
548 549
  taosHashCleanup(newUser.writeDbs);
  taosHashCleanup(newUser.readDbs);
S
Shengliang Guan 已提交
550 551

  return code;
S
Shengliang Guan 已提交
552 553
}

S
Shengliang Guan 已提交
554 555
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_USER, pReq);
S
Shengliang Guan 已提交
556 557 558 559 560 561
  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);

562 563 564
  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 已提交
565 566 567
    mndTransDrop(pTrans);
    return -1;
  }
568
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
569 570 571 572 573 574 575 576 577 578 579

  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 已提交
580 581
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
582 583 584 585 586
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SUserObj    *pOperUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
587
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
588
    terrno = TSDB_CODE_INVALID_MSG;
589
    goto _OVER;
S
Shengliang Guan 已提交
590
  }
S
Shengliang Guan 已提交
591

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

S
Shengliang Guan 已提交
594
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
595
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
596
    goto _OVER;
S
Shengliang Guan 已提交
597 598
  }

S
Shengliang Guan 已提交
599
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
600 601
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
602
    goto _OVER;
S
Shengliang Guan 已提交
603 604
  }

S
Shengliang Guan 已提交
605
  pOperUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
606 607
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
608
    goto _OVER;
S
Shengliang Guan 已提交
609 610
  }

S
Shengliang Guan 已提交
611
  if (mndCheckDropUserAuth(pOperUser) != 0) {
612
    goto _OVER;
S
Shengliang Guan 已提交
613 614
  }

S
Shengliang Guan 已提交
615
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
616
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
617

618
_OVER:
S
Shengliang Guan 已提交
619
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
620
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
621 622
  }

S
Shengliang Guan 已提交
623 624 625 626
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
627 628
}

629
static int32_t mndSetUserAuthRsp(SMnode *pMnode, SUserObj *pUser, SGetUserAuthRsp *pRsp) {
D
dapan 已提交
630 631 632
  memcpy(pRsp->user, pUser->user, TSDB_USER_LEN);
  pRsp->superAuth = pUser->superUser;
  pRsp->version = pUser->authVersion;
633
  taosRLockLatch(&pUser->lock);
D
dapan 已提交
634 635
  pRsp->readDbs = mndDupDbHash(pUser->readDbs);
  pRsp->writeDbs = mndDupDbHash(pUser->writeDbs);
636
  taosRUnLockLatch(&pUser->lock);
D
dapan 已提交
637 638 639 640 641
  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;
  }
642

D
dapan 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  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 已提交
661 662
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
663 664 665 666 667
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
668
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
669
    terrno = TSDB_CODE_INVALID_MSG;
670
    goto _OVER;
S
Shengliang Guan 已提交
671
  }
S
Shengliang Guan 已提交
672 673 674 675 676 677

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

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

D
dapan 已提交
681 682 683
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
684 685
  }

S
Shengliang Guan 已提交
686
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
687 688 689
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
690
    goto _OVER;
S
Shengliang Guan 已提交
691 692
  }

S
Shengliang Guan 已提交
693
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
694

S
Shengliang Guan 已提交
695 696
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
697 698
  code = 0;

699
_OVER:
700

S
Shengliang Guan 已提交
701
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
702
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
703 704 705 706

  return code;
}

S
Shengliang Guan 已提交
707 708
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
709 710 711 712 713 714 715 716 717 718 719
  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;
720
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
721 722

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

725
    colDataAppend(pColInfo, numOfRows, (const char *)name, false);
726

wafwerar's avatar
wafwerar 已提交
727 728
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
729

730 731
    const char *src = pUser->superUser ? "super" : "normal";
    char        b[10 + VARSTR_HEADER_SIZE] = {0};
732
    STR_WITH_SIZE_TO_VARSTR(b, src, strlen(src));
733
    colDataAppend(pColInfo, numOfRows, (const char *)b, false);
734

wafwerar's avatar
wafwerar 已提交
735 736
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
737
    colDataAppend(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
738 739 740 741 742

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

743
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
744 745 746 747 748 749
  return numOfRows;
}

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

752 753
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  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 已提交
769
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
770 771 772 773
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
774

D
dapan 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    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;
790

D
dapan 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    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;
814

D
dapan 已提交
815 816 817
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}