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

taosdata / TDengine / #4963

09 Feb 2026 01:16AM UTC coverage: 66.867% (-0.005%) from 66.872%
#4963

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205748 of 307696 relevant lines covered (66.87%)

127038261.06 hits per line

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

64.04
/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 "mndRole.h"
23
#include "mndUser.h"
24
#include "audit.h"
25
#include "mndDb.h"
26
#include "mndMnode.h"
27
#include "mndPrivilege.h"
28
#include "mndShow.h"
29
#include "mndStb.h"
30
#include "mndSync.h" 
31
#include "mndTopic.h"
32
#include "mndTrans.h"
33
#include "mndToken.h"
34
#include "tbase64.h"
35
#include "totp.h"
36

37
// clang-format on
38

39
#define USER_VER_SUPPORT_WHITELIST           5
40
#define USER_VER_SUPPORT_WHITELIT_DUAL_STACK 7
41
#define USER_VER_SUPPORT_ADVANCED_SECURITY   8
42
#define USER_VER_NUMBER                      USER_VER_SUPPORT_ADVANCED_SECURITY
43

44
#define USER_RESERVE_SIZE 63
45

46
#define BIT_FLAG_MASK(n)              (1 << n)
47
#define BIT_FLAG_SET_MASK(val, mask)  ((val) |= (mask))
48
#define BIT_FLAG_TEST_MASK(val, mask) (((val) & (mask)) != 0)
49

50
#if 0
51
#define PRIVILEGE_TYPE_ALL       BIT_FLAG_MASK(0)
52
#define PRIVILEGE_TYPE_READ      BIT_FLAG_MASK(1)
53
#define PRIVILEGE_TYPE_WRITE     BIT_FLAG_MASK(2)
54
#define PRIVILEGE_TYPE_SUBSCRIBE BIT_FLAG_MASK(3)
55
#define PRIVILEGE_TYPE_ALTER     BIT_FLAG_MASK(4)
56
#endif
57

58
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
59
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
60

61
#if 0
62
#define ALTER_USER_ALL_PRIV(_priv)       (PRIV_HAS((_priv), PRIV_CM_ALL))
63
#define ALTER_USER_READ_PRIV(_priv)      (PRIV_HAS((_priv), PRIV_CM_READ) || PRIV_HAS((_priv), PRIV_CM_ALL))
64
#define ALTER_USER_WRITE_PRIV(_priv)     (PRIV_HAS((_priv), PRIV_CM_WRITE) || PRIV_HAS((_priv), PRIV_CM_ALL))
65
#define ALTER_USER_ALTER_PRIV(_priv)     (PRIV_HAS((_priv), PRIV_CM_ALTER) || PRIV_HAS((_priv), PRIV_CM_ALL))
66
#define ALTER_USER_SUBSCRIBE_PRIV(_priv) (PRIV_HAS((_priv), PRIV_TOPIC_SUBSCRIBE))
67

68
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
69
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
70

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

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

105
#define ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
106
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
107
#define ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
108
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
109
#endif
110

111
#ifdef TD_ENTERPRISE
112
extern int32_t mndAlterUserPrivInfo(SMnode *pMnode, SUserObj *pNew, SAlterRoleReq *pAlterReq);
113
extern int32_t mndAlterUserRoleInfo(SMnode *pMnode, SUserObj *pOperUser, const char *token, SUserObj *pOld,
114
                                    SUserObj *pNew, SAlterRoleReq *pAlterReq);
115
#endif
116

117
static void generateSalt(char *salt, size_t len);
118

119
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList);
120
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppWhiteList, bool supportNeg);
121

122
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b);
123
static bool isIpRangeEqual(SIpRange *a, SIpRange *b);
124

125
#define MND_MAX_USER_IP_RANGE   (TSDB_PRIVILEDGE_HOST_LEN / 24)
126
#define MND_MAX_USER_TIME_RANGE 2048
127

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

149
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq);
150
static int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq);
151
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq);
152
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq);
153
static int32_t mndProcessCreateTotpSecretReq(SRpcMsg *pReq);
154
static int32_t mndProcessDropTotpSecretReq(SRpcMsg *pReq);
155

156
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList);
157
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList);
158

159
typedef struct {
160
  SIpWhiteListDual   *wlIp;
161
  SDateTimeWhiteList *wlTime;
162
  SLoginInfo          loginInfo;
163
} SCachedUserInfo;
164

165
typedef struct {
166
  SHashObj      *users;  // key: user, value: SCachedUserInfo*
167
  int64_t        verIp;
168
  int64_t        verTime;
169
  char           auditLogUser[TSDB_USER_LEN];
170
  TdThreadRwlock rw;
171
} SUserCache;
172

173
static SUserCache userCache;
174
static int8_t     upgradeSecurity = 0;
175

176
static int32_t userCacheInit() {
427,318✔
177
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
427,318✔
178

179
  SHashObj *users = taosHashInit(8, hashFn, 1, HASH_ENTRY_LOCK);
427,318✔
180
  if (users == NULL) {
427,318✔
181
    TAOS_RETURN(terrno);
×
182
  }
183

184
  userCache.users = users;
427,318✔
185
  userCache.verIp = 0;
427,318✔
186
  userCache.verTime = 0;
427,318✔
187
  userCache.auditLogUser[0] = '\0';
427,318✔
188

189
  (void)taosThreadRwlockInit(&userCache.rw, NULL);
427,318✔
190
  TAOS_RETURN(0);
427,318✔
191
}
192

193
static void userCacheCleanup() {
427,258✔
194
  if (userCache.users == NULL) {
427,258✔
195
    return;
×
196
  }
197

198
  void *pIter = taosHashIterate(userCache.users, NULL);
427,258✔
199
  while (pIter) {
872,428✔
200
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
445,170✔
201
    if (pInfo != NULL) {
445,170✔
202
      taosMemoryFree(pInfo->wlIp);
445,170✔
203
      taosMemoryFree(pInfo->wlTime);
445,170✔
204
      taosMemoryFree(pInfo);
445,170✔
205
    }
206
    pIter = taosHashIterate(userCache.users, pIter);
445,170✔
207
  }
208
  taosHashCleanup(userCache.users);
427,258✔
209

210
  (void)taosThreadRwlockDestroy(&userCache.rw);
427,258✔
211
}
212

213
static void userCacheRemoveUser(const char *user) {
35,285✔
214
  size_t userLen = strlen(user);
35,285✔
215

216
  (void)taosThreadRwlockWrlock(&userCache.rw);
35,285✔
217

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

232
  (void)taosThreadRwlockUnlock(&userCache.rw);
35,285✔
233
}
35,285✔
234

235
static void userCacheResetLoginInfo(const char *user) {
855✔
236
  size_t userLen = strlen(user);
855✔
237

238
  (void)taosThreadRwlockWrlock(&userCache.rw);
855✔
239

240
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
855✔
241
  if (ppInfo != NULL && *ppInfo != NULL) {
855✔
242
    (*ppInfo)->loginInfo.lastLoginTime = taosGetTimestampSec();
855✔
243
    (*ppInfo)->loginInfo.failedLoginCount = 0;
855✔
244
    (*ppInfo)->loginInfo.lastFailedLoginTime = 0;
855✔
245
  }
246

247
  (void)taosThreadRwlockUnlock(&userCache.rw);
855✔
248
}
855✔
249

250
static SCachedUserInfo *getCachedUserInfo(const char *user) {
4,609,190✔
251
  size_t            userLen = strlen(user);
4,609,190✔
252
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
4,609,190✔
253
  if (ppInfo != NULL) {
4,609,190✔
254
    return *ppInfo;
4,128,735✔
255
  }
256

257
  SCachedUserInfo *pInfo = (SCachedUserInfo *)taosMemoryCalloc(1, sizeof(SCachedUserInfo));
480,455✔
258
  if (pInfo == NULL) {
480,455✔
259
    return NULL;
×
260
  }
261

262
  if (taosHashPut(userCache.users, user, userLen, &pInfo, sizeof(pInfo)) != 0) {
480,455✔
263
    taosMemoryFree(pInfo);
×
264
    return NULL;
×
265
  }
266

267
  return pInfo;
480,455✔
268
}
269

270
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
2,849,218✔
271
  size_t userLen = strlen(user);
2,849,218✔
272

273
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,849,218✔
274

275
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,849,264✔
276
  if (ppInfo != NULL && *ppInfo != NULL) {
2,848,836✔
277
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,554,239✔
278
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,553,815✔
279
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,553,857✔
280
  } else {
281
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
294,601✔
282
    pLoginInfo->failedLoginCount = 0;
294,597✔
283
    pLoginInfo->lastFailedLoginTime = 0;
294,597✔
284
  }
285

286
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,848,786✔
287

288
  if (pLoginInfo->lastLoginTime == 0) {
2,848,792✔
289
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
49,053✔
290
  }
291
}
2,848,585✔
292

293
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
2,843,146✔
294
  size_t userLen = strlen(user);
2,843,146✔
295

296
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,843,146✔
297

298
  SCachedUserInfo *pInfo = getCachedUserInfo(user);
2,843,423✔
299
  if (pInfo != NULL) {
2,843,423✔
300
    pInfo->loginInfo.lastLoginTime = pLoginInfo->lastLoginTime;
2,843,423✔
301
    pInfo->loginInfo.failedLoginCount = pLoginInfo->failedLoginCount;
2,843,423✔
302
    pInfo->loginInfo.lastFailedLoginTime = pLoginInfo->lastFailedLoginTime;
2,843,423✔
303
  }
304

305
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,843,423✔
306
}
2,843,423✔
307

308
static bool isDateTimeWhiteListEqual(SDateTimeWhiteList *a, SDateTimeWhiteList *b) {
1,372,826✔
309
  if (a == NULL && b == NULL) {
1,372,826✔
310
    return true;
×
311
  }
312

313
  if (a == NULL || b == NULL) {
1,372,826✔
314
    return false;
74,872✔
315
  }
316

317
  if (a->num != b->num) {
1,297,954✔
318
    return false;
286✔
319
  }
320

321
  for (int i = 0; i < a->num; i++) {
1,298,097✔
322
    if (a->ranges[i].start != b->ranges[i].start || a->ranges[i].duration != b->ranges[i].duration ||
715✔
323
        a->ranges[i].neg != b->ranges[i].neg || a->ranges[i].absolute != b->ranges[i].absolute) {
429✔
324
      return false;
286✔
325
    }
326
  }
327

328
  return true;
1,297,382✔
329
}
330

331
static int32_t userCacheUpdateWhiteList(SMnode *pMnode, SUserObj *pUser) {
1,372,826✔
332
  int32_t code = 0, lino = 0;
1,372,826✔
333

334
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,372,826✔
335

336
  SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
1,372,826✔
337
  if (pInfo == NULL) {
1,372,826✔
338
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
339
  }
340

341
  if (!isIpWhiteListEqual(pInfo->wlIp, pUser->pIpWhiteListDual)) {
1,372,826✔
342
    SIpWhiteListDual *p = cloneIpWhiteList(pUser->pIpWhiteListDual);
75,331✔
343
    if (p == NULL) {
75,331✔
344
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
345
    }
346
    taosMemoryFree(pInfo->wlIp);
75,331✔
347
    pInfo->wlIp = p;
75,331✔
348
    userCache.verIp++;
75,331✔
349
  }
350

351
  if (!isDateTimeWhiteListEqual(pInfo->wlTime, pUser->pTimeWhiteList)) {
1,372,826✔
352
    SDateTimeWhiteList *p = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
75,444✔
353
    if (p == NULL) {
75,444✔
354
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
355
    }
356
    taosMemoryFree(pInfo->wlTime);
75,444✔
357
    pInfo->wlTime = p;
75,444✔
358
    userCache.verTime++;
75,444✔
359
  }
360

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

369
static int32_t userCacheRebuildIpWhiteList(SMnode *pMnode) {
515,477✔
370
  int32_t code = 0, lino = 0;
515,477✔
371

372
  SSdb *pSdb = pMnode->pSdb;
515,477✔
373
  void *pIter = NULL;
515,477✔
374
  while (1) {
200,904✔
375
    SUserObj *pUser = NULL;
716,381✔
376
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
716,381✔
377
    if (pIter == NULL) {
716,381✔
378
      break;
515,477✔
379
    }
380

381
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
200,904✔
382
    if (pInfo == NULL) {
200,904✔
383
      sdbRelease(pSdb, pUser);
×
384
      sdbCancelFetch(pSdb, pIter);
×
385
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
386
    }
387

388
    SIpWhiteListDual *wl = cloneIpWhiteList(pUser->pIpWhiteListDual);
200,904✔
389
    if (wl == NULL) {
200,904✔
390
      sdbRelease(pSdb, pUser);
×
391
      sdbCancelFetch(pSdb, pIter);
×
392
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
393
    }
394

395
    taosMemoryFree(pInfo->wlIp);
200,904✔
396
    pInfo->wlIp = wl;
200,904✔
397

398
    sdbRelease(pSdb, pUser);
200,904✔
399
  }
400

401
  userCache.verIp++;
515,477✔
402

403
_OVER:
515,477✔
404
  if (code < 0) {
515,477✔
405
    mError("failed to rebuild ip white list at line %d since %s", lino, tstrerror(code));
×
406
  }
407
  TAOS_RETURN(code);
515,477✔
408
}
409

410
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
42,402,711✔
411
  int64_t ver = 0;
42,402,711✔
412
  int32_t code = 0;
42,402,711✔
413

414
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
42,402,711✔
415
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,488✔
416

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

428
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,488✔
429
  }
430

431
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
42,402,711✔
432
  return ver;
42,402,711✔
433
}
434

435
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
515,477✔
436
  int32_t code = 0;
515,477✔
437
  (void)taosThreadRwlockWrlock(&userCache.rw);
515,477✔
438

439
  if ((code = userCacheRebuildIpWhiteList(pMnode)) != 0) {
515,477✔
440
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
441
    TAOS_RETURN(code);
×
442
  }
443
  userCache.verIp = taosGetTimestampMs();
515,477✔
444
  (void)taosThreadRwlockUnlock(&userCache.rw);
515,477✔
445

446
  TAOS_RETURN(code);
515,477✔
447
}
448

449
static int32_t userCacheRebuildTimeWhiteList(SMnode *pMnode) {
506,610✔
450
  int32_t code = 0, lino = 0;
506,610✔
451

452
  SSdb *pSdb = pMnode->pSdb;
506,610✔
453
  void *pIter = NULL;
506,610✔
454
  while (1) {
192,037✔
455
    SUserObj *pUser = NULL;
698,647✔
456
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
698,647✔
457
    if (pIter == NULL) {
698,647✔
458
      break;
506,610✔
459
    }
460

461
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
192,037✔
462
    if (pInfo == NULL) {
192,037✔
463
      sdbRelease(pSdb, pUser);
×
464
      sdbCancelFetch(pSdb, pIter);
×
465
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
466
    }
467

468
    SDateTimeWhiteList *wl = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
192,037✔
469
    if (wl == NULL) {
192,037✔
470
      sdbRelease(pSdb, pUser);
×
471
      sdbCancelFetch(pSdb, pIter);
×
472
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
473
    }
474

475
    taosMemoryFree(pInfo->wlTime);
192,037✔
476
    pInfo->wlTime = wl;
192,037✔
477

478
    sdbRelease(pSdb, pUser);
192,037✔
479
  }
480

481
  userCache.verTime++;
506,610✔
482

483
_OVER:
506,610✔
484
  if (code < 0) {
506,610✔
485
    mError("failed to rebuild time white list at line %d since %s", lino, tstrerror(code));
×
486
  }
487
  TAOS_RETURN(code);
506,610✔
488
}
489

490
int32_t mndRefreshUserDateTimeWhiteList(SMnode *pMnode) {
506,610✔
491
  int32_t code = 0;
506,610✔
492
  (void)taosThreadRwlockWrlock(&userCache.rw);
506,610✔
493

494
  if ((code = userCacheRebuildTimeWhiteList(pMnode)) != 0) {
506,610✔
495
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
496
    TAOS_RETURN(code);
×
497
  }
498
  userCache.verTime = taosGetTimestampMs();
506,610✔
499
  (void)taosThreadRwlockUnlock(&userCache.rw);
506,610✔
500

501
  TAOS_RETURN(code);
506,610✔
502
}
503

504
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
42,402,711✔
505
  int64_t ver = 0;
42,402,711✔
506
  int32_t code = 0;
42,402,711✔
507

508
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
42,402,711✔
509
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,488✔
510

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

522
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,488✔
523
  }
524

525
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
42,402,711✔
526
  return ver;
42,402,711✔
527
}
528

529
int32_t mndInitUser(SMnode *pMnode) {
427,318✔
530
  TAOS_CHECK_RETURN(userCacheInit());
427,318✔
531

532
  SSdbTable table = {
427,318✔
533
      .sdbType = SDB_USER,
534
      .keyType = SDB_KEY_BINARY,
535
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
536
      .upgradeFp = (SdbUpgradeFp)mndUpgradeUsers,
537
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
538
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
539
      .insertFp = (SdbInsertFp)mndUserActionInsert,
540
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
541
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
542
  };
543

544
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
427,318✔
545
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
427,318✔
546
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
427,318✔
547
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
427,318✔
548
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_USER, mndProcessUpgradeUserReq);
427,318✔
549
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_USER_RSP, mndProcessUpgradeUserRsp);
427,318✔
550

551
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST, mndProcessGetUserIpWhiteListReq);
427,318✔
552
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST_DUAL, mndProcessGetUserIpWhiteListReq);
427,318✔
553
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST, mndProcessRetrieveIpWhiteListReq);
427,318✔
554
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL, mndProcessRetrieveIpWhiteListReq);
427,318✔
555
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_DATETIME_WHITELIST, mndProcessGetUserDateTimeWhiteListReq);
427,318✔
556
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_DATETIME_WHITELIST, mndProcessRetrieveDateTimeWhiteListReq);
427,318✔
557

558
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_TOTP_SECRET, mndProcessCreateTotpSecretReq);
427,318✔
559
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_TOTP_SECRET, mndProcessDropTotpSecretReq);
427,318✔
560

561
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
427,318✔
562
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
427,318✔
563
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndRetrieveUsersFull);
427,318✔
564
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndCancelGetNextUser);
427,318✔
565
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
427,318✔
566
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
427,318✔
567
  return sdbSetTable(pMnode->pSdb, table);
427,318✔
568
}
569

570
void mndCleanupUser(SMnode *pMnode) { userCacheCleanup(); }
427,258✔
571

572
static bool isDefaultRange(SIpRange *pRange) {
×
573
  int32_t code = 0;
×
574
  int32_t lino = 0;
×
575

576
  SIpRange range4 = {0};
×
577
  SIpRange range6 = {0};
×
578

579
  code = createDefaultIp4Range(&range4);
×
580
  TSDB_CHECK_CODE(code, lino, _error);
×
581

582
  code = createDefaultIp6Range(&range6);
×
583
  TSDB_CHECK_CODE(code, lino, _error);
×
584

585
  if (isIpRangeEqual(pRange, &range4) || (isIpRangeEqual(pRange, &range6))) {
×
586
    return true;
×
587
  }
588
_error:
×
589
  return false;
×
590
};
591

592
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
1,626,014✔
593
  int32_t len = 0;
1,626,014✔
594
  for (int i = 0; i < num; i++) {
4,914,032✔
595
    SIpRange *pRange = &range[i];
3,288,018✔
596
    SIpAddr   addr = {0};
3,288,018✔
597
    int32_t   code = tIpUintToStr(pRange, &addr);
3,288,018✔
598
    if (code != 0) {
3,288,018✔
599
      mError("%s failed to convert ip range to str, code: %d", __func__, code);
×
600
    }
601

602
    len += tsnprintf(buf + len, bufLen - len, "%c%s/%d, ", pRange->neg ? '-' : '+', IP_ADDR_STR(&addr), addr.mask);
3,288,018✔
603
  }
604
  if (len > 0) {
1,626,014✔
605
    len -= 2;
1,626,014✔
606
    buf[len] = 0; // remove last ", "
1,626,014✔
607
  }
608
  return len;
1,626,014✔
609
}
610

611
static bool isIpRangeEqual(SIpRange *a, SIpRange *b) {
2,595,735✔
612
  if (a->type != b->type || a->neg != b->neg) {
2,595,735✔
613
    return false;
×
614
  }
615

616
  if (a->type == 0) {
2,595,735✔
617
    SIpV4Range *a4 = &a->ipV4;
1,298,240✔
618
    SIpV4Range *b4 = &b->ipV4;
1,298,240✔
619
    return (a4->ip == b4->ip && a4->mask == b4->mask);
1,298,240✔
620
  }
621

622
  SIpV6Range *a6 = &a->ipV6;
1,297,495✔
623
  SIpV6Range *b6 = &b->ipV6;
1,297,495✔
624
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
1,297,495✔
625
}
626

627
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
1,372,826✔
628
  if (a == NULL && b == NULL) {
1,372,826✔
629
    return true;
×
630
  }
631

632
  if (a == NULL || b == NULL) {
1,372,826✔
633
    return false;
74,872✔
634
  }
635

636
  if (a->num != b->num) {
1,297,954✔
637
    return false;
459✔
638
  }
639
  for (int i = 0; i < a->num; i++) {
3,893,230✔
640
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
2,595,735✔
641
      return false;
×
642
    }
643
  }
644
  return true;
1,297,495✔
645
}
646

647
static int32_t compareIpRange(const void *a, const void *b, const void *arg) {
5,488✔
648
  SIpRange *ra = (SIpRange *)a;
5,488✔
649
  SIpRange *rb = (SIpRange *)b;
5,488✔
650

651
  if (ra->neg != rb->neg) {
5,488✔
652
    return (ra->neg) ? -1 : 1;
715✔
653
  }
654

655
  if (ra->type != rb->type) {
4,773✔
656
    return (ra->type == 0) ? -1 : 1;
1,726✔
657
  }
658

659
  if (ra->type == 0) {
3,047✔
660
    if (ra->ipV4.ip != rb->ipV4.ip) {
3,047✔
661
      return (ra->ipV4.ip < rb->ipV4.ip) ? -1 : 1;
2,701✔
662
    }
663
    return (ra->ipV4.mask < rb->ipV4.mask) ? -1 : 1;
346✔
664
  }
665

666
  if (ra->ipV6.addr[0] != rb->ipV6.addr[0]) {
×
667
    return (ra->ipV6.addr[0] < rb->ipV6.addr[0]) ? -1 : 1;
×
668
  }
669
  if (ra->ipV6.addr[1] != rb->ipV6.addr[1]) {
×
670
    return (ra->ipV6.addr[1] < rb->ipV6.addr[1]) ? -1 : 1;
×
671
  }
672
  return (ra->ipV6.mask < rb->ipV6.mask) ? -1 : 1;
×
673
}
674

675
static void sortIpWhiteList(SIpWhiteListDual *pList) {
2,155✔
676
  (void)taosqsort(pList->pIpRanges, pList->num, sizeof(SIpRange), NULL, compareIpRange);
2,155✔
677
}
2,155✔
678

679
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
1,626,014✔
680
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
1,626,014✔
681

682
  int64_t bufLen = pList->num * 128 + 8;
1,626,014✔
683
  *buf = taosMemoryCalloc(1, bufLen);
1,626,014✔
684
  if (*buf == NULL) {
1,626,014✔
685
    return 0;
×
686
  }
687

688
  if (pList->num == 0) {
1,626,014✔
689
    return tsnprintf(*buf, bufLen, "+ALL");
×
690
  }
691

692
  int32_t len = ipRangeListToStr(pList->pIpRanges, pList->num, *buf, bufLen - 2);
1,626,014✔
693
  if (len == 0) {
1,626,014✔
694
    taosMemoryFreeClear(*buf);
×
695
    return 0;
×
696
  }
697
  return len;
1,626,014✔
698
}
699

700
static int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, uint32_t *pLen) {
3,882,015✔
701
  int32_t  code = 0;
3,882,015✔
702
  int32_t  lino = 0;
3,882,015✔
703
  int32_t  tlen = 0;
3,882,015✔
704
  SEncoder encoder = {0};
3,882,015✔
705
  tEncoderInit(&encoder, buf, len);
3,882,015✔
706

707
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
3,882,015✔
708
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
7,764,030✔
709

710
  for (int i = 0; i < pList->num; i++) {
11,650,522✔
711
    SIpRange *pRange = &(pList->pIpRanges[i]);
7,768,507✔
712
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
7,768,507✔
713
  }
714

715
  tEndEncode(&encoder);
3,882,015✔
716

717
  tlen = encoder.pos;
3,882,015✔
718
_OVER:
3,882,015✔
719
  tEncoderClear(&encoder);
3,882,015✔
720
  if (code < 0) {
3,882,015✔
721
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
722
  }
723
  if (pLen) *pLen = tlen;
3,882,015✔
724
  TAOS_RETURN(code);
3,882,015✔
725
}
726

727
static int32_t tDerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, bool supportNeg) {
3,114,432✔
728
  int32_t  code = 0;
3,114,432✔
729
  int32_t  lino = 0;
3,114,432✔
730
  SDecoder decoder = {0};
3,114,432✔
731
  tDecoderInit(&decoder, buf, len);
3,114,432✔
732

733
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
3,114,432✔
734
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
6,228,864✔
735

736
  for (int i = 0; i < pList->num; i++) {
9,345,937✔
737
    SIpRange *pRange = &(pList->pIpRanges[i]);
6,231,505✔
738
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
6,231,505✔
739
  }
740

741
_OVER:
3,114,432✔
742
  tEndDecode(&decoder);
3,114,432✔
743
  tDecoderClear(&decoder);
3,114,432✔
744
  if (code < 0) {
3,114,432✔
745
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
746
  }
747
  TAOS_RETURN(code);
3,114,432✔
748
}
749

750
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList) {
×
751
  int32_t  code = 0;
×
752
  int32_t  lino = 0;
×
753
  SDecoder decoder = {0};
×
754
  tDecoderInit(&decoder, buf, len);
×
755

756
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
757
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
758

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

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

774
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppList, bool supportNeg) {
3,114,432✔
775
  int32_t           code = 0;
3,114,432✔
776
  int32_t           lino = 0;
3,114,432✔
777
  int32_t           num = 0;
3,114,432✔
778
  SIpWhiteListDual *p = NULL;
3,114,432✔
779
  SDecoder          decoder = {0};
3,114,432✔
780
  tDecoderInit(&decoder, buf, len);
3,114,432✔
781

782
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
3,114,432✔
783
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
3,114,432✔
784

785
  p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + num * sizeof(SIpRange));
3,114,432✔
786
  if (p == NULL) {
3,114,432✔
787
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
788
  }
789
  TAOS_CHECK_GOTO(tDerializeIpWhiteList(buf, len, p, supportNeg), &lino, _OVER);
3,114,432✔
790

791
_OVER:
3,114,432✔
792
  tEndDecode(&decoder);
3,114,432✔
793
  tDecoderClear(&decoder);
3,114,432✔
794
  if (code < 0) {
3,114,432✔
795
    taosMemoryFreeClear(p);
×
796
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
797
  }
798
  *ppList = p;
3,114,432✔
799
  TAOS_RETURN(code);
3,114,432✔
800
}
801

802
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList) {
×
803
  int32_t       code = 0;
×
804
  int32_t       lino = 0;
×
805
  int32_t       num = 0;
×
806
  SIpWhiteList *p = NULL;
×
807
  SDecoder      decoder = {0};
×
808
  tDecoderInit(&decoder, buf, len);
×
809

810
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
811
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
812

813
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
×
814
  if (p == NULL) {
×
815
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
816
  }
817
  TAOS_CHECK_GOTO(tDerializeIpWhileListFromOldVer(buf, len, p), &lino, _OVER);
×
818

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

830
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList) {
376,135✔
831
  int32_t code = 0;
376,135✔
832
  int32_t lino = 0;
376,135✔
833
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
376,135✔
834
  if (*ppWhiteList == NULL) {
376,135✔
835
    TAOS_RETURN(terrno);
×
836
  }
837
  (*ppWhiteList)->num = 2;
376,135✔
838

839
  SIpRange v4 = {0};
376,135✔
840
  SIpRange v6 = {0};
376,135✔
841

842
#ifndef TD_ASTRA
843
  code = createDefaultIp4Range(&v4);
376,135✔
844
  TSDB_CHECK_CODE(code, lino, _error);
376,135✔
845

846
  code = createDefaultIp6Range(&v6);
376,135✔
847
  TSDB_CHECK_CODE(code, lino, _error);
376,135✔
848

849
#endif
850

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

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

865
static int32_t convertTimeRangesToStr(SUserObj *pUser, char **buf) {
1,626,014✔
866
  int32_t bufLen = pUser->pTimeWhiteList->num * 32 + 8;
1,626,014✔
867
  *buf = taosMemoryCalloc(1, bufLen);
1,626,014✔
868
  if (*buf == NULL) {
1,626,014✔
869
    return 0;
×
870
  }
871

872
  int32_t pos = 0;
1,626,014✔
873
  if (pUser->pTimeWhiteList->num == 0) {
1,626,014✔
874
    pos += tsnprintf(*buf + pos, bufLen - pos, "+ALL");
1,548,222✔
875
    return pos;
1,548,222✔
876
  }
877

878
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
177,320✔
879
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
99,528✔
880
    int                     duration = range->duration / 60;
99,528✔
881

882
    if (range->absolute) {
99,528✔
883
      struct STm tm;
58,773✔
884
      (void)taosTs2Tm(range->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
58,773✔
885
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%04d-%02d-%02d %02d:%02d %dm, ", range->neg ? '-' : '+',
58,773✔
886
                       tm.tm.tm_year + 1900, tm.tm.tm_mon + 1, tm.tm.tm_mday, tm.tm.tm_hour, tm.tm.tm_min, duration);
58,773✔
887
    } else {
888
      int day = range->start / 86400;
40,755✔
889
      int hour = (range->start % 86400) / 3600;
40,755✔
890
      int minute = (range->start % 3600) / 60;
40,755✔
891
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%s %02d:%02d %dm, ", range->neg ? '-' : '+', weekdays[day], hour,
40,755✔
892
                       minute, duration);
893
    }
894
  }
895

896
  if (pos > 0) {
77,792✔
897
    pos -= 2;
77,792✔
898
    (*buf)[pos] = 0; // remove last ", "
77,792✔
899
  }
900

901
  return pos;
77,792✔
902
}
903

904
static int32_t compareDateTimeInterval(const void *a, const void *b, const void *arg) {
858✔
905
  SDateTimeWhiteListItem *pA = (SDateTimeWhiteListItem *)a;
858✔
906
  SDateTimeWhiteListItem *pB = (SDateTimeWhiteListItem *)b;
858✔
907

908
  if (pA->neg != pB->neg) {
858✔
909
    return pA->neg ? -1 : 1;
572✔
910
  }
911

912
  if (pA->absolute != pB->absolute) {
286✔
913
    return pA->absolute ? 1 : -1;
286✔
914
  }
915

916
  if (pA->start != pB->start) {
×
917
    return (pA->start < pB->start) ? -1 : 1;
×
918
  }
919

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

924
  return 0;
×
925
}
926

927
static void sortTimeWhiteList(SDateTimeWhiteList *pList) {
2,431✔
928
  (void)taosqsort(pList->ranges, pList->num, sizeof(SDateTimeWhiteListItem), NULL, compareDateTimeInterval);
2,431✔
929
}
2,431✔
930

931
static void dropOldPasswords(SUserObj *pUser) {
9,507,805✔
932
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
9,507,805✔
933
    return;
9,462,770✔
934
  }
935

936
  int32_t reuseMax = pUser->passwordReuseMax;
45,035✔
937
  if (reuseMax == 0) {
45,035✔
938
    reuseMax = 1;  // keep at least one password
42,461✔
939
  }
940

941
  int32_t now = taosGetTimestampSec();
45,035✔
942
  int32_t index = reuseMax;
45,035✔
943
  while(index < pUser->numOfPasswords) {
51,019✔
944
    // the set time of the n-th password is the expire time of the n+1-th password
945
    int32_t expireTime = pUser->passwords[index - 1].setTime;
6,270✔
946
    if (now - expireTime >= pUser->passwordReuseTime) {
6,270✔
947
      break;
286✔
948
    }
949
    index++;
5,984✔
950
  }
951

952
  if (index == pUser->numOfPasswords) {
45,035✔
953
    return;
44,749✔
954
  }
955
  pUser->numOfPasswords = index;
286✔
956
  // this is a shrink operation, no need to check return value
957
  pUser->passwords = taosMemoryRealloc(pUser->passwords, sizeof(SUserPassword) * pUser->numOfPasswords);
286✔
958
}
959

960
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
302,963✔
961
  int32_t  code = 0;
302,963✔
962
  int32_t  lino = 0;
302,963✔
963
  SUserObj userObj = {0};
302,963✔
964

965
  userObj.passwords = taosMemCalloc(1, sizeof(SUserPassword));
302,963✔
966
  if (userObj.passwords == NULL) {
302,963✔
967
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
968
  }
969
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.passwords[0].pass);
302,963✔
970
  userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
302,963✔
971
  if (tsiEncryptPassAlgorithm == DND_CA_SM4 && strlen(tsDataKey) > 0) {
302,963✔
972
    generateSalt(userObj.salt, sizeof(userObj.salt));
×
973
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino,
×
974
                    _ERROR);
975
  }
976

977
  userObj.passwords[0].setTime = taosGetTimestampSec();
302,963✔
978
  userObj.numOfPasswords = 1;
302,963✔
979

980
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
302,963✔
981
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
302,963✔
982
  userObj.createdTime = taosGetTimestampMs();
302,963✔
983
  userObj.updateTime = userObj.createdTime;
302,963✔
984
  userObj.uid = mndGenerateUid(userObj.user, strlen(userObj.user));
302,963✔
985
  userObj.sysInfo = 1;
302,963✔
986
  userObj.enable = 1;
302,963✔
987

988
#ifdef TD_ENTERPRISE
989

990
  // 1: force user to change password
991
  // 2: allow but not force user to change password
992
  userObj.changePass = tsAllowDefaultPassword ? 2 : 1;
302,963✔
993

994
  userObj.ipWhiteListVer = taosGetTimestampMs();
302,963✔
995
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
302,963✔
996
  userObj.connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
302,963✔
997
  userObj.connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
302,963✔
998
  userObj.callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
302,963✔
999
  userObj.vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
302,963✔
1000
  userObj.passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
302,963✔
1001
  userObj.passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
302,963✔
1002
  userObj.passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
302,963✔
1003
  userObj.sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
302,963✔
1004
  userObj.failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
302,963✔
1005
  userObj.passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
302,963✔
1006
  userObj.passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
302,963✔
1007
  userObj.inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
302,963✔
1008
  userObj.allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
302,963✔
1009
  userObj.tokenNum = 0;
302,963✔
1010

1011
#else  // TD_ENTERPRISE
1012

1013
  userObj.ipWhiteListVer = 0;
1014
  userObj.timeWhiteListVer = 0;
1015
  userObj.changePass = 2;  // 2: allow but not force user to change password
1016
  userObj.connectTime = -1;
1017
  userObj.connectIdleTime = -1;
1018
  userObj.callPerSession = -1;
1019
  userObj.vnodePerCall = -1;
1020
  userObj.passwordReuseTime = 0;
1021
  userObj.passwordReuseMax = 0;
1022
  userObj.passwordLockTime = -1;
1023
  userObj.sessionPerUser = -1;
1024
  userObj.failedLoginAttempts = -1;
1025
  userObj.passwordLifeTime = -1;
1026
  userObj.passwordGraceTime = -1;
1027
  userObj.inactiveAccountTime = -1;
1028
  userObj.allowTokenNum = -1;
1029
  userObj.tokenNum = 0;
1030

1031
#endif  // TD_ENTERPRISE
1032

1033
  userObj.pTimeWhiteList = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
302,963✔
1034
  if (userObj.pTimeWhiteList == NULL) {
302,963✔
1035
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
1036
  }
1037

1038
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _ERROR);
302,963✔
1039
  // if this is the root user, change the value of some fields to allow the user login without restriction
1040
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
302,963✔
1041
    userObj.superUser = 1;
302,963✔
1042
    userObj.createdb = 1;
302,963✔
1043
    userObj.sessionPerUser = -1;
302,963✔
1044
    userObj.callPerSession = -1;
302,963✔
1045
    userObj.vnodePerCall = -1;
302,963✔
1046
    userObj.failedLoginAttempts = -1;
302,963✔
1047
    userObj.passwordGraceTime = -1;
302,963✔
1048
    userObj.inactiveAccountTime = -1;
302,963✔
1049
    userObj.allowTokenNum = -1;
302,963✔
1050
  }
1051

1052
  userObj.roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
302,963✔
1053
  if (userObj.roles == NULL) {
302,963✔
1054
    TAOS_CHECK_GOTO(terrno, &lino, _ERROR);
×
1055
  }
1056

1057
  if ((code = taosHashPut(userObj.roles, TSDB_ROLE_SYSDBA, strlen(TSDB_ROLE_SYSDBA) + 1, NULL, 0)) ||
605,926✔
1058
      (code = taosHashPut(userObj.roles, TSDB_ROLE_SYSSEC, strlen(TSDB_ROLE_SYSSEC) + 1, NULL, 0)) ||
605,926✔
1059
      (code = taosHashPut(userObj.roles, TSDB_ROLE_SYSAUDIT, strlen(TSDB_ROLE_SYSAUDIT) + 1, NULL, 0))) {
302,963✔
1060
    TAOS_CHECK_GOTO(code, &lino, _ERROR);
×
1061
  }
1062

1063
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
302,963✔
1064
  if (pRaw == NULL) goto _ERROR;
302,963✔
1065
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
302,963✔
1066

1067
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
302,963✔
1068

1069
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_ROLE, NULL, "create-user");
302,963✔
1070
  if (pTrans == NULL) {
302,963✔
1071
    sdbFreeRaw(pRaw);
×
1072
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
1073
    goto _ERROR;
×
1074
  }
1075
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
302,963✔
1076

1077
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
302,963✔
1078
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1079
    mndTransDrop(pTrans);
×
1080
    goto _ERROR;
×
1081
  }
1082
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
302,963✔
1083

1084
  if (mndTransPrepare(pMnode, pTrans) != 0) {
302,963✔
1085
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1086
    mndTransDrop(pTrans);
×
1087
    goto _ERROR;
×
1088
  }
1089

1090
  mndTransDrop(pTrans);
302,963✔
1091
  mndUserFreeObj(&userObj);
302,963✔
1092
  return 0;
302,963✔
1093

1094
_ERROR:
×
1095
  mndUserFreeObj(&userObj);
×
1096
  if (code == 0) {
×
1097
    code = terrno ? terrno : TSDB_CODE_APP_ERROR;
×
1098
  }
1099
  mError("user:%s, failed to create default user since %s", user, tstrerror(code));
×
1100
  TAOS_RETURN(code);
×
1101
}
1102

1103
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
302,963✔
1104
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
302,963✔
1105
}
1106

1107
static int32_t mndUserPrivUpgradeDbOwners(SMnode *pMnode, SRpcMsg *pReq) {
×
1108
  int32_t code = 0, lino = 0;
×
1109
  SSdb   *pSdb = pMnode->pSdb;
×
1110
  SDbObj *pObj = NULL;
×
1111
  void   *pIter = NULL;
×
1112

1113
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "upgrade-db");
×
1114
  if (pTrans == NULL) {
×
1115
    TAOS_CHECK_EXIT(terrno);
×
1116
  }
1117

1118
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pObj))) {
×
1119
    if (pObj->cfg.isMount) {
×
1120
      sdbRelease(pSdb, pObj);
×
1121
      continue;
×
1122
    }
1123
    if (pObj->ownerId != 0) {
×
1124
      sdbRelease(pSdb, pObj);
×
1125
      continue;
×
1126
    }
1127
    SUserObj *pUser = NULL;
×
1128
    (void)mndAcquireUser(pMnode, pObj->createUser, &pUser);
×
1129
    if (pUser == NULL) {
×
1130
      mWarn("db:%s, owner user:%s not found, skip upgrade owner uid", pObj->name, pObj->createUser);
×
1131
      sdbRelease(pSdb, pObj);
×
1132
      continue;
×
1133
    }
1134
    if (pUser->uid == 0) {
×
1135
      mndReleaseUser(pMnode, pUser);
×
1136
      sdbRelease(pSdb, pObj);
×
1137
      continue;
×
1138
    }
1139
    SDbObj newObj = {0};
×
1140
    memcpy(&newObj, pObj, sizeof(SDbObj));
×
1141
    ++newObj.cfgVersion;
×
1142
    newObj.updateTime = taosGetTimestampMs();
×
1143
    newObj.ownerId = pUser->uid;
×
1144
    mInfo("db:%s, owner uid upgraded to %" PRId64, pObj->name, pUser->uid);
×
1145
    mndReleaseUser(pMnode, pUser);
×
1146
    if ((code = mndSetAlterDbCommitLogs(pMnode, pTrans, pObj, &newObj))) {
×
1147
      sdbCancelFetch(pSdb, pIter);
×
1148
      sdbRelease(pSdb, pObj);
×
1149
      TAOS_CHECK_EXIT(code);
×
1150
    }
1151
    sdbRelease(pSdb, pObj);
×
1152
  }
1153

1154
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
×
1155

1156
_exit:
×
1157
  mndTransDrop(pTrans);
×
1158
  if (code < 0) {
×
1159
    mError("failed at line %d to upgrade db owner uid since %s", lino, tstrerror(code));
×
1160
  }
1161
  TAOS_RETURN(code);
×
1162
}
1163

1164
static int32_t mndUserPrivUpgradeOwnedDbs(SMnode *pMnode, SUserObj *pUser) {
×
1165
  int32_t code = 0, lino = 0;
×
1166
  SSdb   *pSdb = pMnode->pSdb;
×
1167
  SDbObj *pObj = NULL;
×
1168
  void   *pIter = NULL;
×
1169
  char   *key = NULL;
×
1170
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pObj))) {
×
1171
    if (strncmp(pObj->createUser, pUser->name, sizeof(pObj->createUser)) != 0) {
×
1172
      sdbRelease(pSdb, pObj);
×
1173
      pObj = NULL;
×
1174
      continue;
×
1175
    }
1176
    TAOS_CHECK_EXIT(taosHashPut(pUser->ownedDbs, pObj->name, strlen(pObj->name) + 1, NULL, 0));
×
1177
    sdbRelease(pSdb, pObj);
×
1178
    pObj = NULL;
×
1179
  }
1180
_exit:
×
1181
  if (pIter) sdbCancelFetch(pSdb, pIter);
×
1182
  if (pObj) sdbRelease(pSdb, pObj);
×
1183
  TAOS_RETURN(code);
×
1184
}
1185

1186
#ifdef TD_ENTERPRISE
1187
static int32_t mndUserPrivUpgradeTbViews(SMnode *pMnode, SUserObj *pUser, SHashObj **ppTblHash, SHashObj *pTbs, int32_t privType,
×
1188
                                         uint8_t objType) {
1189
  int32_t code = 0, lino = 0;
×
1190
  void   *pIter = NULL;
×
1191
  char   *key = NULL;
×
1192
  char   *value = NULL;
×
1193

1194
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES, .add = 1, .objType = objType, .objLevel = 1};
×
1195

1196
  while ((pIter = taosHashIterate(pTbs, pIter))) {
×
1197
    size_t keyLen = 0;
×
1198
    key = taosHashGetKey(pIter, &keyLen);
×
1199

1200
    SName name = {0};
×
1201
    TAOS_CHECK_EXIT(tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE));
×
1202

1203
    snprintf(alterReq.objFName, TSDB_OBJ_FNAME_LEN, "%d.%s", name.acctId, name.dbname);
×
1204
    snprintf(alterReq.tblName, TSDB_TABLE_NAME_LEN, "%s", name.tname);
×
1205

1206
    privAddType(&alterReq.privileges.privSet, privType);
1207

1208
    if ((objType == PRIV_OBJ_TBL) && (((char *)pIter)[0] != 0) && (((char *)pIter)[1] != 0)) {
×
1209
      alterReq.privileges.cond = taosStrdup(pIter);
×
1210
      if (alterReq.privileges.cond == NULL) {
×
1211
        TAOS_CHECK_EXIT(terrno);
×
1212
      }
1213
      alterReq.privileges.condLen = strlen(pIter) + 1;  // include '\0'
×
1214
      if (ppTblHash && !*ppTblHash) {
×
1215
        *ppTblHash = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_ENTRY_LOCK);
×
1216
        if (!*ppTblHash) {
×
1217
          TAOS_CHECK_EXIT(terrno);
×
1218
        }
1219
        taosHashSetFreeFp(*ppTblHash, privTblPoliciesFree);
×
1220
      }
1221
    }
1222

1223
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1224
    tFreeSAlterRoleReq(&alterReq);
×
1225
  }
1226
_exit:
×
1227
  tFreeSAlterRoleReq(&alterReq);
×
1228
  TAOS_RETURN(code);
×
1229
}
1230

1231
/**
1232
 * @brief upgrade read/write db privileges from 3.3.x.y to 3.4.x.y
1233
 * @param rwType 0x01 read, 0x02 write
1234
 */
1235
static int32_t mndUserPrivUpgradeRwDbs(SMnode *pMnode, SUserObj *pUser, SHashObj *pDbs, int8_t rwType) {
×
1236
  int32_t code = 0, lino = 0;
×
1237
  void   *pIter = NULL;
×
1238
  char   *key = NULL;
×
1239
  while ((pIter = taosHashIterate(pDbs, pIter))) {
×
1240
    key = taosHashGetKey(pIter, NULL);
×
1241
    TAOS_CHECK_EXIT(privUpgradeRwDb(pUser->objPrivs, key, "*", rwType));
×
1242
  }
1243
_exit:
×
1244
  TAOS_RETURN(code);
×
1245
}
1246

1247
static int32_t mndUserPrivUpgradeUsedDbs(SMnode *pMnode, SUserObj *pUser, SHashObj *pDbs) {
×
1248
  int32_t code = 0, lino = 0;
×
1249
  void   *pIter = NULL;
×
1250
  char   *key = NULL;
×
1251
  char   *value = NULL;
×
1252

1253
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES, .add = 1, .objType = PRIV_OBJ_DB};
×
1254

1255
  while ((pIter = taosHashIterate(pDbs, pIter))) {
×
1256
    key = taosHashGetKey(pIter, NULL);
×
1257

1258
    SName name = {0};
×
1259
    TAOS_CHECK_EXIT(tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB));
×
1260

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

1263
    privAddType(&alterReq.privileges.privSet, PRIV_DB_USE);
1264

1265
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1266
  }
1267
_exit:
×
1268
  tFreeSAlterRoleReq(&alterReq);
×
1269
  TAOS_RETURN(code);
×
1270
}
1271

1272
static int32_t mndUserPrivUpgradeTopics(SMnode *pMnode, SUserObj *pUser, SHashObj *pTopics) {
×
1273
  int32_t code = 0, lino = 0;
×
1274
  void   *pIter = NULL;
×
1275
  char   *key = NULL;
×
1276
  char   *value = NULL;
×
1277

1278
  SAlterRoleReq alterReq = {.alterType = TSDB_ALTER_ROLE_PRIVILEGES,
×
1279
                            .add = 1,
1280
                            .objType = PRIV_OBJ_TOPIC,
1281
                            .objLevel = 1,
1282
                            .ignoreNotExists = 1};
1283

1284
  while ((pIter = taosHashIterate(pTopics, pIter))) {
×
1285
    size_t keyLen = 0;
×
1286
    key = taosHashGetKey(pIter, &keyLen);
×
1287

1288
    SName name = {0};
×
1289
    if (tNameFromString(&name, key, T_NAME_ACCT | T_NAME_DB)) {  // 1.topicName
×
1290
      continue;
×
1291
    }
1292
    snprintf(alterReq.tblName, TSDB_TABLE_NAME_LEN, "%s", name.dbname);
×
1293

1294
    SMqTopicObj *pTopic = NULL;
×
1295
    if (mndAcquireTopic(pMnode, key, &pTopic)) {
×
1296
      continue;  // no topic exists
×
1297
    }
1298
    snprintf(alterReq.objFName, TSDB_OBJ_FNAME_LEN, "%s", pTopic->db);
×
1299
    mndReleaseTopic(pMnode, pTopic);
×
1300

1301
    privAddType(&alterReq.privileges.privSet, PRIV_CM_SUBSCRIBE);
1302

1303
    TAOS_CHECK_EXIT(mndAlterUserPrivInfo(pMnode, pUser, &alterReq));
×
1304
  }
1305
_exit:
×
1306
  tFreeSAlterRoleReq(&alterReq);
×
1307
  TAOS_RETURN(code);
×
1308
}
1309
#endif
1310

1311
/**
1312
 * @brief migrate from 3.3.x.y to 3.4.x.y
1313
 * @return int32_t
1314
 */
1315
static int32_t mndUserPrivUpgradeUser(SMnode *pMnode, SUserObj *pObj) {
×
1316
  int32_t          code = 0, lino = 0;
×
1317
  SPrivHashObjSet *pPrivSet = pObj->legacyPrivs;
×
1318

1319
  if (pObj->uid == 0) {
×
1320
    pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
1321
  }
1322

1323
  if (!pObj->objPrivs &&
×
1324
      !(pObj->objPrivs = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
1325
    TAOS_CHECK_EXIT(terrno);
×
1326
  }
1327

1328
  if (!pObj->roles &&
×
1329
      !(pObj->roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
1330
    TAOS_CHECK_EXIT(terrno);
×
1331
  }
1332

1333
  // assign roles and system privileges
1334
  uint8_t flag = 0x01;
×
1335
  if (pObj->superUser) {
×
1336
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSDBA, strlen(TSDB_ROLE_SYSDBA) + 1, &flag, sizeof(flag)));
×
1337
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSSEC, strlen(TSDB_ROLE_SYSSEC) + 1, &flag, sizeof(flag)));
×
1338
    TAOS_CHECK_EXIT(taosHashPut(pObj->roles, TSDB_ROLE_SYSAUDIT, strlen(TSDB_ROLE_SYSAUDIT) + 1, &flag, sizeof(flag)));
×
1339
  } else {
1340
    if (pObj->sysInfo == 1) {
×
1341
      TAOS_CHECK_EXIT(
×
1342
          taosHashPut(pObj->roles, TSDB_ROLE_SYSINFO_1, strlen(TSDB_ROLE_SYSINFO_1) + 1, &flag, sizeof(flag)));
1343
    } else {
1344
      TAOS_CHECK_EXIT(
×
1345
          taosHashPut(pObj->roles, TSDB_ROLE_SYSINFO_0, strlen(TSDB_ROLE_SYSINFO_0) + 1, &flag, sizeof(flag)));
1346
    }
1347
    if (pObj->createdb == 1) {
×
1348
      privAddType(&pObj->sysPrivs, PRIV_DB_CREATE);
×
1349
    }
1350
  }
1351
#ifdef TD_ENTERPRISE
1352
  // read db: db.*
1353
  TAOS_CHECK_EXIT(mndUserPrivUpgradeRwDbs(pMnode, pObj, pPrivSet->pReadDbs, 0x01));
×
1354
  // write db: db.*
1355
  TAOS_CHECK_EXIT(mndUserPrivUpgradeRwDbs(pMnode, pObj, pPrivSet->pWriteDbs, 0x02));
×
1356
  // tables/views
1357
  TAOS_CHECK_EXIT(
×
1358
      mndUserPrivUpgradeTbViews(pMnode, pObj, &pObj->selectTbs, pPrivSet->pReadTbs, PRIV_TBL_SELECT, PRIV_OBJ_TBL));
1359
  TAOS_CHECK_EXIT(
×
1360
      mndUserPrivUpgradeTbViews(pMnode, pObj, &pObj->insertTbs, pPrivSet->pWriteTbs, PRIV_TBL_INSERT, PRIV_OBJ_TBL));
1361
  TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pAlterTbs, PRIV_CM_ALTER, PRIV_OBJ_TBL));
×
1362
  TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pReadViews, PRIV_VIEW_SELECT, PRIV_OBJ_VIEW));
×
1363
  TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pAlterViews, PRIV_CM_ALTER, PRIV_OBJ_VIEW));
×
1364
  TAOS_CHECK_EXIT(mndUserPrivUpgradeTbViews(pMnode, pObj, NULL, pPrivSet->pWriteViews, PRIV_CM_DROP, PRIV_OBJ_VIEW));
×
1365
  // used dbs
1366
  TAOS_CHECK_EXIT(mndUserPrivUpgradeUsedDbs(pMnode, pObj, pPrivSet->pUseDbs));
×
1367
  // subscribe
1368
  TAOS_CHECK_EXIT(mndUserPrivUpgradeTopics(pMnode, pObj, pPrivSet->pTopics));
×
1369
#endif
1370
  // owned dbs
1371
  TAOS_CHECK_EXIT(mndUserPrivUpgradeOwnedDbs(pMnode, pObj));
×
1372
  mInfo("user:%s, upgraded with uid:%" PRId64, pObj->name, pObj->uid);
×
1373
_exit:
×
1374
  TAOS_RETURN(code);
×
1375
}
1376

1377
static int32_t mndUserPrivUpgradeUsers(SMnode *pMnode, SRpcMsg *pReq) {
×
1378
  int32_t   code = 0, lino = 0;
×
1379
  SSdb     *pSdb = pMnode->pSdb;
×
1380
  SUserObj *pObj = NULL;
×
1381
  void     *pIter = NULL;
×
1382
  SUserObj  newObj = {0};
×
1383

1384
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj))) {
×
1385
    if (pObj->uid != 0 && pObj->legacyPrivs == NULL) {
×
1386
      sdbRelease(pSdb, pObj);
×
1387
      pObj = NULL;
×
1388
      continue;
×
1389
    }
1390
    if (pObj->uid == 0) {
×
1391
      // Assign uid firstly because the transactions in mndUserPrivUpgradeUsers may not finish when
1392
      // mndUserPrivUpgradeDbOwners is called
1393
      pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
1394
    }
1395
    memset(&newObj, 0, sizeof(SUserObj));
×
1396
    TAOS_CHECK_EXIT(mndUserDupObj(pObj, &newObj));
×
1397
    TAOS_CHECK_EXIT(mndUserPrivUpgradeUser(pMnode, &newObj));
×
1398
    TAOS_CHECK_EXIT(mndAlterUser(pMnode, &newObj, pReq));
×
1399
    mndUserFreeObj(&newObj);
×
1400
    sdbRelease(pSdb, pObj);
×
1401
    pObj = NULL;
×
1402
  }
1403
_exit:
×
1404
  sdbCancelFetch(pSdb, pIter);
×
1405
  sdbRelease(pSdb, pObj);
×
1406
  mndUserFreeObj(&newObj);
×
1407
  if (code < 0) {
×
1408
    mError("failed at line %d to upgrade db owner uid since %s", lino, tstrerror(code));
×
1409
  }
1410
  TAOS_RETURN(code);
×
1411
}
1412

1413
static int32_t mndProcessUpgradeUserReq(SRpcMsg *pReq) {
×
1414
  SMnode *pMnode = pReq->info.node;
×
1415
  int32_t code = 0, lino = 0;
×
1416

1417
  TAOS_CHECK_EXIT(mndUserPrivUpgradeUsers(pMnode, pReq));
×
1418
  TAOS_CHECK_EXIT(mndUserPrivUpgradeDbOwners(pMnode, pReq));
×
1419
_exit:
×
1420
  if (code < 0) {
×
1421
    mError("failed at line %d to upgrade users since %s", lino, tstrerror(code));
×
1422
  }
1423
  TAOS_RETURN(code);
×
1424
}
1425

1426
static int32_t mndProcessUpgradeUserRsp(SRpcMsg *pReq) { return 0;}
×
1427

1428
static int32_t mndUpgradeUsers(SMnode *pMnode, int32_t version) {
382,746✔
1429
  int32_t code = 0, lino = 0;
382,746✔
1430
  if (upgradeSecurity == 0) return code;
382,746✔
1431
  if (!mndIsLeader(pMnode)) return code;
×
1432

1433
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPGRADE_USER, .info.ahandle = 0, .info.notFreeAhandle = 1};
×
1434
  SEpSet  epSet = {0};
×
1435
  mndGetMnodeEpSet(pMnode, &epSet);
×
1436
  TAOS_CHECK_EXIT(tmsgSendReq(&epSet, &rpcMsg));
×
1437
_exit:
×
1438
  if (code < 0) {
×
1439
    mError("failed at line %d to upgrade users since %s", lino, tstrerror(code));
×
1440
  }
1441
  TAOS_RETURN(code);
×
1442
}
1443

1444
static int32_t tSerializeUserObjExt(void *buf, int32_t bufLen, SUserObj *pObj) {
7,764,030✔
1445
  int32_t  code = 0, lino = 0;
7,764,030✔
1446
  int32_t  tlen = 0;
7,764,030✔
1447
  void    *pIter = NULL;
7,764,030✔
1448
  SEncoder encoder = {0};
7,764,030✔
1449
  tEncoderInit(&encoder, buf, bufLen);
7,764,030✔
1450

1451
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
7,764,030✔
1452
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->uid));
15,528,060✔
1453

1454
  TAOS_CHECK_EXIT(tSerializePrivSysObjPolicies(&encoder, &pObj->sysPrivs, pObj->objPrivs));
7,764,030✔
1455

1456
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->selectTbs));
7,764,030✔
1457
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->insertTbs));
7,764,030✔
1458
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->updateTbs));
7,764,030✔
1459
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->deleteTbs));
7,764,030✔
1460

1461
  int32_t nRoles = taosHashGetSize(pObj->roles);
7,764,030✔
1462
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nRoles));
7,764,030✔
1463

1464
  while ((pIter = taosHashIterate(pObj->roles, pIter))) {
24,671,594✔
1465
    size_t keyLen = 0;
16,907,564✔
1466
    char  *key = taosHashGetKey(pIter, &keyLen);  // key: role name
16,907,564✔
1467
    TAOS_CHECK_EXIT(tEncodeCStr(&encoder, key));
16,907,564✔
1468

1469
    uint8_t flag = *(uint8_t *)pIter;
16,907,564✔
1470
    TAOS_CHECK_EXIT(tEncodeU8(&encoder, flag));  // value: 0 reset, 1 set(default)
33,815,128✔
1471
  }
1472

1473
  int32_t nOwnedDbs = taosHashGetSize(pObj->ownedDbs);
7,764,030✔
1474
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nOwnedDbs));
7,764,030✔
1475
  pIter = NULL;
7,764,030✔
1476
  while ((pIter = taosHashIterate(pObj->ownedDbs, pIter))) {
31,927,584✔
1477
    size_t keyLen = 0;
24,163,554✔
1478
    char  *key = taosHashGetKey(pIter, &keyLen);  // key: dbFName
24,163,554✔
1479
    TAOS_CHECK_EXIT(tEncodeCStr(&encoder, key));
24,163,554✔
1480
  }
1481

1482
  tEndEncode(&encoder);
7,764,030✔
1483
  tlen = encoder.pos;
7,764,030✔
1484
_exit:
7,764,030✔
1485
  tEncoderClear(&encoder);
7,764,030✔
1486
  if (code < 0) {
7,764,030✔
1487
    mError("user:%s, %s failed at line %d since %s", pObj->user, __func__, lino, tstrerror(code));
×
1488
    TAOS_RETURN(code);
×
1489
  }
1490

1491
  return tlen;
7,764,030✔
1492
}
1493

1494
static int32_t tDeserializeUserObjExt(void *buf, int32_t bufLen, SUserObj *pObj) {
3,114,432✔
1495
  int32_t  code = 0, lino = 0;
3,114,432✔
1496
  SDecoder decoder = {0};
3,114,432✔
1497
  tDecoderInit(&decoder, buf, bufLen);
3,114,432✔
1498

1499
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
3,114,432✔
1500
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->uid));
6,228,864✔
1501
  TAOS_CHECK_EXIT(tDeserializePrivSysObjPolicies(&decoder, &pObj->sysPrivs, &pObj->objPrivs));
3,114,432✔
1502
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->selectTbs));
3,114,432✔
1503
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->insertTbs));
3,114,432✔
1504
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->updateTbs));
3,114,432✔
1505
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->deleteTbs));
3,114,432✔
1506
  int32_t nRoles = 0;
3,114,432✔
1507
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nRoles));
3,114,432✔
1508
  if (nRoles > 0) {
3,114,432✔
1509
    if (!pObj->roles &&
3,108,133✔
1510
        !(pObj->roles = taosHashInit(nRoles, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), 1, HASH_ENTRY_LOCK))) {
3,108,133✔
1511
      TAOS_CHECK_EXIT(terrno);
×
1512
    }
1513
    for (int32_t i = 0; i < nRoles; i++) {
9,503,282✔
1514
      int32_t keyLen = 0;
6,395,149✔
1515
      char   *key = NULL;
6,395,149✔
1516
      TAOS_CHECK_EXIT(tDecodeCStrAndLen(&decoder, &key, &keyLen));
6,395,149✔
1517
      uint8_t flag = 0;
6,395,149✔
1518
      TAOS_CHECK_EXIT(tDecodeU8(&decoder, &flag));
6,395,149✔
1519
      TAOS_CHECK_EXIT(taosHashPut(pObj->roles, key, keyLen + 1, &flag, sizeof(flag)));
6,395,149✔
1520
    }
1521
  }
1522
  if (!tDecodeIsEnd(&decoder)) {
3,114,432✔
1523
    int32_t nOwnedDbs = 0;
3,114,432✔
1524
    TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nOwnedDbs));
3,114,432✔
1525
    if (nOwnedDbs > 0) {
3,114,432✔
1526
      if (!pObj->ownedDbs &&
1,308,419✔
1527
          !(pObj->ownedDbs =
1,308,419✔
1528
                taosHashInit(nOwnedDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), 1, HASH_ENTRY_LOCK))) {
1,308,419✔
1529
        TAOS_CHECK_EXIT(terrno);
×
1530
      }
1531
      for (int32_t i = 0; i < nOwnedDbs; ++i) {
12,296,062✔
1532
        int32_t keyLen = 0;
10,987,643✔
1533
        char   *key = NULL;
10,987,643✔
1534
        TAOS_CHECK_EXIT(tDecodeCStrAndLen(&decoder, &key, &keyLen));
10,987,643✔
1535
        TAOS_CHECK_EXIT(taosHashPut(pObj->ownedDbs, key, keyLen + 1, NULL, 0));
10,987,643✔
1536
      }
1537
    }
1538
  }
1539

1540
_exit:
3,114,058✔
1541
  tEndDecode(&decoder);
3,114,432✔
1542
  tDecoderClear(&decoder);
3,114,432✔
1543
  if (code < 0) {
3,114,432✔
1544
    mError("user, %s failed at line %d since %s, row:%p", __func__, lino, tstrerror(code), pObj);
×
1545
  }
1546
  TAOS_RETURN(code);
3,114,432✔
1547
}
1548

1549
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
3,882,015✔
1550
  int32_t code = 0;
3,882,015✔
1551
  int32_t lino = 0;
3,882,015✔
1552
  int32_t passReserve = (sizeof(SUserPassword) + 8) * pUser->numOfPasswords + 4;
3,882,015✔
1553
  int32_t ipWhiteReserve =
×
1554
      pUser->pIpWhiteListDual ? (sizeof(SIpRange) * pUser->pIpWhiteListDual->num + sizeof(SIpWhiteListDual) + 4) : 16;
3,882,015✔
1555
  int32_t timeWhiteReserve =
×
1556
      pUser->pTimeWhiteList
3,882,015✔
1557
          ? (sizeof(SDateTimeWhiteListItem) * pUser->pTimeWhiteList->num + sizeof(SDateTimeWhiteList) + 4)
3,882,015✔
1558
          : 16;
1559
  int32_t numOfReadDbs = 0;     // taosHashGetSize(pUser->readDbs);
3,882,015✔
1560
  int32_t numOfWriteDbs = 0;    // taosHashGetSize(pUser->writeDbs);
3,882,015✔
1561
  int32_t numOfReadTbs = 0;     // taosHashGetSize(pUser->readTbs);
3,882,015✔
1562
  int32_t numOfWriteTbs = 0;    // taosHashGetSize(pUser->writeTbs);
3,882,015✔
1563
  int32_t numOfAlterTbs = 0;    // taosHashGetSize(pUser->alterTbs);
3,882,015✔
1564
  int32_t numOfReadViews = 0;   // taosHashGetSize(pUser->readViews);
3,882,015✔
1565
  int32_t numOfWriteViews = 0;  // taosHashGetSize(pUser->writeViews);
3,882,015✔
1566
  int32_t numOfAlterViews = 0;  // taosHashGetSize(pUser->alterViews);
3,882,015✔
1567
  int32_t numOfTopics = 0;      // taosHashGetSize(pUser->topics);
3,882,015✔
1568
  int32_t numOfUseDbs = 0;      // taosHashGetSize(pUser->useDbs);
3,882,015✔
1569
  int32_t numOfRoles = taosHashGetSize(pUser->roles);
3,882,015✔
1570
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
3,882,015✔
1571
                 numOfTopics * TSDB_TOPIC_FNAME_LEN + ipWhiteReserve + timeWhiteReserve + passReserve;
3,882,015✔
1572
  char    *buf = NULL;
3,882,015✔
1573
  SSdbRaw *pRaw = NULL;
3,882,015✔
1574

1575
  char *stb = NULL;
3,882,015✔
1576
#if 0
1577
  stb = taosHashIterate(pUser->readTbs, NULL);
1578
  while (stb != NULL) {
1579
    size_t keyLen = 0;
1580
    void  *key = taosHashGetKey(stb, &keyLen);
1581
    size += sizeof(int32_t);
1582
    size += keyLen;
1583

1584
    size_t valueLen = 0;
1585
    valueLen = strlen(stb) + 1;
1586
    size += sizeof(int32_t);
1587
    size += valueLen;
1588
    stb = taosHashIterate(pUser->readTbs, stb);
1589
  }
1590

1591
  stb = taosHashIterate(pUser->writeTbs, NULL);
1592
  while (stb != NULL) {
1593
    size_t keyLen = 0;
1594
    void  *key = taosHashGetKey(stb, &keyLen);
1595
    size += sizeof(int32_t);
1596
    size += keyLen;
1597

1598
    size_t valueLen = 0;
1599
    valueLen = strlen(stb) + 1;
1600
    size += sizeof(int32_t);
1601
    size += valueLen;
1602
    stb = taosHashIterate(pUser->writeTbs, stb);
1603
  }
1604
  stb = taosHashIterate(pUser->alterTbs, NULL);
1605
  while (stb != NULL) {
1606
    size_t keyLen = 0;
1607
    void  *key = taosHashGetKey(stb, &keyLen);
1608
    size += sizeof(int32_t);
1609
    size += keyLen;
1610

1611
    size_t valueLen = 0;
1612
    valueLen = strlen(stb) + 1;
1613
    size += sizeof(int32_t);
1614
    size += valueLen;
1615
    stb = taosHashIterate(pUser->alterTbs, stb);
1616
  }
1617

1618
  stb = taosHashIterate(pUser->readViews, NULL);
1619
  while (stb != NULL) {
1620
    size_t keyLen = 0;
1621
    void  *key = taosHashGetKey(stb, &keyLen);
1622
    size += sizeof(int32_t);
1623
    size += keyLen;
1624

1625
    size_t valueLen = 0;
1626
    valueLen = strlen(stb) + 1;
1627
    size += sizeof(int32_t);
1628
    size += valueLen;
1629
    stb = taosHashIterate(pUser->readViews, stb);
1630
  }
1631

1632
  stb = taosHashIterate(pUser->writeViews, NULL);
1633
  while (stb != NULL) {
1634
    size_t keyLen = 0;
1635
    void  *key = taosHashGetKey(stb, &keyLen);
1636
    size += sizeof(int32_t);
1637
    size += keyLen;
1638

1639
    size_t valueLen = 0;
1640
    valueLen = strlen(stb) + 1;
1641
    size += sizeof(int32_t);
1642
    size += valueLen;
1643
    stb = taosHashIterate(pUser->writeViews, stb);
1644
  }
1645

1646
  stb = taosHashIterate(pUser->alterViews, NULL);
1647
  while (stb != NULL) {
1648
    size_t keyLen = 0;
1649
    void  *key = taosHashGetKey(stb, &keyLen);
1650
    size += sizeof(int32_t);
1651
    size += keyLen;
1652

1653
    size_t valueLen = 0;
1654
    valueLen = strlen(stb) + 1;
1655
    size += sizeof(int32_t);
1656
    size += valueLen;
1657
    stb = taosHashIterate(pUser->alterViews, stb);
1658
  }
1659

1660
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
1661
  while (useDb != NULL) {
1662
    size_t keyLen = 0;
1663
    void  *key = taosHashGetKey(useDb, &keyLen);
1664
    size += sizeof(int32_t);
1665
    size += keyLen;
1666
    size += sizeof(int32_t);
1667
    useDb = taosHashIterate(pUser->useDbs, useDb);
1668
  }
1669
#endif
1670
  int32_t sizeExt = tSerializeUserObjExt(NULL, 0, pUser);
3,882,015✔
1671
  if (sizeExt < 0) {
3,882,015✔
1672
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1673
  }
1674
  size += sizeExt;
3,882,015✔
1675

1676
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
3,882,015✔
1677
  if (pRaw == NULL) {
3,882,015✔
1678
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1679
  }
1680

1681
  int32_t dataPos = 0;
3,882,015✔
1682
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
3,882,015✔
1683

1684
  dropOldPasswords(pUser);
3,882,015✔
1685
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
3,882,015✔
1686
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
8,566,174✔
1687
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
4,684,159✔
1688
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
4,684,159✔
1689
  }
1690
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
3,882,015✔
1691

1692
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
3,882,015✔
1693
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
3,882,015✔
1694
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
3,882,015✔
1695
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
3,882,015✔
1696
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
3,882,015✔
1697
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
3,882,015✔
1698
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
3,882,015✔
1699
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
3,882,015✔
1700
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
3,882,015✔
1701
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
3,882,015✔
1702
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
3,882,015✔
1703
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
3,882,015✔
1704
#if 0
1705
  char *db = taosHashIterate(pUser->readDbs, NULL);
1706
  while (db != NULL) {
1707
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
1708
    db = taosHashIterate(pUser->readDbs, db);
1709
  }
1710

1711
  db = taosHashIterate(pUser->writeDbs, NULL);
1712
  while (db != NULL) {
1713
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
1714
    db = taosHashIterate(pUser->writeDbs, db);
1715
  }
1716
  char *topic = taosHashIterate(pUser->topics, NULL);
1717
  while (topic != NULL) {
1718
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
1719
    topic = taosHashIterate(pUser->topics, topic);
1720
  }
1721
#endif
1722
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
3,882,015✔
1723
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
3,882,015✔
1724
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
3,882,015✔
1725
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
3,882,015✔
1726
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
3,882,015✔
1727
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
3,882,015✔
1728
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
3,882,015✔
1729

1730
#if 0
1731
  stb = taosHashIterate(pUser->readTbs, NULL);
1732
  while (stb != NULL) {
1733
    size_t keyLen = 0;
1734
    void  *key = taosHashGetKey(stb, &keyLen);
1735
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1736
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1737

1738
    size_t valueLen = 0;
1739
    valueLen = strlen(stb) + 1;
1740
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1741
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1742
    stb = taosHashIterate(pUser->readTbs, stb);
1743
  }
1744

1745
  stb = taosHashIterate(pUser->writeTbs, NULL);
1746
  while (stb != NULL) {
1747
    size_t keyLen = 0;
1748
    void  *key = taosHashGetKey(stb, &keyLen);
1749
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1750
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1751

1752
    size_t valueLen = 0;
1753
    valueLen = strlen(stb) + 1;
1754
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1755
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1756
    stb = taosHashIterate(pUser->writeTbs, stb);
1757
  }
1758
  stb = taosHashIterate(pUser->alterTbs, NULL);
1759
  while (stb != NULL) {
1760
    size_t keyLen = 0;
1761
    void  *key = taosHashGetKey(stb, &keyLen);
1762
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1763
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1764

1765
    size_t valueLen = 0;
1766
    valueLen = strlen(stb) + 1;
1767
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1768
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1769
    stb = taosHashIterate(pUser->alterTbs, stb);
1770
  }
1771

1772
  stb = taosHashIterate(pUser->readViews, NULL);
1773
  while (stb != NULL) {
1774
    size_t keyLen = 0;
1775
    void  *key = taosHashGetKey(stb, &keyLen);
1776
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1777
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1778

1779
    size_t valueLen = 0;
1780
    valueLen = strlen(stb) + 1;
1781
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1782
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1783
    stb = taosHashIterate(pUser->readViews, stb);
1784
  }
1785

1786
  stb = taosHashIterate(pUser->writeViews, NULL);
1787
  while (stb != NULL) {
1788
    size_t keyLen = 0;
1789
    void  *key = taosHashGetKey(stb, &keyLen);
1790
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1791
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1792

1793
    size_t valueLen = 0;
1794
    valueLen = strlen(stb) + 1;
1795
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1796
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1797
    stb = taosHashIterate(pUser->writeViews, stb);
1798
  }
1799

1800
  stb = taosHashIterate(pUser->alterViews, NULL);
1801
  while (stb != NULL) {
1802
    size_t keyLen = 0;
1803
    void  *key = taosHashGetKey(stb, &keyLen);
1804
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1805
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1806

1807
    size_t valueLen = 0;
1808
    valueLen = strlen(stb) + 1;
1809
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
1810
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
1811
    stb = taosHashIterate(pUser->alterViews, stb);
1812
  }
1813

1814
  useDb = taosHashIterate(pUser->useDbs, NULL);
1815
  while (useDb != NULL) {
1816
    size_t keyLen = 0;
1817
    void  *key = taosHashGetKey(useDb, &keyLen);
1818
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
1819
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
1820

1821
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
1822
    useDb = taosHashIterate(pUser->useDbs, useDb);
1823
  }
1824
#endif
1825
  // save white list
1826
  int32_t num = pUser->pIpWhiteListDual->num;
3,882,015✔
1827
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
3,882,015✔
1828
  int32_t maxBufLen = TMAX(tlen, sizeExt);
3,882,015✔
1829
  if ((buf = taosMemoryCalloc(1, maxBufLen)) == NULL) {
3,882,015✔
1830
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1831
  }
1832
  int32_t len = 0;
3,882,015✔
1833
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
3,882,015✔
1834

1835
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
3,882,015✔
1836
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
3,882,015✔
1837

1838
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
3,882,015✔
1839
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
3,882,015✔
1840

1841
  SDB_SET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
3,882,015✔
1842
  SDB_SET_INT8(pRaw, dataPos, pUser->changePass, _OVER);
3,882,015✔
1843
  SDB_SET_INT32(pRaw, dataPos, pUser->sessionPerUser, _OVER);
3,882,015✔
1844
  SDB_SET_INT32(pRaw, dataPos, pUser->connectTime, _OVER);
3,882,015✔
1845
  SDB_SET_INT32(pRaw, dataPos, pUser->connectIdleTime, _OVER);
3,882,015✔
1846
  SDB_SET_INT32(pRaw, dataPos, pUser->callPerSession, _OVER);
3,882,015✔
1847
  SDB_SET_INT32(pRaw, dataPos, pUser->vnodePerCall, _OVER);
3,882,015✔
1848
  SDB_SET_INT32(pRaw, dataPos, pUser->failedLoginAttempts, _OVER);
3,882,015✔
1849
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLifeTime, _OVER);
3,882,015✔
1850
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseTime, _OVER);
3,882,015✔
1851
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseMax, _OVER);
3,882,015✔
1852
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLockTime, _OVER);
3,882,015✔
1853
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordGraceTime, _OVER);
3,882,015✔
1854
  SDB_SET_INT32(pRaw, dataPos, pUser->inactiveAccountTime, _OVER);
3,882,015✔
1855
  SDB_SET_INT32(pRaw, dataPos, pUser->allowTokenNum, _OVER);
3,882,015✔
1856
  SDB_SET_INT32(pRaw, dataPos, pUser->tokenNum, _OVER);
3,882,015✔
1857

1858
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
3,882,015✔
1859
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
3,889,165✔
1860
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
7,150✔
1861
    SDB_SET_BOOL(pRaw, dataPos, range->absolute, _OVER);
7,150✔
1862
    SDB_SET_BOOL(pRaw, dataPos, range->neg, _OVER);
7,150✔
1863
    SDB_SET_INT64(pRaw, dataPos, range->start, _OVER);
7,150✔
1864
    SDB_SET_INT32(pRaw, dataPos, range->duration, _OVER);
7,150✔
1865
  }
1866

1867
  sizeExt = tSerializeUserObjExt(buf, sizeExt, pUser);
3,882,015✔
1868
  if (sizeExt < 0) {
3,882,015✔
1869
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1870
  }
1871
  SDB_SET_INT32(pRaw, dataPos, sizeExt, _OVER);
3,882,015✔
1872
  SDB_SET_BINARY(pRaw, dataPos, buf, sizeExt, _OVER);
3,882,015✔
1873

1874
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
3,882,015✔
1875

1876
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
3,882,015✔
1877

1878
_OVER:
3,882,015✔
1879
  taosMemoryFree(buf);
3,882,015✔
1880
  if (code < 0) {
3,882,015✔
1881
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1882
           tstrerror(code));
1883
    sdbFreeRaw(pRaw);
×
1884
    pRaw = NULL;
×
1885
    terrno = code;
×
1886
  }
1887

1888
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
3,882,015✔
1889
  return pRaw;
3,882,015✔
1890
}
1891

1892
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
3,114,432✔
1893
  int32_t   code = 0;
3,114,432✔
1894
  int32_t   lino = 0;
3,114,432✔
1895
  SSdbRow  *pRow = NULL;
3,114,432✔
1896
  SUserObj *pUser = NULL;
3,114,432✔
1897
  char     *key = NULL;
3,114,432✔
1898
  char     *value = NULL;
3,114,432✔
1899

1900
  int8_t sver = 0;
3,114,432✔
1901
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
3,114,432✔
1902
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1903
  }
1904

1905
  if (sver < 1 || sver > USER_VER_NUMBER) {
3,114,432✔
1906
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1907
  }
1908

1909
  pRow = sdbAllocRow(sizeof(SUserObj));
3,114,432✔
1910
  if (pRow == NULL) {
3,114,432✔
1911
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1912
  }
1913

1914
  pUser = sdbGetRowObj(pRow);
3,114,432✔
1915
  if (pUser == NULL) {
3,114,432✔
1916
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1917
  }
1918

1919
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
3,114,432✔
1920
    upgradeSecurity = 1;
×
1921
    pUser->legacyPrivs = taosMemCalloc(1, sizeof(SPrivHashObjSet));
×
1922
    if (pUser->legacyPrivs == NULL) {
×
1923
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1924
    }
1925
  }
1926

1927
  int32_t dataPos = 0;
3,114,432✔
1928
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
3,114,432✔
1929

1930
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
3,114,432✔
1931
    pUser->passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
×
1932
    if (pUser->passwords == NULL) {
×
1933
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1934
    }
1935
    SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[0].pass, TSDB_PASSWORD_LEN, _OVER)
×
1936
    pUser->numOfPasswords = 1;
×
1937
    memset(pUser->salt, 0, sizeof(pUser->salt));
×
1938
  } else {
1939
    SDB_GET_INT32(pRaw, dataPos, &pUser->numOfPasswords, _OVER)
3,114,432✔
1940
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
3,114,432✔
1941
    if (pUser->passwords == NULL) {
3,114,432✔
1942
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1943
    }
1944
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
6,986,234✔
1945
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
3,871,802✔
1946
      SDB_GET_INT32(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
3,871,802✔
1947
    }
1948
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
3,114,432✔
1949
  }
1950

1951
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
3,114,432✔
1952
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
3,114,432✔
1953
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
3,114,432✔
1954
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
3,114,432✔
1955
    pUser->passwords[0].setTime = (int32_t)(pUser->updateTime / 1000);
×
1956
  }
1957

1958
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
3,114,432✔
1959
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
3,114,432✔
1960
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
3,114,432✔
1961
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
3,114,432✔
1962
  if (pUser->superUser) pUser->createdb = 1;
3,114,432✔
1963
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
3,114,432✔
1964
  if (sver >= 4) {
3,114,432✔
1965
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
3,114,432✔
1966
  }
1967

1968
  int32_t numOfReadDbs = 0;
3,114,432✔
1969
  int32_t numOfWriteDbs = 0;
3,114,432✔
1970
  int32_t numOfTopics = 0;
3,114,432✔
1971
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
3,114,432✔
1972
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
3,114,432✔
1973
  if (sver >= 2) {
3,114,432✔
1974
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
3,114,432✔
1975
  }
1976

1977
  if (numOfReadDbs > 0) {
3,114,432✔
1978
    pUser->legacyPrivs->pReadDbs =
×
1979
        taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
1980
    if (pUser->legacyPrivs->pReadDbs == NULL) {
×
1981
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1982
    }
1983
  }
1984
  if (numOfWriteDbs > 0) {
3,114,432✔
1985
    pUser->legacyPrivs->pWriteDbs =
×
1986
        taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
1987
    if (pUser->legacyPrivs->pWriteDbs == NULL) {
×
1988
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1989
    }
1990
  }
1991
  if (numOfTopics > 0) {
3,114,432✔
1992
    pUser->legacyPrivs->pTopics =
×
1993
        taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
1994
    if (pUser->legacyPrivs->pTopics == NULL) {
×
1995
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1996
    }
1997
  }
1998
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
3,114,432✔
1999
    char db[TSDB_DB_FNAME_LEN] = {0};
×
2000
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
2001
    int32_t len = strlen(db) + 1;
×
2002
    TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
2003
  }
2004

2005
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
3,114,432✔
2006
    char db[TSDB_DB_FNAME_LEN] = {0};
×
2007
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
2008
    int32_t len = strlen(db) + 1;
×
2009
    TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
2010
  }
2011

2012
  if (sver >= 2) {
3,114,432✔
2013
    for (int32_t i = 0; i < numOfTopics; ++i) {
3,114,432✔
2014
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
×
2015
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
×
2016
      int32_t len = strlen(topic) + 1;
×
2017
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pTopics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
×
2018
    }
2019
  }
2020

2021
  if (sver >= 3) {
3,114,432✔
2022
    int32_t numOfReadTbs = 0;
3,114,432✔
2023
    int32_t numOfWriteTbs = 0;
3,114,432✔
2024
    int32_t numOfAlterTbs = 0;
3,114,432✔
2025
    int32_t numOfReadViews = 0;
3,114,432✔
2026
    int32_t numOfWriteViews = 0;
3,114,432✔
2027
    int32_t numOfAlterViews = 0;
3,114,432✔
2028
    int32_t numOfUseDbs = 0;
3,114,432✔
2029
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
3,114,432✔
2030
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
3,114,432✔
2031
    if (sver >= 6) {
3,114,432✔
2032
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
3,114,432✔
2033
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
3,114,432✔
2034
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
3,114,432✔
2035
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
3,114,432✔
2036
    }
2037
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
3,114,432✔
2038

2039
    if (numOfReadTbs > 0) {
3,114,432✔
2040
      pUser->legacyPrivs->pReadTbs =
×
2041
          taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2042
      if (pUser->legacyPrivs->pReadTbs == NULL) {
×
2043
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2044
      }
2045
    }
2046
    if (numOfWriteTbs > 0) {
3,114,432✔
2047
      pUser->legacyPrivs->pWriteTbs =
×
2048
          taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2049
      if (pUser->legacyPrivs->pWriteTbs == NULL) {
×
2050
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2051
      }
2052
    }
2053
    if (numOfAlterTbs > 0) {
3,114,432✔
2054
      pUser->legacyPrivs->pAlterTbs =
×
2055
          taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2056
      if (pUser->legacyPrivs->pAlterTbs == NULL) {
×
2057
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2058
      }
2059
    }
2060
    if (numOfReadViews > 0) {
3,114,432✔
2061
      pUser->legacyPrivs->pReadViews =
×
2062
          taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2063
      if (pUser->legacyPrivs->pReadViews == NULL) {
×
2064
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2065
      }
2066
    }
2067
    if (numOfWriteViews > 0) {
3,114,432✔
2068
      pUser->legacyPrivs->pWriteViews =
×
2069
          taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2070
      if (pUser->legacyPrivs->pWriteViews == NULL) {
×
2071
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2072
      }
2073
    }
2074
    if (numOfAlterViews > 0) {
3,114,432✔
2075
      pUser->legacyPrivs->pAlterViews =
×
2076
          taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2077
      if (pUser->legacyPrivs->pAlterViews == NULL) {
×
2078
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2079
      }
2080
    }
2081
    if (numOfUseDbs > 0) {
3,114,432✔
2082
      pUser->legacyPrivs->pUseDbs =
×
2083
          taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2084
      if (pUser->legacyPrivs->pUseDbs == NULL) {
×
2085
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2086
      }
2087
    }
2088

2089
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
3,114,432✔
2090
      int32_t keyLen = 0;
×
2091
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2092

2093
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2094
      if (key == NULL) {
×
2095
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2096
      }
2097
      (void)memset(key, 0, keyLen);
×
2098
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2099

2100
      int32_t valuelen = 0;
×
2101
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
2102
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
2103
      if (value == NULL) {
×
2104
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2105
      }
2106
      (void)memset(value, 0, valuelen);
×
2107
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
2108

2109
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2110
    }
2111

2112
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
3,114,432✔
2113
      int32_t keyLen = 0;
×
2114
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2115

2116
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2117
      if (key == NULL) {
×
2118
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2119
      }
2120
      (void)memset(key, 0, keyLen);
×
2121
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2122

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

2132
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2133
    }
2134

2135
    if (sver >= 6) {
3,114,432✔
2136
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
3,114,432✔
2137
        int32_t keyLen = 0;
×
2138
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2139

2140
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2141
        if (key == NULL) {
×
2142
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2143
        }
2144
        (void)memset(key, 0, keyLen);
×
2145
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2146

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

2156
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pAlterTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
2157
      }
2158

2159
      for (int32_t i = 0; i < numOfReadViews; ++i) {
3,114,432✔
2160
        int32_t keyLen = 0;
×
2161
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2162

2163
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2164
        if (key == NULL) {
×
2165
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2166
        }
2167
        (void)memset(key, 0, keyLen);
×
2168
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2169

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

2179
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pReadViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2180
      }
2181

2182
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
3,114,432✔
2183
        int32_t keyLen = 0;
×
2184
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2185

2186
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2187
        if (key == NULL) {
×
2188
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2189
        }
2190
        (void)memset(key, 0, keyLen);
×
2191
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2192

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

2202
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pWriteViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2203
      }
2204

2205
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
3,114,432✔
2206
        int32_t keyLen = 0;
×
2207
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2208

2209
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2210
        if (key == NULL) {
×
2211
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2212
        }
2213
        (void)memset(key, 0, keyLen);
×
2214
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2215

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

2225
        TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pAlterViews, key, keyLen, value, valuelen), &lino, _OVER);
×
2226
      }
2227
    }
2228

2229
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
3,114,432✔
2230
      int32_t keyLen = 0;
×
2231
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
2232

2233
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
2234
      if (key == NULL) {
×
2235
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2236
      }
2237
      (void)memset(key, 0, keyLen);
×
2238
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
2239

2240
      int32_t ref = 0;
×
2241
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
×
2242

2243
      TAOS_CHECK_GOTO(taosHashPut(pUser->legacyPrivs->pUseDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
×
2244
    }
2245
  }
2246
  // decoder white list
2247
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
3,114,432✔
2248
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
3,114,432✔
2249
      int32_t len = 0;
×
2250
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
2251

2252
      TAOS_MEMORY_REALLOC(key, len);
×
2253
      if (key == NULL) {
×
2254
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2255
      }
2256
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
2257

2258
      SIpWhiteList *pIpWhiteList = NULL;
×
2259
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
2260

2261
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
2262

2263
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
2264
      if (code != 0) {
×
2265
        taosMemoryFreeClear(pIpWhiteList);
×
2266
      }
2267
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2268

2269
      taosMemoryFreeClear(pIpWhiteList);
×
2270

2271
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
3,114,432✔
2272
      int32_t len = 0;
3,114,432✔
2273
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
3,114,432✔
2274

2275
      TAOS_MEMORY_REALLOC(key, len);
3,114,432✔
2276
      if (key == NULL) {
3,114,432✔
2277
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2278
      }
2279
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
3,114,432✔
2280

2281
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual, sver >= USER_VER_SUPPORT_ADVANCED_SECURITY),
3,114,432✔
2282
                      &lino, _OVER);
2283
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
3,114,432✔
2284
    }
2285
  }
2286

2287
  if (pUser->pIpWhiteListDual == NULL) {
3,114,432✔
2288
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
2289
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
2290
  }
2291

2292
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
3,114,432✔
2293

2294
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
3,114,432✔
2295
    memset(pUser->totpsecret, 0, sizeof(pUser->totpsecret));
×
2296
    pUser->changePass = 2;
×
2297
    pUser->sessionPerUser = pUser->superUser ? -1 : TSDB_USER_SESSION_PER_USER_DEFAULT;
×
2298
    pUser->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
×
2299
    pUser->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
×
2300
    pUser->callPerSession = pUser->superUser ? -1 : TSDB_USER_CALL_PER_SESSION_DEFAULT;
×
2301
    pUser->vnodePerCall = pUser->superUser ? -1 : TSDB_USER_VNODE_PER_CALL_DEFAULT;
×
2302
    pUser->failedLoginAttempts = pUser->superUser ? -1 : TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
×
2303
    pUser->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
×
2304
    pUser->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
×
2305
    pUser->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
×
2306
    pUser->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
×
2307
    pUser->passwordGraceTime = pUser->superUser ? -1 : TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
×
2308
    pUser->inactiveAccountTime = pUser->superUser ? -1 : TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
×
2309
    pUser->allowTokenNum = pUser->superUser ? -1 : TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
×
2310
    pUser->tokenNum = 0;
×
2311
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList));
×
2312
    if (pUser->pTimeWhiteList == NULL) {
×
2313
    }
2314
  } else {
2315
    SDB_GET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
3,114,432✔
2316
    SDB_GET_INT8(pRaw, dataPos, &pUser->changePass, _OVER);
3,114,432✔
2317
    SDB_GET_INT32(pRaw, dataPos, &pUser->sessionPerUser, _OVER);
3,114,432✔
2318
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectTime, _OVER);
3,114,432✔
2319
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectIdleTime, _OVER);
3,114,432✔
2320
    SDB_GET_INT32(pRaw, dataPos, &pUser->callPerSession, _OVER);
3,114,432✔
2321
    SDB_GET_INT32(pRaw, dataPos, &pUser->vnodePerCall, _OVER);
3,114,432✔
2322
    SDB_GET_INT32(pRaw, dataPos, &pUser->failedLoginAttempts, _OVER);
3,114,432✔
2323
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLifeTime, _OVER);
3,114,432✔
2324
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseTime, _OVER);
3,114,432✔
2325
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseMax, _OVER);
3,114,432✔
2326
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLockTime, _OVER);
3,114,432✔
2327
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordGraceTime, _OVER);
3,114,432✔
2328
    SDB_GET_INT32(pRaw, dataPos, &pUser->inactiveAccountTime, _OVER);
3,114,432✔
2329
    SDB_GET_INT32(pRaw, dataPos, &pUser->allowTokenNum, _OVER);
3,114,432✔
2330
    SDB_GET_INT32(pRaw, dataPos, &pUser->tokenNum, _OVER);
3,114,432✔
2331

2332
    int32_t num = 0;
3,114,432✔
2333
    SDB_GET_INT32(pRaw, dataPos, &num, _OVER);
3,114,432✔
2334
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList) + num * sizeof(SDateTimeWhiteListItem));
3,114,432✔
2335
    if (pUser->pTimeWhiteList == NULL) {
3,114,432✔
2336
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2337
    }
2338

2339
    pUser->pTimeWhiteList->num = num;
3,114,432✔
2340
    for (int32_t i = 0; i < num; i++) {
3,117,578✔
2341
      SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
3,146✔
2342
      SDB_GET_BOOL(pRaw, dataPos, &range->absolute, _OVER);
3,146✔
2343
      SDB_GET_BOOL(pRaw, dataPos, &range->neg, _OVER);
3,146✔
2344
      SDB_GET_INT64(pRaw, dataPos, &range->start, _OVER);
3,146✔
2345
      SDB_GET_INT32(pRaw, dataPos, &range->duration, _OVER);
3,146✔
2346
    }
2347

2348
    int32_t extLen = 0;
3,114,432✔
2349
    SDB_GET_INT32(pRaw, dataPos, &extLen, _OVER);
3,114,432✔
2350
    TAOS_MEMORY_REALLOC(key, extLen);
3,114,432✔
2351
    if (key == NULL) {
3,114,432✔
2352
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2353
    }
2354
    SDB_GET_BINARY(pRaw, dataPos, key, extLen, _OVER);
3,114,432✔
2355
    TAOS_CHECK_GOTO(tDeserializeUserObjExt(key, extLen, pUser), &lino, _OVER);
3,114,432✔
2356
  }
2357

2358
#ifndef TD_ENTERPRISE
2359
  // community users cannot modify these fields, and the default values may prevent
2360
  // the user from logging in, so we set them to different values here.
2361
  pUser->sessionPerUser = -1;
2362
  pUser->connectTime = -1;
2363
  pUser->connectIdleTime = -1;
2364
  pUser->callPerSession = -1;
2365
  pUser->vnodePerCall = -1;
2366
  pUser->failedLoginAttempts = -1;
2367
  pUser->passwordLifeTime = -1;
2368
  pUser->passwordReuseTime = 0;
2369
  pUser->passwordReuseMax = 0;
2370
  pUser->passwordLockTime = -1;
2371
  pUser->passwordGraceTime = -1;
2372
  pUser->inactiveAccountTime = -1;
2373
  pUser->allowTokenNum = -1;
2374
  pUser->tokenNum = 0;
2375
#endif  // TD_ENTERPRISE
2376

2377
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
3,114,432✔
2378

2379
  taosInitRWLatch(&pUser->lock);
3,114,432✔
2380
  dropOldPasswords(pUser);
3,114,432✔
2381
_OVER:
3,114,432✔
2382
  taosMemoryFree(key);
3,114,432✔
2383
  taosMemoryFree(value);
3,114,432✔
2384
  if (code < 0) {
3,114,432✔
2385
    terrno = code;
×
2386
    mError("user:%s, failed to decode at line %d from raw:%p since %s", pUser == NULL ? "null" : pUser->user, lino,
×
2387
           pRaw, tstrerror(code));
2388
    if (pUser != NULL) {
×
2389
      mndUserFreeObj(pUser);
×
2390
    }
2391
    taosMemoryFreeClear(pRow);
×
2392
    return NULL;
×
2393
  }
2394

2395
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
3,114,432✔
2396
  return pRow;
3,114,432✔
2397
}
2398

2399
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
508,597✔
2400
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
508,597✔
2401

2402
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
508,597✔
2403
  if (pAcct == NULL) {
508,597✔
2404
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
2405
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
2406
    TAOS_RETURN(terrno);
×
2407
  }
2408
  pUser->acctId = pAcct->acctId;
508,597✔
2409
  sdbRelease(pSdb, pAcct);
508,597✔
2410

2411
  return 0;
508,597✔
2412
}
2413

2414
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
×
2415
  int32_t code = 0;
×
2416
  *ppNew =
×
2417
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
2418
  if (*ppNew == NULL) {
×
2419
    TAOS_RETURN(terrno);
×
2420
  }
2421

2422
  char *tb = taosHashIterate(pOld, NULL);
×
2423
  while (tb != NULL) {
×
2424
    size_t keyLen = 0;
×
2425
    char  *key = taosHashGetKey(tb, &keyLen);
×
2426

2427
    int32_t valueLen = strlen(tb) + 1;
×
2428
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
×
2429
      taosHashCancelIterate(pOld, tb);
×
2430
      taosHashCleanup(*ppNew);
×
2431
      TAOS_RETURN(code);
×
2432
    }
2433
    tb = taosHashIterate(pOld, tb);
×
2434
  }
2435

2436
  TAOS_RETURN(code);
×
2437
}
2438

2439
int32_t mndDupRoleHash(SHashObj *pOld, SHashObj **ppNew) {
2,514,126✔
2440
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
2,514,126✔
2441
                              HASH_ENTRY_LOCK))) {
2442
    TAOS_RETURN(terrno);
×
2443
  }
2444

2445
  int32_t  code = 0;
2,514,126✔
2446
  uint8_t *flag = NULL;
2,514,126✔
2447
  while ((flag = taosHashIterate(pOld, flag))) {
7,366,625✔
2448
    size_t keyLen = 0;
4,852,499✔
2449
    char  *key = taosHashGetKey(flag, &keyLen);
4,852,499✔
2450

2451
    if ((code = taosHashPut(*ppNew, key, keyLen, flag, sizeof(*flag))) != 0) {
4,852,499✔
2452
      taosHashCancelIterate(pOld, flag);
×
2453
      taosHashCleanup(*ppNew);
×
2454
      TAOS_RETURN(code);
×
2455
    }
2456
  }
2457

2458
  TAOS_RETURN(code);
2,514,126✔
2459
}
2460

2461
int32_t mndMergePrivObjHash(SHashObj *pOld, SHashObj **ppNew) {
81,730,062✔
2462
  if (!(*ppNew)) return mndDupPrivObjHash(pOld, ppNew);
81,730,062✔
2463

2464
  int32_t           code = 0, lino = 0;
81,729,916✔
2465
  SHashObj         *pNew = *ppNew;
81,729,916✔
2466
  SPrivObjPolicies *policies = NULL;
81,730,468✔
2467
  while ((policies = taosHashIterate(pOld, policies))) {
736,725,342✔
2468
    size_t klen = 0;
655,002,164✔
2469
    char  *key = taosHashGetKey(policies, &klen);
655,002,164✔
2470
    size_t vlen = taosHashGetValueSize(policies);
654,999,857✔
2471

2472
    SPrivObjPolicies *pNewPolicies = taosHashGet(pNew, key, klen);
654,999,417✔
2473
    if (pNewPolicies) {
654,999,883✔
2474
      size_t newVlen = taosHashGetValueSize(pNewPolicies);
432,816,498✔
2475
      if (newVlen > 0 && vlen > 0) {
432,816,394✔
2476
        privAddSet(&pNewPolicies->policy, &policies->policy);
432,816,394✔
2477
      }
2478
      continue;
432,812,485✔
2479
    }
2480

2481
    if ((code = taosHashPut(pNew, key, klen, vlen ? policies : NULL, vlen)) != 0) {
222,183,385✔
2482
      taosHashCancelIterate(pOld, policies);
×
2483
      taosHashCleanup(pNew);
×
2484
      *ppNew = NULL;
×
2485
      TAOS_RETURN(code);
×
2486
    }
2487
  }
2488

2489
  TAOS_RETURN(code);
81,730,874✔
2490
}
2491

2492
/**
2493
 * 1. Prefer to use SPrivTblPolicies from user object(the updateUs of policy in user object is INT64_MAX)
2494
 * 2. If two or more roles have SPrivTblPolicies, the policy with latest update time take effect.
2495
 */
2496
int32_t mndMergePrivTblHash(SHashObj *pOld, SHashObj **ppNew, bool updateWithLatest) {
245,192,027✔
2497
  if (!(*ppNew)) return mndDupPrivTblHash(pOld, ppNew, false);
245,192,027✔
2498

2499
  int32_t           code = 0, lino = 0;
245,192,027✔
2500
  SHashObj         *pNew = *ppNew;
245,192,027✔
2501
  SPrivTblPolicies *policies = NULL;
245,192,479✔
2502
  while ((policies = taosHashIterate(pOld, policies))) {
245,192,479✔
2503
    size_t klen = 0;
×
2504
    char  *key = taosHashGetKey(policies, &klen);
×
2505
    size_t vlen = taosHashGetValueSize(policies);
×
2506

2507
    SPrivTblPolicies *pNewPolicies = taosHashGet(pNew, key, klen);
×
2508
    if (pNewPolicies) {
×
2509
      size_t newVlen = taosHashGetValueSize(pNewPolicies);
×
2510
      if (newVlen > 0 && vlen > 0) {
×
2511
        TAOS_CHECK_EXIT(privTblPoliciesMerge(pNewPolicies, policies, updateWithLatest));
×
2512
      }
2513
      continue;
×
2514
    }
2515

2516
    SPrivTblPolicies tmpPolicies = {0};
×
2517
    if (vlen > 0) {
×
2518
      if ((code = privTblPoliciesMerge(&tmpPolicies, policies, updateWithLatest))) {
×
2519
        privTblPoliciesFree(&tmpPolicies);
2520
        goto _exit;
×
2521
      }
2522
    }
2523
    if ((code = taosHashPut(pNew, key, klen, vlen ? &tmpPolicies : NULL, vlen)) != 0) {
×
2524
      privTblPoliciesFree(&tmpPolicies);
2525
      taosHashCancelIterate(pOld, policies);
×
2526
      taosHashCleanup(pNew);
×
2527
      *ppNew = NULL;
×
2528
      TAOS_RETURN(code);
×
2529
    }
2530
  }
2531

2532
_exit:
245,192,073✔
2533
  TAOS_RETURN(code);
245,192,073✔
2534
}
2535

2536
int32_t mndDupKVHash(SHashObj *pOld, SHashObj **ppNew) {
30,144,971✔
2537
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
30,144,971✔
2538
                              HASH_ENTRY_LOCK))) {
2539
    TAOS_RETURN(terrno);
×
2540
  }
2541
  int32_t code = 0;
30,144,711✔
2542
  void   *val = NULL;
30,144,711✔
2543
  while ((val = taosHashIterate(pOld, val))) {
94,567,870✔
2544
    size_t klen = 0;
64,423,159✔
2545
    char  *key = taosHashGetKey(val, &klen);
64,423,159✔
2546
    size_t vlen = taosHashGetValueSize(val);
64,423,159✔
2547
    if ((code = taosHashPut(*ppNew, key, klen, vlen > 0 ? val : NULL, vlen)) != 0) {
64,422,439✔
2548
      taosHashCancelIterate(pOld, val);
×
2549
      taosHashCleanup(*ppNew);
×
2550
      TAOS_RETURN(code);
×
2551
    }
2552
  }
2553

2554
  TAOS_RETURN(code);
30,145,117✔
2555
}
2556

2557
int32_t mndDupPrivObjHash(SHashObj *pOld, SHashObj **ppNew) {
30,146,095✔
2558
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
30,146,095✔
2559
                              HASH_ENTRY_LOCK))) {
2560
    TAOS_RETURN(terrno);
×
2561
  }
2562
  int32_t           code = 0;
30,146,501✔
2563
  SPrivObjPolicies *policies = NULL;
30,146,501✔
2564
  while ((policies = taosHashIterate(pOld, policies))) {
76,401,966✔
2565
    size_t klen = 0;
46,255,465✔
2566
    char  *key = taosHashGetKey(policies, &klen);
46,255,465✔
2567
    size_t vlen = taosHashGetValueSize(policies);
46,255,465✔
2568

2569
    if ((code = taosHashPut(*ppNew, key, klen, vlen > 0 ? policies : NULL, vlen)) != 0) {
46,255,465✔
2570
      taosHashCancelIterate(pOld, policies);
×
2571
      taosHashCleanup(*ppNew);
×
2572
      TAOS_RETURN(code);
×
2573
    }
2574
  }
2575

2576
  TAOS_RETURN(code);
30,145,753✔
2577
}
2578

2579
int32_t mndDupPrivTblHash(SHashObj *pOld, SHashObj **ppNew, bool setUpdateTimeMax) {
92,952,073✔
2580
  if (!(*ppNew = taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
92,952,073✔
2581
                              HASH_ENTRY_LOCK))) {
2582
    TAOS_RETURN(terrno);
×
2583
  }
2584
  taosHashSetFreeFp(*ppNew, privTblPoliciesFree);
92,952,245✔
2585

2586
  int32_t           code = 0, lino = 0;
92,951,693✔
2587
  SPrivTblPolicies *policies = NULL, *pTmpPolicies = NULL;
92,951,693✔
2588
  SPrivTblPolicies  tmpPolicies = {0};
92,951,693✔
2589
  while ((policies = taosHashIterate(pOld, policies))) {
93,437,467✔
2590
    size_t klen = 0;
485,774✔
2591
    char  *key = taosHashGetKey(policies, &klen);
485,774✔
2592
    size_t vlen = taosHashGetValueSize(policies);
485,774✔
2593
    memset(&tmpPolicies, 0, sizeof(tmpPolicies));
485,774✔
2594
    pTmpPolicies = &tmpPolicies;
485,774✔
2595
    if (vlen > 0) {
485,774✔
2596
      TAOS_CHECK_EXIT(privTblPoliciesAdd(&tmpPolicies, policies, true, setUpdateTimeMax));
485,774✔
2597
    }
2598
    TAOS_CHECK_EXIT(taosHashPut(*ppNew, key, klen, vlen > 0 ? &tmpPolicies : NULL, vlen));
485,774✔
2599
    pTmpPolicies = NULL;
485,774✔
2600
  }
2601

2602
_exit:
92,952,245✔
2603
  if (code != 0) {
92,952,245✔
2604
    if (!pTmpPolicies) {
×
2605
      privTblPoliciesFree(pTmpPolicies);
2606
      pTmpPolicies = NULL;
×
2607
    }
2608
    if (policies) taosHashCancelIterate(pOld, policies);
×
2609
    taosHashCleanup(*ppNew);
×
2610
    *ppNew = NULL;
×
2611
  }
2612
  TAOS_RETURN(code);
92,952,245✔
2613
}
2614

2615
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
2,511,358✔
2616
  int32_t code = 0;
2,511,358✔
2617
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
2,511,358✔
2618
  pNew->authVersion++;
2,511,358✔
2619
  pNew->updateTime = taosGetTimestampMs();
2,511,358✔
2620
  taosInitRWLatch(&pNew->lock);
2,511,358✔
2621

2622
  pNew->passwords = NULL;
2,511,358✔
2623
  pNew->pIpWhiteListDual = NULL;
2,511,358✔
2624
  pNew->passwords = NULL;
2,511,358✔
2625
  pNew->objPrivs = NULL;
2,511,358✔
2626
  pNew->selectTbs = NULL;
2,511,358✔
2627
  pNew->insertTbs = NULL;
2,511,358✔
2628
  pNew->updateTbs = NULL;
2,511,358✔
2629
  pNew->deleteTbs = NULL;
2,511,358✔
2630
  pNew->ownedDbs = NULL;
2,511,358✔
2631
  pNew->pTimeWhiteList = NULL;
2,511,358✔
2632
  pNew->roles = NULL;
2,511,358✔
2633
  pNew->legacyPrivs = NULL;
2,511,358✔
2634

2635
  taosRLockLatch(&pUser->lock);
2,511,358✔
2636
  pNew->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
2,511,358✔
2637
  if (pNew->passwords == NULL) {
2,511,358✔
2638
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2639
    goto _OVER;
×
2640
  }
2641
  (void)memcpy(pNew->passwords, pUser->passwords, pUser->numOfPasswords * sizeof(SUserPassword));
2,511,358✔
2642
  TAOS_CHECK_GOTO(mndDupKVHash(pUser->ownedDbs, &pNew->ownedDbs), NULL, _OVER);
2,511,358✔
2643
  TAOS_CHECK_GOTO(mndDupPrivObjHash(pUser->objPrivs, &pNew->objPrivs), NULL, _OVER);
2,511,358✔
2644
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->selectTbs, &pNew->selectTbs, false), NULL, _OVER);
2,511,358✔
2645
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->insertTbs, &pNew->insertTbs, false), NULL, _OVER);
2,511,358✔
2646
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->updateTbs, &pNew->updateTbs, false), NULL, _OVER);
2,511,358✔
2647
  TAOS_CHECK_GOTO(mndDupPrivTblHash(pUser->deleteTbs, &pNew->deleteTbs, false), NULL, _OVER);
2,511,358✔
2648
  TAOS_CHECK_GOTO(mndDupRoleHash(pUser->roles, &pNew->roles), NULL, _OVER);
2,511,358✔
2649
  if(pUser->legacyPrivs) {
2,511,358✔
2650
    pNew->legacyPrivs = taosMemCalloc(1, sizeof(SPrivHashObjSet));
×
2651
    if (pNew->legacyPrivs == NULL) {
×
2652
      code = TSDB_CODE_OUT_OF_MEMORY;
×
2653
      goto _OVER;
×
2654
    }
2655
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadDbs, &pNew->legacyPrivs->pReadDbs), NULL, _OVER);
×
2656
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteDbs, &pNew->legacyPrivs->pWriteDbs), NULL, _OVER);
×
2657
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadTbs, &pNew->legacyPrivs->pReadTbs), NULL, _OVER);
×
2658
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteTbs, &pNew->legacyPrivs->pWriteTbs), NULL, _OVER);
×
2659
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pTopics, &pNew->legacyPrivs->pTopics), NULL, _OVER);
×
2660
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pAlterTbs, &pNew->legacyPrivs->pAlterTbs), NULL, _OVER);
×
2661
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pReadViews, &pNew->legacyPrivs->pReadViews), NULL, _OVER);
×
2662
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pWriteViews, &pNew->legacyPrivs->pWriteViews), NULL, _OVER);
×
2663
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pAlterViews, &pNew->legacyPrivs->pAlterViews), NULL, _OVER);
×
2664
    TAOS_CHECK_GOTO(mndDupKVHash(pUser->legacyPrivs->pUseDbs, &pNew->legacyPrivs->pUseDbs), NULL, _OVER);
×
2665
  }
2666
  pNew->pIpWhiteListDual = cloneIpWhiteList(pUser->pIpWhiteListDual);
2,511,358✔
2667
  if (pNew->pIpWhiteListDual == NULL) {
2,511,358✔
2668
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2669
    goto _OVER;
×
2670
  }
2671

2672
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
2,511,358✔
2673
  if (pNew->pTimeWhiteList == NULL) {
2,511,358✔
2674
    code = TSDB_CODE_OUT_OF_MEMORY;
×
2675
    goto _OVER;
×
2676
  }
2677

2678
_OVER:
2,511,358✔
2679
  taosRUnLockLatch(&pUser->lock);
2,511,358✔
2680
  if (code == 0) {
2,511,358✔
2681
    dropOldPasswords(pNew);
2,511,358✔
2682
  }
2683
  TAOS_RETURN(code);
2,511,358✔
2684
}
2685

2686
void mndUserFreeObj(SUserObj *pUser) {
6,212,121✔
2687
  taosHashCleanup(pUser->ownedDbs);
6,212,121✔
2688
  taosHashCleanup(pUser->objPrivs);
6,212,121✔
2689
  taosHashCleanup(pUser->selectTbs);
6,212,121✔
2690
  taosHashCleanup(pUser->insertTbs);
6,212,121✔
2691
  taosHashCleanup(pUser->updateTbs);
6,212,121✔
2692
  taosHashCleanup(pUser->deleteTbs);
6,212,121✔
2693
  taosHashCleanup(pUser->roles);
6,212,121✔
2694
  taosMemoryFreeClear(pUser->passwords);
6,212,121✔
2695
  taosMemoryFreeClear(pUser->pIpWhiteListDual);
6,212,121✔
2696
  taosMemoryFreeClear(pUser->pTimeWhiteList);
6,212,121✔
2697
  pUser->ownedDbs = NULL;
6,212,121✔
2698
  pUser->objPrivs = NULL;
6,212,121✔
2699
  pUser->selectTbs = NULL;
6,212,121✔
2700
  pUser->insertTbs = NULL;
6,212,121✔
2701
  pUser->updateTbs = NULL;
6,212,121✔
2702
  pUser->deleteTbs = NULL;
6,212,121✔
2703
  pUser->roles = NULL;
6,212,121✔
2704
  if (pUser->legacyPrivs) {
6,212,121✔
2705
    taosHashCleanup(pUser->legacyPrivs->pReadDbs);
×
2706
    taosHashCleanup(pUser->legacyPrivs->pWriteDbs);
×
2707
    taosHashCleanup(pUser->legacyPrivs->pReadTbs);
×
2708
    taosHashCleanup(pUser->legacyPrivs->pWriteTbs);
×
2709
    taosHashCleanup(pUser->legacyPrivs->pTopics);
×
2710
    taosHashCleanup(pUser->legacyPrivs->pAlterTbs);
×
2711
    taosHashCleanup(pUser->legacyPrivs->pReadViews);
×
2712
    taosHashCleanup(pUser->legacyPrivs->pWriteViews);
×
2713
    taosHashCleanup(pUser->legacyPrivs->pAlterViews);
×
2714
    taosHashCleanup(pUser->legacyPrivs->pUseDbs);
×
2715
    taosMemoryFreeClear(pUser->legacyPrivs);
×
2716
  }
2717
}
6,212,121✔
2718

2719
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
3,114,387✔
2720
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
3,114,387✔
2721
  mndUserFreeObj(pUser);
3,114,387✔
2722
  return 0;
3,114,387✔
2723
}
2724

2725
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
2,566,740✔
2726
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
2,566,740✔
2727
  taosWLockLatch(&pOld->lock);
2,566,740✔
2728
  pOld->updateTime = pNew->updateTime;
2,566,740✔
2729
  pOld->authVersion = pNew->authVersion;
2,566,740✔
2730
  pOld->passVersion = pNew->passVersion;
2,566,740✔
2731
  pOld->sysInfo = pNew->sysInfo;
2,566,740✔
2732
  pOld->enable = pNew->enable;
2,566,740✔
2733
  pOld->flag = pNew->flag;
2,566,740✔
2734
  pOld->changePass = pNew->changePass;
2,566,740✔
2735
  pOld->uid = pNew->uid;
2,566,740✔
2736

2737
  pOld->sessionPerUser = pNew->sessionPerUser;
2,566,740✔
2738
  pOld->connectTime = pNew->connectTime;
2,566,740✔
2739
  pOld->connectIdleTime = pNew->connectIdleTime;
2,566,740✔
2740
  pOld->callPerSession = pNew->callPerSession;
2,566,740✔
2741
  pOld->vnodePerCall = pNew->vnodePerCall;
2,566,740✔
2742
  pOld->failedLoginAttempts = pNew->failedLoginAttempts;
2,566,740✔
2743
  pOld->passwordLifeTime = pNew->passwordLifeTime;
2,566,740✔
2744
  pOld->passwordReuseTime = pNew->passwordReuseTime;
2,566,740✔
2745
  pOld->passwordReuseMax = pNew->passwordReuseMax;
2,566,740✔
2746
  pOld->passwordLockTime = pNew->passwordLockTime;
2,566,740✔
2747
  pOld->passwordGraceTime = pNew->passwordGraceTime;
2,566,740✔
2748
  pOld->inactiveAccountTime = pNew->inactiveAccountTime;
2,566,740✔
2749
  pOld->allowTokenNum = pNew->allowTokenNum;
2,566,740✔
2750
  pOld->tokenNum = pNew->tokenNum;
2,566,740✔
2751

2752
  pOld->numOfPasswords = pNew->numOfPasswords;
2,566,740✔
2753
  TSWAP(pOld->passwords, pNew->passwords);
2,566,740✔
2754
  (void)memcpy(pOld->salt, pNew->salt, sizeof(pOld->salt));
2,566,740✔
2755
  (void)memcpy(pOld->totpsecret, pNew->totpsecret, sizeof(pOld->totpsecret));
2,566,740✔
2756
  pOld->sysPrivs = pNew->sysPrivs;
2,566,740✔
2757
  TSWAP(pOld->ownedDbs, pNew->ownedDbs);
2,566,740✔
2758
  TSWAP(pOld->objPrivs, pNew->objPrivs);
2,566,740✔
2759
  TSWAP(pOld->selectTbs, pNew->selectTbs);
2,566,740✔
2760
  TSWAP(pOld->insertTbs, pNew->insertTbs);
2,566,740✔
2761
  TSWAP(pOld->updateTbs, pNew->updateTbs);
2,566,740✔
2762
  TSWAP(pOld->deleteTbs, pNew->deleteTbs);
2,566,740✔
2763
  TSWAP(pOld->roles, pNew->roles);
2,566,740✔
2764
  TSWAP(pOld->legacyPrivs, pNew->legacyPrivs);
2,566,740✔
2765

2766
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
2,566,740✔
2767
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
2,566,740✔
2768
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
2,566,740✔
2769
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
2,566,740✔
2770
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
2,566,740✔
2771

2772
  taosWUnLockLatch(&pOld->lock);
2,566,740✔
2773

2774
  return 0;
2,566,740✔
2775
}
2776

2777
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
102,063,010✔
2778
  int32_t code = 0;
102,063,010✔
2779
  SSdb   *pSdb = pMnode->pSdb;
102,063,010✔
2780

2781
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
102,065,170✔
2782
  if (*ppUser == NULL) {
102,066,178✔
2783
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
87,383✔
2784
      code = TSDB_CODE_MND_USER_NOT_EXIST;
87,383✔
2785
    } else {
2786
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2787
    }
2788
  }
2789
  TAOS_RETURN(code);
102,066,178✔
2790
}
2791

2792
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
150,539,328✔
2793
  SSdb *pSdb = pMnode->pSdb;
150,539,328✔
2794
  sdbRelease(pSdb, pUser);
150,540,048✔
2795
}
150,542,324✔
2796

2797
int32_t mndAcquireUserById(SMnode *pMnode, int64_t userId, SUserObj **ppUser) {
×
2798
  void     *pIter = NULL;
×
2799
  SUserObj *pObj;
×
2800
  SSdb     *pSdb = pMnode->pSdb;
×
2801
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj))) {
×
2802
    if (pObj->uid == userId) {
×
2803
      return mndAcquireUser(pMnode, pObj->user, ppUser);
×
2804
    }
2805
  }
2806
  return 0;
×
2807
}
2808

2809
int32_t mndBuildUidNamesHash(SMnode *pMnode, SSHashObj **ppHash) {
524,262✔
2810
  int32_t    code = 0;
524,262✔
2811
  void      *pIter = NULL;
524,262✔
2812
  SUserObj  *pObj;
524,262✔
2813
  SSHashObj *pHash = NULL;
524,262✔
2814

2815
  int32_t nUser = sdbGetSize(pMnode->pSdb, SDB_USER);
524,262✔
2816

2817
  pHash = tSimpleHashInit(nUser, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
524,262✔
2818
  if (pHash == NULL) {
524,262✔
2819
    TAOS_RETURN(terrno);
×
2820
  }
2821

2822
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_USER, pIter, (void **)&pObj))) {
1,246,975✔
2823
    code = tSimpleHashPut(pHash, &pObj->uid, sizeof(pObj->uid), pObj->name, strlen(pObj->name) + 1);
722,713✔
2824
    if (code != 0) {
722,713✔
2825
      sdbRelease(pMnode->pSdb, pObj);
×
2826
      sdbCancelFetch(pMnode->pSdb, pIter);
×
2827
      tSimpleHashCleanup(pHash);
×
2828
      TAOS_RETURN(code);
×
2829
    }
2830
    sdbRelease(pMnode->pSdb, pObj);
722,713✔
2831
  }
2832

2833
  *ppHash = pHash;
524,262✔
2834
  TAOS_RETURN(code);
524,262✔
2835
}
2836

2837
int32_t mndEncryptPass(char *pass, const char *salt, int8_t *algo) {
96,688✔
2838
  int32_t code = 0;
96,688✔
2839
  if (tsMetaKey[0] == '\0') {
96,688✔
2840
    return 0;
96,607✔
2841
  }
2842

2843
  if (salt[0] != 0) {
81✔
2844
    char passAndSalt[TSDB_PASSWORD_LEN - 1 + TSDB_PASSWORD_SALT_LEN];
81✔
2845
    (void)memcpy(passAndSalt, pass, TSDB_PASSWORD_LEN - 1);
81✔
2846
    (void)memcpy(passAndSalt + TSDB_PASSWORD_LEN - 1, salt, TSDB_PASSWORD_SALT_LEN);
81✔
2847
    taosEncryptPass_c((uint8_t *)passAndSalt, sizeof(passAndSalt), pass);
2848
  }
2849

2850
  unsigned char packetData[TSDB_PASSWORD_LEN] = {0};
81✔
2851
  SCryptOpts    opts = {0};
81✔
2852
  opts.len = TSDB_PASSWORD_LEN;
81✔
2853
  opts.source = pass;
81✔
2854
  opts.result = packetData;
81✔
2855
  opts.unitLen = TSDB_PASSWORD_LEN;
81✔
2856
  tstrncpy(opts.key, tsDataKey, ENCRYPT_KEY_LEN + 1);
81✔
2857
  int newLen = Builtin_CBC_Encrypt(&opts);
81✔
2858
  if (newLen <= 0) return terrno;
81✔
2859

2860
  memcpy(pass, packetData, newLen);
81✔
2861
  if (algo != NULL) {
81✔
2862
    *algo = DND_CA_SM4;
81✔
2863
  }
2864

2865
  return 0;
81✔
2866
}
2867

2868
static void generateSalt(char *salt, size_t len) {
74,872✔
2869
  const char *set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
74,872✔
2870
  int32_t     setLen = 62;
74,872✔
2871
  for (int32_t i = 0; i < len - 1; ++i) {
2,395,904✔
2872
    salt[i] = set[taosSafeRand() % setLen];
2,321,032✔
2873
  }
2874
  salt[len - 1] = 0;
74,872✔
2875
}
74,872✔
2876

2877
static int32_t addDefaultIpToTable(SHashObj *pUniqueTab) {
1,094✔
2878
  int32_t code = 0;
1,094✔
2879
  int32_t lino = 0;
1,094✔
2880
  int32_t dummy = 0;
1,094✔
2881

2882
  SIpRange ipv4 = {0}, ipv6 = {0};
1,094✔
2883
  code = createDefaultIp4Range(&ipv4);
1,094✔
2884
  TSDB_CHECK_CODE(code, lino, _error);
1,094✔
2885

2886
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummy, sizeof(dummy));
1,094✔
2887
  TSDB_CHECK_CODE(code, lino, _error);
1,094✔
2888

2889
  code = createDefaultIp6Range(&ipv6);
1,094✔
2890
  TSDB_CHECK_CODE(code, lino, _error);
1,094✔
2891

2892
  code = taosHashPut(pUniqueTab, &ipv6, sizeof(ipv6), &dummy, sizeof(dummy));
1,094✔
2893
  TSDB_CHECK_CODE(code, lino, _error);
1,094✔
2894
    
2895
_error:
1,094✔
2896
  if (code != 0) {
1,094✔
2897
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
2898
  }
2899
  return code;
1,094✔
2900
}
2901

2902
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
74,695✔
2903
  int32_t  code = 0;
74,695✔
2904
  int32_t  lino = 0;
74,695✔
2905
  SUserObj userObj = {0};
74,695✔
2906

2907
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
74,695✔
2908
  if (userObj.passwords == NULL) {
74,695✔
2909
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2910
  }
2911
  userObj.numOfPasswords = 1;
74,695✔
2912

2913
  if (pCreate->isImport == 1) {
74,695✔
2914
    memset(userObj.salt, 0, sizeof(userObj.salt));
×
2915
    memcpy(userObj.passwords[0].pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
2916
  } else {
2917
    generateSalt(userObj.salt, sizeof(userObj.salt));
74,695✔
2918
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.passwords[0].pass);
74,695✔
2919
    userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
74,695✔
2920
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino,
74,695✔
2921
                    _OVER);
2922
  }
2923
  userObj.passwords[0].setTime = taosGetTimestampSec();
74,695✔
2924

2925
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
74,695✔
2926
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
74,695✔
2927
  if (pCreate->totpseed[0] != 0) {
74,695✔
2928
    int len = taosGenerateTotpSecret(pCreate->totpseed, 0, userObj.totpsecret, sizeof(userObj.totpsecret));
×
2929
    if (len < 0) {
×
2930
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
2931
    }
2932
  }
2933

2934
  userObj.createdTime = taosGetTimestampMs();
74,695✔
2935
  userObj.updateTime = userObj.createdTime;
74,695✔
2936
  userObj.superUser = 0;  // pCreate->superUser;
74,695✔
2937
  userObj.sysInfo = pCreate->sysInfo;
74,695✔
2938
  userObj.enable = pCreate->enable;
74,695✔
2939
  userObj.createdb = pCreate->createDb;
74,695✔
2940
  userObj.uid = mndGenerateUid(userObj.user, strlen(userObj.user));
74,695✔
2941

2942
  if (userObj.createdb == 1) {
74,695✔
2943
    privAddType(&userObj.sysPrivs, PRIV_DB_CREATE);
2944
  }
2945

2946
#ifdef TD_ENTERPRISE
2947

2948
  userObj.changePass = pCreate->changepass;
74,695✔
2949
  userObj.sessionPerUser = pCreate->sessionPerUser;
74,695✔
2950
  userObj.connectTime = pCreate->connectTime;
74,695✔
2951
  userObj.connectIdleTime = pCreate->connectIdleTime;
74,695✔
2952
  userObj.callPerSession = pCreate->callPerSession;
74,695✔
2953
  userObj.vnodePerCall = pCreate->vnodePerCall;
74,695✔
2954
  userObj.failedLoginAttempts = pCreate->failedLoginAttempts;
74,695✔
2955
  userObj.passwordLifeTime = pCreate->passwordLifeTime;
74,695✔
2956
  userObj.passwordReuseTime = pCreate->passwordReuseTime;
74,695✔
2957
  userObj.passwordReuseMax = pCreate->passwordReuseMax;
74,695✔
2958
  userObj.passwordLockTime = pCreate->passwordLockTime;
74,695✔
2959
  userObj.passwordGraceTime = pCreate->passwordGraceTime;
74,695✔
2960
  userObj.inactiveAccountTime = pCreate->inactiveAccountTime;
74,695✔
2961
  userObj.allowTokenNum = pCreate->allowTokenNum;
74,695✔
2962
  userObj.tokenNum = 0;
74,695✔
2963

2964
  if (pCreate->numIpRanges == 0) {
74,695✔
2965
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
73,172✔
2966
  } else {
2967
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
1,523✔
2968
    if (pUniqueTab == NULL) {
1,523✔
2969
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2970
    }
2971
    
2972
    bool hasPositive = false;
1,523✔
2973
    for (int i = 0; i < pCreate->numIpRanges; i++) {
3,821✔
2974
      SIpRange range = {0};
2,298✔
2975
      copyIpRange(&range, pCreate->pIpDualRanges + i);
2,298✔
2976
      hasPositive = hasPositive || !range.neg;
2,298✔
2977
      int32_t dummy = 0;
2,298✔
2978
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummy, sizeof(dummy))) != 0) {
2,298✔
2979
        taosHashCleanup(pUniqueTab);
×
2980
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2981
      }
2982
    }
2983

2984
    // add local ip if there is any positive range
2985
    if (hasPositive) {
1,523✔
2986
      code = addDefaultIpToTable(pUniqueTab);
1,094✔
2987
      if (code != 0) {
1,094✔
2988
        taosHashCleanup(pUniqueTab);
×
2989
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2990
      }
2991
    }
2992

2993
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,523✔
2994
      taosHashCleanup(pUniqueTab);
×
2995
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2996
    }
2997

2998
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
1,523✔
2999
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
1,523✔
3000
    if (p == NULL) {
1,523✔
3001
      taosHashCleanup(pUniqueTab);
×
3002
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3003
    }
3004

3005
    void *pIter = taosHashIterate(pUniqueTab, NULL);
1,523✔
3006
    for (int32_t i = 0; i < numOfRanges; i++) {
5,660✔
3007
      size_t    len = 0;
4,137✔
3008
      SIpRange *key = taosHashGetKey(pIter, &len);
4,137✔
3009
      memcpy(&p->pIpRanges[i], key, sizeof(SIpRange));
4,137✔
3010
      pIter = taosHashIterate(pUniqueTab, pIter);
4,137✔
3011
    }
3012

3013
    taosHashCleanup(pUniqueTab);
1,523✔
3014
    p->num = numOfRanges;
1,523✔
3015
    sortIpWhiteList(p);
1,523✔
3016
    userObj.pIpWhiteListDual = p;
1,523✔
3017
  }
3018

3019
  if (pCreate->numTimeRanges == 0) {
74,695✔
3020
    userObj.pTimeWhiteList = (SDateTimeWhiteList *)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
72,836✔
3021
    if (userObj.pTimeWhiteList == NULL) {
72,836✔
3022
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3023
    }
3024
  } else {
3025
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
1,859✔
3026
    if (pUniqueTab == NULL) {
1,859✔
3027
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3028
    }
3029

3030
    for (int i = 0; i < pCreate->numTimeRanges; i++) {
4,147✔
3031
      SDateTimeRange        *src = pCreate->pTimeRanges + i;
2,288✔
3032
      SDateTimeWhiteListItem range = {0};
2,288✔
3033
      DateTimeRangeToWhiteListItem(&range, src);
2,288✔
3034

3035
      // no need to add expired range
3036
      if (isDateTimeWhiteListItemExpired(&range)) {
2,288✔
3037
        continue;
286✔
3038
      }
3039

3040
      int32_t dummy = 0;
2,002✔
3041
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummy, sizeof(dummy))) != 0) {
2,002✔
3042
        taosHashCleanup(pUniqueTab);
×
3043
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3044
      }
3045
    }
3046

3047
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_TIME_RANGE) {
1,859✔
3048
      taosHashCleanup(pUniqueTab);
×
3049
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
3050
    }
3051

3052
    int32_t             numOfRanges = taosHashGetSize(pUniqueTab);
1,859✔
3053
    SDateTimeWhiteList *p =
3,718✔
3054
        taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
1,859✔
3055
    if (p == NULL) {
1,859✔
3056
      taosHashCleanup(pUniqueTab);
×
3057
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3058
    }
3059

3060
    void *pIter = taosHashIterate(pUniqueTab, NULL);
1,859✔
3061
    for (int32_t i = 0; i < numOfRanges; i++) {
3,861✔
3062
      size_t                  len = 0;
2,002✔
3063
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
2,002✔
3064
      memcpy(p->ranges + i, key, sizeof(SDateTimeWhiteListItem));
2,002✔
3065
      pIter = taosHashIterate(pUniqueTab, pIter);
2,002✔
3066
    }
3067

3068
    taosHashCleanup(pUniqueTab);
1,859✔
3069
    p->num = numOfRanges;
1,859✔
3070
    sortTimeWhiteList(p);
1,859✔
3071
    userObj.pTimeWhiteList = p;
1,859✔
3072
  }
3073

3074
  userObj.ipWhiteListVer = taosGetTimestampMs();
74,695✔
3075
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
74,695✔
3076
  
3077

3078
#else  // TD_ENTERPRISE
3079

3080
  userObj.changePass = 1;
3081
  userObj.sessionPerUser = -1;
3082
  userObj.connectTime = -1;
3083
  userObj.connectIdleTime = -1;
3084
  userObj.callPerSession = -1;
3085
  userObj.vnodePerCall = -1;
3086
  userObj.failedLoginAttempts = -1;
3087
  userObj.passwordLifeTime = -1;
3088
  userObj.passwordReuseTime = 0;
3089
  userObj.passwordReuseMax = 0;
3090
  userObj.passwordLockTime = -1;
3091
  userObj.passwordGraceTime = -1;
3092
  userObj.inactiveAccountTime = -1;
3093
  userObj.allowTokenNum = -1;
3094
  userObj.tokenNum = 0;
3095

3096
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
3097
  userObj.pTimeWhiteList = (SDateTimeWhiteList *)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
3098
  if (userObj.pTimeWhiteList == NULL) {
3099
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
3100
  }
3101

3102
  userObj.ipWhiteListVer = 0;
3103
  userObj.timeWhiteListVer = 0;
3104

3105
#endif  // TD_ENTERPRISE
3106

3107
  userObj.roles = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
74,695✔
3108
  if (userObj.roles == NULL) {
74,695✔
3109
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3110
  }
3111

3112
  uint8_t flag = 0x01;
74,695✔
3113
  if ((code = taosHashPut(userObj.roles, TSDB_ROLE_DEFAULT, strlen(TSDB_ROLE_DEFAULT) + 1, &flag, sizeof(flag))) != 0) {
74,695✔
3114
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3115
  }
3116

3117
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "create-user");
74,695✔
3118
  if (pTrans == NULL) {
74,695✔
3119
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
3120
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3121
  }
3122
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
74,695✔
3123

3124
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
74,695✔
3125
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
74,695✔
3126
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
3127
    mndTransDrop(pTrans);
×
3128
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3129
  }
3130
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
74,695✔
3131

3132
  if (mndTransPrepare(pMnode, pTrans) != 0) {
74,695✔
3133
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3134
    mndTransDrop(pTrans);
×
3135
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3136
  }
3137

3138
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
74,695✔
3139
    mndTransDrop(pTrans);
×
3140
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3141
  }
3142

3143
  if (taosHashGet(userObj.roles, TSDB_ROLE_SYSAUDIT_LOG, strlen(TSDB_ROLE_SYSAUDIT_LOG) + 1)) {
74,695✔
3144
    (void)mndResetAuditLogUser(pMnode, userObj.user, true);
×
3145
  }
3146

3147
  mndTransDrop(pTrans);
74,695✔
3148

3149
_OVER:
74,695✔
3150
  mndUserFreeObj(&userObj);
74,695✔
3151
  TAOS_RETURN(code);
74,695✔
3152
}
3153

3154
static int32_t mndCheckPasswordFmt(const char *pwd) {
97,032✔
3155
  if (tsEnableStrongPassword == 0) {
97,032✔
3156
    for (char c = *pwd; c != 0; c = *(++pwd)) {
48,224✔
3157
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
47,696✔
3158
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
3159
      }
3160
    }
3161
    return 0;
528✔
3162
  }
3163

3164
  int32_t len = strlen(pwd);
96,504✔
3165
  if (len < TSDB_PASSWORD_MIN_LEN) {
96,504✔
3166
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
3167
  }
3168

3169
  if (len > TSDB_PASSWORD_MAX_LEN) {
96,504✔
3170
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
3171
  }
3172

3173
  if (taosIsComplexString(pwd)) {
96,504✔
3174
    return 0;
96,504✔
3175
  }
3176

3177
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
3178
}
3179

3180
static int32_t mndCheckTotpSeedFmt(const char *seed) {
×
3181
  int32_t len = strlen(seed);
×
3182
  if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
3183
    return TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT;
×
3184
  }
3185

3186
  if (taosIsComplexString(seed)) {
×
3187
    return 0;
×
3188
  }
3189

3190
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
3191
}
3192

3193
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq) {
×
3194
  SMnode                *pMnode = pReq->info.node;
×
3195
  int32_t                code = 0;
×
3196
  int32_t                lino = 0;
×
3197
  int32_t                contLen = 0;
×
3198
  void                  *pRsp = NULL;
×
3199
  SUserObj              *pUser = NULL;
×
3200
  SGetUserWhiteListReq   wlReq = {0};
×
3201
  SUserDateTimeWhiteList wlRsp = {0};
×
3202

3203
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
×
3204
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3205
  }
3206
  mTrace("user: %s, start to get date time whitelist", wlReq.user);
×
3207

3208
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
×
3209
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
3210

3211
  contLen = tSerializeSUserDateTimeWhiteList(NULL, 0, &wlRsp);
×
3212
  if (contLen < 0) {
×
3213
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3214
  }
3215
  pRsp = rpcMallocCont(contLen);
×
3216
  if (pRsp == NULL) {
×
3217
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3218
  }
3219

3220
  contLen = tSerializeSUserDateTimeWhiteList(pRsp, contLen, &wlRsp);
×
3221
  if (contLen < 0) {
×
3222
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3223
  }
3224

3225
_OVER:
×
3226
  mndReleaseUser(pMnode, pUser);
×
3227
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
3228
  if (code < 0) {
×
3229
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
3230
    rpcFreeCont(pRsp);
×
3231
    pRsp = NULL;
×
3232
    contLen = 0;
×
3233
  }
3234
  pReq->code = code;
×
3235
  pReq->info.rsp = pRsp;
×
3236
  pReq->info.rspLen = contLen;
×
3237

3238
  TAOS_RETURN(code);
×
3239
  return 0;
3240
}
3241

3242
static int32_t buildRetrieveDateTimeWhiteListRsp(SRetrieveDateTimeWhiteListRsp *pRsp) {
559✔
3243
  (void)taosThreadRwlockRdlock(&userCache.rw);
559✔
3244

3245
  int32_t count = taosHashGetSize(userCache.users);
559✔
3246
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
559✔
3247
  if (pRsp->pUsers == NULL) {
559✔
3248
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
3249
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3250
  }
3251

3252
  count = 0;
559✔
3253
  void *pIter = taosHashIterate(userCache.users, NULL);
559✔
3254
  while (pIter) {
1,118✔
3255
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
559✔
3256
    if (wl == NULL || wl->num <= 0) {
559✔
3257
      pIter = taosHashIterate(userCache.users, pIter);
559✔
3258
      continue;
559✔
3259
    }
3260

3261
    SUserDateTimeWhiteList *pUser = &pRsp->pUsers[count];
×
3262
    pUser->ver = userCache.verTime;
×
3263

3264
    size_t klen;
×
3265
    char  *key = taosHashGetKey(pIter, &klen);
×
3266
    (void)memcpy(pUser->user, key, klen);
×
3267

3268
    pUser->numWhiteLists = wl->num;
×
3269
    pUser->pWhiteLists = taosMemoryCalloc(wl->num, sizeof(SDateTimeWhiteListItem));
×
3270
    if (pUser->pWhiteLists == NULL) {
×
3271
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
3272
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3273
    }
3274

3275
    (void)memcpy(pUser->pWhiteLists, wl->ranges, wl->num * sizeof(SDateTimeWhiteListItem));
×
3276
    count++;
×
3277
    pIter = taosHashIterate(userCache.users, pIter);
×
3278
  }
3279

3280
  pRsp->numOfUser = count;
559✔
3281
  pRsp->ver = userCache.verTime;
559✔
3282
  (void)taosThreadRwlockUnlock(&userCache.rw);
559✔
3283
  TAOS_RETURN(0);
559✔
3284
}
3285

3286
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq) {
559✔
3287
  int32_t                       code = 0;
559✔
3288
  int32_t                       lino = 0;
559✔
3289
  int32_t                       len = 0;
559✔
3290
  void                         *pRsp = NULL;
559✔
3291
  SRetrieveDateTimeWhiteListRsp wlRsp = {0};
559✔
3292

3293
  // impl later
3294
  SRetrieveWhiteListReq req = {0};
559✔
3295
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
559✔
3296
    code = TSDB_CODE_INVALID_MSG;
×
3297
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3298
  }
3299

3300
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
559✔
3301

3302
  len = tSerializeSRetrieveDateTimeWhiteListRsp(NULL, 0, &wlRsp);
559✔
3303
  if (len < 0) {
559✔
3304
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3305
  }
3306

3307
  pRsp = rpcMallocCont(len);
559✔
3308
  if (!pRsp) {
559✔
3309
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3310
  }
3311
  len = tSerializeSRetrieveDateTimeWhiteListRsp(pRsp, len, &wlRsp);
559✔
3312
  if (len < 0) {
559✔
3313
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3314
  }
3315

3316
_OVER:
559✔
3317
  if (code < 0) {
559✔
3318
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
3319
    rpcFreeCont(pRsp);
×
3320
    pRsp = NULL;
×
3321
    len = 0;
×
3322
  }
3323
  pReq->code = code;
559✔
3324
  pReq->info.rsp = pRsp;
559✔
3325
  pReq->info.rspLen = len;
559✔
3326

3327
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
559✔
3328
  TAOS_RETURN(code);
559✔
3329
}
3330

3331
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
75,039✔
3332
  SMnode        *pMnode = pReq->info.node;
75,039✔
3333
  int32_t        code = 0;
75,039✔
3334
  int32_t        lino = 0;
75,039✔
3335
  SRoleObj      *pRole = NULL;
75,039✔
3336
  SUserObj      *pUser = NULL;
75,039✔
3337
  SUserObj      *pOperUser = NULL;
75,039✔
3338
  SCreateUserReq createReq = {0};
75,039✔
3339
  int64_t        tss = taosGetTimestampMs();
75,039✔
3340

3341
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
75,039✔
3342
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3343
  }
3344

3345
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
75,039✔
3346

3347
#ifndef TD_ENTERPRISE
3348
  if (createReq.isImport == 1) {
3349
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
3350
  }
3351
#endif
3352
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
75,039✔
3353
  if (pOperUser == NULL) {
75,039✔
3354
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3355
  }
3356

3357
  if (createReq.isImport != 1) {
75,039✔
3358
    // TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_CREATE_USER), &lino, _OVER);
3359
    TAOS_CHECK_GOTO(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_CREATE, 0, 0, NULL, NULL),
75,039✔
3360
                    &lino, _OVER);
3361
  } else if (strcmp(RPC_MSG_USER(pReq), "root") != 0) {
×
3362
    mError("The operation is not permitted to create user:%s", RPC_MSG_USER(pReq));
×
3363
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
3364
  }
3365

3366
  if (createReq.user[0] == 0) {
75,039✔
3367
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3368
  }
3369

3370
  if (createReq.isImport != 1) {
75,039✔
3371
    code = mndCheckPasswordFmt(createReq.pass);
75,039✔
3372
    TAOS_CHECK_GOTO(code, &lino, _OVER);
75,039✔
3373
  }
3374

3375
  if (createReq.totpseed[0] != 0) {
75,039✔
3376
    code = mndCheckTotpSeedFmt(createReq.totpseed);
×
3377
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3378
  }
3379

3380
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
75,039✔
3381
  if (pUser != NULL) {
75,039✔
3382
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
344✔
3383
  }
3384

3385
  code = mndAcquireRole(pMnode, createReq.user, &pRole);
74,695✔
3386
  if (pRole != NULL) {
74,695✔
3387
    TAOS_CHECK_GOTO(TSDB_CODE_MND_ROLE_ALREADY_EXIST, &lino, _OVER);
×
3388
  }
3389

3390
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
74,695✔
3391

3392
  if (sdbGetSize(pMnode->pSdb, SDB_USER) >= TSDB_MAX_USERS) {
74,695✔
3393
    mError("user:%s, failed to create since reach max user limit %d", createReq.user, TSDB_MAX_USERS);
×
3394
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USERS, &lino, _OVER);
×
3395
  }
3396

3397
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
74,695✔
3398
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
74,695✔
3399

3400
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
74,695✔
3401
    char detail[1000] = {0};
74,695✔
3402
    (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
74,695✔
3403
                    createReq.superUser, createReq.sysInfo);
74,695✔
3404
    char operation[15] = {0};
74,695✔
3405
    if (createReq.isImport == 1) {
74,695✔
3406
      tstrncpy(operation, "importUser", sizeof(operation));
×
3407
    } else {
3408
      tstrncpy(operation, "createUser", sizeof(operation));
74,695✔
3409
    }
3410

3411
    int64_t tse = taosGetTimestampMs();
74,695✔
3412
    double  duration = (double)(tse - tss);
74,695✔
3413
    duration = duration / 1000;
74,695✔
3414
    auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail), duration, 0);
74,695✔
3415
  }
3416

3417
_OVER:
75,039✔
3418
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExists) {
75,039✔
3419
    code = 0;
×
3420
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
75,039✔
3421
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
344✔
3422
  }
3423

3424
  mndReleaseRole(pMnode, pRole);
75,039✔
3425
  mndReleaseUser(pMnode, pUser);
75,039✔
3426
  mndReleaseUser(pMnode, pOperUser);
75,039✔
3427
  tFreeSCreateUserReq(&createReq);
75,039✔
3428

3429
  TAOS_RETURN(code);
75,039✔
3430
}
3431

3432
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq) {
14,077✔
3433
  SMnode                *pMnode = pReq->info.node;
14,077✔
3434
  int32_t                code = 0;
14,077✔
3435
  int32_t                lino = 0;
14,077✔
3436
  int32_t                contLen = 0;
14,077✔
3437
  void                  *pRsp = NULL;
14,077✔
3438
  SUserObj              *pUser = NULL;
14,077✔
3439
  SGetUserWhiteListReq   wlReq = {0};
14,077✔
3440
  SGetUserIpWhiteListRsp wlRsp = {0};
14,077✔
3441

3442
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
14,077✔
3443
  int32_t (*setRspFn)(SMnode *pMnode, SUserObj *pUser, SGetUserIpWhiteListRsp *pRsp) = NULL;
14,077✔
3444

3445
  if (pReq->msgType == TDMT_MND_GET_USER_IP_WHITELIST_DUAL) {
14,077✔
3446
    serialFn = tSerializeSGetUserIpWhiteListDualRsp;
14,077✔
3447
    setRspFn = mndSetUserIpWhiteListDualRsp;
14,077✔
3448
  } else {
3449
    serialFn = tSerializeSGetUserIpWhiteListRsp;
×
3450
    setRspFn = mndSetUserIpWhiteListRsp;
×
3451
  }
3452
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
14,077✔
3453
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
3454
  }
3455
  mTrace("user: %s, start to get ip whitelist", wlReq.user);
14,077✔
3456

3457
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
14,077✔
3458
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
14,077✔
3459

3460
  contLen = serialFn(NULL, 0, &wlRsp);
14,077✔
3461
  if (contLen < 0) {
14,077✔
3462
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3463
  }
3464
  pRsp = rpcMallocCont(contLen);
14,077✔
3465
  if (pRsp == NULL) {
14,077✔
3466
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3467
  }
3468

3469
  contLen = serialFn(pRsp, contLen, &wlRsp);
14,077✔
3470
  if (contLen < 0) {
14,077✔
3471
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3472
  }
3473

3474
_OVER:
14,077✔
3475
  mndReleaseUser(pMnode, pUser);
14,077✔
3476
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
14,077✔
3477
  if (code < 0) {
14,077✔
3478
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
3479
    rpcFreeCont(pRsp);
×
3480
    pRsp = NULL;
×
3481
    contLen = 0;
×
3482
  }
3483
  pReq->code = code;
14,077✔
3484
  pReq->info.rsp = pRsp;
14,077✔
3485
  pReq->info.rspLen = contLen;
14,077✔
3486

3487
  TAOS_RETURN(code);
14,077✔
3488
}
3489

3490
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
559✔
3491
  (void)taosThreadRwlockRdlock(&userCache.rw);
559✔
3492

3493
  int32_t count = taosHashGetSize(userCache.users);
559✔
3494
  pUpdate->pUserIpWhite = taosMemoryCalloc(count, sizeof(SUpdateUserIpWhite));
559✔
3495
  if (pUpdate->pUserIpWhite == NULL) {
559✔
3496
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
3497
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3498
  }
3499

3500
  count = 0;
559✔
3501
  void *pIter = taosHashIterate(userCache.users, NULL);
559✔
3502
  while (pIter) {
1,118✔
3503
    SIpWhiteListDual *wl = (*(SCachedUserInfo **)pIter)->wlIp;
559✔
3504
    if (wl == NULL || wl->num <= 0) {
559✔
3505
      pIter = taosHashIterate(userCache.users, pIter);
×
3506
      continue;
×
3507
    }
3508

3509
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
559✔
3510
    pUser->ver = userCache.verIp;
559✔
3511

3512
    size_t klen;
559✔
3513
    char  *key = taosHashGetKey(pIter, &klen);
559✔
3514
    (void)memcpy(pUser->user, key, klen);
559✔
3515

3516
    pUser->numOfRange = wl->num;
559✔
3517
    pUser->pIpRanges = taosMemoryCalloc(wl->num, sizeof(SIpRange));
559✔
3518
    if (pUser->pIpRanges == NULL) {
559✔
3519
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
3520
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
3521
    }
3522

3523
    (void)memcpy(pUser->pIpRanges, wl->pIpRanges, wl->num * sizeof(SIpRange));
559✔
3524
    count++;
559✔
3525
    pIter = taosHashIterate(userCache.users, pIter);
559✔
3526
  }
3527

3528
  pUpdate->numOfUser = count;
559✔
3529
  pUpdate->ver = userCache.verIp;
559✔
3530
  (void)taosThreadRwlockUnlock(&userCache.rw);
559✔
3531
  TAOS_RETURN(0);
559✔
3532
}
3533

3534
int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq) {
559✔
3535
  int32_t        code = 0;
559✔
3536
  int32_t        lino = 0;
559✔
3537
  int32_t        len = 0;
559✔
3538
  void          *pRsp = NULL;
559✔
3539
  SUpdateIpWhite ipWhite = {0};
559✔
3540

3541
  // impl later
3542
  SRetrieveWhiteListReq req = {0};
559✔
3543
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
559✔
3544
    code = TSDB_CODE_INVALID_MSG;
×
3545
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3546
  }
3547

3548
  int32_t (*fn)(void *, int32_t, SUpdateIpWhite *) = NULL;
559✔
3549
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST) {
559✔
3550
    fn = tSerializeSUpdateIpWhite;
×
3551
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL) {
559✔
3552
    fn = tSerializeSUpdateIpWhiteDual;
559✔
3553
  }
3554

3555
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
559✔
3556

3557
  len = fn(NULL, 0, &ipWhite);
559✔
3558
  if (len < 0) {
559✔
3559
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3560
  }
3561

3562
  pRsp = rpcMallocCont(len);
559✔
3563
  if (!pRsp) {
559✔
3564
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3565
  }
3566
  len = fn(pRsp, len, &ipWhite);
559✔
3567
  if (len < 0) {
559✔
3568
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
3569
  }
3570

3571
_OVER:
559✔
3572
  if (code < 0) {
559✔
3573
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
3574
    rpcFreeCont(pRsp);
×
3575
    pRsp = NULL;
×
3576
    len = 0;
×
3577
  }
3578
  pReq->code = code;
559✔
3579
  pReq->info.rsp = pRsp;
559✔
3580
  pReq->info.rspLen = len;
559✔
3581

3582
  tFreeSUpdateIpWhiteReq(&ipWhite);
559✔
3583
  TAOS_RETURN(code);
559✔
3584
}
3585

3586
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pNew, SRpcMsg *pReq) {
1,298,131✔
3587
  int32_t code = 0, lino = 0;
1,298,131✔
3588
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "alter-user");
1,298,131✔
3589
  if (pTrans == NULL) {
1,298,131✔
3590
    mError("user:%s, failed to alter since %s", pNew->user, terrstr());
×
3591
    TAOS_RETURN(terrno);
×
3592
  }
3593
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pNew->user);
1,298,131✔
3594

3595
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
1,298,131✔
3596
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
1,298,131✔
3597
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
3598
    mndTransDrop(pTrans);
×
3599
    TAOS_RETURN(terrno);
×
3600
  }
3601
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,298,131✔
3602

3603
  if (mndTransPrepare(pMnode, pTrans) != 0) {
1,298,131✔
3604
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3605
    mndTransDrop(pTrans);
×
3606
    TAOS_RETURN(terrno);
×
3607
  }
3608
  if ((code = userCacheUpdateWhiteList(pMnode, pNew)) != 0) {
1,298,131✔
3609
    mndTransDrop(pTrans);
×
3610
    TAOS_RETURN(code);
×
3611
  }
3612
_exit:
1,298,131✔
3613
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,298,131✔
3614
    mError("user:%s, failed to alter at line %d since %s", pNew->user, lino, tstrerror(code));
×
3615
  }
3616
  mndTransDrop(pTrans);
1,298,131✔
3617
  TAOS_RETURN(code);
1,298,131✔
3618
}
3619

3620
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
×
3621
  int32_t code = 0;
×
3622

3623
  *ppNew =
×
3624
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
×
3625
  if (*ppNew == NULL) {
×
3626
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3627
    TAOS_RETURN(code);
×
3628
  }
3629

3630
  char *db = taosHashIterate(pOld, NULL);
×
3631
  while (db != NULL) {
×
3632
    int32_t len = strlen(db) + 1;
×
3633
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
×
3634
      taosHashCancelIterate(pOld, db);
×
3635
      taosHashCleanup(*ppNew);
×
3636
      TAOS_RETURN(code);
×
3637
    }
3638
    db = taosHashIterate(pOld, db);
×
3639
  }
3640

3641
  TAOS_RETURN(code);
×
3642
}
3643

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

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

3648
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
3649
                                  SSdb *pSdb) {
3650
  void *pIter = NULL;
×
3651
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
3652

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

3656
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
×
3657
    char *value = taosHashGet(hash, tbFName, len);
×
3658
    if (value != NULL) {
×
3659
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEGE_EXIST);
×
3660
    }
3661

3662
    int32_t condLen = alterReq->tagCondLen;
×
3663
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
×
3664
  } else {
3665
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
×
3666
  }
3667

3668
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
3669
  int32_t  ref = 1;
×
3670
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
3671
  if (NULL != currRef) {
×
3672
    ref = (*currRef) + 1;
×
3673
  }
3674
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
3675

3676
  TAOS_RETURN(0);
×
3677
}
3678

3679
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
3680
                                        SSdb *pSdb) {
3681
  void *pIter = NULL;
×
3682
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
3683
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
×
3684
  int32_t len = strlen(tbFName) + 1;
×
3685

3686
  if (taosHashRemove(hash, tbFName, len) != 0) {
×
3687
    TAOS_RETURN(0);  // not found
×
3688
  }
3689

3690
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
3691
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
3692
  if (NULL == currRef) {
×
3693
    return 0;
×
3694
  }
3695

3696
  if (1 == *currRef) {
×
3697
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
×
3698
      TAOS_RETURN(0);  // not found
×
3699
    }
3700
    return 0;
×
3701
  }
3702
  int32_t ref = (*currRef) - 1;
×
3703
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
3704

3705
  return 0;
×
3706
}
3707

3708
#if 0
3709
static int32_t mndProcessAlterUserPrivilegesReq(SRpcMsg* pReq, SAlterUserReq *pAlterReq) {
3710
  SMnode   *pMnode = pReq->info.node;
3711
  SSdb     *pSdb = pMnode->pSdb;
3712
  int32_t   code = 0, lino = 0;
3713
  SUserObj *pUser = NULL;
3714
  SUserObj  newUser = {0};
3715
  int64_t   tss = taosGetTimestampMs();
3716

3717
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
3718
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino, _OVER);
3719
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3720

3721
#if 0
3722
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3723
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3724
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3725
      int32_t len = strlen(pAlterReq->objname) + 1;
3726
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3727
      if (pDb == NULL) {
3728
        mndReleaseDb(pMnode, pDb);
3729
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3730
      }
3731
      if ((code = taosHashPut(newUser.readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
3732
          0) {
3733
        mndReleaseDb(pMnode, pDb);
3734
        TAOS_CHECK_GOTO(code, &lino, _OVER);
3735
      }
3736
      mndReleaseDb(pMnode, pDb);
3737
    } else {
3738
      void   *pIter = NULL;
3739
      while (1) {
3740
        SDbObj *pDb = NULL;
3741
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
3742
        if (pIter == NULL) break;
3743
        int32_t len = strlen(pDb->name) + 1;
3744
        if ((code = taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
3745
          sdbRelease(pSdb, pDb);
3746
          sdbCancelFetch(pSdb, pIter);
3747
          TAOS_CHECK_GOTO(code, &lino, _OVER);
3748
        }
3749
        sdbRelease(pSdb, pDb);
3750
      }
3751
    }
3752
  }
3753

3754
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3755
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3756
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3757
      int32_t len = strlen(pAlterReq->objname) + 1;
3758
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3759
      if (pDb == NULL) {
3760
        mndReleaseDb(pMnode, pDb);
3761
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3762
      }
3763
      if ((code = taosHashPut(newUser.writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
3764
          0) {
3765
        mndReleaseDb(pMnode, pDb);
3766
        TAOS_CHECK_GOTO(code, &lino, _OVER);
3767
      }
3768
      mndReleaseDb(pMnode, pDb);
3769
    } else {
3770
      void   *pIter = NULL;
3771
      while (1) {
3772
        SDbObj *pDb = NULL;
3773
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
3774
        if (pIter == NULL) break;
3775
        int32_t len = strlen(pDb->name) + 1;
3776
        if ((code = taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
3777
          sdbRelease(pSdb, pDb);
3778
          sdbCancelFetch(pSdb, pIter);
3779
          TAOS_CHECK_GOTO(code, &lino, _OVER);
3780
        }
3781
        sdbRelease(pSdb, pDb);
3782
      }
3783
    }
3784
  }
3785

3786
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3787
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3788
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3789
      int32_t len = strlen(pAlterReq->objname) + 1;
3790
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3791
      if (pDb == NULL) {
3792
        mndReleaseDb(pMnode, pDb);
3793
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3794
      }
3795
      code = taosHashRemove(newUser.readDbs, pAlterReq->objname, len);
3796
      if (code < 0) {
3797
        mError("read db:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
3798
      }
3799
      mndReleaseDb(pMnode, pDb);
3800
    } else {
3801
      taosHashClear(newUser.readDbs);
3802
    }
3803
  }
3804

3805
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName) ||
3806
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, &pAlterReq->privileges, pAlterReq->tabName)) {
3807
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
3808
      int32_t len = strlen(pAlterReq->objname) + 1;
3809
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
3810
      if (pDb == NULL) {
3811
        mndReleaseDb(pMnode, pDb);
3812
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
3813
      }
3814
      code = taosHashRemove(newUser.writeDbs, pAlterReq->objname, len);
3815
      if (code < 0) {
3816
        mError("user:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
3817
      }
3818
      mndReleaseDb(pMnode, pDb);
3819
    } else {
3820
      taosHashClear(newUser.writeDbs);
3821
    }
3822
  }
3823

3824
  SHashObj *pReadTbs = newUser.readTbs;
3825
  SHashObj *pWriteTbs = newUser.writeTbs;
3826
  SHashObj *pAlterTbs = newUser.alterTbs;
3827

3828
#ifdef TD_ENTERPRISE
3829
  if (pAlterReq->isView) {
3830
    pReadTbs = newUser.readViews;
3831
    pWriteTbs = newUser.writeViews;
3832
    pAlterTbs = newUser.alterViews;
3833
  }
3834
#endif
3835

3836
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3837
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3838
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3839
  }
3840

3841
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3842
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3843
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3844
  }
3845

3846
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3847
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3848
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3849
  }
3850

3851
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3852
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3853
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3854
  }
3855

3856
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3857
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3858
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3859
  }
3860

3861
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3862
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3863
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
3864
  }
3865
#endif
3866

3867
#if 0
3868
// #ifdef USE_TOPIC
3869
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, &pAlterReq->privileges)) {
3870
    int32_t      len = strlen(pAlterReq->objname) + 1;
3871
    SMqTopicObj *pTopic = NULL;
3872
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
3873
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3874
    }
3875
    taosRLockLatch(&pTopic->lock);
3876
    code = taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
3877
    taosRUnLockLatch(&pTopic->lock);
3878
    mndReleaseTopic(pMnode, pTopic);
3879
    TAOS_CHECK_GOTO(code, &lino, _OVER);
3880
  }
3881

3882
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, &pAlterReq->privileges)) {
3883
    int32_t      len = strlen(pAlterReq->objname) + 1;
3884
    SMqTopicObj *pTopic = NULL;
3885
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
3886
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3887
    }
3888
    taosRLockLatch(&pTopic->lock);
3889
    code = taosHashRemove(newUser.topics, pAlterReq->objname, len);
3890
    if (code < 0) {
3891
      mError("user:%s, failed to remove topic:%s since %s", newUser.user, pAlterReq->objname, tstrerror(code));
3892
    }
3893
    taosRUnLockLatch(&pTopic->lock);
3894
    mndReleaseTopic(pMnode, pTopic);
3895
  }
3896
#endif
3897

3898
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
3899
  code = TSDB_CODE_ACTION_IN_PROGRESS;
3900

3901
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
3902
    int64_t tse = taosGetTimestampMs();
3903
    double  duration = (double)(tse - tss);
3904
    duration = duration / 1000;
3905
    if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3906
              ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3907
              ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3908
              ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3909
              ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
3910
              ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
3911
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
3912
        SName name = {0};
3913
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3914
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3915
      } else {
3916
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3917
      }
3918
    } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
3919
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3920
    } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
3921
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3922
    } else {
3923
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
3924
        SName name = {0};
3925
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3926
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3927
      } else {
3928
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
3929
      }
3930
    }
3931
  }
3932
  
3933
_OVER:
3934
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
3935
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
3936
  }
3937
  mndReleaseUser(pMnode, pUser);
3938
  mndUserFreeObj(&newUser);
3939
  TAOS_RETURN(code);
3940
}
3941
#endif
3942

3943
int32_t mndAlterUserFromRole(SRpcMsg *pReq, SUserObj *pOperUser, SAlterRoleReq *pAlterReq) {
1,264,866✔
3944
  SMnode   *pMnode = pReq->info.node;
1,264,866✔
3945
  SSdb     *pSdb = pMnode->pSdb;
1,264,866✔
3946
  void     *pIter = NULL;
1,264,866✔
3947
  int32_t   code = 0, lino = 0;
1,264,866✔
3948
  SUserObj *pUser = NULL;
1,264,866✔
3949
  SUserObj  newUser = {0};
1,264,866✔
3950

3951
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, pAlterReq->principal, &pUser));
1,264,866✔
3952

3953
  if (pUser->enable == 0) {
1,263,810✔
3954
    TAOS_CHECK_EXIT(TSDB_CODE_MND_USER_DISABLED);
×
3955
  }
3956

3957
  if (pAlterReq->alterType == TSDB_ALTER_ROLE_PRIVILEGES) {
1,263,810✔
3958
#ifdef TD_ENTERPRISE
3959
    TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
1,259,734✔
3960
    if ((code = mndAlterUserPrivInfo(pMnode, &newUser, pAlterReq)) == TSDB_CODE_QRY_DUPLICATED_OPERATION) {
1,259,734✔
3961
      code = 0;
×
3962
      goto _exit;
×
3963
    } else {
3964
      TAOS_CHECK_EXIT(code);
1,259,734✔
3965
    }
3966
  } else if (pAlterReq->alterType == TSDB_ALTER_ROLE_ROLE) {
4,076✔
3967
    if ((code = mndAlterUserRoleInfo(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), pUser, &newUser, pAlterReq)) ==
4,076✔
3968
        TSDB_CODE_QRY_DUPLICATED_OPERATION) {
3969
      code = 0;
173✔
3970
      goto _exit;
173✔
3971
    } else {
3972
      TAOS_CHECK_EXIT(code);
3,903✔
3973
    }
3974
#endif
3975
  } else {
3976
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
3977
  }
3978
  code = mndAlterUser(pMnode, &newUser, pReq);
1,263,118✔
3979
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
1,263,118✔
3980

3981
_exit:
1,264,866✔
3982
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,264,866✔
3983
    mError("user:%s, failed to alter user at line %d since %s", pAlterReq->principal, lino, tstrerror(code));
1,575✔
3984
  }
3985
  mndReleaseUser(pMnode, pUser);
1,264,866✔
3986
  mndUserFreeObj(&newUser);
1,264,866✔
3987
  TAOS_RETURN(code);
1,264,866✔
3988
}
3989

3990
static int32_t mndProcessAlterUserBasicInfoReq(SRpcMsg *pReq, SAlterUserReq *pAlterReq) {
37,395✔
3991
  SMnode   *pMnode = pReq->info.node;
37,395✔
3992
  int32_t   code = 0, lino = 0;
37,395✔
3993
  SUserObj *pUser = NULL;
37,395✔
3994
  SUserObj  newUser = {0};
37,395✔
3995
  char      auditLog[1000] = {0};
37,395✔
3996
  int32_t   auditLen = 0;
37,395✔
3997
  int64_t   tss = taosGetTimestampMs();
37,395✔
3998

3999
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
37,395✔
4000
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino,
36,481✔
4001
                  _OVER);
4002
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
35,104✔
4003

4004
  if (pAlterReq->hasPassword) {
35,104✔
4005
    auditLen += tsnprintf(auditLog, sizeof(auditLog), "password,");
21,993✔
4006

4007
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(pAlterReq->pass), &lino, _OVER);
21,993✔
4008
    if (newUser.salt[0] == 0) {
21,993✔
4009
      generateSalt(newUser.salt, sizeof(newUser.salt));
177✔
4010
    }
4011
    char pass[TSDB_PASSWORD_LEN] = {0};
21,993✔
4012
    taosEncryptPass_c((uint8_t *)pAlterReq->pass, strlen(pAlterReq->pass), pass);
21,993✔
4013
    pass[sizeof(pass) - 1] = 0;
21,993✔
4014
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
21,993✔
4015

4016
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
21,993✔
4017
      for (int32_t i = 0; i < newUser.numOfPasswords; ++i) {
779,319✔
4018
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
760,563✔
4019
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
429✔
4020
        }
4021
      }
4022
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
18,756✔
4023
      if (passwords == NULL) {
18,756✔
4024
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
4025
      }
4026
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
18,756✔
4027
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
18,756✔
4028
      passwords[0].setTime = taosGetTimestampSec();
18,756✔
4029
      taosMemoryFree(newUser.passwords);
18,756✔
4030
      newUser.passwords = passwords;
18,756✔
4031
      ++newUser.numOfPasswords;
18,756✔
4032
      ++newUser.passVersion;
18,756✔
4033
      newUser.changePass = 2;
18,756✔
4034
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
2,808✔
4035
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
2,594✔
4036
      newUser.passwords[0].setTime = taosGetTimestampSec();
2,594✔
4037
      ++newUser.passVersion;
2,594✔
4038
      newUser.changePass = 2;
2,594✔
4039
    }
4040
  }
4041

4042
  if (pAlterReq->hasTotpseed) {
34,675✔
4043
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "totpseed,");
×
4044

4045
    if (pAlterReq->totpseed[0] == 0) {  // clear totp secret
×
4046
      memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
×
4047
    } else if (taosGenerateTotpSecret(pAlterReq->totpseed, 0, newUser.totpsecret, sizeof(newUser.totpsecret)) < 0) {
×
4048
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
4049
    }
4050
  }
4051

4052
  if (pAlterReq->hasEnable) {
34,675✔
4053
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "enable:%d,", pAlterReq->enable);
1,376✔
4054

4055
    newUser.enable = pAlterReq->enable;  // lock or unlock user manually
1,376✔
4056
    if (newUser.enable) {
1,376✔
4057
      // reset login info to allow login immediately
4058
      userCacheResetLoginInfo(newUser.user);
855✔
4059
    }
4060
  }
4061

4062
  if (pAlterReq->hasSysinfo) {
34,675✔
4063
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sysinfo:%d,", pAlterReq->sysinfo);
4,218✔
4064
    newUser.sysInfo = pAlterReq->sysinfo;
4,218✔
4065
  }
4066

4067
  if (pAlterReq->hasCreatedb) {
34,675✔
4068
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "createdb:%d,", pAlterReq->createdb);
5,312✔
4069
    newUser.createdb = pAlterReq->createdb;
5,312✔
4070
    if (newUser.createdb == 1) {
5,312✔
4071
      privAddType(&newUser.sysPrivs, PRIV_DB_CREATE);
4072
    } else {
4073
      privRemoveType(&newUser.sysPrivs, PRIV_DB_CREATE);
4074
    }
4075
  }
4076

4077
#ifdef TD_ENTERPRISE
4078
  if (pAlterReq->hasChangepass) {
34,675✔
4079
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "changepass:%d,", pAlterReq->changepass);
×
4080
    newUser.changePass = pAlterReq->changepass;
×
4081
  }
4082

4083
  if (pAlterReq->hasSessionPerUser) {
34,675✔
4084
    auditLen +=
286✔
4085
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sessionPerUser:%d,", pAlterReq->sessionPerUser);
286✔
4086
    newUser.sessionPerUser = pAlterReq->sessionPerUser;
286✔
4087
  }
4088

4089
  if (pAlterReq->hasConnectTime) {
34,675✔
4090
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectTime:%d,", pAlterReq->connectTime);
143✔
4091
    newUser.connectTime = pAlterReq->connectTime;
143✔
4092
  }
4093

4094
  if (pAlterReq->hasConnectIdleTime) {
34,675✔
4095
    auditLen +=
143✔
4096
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectIdleTime:%d,", pAlterReq->connectIdleTime);
143✔
4097
    newUser.connectIdleTime = pAlterReq->connectIdleTime;
143✔
4098
  }
4099

4100
  if (pAlterReq->hasCallPerSession) {
34,675✔
4101
    auditLen +=
429✔
4102
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "callPerSession:%d,", pAlterReq->callPerSession);
429✔
4103
    newUser.callPerSession = pAlterReq->callPerSession;
429✔
4104
  }
4105

4106
  if (pAlterReq->hasVnodePerCall) {
34,675✔
4107
    auditLen +=
286✔
4108
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "vnodePerCall:%d,", pAlterReq->vnodePerCall);
286✔
4109
    newUser.vnodePerCall = pAlterReq->vnodePerCall;
286✔
4110
  }
4111

4112
  if (pAlterReq->hasFailedLoginAttempts) {
34,675✔
4113
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "failedLoginAttempts:%d,",
429✔
4114
                          pAlterReq->failedLoginAttempts);
4115
    newUser.failedLoginAttempts = pAlterReq->failedLoginAttempts;
429✔
4116
  }
4117

4118
  if (pAlterReq->hasPasswordLifeTime) {
34,675✔
4119
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLifeTime:%d,",
143✔
4120
                          pAlterReq->passwordLifeTime);
4121
    newUser.passwordLifeTime = pAlterReq->passwordLifeTime;
143✔
4122
  }
4123

4124
  if (pAlterReq->hasPasswordReuseTime) {
34,675✔
4125
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseTime:%d,",
429✔
4126
                          pAlterReq->passwordReuseTime);
4127
    newUser.passwordReuseTime = pAlterReq->passwordReuseTime;
429✔
4128
  }
4129

4130
  if (pAlterReq->hasPasswordReuseMax) {
34,675✔
4131
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseMax:%d,",
429✔
4132
                          pAlterReq->passwordReuseMax);
4133
    newUser.passwordReuseMax = pAlterReq->passwordReuseMax;
429✔
4134
  }
4135

4136
  if (pAlterReq->hasPasswordLockTime) {
34,675✔
4137
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLockTime:%d,",
143✔
4138
                          pAlterReq->passwordLockTime);
4139
    newUser.passwordLockTime = pAlterReq->passwordLockTime;
143✔
4140
  }
4141

4142
  if (pAlterReq->hasPasswordGraceTime) {
34,675✔
4143
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordGraceTime:%d,",
143✔
4144
                          pAlterReq->passwordGraceTime);
4145
    newUser.passwordGraceTime = pAlterReq->passwordGraceTime;
143✔
4146
  }
4147

4148
  if (pAlterReq->hasInactiveAccountTime) {
34,675✔
4149
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "inactiveAccountTime:%d,",
143✔
4150
                          pAlterReq->inactiveAccountTime);
4151
    newUser.inactiveAccountTime = pAlterReq->inactiveAccountTime;
143✔
4152
  }
4153

4154
  if (pAlterReq->hasAllowTokenNum) {
34,675✔
4155
    auditLen +=
286✔
4156
        tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "allowTokenNum:%d,", pAlterReq->allowTokenNum);
286✔
4157
    newUser.allowTokenNum = pAlterReq->allowTokenNum;
286✔
4158
  }
4159

4160
  if (pAlterReq->numDropIpRanges > 0 || pAlterReq->numIpRanges > 0) {
34,675✔
4161
    int32_t dummy = 0;
632✔
4162

4163
    // put previous ip whitelist into hash table
4164
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
632✔
4165
    if (m == NULL) {
632✔
4166
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4167
    }
4168

4169
    for (int32_t i = 0; i < newUser.pIpWhiteListDual->num; i++) {
2,701✔
4170
      SIpRange range;
2,069✔
4171
      copyIpRange(&range, newUser.pIpWhiteListDual->pIpRanges + i);
2,069✔
4172
      code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
2,069✔
4173
      if (code != 0) {
2,069✔
4174
        taosHashCleanup(m);
×
4175
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4176
      }
4177
    }
4178

4179
    if (pAlterReq->numDropIpRanges > 0) {
632✔
4180
      auditLen +=
316✔
4181
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropIpRanges:%d,", pAlterReq->numDropIpRanges);
316✔
4182

4183
      for (int32_t i = 0; i < pAlterReq->numDropIpRanges; i++) {
775✔
4184
        if (taosHashGetSize(m) == 0) {
459✔
4185
          break;
×
4186
        }
4187

4188
        SIpRange range;
459✔
4189
        copyIpRange(&range, pAlterReq->pDropIpRanges + i);
459✔
4190

4191
        // for white list, drop default ip ranges is allowed, otherwise, we can never
4192
        // convert white list to black list.
4193

4194
        code = taosHashRemove(m, &range, sizeof(range));
459✔
4195
        if (code == TSDB_CODE_NOT_FOUND) {
459✔
4196
          // treat not exist as success
4197
          code = 0;
173✔
4198
        }
4199
        if (code != 0) {
459✔
4200
          taosHashCleanup(m);
×
4201
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4202
        }
4203
      }
4204
    }
4205

4206
    if (pAlterReq->numIpRanges > 0) {
632✔
4207
      auditLen +=
316✔
4208
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addIpRanges:%d,", pAlterReq->numIpRanges);
316✔
4209
      for (int32_t i = 0; i < pAlterReq->numIpRanges; i++) {
775✔
4210
        SIpRange range;
459✔
4211
        copyIpRange(&range, pAlterReq->pIpRanges + i);
459✔
4212
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
459✔
4213
        if (code != 0) {
459✔
4214
          taosHashCleanup(m);
×
4215
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4216
        }
4217
      }
4218
    }
4219

4220
    int32_t numOfRanges = taosHashGetSize(m);
632✔
4221
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
632✔
4222
      taosHashCleanup(m);
×
4223
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
4224
    }
4225

4226
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
632✔
4227
    if (p == NULL) {
632✔
4228
      taosHashCleanup(m);
×
4229
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4230
    }
4231

4232
    void   *pIter = taosHashIterate(m, NULL);
632✔
4233
    int32_t i = 0;
632✔
4234
    while (pIter) {
2,874✔
4235
      size_t    len = 0;
2,242✔
4236
      SIpRange *key = taosHashGetKey(pIter, &len);
2,242✔
4237
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
2,242✔
4238
      pIter = taosHashIterate(m, pIter);
2,242✔
4239
      i++;
2,242✔
4240
    }
4241

4242
    taosHashCleanup(m);
632✔
4243
    p->num = numOfRanges;
632✔
4244
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
632✔
4245
    sortIpWhiteList(p);
632✔
4246
    newUser.pIpWhiteListDual = p;
632✔
4247

4248
    newUser.ipWhiteListVer++;
632✔
4249
  }
4250

4251
  if (pAlterReq->numTimeRanges > 0 || pAlterReq->numDropTimeRanges) {
34,675✔
4252
    int32_t dummy = 0;
572✔
4253

4254
    // put previous ip whitelist into hash table
4255
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
572✔
4256
    if (m == NULL) {
572✔
4257
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4258
    }
4259

4260
    for (int32_t i = 0; i < newUser.pTimeWhiteList->num; i++) {
1,430✔
4261
      SDateTimeWhiteListItem *range = &newUser.pTimeWhiteList->ranges[i];
858✔
4262
      if (isDateTimeWhiteListItemExpired(range)) {
858✔
4263
        continue;
×
4264
      }
4265
      code = taosHashPut(m, range, sizeof(*range), &dummy, sizeof(dummy));
858✔
4266
      if (code != 0) {
858✔
4267
        taosHashCleanup(m);
×
4268
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4269
      }
4270
    }
4271

4272
    if (pAlterReq->numDropTimeRanges > 0) {
572✔
4273
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropTimeRanges:%d,",
429✔
4274
                            pAlterReq->numDropTimeRanges);
4275
      for (int32_t i = 0; i < pAlterReq->numDropTimeRanges; i++) {
1,001✔
4276
        if (taosHashGetSize(m) == 0) {
572✔
4277
          break;
×
4278
        }
4279
        SDateTimeWhiteListItem range = {0};
572✔
4280
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pDropTimeRanges + i);
572✔
4281

4282
        code = taosHashRemove(m, &range, sizeof(range));
572✔
4283
        if (code == TSDB_CODE_NOT_FOUND) {
572✔
4284
          // treat not exist as success
4285
          code = 0;
×
4286
        }
4287
        if (code != 0) {
572✔
4288
          taosHashCleanup(m);
×
4289
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4290
        }
4291
      }
4292
    }
4293

4294
    if (pAlterReq->numTimeRanges > 0) {
572✔
4295
      auditLen +=
429✔
4296
          tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addTimeRanges:%d,", pAlterReq->numTimeRanges);
429✔
4297
      for (int32_t i = 0; i < pAlterReq->numTimeRanges; i++) {
1,001✔
4298
        SDateTimeWhiteListItem range = {0};
572✔
4299
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pTimeRanges + i);
572✔
4300
        if (isDateTimeWhiteListItemExpired(&range)) {
572✔
4301
          continue;
×
4302
        }
4303
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
572✔
4304
        if (code != 0) {
572✔
4305
          taosHashCleanup(m);
×
4306
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4307
        }
4308
      }
4309
    }
4310

4311
    int32_t numOfRanges = taosHashGetSize(m);
572✔
4312
    if (numOfRanges > MND_MAX_USER_TIME_RANGE) {
572✔
4313
      taosHashCleanup(m);
×
4314
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
4315
    }
4316

4317
    SDateTimeWhiteList *p =
1,144✔
4318
        taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
572✔
4319
    if (p == NULL) {
572✔
4320
      taosHashCleanup(m);
×
4321
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4322
    }
4323

4324
    void   *pIter = taosHashIterate(m, NULL);
572✔
4325
    int32_t i = 0;
572✔
4326
    while (pIter) {
1,430✔
4327
      size_t                  len = 0;
858✔
4328
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
858✔
4329
      memcpy(&p->ranges[i], key, sizeof(SDateTimeWhiteListItem));
858✔
4330
      pIter = taosHashIterate(m, pIter);
858✔
4331
      i++;
858✔
4332
    }
4333

4334
    taosHashCleanup(m);
572✔
4335
    p->num = numOfRanges;
572✔
4336
    taosMemoryFreeClear(newUser.pTimeWhiteList);
572✔
4337
    sortTimeWhiteList(p);
572✔
4338
    newUser.pTimeWhiteList = p;
572✔
4339
    newUser.timeWhiteListVer++;
572✔
4340
  }
4341
#endif  // TD_ENTERPRISE
4342

4343
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
34,675✔
4344
  if (pAlterReq->hasEnable) {
34,675✔
4345
    if (newUser.enable) {
1,376✔
4346
      if (taosHashGet(newUser.roles, TSDB_ROLE_SYSAUDIT_LOG, strlen(TSDB_ROLE_SYSAUDIT_LOG) + 1)) {
855✔
4347
        (void)mndResetAuditLogUser(pMnode, newUser.user, true);
×
4348
      }
4349
    } else {
4350
      (void)mndResetAuditLogUser(pMnode, newUser.user, false);
521✔
4351
    }
4352
  }
4353
  code = TSDB_CODE_ACTION_IN_PROGRESS;
34,675✔
4354

4355
  if (auditLen > 0) {
34,675✔
4356
    auditLog[--auditLen] = 0;  // remove last ','
34,675✔
4357
  }
4358
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
34,675✔
4359
    int64_t tse = taosGetTimestampMs();
34,675✔
4360
    double  duration = (double)(tse - tss);
34,675✔
4361
    duration = duration / 1000;
34,675✔
4362
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", pAlterReq->user, auditLog, auditLen, duration, 0);
34,675✔
4363
  }
4364

4365
_OVER:
37,395✔
4366
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
37,395✔
4367
    mError("user:%s, failed to alter at line %d since %s", pAlterReq->user, lino, tstrerror(code));
2,720✔
4368
  }
4369

4370
  mndReleaseUser(pMnode, pUser);
37,395✔
4371
  mndUserFreeObj(&newUser);
37,395✔
4372
  return code;
37,395✔
4373
}
4374

4375
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
37,395✔
4376
  SAlterUserReq alterReq = {0};
37,395✔
4377

4378
  int32_t code = tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq);
37,395✔
4379
  if (code != 0) {
37,395✔
4380
    mError("failed to deserialize alter user request at line %d since %s", __LINE__, tstrerror(code));
×
4381
    TAOS_RETURN(code);
×
4382
  }
4383

4384
  if (alterReq.user[0] == 0) {
37,395✔
4385
    tFreeSAlterUserReq(&alterReq);
×
4386
    mError("failed to alter user at line %d since invalid user format", __LINE__);
×
4387
    TAOS_RETURN(TSDB_CODE_MND_INVALID_USER_FORMAT);
×
4388
  }
4389

4390
  mInfo("user:%s, start to alter", alterReq.user);
37,395✔
4391
  if (alterReq.alterType == TSDB_ALTER_USER_BASIC_INFO) {
37,395✔
4392
    code = mndProcessAlterUserBasicInfoReq(pReq, &alterReq);
37,395✔
4393
  } else {
4394
    // code = mndProcessAlterUserPrivilegesReq(pReq, &alterReq); // obsolete
4395
  }
4396

4397
  tFreeSAlterUserReq(&alterReq);
37,395✔
4398
  TAOS_RETURN(code);
37,395✔
4399
}
4400

4401
int32_t mndResetAuditLogUser(SMnode *pMnode, const char *user, bool isAdd) {
42,437,302✔
4402
  if (user) {
42,437,302✔
4403
    (void)taosThreadRwlockRdlock(&userCache.rw);
35,887✔
4404
    if (isAdd) {
35,887✔
4405
      if (userCache.auditLogUser[0] != 0) {
81✔
4406
        (void)taosThreadRwlockUnlock(&userCache.rw);
×
4407
        return 0;
×
4408
      }
4409
      (void)taosThreadRwlockUnlock(&userCache.rw);
81✔
4410
      (void)taosThreadRwlockWrlock(&userCache.rw);
81✔
4411
      (void)tsnprintf(userCache.auditLogUser, TSDB_USER_LEN, "%s", user);
81✔
4412
      (void)taosThreadRwlockUnlock(&userCache.rw);
81✔
4413
      return 0;
81✔
4414
    } else if (strcmp(userCache.auditLogUser, user) != 0) {
35,806✔
4415
      (void)taosThreadRwlockUnlock(&userCache.rw);
35,806✔
4416
      return 0;
35,806✔
4417
    }
4418
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
4419
  }
4420

4421
  void     *pIter = NULL;
42,401,415✔
4422
  SSdb     *pSdb = pMnode->pSdb;
42,401,415✔
4423
  SUserObj *pUser = NULL;
42,401,415✔
4424
  int32_t   len = strlen(TSDB_ROLE_SYSAUDIT_LOG) + 1;
42,401,415✔
4425
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
90,344,052✔
4426
    if (pUser->enable == 0) {
47,942,637✔
4427
      mndReleaseUser(pMnode, pUser);
18,503✔
4428
      continue;
18,503✔
4429
    }
4430
    if (taosHashGet(pUser->roles, TSDB_ROLE_SYSAUDIT_LOG, len) != NULL) {
47,924,134✔
4431
      (void)taosThreadRwlockWrlock(&userCache.rw);
×
4432
      (void)tsnprintf(userCache.auditLogUser, TSDB_USER_LEN, "%s", pUser->name);
×
4433
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
4434
      sdbCancelFetch(pSdb, pIter);
×
4435
      mndReleaseUser(pMnode, pUser);
×
4436
      return 0;
×
4437
    }
4438
    mndReleaseUser(pMnode, pUser);
47,924,134✔
4439
  }
4440
  (void)taosThreadRwlockWrlock(&userCache.rw);
42,401,415✔
4441
  userCache.auditLogUser[0] = 0;
42,401,415✔
4442
  (void)taosThreadRwlockUnlock(&userCache.rw);
42,401,415✔
4443
  return TSDB_CODE_MND_USER_NOT_AVAILABLE;
42,401,415✔
4444
}
4445

4446
int32_t mndGetAuditUser(SMnode *pMnode, char *user) {
42,402,711✔
4447
  (void)taosThreadRwlockRdlock(&userCache.rw);
42,402,711✔
4448
  if (userCache.auditLogUser[0] != 0) {
42,402,711✔
4449
    (void)tsnprintf(user, TSDB_USER_LEN, "%s", userCache.auditLogUser);
1,296✔
4450
    (void)taosThreadRwlockUnlock(&userCache.rw);
1,296✔
4451
    return 0;
1,296✔
4452
  }
4453
  (void)taosThreadRwlockUnlock(&userCache.rw);
42,401,415✔
4454

4455
  int32_t code = 0;
42,401,415✔
4456
  if ((code = mndResetAuditLogUser(pMnode, NULL, false)) != 0) {
42,401,415✔
4457
    return code;
42,401,415✔
4458
  }
4459

4460
  (void)taosThreadRwlockRdlock(&userCache.rw);
×
4461
  (void)tsnprintf(user, TSDB_USER_LEN, "%s", userCache.auditLogUser);
×
4462
  (void)taosThreadRwlockUnlock(&userCache.rw);
×
4463

4464
  return 0;
×
4465
}
4466

4467
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
35,285✔
4468
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "drop-user");
35,285✔
4469
  if (pTrans == NULL) {
35,285✔
4470
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
4471
    TAOS_RETURN(terrno);
×
4472
  }
4473
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
35,285✔
4474

4475
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
35,285✔
4476
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
35,285✔
4477
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
4478
    mndTransDrop(pTrans);
×
4479
    TAOS_RETURN(terrno);
×
4480
  }
4481
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
35,285✔
4482
    mndTransDrop(pTrans);
×
4483
    TAOS_RETURN(terrno);
×
4484
  }
4485

4486
  if (mndDropTokensByUser(pMnode, pTrans, pUser->user) != 0) {
35,285✔
4487
    mndTransDrop(pTrans);
×
4488
    TAOS_RETURN(terrno);
×
4489
  }
4490

4491
  if (mndTransPrepare(pMnode, pTrans) != 0) {
35,285✔
4492
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
4493
    mndTransDrop(pTrans);
×
4494
    TAOS_RETURN(terrno);
×
4495
  }
4496

4497
  userCacheRemoveUser(pUser->user);
35,285✔
4498
  mndDropCachedTokensByUser(pUser->user);
35,285✔
4499
  (void)mndResetAuditLogUser(pMnode, pUser->user, false);
35,285✔
4500

4501
  mndTransDrop(pTrans);
35,285✔
4502
  TAOS_RETURN(0);
35,285✔
4503
}
4504

4505
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
36,119✔
4506
  SMnode      *pMnode = pReq->info.node;
36,119✔
4507
  int32_t      code = 0;
36,119✔
4508
  int32_t      lino = 0;
36,119✔
4509
  SUserObj    *pOperUser = NULL;
36,119✔
4510
  SUserObj    *pUser = NULL;
36,119✔
4511
  SDropUserReq dropReq = {0};
36,119✔
4512
  int64_t      tss = taosGetTimestampMs();
36,119✔
4513

4514
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
36,119✔
4515

4516
  mInfo("user:%s, start to drop", dropReq.user);
36,119✔
4517

4518
  if (dropReq.user[0] == 0) {
36,119✔
4519
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
4520
  }
4521

4522
  if (0 == strcmp(dropReq.user, TSDB_DEFAULT_USER)) {
36,119✔
4523
    return TSDB_CODE_MND_NO_RIGHTS;
×
4524
  }
4525

4526
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
36,119✔
4527

4528
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
35,285✔
4529
  if (pOperUser == NULL) {
35,285✔
4530
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
4531
  }
4532

4533
  // TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_DROP_USER), &lino, _OVER);
4534
  TAOS_CHECK_GOTO(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_DROP, 0, 0, NULL, NULL),
35,285✔
4535
                  &lino, _OVER);
4536

4537
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
35,285✔
4538
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
35,285✔
4539

4540
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
35,285✔
4541
    int64_t tse = taosGetTimestampMs();
35,285✔
4542
    double  duration = (double)(tse - tss);
35,285✔
4543
    duration = duration / 1000;
35,285✔
4544
    auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen, duration, 0);
35,285✔
4545
  }
4546

4547
_OVER:
36,119✔
4548
  if (dropReq.ignoreNotExists && code == TSDB_CODE_MND_USER_NOT_EXIST) {
36,119✔
4549
    code = 0;
519✔
4550
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
35,600✔
4551
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
315✔
4552
  }
4553

4554
  mndReleaseUser(pMnode, pUser);
36,119✔
4555
  mndReleaseUser(pMnode, pOperUser);
36,119✔
4556
  tFreeSDropUserReq(&dropReq);
36,119✔
4557
  TAOS_RETURN(code);
36,119✔
4558
}
4559

4560
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
6,526,712✔
4561
  SMnode         *pMnode = pReq->info.node;
6,526,712✔
4562
  int32_t         code = 0;
6,526,712✔
4563
  int32_t         lino = 0;
6,526,712✔
4564
  int32_t         contLen = 0;
6,526,712✔
4565
  void           *pRsp = NULL;
6,526,712✔
4566
  SUserObj       *pUser = NULL;
6,526,712✔
4567
  SGetUserAuthReq authReq = {0};
6,526,712✔
4568
  SGetUserAuthRsp authRsp = {0};
6,526,712✔
4569

4570
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
6,526,712✔
4571
  mTrace("user:%s, start to get auth", authReq.user);
6,526,712✔
4572

4573
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
6,526,712✔
4574

4575
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
6,525,280✔
4576

4577
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
6,525,280✔
4578
  if (contLen < 0) {
6,525,280✔
4579
    TAOS_CHECK_EXIT(contLen);
×
4580
  }
4581
  pRsp = rpcMallocCont(contLen);
6,525,280✔
4582
  if (pRsp == NULL) {
6,525,280✔
4583
    TAOS_CHECK_EXIT(terrno);
×
4584
  }
4585

4586
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
6,525,280✔
4587
  if (contLen < 0) {
6,525,280✔
4588
    TAOS_CHECK_EXIT(contLen);
×
4589
  }
4590

4591
_exit:
6,526,712✔
4592
  mndReleaseUser(pMnode, pUser);
6,526,712✔
4593
  tFreeSGetUserAuthRsp(&authRsp);
6,526,712✔
4594
  if (code < 0) {
6,526,712✔
4595
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
1,432✔
4596
    rpcFreeCont(pRsp);
1,432✔
4597
    pRsp = NULL;
1,432✔
4598
    contLen = 0;
1,432✔
4599
  }
4600
  pReq->info.rsp = pRsp;
6,526,712✔
4601
  pReq->info.rspLen = contLen;
6,525,992✔
4602
  pReq->code = code;
6,525,992✔
4603

4604
  TAOS_RETURN(code);
6,525,992✔
4605
}
4606

4607
static void base32Encode(const uint8_t *in, int32_t inLen, char *out) {
18,474✔
4608
  int buffer = 0, bits = 0;
18,474✔
4609
  int outLen = 0;
18,474✔
4610

4611
  // process all input bytes
4612
  for (int i = 0; i < inLen; i++) {
609,642✔
4613
    buffer = (buffer << 8) | in[i];
591,168✔
4614
    bits += 8;
591,168✔
4615

4616
    while (bits >= 5) {
1,533,342✔
4617
      int v = (buffer >> (bits - 5)) & 0x1F;
942,174✔
4618
      out[outLen++] = (v >= 26) ? (v - 26 + '2') : (v + 'A');
942,174✔
4619
      bits -= 5;
942,174✔
4620
    }
4621
  }
4622

4623
  // process remaining bits
4624
  if (bits > 0) {
18,474✔
4625
    int v = (buffer << (5 - bits)) & 0x1F;
18,474✔
4626
    out[outLen++] = (v >= 26) ? (v - 26 + '2') : (v + 'A');
18,474✔
4627
  }
4628

4629
  out[outLen] = '\0';
18,474✔
4630
}
18,474✔
4631

4632
static int32_t mndCreateTotpSecret(SMnode *pMnode, SUserObj *pUser, SRpcMsg *pReq) {
18,474✔
4633
  SCreateTotpSecretRsp rsp = {0};
18,474✔
4634

4635
  base32Encode((uint8_t *)pUser->totpsecret, sizeof(pUser->totpsecret), rsp.totpSecret);
18,474✔
4636
  tstrncpy(rsp.user, pUser->user, sizeof(rsp.user));
18,474✔
4637

4638
  int32_t len = tSerializeSCreateTotpSecretRsp(NULL, 0, &rsp);
18,474✔
4639
  if (len < 0) {
18,474✔
4640
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
4641
  }
4642

4643
  void *pData = taosMemoryMalloc(len);
18,474✔
4644
  if (pData == NULL) {
18,474✔
4645
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
4646
  }
4647

4648
  if (tSerializeSCreateTotpSecretRsp(pData, len, &rsp) != len) {
18,474✔
4649
    taosMemoryFree(pData);
×
4650
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
4651
  }
4652

4653
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "create-totp-secret");
18,474✔
4654
  if (pTrans == NULL) {
18,474✔
4655
    mError("user:%s, failed to create totp secret since %s", pUser->user, terrstr());
×
4656
    taosMemoryFree(pData);
×
4657
    TAOS_RETURN(terrno);
×
4658
  }
4659
  mInfo("trans:%d, used to create totp secret for user:%s", pTrans->id, pUser->user);
18,474✔
4660

4661
  mndTransSetUserData(pTrans, pData, len);
18,474✔
4662

4663
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
18,474✔
4664
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
18,474✔
4665
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
4666
    mndTransDrop(pTrans);
×
4667
    TAOS_RETURN(terrno);
×
4668
  }
4669
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) < 0) {
18,474✔
4670
    mndTransDrop(pTrans);
×
4671
    TAOS_RETURN(terrno);
×
4672
  }
4673

4674
  if (mndTransPrepare(pMnode, pTrans) != 0) {
18,474✔
4675
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
4676
    mndTransDrop(pTrans);
×
4677
    TAOS_RETURN(terrno);
×
4678
  }
4679

4680
  mndTransDrop(pTrans);
18,474✔
4681
  TAOS_RETURN(0);
18,474✔
4682
}
4683

4684
static int32_t mndProcessCreateTotpSecretReq(SRpcMsg *pReq) {
18,643✔
4685
  SMnode              *pMnode = pReq->info.node;
18,643✔
4686
  int32_t              code = 0;
18,643✔
4687
  int32_t              lino = 0;
18,643✔
4688
  SUserObj            *pUser = NULL;
18,643✔
4689
  SUserObj             newUser = {0};
18,643✔
4690
  SCreateTotpSecretReq req = {0};
18,643✔
4691
  int64_t              tss = taosGetTimestampMs();
18,643✔
4692

4693
  TAOS_CHECK_GOTO(tDeserializeSCreateTotpSecretReq(pReq->pCont, pReq->contLen, &req), &lino, _OVER);
18,643✔
4694
  mTrace("user:%s, start to create/update totp secret", req.user);
18,643✔
4695

4696
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, req.user, &pUser), &lino, _OVER);
18,643✔
4697
  TAOS_CHECK_GOTO(mndCheckTotpSecretPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, PRIV_TOTP_CREATE),
18,474✔
4698
                  &lino, _OVER);
4699
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
18,474✔
4700
  taosSafeRandBytes((uint8_t *)newUser.totpsecret, sizeof(newUser.totpsecret));
18,474✔
4701
  TAOS_CHECK_GOTO(mndCreateTotpSecret(pMnode, &newUser, pReq), &lino, _OVER);
18,474✔
4702

4703
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
18,474✔
4704
    double duration = (double)(taosGetTimestampMs() - tss) / 1000.0;
18,474✔
4705
    auditRecord(pReq, pMnode->clusterId, "createTotpSecret", "", req.user, req.sql, req.sqlLen, duration, 0);
18,474✔
4706
  }
4707

4708
_OVER:
18,643✔
4709
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
18,643✔
4710
    mError("user:%s, failed to create totp secret at line %d since %s", req.user, lino, tstrerror(code));
169✔
4711
  }
4712
  mndReleaseUser(pMnode, pUser);
18,643✔
4713
  mndUserFreeObj(&newUser);
18,643✔
4714
  tFreeSCreateTotpSecretReq(&req);
18,643✔
4715
  TAOS_RETURN(code);
18,643✔
4716
}
4717

4718
int32_t mndBuildSMCreateTotpSecretResp(STrans *pTrans, void **ppResp, int32_t *pRespLen) {
18,474✔
4719
  // user data is the response
4720
  *ppResp = pTrans->userData;
18,474✔
4721
  *pRespLen = pTrans->userDataLen;
18,474✔
4722
  pTrans->userData = NULL;
18,474✔
4723
  pTrans->userDataLen = 0;
18,474✔
4724
  return 0;
18,474✔
4725
}
4726

4727
static int32_t mndProcessDropTotpSecretReq(SRpcMsg *pReq) {
7,098✔
4728
  SMnode            *pMnode = pReq->info.node;
7,098✔
4729
  int32_t            code = 0;
7,098✔
4730
  int32_t            lino = 0;
7,098✔
4731
  SUserObj          *pUser = NULL;
7,098✔
4732
  SUserObj           newUser = {0};
7,098✔
4733
  SDropTotpSecretReq req = {0};
7,098✔
4734
  int64_t            tss = taosGetTimestampMs();
7,098✔
4735

4736
  TAOS_CHECK_GOTO(tDeserializeSDropTotpSecretReq(pReq->pCont, pReq->contLen, &req), &lino, _OVER);
7,098✔
4737
  mTrace("user:%s, start to drop totp secret", req.user);
7,098✔
4738

4739
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, req.user, &pUser), &lino, _OVER);
7,098✔
4740
  TAOS_CHECK_GOTO(mndCheckTotpSecretPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, PRIV_TOTP_DROP),
5,408✔
4741
                  &lino, _OVER);
4742

4743
  if (!mndIsTotpEnabledUser(pUser)) {
5,408✔
4744
    TAOS_CHECK_GOTO(TSDB_CODE_MND_TOTP_SECRET_NOT_EXIST, &lino, _OVER);
5,070✔
4745
  }
4746

4747
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
338✔
4748
  (void)memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
338✔
4749
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
338✔
4750

4751
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
338✔
4752
    double duration = (double)(taosGetTimestampMs() - tss) / 1000.0;
338✔
4753
    auditRecord(pReq, pMnode->clusterId, "dropTotpSecret", "", req.user, req.sql, req.sqlLen, duration, 0);
338✔
4754
  }
4755

4756
_OVER:
7,098✔
4757
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
7,098✔
4758
    mError("user:%s, failed to drop totp secret at line %d since %s", req.user, lino, tstrerror(code));
6,760✔
4759
  }
4760
  mndReleaseUser(pMnode, pUser);
7,098✔
4761
  mndUserFreeObj(&newUser);
7,098✔
4762
  tFreeSDropTotpSecretReq(&req);
7,098✔
4763
  TAOS_RETURN(code);
7,098✔
4764
}
4765

4766
bool mndIsTotpEnabledUser(SUserObj *pUser) {
4,471,119✔
4767
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
145,577,971✔
4768
    if (pUser->totpsecret[i] != 0) {
141,167,948✔
4769
      return true;
59,256✔
4770
    }
4771
  }
4772
  return false;
4,410,023✔
4773
}
4774

4775
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
143,494✔
4776
  SMnode   *pMnode = pReq->info.node;
143,494✔
4777
  SSdb     *pSdb = pMnode->pSdb;
143,494✔
4778
  int32_t   code = 0;
143,494✔
4779
  int32_t   lino = 0;
143,494✔
4780
  int32_t   numOfRows = 0;
143,494✔
4781
  SUserObj *pUser = NULL;
143,494✔
4782
  int32_t   cols = 0;
143,494✔
4783
  int8_t    flag = 0;
143,494✔
4784
  char     *pWrite = NULL;
143,494✔
4785
  char     *buf = NULL;
143,494✔
4786
  char     *varstr = NULL;
143,494✔
4787
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
143,494✔
4788
  int32_t   bufSize = sizeof(tBuf) - VARSTR_HEADER_SIZE;
143,494✔
4789

4790
  while (numOfRows < rows) {
1,208,090✔
4791
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
1,208,090✔
4792
    if (pShow->pIter == NULL) break;
1,208,090✔
4793

4794
    cols = 0;
1,064,596✔
4795
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4796
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
1,064,596✔
4797
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1,064,596✔
4798
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
1,064,596✔
4799

4800
    cols++;
1,064,596✔
4801
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4802
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
1,064,596✔
4803

4804
    cols++;
1,064,596✔
4805
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4806
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
1,064,596✔
4807

4808
    cols++;
1,064,596✔
4809
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4810
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
1,064,596✔
4811

4812
    cols++;
1,064,596✔
4813
    flag = pUser->createdb ? 1 : 0;
1,064,596✔
4814
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4815
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
1,064,596✔
4816

4817
    cols++;
1,064,596✔
4818
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4819
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
1,064,596✔
4820

4821
    cols++;
1,064,596✔
4822
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4823
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
1,064,596✔
4824
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
1,064,596✔
4825

4826
    cols++;
1,064,596✔
4827

4828
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
1,064,596✔
4829
    if (tlen != 0) {
1,064,596✔
4830
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
1,064,596✔
4831
      if (varstr == NULL) {
1,064,596✔
4832
        sdbRelease(pSdb, pUser);
×
4833
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4834
      }
4835
      varDataSetLen(varstr, tlen);
1,064,596✔
4836
      (void)memcpy(varDataVal(varstr), buf, tlen);
1,064,596✔
4837

4838
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4839
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
1,064,596✔
4840

4841
      taosMemoryFreeClear(buf);
1,064,596✔
4842
    } else {
4843
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4844
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4845
    }
4846

4847
    cols++;
1,064,596✔
4848
    tlen = convertTimeRangesToStr(pUser, &buf);
1,064,596✔
4849
    if (tlen != 0) {
1,064,596✔
4850
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
1,064,596✔
4851
      if (varstr == NULL) {
1,064,596✔
4852
        sdbRelease(pSdb, pUser);
×
4853
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
4854
      }
4855
      varDataSetLen(varstr, tlen);
1,064,596✔
4856
      (void)memcpy(varDataVal(varstr), buf, tlen);
1,064,596✔
4857

4858
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,064,596✔
4859
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
1,064,596✔
4860

4861
      taosMemoryFreeClear(buf);
1,064,596✔
4862
    } else {
4863
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4864
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4865
    }
4866

4867
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,064,596✔
4868
      void  *pIter = NULL;
1,064,596✔
4869
      size_t klen = 0, tlen = 0;
1,064,596✔
4870
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
1,064,596✔
4871
      while ((pIter = taosHashIterate(pUser->roles, pIter))) {
2,416,526✔
4872
        char *roleName = taosHashGetKey(pIter, &klen);
1,351,930✔
4873
        tlen += snprintf(pBuf + tlen, bufSize - tlen, "%s,", roleName);
1,351,930✔
4874
      }
4875
      if (tlen > 0) {
1,064,596✔
4876
        pBuf[--tlen] = 0;  // remove last ','
1,064,596✔
4877
      } else {
4878
        pBuf[0] = 0;
×
4879
      }
4880
      varDataSetLen(tBuf, tlen);
1,064,596✔
4881
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
1,064,596✔
4882
    }
4883

4884
    numOfRows++;
1,064,596✔
4885
    sdbRelease(pSdb, pUser);
1,064,596✔
4886
  }
4887

4888
  pShow->numOfRows += numOfRows;
143,494✔
4889
_exit:
143,494✔
4890
  taosMemoryFreeClear(buf);
143,494✔
4891
  taosMemoryFreeClear(varstr);
143,494✔
4892
  if (code < 0) {
143,494✔
4893
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4894
    TAOS_RETURN(code);
×
4895
  }
4896
  return numOfRows;
143,494✔
4897
}
4898

4899
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
12,870✔
4900
  int32_t numOfRows = 0;
12,870✔
4901
#ifdef TD_ENTERPRISE
4902
  SMnode   *pMnode = pReq->info.node;
12,870✔
4903
  SSdb     *pSdb = pMnode->pSdb;
12,870✔
4904
  SUserObj *pOperUser = NULL;
12,870✔
4905
  SUserObj *pUser = NULL;
12,870✔
4906
  int32_t   code = 0;
12,870✔
4907
  int32_t   lino = 0;
12,870✔
4908
  int32_t   cols = 0;
12,870✔
4909
  int8_t    flag = 0;
12,870✔
4910
  char     *pWrite = NULL;
12,870✔
4911
  char     *buf = NULL;
12,870✔
4912
  char     *varstr = NULL;
12,870✔
4913
  char     *pBuf = NULL;
12,870✔
4914
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
12,870✔
4915
  int32_t   bufSize = sizeof(tBuf) - VARSTR_HEADER_SIZE;
12,870✔
4916
  bool      showSecurityInfo = false;
12,870✔
4917

4918
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
12,870✔
4919
  if (pOperUser == NULL) {
12,870✔
4920
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_USER_FROM_CONN);
×
4921
  }
4922
  if (0 == mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_USER_SHOW_SECURITY, 0, 0, NULL, NULL)) {
12,870✔
4923
    showSecurityInfo = true;
12,870✔
4924
  }
4925

4926
  while (numOfRows < rows) {
574,288✔
4927
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
574,288✔
4928
    if (pShow->pIter == NULL) break;
574,288✔
4929

4930
    cols = 0;
561,418✔
4931
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4932
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
561,418✔
4933
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
561,418✔
4934
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
561,418✔
4935

4936
    cols++;
561,418✔
4937
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4938
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
561,418✔
4939

4940
    cols++;
561,418✔
4941
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4942
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
561,418✔
4943

4944
    cols++;
561,418✔
4945
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4946
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
561,418✔
4947

4948
    cols++;
561,418✔
4949
    flag = pUser->createdb ? 1 : 0;
561,418✔
4950
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4951
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
561,418✔
4952

4953
    cols++;
561,418✔
4954
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4955
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
561,418✔
4956

4957
    cols++;
561,418✔
4958
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4959
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
561,418✔
4960
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
561,418✔
4961

4962
    cols++;
561,418✔
4963
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4964
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->changePass, false, pUser, pShow->pIter, _exit);
561,418✔
4965

4966
    cols++;
561,418✔
4967
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4968
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
561,418✔
4969
    STR_WITH_MAXSIZE_TO_VARSTR(pass, showSecurityInfo ? pUser->passwords[0].pass : "*",
561,418✔
4970
                               pShow->pMeta->pSchemas[cols].bytes);
4971
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, pShow->pIter, _exit);
561,418✔
4972

4973
    cols++;
561,418✔
4974
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4975
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sessionPerUser, false, pUser, pShow->pIter, _exit);
561,418✔
4976

4977
    cols++;
561,418✔
4978
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4979
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectTime, false, pUser, pShow->pIter, _exit);
561,418✔
4980

4981
    cols++;
561,418✔
4982
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4983
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectIdleTime, false, pUser, pShow->pIter, _exit);
561,418✔
4984

4985
    cols++;
561,418✔
4986
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4987
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->callPerSession, false, pUser, pShow->pIter, _exit);
561,418✔
4988

4989
    cols++;
561,418✔
4990
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4991
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerCall, false, pUser, pShow->pIter, _exit);
561,418✔
4992

4993
    cols++;
561,418✔
4994
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4995
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
561,418✔
4996

4997
    cols++;
561,418✔
4998
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
4999
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLifeTime, false, pUser, pShow->pIter, _exit);
561,418✔
5000

5001
    cols++;
561,418✔
5002
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5003
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseTime, false, pUser, pShow->pIter, _exit);
561,418✔
5004

5005
    cols++;
561,418✔
5006
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5007
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseMax, false, pUser, pShow->pIter, _exit);
561,418✔
5008

5009
    cols++;
561,418✔
5010
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5011
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLockTime, false, pUser, pShow->pIter, _exit);
561,418✔
5012

5013
    cols++;
561,418✔
5014
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5015
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordGraceTime, false, pUser, pShow->pIter, _exit);
561,418✔
5016

5017
    cols++;
561,418✔
5018
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5019
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->inactiveAccountTime, false, pUser, pShow->pIter, _exit);
561,418✔
5020

5021
    cols++;
561,418✔
5022
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5023
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->allowTokenNum, false, pUser, pShow->pIter, _exit);
561,418✔
5024

5025
    cols++;
561,418✔
5026
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
561,418✔
5027
    if (tlen != 0) {
561,418✔
5028
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
561,418✔
5029
      if (varstr == NULL) {
561,418✔
5030
        sdbRelease(pSdb, pUser);
×
5031
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
5032
      }
5033
      varDataSetLen(varstr, tlen);
561,418✔
5034
      (void)memcpy(varDataVal(varstr), buf, tlen);
561,418✔
5035

5036
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5037
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
561,418✔
5038

5039
      taosMemoryFreeClear(buf);
561,418✔
5040
    } else {
5041
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5042
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5043
    }
5044

5045
    cols++;
561,418✔
5046
    tlen = convertTimeRangesToStr(pUser, &buf);
561,418✔
5047
    if (tlen != 0) {
561,418✔
5048
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
561,418✔
5049
      if (varstr == NULL) {
561,418✔
5050
        sdbRelease(pSdb, pUser);
×
5051
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
5052
      }
5053
      varDataSetLen(varstr, tlen);
561,418✔
5054
      (void)memcpy(varDataVal(varstr), buf, tlen);
561,418✔
5055

5056
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
561,418✔
5057
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
561,418✔
5058

5059
      taosMemoryFreeClear(buf);
561,418✔
5060
    } else {
5061
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
5062
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
5063
    }
5064

5065
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
561,418✔
5066
      void  *pIter = NULL;
561,418✔
5067
      size_t klen = 0, tlen = 0;
561,418✔
5068
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
561,418✔
5069
      while ((pIter = taosHashIterate(pUser->roles, pIter))) {
1,148,576✔
5070
        char *roleName = taosHashGetKey(pIter, &klen);
587,158✔
5071
        tlen += snprintf(pBuf + tlen, bufSize - tlen, "%s,", roleName);
587,158✔
5072
      }
5073
      if (tlen > 0) {
561,418✔
5074
        pBuf[--tlen] = 0;  // remove last ','
561,418✔
5075
      } else {
5076
        pBuf[0] = 0;
×
5077
      }
5078
      varDataSetLen(tBuf, tlen);
561,418✔
5079
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pUser, pShow->pIter, _exit);
561,418✔
5080
    }
5081

5082
    numOfRows++;
561,418✔
5083
    sdbRelease(pSdb, pUser);
561,418✔
5084
  }
5085

5086
  pShow->numOfRows += numOfRows;
12,870✔
5087
_exit:
12,870✔
5088
  taosMemoryFreeClear(buf);
12,870✔
5089
  taosMemoryFreeClear(varstr);
12,870✔
5090
  mndReleaseUser(pMnode, pOperUser);
12,870✔
5091
  if (code < 0) {
12,870✔
5092
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5093
    TAOS_RETURN(code);
×
5094
  }
5095
#endif
5096
  return numOfRows;
12,870✔
5097
}
5098

5099
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
5100
  SSdb *pSdb = pMnode->pSdb;
×
5101
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
5102
}
×
5103

5104
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
×
5105
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
5106
  char   *value = taosHashIterate(hash, NULL);
×
5107
  char   *user = pUser->user;
×
5108
  int32_t code = 0;
×
5109
  int32_t lino = 0;
×
5110
  int32_t cols = 0;
×
5111
  int32_t numOfRows = *pNumOfRows;
×
5112

5113
  while (value != NULL) {
×
5114
    cols = 0;
×
5115
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
5116
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
×
5117
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5118
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
×
5119

5120
    char privilege[20] = {0};
×
5121
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
×
5122
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5123
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
×
5124

5125
    size_t keyLen = 0;
×
5126
    void  *key = taosHashGetKey(value, &keyLen);
×
5127

5128
    char dbName[TSDB_DB_NAME_LEN] = {0};
×
5129
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
×
5130
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
5131
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
×
5132
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5133
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
×
5134

5135
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
×
5136
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
×
5137
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
5138
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
×
5139
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5140
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
×
5141

5142
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
×
5143
      SNode  *pAst = NULL;
×
5144
      int32_t sqlLen = 0;
×
5145
      size_t  bufSz = strlen(value) + 1;
×
5146
      if (bufSz < 6) bufSz = 6;
×
5147
      TAOS_MEMORY_REALLOC(*sql, bufSz);
×
5148
      if (*sql == NULL) {
×
5149
        code = terrno;
×
5150
        goto _exit;
×
5151
      }
5152
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
5153
      if ((*condition) == NULL) {
×
5154
        code = terrno;
×
5155
        goto _exit;
×
5156
      }
5157

5158
      if (nodesStringToNode(value, &pAst) == 0) {
×
5159
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
×
5160
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
5161
        }
5162
        nodesDestroyNode(pAst);
×
5163
      }
5164

5165
      if (sqlLen == 0) {
×
5166
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
5167
      }
5168

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

5171
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5172
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
×
5173

5174
      char notes[2] = {0};
×
5175
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
5176
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5177
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
×
5178
    } else {
5179
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
5180
      if ((*condition) == NULL) {
×
5181
        code = terrno;
×
5182
        goto _exit;
×
5183
      }
5184
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
×
5185
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5186
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
×
5187

5188
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
×
5189
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
×
5190
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
5191
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
×
5192
    }
5193

5194
    numOfRows++;
×
5195
    value = taosHashIterate(hash, value);
×
5196
  }
5197
  *pNumOfRows = numOfRows;
×
5198
_exit:
×
5199
  if (code < 0) {
×
5200
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5201
    sdbRelease(pSdb, pUser);
×
5202
    sdbCancelFetch(pSdb, pShow->pIter);
×
5203
  }
5204
  TAOS_RETURN(code);
×
5205
}
5206

5207
#if 0
5208
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
5209
  int32_t   code = 0;
5210
  int32_t   lino = 0;
5211
  SMnode   *pMnode = pReq->info.node;
5212
  SSdb     *pSdb = pMnode->pSdb;
5213
  int32_t   numOfRows = 0;
5214
  SUserObj *pUser = NULL;
5215
  int32_t   cols = 0;
5216
  char     *pWrite = NULL;
5217
  char     *condition = NULL;
5218
  char     *sql = NULL;
5219

5220
  bool fetchNextUser = pShow->restore ? false : true;
5221
  pShow->restore = false;
5222

5223
  while (numOfRows < rows) {
5224
    if (fetchNextUser) {
5225
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
5226
      if (pShow->pIter == NULL) break;
5227
    } else {
5228
      fetchNextUser = true;
5229
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
5230
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
5231
      if (!pUser) {
5232
        continue;
5233
      }
5234
    }
5235

5236
    int32_t numOfReadDbs = 0; //taosHashGetSize(pUser->readDbs);
5237
    int32_t numOfWriteDbs = 0; //taosHashGetSize(pUser->writeDbs);
5238
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
5239
    int32_t numOfReadTbs = taosHashGetSize(pUser->selectTbs);
5240
    int32_t numOfWriteTbs = taosHashGetSize(pUser->insertTbs);
5241
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
5242
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
5243
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
5244
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
5245
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
5246
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
5247
        rows) {
5248
      mInfo(
5249
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
5250
          "%d, alter tables %d, select views %d, write views %d, alter views %d",
5251
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
5252
          numOfReadViews, numOfWriteViews, numOfAlterViews);
5253
      pShow->restore = true;
5254
      sdbRelease(pSdb, pUser);
5255
      break;
5256
    }
5257

5258
    if (pUser->superUser) {
5259
      cols = 0;
5260
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5261
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5262
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5263
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5264

5265
      char privilege[20] = {0};
5266
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
5267
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5268
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5269

5270
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5271
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
5272
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5273
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5274

5275
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5276
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5277
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5278
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5279

5280
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5281
      if (condition == NULL) {
5282
        sdbRelease(pSdb, pUser);
5283
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5284
      }
5285
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5286
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5287
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5288

5289
      char notes[2] = {0};
5290
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5291
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5292
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5293

5294
      numOfRows++;
5295
    }
5296
#if 0
5297
    char *db = taosHashIterate(pUser->readDbs, NULL);
5298
    while (db != NULL) {
5299
      cols = 0;
5300
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5301
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5302
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5303
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5304

5305
      char privilege[20] = {0};
5306
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
5307
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5308
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5309

5310
      SName name = {0};
5311
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5312
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
5313
      if (code < 0) {
5314
        sdbRelease(pSdb, pUser);
5315
        sdbCancelFetch(pSdb, pShow->pIter);
5316
        TAOS_CHECK_GOTO(code, &lino, _exit);
5317
      }
5318
      (void)tNameGetDbName(&name, varDataVal(objName));
5319
      varDataSetLen(objName, strlen(varDataVal(objName)));
5320
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5321
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5322

5323
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5324
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5325
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5326
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5327

5328
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5329
      if (condition == NULL) {
5330
        sdbRelease(pSdb, pUser);
5331
        sdbCancelFetch(pSdb, pShow->pIter);
5332
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5333
      }
5334
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5335
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5336
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5337

5338
      char notes[2] = {0};
5339
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5340
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5341
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5342

5343
      numOfRows++;
5344
      db = taosHashIterate(pUser->readDbs, db);
5345
    }
5346

5347
    db = taosHashIterate(pUser->writeDbs, NULL);
5348
    while (db != NULL) {
5349
      cols = 0;
5350
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5351
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5352
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5353
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5354

5355
      char privilege[20] = {0};
5356
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
5357
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5358
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5359

5360
      SName name = {0};
5361
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5362
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
5363
      if (code < 0) {
5364
        sdbRelease(pSdb, pUser);
5365
        sdbCancelFetch(pSdb, pShow->pIter);
5366
        TAOS_CHECK_GOTO(code, &lino, _exit);
5367
      }
5368
      (void)tNameGetDbName(&name, varDataVal(objName));
5369
      varDataSetLen(objName, strlen(varDataVal(objName)));
5370
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5371
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
5372

5373
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5374
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5375
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5376
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5377

5378
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5379
      if (condition == NULL) {
5380
        sdbRelease(pSdb, pUser);
5381
        sdbCancelFetch(pSdb, pShow->pIter);
5382
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5383
      }
5384
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5385
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5386
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5387

5388
      char notes[2] = {0};
5389
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5390
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5391
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5392

5393
      numOfRows++;
5394
      db = taosHashIterate(pUser->writeDbs, db);
5395
    }
5396
#endif
5397
    TAOS_CHECK_EXIT(mndLoopHash(pUser->selectTbs, "select", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
5398

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

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

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

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

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

5409
    char *topic = taosHashIterate(pUser->topics, NULL);
5410
    while (topic != NULL) {
5411
      cols = 0;
5412
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5413
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5414
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5415
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
5416

5417
      char privilege[20] = {0};
5418
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
5419
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5420
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
5421

5422
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
5423
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
5424
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
5425
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5426
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, pShow->pIter, _exit);
5427

5428
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
5429
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
5430
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5431
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
5432

5433
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
5434
      if (condition == NULL) {
5435
        sdbRelease(pSdb, pUser);
5436
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
5437
      }
5438
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
5439
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5440
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
5441

5442
      char notes[2] = {0};
5443
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
5444
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
5445
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
5446

5447
      numOfRows++;
5448
      topic = taosHashIterate(pUser->topics, topic);
5449
    }
5450

5451
    sdbRelease(pSdb, pUser);
5452
  }
5453

5454
  pShow->numOfRows += numOfRows;
5455
_exit:
5456
  taosMemoryFreeClear(condition);
5457
  taosMemoryFreeClear(sql);
5458
  if (code < 0) {
5459
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
5460
    TAOS_RETURN(code);
5461
  }
5462
  return numOfRows;
5463
}
5464
#endif
5465

5466
int32_t mndShowTablePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows, void *pObj,
262,524✔
5467
                               const char *principalName, SHashObj *privTbs, EPrivType privType, char *pBuf,
5468
                               int32_t bufSize, int32_t *pNumOfRows) {
5469
  int32_t     code = 0, lino = 0;
262,524✔
5470
  SMnode     *pMnode = pReq->info.node;
262,524✔
5471
  SSdb       *pSdb = pMnode->pSdb;
262,524✔
5472
  int32_t     cols = 0, qBufSize = bufSize - VARSTR_HEADER_SIZE;
262,524✔
5473
  int32_t     numOfRows = *pNumOfRows;
262,524✔
5474
  char       *qBuf = NULL;
262,524✔
5475
  char       *sql = NULL;
262,524✔
5476
  char        roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
262,524✔
5477
  const char *privName = privInfoGetName(privType);
262,524✔
5478

5479
  STR_WITH_MAXSIZE_TO_VARSTR(roleName, principalName, pShow->pMeta->pSchemas[cols].bytes);
262,524✔
5480

5481
  void *pIter = NULL;
262,524✔
5482
  while ((pIter = taosHashIterate(privTbs, pIter))) {
306,877✔
5483
    SPrivTblPolicies *pPolices = (SPrivTblPolicies *)pIter;
44,353✔
5484
    SArray           *tblPolicies = pPolices->policy;
44,353✔
5485

5486
    char   *key = taosHashGetKey(pPolices, NULL);
44,353✔
5487
    int32_t objType = PRIV_OBJ_UNKNOWN;
44,353✔
5488
    char    dbName[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
44,353✔
5489
    char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
44,353✔
5490
    if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
44,353✔
5491
      sdbRelease(pSdb, pObj);
×
5492
      sdbCancelFetch(pSdb, pShow->pIter);
×
5493
      TAOS_CHECK_EXIT(code);
×
5494
    }
5495

5496
    int32_t nTbPolicies = taosArrayGetSize(tblPolicies);
44,353✔
5497
    for (int32_t i = 0; i < nTbPolicies; ++i) {
88,706✔
5498
      SPrivTblPolicy *tbPolicy = (SPrivTblPolicy *)TARRAY_GET_ELEM(tblPolicies, i);
44,353✔
5499
      cols = 0;
44,353✔
5500
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
44,353✔
5501
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
44,353✔
5502

5503
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5504
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privName, pShow->pMeta->pSchemas[cols].bytes);
44,353✔
5505
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5506
      }
5507

5508
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5509
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
44,353✔
5510
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5511
      }
5512

5513
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5514
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
44,353✔
5515
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5516
      }
5517

5518
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5519
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
44,353✔
5520
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5521
      }
5522
      // condition
5523
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5524
        SNode  *pAst = NULL;
44,353✔
5525
        int32_t sqlLen = 0;
44,353✔
5526
        qBuf = POINTER_SHIFT(pBuf, VARSTR_HEADER_SIZE);
44,353✔
5527
        if (tbPolicy->condLen > 0) {
44,353✔
5528
          if (nodesStringToNode(tbPolicy->cond, &pAst) == 0) {
44,353✔
5529
            if (nodesNodeToSQLFormat(pAst, qBuf, qBufSize, &sqlLen, true) != 0) {
44,353✔
5530
              sqlLen = tsnprintf(qBuf, qBufSize, "error");
×
5531
            }
5532
            nodesDestroyNode(pAst);
44,353✔
5533
          }
5534
          if (sqlLen == 0) {
44,353✔
5535
            sqlLen = tsnprintf(qBuf, qBufSize, "error");
×
5536
          }
5537
        } else {
5538
          sqlLen = tsnprintf(qBuf, qBufSize, "");
×
5539
        }
5540
        varDataSetLen(pBuf, sqlLen);
44,353✔
5541
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5542
      }
5543
      // notes
5544
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5545
        STR_WITH_MAXSIZE_TO_VARSTR((pBuf), "", 2);
44,353✔
5546
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5547
      }
5548
      // columns
5549
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5550
        SArray *pCols = tbPolicy->cols;
44,353✔
5551
        int32_t nCols = taosArrayGetSize(pCols);
44,353✔
5552
        int32_t totalLen = 0;
44,353✔
5553
        qBuf = POINTER_SHIFT(pBuf, VARSTR_HEADER_SIZE);
44,353✔
5554
        for (int32_t j = 0; j < nCols; ++j) {
44,353✔
5555
          SColNameFlag *pCol = (SColNameFlag *)TARRAY_GET_ELEM(pCols, j);
×
5556
          char          tmpBuf[TSDB_COL_NAME_LEN + 16] = {0};
×
5557
          int32_t       tmpLen = 0;
×
5558
          if (IS_MASK_ON(pCol)) {
×
5559
            tmpLen = snprintf(tmpBuf, sizeof(tmpBuf), "mask(%s)%s", pCol->colName, j == nCols - 1 ? "" : ",");
×
5560
          } else {
5561
            tmpLen = snprintf(tmpBuf, sizeof(tmpBuf), "%s%s", pCol->colName, j == nCols - 1 ? "" : ",");
×
5562
          }
5563
          if (totalLen + tmpLen > qBufSize) {
×
5564
            break;
×
5565
          }
5566
          (void)memcpy(POINTER_SHIFT(qBuf, totalLen), tmpBuf, tmpLen);
×
5567
          totalLen += tmpLen;
×
5568
        }
5569
        varDataSetLen(pBuf, totalLen);
44,353✔
5570
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5571
      }
5572
      // update_time
5573
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
44,353✔
5574
        char updateTime[40] = {0};
44,353✔
5575
        (void)formatTimestampLocal(updateTime, tbPolicy->updateUs, TSDB_TIME_PRECISION_MICRO);
44,353✔
5576
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, updateTime, pShow->pMeta->pSchemas[cols].bytes);
44,353✔
5577
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
44,353✔
5578
      }
5579
      ++numOfRows;
44,353✔
5580
    }
5581
  }
5582
  *pNumOfRows = numOfRows;
262,524✔
5583
_exit:
262,524✔
5584
  TAOS_RETURN(code);
262,524✔
5585
}
5586

5587
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
12,699✔
5588
  int32_t   code = 0, lino = 0;
12,699✔
5589
  SMnode   *pMnode = pReq->info.node;
12,699✔
5590
  SSdb     *pSdb = pMnode->pSdb;
12,699✔
5591
  int32_t   numOfRows = 0;
12,699✔
5592
  int32_t   cols = 0;
12,699✔
5593
  SUserObj *pObj = NULL;
12,699✔
5594
  char     *pBuf = NULL, *qBuf = NULL;
12,699✔
5595
  char     *sql = NULL;
12,699✔
5596
  char      roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
12,699✔
5597
  int32_t   bufSize = TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE;
12,699✔
5598

5599
  bool fetchNextInstance = pShow->restore ? false : true;
12,699✔
5600
  pShow->restore = false;
12,699✔
5601

5602
  while (numOfRows < rows) {
72,275✔
5603
    if (fetchNextInstance) {
72,275✔
5604
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pObj);
72,275✔
5605
      if (pShow->pIter == NULL) break;
72,275✔
5606
    } else {
5607
      fetchNextInstance = true;
×
5608
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
5609
      if (!(pObj = sdbAcquire(pSdb, SDB_USER, pKey))) {
×
5610
        continue;
×
5611
      }
5612
    }
5613

5614
    // count total privileges for current user
5615
    int32_t nSysPrivileges = privPopCnt(&pObj->sysPrivs);
59,576✔
5616
    int32_t nObjPrivileges = 0;
59,576✔
5617
    void   *pIter = NULL;
59,576✔
5618
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
1,161,935✔
5619
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
1,102,359✔
5620
      nObjPrivileges += privPopCnt(&pPolices->policy);
2,204,718✔
5621
    }
5622
    int32_t nTblPrivileges = privTblPrivCnt(pObj->selectTbs);
59,576✔
5623
    nTblPrivileges += privTblPrivCnt(pObj->insertTbs);
59,576✔
5624
    nTblPrivileges += privTblPrivCnt(pObj->updateTbs);
59,576✔
5625
    nTblPrivileges += privTblPrivCnt(pObj->deleteTbs);
59,576✔
5626

5627
    int32_t totalPrivileges = nSysPrivileges + nObjPrivileges + nTblPrivileges;
59,576✔
5628

5629
    if (numOfRows + totalPrivileges >= rows) {
59,576✔
5630
      if (totalPrivileges >= SHOW_PRIVILEGES_STEP_SIZE) {
×
5631
        mError("user:%s, has too many privileges:%d to show", pObj->name, totalPrivileges);
×
5632
        sdbRelease(pSdb, pObj);
×
5633
        TAOS_CHECK_EXIT(TSDB_CODE_MND_TOO_MANY_PRIVS);
×
5634
      }
5635
      pShow->restore = true;
×
5636
      sdbRelease(pSdb, pObj);
×
5637
      break;
×
5638
    }
5639

5640
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
59,576✔
5641
      sdbRelease(pSdb, pObj);
×
5642
      TAOS_CHECK_EXIT(terrno);
×
5643
    }
5644

5645
    cols = 0;
59,576✔
5646
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
59,576✔
5647

5648
    // system privileges
5649
    SPrivIter privIter = {0};
59,576✔
5650
    privIterInit(&privIter, &pObj->sysPrivs);
59,576✔
5651
    SPrivInfo *pPrivInfo = NULL;
59,576✔
5652
    while (privIterNext(&privIter, &pPrivInfo)) {
64,158✔
5653
      cols = 0;
4,582✔
5654
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
4,582✔
5655
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
4,582✔
5656

5657
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
4,582✔
5658
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
4,582✔
5659
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
4,582✔
5660
      }
5661
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
4,582✔
5662
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(PRIV_OBJ_CLUSTER), pShow->pMeta->pSchemas[cols].bytes);
4,582✔
5663
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
4,582✔
5664
      }
5665
      // skip db, table, condition, notes, columns, update_time
5666
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 6);
32,074✔
5667
      numOfRows++;
4,582✔
5668
    }
5669

5670
    // object privileges
5671
    pIter = NULL;
59,576✔
5672
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
1,161,935✔
5673
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
1,102,359✔
5674

5675
      char   *key = taosHashGetKey(pPolices, NULL);
1,102,359✔
5676
      int32_t objType = PRIV_OBJ_UNKNOWN;
1,102,359✔
5677
      char    dbName[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,102,359✔
5678
      char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,102,359✔
5679
      if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
1,102,359✔
5680
        sdbRelease(pSdb, pObj);
×
5681
        TAOS_CHECK_EXIT(code);
×
5682
      }
5683

5684
      SPrivIter privIter = {0};
1,102,359✔
5685
      privIterInit(&privIter, &pPolices->policy);
1,102,359✔
5686
      SPrivInfo *pPrivInfo = NULL;
1,102,359✔
5687
      while (privIterNext(&privIter, &pPrivInfo)) {
3,356,544✔
5688
        cols = 0;
2,254,185✔
5689
        SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2,254,185✔
5690
        COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
2,254,185✔
5691

5692
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,254,185✔
5693
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
2,254,185✔
5694
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,254,185✔
5695
        }
5696

5697
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,254,185✔
5698
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
2,254,185✔
5699
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,254,185✔
5700
        }
5701

5702
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,254,185✔
5703
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
2,254,185✔
5704
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,254,185✔
5705
        }
5706

5707
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
2,254,185✔
5708
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
2,254,185✔
5709
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
2,254,185✔
5710
        }
5711

5712
        // skip condition, notes, columns, update_time
5713
        COL_DATA_SET_EMPTY_VARCHAR(pBuf, 4);
11,270,925✔
5714

5715
        numOfRows++;
2,254,185✔
5716
      }
5717
    }
5718

5719
    // table level privileges
5720
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->selectTbs,
59,576✔
5721
                                           PRIV_TBL_SELECT, pBuf, bufSize, &numOfRows));
5722
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->insertTbs,
59,576✔
5723
                                           PRIV_TBL_INSERT, pBuf, bufSize, &numOfRows));
5724
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->updateTbs,
59,576✔
5725
                                           PRIV_TBL_UPDATE, pBuf, bufSize, &numOfRows));
5726
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->deleteTbs,
59,576✔
5727
                                           PRIV_TBL_DELETE, pBuf, bufSize, &numOfRows));
5728
    sdbRelease(pSdb, pObj);
59,576✔
5729
  }
5730

5731
  pShow->numOfRows += numOfRows;
12,699✔
5732
_exit:
12,699✔
5733
  taosMemoryFreeClear(pBuf);
12,699✔
5734
  taosMemoryFreeClear(sql);
12,699✔
5735
  if (code < 0) {
12,699✔
5736
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5737
    TAOS_RETURN(code);
×
5738
  }
5739
  return numOfRows;
12,699✔
5740
}
5741

5742
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
5743
  SSdb *pSdb = pMnode->pSdb;
×
5744
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
5745
}
×
5746

5747
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
20,734,320✔
5748
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
5749
  int32_t           code = 0;
20,734,320✔
5750
  int32_t           lino = 0;
20,734,320✔
5751
  int32_t           rspLen = 0;
20,734,726✔
5752
  void             *pRsp = NULL;
20,734,726✔
5753
  SUserAuthBatchRsp batchRsp = {0};
20,734,726✔
5754

5755
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
20,734,726✔
5756
  if (batchRsp.pArray == NULL) {
20,734,726✔
5757
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
5758
  }
5759
  int64_t now = taosGetTimestampMs();
20,734,726✔
5760
  for (int32_t i = 0; i < numOfUses; ++i) {
41,849,144✔
5761
    SUserObj *pUser = NULL;
21,114,418✔
5762
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
21,114,418✔
5763
    if (pUser == NULL) {
21,114,418✔
5764
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
5,079✔
5765
        SGetUserAuthRsp rsp = {.dropped = 1};
5,079✔
5766
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
5,079✔
5767
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
10,158✔
5768
      }
5769
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
5,079✔
5770
      code = 0;
5,079✔
5771
      continue;
5,079✔
5772
    }
5773

5774
    pUsers[i].version = ntohl(pUsers[i].version);
21,109,339✔
5775
    if ((pUser->authVersion <= pUsers[i].version) && (ipWhiteListVer == pMnode->ipWhiteVer) &&
21,108,933✔
5776
        !mndNeedRetrieveRole(pUser)) {
×
5777
      mndReleaseUser(pMnode, pUser);
×
5778
      continue;
×
5779
    }
5780

5781
    SGetUserAuthRsp rsp = {0};
21,109,339✔
5782
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
21,109,339✔
5783
    if (code) {
21,109,339✔
5784
      mndReleaseUser(pMnode, pUser);
×
5785
      tFreeSGetUserAuthRsp(&rsp);
×
5786
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
5787
    }
5788

5789
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
42,218,678✔
5790
      code = terrno;
×
5791
      mndReleaseUser(pMnode, pUser);
×
5792
      tFreeSGetUserAuthRsp(&rsp);
×
5793
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
5794
    }
5795
    pUser->lastRoleRetrieve = now;  // update user's last retrieve time
21,109,339✔
5796
    mndReleaseUser(pMnode, pUser);
21,109,339✔
5797
  }
5798

5799
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
20,734,726✔
5800
    *ppRsp = NULL;
×
5801
    *pRspLen = 0;
×
5802

5803
    tFreeSUserAuthBatchRsp(&batchRsp);
×
5804
    return 0;
×
5805
  }
5806

5807
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
20,734,726✔
5808
  if (rspLen < 0) {
20,734,644✔
5809
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
5810
  }
5811
  pRsp = taosMemoryMalloc(rspLen);
20,734,644✔
5812
  if (pRsp == NULL) {
20,734,305✔
5813
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
5814
  }
5815
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
20,734,305✔
5816
  if (rspLen < 0) {
20,734,726✔
5817
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
5818
  }
5819
_OVER:
20,734,726✔
5820
  tFreeSUserAuthBatchRsp(&batchRsp);
20,734,726✔
5821
  if (code < 0) {
20,734,073✔
5822
    for (int32_t i = 0; i < numOfUses; ++i) {
×
5823
      SUserObj *pUser = NULL;
×
5824
      if (mndAcquireUser(pMnode, pUsers[i].user, &pUser) != 0) {
×
5825
        continue;
×
5826
      }
5827
      pUser->lastRoleRetrieve = 0;  // reset last retrieve time on error
×
5828
      mndReleaseUser(pMnode, pUser);
×
5829
    }
5830
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5831
    taosMemoryFreeClear(pRsp);
×
5832
    rspLen = 0;
×
5833
  }
5834
  *ppRsp = pRsp;
20,734,073✔
5835
  *pRspLen = rspLen;
20,733,288✔
5836

5837
  TAOS_RETURN(code);
20,734,313✔
5838
}
5839

5840
int32_t mndUserDropRole(SMnode *pMnode, STrans *pTrans, SRoleObj *pObj) {
×
5841
  int32_t   code = 0, lino = 0;
×
5842
  SSdb     *pSdb = pMnode->pSdb;
×
5843
  SUserObj *pUser = NULL;
×
5844
  void     *pIter = NULL;
×
5845

5846
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
×
5847
    SHashObj *pRole = taosHashGet(pUser->roles, pObj->name, strlen(pObj->name) + 1);
×
5848
    if (!pRole) {
×
5849
      sdbRelease(pSdb, pUser);
×
5850
      pUser = NULL;
×
5851
      continue;
×
5852
    }
5853

5854
    SUserObj newUser = {0};
×
5855
    TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
×
5856
    code = taosHashRemove(newUser.roles, pObj->name, strlen(pObj->name) + 1);
×
5857
    if (code == TSDB_CODE_NOT_FOUND) {
×
5858
      sdbRelease(pSdb, pUser);
×
5859
      pUser = NULL;
×
5860
      mndUserFreeObj(&newUser);
×
5861
      continue;
×
5862
    }
5863
    if (code != 0) {
×
5864
      mndUserFreeObj(&newUser);
×
5865
      TAOS_CHECK_EXIT(code);
×
5866
    }
5867
    SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
5868
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
5869
      mndUserFreeObj(&newUser);
×
5870
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
5871
    }
5872
    if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY))) {
×
5873
      mndUserFreeObj(&newUser);
×
5874
      TAOS_CHECK_EXIT(code);
×
5875
    }
5876
    sdbRelease(pSdb, pUser);
×
5877
    pUser = NULL;
×
5878
    mndUserFreeObj(&newUser);
×
5879
  }
5880
_exit:
×
5881
  if (pIter) sdbCancelFetch(pSdb, pIter);
×
5882
  if (pUser) sdbRelease(pSdb, pUser);
×
5883
  if (code < 0) {
×
5884
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
5885
  }
5886
  TAOS_RETURN(code);
×
5887
}
5888

5889
static int32_t mndUserPrivHashRemoveDb(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, bool *found) {
3,597,080✔
5890
  void *pVal = NULL;
3,597,080✔
5891
  while ((pVal = taosHashIterate(pHash, pVal))) {
3,780,742✔
5892
    size_t klen = 0;
183,662✔
5893
    char  *key = taosHashGetKey(pVal, &klen);
183,662✔
5894
    if (key && privDbKeyMatch(key, dbFName, dbFNameLen)) {
367,324✔
5895
      TAOS_CHECK_RETURN(taosHashRemove(pHash, key, klen));
10,645✔
5896
      if (found) *found = true;
10,645✔
5897
    }
5898
  }
5899
  TAOS_RETURN(0);
3,597,080✔
5900
}
5901

5902
static int32_t mndUserRemoveDbPrivsImpl(SUserObj *pUser, const char *key, int32_t keyLen, bool *pFound) {
719,416✔
5903
  bool found = false;
719,416✔
5904
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->objPrivs, (char *)key, keyLen, found ? NULL : &found));
719,416✔
5905
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->selectTbs, (char *)key, keyLen, found ? NULL : &found));
719,416✔
5906
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->insertTbs, (char *)key, keyLen, found ? NULL : &found));
719,416✔
5907
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->updateTbs, (char *)key, keyLen, found ? NULL : &found));
719,416✔
5908
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pUser->deleteTbs, (char *)key, keyLen, found ? NULL : &found));
719,416✔
5909
  if (taosHashGet(pUser->ownedDbs, key, keyLen)) {
719,416✔
5910
    TAOS_CHECK_RETURN(taosHashRemove(pUser->ownedDbs, (char *)key, keyLen));
×
5911
    if (!found) found = true;
×
5912
  }
5913
  if (pFound) *pFound = found;
719,416✔
5914
  TAOS_RETURN(0);
719,416✔
5915
}
5916

5917
static int32_t mndUserRemoveDbPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
645,331✔
5918
                                    SSHashObj **ppUsers) {
5919
  int32_t    code = 0, lino = 0;
645,331✔
5920
  SSdb      *pSdb = pMnode->pSdb;
645,331✔
5921
  void      *pIter = NULL;
645,331✔
5922
  SUserObj  *pUser = NULL;
645,331✔
5923
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
645,331✔
5924
  bool       output = (ppUsers != NULL);
645,331✔
5925
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
1,364,466✔
5926
    bool found = false;
719,135✔
5927
    TAOS_CHECK_EXIT(mndUserRemoveDbPrivsImpl(pUser, key, keyLen, &found));
719,135✔
5928
    if (!found) {
719,135✔
5929
      sdbRelease(pSdb, pUser);
711,628✔
5930
      pUser = NULL;
711,628✔
5931
      continue;
711,628✔
5932
    }
5933

5934
    if (output) {
7,507✔
5935
      if (!pUsers) {
562✔
5936
        TSDB_CHECK_NULL(pUsers = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
281✔
5937
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
5938
        tSimpleHashSetFreeFp(pUsers, (_hash_free_fn_t)mndUserFreeObj);
281✔
5939
        *ppUsers = pUsers;
281✔
5940
      }
5941
      void   *pVal = NULL;
562✔
5942
      int32_t userLen = strlen(pUser->name) + 1;
562✔
5943
      if ((pVal = tSimpleHashGet(pUsers, pUser->name, userLen)) != NULL) {
562✔
5944
        TAOS_CHECK_EXIT(mndUserRemoveDbPrivsImpl((SUserObj *)pVal, key, keyLen, NULL));
281✔
5945
      } else {
5946
        SUserObj newUser = {0};
281✔
5947
        if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
281✔
5948
          mndUserFreeObj(&newUser);
×
5949
          TAOS_CHECK_EXIT(code);
×
5950
        }
5951
        if ((code = tSimpleHashPut(pUsers, pUser->name, userLen, &newUser, sizeof(SUserObj)))) {
281✔
5952
          mndUserFreeObj(&newUser);
×
5953
          TAOS_CHECK_EXIT(code);
×
5954
        }
5955
      }
5956
    } else {
5957
      int64_t now = taosGetTimestampMs();
6,945✔
5958
      taosWLockLatch(&pUser->lock);
6,945✔
5959
      pUser->updateTime = now;
6,945✔
5960
      ++pUser->authVersion;
6,945✔
5961
      taosWUnLockLatch(&pUser->lock);
6,945✔
5962

5963
      SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
6,945✔
5964
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
6,945✔
5965
        TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
5966
      }
5967
      TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
6,945✔
5968
    }
5969

5970
    sdbRelease(pSdb, pUser);
7,507✔
5971
    pUser = NULL;
7,507✔
5972
  }
5973
_exit:
645,331✔
5974
  if (pUser != NULL) sdbRelease(pSdb, pUser);
645,331✔
5975
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
645,331✔
5976
  TAOS_RETURN(code);
645,331✔
5977
}
5978

5979
static int32_t mndRoleRemoveDbPrivsImpl(SRoleObj *pRole, const char *key, int32_t keyLen, bool *pFound) {
×
5980
  bool found = false;
×
5981
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->objPrivs, (char *)key, keyLen, found ? NULL : &found));
×
5982
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->selectTbs, (char *)key, keyLen, found ? NULL : &found));
×
5983
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->insertTbs, (char *)key, keyLen, found ? NULL : &found));
×
5984
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->updateTbs, (char *)key, keyLen, found ? NULL : &found));
×
5985
  TAOS_CHECK_RETURN(mndUserPrivHashRemoveDb(pRole->deleteTbs, (char *)key, keyLen, found ? NULL : &found));
×
5986
  if (pFound) *pFound = found;
×
5987
  TAOS_RETURN(0);
×
5988
}
5989

5990
static int32_t mndRoleRemoveDbPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
×
5991
                                    SSHashObj **ppRoles) {
5992
  int32_t    code = 0, lino = 0;
×
5993
  SSdb      *pSdb = pMnode->pSdb;
×
5994
  void      *pIter = NULL;
×
5995
  SRoleObj  *pRole = NULL;
×
5996
  SSHashObj *pRoles = ppRoles ? *ppRoles : NULL;
×
5997
  bool       output = (ppRoles != NULL);
×
5998
  while ((pIter = sdbFetch(pSdb, SDB_ROLE, pIter, (void **)&pRole))) {
×
5999
    bool found = false;
×
6000
    TAOS_CHECK_EXIT(mndRoleRemoveDbPrivsImpl(pRole, key, keyLen, &found));
×
6001
    if (!found) {
×
6002
      sdbRelease(pSdb, pRole);
×
6003
      pRole = NULL;
×
6004
      continue;
×
6005
    }
6006

6007
    if (output) {
×
6008
      if (!pRoles) {
×
6009
        TSDB_CHECK_NULL(pRoles = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
×
6010
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
6011
        tSimpleHashSetFreeFp(pRoles, (_hash_free_fn_t)mndRoleFreeObj);
×
6012
        *ppRoles = pRoles;
×
6013
      }
6014
      void   *pVal = NULL;
×
6015
      int32_t roleLen = strlen(pRole->name) + 1;
×
6016
      if ((pVal = tSimpleHashGet(pRoles, pRole->name, roleLen)) != NULL) {
×
6017
        TAOS_CHECK_EXIT(mndRoleRemoveDbPrivsImpl((SRoleObj *)pVal, key, keyLen, NULL));
×
6018
      } else {
6019
        SRoleObj newRole = {0};
×
6020
        if ((code = mndRoleDupObj(pRole, &newRole)) != 0) {
×
6021
          mndRoleFreeObj(&newRole);
×
6022
          TAOS_CHECK_EXIT(code);
×
6023
        }
6024
        if ((code = tSimpleHashPut(pRoles, pRole->name, roleLen, &newRole, sizeof(SRoleObj)))) {
×
6025
          mndRoleFreeObj(&newRole);
×
6026
          TAOS_CHECK_EXIT(code);
×
6027
        }
6028
      }
6029
    } else {
6030
      int64_t now = taosGetTimestampMs();
×
6031
      taosWLockLatch(&pRole->lock);
×
6032
      pRole->updateTime = now;
×
6033
      ++pRole->version;
×
6034
      taosWUnLockLatch(&pRole->lock);
×
6035

6036
      SSdbRaw *pCommitRaw = mndRoleActionEncode(pRole);
×
6037
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
6038
        TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6039
      }
6040
      TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
6041
    }
6042

6043
    sdbRelease(pSdb, pRole);
×
6044
    pRole = NULL;
×
6045
  }
6046
_exit:
×
6047
  if (pRole != NULL) sdbRelease(pSdb, pRole);
×
6048
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
×
6049
  TAOS_RETURN(code);
×
6050
}
6051

6052
int32_t mndPrincipalRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers, SSHashObj **ppRoles) {
645,331✔
6053
  TAOS_RETURN(mndUserRemoveDbPrivs(pMnode, pTrans, pDb->name, strlen(pDb->name), ppUsers));
645,331✔
6054
  return mndRoleRemoveDbPrivs(pMnode, pTrans, pDb->name, strlen(pDb->name), ppRoles);
6055
}
6056

6057
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
451,640✔
6058
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, stb, PRIV_OBJ_TBL);
451,640✔
6059
}
6060

6061
static int32_t mndUserRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
715,092✔
6062
                                     EPrivObjType objType) {
6063
  int32_t   code = 0, lino = 0;
715,092✔
6064
  SSdb     *pSdb = pMnode->pSdb;
715,092✔
6065
  void     *pIter = NULL;
715,092✔
6066
  SUserObj *pUser = NULL;
715,092✔
6067
  while ((pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser))) {
1,439,108✔
6068
    bool found = false;
724,016✔
6069
    if (taosHashGet(pUser->objPrivs, key, keyLen)) {
724,016✔
6070
      TAOS_CHECK_EXIT(taosHashRemove(pUser->objPrivs, key, keyLen));
2,697✔
6071
      found = true;
2,697✔
6072
    }
6073
    if (objType == PRIV_OBJ_TBL) {
724,016✔
6074
      SPrivTblPolicies *pTblPrivs = taosHashGet(pUser->selectTbs, key, keyLen);
451,974✔
6075
      if (pTblPrivs) {
451,974✔
6076
        TAOS_CHECK_EXIT(taosHashRemove(pUser->selectTbs, key, keyLen));
×
6077
        found = true;
×
6078
      }
6079
      pTblPrivs = taosHashGet(pUser->insertTbs, key, keyLen);
451,974✔
6080
      if (pTblPrivs) {
451,974✔
6081
        TAOS_CHECK_EXIT(taosHashRemove(pUser->insertTbs, key, keyLen));
×
6082
        found = true;
×
6083
      }
6084
      pTblPrivs = taosHashGet(pUser->updateTbs, key, keyLen);
451,974✔
6085
      if (pTblPrivs) {
451,974✔
6086
        TAOS_CHECK_EXIT(taosHashRemove(pUser->updateTbs, key, keyLen));
×
6087
        found = true;
×
6088
      }
6089
      pTblPrivs = taosHashGet(pUser->deleteTbs, key, keyLen);
451,974✔
6090
      if (pTblPrivs) {
451,974✔
6091
        TAOS_CHECK_EXIT(taosHashRemove(pUser->deleteTbs, key, keyLen));
×
6092
        found = true;
×
6093
      }
6094
    }
6095
    if (!found) {
724,016✔
6096
      sdbRelease(pSdb, pUser);
721,319✔
6097
      pUser = NULL;
721,319✔
6098
      continue;
721,319✔
6099
    }
6100

6101
    int64_t now = taosGetTimestampMs();
2,697✔
6102
    taosWLockLatch(&pUser->lock);
2,697✔
6103
    pUser->updateTime = now;
2,697✔
6104
    ++pUser->authVersion;
2,697✔
6105
    taosWUnLockLatch(&pUser->lock);
2,697✔
6106

6107
    SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
2,697✔
6108
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
2,697✔
6109
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6110
    }
6111
    TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
2,697✔
6112

6113
    sdbRelease(pSdb, pUser);
2,697✔
6114
    pUser = NULL;
2,697✔
6115
  }
6116
_exit:
715,092✔
6117
  if (pUser != NULL) sdbRelease(pSdb, pUser);
715,092✔
6118
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
715,092✔
6119
  TAOS_RETURN(code);
715,092✔
6120
}
6121

6122
static int32_t mndRoleRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, const char *key, int32_t keyLen,
715,092✔
6123
                                     EPrivObjType objType) {
6124
  int32_t   code = 0, lino = 0;
715,092✔
6125
  SSdb     *pSdb = pMnode->pSdb;
715,092✔
6126
  void     *pIter = NULL;
715,092✔
6127
  SRoleObj *pRole = NULL;
715,092✔
6128
  while ((pIter = sdbFetch(pSdb, SDB_ROLE, pIter, (void **)&pRole))) {
5,005,644✔
6129
    // inherit system roles have no direct privileges
6130
    if (taosStrncasecmp(pRole->name, "sys", 3) == 0) {
4,290,552✔
6131
      sdbRelease(pSdb, pRole);
4,290,552✔
6132
      pRole = NULL;
4,290,552✔
6133
      continue;
4,290,552✔
6134
    }
6135
    bool found = false;
×
6136
    if (taosHashGet(pRole->objPrivs, key, keyLen)) {
×
6137
      TAOS_CHECK_EXIT(taosHashRemove(pRole->objPrivs, key, keyLen));
×
6138
      found = true;
×
6139
    }
6140

6141
    if (objType == PRIV_OBJ_TBL) {
×
6142
      SPrivTblPolicies *pTblPrivs = taosHashGet(pRole->selectTbs, key, keyLen);
×
6143
      if (pTblPrivs) {
×
6144
        TAOS_CHECK_EXIT(taosHashRemove(pRole->selectTbs, key, keyLen));
×
6145
        found = true;
×
6146
      }
6147
      pTblPrivs = taosHashGet(pRole->insertTbs, key, keyLen);
×
6148
      if (pTblPrivs) {
×
6149
        TAOS_CHECK_EXIT(taosHashRemove(pRole->insertTbs, key, keyLen));
×
6150
        found = true;
×
6151
      }
6152
      pTblPrivs = taosHashGet(pRole->updateTbs, key, keyLen);
×
6153
      if (pTblPrivs) {
×
6154
        TAOS_CHECK_EXIT(taosHashRemove(pRole->updateTbs, key, keyLen));
×
6155
        found = true;
×
6156
      }
6157
      pTblPrivs = taosHashGet(pRole->deleteTbs, key, keyLen);
×
6158
      if (pTblPrivs) {
×
6159
        TAOS_CHECK_EXIT(taosHashRemove(pRole->deleteTbs, key, keyLen));
×
6160
        found = true;
×
6161
      }
6162
    }
6163
    if (!found) {
×
6164
      sdbRelease(pSdb, pRole);
×
6165
      pRole = NULL;
×
6166
      continue;
×
6167
    }
6168
    int64_t now = taosGetTimestampMs();
×
6169
    taosWLockLatch(&pRole->lock);
×
6170
    pRole->updateTime = now;
×
6171
    ++pRole->version;
×
6172
    taosWUnLockLatch(&pRole->lock);
×
6173

6174
    SSdbRaw *pCommitRaw = mndRoleActionEncode(pRole);
×
6175
    if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
6176
      TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
×
6177
    }
6178
    TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
6179

6180
    sdbRelease(pSdb, pRole);
×
6181
    pRole = NULL;
×
6182
  }
6183
_exit:
715,092✔
6184
  if (pRole != NULL) sdbRelease(pSdb, pRole);
715,092✔
6185
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
715,092✔
6186
  TAOS_RETURN(code);
715,092✔
6187
}
6188

6189
static int32_t mndPrincipalRemoveObjPrivs(SMnode *pMnode, STrans *pTrans, char *objFName, EPrivObjType objType) {
715,092✔
6190
  int32_t code = 0;
715,092✔
6191
  char    key[TSDB_PRIV_MAX_KEY_LEN] = {0};
715,092✔
6192
  int32_t keyLen = snprintf(key, sizeof(key), "%d.%s", objType, objFName) + 1;
715,092✔
6193
  TAOS_CHECK_RETURN(mndUserRemoveObjPrivs(pMnode, pTrans, key, keyLen, objType));
715,092✔
6194
  TAOS_CHECK_RETURN(mndRoleRemoveObjPrivs(pMnode, pTrans, key, keyLen, objType));
715,092✔
6195
  TAOS_RETURN(code);
715,092✔
6196
}
6197

6198
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
162,740✔
6199
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, view, PRIV_OBJ_VIEW);
162,740✔
6200
}
6201

6202
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
100,712✔
6203
  return mndPrincipalRemoveObjPrivs(pMnode, pTrans, topic, PRIV_OBJ_TOPIC);
100,712✔
6204
}
6205

6206
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
6207
  // ver = 0, disable ip white list
6208
  // ver > 0, enable ip white list
6209
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
6210
}
6211

6212
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
6213
  // ver = 0, disable datetime white list
6214
  // ver > 0, enable datetime white list
6215
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
6216
}
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