• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

132505311.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.05
/source/dnode/mnode/impl/src/mndUser.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
// clang-format off
18
#ifndef TD_ASTRA
19
#include <uv.h>
20
#endif
21
#include "crypt.h"
22
#include "mndCluster.h"
23
#include "mndSecurityPolicy.h"
24
#include "mndRole.h"
25
#include "mndUser.h"
26
#include "audit.h"
27
#include "mndDb.h"
28
#include "mndMnode.h"
29
#include "mndPrivilege.h"
30
#include "mndShow.h"
31
#include "mndStb.h"
32
#include "mndSync.h" 
33
#include "mndTopic.h"
34
#include "mndTrans.h"
35
#include "mndToken.h"
36
#include "tbase64.h"
37
#include "totp.h"
38
#include "mndDnode.h"
39
#include "mndVgroup.h"
40

41
// clang-format on
42

43
#define USER_VER_SUPPORT_WHITELIST           5
44
#define USER_VER_SUPPORT_WHITELIT_DUAL_STACK 7
45
#define USER_VER_SUPPORT_ADVANCED_SECURITY   8
46
#define USER_VER_NUMBER                      USER_VER_SUPPORT_ADVANCED_SECURITY
47

48
#define USER_RESERVE_SIZE 63
49

50
#define BIT_FLAG_MASK(n)              (1 << n)
51
#define BIT_FLAG_SET_MASK(val, mask)  ((val) |= (mask))
52
#define BIT_FLAG_TEST_MASK(val, mask) (((val) & (mask)) != 0)
53

54
#if 0
55
#define PRIVILEGE_TYPE_ALL       BIT_FLAG_MASK(0)
56
#define PRIVILEGE_TYPE_READ      BIT_FLAG_MASK(1)
57
#define PRIVILEGE_TYPE_WRITE     BIT_FLAG_MASK(2)
58
#define PRIVILEGE_TYPE_SUBSCRIBE BIT_FLAG_MASK(3)
59
#define PRIVILEGE_TYPE_ALTER     BIT_FLAG_MASK(4)
60
#endif
61

62
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
63
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
64

65
#if 0
66
#define ALTER_USER_ALL_PRIV(_priv)       (PRIV_HAS((_priv), PRIV_CM_ALL))
67
#define ALTER_USER_READ_PRIV(_priv)      (PRIV_HAS((_priv), PRIV_CM_READ) || PRIV_HAS((_priv), PRIV_CM_ALL))
68
#define ALTER_USER_WRITE_PRIV(_priv)     (PRIV_HAS((_priv), PRIV_CM_WRITE) || PRIV_HAS((_priv), PRIV_CM_ALL))
69
#define ALTER_USER_ALTER_PRIV(_priv)     (PRIV_HAS((_priv), PRIV_CM_ALTER) || PRIV_HAS((_priv), PRIV_CM_ALL))
70
#define ALTER_USER_SUBSCRIBE_PRIV(_priv) (PRIV_HAS((_priv), PRIV_TOPIC_SUBSCRIBE))
71

72
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
73
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
74

75
#define ALTER_USER_ADD_READ_DB_PRIV(_type, _priv, _tbname) \
76
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
77
#define ALTER_USER_DEL_READ_DB_PRIV(_type, _priv, _tbname) \
78
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
79
#define ALTER_USER_ADD_WRITE_DB_PRIV(_type, _priv, _tbname) \
80
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
81
#define ALTER_USER_DEL_WRITE_DB_PRIV(_type, _priv, _tbname) \
82
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
83
#define ALTER_USER_ADD_ALTER_DB_PRIV(_type, _priv, _tbname) \
84
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
85
#define ALTER_USER_DEL_ALTER_DB_PRIV(_type, _priv, _tbname) \
86
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
87
#define ALTER_USER_ADD_ALL_DB_PRIV(_type, _priv, _tbname) \
88
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
89
#define ALTER_USER_DEL_ALL_DB_PRIV(_type, _priv, _tbname) \
90
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
91

92
#define ALTER_USER_ADD_READ_TB_PRIV(_type, _priv, _tbname) \
93
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
94
#define ALTER_USER_DEL_READ_TB_PRIV(_type, _priv, _tbname) \
95
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
96
#define ALTER_USER_ADD_WRITE_TB_PRIV(_type, _priv, _tbname) \
97
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
98
#define ALTER_USER_DEL_WRITE_TB_PRIV(_type, _priv, _tbname) \
99
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
100
#define ALTER_USER_ADD_ALTER_TB_PRIV(_type, _priv, _tbname) \
101
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
102
#define ALTER_USER_DEL_ALTER_TB_PRIV(_type, _priv, _tbname) \
103
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
104
#define ALTER_USER_ADD_ALL_TB_PRIV(_type, _priv, _tbname) \
105
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
106
#define ALTER_USER_DEL_ALL_TB_PRIV(_type, _priv, _tbname) \
107
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
108

109
#define ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
110
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
111
#define ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
112
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
113
#endif
114

115
#ifdef TD_ENTERPRISE
116
extern int32_t mndAlterUserPrivInfo(SMnode *pMnode, SUserObj *pNew, SAlterRoleReq *pAlterReq);
117
extern int32_t mndAlterUserRoleInfo(SMnode *pMnode, SUserObj *pOperUser, const char *token, SUserObj *pOld,
118
                                    SUserObj *pNew, SAlterRoleReq *pAlterReq);
119
#endif
120

121
static void generateSalt(char *salt, size_t len);
122

123
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList);
124
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppWhiteList, bool supportNeg);
125

126
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b);
127
static bool isIpRangeEqual(SIpRange *a, SIpRange *b);
128

129
#define MND_MAX_USER_IP_RANGE   (TSDB_PRIVILEDGE_HOST_LEN / 24)
130
#define MND_MAX_USER_TIME_RANGE 2048
131

132
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
133
static int32_t  mndUpgradeUsers(SMnode *pMnode, int32_t version);
134
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
135
static int32_t  mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
136
static int32_t  mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
137
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
138
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
139
static int32_t  mndAlterUser(SMnode *pMnode, SUserObj *pNew, SRpcMsg *pReq);
140
static int32_t  mndProcessCreateUserReq(SRpcMsg *pReq);
141
static int32_t  mndProcessAlterUserReq(SRpcMsg *pReq);
142
static int32_t  mndProcessDropUserReq(SRpcMsg *pReq);
143
static int32_t  mndProcessGetUserAuthReq(SRpcMsg *pReq);
144
static int32_t  mndProcessUpgradeUserReq(SRpcMsg *pReq);
145
static int32_t  mndProcessUpgradeUserRsp(SRpcMsg *pReq);
146
static int32_t  mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
147
static int32_t  mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
148
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
149
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
150
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
151
static int32_t  mndPrincipalRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, char *objFName, int32_t objType);
152

153
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq);
154
static int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq);
155
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq);
156
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq);
157
static int32_t mndProcessCreateTotpSecretReq(SRpcMsg *pReq);
158
static int32_t mndProcessDropTotpSecretReq(SRpcMsg *pReq);
159

160
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList);
161
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList);
162

163
typedef struct {
164
  SIpWhiteListDual   *wlIp;
165
  SDateTimeWhiteList *wlTime;
166
  SLoginInfo          loginInfo;
167
} SCachedUserInfo;
168

169
typedef struct {
170
  SHashObj      *users;  // key: user, value: SCachedUserInfo*
171
  int64_t        verIp;
172
  int64_t        verTime;
173
  char           auditLogUser[TSDB_USER_LEN];
174
  TdThreadRwlock rw;
175
} SUserCache;
176

177
static SUserCache userCache;
178
static int8_t     upgradeSecurity = 0;
179

180
static int32_t userCacheInit() {
530,898✔
181
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
530,898✔
182

183
  SHashObj *users = taosHashInit(8, hashFn, 1, HASH_ENTRY_LOCK);
530,898✔
184
  if (users == NULL) {
530,898✔
185
    TAOS_RETURN(terrno);
×
186
  }
187

188
  userCache.users = users;
530,898✔
189
  userCache.verIp = 0;
530,898✔
190
  userCache.verTime = 0;
530,898✔
191
  userCache.auditLogUser[0] = '\0';
530,898✔
192

193
  (void)taosThreadRwlockInit(&userCache.rw, NULL);
530,898✔
194
  TAOS_RETURN(0);
530,898✔
195
}
196

197
static void userCacheCleanup() {
530,833✔
198
  if (userCache.users == NULL) {
530,833✔
199
    return;
×
200
  }
201

202
  void *pIter = taosHashIterate(userCache.users, NULL);
530,833✔
203
  while (pIter) {
1,085,180✔
204
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
554,347✔
205
    if (pInfo != NULL) {
554,347✔
206
      taosMemoryFree(pInfo->wlIp);
554,347✔
207
      taosMemoryFree(pInfo->wlTime);
554,347✔
208
      taosMemoryFree(pInfo);
554,347✔
209
    }
210
    pIter = taosHashIterate(userCache.users, pIter);
554,347✔
211
  }
212
  taosHashCleanup(userCache.users);
530,833✔
213

214
  (void)taosThreadRwlockDestroy(&userCache.rw);
530,833✔
215
}
216

217
static void userCacheRemoveUser(const char *user) {
61,160✔
218
  size_t userLen = strlen(user);
61,160✔
219

220
  (void)taosThreadRwlockWrlock(&userCache.rw);
61,160✔
221

222
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
61,160✔
223
  if (ppInfo != NULL) {
61,160✔
224
    if (*ppInfo != NULL) {
61,160✔
225
      taosMemoryFree((*ppInfo)->wlIp);
61,160✔
226
      taosMemoryFree((*ppInfo)->wlTime);
61,160✔
227
      taosMemoryFree(*ppInfo);
61,160✔
228
    }
229
    if (taosHashRemove(userCache.users, user, userLen) != 0) {
61,160✔
230
      mDebug("failed to remove user %s from user cache", user);
×
231
    }
232
    userCache.verIp++;
61,160✔
233
    userCache.verTime++;
61,160✔
234
  }
235

236
  (void)taosThreadRwlockUnlock(&userCache.rw);
61,160✔
237
}
61,160✔
238

239
static void userCacheResetLoginInfo(const char *user) {
1,439✔
240
  size_t userLen = strlen(user);
1,439✔
241

242
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,439✔
243

244
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
1,439✔
245
  if (ppInfo != NULL && *ppInfo != NULL) {
1,439✔
246
    (*ppInfo)->loginInfo.lastLoginTime = taosGetTimestampSec();
1,439✔
247
    (*ppInfo)->loginInfo.failedLoginCount = 0;
1,439✔
248
    (*ppInfo)->loginInfo.lastFailedLoginTime = 0;
1,439✔
249
  }
250

251
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,439✔
252
}
1,439✔
253

254
static SCachedUserInfo *getCachedUserInfo(const char *user) {
5,340,898✔
255
  size_t            userLen = strlen(user);
5,340,898✔
256
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
5,340,898✔
257
  if (ppInfo != NULL) {
5,340,898✔
258
    return *ppInfo;
4,725,391✔
259
  }
260

261
  SCachedUserInfo *pInfo = (SCachedUserInfo *)taosMemoryCalloc(1, sizeof(SCachedUserInfo));
615,507✔
262
  if (pInfo == NULL) {
615,507✔
263
    return NULL;
×
264
  }
265

266
  if (taosHashPut(userCache.users, user, userLen, &pInfo, sizeof(pInfo)) != 0) {
615,507✔
267
    taosMemoryFree(pInfo);
×
268
    return NULL;
×
269
  }
270

271
  return pInfo;
615,507✔
272
}
273

274
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
3,148,824✔
275
  size_t userLen = strlen(user);
3,148,824✔
276

277
  (void)taosThreadRwlockRdlock(&userCache.rw);
3,148,824✔
278

279
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
3,148,821✔
280
  if (ppInfo != NULL && *ppInfo != NULL) {
3,148,925✔
281
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,778,046✔
282
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,777,898✔
283
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,777,958✔
284
  } else {
285
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
370,879✔
286
    pLoginInfo->failedLoginCount = 0;
370,879✔
287
    pLoginInfo->lastFailedLoginTime = 0;
370,879✔
288
  }
289

290
  (void)taosThreadRwlockUnlock(&userCache.rw);
3,148,868✔
291

292
  if (pLoginInfo->lastLoginTime == 0) {
3,148,881✔
293
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
76,426✔
294
  }
295
}
3,148,881✔
296

297
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
3,141,130✔
298
  size_t userLen = strlen(user);
3,141,130✔
299

300
  (void)taosThreadRwlockWrlock(&userCache.rw);
3,141,130✔
301

302
  SCachedUserInfo *pInfo = getCachedUserInfo(user);
3,141,227✔
303
  if (pInfo != NULL) {
3,141,227✔
304
    pInfo->loginInfo.lastLoginTime = pLoginInfo->lastLoginTime;
3,141,227✔
305
    pInfo->loginInfo.failedLoginCount = pLoginInfo->failedLoginCount;
3,141,227✔
306
    pInfo->loginInfo.lastFailedLoginTime = pLoginInfo->lastFailedLoginTime;
3,141,227✔
307
  }
308

309
  (void)taosThreadRwlockUnlock(&userCache.rw);
3,141,227✔
310
}
3,141,227✔
311

312
static bool isDateTimeWhiteListEqual(SDateTimeWhiteList *a, SDateTimeWhiteList *b) {
1,711,264✔
313
  if (a == NULL && b == NULL) {
1,711,264✔
314
    return true;
×
315
  }
316

317
  if (a == NULL || b == NULL) {
1,711,264✔
318
    return false;
110,764✔
319
  }
320

321
  if (a->num != b->num) {
1,600,500✔
322
    return false;
364✔
323
  }
324

325
  for (int i = 0; i < a->num; i++) {
1,600,682✔
326
    if (a->ranges[i].start != b->ranges[i].start || a->ranges[i].duration != b->ranges[i].duration ||
910✔
327
        a->ranges[i].neg != b->ranges[i].neg || a->ranges[i].absolute != b->ranges[i].absolute) {
546✔
328
      return false;
364✔
329
    }
330
  }
331

332
  return true;
1,599,772✔
333
}
334

335
static int32_t userCacheUpdateWhiteList(SMnode *pMnode, SUserObj *pUser) {
1,711,264✔
336
  int32_t code = 0, lino = 0;
1,711,264✔
337

338
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,711,264✔
339

340
  SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
1,711,264✔
341
  if (pInfo == NULL) {
1,711,264✔
342
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
343
  }
344

345
  if (!isIpWhiteListEqual(pInfo->wlIp, pUser->pIpWhiteListDual)) {
1,711,264✔
346
    SIpWhiteListDual *p = cloneIpWhiteList(pUser->pIpWhiteListDual);
111,659✔
347
    if (p == NULL) {
111,659✔
348
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
349
    }
350
    taosMemoryFree(pInfo->wlIp);
111,659✔
351
    pInfo->wlIp = p;
111,659✔
352
    userCache.verIp++;
111,659✔
353
  }
354

355
  if (!isDateTimeWhiteListEqual(pInfo->wlTime, pUser->pTimeWhiteList)) {
1,711,264✔
356
    SDateTimeWhiteList *p = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
111,492✔
357
    if (p == NULL) {
111,492✔
358
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
359
    }
360
    taosMemoryFree(pInfo->wlTime);
111,492✔
361
    pInfo->wlTime = p;
111,492✔
362
    userCache.verTime++;
111,492✔
363
  }
364

365
_OVER:
1,711,264✔
366
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,711,264✔
367
  if (code < 0) {
1,711,264✔
368
    mError("failed to update white list for user: %s at line %d since %s", pUser->user, lino, tstrerror(code));
×
369
  }
370
  TAOS_RETURN(code);
1,711,264✔
371
}
372

373
static int32_t userCacheRebuildIpWhiteList(SMnode *pMnode) {
643,099✔
374
  int32_t code = 0, lino = 0;
643,099✔
375

376
  SSdb *pSdb = pMnode->pSdb;
643,099✔
377
  void *pIter = NULL;
643,099✔
378
  while (1) {
249,670✔
379
    SUserObj *pUser = NULL;
892,769✔
380
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
892,769✔
381
    if (pIter == NULL) {
892,769✔
382
      break;
643,099✔
383
    }
384

385
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
249,670✔
386
    if (pInfo == NULL) {
249,670✔
387
      sdbRelease(pSdb, pUser);
×
388
      sdbCancelFetch(pSdb, pIter);
×
389
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
390
    }
391

392
    SIpWhiteListDual *wl = cloneIpWhiteList(pUser->pIpWhiteListDual);
249,670✔
393
    if (wl == NULL) {
249,670✔
394
      sdbRelease(pSdb, pUser);
×
395
      sdbCancelFetch(pSdb, pIter);
×
396
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
397
    }
398

399
    taosMemoryFree(pInfo->wlIp);
249,670✔
400
    pInfo->wlIp = wl;
249,670✔
401

402
    sdbRelease(pSdb, pUser);
249,670✔
403
  }
404

405
  userCache.verIp++;
643,099✔
406

407
_OVER:
643,099✔
408
  if (code < 0) {
643,099✔
409
    mError("failed to rebuild ip white list at line %d since %s", lino, tstrerror(code));
×
410
  }
411
  TAOS_RETURN(code);
643,099✔
412
}
413

414
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
56,928,965✔
415
  int64_t ver = 0;
56,928,965✔
416
  int32_t code = 0;
56,928,965✔
417

418
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
56,928,965✔
419
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,948✔
420

421
    if (userCache.verIp == 0) {
4,948✔
422
      // get user and dnode ip white list
423
      if ((code = userCacheRebuildIpWhiteList(pMnode)) != 0) {
×
424
        (void)taosThreadRwlockUnlock(&userCache.rw);
×
425
        mError("%s failed to update ip white list since %s", __func__, tstrerror(code));
×
426
        return ver;
×
427
      }
428
      userCache.verIp = taosGetTimestampMs();
×
429
    }
430
    ver = userCache.verIp;
4,948✔
431

432
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,948✔
433
  }
434

435
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
56,928,965✔
436
  return ver;
56,928,965✔
437
}
438

439
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
643,099✔
440
  int32_t code = 0;
643,099✔
441
  (void)taosThreadRwlockWrlock(&userCache.rw);
643,099✔
442

443
  if ((code = userCacheRebuildIpWhiteList(pMnode)) != 0) {
643,099✔
444
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
445
    TAOS_RETURN(code);
×
446
  }
447
  userCache.verIp = taosGetTimestampMs();
643,099✔
448
  (void)taosThreadRwlockUnlock(&userCache.rw);
643,099✔
449

450
  TAOS_RETURN(code);
643,099✔
451
}
452

453
static int32_t userCacheRebuildTimeWhiteList(SMnode *pMnode) {
632,508✔
454
  int32_t code = 0, lino = 0;
632,508✔
455

456
  SSdb *pSdb = pMnode->pSdb;
632,508✔
457
  void *pIter = NULL;
632,508✔
458
  while (1) {
238,737✔
459
    SUserObj *pUser = NULL;
871,245✔
460
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
871,245✔
461
    if (pIter == NULL) {
871,245✔
462
      break;
632,508✔
463
    }
464

465
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
238,737✔
466
    if (pInfo == NULL) {
238,737✔
467
      sdbRelease(pSdb, pUser);
×
468
      sdbCancelFetch(pSdb, pIter);
×
469
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
470
    }
471

472
    SDateTimeWhiteList *wl = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
238,737✔
473
    if (wl == NULL) {
238,737✔
474
      sdbRelease(pSdb, pUser);
×
475
      sdbCancelFetch(pSdb, pIter);
×
476
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
477
    }
478

479
    taosMemoryFree(pInfo->wlTime);
238,737✔
480
    pInfo->wlTime = wl;
238,737✔
481

482
    sdbRelease(pSdb, pUser);
238,737✔
483
  }
484

485
  userCache.verTime++;
632,508✔
486

487
_OVER:
632,508✔
488
  if (code < 0) {
632,508✔
489
    mError("failed to rebuild time white list at line %d since %s", lino, tstrerror(code));
×
490
  }
491
  TAOS_RETURN(code);
632,508✔
492
}
493

494
int32_t mndRefreshUserDateTimeWhiteList(SMnode *pMnode) {
632,508✔
495
  int32_t code = 0;
632,508✔
496
  (void)taosThreadRwlockWrlock(&userCache.rw);
632,508✔
497

498
  if ((code = userCacheRebuildTimeWhiteList(pMnode)) != 0) {
632,508✔
499
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
500
    TAOS_RETURN(code);
×
501
  }
502
  userCache.verTime = taosGetTimestampMs();
632,508✔
503
  (void)taosThreadRwlockUnlock(&userCache.rw);
632,508✔
504

505
  TAOS_RETURN(code);
632,508✔
506
}
507

508
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
56,928,965✔
509
  int64_t ver = 0;
56,928,965✔
510
  int32_t code = 0;
56,928,965✔
511

512
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
56,928,965✔
513
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,948✔
514

515
    if (userCache.verIp == 0) {
4,948✔
516
      // get user and dnode datetime white list
517
      if ((code = userCacheRebuildTimeWhiteList(pMnode)) != 0) {
×
518
        (void)taosThreadRwlockUnlock(&userCache.rw);
×
519
        mError("%s failed to update datetime white list since %s", __func__, tstrerror(code));
×
520
        return ver;
×
521
      }
522
      userCache.verTime = taosGetTimestampMs();
×
523
    }
524
    ver = userCache.verTime;
4,948✔
525

526
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,948✔
527
  }
528

529
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
56,928,965✔
530
  return ver;
56,928,965✔
531
}
532

533
int32_t mndInitUser(SMnode *pMnode) {
530,898✔
534
  TAOS_CHECK_RETURN(userCacheInit());
530,898✔
535

536
  SSdbTable table = {
530,898✔
537
      .sdbType = SDB_USER,
538
      .keyType = SDB_KEY_BINARY,
539
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
540
      .upgradeFp = (SdbUpgradeFp)mndUpgradeUsers,
541
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
542
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
543
      .insertFp = (SdbInsertFp)mndUserActionInsert,
544
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
545
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
546
  };
547

548
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
530,898✔
549
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
530,898✔
550
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
530,898✔
551
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
530,898✔
552
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_USER, mndProcessUpgradeUserReq);
530,898✔
553
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_USER_RSP, mndProcessUpgradeUserRsp);
530,898✔
554

555
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST, mndProcessGetUserIpWhiteListReq);
530,898✔
556
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST_DUAL, mndProcessGetUserIpWhiteListReq);
530,898✔
557
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST, mndProcessRetrieveIpWhiteListReq);
530,898✔
558
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL, mndProcessRetrieveIpWhiteListReq);
530,898✔
559
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_DATETIME_WHITELIST, mndProcessGetUserDateTimeWhiteListReq);
530,898✔
560
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_DATETIME_WHITELIST, mndProcessRetrieveDateTimeWhiteListReq);
530,898✔
561

562
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_TOTP_SECRET, mndProcessCreateTotpSecretReq);
530,898✔
563
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_TOTP_SECRET, mndProcessDropTotpSecretReq);
530,898✔
564

565
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
530,898✔
566
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
530,898✔
567
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndRetrieveUsersFull);
530,898✔
568
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndCancelGetNextUser);
530,898✔
569
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
530,898✔
570
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
530,898✔
571
  return sdbSetTable(pMnode->pSdb, table);
530,898✔
572
}
573

574
void mndCleanupUser(SMnode *pMnode) { userCacheCleanup(); }
530,833✔
575

576
static bool isDefaultRange(SIpRange *pRange) {
×
577
  int32_t code = 0;
×
578
  int32_t lino = 0;
×
579

580
  SIpRange range4 = {0};
×
581
  SIpRange range6 = {0};
×
582

583
  code = createDefaultIp4Range(&range4);
×
584
  TSDB_CHECK_CODE(code, lino, _error);
×
585

586
  code = createDefaultIp6Range(&range6);
×
587
  TSDB_CHECK_CODE(code, lino, _error);
×
588

589
  if (isIpRangeEqual(pRange, &range4) || (isIpRangeEqual(pRange, &range6))) {
×
590
    return true;
×
591
  }
592
_error:
×
593
  return false;
×
594
};
595

596
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
1,894,568✔
597
  int32_t len = 0;
1,894,568✔
598
  for (int i = 0; i < num; i++) {
5,729,551✔
599
    SIpRange *pRange = &range[i];
3,834,983✔
600
    SIpAddr   addr = {0};
3,834,983✔
601
    int32_t   code = tIpUintToStr(pRange, &addr);
3,834,983✔
602
    if (code != 0) {
3,834,983✔
603
      mError("%s failed to convert ip range to str, code: %d", __func__, code);
×
604
    }
605

606
    len += snprintf(buf + len, bufLen - len, "%c%s/%d, ", pRange->neg ? '-' : '+', IP_ADDR_STR(&addr), addr.mask);
3,834,983✔
607
  }
608
  if (len > 0) {
1,894,568✔
609
    len -= 2;
1,894,568✔
610
    buf[len] = 0; // remove last ", "
1,894,568✔
611
  }
612
  return len;
1,894,568✔
613
}
614

615
static bool isIpRangeEqual(SIpRange *a, SIpRange *b) {
3,200,143✔
616
  if (a->type != b->type || a->neg != b->neg) {
3,200,143✔
617
    return false;
×
618
  }
619

620
  if (a->type == 0) {
3,200,143✔
621
    SIpV4Range *a4 = &a->ipV4;
1,600,538✔
622
    SIpV4Range *b4 = &b->ipV4;
1,600,538✔
623
    return (a4->ip == b4->ip && a4->mask == b4->mask);
1,600,538✔
624
  }
625

626
  SIpV6Range *a6 = &a->ipV6;
1,599,605✔
627
  SIpV6Range *b6 = &b->ipV6;
1,599,605✔
628
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
1,599,605✔
629
}
630

631
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
1,711,264✔
632
  if (a == NULL && b == NULL) {
1,711,264✔
633
    return true;
×
634
  }
635

636
  if (a == NULL || b == NULL) {
1,711,264✔
637
    return false;
110,764✔
638
  }
639

640
  if (a->num != b->num) {
1,600,500✔
641
    return false;
895✔
642
  }
643
  for (int i = 0; i < a->num; i++) {
4,799,748✔
644
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
3,200,143✔
645
      return false;
×
646
    }
647
  }
648
  return true;
1,599,605✔
649
}
650

651
static int32_t compareIpRange(const void *a, const void *b, const void *arg) {
8,048✔
652
  SIpRange *ra = (SIpRange *)a;
8,048✔
653
  SIpRange *rb = (SIpRange *)b;
8,048✔
654

655
  if (ra->neg != rb->neg) {
8,048✔
656
    return (ra->neg) ? -1 : 1;
910✔
657
  }
658

659
  if (ra->type != rb->type) {
7,138✔
660
    return (ra->type == 0) ? -1 : 1;
2,612✔
661
  }
662

663
  if (ra->type == 0) {
4,526✔
664
    if (ra->ipV4.ip != rb->ipV4.ip) {
4,526✔
665
      return (ra->ipV4.ip < rb->ipV4.ip) ? -1 : 1;
4,116✔
666
    }
667
    return (ra->ipV4.mask < rb->ipV4.mask) ? -1 : 1;
410✔
668
  }
669

670
  if (ra->ipV6.addr[0] != rb->ipV6.addr[0]) {
×
671
    return (ra->ipV6.addr[0] < rb->ipV6.addr[0]) ? -1 : 1;
×
672
  }
673
  if (ra->ipV6.addr[1] != rb->ipV6.addr[1]) {
×
674
    return (ra->ipV6.addr[1] < rb->ipV6.addr[1]) ? -1 : 1;
×
675
  }
676
  return (ra->ipV6.mask < rb->ipV6.mask) ? -1 : 1;
×
677
}
678

679
static void sortIpWhiteList(SIpWhiteListDual *pList) {
3,158✔
680
  (void)taosqsort(pList->pIpRanges, pList->num, sizeof(SIpRange), NULL, compareIpRange);
3,158✔
681
}
3,158✔
682

683
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
1,894,568✔
684
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
1,894,568✔
685

686
  int64_t bufLen = pList->num * 128 + 8;
1,894,568✔
687
  *buf = taosMemoryCalloc(1, bufLen);
1,894,568✔
688
  if (*buf == NULL) {
1,894,568✔
689
    return 0;
×
690
  }
691

692
  if (pList->num == 0) {
1,894,568✔
693
    return snprintf(*buf, bufLen, "+ALL");
×
694
  }
695

696
  int32_t len = ipRangeListToStr(pList->pIpRanges, pList->num, *buf, bufLen - 2);
1,894,568✔
697
  if (len == 0) {
1,894,568✔
698
    taosMemoryFreeClear(*buf);
×
699
    return 0;
×
700
  }
701
  return len;
1,894,568✔
702
}
703

704
static int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, uint32_t *pLen) {
4,918,809✔
705
  int32_t  code = 0;
4,918,809✔
706
  int32_t  lino = 0;
4,918,809✔
707
  int32_t  tlen = 0;
4,918,809✔
708
  SEncoder encoder = {0};
4,918,809✔
709
  tEncoderInit(&encoder, buf, len);
4,918,809✔
710

711
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
4,918,809✔
712
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
9,837,618✔
713

714
  for (int i = 0; i < pList->num; i++) {
14,762,773✔
715
    SIpRange *pRange = &(pList->pIpRanges[i]);
9,843,964✔
716
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
9,843,964✔
717
  }
718

719
  tEndEncode(&encoder);
4,918,809✔
720

721
  tlen = encoder.pos;
4,918,809✔
722
_OVER:
4,918,809✔
723
  tEncoderClear(&encoder);
4,918,809✔
724
  if (code < 0) {
4,918,809✔
725
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
726
  }
727
  if (pLen) *pLen = tlen;
4,918,809✔
728
  TAOS_RETURN(code);
4,918,809✔
729
}
730

731
static int32_t tDerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, bool supportNeg) {
4,011,759✔
732
  int32_t  code = 0;
4,011,759✔
733
  int32_t  lino = 0;
4,011,759✔
734
  SDecoder decoder = {0};
4,011,759✔
735
  tDecoderInit(&decoder, buf, len);
4,011,759✔
736

737
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
4,011,759✔
738
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
8,023,518✔
739

740
  for (int i = 0; i < pList->num; i++) {
12,039,347✔
741
    SIpRange *pRange = &(pList->pIpRanges[i]);
8,027,588✔
742
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
8,027,588✔
743
  }
744

745
_OVER:
4,011,759✔
746
  tEndDecode(&decoder);
4,011,759✔
747
  tDecoderClear(&decoder);
4,011,759✔
748
  if (code < 0) {
4,011,759✔
749
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
750
  }
751
  TAOS_RETURN(code);
4,011,759✔
752
}
753

754
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList) {
×
755
  int32_t  code = 0;
×
756
  int32_t  lino = 0;
×
757
  SDecoder decoder = {0};
×
758
  tDecoderInit(&decoder, buf, len);
×
759

760
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
761
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
762

763
  for (int i = 0; i < pList->num; i++) {
×
764
    SIpV4Range *pIp4 = &(pList->pIpRange[i]);
×
765
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->ip), &lino, _OVER);
×
766
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->mask), &lino, _OVER);
×
767
  }
768

769
_OVER:
×
770
  tEndDecode(&decoder);
×
771
  tDecoderClear(&decoder);
×
772
  if (code < 0) {
×
773
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
774
  }
775
  TAOS_RETURN(code);
×
776
}
777

778
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppList, bool supportNeg) {
4,011,759✔
779
  int32_t           code = 0;
4,011,759✔
780
  int32_t           lino = 0;
4,011,759✔
781
  int32_t           num = 0;
4,011,759✔
782
  SIpWhiteListDual *p = NULL;
4,011,759✔
783
  SDecoder          decoder = {0};
4,011,759✔
784
  tDecoderInit(&decoder, buf, len);
4,011,759✔
785

786
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
4,011,759✔
787
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
4,011,759✔
788

789
  p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + num * sizeof(SIpRange));
4,011,759✔
790
  if (p == NULL) {
4,011,759✔
791
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
792
  }
793
  TAOS_CHECK_GOTO(tDerializeIpWhiteList(buf, len, p, supportNeg), &lino, _OVER);
4,011,759✔
794

795
_OVER:
4,011,759✔
796
  tEndDecode(&decoder);
4,011,759✔
797
  tDecoderClear(&decoder);
4,011,759✔
798
  if (code < 0) {
4,011,759✔
799
    taosMemoryFreeClear(p);
×
800
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
801
  }
802
  *ppList = p;
4,011,759✔
803
  TAOS_RETURN(code);
4,011,759✔
804
}
805

806
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList) {
×
807
  int32_t       code = 0;
×
808
  int32_t       lino = 0;
×
809
  int32_t       num = 0;
×
810
  SIpWhiteList *p = NULL;
×
811
  SDecoder      decoder = {0};
×
812
  tDecoderInit(&decoder, buf, len);
×
813

814
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
815
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
816

817
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
×
818
  if (p == NULL) {
×
819
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
820
  }
821
  TAOS_CHECK_GOTO(tDerializeIpWhileListFromOldVer(buf, len, p), &lino, _OVER);
×
822

823
_OVER:
×
824
  tEndDecode(&decoder);
×
825
  tDecoderClear(&decoder);
×
826
  if (code < 0) {
×
827
    taosMemoryFreeClear(p);
×
828
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
829
  }
830
  *ppList = p;
×
831
  TAOS_RETURN(code);
×
832
}
833

834
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList) {
489,628✔
835
  int32_t code = 0;
489,628✔
836
  int32_t lino = 0;
489,628✔
837
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
489,628✔
838
  if (*ppWhiteList == NULL) {
489,628✔
839
    TAOS_RETURN(terrno);
×
840
  }
841
  (*ppWhiteList)->num = 2;
489,628✔
842

843
  SIpRange v4 = {0};
489,628✔
844
  SIpRange v6 = {0};
489,628✔
845

846
#ifndef TD_ASTRA
847
  code = createDefaultIp4Range(&v4);
489,628✔
848
  TSDB_CHECK_CODE(code, lino, _error);
489,628✔
849

850
  code = createDefaultIp6Range(&v6);
489,628✔
851
  TSDB_CHECK_CODE(code, lino, _error);
489,628✔
852

853
#endif
854

855
_error:
489,628✔
856
  if (code != 0) {
489,628✔
857
    taosMemoryFree(*ppWhiteList);
×
858
    *ppWhiteList = NULL;
×
859
    mError("failed to create default ip white list at line %d since %s", __LINE__, tstrerror(code));
×
860
  } else {
861
    memcpy(&(*ppWhiteList)->pIpRanges[0], &v4, sizeof(SIpRange));
489,628✔
862
    memcpy(&(*ppWhiteList)->pIpRanges[1], &v6, sizeof(SIpRange));
489,628✔
863
  }
864
  return 0;
489,628✔
865
}
866

867
static const char *weekdays[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
868

869
static int32_t convertTimeRangesToStr(SUserObj *pUser, char **buf) {
1,894,568✔
870
  int32_t bufLen = pUser->pTimeWhiteList->num * 32 + 8;
1,894,568✔
871
  *buf = taosMemoryCalloc(1, bufLen);
1,894,568✔
872
  if (*buf == NULL) {
1,894,568✔
873
    return 0;
×
874
  }
875

876
  int32_t pos = 0;
1,894,568✔
877
  if (pUser->pTimeWhiteList->num == 0) {
1,894,568✔
878
    pos += snprintf(*buf + pos, bufLen - pos, "+ALL");
1,795,560✔
879
    return pos;
1,795,560✔
880
  }
881

882
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
225,680✔
883
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
126,672✔
884
    int                     duration = range->duration / 60;
126,672✔
885

886
    if (range->absolute) {
126,672✔
887
      struct STm tm;
74,802✔
888
      (void)taosTs2Tm(range->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
74,802✔
889
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%04d-%02d-%02d %02d:%02d %dm, ", range->neg ? '-' : '+',
74,802✔
890
                       tm.tm.tm_year + 1900, tm.tm.tm_mon + 1, tm.tm.tm_mday, tm.tm.tm_hour, tm.tm.tm_min, duration);
74,802✔
891
    } else {
892
      int day = range->start / 86400;
51,870✔
893
      int hour = (range->start % 86400) / 3600;
51,870✔
894
      int minute = (range->start % 3600) / 60;
51,870✔
895
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%s %02d:%02d %dm, ", range->neg ? '-' : '+', weekdays[day], hour,
51,870✔
896
                       minute, duration);
897
    }
898
  }
899

900
  if (pos > 0) {
99,008✔
901
    pos -= 2;
99,008✔
902
    (*buf)[pos] = 0; // remove last ", "
99,008✔
903
  }
904

905
  return pos;
99,008✔
906
}
907

908
static int32_t compareDateTimeInterval(const void *a, const void *b, const void *arg) {
1,092✔
909
  SDateTimeWhiteListItem *pA = (SDateTimeWhiteListItem *)a;
1,092✔
910
  SDateTimeWhiteListItem *pB = (SDateTimeWhiteListItem *)b;
1,092✔
911

912
  if (pA->neg != pB->neg) {
1,092✔
913
    return pA->neg ? -1 : 1;
728✔
914
  }
915

916
  if (pA->absolute != pB->absolute) {
364✔
917
    return pA->absolute ? 1 : -1;
364✔
918
  }
919

920
  if (pA->start != pB->start) {
×
921
    return (pA->start < pB->start) ? -1 : 1;
×
922
  }
923

924
  if (pA->duration != pB->duration) {
×
925
    return (pA->duration < pB->duration) ? -1 : 1;
×
926
  }
927

928
  return 0;
×
929
}
930

931
static void sortTimeWhiteList(SDateTimeWhiteList *pList) {
3,094✔
932
  (void)taosqsort(pList->ranges, pList->num, sizeof(SDateTimeWhiteListItem), NULL, compareDateTimeInterval);
3,094✔
933
}
3,094✔
934

935
static void dropOldPasswords(SUserObj *pUser) {
12,114,817✔
936
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
12,114,817✔
937
    return;
653,885✔
938
  }
939

940
  int32_t reuseMax = pUser->passwordReuseMax;
11,460,932✔
941
  if (reuseMax == 0) {
11,460,932✔
942
    reuseMax = 1;  // keep at least one password
11,460,568✔
943
  }
944

945
  int32_t now = taosGetTimestampSec();
11,460,932✔
946
  int32_t index = reuseMax;
11,460,932✔
947
  while(index < pUser->numOfPasswords) {
11,460,932✔
948
    // the set time of the n-th password is the expire time of the n+1-th password
949
    int32_t expireTime = pUser->passwords[index - 1].setTime;
364✔
950
    if (now - expireTime >= pUser->passwordReuseTime) {
364✔
951
      break;
364✔
952
    }
953
    index++;
×
954
  }
955

956
  if (index == pUser->numOfPasswords) {
11,460,932✔
957
    return;
11,460,568✔
958
  }
959
  pUser->numOfPasswords = index;
364✔
960
  // this is a shrink operation, no need to check return value
961
  pUser->passwords = taosMemoryRealloc(pUser->passwords, sizeof(SUserPassword) * pUser->numOfPasswords);
364✔
962
}
963

964
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
381,144✔
965
  int32_t  code = 0;
381,144✔
966
  int32_t  lino = 0;
381,144✔
967
  SUserObj userObj = {0};
381,144✔
968

969
  userObj.passwords = taosMemCalloc(1, sizeof(SUserPassword));
381,144✔
970
  if (userObj.passwords == NULL) {
381,144✔
971
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
972
  }
973
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.passwords[0].pass);
381,144✔
974
  userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
381,144✔
975
  if (strlen(tsDataKey) > 0) {
381,144✔
976
    generateSalt(userObj.salt, sizeof(userObj.salt));
2,405✔
977
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino,
2,405✔
978
                    _ERROR);
979
  }
980

981
  userObj.passwords[0].setTime = taosGetTimestampSec();
381,144✔
982
  userObj.numOfPasswords = 1;
381,144✔
983

984
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
381,144✔
985
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
381,144✔
986
  userObj.createdTime = taosGetTimestampMs();
381,144✔
987
  userObj.updateTime = userObj.createdTime;
381,144✔
988
  userObj.uid = mndGenerateUid(userObj.user, strlen(userObj.user));
381,144✔
989
  userObj.sysInfo = 1;
381,144✔
990
  userObj.enable = 1;
381,144✔
991
  userObj.minSecLevel = TSDB_MIN_SECURITY_LEVEL;
381,144✔
992
  userObj.maxSecLevel = TSDB_MAX_SECURITY_LEVEL;
381,144✔
993

994
#ifdef TD_ENTERPRISE
995

996
  userObj.ipWhiteListVer = taosGetTimestampMs();
381,144✔
997
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
381,144✔
998

999
  if (tsEnableAdvancedSecurity) {
381,144✔
1000
    userObj.changePass = 1;  // force user to change password
×
1001
    userObj.connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
×
1002
    userObj.connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
×
1003
    userObj.callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
×
1004
    userObj.vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
×
1005
    userObj.passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
×
1006
    userObj.passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
×
1007
    userObj.passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
×
1008
    userObj.sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
×
1009
    userObj.failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
×
1010
    userObj.passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
×
1011
    userObj.passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
×
1012
    userObj.inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
×
1013
  } else {
1014
    userObj.changePass = 2;  // allow but not force user to change password
381,144✔
1015
    userObj.connectTime = -1;
381,144✔
1016
    userObj.connectIdleTime = -1;
381,144✔
1017
    userObj.callPerSession = -1;
381,144✔
1018
    userObj.vnodePerCall = -1;
381,144✔
1019
    userObj.passwordReuseTime = 0;
381,144✔
1020
    userObj.passwordReuseMax = 0;
381,144✔
1021
    userObj.passwordLockTime = 1;
381,144✔
1022
    userObj.sessionPerUser = -1;
381,144✔
1023
    userObj.failedLoginAttempts = -1;
381,144✔
1024
    userObj.passwordLifeTime = -1;
381,144✔
1025
    userObj.passwordGraceTime = -1;
381,144✔
1026
    userObj.inactiveAccountTime = -1;
381,144✔
1027
  }
1028

1029
  userObj.allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
381,144✔
1030
  userObj.tokenNum = 0;
381,144✔
1031

1032
#else  // TD_ENTERPRISE
1033

1034
  userObj.ipWhiteListVer = 0;
1035
  userObj.timeWhiteListVer = 0;
1036
  userObj.changePass = 2;  // 2: allow but not force user to change password
1037
  userObj.connectTime = -1;
1038
  userObj.connectIdleTime = -1;
1039
  userObj.callPerSession = -1;
1040
  userObj.vnodePerCall = -1;
1041
  userObj.passwordReuseTime = 0;
1042
  userObj.passwordReuseMax = 0;
1043
  userObj.passwordLockTime = 1;
1044
  userObj.sessionPerUser = -1;
1045
  userObj.failedLoginAttempts = -1;
1046
  userObj.passwordLifeTime = -1;
1047
  userObj.passwordGraceTime = -1;
1048
  userObj.inactiveAccountTime = -1;
1049
  userObj.allowTokenNum = -1;
1050
  userObj.tokenNum = 0;
1051

1052
#endif  // TD_ENTERPRISE
1053

1054
  userObj.pTimeWhiteList = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
381,144✔
1055
  if (userObj.pTimeWhiteList == NULL) {
381,144✔
1056
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
1057
  }
1058

1059
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _ERROR);
381,144✔
1060
  // if this is the root user, change the value of some fields to allow the user login without restriction
1061
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
381,144✔
1062
    userObj.superUser = 1;
381,144✔
1063
    userObj.createdb = 1;
381,144✔
1064
    userObj.sessionPerUser = -1;
381,144✔
1065
    userObj.callPerSession = -1;
381,144✔
1066
    userObj.vnodePerCall = -1;
381,144✔
1067
    userObj.failedLoginAttempts = -1;
381,144✔
1068
    userObj.passwordGraceTime = -1;
381,144✔
1069
    userObj.inactiveAccountTime = -1;
381,144✔
1070
  }
1071

1072
  userObj.roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
381,144✔
1073
  if (userObj.roles == NULL) {
381,144✔
1074
    TAOS_CHECK_GOTO(terrno, &lino, _ERROR);
×
1075
  }
1076

1077
  if ((code = taosHashPut(userObj.roles, TSDB_ROLE_SYSDBA, sizeof(TSDB_ROLE_SYSDBA), NULL, 0)) ||
762,288✔
1078
      (code = taosHashPut(userObj.roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC), NULL, 0)) ||
762,288✔
1079
      (code = taosHashPut(userObj.roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT), NULL, 0))) {
381,144✔
1080
    TAOS_CHECK_GOTO(code, &lino, _ERROR);
×
1081
  }
1082

1083
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
381,144✔
1084
  if (pRaw == NULL) goto _ERROR;
381,144✔
1085
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
381,144✔
1086

1087
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
381,144✔
1088

1089
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_ROLE, NULL, "create-user");
381,144✔
1090
  if (pTrans == NULL) {
381,144✔
1091
    sdbFreeRaw(pRaw);
×
1092
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
1093
    goto _ERROR;
×
1094
  }
1095
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
381,144✔
1096

1097
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
381,144✔
1098
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1099
    mndTransDrop(pTrans);
×
1100
    goto _ERROR;
×
1101
  }
1102
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
381,144✔
1103

1104
  if (mndTransPrepare(pMnode, pTrans) != 0) {
381,144✔
1105
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1106
    mndTransDrop(pTrans);
×
1107
    goto _ERROR;
×
1108
  }
1109

1110
  mndTransDrop(pTrans);
381,144✔
1111
  mndUserFreeObj(&userObj);
381,144✔
1112
  return 0;
381,144✔
1113

1114
_ERROR:
×
1115
  mndUserFreeObj(&userObj);
×
1116
  if (code == 0) {
×
1117
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
1118
  }
1119
  mError("user:%s, failed to create default user since %s", user, tstrerror(code));
×
1120
  TAOS_RETURN(code);
×
1121
}
1122

1123
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
381,144✔
1124
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
381,144✔
1125
}
1126

1127
static int32_t mndUserPrivUpgradeDbOwners(SMnode *pMnode, SRpcMsg *pReq) {
×
1128
  int32_t code = 0, lino = 0;
×
1129
  SSdb   *pSdb = pMnode->pSdb;
×
1130
  SDbObj *pObj = NULL;
×
1131
  void   *pIter = NULL;
×
1132

1133
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "upgrade-db");
×
1134
  if (pTrans == NULL) {
×
1135
    TAOS_CHECK_EXIT(terrno);
×
1136
  }
1137

1138
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pObj))) {
×
1139
    if (pObj->cfg.isMount) {
×
1140
      sdbRelease(pSdb, pObj);
×
1141
      continue;
×
1142
    }
1143
    if (pObj->ownerId != 0) {
×
1144
      sdbRelease(pSdb, pObj);
×
1145
      continue;
×
1146
    }
1147
    SUserObj *pUser = NULL;
×
1148
    (void)mndAcquireUser(pMnode, pObj->createUser, &pUser);
×
1149
    if (pUser == NULL) {
×
1150
      mWarn("db:%s, owner user:%s not found, skip upgrade owner uid", pObj->name, pObj->createUser);
×
1151
      sdbRelease(pSdb, pObj);
×
1152
      continue;
×
1153
    }
1154
    if (pUser->uid == 0) {
×
1155
      mndReleaseUser(pMnode, pUser);
×
1156
      sdbRelease(pSdb, pObj);
×
1157
      continue;
×
1158
    }
1159
    SDbObj newObj = {0};
×
1160
    memcpy(&newObj, pObj, sizeof(SDbObj));
×
1161
    ++newObj.cfgVersion;
×
1162
    newObj.updateTime = taosGetTimestampMs();
×
1163
    newObj.ownerId = pUser->uid;
×
1164
    mInfo("db:%s, owner uid upgraded to %" PRId64, pObj->name, pUser->uid);
×
1165
    mndReleaseUser(pMnode, pUser);
×
1166
    if ((code = mndSetAlterDbCommitLogs(pMnode, pTrans, pObj, &newObj))) {
×
1167
      sdbCancelFetch(pSdb, pIter);
×
1168
      sdbRelease(pSdb, pObj);
×
1169
      TAOS_CHECK_EXIT(code);
×
1170
    }
1171
    sdbRelease(pSdb, pObj);
×
1172
  }
1173

1174
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
×
1175

1176
_exit:
×
1177
  mndTransDrop(pTrans);
×
1178
  if (code < 0) {
×
1179
    mError("failed at line %d to upgrade db owner uid since %s", lino, tstrerror(code));
×
1180
  }
1181
  TAOS_RETURN(code);
×
1182
}
1183

1184
static int32_t mndUserPrivUpgradeOwnedDbs(SMnode *pMnode, SUserObj *pUser) {
×
1185
  int32_t code = 0, lino = 0;
×
1186
  SSdb   *pSdb = pMnode->pSdb;
×
1187
  SDbObj *pObj = NULL;
×
1188
  void   *pIter = NULL;
×
1189
  char   *key = NULL;
×
1190
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pObj))) {
×
1191
    if (strncmp(pObj->createUser, pUser->name, sizeof(pObj->createUser)) != 0) {
×
1192
      sdbRelease(pSdb, pObj);
×
1193
      pObj = NULL;
×
1194
      continue;
×
1195
    }
1196
    TAOS_CHECK_EXIT(taosHashPut(pUser->ownedDbs, pObj->name, strlen(pObj->name) + 1, NULL, 0));
×
1197
    sdbRelease(pSdb, pObj);
×
1198
    pObj = NULL;
×
1199
  }
1200
_exit:
×
1201
  if (pIter) sdbCancelFetch(pSdb, pIter);
×
1202
  if (pObj) sdbRelease(pSdb, pObj);
×
1203
  TAOS_RETURN(code);
×
1204
}
1205

1206
#ifdef TD_ENTERPRISE
1207
static int32_t mndUserPrivUpgradeTbViews(SMnode *pMnode, SUserObj *pUser, SHashObj **ppTblHash, SHashObj *pTbs, int32_t privType,
×
1208
                                         uint8_t objType) {
1209
  int32_t code = 0, lino = 0;
×
1210
  void   *pIter = NULL;
×
1211
  char   *key = NULL;
×
1212
  char   *value = NULL;
×
1213

1214
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES, .add = 1, .objType = objType, .objLevel = 1};
×
1215

1216
  while ((pIter = taosHashIterate(pTbs, pIter))) {
×
1217
    size_t keyLen = 0;
×
1218
    key = taosHashGetKey(pIter, &keyLen);
×
1219

1220
    SName name = {0};
×
1221
    TAOS_CHECK_EXIT(tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
×
1222

1223
    snprintf(alterReq.objFName, TSDB_OBJ_FNAME_LEN, "%d.%s", name.acctId, name.dbname);
×
1224
    snprintf(alterReq.tblName, TSDB_TABLE_NAME_LEN, "%s", name.tname);
×
1225

1226
    privAddType(&alterReq.privileges.privSet, privType);
1227

1228
    if ((objType == PRIV_OBJ_TBL) && (((char *)pIter)[0] != 0) && (((char *)pIter)[1] != 0)) {
×
1229
      alterReq.privileges.cond = taosStrdup(pIter);
×
1230
      if (alterReq.privileges.cond == NULL) {
×
1231
        TAOS_CHECK_EXIT(terrno);
×
1232
      }
1233
      alterReq.privileges.condLen = strlen(pIter) + 1;  // include '\0'
×
1234
      if (ppTblHash && !*ppTblHash) {
×
1235
        *ppTblHash = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
×
1236
        if (!*ppTblHash) {
×
1237
          TAOS_CHECK_EXIT(terrno);
×
1238
        }
1239
        taosHashSetFreeFp(*ppTblHash, privTblPoliciesFree);
×
1240
      }
1241
    }
1242

1243
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1244
    tFreeSAlterRoleReq(&alterReq);
×
1245
  }
1246
_exit:
×
1247
  tFreeSAlterRoleReq(&alterReq);
×
1248
  TAOS_RETURN(code);
×
1249
}
1250

1251
/**
1252
 * @brief upgrade read/write db privileges from 3.3.x.y to 3.4.x.y
1253
 * @param rwType 0x01 read, 0x02 write
1254
 */
1255
static int32_t mndUserPrivUpgradeRwDbs(SMnode *pMnode, SUserObj *pUser, SHashObj *pDbs, int8_t rwType) {
×
1256
  int32_t code = 0, lino = 0;
×
1257
  void   *pIter = NULL;
×
1258
  char   *key = NULL;
×
1259
  while ((pIter = taosHashIterate(pDbs, pIter))) {
×
1260
    key = taosHashGetKey(pIter, NULL);
×
1261
    TAOS_CHECK_EXIT(privUpgradeRwDb(pUser->objPrivs, key, "*", rwType));
×
1262
  }
1263
_exit:
×
1264
  TAOS_RETURN(code);
×
1265
}
1266

1267
static int32_t mndUserPrivUpgradeUsedDbs(SMnode *pMnode, SUserObj *pUser, SHashObj *pDbs) {
×
1268
  int32_t code = 0, lino = 0;
×
1269
  void   *pIter = NULL;
×
1270
  char   *key = NULL;
×
1271
  char   *value = NULL;
×
1272

1273
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES, .add = 1, .objType = PRIV_OBJ_DB};
×
1274

1275
  while ((pIter = taosHashIterate(pDbs, pIter))) {
×
1276
    key = taosHashGetKey(pIter, NULL);
×
1277

1278
    SName name = {0};
×
1279
    TAOS_CHECK_EXIT(tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB));
×
1280

1281
    snprintf(alterReq.objFName, TSDB_OBJ_FNAME_LEN, "%d.%s", name.acctId, name.dbname);
×
1282

1283
    privAddType(&alterReq.privileges.privSet, PRIV_DB_USE);
1284

1285
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1286
  }
1287
_exit:
×
1288
  tFreeSAlterRoleReq(&alterReq);
×
1289
  TAOS_RETURN(code);
×
1290
}
1291

1292
static int32_t mndUserPrivUpgradeTopics(SMnode *pMnode, SUserObj *pUser, SHashObj *pTopics) {
×
1293
  int32_t code = 0, lino = 0;
×
1294
  void   *pIter = NULL;
×
1295
  char   *key = NULL;
×
1296
  char   *value = NULL;
×
1297

1298
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES,
×
1299
                            .add = 1,
1300
                            .objType = PRIV_OBJ_TOPIC,
1301
                            .objLevel = 1,
1302
                            .ignoreNotExists = 1};
1303

1304
  while ((pIter = taosHashIterate(pTopics, pIter))) {
×
1305
    size_t keyLen = 0;
×
1306
    key = taosHashGetKey(pIter, &keyLen);
×
1307

1308
    SName name = {0};
×
1309
    if (tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB)) {  // 1.topicName
×
1310
      continue;
×
1311
    }
1312
    snprintf(alterReq.tblName, TSDB_TABLE_NAME_LEN, "%s", name.dbname);
×
1313

1314
    SMqTopicObj *pTopic = NULL;
×
1315
    if (mndAcquireTopic(pMnode, key, &pTopic)) {
×
1316
      continue;  // no topic exists
×
1317
    }
1318
    snprintf(alterReq.objFName, TSDB_OBJ_FNAME_LEN, "%s", pTopic->db);
×
1319
    mndReleaseTopic(pMnode, pTopic);
×
1320

1321
    privAddType(&alterReq.privileges.privSet, PRIV_CM_SUBSCRIBE);
1322

1323
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1324
  }
1325
_exit:
×
1326
  tFreeSAlterRoleReq(&alterReq);
×
1327
  TAOS_RETURN(code);
×
1328
}
1329
#endif
1330

1331
/**
1332
 * @brief migrate from 3.3.x.y to 3.4.x.y
1333
 * @return int32_t
1334
 */
1335
static int32_t mndUserPrivUpgradeUser(SMnode *pMnode, SUserObj *pObj) {
×
1336
  int32_t          code = 0, lino = 0;
×
1337
  SPrivHashObjSet *pPrivSet = pObj->legacyPrivs;
×
1338

1339
  if (pObj->uid == 0) {
×
1340
    pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
1341
  }
1342

1343
  if (!pObj->objPrivs &&
×
1344
      !(pObj->objPrivs = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
1345
    TAOS_CHECK_EXIT(terrno);
×
1346
  }
1347

1348
  if (!pObj->roles &&
×
1349
      !(pObj->roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
1350
    TAOS_CHECK_EXIT(terrno);
×
1351
  }
1352

1353
  // assign roles and system privileges
1354
  uint8_t flag = 0x01;
×
1355
  if (pObj->superUser) {
×
1356
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSDBA, sizeof(TSDB_ROLE_SYSDBA), &flag, sizeof(flag)));
×
1357
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC), &flag, sizeof(flag)));
×
1358
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT), &flag, sizeof(flag)));
×
1359
  } else {
1360
    if (pObj->sysInfo == 1) {
×
1361
      TAOS_CHECK_EXIT(
×
1362
          taosHashPut(pObj->roles, TSDB_ROLE_SYSINFO_1, sizeof(TSDB_ROLE_SYSINFO_1), &flag, sizeof(flag)));
1363
    } else {
1364
      TAOS_CHECK_EXIT(
×
1365
          taosHashPut(pObj->roles, TSDB_ROLE_SYSINFO_0, sizeof(TSDB_ROLE_SYSINFO_0), &flag, sizeof(flag)));
1366
    }
1367
    if (pObj->createdb == 1) {
×
1368
      privAddType(&pObj->sysPrivs, PRIV_DB_CREATE);
×
1369
    }
1370
  }
1371
#ifdef TD_ENTERPRISE
1372
  if (pPrivSet) {
×
1373
    // read db: db.*
1374
    TAOS_CHECK_EXIT(mndUserPrivUpgradeRwDbs(pMnode, pObj, pPrivSet->pReadDbs, 0x01));
×
1375
    // write db: db.*
1376
    TAOS_CHECK_EXIT(mndUserPrivUpgradeRwDbs(pMnode, pObj, pPrivSet->pWriteDbs, 0x02));
×
1377
    // tables/views
1378
    TAOS_CHECK_EXIT(
×
1379
        mndUserPrivUpgradeTbViews(pMnode, pObj, &pObj->selectTbs, pPrivSet->pReadTbs, PRIV_TBL_SELECT, PRIV_OBJ_TBL));
1380
    TAOS_CHECK_EXIT(
×
1381
        mndUserPrivUpgradeTbViews(pMnode, pObj, &pObj->insertTbs, pPrivSet->pWriteTbs, PRIV_TBL_INSERT, PRIV_OBJ_TBL));
1382
    TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pAlterTbs, PRIV_CM_ALTER, PRIV_OBJ_TBL));
×
1383
    TAOS_CHECK_EXIT(
×
1384
        mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pReadViews, PRIV_VIEW_SELECT, PRIV_OBJ_VIEW));
1385
    TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pAlterViews, PRIV_CM_ALTER, PRIV_OBJ_VIEW));
×
1386
    TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pAlterViews, PRIV_CM_DROP, PRIV_OBJ_VIEW));
×
1387
    // used dbs
1388
    TAOS_CHECK_EXIT(mndUserPrivUpgradeUsedDbs(pMnode, pObj, pPrivSet->pUseDbs));
×
1389
    // subscribe
1390
    TAOS_CHECK_EXIT(mndUserPrivUpgradeTopics(pMnode, pObj, pPrivSet->pTopics));
×
1391
  }
1392
#endif
1393
  // owned dbs
1394
  TAOS_CHECK_EXIT(mndUserPrivUpgradeOwnedDbs(pMnode, pObj));
×
1395
  mInfo("user:%s, upgraded with uid:%" PRId64, pObj->name, pObj->uid);
×
1396
_exit:
×
1397
  TAOS_RETURN(code);
×
1398
}
1399

1400
static int32_t mndUserPrivUpgradeUsers(SMnode *pMnode, SRpcMsg *pReq) {
×
1401
  int32_t   code = 0, lino = 0;
×
1402
  SSdb     *pSdb = pMnode->pSdb;
×
1403
  SUserObj *pObj = NULL;
×
1404
  void     *pIter = NULL;
×
1405
  SUserObj  newObj = {0};
×
1406
  SUserObj *pRoot = NULL;
×
1407
  bool      forceUpgrade = false;
×
1408

1409
  if (0 == mndAcquireUser(pMnode, TSDB_DEFAULT_USER, &pRoot)) {
×
1410
    if (taosHashGetSize(pRoot->roles) == 0) {
×
1411
      forceUpgrade = true;
×
1412
    }
1413
    mndReleaseUser(pMnode, pRoot);
×
1414
  }
1415

1416
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj))) {
×
1417
    if (pObj->uid != 0 && pObj->legacyPrivs == NULL && !forceUpgrade) {
×
1418
      sdbRelease(pSdb, pObj);
×
1419
      pObj = NULL;
×
1420
      continue;
×
1421
    }
1422
    if (pObj->uid == 0) {
×
1423
      // Assign uid firstly because the transactions in mndUserPrivUpgradeUsers may not finish when
1424
      // mndUserPrivUpgradeDbOwners is called
1425
      pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
1426
    }
1427
    memset(&newObj, 0, sizeof(SUserObj));
×
1428
    TAOS_CHECK_EXIT(mndUserDupObj(pObj, &newObj));
×
1429
    TAOS_CHECK_EXIT(mndUserPrivUpgradeUser(pMnode, &newObj));
×
1430
    TAOS_CHECK_EXIT(mndAlterUser(pMnode, &newObj, pReq));
×
1431
    mndUserFreeObj(&newObj);
×
1432
    sdbRelease(pSdb, pObj);
×
1433
    pObj = NULL;
×
1434
  }
1435
_exit:
×
1436
  sdbCancelFetch(pSdb, pIter);
×
1437
  sdbRelease(pSdb, pObj);
×
1438
  mndUserFreeObj(&newObj);
×
1439
  if (code < 0) {
×
1440
    mError("failed at line %d to upgrade db owner uid since %s", lino, tstrerror(code));
×
1441
  }
1442
  TAOS_RETURN(code);
×
1443
}
1444

1445
static int32_t mndProcessUpgradeUserReq(SRpcMsg *pReq) {
×
1446
  SMnode *pMnode = pReq->info.node;
×
1447
  int32_t code = 0, lino = 0;
×
1448

1449
  TAOS_CHECK_EXIT(mndUserPrivUpgradeUsers(pMnode, pReq));
×
1450
  TAOS_CHECK_EXIT(mndUserPrivUpgradeDbOwners(pMnode, pReq));
×
1451
_exit:
×
1452
  if (code < 0) {
×
1453
    mError("failed at line %d to upgrade users since %s", lino, tstrerror(code));
×
1454
  }
1455
  TAOS_RETURN(code);
×
1456
}
1457

1458
static int32_t mndProcessUpgradeUserRsp(SRpcMsg *pReq) { return 0;}
×
1459

1460
static int32_t mndUpgradeUsers(SMnode *pMnode, int32_t version) {
441,210✔
1461
  int32_t code = 0, lino = 0;
441,210✔
1462
  if (upgradeSecurity == 0) return code;
441,210✔
1463

1464
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPGRADE_USER, .info.ahandle = 0, .info.notFreeAhandle = 1};
×
1465
  SEpSet  epSet = {0};
×
1466
  mndGetMnodeEpSet(pMnode, &epSet);
×
1467
  TAOS_CHECK_EXIT(tmsgSendReq(&epSet, &rpcMsg));
×
1468
_exit:
×
1469
  if (code < 0) {
×
1470
    mError("failed at line %d to upgrade users since %s", lino, tstrerror(code));
×
1471
  }
1472
  TAOS_RETURN(code);
×
1473
}
1474

1475
static int32_t tSerializeUserObjExt(void *buf, int32_t bufLen, SUserObj *pObj) {
9,837,618✔
1476
  int32_t  code = 0, lino = 0;
9,837,618✔
1477
  int32_t  tlen = 0;
9,837,618✔
1478
  void    *pIter = NULL;
9,837,618✔
1479
  SEncoder encoder = {0};
9,837,618✔
1480
  tEncoderInit(&encoder, buf, bufLen);
9,837,618✔
1481

1482
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
9,837,618✔
1483
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->uid));
19,675,236✔
1484

1485
  TAOS_CHECK_EXIT(tSerializePrivSysObjPolicies(&encoder, &pObj->sysPrivs, pObj->objPrivs));
9,837,618✔
1486

1487
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->selectTbs));
9,837,618✔
1488
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->insertTbs));
9,837,618✔
1489
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->updateTbs));
9,837,618✔
1490
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->deleteTbs));
9,837,618✔
1491

1492
  int32_t nRoles = taosHashGetSize(pObj->roles);
9,837,618✔
1493
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nRoles));
9,837,618✔
1494

1495
  while ((pIter = taosHashIterate(pObj->roles, pIter))) {
31,200,864✔
1496
    size_t keyLen = 0;
21,363,246✔
1497
    char  *key = taosHashGetKey(pIter, &keyLen);  // key: role name
21,363,246✔
1498
    TAOS_CHECK_EXIT(tEncodeCStr(&encoder, key));
21,363,246✔
1499

1500
    uint8_t flag = *(uint8_t *)pIter;
21,363,246✔
1501
    TAOS_CHECK_EXIT(tEncodeU8(&encoder, flag));  // value: 0 reset, 1 set(default)
42,726,492✔
1502
  }
1503

1504
  int32_t nOwnedDbs = taosHashGetSize(pObj->ownedDbs);
9,837,618✔
1505
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nOwnedDbs));
9,837,618✔
1506
  pIter = NULL;
9,837,618✔
1507
  while ((pIter = taosHashIterate(pObj->ownedDbs, pIter))) {
39,538,922✔
1508
    size_t keyLen = 0;
29,701,304✔
1509
    char  *key = taosHashGetKey(pIter, &keyLen);  // key: dbFName
29,701,304✔
1510
    TAOS_CHECK_EXIT(tEncodeCStr(&encoder, key));
29,701,304✔
1511
  }
1512

1513
  tEndEncode(&encoder);
9,837,618✔
1514
  tlen = encoder.pos;
9,837,618✔
1515
_exit:
9,837,618✔
1516
  tEncoderClear(&encoder);
9,837,618✔
1517
  if (code < 0) {
9,837,618✔
1518
    mError("user:%s, %s failed at line %d since %s", pObj->user, __func__, lino, tstrerror(code));
×
1519
    TAOS_RETURN(code);
×
1520
  }
1521

1522
  return tlen;
9,837,618✔
1523
}
1524

1525
static int32_t tDeserializeUserObjExt(void *buf, int32_t bufLen, SUserObj *pObj) {
4,011,759✔
1526
  int32_t  code = 0, lino = 0;
4,011,759✔
1527
  SDecoder decoder = {0};
4,011,759✔
1528
  tDecoderInit(&decoder, buf, bufLen);
4,011,759✔
1529

1530
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
4,011,759✔
1531
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->uid));
8,023,518✔
1532
  TAOS_CHECK_EXIT(tDeserializePrivSysObjPolicies(&decoder, &pObj->sysPrivs, &pObj->objPrivs));
4,011,759✔
1533
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->selectTbs));
4,011,759✔
1534
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->insertTbs));
4,011,759✔
1535
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->updateTbs));
4,011,759✔
1536
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->deleteTbs));
4,011,759✔
1537
  int32_t nRoles = 0;
4,011,759✔
1538
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nRoles));
4,011,759✔
1539
  if (nRoles > 0) {
4,011,759✔
1540
    if (!pObj->roles &&
3,952,876✔
1541
        !(pObj->roles = taosHashInit(nRoles, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), 1, HASH_ENTRY_LOCK))) {
3,952,876✔
1542
      TAOS_CHECK_EXIT(terrno);
×
1543
    }
1544
    for (int32_t i = 0; i < nRoles; i++) {
12,123,841✔
1545
      int32_t keyLen = 0;
8,170,965✔
1546
      char   *key = NULL;
8,170,965✔
1547
      TAOS_CHECK_EXIT(tDecodeCStrAndLen(&decoder, &key, &keyLen));
8,170,965✔
1548
      uint8_t flag = 0;
8,170,965✔
1549
      TAOS_CHECK_EXIT(tDecodeU8(&decoder, &flag));
8,170,965✔
1550
      TAOS_CHECK_EXIT(taosHashPut(pObj->roles, key, keyLen + 1, &flag, sizeof(flag)));
8,170,965✔
1551
    }
1552
  }
1553
  if (!tDecodeIsEnd(&decoder)) {
4,011,759✔
1554
    int32_t nOwnedDbs = 0;
4,011,759✔
1555
    TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nOwnedDbs));
4,011,759✔
1556
    if (nOwnedDbs > 0) {
4,011,759✔
1557
      if (!pObj->ownedDbs &&
1,689,813✔
1558
          !(pObj->ownedDbs =
1,689,813✔
1559
                taosHashInit(nOwnedDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), 1, HASH_ENTRY_LOCK))) {
1,689,813✔
1560
        TAOS_CHECK_EXIT(terrno);
×
1561
      }
1562
      for (int32_t i = 0; i < nOwnedDbs; ++i) {
15,181,933✔
1563
        int32_t keyLen = 0;
13,492,120✔
1564
        char   *key = NULL;
13,492,120✔
1565
        TAOS_CHECK_EXIT(tDecodeCStrAndLen(&decoder, &key, &keyLen));
13,492,120✔
1566
        TAOS_CHECK_EXIT(taosHashPut(pObj->ownedDbs, key, keyLen + 1, NULL, 0));
13,492,120✔
1567
      }
1568
    }
1569
  }
1570

1571
_exit:
4,007,621✔
1572
  tEndDecode(&decoder);
4,011,759✔
1573
  tDecoderClear(&decoder);
4,011,759✔
1574
  if (code < 0) {
4,011,759✔
1575
    mError("user, %s failed at line %d since %s, row:%p", __func__, lino, tstrerror(code), pObj);
×
1576
  }
1577
  TAOS_RETURN(code);
4,011,759✔
1578
}
1579

1580
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
4,918,809✔
1581
  int32_t code = 0;
4,918,809✔
1582
  int32_t lino = 0;
4,918,809✔
1583
  int32_t passReserve = (sizeof(SUserPassword) + 8) * pUser->numOfPasswords + 4;
4,918,809✔
1584
  int32_t ipWhiteReserve =
×
1585
      pUser->pIpWhiteListDual ? (sizeof(SIpRange) * pUser->pIpWhiteListDual->num + sizeof(SIpWhiteListDual) + 4) : 16;
4,918,809✔
1586
  int32_t timeWhiteReserve =
×
1587
      pUser->pTimeWhiteList
4,918,809✔
1588
          ? (sizeof(SDateTimeWhiteListItem) * pUser->pTimeWhiteList->num + sizeof(SDateTimeWhiteList) + 4)
4,918,809✔
1589
          : 16;
1590
  int32_t numOfReadDbs = 0;     // taosHashGetSize(pUser->readDbs);
4,918,809✔
1591
  int32_t numOfWriteDbs = 0;    // taosHashGetSize(pUser->writeDbs);
4,918,809✔
1592
  int32_t numOfReadTbs = 0;     // taosHashGetSize(pUser->readTbs);
4,918,809✔
1593
  int32_t numOfWriteTbs = 0;    // taosHashGetSize(pUser->writeTbs);
4,918,809✔
1594
  int32_t numOfAlterTbs = 0;    // taosHashGetSize(pUser->alterTbs);
4,918,809✔
1595
  int32_t numOfReadViews = 0;   // taosHashGetSize(pUser->readViews);
4,918,809✔
1596
  int32_t numOfWriteViews = 0;  // taosHashGetSize(pUser->writeViews);
4,918,809✔
1597
  int32_t numOfAlterViews = 0;  // taosHashGetSize(pUser->alterViews);
4,918,809✔
1598
  int32_t numOfTopics = 0;      // taosHashGetSize(pUser->topics);
4,918,809✔
1599
  int32_t numOfUseDbs = 0;      // taosHashGetSize(pUser->useDbs);
4,918,809✔
1600
  int32_t numOfRoles = taosHashGetSize(pUser->roles);
4,918,809✔
1601
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
4,918,809✔
1602
                 numOfTopics * TSDB_TOPIC_FNAME_LEN + ipWhiteReserve + timeWhiteReserve + passReserve;
4,918,809✔
1603
  char    *buf = NULL;
4,918,809✔
1604
  SSdbRaw *pRaw = NULL;
4,918,809✔
1605

1606
  char *stb = NULL;
4,918,809✔
1607
#if 0
1608
  stb = taosHashIterate(pUser->readTbs, NULL);
1609
  while (stb != NULL) {
1610
    size_t keyLen = 0;
1611
    void  *key = taosHashGetKey(stb, &keyLen);
1612
    size += sizeof(int32_t);
1613
    size += keyLen;
1614

1615
    size_t valueLen = 0;
1616
    valueLen = strlen(stb) + 1;
1617
    size += sizeof(int32_t);
1618
    size += valueLen;
1619
    stb = taosHashIterate(pUser->readTbs, stb);
1620
  }
1621

1622
  stb = taosHashIterate(pUser->writeTbs, NULL);
1623
  while (stb != NULL) {
1624
    size_t keyLen = 0;
1625
    void  *key = taosHashGetKey(stb, &keyLen);
1626
    size += sizeof(int32_t);
1627
    size += keyLen;
1628

1629
    size_t valueLen = 0;
1630
    valueLen = strlen(stb) + 1;
1631
    size += sizeof(int32_t);
1632
    size += valueLen;
1633
    stb = taosHashIterate(pUser->writeTbs, stb);
1634
  }
1635
  stb = taosHashIterate(pUser->alterTbs, NULL);
1636
  while (stb != NULL) {
1637
    size_t keyLen = 0;
1638
    void  *key = taosHashGetKey(stb, &keyLen);
1639
    size += sizeof(int32_t);
1640
    size += keyLen;
1641

1642
    size_t valueLen = 0;
1643
    valueLen = strlen(stb) + 1;
1644
    size += sizeof(int32_t);
1645
    size += valueLen;
1646
    stb = taosHashIterate(pUser->alterTbs, stb);
1647
  }
1648

1649
  stb = taosHashIterate(pUser->readViews, NULL);
1650
  while (stb != NULL) {
1651
    size_t keyLen = 0;
1652
    void  *key = taosHashGetKey(stb, &keyLen);
1653
    size += sizeof(int32_t);
1654
    size += keyLen;
1655

1656
    size_t valueLen = 0;
1657
    valueLen = strlen(stb) + 1;
1658
    size += sizeof(int32_t);
1659
    size += valueLen;
1660
    stb = taosHashIterate(pUser->readViews, stb);
1661
  }
1662

1663
  stb = taosHashIterate(pUser->writeViews, NULL);
1664
  while (stb != NULL) {
1665
    size_t keyLen = 0;
1666
    void  *key = taosHashGetKey(stb, &keyLen);
1667
    size += sizeof(int32_t);
1668
    size += keyLen;
1669

1670
    size_t valueLen = 0;
1671
    valueLen = strlen(stb) + 1;
1672
    size += sizeof(int32_t);
1673
    size += valueLen;
1674
    stb = taosHashIterate(pUser->writeViews, stb);
1675
  }
1676

1677
  stb = taosHashIterate(pUser->alterViews, NULL);
1678
  while (stb != NULL) {
1679
    size_t keyLen = 0;
1680
    void  *key = taosHashGetKey(stb, &keyLen);
1681
    size += sizeof(int32_t);
1682
    size += keyLen;
1683

1684
    size_t valueLen = 0;
1685
    valueLen = strlen(stb) + 1;
1686
    size += sizeof(int32_t);
1687
    size += valueLen;
1688
    stb = taosHashIterate(pUser->alterViews, stb);
1689
  }
1690

1691
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
1692
  while (useDb != NULL) {
1693
    size_t keyLen = 0;
1694
    void  *key = taosHashGetKey(useDb, &keyLen);
1695
    size += sizeof(int32_t);
1696
    size += keyLen;
1697
    size += sizeof(int32_t);
1698
    useDb = taosHashIterate(pUser->useDbs, useDb);
1699
  }
1700
#endif
1701
  int32_t sizeExt = tSerializeUserObjExt(NULL, 0, pUser);
4,918,809✔
1702
  if (sizeExt < 0) {
4,918,809✔
1703
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1704
  }
1705
  size += sizeExt;
4,918,809✔
1706

1707
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
4,918,809✔
1708
  if (pRaw == NULL) {
4,918,809✔
1709
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1710
  }
1711

1712
  int32_t dataPos = 0;
4,918,809✔
1713
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
4,918,809✔
1714

1715
  dropOldPasswords(pUser);
4,918,809✔
1716
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
4,918,809✔
1717
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
10,840,620✔
1718
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
5,921,811✔
1719
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
5,921,811✔
1720
  }
1721
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
4,918,809✔
1722

1723
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
4,918,809✔
1724
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
4,918,809✔
1725
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
4,918,809✔
1726
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
4,918,809✔
1727
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
4,918,809✔
1728
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
4,918,809✔
1729
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
4,918,809✔
1730
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
4,918,809✔
1731
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
4,918,809✔
1732
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
4,918,809✔
1733
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
4,918,809✔
1734
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
4,918,809✔
1735
#if 0
1736
  char *db = taosHashIterate(pUser->readDbs, NULL);
1737
  while (db != NULL) {
1738
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
1739
    db = taosHashIterate(pUser->readDbs, db);
1740
  }
1741

1742
  db = taosHashIterate(pUser->writeDbs, NULL);
1743
  while (db != NULL) {
1744
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
1745
    db = taosHashIterate(pUser->writeDbs, db);
1746
  }
1747
  char *topic = taosHashIterate(pUser->topics, NULL);
1748
  while (topic != NULL) {
1749
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
1750
    topic = taosHashIterate(pUser->topics, topic);
1751
  }
1752
#endif
1753
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
4,918,809✔
1754
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
4,918,809✔
1755
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
4,918,809✔
1756
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
4,918,809✔
1757
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
4,918,809✔
1758
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
4,918,809✔
1759
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
4,918,809✔
1760

1761
#if 0
1762
  stb = taosHashIterate(pUser->readTbs, NULL);
1763
  while (stb != NULL) {
1764
    size_t keyLen = 0;
1765
    void  *key = taosHashGetKey(stb, &keyLen);
1766
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1767
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1768

1769
    size_t valueLen = 0;
1770
    valueLen = strlen(stb) + 1;
1771
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1772
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1773
    stb = taosHashIterate(pUser->readTbs, stb);
1774
  }
1775

1776
  stb = taosHashIterate(pUser->writeTbs, NULL);
1777
  while (stb != NULL) {
1778
    size_t keyLen = 0;
1779
    void  *key = taosHashGetKey(stb, &keyLen);
1780
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1781
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1782

1783
    size_t valueLen = 0;
1784
    valueLen = strlen(stb) + 1;
1785
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1786
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1787
    stb = taosHashIterate(pUser->writeTbs, stb);
1788
  }
1789
  stb = taosHashIterate(pUser->alterTbs, NULL);
1790
  while (stb != NULL) {
1791
    size_t keyLen = 0;
1792
    void  *key = taosHashGetKey(stb, &keyLen);
1793
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1794
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1795

1796
    size_t valueLen = 0;
1797
    valueLen = strlen(stb) + 1;
1798
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1799
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1800
    stb = taosHashIterate(pUser->alterTbs, stb);
1801
  }
1802

1803
  stb = taosHashIterate(pUser->readViews, NULL);
1804
  while (stb != NULL) {
1805
    size_t keyLen = 0;
1806
    void  *key = taosHashGetKey(stb, &keyLen);
1807
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1808
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1809

1810
    size_t valueLen = 0;
1811
    valueLen = strlen(stb) + 1;
1812
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1813
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1814
    stb = taosHashIterate(pUser->readViews, stb);
1815
  }
1816

1817
  stb = taosHashIterate(pUser->writeViews, NULL);
1818
  while (stb != NULL) {
1819
    size_t keyLen = 0;
1820
    void  *key = taosHashGetKey(stb, &keyLen);
1821
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1822
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1823

1824
    size_t valueLen = 0;
1825
    valueLen = strlen(stb) + 1;
1826
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1827
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1828
    stb = taosHashIterate(pUser->writeViews, stb);
1829
  }
1830

1831
  stb = taosHashIterate(pUser->alterViews, NULL);
1832
  while (stb != NULL) {
1833
    size_t keyLen = 0;
1834
    void  *key = taosHashGetKey(stb, &keyLen);
1835
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1836
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1837

1838
    size_t valueLen = 0;
1839
    valueLen = strlen(stb) + 1;
1840
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1841
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1842
    stb = taosHashIterate(pUser->alterViews, stb);
1843
  }
1844

1845
  useDb = taosHashIterate(pUser->useDbs, NULL);
1846
  while (useDb != NULL) {
1847
    size_t keyLen = 0;
1848
    void  *key = taosHashGetKey(useDb, &keyLen);
1849
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1850
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1851

1852
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
1853
    useDb = taosHashIterate(pUser->useDbs, useDb);
1854
  }
1855
#endif
1856
  // save white list
1857
  int32_t num = pUser->pIpWhiteListDual->num;
4,918,809✔
1858
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
4,918,809✔
1859
  int32_t maxBufLen = TMAX(tlen, sizeExt);
4,918,809✔
1860
  if ((buf = taosMemoryCalloc(1, maxBufLen)) == NULL) {
4,918,809✔
1861
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1862
  }
1863
  int32_t len = 0;
4,918,809✔
1864
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
4,918,809✔
1865

1866
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
4,918,809✔
1867
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
4,918,809✔
1868

1869
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
4,918,809✔
1870
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
4,918,809✔
1871

1872
  SDB_SET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
4,918,809✔
1873
  SDB_SET_INT8(pRaw, dataPos, pUser->changePass, _OVER);
4,918,809✔
1874
  SDB_SET_INT32(pRaw, dataPos, pUser->sessionPerUser, _OVER);
4,918,809✔
1875
  SDB_SET_INT32(pRaw, dataPos, pUser->connectTime, _OVER);
4,918,809✔
1876
  SDB_SET_INT32(pRaw, dataPos, pUser->connectIdleTime, _OVER);
4,918,809✔
1877
  SDB_SET_INT32(pRaw, dataPos, pUser->callPerSession, _OVER);
4,918,809✔
1878
  SDB_SET_INT32(pRaw, dataPos, pUser->vnodePerCall, _OVER);
4,918,809✔
1879
  SDB_SET_INT32(pRaw, dataPos, pUser->failedLoginAttempts, _OVER);
4,918,809✔
1880
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLifeTime, _OVER);
4,918,809✔
1881
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseTime, _OVER);
4,918,809✔
1882
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseMax, _OVER);
4,918,809✔
1883
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLockTime, _OVER);
4,918,809✔
1884
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordGraceTime, _OVER);
4,918,809✔
1885
  SDB_SET_INT32(pRaw, dataPos, pUser->inactiveAccountTime, _OVER);
4,918,809✔
1886
  SDB_SET_INT32(pRaw, dataPos, pUser->allowTokenNum, _OVER);
4,918,809✔
1887
  SDB_SET_INT32(pRaw, dataPos, pUser->tokenNum, _OVER);
4,918,809✔
1888

1889
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
4,918,809✔
1890
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
4,927,909✔
1891
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
9,100✔
1892
    SDB_SET_BOOL(pRaw, dataPos, range->absolute, _OVER);
9,100✔
1893
    SDB_SET_BOOL(pRaw, dataPos, range->neg, _OVER);
9,100✔
1894
    SDB_SET_INT64(pRaw, dataPos, range->start, _OVER);
9,100✔
1895
    SDB_SET_INT32(pRaw, dataPos, range->duration, _OVER);
9,100✔
1896
  }
1897

1898
  sizeExt = tSerializeUserObjExt(buf, sizeExt, pUser);
4,918,809✔
1899
  if (sizeExt < 0) {
4,918,809✔
1900
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1901
  }
1902
  SDB_SET_INT32(pRaw, dataPos, sizeExt, _OVER);
4,918,809✔
1903
  SDB_SET_BINARY(pRaw, dataPos, buf, sizeExt, _OVER);
4,918,809✔
1904

1905
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
4,918,809✔
1906

1907
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
4,918,809✔
1908

1909
_OVER:
4,918,809✔
1910
  taosMemoryFree(buf);
4,918,809✔
1911
  if (code < 0) {
4,918,809✔
1912
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1913
           tstrerror(code));
1914
    sdbFreeRaw(pRaw);
×
1915
    pRaw = NULL;
×
1916
    terrno = code;
×
1917
  }
1918

1919
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
4,918,809✔
1920
  return pRaw;
4,918,809✔
1921
}
1922

1923
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
4,011,759✔
1924
  int32_t   code = 0;
4,011,759✔
1925
  int32_t   lino = 0;
4,011,759✔
1926
  SSdbRow  *pRow = NULL;
4,011,759✔
1927
  SUserObj *pUser = NULL;
4,011,759✔
1928
  char     *key = NULL;
4,011,759✔
1929
  char     *value = NULL;
4,011,759✔
1930

1931
  int8_t sver = 0;
4,011,759✔
1932
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
4,011,759✔
1933
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1934
  }
1935

1936
  if (sver < 1 || sver > USER_VER_NUMBER) {
4,011,759✔
1937
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1938
  }
1939

1940
  pRow = sdbAllocRow(sizeof(SUserObj));
4,011,759✔
1941
  if (pRow == NULL) {
4,011,759✔
1942
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1943
  }
1944

1945
  pUser = sdbGetRowObj(pRow);
4,011,759✔
1946
  if (pUser == NULL) {
4,011,759✔
1947
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1948
  }
1949

1950
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
4,011,759✔
1951
    upgradeSecurity = 1;
×
1952
    pUser->legacyPrivs = taosMemCalloc(1, sizeof(SPrivHashObjSet));
×
1953
    if (pUser->legacyPrivs == NULL) {
×
1954
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1955
    }
1956
  }
1957

1958
  int32_t dataPos = 0;
4,011,759✔
1959
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
4,011,759✔
1960

1961
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
4,011,759✔
1962
    pUser->passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
×
1963
    if (pUser->passwords == NULL) {
×
1964
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1965
    }
1966
    SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[0].pass, TSDB_PASSWORD_LEN, _OVER)
×
1967
    pUser->numOfPasswords = 1;
×
1968
    memset(pUser->salt, 0, sizeof(pUser->salt));
×
1969
  } else {
1970
    SDB_GET_INT32(pRaw, dataPos, &pUser->numOfPasswords, _OVER)
4,011,759✔
1971
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
4,011,759✔
1972
    if (pUser->passwords == NULL) {
4,011,759✔
1973
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1974
    }
1975
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
8,961,728✔
1976
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
4,949,969✔
1977
      SDB_GET_INT32(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
4,949,969✔
1978
    }
1979
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
4,011,759✔
1980
  }
1981

1982
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
4,011,759✔
1983
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
4,011,759✔
1984
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
4,011,759✔
1985
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
4,011,759✔
1986
    pUser->passwords[0].setTime = (int32_t)(pUser->updateTime / 1000);
×
1987
  }
1988

1989
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
4,011,759✔
1990
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
4,011,759✔
1991
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
4,011,759✔
1992
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
4,011,759✔
1993
  if (pUser->superUser) pUser->createdb = 1;
4,011,759✔
1994
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
4,011,759✔
1995
  if (sver >= 4) {
4,011,759✔
1996
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
4,011,759✔
1997
  }
1998

1999
  int32_t numOfReadDbs = 0;
4,011,759✔
2000
  int32_t numOfWriteDbs = 0;
4,011,759✔
2001
  int32_t numOfTopics = 0;
4,011,759✔
2002
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
4,011,759✔
2003
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
4,011,759✔
2004
  if (sver >= 2) {
4,011,759✔
2005
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
4,011,759✔
2006
  }
2007

2008
  if (numOfReadDbs > 0) {
4,011,759✔
2009
    pUser->legacyPrivs->pReadDbs =
×
2010
        taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2011
    if (pUser->legacyPrivs->pReadDbs == NULL) {
×
2012
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2013
    }
2014
  }
2015
  if (numOfWriteDbs > 0) {
4,011,759✔
2016
    pUser->legacyPrivs->pWriteDbs =
×
2017
        taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2018
    if (pUser->legacyPrivs->pWriteDbs == NULL) {
×
2019
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2020
    }
2021
  }
2022
  if (numOfTopics > 0) {
4,011,759✔
2023
    pUser->legacyPrivs->pTopics =
×
2024
        taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2025
    if (pUser->legacyPrivs->pTopics == NULL) {
×
2026
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2027
    }
2028
  }
2029
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
4,011,759✔
2030
    char db[TSDB_DB_FNAME_LEN] = {0};
×
2031
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
2032
    int32_t len = strlen(db) + 1;
×
2033
    TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
2034
  }
2035

2036
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
4,011,759✔
2037
    char db[TSDB_DB_FNAME_LEN] = {0};
×
2038
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
2039
    int32_t len = strlen(db) + 1;
×
2040
    TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
2041
  }
2042

2043
  if (sver >= 2) {
4,011,759✔
2044
    for (int32_t i = 0; i < numOfTopics; ++i) {
4,011,759✔
2045
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
×
2046
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
×
2047
      int32_t len = strlen(topic) + 1;
×
2048
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pTopics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
×
2049
    }
2050
  }
2051

2052
  if (sver >= 3) {
4,011,759✔
2053
    int32_t numOfReadTbs = 0;
4,011,759✔
2054
    int32_t numOfWriteTbs = 0;
4,011,759✔
2055
    int32_t numOfAlterTbs = 0;
4,011,759✔
2056
    int32_t numOfReadViews = 0;
4,011,759✔
2057
    int32_t numOfWriteViews = 0;
4,011,759✔
2058
    int32_t numOfAlterViews = 0;
4,011,759✔
2059
    int32_t numOfUseDbs = 0;
4,011,759✔
2060
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
4,011,759✔
2061
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
4,011,759✔
2062
    if (sver >= 6) {
4,011,759✔
2063
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
4,011,759✔
2064
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
4,011,759✔
2065
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
4,011,759✔
2066
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
4,011,759✔
2067
    }
2068
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
4,011,759✔
2069

2070
    if (numOfReadTbs > 0) {
4,011,759✔
2071
      pUser->legacyPrivs->pReadTbs =
×
2072
          taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2073
      if (pUser->legacyPrivs->pReadTbs == NULL) {
×
2074
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2075
      }
2076
    }
2077
    if (numOfWriteTbs > 0) {
4,011,759✔
2078
      pUser->legacyPrivs->pWriteTbs =
×
2079
          taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2080
      if (pUser->legacyPrivs->pWriteTbs == NULL) {
×
2081
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2082
      }
2083
    }
2084
    if (numOfAlterTbs > 0) {
4,011,759✔
2085
      pUser->legacyPrivs->pAlterTbs =
×
2086
          taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2087
      if (pUser->legacyPrivs->pAlterTbs == NULL) {
×
2088
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2089
      }
2090
    }
2091
    if (numOfReadViews > 0) {
4,011,759✔
2092
      pUser->legacyPrivs->pReadViews =
×
2093
          taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2094
      if (pUser->legacyPrivs->pReadViews == NULL) {
×
2095
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2096
      }
2097
    }
2098
    if (numOfWriteViews > 0) {
4,011,759✔
2099
      pUser->legacyPrivs->pWriteViews =
×
2100
          taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2101
      if (pUser->legacyPrivs->pWriteViews == NULL) {
×
2102
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2103
      }
2104
    }
2105
    if (numOfAlterViews > 0) {
4,011,759✔
2106
      pUser->legacyPrivs->pAlterViews =
×
2107
          taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2108
      if (pUser->legacyPrivs->pAlterViews == NULL) {
×
2109
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2110
      }
2111
    }
2112
    if (numOfUseDbs > 0) {
4,011,759✔
2113
      pUser->legacyPrivs->pUseDbs =
×
2114
          taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2115
      if (pUser->legacyPrivs->pUseDbs == NULL) {
×
2116
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2117
      }
2118
    }
2119

2120
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
4,011,759✔
2121
      int32_t keyLen = 0;
×
2122
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2123

2124
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2125
      if (key == NULL) {
×
2126
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2127
      }
2128
      (void)memset(key, 0, keyLen);
×
2129
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2130

2131
      int32_t valuelen = 0;
×
2132
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2133
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2134
      if (value == NULL) {
×
2135
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2136
      }
2137
      (void)memset(value, 0, valuelen);
×
2138
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2139

2140
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2141
    }
2142

2143
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
4,011,759✔
2144
      int32_t keyLen = 0;
×
2145
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2146

2147
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2148
      if (key == NULL) {
×
2149
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2150
      }
2151
      (void)memset(key, 0, keyLen);
×
2152
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2153

2154
      int32_t valuelen = 0;
×
2155
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2156
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2157
      if (value == NULL) {
×
2158
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2159
      }
2160
      (void)memset(value, 0, valuelen);
×
2161
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2162

2163
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2164
    }
2165

2166
    if (sver >= 6) {
4,011,759✔
2167
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
4,011,759✔
2168
        int32_t keyLen = 0;
×
2169
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2170

2171
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2172
        if (key == NULL) {
×
2173
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2174
        }
2175
        (void)memset(key, 0, keyLen);
×
2176
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2177

2178
        int32_t valuelen = 0;
×
2179
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2180
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2181
        if (value == NULL) {
×
2182
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2183
        }
2184
        (void)memset(value, 0, valuelen);
×
2185
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2186

2187
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pAlterTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2188
      }
2189

2190
      for (int32_t i = 0; i < numOfReadViews; ++i) {
4,011,759✔
2191
        int32_t keyLen = 0;
×
2192
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2193

2194
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2195
        if (key == NULL) {
×
2196
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2197
        }
2198
        (void)memset(key, 0, keyLen);
×
2199
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2200

2201
        int32_t valuelen = 0;
×
2202
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2203
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2204
        if (value == NULL) {
×
2205
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2206
        }
2207
        (void)memset(value, 0, valuelen);
×
2208
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2209

2210
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2211
      }
2212

2213
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
4,011,759✔
2214
        int32_t keyLen = 0;
×
2215
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2216

2217
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2218
        if (key == NULL) {
×
2219
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2220
        }
2221
        (void)memset(key, 0, keyLen);
×
2222
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2223

2224
        int32_t valuelen = 0;
×
2225
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2226
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2227
        if (value == NULL) {
×
2228
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2229
        }
2230
        (void)memset(value, 0, valuelen);
×
2231
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2232

2233
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2234
      }
2235

2236
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
4,011,759✔
2237
        int32_t keyLen = 0;
×
2238
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2239

2240
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2241
        if (key == NULL) {
×
2242
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2243
        }
2244
        (void)memset(key, 0, keyLen);
×
2245
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2246

2247
        int32_t valuelen = 0;
×
2248
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2249
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2250
        if (value == NULL) {
×
2251
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2252
        }
2253
        (void)memset(value, 0, valuelen);
×
2254
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2255

2256
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pAlterViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2257
      }
2258
    }
2259

2260
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
4,011,759✔
2261
      int32_t keyLen = 0;
×
2262
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2263

2264
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2265
      if (key == NULL) {
×
2266
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2267
      }
2268
      (void)memset(key, 0, keyLen);
×
2269
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2270

2271
      int32_t ref = 0;
×
2272
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
×
2273

2274
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pUseDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
×
2275
    }
2276
  }
2277
  // decoder white list
2278
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
4,011,759✔
2279
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
4,011,759✔
2280
      int32_t len = 0;
×
2281
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
2282

2283
      TAOS_MEMORY_REALLOC(key, len);
×
2284
      if (key == NULL) {
×
2285
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2286
      }
2287
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
2288

2289
      SIpWhiteList *pIpWhiteList = NULL;
×
2290
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
2291

2292
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
2293

2294
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
2295
      if (code != 0) {
×
2296
        taosMemoryFreeClear(pIpWhiteList);
×
2297
      }
2298
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2299

2300
      taosMemoryFreeClear(pIpWhiteList);
×
2301

2302
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
4,011,759✔
2303
      int32_t len = 0;
4,011,759✔
2304
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
4,011,759✔
2305

2306
      TAOS_MEMORY_REALLOC(key, len);
4,011,759✔
2307
      if (key == NULL) {
4,011,759✔
2308
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2309
      }
2310
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
4,011,759✔
2311

2312
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual, sver >= USER_VER_SUPPORT_ADVANCED_SECURITY),
4,011,759✔
2313
                      &lino, _OVER);
2314
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
4,011,759✔
2315
    }
2316
  }
2317

2318
  if (pUser->pIpWhiteListDual == NULL) {
4,011,759✔
2319
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
2320
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
2321
  }
2322

2323
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
4,011,759✔
2324

2325
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
4,011,759✔
2326
    memset(pUser->totpsecret, 0, sizeof(pUser->totpsecret));
×
2327
    pUser->changePass = 2;
×
2328

2329
    if (tsEnableAdvancedSecurity) {
×
2330
      pUser->sessionPerUser = pUser->superUser ? -1 : TSDB_USER_SESSION_PER_USER_DEFAULT;
×
2331
      pUser->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
×
2332
      pUser->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
×
2333
      pUser->callPerSession = pUser->superUser ? -1 : TSDB_USER_CALL_PER_SESSION_DEFAULT;
×
2334
      pUser->vnodePerCall = pUser->superUser ? -1 : TSDB_USER_VNODE_PER_CALL_DEFAULT;
×
2335
      pUser->failedLoginAttempts = pUser->superUser ? -1 : TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
×
2336
      pUser->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
×
2337
      pUser->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
×
2338
      pUser->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
×
2339
      pUser->passwordLockTime = pUser->superUser ? 1 : TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
×
2340
      pUser->passwordGraceTime = pUser->superUser ? -1 : TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
×
2341
      pUser->inactiveAccountTime = pUser->superUser ? -1 : TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
×
2342
    } else {
2343
      pUser->sessionPerUser = -1;
×
2344
      pUser->connectTime = -1;
×
2345
      pUser->connectIdleTime = -1;
×
2346
      pUser->callPerSession = -1;
×
2347
      pUser->vnodePerCall = -1;
×
2348
      pUser->failedLoginAttempts = -1;
×
2349
      pUser->passwordLifeTime = -1;
×
2350
      pUser->passwordReuseTime = 0;
×
2351
      pUser->passwordReuseMax = 0;
×
2352
      pUser->passwordLockTime = 1;
×
2353
      pUser->passwordGraceTime = -1;
×
2354
      pUser->inactiveAccountTime =-1;
×
2355
    }
2356

2357
    pUser->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
×
2358
    pUser->tokenNum = 0;
×
2359
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList));
×
2360
    if (pUser->pTimeWhiteList == NULL) {
×
2361
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2362
    }
2363
  } else {
2364
    SDB_GET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
4,011,759✔
2365
    SDB_GET_INT8(pRaw, dataPos, &pUser->changePass, _OVER);
4,011,759✔
2366
    SDB_GET_INT32(pRaw, dataPos, &pUser->sessionPerUser, _OVER);
4,011,759✔
2367
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectTime, _OVER);
4,011,759✔
2368
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectIdleTime, _OVER);
4,011,759✔
2369
    SDB_GET_INT32(pRaw, dataPos, &pUser->callPerSession, _OVER);
4,011,759✔
2370
    SDB_GET_INT32(pRaw, dataPos, &pUser->vnodePerCall, _OVER);
4,011,759✔
2371
    SDB_GET_INT32(pRaw, dataPos, &pUser->failedLoginAttempts, _OVER);
4,011,759✔
2372
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLifeTime, _OVER);
4,011,759✔
2373
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseTime, _OVER);
4,011,759✔
2374
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseMax, _OVER);
4,011,759✔
2375
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLockTime, _OVER);
4,011,759✔
2376
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordGraceTime, _OVER);
4,011,759✔
2377
    SDB_GET_INT32(pRaw, dataPos, &pUser->inactiveAccountTime, _OVER);
4,011,759✔
2378
    SDB_GET_INT32(pRaw, dataPos, &pUser->allowTokenNum, _OVER);
4,011,759✔
2379
    SDB_GET_INT32(pRaw, dataPos, &pUser->tokenNum, _OVER);
4,011,759✔
2380

2381
    int32_t num = 0;
4,011,759✔
2382
    SDB_GET_INT32(pRaw, dataPos, &num, _OVER);
4,011,759✔
2383
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList) + num * sizeof(SDateTimeWhiteListItem));
4,011,759✔
2384
    if (pUser->pTimeWhiteList == NULL) {
4,011,759✔
2385
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2386
    }
2387

2388
    pUser->pTimeWhiteList->num = num;
4,011,759✔
2389
    for (int32_t i = 0; i < num; i++) {
4,015,763✔
2390
      SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
4,004✔
2391
      SDB_GET_BOOL(pRaw, dataPos, &range->absolute, _OVER);
4,004✔
2392
      SDB_GET_BOOL(pRaw, dataPos, &range->neg, _OVER);
4,004✔
2393
      SDB_GET_INT64(pRaw, dataPos, &range->start, _OVER);
4,004✔
2394
      SDB_GET_INT32(pRaw, dataPos, &range->duration, _OVER);
4,004✔
2395
    }
2396

2397
    int32_t extLen = 0;
4,011,759✔
2398
    SDB_GET_INT32(pRaw, dataPos, &extLen, _OVER);
4,011,759✔
2399
    TAOS_MEMORY_REALLOC(key, extLen);
4,011,759✔
2400
    if (key == NULL) {
4,011,759✔
2401
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2402
    }
2403
    SDB_GET_BINARY(pRaw, dataPos, key, extLen, _OVER);
4,011,759✔
2404
    TAOS_CHECK_GOTO(tDeserializeUserObjExt(key, extLen, pUser), &lino, _OVER);
4,011,759✔
2405
    if (pUser->superUser && taosHashGetSize(pUser->roles) == 0) {
4,011,759✔
2406
      upgradeSecurity = 1;
×
2407
    }
2408
  }
2409

2410
#ifndef TD_ENTERPRISE
2411
  // community users cannot modify these fields, and the default values may prevent
2412
  // the user from logging in, so we set them to different values here.
2413
  pUser->sessionPerUser = -1;
2414
  pUser->connectTime = -1;
2415
  pUser->connectIdleTime = -1;
2416
  pUser->callPerSession = -1;
2417
  pUser->vnodePerCall = -1;
2418
  pUser->failedLoginAttempts = -1;
2419
  pUser->passwordLifeTime = -1;
2420
  pUser->passwordReuseTime = 0;
2421
  pUser->passwordReuseMax = 0;
2422
  pUser->passwordLockTime = -1;
2423
  pUser->passwordGraceTime = -1;
2424
  pUser->inactiveAccountTime = -1;
2425
  pUser->allowTokenNum = -1;
2426
  pUser->tokenNum = 0;
2427
#endif  // TD_ENTERPRISE
2428

2429
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
4,011,759✔
2430

2431
  taosInitRWLatch(&pUser->lock);
4,011,759✔
2432
  dropOldPasswords(pUser);
4,011,759✔
2433
_OVER:
4,011,759✔
2434
  taosMemoryFree(key);
4,011,759✔
2435
  taosMemoryFree(value);
4,011,759✔
2436
  if (code < 0) {
4,011,759✔
2437
    terrno = code;
×
2438
    mError("user:%s, failed to decode at line %d from raw:%p since %s", pUser == NULL ? "null" : pUser->user, lino,
×
2439
           pRaw, tstrerror(code));
2440
    if (pUser != NULL) {
×
2441
      mndUserFreeObj(pUser);
×
2442
    }
2443
    taosMemoryFreeClear(pRow);
×
2444
    return NULL;
×
2445
  }
2446

2447
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
4,011,759✔
2448
  return pRow;
4,011,759✔
2449
}
2450

2451
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
658,850✔
2452
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
658,850✔
2453

2454
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
658,850✔
2455
  if (pAcct == NULL) {
658,850✔
2456
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
2457
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
2458
    TAOS_RETURN(terrno);
×
2459
  }
2460
  pUser->acctId = pAcct->acctId;
658,850✔
2461
  sdbRelease(pSdb, pAcct);
658,850✔
2462

2463
  return 0;
658,850✔
2464
}
2465

2466
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
×
2467
  int32_t code = 0;
×
2468
  *ppNew =
×
2469
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2470
  if (*ppNew == NULL) {
×
2471
    TAOS_RETURN(terrno);
×
2472
  }
2473

2474
  char *tb = taosHashIterate(pOld, NULL);
×
2475
  while (tb != NULL) {
×
2476
    size_t keyLen = 0;
×
2477
    char  *key = taosHashGetKey(tb, &keyLen);
×
2478

2479
    int32_t valueLen = strlen(tb) + 1;
×
2480
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
×
2481
      taosHashCancelIterate(pOld, tb);
×
2482
      taosHashCleanup(*ppNew);
×
2483
      TAOS_RETURN(code);
×
2484
    }
2485
    tb = taosHashIterate(pOld, tb);
×
2486
  }
2487

2488
  TAOS_RETURN(code);
×
2489
}
2490

2491
int32_t mndDupRoleHash(SHashObj *pOld, SHashObj **ppNew) {
3,198,965✔
2492
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
3,198,965✔
2493
                              HASH_ENTRY_LOCK))) {
2494
    TAOS_RETURN(terrno);
×
2495
  }
2496

2497
  int32_t  code = 0;
3,198,965✔
2498
  uint8_t *flag = NULL;
3,198,965✔
2499
  while ((flag = taosHashIterate(pOld, flag))) {
9,383,477✔
2500
    size_t keyLen = 0;
6,184,512✔
2501
    char  *key = taosHashGetKey(flag, &keyLen);
6,184,512✔
2502

2503
    if ((code = taosHashPut(*ppNew, key, keyLen, flag, sizeof(*flag))) != 0) {
6,184,512✔
2504
      taosHashCancelIterate(pOld, flag);
×
2505
      taosHashCleanup(*ppNew);
×
2506
      TAOS_RETURN(code);
×
2507
    }
2508
  }
2509

2510
  TAOS_RETURN(code);
3,198,965✔
2511
}
2512

2513
int32_t mndMergePrivObjHash(SHashObj *pOld, SHashObj **ppNew) {
107,078,379✔
2514
  if (!(*ppNew)) return mndDupPrivObjHash(pOld, ppNew);
107,078,379✔
2515

2516
  int32_t           code = 0, lino = 0;
107,078,379✔
2517
  SHashObj         *pNew = *ppNew;
107,078,379✔
2518
  SPrivObjPolicies *policies = NULL;
107,077,903✔
2519
  while ((policies = taosHashIterate(pOld, policies))) {
1,107,833,295✔
2520
    size_t klen = 0;
1,000,757,539✔
2521
    char  *key = taosHashGetKey(policies, &klen);
1,000,757,539✔
2522
    size_t vlen = taosHashGetValueSize(policies);
1,000,757,226✔
2523

2524
    SPrivObjPolicies *pNewPolicies = taosHashGet(pNew, key, klen);
1,000,757,007✔
2525
    if (pNewPolicies) {
1,000,757,152✔
2526
      size_t newVlen = taosHashGetValueSize(pNewPolicies);
637,627,994✔
2527
      if (newVlen > 0 && vlen > 0) {
637,627,619✔
2528
        privAddSet(&pNewPolicies->policy, &policies->policy);
637,627,994✔
2529
      }
2530
      continue;
637,627,139✔
2531
    }
2532

2533
    if ((code = taosHashPut(pNew, key, klen, vlen ? policies : NULL, vlen)) != 0) {
363,129,158✔
UNCOV
2534
      taosHashCancelIterate(pOld, policies);
×
2535
      taosHashCleanup(pNew);
×
2536
      *ppNew = NULL;
×
2537
      TAOS_RETURN(code);
×
2538
    }
2539
  }
2540

2541
  TAOS_RETURN(code);
107,075,966✔
2542
}
2543

2544
/**
2545
 * 1. Prefer to use SPrivTblPolicies from user object(the updateUs of policy in user object is INT64_MAX)
2546
 * 2. If two or more roles have SPrivTblPolicies, the policy with latest update time take effect.
2547
 */
2548
int32_t mndMergePrivTblHash(SHashObj *pOld, SHashObj **ppNew, bool updateWithLatest) {
321,232,197✔
2549
  if (!(*ppNew)) return mndDupPrivTblHash(pOld, ppNew, false);
321,232,197✔
2550

2551
  int32_t           code = 0, lino = 0;
321,233,069✔
2552
  SHashObj         *pNew = *ppNew;
321,233,069✔
2553
  SPrivTblPolicies *policies = NULL;
321,233,069✔
2554
  while ((policies = taosHashIterate(pOld, policies))) {
321,233,753✔
2555
    size_t klen = 0;
684✔
2556
    char  *key = taosHashGetKey(policies, &klen);
684✔
2557
    size_t vlen = taosHashGetValueSize(policies);
684✔
2558

2559
    SPrivTblPolicies *pNewPolicies = taosHashGet(pNew, key, klen);
684✔
2560
    if (pNewPolicies) {
684✔
2561
      size_t newVlen = taosHashGetValueSize(pNewPolicies);
684✔
2562
      if (newVlen > 0 && vlen > 0) {
684✔
2563
        TAOS_CHECK_EXIT(privTblPoliciesMerge(pNewPolicies, policies, updateWithLatest));
684✔
2564
      }
2565
      continue;
684✔
2566
    }
2567

2568
    SPrivTblPolicies tmpPolicies = {0};
×
2569
    if (vlen > 0) {
×
2570
      if ((code = privTblPoliciesMerge(&tmpPolicies, policies, updateWithLatest))) {
×
2571
        privTblPoliciesFree(&tmpPolicies);
2572
        goto _exit;
×
2573
      }
2574
    }
2575
    if ((code = taosHashPut(pNew, key, klen, vlen ? &tmpPolicies : NULL, vlen)) != 0) {
×
2576
      privTblPoliciesFree(&tmpPolicies);
2577
      taosHashCancelIterate(pOld, policies);
×
2578
      taosHashCleanup(pNew);
×
2579
      *ppNew = NULL;
×
2580
      TAOS_RETURN(code);
×
2581
    }
2582
  }
2583

2584
_exit:
321,233,025✔
2585
  TAOS_RETURN(code);
321,233,025✔
2586
}
2587

2588
int32_t mndDupKVHash(SHashObj *pOld, SHashObj **ppNew) {
39,447,689✔
2589
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
39,447,689✔
2590
                              HASH_ENTRY_LOCK))) {
2591
    TAOS_RETURN(terrno);
×
2592
  }
2593
  int32_t code = 0;
39,447,689✔
2594
  void   *val = NULL;
39,447,689✔
2595
  while ((val = taosHashIterate(pOld, val))) {
129,310,850✔
2596
    size_t klen = 0;
89,863,161✔
2597
    char  *key = taosHashGetKey(val, &klen);
89,863,161✔
2598
    size_t vlen = taosHashGetValueSize(val);
89,862,969✔
2599
    if ((code = taosHashPut(*ppNew, key, klen, vlen > 0 ? val : NULL, vlen)) != 0) {
89,862,969✔
2600
      taosHashCancelIterate(pOld, val);
×
2601
      taosHashCleanup(*ppNew);
×
2602
      TAOS_RETURN(code);
×
2603
    }
2604
  }
2605

2606
  TAOS_RETURN(code);
39,447,645✔
2607
}
2608

2609
int32_t mndDupPrivObjHash(SHashObj *pOld, SHashObj **ppNew) {
39,454,220✔
2610
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
39,454,220✔
2611
                              HASH_ENTRY_LOCK))) {
2612
    TAOS_RETURN(terrno);
×
2613
  }
2614
  int32_t           code = 0;
39,454,386✔
2615
  SPrivObjPolicies *policies = NULL;
39,454,386✔
2616
  while ((policies = taosHashIterate(pOld, policies))) {
94,440,150✔
2617
    size_t klen = 0;
54,985,764✔
2618
    char  *key = taosHashGetKey(policies, &klen);
54,985,764✔
2619
    size_t vlen = taosHashGetValueSize(policies);
54,985,593✔
2620

2621
    if ((code = taosHashPut(*ppNew, key, klen, vlen > 0 ? policies : NULL, vlen)) != 0) {
54,985,764✔
2622
      taosHashCancelIterate(pOld, policies);
×
2623
      taosHashCleanup(*ppNew);
×
2624
      TAOS_RETURN(code);
×
2625
    }
2626
  }
2627

2628
  TAOS_RETURN(code);
39,455,047✔
2629
}
2630

2631
int32_t mndDupPrivTblHash(SHashObj *pOld, SHashObj **ppNew, bool setUpdateTimeMax) {
121,555,215✔
2632
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
121,555,215✔
2633
                              HASH_ENTRY_LOCK))) {
2634
    TAOS_RETURN(terrno);
×
2635
  }
2636
  taosHashSetFreeFp(*ppNew, privTblPoliciesFree);
121,556,737✔
2637

2638
  int32_t           code = 0, lino = 0;
121,556,060✔
2639
  SPrivTblPolicies *policies = NULL, *pTmpPolicies = NULL;
121,556,060✔
2640
  SPrivTblPolicies  tmpPolicies = {0};
121,556,060✔
2641
  while ((policies = taosHashIterate(pOld, policies))) {
122,148,836✔
2642
    size_t klen = 0;
592,776✔
2643
    char  *key = taosHashGetKey(policies, &klen);
592,776✔
2644
    size_t vlen = taosHashGetValueSize(policies);
592,776✔
2645
    memset(&tmpPolicies, 0, sizeof(tmpPolicies));
592,776✔
2646
    pTmpPolicies = &tmpPolicies;
592,776✔
2647
    if (vlen > 0) {
592,776✔
2648
      TAOS_CHECK_EXIT(privTblPoliciesAdd(&tmpPolicies, policies, true, setUpdateTimeMax));
592,776✔
2649
    }
2650
    TAOS_CHECK_EXIT(taosHashPut(*ppNew, key, klen, vlen > 0 ? &tmpPolicies : NULL, vlen));
592,776✔
2651
    pTmpPolicies = NULL;
592,776✔
2652
  }
2653

2654
_exit:
121,556,577✔
2655
  if (code != 0) {
121,556,577✔
2656
    if (!pTmpPolicies) {
×
2657
      privTblPoliciesFree(pTmpPolicies);
2658
      pTmpPolicies = NULL;
×
2659
    }
2660
    if (policies) taosHashCancelIterate(pOld, policies);
×
2661
    taosHashCleanup(*ppNew);
×
2662
    *ppNew = NULL;
×
2663
  }
2664
  TAOS_RETURN(code);
121,556,577✔
2665
}
2666

2667
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
3,184,249✔
2668
  int32_t code = 0;
3,184,249✔
2669
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
3,184,249✔
2670
  pNew->authVersion++;
3,184,249✔
2671
  pNew->updateTime = taosGetTimestampMs();
3,184,249✔
2672
  taosInitRWLatch(&pNew->lock);
3,184,249✔
2673

2674
  pNew->passwords = NULL;
3,184,249✔
2675
  pNew->pIpWhiteListDual = NULL;
3,184,249✔
2676
  pNew->passwords = NULL;
3,184,249✔
2677
  pNew->objPrivs = NULL;
3,184,249✔
2678
  pNew->selectTbs = NULL;
3,184,249✔
2679
  pNew->insertTbs = NULL;
3,184,249✔
2680
  pNew->updateTbs = NULL;
3,184,249✔
2681
  pNew->deleteTbs = NULL;
3,184,249✔
2682
  pNew->ownedDbs = NULL;
3,184,249✔
2683
  pNew->pTimeWhiteList = NULL;
3,184,249✔
2684
  pNew->roles = NULL;
3,184,249✔
2685
  pNew->legacyPrivs = NULL;
3,184,249✔
2686

2687
  taosRLockLatch(&pUser->lock);
3,184,249✔
2688
  pNew->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
3,184,249✔
2689
  if (pNew->passwords == NULL) {
3,184,249✔
2690
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2691
    goto _OVER;
×
2692
  }
2693
  (void)memcpy(pNew->passwords, pUser->passwords, pUser->numOfPasswords * sizeof(SUserPassword));
3,184,249✔
2694
  TAOS_CHECK_GOTO(mndDupKVHash(pUser->ownedDbs, &pNew->ownedDbs), NULL, _OVER);
3,184,249✔
2695
  TAOS_CHECK_GOTO(mndDupPrivObjHash(pUser->objPrivs, &pNew->objPrivs), NULL, _OVER);
3,184,249✔
2696
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->selectTbs, &pNew->selectTbs, false), NULL, _OVER);
3,184,249✔
2697
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->insertTbs, &pNew->insertTbs, false), NULL, _OVER);
3,184,249✔
2698
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->updateTbs, &pNew->updateTbs, false), NULL, _OVER);
3,184,249✔
2699
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->deleteTbs, &pNew->deleteTbs, false), NULL, _OVER);
3,184,249✔
2700
  TAOS_CHECK_GOTO(mndDupRoleHash(pUser->roles, &pNew->roles), NULL, _OVER);
3,184,249✔
2701
  if(pUser->legacyPrivs) {
3,184,249✔
2702
    pNew->legacyPrivs = taosMemCalloc(1, sizeof(SPrivHashObjSet));
×
2703
    if (pNew->legacyPrivs == NULL) {
×
2704
      code = TSDB_CODE_OUT_OF_MEMORY;
×
2705
      goto _OVER;
×
2706
    }
2707
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadDbs, &pNew->legacyPrivs->pReadDbs), NULL, _OVER);
×
2708
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteDbs, &pNew->legacyPrivs->pWriteDbs), NULL, _OVER);
×
2709
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadTbs, &pNew->legacyPrivs->pReadTbs), NULL, _OVER);
×
2710
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteTbs, &pNew->legacyPrivs->pWriteTbs), NULL, _OVER);
×
2711
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pTopics, &pNew->legacyPrivs->pTopics), NULL, _OVER);
×
2712
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pAlterTbs, &pNew->legacyPrivs->pAlterTbs), NULL, _OVER);
×
2713
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadViews, &pNew->legacyPrivs->pReadViews), NULL, _OVER);
×
2714
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteViews, &pNew->legacyPrivs->pWriteViews), NULL, _OVER);
×
2715
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pAlterViews, &pNew->legacyPrivs->pAlterViews), NULL, _OVER);
×
2716
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pUseDbs, &pNew->legacyPrivs->pUseDbs), NULL, _OVER);
×
2717
  }
2718
  pNew->pIpWhiteListDual = cloneIpWhiteList(pUser->pIpWhiteListDual);
3,184,249✔
2719
  if (pNew->pIpWhiteListDual == NULL) {
3,184,249✔
2720
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2721
    goto _OVER;
×
2722
  }
2723

2724
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
3,184,249✔
2725
  if (pNew->pTimeWhiteList == NULL) {
3,184,249✔
2726
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2727
    goto _OVER;
×
2728
  }
2729

2730
_OVER:
3,184,249✔
2731
  taosRUnLockLatch(&pUser->lock);
3,184,249✔
2732
  if (code == 0) {
3,184,249✔
2733
    dropOldPasswords(pNew);
3,184,249✔
2734
  }
2735
  TAOS_RETURN(code);
3,184,249✔
2736
}
2737

2738
void mndUserFreeObj(SUserObj *pUser) {
7,917,226✔
2739
  taosHashCleanup(pUser->ownedDbs);
7,917,226✔
2740
  taosHashCleanup(pUser->objPrivs);
7,917,226✔
2741
  taosHashCleanup(pUser->selectTbs);
7,917,226✔
2742
  taosHashCleanup(pUser->insertTbs);
7,917,226✔
2743
  taosHashCleanup(pUser->updateTbs);
7,917,226✔
2744
  taosHashCleanup(pUser->deleteTbs);
7,917,226✔
2745
  taosHashCleanup(pUser->roles);
7,917,226✔
2746
  taosMemoryFreeClear(pUser->passwords);
7,917,226✔
2747
  taosMemoryFreeClear(pUser->pIpWhiteListDual);
7,917,226✔
2748
  taosMemoryFreeClear(pUser->pTimeWhiteList);
7,917,226✔
2749
  pUser->ownedDbs = NULL;
7,917,226✔
2750
  pUser->objPrivs = NULL;
7,917,226✔
2751
  pUser->selectTbs = NULL;
7,917,226✔
2752
  pUser->insertTbs = NULL;
7,917,226✔
2753
  pUser->updateTbs = NULL;
7,917,226✔
2754
  pUser->deleteTbs = NULL;
7,917,226✔
2755
  pUser->roles = NULL;
7,917,226✔
2756
  if (pUser->legacyPrivs) {
7,917,226✔
2757
    taosHashCleanup(pUser->legacyPrivs->pReadDbs);
×
2758
    taosHashCleanup(pUser->legacyPrivs->pWriteDbs);
×
2759
    taosHashCleanup(pUser->legacyPrivs->pReadTbs);
×
2760
    taosHashCleanup(pUser->legacyPrivs->pWriteTbs);
×
2761
    taosHashCleanup(pUser->legacyPrivs->pTopics);
×
2762
    taosHashCleanup(pUser->legacyPrivs->pAlterTbs);
×
2763
    taosHashCleanup(pUser->legacyPrivs->pReadViews);
×
2764
    taosHashCleanup(pUser->legacyPrivs->pWriteViews);
×
2765
    taosHashCleanup(pUser->legacyPrivs->pAlterViews);
×
2766
    taosHashCleanup(pUser->legacyPrivs->pUseDbs);
×
2767
    taosMemoryFreeClear(pUser->legacyPrivs);
×
2768
  }
2769
}
7,917,226✔
2770

2771
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
4,011,711✔
2772
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
4,011,711✔
2773
  mndUserFreeObj(pUser);
4,011,711✔
2774
  return 0;
4,011,711✔
2775
}
2776

2777
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
3,278,699✔
2778
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
3,278,699✔
2779
  taosWLockLatch(&pOld->lock);
3,278,699✔
2780
  pOld->updateTime = pNew->updateTime;
3,278,699✔
2781
  pOld->authVersion = pNew->authVersion;
3,278,699✔
2782
  pOld->passVersion = pNew->passVersion;
3,278,699✔
2783
  pOld->sysInfo = pNew->sysInfo;
3,278,699✔
2784
  pOld->enable = pNew->enable;
3,278,699✔
2785
  pOld->flag = pNew->flag;
3,278,699✔
2786
  pOld->changePass = pNew->changePass;
3,278,699✔
2787
  pOld->uid = pNew->uid;
3,278,699✔
2788

2789
  pOld->sessionPerUser = pNew->sessionPerUser;
3,278,699✔
2790
  pOld->connectTime = pNew->connectTime;
3,278,699✔
2791
  pOld->connectIdleTime = pNew->connectIdleTime;
3,278,699✔
2792
  pOld->callPerSession = pNew->callPerSession;
3,278,699✔
2793
  pOld->vnodePerCall = pNew->vnodePerCall;
3,278,699✔
2794
  pOld->failedLoginAttempts = pNew->failedLoginAttempts;
3,278,699✔
2795
  pOld->passwordLifeTime = pNew->passwordLifeTime;
3,278,699✔
2796
  pOld->passwordReuseTime = pNew->passwordReuseTime;
3,278,699✔
2797
  pOld->passwordReuseMax = pNew->passwordReuseMax;
3,278,699✔
2798
  pOld->passwordLockTime = pNew->passwordLockTime;
3,278,699✔
2799
  pOld->passwordGraceTime = pNew->passwordGraceTime;
3,278,699✔
2800
  pOld->inactiveAccountTime = pNew->inactiveAccountTime;
3,278,699✔
2801
  pOld->allowTokenNum = pNew->allowTokenNum;
3,278,699✔
2802
  pOld->tokenNum = pNew->tokenNum;
3,278,699✔
2803

2804
  pOld->numOfPasswords = pNew->numOfPasswords;
3,278,699✔
2805
  TSWAP(pOld->passwords, pNew->passwords);
3,278,699✔
2806
  (void)memcpy(pOld->salt, pNew->salt, sizeof(pOld->salt));
3,278,699✔
2807
  (void)memcpy(pOld->totpsecret, pNew->totpsecret, sizeof(pOld->totpsecret));
3,278,699✔
2808
  pOld->sysPrivs = pNew->sysPrivs;
3,278,699✔
2809
  TSWAP(pOld->ownedDbs, pNew->ownedDbs);
3,278,699✔
2810
  TSWAP(pOld->objPrivs, pNew->objPrivs);
3,278,699✔
2811
  TSWAP(pOld->selectTbs, pNew->selectTbs);
3,278,699✔
2812
  TSWAP(pOld->insertTbs, pNew->insertTbs);
3,278,699✔
2813
  TSWAP(pOld->updateTbs, pNew->updateTbs);
3,278,699✔
2814
  TSWAP(pOld->deleteTbs, pNew->deleteTbs);
3,278,699✔
2815
  TSWAP(pOld->roles, pNew->roles);
3,278,699✔
2816
  TSWAP(pOld->legacyPrivs, pNew->legacyPrivs);
3,278,699✔
2817

2818
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
3,278,699✔
2819
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
3,278,699✔
2820
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
3,278,699✔
2821
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
3,278,699✔
2822
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
3,278,699✔
2823

2824
  taosWUnLockLatch(&pOld->lock);
3,278,699✔
2825

2826
  return 0;
3,278,699✔
2827
}
2828

2829
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
124,574,892✔
2830
  int32_t code = 0;
124,574,892✔
2831
  SSdb   *pSdb = pMnode->pSdb;
124,574,892✔
2832

2833
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
124,574,913✔
2834
  if (*ppUser == NULL) {
124,575,300✔
2835
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
138,683✔
2836
      code = TSDB_CODE_MND_USER_NOT_EXIST;
138,683✔
2837
    } else {
2838
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2839
    }
2840
  }
2841
  TAOS_RETURN(code);
124,575,373✔
2842
}
2843

2844
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
188,968,569✔
2845
  SSdb *pSdb = pMnode->pSdb;
188,968,569✔
2846
  sdbRelease(pSdb, pUser);
188,968,569✔
2847
}
188,969,161✔
2848

2849
int32_t mndAcquireUserById(SMnode *pMnode, int64_t userId, SUserObj **ppUser) {
×
2850
  void     *pIter = NULL;
×
2851
  SUserObj *pObj;
×
2852
  SSdb     *pSdb = pMnode->pSdb;
×
2853
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj))) {
×
2854
    if (pObj->uid == userId) {
×
2855
      return mndAcquireUser(pMnode, pObj->user, ppUser);
×
2856
    }
2857
  }
2858
  return 0;
×
2859
}
2860

2861
int32_t mndBuildUidNamesHash(SMnode *pMnode, SSHashObj **ppHash) {
626,859✔
2862
  int32_t    code = 0;
626,859✔
2863
  void      *pIter = NULL;
626,859✔
2864
  SUserObj  *pObj;
624,681✔
2865
  SSHashObj *pHash = NULL;
626,859✔
2866

2867
  int32_t nUser = sdbGetSize(pMnode->pSdb, SDB_USER);
626,859✔
2868

2869
  pHash = tSimpleHashInit(nUser, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
626,859✔
2870
  if (pHash == NULL) {
626,859✔
2871
    TAOS_RETURN(terrno);
×
2872
  }
2873

2874
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_USER, pIter, (void **)&pObj))) {
1,520,714✔
2875
    code = tSimpleHashPut(pHash, &pObj->uid, sizeof(pObj->uid), pObj->name, strlen(pObj->name) + 1);
893,855✔
2876
    if (code != 0) {
893,855✔
2877
      sdbRelease(pMnode->pSdb, pObj);
×
2878
      sdbCancelFetch(pMnode->pSdb, pIter);
×
2879
      tSimpleHashCleanup(pHash);
×
2880
      TAOS_RETURN(code);
×
2881
    }
2882
    sdbRelease(pMnode->pSdb, pObj);
893,855✔
2883
  }
2884

2885
  *ppHash = pHash;
626,859✔
2886
  TAOS_RETURN(code);
626,859✔
2887
}
2888

2889
int32_t mndEncryptPass(char *pass, const char *salt, int8_t *algo) {
5,573✔
2890
  int32_t code = 0;
5,573✔
2891

2892
  if (salt[0] != 0) {
5,573✔
2893
    char passAndSalt[TSDB_PASSWORD_LEN - 1 + TSDB_PASSWORD_SALT_LEN];
5,533✔
2894
    (void)memcpy(passAndSalt, pass, TSDB_PASSWORD_LEN - 1);
5,573✔
2895
    (void)memcpy(passAndSalt + TSDB_PASSWORD_LEN - 1, salt, TSDB_PASSWORD_SALT_LEN);
5,573✔
2896
    taosEncryptPass_c((uint8_t *)passAndSalt, sizeof(passAndSalt), pass);
2897
  }
2898

2899
  unsigned char packetData[TSDB_PASSWORD_LEN] = {0};
5,573✔
2900
  SCryptOpts    opts = {0};
5,573✔
2901
  opts.len = TSDB_PASSWORD_LEN;
5,573✔
2902
  opts.source = pass;
5,573✔
2903
  opts.result = packetData;
5,573✔
2904
  opts.unitLen = TSDB_PASSWORD_LEN;
5,573✔
2905
  tstrncpy(opts.key, tsDataKey, ENCRYPT_KEY_LEN + 1);
5,573✔
2906
  int newLen = Builtin_CBC_Encrypt(&opts);
5,573✔
2907
  if (newLen <= 0) return terrno;
5,573✔
2908

2909
  memcpy(pass, packetData, newLen);
5,573✔
2910
  if (algo != NULL) {
5,573✔
2911
    *algo = DND_CA_SM4;
2,828✔
2912
  }
2913

2914
  return TSDB_CODE_SUCCESS;
5,573✔
2915
}
2916

2917
static void generateSalt(char *salt, size_t len) {
113,169✔
2918
  const char *set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
113,169✔
2919
  int32_t     setLen = 62;
113,169✔
2920
  for (int32_t i = 0; i < len - 1; ++i) {
3,621,408✔
2921
    salt[i] = set[taosSafeRand() % setLen];
3,508,239✔
2922
  }
2923
  salt[len - 1] = 0;
113,169✔
2924
}
113,169✔
2925

2926
static int32_t addDefaultIpToTable(SHashObj *pUniqueTab) {
1,512✔
2927
  int32_t code = 0;
1,512✔
2928
  int32_t lino = 0;
1,512✔
2929
  int32_t dummy = 0;
1,512✔
2930

2931
  SIpRange ipv4 = {0}, ipv6 = {0};
1,512✔
2932
  code = createDefaultIp4Range(&ipv4);
1,512✔
2933
  TSDB_CHECK_CODE(code, lino, _error);
1,512✔
2934

2935
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummy, sizeof(dummy));
1,512✔
2936
  TSDB_CHECK_CODE(code, lino, _error);
1,512✔
2937

2938
  code = createDefaultIp6Range(&ipv6);
1,512✔
2939
  TSDB_CHECK_CODE(code, lino, _error);
1,512✔
2940

2941
  code = taosHashPut(pUniqueTab, &ipv6, sizeof(ipv6), &dummy, sizeof(dummy));
1,512✔
2942
  TSDB_CHECK_CODE(code, lino, _error);
1,512✔
2943
    
2944
_error:
1,512✔
2945
  if (code != 0) {
1,512✔
2946
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
2947
  }
2948
  return code;
1,512✔
2949
}
2950

2951
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
110,542✔
2952
  int32_t  code = 0;
110,542✔
2953
  int32_t  lino = 0;
110,542✔
2954
  SUserObj userObj = {0};
110,542✔
2955

2956
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
110,542✔
2957
  if (userObj.passwords == NULL) {
110,542✔
2958
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2959
  }
2960
  userObj.numOfPasswords = 1;
110,542✔
2961

2962
  if (pCreate->isImport == 1) {
110,542✔
2963
    memset(userObj.salt, 0, sizeof(userObj.salt));
×
2964
    memcpy(userObj.passwords[0].pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
2965
  } else {
2966
    generateSalt(userObj.salt, sizeof(userObj.salt));
110,542✔
2967
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.passwords[0].pass);
110,542✔
2968
    userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
110,542✔
2969
    if (strlen(tsDataKey) > 0) {
110,542✔
2970
      TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino,
423✔
2971
                      _OVER);
2972
    }
2973
  }
2974
  userObj.passwords[0].setTime = taosGetTimestampSec();
110,542✔
2975

2976
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
110,542✔
2977
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
110,542✔
2978
  if (pCreate->totpseed[0] != 0) {
110,542✔
2979
    int len = taosGenerateTotpSecret(pCreate->totpseed, 0, userObj.totpsecret, sizeof(userObj.totpsecret));
×
2980
    if (len < 0) {
×
2981
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
2982
    }
2983
  }
2984

2985
  userObj.createdTime = taosGetTimestampMs();
110,542✔
2986
  userObj.updateTime = userObj.createdTime;
110,542✔
2987
  userObj.superUser = 0;  // pCreate->superUser;
110,542✔
2988
  userObj.sysInfo = pCreate->sysInfo;
110,542✔
2989
  userObj.enable = pCreate->enable;
110,542✔
2990
  userObj.createdb = pCreate->createDb;
110,542✔
2991
  userObj.uid = mndGenerateUid(userObj.user, strlen(userObj.user));
110,542✔
2992
  userObj.minSecLevel = (uint8_t)pCreate->minSecLevel;
110,542✔
2993
  userObj.maxSecLevel = (uint8_t)pCreate->maxSecLevel;
110,542✔
2994

2995
  if (userObj.createdb == 1) {
110,542✔
2996
    privAddType(&userObj.sysPrivs, PRIV_DB_CREATE);
2997
  }
2998

2999
#ifdef TD_ENTERPRISE
3000

3001
  userObj.changePass = pCreate->changepass;
110,542✔
3002
  userObj.sessionPerUser = pCreate->sessionPerUser;
110,542✔
3003
  userObj.connectTime = pCreate->connectTime;
110,542✔
3004
  userObj.connectIdleTime = pCreate->connectIdleTime;
110,542✔
3005
  userObj.callPerSession = pCreate->callPerSession;
110,542✔
3006
  userObj.vnodePerCall = pCreate->vnodePerCall;
110,542✔
3007
  userObj.failedLoginAttempts = pCreate->failedLoginAttempts;
110,542✔
3008
  userObj.passwordLifeTime = pCreate->passwordLifeTime;
110,542✔
3009
  userObj.passwordReuseTime = pCreate->passwordReuseTime;
110,542✔
3010
  userObj.passwordReuseMax = pCreate->passwordReuseMax;
110,542✔
3011
  userObj.passwordLockTime = pCreate->passwordLockTime;
110,542✔
3012
  userObj.passwordGraceTime = pCreate->passwordGraceTime;
110,542✔
3013
  userObj.inactiveAccountTime = pCreate->inactiveAccountTime;
110,542✔
3014
  userObj.allowTokenNum = pCreate->allowTokenNum;
110,542✔
3015
  userObj.tokenNum = 0;
110,542✔
3016

3017
  if (pCreate->numIpRanges == 0) {
110,542✔
3018
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
108,484✔
3019
  } else {
3020
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
2,058✔
3021
    if (pUniqueTab == NULL) {
2,058✔
3022
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3023
    }
3024
    
3025
    bool hasPositive = false;
2,058✔
3026
    for (int i = 0; i < pCreate->numIpRanges; i++) {
5,072✔
3027
      SIpRange range = {0};
3,014✔
3028
      copyIpRange(&range, pCreate->pIpDualRanges + i);
3,014✔
3029
      hasPositive = hasPositive || !range.neg;
3,014✔
3030
      int32_t dummy = 0;
3,014✔
3031
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummy, sizeof(dummy))) != 0) {
3,014✔
3032
        taosHashCleanup(pUniqueTab);
×
3033
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3034
      }
3035
    }
3036

3037
    // add local ip if there is any positive range
3038
    if (hasPositive) {
2,058✔
3039
      code = addDefaultIpToTable(pUniqueTab);
1,512✔
3040
      if (code != 0) {
1,512✔
3041
        taosHashCleanup(pUniqueTab);
×
3042
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3043
      }
3044
    }
3045

3046
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
2,058✔
3047
      taosHashCleanup(pUniqueTab);
×
3048
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
3049
    }
3050

3051
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
2,058✔
3052
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
2,058✔
3053
    if (p == NULL) {
2,058✔
3054
      taosHashCleanup(pUniqueTab);
×
3055
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3056
    }
3057

3058
    void *pIter = taosHashIterate(pUniqueTab, NULL);
2,058✔
3059
    for (int32_t i = 0; i < numOfRanges; i++) {
7,680✔
3060
      size_t    len = 0;
5,622✔
3061
      SIpRange *key = taosHashGetKey(pIter, &len);
5,622✔
3062
      memcpy(&p->pIpRanges[i], key, sizeof(SIpRange));
5,622✔
3063
      pIter = taosHashIterate(pUniqueTab, pIter);
5,622✔
3064
    }
3065

3066
    taosHashCleanup(pUniqueTab);
2,058✔
3067
    p->num = numOfRanges;
2,058✔
3068
    sortIpWhiteList(p);
2,058✔
3069
    userObj.pIpWhiteListDual = p;
2,058✔
3070
  }
3071

3072
  if (pCreate->numTimeRanges == 0) {
110,542✔
3073
    userObj.pTimeWhiteList = (SDateTimeWhiteList *)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
108,176✔
3074
    if (userObj.pTimeWhiteList == NULL) {
108,176✔
3075
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3076
    }
3077
  } else {
3078
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
2,366✔
3079
    if (pUniqueTab == NULL) {
2,366✔
3080
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3081
    }
3082

3083
    for (int i = 0; i < pCreate->numTimeRanges; i++) {
5,278✔
3084
      SDateTimeRange        *src = pCreate->pTimeRanges + i;
2,912✔
3085
      SDateTimeWhiteListItem range = {0};
2,912✔
3086
      DateTimeRangeToWhiteListItem(&range, src);
2,912✔
3087

3088
      // no need to add expired range
3089
      if (isDateTimeWhiteListItemExpired(&range)) {
2,912✔
3090
        continue;
364✔
3091
      }
3092

3093
      int32_t dummy = 0;
2,548✔
3094
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummy, sizeof(dummy))) != 0) {
2,548✔
3095
        taosHashCleanup(pUniqueTab);
×
3096
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3097
      }
3098
    }
3099

3100
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_TIME_RANGE) {
2,366✔
3101
      taosHashCleanup(pUniqueTab);
×
3102
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
3103
    }
3104

3105
    int32_t             numOfRanges = taosHashGetSize(pUniqueTab);
2,366✔
3106
    SDateTimeWhiteList *p =
4,732✔
3107
        taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
2,366✔
3108
    if (p == NULL) {
2,366✔
3109
      taosHashCleanup(pUniqueTab);
×
3110
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3111
    }
3112

3113
    void *pIter = taosHashIterate(pUniqueTab, NULL);
2,366✔
3114
    for (int32_t i = 0; i < numOfRanges; i++) {
4,914✔
3115
      size_t                  len = 0;
2,548✔
3116
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
2,548✔
3117
      memcpy(p->ranges + i, key, sizeof(SDateTimeWhiteListItem));
2,548✔
3118
      pIter = taosHashIterate(pUniqueTab, pIter);
2,548✔
3119
    }
3120

3121
    taosHashCleanup(pUniqueTab);
2,366✔
3122
    p->num = numOfRanges;
2,366✔
3123
    sortTimeWhiteList(p);
2,366✔
3124
    userObj.pTimeWhiteList = p;
2,366✔
3125
  }
3126

3127
  userObj.ipWhiteListVer = taosGetTimestampMs();
110,542✔
3128
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
110,542✔
3129
  
3130

3131
#else  // TD_ENTERPRISE
3132

3133
  userObj.changePass = 1;
3134
  userObj.sessionPerUser = -1;
3135
  userObj.connectTime = -1;
3136
  userObj.connectIdleTime = -1;
3137
  userObj.callPerSession = -1;
3138
  userObj.vnodePerCall = -1;
3139
  userObj.failedLoginAttempts = -1;
3140
  userObj.passwordLifeTime = -1;
3141
  userObj.passwordReuseTime = 0;
3142
  userObj.passwordReuseMax = 0;
3143
  userObj.passwordLockTime = -1;
3144
  userObj.passwordGraceTime = -1;
3145
  userObj.inactiveAccountTime = -1;
3146
  userObj.allowTokenNum = -1;
3147
  userObj.tokenNum = 0;
3148

3149
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
3150
  userObj.pTimeWhiteList = (SDateTimeWhiteList *)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
3151
  if (userObj.pTimeWhiteList == NULL) {
3152
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
3153
  }
3154

3155
  userObj.ipWhiteListVer = 0;
3156
  userObj.timeWhiteListVer = 0;
3157

3158
#endif  // TD_ENTERPRISE
3159

3160
  userObj.roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
110,542✔
3161
  if (userObj.roles == NULL) {
110,542✔
3162
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3163
  }
3164

3165
  uint8_t flag = 0x01;
110,542✔
3166
  if ((code = taosHashPut(userObj.roles, TSDB_ROLE_DEFAULT, sizeof(TSDB_ROLE_DEFAULT), &flag, sizeof(flag))) != 0) {
110,542✔
3167
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3168
  }
3169

3170
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "create-user");
110,542✔
3171
  if (pTrans == NULL) {
110,542✔
3172
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
3173
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3174
  }
3175
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
110,542✔
3176

3177
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
110,542✔
3178
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
110,542✔
3179
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
3180
    mndTransDrop(pTrans);
×
3181
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3182
  }
3183
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
110,542✔
3184

3185
  if (mndTransPrepare(pMnode, pTrans) != 0) {
110,542✔
3186
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3187
    mndTransDrop(pTrans);
×
3188
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3189
  }
3190

3191
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
110,542✔
3192
    mndTransDrop(pTrans);
×
3193
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3194
  }
3195

3196
  if (taosHashGet(userObj.roles, TSDB_ROLE_SYSAUDIT_LOG, sizeof(TSDB_ROLE_SYSAUDIT_LOG))) {
110,542✔
3197
    (void)mndResetAuditLogUser(pMnode, userObj.user, true);
×
3198
  }
3199

3200
  mndTransDrop(pTrans);
110,542✔
3201

3202
_OVER:
110,542✔
3203
  mndUserFreeObj(&userObj);
110,542✔
3204
  TAOS_RETURN(code);
110,542✔
3205
}
3206

3207
static int32_t mndCheckPasswordFmt(const char *pwd) {
164,711✔
3208
  if (tsEnableAdvancedSecurity == 0 && strcmp(pwd, "taosdata") == 0) {
164,711✔
3209
    return 0;
×
3210
  }
3211

3212
  if (tsEnableStrongPassword == 0) {
164,711✔
3213
    for (char c = *pwd; c != 0; c = *(++pwd)) {
131,344✔
3214
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
123,358✔
3215
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
3216
      }
3217
    }
3218
    return 0;
7,986✔
3219
  }
3220

3221
  int32_t len = strlen(pwd);
156,725✔
3222
  if (len < TSDB_PASSWORD_MIN_LEN) {
156,725✔
3223
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
3224
  }
3225

3226
  if (len > TSDB_PASSWORD_MAX_LEN) {
156,725✔
3227
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
3228
  }
3229

3230
  if (taosIsComplexString(pwd)) {
156,725✔
3231
    return 0;
131,391✔
3232
  }
3233

3234
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
25,334✔
3235
}
3236

3237
static int32_t mndCheckTotpSeedFmt(const char *seed) {
×
3238
  int32_t len = strlen(seed);
×
3239
  if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
3240
    return TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT;
×
3241
  }
3242

3243
  if (taosIsComplexString(seed)) {
×
3244
    return 0;
×
3245
  }
3246

3247
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
3248
}
3249

3250
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq) {
×
3251
  SMnode                *pMnode = pReq->info.node;
×
3252
  int32_t                code = 0;
×
3253
  int32_t                lino = 0;
×
3254
  int32_t                contLen = 0;
×
3255
  void                  *pRsp = NULL;
×
3256
  SUserObj              *pUser = NULL;
×
3257
  SGetUserWhiteListReq   wlReq = {0};
×
3258
  SUserDateTimeWhiteList wlRsp = {0};
×
3259

3260
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
×
3261
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3262
  }
3263
  mTrace("user: %s, start to get date time whitelist", wlReq.user);
×
3264

3265
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
×
3266
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
3267

3268
  contLen = tSerializeSUserDateTimeWhiteList(NULL, 0, &wlRsp);
×
3269
  if (contLen < 0) {
×
3270
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3271
  }
3272
  pRsp = rpcMallocCont(contLen);
×
3273
  if (pRsp == NULL) {
×
3274
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3275
  }
3276

3277
  contLen = tSerializeSUserDateTimeWhiteList(pRsp, contLen, &wlRsp);
×
3278
  if (contLen < 0) {
×
3279
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3280
  }
3281

3282
_OVER:
×
3283
  mndReleaseUser(pMnode, pUser);
×
3284
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
3285
  if (code < 0) {
×
3286
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
3287
    rpcFreeCont(pRsp);
×
3288
    pRsp = NULL;
×
3289
    contLen = 0;
×
3290
  }
3291
  pReq->code = code;
×
3292
  pReq->info.rsp = pRsp;
×
3293
  pReq->info.rspLen = contLen;
×
3294

3295
  TAOS_RETURN(code);
×
3296
  return 0;
3297
}
3298

3299
static int32_t buildRetrieveDateTimeWhiteListRsp(SRetrieveDateTimeWhiteListRsp *pRsp) {
626✔
3300
  (void)taosThreadRwlockRdlock(&userCache.rw);
626✔
3301

3302
  int32_t count = taosHashGetSize(userCache.users);
626✔
3303
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
626✔
3304
  if (pRsp->pUsers == NULL) {
626✔
3305
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
3306
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3307
  }
3308

3309
  count = 0;
626✔
3310
  void *pIter = taosHashIterate(userCache.users, NULL);
626✔
3311
  while (pIter) {
1,252✔
3312
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
626✔
3313
    if (wl == NULL || wl->num <= 0) {
626✔
3314
      pIter = taosHashIterate(userCache.users, pIter);
626✔
3315
      continue;
626✔
3316
    }
3317

3318
    SUserDateTimeWhiteList *pUser = &pRsp->pUsers[count];
×
3319
    pUser->ver = userCache.verTime;
×
3320

3321
    size_t klen;
×
3322
    char  *key = taosHashGetKey(pIter, &klen);
×
3323
    (void)memcpy(pUser->user, key, klen);
×
3324

3325
    pUser->numWhiteLists = wl->num;
×
3326
    pUser->pWhiteLists = taosMemoryCalloc(wl->num, sizeof(SDateTimeWhiteListItem));
×
3327
    if (pUser->pWhiteLists == NULL) {
×
3328
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
3329
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3330
    }
3331

3332
    (void)memcpy(pUser->pWhiteLists, wl->ranges, wl->num * sizeof(SDateTimeWhiteListItem));
×
3333
    count++;
×
3334
    pIter = taosHashIterate(userCache.users, pIter);
×
3335
  }
3336

3337
  pRsp->numOfUser = count;
626✔
3338
  pRsp->ver = userCache.verTime;
626✔
3339
  (void)taosThreadRwlockUnlock(&userCache.rw);
626✔
3340
  TAOS_RETURN(0);
626✔
3341
}
3342

3343
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq) {
626✔
3344
  int32_t                       code = 0;
626✔
3345
  int32_t                       lino = 0;
626✔
3346
  int32_t                       len = 0;
626✔
3347
  void                         *pRsp = NULL;
626✔
3348
  SRetrieveDateTimeWhiteListRsp wlRsp = {0};
626✔
3349

3350
  // impl later
3351
  SRetrieveWhiteListReq req = {0};
626✔
3352
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
626✔
3353
    code = TSDB_CODE_INVALID_MSG;
×
3354
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3355
  }
3356

3357
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
626✔
3358

3359
  len = tSerializeSRetrieveDateTimeWhiteListRsp(NULL, 0, &wlRsp);
626✔
3360
  if (len < 0) {
626✔
3361
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3362
  }
3363

3364
  pRsp = rpcMallocCont(len);
626✔
3365
  if (!pRsp) {
626✔
3366
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3367
  }
3368
  len = tSerializeSRetrieveDateTimeWhiteListRsp(pRsp, len, &wlRsp);
626✔
3369
  if (len < 0) {
626✔
3370
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3371
  }
3372

3373
_OVER:
626✔
3374
  if (code < 0) {
626✔
3375
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
3376
    rpcFreeCont(pRsp);
×
3377
    pRsp = NULL;
×
3378
    len = 0;
×
3379
  }
3380
  pReq->code = code;
626✔
3381
  pReq->info.rsp = pRsp;
626✔
3382
  pReq->info.rspLen = len;
626✔
3383

3384
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
626✔
3385
  TAOS_RETURN(code);
626✔
3386
}
3387

3388
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
124,028✔
3389
  SMnode        *pMnode = pReq->info.node;
124,028✔
3390
  int32_t        code = 0;
124,028✔
3391
  int32_t        lino = 0;
124,028✔
3392
  SRoleObj      *pRole = NULL;
124,028✔
3393
  SUserObj      *pUser = NULL;
124,028✔
3394
  SUserObj      *pOperUser = NULL;
124,028✔
3395
  SCreateUserReq createReq = {0};
124,028✔
3396
  int64_t        tss = taosGetTimestampMs();
124,028✔
3397

3398
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
124,028✔
3399
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3400
  }
3401

3402
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
124,028✔
3403

3404
#ifndef TD_ENTERPRISE
3405
  if (createReq.isImport == 1) {
3406
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
3407
  }
3408
#endif
3409
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
124,028✔
3410
  if (pOperUser == NULL) {
124,028✔
3411
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3412
  }
3413

3414
  if (createReq.isImport != 1) {
124,028✔
3415
    // TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_CREATE_USER), &lino, _OVER);
3416
    TAOS_CHECK_GOTO(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_CREATE, 0, 0, NULL, NULL),
124,028✔
3417
                    &lino, _OVER);
3418
  } else if (strcmp(RPC_MSG_USER(pReq), "root") != 0) {
×
3419
    mError("The operation is not permitted to create user:%s", RPC_MSG_USER(pReq));
×
3420
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
3421
  }
3422

3423
  if (createReq.user[0] == 0) {
124,028✔
3424
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3425
  }
3426

3427
  if (createReq.isImport != 1) {
124,028✔
3428
    code = mndCheckPasswordFmt(createReq.pass);
124,028✔
3429
    TAOS_CHECK_GOTO(code, &lino, _OVER);
124,028✔
3430
  }
3431

3432
  if (createReq.totpseed[0] != 0) {
111,154✔
3433
    code = mndCheckTotpSeedFmt(createReq.totpseed);
×
3434
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3435
  }
3436

3437
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
111,154✔
3438
  if (pUser != NULL) {
111,154✔
3439
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
424✔
3440
  }
3441

3442
  code = mndAcquireRole(pMnode, createReq.user, &pRole);
110,730✔
3443
  if (pRole != NULL) {
110,730✔
3444
    TAOS_CHECK_GOTO(TSDB_CODE_MND_ROLE_ALREADY_EXIST, &lino, _OVER);
×
3445
  }
3446

3447
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
110,730✔
3448

3449
  if (sdbGetSize(pMnode->pSdb, SDB_USER) >= TSDB_MAX_USERS) {
110,730✔
3450
    mError("user:%s, failed to create since reach max user limit %d", createReq.user, TSDB_MAX_USERS);
×
3451
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USERS, &lino, _OVER);
×
3452
  }
3453

3454
  if (!createReq.hasSessionPerUser) createReq.sessionPerUser = (tsEnableAdvancedSecurity ? TSDB_USER_SESSION_PER_USER_DEFAULT : -1);
110,730✔
3455
  if (!createReq.hasConnectTime) createReq.connectTime = (tsEnableAdvancedSecurity ? TSDB_USER_CONNECT_TIME_DEFAULT : -1);
110,730✔
3456
  if (!createReq.hasConnectIdleTime) createReq.connectIdleTime = (tsEnableAdvancedSecurity ? TSDB_USER_CONNECT_IDLE_TIME_DEFAULT : -1);
110,730✔
3457
  if (!createReq.hasCallPerSession) createReq.callPerSession = (tsEnableAdvancedSecurity ? TSDB_USER_CALL_PER_SESSION_DEFAULT : -1);
110,730✔
3458
  if (!createReq.hasVnodePerCall) createReq.vnodePerCall = (tsEnableAdvancedSecurity ? TSDB_USER_VNODE_PER_CALL_DEFAULT : -1);
110,730✔
3459
  if (!createReq.hasFailedLoginAttempts) createReq.failedLoginAttempts = (tsEnableAdvancedSecurity ? TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT : -1);
110,730✔
3460
  if (!createReq.hasPasswordLifeTime) createReq.passwordLifeTime = (tsEnableAdvancedSecurity ? TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT : -1);
110,730✔
3461
  if (!createReq.hasPasswordReuseTime) createReq.passwordReuseTime = (tsEnableAdvancedSecurity ? TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT : 0);
110,730✔
3462
  if (!createReq.hasPasswordReuseMax) createReq.passwordReuseMax = (tsEnableAdvancedSecurity ? TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT : 0);
110,730✔
3463
  if (!createReq.hasPasswordLockTime) createReq.passwordLockTime = (tsEnableAdvancedSecurity ? TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT : 1);
110,730✔
3464
  if (!createReq.hasPasswordGraceTime) createReq.passwordGraceTime = (tsEnableAdvancedSecurity ? TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT : -1);
110,730✔
3465
  if (!createReq.hasInactiveAccountTime) createReq.inactiveAccountTime = (tsEnableAdvancedSecurity ? TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT : -1);
110,730✔
3466
  if (!createReq.hasAllowTokenNum) createReq.allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
110,730✔
3467

3468
#ifdef TD_ENTERPRISE
3469
  // MAC: per FS §4.2.1.4, CREATE USER with security_level obeys:
3470
  //  - operator with PRIV_SECURITY_POLICY_ALTER  : may specify any [min,max] in [0,4]
3471
  //  - operator without PRIV_SECURITY_POLICY_ALTER: may only specify [0,0] (equivalent to default);
3472
  //                                                 specifying any value > 0 returns MND_NO_RIGHTS.
3473
  // No escalation check is applied on user level (bootstrap-dead-lock avoidance).
3474
  if (createReq.hasSecurityLevel) {
110,730✔
3475
    bool onlyZero = (createReq.minSecLevel == 0 && createReq.maxSecLevel == 0);
188✔
3476
    if (!onlyZero && !mndUserHasMacLabelPriv(pMnode, pOperUser)) {
188✔
3477
      mError("user:%s, failed to create with security_level[%d,%d], operator %s lacks PRIV_SECURITY_POLICY_ALTER",
188✔
3478
             createReq.user, createReq.minSecLevel, createReq.maxSecLevel, RPC_MSG_USER(pReq));
3479
      TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
188✔
3480
    }
3481
  }
3482
#endif
3483

3484
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
110,542✔
3485
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
110,542✔
3486

3487
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
110,542✔
3488
    char detail[1000] = {0};
110,542✔
3489
    (void)snprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
110,542✔
3490
                   createReq.superUser, createReq.sysInfo);
110,542✔
3491
    char operation[15] = {0};
110,542✔
3492
    if (createReq.isImport == 1) {
110,542✔
3493
      tstrncpy(operation, "importUser", sizeof(operation));
×
3494
    } else {
3495
      tstrncpy(operation, "createUser", sizeof(operation));
110,542✔
3496
    }
3497

3498
    int64_t tse = taosGetTimestampMs();
110,542✔
3499
    double  duration = (double)(tse - tss);
110,542✔
3500
    duration = duration / 1000;
110,542✔
3501

3502
    auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail), duration, 0);
110,542✔
3503
  }
3504

3505
_OVER:
124,028✔
3506
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExists) {
124,028✔
3507
    code = 0;
×
3508
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
124,028✔
3509
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
13,486✔
3510
  }
3511

3512
  mndReleaseRole(pMnode, pRole);
124,028✔
3513
  mndReleaseUser(pMnode, pUser);
124,028✔
3514
  mndReleaseUser(pMnode, pOperUser);
124,028✔
3515
  tFreeSCreateUserReq(&createReq);
124,028✔
3516

3517
  TAOS_RETURN(code);
124,028✔
3518
}
3519

3520
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq) {
11,275✔
3521
  SMnode                *pMnode = pReq->info.node;
11,275✔
3522
  int32_t                code = 0;
11,275✔
3523
  int32_t                lino = 0;
11,275✔
3524
  int32_t                contLen = 0;
11,275✔
3525
  void                  *pRsp = NULL;
11,275✔
3526
  SUserObj              *pUser = NULL;
11,275✔
3527
  SGetUserWhiteListReq   wlReq = {0};
11,275✔
3528
  SGetUserIpWhiteListRsp wlRsp = {0};
11,275✔
3529

3530
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
11,275✔
3531
  int32_t (*setRspFn)(SMnode *pMnode, SUserObj *pUser, SGetUserIpWhiteListRsp *pRsp) = NULL;
11,275✔
3532

3533
  if (pReq->msgType == TDMT_MND_GET_USER_IP_WHITELIST_DUAL) {
11,275✔
3534
    serialFn = tSerializeSGetUserIpWhiteListDualRsp;
11,275✔
3535
    setRspFn = mndSetUserIpWhiteListDualRsp;
11,275✔
3536
  } else {
3537
    serialFn = tSerializeSGetUserIpWhiteListRsp;
×
3538
    setRspFn = mndSetUserIpWhiteListRsp;
×
3539
  }
3540
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
11,275✔
3541
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3542
  }
3543
  mTrace("user: %s, start to get ip whitelist", wlReq.user);
11,275✔
3544

3545
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
11,275✔
3546
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
11,275✔
3547

3548
  contLen = serialFn(NULL, 0, &wlRsp);
11,275✔
3549
  if (contLen < 0) {
11,275✔
3550
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3551
  }
3552
  pRsp = rpcMallocCont(contLen);
11,275✔
3553
  if (pRsp == NULL) {
11,275✔
3554
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3555
  }
3556

3557
  contLen = serialFn(pRsp, contLen, &wlRsp);
11,275✔
3558
  if (contLen < 0) {
11,275✔
3559
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3560
  }
3561

3562
_OVER:
11,275✔
3563
  mndReleaseUser(pMnode, pUser);
11,275✔
3564
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
11,275✔
3565
  if (code < 0) {
11,275✔
3566
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
3567
    rpcFreeCont(pRsp);
×
3568
    pRsp = NULL;
×
3569
    contLen = 0;
×
3570
  }
3571
  pReq->code = code;
11,275✔
3572
  pReq->info.rsp = pRsp;
11,275✔
3573
  pReq->info.rspLen = contLen;
11,275✔
3574

3575
  TAOS_RETURN(code);
11,275✔
3576
}
3577

3578
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
626✔
3579
  (void)taosThreadRwlockRdlock(&userCache.rw);
626✔
3580

3581
  int32_t count = taosHashGetSize(userCache.users);
626✔
3582
  pUpdate->pUserIpWhite = taosMemoryCalloc(count, sizeof(SUpdateUserIpWhite));
626✔
3583
  if (pUpdate->pUserIpWhite == NULL) {
626✔
3584
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
3585
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3586
  }
3587

3588
  count = 0;
626✔
3589
  void *pIter = taosHashIterate(userCache.users, NULL);
626✔
3590
  while (pIter) {
1,252✔
3591
    SIpWhiteListDual *wl = (*(SCachedUserInfo **)pIter)->wlIp;
626✔
3592
    if (wl == NULL || wl->num <= 0) {
626✔
3593
      pIter = taosHashIterate(userCache.users, pIter);
×
3594
      continue;
×
3595
    }
3596

3597
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
626✔
3598
    pUser->ver = userCache.verIp;
626✔
3599

3600
    size_t klen;
626✔
3601
    char  *key = taosHashGetKey(pIter, &klen);
626✔
3602
    (void)memcpy(pUser->user, key, klen);
626✔
3603

3604
    pUser->numOfRange = wl->num;
626✔
3605
    pUser->pIpRanges = taosMemoryCalloc(wl->num, sizeof(SIpRange));
626✔
3606
    if (pUser->pIpRanges == NULL) {
626✔
3607
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
3608
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3609
    }
3610

3611
    (void)memcpy(pUser->pIpRanges, wl->pIpRanges, wl->num * sizeof(SIpRange));
626✔
3612
    count++;
626✔
3613
    pIter = taosHashIterate(userCache.users, pIter);
626✔
3614
  }
3615

3616
  pUpdate->numOfUser = count;
626✔
3617
  pUpdate->ver = userCache.verIp;
626✔
3618
  (void)taosThreadRwlockUnlock(&userCache.rw);
626✔
3619
  TAOS_RETURN(0);
626✔
3620
}
3621

3622
int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq) {
626✔
3623
  int32_t        code = 0;
626✔
3624
  int32_t        lino = 0;
626✔
3625
  int32_t        len = 0;
626✔
3626
  void          *pRsp = NULL;
626✔
3627
  SUpdateIpWhite ipWhite = {0};
626✔
3628

3629
  // impl later
3630
  SRetrieveWhiteListReq req = {0};
626✔
3631
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
626✔
3632
    code = TSDB_CODE_INVALID_MSG;
×
3633
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3634
  }
3635

3636
  int32_t (*fn)(void *, int32_t, SUpdateIpWhite *) = NULL;
626✔
3637
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST) {
626✔
3638
    fn = tSerializeSUpdateIpWhite;
×
3639
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL) {
626✔
3640
    fn = tSerializeSUpdateIpWhiteDual;
626✔
3641
  }
3642

3643
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
626✔
3644

3645
  len = fn(NULL, 0, &ipWhite);
626✔
3646
  if (len < 0) {
626✔
3647
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3648
  }
3649

3650
  pRsp = rpcMallocCont(len);
626✔
3651
  if (!pRsp) {
626✔
3652
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3653
  }
3654
  len = fn(pRsp, len, &ipWhite);
626✔
3655
  if (len < 0) {
626✔
3656
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3657
  }
3658

3659
_OVER:
626✔
3660
  if (code < 0) {
626✔
3661
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
3662
    rpcFreeCont(pRsp);
×
3663
    pRsp = NULL;
×
3664
    len = 0;
×
3665
  }
3666
  pReq->code = code;
626✔
3667
  pReq->info.rsp = pRsp;
626✔
3668
  pReq->info.rspLen = len;
626✔
3669

3670
  tFreeSUpdateIpWhiteReq(&ipWhite);
626✔
3671
  TAOS_RETURN(code);
626✔
3672
}
3673

3674
static int32_t mndAlterUserEx(SMnode *pMnode, SUserObj *pNew, SRpcMsg *pReq, ETrnFunc stopFunc) {
1,600,722✔
3675
  int32_t code = 0, lino = 0;
1,600,722✔
3676
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "alter-user");
1,600,722✔
3677
  if (pTrans == NULL) {
1,600,722✔
3678
    mError("user:%s, failed to alter since %s", pNew->user, terrstr());
×
3679
    TAOS_RETURN(terrno);
×
3680
  }
3681
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pNew->user);
1,600,722✔
3682

3683
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
1,600,722✔
3684
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
1,600,722✔
3685
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
3686
    mndTransDrop(pTrans);
×
3687
    TAOS_RETURN(terrno);
×
3688
  }
3689
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,600,722✔
3690

3691
  if (stopFunc > 0) {
1,600,722✔
3692
    mndTransSetCb(pTrans, 0, stopFunc, NULL, 0);
×
3693
  }
3694
  if (mndTransPrepare(pMnode, pTrans) != 0) {
1,600,722✔
3695
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3696
    mndTransDrop(pTrans);
×
3697
    TAOS_RETURN(terrno);
×
3698
  }
3699
  if ((code = userCacheUpdateWhiteList(pMnode, pNew)) != 0) {
1,600,722✔
3700
    mndTransDrop(pTrans);
×
3701
    TAOS_RETURN(code);
×
3702
  }
3703
_exit:
1,600,722✔
3704
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,600,722✔
3705
    mError("user:%s, failed to alter at line %d since %s", pNew->user, lino, tstrerror(code));
×
3706
  }
3707
  mndTransDrop(pTrans);
1,600,722✔
3708
  TAOS_RETURN(code);
1,600,722✔
3709
}
3710

3711
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pNew, SRpcMsg *pReq) {
48,879✔
3712
  return mndAlterUserEx(pMnode, pNew, pReq, 0);
48,879✔
3713
}
3714

3715
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
×
3716
  int32_t code = 0;
×
3717

3718
  *ppNew =
×
3719
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
3720
  if (*ppNew == NULL) {
×
3721
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3722
    TAOS_RETURN(code);
×
3723
  }
3724

3725
  char *db = taosHashIterate(pOld, NULL);
×
3726
  while (db != NULL) {
×
3727
    int32_t len = strlen(db) + 1;
×
3728
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
×
3729
      taosHashCancelIterate(pOld, db);
×
3730
      taosHashCleanup(*ppNew);
×
3731
      TAOS_RETURN(code);
×
3732
    }
3733
    db = taosHashIterate(pOld, db);
×
3734
  }
3735

3736
  TAOS_RETURN(code);
×
3737
}
3738

3739
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
×
3740

3741
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
×
3742

3743
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
3744
                                  SSdb *pSdb) {
3745
  void *pIter = NULL;
×
3746
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
3747

3748
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
×
3749
  int32_t len = strlen(tbFName) + 1;
×
3750

3751
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
×
3752
    char *value = taosHashGet(hash, tbFName, len);
×
3753
    if (value != NULL) {
×
3754
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEGE_EXIST);
×
3755
    }
3756

3757
    int32_t condLen = alterReq->tagCondLen;
×
3758
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
×
3759
  } else {
3760
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
×
3761
  }
3762

3763
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
3764
  int32_t  ref = 1;
×
3765
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
3766
  if (NULL != currRef) {
×
3767
    ref = (*currRef) + 1;
×
3768
  }
3769
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
3770

3771
  TAOS_RETURN(0);
×
3772
}
3773

3774
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
3775
                                        SSdb *pSdb) {
3776
  void *pIter = NULL;
×
3777
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
3778
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
×
3779
  int32_t len = strlen(tbFName) + 1;
×
3780

3781
  if (taosHashRemove(hash, tbFName, len) != 0) {
×
3782
    TAOS_RETURN(0);  // not found
×
3783
  }
3784

3785
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
3786
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
3787
  if (NULL == currRef) {
×
3788
    return 0;
×
3789
  }
3790

3791
  if (1 == *currRef) {
×
3792
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
×
3793
      TAOS_RETURN(0);  // not found
×
3794
    }
3795
    return 0;
×
3796
  }
3797
  int32_t ref = (*currRef) - 1;
×
3798
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
3799

3800
  return 0;
×
3801
}
3802

3803
#if 0
3804
static int32_t mndProcessAlterUserPrivilegesReq(SRpcMsg* pReq, SAlterUserReq *pAlterReq) {
3805
  SMnode   *pMnode = pReq->info.node;
3806
  SSdb     *pSdb = pMnode->pSdb;
3807
  int32_t   code = 0, lino = 0;
3808
  SUserObj *pUser = NULL;
3809
  SUserObj  newUser = {0};
3810
  int64_t   tss = taosGetTimestampMs();
3811

3812
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
3813
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino, _OVER);
3814
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3815

3816
#if 0
3817
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3818
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3819
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3820
      int32_t len = strlen(pAlterReq->objname) + 1;
3821
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3822
      if (pDb == NULL) {
3823
        mndReleaseDb(pMnode, pDb);
3824
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3825
      }
3826
      if ((code = taosHashPut(newUser.readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
3827
          0) {
3828
        mndReleaseDb(pMnode, pDb);
3829
        TAOS_CHECK_GOTO(code, &lino, _OVER);
3830
      }
3831
      mndReleaseDb(pMnode, pDb);
3832
    } else {
3833
      void   *pIter = NULL;
3834
      while (1) {
3835
        SDbObj *pDb = NULL;
3836
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
3837
        if (pIter == NULL) break;
3838
        int32_t len = strlen(pDb->name) + 1;
3839
        if ((code = taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
3840
          sdbRelease(pSdb, pDb);
3841
          sdbCancelFetch(pSdb, pIter);
3842
          TAOS_CHECK_GOTO(code, &lino, _OVER);
3843
        }
3844
        sdbRelease(pSdb, pDb);
3845
      }
3846
    }
3847
  }
3848

3849
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3850
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3851
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3852
      int32_t len = strlen(pAlterReq->objname) + 1;
3853
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3854
      if (pDb == NULL) {
3855
        mndReleaseDb(pMnode, pDb);
3856
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3857
      }
3858
      if ((code = taosHashPut(newUser.writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
3859
          0) {
3860
        mndReleaseDb(pMnode, pDb);
3861
        TAOS_CHECK_GOTO(code, &lino, _OVER);
3862
      }
3863
      mndReleaseDb(pMnode, pDb);
3864
    } else {
3865
      void   *pIter = NULL;
3866
      while (1) {
3867
        SDbObj *pDb = NULL;
3868
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
3869
        if (pIter == NULL) break;
3870
        int32_t len = strlen(pDb->name) + 1;
3871
        if ((code = taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
3872
          sdbRelease(pSdb, pDb);
3873
          sdbCancelFetch(pSdb, pIter);
3874
          TAOS_CHECK_GOTO(code, &lino, _OVER);
3875
        }
3876
        sdbRelease(pSdb, pDb);
3877
      }
3878
    }
3879
  }
3880

3881
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3882
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3883
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3884
      int32_t len = strlen(pAlterReq->objname) + 1;
3885
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3886
      if (pDb == NULL) {
3887
        mndReleaseDb(pMnode, pDb);
3888
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3889
      }
3890
      code = taosHashRemove(newUser.readDbs, pAlterReq->objname, len);
3891
      if (code < 0) {
3892
        mError("read db:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
3893
      }
3894
      mndReleaseDb(pMnode, pDb);
3895
    } else {
3896
      taosHashClear(newUser.readDbs);
3897
    }
3898
  }
3899

3900
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3901
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3902
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3903
      int32_t len = strlen(pAlterReq->objname) + 1;
3904
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3905
      if (pDb == NULL) {
3906
        mndReleaseDb(pMnode, pDb);
3907
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3908
      }
3909
      code = taosHashRemove(newUser.writeDbs, pAlterReq->objname, len);
3910
      if (code < 0) {
3911
        mError("user:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
3912
      }
3913
      mndReleaseDb(pMnode, pDb);
3914
    } else {
3915
      taosHashClear(newUser.writeDbs);
3916
    }
3917
  }
3918

3919
  SHashObj *pReadTbs = newUser.readTbs;
3920
  SHashObj *pWriteTbs = newUser.writeTbs;
3921
  SHashObj *pAlterTbs = newUser.alterTbs;
3922

3923
#ifdef TD_ENTERPRISE
3924
  if (pAlterReq->isView) {
3925
    pReadTbs = newUser.readViews;
3926
    pWriteTbs = newUser.writeViews;
3927
    pAlterTbs = newUser.alterViews;
3928
  }
3929
#endif
3930

3931
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3932
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3933
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3934
  }
3935

3936
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3937
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3938
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3939
  }
3940

3941
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3942
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3943
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3944
  }
3945

3946
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3947
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3948
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3949
  }
3950

3951
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3952
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3953
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3954
  }
3955

3956
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3957
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3958
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3959
  }
3960
#endif
3961

3962
#if 0
3963
// #ifdef USE_TOPIC
3964
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, &pAlterReq->privileges)) {
3965
    int32_t      len = strlen(pAlterReq->objname) + 1;
3966
    SMqTopicObj *pTopic = NULL;
3967
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
3968
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3969
    }
3970
    taosRLockLatch(&pTopic->lock);
3971
    code = taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
3972
    taosRUnLockLatch(&pTopic->lock);
3973
    mndReleaseTopic(pMnode, pTopic);
3974
    TAOS_CHECK_GOTO(code, &lino, _OVER);
3975
  }
3976

3977
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, &pAlterReq->privileges)) {
3978
    int32_t      len = strlen(pAlterReq->objname) + 1;
3979
    SMqTopicObj *pTopic = NULL;
3980
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
3981
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3982
    }
3983
    taosRLockLatch(&pTopic->lock);
3984
    code = taosHashRemove(newUser.topics, pAlterReq->objname, len);
3985
    if (code < 0) {
3986
      mError("user:%s, failed to remove topic:%s since %s", newUser.user, pAlterReq->objname, tstrerror(code));
3987
    }
3988
    taosRUnLockLatch(&pTopic->lock);
3989
    mndReleaseTopic(pMnode, pTopic);
3990
  }
3991
#endif
3992

3993
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
3994
  code = TSDB_CODE_ACTION_IN_PROGRESS;
3995

3996
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
3997
    int64_t tse = taosGetTimestampMs();
3998
    double  duration = (double)(tse - tss);
3999
    duration = duration / 1000;
4000
    if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
4001
              ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
4002
              ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
4003
              ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
4004
              ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
4005
              ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
4006
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
4007
        SName name = {0};
4008
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
4009
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4010
      } else {
4011
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4012
      }
4013
    } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
4014
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4015
    } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
4016
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4017
    } else {
4018
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
4019
        SName name = {0};
4020
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
4021
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4022
      } else {
4023
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
4024
      }
4025
    }
4026
  }
4027
  
4028
_OVER:
4029
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
4030
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
4031
  }
4032
  mndReleaseUser(pMnode, pUser);
4033
  mndUserFreeObj(&newUser);
4034
  TAOS_RETURN(code);
4035
}
4036
#endif
4037

4038
// Returns the minimum maxSecLevel a user must have to hold its current role set under MAC.
4039
// Floor mapping: SYSSEC/SYSAUDIT/SYSAUDIT_LOG=4, SYSDBA=3, SYSINFO_1=1, others=0.
4040
int8_t mndGetUserRoleFloorMaxLevel(SHashObj *roles) {
6,420✔
4041
  if (roles == NULL) return TSDB_MIN_SECURITY_LEVEL;
6,420✔
4042
  if (taosHashGet(roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC)) ||
10,673✔
4043
      taosHashGet(roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT)) ||
8,112✔
4044
      taosHashGet(roles, TSDB_ROLE_SYSAUDIT_LOG, sizeof(TSDB_ROLE_SYSAUDIT_LOG))) {
3,859✔
4045
    return TSDB_MAX_SECURITY_LEVEL;
2,561✔
4046
  }
4047
  if (taosHashGet(roles, TSDB_ROLE_SYSDBA, sizeof(TSDB_ROLE_SYSDBA))) {
3,859✔
4048
    return 3;
1,182✔
4049
  }
4050
  if (taosHashGet(roles, TSDB_ROLE_SYSINFO_1, sizeof(TSDB_ROLE_SYSINFO_1))) {
2,677✔
4051
    return 1;
2,677✔
4052
  }
4053
  return TSDB_MIN_SECURITY_LEVEL;
×
4054
}
4055

4056
// Returns the minSecLevel floor imposed by system roles:
4057
// SYSSEC/SYSAUDIT/SYSAUDIT_LOG require minSecLevel=4; SYSDBA requires minSecLevel=0 (no constraint).
4058
int8_t mndGetUserRoleFloorMinLevel(SHashObj *roles) {
6,420✔
4059
  if (roles == NULL) return TSDB_MIN_SECURITY_LEVEL;
6,420✔
4060
  if (taosHashGet(roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC)) ||
10,673✔
4061
      taosHashGet(roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT)) ||
8,112✔
4062
      taosHashGet(roles, TSDB_ROLE_SYSAUDIT_LOG, sizeof(TSDB_ROLE_SYSAUDIT_LOG))) {
3,859✔
4063
    return TSDB_MAX_SECURITY_LEVEL;
2,561✔
4064
  }
4065
  return TSDB_MIN_SECURITY_LEVEL;
3,859✔
4066
}
4067

4068
// Check if a user holds PRIV_SECURITY_POLICY_ALTER — directly or via any assigned role.
4069
// When MAC is mandatory, the holder must also have maxSecLevel == TSDB_MAX_SECURITY_LEVEL.
4070
// superUser always qualifies.
4071
bool mndUserHasMacLabelPriv(SMnode *pMnode, SUserObj *pUser) {
2,221,227✔
4072
#ifdef TD_ENTERPRISE
4073
  if (pUser->superUser) return true;
2,221,227✔
4074
  bool hasPriv = PRIV_HAS(&pUser->sysPrivs, PRIV_SECURITY_POLICY_ALTER);
16,988✔
4075
  if (!hasPriv && pUser->roles) {
16,988✔
4076
    void     *pIter = NULL;
16,988✔
4077
    SRoleObj *pRole = NULL;
16,988✔
4078
    while ((pIter = taosHashIterate(pUser->roles, pIter)) != NULL) {
36,977✔
4079
      char *roleName = taosHashGetKey(pIter, NULL);
29,730✔
4080
      if (!roleName) continue;
29,730✔
4081
      if (mndAcquireRole(pMnode, roleName, &pRole) != 0) continue;
29,730✔
4082
      if (pRole->enable && PRIV_HAS(&pRole->sysPrivs, PRIV_SECURITY_POLICY_ALTER)) {
29,730✔
4083
        mndReleaseRole(pMnode, pRole);
9,741✔
4084
        taosHashCancelIterate(pUser->roles, pIter);
9,741✔
4085
        hasPriv = true;
9,741✔
4086
        break;
9,741✔
4087
      }
4088
      mndReleaseRole(pMnode, pRole);
19,989✔
4089
    }
4090
  }
4091
  if (!hasPriv) return false;
16,988✔
4092
  // When MAC is mandatory, PRIV_SECURITY_POLICY_ALTER holder must have the highest security level
4093
  if (pMnode->macActive == MAC_MODE_MANDATORY && pUser->maxSecLevel < TSDB_MAX_SECURITY_LEVEL) {
9,741✔
4094
    return false;
×
4095
  }
4096
#endif
4097
  return true;
9,741✔
4098
}
4099

4100
int32_t mndAlterUserFromRole(SRpcMsg *pReq, SUserObj *pOperUser, SAlterRoleReq *pAlterReq) {
1,565,145✔
4101
  SMnode   *pMnode = pReq->info.node;
1,565,145✔
4102
  SSdb     *pSdb = pMnode->pSdb;
1,565,145✔
4103
  void     *pIter = NULL;
1,565,145✔
4104
  int32_t   code = 0, lino = 0;
1,565,145✔
4105
  SUserObj *pUser = NULL;
1,565,145✔
4106
  SUserObj  newUser = {0};
1,565,145✔
4107

4108
  if ((pAlterReq->alterType == TSDB_ALTER_ROLE_ROLE) && (pAlterReq->add == 0) &&
1,574,031✔
4109
      (mndGetSoDPhase(pMnode) == TSDB_SOD_PHASE_ENFORCE)) {
8,886✔
4110
    TAOS_RETURN(TSDB_CODE_MND_SOD_RESTRICTED);
×
4111
  }
4112

4113
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, pAlterReq->principal, &pUser));
1,565,145✔
4114

4115
  if (pUser->enable == 0) {
1,561,395✔
4116
    TAOS_CHECK_EXIT(TSDB_CODE_MND_USER_DISABLED);
×
4117
  }
4118
  if(pUser->superUser) {
1,561,395✔
4119
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_RIGHTS);
2,845✔
4120
  }
4121

4122
  ETrnFunc stopFunc = 0;
1,558,550✔
4123
  if (pAlterReq->alterType == TSDB_ALTER_ROLE_PRIVILEGES) {
1,558,550✔
4124
#ifdef TD_ENTERPRISE
4125
    TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
1,538,972✔
4126
    if ((code = mndAlterUserPrivInfo(pMnode, &newUser, pAlterReq)) == TSDB_CODE_QRY_DUPLICATED_OPERATION) {
1,538,972✔
4127
      code = 0;
×
4128
      goto _exit;
×
4129
    } else {
4130
      TAOS_CHECK_EXIT(code);
1,538,972✔
4131
    }
4132
  } else if (pAlterReq->alterType == TSDB_ALTER_ROLE_ROLE) {
19,578✔
4133
    bool isSysRole = IS_SYS_PREFIX(pAlterReq->roleName);
19,578✔
4134
    // SoD mandatory mode: check revoke of management roles
4135
    if ((pAlterReq->add == 0) && isSysRole && (mndGetClusterSoDMode(pMnode) == SOD_MODE_MANDATORY)) {
19,578✔
4136
      uint8_t roleType = 0;
1,379✔
4137
      if (strcmp(pAlterReq->roleName, TSDB_ROLE_SYSDBA) == 0) {
1,379✔
4138
        if (taosHashGet(pUser->roles, TSDB_ROLE_SYSDBA, sizeof(TSDB_ROLE_SYSDBA))) {
197✔
4139
          roleType = T_ROLE_SYSDBA;
197✔
4140
        }
4141
      } else if (strcmp(pAlterReq->roleName, TSDB_ROLE_SYSSEC) == 0) {
1,182✔
4142
        if (taosHashGet(pUser->roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC))) {
788✔
4143
          roleType = T_ROLE_SYSSEC;
788✔
4144
        }
4145
      } else if (strcmp(pAlterReq->roleName, TSDB_ROLE_SYSAUDIT) == 0) {
394✔
4146
        if (taosHashGet(pUser->roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT))) {
197✔
4147
          roleType = T_ROLE_SYSAUDIT;
197✔
4148
        }
4149
      }
4150
      if (roleType != 0) {
1,379✔
4151
        TAOS_CHECK_EXIT(mndCheckManagementRoleStatus(pMnode, pAlterReq->principal, 0));
1,182✔
4152
      }
4153
    }
4154

4155
    if ((code = mndAlterUserRoleInfo(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), pUser, &newUser, pAlterReq)) ==
18,987✔
4156
        TSDB_CODE_QRY_DUPLICATED_OPERATION) {
4157
      code = 0;
364✔
4158
      goto _exit;
364✔
4159
    } else {
4160
      TAOS_CHECK_EXIT(code);
18,623✔
4161
    }
4162
    // MAC mandatory: if granting a role, user's security_level must satisfy the role's min and max floors
4163
    if ((pAlterReq->add == 1) && (pMnode->macActive == MAC_MODE_MANDATORY)) {
16,847✔
4164
      int8_t floorMaxLevel = mndGetUserRoleFloorMaxLevel(newUser.roles);
591✔
4165
      int8_t floorMinLevel = mndGetUserRoleFloorMinLevel(newUser.roles);
591✔
4166
      if (newUser.maxSecLevel < floorMaxLevel) {
591✔
4167
        mError("user:%s, GRANT role:%s rejected under MAC: maxSecLevel(%d) < role maxFloor(%d)", pAlterReq->principal,
197✔
4168
               pAlterReq->roleName, (int32_t)newUser.maxSecLevel, (int32_t)floorMaxLevel);
4169
        TAOS_CHECK_EXIT(TSDB_CODE_MAC_SEC_LEVEL_CONFLICTS_ROLE);
197✔
4170
      }
4171
      if (newUser.minSecLevel < floorMinLevel) {
394✔
4172
        mError("user:%s, GRANT role:%s rejected under MAC: minSecLevel(%d) < role minFloor(%d)", pAlterReq->principal,
197✔
4173
               pAlterReq->roleName, (int32_t)newUser.minSecLevel, (int32_t)floorMinLevel);
4174
        TAOS_CHECK_EXIT(TSDB_CODE_MAC_SEC_LEVEL_CONFLICTS_ROLE);
197✔
4175
      }
4176
    }
4177
    // REVOKE system role: security_level does not auto-change; write audit warning
4178
    if ((pAlterReq->add == 0) && isSysRole && (pMnode->macActive == MAC_MODE_MANDATORY)) {
16,453✔
4179
      mWarn("user:%s, REVOKE system role:%s — security_level [%d,%d] unchanged; manual ALTER USER may be required",
197✔
4180
            pAlterReq->principal, pAlterReq->roleName, (int32_t)pUser->minSecLevel,
4181
            (int32_t)pUser->maxSecLevel);
4182
    }
4183
    // Check if we need to set SoD role check callback
4184
    if ((pAlterReq->add == 1) && isSysRole &&
16,453✔
4185
        (strcmp(pAlterReq->roleName, TSDB_ROLE_SYSDBA) == 0 || strcmp(pAlterReq->roleName, TSDB_ROLE_SYSSEC) == 0 ||
6,742✔
4186
         strcmp(pAlterReq->roleName, TSDB_ROLE_SYSAUDIT) == 0) &&
7,794✔
4187
        (mndGetSoDPhase(pMnode) == TSDB_SOD_PHASE_INITIAL)) {
4,847✔
4188
      stopFunc = TRANS_STOP_FUNC_SOD_ROLE_CHECK;
×
4189
    }
4190
#endif
4191
  } else {
4192
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
4193
  }
4194
  TAOS_CHECK_EXIT(mndAlterUserEx(pMnode, &newUser, pReq, stopFunc));
1,551,843✔
4195
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
1,551,843✔
4196

4197
_exit:
1,565,145✔
4198
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,565,145✔
4199
    mError("user:%s, failed to alter user at line %d since %s", pAlterReq->principal, lino, tstrerror(code));
12,938✔
4200
  }
4201
  mndReleaseUser(pMnode, pUser);
1,565,145✔
4202
  mndUserFreeObj(&newUser);
1,565,145✔
4203
  TAOS_RETURN(code);
1,565,145✔
4204
}
4205

4206
static int32_t mndProcessAlterUserBasicInfoReq(SRpcMsg *pReq, SAlterUserReq *pAlterReq) {
66,060✔
4207
  SMnode   *pMnode = pReq->info.node;
66,060✔
4208
  int32_t   code = 0, lino = 0;
66,060✔
4209
  SUserObj *pUser = NULL;
66,060✔
4210
  SUserObj  newUser = {0};
66,060✔
4211
  char      auditLog[1000] = {0};
66,060✔
4212
  int32_t   auditLen = 0;
66,060✔
4213
  int64_t   tss = taosGetTimestampMs();
66,060✔
4214

4215
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
66,060✔
4216
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino,
64,963✔
4217
                  _OVER);
4218
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
63,252✔
4219

4220
  if (pAlterReq->hasPassword) {
63,252✔
4221
    auditLen += snprintf(auditLog, sizeof(auditLog), "password,");
40,683✔
4222

4223
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(pAlterReq->pass), &lino, _OVER);
40,683✔
4224
    if (newUser.salt[0] == 0) {
28,223✔
4225
      generateSalt(newUser.salt, sizeof(newUser.salt));
222✔
4226
    }
4227
    char pass[TSDB_PASSWORD_LEN] = {0};
28,223✔
4228
    taosEncryptPass_c((uint8_t *)pAlterReq->pass, strlen(pAlterReq->pass), pass);
28,223✔
4229
    pass[sizeof(pass) - 1] = 0;
28,223✔
4230
    if (strlen(tsDataKey) > 0) {
28,223✔
4231
      TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
×
4232
    }
4233

4234
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
28,223✔
4235
      for (int32_t i = 0; i < newUser.numOfPasswords; ++i) {
976,066✔
4236
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
956,956✔
4237
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
546✔
4238
        }
4239
      }
4240
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
19,110✔
4241
      if (passwords == NULL) {
19,110✔
4242
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
4243
      }
4244
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
19,110✔
4245
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
19,110✔
4246
      passwords[0].setTime = taosGetTimestampSec();
19,110✔
4247
      taosMemoryFree(newUser.passwords);
19,110✔
4248
      newUser.passwords = passwords;
19,110✔
4249
      ++newUser.numOfPasswords;
19,110✔
4250
      ++newUser.passVersion;
19,110✔
4251
      newUser.changePass = 2;
19,110✔
4252
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
8,567✔
4253
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
8,315✔
4254
      newUser.passwords[0].setTime = taosGetTimestampSec();
8,315✔
4255
      ++newUser.passVersion;
8,315✔
4256
      newUser.changePass = 2;
8,315✔
4257
    }
4258
  }
4259

4260
  if (pAlterReq->hasTotpseed) {
50,246✔
4261
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "totpseed,");
×
4262

4263
    if (pAlterReq->totpseed[0] == 0) {  // clear totp secret
×
4264
      memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
×
4265
    } else if (taosGenerateTotpSecret(pAlterReq->totpseed, 0, newUser.totpsecret, sizeof(newUser.totpsecret)) < 0) {
×
4266
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
4267
    }
4268
  }
4269

4270
  if (pAlterReq->hasEnable) {
50,246✔
4271
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "enable:%d,", pAlterReq->enable);
3,040✔
4272
#ifdef TD_ENTERPRISE
4273
    if (pAlterReq->enable == 0) {
3,040✔
4274
      if (mndGetSoDPhase(pMnode) == TSDB_SOD_PHASE_ENFORCE) {
1,601✔
4275
        TAOS_CHECK_GOTO(TSDB_CODE_MND_SOD_RESTRICTED, &lino, _OVER);
×
4276
      }
4277
      // SoD mandatory mode: ensure 3 management roles still satisfied after disable
4278
      if (mndGetClusterSoDMode(pMnode) == SOD_MODE_MANDATORY) {
1,601✔
4279
        TAOS_CHECK_GOTO(mndCheckManagementRoleStatus(pMnode, pUser->user, 0), &lino, _OVER);
788✔
4280
      }
4281
    } else {
4282
      if ((strncmp(pUser->name, TSDB_DEFAULT_USER, TSDB_USER_LEN) == 0) &&
1,439✔
4283
          (mndGetClusterSoDMode(pMnode) == SOD_MODE_MANDATORY)) {
×
4284
        TAOS_CHECK_GOTO(TSDB_CODE_MND_SOD_RESTRICTED, &lino, _OVER);
×
4285
      }
4286
    }
4287
#endif
4288

4289
    newUser.enable = pAlterReq->enable;  // lock or unlock user manually
2,449✔
4290
    if (newUser.enable) {
2,449✔
4291
      // reset login info to allow login immediately
4292
      userCacheResetLoginInfo(newUser.user);
1,439✔
4293
    }
4294
  }
4295

4296
  if (pAlterReq->hasSysinfo) {
49,655✔
4297
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sysinfo:%d,", pAlterReq->sysinfo);
5,086✔
4298
    newUser.sysInfo = pAlterReq->sysinfo;
5,086✔
4299
  }
4300

4301
#ifdef TD_ENTERPRISE
4302
  if (pAlterReq->hasSecurityLevel) {
49,655✔
4303
    // MAC: per FS §4.2.1.4, ALTER USER security_level obeys:
4304
    //  - operator with PRIV_SECURITY_POLICY_ALTER  : allowed; no escalation check (bootstrap-dead-lock avoidance).
4305
    //  - operator without PRIV_SECURITY_POLICY_ALTER: rejected regardless of target value (including [0,0]).
4306
    SUserObj *pOperUser = NULL;
5,023✔
4307
    TAOS_CHECK_GOTO(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser), &lino, _OVER);
5,023✔
4308
    if (!mndUserHasMacLabelPriv(pMnode, pOperUser)) {
5,023✔
4309
      mndReleaseUser(pMnode, pOperUser);
376✔
4310
      mError("user:%s, failed to alter security_level, operator %s lacks PRIV_SECURITY_POLICY_ALTER", pAlterReq->user, RPC_MSG_USER(pReq));
376✔
4311
      TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
376✔
4312
    }
4313
    mndReleaseUser(pMnode, pOperUser);
4,647✔
4314
    // MAC mandatory: new security_level must satisfy role floors for both min and max,
4315
    // and direct PRIV_SECURITY_POLICY_ALTER holders must keep maxSecLevel=4.
4316
    if (pMnode->macActive == MAC_MODE_MANDATORY) {
4,647✔
4317
      int8_t floorMaxLevel = mndGetUserRoleFloorMaxLevel(pUser->roles);
3,465✔
4318
      int8_t floorMinLevel = mndGetUserRoleFloorMinLevel(pUser->roles);
3,465✔
4319
      if (pAlterReq->maxSecLevel < floorMaxLevel) {
3,465✔
4320
        mError("user:%s, ALTER security_level rejected under MAC: maxSecLevel(%d) < role maxFloor(%d)", pAlterReq->user,
394✔
4321
               (int32_t)pAlterReq->maxSecLevel, (int32_t)floorMaxLevel);
4322
        TAOS_CHECK_GOTO(TSDB_CODE_MAC_SEC_LEVEL_CONFLICTS_ROLE, &lino, _OVER);
394✔
4323
      }
4324
      if (pAlterReq->minSecLevel < floorMinLevel) {
3,071✔
4325
        mError("user:%s, ALTER security_level rejected under MAC: minSecLevel(%d) < role minFloor(%d)", pAlterReq->user,
197✔
4326
               (int32_t)pAlterReq->minSecLevel, (int32_t)floorMinLevel);
4327
        TAOS_CHECK_GOTO(TSDB_CODE_MAC_SEC_LEVEL_CONFLICTS_ROLE, &lino, _OVER);
197✔
4328
      }
4329
      // Direct PRIV_SECURITY_POLICY_ALTER holder must keep maxSecLevel = TSDB_MAX_SECURITY_LEVEL(4)
4330
      if (PRIV_HAS(&pUser->sysPrivs, PRIV_SECURITY_POLICY_ALTER) &&
2,874✔
4331
          pAlterReq->maxSecLevel < TSDB_MAX_SECURITY_LEVEL) {
197✔
4332
        mError("user:%s, ALTER security_level rejected under MAC: direct PRIV_SECURITY_POLICY_ALTER holder "
197✔
4333
               "must keep maxSecLevel=%d (got %d)",
4334
               pAlterReq->user, (int32_t)TSDB_MAX_SECURITY_LEVEL, (int32_t)pAlterReq->maxSecLevel);
4335
        TAOS_CHECK_GOTO(TSDB_CODE_MAC_SEC_LEVEL_CONFLICTS_ROLE, &lino, _OVER);
197✔
4336
      }
4337
    }
4338
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "securityLevels:[%d,%d],",
11,577✔
4339
                         pAlterReq->minSecLevel, pAlterReq->maxSecLevel);
3,859✔
4340
    newUser.minSecLevel = pAlterReq->minSecLevel;
3,859✔
4341
    newUser.maxSecLevel = pAlterReq->maxSecLevel;
3,859✔
4342
  }
4343
#endif
4344

4345
  if (pAlterReq->hasCreatedb) {
48,491✔
4346
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "createdb:%d,", pAlterReq->createdb);
6,318✔
4347
    newUser.createdb = pAlterReq->createdb;
6,318✔
4348
    if (newUser.createdb == 1) {
6,318✔
4349
      privAddType(&newUser.sysPrivs, PRIV_DB_CREATE);
4350
    } else {
4351
      privRemoveType(&newUser.sysPrivs, PRIV_DB_CREATE);
4352
    }
4353
  }
4354

4355
#ifdef TD_ENTERPRISE
4356
  if (pAlterReq->hasChangepass) {
48,491✔
4357
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "changepass:%d,", pAlterReq->changepass);
×
4358
    newUser.changePass = pAlterReq->changepass;
×
4359
  }
4360

4361
  if (pAlterReq->hasSessionPerUser) {
48,491✔
4362
    auditLen +=
364✔
4363
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sessionPerUser:%d,", pAlterReq->sessionPerUser);
364✔
4364
    newUser.sessionPerUser = pAlterReq->sessionPerUser;
364✔
4365
  }
4366

4367
  if (pAlterReq->hasConnectTime) {
48,491✔
4368
    auditLen += snprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectTime:%d,", pAlterReq->connectTime);
182✔
4369
    newUser.connectTime = pAlterReq->connectTime;
182✔
4370
  }
4371

4372
  if (pAlterReq->hasConnectIdleTime) {
48,491✔
4373
    auditLen +=
182✔
4374
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectIdleTime:%d,", pAlterReq->connectIdleTime);
182✔
4375
    newUser.connectIdleTime = pAlterReq->connectIdleTime;
182✔
4376
  }
4377

4378
  if (pAlterReq->hasCallPerSession) {
48,491✔
4379
    auditLen +=
546✔
4380
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "callPerSession:%d,", pAlterReq->callPerSession);
546✔
4381
    newUser.callPerSession = pAlterReq->callPerSession;
546✔
4382
  }
4383

4384
  if (pAlterReq->hasVnodePerCall) {
48,491✔
4385
    auditLen +=
364✔
4386
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "vnodePerCall:%d,", pAlterReq->vnodePerCall);
364✔
4387
    newUser.vnodePerCall = pAlterReq->vnodePerCall;
364✔
4388
  }
4389

4390
  if (pAlterReq->hasFailedLoginAttempts) {
48,491✔
4391
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "failedLoginAttempts:%d,",
546✔
4392
                          pAlterReq->failedLoginAttempts);
4393
    newUser.failedLoginAttempts = pAlterReq->failedLoginAttempts;
546✔
4394
  }
4395

4396
  if (pAlterReq->hasPasswordLifeTime) {
48,491✔
4397
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLifeTime:%d,",
182✔
4398
                          pAlterReq->passwordLifeTime);
4399
    newUser.passwordLifeTime = pAlterReq->passwordLifeTime;
182✔
4400
  }
4401

4402
  if (pAlterReq->hasPasswordReuseTime) {
48,491✔
4403
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseTime:%d,",
546✔
4404
                          pAlterReq->passwordReuseTime);
4405
    newUser.passwordReuseTime = pAlterReq->passwordReuseTime;
546✔
4406
  }
4407

4408
  if (pAlterReq->hasPasswordReuseMax) {
48,491✔
4409
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseMax:%d,",
546✔
4410
                          pAlterReq->passwordReuseMax);
4411
    newUser.passwordReuseMax = pAlterReq->passwordReuseMax;
546✔
4412
  }
4413

4414
  if (pAlterReq->hasPasswordLockTime) {
48,491✔
4415
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLockTime:%d,",
182✔
4416
                          pAlterReq->passwordLockTime);
4417
    newUser.passwordLockTime = pAlterReq->passwordLockTime;
182✔
4418
  }
4419

4420
  if (pAlterReq->hasPasswordGraceTime) {
48,491✔
4421
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordGraceTime:%d,",
182✔
4422
                          pAlterReq->passwordGraceTime);
4423
    newUser.passwordGraceTime = pAlterReq->passwordGraceTime;
182✔
4424
  }
4425

4426
  if (pAlterReq->hasInactiveAccountTime) {
48,491✔
4427
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "inactiveAccountTime:%d,",
182✔
4428
                          pAlterReq->inactiveAccountTime);
4429
    newUser.inactiveAccountTime = pAlterReq->inactiveAccountTime;
182✔
4430
  }
4431

4432
  if (pAlterReq->hasAllowTokenNum) {
48,491✔
4433
    auditLen +=
364✔
4434
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "allowTokenNum:%d,", pAlterReq->allowTokenNum);
364✔
4435
    newUser.allowTokenNum = pAlterReq->allowTokenNum;
364✔
4436
  }
4437

4438
  if (pAlterReq->numDropIpRanges > 0 || pAlterReq->numIpRanges > 0) {
48,491✔
4439
    int32_t dummy = 0;
1,100✔
4440

4441
    // put previous ip whitelist into hash table
4442
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
1,100✔
4443
    if (m == NULL) {
1,100✔
4444
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4445
    }
4446

4447
    for (int32_t i = 0; i < newUser.pIpWhiteListDual->num; i++) {
4,768✔
4448
      SIpRange range;
3,668✔
4449
      copyIpRange(&range, newUser.pIpWhiteListDual->pIpRanges + i);
3,668✔
4450
      code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
3,668✔
4451
      if (code != 0) {
3,668✔
4452
        taosHashCleanup(m);
×
4453
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4454
      }
4455
    }
4456

4457
    if (pAlterReq->numDropIpRanges > 0) {
1,100✔
4458
      auditLen +=
550✔
4459
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropIpRanges:%d,", pAlterReq->numDropIpRanges);
550✔
4460

4461
      for (int32_t i = 0; i < pAlterReq->numDropIpRanges; i++) {
1,282✔
4462
        if (taosHashGetSize(m) == 0) {
732✔
4463
          break;
×
4464
        }
4465

4466
        SIpRange range;
732✔
4467
        copyIpRange(&range, pAlterReq->pDropIpRanges + i);
732✔
4468

4469
        // for white list, drop default ip ranges is allowed, otherwise, we can never
4470
        // convert white list to black list.
4471

4472
        code = taosHashRemove(m, &range, sizeof(range));
732✔
4473
        if (code == TSDB_CODE_NOT_FOUND) {
732✔
4474
          // treat not exist as success
4475
          code = 0;
205✔
4476
        }
4477
        if (code != 0) {
732✔
4478
          taosHashCleanup(m);
×
4479
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4480
        }
4481
      }
4482
    }
4483

4484
    if (pAlterReq->numIpRanges > 0) {
1,100✔
4485
      auditLen +=
550✔
4486
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addIpRanges:%d,", pAlterReq->numIpRanges);
550✔
4487
      for (int32_t i = 0; i < pAlterReq->numIpRanges; i++) {
1,282✔
4488
        SIpRange range;
732✔
4489
        copyIpRange(&range, pAlterReq->pIpRanges + i);
732✔
4490
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
732✔
4491
        if (code != 0) {
732✔
4492
          taosHashCleanup(m);
×
4493
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4494
        }
4495
      }
4496
    }
4497

4498
    int32_t numOfRanges = taosHashGetSize(m);
1,100✔
4499
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
1,100✔
4500
      taosHashCleanup(m);
×
4501
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
4502
    }
4503

4504
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
1,100✔
4505
    if (p == NULL) {
1,100✔
4506
      taosHashCleanup(m);
×
4507
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4508
    }
4509

4510
    void   *pIter = taosHashIterate(m, NULL);
1,100✔
4511
    int32_t i = 0;
1,100✔
4512
    while (pIter) {
4,973✔
4513
      size_t    len = 0;
3,873✔
4514
      SIpRange *key = taosHashGetKey(pIter, &len);
3,873✔
4515
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
3,873✔
4516
      pIter = taosHashIterate(m, pIter);
3,873✔
4517
      i++;
3,873✔
4518
    }
4519

4520
    taosHashCleanup(m);
1,100✔
4521
    p->num = numOfRanges;
1,100✔
4522
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
1,100✔
4523
    sortIpWhiteList(p);
1,100✔
4524
    newUser.pIpWhiteListDual = p;
1,100✔
4525

4526
    newUser.ipWhiteListVer++;
1,100✔
4527
  }
4528

4529
  if (pAlterReq->numTimeRanges > 0 || pAlterReq->numDropTimeRanges) {
48,491✔
4530
    int32_t dummy = 0;
728✔
4531

4532
    // put previous ip whitelist into hash table
4533
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
728✔
4534
    if (m == NULL) {
728✔
4535
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4536
    }
4537

4538
    for (int32_t i = 0; i < newUser.pTimeWhiteList->num; i++) {
1,820✔
4539
      SDateTimeWhiteListItem *range = &newUser.pTimeWhiteList->ranges[i];
1,092✔
4540
      if (isDateTimeWhiteListItemExpired(range)) {
1,092✔
4541
        continue;
×
4542
      }
4543
      code = taosHashPut(m, range, sizeof(*range), &dummy, sizeof(dummy));
1,092✔
4544
      if (code != 0) {
1,092✔
4545
        taosHashCleanup(m);
×
4546
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4547
      }
4548
    }
4549

4550
    if (pAlterReq->numDropTimeRanges > 0) {
728✔
4551
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropTimeRanges:%d,",
546✔
4552
                            pAlterReq->numDropTimeRanges);
4553
      for (int32_t i = 0; i < pAlterReq->numDropTimeRanges; i++) {
1,274✔
4554
        if (taosHashGetSize(m) == 0) {
728✔
4555
          break;
×
4556
        }
4557
        SDateTimeWhiteListItem range = {0};
728✔
4558
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pDropTimeRanges + i);
728✔
4559

4560
        code = taosHashRemove(m, &range, sizeof(range));
728✔
4561
        if (code == TSDB_CODE_NOT_FOUND) {
728✔
4562
          // treat not exist as success
4563
          code = 0;
×
4564
        }
4565
        if (code != 0) {
728✔
4566
          taosHashCleanup(m);
×
4567
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4568
        }
4569
      }
4570
    }
4571

4572
    if (pAlterReq->numTimeRanges > 0) {
728✔
4573
      auditLen +=
546✔
4574
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addTimeRanges:%d,", pAlterReq->numTimeRanges);
546✔
4575
      for (int32_t i = 0; i < pAlterReq->numTimeRanges; i++) {
1,274✔
4576
        SDateTimeWhiteListItem range = {0};
728✔
4577
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pTimeRanges + i);
728✔
4578
        if (isDateTimeWhiteListItemExpired(&range)) {
728✔
4579
          continue;
×
4580
        }
4581
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
728✔
4582
        if (code != 0) {
728✔
4583
          taosHashCleanup(m);
×
4584
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4585
        }
4586
      }
4587
    }
4588

4589
    int32_t numOfRanges = taosHashGetSize(m);
728✔
4590
    if (numOfRanges > MND_MAX_USER_TIME_RANGE) {
728✔
4591
      taosHashCleanup(m);
×
4592
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
4593
    }
4594

4595
    SDateTimeWhiteList *p =
1,456✔
4596
        taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
728✔
4597
    if (p == NULL) {
728✔
4598
      taosHashCleanup(m);
×
4599
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4600
    }
4601

4602
    void   *pIter = taosHashIterate(m, NULL);
728✔
4603
    int32_t i = 0;
728✔
4604
    while (pIter) {
1,820✔
4605
      size_t                  len = 0;
1,092✔
4606
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
1,092✔
4607
      memcpy(&p->ranges[i], key, sizeof(SDateTimeWhiteListItem));
1,092✔
4608
      pIter = taosHashIterate(m, pIter);
1,092✔
4609
      i++;
1,092✔
4610
    }
4611

4612
    taosHashCleanup(m);
728✔
4613
    p->num = numOfRanges;
728✔
4614
    taosMemoryFreeClear(newUser.pTimeWhiteList);
728✔
4615
    sortTimeWhiteList(p);
728✔
4616
    newUser.pTimeWhiteList = p;
728✔
4617
    newUser.timeWhiteListVer++;
728✔
4618
  }
4619
#endif  // TD_ENTERPRISE
4620

4621
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
48,491✔
4622
  if (pAlterReq->hasEnable) {
48,491✔
4623
    if (newUser.enable) {
2,449✔
4624
      if (taosHashGet(newUser.roles, TSDB_ROLE_SYSAUDIT_LOG, sizeof(TSDB_ROLE_SYSAUDIT_LOG))) {
1,439✔
4625
        (void)mndResetAuditLogUser(pMnode, newUser.user, true);
×
4626
      }
4627
    } else {
4628
      (void)mndResetAuditLogUser(pMnode, newUser.user, false);
1,010✔
4629
    }
4630
  }
4631
  code = TSDB_CODE_ACTION_IN_PROGRESS;
48,491✔
4632

4633
  if (auditLen > 0) {
48,491✔
4634
    auditLog[--auditLen] = 0;  // remove last ','
48,491✔
4635
  }
4636
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
48,491✔
4637
    int64_t tse = taosGetTimestampMs();
48,491✔
4638
    double  duration = (double)(tse - tss);
48,491✔
4639
    duration = duration / 1000;
48,491✔
4640
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", pAlterReq->user, auditLog, auditLen, duration, 0);
48,491✔
4641
  }
4642

4643
_OVER:
66,060✔
4644
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
66,060✔
4645
    mError("user:%s, failed to alter at line %d since %s", pAlterReq->user, lino, tstrerror(code));
17,569✔
4646
  }
4647

4648
  mndReleaseUser(pMnode, pUser);
66,060✔
4649
  mndUserFreeObj(&newUser);
66,060✔
4650
  return code;
66,060✔
4651
}
4652

4653
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
66,060✔
4654
  SAlterUserReq alterReq = {0};
66,060✔
4655

4656
  int32_t code = tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq);
66,060✔
4657
  if (code != 0) {
66,060✔
4658
    mError("failed to deserialize alter user request at line %d since %s", __LINE__, tstrerror(code));
×
4659
    TAOS_RETURN(code);
×
4660
  }
4661

4662
  if (alterReq.user[0] == 0) {
66,060✔
4663
    tFreeSAlterUserReq(&alterReq);
×
4664
    mError("failed to alter user at line %d since invalid user format", __LINE__);
×
4665
    TAOS_RETURN(TSDB_CODE_MND_INVALID_USER_FORMAT);
×
4666
  }
4667

4668
  mInfo("user:%s, start to alter", alterReq.user);
66,060✔
4669
  if (alterReq.alterType == TSDB_ALTER_USER_BASIC_INFO) {
66,060✔
4670
    code = mndProcessAlterUserBasicInfoReq(pReq, &alterReq);
66,060✔
4671
  } else {
4672
    // code = mndProcessAlterUserPrivilegesReq(pReq, &alterReq); // obsolete
4673
  }
4674

4675
  tFreeSAlterUserReq(&alterReq);
66,060✔
4676
  TAOS_RETURN(code);
66,060✔
4677
}
4678

4679
int32_t mndResetAuditLogUser(SMnode *pMnode, const char *user, bool isAdd) {
56,984,545✔
4680
  if (user) {
56,984,545✔
4681
    (void)taosThreadRwlockRdlock(&userCache.rw);
62,259✔
4682
    if (isAdd) {
62,259✔
4683
      if (userCache.auditLogUser[0] != 0) {
89✔
4684
        (void)taosThreadRwlockUnlock(&userCache.rw);
×
4685
        return 0;
×
4686
      }
4687
      (void)taosThreadRwlockUnlock(&userCache.rw);
89✔
4688
      (void)taosThreadRwlockWrlock(&userCache.rw);
89✔
4689
      (void)tsnprintf(userCache.auditLogUser, TSDB_USER_LEN, "%s", user);
89✔
4690
      (void)taosThreadRwlockUnlock(&userCache.rw);
89✔
4691
      return 0;
89✔
4692
    } else if (strcmp(userCache.auditLogUser, user) != 0) {
62,170✔
4693
      (void)taosThreadRwlockUnlock(&userCache.rw);
62,170✔
4694
      return 0;
62,170✔
4695
    }
4696
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
4697
  }
4698

4699
  void     *pIter = NULL;
56,922,286✔
4700
  SSdb     *pSdb = pMnode->pSdb;
56,922,286✔
4701
  SUserObj *pUser = NULL;
56,922,286✔
4702
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
120,611,064✔
4703
    if (pUser->enable == 0) {
63,688,778✔
4704
      mndReleaseUser(pMnode, pUser);
39,486✔
4705
      continue;
39,486✔
4706
    }
4707
    if (taosHashGet(pUser->roles, TSDB_ROLE_SYSAUDIT_LOG, sizeof(TSDB_ROLE_SYSAUDIT_LOG)) != NULL) {
63,649,292✔
4708
      (void)taosThreadRwlockWrlock(&userCache.rw);
×
4709
      (void)tsnprintf(userCache.auditLogUser, TSDB_USER_LEN, "%s", pUser->name);
×
4710
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
4711
      sdbCancelFetch(pSdb, pIter);
×
4712
      mndReleaseUser(pMnode, pUser);
×
4713
      return 0;
×
4714
    }
4715
    mndReleaseUser(pMnode, pUser);
63,649,292✔
4716
  }
4717
  (void)taosThreadRwlockWrlock(&userCache.rw);
56,922,286✔
4718
  userCache.auditLogUser[0] = 0;
56,922,286✔
4719
  (void)taosThreadRwlockUnlock(&userCache.rw);
56,922,286✔
4720
  return TSDB_CODE_MND_USER_NOT_AVAILABLE;
56,922,286✔
4721
}
4722

4723
int32_t mndGetAuditUser(SMnode *pMnode, char *user) {
56,923,443✔
4724
  (void)taosThreadRwlockRdlock(&userCache.rw);
56,923,443✔
4725
  if (userCache.auditLogUser[0] != 0) {
56,923,443✔
4726
    (void)tsnprintf(user, TSDB_USER_LEN, "%s", userCache.auditLogUser);
1,157✔
4727
    (void)taosThreadRwlockUnlock(&userCache.rw);
1,157✔
4728
    return 0;
1,157✔
4729
  }
4730
  (void)taosThreadRwlockUnlock(&userCache.rw);
56,922,286✔
4731

4732
  int32_t code = 0;
56,922,286✔
4733
  if ((code = mndResetAuditLogUser(pMnode, NULL, false)) != 0) {
56,922,286✔
4734
    return code;
56,922,286✔
4735
  }
4736

4737
  (void)taosThreadRwlockRdlock(&userCache.rw);
×
4738
  (void)tsnprintf(user, TSDB_USER_LEN, "%s", userCache.auditLogUser);
×
4739
  (void)taosThreadRwlockUnlock(&userCache.rw);
×
4740

4741
  return 0;
×
4742
}
4743

4744
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
61,160✔
4745
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "drop-user");
61,160✔
4746
  if (pTrans == NULL) {
61,160✔
4747
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
4748
    TAOS_RETURN(terrno);
×
4749
  }
4750
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
61,160✔
4751

4752
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
61,160✔
4753
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
61,160✔
4754
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
4755
    mndTransDrop(pTrans);
×
4756
    TAOS_RETURN(terrno);
×
4757
  }
4758
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
61,160✔
4759
    mndTransDrop(pTrans);
×
4760
    TAOS_RETURN(terrno);
×
4761
  }
4762

4763
  if (mndDropTokensByUser(pMnode, pTrans, pUser->user) != 0) {
61,160✔
4764
    mndTransDrop(pTrans);
×
4765
    TAOS_RETURN(terrno);
×
4766
  }
4767

4768
  if (mndTransPrepare(pMnode, pTrans) != 0) {
61,160✔
4769
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
4770
    mndTransDrop(pTrans);
×
4771
    TAOS_RETURN(terrno);
×
4772
  }
4773

4774
  userCacheRemoveUser(pUser->user);
61,160✔
4775
  mndDropCachedTokensByUser(pUser->user);
61,160✔
4776
  (void)mndResetAuditLogUser(pMnode, pUser->user, false);
61,160✔
4777

4778
  mndTransDrop(pTrans);
61,160✔
4779
  TAOS_RETURN(0);
61,160✔
4780
}
4781

4782
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
66,904✔
4783
  SMnode      *pMnode = pReq->info.node;
66,904✔
4784
  int32_t      code = 0;
66,904✔
4785
  int32_t      lino = 0;
66,904✔
4786
  SUserObj    *pOperUser = NULL;
66,904✔
4787
  SUserObj    *pUser = NULL;
66,904✔
4788
  SDropUserReq dropReq = {0};
66,904✔
4789
  int64_t      tss = taosGetTimestampMs();
66,904✔
4790

4791
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
66,904✔
4792

4793
  mInfo("user:%s, start to drop", dropReq.user);
66,904✔
4794

4795
  if (dropReq.user[0] == 0) {
66,904✔
4796
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
4797
  }
4798

4799
  if (0 == strcmp(dropReq.user, TSDB_DEFAULT_USER)) {
66,904✔
4800
    return TSDB_CODE_MND_NO_RIGHTS;
×
4801
  }
4802

4803
#ifdef TD_ENTERPRISE
4804
  if (mndGetSoDPhase(pMnode) == TSDB_SOD_PHASE_ENFORCE) {
66,904✔
4805
    TAOS_CHECK_GOTO(TSDB_CODE_MND_SOD_RESTRICTED, &lino, _OVER);
×
4806
  }
4807
#endif
4808

4809
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
66,904✔
4810
#ifdef TD_ENTERPRISE
4811
  // SoD mandatory mode: ensure 3 management roles still satisfied after drop
4812
  if (mndGetClusterSoDMode(pMnode) == SOD_MODE_MANDATORY) {
62,342✔
4813
    TAOS_CHECK_GOTO(mndCheckManagementRoleStatus(pMnode, dropReq.user, 0), &lino, _OVER);
3,680✔
4814
  }
4815
#endif
4816

4817
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
61,160✔
4818
  if (pOperUser == NULL) {
61,160✔
4819
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
4820
  }
4821

4822
  // TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_DROP_USER), &lino, _OVER);
4823
  TAOS_CHECK_GOTO(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_DROP, 0, 0, NULL, NULL),
61,160✔
4824
                  &lino, _OVER);
4825

4826
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
61,160✔
4827
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
61,160✔
4828

4829
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
61,160✔
4830
    int64_t tse = taosGetTimestampMs();
61,160✔
4831
    double  duration = (double)(tse - tss);
61,160✔
4832
    duration = duration / 1000;
61,160✔
4833
    auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen, duration, 0);
61,160✔
4834
  }
4835

4836
_OVER:
66,904✔
4837
  if (dropReq.ignoreNotExists && code == TSDB_CODE_MND_USER_NOT_EXIST) {
66,904✔
4838
    code = 0;
1,854✔
4839
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
65,050✔
4840
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
3,890✔
4841
  }
4842

4843
  mndReleaseUser(pMnode, pUser);
66,904✔
4844
  mndReleaseUser(pMnode, pOperUser);
66,904✔
4845
  tFreeSDropUserReq(&dropReq);
66,904✔
4846
  TAOS_RETURN(code);
66,904✔
4847
}
4848

4849
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
6,930,713✔
4850
  SMnode         *pMnode = pReq->info.node;
6,930,713✔
4851
  int32_t         code = 0;
6,930,713✔
4852
  int32_t         lino = 0;
6,930,713✔
4853
  int32_t         contLen = 0;
6,930,713✔
4854
  void           *pRsp = NULL;
6,930,713✔
4855
  SUserObj       *pUser = NULL;
6,930,713✔
4856
  SGetUserAuthReq authReq = {0};
6,930,713✔
4857
  SGetUserAuthRsp authRsp = {0};
6,930,713✔
4858

4859
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
6,930,713✔
4860
  mTrace("user:%s, start to get auth", authReq.user);
6,930,669✔
4861

4862
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
6,930,669✔
4863

4864
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
6,929,422✔
4865

4866
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
6,929,422✔
4867
  if (contLen < 0) {
6,929,422✔
4868
    TAOS_CHECK_EXIT(contLen);
×
4869
  }
4870
  pRsp = rpcMallocCont(contLen);
6,929,422✔
4871
  if (pRsp == NULL) {
6,929,422✔
4872
    TAOS_CHECK_EXIT(terrno);
×
4873
  }
4874

4875
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
6,929,422✔
4876
  if (contLen < 0) {
6,929,422✔
4877
    TAOS_CHECK_EXIT(contLen);
×
4878
  }
4879

4880
_exit:
6,930,713✔
4881
  mndReleaseUser(pMnode, pUser);
6,930,713✔
4882
  tFreeSGetUserAuthRsp(&authRsp);
6,930,713✔
4883
  if (code < 0) {
6,930,713✔
4884
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
1,291✔
4885
    rpcFreeCont(pRsp);
1,291✔
4886
    pRsp = NULL;
1,291✔
4887
    contLen = 0;
1,291✔
4888
  }
4889
  pReq->info.rsp = pRsp;
6,930,713✔
4890
  pReq->info.rspLen = contLen;
6,930,713✔
4891
  pReq->code = code;
6,930,669✔
4892

4893
  TAOS_RETURN(code);
6,930,713✔
4894
}
4895

4896
static void base32Encode(const uint8_t *in, int32_t inLen, char *out) {
21,400✔
4897
  int buffer = 0, bits = 0;
21,400✔
4898
  int outLen = 0;
21,400✔
4899

4900
  // process all input bytes
4901
  for (int i = 0; i < inLen; i++) {
706,200✔
4902
    buffer = (buffer << 8) | in[i];
684,800✔
4903
    bits += 8;
684,800✔
4904

4905
    while (bits >= 5) {
1,776,200✔
4906
      int v = (buffer >> (bits - 5)) & 0x1F;
1,091,400✔
4907
      out[outLen++] = (v >= 26) ? (v - 26 + '2') : (v + 'A');
1,091,400✔
4908
      bits -= 5;
1,091,400✔
4909
    }
4910
  }
4911

4912
  // process remaining bits
4913
  if (bits > 0) {
21,400✔
4914
    int v = (buffer << (5 - bits)) & 0x1F;
21,400✔
4915
    out[outLen++] = (v >= 26) ? (v - 26 + '2') : (v + 'A');
21,400✔
4916
  }
4917

4918
  out[outLen] = '\0';
21,400✔
4919
}
21,400✔
4920

4921
static int32_t mndCreateTotpSecret(SMnode *pMnode, SUserObj *pUser, SRpcMsg *pReq) {
21,400✔
4922
  SCreateTotpSecretRsp rsp = {0};
21,400✔
4923

4924
  base32Encode((uint8_t *)pUser->totpsecret, sizeof(pUser->totpsecret), rsp.totpSecret);
21,400✔
4925
  tstrncpy(rsp.user, pUser->user, sizeof(rsp.user));
21,400✔
4926

4927
  int32_t len = tSerializeSCreateTotpSecretRsp(NULL, 0, &rsp);
21,400✔
4928
  if (len < 0) {
21,400✔
4929
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
4930
  }
4931

4932
  void *pData = taosMemoryMalloc(len);
21,400✔
4933
  if (pData == NULL) {
21,400✔
4934
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
4935
  }
4936

4937
  if (tSerializeSCreateTotpSecretRsp(pData, len, &rsp) != len) {
21,400✔
4938
    taosMemoryFree(pData);
×
4939
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
4940
  }
4941

4942
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "create-totp-secret");
21,400✔
4943
  if (pTrans == NULL) {
21,400✔
4944
    mError("user:%s, failed to create totp secret since %s", pUser->user, terrstr());
×
4945
    taosMemoryFree(pData);
×
4946
    TAOS_RETURN(terrno);
×
4947
  }
4948
  mInfo("trans:%d, used to create totp secret for user:%s", pTrans->id, pUser->user);
21,400✔
4949

4950
  mndTransSetUserData(pTrans, pData, len);
21,400✔
4951

4952
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
21,400✔
4953
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
21,400✔
4954
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
4955
    mndTransDrop(pTrans);
×
4956
    TAOS_RETURN(terrno);
×
4957
  }
4958
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) < 0) {
21,400✔
4959
    mndTransDrop(pTrans);
×
4960
    TAOS_RETURN(terrno);
×
4961
  }
4962

4963
  if (mndTransPrepare(pMnode, pTrans) != 0) {
21,400✔
4964
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
4965
    mndTransDrop(pTrans);
×
4966
    TAOS_RETURN(terrno);
×
4967
  }
4968

4969
  mndTransDrop(pTrans);
21,400✔
4970
  TAOS_RETURN(0);
21,400✔
4971
}
4972

4973
static int32_t mndProcessCreateTotpSecretReq(SRpcMsg *pReq) {
21,594✔
4974
  SMnode              *pMnode = pReq->info.node;
21,594✔
4975
  int32_t              code = 0;
21,594✔
4976
  int32_t              lino = 0;
21,594✔
4977
  SUserObj            *pUser = NULL;
21,594✔
4978
  SUserObj             newUser = {0};
21,594✔
4979
  SCreateTotpSecretReq req = {0};
21,594✔
4980
  int64_t              tss = taosGetTimestampMs();
21,594✔
4981

4982
  TAOS_CHECK_GOTO(tDeserializeSCreateTotpSecretReq(pReq->pCont, pReq->contLen, &req), &lino, _OVER);
21,594✔
4983
  mTrace("user:%s, start to create/update totp secret", req.user);
21,594✔
4984

4985
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, req.user, &pUser), &lino, _OVER);
21,594✔
4986
  TAOS_CHECK_GOTO(mndCheckTotpSecretPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, PRIV_TOTP_CREATE),
21,400✔
4987
                  &lino, _OVER);
4988
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
21,400✔
4989
  taosSafeRandBytes((uint8_t *)newUser.totpsecret, sizeof(newUser.totpsecret));
21,400✔
4990
  TAOS_CHECK_GOTO(mndCreateTotpSecret(pMnode, &newUser, pReq), &lino, _OVER);
21,400✔
4991

4992
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
21,400✔
4993
    double duration = (double)(taosGetTimestampMs() - tss) / 1000.0;
21,400✔
4994
    auditRecord(pReq, pMnode->clusterId, "createTotpSecret", "", req.user, req.sql, req.sqlLen, duration, 0);
21,400✔
4995
  }
4996

4997
_OVER:
21,594✔
4998
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
21,594✔
4999
    mError("user:%s, failed to create totp secret at line %d since %s", req.user, lino, tstrerror(code));
194✔
5000
  }
5001
  mndReleaseUser(pMnode, pUser);
21,594✔
5002
  mndUserFreeObj(&newUser);
21,594✔
5003
  tFreeSCreateTotpSecretReq(&req);
21,594✔
5004
  TAOS_RETURN(code);
21,594✔
5005
}
5006

5007
int32_t mndBuildSMCreateTotpSecretResp(STrans *pTrans, void **ppResp, int32_t *pRespLen) {
21,400✔
5008
  // user data is the response
5009
  *ppResp = pTrans->userData;
21,400✔
5010
  *pRespLen = pTrans->userDataLen;
21,400✔
5011
  pTrans->userData = NULL;
21,400✔
5012
  pTrans->userDataLen = 0;
21,400✔
5013
  return 0;
21,400✔
5014
}
5015

5016
static int32_t mndProcessDropTotpSecretReq(SRpcMsg *pReq) {
8,148✔
5017
  SMnode            *pMnode = pReq->info.node;
8,148✔
5018
  int32_t            code = 0;
8,148✔
5019
  int32_t            lino = 0;
8,148✔
5020
  SUserObj          *pUser = NULL;
8,148✔
5021
  SUserObj           newUser = {0};
8,148✔
5022
  SDropTotpSecretReq req = {0};
8,148✔
5023
  int64_t            tss = taosGetTimestampMs();
8,148✔
5024

5025
  TAOS_CHECK_GOTO(tDeserializeSDropTotpSecretReq(pReq->pCont, pReq->contLen, &req), &lino, _OVER);
8,148✔
5026
  mTrace("user:%s, start to drop totp secret", req.user);
8,148✔
5027

5028
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, req.user, &pUser), &lino, _OVER);
8,148✔
5029
  TAOS_CHECK_GOTO(mndCheckTotpSecretPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, PRIV_TOTP_DROP),
6,208✔
5030
                  &lino, _OVER);
5031

5032
  if (!mndIsTotpEnabledUser(pUser)) {
6,208✔
5033
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOTP_SECRET_NOT_EXIST, &lino, _OVER);
5,820✔
5034
  }
5035

5036
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
388✔
5037
  (void)memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
388✔
5038
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
388✔
5039

5040
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
388✔
5041
    double duration = (double)(taosGetTimestampMs() - tss) / 1000.0;
388✔
5042
    auditRecord(pReq, pMnode->clusterId, "dropTotpSecret", "", req.user, req.sql, req.sqlLen, duration, 0);
388✔
5043
  }
5044

5045
_OVER:
8,148✔
5046
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
8,148✔
5047
    mError("user:%s, failed to drop totp secret at line %d since %s", req.user, lino, tstrerror(code));
7,760✔
5048
  }
5049
  mndReleaseUser(pMnode, pUser);
8,148✔
5050
  mndUserFreeObj(&newUser);
8,148✔
5051
  tFreeSDropTotpSecretReq(&req);
8,148✔
5052
  TAOS_RETURN(code);
8,148✔
5053
}
5054

5055
bool mndIsTotpEnabledUser(SUserObj *pUser) {
5,035,681✔
5056
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
163,987,543✔
5057
    if (pUser->totpsecret[i] != 0) {
159,021,403✔
5058
      return true;
68,020✔
5059
    }
5060
  }
5061
  return false;
4,966,140✔
5062
}
5063

5064
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
152,825✔
5065
  SMnode   *pMnode = pReq->info.node;
152,825✔
5066
  SSdb     *pSdb = pMnode->pSdb;
152,825✔
5067
  int32_t   code = 0;
152,825✔
5068
  int32_t   lino = 0;
152,825✔
5069
  int32_t   numOfRows = 0;
152,825✔
5070
  SUserObj *pUser = NULL;
152,825✔
5071
  int32_t   cols = 0;
152,825✔
5072
  int8_t    flag = 0;
152,825✔
5073
  char     *pWrite = NULL;
152,825✔
5074
  char     *buf = NULL;
152,825✔
5075
  char     *varstr = NULL;
152,825✔
5076
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
152,825✔
5077
  int32_t   bufSize = sizeof(tBuf) - VARSTR_HEADER_SIZE;
152,825✔
5078

5079
  while (numOfRows < rows) {
1,331,104✔
5080
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
1,331,104✔
5081
    if (pShow->pIter == NULL) break;
1,331,104✔
5082

5083
    cols = 0;
1,178,279✔
5084
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5085
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
1,178,279✔
5086
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1,178,279✔
5087
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
1,178,279✔
5088

5089
    cols++;
1,178,279✔
5090
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5091
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
1,178,279✔
5092

5093
    cols++;
1,178,279✔
5094
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5095
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
1,178,279✔
5096

5097
    cols++;
1,178,279✔
5098
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5099
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
1,178,279✔
5100

5101
    cols++;
1,178,279✔
5102
    flag = pUser->createdb ? 1 : 0;
1,178,279✔
5103
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5104
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
1,178,279✔
5105

5106
    cols++;
1,178,279✔
5107
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5108
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
1,178,279✔
5109

5110
    cols++;
1,178,279✔
5111
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5112
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
1,178,279✔
5113
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
1,178,279✔
5114

5115
    cols++;
1,178,279✔
5116

5117
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
1,178,279✔
5118
    if (tlen != 0) {
1,178,279✔
5119
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
1,178,279✔
5120
      if (varstr == NULL) {
1,178,279✔
5121
        sdbRelease(pSdb, pUser);
×
5122
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
5123
      }
5124
      varDataSetLen(varstr, tlen);
1,178,279✔
5125
      (void)memcpy(varDataVal(varstr), buf, tlen);
1,178,279✔
5126

5127
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5128
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
1,178,279✔
5129

5130
      taosMemoryFreeClear(buf);
1,178,279✔
5131
    } else {
5132
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5133
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5134
    }
5135

5136
    cols++;
1,178,279✔
5137
    tlen = convertTimeRangesToStr(pUser, &buf);
1,178,279✔
5138
    if (tlen != 0) {
1,178,279✔
5139
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
1,178,279✔
5140
      if (varstr == NULL) {
1,178,279✔
5141
        sdbRelease(pSdb, pUser);
×
5142
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
5143
      }
5144
      varDataSetLen(varstr, tlen);
1,178,279✔
5145
      (void)memcpy(varDataVal(varstr), buf, tlen);
1,178,279✔
5146

5147
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,178,279✔
5148
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
1,178,279✔
5149

5150
      taosMemoryFreeClear(buf);
1,178,279✔
5151
    } else {
5152
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5153
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5154
    }
5155

5156
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,178,279✔
5157
      void  *pIter = NULL;
1,178,279✔
5158
      size_t klen = 0, tlen = 0;
1,178,279✔
5159
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
1,178,279✔
5160
      while ((pIter = taosHashIterate(pUser->roles, pIter))) {
2,669,577✔
5161
        char *roleName = taosHashGetKey(pIter, &klen);
1,491,298✔
5162
        tlen += snprintf(pBuf + tlen, bufSize - tlen, "%s,", roleName);
1,491,298✔
5163
      }
5164
      if (tlen > 0) {
1,178,279✔
5165
        pBuf[--tlen] = 0;  // remove last ','
1,178,108✔
5166
      } else {
5167
        pBuf[0] = 0;
171✔
5168
      }
5169
      varDataSetLen(tBuf, tlen);
1,178,279✔
5170
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
1,178,279✔
5171
    }
5172

5173
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,178,279✔
5174
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
1,178,279✔
5175
      size_t vlen = snprintf(pBuf, bufSize, "[%d,%d]", pUser->minSecLevel, pUser->maxSecLevel);
1,178,279✔
5176
      varDataSetLen(tBuf, vlen);
1,178,279✔
5177
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
1,178,279✔
5178
    }
5179

5180
    numOfRows++;
1,178,279✔
5181
    sdbRelease(pSdb, pUser);
1,178,279✔
5182
  }
5183

5184
  pShow->numOfRows += numOfRows;
152,825✔
5185
_exit:
152,825✔
5186
  taosMemoryFreeClear(buf);
152,825✔
5187
  taosMemoryFreeClear(varstr);
152,825✔
5188
  if (code < 0) {
152,825✔
5189
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5190
    TAOS_RETURN(code);
×
5191
  }
5192
  return numOfRows;
152,825✔
5193
}
5194

5195
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
16,967✔
5196
  int32_t numOfRows = 0;
16,967✔
5197
#ifdef TD_ENTERPRISE
5198
  SMnode   *pMnode = pReq->info.node;
16,967✔
5199
  SSdb     *pSdb = pMnode->pSdb;
16,967✔
5200
  SUserObj *pOperUser = NULL;
16,967✔
5201
  SUserObj *pUser = NULL;
16,967✔
5202
  int32_t   code = 0;
16,967✔
5203
  int32_t   lino = 0;
16,967✔
5204
  int32_t   cols = 0;
16,967✔
5205
  int8_t    flag = 0;
16,967✔
5206
  char     *pWrite = NULL;
16,967✔
5207
  char     *buf = NULL;
16,967✔
5208
  char     *varstr = NULL;
16,967✔
5209
  char     *pBuf = NULL;
16,967✔
5210
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
16,967✔
5211
  int32_t   bufSize = sizeof(tBuf) - VARSTR_HEADER_SIZE;
16,967✔
5212
  bool      showSecurityInfo = false;
16,967✔
5213

5214
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
16,967✔
5215
  if (pOperUser == NULL) {
16,967✔
5216
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_USER_FROM_CONN);
×
5217
  }
5218
  if (0 == mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_SHOW_SECURITY, 0, 0, NULL, NULL)) {
16,967✔
5219
    showSecurityInfo = true;
16,967✔
5220
  }
5221

5222
  while (numOfRows < rows) {
733,256✔
5223
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
733,256✔
5224
    if (pShow->pIter == NULL) break;
733,256✔
5225

5226
    cols = 0;
716,289✔
5227
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5228
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
716,289✔
5229
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
716,289✔
5230
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
716,289✔
5231

5232
    cols++;
716,289✔
5233
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5234
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
716,289✔
5235

5236
    cols++;
716,289✔
5237
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5238
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
716,289✔
5239

5240
    cols++;
716,289✔
5241
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5242
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
716,289✔
5243

5244
    cols++;
716,289✔
5245
    flag = pUser->createdb ? 1 : 0;
716,289✔
5246
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5247
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
716,289✔
5248

5249
    cols++;
716,289✔
5250
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5251
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
716,289✔
5252

5253
    cols++;
716,289✔
5254
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5255
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
716,289✔
5256
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
716,289✔
5257

5258
    cols++;
716,289✔
5259
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5260
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->changePass, false, pUser, pShow->pIter, _exit);
716,289✔
5261

5262
    cols++;
716,289✔
5263
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5264
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
716,289✔
5265
    STR_WITH_MAXSIZE_TO_VARSTR(pass, showSecurityInfo ? pUser->passwords[0].pass : "*",
716,289✔
5266
                               pShow->pMeta->pSchemas[cols].bytes);
5267
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, pShow->pIter, _exit);
716,289✔
5268

5269
    cols++;
716,289✔
5270
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5271
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sessionPerUser, false, pUser, pShow->pIter, _exit);
716,289✔
5272

5273
    cols++;
716,289✔
5274
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5275
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectTime, false, pUser, pShow->pIter, _exit);
716,289✔
5276

5277
    cols++;
716,289✔
5278
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5279
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectIdleTime, false, pUser, pShow->pIter, _exit);
716,289✔
5280

5281
    cols++;
716,289✔
5282
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5283
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->callPerSession, false, pUser, pShow->pIter, _exit);
716,289✔
5284

5285
    cols++;
716,289✔
5286
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5287
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerCall, false, pUser, pShow->pIter, _exit);
716,289✔
5288

5289
    cols++;
716,289✔
5290
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5291
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
716,289✔
5292

5293
    cols++;
716,289✔
5294
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5295
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLifeTime, false, pUser, pShow->pIter, _exit);
716,289✔
5296

5297
    cols++;
716,289✔
5298
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5299
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseTime, false, pUser, pShow->pIter, _exit);
716,289✔
5300

5301
    cols++;
716,289✔
5302
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5303
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseMax, false, pUser, pShow->pIter, _exit);
716,289✔
5304

5305
    cols++;
716,289✔
5306
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5307
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLockTime, false, pUser, pShow->pIter, _exit);
716,289✔
5308

5309
    cols++;
716,289✔
5310
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5311
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordGraceTime, false, pUser, pShow->pIter, _exit);
716,289✔
5312

5313
    cols++;
716,289✔
5314
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5315
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->inactiveAccountTime, false, pUser, pShow->pIter, _exit);
716,289✔
5316

5317
    cols++;
716,289✔
5318
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5319
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->allowTokenNum, false, pUser, pShow->pIter, _exit);
716,289✔
5320

5321
    cols++;
716,289✔
5322
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
716,289✔
5323
    if (tlen != 0) {
716,289✔
5324
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
716,289✔
5325
      if (varstr == NULL) {
716,289✔
5326
        sdbRelease(pSdb, pUser);
×
5327
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
5328
      }
5329
      varDataSetLen(varstr, tlen);
716,289✔
5330
      (void)memcpy(varDataVal(varstr), buf, tlen);
716,289✔
5331

5332
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5333
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
716,289✔
5334

5335
      taosMemoryFreeClear(buf);
716,289✔
5336
    } else {
5337
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5338
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5339
    }
5340

5341
    cols++;
716,289✔
5342
    tlen = convertTimeRangesToStr(pUser, &buf);
716,289✔
5343
    if (tlen != 0) {
716,289✔
5344
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
716,289✔
5345
      if (varstr == NULL) {
716,289✔
5346
        sdbRelease(pSdb, pUser);
×
5347
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
5348
      }
5349
      varDataSetLen(varstr, tlen);
716,289✔
5350
      (void)memcpy(varDataVal(varstr), buf, tlen);
716,289✔
5351

5352
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
716,289✔
5353
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
716,289✔
5354

5355
      taosMemoryFreeClear(buf);
716,289✔
5356
    } else {
5357
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5358
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5359
    }
5360

5361
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
716,289✔
5362
      void  *pIter = NULL;
716,289✔
5363
      size_t klen = 0, tlen = 0;
716,289✔
5364
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
716,289✔
5365
      while ((pIter = taosHashIterate(pUser->roles, pIter))) {
1,466,512✔
5366
        char *roleName = taosHashGetKey(pIter, &klen);
750,223✔
5367
        tlen += snprintf(pBuf + tlen, bufSize - tlen, "%s,", roleName);
750,223✔
5368
      }
5369
      if (tlen > 0) {
716,289✔
5370
        pBuf[--tlen] = 0;  // remove last ','
716,289✔
5371
      } else {
5372
        pBuf[0] = 0;
×
5373
      }
5374
      varDataSetLen(tBuf, tlen);
716,289✔
5375
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
716,289✔
5376
    }
5377

5378
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
716,289✔
5379
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
716,289✔
5380
      size_t vlen = snprintf(pBuf, bufSize, "[%d,%d]", pUser->minSecLevel, pUser->maxSecLevel);
716,289✔
5381
      varDataSetLen(tBuf, vlen);
716,289✔
5382
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
716,289✔
5383
    }
5384

5385
    numOfRows++;
716,289✔
5386
    sdbRelease(pSdb, pUser);
716,289✔
5387
  }
5388

5389
  pShow->numOfRows += numOfRows;
16,967✔
5390
_exit:
16,967✔
5391
  taosMemoryFreeClear(buf);
16,967✔
5392
  taosMemoryFreeClear(varstr);
16,967✔
5393
  mndReleaseUser(pMnode, pOperUser);
16,967✔
5394
  if (code < 0) {
16,967✔
5395
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5396
    TAOS_RETURN(code);
×
5397
  }
5398
#endif
5399
  return numOfRows;
16,967✔
5400
}
5401

5402
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
5403
  SSdb *pSdb = pMnode->pSdb;
×
5404
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
5405
}
×
5406

5407
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
×
5408
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
5409
  char   *value = taosHashIterate(hash, NULL);
×
5410
  char   *user = pUser->user;
×
5411
  int32_t code = 0;
×
5412
  int32_t lino = 0;
×
5413
  int32_t cols = 0;
×
5414
  int32_t numOfRows = *pNumOfRows;
×
5415

5416
  while (value != NULL) {
×
5417
    cols = 0;
×
5418
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
5419
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
×
5420
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5421
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
×
5422

5423
    char privilege[20] = {0};
×
5424
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
×
5425
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5426
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
×
5427

5428
    size_t keyLen = 0;
×
5429
    void  *key = taosHashGetKey(value, &keyLen);
×
5430

5431
    char dbName[TSDB_DB_NAME_LEN] = {0};
×
5432
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
×
5433
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
5434
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
×
5435
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5436
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
×
5437

5438
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
×
5439
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
×
5440
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
5441
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
×
5442
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5443
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
×
5444

5445
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
×
5446
      SNode  *pAst = NULL;
×
5447
      int32_t sqlLen = 0;
×
5448
      size_t  bufSz = strlen(value) + 1;
×
5449
      if (bufSz < 6) bufSz = 6;
×
5450
      TAOS_MEMORY_REALLOC(*sql, bufSz);
×
5451
      if (*sql == NULL) {
×
5452
        code = terrno;
×
5453
        goto _exit;
×
5454
      }
5455
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
5456
      if ((*condition) == NULL) {
×
5457
        code = terrno;
×
5458
        goto _exit;
×
5459
      }
5460

5461
      if (nodesStringToNode(value, &pAst) == 0) {
×
5462
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
×
5463
          sqlLen = snprintf(*sql, bufSz, "error");
×
5464
        }
5465
        nodesDestroyNode(pAst);
×
5466
      }
5467

5468
      if (sqlLen == 0) {
×
5469
        sqlLen = snprintf(*sql, bufSz, "error");
×
5470
      }
5471

5472
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
×
5473

5474
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5475
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
×
5476

5477
      char notes[2] = {0};
×
5478
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
5479
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5480
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
×
5481
    } else {
5482
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
5483
      if ((*condition) == NULL) {
×
5484
        code = terrno;
×
5485
        goto _exit;
×
5486
      }
5487
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
×
5488
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5489
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
×
5490

5491
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
×
5492
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
×
5493
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5494
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
×
5495
    }
5496

5497
    numOfRows++;
×
5498
    value = taosHashIterate(hash, value);
×
5499
  }
5500
  *pNumOfRows = numOfRows;
×
5501
_exit:
×
5502
  if (code < 0) {
×
5503
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5504
    sdbRelease(pSdb, pUser);
×
5505
    sdbCancelFetch(pSdb, pShow->pIter);
×
5506
  }
5507
  TAOS_RETURN(code);
×
5508
}
5509

5510
#if 0
5511
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
5512
  int32_t   code = 0;
5513
  int32_t   lino = 0;
5514
  SMnode   *pMnode = pReq->info.node;
5515
  SSdb     *pSdb = pMnode->pSdb;
5516
  int32_t   numOfRows = 0;
5517
  SUserObj *pUser = NULL;
5518
  int32_t   cols = 0;
5519
  char     *pWrite = NULL;
5520
  char     *condition = NULL;
5521
  char     *sql = NULL;
5522

5523
  bool fetchNextUser = pShow->restore ? false : true;
5524
  pShow->restore = false;
5525

5526
  while (numOfRows < rows) {
5527
    if (fetchNextUser) {
5528
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
5529
      if (pShow->pIter == NULL) break;
5530
    } else {
5531
      fetchNextUser = true;
5532
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
5533
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
5534
      if (!pUser) {
5535
        continue;
5536
      }
5537
    }
5538

5539
    int32_t numOfReadDbs = 0; //taosHashGetSize(pUser->readDbs);
5540
    int32_t numOfWriteDbs = 0; //taosHashGetSize(pUser->writeDbs);
5541
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
5542
    int32_t numOfReadTbs = taosHashGetSize(pUser->selectTbs);
5543
    int32_t numOfWriteTbs = taosHashGetSize(pUser->insertTbs);
5544
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
5545
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
5546
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
5547
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
5548
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
5549
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
5550
        rows) {
5551
      mInfo(
5552
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
5553
          "%d, alter tables %d, select views %d, write views %d, alter views %d",
5554
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
5555
          numOfReadViews, numOfWriteViews, numOfAlterViews);
5556
      pShow->restore = true;
5557
      sdbRelease(pSdb, pUser);
5558
      break;
5559
    }
5560

5561
    if (pUser->superUser) {
5562
      cols = 0;
5563
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5564
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5565
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5566
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5567

5568
      char privilege[20] = {0};
5569
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
5570
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5571
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5572

5573
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5574
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
5575
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5576
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5577

5578
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5579
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5580
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5581
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5582

5583
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5584
      if (condition == NULL) {
5585
        sdbRelease(pSdb, pUser);
5586
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5587
      }
5588
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5589
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5590
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5591

5592
      char notes[2] = {0};
5593
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5594
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5595
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5596

5597
      numOfRows++;
5598
    }
5599
#if 0
5600
    char *db = taosHashIterate(pUser->readDbs, NULL);
5601
    while (db != NULL) {
5602
      cols = 0;
5603
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5604
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5605
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5606
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5607

5608
      char privilege[20] = {0};
5609
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
5610
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5611
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5612

5613
      SName name = {0};
5614
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5615
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
5616
      if (code < 0) {
5617
        sdbRelease(pSdb, pUser);
5618
        sdbCancelFetch(pSdb, pShow->pIter);
5619
        TAOS_CHECK_GOTO(code, &lino, _exit);
5620
      }
5621
      (void)tNameGetDbName(&name, varDataVal(objName));
5622
      varDataSetLen(objName, strlen(varDataVal(objName)));
5623
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5624
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5625

5626
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5627
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5628
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5629
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5630

5631
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5632
      if (condition == NULL) {
5633
        sdbRelease(pSdb, pUser);
5634
        sdbCancelFetch(pSdb, pShow->pIter);
5635
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5636
      }
5637
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5638
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5639
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5640

5641
      char notes[2] = {0};
5642
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5643
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5644
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5645

5646
      numOfRows++;
5647
      db = taosHashIterate(pUser->readDbs, db);
5648
    }
5649

5650
    db = taosHashIterate(pUser->writeDbs, NULL);
5651
    while (db != NULL) {
5652
      cols = 0;
5653
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5654
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5655
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5656
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5657

5658
      char privilege[20] = {0};
5659
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
5660
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5661
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5662

5663
      SName name = {0};
5664
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5665
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
5666
      if (code < 0) {
5667
        sdbRelease(pSdb, pUser);
5668
        sdbCancelFetch(pSdb, pShow->pIter);
5669
        TAOS_CHECK_GOTO(code, &lino, _exit);
5670
      }
5671
      (void)tNameGetDbName(&name, varDataVal(objName));
5672
      varDataSetLen(objName, strlen(varDataVal(objName)));
5673
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5674
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5675

5676
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5677
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5678
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5679
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5680

5681
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5682
      if (condition == NULL) {
5683
        sdbRelease(pSdb, pUser);
5684
        sdbCancelFetch(pSdb, pShow->pIter);
5685
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5686
      }
5687
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5688
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5689
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5690

5691
      char notes[2] = {0};
5692
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5693
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5694
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5695

5696
      numOfRows++;
5697
      db = taosHashIterate(pUser->writeDbs, db);
5698
    }
5699
#endif
5700
    TAOS_CHECK_EXIT(mndLoopHash(pUser->selectTbs, "select", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5701

5702
    TAOS_CHECK_EXIT(mndLoopHash(pUser->insertTbs, "insert", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5703

5704
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5705

5706
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5707

5708
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5709

5710
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5711

5712
    char *topic = taosHashIterate(pUser->topics, NULL);
5713
    while (topic != NULL) {
5714
      cols = 0;
5715
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5716
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5717
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5718
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5719

5720
      char privilege[20] = {0};
5721
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
5722
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5723
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5724

5725
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
5726
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
5727
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
5728
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5729
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, pShow->pIter, _exit);
5730

5731
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5732
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5733
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5734
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5735

5736
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5737
      if (condition == NULL) {
5738
        sdbRelease(pSdb, pUser);
5739
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5740
      }
5741
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5742
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5743
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5744

5745
      char notes[2] = {0};
5746
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5747
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5748
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5749

5750
      numOfRows++;
5751
      topic = taosHashIterate(pUser->topics, topic);
5752
    }
5753

5754
    sdbRelease(pSdb, pUser);
5755
  }
5756

5757
  pShow->numOfRows += numOfRows;
5758
_exit:
5759
  taosMemoryFreeClear(condition);
5760
  taosMemoryFreeClear(sql);
5761
  if (code < 0) {
5762
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
5763
    TAOS_RETURN(code);
5764
  }
5765
  return numOfRows;
5766
}
5767
#endif
5768

5769
int32_t mndShowTablePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows, void *pObj,
489,996✔
5770
                               const char *principalName, SHashObj *privTbs, EPrivType privType, char *pBuf,
5771
                               int32_t bufSize, int32_t *pNumOfRows) {
5772
  int32_t     code = 0, lino = 0;
489,996✔
5773
  SMnode     *pMnode = pReq->info.node;
489,996✔
5774
  SSdb       *pSdb = pMnode->pSdb;
489,996✔
5775
  int32_t     cols = 0, qBufSize = bufSize - VARSTR_HEADER_SIZE;
489,996✔
5776
  int32_t     numOfRows = *pNumOfRows;
489,996✔
5777
  char       *qBuf = NULL;
489,996✔
5778
  char       *sql = NULL;
489,996✔
5779
  char        roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
489,996✔
5780
  const char *privName = privInfoGetName(privType);
489,996✔
5781

5782
  STR_WITH_MAXSIZE_TO_VARSTR(roleName, principalName, pShow->pMeta->pSchemas[cols].bytes);
489,996✔
5783

5784
  void *pIter = NULL;
489,996✔
5785
  while ((pIter = taosHashIterate(privTbs, pIter))) {
564,906✔
5786
    SPrivTblPolicies *pPolices = (SPrivTblPolicies *)pIter;
74,910✔
5787
    SArray           *tblPolicies = pPolices->policy;
74,910✔
5788

5789
    char   *key = taosHashGetKey(pPolices, NULL);
74,910✔
5790
    int32_t objType = PRIV_OBJ_UNKNOWN;
74,910✔
5791
    char    dbName[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
74,910✔
5792
    char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
74,910✔
5793
    if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
74,910✔
5794
      sdbRelease(pSdb, pObj);
×
5795
      sdbCancelFetch(pSdb, pShow->pIter);
×
5796
      TAOS_CHECK_EXIT(code);
×
5797
    }
5798

5799
    int32_t nTbPolicies = taosArrayGetSize(tblPolicies);
74,910✔
5800
    for (int32_t i = 0; i < nTbPolicies; ++i) {
149,820✔
5801
      SPrivTblPolicy *tbPolicy = (SPrivTblPolicy *)TARRAY_GET_ELEM(tblPolicies, i);
74,910✔
5802
      cols = 0;
74,910✔
5803
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
74,910✔
5804
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
74,910✔
5805

5806
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5807
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privName, pShow->pMeta->pSchemas[cols].bytes);
74,910✔
5808
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5809
      }
5810

5811
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5812
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
74,910✔
5813
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5814
      }
5815

5816
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5817
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
74,910✔
5818
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5819
      }
5820

5821
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5822
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
74,910✔
5823
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5824
      }
5825
      // condition
5826
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5827
        SNode  *pAst = NULL;
74,910✔
5828
        int32_t sqlLen = 0;
74,910✔
5829
        qBuf = POINTER_SHIFT(pBuf, VARSTR_HEADER_SIZE);
74,910✔
5830
        if (tbPolicy->condLen > 0) {
74,910✔
5831
          if (nodesStringToNode(tbPolicy->cond, &pAst) == 0) {
74,910✔
5832
            if (nodesNodeToSQLFormat(pAst, qBuf, qBufSize, &sqlLen, true) != 0) {
74,910✔
5833
              sqlLen = tsnprintf(qBuf, qBufSize, "error");
×
5834
            }
5835
            nodesDestroyNode(pAst);
74,910✔
5836
          }
5837
          if (sqlLen == 0) {
74,910✔
5838
            sqlLen = tsnprintf(qBuf, qBufSize, "error");
×
5839
          }
5840
        } else {
5841
          sqlLen = tsnprintf(qBuf, qBufSize, "");
×
5842
        }
5843
        varDataSetLen(pBuf, sqlLen);
74,910✔
5844
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5845
      }
5846
      // notes
5847
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5848
        STR_WITH_MAXSIZE_TO_VARSTR((pBuf), "", 2);
74,910✔
5849
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5850
      }
5851
      // columns
5852
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5853
        SArray *pCols = tbPolicy->cols;
74,910✔
5854
        int32_t nCols = taosArrayGetSize(pCols);
74,910✔
5855
        int32_t totalLen = 0;
74,910✔
5856
        qBuf = POINTER_SHIFT(pBuf, VARSTR_HEADER_SIZE);
74,910✔
5857
        for (int32_t j = 0; j < nCols; ++j) {
111,001✔
5858
          SColNameFlag *pCol = (SColNameFlag *)TARRAY_GET_ELEM(pCols, j);
36,091✔
5859
          char          tmpBuf[TSDB_COL_NAME_LEN + 16] = {0};
36,091✔
5860
          int32_t       tmpLen = 0;
36,091✔
5861
          if (IS_MASK_ON(pCol)) {
36,091✔
5862
            tmpLen = snprintf(tmpBuf, sizeof(tmpBuf), "mask(%s)%s", pCol->colName, j == nCols - 1 ? "" : ",");
×
5863
          } else {
5864
            tmpLen = snprintf(tmpBuf, sizeof(tmpBuf), "%s%s", pCol->colName, j == nCols - 1 ? "" : ",");
36,091✔
5865
          }
5866
          if (totalLen + tmpLen > qBufSize) {
36,091✔
5867
            break;
×
5868
          }
5869
          (void)memcpy(POINTER_SHIFT(qBuf, totalLen), tmpBuf, tmpLen);
36,091✔
5870
          totalLen += tmpLen;
36,091✔
5871
        }
5872
        varDataSetLen(pBuf, totalLen);
74,910✔
5873
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5874
      }
5875
      // update_time
5876
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
74,910✔
5877
        char updateTime[40] = {0};
74,910✔
5878
        (void)formatTimestampLocal(updateTime, sizeof(updateTime), tbPolicy->updateUs, TSDB_TIME_PRECISION_MICRO);
74,910✔
5879
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, updateTime, pShow->pMeta->pSchemas[cols].bytes);
74,910✔
5880
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
74,910✔
5881
      }
5882
      ++numOfRows;
74,910✔
5883
    }
5884
  }
5885
  *pNumOfRows = numOfRows;
489,996✔
5886
_exit:
489,996✔
5887
  TAOS_RETURN(code);
489,996✔
5888
}
5889

5890
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
19,960✔
5891
  int32_t   code = 0, lino = 0;
19,960✔
5892
  SMnode   *pMnode = pReq->info.node;
19,960✔
5893
  SSdb     *pSdb = pMnode->pSdb;
19,960✔
5894
  int32_t   numOfRows = 0;
19,960✔
5895
  int32_t   cols = 0;
19,960✔
5896
  SUserObj *pObj = NULL;
19,960✔
5897
  char     *pBuf = NULL, *qBuf = NULL;
19,960✔
5898
  char     *sql = NULL;
19,960✔
5899
  char      roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
19,960✔
5900
  int32_t   bufSize = TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE;
19,960✔
5901

5902
  bool fetchNextInstance = pShow->restore ? false : true;
19,960✔
5903
  pShow->restore = false;
19,960✔
5904

5905
  while (numOfRows < rows) {
133,128✔
5906
    if (fetchNextInstance) {
133,128✔
5907
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pObj);
133,128✔
5908
      if (pShow->pIter == NULL) break;
133,128✔
5909
    } else {
5910
      fetchNextInstance = true;
×
5911
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
5912
      if (!(pObj = sdbAcquire(pSdb, SDB_USER, pKey))) {
×
5913
        continue;
×
5914
      }
5915
    }
5916

5917
    // count total privileges for current user
5918
    int32_t nSysPrivileges = privPopCnt(&pObj->sysPrivs);
113,168✔
5919
    int32_t nObjPrivileges = 0;
113,168✔
5920
    void   *pIter = NULL;
113,168✔
5921
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
1,441,368✔
5922
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
1,328,200✔
5923
      nObjPrivileges += privPopCnt(&pPolices->policy);
2,656,400✔
5924
    }
5925
    int32_t nTblPrivileges = privTblPrivCnt(pObj->selectTbs);
113,168✔
5926
    nTblPrivileges += privTblPrivCnt(pObj->insertTbs);
113,168✔
5927
    nTblPrivileges += privTblPrivCnt(pObj->updateTbs);
113,168✔
5928
    nTblPrivileges += privTblPrivCnt(pObj->deleteTbs);
113,168✔
5929

5930
    int32_t totalPrivileges = nSysPrivileges + nObjPrivileges + nTblPrivileges;
113,168✔
5931

5932
    if (numOfRows + totalPrivileges >= rows) {
113,168✔
5933
      if (totalPrivileges >= SHOW_PRIVILEGES_STEP_SIZE) {
×
5934
        mError("user:%s, has too many privileges:%d to show", pObj->name, totalPrivileges);
×
5935
        sdbRelease(pSdb, pObj);
×
5936
        TAOS_CHECK_EXIT(TSDB_CODE_MND_TOO_MANY_PRIVS);
×
5937
      }
5938
      pShow->restore = true;
×
5939
      sdbRelease(pSdb, pObj);
×
5940
      break;
×
5941
    }
5942

5943
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
113,168✔
5944
      sdbRelease(pSdb, pObj);
×
5945
      TAOS_CHECK_EXIT(terrno);
×
5946
    }
5947

5948
    cols = 0;
113,168✔
5949
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
113,168✔
5950

5951
    // system privileges
5952
    SPrivIter privIter = {0};
113,168✔
5953
    privIterInit(&privIter, &pObj->sysPrivs);
113,168✔
5954
    SPrivInfo *pPrivInfo = NULL;
113,168✔
5955
    while (privIterNext(&privIter, &pPrivInfo)) {
150,433✔
5956
      cols = 0;
37,265✔
5957
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
37,265✔
5958
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
37,265✔
5959

5960
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
37,265✔
5961
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
37,265✔
5962
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
37,265✔
5963
      }
5964
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
37,265✔
5965
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(PRIV_OBJ_CLUSTER), pShow->pMeta->pSchemas[cols].bytes);
37,265✔
5966
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
37,265✔
5967
      }
5968
      // skip db, table, condition, notes, columns, update_time
5969
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 6);
260,855✔
5970
      numOfRows++;
37,265✔
5971
    }
5972

5973
    // object privileges
5974
    pIter = NULL;
113,168✔
5975
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
1,441,368✔
5976
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
1,328,200✔
5977

5978
      char   *key = taosHashGetKey(pPolices, NULL);
1,328,200✔
5979
      int32_t objType = PRIV_OBJ_UNKNOWN;
1,328,200✔
5980
      char    dbName[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,328,200✔
5981
      char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,328,200✔
5982
      if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
1,328,200✔
5983
        sdbRelease(pSdb, pObj);
×
5984
        TAOS_CHECK_EXIT(code);
×
5985
      }
5986

5987
      SPrivIter privIter = {0};
1,328,200✔
5988
      privIterInit(&privIter, &pPolices->policy);
1,328,200✔
5989
      SPrivInfo *pPrivInfo = NULL;
1,328,200✔
5990
      while (privIterNext(&privIter, &pPrivInfo)) {
4,054,822✔
5991
        cols = 0;
2,726,622✔
5992
        SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2,726,622✔
5993
        COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
2,726,622✔
5994

5995
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,726,622✔
5996
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
2,726,622✔
5997
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,726,622✔
5998
        }
5999

6000
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,726,622✔
6001
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
2,726,622✔
6002
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,726,622✔
6003
        }
6004

6005
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,726,622✔
6006
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
2,726,622✔
6007
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,726,622✔
6008
        }
6009

6010
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,726,622✔
6011
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
2,726,622✔
6012
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,726,622✔
6013
        }
6014

6015
        // skip condition, notes, columns, update_time
6016
        COL_DATA_SET_EMPTY_VARCHAR(pBuf, 4);
13,633,110✔
6017

6018
        numOfRows++;
2,726,622✔
6019
      }
6020
    }
6021

6022
    // table level privileges
6023
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->selectTbs,
113,168✔
6024
                                           PRIV_TBL_SELECT, pBuf, bufSize, &numOfRows));
6025
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->insertTbs,
113,168✔
6026
                                           PRIV_TBL_INSERT, pBuf, bufSize, &numOfRows));
6027
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->updateTbs,
113,168✔
6028
                                           PRIV_TBL_UPDATE, pBuf, bufSize, &numOfRows));
6029
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->deleteTbs,
113,168✔
6030
                                           PRIV_TBL_DELETE, pBuf, bufSize, &numOfRows));
6031
    sdbRelease(pSdb, pObj);
113,168✔
6032
  }
6033

6034
  pShow->numOfRows += numOfRows;
19,960✔
6035
_exit:
19,960✔
6036
  taosMemoryFreeClear(pBuf);
19,960✔
6037
  taosMemoryFreeClear(sql);
19,960✔
6038
  if (code < 0) {
19,960✔
6039
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
6040
    TAOS_RETURN(code);
×
6041
  }
6042
  return numOfRows;
19,960✔
6043
}
6044

6045
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
6046
  SSdb *pSdb = pMnode->pSdb;
×
6047
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
6048
}
×
6049

6050
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
28,758,542✔
6051
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
6052
  int32_t           code = 0;
28,758,542✔
6053
  int32_t           lino = 0;
28,758,542✔
6054
  int32_t           rspLen = 0;
28,759,369✔
6055
  void             *pRsp = NULL;
28,759,369✔
6056
  SUserAuthBatchRsp batchRsp = {0};
28,759,369✔
6057

6058
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
28,759,369✔
6059
  if (batchRsp.pArray == NULL) {
28,758,896✔
6060
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
6061
  }
6062
  int64_t now = taosGetTimestampMs();
28,759,181✔
6063
  for (int32_t i = 0; i < numOfUses; ++i) {
58,117,241✔
6064
    SUserObj *pUser = NULL;
29,358,060✔
6065
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
29,358,060✔
6066
    if (pUser == NULL) {
29,358,060✔
6067
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
11,452✔
6068
        SGetUserAuthRsp rsp = {.dropped = 1};
11,452✔
6069
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
11,452✔
6070
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
22,904✔
6071
      }
6072
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
11,452✔
6073
      code = 0;
11,452✔
6074
      continue;
11,452✔
6075
    }
6076

6077
    pUsers[i].version = ntohl(pUsers[i].version);
29,346,608✔
6078
    if ((pUser->authVersion <= pUsers[i].version) && (ipWhiteListVer == pMnode->ipWhiteVer) &&
29,345,964✔
6079
        !mndNeedRetrieveRole(pUser)) {
×
6080
      mndReleaseUser(pMnode, pUser);
×
6081
      continue;
×
6082
    }
6083

6084
    SGetUserAuthRsp rsp = {0};
29,346,608✔
6085
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
29,346,420✔
6086
    if (code) {
29,346,608✔
6087
      mndReleaseUser(pMnode, pUser);
×
6088
      tFreeSGetUserAuthRsp(&rsp);
×
6089
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
6090
    }
6091

6092
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
58,693,216✔
6093
      code = terrno;
×
6094
      mndReleaseUser(pMnode, pUser);
×
6095
      tFreeSGetUserAuthRsp(&rsp);
×
6096
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
6097
    }
6098
    pUser->lastRoleRetrieve = now;  // update user's last retrieve time
29,346,608✔
6099
    mndReleaseUser(pMnode, pUser);
29,346,608✔
6100
  }
6101

6102
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
28,759,181✔
6103
    *ppRsp = NULL;
×
6104
    *pRspLen = 0;
×
6105

6106
    tFreeSUserAuthBatchRsp(&batchRsp);
×
6107
    return 0;
×
6108
  }
6109

6110
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
28,759,369✔
6111
  if (rspLen < 0) {
28,758,994✔
6112
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
6113
  }
6114
  pRsp = taosMemoryMalloc(rspLen);
28,758,994✔
6115
  if (pRsp == NULL) {
28,757,449✔
6116
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
6117
  }
6118
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
28,757,449✔
6119
  if (rspLen < 0) {
28,758,994✔
6120
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
6121
  }
6122
_OVER:
28,758,994✔
6123
  tFreeSUserAuthBatchRsp(&batchRsp);
28,759,369✔
6124
  if (code < 0) {
28,757,005✔
6125
    for (int32_t i = 0; i < numOfUses; ++i) {
×
6126
      SUserObj *pUser = NULL;
×
6127
      if (mndAcquireUser(pMnode, pUsers[i].user, &pUser) != 0) {
×
6128
        continue;
×
6129
      }
6130
      pUser->lastRoleRetrieve = 0;  // reset last retrieve time on error
×
6131
      mndReleaseUser(pMnode, pUser);
×
6132
    }
6133
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
6134
    taosMemoryFreeClear(pRsp);
×
6135
    rspLen = 0;
×
6136
  }
6137
  *ppRsp = pRsp;
28,757,005✔
6138
  *pRspLen = rspLen;
28,758,458✔
6139

6140
  TAOS_RETURN(code);
28,755,961✔
6141
}
6142

6143
int32_t mndUserDropRole(SMnode *pMnode, STrans *pTrans, SRoleObj *pObj) {
1,710✔
6144
  int32_t   code = 0, lino = 0;
1,710✔
6145
  SSdb     *pSdb = pMnode->pSdb;
1,710✔
6146
  SUserObj *pUser = NULL;
1,710✔
6147
  void     *pIter = NULL;
1,710✔
6148

6149
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
5,814✔
6150
    SHashObj *pRole = taosHashGet(pUser->roles, pObj->name, strlen(pObj->name) + 1);
4,104✔
6151
    if (!pRole) {
4,104✔
6152
      sdbRelease(pSdb, pUser);
3,420✔
6153
      pUser = NULL;
3,420✔
6154
      continue;
3,420✔
6155
    }
6156

6157
    SUserObj newUser = {0};
684✔
6158
    TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
684✔
6159
    code = taosHashRemove(newUser.roles, pObj->name, strlen(pObj->name) + 1);
684✔
6160
    if (code == TSDB_CODE_NOT_FOUND) {
684✔
6161
      sdbRelease(pSdb, pUser);
×
6162
      pUser = NULL;
×
6163
      mndUserFreeObj(&newUser);
×
6164
      continue;
×
6165
    }
6166
    if (code != 0) {
684✔
6167
      mndUserFreeObj(&newUser);
×
6168
      TAOS_CHECK_EXIT(code);
×
6169
    }
6170
    SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
684✔
6171
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
684✔
6172
      mndUserFreeObj(&newUser);
×
6173
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6174
    }
6175
    if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY))) {
684✔
6176
      mndUserFreeObj(&newUser);
×
6177
      TAOS_CHECK_EXIT(code);
×
6178
    }
6179
    sdbRelease(pSdb, pUser);
684✔
6180
    pUser = NULL;
684✔
6181
    mndUserFreeObj(&newUser);
684✔
6182
  }
6183
_exit:
1,710✔
6184
  if (pIter) sdbCancelFetch(pSdb, pIter);
1,710✔
6185
  if (pUser) sdbRelease(pSdb, pUser);
1,710✔
6186
  if (code < 0) {
1,710✔
6187
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
6188
  }
6189
  TAOS_RETURN(code);
1,710✔
6190
}
6191

6192
static int32_t mndUserPrivHashRemoveDb(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, bool *found) {
4,717,355✔
6193
  void *pVal = NULL;
4,717,355✔
6194
  while ((pVal = taosHashIterate(pHash, pVal))) {
4,975,364✔
6195
    size_t klen = 0;
258,009✔
6196
    char  *key = taosHashGetKey(pVal, &klen);
258,009✔
6197
    if (key && privDbKeyMatch(key, dbFName, dbFNameLen)) {
516,018✔
6198
      TAOS_CHECK_RETURN(taosHashRemove(pHash, key, klen));
33,355✔
6199
      if (found) *found = true;
33,355✔
6200
    }
6201
  }
6202
  TAOS_RETURN(0);
4,717,355✔
6203
}
6204

6205
static int32_t mndUserRemoveDbPrivsImpl(SUserObj *pUser, const char *key, int32_t keyLen, bool *pFound) {
943,471✔
6206
  bool found = false;
943,471✔
6207
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->objPrivs, (char *)key, keyLen, found ? NULL : &found));
943,471✔
6208
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->selectTbs, (char *)key, keyLen, found ? NULL : &found));
943,471✔
6209
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->insertTbs, (char *)key, keyLen, found ? NULL : &found));
943,471✔
6210
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->updateTbs, (char *)key, keyLen, found ? NULL : &found));
943,471✔
6211
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->deleteTbs, (char *)key, keyLen, found ? NULL : &found));
943,471✔
6212
  if (taosHashGet(pUser->ownedDbs, key, keyLen)) {
943,471✔
6213
    TAOS_CHECK_RETURN(taosHashRemove(pUser->ownedDbs, (char *)key, keyLen));
×
6214
    if (!found) found = true;
×
6215
  }
6216
  if (pFound) *pFound = found;
943,471✔
6217
  TAOS_RETURN(0);
943,471✔
6218
}
6219

6220
static int32_t mndUserRemoveDbPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
825,521✔
6221
                                    SSHashObj **ppUsers) {
6222
  int32_t    code = 0, lino = 0;
825,521✔
6223
  SSdb      *pSdb = pMnode->pSdb;
825,521✔
6224
  void      *pIter = NULL;
825,521✔
6225
  SUserObj  *pUser = NULL;
825,521✔
6226
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
825,521✔
6227
  bool       output = (ppUsers != NULL);
825,521✔
6228
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
1,768,642✔
6229
    bool found = false;
943,121✔
6230
    TAOS_CHECK_EXIT(mndUserRemoveDbPrivsImpl(pUser, key, keyLen, &found));
943,121✔
6231
    if (!found) {
943,121✔
6232
      sdbRelease(pSdb, pUser);
923,885✔
6233
      pUser = NULL;
923,885✔
6234
      continue;
923,885✔
6235
    }
6236

6237
    if (output) {
19,236✔
6238
      if (!pUsers) {
700✔
6239
        TSDB_CHECK_NULL(pUsers = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
350✔
6240
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
6241
        tSimpleHashSetFreeFp(pUsers, (_hash_free_fn_t)mndUserFreeObj);
350✔
6242
        *ppUsers = pUsers;
350✔
6243
      }
6244
      void   *pVal = NULL;
700✔
6245
      int32_t userLen = strlen(pUser->name) + 1;
700✔
6246
      if ((pVal = tSimpleHashGet(pUsers, pUser->name, userLen)) != NULL) {
700✔
6247
        TAOS_CHECK_EXIT(mndUserRemoveDbPrivsImpl((SUserObj *)pVal, key, keyLen, NULL));
350✔
6248
      } else {
6249
        SUserObj newUser = {0};
350✔
6250
        if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
350✔
6251
          mndUserFreeObj(&newUser);
×
6252
          TAOS_CHECK_EXIT(code);
×
6253
        }
6254
        if ((code = tSimpleHashPut(pUsers, pUser->name, userLen, &newUser, sizeof(SUserObj)))) {
350✔
6255
          mndUserFreeObj(&newUser);
×
6256
          TAOS_CHECK_EXIT(code);
×
6257
        }
6258
      }
6259
    } else {
6260
      int64_t now = taosGetTimestampMs();
18,536✔
6261
      taosWLockLatch(&pUser->lock);
18,536✔
6262
      pUser->updateTime = now;
18,536✔
6263
      ++pUser->authVersion;
18,536✔
6264
      taosWUnLockLatch(&pUser->lock);
18,536✔
6265

6266
      SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
18,536✔
6267
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
18,536✔
6268
        TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6269
      }
6270
      TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
18,536✔
6271
    }
6272

6273
    sdbRelease(pSdb, pUser);
19,236✔
6274
    pUser = NULL;
19,236✔
6275
  }
6276
_exit:
825,521✔
6277
  if (pUser != NULL) sdbRelease(pSdb, pUser);
825,521✔
6278
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
825,521✔
6279
  TAOS_RETURN(code);
825,521✔
6280
}
6281

6282
static int32_t mndRoleRemoveDbPrivsImpl(SRoleObj *pRole, const char *key, int32_t keyLen, bool *pFound) {
×
6283
  bool found = false;
×
6284
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->objPrivs, (char *)key, keyLen, found ? NULL : &found));
×
6285
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->selectTbs, (char *)key, keyLen, found ? NULL : &found));
×
6286
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->insertTbs, (char *)key, keyLen, found ? NULL : &found));
×
6287
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->updateTbs, (char *)key, keyLen, found ? NULL : &found));
×
6288
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->deleteTbs, (char *)key, keyLen, found ? NULL : &found));
×
6289
  if (pFound) *pFound = found;
×
6290
  TAOS_RETURN(0);
×
6291
}
6292

6293
static int32_t mndRoleRemoveDbPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
×
6294
                                    SSHashObj **ppRoles) {
6295
  int32_t    code = 0, lino = 0;
×
6296
  SSdb      *pSdb = pMnode->pSdb;
×
6297
  void      *pIter = NULL;
×
6298
  SRoleObj  *pRole = NULL;
×
6299
  SSHashObj *pRoles = ppRoles ? *ppRoles : NULL;
×
6300
  bool       output = (ppRoles != NULL);
×
6301
  while ((pIter = sdbFetch(pSdb, SDB_ROLE, pIter, (void **)&pRole))) {
×
6302
    bool found = false;
×
6303
    TAOS_CHECK_EXIT(mndRoleRemoveDbPrivsImpl(pRole, key, keyLen, &found));
×
6304
    if (!found) {
×
6305
      sdbRelease(pSdb, pRole);
×
6306
      pRole = NULL;
×
6307
      continue;
×
6308
    }
6309

6310
    if (output) {
×
6311
      if (!pRoles) {
×
6312
        TSDB_CHECK_NULL(pRoles = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
×
6313
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
6314
        tSimpleHashSetFreeFp(pRoles, (_hash_free_fn_t)mndRoleFreeObj);
×
6315
        *ppRoles = pRoles;
×
6316
      }
6317
      void   *pVal = NULL;
×
6318
      int32_t roleLen = strlen(pRole->name) + 1;
×
6319
      if ((pVal = tSimpleHashGet(pRoles, pRole->name, roleLen)) != NULL) {
×
6320
        TAOS_CHECK_EXIT(mndRoleRemoveDbPrivsImpl((SRoleObj *)pVal, key, keyLen, NULL));
×
6321
      } else {
6322
        SRoleObj newRole = {0};
×
6323
        if ((code = mndRoleDupObj(pRole, &newRole)) != 0) {
×
6324
          mndRoleFreeObj(&newRole);
×
6325
          TAOS_CHECK_EXIT(code);
×
6326
        }
6327
        if ((code = tSimpleHashPut(pRoles, pRole->name, roleLen, &newRole, sizeof(SRoleObj)))) {
×
6328
          mndRoleFreeObj(&newRole);
×
6329
          TAOS_CHECK_EXIT(code);
×
6330
        }
6331
      }
6332
    } else {
6333
      int64_t now = taosGetTimestampMs();
×
6334
      taosWLockLatch(&pRole->lock);
×
6335
      pRole->updateTime = now;
×
6336
      ++pRole->version;
×
6337
      taosWUnLockLatch(&pRole->lock);
×
6338

6339
      SSdbRaw *pCommitRaw = mndRoleActionEncode(pRole);
×
6340
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
6341
        TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6342
      }
6343
      TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
6344
    }
6345

6346
    sdbRelease(pSdb, pRole);
×
6347
    pRole = NULL;
×
6348
  }
6349
_exit:
×
6350
  if (pRole != NULL) sdbRelease(pSdb, pRole);
×
6351
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
×
6352
  TAOS_RETURN(code);
×
6353
}
6354

6355
int32_t mndPrincipalRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers, SSHashObj **ppRoles) {
825,521✔
6356
  TAOS_RETURN(mndUserRemoveDbPrivs(pMnode, pTrans, pDb->name, strlen(pDb->name), ppUsers));
825,521✔
6357
  return mndRoleRemoveDbPrivs(pMnode, pTrans, pDb->name, strlen(pDb->name), ppRoles);
6358
}
6359

6360
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
523,997✔
6361
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, stb, PRIV_OBJ_TBL);
523,997✔
6362
}
6363

6364
static int32_t mndUserRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
813,522✔
6365
                                     EPrivObjType objType) {
6366
  int32_t   code = 0, lino = 0;
813,522✔
6367
  SSdb     *pSdb = pMnode->pSdb;
813,522✔
6368
  void     *pIter = NULL;
813,522✔
6369
  SUserObj *pUser = NULL;
813,522✔
6370
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
1,639,860✔
6371
    bool found = false;
826,338✔
6372
    if (taosHashGet(pUser->objPrivs, key, keyLen)) {
826,338✔
6373
      TAOS_CHECK_EXIT(taosHashRemove(pUser->objPrivs, key, keyLen));
3,279✔
6374
      found = true;
3,279✔
6375
    }
6376
    if (objType == PRIV_OBJ_TBL) {
826,338✔
6377
      SPrivTblPolicies *pTblPrivs = taosHashGet(pUser->selectTbs, key, keyLen);
524,898✔
6378
      if (pTblPrivs) {
524,898✔
6379
        TAOS_CHECK_EXIT(taosHashRemove(pUser->selectTbs, key, keyLen));
×
6380
        found = true;
×
6381
      }
6382
      pTblPrivs = taosHashGet(pUser->insertTbs, key, keyLen);
524,898✔
6383
      if (pTblPrivs) {
524,898✔
6384
        TAOS_CHECK_EXIT(taosHashRemove(pUser->insertTbs, key, keyLen));
×
6385
        found = true;
×
6386
      }
6387
      pTblPrivs = taosHashGet(pUser->updateTbs, key, keyLen);
524,898✔
6388
      if (pTblPrivs) {
524,898✔
6389
        TAOS_CHECK_EXIT(taosHashRemove(pUser->updateTbs, key, keyLen));
×
6390
        found = true;
×
6391
      }
6392
      pTblPrivs = taosHashGet(pUser->deleteTbs, key, keyLen);
524,898✔
6393
      if (pTblPrivs) {
524,898✔
6394
        TAOS_CHECK_EXIT(taosHashRemove(pUser->deleteTbs, key, keyLen));
×
6395
        found = true;
×
6396
      }
6397
    }
6398
    if (!found) {
826,338✔
6399
      sdbRelease(pSdb, pUser);
823,059✔
6400
      pUser = NULL;
823,059✔
6401
      continue;
823,059✔
6402
    }
6403

6404
    int64_t now = taosGetTimestampMs();
3,279✔
6405
    taosWLockLatch(&pUser->lock);
3,279✔
6406
    pUser->updateTime = now;
3,279✔
6407
    ++pUser->authVersion;
3,279✔
6408
    taosWUnLockLatch(&pUser->lock);
3,279✔
6409

6410
    SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
3,279✔
6411
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
3,279✔
6412
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6413
    }
6414
    TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
3,279✔
6415

6416
    sdbRelease(pSdb, pUser);
3,279✔
6417
    pUser = NULL;
3,279✔
6418
  }
6419
_exit:
813,522✔
6420
  if (pUser != NULL) sdbRelease(pSdb, pUser);
813,522✔
6421
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
813,522✔
6422
  TAOS_RETURN(code);
813,522✔
6423
}
6424

6425
static int32_t mndRoleRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
813,522✔
6426
                                     EPrivObjType objType) {
6427
  int32_t   code = 0, lino = 0;
813,522✔
6428
  SSdb     *pSdb = pMnode->pSdb;
813,522✔
6429
  void     *pIter = NULL;
813,522✔
6430
  SRoleObj *pRole = NULL;
813,522✔
6431
  while ((pIter = sdbFetch(pSdb, SDB_ROLE, pIter, (void **)&pRole))) {
5,694,654✔
6432
    // inherit system roles have no direct privileges
6433
    if (taosStrncasecmp(pRole->name, "sys", 3) == 0) {
4,881,132✔
6434
      sdbRelease(pSdb, pRole);
4,881,132✔
6435
      pRole = NULL;
4,881,132✔
6436
      continue;
4,881,132✔
6437
    }
6438
    bool found = false;
×
6439
    if (taosHashGet(pRole->objPrivs, key, keyLen)) {
×
6440
      TAOS_CHECK_EXIT(taosHashRemove(pRole->objPrivs, key, keyLen));
×
6441
      found = true;
×
6442
    }
6443

6444
    if (objType == PRIV_OBJ_TBL) {
×
6445
      SPrivTblPolicies *pTblPrivs = taosHashGet(pRole->selectTbs, key, keyLen);
×
6446
      if (pTblPrivs) {
×
6447
        TAOS_CHECK_EXIT(taosHashRemove(pRole->selectTbs, key, keyLen));
×
6448
        found = true;
×
6449
      }
6450
      pTblPrivs = taosHashGet(pRole->insertTbs, key, keyLen);
×
6451
      if (pTblPrivs) {
×
6452
        TAOS_CHECK_EXIT(taosHashRemove(pRole->insertTbs, key, keyLen));
×
6453
        found = true;
×
6454
      }
6455
      pTblPrivs = taosHashGet(pRole->updateTbs, key, keyLen);
×
6456
      if (pTblPrivs) {
×
6457
        TAOS_CHECK_EXIT(taosHashRemove(pRole->updateTbs, key, keyLen));
×
6458
        found = true;
×
6459
      }
6460
      pTblPrivs = taosHashGet(pRole->deleteTbs, key, keyLen);
×
6461
      if (pTblPrivs) {
×
6462
        TAOS_CHECK_EXIT(taosHashRemove(pRole->deleteTbs, key, keyLen));
×
6463
        found = true;
×
6464
      }
6465
    }
6466
    if (!found) {
×
6467
      sdbRelease(pSdb, pRole);
×
6468
      pRole = NULL;
×
6469
      continue;
×
6470
    }
6471
    int64_t now = taosGetTimestampMs();
×
6472
    taosWLockLatch(&pRole->lock);
×
6473
    pRole->updateTime = now;
×
6474
    ++pRole->version;
×
6475
    taosWUnLockLatch(&pRole->lock);
×
6476

6477
    SSdbRaw *pCommitRaw = mndRoleActionEncode(pRole);
×
6478
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
6479
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6480
    }
6481
    TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
6482

6483
    sdbRelease(pSdb, pRole);
×
6484
    pRole = NULL;
×
6485
  }
6486
_exit:
813,522✔
6487
  if (pRole != NULL) sdbRelease(pSdb, pRole);
813,522✔
6488
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
813,522✔
6489
  TAOS_RETURN(code);
813,522✔
6490
}
6491

6492
static int32_t mndPrincipalRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, char *objFName, EPrivObjType objType) {
813,522✔
6493
  int32_t code = 0;
813,522✔
6494
  char    key[TSDB_PRIV_MAX_KEY_LEN] = {0};
813,522✔
6495
  int32_t keyLen = snprintf(key, sizeof(key), "%d.%s", objType, objFName) + 1;
813,522✔
6496
  TAOS_CHECK_RETURN(mndUserRemoveObjPrivs(pMnode, pTrans, key, keyLen, objType));
813,522✔
6497
  TAOS_CHECK_RETURN(mndRoleRemoveObjPrivs(pMnode, pTrans, key, keyLen, objType));
813,522✔
6498
  TAOS_RETURN(code);
813,522✔
6499
}
6500

6501
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
168,058✔
6502
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, view, PRIV_OBJ_VIEW);
168,058✔
6503
}
6504

6505
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
121,467✔
6506
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, topic, PRIV_OBJ_TOPIC);
121,467✔
6507
}
6508

6509
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
6510
  // ver = 0, disable ip white list
6511
  // ver > 0, enable ip white list
6512
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
6513
}
6514

6515
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
6516
  // ver = 0, disable datetime white list
6517
  // ver > 0, enable datetime white list
6518
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
6519
}
6520

6521
#ifdef TD_ENTERPRISE
6522
/**
6523
 * @brief Check if there is at least one valid user with SYSDBA, SYSSEC or SYSAUDIT role in the system, if not, return
6524
 * error code.
6525
 *
6526
 * @param pMnode
6527
 * @param skipUser
6528
 * @param skipRole  0 or T_ROLE_SYSDBA, T_ROLE_SYSSEC, T_ROLE_SYSAUDIT
6529
 * @return int32_t
6530
 */
6531
int32_t mndCheckManagementRoleStatus(SMnode *pMnode, const char *skipUser, uint8_t skipRole) {
6,438✔
6532
  SUserObj *pUser = NULL;
6,438✔
6533
  SSdb     *pSdb = pMnode->pSdb;
6,438✔
6534
  uint8_t   foundRoles = skipRole;  // 0x01: T_ROLE_SYSDBA, 0x02: T_ROLE_SYSSEC, 0x04: T_ROLE_SYSAUDIT
6,438✔
6535

6536
  void *pIter = NULL;
6,438✔
6537
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
36,971✔
6538
    if (pUser->enable == 0 || pUser->superUser == 1 || taosHashGetSize(pUser->roles) == 0 ||
34,016✔
6539
        (skipUser && strncmp(pUser->user, skipUser, TSDB_USER_LEN) == 0)) {
25,214✔
6540
      sdbRelease(pSdb, pUser);
12,088✔
6541
      continue;
12,088✔
6542
    }
6543

6544
    if ((foundRoles & T_ROLE_SYSDBA) == 0 && taosHashGet(pUser->roles, TSDB_ROLE_SYSDBA, sizeof(TSDB_ROLE_SYSDBA))) {
21,928✔
6545
      foundRoles |= T_ROLE_SYSDBA;
5,453✔
6546
    } else if ((foundRoles & T_ROLE_SYSSEC) == 0 &&
31,043✔
6547
               taosHashGet(pUser->roles, TSDB_ROLE_SYSSEC, sizeof(TSDB_ROLE_SYSSEC))) {
14,568✔
6548
      foundRoles |= T_ROLE_SYSSEC;
5,256✔
6549
    } else if ((foundRoles & T_ROLE_SYSAUDIT) == 0 &&
22,438✔
6550
               taosHashGet(pUser->roles, TSDB_ROLE_SYSAUDIT, sizeof(TSDB_ROLE_SYSAUDIT))) {
11,219✔
6551
      foundRoles |= T_ROLE_SYSAUDIT;
5,059✔
6552
    }
6553
    sdbRelease(pSdb, pUser);
21,928✔
6554
    if (foundRoles == (T_ROLE_SYSDBA | T_ROLE_SYSSEC | T_ROLE_SYSAUDIT)) {
21,928✔
6555
      sdbCancelFetch(pSdb, pIter);
3,483✔
6556
      return TSDB_CODE_SUCCESS;
3,483✔
6557
    }
6558
  }
6559

6560
  if ((foundRoles & T_ROLE_SYSDBA) == 0) {
2,955✔
6561
    return TSDB_CODE_MND_ROLE_NO_VALID_SYSDBA;
985✔
6562
  } else if ((foundRoles & T_ROLE_SYSSEC) == 0) {
1,970✔
6563
    return TSDB_CODE_MND_ROLE_NO_VALID_SYSSEC;
985✔
6564
  } else if ((foundRoles & T_ROLE_SYSAUDIT) == 0) {
985✔
6565
    return TSDB_CODE_MND_ROLE_NO_VALID_SYSAUDIT;
985✔
6566
  }
6567
  return TSDB_CODE_SUCCESS;
×
6568
}
6569

6570
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc