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

taosdata / TDengine / #4898

26 Dec 2025 09:58AM UTC coverage: 65.061% (-0.7%) from 65.717%
#4898

push

travis-ci

web-flow
feat: support encryption of configuration files, data files and metadata files (#33801)

350 of 1333 new or added lines in 31 files covered. (26.26%)

2796 existing lines in 159 files now uncovered.

184024 of 282850 relevant lines covered (65.06%)

113940470.33 hits per line

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

72.23
/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 "mndToken.h"
31
#include "tbase64.h"
32
#include "totp.h"
33

34
// clang-format on
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

141

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

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

155
static SUserCache userCache;
156

157

158
static int32_t userCacheInit() {
402,153✔
159
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
402,153✔
160

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

166
  userCache.users = users;
402,153✔
167
  userCache.verIp = 0;
402,153✔
168
  userCache.verTime = 0;
402,153✔
169

170
  (void)taosThreadRwlockInit(&userCache.rw, NULL);
402,153✔
171
  TAOS_RETURN(0);
402,153✔
172
}
173

174

175

176
static void userCacheCleanup() {
402,089✔
177
  if (userCache.users == NULL) {
402,089✔
178
    return;
×
179
  }
180

181
  void *pIter = taosHashIterate(userCache.users, NULL);
402,089✔
182
  while (pIter) {
812,599✔
183
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
410,510✔
184
    if (pInfo != NULL) {
410,510✔
185
      taosMemoryFree(pInfo->wlIp);
410,510✔
186
      taosMemoryFree(pInfo->wlTime);
410,510✔
187
      taosMemoryFree(pInfo);
410,510✔
188
    }
189
    pIter = taosHashIterate(userCache.users, pIter);
410,510✔
190
  }
191
  taosHashCleanup(userCache.users);
402,089✔
192

193
  (void)taosThreadRwlockDestroy(&userCache.rw);
402,089✔
194
}
195

196

197

198
static void userCacheRemoveUser(const char *user) {
29,911✔
199
  size_t userLen = strlen(user);
29,911✔
200

201
  (void)taosThreadRwlockWrlock(&userCache.rw);
29,911✔
202

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

217
  (void)taosThreadRwlockUnlock(&userCache.rw);
29,911✔
218
}
29,911✔
219

220

221

222
static void userCacheResetLoginInfo(const char *user) {
526✔
223
  size_t userLen = strlen(user);
526✔
224

225
  (void)taosThreadRwlockWrlock(&userCache.rw);
526✔
226

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

234
  (void)taosThreadRwlockUnlock(&userCache.rw);
526✔
235
}
526✔
236

237

238

239
static SCachedUserInfo* getCachedUserInfo(const char* user) {
3,286,988✔
240
  size_t userLen = strlen(user);
3,286,988✔
241
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
3,286,988✔
242
  if (ppInfo != NULL) {
3,286,988✔
243
    return *ppInfo;
2,846,567✔
244
  }
245

246
  SCachedUserInfo  *pInfo = (SCachedUserInfo *)taosMemoryCalloc(1, sizeof(SCachedUserInfo));
440,421✔
247
  if (pInfo == NULL) {
440,421✔
248
    return NULL;
×
249
  }
250

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

256
  return pInfo;
440,421✔
257
}
258

259

260

261
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
2,095,977✔
262
  size_t userLen = strlen(user);
2,095,977✔
263

264
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,095,977✔
265

266
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,095,973✔
267
  if (ppInfo != NULL && *ppInfo != NULL) {
2,095,921✔
268
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
1,816,099✔
269
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
1,816,155✔
270
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
1,816,113✔
271
  } else {
272
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
279,822✔
273
    pLoginInfo->failedLoginCount = 0;
279,822✔
274
    pLoginInfo->lastFailedLoginTime = 0;
279,822✔
275
  }
276

277
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,095,926✔
278

279
  if (pLoginInfo->lastLoginTime == 0) {
2,095,486✔
280
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
39,210✔
281
  }
282
}
2,095,533✔
283

284

285

286
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
2,095,810✔
287
  size_t userLen = strlen(user);
2,095,810✔
288

289
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,095,810✔
290

291
  SCachedUserInfo  *pInfo = getCachedUserInfo(user);
2,095,860✔
292
  if (pInfo != NULL) {
2,095,860✔
293
    pInfo->loginInfo.lastLoginTime = pLoginInfo->lastLoginTime;
2,095,860✔
294
    pInfo->loginInfo.failedLoginCount = pLoginInfo->failedLoginCount;
2,095,860✔
295
    pInfo->loginInfo.lastFailedLoginTime = pLoginInfo->lastFailedLoginTime;
2,095,860✔
296
  }
297

298
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,095,860✔
299
}
2,095,860✔
300

301

302

303
static bool isDateTimeWhiteListEqual(SDateTimeWhiteList *a, SDateTimeWhiteList *b) {
817,895✔
304
  if (a == NULL && b == NULL) {
817,895✔
305
    return true;
×
306
  }
307

308
  if (a == NULL || b == NULL) {
817,895✔
309
    return false;
58,093✔
310
  }
311

312
  if (a->num != b->num) {
759,802✔
313
    return false;
×
314
  }
315

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

325
  return true;
759,802✔
326
}
327

328

329

330
static int32_t userCacheUpdateWhiteList(SMnode* pMnode, SUserObj* pUser) {
817,895✔
331
  int32_t code = 0, lino = 0;
817,895✔
332

333
  (void)taosThreadRwlockWrlock(&userCache.rw);
817,895✔
334

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

340
  if (!isIpWhiteListEqual(pInfo->wlIp, pUser->pIpWhiteListDual)) {
817,895✔
341
    SIpWhiteListDual *p = cloneIpWhiteList(pUser->pIpWhiteListDual);
58,253✔
342
    if (p == NULL) {
58,253✔
343
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
344
    }
345
    taosMemoryFree(pInfo->wlIp);
58,253✔
346
    pInfo->wlIp = p;
58,253✔
347
    userCache.verIp++;
58,253✔
348
  }
349

350
  if (!isDateTimeWhiteListEqual(pInfo->wlTime, pUser->pTimeWhiteList)) {
817,895✔
351
    SDateTimeWhiteList *p = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
58,093✔
352
    if (p == NULL) {
58,093✔
353
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
354
    }
355
    taosMemoryFree(pInfo->wlTime);
58,093✔
356
    pInfo->wlTime = p;
58,093✔
357
    userCache.verTime++;
58,093✔
358
  }
359

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

368

369

370
static int32_t userCacheRebuildIpWhiteList(SMnode *pMnode) {
488,831✔
371
  int32_t   code = 0, lino = 0;
488,831✔
372

373
  SSdb     *pSdb = pMnode->pSdb;
488,831✔
374
  void     *pIter = NULL;
488,831✔
375
  while (1) {
190,903✔
376
    SUserObj *pUser = NULL;
679,734✔
377
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
679,734✔
378
    if (pIter == NULL) {
679,734✔
379
      break;
488,831✔
380
    }
381

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

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

396
    taosMemoryFree(pInfo->wlIp);
190,903✔
397
    pInfo->wlIp = wl;
190,903✔
398

399
    sdbRelease(pSdb, pUser);
190,903✔
400
  }
401

402
  userCache.verIp++;
488,831✔
403

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

411

412

413
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
40,150,358✔
414
  int64_t ver = 0;
40,150,358✔
415
  int32_t code = 0;
40,150,358✔
416

417
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,150,358✔
418
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,781✔
419

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

431
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,781✔
432
  }
433

434
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
40,150,358✔
435
  return ver;
40,150,358✔
436
}
437

438

439

440
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
488,831✔
441
  int32_t code = 0;
488,831✔
442
  (void)taosThreadRwlockWrlock(&userCache.rw);
488,831✔
443

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

451
  TAOS_RETURN(code);
488,831✔
452
}
453

454

455

456
static int32_t userCacheRebuildTimeWhiteList(SMnode *pMnode) {
480,258✔
457
  int32_t   code = 0, lino = 0;
480,258✔
458

459
  SSdb     *pSdb = pMnode->pSdb;
480,258✔
460
  void     *pIter = NULL;
480,258✔
461
  while (1) {
182,330✔
462
    SUserObj *pUser = NULL;
662,588✔
463
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
662,588✔
464
    if (pIter == NULL) {
662,588✔
465
      break;
480,258✔
466
    }
467

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

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

482
    taosMemoryFree(pInfo->wlTime);
182,330✔
483
    pInfo->wlTime = wl;
182,330✔
484

485
    sdbRelease(pSdb, pUser);
182,330✔
486
  }
487

488
  userCache.verTime++;
480,258✔
489

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

497

498

499
int32_t mndRefreshUserDateTimeWhiteList(SMnode *pMnode) {
480,258✔
500
  int32_t code = 0;
480,258✔
501
  (void)taosThreadRwlockWrlock(&userCache.rw);
480,258✔
502

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

510
  TAOS_RETURN(code);
480,258✔
511
}
512

513

514

515
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
40,150,358✔
516
  int64_t ver = 0;
40,150,358✔
517
  int32_t code = 0;
40,150,358✔
518

519
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,150,358✔
520
    (void)taosThreadRwlockWrlock(&userCache.rw);
4,781✔
521

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

533
    (void)taosThreadRwlockUnlock(&userCache.rw);
4,781✔
534
  }
535

536
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
40,150,358✔
537
  return ver;
40,150,358✔
538
}
539

540

541

542
int32_t mndInitUser(SMnode *pMnode) {
402,153✔
543
  TAOS_CHECK_RETURN(userCacheInit());
402,153✔
544

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

556
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
402,153✔
557
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
402,153✔
558
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
402,153✔
559
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
402,153✔
560

561
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST, mndProcessGetUserIpWhiteListReq);
402,153✔
562
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST_DUAL, mndProcessGetUserIpWhiteListReq);
402,153✔
563
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST, mndProcessRetrieveIpWhiteListReq);
402,153✔
564
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL, mndProcessRetrieveIpWhiteListReq);
402,153✔
565
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_DATETIME_WHITELIST, mndProcessGetUserDateTimeWhiteListReq);
402,153✔
566
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_DATETIME_WHITELIST, mndProcessRetrieveDateTimeWhiteListReq);
402,153✔
567

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

577

578

579
void mndCleanupUser(SMnode *pMnode) {
402,089✔
580
  userCacheCleanup();
402,089✔
581
}
402,089✔
582

583

584

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

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

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

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

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

605

606

607
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
24,030✔
608
  int32_t len = 0;
24,030✔
609
  for (int i = 0; i < num; i++) {
72,730✔
610
    SIpRange *pRange = &range[i];
48,700✔
611
    SIpAddr   addr = {0};
48,700✔
612
    int32_t code = tIpUintToStr(pRange, &addr);
48,700✔
613
    if (code != 0) {
48,700✔
614
      mError("%s failed to convert ip range to str, code: %d", __func__, code);
×
615
    }
616

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

623

624

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

630
  if (a->type == 0) {
1,519,284✔
631
    SIpV4Range *a4 = &a->ipV4;
759,802✔
632
    SIpV4Range *b4 = &b->ipV4;
759,802✔
633
    return (a4->ip == b4->ip && a4->mask == b4->mask);
759,802✔
634
  }
635
  
636
  SIpV6Range *a6 = &a->ipV6;
759,482✔
637
  SIpV6Range *b6 = &b->ipV6;
759,482✔
638
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
759,482✔
639
}
640

641

642

643
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
817,895✔
644
  if (a == NULL && b == NULL) {
817,895✔
645
    return true;
×
646
  }
647
  
648
  if (a == NULL || b == NULL) {
817,895✔
649
    return false;
58,093✔
650
  }
651

652
  if (a->num != b->num) {
759,802✔
653
    return false;
160✔
654
  }
655
  for (int i = 0; i < a->num; i++) {
2,278,926✔
656
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,519,284✔
657
      return false;
×
658
    }
659
  }
660
  return true;
759,642✔
661
}
662

663

664
static int32_t compareIpRange(const void *a, const void *b, const void* arg) {
1,760✔
665
  SIpRange *ra = (SIpRange *)a;
1,760✔
666
  SIpRange *rb = (SIpRange *)b;
1,760✔
667

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

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

676
  if (ra->type == 0) {
1,760✔
677
    if (ra->ipV4.ip != rb->ipV4.ip) {
1,760✔
678
      return (ra->ipV4.ip < rb->ipV4.ip) ? -1 : 1;
1,440✔
679
    }
680
    return (ra->ipV4.mask < rb->ipV4.mask) ? -1 : 1;
320✔
681
  }
682

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

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

696

697

698
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
24,030✔
699
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
24,030✔
700

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

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

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

719

720

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

728
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,009,180✔
729
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
4,018,360✔
730

731
  for (int i = 0; i < pList->num; i++) {
6,028,016✔
732
    SIpRange *pRange = &(pList->pIpRanges[i]);
4,018,836✔
733
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
4,018,836✔
734
  }
735

736
  tEndEncode(&encoder);
2,009,180✔
737

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

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

754
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,276,091✔
755
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
2,552,182✔
756

757
  for (int i = 0; i < pList->num; i++) {
3,828,591✔
758
    SIpRange *pRange = &(pList->pIpRanges[i]);
2,552,500✔
759
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
2,552,500✔
760
  }
761

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

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

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

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

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

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

803
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,276,091✔
804
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
1,276,091✔
805

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

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

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

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

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

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

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

860
  SIpRange v4 = {0};
345,585✔
861
  SIpRange v6 = {0};
345,585✔
862

863
#ifndef TD_ASTRA
864
  code = createDefaultIp4Range(&v4);
345,585✔
865
  TSDB_CHECK_CODE(code, lino, _error);
345,585✔
866

867
  code = createDefaultIp6Range(&v6);
345,585✔
868
  TSDB_CHECK_CODE(code, lino, _error);
345,585✔
869

870
#endif
871

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

884

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

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

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

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

904
    if (range->absolute) {
×
905
      struct STm tm;
×
906
      (void)taosTs2Tm(range->start, TSDB_TIME_PRECISION_SECONDS, &tm, NULL);
×
907
      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);
×
908
    } else {
909
      int day = range->start / 86400;
×
910
      int hour = (range->start % 86400) / 3600;
×
911
      int minute = (range->start % 3600) / 60;
×
912
      pos += tsnprintf(*buf + pos, bufLen - pos, "%c%s %02d:%02d %dm, ", range->neg ? '-' : '+', weekdays[day], hour, minute, duration);
×
913
    }
914
  }
915

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

920
  return pos;
×
921
}
922

923

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

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

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

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

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

944
  return 0;
×
945
}
946

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

951

952

953

954
static void dropOldPasswords(SUserObj *pUser) {
3,285,271✔
955
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
3,285,271✔
956
    return;
3,262,962✔
957
  }
958

959
  int32_t reuseMax = pUser->passwordReuseMax;
22,309✔
960
  if (reuseMax == 0) {
22,309✔
961
    reuseMax = 1; // keep at least one password
20,689✔
962
  }
963

964
  int32_t now = taosGetTimestampSec();
22,309✔
965
  int32_t index = reuseMax;
22,309✔
966
  while(index < pUser->numOfPasswords) {
26,845✔
967
    SUserPassword *pPass = &pUser->passwords[index];
4,536✔
968
    if (now - pPass->setTime >= pUser->passwordReuseTime) {
4,536✔
969
      break;
×
970
    }
971
    index++;
4,536✔
972
  }
973

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

982

983

984

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

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

1001
  userObj.passwords[0].setTime = taosGetTimestampSec();
288,137✔
1002
  userObj.numOfPasswords = 1;
288,137✔
1003

1004
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
288,137✔
1005
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
288,137✔
1006
  userObj.createdTime = taosGetTimestampMs();
288,137✔
1007
  userObj.updateTime = userObj.createdTime;
288,137✔
1008
  userObj.sysInfo = 1;
288,137✔
1009
  userObj.enable = 1;
288,137✔
1010
  userObj.changePass = 2;
288,137✔
1011
  userObj.ipWhiteListVer = taosGetTimestampMs();
288,137✔
1012
  userObj.connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
288,137✔
1013
  userObj.connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
288,137✔
1014
  userObj.callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
288,137✔
1015
  userObj.vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
288,137✔
1016
  userObj.passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
288,137✔
1017
  userObj.passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
288,137✔
1018
  userObj.passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
288,137✔
1019
  // this is the root user, set some fields to -1 to allow the user login without restriction
1020
  userObj.sessionPerUser = -1;
288,137✔
1021
  userObj.failedLoginAttempts = -1;
288,137✔
1022
  userObj.passwordLifeTime = -1;
288,137✔
1023
  userObj.passwordGraceTime = -1;
288,137✔
1024
  userObj.inactiveAccountTime = -1;
288,137✔
1025
  userObj.allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
288,137✔
1026
  userObj.tokenNum = 0;
288,137✔
1027

1028
  userObj.pTimeWhiteList = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
288,137✔
1029
  if (userObj.pTimeWhiteList == NULL) {
288,137✔
1030
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
1031
  }
1032
  
1033
  TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _ERROR);
288,137✔
1034
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
288,137✔
1035
    userObj.superUser = 1;
288,137✔
1036
    userObj.createdb = 1;
288,137✔
1037
    userObj.sessionPerUser = -1;
288,137✔
1038
    userObj.callPerSession = -1;
288,137✔
1039
    userObj.vnodePerCall = -1;
288,137✔
1040
    userObj.failedLoginAttempts = -1;
288,137✔
1041
    userObj.passwordLifeTime = -1;
288,137✔
1042
    userObj.passwordLockTime = 1;
288,137✔
1043
    userObj.inactiveAccountTime = -1;
288,137✔
1044
    userObj.allowTokenNum = -1;
288,137✔
1045
    userObj.tokenNum = 0;
288,137✔
1046
  }
1047

1048
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
288,137✔
1049
  if (pRaw == NULL) goto _ERROR;
288,137✔
1050
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
288,137✔
1051

1052
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
288,137✔
1053

1054
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
288,137✔
1055
  if (pTrans == NULL) {
288,137✔
1056
    sdbFreeRaw(pRaw);
×
1057
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
1058
    goto _ERROR;
×
1059
  }
1060
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
288,137✔
1061

1062
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
288,137✔
1063
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1064
    mndTransDrop(pTrans);
×
1065
    goto _ERROR;
×
1066
  }
1067
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
288,137✔
1068

1069
  if (mndTransPrepare(pMnode, pTrans) != 0) {
288,137✔
1070
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1071
    mndTransDrop(pTrans);
×
1072
    goto _ERROR;
×
1073
  }
1074

1075
  mndTransDrop(pTrans);
288,137✔
1076
  taosMemoryFree(userObj.passwords);
288,137✔
1077
  taosMemoryFree(userObj.pIpWhiteListDual);
288,137✔
1078
  taosMemoryFree(userObj.pTimeWhiteList);
288,137✔
1079
  return 0;
288,137✔
1080

1081
_ERROR:
×
1082
  taosMemoryFree(userObj.passwords);
×
1083
  taosMemoryFree(userObj.pIpWhiteListDual);
×
1084
  taosMemoryFree(userObj.pTimeWhiteList);
×
1085
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
1086
}
1087

1088
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
288,137✔
1089
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
288,137✔
1090
}
1091

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

1113
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,009,180✔
1114
  while (stb != NULL) {
2,390,224✔
1115
    size_t keyLen = 0;
381,044✔
1116
    void  *key = taosHashGetKey(stb, &keyLen);
381,044✔
1117
    size += sizeof(int32_t);
381,044✔
1118
    size += keyLen;
381,044✔
1119

1120
    size_t valueLen = 0;
381,044✔
1121
    valueLen = strlen(stb) + 1;
381,044✔
1122
    size += sizeof(int32_t);
381,044✔
1123
    size += valueLen;
381,044✔
1124
    stb = taosHashIterate(pUser->readTbs, stb);
381,044✔
1125
  }
1126

1127
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,009,180✔
1128
  while (stb != NULL) {
2,395,184✔
1129
    size_t keyLen = 0;
386,004✔
1130
    void  *key = taosHashGetKey(stb, &keyLen);
386,004✔
1131
    size += sizeof(int32_t);
386,004✔
1132
    size += keyLen;
386,004✔
1133

1134
    size_t valueLen = 0;
386,004✔
1135
    valueLen = strlen(stb) + 1;
386,004✔
1136
    size += sizeof(int32_t);
386,004✔
1137
    size += valueLen;
386,004✔
1138
    stb = taosHashIterate(pUser->writeTbs, stb);
386,004✔
1139
  }
1140

1141
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,009,180✔
1142
  while (stb != NULL) {
2,201,562✔
1143
    size_t keyLen = 0;
192,382✔
1144
    void  *key = taosHashGetKey(stb, &keyLen);
192,382✔
1145
    size += sizeof(int32_t);
192,382✔
1146
    size += keyLen;
192,382✔
1147

1148
    size_t valueLen = 0;
192,382✔
1149
    valueLen = strlen(stb) + 1;
192,382✔
1150
    size += sizeof(int32_t);
192,382✔
1151
    size += valueLen;
192,382✔
1152
    stb = taosHashIterate(pUser->alterTbs, stb);
192,382✔
1153
  }
1154

1155
  stb = taosHashIterate(pUser->readViews, NULL);
2,009,180✔
1156
  while (stb != NULL) {
2,045,067✔
1157
    size_t keyLen = 0;
35,887✔
1158
    void  *key = taosHashGetKey(stb, &keyLen);
35,887✔
1159
    size += sizeof(int32_t);
35,887✔
1160
    size += keyLen;
35,887✔
1161

1162
    size_t valueLen = 0;
35,887✔
1163
    valueLen = strlen(stb) + 1;
35,887✔
1164
    size += sizeof(int32_t);
35,887✔
1165
    size += valueLen;
35,887✔
1166
    stb = taosHashIterate(pUser->readViews, stb);
35,887✔
1167
  }
1168

1169
  stb = taosHashIterate(pUser->writeViews, NULL);
2,009,180✔
1170
  while (stb != NULL) {
2,037,464✔
1171
    size_t keyLen = 0;
28,284✔
1172
    void  *key = taosHashGetKey(stb, &keyLen);
28,284✔
1173
    size += sizeof(int32_t);
28,284✔
1174
    size += keyLen;
28,284✔
1175

1176
    size_t valueLen = 0;
28,284✔
1177
    valueLen = strlen(stb) + 1;
28,284✔
1178
    size += sizeof(int32_t);
28,284✔
1179
    size += valueLen;
28,284✔
1180
    stb = taosHashIterate(pUser->writeViews, stb);
28,284✔
1181
  }
1182

1183
  stb = taosHashIterate(pUser->alterViews, NULL);
2,009,180✔
1184
  while (stb != NULL) {
2,037,924✔
1185
    size_t keyLen = 0;
28,744✔
1186
    void  *key = taosHashGetKey(stb, &keyLen);
28,744✔
1187
    size += sizeof(int32_t);
28,744✔
1188
    size += keyLen;
28,744✔
1189

1190
    size_t valueLen = 0;
28,744✔
1191
    valueLen = strlen(stb) + 1;
28,744✔
1192
    size += sizeof(int32_t);
28,744✔
1193
    size += valueLen;
28,744✔
1194
    stb = taosHashIterate(pUser->alterViews, stb);
28,744✔
1195
  }
1196

1197
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,009,180✔
1198
  while (useDb != NULL) {
2,488,563✔
1199
    size_t keyLen = 0;
479,383✔
1200
    void  *key = taosHashGetKey(useDb, &keyLen);
479,383✔
1201
    size += sizeof(int32_t);
479,383✔
1202
    size += keyLen;
479,383✔
1203
    size += sizeof(int32_t);
479,383✔
1204
    useDb = taosHashIterate(pUser->useDbs, useDb);
479,383✔
1205
  }
1206

1207
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
2,009,180✔
1208
  if (pRaw == NULL) {
2,009,180✔
1209
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1210
  }
1211

1212
  int32_t dataPos = 0;
2,009,180✔
1213
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
2,009,180✔
1214

1215
  dropOldPasswords(pUser);
2,009,180✔
1216
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,009,180✔
1217
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
4,030,713✔
1218
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,021,533✔
1219
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,021,533✔
1220
  }
1221
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,009,180✔
1222

1223
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
2,009,180✔
1224
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
2,009,180✔
1225
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
2,009,180✔
1226
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
2,009,180✔
1227
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
2,009,180✔
1228
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
2,009,180✔
1229
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
2,009,180✔
1230
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
2,009,180✔
1231
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
2,009,180✔
1232
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
2,009,180✔
1233
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
2,009,180✔
1234
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
2,009,180✔
1235

1236
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,009,180✔
1237
  while (db != NULL) {
2,282,591✔
1238
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
273,411✔
1239
    db = taosHashIterate(pUser->readDbs, db);
273,411✔
1240
  }
1241

1242
  db = taosHashIterate(pUser->writeDbs, NULL);
2,009,180✔
1243
  while (db != NULL) {
2,278,282✔
1244
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
269,102✔
1245
    db = taosHashIterate(pUser->writeDbs, db);
269,102✔
1246
  }
1247

1248
  char *topic = taosHashIterate(pUser->topics, NULL);
2,009,180✔
1249
  while (topic != NULL) {
2,016,874✔
1250
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
7,694✔
1251
    topic = taosHashIterate(pUser->topics, topic);
7,694✔
1252
  }
1253

1254
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
2,009,180✔
1255
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
2,009,180✔
1256
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
2,009,180✔
1257
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
2,009,180✔
1258
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
2,009,180✔
1259
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
2,009,180✔
1260
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
2,009,180✔
1261

1262
  stb = taosHashIterate(pUser->readTbs, NULL);
2,009,180✔
1263
  while (stb != NULL) {
2,390,224✔
1264
    size_t keyLen = 0;
381,044✔
1265
    void  *key = taosHashGetKey(stb, &keyLen);
381,044✔
1266
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
381,044✔
1267
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
381,044✔
1268

1269
    size_t valueLen = 0;
381,044✔
1270
    valueLen = strlen(stb) + 1;
381,044✔
1271
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
381,044✔
1272
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
381,044✔
1273
    stb = taosHashIterate(pUser->readTbs, stb);
381,044✔
1274
  }
1275

1276
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,009,180✔
1277
  while (stb != NULL) {
2,395,184✔
1278
    size_t keyLen = 0;
386,004✔
1279
    void  *key = taosHashGetKey(stb, &keyLen);
386,004✔
1280
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
386,004✔
1281
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
386,004✔
1282

1283
    size_t valueLen = 0;
386,004✔
1284
    valueLen = strlen(stb) + 1;
386,004✔
1285
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
386,004✔
1286
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
386,004✔
1287
    stb = taosHashIterate(pUser->writeTbs, stb);
386,004✔
1288
  }
1289

1290
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,009,180✔
1291
  while (stb != NULL) {
2,201,562✔
1292
    size_t keyLen = 0;
192,382✔
1293
    void  *key = taosHashGetKey(stb, &keyLen);
192,382✔
1294
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
192,382✔
1295
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
192,382✔
1296

1297
    size_t valueLen = 0;
192,382✔
1298
    valueLen = strlen(stb) + 1;
192,382✔
1299
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
192,382✔
1300
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
192,382✔
1301
    stb = taosHashIterate(pUser->alterTbs, stb);
192,382✔
1302
  }
1303

1304
  stb = taosHashIterate(pUser->readViews, NULL);
2,009,180✔
1305
  while (stb != NULL) {
2,045,067✔
1306
    size_t keyLen = 0;
35,887✔
1307
    void  *key = taosHashGetKey(stb, &keyLen);
35,887✔
1308
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
35,887✔
1309
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
35,887✔
1310

1311
    size_t valueLen = 0;
35,887✔
1312
    valueLen = strlen(stb) + 1;
35,887✔
1313
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
35,887✔
1314
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
35,887✔
1315
    stb = taosHashIterate(pUser->readViews, stb);
35,887✔
1316
  }
1317

1318
  stb = taosHashIterate(pUser->writeViews, NULL);
2,009,180✔
1319
  while (stb != NULL) {
2,037,464✔
1320
    size_t keyLen = 0;
28,284✔
1321
    void  *key = taosHashGetKey(stb, &keyLen);
28,284✔
1322
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
28,284✔
1323
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
28,284✔
1324

1325
    size_t valueLen = 0;
28,284✔
1326
    valueLen = strlen(stb) + 1;
28,284✔
1327
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
28,284✔
1328
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
28,284✔
1329
    stb = taosHashIterate(pUser->writeViews, stb);
28,284✔
1330
  }
1331

1332
  stb = taosHashIterate(pUser->alterViews, NULL);
2,009,180✔
1333
  while (stb != NULL) {
2,037,924✔
1334
    size_t keyLen = 0;
28,744✔
1335
    void  *key = taosHashGetKey(stb, &keyLen);
28,744✔
1336
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
28,744✔
1337
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
28,744✔
1338

1339
    size_t valueLen = 0;
28,744✔
1340
    valueLen = strlen(stb) + 1;
28,744✔
1341
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
28,744✔
1342
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
28,744✔
1343
    stb = taosHashIterate(pUser->alterViews, stb);
28,744✔
1344
  }
1345

1346
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,009,180✔
1347
  while (useDb != NULL) {
2,488,563✔
1348
    size_t keyLen = 0;
479,383✔
1349
    void  *key = taosHashGetKey(useDb, &keyLen);
479,383✔
1350
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
479,383✔
1351
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
479,383✔
1352

1353
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
479,383✔
1354
    useDb = taosHashIterate(pUser->useDbs, useDb);
479,383✔
1355
  }
1356

1357
  // save white list
1358
  int32_t num = pUser->pIpWhiteListDual->num;
2,009,180✔
1359
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
2,009,180✔
1360
  if ((buf = taosMemoryCalloc(1, tlen)) == NULL) {
2,009,180✔
1361
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1362
  }
1363
  int32_t len = 0;
2,009,180✔
1364
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
2,009,180✔
1365

1366
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
2,009,180✔
1367
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
2,009,180✔
1368

1369
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
2,009,180✔
1370
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
2,009,180✔
1371

1372
  SDB_SET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
2,009,180✔
1373
  SDB_SET_INT8(pRaw, dataPos, pUser->changePass, _OVER);
2,009,180✔
1374
  SDB_SET_INT32(pRaw, dataPos, pUser->sessionPerUser, _OVER);
2,009,180✔
1375
  SDB_SET_INT32(pRaw, dataPos, pUser->connectTime, _OVER);
2,009,180✔
1376
  SDB_SET_INT32(pRaw, dataPos, pUser->connectIdleTime, _OVER);
2,009,180✔
1377
  SDB_SET_INT32(pRaw, dataPos, pUser->callPerSession, _OVER);
2,009,180✔
1378
  SDB_SET_INT32(pRaw, dataPos, pUser->vnodePerCall, _OVER);
2,009,180✔
1379
  SDB_SET_INT32(pRaw, dataPos, pUser->failedLoginAttempts, _OVER);
2,009,180✔
1380
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLifeTime, _OVER);
2,009,180✔
1381
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseTime, _OVER);
2,009,180✔
1382
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseMax, _OVER);
2,009,180✔
1383
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLockTime, _OVER);
2,009,180✔
1384
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordGraceTime, _OVER);
2,009,180✔
1385
  SDB_SET_INT32(pRaw, dataPos, pUser->inactiveAccountTime, _OVER);
2,009,180✔
1386
  SDB_SET_INT32(pRaw, dataPos, pUser->allowTokenNum, _OVER);
2,009,180✔
1387
  SDB_SET_INT32(pRaw, dataPos, pUser->tokenNum, _OVER);
2,009,180✔
1388

1389
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
2,009,180✔
1390
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
2,009,180✔
1391
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1392
    SDB_SET_BOOL(pRaw, dataPos, range->absolute, _OVER);
×
1393
    SDB_SET_BOOL(pRaw, dataPos, range->neg, _OVER);
×
1394
    SDB_SET_INT64(pRaw, dataPos, range->start, _OVER);
×
1395
    SDB_SET_INT32(pRaw, dataPos, range->duration, _OVER);
×
1396
  }
1397

1398
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,009,180✔
1399
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,009,180✔
1400

1401
_OVER:
2,009,180✔
1402
  taosMemoryFree(buf);
2,009,180✔
1403
  if (code < 0) {
2,009,180✔
1404
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1405
           tstrerror(code));
1406
    sdbFreeRaw(pRaw);
×
1407
    pRaw = NULL;
×
1408
    terrno = code;
×
1409
  }
1410

1411
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
2,009,180✔
1412
  return pRaw;
2,009,180✔
1413
}
1414

1415
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
1,276,091✔
1416
  int32_t   code = 0;
1,276,091✔
1417
  int32_t   lino = 0;
1,276,091✔
1418
  SSdbRow  *pRow = NULL;
1,276,091✔
1419
  SUserObj *pUser = NULL;
1,276,091✔
1420
  char     *key = NULL;
1,276,091✔
1421
  char     *value = NULL;
1,276,091✔
1422

1423
  int8_t sver = 0;
1,276,091✔
1424
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
1,276,091✔
1425
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1426
  }
1427

1428
  if (sver < 1 || sver > USER_VER_NUMBER) {
1,276,091✔
1429
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1430
  }
1431

1432
  pRow = sdbAllocRow(sizeof(SUserObj));
1,276,091✔
1433
  if (pRow == NULL) {
1,276,091✔
1434
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1435
  }
1436

1437
  pUser = sdbGetRowObj(pRow);
1,276,091✔
1438
  if (pUser == NULL) {
1,276,091✔
1439
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1440
  }
1441

1442
  int32_t dataPos = 0;
1,276,091✔
1443
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
1,276,091✔
1444

1445
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,276,091✔
1446
    pUser->passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
×
1447
    if (pUser->passwords == NULL) {
×
1448
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1449
    }
1450
    SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[0].pass, TSDB_PASSWORD_LEN, _OVER)
×
1451
    pUser->numOfPasswords = 1;
×
1452
    memset(pUser->salt, 0, sizeof(pUser->salt));
×
1453
  } else {
1454
    SDB_GET_INT32(pRaw, dataPos, &pUser->numOfPasswords, _OVER)
1,276,091✔
1455
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,276,091✔
1456
    if (pUser->passwords == NULL) {
1,276,091✔
1457
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1458
    }
1459
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
2,570,874✔
1460
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
1,294,783✔
1461
      SDB_GET_INT32(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
1,294,783✔
1462
    }
1463
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
1,276,091✔
1464
  }
1465
  
1466
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
1,276,091✔
1467
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
1,276,091✔
1468
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
1,276,091✔
1469
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,276,091✔
1470
    pUser->passwords[0].setTime = (int32_t)(pUser->updateTime / 1000);
×
1471
  }
1472

1473
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
1,276,091✔
1474
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
1,276,091✔
1475
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
1,276,091✔
1476
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
1,276,091✔
1477
  if (pUser->superUser) pUser->createdb = 1;
1,276,091✔
1478
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
1,276,091✔
1479
  if (sver >= 4) {
1,276,091✔
1480
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
1,276,091✔
1481
  }
1482

1483
  int32_t numOfReadDbs = 0;
1,276,091✔
1484
  int32_t numOfWriteDbs = 0;
1,276,091✔
1485
  int32_t numOfTopics = 0;
1,276,091✔
1486
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
1,276,091✔
1487
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
1,276,091✔
1488
  if (sver >= 2) {
1,276,091✔
1489
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
1,276,091✔
1490
  }
1491

1492
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1493
  pUser->writeDbs =
1,276,091✔
1494
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1495
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1496
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
1,276,091✔
1497
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1498
    goto _OVER;
×
1499
  }
1500

1501
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
1,541,663✔
1502
    char db[TSDB_DB_FNAME_LEN] = {0};
265,572✔
1503
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
265,572✔
1504
    int32_t len = strlen(db) + 1;
265,572✔
1505
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
265,572✔
1506
  }
1507

1508
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
1,537,452✔
1509
    char db[TSDB_DB_FNAME_LEN] = {0};
261,361✔
1510
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
261,361✔
1511
    int32_t len = strlen(db) + 1;
261,361✔
1512
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
261,361✔
1513
  }
1514

1515
  if (sver >= 2) {
1,276,091✔
1516
    for (int32_t i = 0; i < numOfTopics; ++i) {
1,283,146✔
1517
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
7,055✔
1518
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
7,055✔
1519
      int32_t len = strlen(topic) + 1;
7,055✔
1520
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
7,055✔
1521
    }
1522
  }
1523

1524
  if (sver >= 3) {
1,276,091✔
1525
    int32_t numOfReadTbs = 0;
1,276,091✔
1526
    int32_t numOfWriteTbs = 0;
1,276,091✔
1527
    int32_t numOfAlterTbs = 0;
1,276,091✔
1528
    int32_t numOfReadViews = 0;
1,276,091✔
1529
    int32_t numOfWriteViews = 0;
1,276,091✔
1530
    int32_t numOfAlterViews = 0;
1,276,091✔
1531
    int32_t numOfUseDbs = 0;
1,276,091✔
1532
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
1,276,091✔
1533
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
1,276,091✔
1534
    if (sver >= 6) {
1,276,091✔
1535
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
1,276,091✔
1536
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
1,276,091✔
1537
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
1,276,091✔
1538
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
1,276,091✔
1539
    }
1540
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
1,276,091✔
1541

1542
    pUser->readTbs =
1,276,091✔
1543
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1544
    pUser->writeTbs =
1,276,091✔
1545
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1546
    pUser->alterTbs =
1,276,091✔
1547
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1548

1549
    pUser->readViews =
1,276,091✔
1550
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1551
    pUser->writeViews =
1,276,091✔
1552
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1553
    pUser->alterViews =
1,276,091✔
1554
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1555

1556
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,276,091✔
1557

1558
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
1,276,091✔
1559
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
1,276,091✔
1560
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1561
      goto _OVER;
×
1562
    }
1563

1564
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
1,640,443✔
1565
      int32_t keyLen = 0;
364,352✔
1566
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
364,352✔
1567

1568
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
364,352✔
1569
      if (key == NULL) {
364,352✔
1570
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1571
      }
1572
      (void)memset(key, 0, keyLen);
364,352✔
1573
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
364,352✔
1574

1575
      int32_t valuelen = 0;
364,352✔
1576
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
364,352✔
1577
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
364,352✔
1578
      if (value == NULL) {
364,352✔
1579
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1580
      }
1581
      (void)memset(value, 0, valuelen);
364,352✔
1582
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
364,352✔
1583

1584
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
364,352✔
1585
    }
1586

1587
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
1,644,848✔
1588
      int32_t keyLen = 0;
368,757✔
1589
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
368,757✔
1590

1591
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
368,757✔
1592
      if (key == NULL) {
368,757✔
1593
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1594
      }
1595
      (void)memset(key, 0, keyLen);
368,757✔
1596
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
368,757✔
1597

1598
      int32_t valuelen = 0;
368,757✔
1599
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
368,757✔
1600
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
368,757✔
1601
      if (value == NULL) {
368,757✔
1602
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1603
      }
1604
      (void)memset(value, 0, valuelen);
368,757✔
1605
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
368,757✔
1606

1607
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
368,757✔
1608
    }
1609

1610
    if (sver >= 6) {
1,276,091✔
1611
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,463,983✔
1612
        int32_t keyLen = 0;
187,892✔
1613
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
187,892✔
1614

1615
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
187,892✔
1616
        if (key == NULL) {
187,892✔
1617
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1618
        }
1619
        (void)memset(key, 0, keyLen);
187,892✔
1620
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
187,892✔
1621

1622
        int32_t valuelen = 0;
187,892✔
1623
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
187,892✔
1624
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
187,892✔
1625
        if (value == NULL) {
187,892✔
1626
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1627
        }
1628
        (void)memset(value, 0, valuelen);
187,892✔
1629
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
187,892✔
1630

1631
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
187,892✔
1632
      }
1633

1634
      for (int32_t i = 0; i < numOfReadViews; ++i) {
1,310,922✔
1635
        int32_t keyLen = 0;
34,831✔
1636
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
34,831✔
1637

1638
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
34,831✔
1639
        if (key == NULL) {
34,831✔
1640
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1641
        }
1642
        (void)memset(key, 0, keyLen);
34,831✔
1643
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
34,831✔
1644

1645
        int32_t valuelen = 0;
34,831✔
1646
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
34,831✔
1647
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
34,831✔
1648
        if (value == NULL) {
34,831✔
1649
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1650
        }
1651
        (void)memset(value, 0, valuelen);
34,831✔
1652
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
34,831✔
1653

1654
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
34,831✔
1655
      }
1656

1657
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
1,303,319✔
1658
        int32_t keyLen = 0;
27,228✔
1659
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
27,228✔
1660

1661
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
27,228✔
1662
        if (key == NULL) {
27,228✔
1663
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1664
        }
1665
        (void)memset(key, 0, keyLen);
27,228✔
1666
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
27,228✔
1667

1668
        int32_t valuelen = 0;
27,228✔
1669
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
27,228✔
1670
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
27,228✔
1671
        if (value == NULL) {
27,228✔
1672
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1673
        }
1674
        (void)memset(value, 0, valuelen);
27,228✔
1675
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
27,228✔
1676

1677
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
27,228✔
1678
      }
1679

1680
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
1,303,779✔
1681
        int32_t keyLen = 0;
27,688✔
1682
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
27,688✔
1683

1684
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
27,688✔
1685
        if (key == NULL) {
27,688✔
1686
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1687
        }
1688
        (void)memset(key, 0, keyLen);
27,688✔
1689
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
27,688✔
1690

1691
        int32_t valuelen = 0;
27,688✔
1692
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
27,688✔
1693
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
27,688✔
1694
        if (value == NULL) {
27,688✔
1695
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1696
        }
1697
        (void)memset(value, 0, valuelen);
27,688✔
1698
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
27,688✔
1699

1700
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterViews, key, keyLen, value, valuelen), &lino, _OVER);
27,688✔
1701
      }
1702
    }
1703

1704
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
1,725,593✔
1705
      int32_t keyLen = 0;
449,502✔
1706
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
449,502✔
1707

1708
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
449,502✔
1709
      if (key == NULL) {
449,502✔
1710
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1711
      }
1712
      (void)memset(key, 0, keyLen);
449,502✔
1713
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
449,502✔
1714

1715
      int32_t ref = 0;
449,502✔
1716
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
449,502✔
1717

1718
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
449,502✔
1719
    }
1720
  }
1721
  // decoder white list
1722
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
1,276,091✔
1723
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,276,091✔
1724
      int32_t len = 0;
×
1725
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
1726

1727
      TAOS_MEMORY_REALLOC(key, len);
×
1728
      if (key == NULL) {
×
1729
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1730
      }
1731
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
1732

1733
      SIpWhiteList *pIpWhiteList = NULL;
×
1734
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
1735

1736
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
1737

1738
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
1739
      if (code != 0) {
×
1740
        taosMemoryFreeClear(pIpWhiteList);
×
1741
      }
1742
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1743

1744
      taosMemoryFreeClear(pIpWhiteList);
×
1745

1746
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,276,091✔
1747
      int32_t len = 0;
1,276,091✔
1748
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
1,276,091✔
1749

1750
      TAOS_MEMORY_REALLOC(key, len);
1,276,091✔
1751
      if (key == NULL) {
1,276,091✔
1752
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1753
      }
1754
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
1,276,091✔
1755

1756
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual, sver >= USER_VER_SUPPORT_ADVANCED_SECURITY), &lino, _OVER);
1,276,091✔
1757
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
1,276,091✔
1758
    }
1759
  }
1760

1761
  if (pUser->pIpWhiteListDual == NULL) {
1,276,091✔
1762
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
1763
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1764
  }
1765

1766
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
1,276,091✔
1767

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

1805
    int32_t num = 0;
1,276,091✔
1806
    SDB_GET_INT32(pRaw, dataPos, &num, _OVER);
1,276,091✔
1807
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList) + num * sizeof(SDateTimeWhiteListItem));
1,276,091✔
1808
    if (pUser->pTimeWhiteList == NULL) {
1,276,091✔
1809
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1810
    }
1811

1812
    pUser->pTimeWhiteList->num = num;
1,276,091✔
1813
    for (int32_t i = 0; i < num; i++) {
1,276,091✔
1814
      SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1815
      SDB_GET_BOOL(pRaw, dataPos, &range->absolute, _OVER);
×
1816
      SDB_GET_BOOL(pRaw, dataPos, &range->neg, _OVER);
×
1817
      SDB_GET_INT64(pRaw, dataPos, &range->start, _OVER);
×
1818
      SDB_GET_INT32(pRaw, dataPos, &range->duration, _OVER);
×
1819
    }
1820
  }
1821

1822
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
1,276,091✔
1823
  taosInitRWLatch(&pUser->lock);
1,276,091✔
1824
  dropOldPasswords(pUser);
1,276,091✔
1825

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

1851
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
1,276,091✔
1852
  return pRow;
1,276,091✔
1853
}
1854

1855
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
466,317✔
1856
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
466,317✔
1857

1858
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
466,317✔
1859
  if (pAcct == NULL) {
466,317✔
1860
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
1861
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
1862
    TAOS_RETURN(terrno);
×
1863
  }
1864
  pUser->acctId = pAcct->acctId;
466,317✔
1865
  sdbRelease(pSdb, pAcct);
466,317✔
1866

1867
  return 0;
466,317✔
1868
}
1869

1870
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
35,388,471✔
1871
  int32_t code = 0;
35,388,471✔
1872
  *ppNew =
35,388,471✔
1873
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
35,388,471✔
1874
  if (*ppNew == NULL) {
35,388,471✔
1875
    TAOS_RETURN(terrno);
×
1876
  }
1877

1878
  char *tb = taosHashIterate(pOld, NULL);
35,388,471✔
1879
  while (tb != NULL) {
37,516,004✔
1880
    size_t keyLen = 0;
2,127,533✔
1881
    char  *key = taosHashGetKey(tb, &keyLen);
2,127,533✔
1882

1883
    int32_t valueLen = strlen(tb) + 1;
2,127,533✔
1884
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
2,127,533✔
1885
      taosHashCancelIterate(pOld, tb);
×
1886
      taosHashCleanup(*ppNew);
×
1887
      TAOS_RETURN(code);
×
1888
    }
1889
    tb = taosHashIterate(pOld, tb);
2,127,533✔
1890
  }
1891

1892
  TAOS_RETURN(code);
35,388,471✔
1893
}
1894

1895
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
1,483,014✔
1896
  int32_t code = 0;
1,483,014✔
1897
  *ppNew =
1,483,014✔
1898
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,483,014✔
1899
  if (*ppNew == NULL) {
1,483,014✔
1900
    TAOS_RETURN(terrno);
×
1901
  }
1902

1903
  int32_t *db = taosHashIterate(pOld, NULL);
1,483,014✔
1904
  while (db != NULL) {
1,936,514✔
1905
    size_t keyLen = 0;
453,500✔
1906
    char  *key = taosHashGetKey(db, &keyLen);
453,500✔
1907

1908
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
453,500✔
1909
      taosHashCancelIterate(pOld, db);
×
1910
      taosHashCleanup(*ppNew);
×
1911
      TAOS_RETURN(code);
×
1912
    }
1913
    db = taosHashIterate(pOld, db);
453,500✔
1914
  }
1915

1916
  TAOS_RETURN(code);
1,483,014✔
1917
}
1918

1919
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
1,483,014✔
1920
  int32_t code = 0;
1,483,014✔
1921
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
1,483,014✔
1922
  pNew->authVersion++;
1,483,014✔
1923
  pNew->updateTime = taosGetTimestampMs();
1,483,014✔
1924
  taosInitRWLatch(&pNew->lock);
1,483,014✔
1925

1926
  pNew->passwords = NULL;
1,483,014✔
1927
  pNew->readDbs = NULL;
1,483,014✔
1928
  pNew->writeDbs = NULL;
1,483,014✔
1929
  pNew->readTbs = NULL;
1,483,014✔
1930
  pNew->writeTbs = NULL;
1,483,014✔
1931
  pNew->alterTbs = NULL;
1,483,014✔
1932
  pNew->readViews = NULL;
1,483,014✔
1933
  pNew->writeViews = NULL;
1,483,014✔
1934
  pNew->alterViews = NULL;
1,483,014✔
1935
  pNew->topics = NULL;
1,483,014✔
1936
  pNew->useDbs = NULL;
1,483,014✔
1937
  pNew->pIpWhiteListDual = NULL;
1,483,014✔
1938
  pNew->pTimeWhiteList = NULL;
1,483,014✔
1939

1940
  taosRLockLatch(&pUser->lock);
1,483,014✔
1941
  pNew->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,483,014✔
1942
  if (pNew->passwords == NULL) {
1,483,014✔
1943
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1944
    goto _OVER;
×
1945
  }
1946
  (void)memcpy(pNew->passwords, pUser->passwords, pUser->numOfPasswords * sizeof(SUserPassword));
1,483,014✔
1947

1948
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->readDbs, &pNew->readDbs), NULL, _OVER);
1,483,014✔
1949
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->writeDbs, &pNew->writeDbs), NULL, _OVER);
1,483,014✔
1950
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readTbs, &pNew->readTbs), NULL, _OVER);
1,483,014✔
1951
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeTbs, &pNew->writeTbs), NULL, _OVER);
1,483,014✔
1952
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterTbs, &pNew->alterTbs), NULL, _OVER);
1,483,014✔
1953
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readViews, &pNew->readViews), NULL, _OVER);
1,483,014✔
1954
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeViews, &pNew->writeViews), NULL, _OVER);
1,483,014✔
1955
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterViews, &pNew->alterViews), NULL, _OVER);
1,483,014✔
1956
  TAOS_CHECK_GOTO(mndDupTopicHash(pUser->topics, &pNew->topics), NULL, _OVER);
1,483,014✔
1957
  TAOS_CHECK_GOTO(mndDupUseDbHash(pUser->useDbs, &pNew->useDbs), NULL, _OVER);
1,483,014✔
1958
  pNew->pIpWhiteListDual = cloneIpWhiteList(pUser->pIpWhiteListDual);
1,483,014✔
1959
  if (pNew->pIpWhiteListDual == NULL) {
1,483,014✔
1960
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1961
    goto _OVER;
×
1962
  }
1963

1964
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
1,483,014✔
1965
  if (pNew->pTimeWhiteList == NULL) {
1,483,014✔
1966
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1967
    goto _OVER;
×
1968
  }
1969

1970
_OVER:
1,483,014✔
1971
  taosRUnLockLatch(&pUser->lock);
1,483,014✔
1972
  TAOS_RETURN(code);
1,483,014✔
1973
}
1974

1975
void mndUserFreeObj(SUserObj *pUser) {
5,296,602✔
1976
  taosHashCleanup(pUser->readDbs);
5,296,602✔
1977
  taosHashCleanup(pUser->writeDbs);
5,296,602✔
1978
  taosHashCleanup(pUser->topics);
5,296,602✔
1979
  taosHashCleanup(pUser->readTbs);
5,296,602✔
1980
  taosHashCleanup(pUser->writeTbs);
5,296,602✔
1981
  taosHashCleanup(pUser->alterTbs);
5,296,602✔
1982
  taosHashCleanup(pUser->readViews);
5,296,602✔
1983
  taosHashCleanup(pUser->writeViews);
5,296,602✔
1984
  taosHashCleanup(pUser->alterViews);
5,296,602✔
1985
  taosHashCleanup(pUser->useDbs);
5,296,602✔
1986
  taosMemoryFreeClear(pUser->passwords);
5,296,602✔
1987
  taosMemoryFreeClear(pUser->pIpWhiteListDual);
5,296,602✔
1988
  taosMemoryFreeClear(pUser->pTimeWhiteList);
5,296,602✔
1989
  pUser->readDbs = NULL;
5,296,602✔
1990
  pUser->writeDbs = NULL;
5,296,602✔
1991
  pUser->topics = NULL;
5,296,602✔
1992
  pUser->readTbs = NULL;
5,296,602✔
1993
  pUser->writeTbs = NULL;
5,296,602✔
1994
  pUser->alterTbs = NULL;
5,296,602✔
1995
  pUser->readViews = NULL;
5,296,602✔
1996
  pUser->writeViews = NULL;
5,296,602✔
1997
  pUser->alterViews = NULL;
5,296,602✔
1998
  pUser->useDbs = NULL;
5,296,602✔
1999
}
5,296,602✔
2000

2001
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
1,276,043✔
2002
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
1,276,043✔
2003
  mndUserFreeObj(pUser);
1,276,043✔
2004
  return 0;
1,276,043✔
2005
}
2006

2007
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
776,213✔
2008
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
776,213✔
2009
  taosWLockLatch(&pOld->lock);
776,213✔
2010
  pOld->updateTime = pNew->updateTime;
776,213✔
2011
  pOld->authVersion = pNew->authVersion;
776,213✔
2012
  pOld->passVersion = pNew->passVersion;
776,213✔
2013
  pOld->sysInfo = pNew->sysInfo;
776,213✔
2014
  pOld->enable = pNew->enable;
776,213✔
2015
  pOld->flag = pNew->flag;
776,213✔
2016
  pOld->changePass = pNew->changePass;
776,213✔
2017

2018
  pOld->sessionPerUser = pNew->sessionPerUser;
776,213✔
2019
  pOld->connectTime = pNew->connectTime;
776,213✔
2020
  pOld->connectIdleTime = pNew->connectIdleTime;
776,213✔
2021
  pOld->callPerSession = pNew->callPerSession;
776,213✔
2022
  pOld->vnodePerCall = pNew->vnodePerCall;
776,213✔
2023
  pOld->failedLoginAttempts = pNew->failedLoginAttempts;
776,213✔
2024
  pOld->passwordLifeTime = pNew->passwordLifeTime;
776,213✔
2025
  pOld->passwordReuseTime = pNew->passwordReuseTime;
776,213✔
2026
  pOld->passwordReuseMax = pNew->passwordReuseMax;
776,213✔
2027
  pOld->passwordLockTime = pNew->passwordLockTime;
776,213✔
2028
  pOld->passwordGraceTime = pNew->passwordGraceTime;
776,213✔
2029
  pOld->inactiveAccountTime = pNew->inactiveAccountTime;
776,213✔
2030
  pOld->allowTokenNum = pNew->allowTokenNum;
776,213✔
2031
  pOld->tokenNum = pNew->tokenNum;
776,213✔
2032

2033
  pOld->numOfPasswords = pNew->numOfPasswords;
776,213✔
2034
  TSWAP(pOld->passwords, pNew->passwords);
776,213✔
2035
  (void)memcpy(pOld->salt, pNew->salt, sizeof(pOld->salt));
776,213✔
2036
  (void)memcpy(pOld->totpsecret, pNew->totpsecret, sizeof(pOld->totpsecret));
776,213✔
2037
  TSWAP(pOld->readDbs, pNew->readDbs);
776,213✔
2038
  TSWAP(pOld->writeDbs, pNew->writeDbs);
776,213✔
2039
  TSWAP(pOld->topics, pNew->topics);
776,213✔
2040
  TSWAP(pOld->readTbs, pNew->readTbs);
776,213✔
2041
  TSWAP(pOld->writeTbs, pNew->writeTbs);
776,213✔
2042
  TSWAP(pOld->alterTbs, pNew->alterTbs);
776,213✔
2043
  TSWAP(pOld->readViews, pNew->readViews);
776,213✔
2044
  TSWAP(pOld->writeViews, pNew->writeViews);
776,213✔
2045
  TSWAP(pOld->alterViews, pNew->alterViews);
776,213✔
2046
  TSWAP(pOld->useDbs, pNew->useDbs);
776,213✔
2047

2048
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
776,213✔
2049
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
776,213✔
2050
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
776,213✔
2051
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
776,213✔
2052
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
776,213✔
2053

2054
  taosWUnLockLatch(&pOld->lock);
776,213✔
2055

2056
  return 0;
776,213✔
2057
}
2058

2059
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
75,585,344✔
2060
  int32_t code = 0;
75,585,344✔
2061
  SSdb   *pSdb = pMnode->pSdb;
75,585,344✔
2062

2063
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
75,586,581✔
2064
  if (*ppUser == NULL) {
75,586,074✔
2065
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
67,479✔
2066
      code = TSDB_CODE_MND_USER_NOT_EXIST;
67,479✔
2067
    } else {
2068
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2069
    }
2070
  }
2071
  TAOS_RETURN(code);
75,586,074✔
2072
}
2073

2074
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
75,687,286✔
2075
  SSdb *pSdb = pMnode->pSdb;
75,687,286✔
2076
  sdbRelease(pSdb, pUser);
75,687,440✔
2077
}
75,688,664✔
2078

2079

2080

2081
int32_t mndEncryptPass(char *pass, const char* salt, int8_t *algo) {
63,410✔
2082
  int32_t code = 0;
63,410✔
2083
  if (tsMetaKey[0] == '\0') {
63,410✔
2084
    return 0;
63,410✔
2085
  }
2086

UNCOV
2087
  if (salt[0] != 0) {
×
2088
    char passAndSalt[TSDB_PASSWORD_LEN - 1 + TSDB_PASSWORD_SALT_LEN];
×
UNCOV
2089
    (void)memcpy(passAndSalt, pass, TSDB_PASSWORD_LEN - 1);
×
UNCOV
2090
    (void)memcpy(passAndSalt + TSDB_PASSWORD_LEN - 1, salt, TSDB_PASSWORD_SALT_LEN);
×
2091
    taosEncryptPass_c((uint8_t *)passAndSalt, sizeof(passAndSalt), pass);
2092
  }
2093

UNCOV
2094
  unsigned char packetData[TSDB_PASSWORD_LEN] = {0};
×
UNCOV
2095
  SCryptOpts opts = {0};
×
UNCOV
2096
  opts.len = TSDB_PASSWORD_LEN;
×
UNCOV
2097
  opts.source = pass;
×
UNCOV
2098
  opts.result = packetData;
×
UNCOV
2099
  opts.unitLen = TSDB_PASSWORD_LEN;
×
NEW
2100
  tstrncpy(opts.key, tsDbKey, ENCRYPT_KEY_LEN + 1);
×
UNCOV
2101
  int newLen = Builtin_CBC_Encrypt(&opts);
×
UNCOV
2102
  if (newLen <= 0) return terrno;
×
2103

UNCOV
2104
  memcpy(pass, packetData, newLen);
×
UNCOV
2105
  if (algo != NULL) {
×
UNCOV
2106
    *algo = DND_CA_SM4;
×
2107
  }
2108

UNCOV
2109
  return 0;
×
2110
}
2111

2112

2113

2114
static void generateSalt(char *salt, size_t len) {
58,256✔
2115
  const char* set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
58,256✔
2116
  int32_t     setLen = 62;
58,256✔
2117
  for (int32_t i = 0; i < len - 1; ++i) {
1,864,192✔
2118
    salt[i] = set[taosSafeRand() % setLen];
1,805,936✔
2119
  }
2120
  salt[len - 1] = 0;
58,256✔
2121
}
58,256✔
2122

2123

2124

2125
static int32_t addDefaultIpToTable(int8_t enableIpv6, SHashObj *pUniqueTab) {
482✔
2126
  int32_t code = 0;
482✔
2127
  int32_t lino = 0;
482✔
2128
  int32_t dummpy = 0;
482✔
2129

2130
  SIpRange ipv4 = {0}, ipv6 = {0};
482✔
2131
  code = createDefaultIp4Range(&ipv4);
482✔
2132
  TSDB_CHECK_CODE(code, lino, _error);
482✔
2133

2134
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummpy, sizeof(dummpy));
482✔
2135
  TSDB_CHECK_CODE(code, lino, _error);
482✔
2136

2137
  if (enableIpv6) {
482✔
2138
    code = createDefaultIp6Range(&ipv6);
×
2139
    TSDB_CHECK_CODE(code, lino, _error);
×
2140

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

2151
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
57,930✔
2152
  int32_t  code = 0;
57,930✔
2153
  int32_t  lino = 0;
57,930✔
2154
  SUserObj userObj = {0};
57,930✔
2155

2156
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
57,930✔
2157
  if (userObj.passwords == NULL) {
57,930✔
2158
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2159
  }
2160
  userObj.numOfPasswords = 1;
57,930✔
2161

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

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

2182
  userObj.createdTime = taosGetTimestampMs();
57,930✔
2183
  userObj.updateTime = userObj.createdTime;
57,930✔
2184
  userObj.superUser = 0;  // pCreate->superUser;
57,930✔
2185
  userObj.sysInfo = pCreate->sysInfo;
57,930✔
2186
  userObj.enable = pCreate->enable;
57,930✔
2187
  userObj.createdb = pCreate->createDb;
57,930✔
2188

2189
  userObj.changePass = pCreate->changepass;
57,930✔
2190
  userObj.sessionPerUser = pCreate->sessionPerUser;
57,930✔
2191
  userObj.connectTime = pCreate->connectTime;
57,930✔
2192
  userObj.connectIdleTime = pCreate->connectIdleTime;
57,930✔
2193
  userObj.callPerSession = pCreate->callPerSession;
57,930✔
2194
  userObj.vnodePerCall = pCreate->vnodePerCall;
57,930✔
2195
  userObj.failedLoginAttempts = pCreate->failedLoginAttempts;
57,930✔
2196
  userObj.passwordLifeTime = pCreate->passwordLifeTime;
57,930✔
2197
  userObj.passwordReuseTime = pCreate->passwordReuseTime;
57,930✔
2198
  userObj.passwordReuseMax = pCreate->passwordReuseMax;
57,930✔
2199
  userObj.passwordLockTime = pCreate->passwordLockTime;
57,930✔
2200
  userObj.passwordGraceTime = pCreate->passwordGraceTime;
57,930✔
2201
  userObj.inactiveAccountTime = pCreate->inactiveAccountTime;
57,930✔
2202
  userObj.allowTokenNum = pCreate->allowTokenNum;
57,930✔
2203
  userObj.tokenNum = 0;
57,930✔
2204

2205
  if (pCreate->numIpRanges == 0) {
57,930✔
2206
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
57,448✔
2207
  } else {
2208
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
482✔
2209
    if (pUniqueTab == NULL) {
482✔
2210
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2211
    }
2212
    int32_t dummpy = 0;
482✔
2213
    
2214
    for (int i = 0; i < pCreate->numIpRanges; i++) {
1,284✔
2215
      SIpRange range = {0};
802✔
2216
      copyIpRange(&range, pCreate->pIpDualRanges + i);
802✔
2217
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
802✔
2218
        taosHashCleanup(pUniqueTab);
×
2219
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2220
      }
2221
    }
2222

2223
    code = addDefaultIpToTable(tsEnableIpv6, pUniqueTab);
482✔
2224
    if (code != 0) {
482✔
2225
      taosHashCleanup(pUniqueTab);
×
2226
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2227
    }
2228

2229
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
482✔
2230
      taosHashCleanup(pUniqueTab);
×
2231
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2232
    }
2233

2234
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
482✔
2235
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
482✔
2236
    if (p == NULL) {
482✔
2237
      taosHashCleanup(pUniqueTab);
×
2238
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2239
    }
2240

2241
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
482✔
2242
    for (int32_t i = 0; i < numOfRanges; i++) {
1,444✔
2243
      size_t    len = 0;
962✔
2244
      SIpRange *key = taosHashGetKey(pIter, &len);
962✔
2245
      memcpy(&p->pIpRanges[i], key, sizeof(SIpRange));
962✔
2246
      pIter = taosHashIterate(pUniqueTab, pIter);
962✔
2247
    }
2248

2249
    taosHashCleanup(pUniqueTab);
482✔
2250
    p->num = numOfRanges;
482✔
2251
    sortIpWhiteList(p);
482✔
2252
    userObj.pIpWhiteListDual = p;
482✔
2253
  }
2254

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

2272
      // no need to add expired range
2273
      if (isDateTimeWhiteListItemExpired(&range)) {
×
2274
        continue;
×
2275
      }
2276

2277
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
×
2278
        taosHashCleanup(pUniqueTab);
×
2279
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2280
      }
2281
    }
2282

2283
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_TIME_RANGE) {
×
2284
      taosHashCleanup(pUniqueTab);
×
2285
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
2286
    }
2287

2288
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
×
2289
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
2290
    if (p == NULL) {
×
2291
      taosHashCleanup(pUniqueTab);
×
2292
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2293
    }
2294

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

2303
    taosHashCleanup(pUniqueTab);
×
2304
    p->num = numOfRanges;
×
2305
    sortTimeWhiteList(p);
×
2306
    userObj.pTimeWhiteList = p;
×
2307
  }
2308

2309
  userObj.ipWhiteListVer = taosGetTimestampMs();
57,930✔
2310
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
57,930✔
2311

2312
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
57,930✔
2313
  if (pTrans == NULL) {
57,930✔
2314
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
2315
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2316
  }
2317
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
57,930✔
2318

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

2327
  if (mndTransPrepare(pMnode, pTrans) != 0) {
57,930✔
2328
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2329
    mndTransDrop(pTrans);
×
2330
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2331
  }
2332

2333
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
57,930✔
2334
    mndTransDrop(pTrans);
×
2335
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2336
  }
2337

2338
  mndTransDrop(pTrans);
57,930✔
2339

2340
_OVER:
57,930✔
2341
  taosMemoryFree(userObj.passwords);
57,930✔
2342
  taosMemoryFree(userObj.pIpWhiteListDual);
57,930✔
2343
  taosMemoryFree(userObj.pTimeWhiteList);
57,930✔
2344
  TAOS_RETURN(code);
57,930✔
2345
}
2346

2347

2348
static int32_t mndCheckPasswordFmt(const char *pwd) {
63,730✔
2349
  if (strcmp(pwd, "taosdata") == 0) {
63,730✔
2350
    return 0;
17,624✔
2351
  }
2352

2353
  if (tsEnableStrongPassword == 0) {
46,106✔
2354
    for (char c = *pwd; c != 0; c = *(++pwd)) {
44,388✔
2355
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
43,902✔
2356
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2357
      }
2358
    }
2359
    return 0;
486✔
2360
  }
2361

2362
  int32_t len = strlen(pwd);
45,620✔
2363
  if (len < TSDB_PASSWORD_MIN_LEN) {
45,620✔
2364
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
2365
  }
2366

2367
  if (len > TSDB_PASSWORD_MAX_LEN) {
45,620✔
2368
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
2369
  }
2370

2371
  if (taosIsComplexString(pwd)) {
45,620✔
2372
    return 0;
45,620✔
2373
  }
2374

2375
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2376
}
2377

2378

2379

2380
static int32_t mndCheckTotpSeedFmt(const char *seed) {
×
2381
  int32_t len = strlen(seed);
×
2382
  if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
2383
    return TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT;
×
2384
  }
2385

2386
  if (taosIsComplexString(seed)) {
×
2387
    return 0;
×
2388
  }
2389

2390
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
2391
}
2392

2393

2394

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

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

2410
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
×
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) {
569✔
2447
  (void)taosThreadRwlockRdlock(&userCache.rw);
569✔
2448
  
2449
  int32_t count = taosHashGetSize(userCache.users);
569✔
2450
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
569✔
2451
  if (pRsp->pUsers == NULL) {
569✔
2452
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
2453
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2454
  }
2455

2456
  count = 0;
569✔
2457
  void   *pIter = taosHashIterate(userCache.users, NULL);
569✔
2458
  while (pIter) {
1,138✔
2459
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
569✔
2460
    if (wl == NULL || wl->num <= 0) {
569✔
2461
      pIter = taosHashIterate(userCache.users, pIter);
569✔
2462
      continue;
569✔
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;
569✔
2485
  pRsp->ver = userCache.verTime;
569✔
2486
  (void)taosThreadRwlockUnlock(&userCache.rw);
569✔
2487
  TAOS_RETURN(0);
569✔
2488
}
2489

2490

2491

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

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

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

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

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

2522
_OVER:
569✔
2523
  if (code < 0) {
569✔
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;
569✔
2530
  pReq->info.rsp = pRsp;
569✔
2531
  pReq->info.rspLen = len;
569✔
2532

2533
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
569✔
2534
  TAOS_RETURN(code);
569✔
2535
}
2536

2537

2538

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

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

2552
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
58,250✔
2553

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

2560
  if (createReq.isImport != 1) {
58,250✔
2561
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_CREATE_USER), &lino, _OVER);
58,250✔
2562
  } else if (strcmp(RPC_MSG_USER(pReq), "root") != 0) {
×
2563
    mError("The operation is not permitted, user:%s", RPC_MSG_USER(pReq));
×
2564
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
2565
  }
2566

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

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

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

2581
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
58,250✔
2582
  if (pUser != NULL) {
58,250✔
2583
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
320✔
2584
  }
2585

2586
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser), &lino, _OVER);
57,930✔
2587
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
57,930✔
2588

2589
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
57,930✔
2590
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
57,930✔
2591

2592
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
57,930✔
2593
    char detail[1000] = {0};
57,930✔
2594
    (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
57,930✔
2595
                    createReq.superUser, createReq.sysInfo);
57,930✔
2596
    char operation[15] = {0};
57,930✔
2597
    if (createReq.isImport == 1) {
57,930✔
2598
      tstrncpy(operation, "importUser", sizeof(operation));
×
2599
    } else {
2600
      tstrncpy(operation, "createUser", sizeof(operation));
57,930✔
2601
    }
2602

2603
    int64_t tse = taosGetTimestampMs();
57,930✔
2604
    double  duration = (double)(tse - tss);
57,930✔
2605
    duration = duration / 1000;
57,930✔
2606
    auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail), duration, 0);
57,930✔
2607
  }
2608

2609
_OVER:
58,250✔
2610
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExists) {
58,250✔
2611
    code = 0;
×
2612
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
58,250✔
2613
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
320✔
2614
  }
2615

2616
  mndReleaseUser(pMnode, pUser);
58,250✔
2617
  mndReleaseUser(pMnode, pOperUser);
58,250✔
2618
  tFreeSCreateUserReq(&createReq);
58,250✔
2619

2620
  TAOS_RETURN(code);
58,250✔
2621
}
2622

2623

2624

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

2635
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
19,522✔
2636
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserIpWhiteListRsp * pRsp) = NULL;
19,522✔
2637

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

2650
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, wlReq.user, &pUser), &lino, _OVER);
19,522✔
2651
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
19,522✔
2652

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

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

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

2680
  TAOS_RETURN(code);
19,522✔
2681
}
2682

2683

2684

2685
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
569✔
2686
  (void)taosThreadRwlockRdlock(&userCache.rw);
569✔
2687

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

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

2704
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
569✔
2705
    pUser->ver = userCache.verIp;
569✔
2706

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

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

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

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

2729

2730

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

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

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

2752
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
569✔
2753

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

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

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

2779
  tFreeSUpdateIpWhiteReq(&ipWhite);
569✔
2780
  TAOS_RETURN(code);
569✔
2781
}
2782

2783

2784

2785
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pNew, SRpcMsg *pReq) {
759,965✔
2786
  int32_t code = 0;
759,965✔
2787
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
759,965✔
2788
  if (pTrans == NULL) {
759,965✔
2789
    mError("user:%s, failed to alter since %s", pNew->user, terrstr());
×
2790
    TAOS_RETURN(terrno);
×
2791
  }
2792
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pNew->user);
759,965✔
2793

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

2806
  if (mndTransPrepare(pMnode, pTrans) != 0) {
759,965✔
2807
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2808
    mndTransDrop(pTrans);
×
2809
    TAOS_RETURN(terrno);
×
2810
  }
2811
  if ((code = userCacheUpdateWhiteList(pMnode, pNew)) != 0) {
759,965✔
2812
    mndTransDrop(pTrans);
×
2813
    TAOS_RETURN(code);
×
2814
  }
2815
  mndTransDrop(pTrans);
759,965✔
2816
  return 0;
759,965✔
2817
}
2818

2819
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
12,017,724✔
2820
  int32_t code = 0;
12,017,724✔
2821

2822
  *ppNew =
12,017,724✔
2823
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
12,017,724✔
2824
  if (*ppNew == NULL) {
12,017,724✔
2825
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2826
    TAOS_RETURN(code);
×
2827
  }
2828

2829
  char *db = taosHashIterate(pOld, NULL);
12,017,724✔
2830
  while (db != NULL) {
12,991,657✔
2831
    int32_t len = strlen(db) + 1;
973,933✔
2832
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
973,933✔
2833
      taosHashCancelIterate(pOld, db);
×
2834
      taosHashCleanup(*ppNew);
×
2835
      TAOS_RETURN(code);
×
2836
    }
2837
    db = taosHashIterate(pOld, db);
973,933✔
2838
  }
2839

2840
  TAOS_RETURN(code);
12,017,724✔
2841
}
2842

2843
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
10,534,710✔
2844

2845
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
1,483,014✔
2846

2847
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
370,345✔
2848
                                  SSdb *pSdb) {
2849
  void *pIter = NULL;
370,345✔
2850
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
370,345✔
2851

2852
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
370,345✔
2853
  int32_t len = strlen(tbFName) + 1;
370,345✔
2854

2855
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
407,976✔
2856
    char *value = taosHashGet(hash, tbFName, len);
37,631✔
2857
    if (value != NULL) {
37,631✔
2858
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2859
    }
2860

2861
    int32_t condLen = alterReq->tagCondLen;
37,631✔
2862
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
37,631✔
2863
  } else {
2864
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
332,714✔
2865
  }
2866

2867
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
370,345✔
2868
  int32_t  ref = 1;
370,345✔
2869
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
370,345✔
2870
  if (NULL != currRef) {
370,345✔
2871
    ref = (*currRef) + 1;
216,976✔
2872
  }
2873
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
370,345✔
2874

2875
  TAOS_RETURN(0);
370,345✔
2876
}
2877

2878
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
342,813✔
2879
                                        SSdb *pSdb) {
2880
  void *pIter = NULL;
342,813✔
2881
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
342,813✔
2882
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
342,813✔
2883
  int32_t len = strlen(tbFName) + 1;
342,813✔
2884

2885
  if (taosHashRemove(hash, tbFName, len) != 0) {
342,813✔
2886
    TAOS_RETURN(0);  // not found
1,041✔
2887
  }
2888

2889
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
341,772✔
2890
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
341,772✔
2891
  if (NULL == currRef) {
341,772✔
2892
    return 0;
×
2893
  }
2894

2895
  if (1 == *currRef) {
341,772✔
2896
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
140,353✔
2897
      TAOS_RETURN(0);  // not found
×
2898
    }
2899
    return 0;
140,353✔
2900
  }
2901
  int32_t ref = (*currRef) - 1;
201,419✔
2902
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
201,419✔
2903

2904
  return 0;
201,419✔
2905
}
2906

2907

2908

2909
static int32_t mndProcessAlterUserPrivilegesReq(SRpcMsg* pReq, SAlterUserReq *pAlterReq) {
754,700✔
2910
  SMnode   *pMnode = pReq->info.node;
754,700✔
2911
  SSdb     *pSdb = pMnode->pSdb;
754,700✔
2912
  int32_t   code = 0, lino = 0;
754,700✔
2913
  SUserObj *pUser = NULL;
754,700✔
2914
  SUserObj  newUser = {0};
754,700✔
2915
  int64_t   tss = taosGetTimestampMs();
754,700✔
2916

2917
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
754,700✔
2918
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino, _OVER);
751,946✔
2919
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
743,635✔
2920

2921
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
743,635✔
2922
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
636,162✔
2923
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
107,473✔
2924
      int32_t len = strlen(pAlterReq->objname) + 1;
105,867✔
2925
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
105,867✔
2926
      if (pDb == NULL) {
105,867✔
2927
        mndReleaseDb(pMnode, pDb);
1,708✔
2928
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
1,708✔
2929
      }
2930
      if ((code = taosHashPut(newUser.readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
104,159✔
2931
          0) {
2932
        mndReleaseDb(pMnode, pDb);
×
2933
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2934
      }
2935
      mndReleaseDb(pMnode, pDb);
104,159✔
2936
    } else {
2937
      void   *pIter = NULL;
1,606✔
2938
      while (1) {
2,578✔
2939
        SDbObj *pDb = NULL;
4,184✔
2940
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
4,184✔
2941
        if (pIter == NULL) break;
4,184✔
2942
        int32_t len = strlen(pDb->name) + 1;
2,578✔
2943
        if ((code = taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
2,578✔
2944
          sdbRelease(pSdb, pDb);
×
2945
          sdbCancelFetch(pSdb, pIter);
×
2946
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2947
        }
2948
        sdbRelease(pSdb, pDb);
2,578✔
2949
      }
2950
    }
2951
  }
2952

2953
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,927✔
2954
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
636,956✔
2955
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
104,971✔
2956
      int32_t len = strlen(pAlterReq->objname) + 1;
103,525✔
2957
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
103,525✔
2958
      if (pDb == NULL) {
103,525✔
2959
        mndReleaseDb(pMnode, pDb);
162✔
2960
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
162✔
2961
      }
2962
      if ((code = taosHashPut(newUser.writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
103,363✔
2963
          0) {
2964
        mndReleaseDb(pMnode, pDb);
×
2965
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2966
      }
2967
      mndReleaseDb(pMnode, pDb);
103,363✔
2968
    } else {
2969
      void   *pIter = NULL;
1,446✔
2970
      while (1) {
2,418✔
2971
        SDbObj *pDb = NULL;
3,864✔
2972
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
3,864✔
2973
        if (pIter == NULL) break;
3,864✔
2974
        int32_t len = strlen(pDb->name) + 1;
2,418✔
2975
        if ((code = taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
2,418✔
2976
          sdbRelease(pSdb, pDb);
×
2977
          sdbCancelFetch(pSdb, pIter);
×
2978
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2979
        }
2980
        sdbRelease(pSdb, pDb);
2,418✔
2981
      }
2982
    }
2983
  }
2984

2985
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,765✔
2986
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
642,757✔
2987
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
99,008✔
2988
      int32_t len = strlen(pAlterReq->objname) + 1;
97,402✔
2989
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
97,402✔
2990
      if (pDb == NULL) {
97,402✔
2991
        mndReleaseDb(pMnode, pDb);
160✔
2992
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
160✔
2993
      }
2994
      code = taosHashRemove(newUser.readDbs, pAlterReq->objname, len);
97,242✔
2995
      if (code < 0) {
97,242✔
2996
        mError("read db:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
×
2997
      }
2998
      mndReleaseDb(pMnode, pDb);
97,242✔
2999
    } else {
3000
      taosHashClear(newUser.readDbs);
1,606✔
3001
    }
3002
  }
3003

3004
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3005
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
642,786✔
3006
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
98,819✔
3007
      int32_t len = strlen(pAlterReq->objname) + 1;
97,053✔
3008
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
97,053✔
3009
      if (pDb == NULL) {
97,053✔
3010
        mndReleaseDb(pMnode, pDb);
×
3011
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
3012
      }
3013
      code = taosHashRemove(newUser.writeDbs, pAlterReq->objname, len);
97,053✔
3014
      if (code < 0) {
97,053✔
3015
        mError("user:%s, failed to remove db:%s since %s", newUser.user, pAlterReq->objname, terrstr());
×
3016
      }
3017
      mndReleaseDb(pMnode, pDb);
97,053✔
3018
    } else {
3019
      taosHashClear(newUser.writeDbs);
1,766✔
3020
    }
3021
  }
3022

3023
  SHashObj *pReadTbs = newUser.readTbs;
741,605✔
3024
  SHashObj *pWriteTbs = newUser.writeTbs;
741,605✔
3025
  SHashObj *pAlterTbs = newUser.alterTbs;
741,605✔
3026

3027
#ifdef TD_ENTERPRISE
3028
  if (pAlterReq->isView) {
741,605✔
3029
    pReadTbs = newUser.readViews;
9,935✔
3030
    pWriteTbs = newUser.writeViews;
9,935✔
3031
    pAlterTbs = newUser.alterViews;
9,935✔
3032
  }
3033
#endif
3034

3035
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3036
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
593,337✔
3037
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
148,268✔
3038
  }
3039

3040
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3041
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
594,885✔
3042
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
146,720✔
3043
  }
3044

3045
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3046
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
666,248✔
3047
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
75,357✔
3048
  }
3049

3050
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3051
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
603,845✔
3052
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
137,760✔
3053
  }
3054

3055
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3056
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
604,063✔
3057
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
137,542✔
3058
  }
3059

3060
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,605✔
3061
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
674,094✔
3062
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, newUser.useDbs, pAlterReq, pSdb), &lino, _OVER);
67,511✔
3063
  }
3064

3065
#ifdef USE_TOPIC
3066
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
741,605✔
3067
    int32_t      len = strlen(pAlterReq->objname) + 1;
2,881✔
3068
    SMqTopicObj *pTopic = NULL;
2,881✔
3069
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
2,881✔
3070
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3071
    }
3072
    taosRLockLatch(&pTopic->lock);
2,881✔
3073
    code = taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
2,881✔
3074
    taosRUnLockLatch(&pTopic->lock);
2,881✔
3075
    mndReleaseTopic(pMnode, pTopic);
2,881✔
3076
    TAOS_CHECK_GOTO(code, &lino, _OVER);
2,881✔
3077
  }
3078

3079
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
741,605✔
3080
    int32_t      len = strlen(pAlterReq->objname) + 1;
1,920✔
3081
    SMqTopicObj *pTopic = NULL;
1,920✔
3082
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
1,920✔
3083
      TAOS_CHECK_GOTO(code, &lino, _OVER);
160✔
3084
    }
3085
    taosRLockLatch(&pTopic->lock);
1,760✔
3086
    code = taosHashRemove(newUser.topics, pAlterReq->objname, len);
1,760✔
3087
    if (code < 0) {
1,760✔
3088
      mError("user:%s, failed to remove topic:%s since %s", newUser.user, pAlterReq->objname, tstrerror(code));
×
3089
    }
3090
    taosRUnLockLatch(&pTopic->lock);
1,760✔
3091
    mndReleaseTopic(pMnode, pTopic);
1,760✔
3092
  }
3093
#endif
3094

3095
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
741,445✔
3096
  code = TSDB_CODE_ACTION_IN_PROGRESS;
741,445✔
3097

3098
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
741,445✔
3099
    int64_t tse = taosGetTimestampMs();
741,445✔
3100
    double  duration = (double)(tse - tss);
741,445✔
3101
    duration = duration / 1000;
741,445✔
3102
    if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
741,445✔
3103
              ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
635,680✔
3104
              ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
584,526✔
3105
              ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
584,526✔
3106
              ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
436,258✔
3107
              ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
361,975✔
3108
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
379,470✔
3109
        SName name = {0};
377,222✔
3110
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
377,222✔
3111
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
377,222✔
3112
      } else {
3113
        auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
2,248✔
3114
      }
3115
    } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
361,975✔
3116
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
2,881✔
3117
    } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
359,094✔
3118
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", pAlterReq->objname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
1,760✔
3119
    } else {
3120
      if (strcmp(pAlterReq->objname, "1.*") != 0) {
357,334✔
3121
        SName name = {0};
354,766✔
3122
        TAOS_CHECK_GOTO(tNameFromString(&name, pAlterReq->objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
354,766✔
3123
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
354,766✔
3124
      } else {
3125
        auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", pAlterReq->user, pAlterReq->sql, pAlterReq->sqlLen, duration, 0);
2,568✔
3126
      }
3127
    }
3128
  }
3129
  
3130
_OVER:
754,700✔
3131
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
754,700✔
3132
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
13,255✔
3133
  }
3134
  mndReleaseUser(pMnode, pUser);
754,700✔
3135
  mndUserFreeObj(&newUser);
754,700✔
3136
  TAOS_RETURN(code);
754,700✔
3137
}
3138

3139

3140

3141
static int32_t mndProcessAlterUserBasicInfoReq(SRpcMsg *pReq, SAlterUserReq *pAlterReq) {
21,475✔
3142
  SMnode       *pMnode = pReq->info.node;
21,475✔
3143
  int32_t       code = 0, lino = 0;
21,475✔
3144
  SUserObj     *pUser = NULL;
21,475✔
3145
  SUserObj      newUser = {0};
21,475✔
3146
  char          auditLog[1000] = {0};
21,475✔
3147
  int32_t       auditLen = 0;
21,475✔
3148
  int64_t       tss = taosGetTimestampMs();
21,475✔
3149

3150
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pAlterReq->user, &pUser), &lino, _OVER);
21,475✔
3151
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), pUser, pAlterReq), &lino, _OVER);
20,630✔
3152
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
18,683✔
3153

3154
  if (pAlterReq->hasPassword) {
18,683✔
3155
    auditLen += tsnprintf(auditLog, sizeof(auditLog), "password,");
5,480✔
3156

3157
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(pAlterReq->pass), &lino, _OVER);
5,480✔
3158
    if (newUser.salt[0] == 0) {
5,480✔
3159
      generateSalt(newUser.salt, sizeof(newUser.salt));
326✔
3160
    }
3161
    char pass[TSDB_PASSWORD_LEN] = {0};
5,480✔
3162
    taosEncryptPass_c((uint8_t *)pAlterReq->pass, strlen(pAlterReq->pass), pass);
5,480✔
3163
    pass[sizeof(pass) - 1] = 0;
5,480✔
3164
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
5,480✔
3165

3166
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
5,480✔
3167
      for(int32_t i = 0; i < newUser.numOfPasswords; ++i) {
11,721✔
3168
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
8,210✔
3169
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
163✔
3170
        }
3171
      }
3172
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
3,511✔
3173
      if (passwords == NULL) {
3,511✔
3174
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3175
      }
3176
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
3,511✔
3177
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
3,511✔
3178
      passwords[0].setTime = taosGetTimestampSec();
3,511✔
3179
      taosMemoryFree(newUser.passwords);
3,511✔
3180
      newUser.passwords = passwords;
3,511✔
3181
      ++newUser.numOfPasswords;
3,511✔
3182
      ++newUser.passVersion;
3,511✔
3183
      newUser.changePass = 2;
3,511✔
3184
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
1,806✔
3185
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
1,283✔
3186
      newUser.passwords[0].setTime = taosGetTimestampSec();
1,283✔
3187
      ++newUser.passVersion;
1,283✔
3188
      newUser.changePass = 2;
1,283✔
3189
    }
3190
  }
3191

3192
  if (pAlterReq->hasTotpseed) {
18,520✔
3193
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "totpseed,");
×
3194

3195
    if (pAlterReq->totpseed[0] == 0) { // clear totp secret
×
3196
      memset(newUser.totpsecret, 0, sizeof(newUser.totpsecret));
×
3197
    } else if (taosGenerateTotpSecret(pAlterReq->totpseed, 0, newUser.totpsecret, sizeof(newUser.totpsecret)) < 0) {
×
3198
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_INVALID_OPTION_VALUE, &lino, _OVER);
×
3199
    }
3200
  }
3201

3202
  if (pAlterReq->hasEnable) {
18,520✔
3203
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "enable:%d,", pAlterReq->enable);
1,007✔
3204

3205
    newUser.enable = pAlterReq->enable; // lock or unlock user manually
1,007✔
3206
    if (newUser.enable) {
1,007✔
3207
      // reset login info to allow login immediately
3208
      userCacheResetLoginInfo(newUser.user);
526✔
3209
    }
3210
  }
3211

3212
  if (pAlterReq->hasSysinfo) {
18,520✔
3213
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sysinfo:%d,", pAlterReq->sysinfo);
5,383✔
3214
    newUser.sysInfo = pAlterReq->sysinfo;
5,383✔
3215
  }
3216

3217
  if (pAlterReq->hasCreatedb) {
18,520✔
3218
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "createdb:%d,", pAlterReq->createdb);
6,493✔
3219
    newUser.createdb = pAlterReq->createdb;
6,493✔
3220
  }
3221

3222
  if (pAlterReq->hasChangepass) {
18,520✔
3223
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "changepass:%d,", pAlterReq->changepass);
×
3224
    newUser.changePass = pAlterReq->changepass;
×
3225
  }
3226

3227
  if (pAlterReq->hasSessionPerUser) {
18,520✔
3228
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "sessionPerUser:%d,", pAlterReq->sessionPerUser);
×
3229
    newUser.sessionPerUser = pAlterReq->sessionPerUser;
×
3230
  }
3231

3232
  if (pAlterReq->hasConnectTime) {
18,520✔
3233
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectTime:%d,", pAlterReq->connectTime);
×
3234
    newUser.connectTime = pAlterReq->connectTime;
×
3235
  }
3236
  
3237
  if (pAlterReq->hasConnectIdleTime) {
18,520✔
3238
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "connectIdleTime:%d,", pAlterReq->connectIdleTime);
×
3239
    newUser.connectIdleTime = pAlterReq->connectIdleTime;
×
3240
  }
3241

3242
  if (pAlterReq->hasCallPerSession) {
18,520✔
3243
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "callPerSession:%d,", pAlterReq->callPerSession);
×
3244
    newUser.callPerSession = pAlterReq->callPerSession;
×
3245
  }
3246

3247
  if (pAlterReq->hasVnodePerCall) {
18,520✔
3248
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "vnodePerCall:%d,", pAlterReq->vnodePerCall);
×
3249
    newUser.vnodePerCall = pAlterReq->vnodePerCall;
×
3250
  }
3251

3252
  if (pAlterReq->hasFailedLoginAttempts) {
18,520✔
3253
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "failedLoginAttempts:%d,", pAlterReq->failedLoginAttempts);
×
3254
    newUser.failedLoginAttempts = pAlterReq->failedLoginAttempts;
×
3255
  }
3256

3257
  if (pAlterReq->hasPasswordLifeTime) {
18,520✔
3258
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLifeTime:%d,", pAlterReq->passwordLifeTime);
×
3259
    newUser.passwordLifeTime = pAlterReq->passwordLifeTime;
×
3260
  }
3261

3262
  if (pAlterReq->hasPasswordReuseTime) {
18,520✔
3263
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseTime:%d,", pAlterReq->passwordReuseTime);
×
3264
    newUser.passwordReuseTime = pAlterReq->passwordReuseTime;
×
3265
  }
3266

3267
  if (pAlterReq->hasPasswordReuseMax) {
18,520✔
3268
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordReuseMax:%d,", pAlterReq->passwordReuseMax);
×
3269
    newUser.passwordReuseMax = pAlterReq->passwordReuseMax;
×
3270
  }
3271

3272
  if (pAlterReq->hasPasswordLockTime) {
18,520✔
3273
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordLockTime:%d,", pAlterReq->passwordLockTime);
×
3274
    newUser.passwordLockTime = pAlterReq->passwordLockTime;
×
3275
  }
3276

3277
  if (pAlterReq->hasPasswordGraceTime) {
18,520✔
3278
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "passwordGraceTime:%d,", pAlterReq->passwordGraceTime);
×
3279
    newUser.passwordGraceTime = pAlterReq->passwordGraceTime;
×
3280
  }
3281

3282
  if (pAlterReq->hasInactiveAccountTime) {
18,520✔
3283
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "inactiveAccountTime:%d,", pAlterReq->inactiveAccountTime);
×
3284
    newUser.inactiveAccountTime = pAlterReq->inactiveAccountTime;
×
3285
  }
3286

3287
  if (pAlterReq->hasAllowTokenNum) {
18,520✔
3288
    auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "allowTokenNum:%d,", pAlterReq->allowTokenNum);
×
3289
    newUser.allowTokenNum = pAlterReq->allowTokenNum;
×
3290
  }
3291

3292
  if (pAlterReq->numDropIpRanges > 0 || pAlterReq->numIpRanges > 0) {
18,520✔
3293
    int32_t dummy = 0;
320✔
3294

3295
    // put previous ip whitelist into hash table
3296
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
320✔
3297
    if (m == NULL) {
320✔
3298
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3299
    }
3300

3301
    for (int32_t i = 0; i < newUser.pIpWhiteListDual->num; i++) {
1,120✔
3302
      SIpRange range;
800✔
3303
      copyIpRange(&range, newUser.pIpWhiteListDual->pIpRanges + i);
800✔
3304
      code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
800✔
3305
      if (code != 0) {
800✔
3306
        taosHashCleanup(m);
×
3307
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3308
      }
3309
    }
3310

3311
    if (pAlterReq->numDropIpRanges > 0) {
320✔
3312
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropIpRanges:%d,", pAlterReq->numDropIpRanges);
160✔
3313

3314
      for (int32_t i = 0; i < pAlterReq->numDropIpRanges; i++) {
320✔
3315
        if (taosHashGetSize(m) == 0) {
160✔
3316
          break;
×
3317
        }
3318

3319
        SIpRange range;
160✔
3320
        copyIpRange(&range, pAlterReq->pDropIpRanges + i);
160✔
3321

3322
        // for white list, drop default ip ranges is allowed, otherwise, we can never
3323
        // convert white list to black list.
3324

3325
        code = taosHashRemove(m, &range, sizeof(range));
160✔
3326
        if (code == TSDB_CODE_NOT_FOUND) {
160✔
3327
          // treat not exist as success
3328
          code = 0;
160✔
3329
        }
3330
        if (code != 0) {
160✔
3331
          taosHashCleanup(m);
×
3332
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3333
        }
3334
      }
3335
    }
3336

3337
    if (pAlterReq->numIpRanges > 0) {
320✔
3338
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addIpRanges:%d,", pAlterReq->numIpRanges);
160✔
3339
      for (int32_t i = 0; i < pAlterReq->numIpRanges; i++) {
320✔
3340
        SIpRange range;
160✔
3341
        copyIpRange(&range, pAlterReq->pIpRanges + i);
160✔
3342
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
160✔
3343
        if (code != 0) {
160✔
3344
          taosHashCleanup(m);
×
3345
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3346
        }
3347
      }
3348
    }
3349

3350
    int32_t numOfRanges = taosHashGetSize(m);
320✔
3351
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
320✔
3352
      taosHashCleanup(m);
×
3353
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
3354
    }
3355

3356
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
320✔
3357
    if (p == NULL) {
320✔
3358
      taosHashCleanup(m);
×
3359
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3360
    }
3361

3362
    void *pIter = taosHashIterate(m, NULL);
320✔
3363
    int32_t i = 0;
320✔
3364
    while (pIter) {
1,280✔
3365
      size_t len = 0;
960✔
3366
      SIpRange *key = taosHashGetKey(pIter, &len);
960✔
3367
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
960✔
3368
      pIter = taosHashIterate(m, pIter);
960✔
3369
      i++;
960✔
3370
    }
3371

3372
    taosHashCleanup(m);
320✔
3373
    p->num = numOfRanges;
320✔
3374
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
320✔
3375
    sortIpWhiteList(p);
320✔
3376
    newUser.pIpWhiteListDual = p;
320✔
3377

3378
    newUser.ipWhiteListVer++;
320✔
3379
  }
3380

3381

3382
  if (pAlterReq->numTimeRanges > 0 || pAlterReq->numDropTimeRanges) {
18,520✔
3383
    int32_t dummy = 0;
×
3384

3385
    // put previous ip whitelist into hash table
3386
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
3387
    if (m == NULL) {
×
3388
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3389
    }
3390

3391
    for (int32_t i = 0; i < newUser.pTimeWhiteList->num; i++) {
×
3392
      SDateTimeWhiteListItem *range = &newUser.pTimeWhiteList->ranges[i];
×
3393
      if (isDateTimeWhiteListItemExpired(range)) {
×
3394
        continue;
×
3395
      }
3396
      code = taosHashPut(m, range, sizeof(*range), &dummy, sizeof(dummy));
×
3397
      if (code != 0) {
×
3398
        taosHashCleanup(m);
×
3399
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3400
      }
3401
    }
3402

3403
    if (pAlterReq->numDropTimeRanges > 0) {
×
3404
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "dropTimeRanges:%d,", pAlterReq->numDropTimeRanges);
×
3405
      for (int32_t i = 0; i < pAlterReq->numDropTimeRanges; i++) {
×
3406
        if (taosHashGetSize(m) == 0) {
×
3407
          break;
×
3408
        }
3409
        SDateTimeWhiteListItem range = { 0 };
×
3410
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pDropTimeRanges + i);
×
3411

3412
        code = taosHashRemove(m, &range, sizeof(range));
×
3413
        if (code == TSDB_CODE_NOT_FOUND) {
×
3414
          // treat not exist as success
3415
          code = 0;
×
3416
        }
3417
        if (code != 0) {
×
3418
          taosHashCleanup(m);
×
3419
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3420
        }
3421
      }
3422
    }
3423

3424
    if (pAlterReq->numTimeRanges > 0) {
×
3425
      auditLen += tsnprintf(auditLog + auditLen, sizeof(auditLog) - auditLen, "addTimeRanges:%d,", pAlterReq->numTimeRanges);
×
3426
      for (int32_t i = 0; i < pAlterReq->numTimeRanges; i++) {
×
3427
        SDateTimeWhiteListItem range = { 0 };
×
3428
        DateTimeRangeToWhiteListItem(&range, pAlterReq->pTimeRanges + i);
×
3429
        if (isDateTimeWhiteListItemExpired(&range)) {
×
3430
          continue;
×
3431
        }
3432
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
×
3433
        if (code != 0) {
×
3434
          taosHashCleanup(m);
×
3435
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3436
        }
3437
      }
3438
    }
3439

3440
    int32_t numOfRanges = taosHashGetSize(m);
×
3441
    if (numOfRanges > MND_MAX_USER_TIME_RANGE) {
×
3442
      taosHashCleanup(m);
×
3443
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
3444
    }
3445

3446
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
3447
    if (p == NULL) {
×
3448
      taosHashCleanup(m);
×
3449
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3450
    }
3451

3452
    void *pIter = taosHashIterate(m, NULL);
×
3453
    int32_t i = 0;
×
3454
    while (pIter) {
×
3455
      size_t len = 0;
×
3456
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
×
3457
      memcpy(&p->ranges[i], key, sizeof(SDateTimeWhiteListItem));
×
3458
      pIter = taosHashIterate(m, pIter);
×
3459
      i++;
×
3460
    }
3461

3462
    taosHashCleanup(m);
×
3463
    p->num = numOfRanges;
×
3464
    taosMemoryFreeClear(newUser.pTimeWhiteList);
×
3465
    sortTimeWhiteList(p);
×
3466
    newUser.pTimeWhiteList = p;
×
3467
    newUser.timeWhiteListVer++;
×
3468
  }
3469

3470
  TAOS_CHECK_GOTO(mndAlterUser(pMnode, &newUser, pReq), &lino, _OVER);
18,520✔
3471
  code = TSDB_CODE_ACTION_IN_PROGRESS;
18,520✔
3472

3473
  if (auditLen > 0) {
18,520✔
3474
    auditLog[--auditLen] = 0; // remove last ','
18,520✔
3475
  }
3476
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
18,520✔
3477
    int64_t tse = taosGetTimestampMs();
18,520✔
3478
    double  duration = (double)(tse - tss);
18,520✔
3479
    duration = duration / 1000;
18,520✔
3480
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", pAlterReq->user, auditLog, auditLen, duration, 0);
18,520✔
3481
  }
3482

3483
_OVER:
21,475✔
3484
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
21,475✔
3485
    mError("user:%s, failed to alter at line %d since %s", pAlterReq->user, lino, tstrerror(code));
2,955✔
3486
  }
3487

3488
  mndReleaseUser(pMnode, pUser);
21,475✔
3489
  mndUserFreeObj(&newUser);
21,475✔
3490
  return code;
21,475✔
3491
}
3492

3493

3494

3495
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
776,175✔
3496
  SAlterUserReq alterReq = {0};
776,175✔
3497

3498
  int32_t code = tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq);
776,175✔
3499
  if (code != 0) {
776,175✔
3500
    mError("failed to deserialize alter user request at line %d since %s", __LINE__, tstrerror(code));
×
3501
    TAOS_RETURN(code);
×
3502
  }
3503

3504
  if (alterReq.user[0] == 0) {
776,175✔
3505
    tFreeSAlterUserReq(&alterReq);
×
3506
    mError("failed to alter user at line %d since invalid user format", __LINE__);
×
3507
    TAOS_RETURN(TSDB_CODE_MND_INVALID_USER_FORMAT);
×
3508
  }
3509

3510
  mInfo("user:%s, start to alter", alterReq.user);
776,175✔
3511
  if (alterReq.alterType == TSDB_ALTER_USER_BASIC_INFO) {
776,175✔
3512
    code = mndProcessAlterUserBasicInfoReq(pReq, &alterReq);
21,475✔
3513
  } else {
3514
    code = mndProcessAlterUserPrivilegesReq(pReq, &alterReq);
754,700✔
3515
  }
3516

3517
  tFreeSAlterUserReq(&alterReq);
776,175✔
3518
  TAOS_RETURN(code);
776,175✔
3519
}
3520

3521
int32_t mndGetAuditUser(SMnode *pMnode, char* user){
40,150,358✔
3522
  (void)tsnprintf(user, TSDB_USER_LEN, "audit");
40,150,358✔
3523
  return 0;
40,150,358✔
3524
}
3525

3526
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
29,911✔
3527
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
29,911✔
3528
  if (pTrans == NULL) {
29,911✔
3529
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
3530
    TAOS_RETURN(terrno);
×
3531
  }
3532
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
29,911✔
3533

3534
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
29,911✔
3535
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
29,911✔
3536
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
3537
    mndTransDrop(pTrans);
×
3538
    TAOS_RETURN(terrno);
×
3539
  }
3540
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
29,911✔
3541
    mndTransDrop(pTrans);
×
3542
    TAOS_RETURN(terrno);
×
3543
  }
3544

3545
  if (mndDropTokensByUser(pMnode, pTrans, pUser->user) != 0) {
29,911✔
3546
    mndTransDrop(pTrans);
×
3547
    TAOS_RETURN(terrno);
×
3548
  }
3549

3550
  if (mndTransPrepare(pMnode, pTrans) != 0) {
29,911✔
3551
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3552
    mndTransDrop(pTrans);
×
3553
    TAOS_RETURN(terrno);
×
3554
  }
3555

3556
  userCacheRemoveUser(pUser->user);
29,911✔
3557
  mndDropCachedTokensByUser(pUser->user);
29,911✔
3558

3559
  mndTransDrop(pTrans);
29,911✔
3560
  TAOS_RETURN(0);
29,911✔
3561
}
3562

3563
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
30,070✔
3564
  SMnode      *pMnode = pReq->info.node;
30,070✔
3565
  int32_t      code = 0;
30,070✔
3566
  int32_t      lino = 0;
30,070✔
3567
  SUserObj    *pUser = NULL;
30,070✔
3568
  SDropUserReq dropReq = {0};
30,070✔
3569
  int64_t      tss = taosGetTimestampMs();
30,070✔
3570

3571
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
30,070✔
3572

3573
  mInfo("user:%s, start to drop", dropReq.user);
30,070✔
3574
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_DROP_USER), &lino, _OVER);
30,070✔
3575

3576
  if (dropReq.user[0] == 0) {
30,070✔
3577
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3578
  }
3579

3580
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
30,070✔
3581

3582
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
29,911✔
3583
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
29,911✔
3584

3585
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
29,911✔
3586
    int64_t tse = taosGetTimestampMs();
29,911✔
3587
    double  duration = (double)(tse - tss);
29,911✔
3588
    duration = duration / 1000;
29,911✔
3589
    auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen, duration, 0);
29,911✔
3590
  }
3591

3592
_OVER:
30,070✔
3593
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
30,070✔
3594
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
159✔
3595
  }
3596

3597
  mndReleaseUser(pMnode, pUser);
30,070✔
3598
  tFreeSDropUserReq(&dropReq);
30,070✔
3599
  TAOS_RETURN(code);
30,070✔
3600
}
3601

3602
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
3,123,417✔
3603
  SMnode         *pMnode = pReq->info.node;
3,123,417✔
3604
  int32_t         code = 0;
3,123,417✔
3605
  int32_t         lino = 0;
3,123,417✔
3606
  int32_t         contLen = 0;
3,123,417✔
3607
  void           *pRsp = NULL;
3,123,417✔
3608
  SUserObj       *pUser = NULL;
3,123,417✔
3609
  SGetUserAuthReq authReq = {0};
3,123,417✔
3610
  SGetUserAuthRsp authRsp = {0};
3,123,417✔
3611

3612
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
3,123,369✔
3613
  mTrace("user:%s, start to get auth", authReq.user);
3,123,417✔
3614

3615
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
3,123,417✔
3616

3617
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,122,355✔
3618

3619
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
3,122,355✔
3620
  if (contLen < 0) {
3,122,307✔
3621
    TAOS_CHECK_EXIT(contLen);
×
3622
  }
3623
  pRsp = rpcMallocCont(contLen);
3,122,307✔
3624
  if (pRsp == NULL) {
3,122,355✔
3625
    TAOS_CHECK_EXIT(terrno);
×
3626
  }
3627

3628
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
3,122,355✔
3629
  if (contLen < 0) {
3,122,355✔
3630
    TAOS_CHECK_EXIT(contLen);
×
3631
  }
3632

3633
_exit:
3,123,417✔
3634
  mndReleaseUser(pMnode, pUser);
3,123,417✔
3635
  tFreeSGetUserAuthRsp(&authRsp);
3,123,417✔
3636
  if (code < 0) {
3,123,369✔
3637
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
1,062✔
3638
    rpcFreeCont(pRsp);
1,062✔
3639
    pRsp = NULL;
1,062✔
3640
    contLen = 0;
1,062✔
3641
  }
3642
  pReq->info.rsp = pRsp;
3,123,369✔
3643
  pReq->info.rspLen = contLen;
3,123,417✔
3644
  pReq->code = code;
3,123,369✔
3645

3646
  TAOS_RETURN(code);
3,123,417✔
3647
}
3648

3649

3650
bool mndIsTotpEnabledUser(SUserObj *pUser) {
2,119,801✔
3651
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
69,912,814✔
3652
    if (pUser->totpsecret[i] != 0) {
67,795,361✔
3653
      return true;
×
3654
    }
3655
  }
3656
  return false;
2,117,453✔
3657
}
3658

3659

3660
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
8,953✔
3661
  SMnode   *pMnode = pReq->info.node;
8,953✔
3662
  SSdb     *pSdb = pMnode->pSdb;
8,953✔
3663
  int32_t   code = 0;
8,953✔
3664
  int32_t   lino = 0;
8,953✔
3665
  int32_t   numOfRows = 0;
8,953✔
3666
  SUserObj *pUser = NULL;
8,953✔
3667
  int32_t   cols = 0;
8,953✔
3668
  int8_t    flag = 0;
8,953✔
3669
  char     *pWrite = NULL;
8,953✔
3670
  char     *buf = NULL;
8,953✔
3671
  char     *varstr = NULL;
8,953✔
3672

3673
  while (numOfRows < rows) {
32,983✔
3674
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
32,983✔
3675
    if (pShow->pIter == NULL) break;
32,983✔
3676

3677
    cols = 0;
24,030✔
3678
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3679
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
24,030✔
3680
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
24,030✔
3681
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
24,030✔
3682

3683
    cols++;
24,030✔
3684
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3685
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
24,030✔
3686

3687
    cols++;
24,030✔
3688
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3689
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
24,030✔
3690

3691
    cols++;
24,030✔
3692
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3693
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
24,030✔
3694

3695
    cols++;
24,030✔
3696
    flag = pUser->createdb ? 1 : 0;
24,030✔
3697
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3698
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
24,030✔
3699

3700
    cols++;
24,030✔
3701
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3702
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
24,030✔
3703

3704
    cols++;
24,030✔
3705
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3706
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
24,030✔
3707
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
24,030✔
3708

3709
    cols++;
24,030✔
3710

3711
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
24,030✔
3712
    if (tlen != 0) {
24,030✔
3713
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
24,030✔
3714
      if (varstr == NULL) {
24,030✔
3715
        sdbRelease(pSdb, pUser);
×
3716
        sdbCancelFetch(pSdb, pShow->pIter);
×
3717
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3718
      }
3719
      varDataSetLen(varstr, tlen);
24,030✔
3720
      (void)memcpy(varDataVal(varstr), buf, tlen);
24,030✔
3721

3722
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3723
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
24,030✔
3724

3725
      taosMemoryFreeClear(buf);
24,030✔
3726
    } else {
3727
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3728
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3729
    }
3730

3731
    cols++;
24,030✔
3732
    tlen = convertTimeRangesToStr(pUser, &buf);
24,030✔
3733
    if (tlen != 0) {
24,030✔
3734
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
24,030✔
3735
      if (varstr == NULL) {
24,030✔
3736
        sdbRelease(pSdb, pUser);
×
3737
        sdbCancelFetch(pSdb, pShow->pIter);
×
3738
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3739
      }
3740
      varDataSetLen(varstr, tlen);
24,030✔
3741
      (void)memcpy(varDataVal(varstr), buf, tlen);
24,030✔
3742

3743
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
24,030✔
3744
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
24,030✔
3745

3746
      taosMemoryFreeClear(buf);
24,030✔
3747
    } else {
3748
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3749
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3750
    }
3751

3752
    numOfRows++;
24,030✔
3753
    sdbRelease(pSdb, pUser);
24,030✔
3754
  }
3755

3756
  pShow->numOfRows += numOfRows;
8,953✔
3757
_exit:
8,953✔
3758
  taosMemoryFreeClear(buf);
8,953✔
3759
  taosMemoryFreeClear(varstr);
8,953✔
3760
  if (code < 0) {
8,953✔
3761
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3762
    TAOS_RETURN(code);
×
3763
  }
3764
  return numOfRows;
8,953✔
3765
}
3766

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

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

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

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

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

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

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

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

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

3817
    cols++;
×
3818
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3819
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->changePass, false, pUser, pShow->pIter, _exit);
×
3820

3821
    cols++;
×
3822
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3823
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
×
3824
    STR_WITH_MAXSIZE_TO_VARSTR(pass, pUser->passwords[0].pass, pShow->pMeta->pSchemas[cols].bytes);
×
3825
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, pShow->pIter, _exit);
×
3826

3827
    cols++;
×
3828
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3829
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sessionPerUser, false, pUser, pShow->pIter, _exit);
×
3830

3831
    cols++;
×
3832
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3833
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectTime, false, pUser, pShow->pIter, _exit);
×
3834

3835
    cols++;
×
3836
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3837
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->connectIdleTime, false, pUser, pShow->pIter, _exit);
×
3838

3839
    cols++;
×
3840
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3841
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->callPerSession, false, pUser, pShow->pIter, _exit);
×
3842

3843
    /* not supported yet
3844
    cols++;
3845
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
3846
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerSession, false, pUser, pShow->pIter, _exit);
3847
*/
3848

3849
    cols++;
×
3850
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3851
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
×
3852

3853
    cols++;
×
3854
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3855
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLifeTime, false, pUser, pShow->pIter, _exit);
×
3856

3857
    cols++;
×
3858
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3859
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseTime, false, pUser, pShow->pIter, _exit);
×
3860

3861
    cols++;
×
3862
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3863
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseMax, false, pUser, pShow->pIter, _exit);
×
3864

3865
    cols++;
×
3866
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3867
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordLockTime, false, pUser, pShow->pIter, _exit);
×
3868

3869
    cols++;
×
3870
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3871
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordGraceTime, false, pUser, pShow->pIter, _exit);
×
3872

3873
    cols++;
×
3874
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3875
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->inactiveAccountTime, false, pUser, pShow->pIter, _exit);
×
3876

3877
    cols++;
×
3878
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3879
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->allowTokenNum, false, pUser, pShow->pIter, _exit);
×
3880

3881
    cols++;
×
3882
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
×
3883
    if (tlen != 0) {
×
3884
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
3885
      if (varstr == NULL) {
×
3886
        sdbRelease(pSdb, pUser);
×
3887
        sdbCancelFetch(pSdb, pShow->pIter);
×
3888
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3889
      }
3890
      varDataSetLen(varstr, tlen);
×
3891
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
3892

3893
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3894
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
3895

3896
      taosMemoryFreeClear(buf);
×
3897
    } else {
3898
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3899
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3900
    }
3901

3902
    cols++;
×
3903
    tlen = convertTimeRangesToStr(pUser, &buf);
×
3904
    if (tlen != 0) {
×
3905
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
3906
      if (varstr == NULL) {
×
3907
        sdbRelease(pSdb, pUser);
×
3908
        sdbCancelFetch(pSdb, pShow->pIter);
×
3909
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3910
      }
3911
      varDataSetLen(varstr, tlen);
×
3912
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
3913

3914
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3915
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
3916

3917
      taosMemoryFreeClear(buf);
×
3918
    } else {
3919
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3920
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3921
    }
3922

3923
    numOfRows++;
×
3924
    sdbRelease(pSdb, pUser);
×
3925
  }
3926

3927
  pShow->numOfRows += numOfRows;
×
3928
_exit:
×
3929
  taosMemoryFreeClear(buf);
×
3930
  taosMemoryFreeClear(varstr);
×
3931
  if (code < 0) {
×
3932
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3933
    TAOS_RETURN(code);
×
3934
  }
3935
#endif
3936
  return numOfRows;
×
3937
}
3938

3939
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
3940
  SSdb *pSdb = pMnode->pSdb;
×
3941
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
3942
}
×
3943

3944
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
316,584✔
3945
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
3946
  char   *value = taosHashIterate(hash, NULL);
316,584✔
3947
  char   *user = pUser->user;
316,584✔
3948
  int32_t code = 0;
316,584✔
3949
  int32_t lino = 0;
316,584✔
3950
  int32_t cols = 0;
316,584✔
3951
  int32_t numOfRows = *pNumOfRows;
316,584✔
3952

3953
  while (value != NULL) {
354,709✔
3954
    cols = 0;
38,125✔
3955
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
38,125✔
3956
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
38,125✔
3957
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,125✔
3958
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
38,125✔
3959

3960
    char privilege[20] = {0};
38,125✔
3961
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
38,125✔
3962
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,125✔
3963
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
38,125✔
3964

3965
    size_t keyLen = 0;
38,125✔
3966
    void  *key = taosHashGetKey(value, &keyLen);
38,125✔
3967

3968
    char dbName[TSDB_DB_NAME_LEN] = {0};
38,125✔
3969
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
38,125✔
3970
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
38,125✔
3971
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
38,125✔
3972
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,125✔
3973
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
38,125✔
3974

3975
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
38,125✔
3976
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
38,125✔
3977
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
38,125✔
3978
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
38,125✔
3979
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
38,125✔
3980
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
38,125✔
3981

3982
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
63,253✔
3983
      SNode  *pAst = NULL;
25,128✔
3984
      int32_t sqlLen = 0;
25,128✔
3985
      size_t  bufSz = strlen(value) + 1;
25,128✔
3986
      if (bufSz < 6) bufSz = 6;
25,128✔
3987
      TAOS_MEMORY_REALLOC(*sql, bufSz);
25,128✔
3988
      if (*sql == NULL) {
25,128✔
3989
        code = terrno;
×
3990
        goto _exit;
×
3991
      }
3992
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
25,128✔
3993
      if ((*condition) == NULL) {
25,128✔
3994
        code = terrno;
×
3995
        goto _exit;
×
3996
      }
3997

3998
      if (nodesStringToNode(value, &pAst) == 0) {
25,128✔
3999
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
25,128✔
4000
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
4001
        }
4002
        nodesDestroyNode(pAst);
25,128✔
4003
      }
4004

4005
      if (sqlLen == 0) {
25,128✔
4006
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
4007
      }
4008

4009
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
25,128✔
4010

4011
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,128✔
4012
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
25,128✔
4013

4014
      char notes[2] = {0};
25,128✔
4015
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
25,128✔
4016
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,128✔
4017
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
25,128✔
4018
    } else {
4019
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
12,997✔
4020
      if ((*condition) == NULL) {
12,997✔
4021
        code = terrno;
×
4022
        goto _exit;
×
4023
      }
4024
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
12,997✔
4025
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,997✔
4026
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
12,997✔
4027

4028
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
12,997✔
4029
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
12,997✔
4030
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,997✔
4031
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
12,997✔
4032
    }
4033

4034
    numOfRows++;
38,125✔
4035
    value = taosHashIterate(hash, value);
38,125✔
4036
  }
4037
  *pNumOfRows = numOfRows;
316,584✔
4038
_exit:
316,584✔
4039
  if (code < 0) {
316,584✔
4040
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4041
    sdbRelease(pSdb, pUser);
×
4042
    sdbCancelFetch(pSdb, pShow->pIter);
×
4043
  }
4044
  TAOS_RETURN(code);
316,584✔
4045
}
4046

4047
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
11,285✔
4048
  int32_t   code = 0;
11,285✔
4049
  int32_t   lino = 0;
11,285✔
4050
  SMnode   *pMnode = pReq->info.node;
11,285✔
4051
  SSdb     *pSdb = pMnode->pSdb;
11,285✔
4052
  int32_t   numOfRows = 0;
11,285✔
4053
  SUserObj *pUser = NULL;
11,285✔
4054
  int32_t   cols = 0;
11,285✔
4055
  char     *pWrite = NULL;
11,285✔
4056
  char     *condition = NULL;
11,285✔
4057
  char     *sql = NULL;
11,285✔
4058

4059
  bool fetchNextUser = pShow->restore ? false : true;
11,285✔
4060
  pShow->restore = false;
11,285✔
4061

4062
  while (numOfRows < rows) {
64,049✔
4063
    if (fetchNextUser) {
64,049✔
4064
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
64,049✔
4065
      if (pShow->pIter == NULL) break;
64,049✔
4066
    } else {
4067
      fetchNextUser = true;
×
4068
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
4069
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
4070
      if (!pUser) {
×
4071
        continue;
×
4072
      }
4073
    }
4074

4075
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
52,764✔
4076
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
52,764✔
4077
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
52,764✔
4078
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
52,764✔
4079
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
52,764✔
4080
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
52,764✔
4081
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
52,764✔
4082
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
52,764✔
4083
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
52,764✔
4084
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
52,764✔
4085
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
52,764✔
4086
        rows) {
4087
      mInfo(
×
4088
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
4089
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
4090
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
4091
          numOfReadViews, numOfWriteViews, numOfAlterViews);
4092
      pShow->restore = true;
×
4093
      sdbRelease(pSdb, pUser);
×
4094
      break;
×
4095
    }
4096

4097
    if (pUser->superUser) {
52,764✔
4098
      cols = 0;
11,285✔
4099
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
11,285✔
4100
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
11,285✔
4101
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4102
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
11,285✔
4103

4104
      char privilege[20] = {0};
11,285✔
4105
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
11,285✔
4106
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4107
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
11,285✔
4108

4109
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
11,285✔
4110
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
11,285✔
4111
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4112
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
11,285✔
4113

4114
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
11,285✔
4115
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
11,285✔
4116
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4117
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
11,285✔
4118

4119
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
11,285✔
4120
      if (condition == NULL) {
11,285✔
4121
        sdbRelease(pSdb, pUser);
×
4122
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4123
      }
4124
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
11,285✔
4125
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4126
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
11,285✔
4127

4128
      char notes[2] = {0};
11,285✔
4129
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
11,285✔
4130
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,285✔
4131
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
11,285✔
4132

4133
      numOfRows++;
11,285✔
4134
    }
4135

4136
    char *db = taosHashIterate(pUser->readDbs, NULL);
52,764✔
4137
    while (db != NULL) {
62,548✔
4138
      cols = 0;
9,784✔
4139
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
9,784✔
4140
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
9,784✔
4141
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4142
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
9,784✔
4143

4144
      char privilege[20] = {0};
9,784✔
4145
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
9,784✔
4146
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4147
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
9,784✔
4148

4149
      SName name = {0};
9,784✔
4150
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
9,784✔
4151
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
9,784✔
4152
      if (code < 0) {
9,784✔
4153
        sdbRelease(pSdb, pUser);
×
4154
        sdbCancelFetch(pSdb, pShow->pIter);
×
4155
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4156
      }
4157
      (void)tNameGetDbName(&name, varDataVal(objName));
9,784✔
4158
      varDataSetLen(objName, strlen(varDataVal(objName)));
9,784✔
4159
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4160
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
9,784✔
4161

4162
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
9,784✔
4163
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
9,784✔
4164
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4165
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
9,784✔
4166

4167
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
9,784✔
4168
      if (condition == NULL) {
9,784✔
4169
        sdbRelease(pSdb, pUser);
×
4170
        sdbCancelFetch(pSdb, pShow->pIter);
×
4171
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4172
      }
4173
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
9,784✔
4174
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4175
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
9,784✔
4176

4177
      char notes[2] = {0};
9,784✔
4178
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
9,784✔
4179
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
9,784✔
4180
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
9,784✔
4181

4182
      numOfRows++;
9,784✔
4183
      db = taosHashIterate(pUser->readDbs, db);
9,784✔
4184
    }
4185

4186
    db = taosHashIterate(pUser->writeDbs, NULL);
52,764✔
4187
    while (db != NULL) {
61,464✔
4188
      cols = 0;
8,700✔
4189
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
8,700✔
4190
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
8,700✔
4191
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4192
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
8,700✔
4193

4194
      char privilege[20] = {0};
8,700✔
4195
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
8,700✔
4196
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4197
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
8,700✔
4198

4199
      SName name = {0};
8,700✔
4200
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
8,700✔
4201
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
8,700✔
4202
      if (code < 0) {
8,700✔
4203
        sdbRelease(pSdb, pUser);
×
4204
        sdbCancelFetch(pSdb, pShow->pIter);
×
4205
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4206
      }
4207
      (void)tNameGetDbName(&name, varDataVal(objName));
8,700✔
4208
      varDataSetLen(objName, strlen(varDataVal(objName)));
8,700✔
4209
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4210
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
8,700✔
4211

4212
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
8,700✔
4213
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
8,700✔
4214
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4215
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
8,700✔
4216

4217
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
8,700✔
4218
      if (condition == NULL) {
8,700✔
4219
        sdbRelease(pSdb, pUser);
×
4220
        sdbCancelFetch(pSdb, pShow->pIter);
×
4221
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4222
      }
4223
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
8,700✔
4224
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4225
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
8,700✔
4226

4227
      char notes[2] = {0};
8,700✔
4228
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
8,700✔
4229
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8,700✔
4230
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
8,700✔
4231

4232
      numOfRows++;
8,700✔
4233
      db = taosHashIterate(pUser->writeDbs, db);
8,700✔
4234
    }
4235

4236
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4237

4238
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4239

4240
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4241

4242
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4243

4244
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4245

4246
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
52,764✔
4247

4248
    char *topic = taosHashIterate(pUser->topics, NULL);
52,764✔
4249
    while (topic != NULL) {
54,366✔
4250
      cols = 0;
1,602✔
4251
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
1,602✔
4252
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1,602✔
4253
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4254
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
1,602✔
4255

4256
      char privilege[20] = {0};
1,602✔
4257
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
1,602✔
4258
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4259
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
1,602✔
4260

4261
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
1,602✔
4262
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
1,602✔
4263
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
1,602✔
4264
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4265
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, pShow->pIter, _exit);
1,602✔
4266

4267
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,602✔
4268
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
1,602✔
4269
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4270
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
1,602✔
4271

4272
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
1,602✔
4273
      if (condition == NULL) {
1,602✔
4274
        sdbRelease(pSdb, pUser);
×
4275
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4276
      }
4277
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
1,602✔
4278
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4279
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
1,602✔
4280

4281
      char notes[2] = {0};
1,602✔
4282
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
1,602✔
4283
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,602✔
4284
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
1,602✔
4285

4286
      numOfRows++;
1,602✔
4287
      topic = taosHashIterate(pUser->topics, topic);
1,602✔
4288
    }
4289

4290
    sdbRelease(pSdb, pUser);
52,764✔
4291
  }
4292

4293
  pShow->numOfRows += numOfRows;
11,285✔
4294
_exit:
11,285✔
4295
  taosMemoryFreeClear(condition);
11,285✔
4296
  taosMemoryFreeClear(sql);
11,285✔
4297
  if (code < 0) {
11,285✔
4298
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4299
    TAOS_RETURN(code);
×
4300
  }
4301
  return numOfRows;
11,285✔
4302
}
4303

4304
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
4305
  SSdb *pSdb = pMnode->pSdb;
×
4306
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4307
}
×
4308

4309
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
18,202,787✔
4310
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
4311
  int32_t           code = 0;
18,202,787✔
4312
  int32_t           lino = 0;
18,202,787✔
4313
  int32_t           rspLen = 0;
18,202,787✔
4314
  void             *pRsp = NULL;
18,202,787✔
4315
  SUserAuthBatchRsp batchRsp = {0};
18,202,787✔
4316

4317
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
18,202,787✔
4318
  if (batchRsp.pArray == NULL) {
18,202,787✔
4319
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4320
  }
4321

4322
  for (int32_t i = 0; i < numOfUses; ++i) {
36,595,953✔
4323
    SUserObj *pUser = NULL;
18,392,603✔
4324
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
18,392,603✔
4325
    if (pUser == NULL) {
18,392,603✔
4326
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
4,061✔
4327
        SGetUserAuthRsp rsp = {.dropped = 1};
4,061✔
4328
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
4,061✔
4329
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
8,122✔
4330
      }
4331
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
4,061✔
4332
      code = 0;
4,061✔
4333
      continue;
6,014✔
4334
    }
4335

4336
    pUsers[i].version = ntohl(pUsers[i].version);
18,388,542✔
4337
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
18,388,542✔
4338
      mndReleaseUser(pMnode, pUser);
17,727,119✔
4339
      continue;
17,727,119✔
4340
    }
4341

4342
    SGetUserAuthRsp rsp = {0};
661,423✔
4343
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
661,986✔
4344
    if (code) {
661,986✔
4345
      mndReleaseUser(pMnode, pUser);
×
4346
      tFreeSGetUserAuthRsp(&rsp);
×
4347
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4348
    }
4349

4350
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
1,323,972✔
4351
      code = terrno;
×
4352
      mndReleaseUser(pMnode, pUser);
×
4353
      tFreeSGetUserAuthRsp(&rsp);
×
4354
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4355
    }
4356
    mndReleaseUser(pMnode, pUser);
661,986✔
4357
  }
4358

4359
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
18,203,350✔
4360
    *ppRsp = NULL;
17,568,839✔
4361
    *pRspLen = 0;
17,568,839✔
4362

4363
    tFreeSUserAuthBatchRsp(&batchRsp);
17,568,839✔
4364
    return 0;
17,568,839✔
4365
  }
4366

4367
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
634,511✔
4368
  if (rspLen < 0) {
634,511✔
4369
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4370
  }
4371
  pRsp = taosMemoryMalloc(rspLen);
634,511✔
4372
  if (pRsp == NULL) {
634,511✔
4373
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4374
  }
4375
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
634,511✔
4376
  if (rspLen < 0) {
634,511✔
4377
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4378
  }
4379
_OVER:
634,511✔
4380
  tFreeSUserAuthBatchRsp(&batchRsp);
634,511✔
4381
  if (code < 0) {
634,511✔
4382
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4383
    taosMemoryFreeClear(pRsp);
×
4384
    rspLen = 0;
×
4385
  }
4386
  *ppRsp = pRsp;
634,511✔
4387
  *pRspLen = rspLen;
634,511✔
4388

4389
  TAOS_RETURN(code);
634,511✔
4390
}
4391

4392
static int32_t mndRemoveDbPrivileges(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, int32_t *nRemoved) {
89,586✔
4393
  void *pVal = NULL;
89,586✔
4394
  while ((pVal = taosHashIterate(pHash, pVal))) {
124,828✔
4395
    size_t keyLen = 0;
35,242✔
4396
    char  *pKey = (char *)taosHashGetKey(pVal, &keyLen);
35,242✔
4397
    if (pKey == NULL || keyLen <= dbFNameLen) continue;
35,242✔
4398
    if ((*(pKey + dbFNameLen) == '.') && strncmp(pKey, dbFName, dbFNameLen) == 0) {
35,242✔
4399
      TAOS_CHECK_RETURN(taosHashRemove(pHash, pKey, keyLen));
4,933✔
4400
      if (nRemoved) ++(*nRemoved);
4,933✔
4401
    }
4402
  }
4403
  TAOS_RETURN(0);
89,586✔
4404
}
4405

4406
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers) {
610,927✔
4407
  int32_t    code = 0, lino = 0;
610,927✔
4408
  SSdb      *pSdb = pMnode->pSdb;
610,927✔
4409
  int32_t    dbLen = strlen(pDb->name);
610,927✔
4410
  void      *pIter = NULL;
610,927✔
4411
  SUserObj  *pUser = NULL;
610,927✔
4412
  SUserObj   newUser = {0};
610,927✔
4413
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
610,927✔
4414
  bool       output = (ppUsers != NULL);
610,927✔
4415

4416
  while (1) {
674,217✔
4417
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,285,144✔
4418
    if (pIter == NULL) break;
1,285,144✔
4419

4420
    bool update = false;
674,217✔
4421
    bool inReadDb = (taosHashGet(pUser->readDbs, pDb->name, dbLen + 1) != NULL);
674,217✔
4422
    bool inWriteDb = (taosHashGet(pUser->writeDbs, pDb->name, dbLen + 1) != NULL);
674,217✔
4423
    bool inUseDb = (taosHashGet(pUser->useDbs, pDb->name, dbLen + 1) != NULL);
674,217✔
4424
    bool inReadTbs = taosHashGetSize(pUser->readTbs) > 0;
674,217✔
4425
    bool inWriteTbs = taosHashGetSize(pUser->writeTbs) > 0;
674,217✔
4426
    bool inAlterTbs = taosHashGetSize(pUser->alterTbs) > 0;
674,217✔
4427
    bool inReadViews = taosHashGetSize(pUser->readViews) > 0;
674,217✔
4428
    bool inWriteViews = taosHashGetSize(pUser->writeViews) > 0;
674,217✔
4429
    bool inAlterViews = taosHashGetSize(pUser->alterViews) > 0;
674,217✔
4430
    // no need remove pUser->topics since topics must be dropped ahead of db
4431
    if (!inReadDb && !inWriteDb && !inReadTbs && !inWriteTbs && !inAlterTbs && !inReadViews && !inWriteViews &&
674,217✔
4432
        !inAlterViews) {
644,112✔
4433
      sdbRelease(pSdb, pUser);
644,112✔
4434
      continue;
644,112✔
4435
    }
4436
    SUserObj *pTargetUser = &newUser;
30,105✔
4437
    if (output) {
30,105✔
4438
      if (!pUsers) {
514✔
4439
        TSDB_CHECK_NULL(pUsers = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
257✔
4440
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
4441
        *ppUsers = pUsers;
257✔
4442
      }
4443
      void   *pVal = NULL;
514✔
4444
      int32_t userLen = strlen(pUser->user) + 1;
514✔
4445
      if ((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)) != NULL) {
514✔
4446
        pTargetUser = (SUserObj *)pVal;
257✔
4447
      } else {
4448
        TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
257✔
4449
        TAOS_CHECK_EXIT(tSimpleHashPut(pUsers, pUser->user, userLen, &newUser, sizeof(SUserObj)));
257✔
4450
        TSDB_CHECK_NULL((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)), code, lino, _exit,
257✔
4451
                        TSDB_CODE_OUT_OF_MEMORY);
4452
        pTargetUser = (SUserObj *)pVal;
257✔
4453
      }
4454
    } else {
4455
      TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
29,591✔
4456
    }
4457
    if (inReadDb) {
30,105✔
4458
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->readDbs, pDb->name, dbLen + 1));
1,081✔
4459
    }
4460
    if (inWriteDb) {
30,105✔
4461
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->writeDbs, pDb->name, dbLen + 1));
919✔
4462
    }
4463
    if (inUseDb) {
30,105✔
4464
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->useDbs, pDb->name, dbLen + 1));
676✔
4465
    }
4466
    update = inReadDb || inWriteDb || inUseDb;
30,105✔
4467

4468
    int32_t nRemovedReadTbs = 0;
30,105✔
4469
    int32_t nRemovedWriteTbs = 0;
30,105✔
4470
    int32_t nRemovedAlterTbs = 0;
30,105✔
4471
    if (inReadTbs || inWriteTbs || inAlterTbs) {
30,105✔
4472
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readTbs, pDb->name, dbLen, &nRemovedReadTbs));
29,700✔
4473
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeTbs, pDb->name, dbLen, &nRemovedWriteTbs));
29,700✔
4474
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterTbs, pDb->name, dbLen, &nRemovedAlterTbs));
29,700✔
4475
      if (!update) update = nRemovedReadTbs > 0 || nRemovedWriteTbs > 0 || nRemovedAlterTbs > 0;
29,700✔
4476
    }
4477

4478
    int32_t nRemovedReadViews = 0;
30,105✔
4479
    int32_t nRemovedWriteViews = 0;
30,105✔
4480
    int32_t nRemovedAlterViews = 0;
30,105✔
4481
    if (inReadViews || inWriteViews || inAlterViews) {
30,105✔
4482
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readViews, pDb->name, dbLen, &nRemovedReadViews));
162✔
4483
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeViews, pDb->name, dbLen, &nRemovedWriteViews));
162✔
4484
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterViews, pDb->name, dbLen, &nRemovedAlterViews));
162✔
4485
      if (!update) update = nRemovedReadViews > 0 || nRemovedWriteViews > 0 || nRemovedAlterViews > 0;
162✔
4486
    }
4487

4488
    if (!output) {
30,105✔
4489
      if (update) {
29,591✔
4490
        SSdbRaw *pCommitRaw = mndUserActionEncode(pTargetUser);
567✔
4491
        if (pCommitRaw == NULL) {
567✔
4492
          TAOS_CHECK_EXIT(terrno);
×
4493
        }
4494
        TAOS_CHECK_EXIT(mndTransAppendCommitlog(pTrans, pCommitRaw));
567✔
4495
        TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
567✔
4496
      }
4497
      mndUserFreeObj(&newUser);
29,591✔
4498
    }
4499
    sdbRelease(pSdb, pUser);
30,105✔
4500
  }
4501

4502
_exit:
610,927✔
4503
  if (code < 0) {
610,927✔
4504
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4505
    mndUserFreeObj(&newUser);
×
4506
  }
4507
  if (pUser != NULL) sdbRelease(pSdb, pUser);
610,927✔
4508
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
610,927✔
4509
  if (!output) mndUserFreeObj(&newUser);
610,927✔
4510
  TAOS_RETURN(code);
610,927✔
4511
}
4512

4513
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
437,002✔
4514
  int32_t   code = 0;
437,002✔
4515
  SSdb     *pSdb = pMnode->pSdb;
437,002✔
4516
  int32_t   len = strlen(stb) + 1;
437,002✔
4517
  void     *pIter = NULL;
437,002✔
4518
  SUserObj *pUser = NULL;
437,002✔
4519
  SUserObj  newUser = {0};
437,002✔
4520

4521
  while (1) {
437,002✔
4522
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
874,004✔
4523
    if (pIter == NULL) break;
874,004✔
4524

4525
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
437,002✔
4526
      break;
×
4527
    }
4528

4529
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
437,002✔
4530
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
437,002✔
4531
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
437,002✔
4532
    if (inRead || inWrite || inAlter) {
437,002✔
4533
      code = taosHashRemove(newUser.readTbs, stb, len);
×
4534
      if (code < 0) {
×
4535
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
4536
      }
4537
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
4538
      if (code < 0) {
×
4539
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
4540
      }
4541
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
4542
      if (code < 0) {
×
4543
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
4544
      }
4545

4546
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
4547
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
4548
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4549
        break;
×
4550
      }
4551
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
4552
      if (code != 0) {
×
4553
        mndUserFreeObj(&newUser);
×
4554
        sdbRelease(pSdb, pUser);
×
4555
        TAOS_RETURN(code);
×
4556
      }
4557
    }
4558

4559
    mndUserFreeObj(&newUser);
437,002✔
4560
    sdbRelease(pSdb, pUser);
437,002✔
4561
  }
4562

4563
  if (pUser != NULL) sdbRelease(pSdb, pUser);
437,002✔
4564
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
437,002✔
4565
  mndUserFreeObj(&newUser);
437,002✔
4566
  TAOS_RETURN(code);
437,002✔
4567
}
4568

4569
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
137,568✔
4570
  int32_t   code = 0;
137,568✔
4571
  SSdb     *pSdb = pMnode->pSdb;
137,568✔
4572
  int32_t   len = strlen(view) + 1;
137,568✔
4573
  void     *pIter = NULL;
137,568✔
4574
  SUserObj *pUser = NULL;
137,568✔
4575
  SUserObj  newUser = {0};
137,568✔
4576

4577
  while (1) {
143,183✔
4578
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
280,751✔
4579
    if (pIter == NULL) break;
280,751✔
4580

4581
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
143,183✔
4582
      break;
×
4583
    }
4584

4585
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
143,183✔
4586
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
143,183✔
4587
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
143,183✔
4588
    if (inRead || inWrite || inAlter) {
143,183✔
4589
      code = taosHashRemove(newUser.readViews, view, len);
3,491✔
4590
      if (code < 0) {
3,491✔
4591
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
694✔
4592
      }
4593
      code = taosHashRemove(newUser.writeViews, view, len);
3,491✔
4594
      if (code < 0) {
3,491✔
4595
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
1,756✔
4596
      }
4597
      code = taosHashRemove(newUser.alterViews, view, len);
3,491✔
4598
      if (code < 0) {
3,491✔
4599
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
1,062✔
4600
      }
4601

4602
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
3,491✔
4603
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
3,491✔
4604
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4605
        break;
×
4606
      }
4607
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
3,491✔
4608
      if (code < 0) {
3,491✔
4609
        mndUserFreeObj(&newUser);
×
4610
        sdbRelease(pSdb, pUser);
×
4611
        TAOS_RETURN(code);
×
4612
      }
4613
    }
4614

4615
    mndUserFreeObj(&newUser);
143,183✔
4616
    sdbRelease(pSdb, pUser);
143,183✔
4617
  }
4618

4619
  if (pUser != NULL) sdbRelease(pSdb, pUser);
137,568✔
4620
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
137,568✔
4621
  mndUserFreeObj(&newUser);
137,568✔
4622
  TAOS_RETURN(code);
137,568✔
4623
}
4624

4625
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
95,846✔
4626
  int32_t   code = 0;
95,846✔
4627
  SSdb     *pSdb = pMnode->pSdb;
95,846✔
4628
  int32_t   len = strlen(topic) + 1;
95,846✔
4629
  void     *pIter = NULL;
95,846✔
4630
  SUserObj *pUser = NULL;
95,846✔
4631
  SUserObj  newUser = {0};
95,846✔
4632

4633
  while (1) {
102,542✔
4634
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
198,388✔
4635
    if (pIter == NULL) {
198,388✔
4636
      break;
95,846✔
4637
    }
4638

4639
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
102,542✔
4640
      break;
×
4641
    }
4642

4643
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
102,542✔
4644
    if (inTopic) {
102,542✔
4645
      code = taosHashRemove(newUser.topics, topic, len);
162✔
4646
      if (code < 0) {
162✔
4647
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
4648
      }
4649
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
162✔
4650
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
162✔
4651
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4652
        break;
×
4653
      }
4654
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
162✔
4655
      if (code < 0) {
162✔
4656
        mndUserFreeObj(&newUser);
×
4657
        sdbRelease(pSdb, pUser);
×
4658
        TAOS_RETURN(code);
×
4659
      }
4660
    }
4661

4662
    mndUserFreeObj(&newUser);
102,542✔
4663
    sdbRelease(pSdb, pUser);
102,542✔
4664
  }
4665

4666
  if (pUser != NULL) sdbRelease(pSdb, pUser);
95,846✔
4667
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
95,846✔
4668
  mndUserFreeObj(&newUser);
95,846✔
4669
  TAOS_RETURN(code);
95,846✔
4670
}
4671

4672
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4673
  // ver = 0, disable ip white list
4674
  // ver > 0, enable ip white list
4675
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
4676
}
4677

4678
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4679
  // ver = 0, disable datetime white list
4680
  // ver > 0, enable datetime white list
4681
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
4682
}
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