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

taosdata / TDengine / #4885

15 Dec 2025 03:26AM UTC coverage: 65.258% (+4.6%) from 60.617%
#4885

push

travis-ci

web-flow
feat(tmq): [TS-6379]remove limition for table operation in tmq  (#33834)

872 of 1074 new or added lines in 16 files covered. (81.19%)

659 existing lines in 92 files now uncovered.

177890 of 272597 relevant lines covered (65.26%)

103732965.73 hits per line

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

72.34
/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 "mndUser.h"
23
#include "audit.h"
24
#include "mndDb.h"
25
#include "mndPrivilege.h"
26
#include "mndShow.h"
27
#include "mndStb.h"
28
#include "mndTopic.h"
29
#include "mndTrans.h"
30
#include "tbase64.h"
31
#include "totp.h"
32

33
// clang-format on
34

35
#define USER_VER_SUPPORT_WHITELIST           5
36
#define USER_VER_SUPPORT_WHITELIT_DUAL_STACK 7
37
#define USER_VER_SUPPORT_ADVANCED_SECURITY   8
38
#define USER_VER_NUMBER                      USER_VER_SUPPORT_ADVANCED_SECURITY 
39
#define USER_RESERVE_SIZE                    63
40

41
#define BIT_FLAG_MASK(n)              (1 << n)
42
#define BIT_FLAG_SET_MASK(val, mask)  ((val) |= (mask))
43
#define BIT_FLAG_TEST_MASK(val, mask) (((val) & (mask)) != 0)
44

45
#define PRIVILEGE_TYPE_ALL       BIT_FLAG_MASK(0)
46
#define PRIVILEGE_TYPE_READ      BIT_FLAG_MASK(1)
47
#define PRIVILEGE_TYPE_WRITE     BIT_FLAG_MASK(2)
48
#define PRIVILEGE_TYPE_SUBSCRIBE BIT_FLAG_MASK(3)
49
#define PRIVILEGE_TYPE_ALTER     BIT_FLAG_MASK(4)
50

51
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
52
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
53

54
#define ALTER_USER_ALL_PRIV(_priv) (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
55
#define ALTER_USER_READ_PRIV(_priv) \
56
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_READ) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
57
#define ALTER_USER_WRITE_PRIV(_priv) \
58
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_WRITE) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
59
#define ALTER_USER_ALTER_PRIV(_priv) \
60
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALTER) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
61
#define ALTER_USER_SUBSCRIBE_PRIV(_priv) (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_SUBSCRIBE))
62

63
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
64
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
65

66
#define ALTER_USER_ADD_READ_DB_PRIV(_type, _priv, _tbname) \
67
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
68
#define ALTER_USER_DEL_READ_DB_PRIV(_type, _priv, _tbname) \
69
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
70
#define ALTER_USER_ADD_WRITE_DB_PRIV(_type, _priv, _tbname) \
71
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
72
#define ALTER_USER_DEL_WRITE_DB_PRIV(_type, _priv, _tbname) \
73
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
74
#define ALTER_USER_ADD_ALTER_DB_PRIV(_type, _priv, _tbname) \
75
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
76
#define ALTER_USER_DEL_ALTER_DB_PRIV(_type, _priv, _tbname) \
77
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
78
#define ALTER_USER_ADD_ALL_DB_PRIV(_type, _priv, _tbname) \
79
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
80
#define ALTER_USER_DEL_ALL_DB_PRIV(_type, _priv, _tbname) \
81
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
82

83
#define ALTER_USER_ADD_READ_TB_PRIV(_type, _priv, _tbname) \
84
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
85
#define ALTER_USER_DEL_READ_TB_PRIV(_type, _priv, _tbname) \
86
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
87
#define ALTER_USER_ADD_WRITE_TB_PRIV(_type, _priv, _tbname) \
88
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
89
#define ALTER_USER_DEL_WRITE_TB_PRIV(_type, _priv, _tbname) \
90
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
91
#define ALTER_USER_ADD_ALTER_TB_PRIV(_type, _priv, _tbname) \
92
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
93
#define ALTER_USER_DEL_ALTER_TB_PRIV(_type, _priv, _tbname) \
94
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
95
#define ALTER_USER_ADD_ALL_TB_PRIV(_type, _priv, _tbname) \
96
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
97
#define ALTER_USER_DEL_ALL_TB_PRIV(_type, _priv, _tbname) \
98
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
99

100
#define ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
101
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
102
#define ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
103
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
104

105
static void generateSalt(char *salt, size_t len);
106

107
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList);
108
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppWhiteList, bool supportNeg);
109

110
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b);
111
static bool isIpRangeEqual(SIpRange *a, SIpRange *b);
112

113
#define MND_MAX_USER_IP_RANGE   (TSDB_PRIVILEDGE_HOST_LEN / 24)
114
#define MND_MAX_USER_TIME_RANGE 2048
115

116
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
117
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
118
static int32_t  mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
119
static int32_t  mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
120
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
121
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
122
static int32_t  mndProcessCreateUserReq(SRpcMsg *pReq);
123
static int32_t  mndProcessAlterUserReq(SRpcMsg *pReq);
124
static int32_t  mndProcessDropUserReq(SRpcMsg *pReq);
125
static int32_t  mndProcessGetUserAuthReq(SRpcMsg *pReq);
126
static int32_t  mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
127
static int32_t  mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
128
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
129
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
130
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
131

132
static int32_t  mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq);
133
static int32_t  mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq);
134
static int32_t  mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq);
135
static int32_t  mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq);
136

137
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList);
138
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList);
139

140

141
typedef struct {
142
  SIpWhiteListDual   *wlIp;
143
  SDateTimeWhiteList *wlTime;
144
  SLoginInfo          loginInfo;
145
} SCachedUserInfo;
146

147
typedef struct {
148
  SHashObj      *users;  // key: user, value: SCachedUserInfo*
149
  int64_t        verIp;
150
  int64_t        verTime;
151
  TdThreadRwlock rw;
152
} SUserCache;
153

154
static SUserCache userCache;
155

156

157
static int32_t userCacheInit() {
504,607✔
158
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
504,607✔
159

160
  SHashObj *users = taosHashInit(8, hashFn, 1, HASH_ENTRY_LOCK);
504,607✔
161
  if (users == NULL) {
504,607✔
162
    TAOS_RETURN(terrno);
×
163
  }
164

165
  userCache.users = users;
504,607✔
166
  userCache.verIp = 0;
504,607✔
167
  userCache.verTime = 0;
504,607✔
168

169
  (void)taosThreadRwlockInit(&userCache.rw, NULL);
504,607✔
170
  TAOS_RETURN(0);
504,607✔
171
}
172

173

174

175
static void userCacheCleanup() {
504,489✔
176
  if (userCache.users == NULL) {
504,489✔
177
    return;
×
178
  }
179

180
  void *pIter = taosHashIterate(userCache.users, NULL);
504,489✔
181
  while (pIter) {
1,018,944✔
182
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
514,455✔
183
    if (pInfo != NULL) {
514,455✔
184
      taosMemoryFree(pInfo->wlIp);
514,455✔
185
      taosMemoryFree(pInfo->wlTime);
514,455✔
186
      taosMemoryFree(pInfo);
514,455✔
187
    }
188
    pIter = taosHashIterate(userCache.users, pIter);
514,455✔
189
  }
190
  taosHashCleanup(userCache.users);
504,489✔
191

192
  (void)taosThreadRwlockDestroy(&userCache.rw);
504,489✔
193
}
194

195

196

197
static void userCacheRemoveUser(const char *user) {
59,313✔
198
  size_t userLen = strlen(user);
59,313✔
199

200
  (void)taosThreadRwlockWrlock(&userCache.rw);
59,313✔
201

202
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
59,313✔
203
  if (ppInfo != NULL) {
59,313✔
204
    if (*ppInfo != NULL) {
59,313✔
205
      taosMemoryFree((*ppInfo)->wlIp);
59,313✔
206
      taosMemoryFree((*ppInfo)->wlTime);
59,313✔
207
      taosMemoryFree(*ppInfo);
59,313✔
208
    }
209
    if (taosHashRemove(userCache.users, user, userLen) != 0) {
59,313✔
210
      mDebug("failed to remove user %s from user cache", user);
×
211
    }
212
    userCache.verIp++;
59,313✔
213
    userCache.verTime++;
59,313✔
214
  }
215

216
  (void)taosThreadRwlockUnlock(&userCache.rw);
59,313✔
217
}
59,313✔
218

219

220

221
static void userCacheResetLoginInfo(const char *user) {
1,339✔
222
  size_t userLen = strlen(user);
1,339✔
223

224
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,339✔
225

226
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
1,339✔
227
  if (ppInfo != NULL && *ppInfo != NULL) {
1,339✔
228
    (*ppInfo)->loginInfo.lastLoginTime = taosGetTimestampSec();
1,339✔
229
    (*ppInfo)->loginInfo.failedLoginCount = 0;
1,339✔
230
    (*ppInfo)->loginInfo.lastFailedLoginTime = 0;
1,339✔
231
  }
232

233
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,339✔
234
}
1,339✔
235

236

237

238
static SCachedUserInfo* getCachedUserInfo(const char* user) {
4,537,003✔
239
  size_t userLen = strlen(user);
4,537,003✔
240
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
4,537,003✔
241
  if (ppInfo != NULL) {
4,537,003✔
242
    return *ppInfo;
3,963,235✔
243
  }
244

245
  SCachedUserInfo  *pInfo = (SCachedUserInfo *)taosMemoryCalloc(1, sizeof(SCachedUserInfo));
573,768✔
246
  if (pInfo == NULL) {
573,768✔
247
    return NULL;
×
248
  }
249

250
  if (taosHashPut(userCache.users, user, userLen, &pInfo, sizeof(pInfo)) != 0) {
573,768✔
251
    taosMemoryFree(pInfo);
×
252
    return NULL;
×
253
  }
254

255
  return pInfo;
573,768✔
256
}
257

258

259

260
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
3,003,290✔
261
  size_t userLen = strlen(user);
3,003,290✔
262

263
  (void)taosThreadRwlockRdlock(&userCache.rw);
3,003,290✔
264

265
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
3,003,767✔
266
  if (ppInfo != NULL && *ppInfo != NULL) {
3,003,513✔
267
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,682,902✔
268
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,682,845✔
269
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,682,622✔
270
  } else {
271
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
320,611✔
272
    pLoginInfo->failedLoginCount = 0;
320,611✔
273
    pLoginInfo->lastFailedLoginTime = 0;
320,611✔
274
  }
275

276
  (void)taosThreadRwlockUnlock(&userCache.rw);
3,003,544✔
277

278
  if (pLoginInfo->lastLoginTime == 0) {
3,003,456✔
279
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
48,038✔
280
  }
281
}
3,003,477✔
282

283

284

285
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
3,003,043✔
286
  size_t userLen = strlen(user);
3,003,043✔
287

288
  (void)taosThreadRwlockWrlock(&userCache.rw);
3,003,043✔
289

290
  SCachedUserInfo  *pInfo = getCachedUserInfo(user);
3,003,767✔
291
  if (pInfo != NULL) {
3,003,767✔
292
    pInfo->loginInfo.lastLoginTime = pLoginInfo->lastLoginTime;
3,003,767✔
293
    pInfo->loginInfo.failedLoginCount = pLoginInfo->failedLoginCount;
3,003,767✔
294
    pInfo->loginInfo.lastFailedLoginTime = pLoginInfo->lastFailedLoginTime;
3,003,767✔
295
  }
296

297
  (void)taosThreadRwlockUnlock(&userCache.rw);
3,003,767✔
298
}
3,003,767✔
299

300

301

302
static bool isDateTimeWhiteListEqual(SDateTimeWhiteList *a, SDateTimeWhiteList *b) {
1,016,262✔
303
  if (a == NULL && b == NULL) {
1,016,262✔
304
    return true;
×
305
  }
306

307
  if (a == NULL || b == NULL) {
1,016,262✔
308
    return false;
100,841✔
309
  }
310

311
  if (a->num != b->num) {
915,421✔
312
    return false;
×
313
  }
314

315
  for (int i = 0; i < a->num; i++) {
915,421✔
316
    if (a->ranges[i].start != b->ranges[i].start ||
×
317
        a->ranges[i].duration != b->ranges[i].duration ||
×
318
        a->ranges[i].neg != b->ranges[i].neg ||
×
319
        a->ranges[i].absolute != b->ranges[i].absolute) {
×
320
      return false;
×
321
    }
322
  }
323

324
  return true;
915,421✔
325
}
326

327

328

329
static int32_t userCacheUpdateWhiteList(SMnode* pMnode, SUserObj* pUser) {
1,016,262✔
330
  int32_t code = 0, lino = 0;
1,016,262✔
331

332
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,016,262✔
333

334
  SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
1,016,262✔
335
  if (pInfo == NULL) {
1,016,262✔
336
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
337
  }
338

339
  if (!isIpWhiteListEqual(pInfo->wlIp, pUser->pIpWhiteListDual)) {
1,016,262✔
340
    SIpWhiteListDual *p = cloneIpWhiteList(pUser->pIpWhiteListDual);
101,200✔
341
    if (p == NULL) {
101,200✔
342
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
343
    }
344
    taosMemoryFree(pInfo->wlIp);
101,200✔
345
    pInfo->wlIp = p;
101,200✔
346
    userCache.verIp++;
101,200✔
347
  }
348

349
  if (!isDateTimeWhiteListEqual(pInfo->wlTime, pUser->pTimeWhiteList)) {
1,016,262✔
350
    SDateTimeWhiteList *p = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
100,841✔
351
    if (p == NULL) {
100,841✔
352
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
353
    }
354
    taosMemoryFree(pInfo->wlTime);
100,841✔
355
    pInfo->wlTime = p;
100,841✔
356
    userCache.verTime++;
100,841✔
357
  }
358

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

367

368

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

372
  SSdb     *pSdb = pMnode->pSdb;
612,987✔
373
  void     *pIter = NULL;
612,987✔
374
  while (1) {
264,369✔
375
    SUserObj *pUser = NULL;
877,356✔
376
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
877,356✔
377
    if (pIter == NULL) {
877,356✔
378
      break;
612,987✔
379
    }
380

381
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
264,369✔
382
    if (pInfo == NULL) {
264,369✔
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);
264,369✔
389
    if (wl == NULL) {
264,369✔
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);
264,369✔
396
    pInfo->wlIp = wl;
264,369✔
397

398
    sdbRelease(pSdb, pUser);
264,369✔
399
  }
400

401
  userCache.verIp++;
612,987✔
402

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

410

411

412
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
39,252,074✔
413
  int64_t ver = 0;
39,252,074✔
414
  int32_t code = 0;
39,252,074✔
415

416
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
39,252,074✔
417
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,330✔
418

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

430
    (void)taosThreadRwlockUnlock(&userCache.rw);
13,330✔
431
  }
432

433
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
39,252,074✔
434
  return ver;
39,252,074✔
435
}
436

437

438

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

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

450
  TAOS_RETURN(code);
612,987✔
451
}
452

453

454

455
static int32_t userCacheRebuildTimeWhiteList(SMnode *pMnode) {
601,223✔
456
  int32_t   code = 0, lino = 0;
601,223✔
457

458
  SSdb     *pSdb = pMnode->pSdb;
601,223✔
459
  void     *pIter = NULL;
601,223✔
460
  while (1) {
252,605✔
461
    SUserObj *pUser = NULL;
853,828✔
462
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
853,828✔
463
    if (pIter == NULL) {
853,828✔
464
      break;
601,223✔
465
    }
466

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

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

481
    taosMemoryFree(pInfo->wlTime);
252,605✔
482
    pInfo->wlTime = wl;
252,605✔
483

484
    sdbRelease(pSdb, pUser);
252,605✔
485
  }
486

487
  userCache.verTime++;
601,223✔
488

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

496

497

498
int32_t mndRefreshUserDateTimeWhiteList(SMnode *pMnode) {
601,223✔
499
  int32_t code = 0;
601,223✔
500
  (void)taosThreadRwlockWrlock(&userCache.rw);
601,223✔
501

502
  if ((code = userCacheRebuildTimeWhiteList(pMnode)) != 0) {
601,223✔
503
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
504
    TAOS_RETURN(code);
×
505
  }
506
  userCache.verTime = taosGetTimestampMs();
601,223✔
507
  (void)taosThreadRwlockUnlock(&userCache.rw);
601,223✔
508

509
  TAOS_RETURN(code);
601,223✔
510
}
511

512

513

514
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
39,252,074✔
515
  int64_t ver = 0;
39,252,074✔
516
  int32_t code = 0;
39,252,074✔
517

518
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
39,252,074✔
519
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,330✔
520

521
    if (userCache.verIp == 0) {
13,330✔
522
      // get user and dnode datetime white list
523
      if ((code = userCacheRebuildTimeWhiteList(pMnode)) != 0) {
×
524
        (void)taosThreadRwlockUnlock(&userCache.rw);
×
525
        mError("%s failed to update datetime white list since %s", __func__, tstrerror(code));
×
526
        return ver;
×
527
      }
528
      userCache.verTime = taosGetTimestampMs();
×
529
    }
530
    ver = userCache.verTime;
13,330✔
531

532
    (void)taosThreadRwlockUnlock(&userCache.rw);
13,330✔
533
  }
534

535
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
39,252,074✔
536
  return ver;
39,252,074✔
537
}
538

539

540

541
int32_t mndInitUser(SMnode *pMnode) {
504,607✔
542
  TAOS_CHECK_RETURN(userCacheInit());
504,607✔
543

544
  SSdbTable table = {
504,607✔
545
      .sdbType = SDB_USER,
546
      .keyType = SDB_KEY_BINARY,
547
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
548
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
549
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
550
      .insertFp = (SdbInsertFp)mndUserActionInsert,
551
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
552
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
553
  };
554

555
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
504,607✔
556
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
504,607✔
557
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
504,607✔
558
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
504,607✔
559

560
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST, mndProcessGetUserIpWhiteListReq);
504,607✔
561
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST_DUAL, mndProcessGetUserIpWhiteListReq);
504,607✔
562
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST, mndProcessRetrieveIpWhiteListReq);
504,607✔
563
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL, mndProcessRetrieveIpWhiteListReq);
504,607✔
564
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_DATETIME_WHITELIST, mndProcessGetUserDateTimeWhiteListReq);
504,607✔
565
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_DATETIME_WHITELIST, mndProcessRetrieveDateTimeWhiteListReq);
504,607✔
566

567
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
504,607✔
568
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
504,607✔
569
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndRetrieveUsersFull);
504,607✔
570
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndCancelGetNextUser);
504,607✔
571
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
504,607✔
572
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
504,607✔
573
  return sdbSetTable(pMnode->pSdb, table);
504,607✔
574
}
575

576

577

578
void mndCleanupUser(SMnode *pMnode) {
504,489✔
579
  userCacheCleanup();
504,489✔
580
}
504,489✔
581

582

583

584
static bool isDefaultRange(SIpRange *pRange) {
×
585
  int32_t code = 0;
×
586
  int32_t lino = 0;
×
587

588
  SIpRange range4 = {0};
×
589
  SIpRange range6 = {0};
×
590

591
  code = createDefaultIp4Range(&range4);
×
592
  TSDB_CHECK_CODE(code, lino, _error);
×
593

594
  code = createDefaultIp6Range(&range6);
×
595
  TSDB_CHECK_CODE(code, lino, _error);
×
596

597
  if (isIpRangeEqual(pRange, &range4) || (isIpRangeEqual(pRange, &range6))) {
×
598
    return true;
×
599
  }
600
_error:
×
601
  return false;
×
602
};
603

604

605

606
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
53,114✔
607
  int32_t len = 0;
53,114✔
608
  for (int i = 0; i < num; i++) {
160,778✔
609
    SIpRange *pRange = &range[i];
107,664✔
610
    SIpAddr   addr = {0};
107,664✔
611
    int32_t code = tIpUintToStr(pRange, &addr);
107,664✔
612
    if (code != 0) {
107,664✔
NEW
613
      mError("%s failed to convert ip range to str, code: %d", __func__, code);
×
614
    }
615

616
    len += tsnprintf(buf + len, bufLen - len, "%c%s/%d, ", pRange->neg ? '-' : '+', IP_ADDR_STR(&addr), addr.mask);
107,664✔
617
  }
618
  if (len > 0) buf[len - 2] = 0;
53,114✔
619
  return len;
53,114✔
620
}
621

622

623

624
static bool isIpRangeEqual(SIpRange *a, SIpRange *b) {
1,830,124✔
625
  if (a->type != b->type || a->neg != b->neg) {
1,830,124✔
626
    return false;
×
627
  }
628

629
  if (a->type == 0) {
1,830,124✔
630
    SIpV4Range *a4 = &a->ipV4;
915,421✔
631
    SIpV4Range *b4 = &b->ipV4;
915,421✔
632
    return (a4->ip == b4->ip && a4->mask == b4->mask);
915,421✔
633
  }
634
  
635
  SIpV6Range *a6 = &a->ipV6;
914,703✔
636
  SIpV6Range *b6 = &b->ipV6;
914,703✔
637
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
914,703✔
638
}
639

640

641

642
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
1,016,262✔
643
  if (a == NULL && b == NULL) {
1,016,262✔
644
    return true;
×
645
  }
646
  
647
  if (a == NULL || b == NULL) {
1,016,262✔
648
    return false;
100,841✔
649
  }
650

651
  if (a->num != b->num) {
915,421✔
652
    return false;
359✔
653
  }
654
  for (int i = 0; i < a->num; i++) {
2,745,186✔
655
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,830,124✔
656
      return false;
×
657
    }
658
  }
659
  return true;
915,062✔
660
}
661

662

663
static int32_t compareIpRange(const void *a, const void *b, const void* arg) {
3,949✔
664
  SIpRange *ra = (SIpRange *)a;
3,949✔
665
  SIpRange *rb = (SIpRange *)b;
3,949✔
666

667
  if (ra->neg != rb->neg) {
3,949✔
668
    return (ra->neg) ? -1 : 1;
×
669
  }
670

671
  if (ra->type != rb->type) {
3,949✔
672
    return (ra->type == 0) ? -1 : 1;
×
673
  }
674

675
  if (ra->type == 0) {
3,949✔
676
    if (ra->ipV4.ip != rb->ipV4.ip) {
3,949✔
677
      return (ra->ipV4.ip < rb->ipV4.ip) ? -1 : 1;
3,231✔
678
    }
679
    return (ra->ipV4.mask < rb->ipV4.mask) ? -1 : 1;
718✔
680
  }
681

682
  if (ra->ipV6.addr[0] != rb->ipV6.addr[0]) {
×
683
    return (ra->ipV6.addr[0] < rb->ipV6.addr[0]) ? -1 : 1;
×
684
  }
685
  if (ra->ipV6.addr[1] != rb->ipV6.addr[1]) {
×
686
    return (ra->ipV6.addr[1] < rb->ipV6.addr[1]) ? -1 : 1;
×
687
  }
688
  return (ra->ipV6.mask < rb->ipV6.mask) ? -1 : 1;
×
689
}
690

691
static void sortIpWhiteList(SIpWhiteListDual *pList) {
1,798✔
692
  (void)taosqsort(pList->pIpRanges, pList->num, sizeof(SIpRange), NULL, compareIpRange);
1,798✔
693
}
1,798✔
694

695

696

697
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
53,114✔
698
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
53,114✔
699

700
  int64_t bufLen = pList->num * 128 + 8;
53,114✔
701
  *buf = taosMemoryCalloc(1, bufLen);
53,114✔
702
  if (*buf == NULL) {
53,114✔
703
    return 0;
×
704
  }
705

706
  if (pList->num == 0) {
53,114✔
707
    return tsnprintf(*buf, bufLen, "+ALL");
×
708
  }
709

710
  int32_t len = ipRangeListToStr(pList->pIpRanges, pList->num, *buf, bufLen - 2);
53,114✔
711
  if (len == 0) {
53,114✔
712
    taosMemoryFreeClear(*buf);
×
713
    return 0;
×
714
  }
715
  return len;
53,114✔
716
}
717

718

719

720
static int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, uint32_t *pLen) {
2,515,465✔
721
  int32_t  code = 0;
2,515,465✔
722
  int32_t  lino = 0;
2,515,465✔
723
  int32_t  tlen = 0;
2,515,465✔
724
  SEncoder encoder = {0};
2,515,465✔
725
  tEncoderInit(&encoder, buf, len);
2,515,465✔
726

727
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,515,465✔
728
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
5,030,930✔
729

730
  for (int i = 0; i < pList->num; i++) {
7,547,466✔
731
    SIpRange *pRange = &(pList->pIpRanges[i]);
5,032,001✔
732
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
5,032,001✔
733
  }
734

735
  tEndEncode(&encoder);
2,515,465✔
736

737
  tlen = encoder.pos;
2,515,465✔
738
_OVER:
2,515,465✔
739
  tEncoderClear(&encoder);
2,515,465✔
740
  if (code < 0) {
2,515,465✔
741
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
742
  }
743
  if (pLen) *pLen = tlen;
2,515,465✔
744
  TAOS_RETURN(code);
2,515,465✔
745
}
746

747
static int32_t tDerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, bool supportNeg) {
1,639,441✔
748
  int32_t  code = 0;
1,639,441✔
749
  int32_t  lino = 0;
1,639,441✔
750
  SDecoder decoder = {0};
1,639,441✔
751
  tDecoderInit(&decoder, buf, len);
1,639,441✔
752

753
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,639,441✔
754
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
3,278,882✔
755

756
  for (int i = 0; i < pList->num; i++) {
4,919,038✔
757
    SIpRange *pRange = &(pList->pIpRanges[i]);
3,279,597✔
758
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
3,279,597✔
759
  }
760

761
_OVER:
1,639,441✔
762
  tEndDecode(&decoder);
1,639,441✔
763
  tDecoderClear(&decoder);
1,639,441✔
764
  if (code < 0) {
1,639,441✔
765
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
766
  }
767
  TAOS_RETURN(code);
1,639,441✔
768
}
769

770
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList) {
×
771
  int32_t  code = 0;
×
772
  int32_t  lino = 0;
×
773
  SDecoder decoder = {0};
×
774
  tDecoderInit(&decoder, buf, len);
×
775

776
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
777
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
778

779
  for (int i = 0; i < pList->num; i++) {
×
780
    SIpV4Range *pIp4 = &(pList->pIpRange[i]);
×
781
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->ip), &lino, _OVER);
×
782
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->mask), &lino, _OVER);
×
783
  }
784

785
_OVER:
×
786
  tEndDecode(&decoder);
×
787
  tDecoderClear(&decoder);
×
788
  if (code < 0) {
×
789
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
790
  }
791
  TAOS_RETURN(code);
×
792
}
793

794
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppList, bool supportNeg) {
1,639,441✔
795
  int32_t           code = 0;
1,639,441✔
796
  int32_t           lino = 0;
1,639,441✔
797
  int32_t           num = 0;
1,639,441✔
798
  SIpWhiteListDual *p = NULL;
1,639,441✔
799
  SDecoder          decoder = {0};
1,639,441✔
800
  tDecoderInit(&decoder, buf, len);
1,639,441✔
801

802
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,639,441✔
803
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
1,639,441✔
804

805
  p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + num * sizeof(SIpRange));
1,639,441✔
806
  if (p == NULL) {
1,639,441✔
807
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
808
  }
809
  TAOS_CHECK_GOTO(tDerializeIpWhiteList(buf, len, p, supportNeg), &lino, _OVER);
1,639,441✔
810

811
_OVER:
1,639,441✔
812
  tEndDecode(&decoder);
1,639,441✔
813
  tDecoderClear(&decoder);
1,639,441✔
814
  if (code < 0) {
1,639,441✔
815
    taosMemoryFreeClear(p);
×
816
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
817
  }
818
  *ppList = p;
1,639,441✔
819
  TAOS_RETURN(code);
1,639,441✔
820
}
821

822
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList) {
×
823
  int32_t       code = 0;
×
824
  int32_t       lino = 0;
×
825
  int32_t       num = 0;
×
826
  SIpWhiteList *p = NULL;
×
827
  SDecoder      decoder = {0};
×
828
  tDecoderInit(&decoder, buf, len);
×
829

830
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
831
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
832

833
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
×
834
  if (p == NULL) {
×
835
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
836
  }
837
  TAOS_CHECK_GOTO(tDerializeIpWhileListFromOldVer(buf, len, p), &lino, _OVER);
×
838

839
_OVER:
×
840
  tEndDecode(&decoder);
×
841
  tDecoderClear(&decoder);
×
842
  if (code < 0) {
×
843
    taosMemoryFreeClear(p);
×
844
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
845
  }
846
  *ppList = p;
×
847
  TAOS_RETURN(code);
×
848
}
849

850
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList) {
424,808✔
851
  int32_t code = 0;
424,808✔
852
  int32_t lino = 0;
424,808✔
853
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
424,808✔
854
  if (*ppWhiteList == NULL) {
424,808✔
855
    TAOS_RETURN(terrno);
×
856
  }
857
  (*ppWhiteList)->num = 2;
424,808✔
858

859
  SIpRange v4 = {0};
424,808✔
860
  SIpRange v6 = {0};
424,808✔
861

862
#ifndef TD_ASTRA
863
  code = createDefaultIp4Range(&v4);
424,808✔
864
  TSDB_CHECK_CODE(code, lino, _error);
424,808✔
865

866
  code = createDefaultIp6Range(&v6);
424,808✔
867
  TSDB_CHECK_CODE(code, lino, _error);
424,808✔
868

869
#endif
870

871
_error:
424,808✔
872
  if (code != 0) {
424,808✔
873
    taosMemoryFree(*ppWhiteList);
×
874
    *ppWhiteList = NULL;
×
875
    mError("failed to create default ip white list at line %d since %s", __LINE__, tstrerror(code));
×
876
  } else {
877
    memcpy(&(*ppWhiteList)->pIpRanges[0], &v4, sizeof(SIpRange));
424,808✔
878
    memcpy(&(*ppWhiteList)->pIpRanges[1], &v6, sizeof(SIpRange));
424,808✔
879
  }
880
  return 0;
424,808✔
881
}
882

883

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

886
static int32_t convertTimeRangesToStr(SUserObj *pUser, char **buf) {
53,114✔
887
  int32_t bufLen = pUser->pTimeWhiteList->num * 32 + 8;
53,114✔
888
  *buf = taosMemoryCalloc(1, bufLen);
53,114✔
889
  if (*buf == NULL) {
53,114✔
890
    return 0;
×
891
  }
892

893
  int32_t pos = 0;
53,114✔
894
  if (pUser->pTimeWhiteList->num == 0) {
53,114✔
895
    pos += tsnprintf(*buf + pos, bufLen - pos, "+ALL");
53,114✔
896
    return pos;
53,114✔
897
  }
898

899
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
×
900
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
901
    int duration = range->duration / 60;
×
902

903
    if (range->absolute) {
×
904
      struct STm tm;
×
905
      (void)taosTs2Tm(range->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
×
906
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%04d-%02d-%02d %02d:%02d %dm, ", range->neg ? '-' : '+', tm.tm.tm_year + 1900, tm.tm.tm_mon + 1, tm.tm.tm_mday, tm.tm.tm_hour, tm.tm.tm_min, duration);
×
907
    } else {
908
      int day = range->start / 86400;
×
909
      int hour = (range->start % 86400) / 3600;
×
910
      int minute = (range->start % 3600) / 60;
×
911
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%s %02d:%02d %dm, ", range->neg ? '-' : '+', weekdays[day], hour, minute, duration);
×
912
    }
913
  }
914

915
  if (pos > 0) {
×
916
    (*buf)[pos - 2] = 0; // remove last ", "
×
917
  }
918

919
  return pos;
×
920
}
921

922

923
static int32_t compareDateTimeInterval(const void *a, const void *b, const void* arg) {
×
924
  SDateTimeWhiteListItem *pA = (SDateTimeWhiteListItem *)a;
×
925
  SDateTimeWhiteListItem *pB = (SDateTimeWhiteListItem *)b;
×
926

927
  if (pA->neg != pB->neg) {
×
928
    return pA->neg ? -1 : 1;
×
929
  }
930

931
  if (pA->absolute != pB->absolute) {
×
932
    return pA->absolute ? 1 : -1;
×
933
  }
934

935
  if (pA->start != pB->start) {
×
936
    return (pA->start < pB->start) ? -1 : 1;
×
937
  }
938

939
  if (pA->duration != pB->duration) {
×
940
    return (pA->duration < pB->duration) ? -1 : 1;
×
941
  }
942

943
  return 0;
×
944
}
945

946
static void sortTimeWhiteList(SDateTimeWhiteList *pList) {
×
947
  (void)taosqsort(pList->ranges, pList->num, sizeof(SDateTimeWhiteListItem), NULL, compareDateTimeInterval);
×
948
}
×
949

950

951

952

953
static void dropOldPasswords(SUserObj *pUser) {
4,154,906✔
954
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
4,154,906✔
955
    return;
4,103,970✔
956
  }
957

958
  int32_t reuseMax = pUser->passwordReuseMax;
50,936✔
959
  if (reuseMax == 0) {
50,936✔
960
    reuseMax = 1; // keep at least one password
47,316✔
961
  }
962

963
  int32_t now = taosGetTimestampSec();
50,936✔
964
  int32_t index = reuseMax;
50,936✔
965
  while(index < pUser->numOfPasswords) {
61,072✔
966
    SUserPassword *pPass = &pUser->passwords[index];
10,136✔
967
    if (now - pPass->setTime >= pUser->passwordReuseTime) {
10,136✔
968
      break;
×
969
    }
970
    index++;
10,136✔
971
  }
972

973
  if (index == pUser->numOfPasswords) {
50,936✔
974
    return;
50,936✔
975
  }
976
  pUser->numOfPasswords = index;
×
977
  // this is a shrink operation, no need to check return value
978
  pUser->passwords = taosMemoryRealloc(pUser->passwords, sizeof(SUserPassword) * pUser->numOfPasswords);
×
979
}
980

981

982

983

984
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
325,410✔
985
  int32_t  code = 0;
325,410✔
986
  int32_t  lino = 0;
325,410✔
987
  SUserObj userObj = {0};
325,410✔
988

989
  userObj.passwords = taosMemCalloc(1, sizeof(SUserPassword));
325,410✔
990
  if (userObj.passwords == NULL) {
325,410✔
991
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
992
  }
993
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.passwords[0].pass);
325,410✔
994
  userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
325,410✔
995
  if (tsiEncryptPassAlgorithm == DND_CA_SM4 && strlen(tsEncryptKey) > 0) {
325,410✔
996
    generateSalt(userObj.salt, sizeof(userObj.salt));
×
997
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino, _ERROR);
×
998
  }
999

1000
  userObj.passwords[0].setTime = taosGetTimestampSec();
325,410✔
1001
  userObj.numOfPasswords = 1;
325,410✔
1002

1003
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
325,410✔
1004
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
325,410✔
1005
  userObj.createdTime = taosGetTimestampMs();
325,410✔
1006
  userObj.updateTime = userObj.createdTime;
325,410✔
1007
  userObj.sysInfo = 1;
325,410✔
1008
  userObj.enable = 1;
325,410✔
1009
  userObj.changePass = 2;
325,410✔
1010
  userObj.ipWhiteListVer = taosGetTimestampMs();
325,410✔
1011
  userObj.connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
325,410✔
1012
  userObj.connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
325,410✔
1013
  userObj.callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
325,410✔
1014
  userObj.vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
325,410✔
1015
  userObj.passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
325,410✔
1016
  userObj.passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
325,410✔
1017
  userObj.passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
325,410✔
1018
  // this is the root user, set some fields to -1 to allow the user login without restriction
1019
  userObj.sessionPerUser = -1;
325,410✔
1020
  userObj.failedLoginAttempts = -1;
325,410✔
1021
  userObj.passwordLifeTime = -1;
325,410✔
1022
  userObj.passwordGraceTime = -1;
325,410✔
1023
  userObj.inactiveAccountTime = -1;
325,410✔
1024
  userObj.allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
325,410✔
1025
  userObj.pTimeWhiteList = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
325,410✔
1026
  if (userObj.pTimeWhiteList == NULL) {
325,410✔
1027
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
1028
  }
1029
  
1030
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _ERROR);
325,410✔
1031
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
325,410✔
1032
    userObj.superUser = 1;
325,410✔
1033
    userObj.createdb = 1;
325,410✔
1034
    userObj.sessionPerUser = -1;
325,410✔
1035
    userObj.callPerSession = -1;
325,410✔
1036
    userObj.vnodePerCall = -1;
325,410✔
1037
    userObj.failedLoginAttempts = -1;
325,410✔
1038
    userObj.passwordLifeTime = -1;
325,410✔
1039
    userObj.passwordLockTime = -1;
325,410✔
1040
    userObj.inactiveAccountTime = -1;
325,410✔
1041
    userObj.allowTokenNum = -1;
325,410✔
1042
  }
1043

1044
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
325,410✔
1045
  if (pRaw == NULL) goto _ERROR;
325,410✔
1046
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
325,410✔
1047

1048
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
325,410✔
1049

1050
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
325,410✔
1051
  if (pTrans == NULL) {
325,410✔
1052
    sdbFreeRaw(pRaw);
×
1053
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
1054
    goto _ERROR;
×
1055
  }
1056
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
325,410✔
1057

1058
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
325,410✔
1059
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1060
    mndTransDrop(pTrans);
×
1061
    goto _ERROR;
×
1062
  }
1063
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
325,410✔
1064

1065
  if (mndTransPrepare(pMnode, pTrans) != 0) {
325,410✔
1066
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1067
    mndTransDrop(pTrans);
×
1068
    goto _ERROR;
×
1069
  }
1070

1071
  mndTransDrop(pTrans);
325,410✔
1072
  taosMemoryFree(userObj.passwords);
325,410✔
1073
  taosMemoryFree(userObj.pIpWhiteListDual);
325,410✔
1074
  taosMemoryFree(userObj.pTimeWhiteList);
325,410✔
1075
  return 0;
325,410✔
1076

1077
_ERROR:
×
1078
  taosMemoryFree(userObj.passwords);
×
1079
  taosMemoryFree(userObj.pIpWhiteListDual);
×
1080
  taosMemoryFree(userObj.pTimeWhiteList);
×
1081
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
1082
}
1083

1084
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
325,410✔
1085
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
325,410✔
1086
}
1087

1088
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
2,515,465✔
1089
  int32_t code = 0;
2,515,465✔
1090
  int32_t lino = 0;
2,515,465✔
1091
  int32_t passReserve = (sizeof(SUserPassword) + 8) * pUser->numOfPasswords + 4;
2,515,465✔
1092
  int32_t ipWhiteReserve = pUser->pIpWhiteListDual ? (sizeof(SIpRange) * pUser->pIpWhiteListDual->num + sizeof(SIpWhiteListDual) + 4) : 16;
2,515,465✔
1093
  int32_t timeWhiteReserve = pUser->pTimeWhiteList ? (sizeof(SDateTimeWhiteListItem) * pUser->pTimeWhiteList->num + sizeof(SDateTimeWhiteList) + 4) : 16;
2,515,465✔
1094
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
2,515,465✔
1095
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
2,515,465✔
1096
  int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
2,515,465✔
1097
  int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
2,515,465✔
1098
  int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
2,515,465✔
1099
  int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
2,515,465✔
1100
  int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
2,515,465✔
1101
  int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
2,515,465✔
1102
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
2,515,465✔
1103
  int32_t numOfUseDbs = taosHashGetSize(pUser->useDbs);
2,515,465✔
1104
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
2,515,465✔
1105
                 numOfTopics * TSDB_TOPIC_FNAME_LEN + ipWhiteReserve + timeWhiteReserve + passReserve;
2,515,465✔
1106
  char    *buf = NULL;
2,515,465✔
1107
  SSdbRaw *pRaw = NULL;
2,515,465✔
1108

1109
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,515,465✔
1110
  while (stb != NULL) {
2,955,588✔
1111
    size_t keyLen = 0;
440,123✔
1112
    void  *key = taosHashGetKey(stb, &keyLen);
440,123✔
1113
    size += sizeof(int32_t);
440,123✔
1114
    size += keyLen;
440,123✔
1115

1116
    size_t valueLen = 0;
440,123✔
1117
    valueLen = strlen(stb) + 1;
440,123✔
1118
    size += sizeof(int32_t);
440,123✔
1119
    size += valueLen;
440,123✔
1120
    stb = taosHashIterate(pUser->readTbs, stb);
440,123✔
1121
  }
1122

1123
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,515,465✔
1124
  while (stb != NULL) {
2,972,501✔
1125
    size_t keyLen = 0;
457,036✔
1126
    void  *key = taosHashGetKey(stb, &keyLen);
457,036✔
1127
    size += sizeof(int32_t);
457,036✔
1128
    size += keyLen;
457,036✔
1129

1130
    size_t valueLen = 0;
457,036✔
1131
    valueLen = strlen(stb) + 1;
457,036✔
1132
    size += sizeof(int32_t);
457,036✔
1133
    size += valueLen;
457,036✔
1134
    stb = taosHashIterate(pUser->writeTbs, stb);
457,036✔
1135
  }
1136

1137
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,515,465✔
1138
  while (stb != NULL) {
2,750,170✔
1139
    size_t keyLen = 0;
234,705✔
1140
    void  *key = taosHashGetKey(stb, &keyLen);
234,705✔
1141
    size += sizeof(int32_t);
234,705✔
1142
    size += keyLen;
234,705✔
1143

1144
    size_t valueLen = 0;
234,705✔
1145
    valueLen = strlen(stb) + 1;
234,705✔
1146
    size += sizeof(int32_t);
234,705✔
1147
    size += valueLen;
234,705✔
1148
    stb = taosHashIterate(pUser->alterTbs, stb);
234,705✔
1149
  }
1150

1151
  stb = taosHashIterate(pUser->readViews, NULL);
2,515,465✔
1152
  while (stb != NULL) {
2,565,246✔
1153
    size_t keyLen = 0;
49,781✔
1154
    void  *key = taosHashGetKey(stb, &keyLen);
49,781✔
1155
    size += sizeof(int32_t);
49,781✔
1156
    size += keyLen;
49,781✔
1157

1158
    size_t valueLen = 0;
49,781✔
1159
    valueLen = strlen(stb) + 1;
49,781✔
1160
    size += sizeof(int32_t);
49,781✔
1161
    size += valueLen;
49,781✔
1162
    stb = taosHashIterate(pUser->readViews, stb);
49,781✔
1163
  }
1164

1165
  stb = taosHashIterate(pUser->writeViews, NULL);
2,515,465✔
1166
  while (stb != NULL) {
2,553,860✔
1167
    size_t keyLen = 0;
38,395✔
1168
    void  *key = taosHashGetKey(stb, &keyLen);
38,395✔
1169
    size += sizeof(int32_t);
38,395✔
1170
    size += keyLen;
38,395✔
1171

1172
    size_t valueLen = 0;
38,395✔
1173
    valueLen = strlen(stb) + 1;
38,395✔
1174
    size += sizeof(int32_t);
38,395✔
1175
    size += valueLen;
38,395✔
1176
    stb = taosHashIterate(pUser->writeViews, stb);
38,395✔
1177
  }
1178

1179
  stb = taosHashIterate(pUser->alterViews, NULL);
2,515,465✔
1180
  while (stb != NULL) {
2,553,255✔
1181
    size_t keyLen = 0;
37,790✔
1182
    void  *key = taosHashGetKey(stb, &keyLen);
37,790✔
1183
    size += sizeof(int32_t);
37,790✔
1184
    size += keyLen;
37,790✔
1185

1186
    size_t valueLen = 0;
37,790✔
1187
    valueLen = strlen(stb) + 1;
37,790✔
1188
    size += sizeof(int32_t);
37,790✔
1189
    size += valueLen;
37,790✔
1190
    stb = taosHashIterate(pUser->alterViews, stb);
37,790✔
1191
  }
1192

1193
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,515,465✔
1194
  while (useDb != NULL) {
3,081,054✔
1195
    size_t keyLen = 0;
565,589✔
1196
    void  *key = taosHashGetKey(useDb, &keyLen);
565,589✔
1197
    size += sizeof(int32_t);
565,589✔
1198
    size += keyLen;
565,589✔
1199
    size += sizeof(int32_t);
565,589✔
1200
    useDb = taosHashIterate(pUser->useDbs, useDb);
565,589✔
1201
  }
1202

1203
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
2,515,465✔
1204
  if (pRaw == NULL) {
2,515,465✔
1205
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1206
  }
1207

1208
  int32_t dataPos = 0;
2,515,465✔
1209
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
2,515,465✔
1210

1211
  dropOldPasswords(pUser);
2,515,465✔
1212
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,515,465✔
1213
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
5,061,688✔
1214
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,546,223✔
1215
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,546,223✔
1216
  }
1217
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,515,465✔
1218

1219
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
2,515,465✔
1220
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
2,515,465✔
1221
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
2,515,465✔
1222
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
2,515,465✔
1223
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
2,515,465✔
1224
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
2,515,465✔
1225
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
2,515,465✔
1226
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
2,515,465✔
1227
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
2,515,465✔
1228
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
2,515,465✔
1229
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
2,515,465✔
1230
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
2,515,465✔
1231

1232
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,515,465✔
1233
  while (db != NULL) {
2,849,913✔
1234
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
334,448✔
1235
    db = taosHashIterate(pUser->readDbs, db);
334,448✔
1236
  }
1237

1238
  db = taosHashIterate(pUser->writeDbs, NULL);
2,515,465✔
1239
  while (db != NULL) {
2,840,699✔
1240
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
325,234✔
1241
    db = taosHashIterate(pUser->writeDbs, db);
325,234✔
1242
  }
1243

1244
  char *topic = taosHashIterate(pUser->topics, NULL);
2,515,465✔
1245
  while (topic != NULL) {
2,532,415✔
1246
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
16,950✔
1247
    topic = taosHashIterate(pUser->topics, topic);
16,950✔
1248
  }
1249

1250
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
2,515,465✔
1251
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
2,515,465✔
1252
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
2,515,465✔
1253
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
2,515,465✔
1254
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
2,515,465✔
1255
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
2,515,465✔
1256
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
2,515,465✔
1257

1258
  stb = taosHashIterate(pUser->readTbs, NULL);
2,515,465✔
1259
  while (stb != NULL) {
2,955,588✔
1260
    size_t keyLen = 0;
440,123✔
1261
    void  *key = taosHashGetKey(stb, &keyLen);
440,123✔
1262
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
440,123✔
1263
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
440,123✔
1264

1265
    size_t valueLen = 0;
440,123✔
1266
    valueLen = strlen(stb) + 1;
440,123✔
1267
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
440,123✔
1268
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
440,123✔
1269
    stb = taosHashIterate(pUser->readTbs, stb);
440,123✔
1270
  }
1271

1272
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,515,465✔
1273
  while (stb != NULL) {
2,972,501✔
1274
    size_t keyLen = 0;
457,036✔
1275
    void  *key = taosHashGetKey(stb, &keyLen);
457,036✔
1276
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
457,036✔
1277
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
457,036✔
1278

1279
    size_t valueLen = 0;
457,036✔
1280
    valueLen = strlen(stb) + 1;
457,036✔
1281
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
457,036✔
1282
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
457,036✔
1283
    stb = taosHashIterate(pUser->writeTbs, stb);
457,036✔
1284
  }
1285

1286
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,515,465✔
1287
  while (stb != NULL) {
2,750,170✔
1288
    size_t keyLen = 0;
234,705✔
1289
    void  *key = taosHashGetKey(stb, &keyLen);
234,705✔
1290
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
234,705✔
1291
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
234,705✔
1292

1293
    size_t valueLen = 0;
234,705✔
1294
    valueLen = strlen(stb) + 1;
234,705✔
1295
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
234,705✔
1296
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
234,705✔
1297
    stb = taosHashIterate(pUser->alterTbs, stb);
234,705✔
1298
  }
1299

1300
  stb = taosHashIterate(pUser->readViews, NULL);
2,515,465✔
1301
  while (stb != NULL) {
2,565,246✔
1302
    size_t keyLen = 0;
49,781✔
1303
    void  *key = taosHashGetKey(stb, &keyLen);
49,781✔
1304
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
49,781✔
1305
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
49,781✔
1306

1307
    size_t valueLen = 0;
49,781✔
1308
    valueLen = strlen(stb) + 1;
49,781✔
1309
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
49,781✔
1310
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
49,781✔
1311
    stb = taosHashIterate(pUser->readViews, stb);
49,781✔
1312
  }
1313

1314
  stb = taosHashIterate(pUser->writeViews, NULL);
2,515,465✔
1315
  while (stb != NULL) {
2,553,860✔
1316
    size_t keyLen = 0;
38,395✔
1317
    void  *key = taosHashGetKey(stb, &keyLen);
38,395✔
1318
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
38,395✔
1319
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
38,395✔
1320

1321
    size_t valueLen = 0;
38,395✔
1322
    valueLen = strlen(stb) + 1;
38,395✔
1323
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
38,395✔
1324
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
38,395✔
1325
    stb = taosHashIterate(pUser->writeViews, stb);
38,395✔
1326
  }
1327

1328
  stb = taosHashIterate(pUser->alterViews, NULL);
2,515,465✔
1329
  while (stb != NULL) {
2,553,255✔
1330
    size_t keyLen = 0;
37,790✔
1331
    void  *key = taosHashGetKey(stb, &keyLen);
37,790✔
1332
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
37,790✔
1333
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
37,790✔
1334

1335
    size_t valueLen = 0;
37,790✔
1336
    valueLen = strlen(stb) + 1;
37,790✔
1337
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
37,790✔
1338
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
37,790✔
1339
    stb = taosHashIterate(pUser->alterViews, stb);
37,790✔
1340
  }
1341

1342
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,515,465✔
1343
  while (useDb != NULL) {
3,081,054✔
1344
    size_t keyLen = 0;
565,589✔
1345
    void  *key = taosHashGetKey(useDb, &keyLen);
565,589✔
1346
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
565,589✔
1347
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
565,589✔
1348

1349
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
565,589✔
1350
    useDb = taosHashIterate(pUser->useDbs, useDb);
565,589✔
1351
  }
1352

1353
  // save white list
1354
  int32_t num = pUser->pIpWhiteListDual->num;
2,515,465✔
1355
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
2,515,465✔
1356
  if ((buf = taosMemoryCalloc(1, tlen)) == NULL) {
2,515,465✔
1357
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1358
  }
1359
  int32_t len = 0;
2,515,465✔
1360
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
2,515,465✔
1361

1362
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
2,515,465✔
1363
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
2,515,465✔
1364

1365
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
2,515,465✔
1366
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
2,515,465✔
1367

1368
  SDB_SET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
2,515,465✔
1369
  SDB_SET_INT8(pRaw, dataPos, pUser->changePass, _OVER);
2,515,465✔
1370
  SDB_SET_INT32(pRaw, dataPos, pUser->sessionPerUser, _OVER);
2,515,465✔
1371
  SDB_SET_INT32(pRaw, dataPos, pUser->connectTime, _OVER);
2,515,465✔
1372
  SDB_SET_INT32(pRaw, dataPos, pUser->connectIdleTime, _OVER);
2,515,465✔
1373
  SDB_SET_INT32(pRaw, dataPos, pUser->callPerSession, _OVER);
2,515,465✔
1374
  SDB_SET_INT32(pRaw, dataPos, pUser->vnodePerCall, _OVER);
2,515,465✔
1375
  SDB_SET_INT32(pRaw, dataPos, pUser->failedLoginAttempts, _OVER);
2,515,465✔
1376
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLifeTime, _OVER);
2,515,465✔
1377
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseTime, _OVER);
2,515,465✔
1378
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseMax, _OVER);
2,515,465✔
1379
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLockTime, _OVER);
2,515,465✔
1380
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordGraceTime, _OVER);
2,515,465✔
1381
  SDB_SET_INT32(pRaw, dataPos, pUser->inactiveAccountTime, _OVER);
2,515,465✔
1382
  SDB_SET_INT32(pRaw, dataPos, pUser->allowTokenNum, _OVER);
2,515,465✔
1383

1384
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
2,515,465✔
1385
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
2,515,465✔
1386
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1387
    SDB_SET_BOOL(pRaw, dataPos, range->absolute, _OVER);
×
1388
    SDB_SET_BOOL(pRaw, dataPos, range->neg, _OVER);
×
1389
    SDB_SET_INT64(pRaw, dataPos, range->start, _OVER);
×
1390
    SDB_SET_INT32(pRaw, dataPos, range->duration, _OVER);
×
1391
  }
1392

1393
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,515,465✔
1394
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,515,465✔
1395

1396
_OVER:
2,515,465✔
1397
  taosMemoryFree(buf);
2,515,465✔
1398
  if (code < 0) {
2,515,465✔
1399
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1400
           tstrerror(code));
1401
    sdbFreeRaw(pRaw);
×
1402
    pRaw = NULL;
×
1403
    terrno = code;
×
1404
  }
1405

1406
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
2,515,465✔
1407
  return pRaw;
2,515,465✔
1408
}
1409

1410
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
1,639,441✔
1411
  int32_t   code = 0;
1,639,441✔
1412
  int32_t   lino = 0;
1,639,441✔
1413
  SSdbRow  *pRow = NULL;
1,639,441✔
1414
  SUserObj *pUser = NULL;
1,639,441✔
1415
  char     *key = NULL;
1,639,441✔
1416
  char     *value = NULL;
1,639,441✔
1417

1418
  int8_t sver = 0;
1,639,441✔
1419
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
1,639,441✔
1420
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1421
  }
1422

1423
  if (sver < 1 || sver > USER_VER_NUMBER) {
1,639,441✔
1424
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1425
  }
1426

1427
  pRow = sdbAllocRow(sizeof(SUserObj));
1,639,441✔
1428
  if (pRow == NULL) {
1,639,441✔
1429
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1430
  }
1431

1432
  pUser = sdbGetRowObj(pRow);
1,639,441✔
1433
  if (pUser == NULL) {
1,639,441✔
1434
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1435
  }
1436

1437
  int32_t dataPos = 0;
1,639,441✔
1438
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
1,639,441✔
1439

1440
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,639,441✔
1441
    pUser->passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
×
1442
    if (pUser->passwords == NULL) {
×
1443
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1444
    }
1445
    SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[0].pass, TSDB_PASSWORD_LEN, _OVER)
×
1446
    pUser->numOfPasswords = 1;
×
1447
    memset(pUser->salt, 0, sizeof(pUser->salt));
×
1448
  } else {
1449
    SDB_GET_INT32(pRaw, dataPos, &pUser->numOfPasswords, _OVER)
1,639,441✔
1450
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,639,441✔
1451
    if (pUser->passwords == NULL) {
1,639,441✔
1452
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1453
    }
1454
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
3,330,739✔
1455
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
1,691,298✔
1456
      SDB_GET_INT32(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
1,691,298✔
1457
    }
1458
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
1,639,441✔
1459
  }
1460
  
1461
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
1,639,441✔
1462
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
1,639,441✔
1463
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
1,639,441✔
1464
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,639,441✔
1465
    pUser->passwords[0].setTime = (int32_t)(pUser->updateTime / 1000);
×
1466
  }
1467

1468
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
1,639,441✔
1469
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
1,639,441✔
1470
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
1,639,441✔
1471
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
1,639,441✔
1472
  if (pUser->superUser) pUser->createdb = 1;
1,639,441✔
1473
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
1,639,441✔
1474
  if (sver >= 4) {
1,639,441✔
1475
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
1,639,441✔
1476
  }
1477

1478
  int32_t numOfReadDbs = 0;
1,639,441✔
1479
  int32_t numOfWriteDbs = 0;
1,639,441✔
1480
  int32_t numOfTopics = 0;
1,639,441✔
1481
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
1,639,441✔
1482
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
1,639,441✔
1483
  if (sver >= 2) {
1,639,441✔
1484
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
1,639,441✔
1485
  }
1486

1487
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1488
  pUser->writeDbs =
1,639,441✔
1489
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1490
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1491
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
1,639,441✔
1492
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1493
    goto _OVER;
×
1494
  }
1495

1496
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
1,962,033✔
1497
    char db[TSDB_DB_FNAME_LEN] = {0};
322,592✔
1498
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
322,592✔
1499
    int32_t len = strlen(db) + 1;
322,592✔
1500
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
322,592✔
1501
  }
1502

1503
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
1,955,205✔
1504
    char db[TSDB_DB_FNAME_LEN] = {0};
315,764✔
1505
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
315,764✔
1506
    int32_t len = strlen(db) + 1;
315,764✔
1507
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
315,764✔
1508
  }
1509

1510
  if (sver >= 2) {
1,639,441✔
1511
    for (int32_t i = 0; i < numOfTopics; ++i) {
1,654,973✔
1512
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
15,532✔
1513
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
15,532✔
1514
      int32_t len = strlen(topic) + 1;
15,532✔
1515
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
15,532✔
1516
    }
1517
  }
1518

1519
  if (sver >= 3) {
1,639,441✔
1520
    int32_t numOfReadTbs = 0;
1,639,441✔
1521
    int32_t numOfWriteTbs = 0;
1,639,441✔
1522
    int32_t numOfAlterTbs = 0;
1,639,441✔
1523
    int32_t numOfReadViews = 0;
1,639,441✔
1524
    int32_t numOfWriteViews = 0;
1,639,441✔
1525
    int32_t numOfAlterViews = 0;
1,639,441✔
1526
    int32_t numOfUseDbs = 0;
1,639,441✔
1527
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
1,639,441✔
1528
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
1,639,441✔
1529
    if (sver >= 6) {
1,639,441✔
1530
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
1,639,441✔
1531
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
1,639,441✔
1532
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
1,639,441✔
1533
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
1,639,441✔
1534
    }
1535
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
1,639,441✔
1536

1537
    pUser->readTbs =
1,639,441✔
1538
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1539
    pUser->writeTbs =
1,639,441✔
1540
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1541
    pUser->alterTbs =
1,639,441✔
1542
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1543

1544
    pUser->readViews =
1,639,441✔
1545
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1546
    pUser->writeViews =
1,639,441✔
1547
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1548
    pUser->alterViews =
1,639,441✔
1549
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1550

1551
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,639,441✔
1552

1553
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
1,639,441✔
1554
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
1,639,441✔
1555
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1556
      goto _OVER;
×
1557
    }
1558

1559
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,055,790✔
1560
      int32_t keyLen = 0;
416,349✔
1561
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
416,349✔
1562

1563
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
416,349✔
1564
      if (key == NULL) {
416,349✔
1565
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1566
      }
1567
      (void)memset(key, 0, keyLen);
416,349✔
1568
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
416,349✔
1569

1570
      int32_t valuelen = 0;
416,349✔
1571
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
416,349✔
1572
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
416,349✔
1573
      if (value == NULL) {
416,349✔
1574
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1575
      }
1576
      (void)memset(value, 0, valuelen);
416,349✔
1577
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
416,349✔
1578

1579
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
416,349✔
1580
    }
1581

1582
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
2,067,353✔
1583
      int32_t keyLen = 0;
427,912✔
1584
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
427,912✔
1585

1586
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
427,912✔
1587
      if (key == NULL) {
427,912✔
1588
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1589
      }
1590
      (void)memset(key, 0, keyLen);
427,912✔
1591
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
427,912✔
1592

1593
      int32_t valuelen = 0;
427,912✔
1594
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
427,912✔
1595
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
427,912✔
1596
      if (value == NULL) {
427,912✔
1597
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1598
      }
1599
      (void)memset(value, 0, valuelen);
427,912✔
1600
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
427,912✔
1601

1602
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
427,912✔
1603
    }
1604

1605
    if (sver >= 6) {
1,639,441✔
1606
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,863,020✔
1607
        int32_t keyLen = 0;
223,579✔
1608
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
223,579✔
1609

1610
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
223,579✔
1611
        if (key == NULL) {
223,579✔
1612
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1613
        }
1614
        (void)memset(key, 0, keyLen);
223,579✔
1615
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
223,579✔
1616

1617
        int32_t valuelen = 0;
223,579✔
1618
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
223,579✔
1619
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
223,579✔
1620
        if (value == NULL) {
223,579✔
1621
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1622
        }
1623
        (void)memset(value, 0, valuelen);
223,579✔
1624
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
223,579✔
1625

1626
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
223,579✔
1627
      }
1628

1629
      for (int32_t i = 0; i < numOfReadViews; ++i) {
1,687,848✔
1630
        int32_t keyLen = 0;
48,407✔
1631
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
48,407✔
1632

1633
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
48,407✔
1634
        if (key == NULL) {
48,407✔
1635
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1636
        }
1637
        (void)memset(key, 0, keyLen);
48,407✔
1638
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
48,407✔
1639

1640
        int32_t valuelen = 0;
48,407✔
1641
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
48,407✔
1642
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
48,407✔
1643
        if (value == NULL) {
48,407✔
1644
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1645
        }
1646
        (void)memset(value, 0, valuelen);
48,407✔
1647
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
48,407✔
1648

1649
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
48,407✔
1650
      }
1651

1652
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
1,676,462✔
1653
        int32_t keyLen = 0;
37,021✔
1654
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
37,021✔
1655

1656
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
37,021✔
1657
        if (key == NULL) {
37,021✔
1658
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1659
        }
1660
        (void)memset(key, 0, keyLen);
37,021✔
1661
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
37,021✔
1662

1663
        int32_t valuelen = 0;
37,021✔
1664
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
37,021✔
1665
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
37,021✔
1666
        if (value == NULL) {
37,021✔
1667
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1668
        }
1669
        (void)memset(value, 0, valuelen);
37,021✔
1670
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
37,021✔
1671

1672
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
37,021✔
1673
      }
1674

1675
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
1,675,857✔
1676
        int32_t keyLen = 0;
36,416✔
1677
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36,416✔
1678

1679
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
36,416✔
1680
        if (key == NULL) {
36,416✔
1681
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1682
        }
1683
        (void)memset(key, 0, keyLen);
36,416✔
1684
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,416✔
1685

1686
        int32_t valuelen = 0;
36,416✔
1687
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
36,416✔
1688
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
36,416✔
1689
        if (value == NULL) {
36,416✔
1690
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1691
        }
1692
        (void)memset(value, 0, valuelen);
36,416✔
1693
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
36,416✔
1694

1695
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterViews, key, keyLen, value, valuelen), &lino, _OVER);
36,416✔
1696
      }
1697
    }
1698

1699
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
2,162,343✔
1700
      int32_t keyLen = 0;
522,902✔
1701
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
522,902✔
1702

1703
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
522,902✔
1704
      if (key == NULL) {
522,902✔
1705
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1706
      }
1707
      (void)memset(key, 0, keyLen);
522,902✔
1708
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
522,902✔
1709

1710
      int32_t ref = 0;
522,902✔
1711
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
522,902✔
1712

1713
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
522,902✔
1714
    }
1715
  }
1716
  // decoder white list
1717
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
1,639,441✔
1718
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,639,441✔
1719
      int32_t len = 0;
×
1720
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
1721

1722
      TAOS_MEMORY_REALLOC(key, len);
×
1723
      if (key == NULL) {
×
1724
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1725
      }
1726
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
1727

1728
      SIpWhiteList *pIpWhiteList = NULL;
×
1729
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
1730

1731
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
1732

1733
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
1734
      if (code != 0) {
×
1735
        taosMemoryFreeClear(pIpWhiteList);
×
1736
      }
1737
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1738

1739
      taosMemoryFreeClear(pIpWhiteList);
×
1740

1741
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,639,441✔
1742
      int32_t len = 0;
1,639,441✔
1743
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
1,639,441✔
1744

1745
      TAOS_MEMORY_REALLOC(key, len);
1,639,441✔
1746
      if (key == NULL) {
1,639,441✔
1747
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1748
      }
1749
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
1,639,441✔
1750

1751
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual, sver >= USER_VER_SUPPORT_ADVANCED_SECURITY), &lino, _OVER);
1,639,441✔
1752
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
1,639,441✔
1753
    }
1754
  }
1755

1756
  if (pUser->pIpWhiteListDual == NULL) {
1,639,441✔
1757
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
1758
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1759
  }
1760

1761
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
1,639,441✔
1762

1763
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,639,441✔
1764
    memset(pUser->totpsecret, 0, sizeof(pUser->totpsecret));
×
1765
    pUser->changePass = 2;
×
1766
    pUser->sessionPerUser = pUser->superUser ? -1 : TSDB_USER_SESSION_PER_USER_DEFAULT;
×
1767
    pUser->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
×
1768
    pUser->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
×
1769
    pUser->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
×
1770
    pUser->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
×
1771
    pUser->failedLoginAttempts = pUser->superUser ? -1 : TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
×
1772
    pUser->passwordLifeTime = pUser->superUser ? -1 : TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
×
1773
    pUser->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
×
1774
    pUser->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
×
1775
    pUser->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
×
1776
    pUser->passwordGraceTime = pUser->superUser ? -1 : TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
×
1777
    pUser->inactiveAccountTime = pUser->superUser ? -1 : TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
×
1778
    pUser->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
×
1779
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList));
×
1780
    if (pUser->pTimeWhiteList == NULL) {
×
1781
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1782
    }
1783
  } else {
1784
    SDB_GET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
1,639,441✔
1785
    SDB_GET_INT8(pRaw, dataPos, &pUser->changePass, _OVER);
1,639,441✔
1786
    SDB_GET_INT32(pRaw, dataPos, &pUser->sessionPerUser, _OVER);
1,639,441✔
1787
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectTime, _OVER);
1,639,441✔
1788
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectIdleTime, _OVER);
1,639,441✔
1789
    SDB_GET_INT32(pRaw, dataPos, &pUser->callPerSession, _OVER);
1,639,441✔
1790
    SDB_GET_INT32(pRaw, dataPos, &pUser->vnodePerCall, _OVER);
1,639,441✔
1791
    SDB_GET_INT32(pRaw, dataPos, &pUser->failedLoginAttempts, _OVER);
1,639,441✔
1792
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLifeTime, _OVER);
1,639,441✔
1793
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseTime, _OVER);
1,639,441✔
1794
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseMax, _OVER);
1,639,441✔
1795
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLockTime, _OVER);
1,639,441✔
1796
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordGraceTime, _OVER);
1,639,441✔
1797
    SDB_GET_INT32(pRaw, dataPos, &pUser->inactiveAccountTime, _OVER);
1,639,441✔
1798
    SDB_GET_INT32(pRaw, dataPos, &pUser->allowTokenNum, _OVER);
1,639,441✔
1799

1800
    int32_t num = 0;
1,639,441✔
1801
    SDB_GET_INT32(pRaw, dataPos, &num, _OVER);
1,639,441✔
1802
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList) + num * sizeof(SDateTimeWhiteListItem));
1,639,441✔
1803
    if (pUser->pTimeWhiteList == NULL) {
1,639,441✔
1804
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1805
    }
1806

1807
    pUser->pTimeWhiteList->num = num;
1,639,441✔
1808
    for (int32_t i = 0; i < num; i++) {
1,639,441✔
1809
      SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1810
      SDB_GET_BOOL(pRaw, dataPos, &range->absolute, _OVER);
×
1811
      SDB_GET_BOOL(pRaw, dataPos, &range->neg, _OVER);
×
1812
      SDB_GET_INT64(pRaw, dataPos, &range->start, _OVER);
×
1813
      SDB_GET_INT32(pRaw, dataPos, &range->duration, _OVER);
×
1814
    }
1815
  }
1816

1817
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
1,639,441✔
1818
  taosInitRWLatch(&pUser->lock);
1,639,441✔
1819
  dropOldPasswords(pUser);
1,639,441✔
1820

1821
_OVER:
1,639,441✔
1822
  taosMemoryFree(key);
1,639,441✔
1823
  taosMemoryFree(value);
1,639,441✔
1824
  if (code < 0) {
1,639,441✔
1825
    terrno = code;
×
1826
    mError("user:%s, failed to decode at line %d from raw:%p since %s", pUser == NULL ? "null" : pUser->user, lino,
×
1827
           pRaw, tstrerror(code));
1828
    if (pUser != NULL) {
×
1829
      taosHashCleanup(pUser->readDbs);
×
1830
      taosHashCleanup(pUser->writeDbs);
×
1831
      taosHashCleanup(pUser->topics);
×
1832
      taosHashCleanup(pUser->readTbs);
×
1833
      taosHashCleanup(pUser->writeTbs);
×
1834
      taosHashCleanup(pUser->alterTbs);
×
1835
      taosHashCleanup(pUser->readViews);
×
1836
      taosHashCleanup(pUser->writeViews);
×
1837
      taosHashCleanup(pUser->alterViews);
×
1838
      taosHashCleanup(pUser->useDbs);
×
1839
      taosMemoryFreeClear(pUser->pIpWhiteListDual);
×
1840
      taosMemoryFreeClear(pUser->pTimeWhiteList);
×
1841
    }
1842
    taosMemoryFreeClear(pRow);
×
1843
    return NULL;
×
1844
  }
1845

1846
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
1,639,441✔
1847
  return pRow;
1,639,441✔
1848
}
1849

1850
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
623,030✔
1851
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
623,030✔
1852

1853
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
623,030✔
1854
  if (pAcct == NULL) {
623,030✔
1855
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
1856
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
1857
    TAOS_RETURN(terrno);
×
1858
  }
1859
  pUser->acctId = pAcct->acctId;
623,030✔
1860
  sdbRelease(pSdb, pAcct);
623,030✔
1861

1862
  return 0;
623,030✔
1863
}
1864

1865
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
41,905,261✔
1866
  int32_t code = 0;
41,905,261✔
1867
  *ppNew =
41,905,484✔
1868
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
41,905,261✔
1869
  if (*ppNew == NULL) {
41,905,484✔
1870
    TAOS_RETURN(terrno);
×
1871
  }
1872

1873
  char *tb = taosHashIterate(pOld, NULL);
41,905,484✔
1874
  while (tb != NULL) {
44,270,997✔
1875
    size_t keyLen = 0;
2,365,513✔
1876
    char  *key = taosHashGetKey(tb, &keyLen);
2,365,513✔
1877

1878
    int32_t valueLen = strlen(tb) + 1;
2,365,513✔
1879
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
2,365,513✔
1880
      taosHashCancelIterate(pOld, tb);
×
1881
      taosHashCleanup(*ppNew);
×
1882
      TAOS_RETURN(code);
×
1883
    }
1884
    tb = taosHashIterate(pOld, tb);
2,365,513✔
1885
  }
1886

1887
  TAOS_RETURN(code);
41,905,484✔
1888
}
1889

1890
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
1,679,393✔
1891
  int32_t code = 0;
1,679,393✔
1892
  *ppNew =
1,679,393✔
1893
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,679,393✔
1894
  if (*ppNew == NULL) {
1,679,393✔
1895
    TAOS_RETURN(terrno);
×
1896
  }
1897

1898
  int32_t *db = taosHashIterate(pOld, NULL);
1,679,393✔
1899
  while (db != NULL) {
2,181,467✔
1900
    size_t keyLen = 0;
502,074✔
1901
    char  *key = taosHashGetKey(db, &keyLen);
502,074✔
1902

1903
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
502,074✔
1904
      taosHashCancelIterate(pOld, db);
×
1905
      taosHashCleanup(*ppNew);
×
1906
      TAOS_RETURN(code);
×
1907
    }
1908
    db = taosHashIterate(pOld, db);
502,074✔
1909
  }
1910

1911
  TAOS_RETURN(code);
1,679,393✔
1912
}
1913

1914
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
1,679,393✔
1915
  int32_t code = 0;
1,679,393✔
1916
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
1,679,393✔
1917
  pNew->authVersion++;
1,679,393✔
1918
  pNew->updateTime = taosGetTimestampMs();
1,679,393✔
1919

1920
  pNew->passwords = NULL;
1,679,393✔
1921
  pNew->readDbs = NULL;
1,679,393✔
1922
  pNew->writeDbs = NULL;
1,679,393✔
1923
  pNew->readTbs = NULL;
1,679,393✔
1924
  pNew->writeTbs = NULL;
1,679,393✔
1925
  pNew->alterTbs = NULL;
1,679,393✔
1926
  pNew->readViews = NULL;
1,679,393✔
1927
  pNew->writeViews = NULL;
1,679,393✔
1928
  pNew->alterViews = NULL;
1,679,393✔
1929
  pNew->topics = NULL;
1,679,393✔
1930
  pNew->useDbs = NULL;
1,679,393✔
1931
  pNew->pIpWhiteListDual = NULL;
1,679,393✔
1932
  pNew->pTimeWhiteList = NULL;
1,679,393✔
1933

1934
  taosRLockLatch(&pUser->lock);
1,679,393✔
1935
  pNew->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,679,393✔
1936
  if (pNew->passwords == NULL) {
1,679,393✔
1937
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1938
    goto _OVER;
×
1939
  }
1940
  (void)memcpy(pNew->passwords, pUser->passwords, pUser->numOfPasswords * sizeof(SUserPassword));
1,679,393✔
1941

1942
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->readDbs, &pNew->readDbs), NULL, _OVER);
1,679,393✔
1943
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->writeDbs, &pNew->writeDbs), NULL, _OVER);
1,679,393✔
1944
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readTbs, &pNew->readTbs), NULL, _OVER);
1,679,393✔
1945
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeTbs, &pNew->writeTbs), NULL, _OVER);
1,679,393✔
1946
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterTbs, &pNew->alterTbs), NULL, _OVER);
1,679,393✔
1947
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readViews, &pNew->readViews), NULL, _OVER);
1,679,393✔
1948
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeViews, &pNew->writeViews), NULL, _OVER);
1,679,393✔
1949
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterViews, &pNew->alterViews), NULL, _OVER);
1,679,393✔
1950
  TAOS_CHECK_GOTO(mndDupTopicHash(pUser->topics, &pNew->topics), NULL, _OVER);
1,679,393✔
1951
  TAOS_CHECK_GOTO(mndDupUseDbHash(pUser->useDbs, &pNew->useDbs), NULL, _OVER);
1,679,393✔
1952
  pNew->pIpWhiteListDual = cloneIpWhiteList(pUser->pIpWhiteListDual);
1,679,393✔
1953
  if (pNew->pIpWhiteListDual == NULL) {
1,679,393✔
1954
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1955
    goto _OVER;
×
1956
  }
1957

1958
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
1,679,393✔
1959
  if (pNew->pTimeWhiteList == NULL) {
1,679,393✔
1960
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1961
    goto _OVER;
×
1962
  }
1963

1964
_OVER:
1,679,393✔
1965
  taosRUnLockLatch(&pUser->lock);
1,679,393✔
1966
  TAOS_RETURN(code);
1,679,393✔
1967
}
1968

1969
void mndUserFreeObj(SUserObj *pUser) {
6,172,969✔
1970
  taosHashCleanup(pUser->readDbs);
6,172,969✔
1971
  taosHashCleanup(pUser->writeDbs);
6,172,969✔
1972
  taosHashCleanup(pUser->topics);
6,172,969✔
1973
  taosHashCleanup(pUser->readTbs);
6,172,969✔
1974
  taosHashCleanup(pUser->writeTbs);
6,172,969✔
1975
  taosHashCleanup(pUser->alterTbs);
6,172,969✔
1976
  taosHashCleanup(pUser->readViews);
6,172,969✔
1977
  taosHashCleanup(pUser->writeViews);
6,172,969✔
1978
  taosHashCleanup(pUser->alterViews);
6,172,969✔
1979
  taosHashCleanup(pUser->useDbs);
6,172,969✔
1980
  taosMemoryFreeClear(pUser->passwords);
6,172,969✔
1981
  taosMemoryFreeClear(pUser->pIpWhiteListDual);
6,172,969✔
1982
  taosMemoryFreeClear(pUser->pTimeWhiteList);
6,172,969✔
1983
  pUser->readDbs = NULL;
6,172,969✔
1984
  pUser->writeDbs = NULL;
6,172,969✔
1985
  pUser->topics = NULL;
6,172,969✔
1986
  pUser->readTbs = NULL;
6,172,969✔
1987
  pUser->writeTbs = NULL;
6,172,969✔
1988
  pUser->alterTbs = NULL;
6,172,969✔
1989
  pUser->readViews = NULL;
6,172,969✔
1990
  pUser->writeViews = NULL;
6,172,969✔
1991
  pUser->alterViews = NULL;
6,172,969✔
1992
  pUser->useDbs = NULL;
6,172,969✔
1993
}
6,172,969✔
1994

1995
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
1,639,358✔
1996
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
1,639,358✔
1997
  mndUserFreeObj(pUser);
1,639,358✔
1998
  return 0;
1,639,358✔
1999
}
2000

2001
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
945,498✔
2002
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
945,498✔
2003
  taosWLockLatch(&pOld->lock);
945,498✔
2004
  pOld->updateTime = pNew->updateTime;
945,498✔
2005
  pOld->authVersion = pNew->authVersion;
945,498✔
2006
  pOld->passVersion = pNew->passVersion;
945,498✔
2007
  pOld->sysInfo = pNew->sysInfo;
945,498✔
2008
  pOld->enable = pNew->enable;
945,498✔
2009
  pOld->flag = pNew->flag;
945,498✔
2010
  pOld->changePass = pNew->changePass;
945,498✔
2011

2012
  pOld->sessionPerUser = pNew->sessionPerUser;
945,498✔
2013
  pOld->connectTime = pNew->connectTime;
945,498✔
2014
  pOld->connectIdleTime = pNew->connectIdleTime;
945,498✔
2015
  pOld->callPerSession = pNew->callPerSession;
945,498✔
2016
  pOld->vnodePerCall = pNew->vnodePerCall;
945,498✔
2017
  pOld->failedLoginAttempts = pNew->failedLoginAttempts;
945,498✔
2018
  pOld->passwordLifeTime = pNew->passwordLifeTime;
945,498✔
2019
  pOld->passwordReuseTime = pNew->passwordReuseTime;
945,498✔
2020
  pOld->passwordReuseMax = pNew->passwordReuseMax;
945,498✔
2021
  pOld->passwordLockTime = pNew->passwordLockTime;
945,498✔
2022
  pOld->passwordGraceTime = pNew->passwordGraceTime;
945,498✔
2023
  pOld->inactiveAccountTime = pNew->inactiveAccountTime;
945,498✔
2024
  pOld->allowTokenNum = pNew->allowTokenNum;
945,498✔
2025

2026
  pOld->numOfPasswords = pNew->numOfPasswords;
945,498✔
2027
  TSWAP(pOld->passwords, pNew->passwords);
945,498✔
2028
  (void)memcpy(pOld->salt, pNew->salt, sizeof(pOld->salt));
945,498✔
2029
  (void)memcpy(pOld->totpsecret, pNew->totpsecret, sizeof(pOld->totpsecret));
945,498✔
2030
  TSWAP(pOld->readDbs, pNew->readDbs);
945,498✔
2031
  TSWAP(pOld->writeDbs, pNew->writeDbs);
945,498✔
2032
  TSWAP(pOld->topics, pNew->topics);
945,498✔
2033
  TSWAP(pOld->readTbs, pNew->readTbs);
945,498✔
2034
  TSWAP(pOld->writeTbs, pNew->writeTbs);
945,498✔
2035
  TSWAP(pOld->alterTbs, pNew->alterTbs);
945,498✔
2036
  TSWAP(pOld->readViews, pNew->readViews);
945,498✔
2037
  TSWAP(pOld->writeViews, pNew->writeViews);
945,498✔
2038
  TSWAP(pOld->alterViews, pNew->alterViews);
945,498✔
2039
  TSWAP(pOld->useDbs, pNew->useDbs);
945,498✔
2040

2041
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
945,498✔
2042
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
945,498✔
2043
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
945,498✔
2044
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
945,498✔
2045
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
945,498✔
2046

2047
  taosWUnLockLatch(&pOld->lock);
945,498✔
2048

2049
  return 0;
945,498✔
2050
}
2051

2052
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
84,496,330✔
2053
  int32_t code = 0;
84,496,330✔
2054
  SSdb   *pSdb = pMnode->pSdb;
84,496,330✔
2055

2056
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
84,496,553✔
2057
  if (*ppUser == NULL) {
84,496,517✔
2058
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
119,517✔
2059
      code = TSDB_CODE_MND_USER_NOT_EXIST;
119,517✔
2060
    } else {
2061
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2062
    }
2063
  }
2064
  TAOS_RETURN(code);
84,496,517✔
2065
}
2066

2067
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
84,556,778✔
2068
  SSdb *pSdb = pMnode->pSdb;
84,556,778✔
2069
  sdbRelease(pSdb, pUser);
84,556,778✔
2070
}
84,557,483✔
2071

2072

2073

2074
int32_t mndEncryptPass(char *pass, const char* salt, int8_t *algo) {
114,825✔
2075
  int32_t code = 0;
114,825✔
2076
  if (tsiEncryptPassAlgorithm != DND_CA_SM4) {
114,825✔
2077
    return 0;
114,099✔
2078
  }
2079

2080
  if (strlen(tsEncryptKey) == 0) {
726✔
2081
    return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
2082
  }
2083

2084
  if (salt[0] != 0) {
726✔
2085
    char passAndSalt[TSDB_PASSWORD_LEN - 1 + TSDB_PASSWORD_SALT_LEN];
726✔
2086
    (void)memcpy(passAndSalt, pass, TSDB_PASSWORD_LEN - 1);
726✔
2087
    (void)memcpy(passAndSalt + TSDB_PASSWORD_LEN - 1, salt, TSDB_PASSWORD_SALT_LEN);
726✔
2088
    taosEncryptPass_c((uint8_t *)passAndSalt, sizeof(passAndSalt), pass);
2089
  }
2090

2091
  unsigned char packetData[TSDB_PASSWORD_LEN] = {0};
726✔
2092
  SCryptOpts opts = {0};
726✔
2093
  opts.len = TSDB_PASSWORD_LEN;
726✔
2094
  opts.source = pass;
726✔
2095
  opts.result = packetData;
726✔
2096
  opts.unitLen = TSDB_PASSWORD_LEN;
726✔
2097
  tstrncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
726✔
2098
  int newLen = Builtin_CBC_Encrypt(&opts);
726✔
2099
  if (newLen <= 0) return terrno;
726✔
2100

2101
  memcpy(pass, packetData, newLen);
726✔
2102
  if (algo != NULL) {
726✔
2103
    *algo = DND_CA_SM4;
363✔
2104
  }
2105

2106
  return 0;
726✔
2107
}
2108

2109

2110

2111
static void generateSalt(char *salt, size_t len) {
101,204✔
2112
  const char* set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
101,204✔
2113
  int32_t     setLen = 62;
101,204✔
2114
  for (int32_t i = 0; i < len - 1; ++i) {
3,238,528✔
2115
    salt[i] = set[taosSafeRand() % setLen];
3,137,324✔
2116
  }
2117
  salt[len - 1] = 0;
101,204✔
2118
}
101,204✔
2119

2120

2121

2122
static int32_t addDefaultIpToTable(int8_t enableIpv6, SHashObj *pUniqueTab) {
1,080✔
2123
  int32_t code = 0;
1,080✔
2124
  int32_t lino = 0;
1,080✔
2125
  int32_t dummpy = 0;
1,080✔
2126

2127
  SIpRange ipv4 = {0}, ipv6 = {0};
1,080✔
2128
  code = createDefaultIp4Range(&ipv4);
1,080✔
2129
  TSDB_CHECK_CODE(code, lino, _error);
1,080✔
2130

2131
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummpy, sizeof(dummpy));
1,080✔
2132
  TSDB_CHECK_CODE(code, lino, _error);
1,080✔
2133

2134
  if (enableIpv6) {
1,080✔
2135
    code = createDefaultIp6Range(&ipv6);
×
2136
    TSDB_CHECK_CODE(code, lino, _error);
×
2137

2138
    code = taosHashPut(pUniqueTab, &ipv6, sizeof(ipv6), &dummpy, sizeof(dummpy));
×
2139
    TSDB_CHECK_CODE(code, lino, _error);
×
2140
  }
2141
_error:
1,080✔
2142
  if (code != 0) {
1,080✔
2143
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
2144
  }
2145
  return code;
1,080✔
2146
}
2147

2148
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
100,478✔
2149
  int32_t  code = 0;
100,478✔
2150
  int32_t  lino = 0;
100,478✔
2151
  SUserObj userObj = {0};
100,478✔
2152

2153
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
100,478✔
2154
  if (userObj.passwords == NULL) {
100,478✔
2155
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2156
  }
2157
  userObj.numOfPasswords = 1;
100,478✔
2158

2159
  if (pCreate->isImport == 1) {
100,478✔
2160
    memset(userObj.salt, 0, sizeof(userObj.salt));
×
2161
    memcpy(userObj.passwords[0].pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
2162
  } else {
2163
    generateSalt(userObj.salt, sizeof(userObj.salt));
100,478✔
2164
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.passwords[0].pass);
100,478✔
2165
    userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
100,478✔
2166
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino, _OVER);
100,478✔
2167
  }
2168
  userObj.passwords[0].setTime = taosGetTimestampSec();
100,478✔
2169

2170
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
100,478✔
2171
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
100,478✔
2172
  if (pCreate->totpseed[0] != 0) {
100,478✔
2173
    int len = taosGenerateTotpSecret(pCreate->totpseed, 0, userObj.totpsecret, sizeof(userObj.totpsecret));
×
2174
    if (len < 0) {
×
2175
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
2176
    }
2177
  }
2178

2179
  userObj.createdTime = taosGetTimestampMs();
100,478✔
2180
  userObj.updateTime = userObj.createdTime;
100,478✔
2181
  userObj.superUser = 0;  // pCreate->superUser;
100,478✔
2182
  userObj.sysInfo = pCreate->sysInfo;
100,478✔
2183
  userObj.enable = pCreate->enable;
100,478✔
2184
  userObj.createdb = pCreate->createDb;
100,478✔
2185

2186
  userObj.changePass = pCreate->changepass;
100,478✔
2187
  userObj.sessionPerUser = pCreate->sessionPerUser;
100,478✔
2188
  userObj.connectTime = pCreate->connectTime;
100,478✔
2189
  userObj.connectIdleTime = pCreate->connectIdleTime;
100,478✔
2190
  userObj.callPerSession = pCreate->callPerSession;
100,478✔
2191
  userObj.vnodePerCall = pCreate->vnodePerCall;
100,478✔
2192
  userObj.failedLoginAttempts = pCreate->failedLoginAttempts;
100,478✔
2193
  userObj.passwordLifeTime = pCreate->passwordLifeTime;
100,478✔
2194
  userObj.passwordReuseTime = pCreate->passwordReuseTime;
100,478✔
2195
  userObj.passwordReuseMax = pCreate->passwordReuseMax;
100,478✔
2196
  userObj.passwordLockTime = pCreate->passwordLockTime;
100,478✔
2197
  userObj.passwordGraceTime = pCreate->passwordGraceTime;
100,478✔
2198
  userObj.inactiveAccountTime = pCreate->inactiveAccountTime;
100,478✔
2199
  userObj.allowTokenNum = pCreate->allowTokenNum;
100,478✔
2200

2201
  if (pCreate->numIpRanges == 0) {
100,478✔
2202
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
99,398✔
2203
  } else {
2204
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
1,080✔
2205
    if (pUniqueTab == NULL) {
1,080✔
2206
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2207
    }
2208
    int32_t dummpy = 0;
1,080✔
2209
    
2210
    for (int i = 0; i < pCreate->numIpRanges; i++) {
2,878✔
2211
      SIpRange range = {0};
1,798✔
2212
      copyIpRange(&range, pCreate->pIpDualRanges + i);
1,798✔
2213
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
1,798✔
2214
        taosHashCleanup(pUniqueTab);
×
2215
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2216
      }
2217
    }
2218

2219
    code = addDefaultIpToTable(tsEnableIpv6, pUniqueTab);
1,080✔
2220
    if (code != 0) {
1,080✔
2221
      taosHashCleanup(pUniqueTab);
×
2222
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2223
    }
2224

2225
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,080✔
2226
      taosHashCleanup(pUniqueTab);
×
2227
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2228
    }
2229

2230
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
1,080✔
2231
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
1,080✔
2232
    if (p == NULL) {
1,080✔
2233
      taosHashCleanup(pUniqueTab);
×
2234
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2235
    }
2236

2237
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
1,080✔
2238
    for (int32_t i = 0; i < numOfRanges; i++) {
3,237✔
2239
      size_t    len = 0;
2,157✔
2240
      SIpRange *key = taosHashGetKey(pIter, &len);
2,157✔
2241
      memcpy(&p->pIpRanges[i], key, sizeof(SIpRange));
2,157✔
2242
      pIter = taosHashIterate(pUniqueTab, pIter);
2,157✔
2243
    }
2244

2245
    taosHashCleanup(pUniqueTab);
1,080✔
2246
    p->num = numOfRanges;
1,080✔
2247
    sortIpWhiteList(p);
1,080✔
2248
    userObj.pIpWhiteListDual = p;
1,080✔
2249
  }
2250

2251
  if (pCreate->numTimeRanges == 0) {
100,478✔
2252
    userObj.pTimeWhiteList = (SDateTimeWhiteList*)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
100,478✔
2253
    if (userObj.pTimeWhiteList == NULL) {
100,478✔
2254
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2255
    }
2256
  } else {
2257
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
2258
    if (pUniqueTab == NULL) {
×
2259
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2260
    }
2261
    int32_t dummpy = 0;
×
2262
    
2263
    for (int i = 0; i < pCreate->numIpRanges; i++) {
×
2264
      SDateTimeRange* src = pCreate->pTimeRanges + i;
×
2265
      SDateTimeWhiteListItem range = {0};
×
2266
      DateTimeRangeToWhiteListItem(&range, src);
×
2267

2268
      // no need to add expired range
2269
      if (isDateTimeWhiteListItemExpired(&range)) {
×
2270
        continue;
×
2271
      }
2272

2273
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
×
2274
        taosHashCleanup(pUniqueTab);
×
2275
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2276
      }
2277
    }
2278

2279
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_TIME_RANGE) {
×
2280
      taosHashCleanup(pUniqueTab);
×
2281
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
2282
    }
2283

2284
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
×
2285
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
2286
    if (p == NULL) {
×
2287
      taosHashCleanup(pUniqueTab);
×
2288
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2289
    }
2290

2291
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
×
2292
    for (int32_t i = 0; i < numOfRanges; i++) {
×
2293
      size_t    len = 0;
×
2294
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
×
2295
      memcpy(p->ranges + i, key, sizeof(SDateTimeWhiteListItem));
×
2296
      pIter = taosHashIterate(pUniqueTab, pIter);
×
2297
    }
2298

2299
    taosHashCleanup(pUniqueTab);
×
2300
    p->num = numOfRanges;
×
2301
    sortTimeWhiteList(p);
×
2302
    userObj.pTimeWhiteList = p;
×
2303
  }
2304

2305
  userObj.ipWhiteListVer = taosGetTimestampMs();
100,478✔
2306
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
100,478✔
2307

2308
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
100,478✔
2309
  if (pTrans == NULL) {
100,478✔
2310
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
2311
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2312
  }
2313
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
100,478✔
2314

2315
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
100,478✔
2316
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
100,478✔
2317
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
2318
    mndTransDrop(pTrans);
×
2319
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2320
  }
2321
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
100,478✔
2322

2323
  if (mndTransPrepare(pMnode, pTrans) != 0) {
100,478✔
2324
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2325
    mndTransDrop(pTrans);
×
2326
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2327
  }
2328

2329
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
100,478✔
2330
    mndTransDrop(pTrans);
×
2331
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2332
  }
2333

2334
  mndTransDrop(pTrans);
100,478✔
2335

2336
_OVER:
100,478✔
2337
  taosMemoryFree(userObj.passwords);
100,478✔
2338
  taosMemoryFree(userObj.pIpWhiteListDual);
100,478✔
2339
  taosMemoryFree(userObj.pTimeWhiteList);
100,478✔
2340
  TAOS_RETURN(code);
100,478✔
2341
}
2342

2343

2344
static int32_t mndCheckPasswordFmt(const char *pwd) {
115,166✔
2345
  if (strcmp(pwd, "taosdata") == 0) {
115,166✔
2346
    return 0;
27,914✔
2347
  }
2348

2349
  if (tsEnableStrongPassword == 0) {
87,252✔
2350
    for (char c = *pwd; c != 0; c = *(++pwd)) {
99,188✔
2351
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
98,102✔
2352
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2353
      }
2354
    }
2355
    return 0;
1,086✔
2356
  }
2357

2358
  int32_t len = strlen(pwd);
86,166✔
2359
  if (len < TSDB_PASSWORD_MIN_LEN) {
86,166✔
2360
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
2361
  }
2362

2363
  if (len > TSDB_PASSWORD_MAX_LEN) {
86,166✔
2364
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
2365
  }
2366

2367
  if (taosIsComplexString(pwd)) {
86,166✔
2368
    return 0;
86,166✔
2369
  }
2370

2371
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2372
}
2373

2374

2375

2376
static int32_t mndCheckTotpSeedFmt(const char *seed) {
×
2377
  int32_t len = strlen(seed);
×
2378
  if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
2379
    return TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT;
×
2380
  }
2381

2382
  if (taosIsComplexString(seed)) {
×
2383
    return 0;
×
2384
  }
2385

2386
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
2387
}
2388

2389

2390

2391
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq) {
×
2392
  SMnode              *pMnode = pReq->info.node;
×
2393
  int32_t              code = 0;
×
2394
  int32_t              lino = 0;
×
2395
  int32_t              contLen = 0;
×
2396
  void                *pRsp = NULL;
×
2397
  SUserObj            *pUser = NULL;
×
2398
  SGetUserWhiteListReq wlReq = {0};
×
2399
  SUserDateTimeWhiteList wlRsp = {0};
×
2400

2401
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
×
2402
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2403
  }
2404
  mTrace("user: %s, start to get date time whitelist", wlReq.user);
×
2405

2406
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
×
2407
  if (pUser == NULL) {
×
2408
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2409
  }
2410

2411
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
2412

2413
  contLen = tSerializeSUserDateTimeWhiteList(NULL, 0, &wlRsp);
×
2414
  if (contLen < 0) {
×
2415
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2416
  }
2417
  pRsp = rpcMallocCont(contLen);
×
2418
  if (pRsp == NULL) {
×
2419
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2420
  }
2421
  
2422
  contLen = tSerializeSUserDateTimeWhiteList(pRsp, contLen, &wlRsp);
×
2423
  if (contLen < 0) {
×
2424
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2425
  }
2426

2427
_OVER:
×
2428
  mndReleaseUser(pMnode, pUser);
×
2429
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
2430
  if (code < 0) {
×
2431
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
2432
    rpcFreeCont(pRsp);
×
2433
    pRsp = NULL;
×
2434
    contLen = 0;
×
2435
  }
2436
  pReq->code = code;
×
2437
  pReq->info.rsp = pRsp;
×
2438
  pReq->info.rspLen = contLen;
×
2439

2440
  TAOS_RETURN(code);
×
2441
  return 0;
2442
}
2443

2444

2445

2446
static int32_t buildRetrieveDateTimeWhiteListRsp(SRetrieveDateTimeWhiteListRsp *pRsp) {
1,810✔
2447
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,810✔
2448
  
2449
  int32_t count = taosHashGetSize(userCache.users);
1,810✔
2450
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
1,810✔
2451
  if (pRsp->pUsers == NULL) {
1,810✔
2452
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
2453
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2454
  }
2455

2456
  count = 0;
1,810✔
2457
  void   *pIter = taosHashIterate(userCache.users, NULL);
1,810✔
2458
  while (pIter) {
3,620✔
2459
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
1,810✔
2460
    if (wl == NULL || wl->num <= 0) {
1,810✔
2461
      pIter = taosHashIterate(userCache.users, pIter);
1,810✔
2462
      continue;
1,810✔
2463
    }
2464

2465
    SUserDateTimeWhiteList *pUser = &pRsp->pUsers[count];
×
2466
    pUser->ver = userCache.verTime;
×
2467

2468
    size_t klen;
×
2469
    char  *key = taosHashGetKey(pIter, &klen);
×
2470
    (void)memcpy(pUser->user, key, klen);
×
2471

2472
    pUser->numWhiteLists = wl->num;
×
2473
    pUser->pWhiteLists = taosMemoryCalloc(wl->num, sizeof(SDateTimeWhiteListItem));
×
2474
    if (pUser->pWhiteLists == NULL) {
×
2475
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
2476
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2477
    }
2478

2479
    (void)memcpy(pUser->pWhiteLists, wl->ranges, wl->num * sizeof(SDateTimeWhiteListItem));
×
2480
    count++;
×
2481
    pIter = taosHashIterate(userCache.users, pIter);
×
2482
  }
2483

2484
  pRsp->numOfUser = count;
1,810✔
2485
  pRsp->ver = userCache.verTime;
1,810✔
2486
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,810✔
2487
  TAOS_RETURN(0);
1,810✔
2488
}
2489

2490

2491

2492
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq) {
1,810✔
2493
  int32_t        code = 0;
1,810✔
2494
  int32_t        lino = 0;
1,810✔
2495
  int32_t        len = 0;
1,810✔
2496
  void          *pRsp = NULL;
1,810✔
2497
  SRetrieveDateTimeWhiteListRsp wlRsp = {0};
1,810✔
2498

2499
  // impl later
2500
  SRetrieveWhiteListReq req = {0};
1,810✔
2501
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,810✔
2502
    code = TSDB_CODE_INVALID_MSG;
×
2503
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2504
  }
2505

2506
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
1,810✔
2507

2508
  len = tSerializeSRetrieveDateTimeWhiteListRsp(NULL, 0, &wlRsp);
1,810✔
2509
  if (len < 0) {
1,810✔
2510
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2511
  }
2512

2513
  pRsp = rpcMallocCont(len);
1,810✔
2514
  if (!pRsp) {
1,810✔
2515
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2516
  }
2517
  len = tSerializeSRetrieveDateTimeWhiteListRsp(pRsp, len, &wlRsp);
1,810✔
2518
  if (len < 0) {
1,810✔
2519
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2520
  }
2521

2522
_OVER:
1,810✔
2523
  if (code < 0) {
1,810✔
2524
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2525
    rpcFreeCont(pRsp);
×
2526
    pRsp = NULL;
×
2527
    len = 0;
×
2528
  }
2529
  pReq->code = code;
1,810✔
2530
  pReq->info.rsp = pRsp;
1,810✔
2531
  pReq->info.rspLen = len;
1,810✔
2532

2533
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
1,810✔
2534
  TAOS_RETURN(code);
1,810✔
2535
}
2536

2537

2538

2539
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
101,182✔
2540
  SMnode        *pMnode = pReq->info.node;
101,182✔
2541
  int32_t        code = 0;
101,182✔
2542
  int32_t        lino = 0;
101,182✔
2543
  SUserObj      *pUser = NULL;
101,182✔
2544
  SUserObj      *pOperUser = NULL;
101,182✔
2545
  SCreateUserReq createReq = {0};
101,182✔
2546

2547
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
101,182✔
2548
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2549
  }
2550

2551
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
101,182✔
2552

2553
#ifndef TD_ENTERPRISE
2554
  if (createReq.isImport == 1) {
2555
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
2556
  }
2557
#endif
2558

2559
  if (createReq.isImport != 1) {
101,182✔
2560
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
101,182✔
2561
  } else if (strcmp(pReq->info.conn.user, "root") != 0) {
×
2562
    mError("The operation is not permitted, user:%s", pReq->info.conn.user);
×
2563
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
2564
  }
2565

2566
  if (createReq.user[0] == 0) {
101,182✔
2567
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2568
  }
2569

2570
  if (createReq.isImport != 1) {
101,182✔
2571
    code = mndCheckPasswordFmt(createReq.pass);
101,182✔
2572
    TAOS_CHECK_GOTO(code, &lino, _OVER);
101,182✔
2573
  }
2574

2575
  if (createReq.totpseed[0] != 0) {
101,182✔
2576
    code = mndCheckTotpSeedFmt(createReq.totpseed);
×
2577
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2578
  }
2579

2580
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
101,182✔
2581
  if (pUser != NULL) {
101,182✔
2582
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
704✔
2583
  }
2584

2585
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
100,478✔
2586
  if (pOperUser == NULL) {
100,478✔
2587
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2588
  }
2589

2590
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
100,478✔
2591

2592
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
100,478✔
2593
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
100,478✔
2594

2595
  char detail[1000] = {0};
100,478✔
2596
  (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
100,478✔
2597
                  createReq.superUser, createReq.sysInfo);
100,478✔
2598
  char operation[15] = {0};
100,478✔
2599
  if (createReq.isImport == 1) {
100,478✔
2600
    tstrncpy(operation, "importUser", sizeof(operation));
×
2601
  } else {
2602
    tstrncpy(operation, "createUser", sizeof(operation));
100,478✔
2603
  }
2604

2605
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
100,478✔
2606

2607
_OVER:
101,182✔
2608
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExisting) {
101,182✔
2609
    code = 0;
×
2610
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
101,182✔
2611
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
704✔
2612
  }
2613

2614
  mndReleaseUser(pMnode, pUser);
101,182✔
2615
  mndReleaseUser(pMnode, pOperUser);
101,182✔
2616
  tFreeSCreateUserReq(&createReq);
101,182✔
2617

2618
  TAOS_RETURN(code);
101,182✔
2619
}
2620

2621

2622

2623
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq) {
140,036✔
2624
  SMnode              *pMnode = pReq->info.node;
140,036✔
2625
  int32_t              code = 0;
140,036✔
2626
  int32_t              lino = 0;
140,036✔
2627
  int32_t              contLen = 0;
140,036✔
2628
  void                *pRsp = NULL;
140,036✔
2629
  SUserObj            *pUser = NULL;
140,036✔
2630
  SGetUserWhiteListReq wlReq = {0};
140,036✔
2631
  SGetUserIpWhiteListRsp wlRsp = {0};
140,036✔
2632

2633
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
140,036✔
2634
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserIpWhiteListRsp * pRsp) = NULL;
140,036✔
2635

2636
  if (pReq->msgType == TDMT_MND_GET_USER_IP_WHITELIST_DUAL) {
140,036✔
2637
    serialFn = tSerializeSGetUserIpWhiteListDualRsp;
140,036✔
2638
    setRspFn = mndSetUserIpWhiteListDualRsp;
140,036✔
2639
  } else {
2640
    serialFn = tSerializeSGetUserIpWhiteListRsp;
×
2641
    setRspFn = mndSetUserIpWhiteListRsp;
×
2642
  }
2643
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
140,036✔
2644
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2645
  }
2646
  mTrace("user: %s, start to get ip whitelist", wlReq.user);
140,036✔
2647

2648
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
140,036✔
2649
  if (pUser == NULL) {
140,036✔
2650
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2651
  }
2652

2653
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
140,036✔
2654
  contLen = serialFn(NULL, 0, &wlRsp);
140,036✔
2655
  if (contLen < 0) {
140,036✔
2656
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2657
  }
2658
  pRsp = rpcMallocCont(contLen);
140,036✔
2659
  if (pRsp == NULL) {
140,036✔
2660
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2661
  }
2662

2663
  contLen = serialFn(pRsp, contLen, &wlRsp);
140,036✔
2664
  if (contLen < 0) {
140,036✔
2665
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2666
  }
2667

2668
_OVER:
140,036✔
2669
  mndReleaseUser(pMnode, pUser);
140,036✔
2670
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
140,036✔
2671
  if (code < 0) {
140,036✔
2672
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
2673
    rpcFreeCont(pRsp);
×
2674
    pRsp = NULL;
×
2675
    contLen = 0;
×
2676
  }
2677
  pReq->code = code;
140,036✔
2678
  pReq->info.rsp = pRsp;
140,036✔
2679
  pReq->info.rspLen = contLen;
140,036✔
2680

2681
  TAOS_RETURN(code);
140,036✔
2682
}
2683

2684

2685

2686
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
1,810✔
2687
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,810✔
2688

2689
  int32_t count = taosHashGetSize(userCache.users);
1,810✔
2690
  pUpdate->pUserIpWhite = taosMemoryCalloc(count, sizeof(SUpdateUserIpWhite));
1,810✔
2691
  if (pUpdate->pUserIpWhite == NULL) {
1,810✔
2692
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
2693
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2694
  }
2695

2696
  count = 0;
1,810✔
2697
  void   *pIter = taosHashIterate(userCache.users, NULL);
1,810✔
2698
  while (pIter) {
3,620✔
2699
    SIpWhiteListDual   *wl = (*(SCachedUserInfo**)pIter)->wlIp;
1,810✔
2700
    if (wl == NULL || wl->num <= 0) {
1,810✔
2701
      pIter = taosHashIterate(userCache.users, pIter);
×
2702
      continue;
×
2703
    }
2704

2705
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
1,810✔
2706
    pUser->ver = userCache.verIp;
1,810✔
2707

2708
    size_t klen;
1,810✔
2709
    char  *key = taosHashGetKey(pIter, &klen);
1,810✔
2710
    (void)memcpy(pUser->user, key, klen);
1,810✔
2711

2712
    pUser->numOfRange = wl->num;
1,810✔
2713
    pUser->pIpRanges = taosMemoryCalloc(wl->num, sizeof(SIpRange));
1,810✔
2714
    if (pUser->pIpRanges == NULL) {
1,810✔
2715
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
2716
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2717
    }
2718

2719
    (void)memcpy(pUser->pIpRanges, wl->pIpRanges, wl->num * sizeof(SIpRange));
1,810✔
2720
    count++;
1,810✔
2721
    pIter = taosHashIterate(userCache.users, pIter);
1,810✔
2722
  }
2723

2724
  pUpdate->numOfUser = count;
1,810✔
2725
  pUpdate->ver = userCache.verIp;
1,810✔
2726
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,810✔
2727
  TAOS_RETURN(0);
1,810✔
2728
}
2729

2730

2731

2732
int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq) {
1,810✔
2733
  int32_t        code = 0;
1,810✔
2734
  int32_t        lino = 0;
1,810✔
2735
  int32_t        len = 0;
1,810✔
2736
  void          *pRsp = NULL;
1,810✔
2737
  SUpdateIpWhite ipWhite = {0};
1,810✔
2738

2739
  // impl later
2740
  SRetrieveWhiteListReq req = {0};
1,810✔
2741
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,810✔
2742
    code = TSDB_CODE_INVALID_MSG;
×
2743
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2744
  }
2745

2746
  int32_t (*fn)(void *, int32_t, SUpdateIpWhite *) = NULL;
1,810✔
2747
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST) {
1,810✔
2748
    fn = tSerializeSUpdateIpWhite;
×
2749
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL) {
1,810✔
2750
    fn = tSerializeSUpdateIpWhiteDual;
1,810✔
2751
  }
2752

2753
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
1,810✔
2754

2755
  len = fn(NULL, 0, &ipWhite);
1,810✔
2756
  if (len < 0) {
1,810✔
2757
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2758
  }
2759

2760
  pRsp = rpcMallocCont(len);
1,810✔
2761
  if (!pRsp) {
1,810✔
2762
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2763
  }
2764
  len = fn(pRsp, len, &ipWhite);
1,810✔
2765
  if (len < 0) {
1,810✔
2766
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2767
  }
2768

2769
_OVER:
1,810✔
2770
  if (code < 0) {
1,810✔
2771
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2772
    rpcFreeCont(pRsp);
×
2773
    pRsp = NULL;
×
2774
    len = 0;
×
2775
  }
2776
  pReq->code = code;
1,810✔
2777
  pReq->info.rsp = pRsp;
1,810✔
2778
  pReq->info.rspLen = len;
1,810✔
2779

2780
  tFreeSUpdateIpWhiteReq(&ipWhite);
1,810✔
2781
  TAOS_RETURN(code);
1,810✔
2782
}
2783

2784

2785

2786
void mndUpdateUser(SMnode *pMnode, SUserObj *pUser, SRpcMsg *pReq) {
×
2787
  int32_t code = 0;
×
2788
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "update-user");
×
2789
  if (pTrans == NULL) {
×
2790
    mError("user:%s, failed to update since %s", pUser->user, terrstr());
×
2791
    return;
×
2792
  }
2793
  mInfo("trans:%d, used to update user:%s", pTrans->id, pUser->user);
×
2794

2795
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
×
2796
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
2797
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2798
    mndTransDrop(pTrans);
×
2799
    return;
×
2800
  }
2801
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
2802
  if (code < 0) {
×
2803
    mndTransDrop(pTrans);
×
2804
    return;
×
2805
  }
2806

2807
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
2808
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2809
    mndTransDrop(pTrans);
×
2810
    return;
×
2811
  }
2812

2813
  mndTransDrop(pTrans);
×
2814
}
2815

2816

2817

2818
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
915,784✔
2819
  int32_t code = 0;
915,784✔
2820
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
915,784✔
2821
  if (pTrans == NULL) {
915,784✔
2822
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
×
2823
    TAOS_RETURN(terrno);
×
2824
  }
2825
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
915,784✔
2826

2827
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
915,784✔
2828
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
915,784✔
2829
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2830
    mndTransDrop(pTrans);
×
2831
    TAOS_RETURN(terrno);
×
2832
  }
2833
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
915,784✔
2834
  if (code < 0) {
915,784✔
2835
    mndTransDrop(pTrans);
×
2836
    TAOS_RETURN(code);
×
2837
  }
2838

2839
  if (mndTransPrepare(pMnode, pTrans) != 0) {
915,784✔
2840
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2841
    mndTransDrop(pTrans);
×
2842
    TAOS_RETURN(terrno);
×
2843
  }
2844
  if ((code = userCacheUpdateWhiteList(pMnode, pNew)) != 0) {
915,784✔
2845
    mndTransDrop(pTrans);
×
2846
    TAOS_RETURN(code);
×
2847
  }
2848
  mndTransDrop(pTrans);
915,784✔
2849
  return 0;
915,784✔
2850
}
2851

2852
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
14,132,215✔
2853
  int32_t code = 0;
14,132,215✔
2854

2855
  *ppNew =
14,132,215✔
2856
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
14,132,215✔
2857
  if (*ppNew == NULL) {
14,132,215✔
2858
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2859
    TAOS_RETURN(code);
×
2860
  }
2861

2862
  char *db = taosHashIterate(pOld, NULL);
14,132,215✔
2863
  while (db != NULL) {
15,291,790✔
2864
    int32_t len = strlen(db) + 1;
1,159,575✔
2865
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
1,159,575✔
2866
      taosHashCancelIterate(pOld, db);
×
2867
      taosHashCleanup(*ppNew);
×
2868
      TAOS_RETURN(code);
×
2869
    }
2870
    db = taosHashIterate(pOld, db);
1,159,575✔
2871
  }
2872

2873
  TAOS_RETURN(code);
14,132,215✔
2874
}
2875

2876
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
12,452,822✔
2877

2878
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
1,679,393✔
2879

2880
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
443,465✔
2881
                                  SSdb *pSdb) {
2882
  void *pIter = NULL;
443,465✔
2883
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
443,465✔
2884

2885
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
443,465✔
2886
  int32_t len = strlen(tbFName) + 1;
443,465✔
2887

2888
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
513,384✔
2889
    char *value = taosHashGet(hash, tbFName, len);
69,919✔
2890
    if (value != NULL) {
69,919✔
2891
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2892
    }
2893

2894
    int32_t condLen = alterReq->tagCondLen;
69,919✔
2895
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
69,919✔
2896
  } else {
2897
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
373,546✔
2898
  }
2899

2900
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
443,465✔
2901
  int32_t  ref = 1;
443,465✔
2902
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
443,465✔
2903
  if (NULL != currRef) {
443,465✔
2904
    ref = (*currRef) + 1;
257,649✔
2905
  }
2906
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
443,465✔
2907

2908
  TAOS_RETURN(0);
443,465✔
2909
}
2910

2911
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
406,427✔
2912
                                        SSdb *pSdb) {
2913
  void *pIter = NULL;
406,427✔
2914
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
406,427✔
2915
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
406,427✔
2916
  int32_t len = strlen(tbFName) + 1;
406,427✔
2917

2918
  if (taosHashRemove(hash, tbFName, len) != 0) {
406,427✔
2919
    TAOS_RETURN(0);  // not found
1,356✔
2920
  }
2921

2922
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
405,071✔
2923
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
405,071✔
2924
  if (NULL == currRef) {
405,071✔
2925
    return 0;
×
2926
  }
2927

2928
  if (1 == *currRef) {
405,071✔
2929
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
172,541✔
2930
      TAOS_RETURN(0);  // not found
×
2931
    }
2932
    return 0;
172,541✔
2933
  }
2934
  int32_t ref = (*currRef) - 1;
232,530✔
2935
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
232,530✔
2936

2937
  return 0;
232,530✔
2938
}
2939

2940
static char *mndUserAuditTypeStr(int32_t type) {
×
2941
  #if 0
2942
  if (type == TSDB_ALTER_USER_PASSWD) {
2943
    return "changePassword";
2944
  }
2945
  if (type == TSDB_ALTER_USER_SUPERUSER) {
2946
    return "changeSuperUser";
2947
  }
2948
  if (type == TSDB_ALTER_USER_ENABLE) {
2949
    return "enableUser";
2950
  }
2951
  if (type == TSDB_ALTER_USER_SYSINFO) {
2952
    return "userSysInfo";
2953
  }
2954
  if (type == TSDB_ALTER_USER_CREATEDB) {
2955
    return "userCreateDB";
2956
  }
2957
    #endif
2958
  return "error";
×
2959
}
2960

2961
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
880,964✔
2962
  SSdb   *pSdb = pMnode->pSdb;
880,964✔
2963
  void   *pIter = NULL;
880,964✔
2964
  int32_t code = 0;
880,964✔
2965
  int32_t lino = 0;
880,964✔
2966

2967
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
880,964✔
2968
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
753,854✔
2969
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
127,110✔
2970
      int32_t len = strlen(pAlterReq->objname) + 1;
123,563✔
2971
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
123,563✔
2972
      if (pDb == NULL) {
123,563✔
2973
        mndReleaseDb(pMnode, pDb);
2,814✔
2974
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
2,814✔
2975
      }
2976
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
120,749✔
2977
          0) {
2978
        mndReleaseDb(pMnode, pDb);
×
2979
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2980
      }
2981
      mndReleaseDb(pMnode, pDb);
120,749✔
2982
    } else {
2983
      while (1) {
5,713✔
2984
        SDbObj *pDb = NULL;
9,260✔
2985
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
9,260✔
2986
        if (pIter == NULL) break;
9,260✔
2987
        int32_t len = strlen(pDb->name) + 1;
5,713✔
2988
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,713✔
2989
          sdbRelease(pSdb, pDb);
×
2990
          sdbCancelFetch(pSdb, pIter);
×
2991
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2992
        }
2993
        sdbRelease(pSdb, pDb);
5,713✔
2994
      }
2995
    }
2996
  }
2997

2998
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
878,150✔
2999
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
755,339✔
3000
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
122,811✔
3001
      int32_t len = strlen(pAlterReq->objname) + 1;
119,616✔
3002
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
119,616✔
3003
      if (pDb == NULL) {
119,616✔
3004
        mndReleaseDb(pMnode, pDb);
361✔
3005
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
361✔
3006
      }
3007
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
119,255✔
3008
          0) {
3009
        mndReleaseDb(pMnode, pDb);
×
3010
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3011
      }
3012
      mndReleaseDb(pMnode, pDb);
119,255✔
3013
    } else {
3014
      while (1) {
5,361✔
3015
        SDbObj *pDb = NULL;
8,556✔
3016
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
8,556✔
3017
        if (pIter == NULL) break;
8,556✔
3018
        int32_t len = strlen(pDb->name) + 1;
5,361✔
3019
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,361✔
3020
          sdbRelease(pSdb, pDb);
×
3021
          sdbCancelFetch(pSdb, pIter);
×
3022
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3023
        }
3024
        sdbRelease(pSdb, pDb);
5,361✔
3025
      }
3026
    }
3027
  }
3028

3029
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,789✔
3030
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
764,297✔
3031
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
113,492✔
3032
      int32_t len = strlen(pAlterReq->objname) + 1;
109,945✔
3033
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
109,945✔
3034
      if (pDb == NULL) {
109,945✔
3035
        mndReleaseDb(pMnode, pDb);
352✔
3036
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
352✔
3037
      }
3038
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
109,593✔
3039
      if (code < 0) {
109,593✔
3040
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3041
      }
3042
      mndReleaseDb(pMnode, pDb);
109,593✔
3043
    } else {
3044
      taosHashClear(pNewUser->readDbs);
3,547✔
3045
    }
3046
  }
3047

3048
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3049
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
764,266✔
3050
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
113,171✔
3051
      int32_t len = strlen(pAlterReq->objname) + 1;
109,272✔
3052
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
109,272✔
3053
      if (pDb == NULL) {
109,272✔
3054
        mndReleaseDb(pMnode, pDb);
×
3055
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
3056
      }
3057
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
109,272✔
3058
      if (code < 0) {
109,272✔
3059
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3060
      }
3061
      mndReleaseDb(pMnode, pDb);
109,272✔
3062
    } else {
3063
      taosHashClear(pNewUser->writeDbs);
3,899✔
3064
    }
3065
  }
3066

3067
  SHashObj *pReadTbs = pNewUser->readTbs;
877,437✔
3068
  SHashObj *pWriteTbs = pNewUser->writeTbs;
877,437✔
3069
  SHashObj *pAlterTbs = pNewUser->alterTbs;
877,437✔
3070

3071
#ifdef TD_ENTERPRISE
3072
  if (pAlterReq->isView) {
877,437✔
3073
    pReadTbs = pNewUser->readViews;
13,965✔
3074
    pWriteTbs = pNewUser->writeViews;
13,965✔
3075
    pAlterTbs = pNewUser->alterViews;
13,965✔
3076
  }
3077
#endif
3078

3079
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3080
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
702,119✔
3081
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
175,318✔
3082
  }
3083

3084
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3085
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
701,496✔
3086
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
175,941✔
3087
  }
3088

3089
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3090
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
785,231✔
3091
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
92,206✔
3092
  }
3093

3094
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3095
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
715,491✔
3096
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
161,946✔
3097
  }
3098

3099
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3100
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
712,970✔
3101
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
164,467✔
3102
  }
3103

3104
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,437✔
3105
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
797,423✔
3106
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
80,014✔
3107
  }
3108

3109
#ifdef USE_TOPIC
3110
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
877,437✔
3111
    int32_t      len = strlen(pAlterReq->objname) + 1;
6,203✔
3112
    SMqTopicObj *pTopic = NULL;
6,203✔
3113
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
6,203✔
3114
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3115
    }
3116
    taosRLockLatch(&pTopic->lock);
6,203✔
3117
    code = taosHashPut(pNewUser->topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
6,203✔
3118
    taosRUnLockLatch(&pTopic->lock);
6,203✔
3119
    mndReleaseTopic(pMnode, pTopic);
6,203✔
3120
    TAOS_CHECK_GOTO(code, &lino, _OVER);
6,203✔
3121
  }
3122

3123
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
877,437✔
3124
    int32_t      len = strlen(pAlterReq->objname) + 1;
4,066✔
3125
    SMqTopicObj *pTopic = NULL;
4,066✔
3126
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
4,066✔
3127
      TAOS_CHECK_GOTO(code, &lino, _OVER);
358✔
3128
    }
3129
    taosRLockLatch(&pTopic->lock);
3,708✔
3130
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
3,708✔
3131
    if (code < 0) {
3,708✔
3132
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
3133
    }
3134
    taosRUnLockLatch(&pTopic->lock);
3,708✔
3135
    mndReleaseTopic(pMnode, pTopic);
3,708✔
3136
  }
3137
#endif
3138
_OVER:
880,964✔
3139
  if (code < 0) {
880,964✔
3140
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
3,885✔
3141
  }
3142
  TAOS_RETURN(code);
880,964✔
3143
}
3144

3145
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
947,097✔
3146
  SMnode       *pMnode = pReq->info.node;
947,097✔
3147
  SSdb         *pSdb = pMnode->pSdb;
947,097✔
3148
  void         *pIter = NULL;
947,097✔
3149
  int32_t       code = 0;
947,097✔
3150
  int32_t       lino = 0;
947,097✔
3151
  SUserObj     *pUser = NULL;
947,097✔
3152
  SUserObj     *pOperUser = NULL;
947,097✔
3153
  SUserObj      newUser = {0};
947,097✔
3154
  SAlterUserReq alterReq = {0};
947,097✔
3155
  TAOS_CHECK_GOTO(tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq), &lino, _OVER);
947,097✔
3156

3157
  mInfo("user:%s, start to alter", alterReq.user);
947,097✔
3158

3159
  if (alterReq.user[0] == 0) {
947,097✔
3160
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3161
  }
3162

3163
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
947,097✔
3164

3165
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
938,897✔
3166
  if (pOperUser == NULL) {
938,897✔
3167
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3168
  }
3169

3170
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
938,897✔
3171
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
920,032✔
3172

3173
  if (alterReq.hasPassword) {
920,032✔
3174
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(alterReq.pass), &lino, _OVER);
13,984✔
3175
    if (newUser.salt[0] == 0) {
13,984✔
3176
      generateSalt(newUser.salt, sizeof(newUser.salt));
726✔
3177
    }
3178
    char pass[TSDB_PASSWORD_LEN] = {0};
13,984✔
3179
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
13,984✔
3180
    pass[sizeof(pass) - 1] = 0;
13,984✔
3181
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
13,984✔
3182

3183
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
23,435✔
3184
      for(int32_t i = 0; i < newUser.numOfPasswords; ++i) {
29,401✔
3185
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
19,950✔
3186
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
363✔
3187
        }
3188
      }
3189
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
9,451✔
3190
      if (passwords == NULL) {
9,451✔
3191
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3192
      }
3193
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
9,451✔
3194
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
9,451✔
3195
      passwords[0].setTime = taosGetTimestampSec();
9,451✔
3196
      taosMemoryFree(newUser.passwords);
9,451✔
3197
      newUser.passwords = passwords;
9,451✔
3198
      ++newUser.numOfPasswords;
9,451✔
3199
      ++newUser.passVersion;
9,451✔
3200
      if (strcmp(newUser.user, pOperUser->user) == 0) {
9,451✔
3201
        // if user change own password, set changePass to 2 so that user won't be
3202
        // forced to change password at next login
3203
        newUser.changePass = 2;
6,193✔
3204
      }
3205
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
4,170✔
3206
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
2,834✔
3207
      newUser.passwords[0].setTime = taosGetTimestampSec();
2,834✔
3208
      ++newUser.passVersion;
2,834✔
3209
      if (strcmp(newUser.user, pOperUser->user) == 0) {
2,834✔
3210
        newUser.changePass = 2;
350✔
3211
      }
3212
    }
3213
  }
3214

3215
  if (alterReq.hasTotpseed) {
919,669✔
3216
    if (alterReq.totpseed[0] == 0) { // clear totp secret
×
3217
      memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
×
3218
    } else if (taosGenerateTotpSecret(alterReq.totpseed, 0, newUser.totpsecret, sizeof(newUser.totpsecret)) < 0) {
×
3219
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
3220
    }
3221
  }
3222

3223
  if (alterReq.hasEnable) {
919,669✔
3224
    newUser.enable = alterReq.enable; // lock or unlock user manually
2,402✔
3225
    if (newUser.enable) {
2,402✔
3226
      // reset login info to allow login immediately
3227
      userCacheResetLoginInfo(newUser.user);
1,339✔
3228
    }
3229
  }
3230

3231
  if (alterReq.hasSysinfo) newUser.sysInfo = alterReq.sysinfo;
919,669✔
3232
  if (alterReq.hasCreatedb) newUser.createdb = alterReq.createdb;
919,669✔
3233
  if (alterReq.hasChangepass) newUser.changePass = alterReq.changepass;
919,669✔
3234
  if (alterReq.hasSessionPerUser) newUser.sessionPerUser = alterReq.sessionPerUser;
919,669✔
3235
  if (alterReq.hasConnectTime) newUser.connectTime = alterReq.connectTime;
919,669✔
3236
  if (alterReq.hasConnectIdleTime) newUser.connectIdleTime = alterReq.connectIdleTime;
919,669✔
3237
  if (alterReq.hasCallPerSession) newUser.callPerSession = alterReq.callPerSession;
919,669✔
3238
  if (alterReq.hasVnodePerCall) newUser.vnodePerCall = alterReq.vnodePerCall;
919,669✔
3239
  if (alterReq.hasFailedLoginAttempts) newUser.failedLoginAttempts = alterReq.failedLoginAttempts;
919,669✔
3240
  if (alterReq.hasPasswordLifeTime) newUser.passwordLifeTime = alterReq.passwordLifeTime;
919,669✔
3241
  if (alterReq.hasPasswordReuseTime) newUser.passwordReuseTime = alterReq.passwordReuseTime;
919,669✔
3242
  if (alterReq.hasPasswordReuseMax) newUser.passwordReuseMax = alterReq.passwordReuseMax;
919,669✔
3243
  if (alterReq.hasPasswordLockTime) newUser.passwordLockTime = alterReq.passwordLockTime;
919,669✔
3244
  if (alterReq.hasPasswordGraceTime) newUser.passwordGraceTime = alterReq.passwordGraceTime;
919,669✔
3245
  if (alterReq.hasInactiveAccountTime) newUser.inactiveAccountTime = alterReq.inactiveAccountTime;
919,669✔
3246
  if (alterReq.hasAllowTokenNum) newUser.allowTokenNum = alterReq.allowTokenNum;
919,669✔
3247

3248

3249
  if (alterReq.numDropIpRanges > 0 || alterReq.numIpRanges > 0) {
919,669✔
3250
    int32_t dummy = 0;
718✔
3251

3252
    // put previous ip whitelist into hash table
3253
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
718✔
3254
    if (m == NULL) {
718✔
3255
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3256
    }
3257

3258
    for (int32_t i = 0; i < newUser.pIpWhiteListDual->num; i++) {
2,513✔
3259
      SIpRange range;
1,795✔
3260
      copyIpRange(&range, newUser.pIpWhiteListDual->pIpRanges + i);
1,795✔
3261
      code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
1,795✔
3262
      if (code != 0) {
1,795✔
3263
        taosHashCleanup(m);
×
3264
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3265
      }
3266
    }
3267

3268
    if (alterReq.numDropIpRanges > 0) {
718✔
3269
      for (int32_t i = 0; i < alterReq.numDropIpRanges; i++) {
718✔
3270
        if (taosHashGetSize(m) == 0) {
359✔
3271
          break;
×
3272
        }
3273

3274
        SIpRange range;
359✔
3275
        copyIpRange(&range, alterReq.pDropIpRanges + i);
359✔
3276

3277
        // for white list, drop default ip ranges is allowed, otherwise, we can never
3278
        // convert white list to black list.
3279

3280
        code = taosHashRemove(m, &range, sizeof(range));
359✔
3281
        if (code == TSDB_CODE_NOT_FOUND) {
359✔
3282
          // treat not exist as success
3283
          code = 0;
359✔
3284
        }
3285
        if (code != 0) {
359✔
3286
          taosHashCleanup(m);
×
3287
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3288
        }
3289
      }
3290
    }
3291

3292
    if (alterReq.numIpRanges > 0) {
718✔
3293
      for (int32_t i = 0; i < alterReq.numIpRanges; i++) {
718✔
3294
        SIpRange range;
359✔
3295
        copyIpRange(&range, alterReq.pIpRanges + i);
359✔
3296
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
359✔
3297
        if (code != 0) {
359✔
3298
          taosHashCleanup(m);
×
3299
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3300
        }
3301
      }
3302
    }
3303

3304
    int32_t numOfRanges = taosHashGetSize(m);
718✔
3305
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
718✔
3306
      taosHashCleanup(m);
×
3307
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
3308
    }
3309

3310
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
718✔
3311
    if (p == NULL) {
718✔
3312
      taosHashCleanup(m);
×
3313
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3314
    }
3315

3316
    void *pIter = taosHashIterate(m, NULL);
718✔
3317
    int32_t i = 0;
718✔
3318
    while (pIter) {
2,872✔
3319
      size_t len = 0;
2,154✔
3320
      SIpRange *key = taosHashGetKey(pIter, &len);
2,154✔
3321
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
2,154✔
3322
      pIter = taosHashIterate(m, pIter);
2,154✔
3323
      i++;
2,154✔
3324
    }
3325

3326
    taosHashCleanup(m);
718✔
3327
    p->num = numOfRanges;
718✔
3328
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
718✔
3329
    sortIpWhiteList(p);
718✔
3330
    newUser.pIpWhiteListDual = p;
718✔
3331

3332
    newUser.ipWhiteListVer++;
718✔
3333
  }
3334

3335

3336
  if (alterReq.numTimeRanges > 0 || alterReq.numDropTimeRanges) {
919,669✔
3337
    int32_t dummy = 0;
×
3338

3339
    // put previous ip whitelist into hash table
3340
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
3341
    if (m == NULL) {
×
3342
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3343
    }
3344

3345
    for (int32_t i = 0; i < newUser.pTimeWhiteList->num; i++) {
×
3346
      SDateTimeWhiteListItem *range = &newUser.pTimeWhiteList->ranges[i];
×
3347
      if (isDateTimeWhiteListItemExpired(range)) {
×
3348
        continue;
×
3349
      }
3350
      code = taosHashPut(m, range, sizeof(*range), &dummy, sizeof(dummy));
×
3351
      if (code != 0) {
×
3352
        taosHashCleanup(m);
×
3353
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3354
      }
3355
    }
3356

3357
    if (alterReq.numDropTimeRanges > 0) {
×
3358
      for (int32_t i = 0; i < alterReq.numDropTimeRanges; i++) {
×
3359
        if (taosHashGetSize(m) == 0) {
×
3360
          break;
×
3361
        }
3362
        SDateTimeWhiteListItem range = { 0 };
×
3363
        DateTimeRangeToWhiteListItem(&range, alterReq.pDropTimeRanges + i);
×
3364

3365
        code = taosHashRemove(m, &range, sizeof(range));
×
3366
        if (code == TSDB_CODE_NOT_FOUND) {
×
3367
          // treat not exist as success
3368
          code = 0;
×
3369
        }
3370
        if (code != 0) {
×
3371
          taosHashCleanup(m);
×
3372
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3373
        }
3374
      }
3375
    }
3376

3377
    if (alterReq.numTimeRanges > 0) {
×
3378
      for (int32_t i = 0; i < alterReq.numTimeRanges; i++) {
×
3379
        SDateTimeWhiteListItem range = { 0 };
×
3380
        DateTimeRangeToWhiteListItem(&range, alterReq.pTimeRanges + i);
×
3381
        if (isDateTimeWhiteListItemExpired(&range)) {
×
3382
          continue;
×
3383
        }
3384
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
×
3385
        if (code != 0) {
×
3386
          taosHashCleanup(m);
×
3387
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3388
        }
3389
      }
3390
    }
3391

3392
    int32_t numOfRanges = taosHashGetSize(m);
×
3393
    if (numOfRanges > MND_MAX_USER_TIME_RANGE) {
×
3394
      taosHashCleanup(m);
×
3395
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
3396
    }
3397

3398
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
3399
    if (p == NULL) {
×
3400
      taosHashCleanup(m);
×
3401
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3402
    }
3403

3404
    void *pIter = taosHashIterate(m, NULL);
×
3405
    int32_t i = 0;
×
3406
    while (pIter) {
×
3407
      size_t len = 0;
×
3408
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
×
3409
      memcpy(&p->ranges[i], key, sizeof(SDateTimeWhiteListItem));
×
3410
      pIter = taosHashIterate(m, pIter);
×
3411
      i++;
×
3412
    }
3413

3414
    taosHashCleanup(m);
×
3415
    p->num = numOfRanges;
×
3416
    taosMemoryFreeClear(newUser.pTimeWhiteList);
×
3417
    sortTimeWhiteList(p);
×
3418
    newUser.pTimeWhiteList = p;
×
3419
    newUser.timeWhiteListVer++;
×
3420
  }
3421

3422

3423
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
919,669✔
3424
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
880,964✔
3425
  }
3426

3427
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
915,784✔
3428
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
915,784✔
3429

3430
#if 0
3431
  if (alterReq.passIsMd5 == 0) {
3432
    if (TSDB_ALTER_USER_PASSWD == alterReq.alterType) {
3433
      code = mndCheckPasswordFmt(alterReq.pass, len);
3434
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3435
    }
3436
  }
3437

3438
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
3439

3440
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
3441
  if (pOperUser == NULL) {
3442
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
3443
  }
3444

3445
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
3446

3447
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3448

3449
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
3450
    if (alterReq.passIsMd5 == 1) {
3451
      (void)memcpy(newUser.pass, alterReq.pass, TSDB_PASSWORD_LEN);
3452
    } else {
3453
      taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), newUser.pass);
3454
    }
3455

3456
    TAOS_CHECK_GOTO(mndEncryptPass(newUser.pass, &newUser.passEncryptAlgorithm), &lino, _OVER);
3457

3458
    if (0 != strncmp(pUser->pass, newUser.pass, TSDB_PASSWORD_LEN)) {
3459
      ++newUser.passVersion;
3460
    }
3461
  }
3462

3463
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
3464
    newUser.superUser = alterReq.superUser;
3465
  }
3466

3467
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
3468
    newUser.enable = alterReq.enable;
3469
  }
3470

3471
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
3472
    newUser.sysInfo = alterReq.sysInfo;
3473
  }
3474

3475
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3476
    newUser.createdb = alterReq.createdb;
3477
  }
3478

3479
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
3480
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
3481
  }
3482

3483
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_ALLOWED_HOST) {
3484
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3485

3486
    int32_t           num = pUser->pIpWhiteListDual->num + alterReq.numIpRanges;
3487
    int32_t           idx = pUser->pIpWhiteListDual->num;
3488
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3489

3490
    if (pNew == NULL) {
3491
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3492
    }
3493

3494
    bool exist = false;
3495
    (void)memcpy(pNew->pIpRanges, pUser->pIpWhiteListDual->pIpRanges, sizeof(SIpRange) * idx);
3496
    for (int i = 0; i < alterReq.numIpRanges; i++) {
3497
      SIpRange range = {0};
3498
      if (alterReq.pIpDualRanges == NULL) {
3499
        range.type = 0;
3500
        memcpy(&range.ipV4, &alterReq.pIpRanges[i], sizeof(SIpV4Range));
3501
      } else {
3502
        memcpy(&range, &alterReq.pIpDualRanges[i], sizeof(SIpRange));
3503
        range = alterReq.pIpDualRanges[i];
3504
      }
3505
      if (!isRangeInIpWhiteList(pUser->pIpWhiteListDual, &range)) {
3506
        // already exist, just ignore;
3507
        (void)memcpy(&pNew->pIpRanges[idx], &range, sizeof(SIpRange));
3508
        idx++;
3509
        continue;
3510
      } else {
3511
        exist = true;
3512
      }
3513
    }
3514
    if (exist) {
3515
      taosMemoryFree(pNew);
3516
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_EXIST, &lino, _OVER);
3517
    }
3518
    pNew->num = idx;
3519
    newUser.pIpWhiteListDual = pNew;
3520
    newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3521

3522
    if (pNew->num > MND_MAX_USER_IP_RANGE) {
3523
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
3524
    }
3525
  }
3526
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_ALLOWED_HOST) {
3527
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3528

3529
    int32_t           num = pUser->pIpWhiteListDual->num;
3530
    bool              noexist = true;
3531
    bool              localHost = false;
3532
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3533

3534
    if (pNew == NULL) {
3535
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3536
    }
3537

3538
    if (pUser->pIpWhiteListDual->num > 0) {
3539
      int idx = 0;
3540
      for (int i = 0; i < pUser->pIpWhiteListDual->num; i++) {
3541
        SIpRange *oldRange = &pUser->pIpWhiteListDual->pIpRanges[i];
3542

3543
        bool found = false;
3544
        for (int j = 0; j < alterReq.numIpRanges; j++) {
3545
          SIpRange range = {0};
3546
          if (alterReq.pIpDualRanges == NULL) {
3547
            SIpV4Range *trange = &alterReq.pIpRanges[j];
3548
            memcpy(&range.ipV4, trange, sizeof(SIpV4Range));
3549
          } else {
3550
            memcpy(&range, &alterReq.pIpDualRanges[j], sizeof(SIpRange));
3551
          }
3552

3553
          if (isDefaultRange(&range)) {
3554
            localHost = true;
3555
            break;
3556
          }
3557
          if (isIpRangeEqual(oldRange, &range)) {
3558
            found = true;
3559
            break;
3560
          }
3561
        }
3562
        if (localHost) break;
3563

3564
        if (found == false) {
3565
          (void)memcpy(&pNew->pIpRanges[idx], oldRange, sizeof(SIpRange));
3566
          idx++;
3567
        } else {
3568
          noexist = false;
3569
        }
3570
      }
3571
      pNew->num = idx;
3572
      newUser.pIpWhiteListDual = pNew;
3573
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3574

3575
    } else {
3576
      pNew->num = 0;
3577
      newUser.pIpWhiteListDual = pNew;
3578
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3579
    }
3580

3581
    if (localHost) {
3582
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
3583
    }
3584
    if (noexist) {
3585
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
3586
    }
3587
  }
3588

3589
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
3590
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
3591

3592
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
3593
    char detail[1000] = {0};
3594
    (void)tsnprintf(detail, sizeof(detail),
3595
                    "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx",
3596
                    mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
3597
                    alterReq.createdb ? 1 : 0, alterReq.tabName);
3598
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
3599
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
3600
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3601
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3602
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3603
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3604
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3605
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3606
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3607
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
3608
    if (strcmp(alterReq.objname, "1.*") != 0) {
3609
      SName name = {0};
3610
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3611
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
3612
                  alterReq.sqlLen);
3613
    } else {
3614
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3615
    }
3616
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
3617
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
3618
                alterReq.sqlLen);
3619
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
3620
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
3621
                alterReq.sqlLen);
3622
  } else {
3623
    if (strcmp(alterReq.objname, "1.*") != 0) {
3624
      SName name = {0};
3625
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3626
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
3627
                  alterReq.sqlLen);
3628
    } else {
3629
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3630
    }
3631
  }
3632

3633
#endif
3634

3635
_OVER:
947,097✔
3636
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
947,097✔
3637
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
31,313✔
3638
  }
3639

3640
  tFreeSAlterUserReq(&alterReq);
947,097✔
3641
  mndReleaseUser(pMnode, pOperUser);
947,097✔
3642
  mndReleaseUser(pMnode, pUser);
947,097✔
3643
  mndUserFreeObj(&newUser);
947,097✔
3644
  TAOS_RETURN(code);
947,097✔
3645
}
3646

3647
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
59,313✔
3648
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
59,313✔
3649
  if (pTrans == NULL) {
59,313✔
3650
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
3651
    TAOS_RETURN(terrno);
×
3652
  }
3653
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
59,313✔
3654

3655
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
59,313✔
3656
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
59,313✔
3657
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
3658
    mndTransDrop(pTrans);
×
3659
    TAOS_RETURN(terrno);
×
3660
  }
3661
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
59,313✔
3662
    mndTransDrop(pTrans);
×
3663
    TAOS_RETURN(terrno);
×
3664
  }
3665

3666
  if (mndTransPrepare(pMnode, pTrans) != 0) {
59,313✔
3667
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3668
    mndTransDrop(pTrans);
×
3669
    TAOS_RETURN(terrno);
×
3670
  }
3671

3672
  userCacheRemoveUser(pUser->user);
59,313✔
3673

3674
  mndTransDrop(pTrans);
59,313✔
3675
  TAOS_RETURN(0);
59,313✔
3676
}
3677

3678
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
59,663✔
3679
  SMnode      *pMnode = pReq->info.node;
59,663✔
3680
  int32_t      code = 0;
59,663✔
3681
  int32_t      lino = 0;
59,663✔
3682
  SUserObj    *pUser = NULL;
59,663✔
3683
  SDropUserReq dropReq = {0};
59,663✔
3684

3685
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
59,663✔
3686

3687
  mInfo("user:%s, start to drop", dropReq.user);
59,663✔
3688
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
59,663✔
3689

3690
  if (dropReq.user[0] == 0) {
59,663✔
3691
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3692
  }
3693

3694
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
59,663✔
3695

3696
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
59,313✔
3697
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
59,313✔
3698

3699
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
59,313✔
3700

3701
_OVER:
59,663✔
3702
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
59,663✔
3703
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
350✔
3704
  }
3705

3706
  mndReleaseUser(pMnode, pUser);
59,663✔
3707
  tFreeSDropUserReq(&dropReq);
59,663✔
3708
  TAOS_RETURN(code);
59,663✔
3709
}
3710

3711
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
3,684,620✔
3712
  SMnode         *pMnode = pReq->info.node;
3,684,620✔
3713
  int32_t         code = 0;
3,684,620✔
3714
  int32_t         lino = 0;
3,684,620✔
3715
  int32_t         contLen = 0;
3,684,620✔
3716
  void           *pRsp = NULL;
3,684,620✔
3717
  SUserObj       *pUser = NULL;
3,684,620✔
3718
  SGetUserAuthReq authReq = {0};
3,684,620✔
3719
  SGetUserAuthRsp authRsp = {0};
3,684,620✔
3720

3721
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
3,684,620✔
3722
  mTrace("user:%s, start to get auth", authReq.user);
3,684,620✔
3723

3724
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
3,684,620✔
3725

3726
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,683,241✔
3727

3728
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
3,683,241✔
3729
  if (contLen < 0) {
3,683,241✔
3730
    TAOS_CHECK_EXIT(contLen);
×
3731
  }
3732
  pRsp = rpcMallocCont(contLen);
3,683,241✔
3733
  if (pRsp == NULL) {
3,683,241✔
3734
    TAOS_CHECK_EXIT(terrno);
×
3735
  }
3736

3737
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
3,683,241✔
3738
  if (contLen < 0) {
3,683,241✔
3739
    TAOS_CHECK_EXIT(contLen);
×
3740
  }
3741

3742
_exit:
3,684,620✔
3743
  mndReleaseUser(pMnode, pUser);
3,684,620✔
3744
  tFreeSGetUserAuthRsp(&authRsp);
3,684,620✔
3745
  if (code < 0) {
3,684,620✔
3746
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
1,379✔
3747
    rpcFreeCont(pRsp);
1,379✔
3748
    pRsp = NULL;
1,379✔
3749
    contLen = 0;
1,379✔
3750
  }
3751
  pReq->info.rsp = pRsp;
3,684,620✔
3752
  pReq->info.rspLen = contLen;
3,684,620✔
3753
  pReq->code = code;
3,684,620✔
3754

3755
  TAOS_RETURN(code);
3,684,620✔
3756
}
3757

3758

3759
bool mndIsTotpEnabledUser(SUserObj *pUser) {
3,056,658✔
3760
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
100,808,395✔
3761
    if (pUser->totpsecret[i] != 0) {
97,752,564✔
3762
      return true;
×
3763
    }
3764
  }
3765
  return false;
3,055,831✔
3766
}
3767

3768

3769
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
20,207✔
3770
  SMnode   *pMnode = pReq->info.node;
20,207✔
3771
  SSdb     *pSdb = pMnode->pSdb;
20,207✔
3772
  int32_t   code = 0;
20,207✔
3773
  int32_t   lino = 0;
20,207✔
3774
  int32_t   numOfRows = 0;
20,207✔
3775
  SUserObj *pUser = NULL;
20,207✔
3776
  int32_t   cols = 0;
20,207✔
3777
  int8_t    flag = 0;
20,207✔
3778
  char     *pWrite = NULL;
20,207✔
3779
  char     *buf = NULL;
20,207✔
3780
  char     *varstr = NULL;
20,207✔
3781

3782
  while (numOfRows < rows) {
73,321✔
3783
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
73,321✔
3784
    if (pShow->pIter == NULL) break;
73,321✔
3785

3786
    cols = 0;
53,114✔
3787
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3788
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
53,114✔
3789
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
53,114✔
3790
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
53,114✔
3791

3792
    cols++;
53,114✔
3793
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3794
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
53,114✔
3795

3796
    cols++;
53,114✔
3797
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3798
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
53,114✔
3799

3800
    cols++;
53,114✔
3801
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3802
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
53,114✔
3803

3804
    cols++;
53,114✔
3805
    flag = pUser->createdb ? 1 : 0;
53,114✔
3806
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3807
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
53,114✔
3808

3809
    cols++;
53,114✔
3810
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3811
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
53,114✔
3812

3813
    cols++;
53,114✔
3814
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3815
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
53,114✔
3816
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
53,114✔
3817

3818
    cols++;
53,114✔
3819

3820
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
53,114✔
3821
    if (tlen != 0) {
53,114✔
3822
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
53,114✔
3823
      if (varstr == NULL) {
53,114✔
3824
        sdbRelease(pSdb, pUser);
×
3825
        sdbCancelFetch(pSdb, pShow->pIter);
×
3826
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3827
      }
3828
      varDataSetLen(varstr, tlen);
53,114✔
3829
      (void)memcpy(varDataVal(varstr), buf, tlen);
53,114✔
3830

3831
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3832
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
53,114✔
3833

3834
      taosMemoryFreeClear(buf);
53,114✔
3835
    } else {
3836
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3837
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3838
    }
3839

3840
    cols++;
53,114✔
3841
    tlen = convertTimeRangesToStr(pUser, &buf);
53,114✔
3842
    if (tlen != 0) {
53,114✔
3843
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
53,114✔
3844
      if (varstr == NULL) {
53,114✔
3845
        sdbRelease(pSdb, pUser);
×
3846
        sdbCancelFetch(pSdb, pShow->pIter);
×
3847
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3848
      }
3849
      varDataSetLen(varstr, tlen);
53,114✔
3850
      (void)memcpy(varDataVal(varstr), buf, tlen);
53,114✔
3851

3852
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,114✔
3853
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
53,114✔
3854

3855
      taosMemoryFreeClear(buf);
53,114✔
3856
    } else {
3857
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3858
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3859
    }
3860

3861
    numOfRows++;
53,114✔
3862
    sdbRelease(pSdb, pUser);
53,114✔
3863
  }
3864

3865
  pShow->numOfRows += numOfRows;
20,207✔
3866
_exit:
20,207✔
3867
  taosMemoryFreeClear(buf);
20,207✔
3868
  taosMemoryFreeClear(varstr);
20,207✔
3869
  if (code < 0) {
20,207✔
3870
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3871
    TAOS_RETURN(code);
×
3872
  }
3873
  return numOfRows;
20,207✔
3874
}
3875

3876
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
3877
  int32_t numOfRows = 0;
×
3878
#ifdef TD_ENTERPRISE
3879
  SMnode   *pMnode = pReq->info.node;
×
3880
  SSdb     *pSdb = pMnode->pSdb;
×
3881
  SUserObj *pUser = NULL;
×
3882
  int32_t   code = 0;
×
3883
  int32_t   lino = 0;
×
3884
  int32_t   cols = 0;
×
3885
  int8_t    flag = 0;
×
3886
  char     *pWrite = NULL;
×
3887
  char     *buf = NULL;
×
3888
  char     *varstr = NULL;
×
3889

3890
  while (numOfRows < rows) {
×
3891
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
3892
    if (pShow->pIter == NULL) break;
×
3893

3894
    cols = 0;
×
3895
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3896
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
3897
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
3898
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
×
3899

3900
    cols++;
×
3901
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3902
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
×
3903

3904
    cols++;
×
3905
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3906
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
×
3907

3908
    cols++;
×
3909
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3910
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
×
3911

3912
    cols++;
×
3913
    flag = pUser->createdb ? 1 : 0;
×
3914
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3915
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
×
3916

3917
    cols++;
×
3918
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3919
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
×
3920

3921
    cols++;
×
3922
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3923
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
×
3924
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
×
3925

3926
    cols++;
×
3927
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3928
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->changePass, false, pUser, pShow->pIter, _exit);
×
3929

3930
    cols++;
×
3931
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3932
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
×
3933
    STR_WITH_MAXSIZE_TO_VARSTR(pass, pUser->passwords[0].pass, pShow->pMeta->pSchemas[cols].bytes);
×
3934
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, pShow->pIter, _exit);
×
3935

3936
    cols++;
×
3937
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3938
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sessionPerUser, false, pUser, pShow->pIter, _exit);
×
3939

3940
    cols++;
×
3941
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3942
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectTime, false, pUser, pShow->pIter, _exit);
×
3943

3944
    cols++;
×
3945
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3946
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectIdleTime, false, pUser, pShow->pIter, _exit);
×
3947

3948
    cols++;
×
3949
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3950
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->callPerSession, false, pUser, pShow->pIter, _exit);
×
3951

3952
    /* not supported yet
3953
    cols++;
3954
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
3955
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerSession, false, pUser, pShow->pIter, _exit);
3956
*/
3957

3958
    cols++;
×
3959
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3960
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
×
3961

3962
    cols++;
×
3963
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3964
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLifeTime, false, pUser, pShow->pIter, _exit);
×
3965

3966
    cols++;
×
3967
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3968
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseTime, false, pUser, pShow->pIter, _exit);
×
3969

3970
    cols++;
×
3971
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3972
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseMax, false, pUser, pShow->pIter, _exit);
×
3973

3974
    cols++;
×
3975
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3976
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLockTime, false, pUser, pShow->pIter, _exit);
×
3977

3978
    cols++;
×
3979
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3980
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordGraceTime, false, pUser, pShow->pIter, _exit);
×
3981

3982
    cols++;
×
3983
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3984
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->inactiveAccountTime, false, pUser, pShow->pIter, _exit);
×
3985

3986
    cols++;
×
3987
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3988
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->allowTokenNum, false, pUser, pShow->pIter, _exit);
×
3989

3990
    cols++;
×
3991
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
×
3992
    if (tlen != 0) {
×
3993
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
3994
      if (varstr == NULL) {
×
3995
        sdbRelease(pSdb, pUser);
×
3996
        sdbCancelFetch(pSdb, pShow->pIter);
×
3997
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3998
      }
3999
      varDataSetLen(varstr, tlen);
×
4000
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
4001

4002
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4003
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
4004

4005
      taosMemoryFreeClear(buf);
×
4006
    } else {
4007
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4008
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4009
    }
4010

4011
    cols++;
×
4012
    tlen = convertTimeRangesToStr(pUser, &buf);
×
4013
    if (tlen != 0) {
×
4014
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
4015
      if (varstr == NULL) {
×
4016
        sdbRelease(pSdb, pUser);
×
4017
        sdbCancelFetch(pSdb, pShow->pIter);
×
4018
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
4019
      }
4020
      varDataSetLen(varstr, tlen);
×
4021
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
4022

4023
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4024
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
4025

4026
      taosMemoryFreeClear(buf);
×
4027
    } else {
4028
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4029
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4030
    }
4031

4032
    numOfRows++;
×
4033
    sdbRelease(pSdb, pUser);
×
4034
  }
4035

4036
  pShow->numOfRows += numOfRows;
×
4037
_exit:
×
4038
  taosMemoryFreeClear(buf);
×
4039
  taosMemoryFreeClear(varstr);
×
4040
  if (code < 0) {
×
4041
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4042
    TAOS_RETURN(code);
×
4043
  }
4044
#endif
4045
  return numOfRows;
×
4046
}
4047

4048
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
4049
  SSdb *pSdb = pMnode->pSdb;
×
4050
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4051
}
×
4052

4053
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
604,944✔
4054
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
4055
  char   *value = taosHashIterate(hash, NULL);
604,944✔
4056
  char   *user = pUser->user;
604,944✔
4057
  int32_t code = 0;
604,944✔
4058
  int32_t lino = 0;
604,944✔
4059
  int32_t cols = 0;
604,944✔
4060
  int32_t numOfRows = *pNumOfRows;
604,944✔
4061

4062
  while (value != NULL) {
688,509✔
4063
    cols = 0;
83,565✔
4064
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
83,565✔
4065
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
83,565✔
4066
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
83,565✔
4067
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
83,565✔
4068

4069
    char privilege[20] = {0};
83,565✔
4070
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
83,565✔
4071
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
83,565✔
4072
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
83,565✔
4073

4074
    size_t keyLen = 0;
83,565✔
4075
    void  *key = taosHashGetKey(value, &keyLen);
83,565✔
4076

4077
    char dbName[TSDB_DB_NAME_LEN] = {0};
83,565✔
4078
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
83,565✔
4079
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
83,565✔
4080
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
83,565✔
4081
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
83,565✔
4082
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
83,565✔
4083

4084
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
83,565✔
4085
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
83,565✔
4086
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
83,565✔
4087
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
83,565✔
4088
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
83,565✔
4089
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
83,565✔
4090

4091
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
141,411✔
4092
      SNode  *pAst = NULL;
57,846✔
4093
      int32_t sqlLen = 0;
57,846✔
4094
      size_t  bufSz = strlen(value) + 1;
57,846✔
4095
      if (bufSz < 6) bufSz = 6;
57,846✔
4096
      TAOS_MEMORY_REALLOC(*sql, bufSz);
57,846✔
4097
      if (*sql == NULL) {
57,846✔
4098
        code = terrno;
×
4099
        goto _exit;
×
4100
      }
4101
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
57,846✔
4102
      if ((*condition) == NULL) {
57,846✔
4103
        code = terrno;
×
4104
        goto _exit;
×
4105
      }
4106

4107
      if (nodesStringToNode(value, &pAst) == 0) {
57,846✔
4108
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
57,846✔
4109
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
4110
        }
4111
        nodesDestroyNode(pAst);
57,846✔
4112
      }
4113

4114
      if (sqlLen == 0) {
57,846✔
4115
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
4116
      }
4117

4118
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
57,846✔
4119

4120
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,846✔
4121
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
57,846✔
4122

4123
      char notes[2] = {0};
57,846✔
4124
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
57,846✔
4125
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,846✔
4126
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
57,846✔
4127
    } else {
4128
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
25,719✔
4129
      if ((*condition) == NULL) {
25,719✔
4130
        code = terrno;
×
4131
        goto _exit;
×
4132
      }
4133
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
25,719✔
4134
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,719✔
4135
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
25,719✔
4136

4137
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
25,719✔
4138
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
25,719✔
4139
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,719✔
4140
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
25,719✔
4141
    }
4142

4143
    numOfRows++;
83,565✔
4144
    value = taosHashIterate(hash, value);
83,565✔
4145
  }
4146
  *pNumOfRows = numOfRows;
604,944✔
4147
_exit:
604,944✔
4148
  if (code < 0) {
604,944✔
4149
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4150
    sdbRelease(pSdb, pUser);
×
4151
    sdbCancelFetch(pSdb, pShow->pIter);
×
4152
  }
4153
  TAOS_RETURN(code);
604,944✔
4154
}
4155

4156
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
19,972✔
4157
  int32_t   code = 0;
19,972✔
4158
  int32_t   lino = 0;
19,972✔
4159
  SMnode   *pMnode = pReq->info.node;
19,972✔
4160
  SSdb     *pSdb = pMnode->pSdb;
19,972✔
4161
  int32_t   numOfRows = 0;
19,972✔
4162
  SUserObj *pUser = NULL;
19,972✔
4163
  int32_t   cols = 0;
19,972✔
4164
  char     *pWrite = NULL;
19,972✔
4165
  char     *condition = NULL;
19,972✔
4166
  char     *sql = NULL;
19,972✔
4167

4168
  bool fetchNextUser = pShow->restore ? false : true;
19,972✔
4169
  pShow->restore = false;
19,972✔
4170

4171
  while (numOfRows < rows) {
120,796✔
4172
    if (fetchNextUser) {
120,796✔
4173
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
120,796✔
4174
      if (pShow->pIter == NULL) break;
120,796✔
4175
    } else {
4176
      fetchNextUser = true;
×
4177
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
4178
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
4179
      if (!pUser) {
×
4180
        continue;
×
4181
      }
4182
    }
4183

4184
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
100,824✔
4185
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
100,824✔
4186
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
100,824✔
4187
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
100,824✔
4188
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
100,824✔
4189
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
100,824✔
4190
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
100,824✔
4191
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
100,824✔
4192
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
100,824✔
4193
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
100,824✔
4194
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
100,824✔
4195
        rows) {
4196
      mInfo(
×
4197
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
4198
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
4199
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
4200
          numOfReadViews, numOfWriteViews, numOfAlterViews);
4201
      pShow->restore = true;
×
4202
      sdbRelease(pSdb, pUser);
×
4203
      break;
×
4204
    }
4205

4206
    if (pUser->superUser) {
100,824✔
4207
      cols = 0;
19,972✔
4208
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19,972✔
4209
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19,972✔
4210
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4211
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
19,972✔
4212

4213
      char privilege[20] = {0};
19,972✔
4214
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
19,972✔
4215
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4216
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
19,972✔
4217

4218
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,972✔
4219
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
19,972✔
4220
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4221
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
19,972✔
4222

4223
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,972✔
4224
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19,972✔
4225
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4226
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
19,972✔
4227

4228
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19,972✔
4229
      if (condition == NULL) {
19,972✔
4230
        sdbRelease(pSdb, pUser);
×
4231
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4232
      }
4233
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19,972✔
4234
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4235
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
19,972✔
4236

4237
      char notes[2] = {0};
19,972✔
4238
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19,972✔
4239
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,972✔
4240
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
19,972✔
4241

4242
      numOfRows++;
19,972✔
4243
    }
4244

4245
    char *db = taosHashIterate(pUser->readDbs, NULL);
100,824✔
4246
    while (db != NULL) {
120,085✔
4247
      cols = 0;
19,261✔
4248
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19,261✔
4249
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19,261✔
4250
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4251
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
19,261✔
4252

4253
      char privilege[20] = {0};
19,261✔
4254
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
19,261✔
4255
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4256
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
19,261✔
4257

4258
      SName name = {0};
19,261✔
4259
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,261✔
4260
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19,261✔
4261
      if (code < 0) {
19,261✔
4262
        sdbRelease(pSdb, pUser);
×
4263
        sdbCancelFetch(pSdb, pShow->pIter);
×
4264
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4265
      }
4266
      (void)tNameGetDbName(&name, varDataVal(objName));
19,261✔
4267
      varDataSetLen(objName, strlen(varDataVal(objName)));
19,261✔
4268
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4269
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
19,261✔
4270

4271
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,261✔
4272
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19,261✔
4273
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4274
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
19,261✔
4275

4276
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19,261✔
4277
      if (condition == NULL) {
19,261✔
4278
        sdbRelease(pSdb, pUser);
×
4279
        sdbCancelFetch(pSdb, pShow->pIter);
×
4280
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4281
      }
4282
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19,261✔
4283
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4284
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
19,261✔
4285

4286
      char notes[2] = {0};
19,261✔
4287
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19,261✔
4288
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,261✔
4289
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
19,261✔
4290

4291
      numOfRows++;
19,261✔
4292
      db = taosHashIterate(pUser->readDbs, db);
19,261✔
4293
    }
4294

4295
    db = taosHashIterate(pUser->writeDbs, NULL);
100,824✔
4296
    while (db != NULL) {
117,904✔
4297
      cols = 0;
17,080✔
4298
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
17,080✔
4299
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
17,080✔
4300
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4301
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
17,080✔
4302

4303
      char privilege[20] = {0};
17,080✔
4304
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
17,080✔
4305
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4306
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
17,080✔
4307

4308
      SName name = {0};
17,080✔
4309
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
17,080✔
4310
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
17,080✔
4311
      if (code < 0) {
17,080✔
4312
        sdbRelease(pSdb, pUser);
×
4313
        sdbCancelFetch(pSdb, pShow->pIter);
×
4314
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4315
      }
4316
      (void)tNameGetDbName(&name, varDataVal(objName));
17,080✔
4317
      varDataSetLen(objName, strlen(varDataVal(objName)));
17,080✔
4318
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4319
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
17,080✔
4320

4321
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
17,080✔
4322
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
17,080✔
4323
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4324
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
17,080✔
4325

4326
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
17,080✔
4327
      if (condition == NULL) {
17,080✔
4328
        sdbRelease(pSdb, pUser);
×
4329
        sdbCancelFetch(pSdb, pShow->pIter);
×
4330
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4331
      }
4332
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
17,080✔
4333
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4334
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
17,080✔
4335

4336
      char notes[2] = {0};
17,080✔
4337
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
17,080✔
4338
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,080✔
4339
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
17,080✔
4340

4341
      numOfRows++;
17,080✔
4342
      db = taosHashIterate(pUser->writeDbs, db);
17,080✔
4343
    }
4344

4345
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4346

4347
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4348

4349
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4350

4351
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4352

4353
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4354

4355
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
100,824✔
4356

4357
    char *topic = taosHashIterate(pUser->topics, NULL);
100,824✔
4358
    while (topic != NULL) {
104,177✔
4359
      cols = 0;
3,353✔
4360
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
3,353✔
4361
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
3,353✔
4362
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4363
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
3,353✔
4364

4365
      char privilege[20] = {0};
3,353✔
4366
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
3,353✔
4367
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4368
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
3,353✔
4369

4370
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
3,353✔
4371
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
3,353✔
4372
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
3,353✔
4373
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4374
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, pShow->pIter, _exit);
3,353✔
4375

4376
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
3,353✔
4377
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
3,353✔
4378
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4379
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
3,353✔
4380

4381
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
3,353✔
4382
      if (condition == NULL) {
3,353✔
4383
        sdbRelease(pSdb, pUser);
×
4384
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4385
      }
4386
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
3,353✔
4387
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4388
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
3,353✔
4389

4390
      char notes[2] = {0};
3,353✔
4391
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
3,353✔
4392
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,353✔
4393
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
3,353✔
4394

4395
      numOfRows++;
3,353✔
4396
      topic = taosHashIterate(pUser->topics, topic);
3,353✔
4397
    }
4398

4399
    sdbRelease(pSdb, pUser);
100,824✔
4400
  }
4401

4402
  pShow->numOfRows += numOfRows;
19,972✔
4403
_exit:
19,972✔
4404
  taosMemoryFreeClear(condition);
19,972✔
4405
  taosMemoryFreeClear(sql);
19,972✔
4406
  if (code < 0) {
19,972✔
4407
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4408
    TAOS_RETURN(code);
×
4409
  }
4410
  return numOfRows;
19,972✔
4411
}
4412

4413
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
4414
  SSdb *pSdb = pMnode->pSdb;
×
4415
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4416
}
×
4417

4418
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
13,725,498✔
4419
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
4420
  int32_t           code = 0;
13,725,498✔
4421
  int32_t           lino = 0;
13,725,498✔
4422
  int32_t           rspLen = 0;
13,725,498✔
4423
  void             *pRsp = NULL;
13,725,498✔
4424
  SUserAuthBatchRsp batchRsp = {0};
13,725,498✔
4425

4426
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
13,725,498✔
4427
  if (batchRsp.pArray == NULL) {
13,725,498✔
4428
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4429
  }
4430

4431
  for (int32_t i = 0; i < numOfUses; ++i) {
27,781,469✔
4432
    SUserObj *pUser = NULL;
14,055,971✔
4433
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
14,055,971✔
4434
    if (pUser == NULL) {
14,055,971✔
4435
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
6,928✔
4436
        SGetUserAuthRsp rsp = {.dropped = 1};
6,928✔
4437
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
6,928✔
4438
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
13,856✔
4439
      }
4440
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
6,928✔
4441
      code = 0;
6,928✔
4442
      continue;
9,310✔
4443
    }
4444

4445
    pUsers[i].version = ntohl(pUsers[i].version);
14,049,043✔
4446
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
14,049,043✔
4447
      mndReleaseUser(pMnode, pUser);
13,185,266✔
4448
      continue;
13,185,266✔
4449
    }
4450

4451
    SGetUserAuthRsp rsp = {0};
863,777✔
4452
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
863,777✔
4453
    if (code) {
863,777✔
4454
      mndReleaseUser(pMnode, pUser);
×
4455
      tFreeSGetUserAuthRsp(&rsp);
×
4456
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4457
    }
4458

4459
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
1,727,554✔
4460
      code = terrno;
×
4461
      mndReleaseUser(pMnode, pUser);
×
4462
      tFreeSGetUserAuthRsp(&rsp);
×
4463
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4464
    }
4465
    mndReleaseUser(pMnode, pUser);
863,777✔
4466
  }
4467

4468
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
13,725,498✔
4469
    *ppRsp = NULL;
12,871,281✔
4470
    *pRspLen = 0;
12,871,281✔
4471

4472
    tFreeSUserAuthBatchRsp(&batchRsp);
12,871,281✔
4473
    return 0;
12,871,281✔
4474
  }
4475

4476
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
854,217✔
4477
  if (rspLen < 0) {
854,217✔
4478
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4479
  }
4480
  pRsp = taosMemoryMalloc(rspLen);
854,217✔
4481
  if (pRsp == NULL) {
854,217✔
4482
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4483
  }
4484
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
854,217✔
4485
  if (rspLen < 0) {
854,217✔
4486
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4487
  }
4488
_OVER:
854,217✔
4489
  tFreeSUserAuthBatchRsp(&batchRsp);
854,217✔
4490
  if (code < 0) {
854,217✔
4491
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4492
    taosMemoryFreeClear(pRsp);
×
4493
    rspLen = 0;
×
4494
  }
4495
  *ppRsp = pRsp;
854,217✔
4496
  *pRspLen = rspLen;
854,217✔
4497

4498
  TAOS_RETURN(code);
854,217✔
4499
}
4500

4501
static int32_t mndRemoveDbPrivileges(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, int32_t *nRemoved) {
29,298✔
4502
  void *pVal = NULL;
29,298✔
4503
  while ((pVal = taosHashIterate(pHash, pVal))) {
52,079✔
4504
    size_t keyLen = 0;
22,781✔
4505
    char  *pKey = (char *)taosHashGetKey(pVal, &keyLen);
22,781✔
4506
    if (pKey == NULL || keyLen <= dbFNameLen) continue;
22,781✔
4507
    if ((*(pKey + dbFNameLen) == '.') && strncmp(pKey, dbFName, dbFNameLen) == 0) {
22,781✔
4508
      TAOS_CHECK_RETURN(taosHashRemove(pHash, pKey, keyLen));
11,799✔
4509
      if (nRemoved) ++(*nRemoved);
11,799✔
4510
    }
4511
  }
4512
  TAOS_RETURN(0);
29,298✔
4513
}
4514

4515
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers) {
705,248✔
4516
  int32_t    code = 0, lino = 0;
705,248✔
4517
  SSdb      *pSdb = pMnode->pSdb;
705,248✔
4518
  int32_t    dbLen = strlen(pDb->name);
705,248✔
4519
  void      *pIter = NULL;
705,248✔
4520
  SUserObj  *pUser = NULL;
705,248✔
4521
  SUserObj   newUser = {0};
705,248✔
4522
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
705,248✔
4523
  bool       output = (ppUsers != NULL);
705,248✔
4524

4525
  while (1) {
766,469✔
4526
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,471,717✔
4527
    if (pIter == NULL) break;
1,471,717✔
4528

4529
    bool update = false;
766,469✔
4530
    bool inReadDb = (taosHashGet(pUser->readDbs, pDb->name, dbLen + 1) != NULL);
766,469✔
4531
    bool inWriteDb = (taosHashGet(pUser->writeDbs, pDb->name, dbLen + 1) != NULL);
766,469✔
4532
    bool inUseDb = (taosHashGet(pUser->useDbs, pDb->name, dbLen + 1) != NULL);
766,469✔
4533
    bool inReadTbs = taosHashGetSize(pUser->readTbs) > 0;
766,469✔
4534
    bool inWriteTbs = taosHashGetSize(pUser->writeTbs) > 0;
766,469✔
4535
    bool inAlterTbs = taosHashGetSize(pUser->alterTbs) > 0;
766,469✔
4536
    bool inReadViews = taosHashGetSize(pUser->readViews) > 0;
766,469✔
4537
    bool inWriteViews = taosHashGetSize(pUser->writeViews) > 0;
766,469✔
4538
    bool inAlterViews = taosHashGetSize(pUser->alterViews) > 0;
766,469✔
4539
    // no need remove pUser->topics since topics must be dropped ahead of db
4540
    if (!inReadDb && !inWriteDb && !inReadTbs && !inWriteTbs && !inAlterTbs && !inReadViews && !inWriteViews &&
766,469✔
4541
        !inAlterViews) {
756,128✔
4542
      sdbRelease(pSdb, pUser);
756,128✔
4543
      continue;
756,128✔
4544
    }
4545
    SUserObj *pTargetUser = &newUser;
10,341✔
4546
    if (output) {
10,341✔
4547
      if (!pUsers) {
1,292✔
4548
        TSDB_CHECK_NULL(pUsers = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
646✔
4549
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
4550
        *ppUsers = pUsers;
646✔
4551
      }
4552
      void   *pVal = NULL;
1,292✔
4553
      int32_t userLen = strlen(pUser->user) + 1;
1,292✔
4554
      if ((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)) != NULL) {
1,292✔
4555
        pTargetUser = (SUserObj *)pVal;
646✔
4556
      } else {
4557
        TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
646✔
4558
        TAOS_CHECK_EXIT(tSimpleHashPut(pUsers, pUser->user, userLen, &newUser, sizeof(SUserObj)));
646✔
4559
        TSDB_CHECK_NULL((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)), code, lino, _exit,
646✔
4560
                        TSDB_CODE_OUT_OF_MEMORY);
4561
        pTargetUser = (SUserObj *)pVal;
646✔
4562
      }
4563
    } else {
4564
      TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
9,049✔
4565
    }
4566
    if (inReadDb) {
10,341✔
4567
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->readDbs, pDb->name, dbLen + 1));
2,589✔
4568
    }
4569
    if (inWriteDb) {
10,341✔
4570
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->writeDbs, pDb->name, dbLen + 1));
2,228✔
4571
    }
4572
    if (inUseDb) {
10,341✔
4573
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->useDbs, pDb->name, dbLen + 1));
1,653✔
4574
    }
4575
    update = inReadDb || inWriteDb || inUseDb;
10,341✔
4576

4577
    int32_t nRemovedReadTbs = 0;
10,341✔
4578
    int32_t nRemovedWriteTbs = 0;
10,341✔
4579
    int32_t nRemovedAlterTbs = 0;
10,341✔
4580
    if (inReadTbs || inWriteTbs || inAlterTbs) {
10,341✔
4581
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readTbs, pDb->name, dbLen, &nRemovedReadTbs));
9,405✔
4582
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeTbs, pDb->name, dbLen, &nRemovedWriteTbs));
9,405✔
4583
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterTbs, pDb->name, dbLen, &nRemovedAlterTbs));
9,405✔
4584
      if (!update) update = nRemovedReadTbs > 0 || nRemovedWriteTbs > 0 || nRemovedAlterTbs > 0;
9,405✔
4585
    }
4586

4587
    int32_t nRemovedReadViews = 0;
10,341✔
4588
    int32_t nRemovedWriteViews = 0;
10,341✔
4589
    int32_t nRemovedAlterViews = 0;
10,341✔
4590
    if (inReadViews || inWriteViews || inAlterViews) {
10,341✔
4591
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readViews, pDb->name, dbLen, &nRemovedReadViews));
361✔
4592
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeViews, pDb->name, dbLen, &nRemovedWriteViews));
361✔
4593
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterViews, pDb->name, dbLen, &nRemovedAlterViews));
361✔
4594
      if (!update) update = nRemovedReadViews > 0 || nRemovedWriteViews > 0 || nRemovedAlterViews > 0;
361✔
4595
    }
4596

4597
    if (!output) {
10,341✔
4598
      if (update) {
9,049✔
4599
        SSdbRaw *pCommitRaw = mndUserActionEncode(pTargetUser);
1,297✔
4600
        if (pCommitRaw == NULL) {
1,297✔
4601
          TAOS_CHECK_EXIT(terrno);
×
4602
        }
4603
        TAOS_CHECK_EXIT(mndTransAppendCommitlog(pTrans, pCommitRaw));
1,297✔
4604
        TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,297✔
4605
      }
4606
      mndUserFreeObj(&newUser);
9,049✔
4607
    }
4608
    sdbRelease(pSdb, pUser);
10,341✔
4609
  }
4610

4611
_exit:
705,248✔
4612
  if (code < 0) {
705,248✔
4613
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4614
    mndUserFreeObj(&newUser);
×
4615
  }
4616
  if (pUser != NULL) sdbRelease(pSdb, pUser);
705,248✔
4617
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
705,248✔
4618
  if (!output) mndUserFreeObj(&newUser);
705,248✔
4619
  TAOS_RETURN(code);
705,248✔
4620
}
4621

4622
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
506,338✔
4623
  int32_t   code = 0;
506,338✔
4624
  SSdb     *pSdb = pMnode->pSdb;
506,338✔
4625
  int32_t   len = strlen(stb) + 1;
506,338✔
4626
  void     *pIter = NULL;
506,338✔
4627
  SUserObj *pUser = NULL;
506,338✔
4628
  SUserObj  newUser = {0};
506,338✔
4629

4630
  while (1) {
506,338✔
4631
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,012,676✔
4632
    if (pIter == NULL) break;
1,012,676✔
4633

4634
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
506,338✔
4635
      break;
×
4636
    }
4637

4638
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
506,338✔
4639
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
506,338✔
4640
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
506,338✔
4641
    if (inRead || inWrite || inAlter) {
506,338✔
4642
      code = taosHashRemove(newUser.readTbs, stb, len);
×
4643
      if (code < 0) {
×
4644
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
4645
      }
4646
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
4647
      if (code < 0) {
×
4648
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
4649
      }
4650
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
4651
      if (code < 0) {
×
4652
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
4653
      }
4654

4655
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
4656
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
4657
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4658
        break;
×
4659
      }
4660
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
4661
      if (code != 0) {
×
4662
        mndUserFreeObj(&newUser);
×
4663
        sdbRelease(pSdb, pUser);
×
4664
        TAOS_RETURN(code);
×
4665
      }
4666
    }
4667

4668
    mndUserFreeObj(&newUser);
506,338✔
4669
    sdbRelease(pSdb, pUser);
506,338✔
4670
  }
4671

4672
  if (pUser != NULL) sdbRelease(pSdb, pUser);
506,338✔
4673
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
506,338✔
4674
  mndUserFreeObj(&newUser);
506,338✔
4675
  TAOS_RETURN(code);
506,338✔
4676
}
4677

4678
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
178,947✔
4679
  int32_t   code = 0;
178,947✔
4680
  SSdb     *pSdb = pMnode->pSdb;
178,947✔
4681
  int32_t   len = strlen(view) + 1;
178,947✔
4682
  void     *pIter = NULL;
178,947✔
4683
  SUserObj *pUser = NULL;
178,947✔
4684
  SUserObj  newUser = {0};
178,947✔
4685

4686
  while (1) {
186,248✔
4687
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
365,195✔
4688
    if (pIter == NULL) break;
365,195✔
4689

4690
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
186,248✔
4691
      break;
×
4692
    }
4693

4694
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
186,248✔
4695
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
186,248✔
4696
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
186,248✔
4697
    if (inRead || inWrite || inAlter) {
186,248✔
4698
      code = taosHashRemove(newUser.readViews, view, len);
4,543✔
4699
      if (code < 0) {
4,543✔
4700
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
904✔
4701
      }
4702
      code = taosHashRemove(newUser.writeViews, view, len);
4,543✔
4703
      if (code < 0) {
4,543✔
4704
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
2,283✔
4705
      }
4706
      code = taosHashRemove(newUser.alterViews, view, len);
4,543✔
4707
      if (code < 0) {
4,543✔
4708
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
1,379✔
4709
      }
4710

4711
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
4,543✔
4712
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
4,543✔
4713
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4714
        break;
×
4715
      }
4716
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
4,543✔
4717
      if (code < 0) {
4,543✔
4718
        mndUserFreeObj(&newUser);
×
4719
        sdbRelease(pSdb, pUser);
×
4720
        TAOS_RETURN(code);
×
4721
      }
4722
    }
4723

4724
    mndUserFreeObj(&newUser);
186,248✔
4725
    sdbRelease(pSdb, pUser);
186,248✔
4726
  }
4727

4728
  if (pUser != NULL) sdbRelease(pSdb, pUser);
178,947✔
4729
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
178,947✔
4730
  mndUserFreeObj(&newUser);
178,947✔
4731
  TAOS_RETURN(code);
178,947✔
4732
}
4733

4734
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
31,263✔
4735
  int32_t   code = 0;
31,263✔
4736
  SSdb     *pSdb = pMnode->pSdb;
31,263✔
4737
  int32_t   len = strlen(topic) + 1;
31,263✔
4738
  void     *pIter = NULL;
31,263✔
4739
  SUserObj *pUser = NULL;
31,263✔
4740
  SUserObj  newUser = {0};
31,263✔
4741

4742
  while (1) {
45,813✔
4743
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
77,076✔
4744
    if (pIter == NULL) {
77,076✔
4745
      break;
31,263✔
4746
    }
4747

4748
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
45,813✔
4749
      break;
×
4750
    }
4751

4752
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
45,813✔
4753
    if (inTopic) {
45,813✔
4754
      code = taosHashRemove(newUser.topics, topic, len);
361✔
4755
      if (code < 0) {
361✔
4756
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
4757
      }
4758
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
361✔
4759
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
361✔
4760
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4761
        break;
×
4762
      }
4763
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
361✔
4764
      if (code < 0) {
361✔
4765
        mndUserFreeObj(&newUser);
×
4766
        sdbRelease(pSdb, pUser);
×
4767
        TAOS_RETURN(code);
×
4768
      }
4769
    }
4770

4771
    mndUserFreeObj(&newUser);
45,813✔
4772
    sdbRelease(pSdb, pUser);
45,813✔
4773
  }
4774

4775
  if (pUser != NULL) sdbRelease(pSdb, pUser);
31,263✔
4776
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
31,263✔
4777
  mndUserFreeObj(&newUser);
31,263✔
4778
  TAOS_RETURN(code);
31,263✔
4779
}
4780

4781
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4782
  // ver = 0, disable ip white list
4783
  // ver > 0, enable ip white list
4784
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
4785
}
4786

4787
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4788
  // ver = 0, disable datetime white list
4789
  // ver > 0, enable datetime white list
4790
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
4791
}
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