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

taosdata / TDengine / #4875

09 Dec 2025 01:22AM UTC coverage: 64.472% (-0.2%) from 64.623%
#4875

push

travis-ci

guanshengliang
fix: temporarily disable memory leak detection for UDF tests (#33856)

162014 of 251293 relevant lines covered (64.47%)

104318075.66 hits per line

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

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

16
#define _DEFAULT_SOURCE
17
// clang-format off
18
#ifndef TD_ASTRA
19
#include <uv.h>
20
#endif
21
#include "crypt.h"
22
#include "mndUser.h"
23
#include "audit.h"
24
#include "mndDb.h"
25
#include "mndPrivilege.h"
26
#include "mndShow.h"
27
#include "mndStb.h"
28
#include "mndTopic.h"
29
#include "mndTrans.h"
30
#include "tbase64.h"
31

32
// clang-format on
33

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

139

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

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

153
static SUserCache userCache;
154

155

156
static int32_t userCacheInit() {
503,631✔
157
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
503,631✔
158

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

164
  userCache.users = users;
503,631✔
165
  userCache.verIp = 0;
503,631✔
166
  userCache.verTime = 0;
503,631✔
167

168
  (void)taosThreadRwlockInit(&userCache.rw, NULL);
503,631✔
169
  TAOS_RETURN(0);
503,631✔
170
}
171

172

173

174
static void userCacheCleanup() {
502,819✔
175
  if (userCache.users == NULL) {
502,819✔
176
    return;
×
177
  }
178

179
  void *pIter = taosHashIterate(userCache.users, NULL);
502,819✔
180
  while (pIter) {
1,016,484✔
181
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
513,665✔
182
    if (pInfo != NULL) {
513,665✔
183
      taosMemoryFree(pInfo->wlIp);
513,665✔
184
      taosMemoryFree(pInfo->wlTime);
513,665✔
185
      taosMemoryFree(pInfo);
513,665✔
186
    }
187
    pIter = taosHashIterate(userCache.users, pIter);
513,665✔
188
  }
189
  taosHashCleanup(userCache.users);
502,819✔
190

191
  (void)taosThreadRwlockDestroy(&userCache.rw);
502,819✔
192
}
193

194

195

196
static void userCacheRemoveUser(const char *user) {
60,915✔
197
  size_t userLen = strlen(user);
60,915✔
198

199
  (void)taosThreadRwlockWrlock(&userCache.rw);
60,915✔
200

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

215
  (void)taosThreadRwlockUnlock(&userCache.rw);
60,915✔
216
}
60,915✔
217

218

219

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

223
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,341✔
224

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

232
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,341✔
233
}
1,341✔
234

235

236

237
static SCachedUserInfo* getCachedUserInfo(const char* user) {
4,221,957✔
238
  size_t userLen = strlen(user);
4,221,957✔
239
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
4,221,957✔
240
  if (ppInfo != NULL) {
4,221,957✔
241
    return *ppInfo;
3,647,377✔
242
  }
243

244
  SCachedUserInfo  *pInfo = (SCachedUserInfo *)taosMemoryCalloc(1, sizeof(SCachedUserInfo));
574,580✔
245
  if (pInfo == NULL) {
574,580✔
246
    return NULL;
×
247
  }
248

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

254
  return pInfo;
574,580✔
255
}
256

257

258

259
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
2,699,713✔
260
  size_t userLen = strlen(user);
2,699,713✔
261

262
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,699,713✔
263

264
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,699,713✔
265
  if (ppInfo != NULL && *ppInfo != NULL) {
2,699,713✔
266
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,376,427✔
267
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,376,427✔
268
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,376,427✔
269
  } else {
270
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
323,286✔
271
    pLoginInfo->failedLoginCount = 0;
323,286✔
272
    pLoginInfo->lastFailedLoginTime = 0;
323,286✔
273
  }
274

275
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,699,458✔
276

277
  if (pLoginInfo->lastLoginTime == 0) {
2,699,713✔
278
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
46,576✔
279
  }
280
}
2,699,713✔
281

282

283

284
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
2,699,713✔
285
  size_t userLen = strlen(user);
2,699,713✔
286

287
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,699,713✔
288

289
  SCachedUserInfo  *pInfo = getCachedUserInfo(user);
2,699,713✔
290
  if (pInfo != NULL) {
2,699,713✔
291
    pInfo->loginInfo.lastLoginTime = pLoginInfo->lastLoginTime;
2,699,713✔
292
    pInfo->loginInfo.failedLoginCount = pLoginInfo->failedLoginCount;
2,699,713✔
293
    pInfo->loginInfo.lastFailedLoginTime = pLoginInfo->lastFailedLoginTime;
2,699,713✔
294
  }
295

296
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,699,713✔
297
}
2,699,713✔
298

299

300

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

306
  if (a == NULL || b == NULL) {
1,017,221✔
307
    return false;
102,633✔
308
  }
309

310
  if (a->num != b->num) {
914,588✔
311
    return false;
×
312
  }
313

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

323
  return true;
914,588✔
324
}
325

326

327

328
static int32_t userCacheUpdateWhiteList(SMnode* pMnode, SUserObj* pUser) {
1,017,221✔
329
  int32_t code = 0, lino = 0;
1,017,221✔
330

331
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,017,221✔
332

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

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

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

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

366

367

368
static int32_t userCacheRebuildIpWhiteList(SMnode *pMnode) {
936,089✔
369
  int32_t   code = 0, lino = 0;
936,089✔
370

371
  SSdb     *pSdb = pMnode->pSdb;
936,089✔
372
  void     *pIter = NULL;
936,089✔
373
  while (1) {
258,365✔
374
    SUserObj *pUser = NULL;
1,194,454✔
375
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,194,454✔
376
    if (pIter == NULL) {
1,194,454✔
377
      break;
936,089✔
378
    }
379

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

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

394
    taosMemoryFree(pInfo->wlIp);
258,365✔
395
    pInfo->wlIp = wl;
258,365✔
396

397
    sdbRelease(pSdb, pUser);
258,365✔
398
  }
399

400
  userCache.verIp++;
936,089✔
401

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

409

410

411
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
40,252,961✔
412
  int64_t ver = 0;
40,252,961✔
413
  int32_t code = 0;
40,252,961✔
414

415
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,252,961✔
416
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,182✔
417

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

429
    (void)taosThreadRwlockUnlock(&userCache.rw);
13,182✔
430
  }
431

432
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
40,252,961✔
433
  return ver;
40,252,961✔
434
}
435

436

437

438
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
936,089✔
439
  int32_t code = 0;
936,089✔
440
  (void)taosThreadRwlockWrlock(&userCache.rw);
936,089✔
441

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

449
  TAOS_RETURN(code);
936,089✔
450
}
451

452

453

454
static int32_t userCacheRebuildTimeWhiteList(SMnode *pMnode) {
924,382✔
455
  int32_t   code = 0, lino = 0;
924,382✔
456

457
  SSdb     *pSdb = pMnode->pSdb;
924,382✔
458
  void     *pIter = NULL;
924,382✔
459
  while (1) {
246,658✔
460
    SUserObj *pUser = NULL;
1,171,040✔
461
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,171,040✔
462
    if (pIter == NULL) {
1,171,040✔
463
      break;
924,382✔
464
    }
465

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

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

480
    taosMemoryFree(pInfo->wlTime);
246,658✔
481
    pInfo->wlTime = wl;
246,658✔
482

483
    sdbRelease(pSdb, pUser);
246,658✔
484
  }
485

486
  userCache.verTime++;
924,382✔
487

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

495

496

497
int32_t mndRefreshUserDateTimeWhiteList(SMnode *pMnode) {
924,382✔
498
  int32_t code = 0;
924,382✔
499
  (void)taosThreadRwlockWrlock(&userCache.rw);
924,382✔
500

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

508
  TAOS_RETURN(code);
924,382✔
509
}
510

511

512

513
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
40,252,961✔
514
  int64_t ver = 0;
40,252,961✔
515
  int32_t code = 0;
40,252,961✔
516

517
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,252,961✔
518
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,182✔
519

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

531
    (void)taosThreadRwlockUnlock(&userCache.rw);
13,182✔
532
  }
533

534
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
40,252,961✔
535
  return ver;
40,252,961✔
536
}
537

538

539

540
int32_t mndInitUser(SMnode *pMnode) {
503,631✔
541
  TAOS_CHECK_RETURN(userCacheInit());
503,631✔
542

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

554
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
503,631✔
555
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
503,631✔
556
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
503,631✔
557
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
503,631✔
558

559
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST, mndProcessGetUserIpWhiteListReq);
503,631✔
560
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_IP_WHITELIST_DUAL, mndProcessGetUserIpWhiteListReq);
503,631✔
561
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST, mndProcessRetrieveIpWhiteListReq);
503,631✔
562
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL, mndProcessRetrieveIpWhiteListReq);
503,631✔
563
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_DATETIME_WHITELIST, mndProcessGetUserDateTimeWhiteListReq);
503,631✔
564
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_DATETIME_WHITELIST, mndProcessRetrieveDateTimeWhiteListReq);
503,631✔
565

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

575

576

577
void mndCleanupUser(SMnode *pMnode) {
502,819✔
578
  userCacheCleanup();
502,819✔
579
}
502,819✔
580

581

582

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

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

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

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

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

603

604

605
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
53,338✔
606
  int32_t len = 0;
53,338✔
607
  for (int i = 0; i < num; i++) {
161,450✔
608
    SIpRange *pRange = &range[i];
108,112✔
609
    SIpAddr   addr = {0};
108,112✔
610
    (void)tIpUintToStr(pRange, &addr);
108,112✔
611

612
    len += tsnprintf(buf + len, bufLen - len, "%c%s/%d, ", pRange->neg ? '-' : '+', IP_ADDR_STR(&addr), addr.mask);
108,112✔
613
  }
614
  if (len > 0) buf[len - 2] = 0;
53,338✔
615
  return len;
53,338✔
616
}
617

618

619

620
static bool isIpRangeEqual(SIpRange *a, SIpRange *b) {
1,828,458✔
621
  if (a->type != b->type || a->neg != b->neg) {
1,828,458✔
622
    return false;
×
623
  }
624

625
  if (a->type == 0) {
1,828,458✔
626
    SIpV4Range *a4 = &a->ipV4;
914,588✔
627
    SIpV4Range *b4 = &b->ipV4;
914,588✔
628
    return (a4->ip == b4->ip && a4->mask == b4->mask);
914,588✔
629
  }
630
  
631
  SIpV6Range *a6 = &a->ipV6;
913,870✔
632
  SIpV6Range *b6 = &b->ipV6;
913,870✔
633
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
913,870✔
634
}
635

636

637

638
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
1,017,221✔
639
  if (a == NULL && b == NULL) {
1,017,221✔
640
    return true;
×
641
  }
642
  
643
  if (a == NULL || b == NULL) {
1,017,221✔
644
    return false;
102,633✔
645
  }
646

647
  if (a->num != b->num) {
914,588✔
648
    return false;
359✔
649
  }
650
  for (int i = 0; i < a->num; i++) {
2,742,687✔
651
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,828,458✔
652
      return false;
×
653
    }
654
  }
655
  return true;
914,229✔
656
}
657

658

659
static int32_t compareIpRange(const void *a, const void *b, const void* arg) {
3,949✔
660
  SIpRange *ra = (SIpRange *)a;
3,949✔
661
  SIpRange *rb = (SIpRange *)b;
3,949✔
662

663
  if (ra->neg != rb->neg) {
3,949✔
664
    return (ra->neg) ? -1 : 1;
×
665
  }
666

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

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

678
  if (ra->ipV6.addr[0] != rb->ipV6.addr[0]) {
×
679
    return (ra->ipV6.addr[0] < rb->ipV6.addr[0]) ? -1 : 1;
×
680
  }
681
  if (ra->ipV6.addr[1] != rb->ipV6.addr[1]) {
×
682
    return (ra->ipV6.addr[1] < rb->ipV6.addr[1]) ? -1 : 1;
×
683
  }
684
  return (ra->ipV6.mask < rb->ipV6.mask) ? -1 : 1;
×
685
}
686

687
static void sortIpWhiteList(SIpWhiteListDual *pList) {
1,796✔
688
  (void)taosqsort(pList->pIpRanges, pList->num, sizeof(SIpRange), NULL, compareIpRange);
1,796✔
689
}
1,796✔
690

691

692

693
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
53,338✔
694
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
53,338✔
695

696
  int64_t bufLen = pList->num * 128 + 8;
53,338✔
697
  *buf = taosMemoryCalloc(1, bufLen);
53,338✔
698
  if (*buf == NULL) {
53,338✔
699
    return 0;
×
700
  }
701

702
  if (pList->num == 0) {
53,338✔
703
    return tsnprintf(*buf, bufLen, "+ALL");
×
704
  }
705

706
  int32_t len = ipRangeListToStr(pList->pIpRanges, pList->num, *buf, bufLen - 2);
53,338✔
707
  if (len == 0) {
53,338✔
708
    taosMemoryFreeClear(*buf);
×
709
    return 0;
×
710
  }
711
  return len;
53,338✔
712
}
713

714

715

716
static int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, uint32_t *pLen) {
2,519,454✔
717
  int32_t  code = 0;
2,519,454✔
718
  int32_t  lino = 0;
2,519,454✔
719
  int32_t  tlen = 0;
2,519,454✔
720
  SEncoder encoder = {0};
2,519,454✔
721
  tEncoderInit(&encoder, buf, len);
2,519,454✔
722

723
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,519,454✔
724
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
5,038,908✔
725

726
  for (int i = 0; i < pList->num; i++) {
7,559,437✔
727
    SIpRange *pRange = &(pList->pIpRanges[i]);
5,039,983✔
728
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
5,039,983✔
729
  }
730

731
  tEndEncode(&encoder);
2,519,454✔
732

733
  tlen = encoder.pos;
2,519,454✔
734
_OVER:
2,519,454✔
735
  tEncoderClear(&encoder);
2,519,454✔
736
  if (code < 0) {
2,519,454✔
737
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
738
  }
739
  if (pLen) *pLen = tlen;
2,519,454✔
740
  TAOS_RETURN(code);
2,519,454✔
741
}
742

743
static int32_t tDerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, bool supportNeg) {
1,641,874✔
744
  int32_t  code = 0;
1,641,874✔
745
  int32_t  lino = 0;
1,641,874✔
746
  SDecoder decoder = {0};
1,641,874✔
747
  tDecoderInit(&decoder, buf, len);
1,641,874✔
748

749
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,641,874✔
750
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
3,283,748✔
751

752
  for (int i = 0; i < pList->num; i++) {
4,926,339✔
753
    SIpRange *pRange = &(pList->pIpRanges[i]);
3,284,465✔
754
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
3,284,465✔
755
  }
756

757
_OVER:
1,641,874✔
758
  tEndDecode(&decoder);
1,641,874✔
759
  tDecoderClear(&decoder);
1,641,874✔
760
  if (code < 0) {
1,641,874✔
761
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
762
  }
763
  TAOS_RETURN(code);
1,641,874✔
764
}
765

766
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList) {
×
767
  int32_t  code = 0;
×
768
  int32_t  lino = 0;
×
769
  SDecoder decoder = {0};
×
770
  tDecoderInit(&decoder, buf, len);
×
771

772
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
773
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
774

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

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

790
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppList, bool supportNeg) {
1,641,874✔
791
  int32_t           code = 0;
1,641,874✔
792
  int32_t           lino = 0;
1,641,874✔
793
  int32_t           num = 0;
1,641,874✔
794
  SIpWhiteListDual *p = NULL;
1,641,874✔
795
  SDecoder          decoder = {0};
1,641,874✔
796
  tDecoderInit(&decoder, buf, len);
1,641,874✔
797

798
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,641,874✔
799
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
1,641,874✔
800

801
  p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + num * sizeof(SIpRange));
1,641,874✔
802
  if (p == NULL) {
1,641,874✔
803
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
804
  }
805
  TAOS_CHECK_GOTO(tDerializeIpWhiteList(buf, len, p, supportNeg), &lino, _OVER);
1,641,874✔
806

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

818
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList) {
×
819
  int32_t       code = 0;
×
820
  int32_t       lino = 0;
×
821
  int32_t       num = 0;
×
822
  SIpWhiteList *p = NULL;
×
823
  SDecoder      decoder = {0};
×
824
  tDecoderInit(&decoder, buf, len);
×
825

826
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
827
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
828

829
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
×
830
  if (p == NULL) {
×
831
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
832
  }
833
  TAOS_CHECK_GOTO(tDerializeIpWhileListFromOldVer(buf, len, p), &lino, _OVER);
×
834

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

846
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList) {
429,288✔
847
  int32_t code = 0;
429,288✔
848
  int32_t lino = 0;
429,288✔
849
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
429,288✔
850
  if (*ppWhiteList == NULL) {
429,288✔
851
    TAOS_RETURN(terrno);
×
852
  }
853
  (*ppWhiteList)->num = 2;
429,288✔
854

855
  SIpRange v4 = {0};
429,288✔
856
  SIpRange v6 = {0};
429,288✔
857

858
#ifndef TD_ASTRA
859
  code = createDefaultIp4Range(&v4);
429,288✔
860
  TSDB_CHECK_CODE(code, lino, _error);
429,288✔
861

862
  code = createDefaultIp6Range(&v6);
429,288✔
863
  TSDB_CHECK_CODE(code, lino, _error);
429,288✔
864

865
#endif
866

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

879

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

882
static int32_t convertTimeRangesToStr(SUserObj *pUser, char **buf) {
53,338✔
883
  int32_t bufLen = pUser->pTimeWhiteList->num * 32 + 8;
53,338✔
884
  *buf = taosMemoryCalloc(1, bufLen);
53,338✔
885
  if (*buf == NULL) {
53,338✔
886
    return 0;
×
887
  }
888

889
  int32_t pos = 0;
53,338✔
890
  if (pUser->pTimeWhiteList->num == 0) {
53,338✔
891
    pos += tsnprintf(*buf + pos, bufLen - pos, "+ALL");
53,338✔
892
    return pos;
53,338✔
893
  }
894

895
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
×
896
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
897
    int duration = range->duration / 60;
×
898

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

911
  if (pos > 0) {
×
912
    (*buf)[pos - 2] = 0; // remove last ", "
×
913
  }
914

915
  return pos;
×
916
}
917

918

919
static int32_t compareDateTimeInterval(const void *a, const void *b, const void* arg) {
×
920
  SDateTimeWhiteListItem *pA = (SDateTimeWhiteListItem *)a;
×
921
  SDateTimeWhiteListItem *pB = (SDateTimeWhiteListItem *)b;
×
922

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

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

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

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

939
  return 0;
×
940
}
941

942
static void sortTimeWhiteList(SDateTimeWhiteList *pList) {
×
943
  (void)taosqsort(pList->ranges, pList->num, sizeof(SDateTimeWhiteListItem), NULL, compareDateTimeInterval);
×
944
}
×
945

946

947

948

949
static void dropOldPasswords(SUserObj *pUser) {
4,161,328✔
950
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
4,161,328✔
951
    return;
4,109,821✔
952
  }
953

954
  int32_t reuseMax = pUser->passwordReuseMax;
51,507✔
955
  if (reuseMax == 0) {
51,507✔
956
    reuseMax = 1; // keep at least one password
47,907✔
957
  }
958

959
  int64_t now = taosGetTimestampSec();
51,507✔
960
  int32_t index = reuseMax;
51,507✔
961
  while(index < pUser->numOfPasswords) {
61,587✔
962
    SUserPassword *pPass = &pUser->passwords[index];
10,080✔
963
    if (now - pPass->setTime >= pUser->passwordReuseTime) {
10,080✔
964
      break;
×
965
    }
966
    index++;
10,080✔
967
  }
968

969
  if (index == pUser->numOfPasswords) {
51,507✔
970
    return;
51,507✔
971
  }
972
  pUser->numOfPasswords = index;
×
973
  // this is a shrink operation, no need to check return value
974
  pUser->passwords = taosMemoryRealloc(pUser->passwords, sizeof(SUserPassword) * pUser->numOfPasswords);
×
975
}
976

977

978

979

980
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
328,095✔
981
  int32_t  code = 0;
328,095✔
982
  int32_t  lino = 0;
328,095✔
983
  SUserObj userObj = {0};
328,095✔
984

985
  userObj.passwords = taosMemCalloc(1, sizeof(SUserPassword));
328,095✔
986
  if (userObj.passwords == NULL) {
328,095✔
987
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _ERROR);
×
988
  }
989
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.passwords[0].pass);
328,095✔
990
  userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
328,095✔
991
  if (tsiEncryptPassAlgorithm == DND_CA_SM4 && strlen(tsEncryptKey) > 0) {
328,095✔
992
    generateSalt(userObj.salt, sizeof(userObj.salt));
×
993
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino, _ERROR);
×
994
  }
995

996
  userObj.passwords[0].setTime = taosGetTimestampSec();
328,095✔
997
  userObj.numOfPasswords = 1;
328,095✔
998

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

1040
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
328,095✔
1041
  if (pRaw == NULL) goto _ERROR;
328,095✔
1042
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
328,095✔
1043

1044
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
328,095✔
1045

1046
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
328,095✔
1047
  if (pTrans == NULL) {
328,095✔
1048
    sdbFreeRaw(pRaw);
×
1049
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
1050
    goto _ERROR;
×
1051
  }
1052
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
328,095✔
1053

1054
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
328,095✔
1055
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1056
    mndTransDrop(pTrans);
×
1057
    goto _ERROR;
×
1058
  }
1059
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
328,095✔
1060

1061
  if (mndTransPrepare(pMnode, pTrans) != 0) {
328,095✔
1062
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1063
    mndTransDrop(pTrans);
×
1064
    goto _ERROR;
×
1065
  }
1066

1067
  mndTransDrop(pTrans);
328,095✔
1068
  taosMemoryFree(userObj.passwords);
328,095✔
1069
  taosMemoryFree(userObj.pIpWhiteListDual);
328,095✔
1070
  taosMemoryFree(userObj.pTimeWhiteList);
328,095✔
1071
  return 0;
328,095✔
1072

1073
_ERROR:
×
1074
  taosMemoryFree(userObj.passwords);
×
1075
  taosMemoryFree(userObj.pIpWhiteListDual);
×
1076
  taosMemoryFree(userObj.pTimeWhiteList);
×
1077
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
1078
}
1079

1080
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
328,095✔
1081
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
328,095✔
1082
}
1083

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

1105
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,519,454✔
1106
  while (stb != NULL) {
2,958,987✔
1107
    size_t keyLen = 0;
439,533✔
1108
    void  *key = taosHashGetKey(stb, &keyLen);
439,533✔
1109
    size += sizeof(int32_t);
439,533✔
1110
    size += keyLen;
439,533✔
1111

1112
    size_t valueLen = 0;
439,533✔
1113
    valueLen = strlen(stb) + 1;
439,533✔
1114
    size += sizeof(int32_t);
439,533✔
1115
    size += valueLen;
439,533✔
1116
    stb = taosHashIterate(pUser->readTbs, stb);
439,533✔
1117
  }
1118

1119
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,519,454✔
1120
  while (stb != NULL) {
2,978,458✔
1121
    size_t keyLen = 0;
459,004✔
1122
    void  *key = taosHashGetKey(stb, &keyLen);
459,004✔
1123
    size += sizeof(int32_t);
459,004✔
1124
    size += keyLen;
459,004✔
1125

1126
    size_t valueLen = 0;
459,004✔
1127
    valueLen = strlen(stb) + 1;
459,004✔
1128
    size += sizeof(int32_t);
459,004✔
1129
    size += valueLen;
459,004✔
1130
    stb = taosHashIterate(pUser->writeTbs, stb);
459,004✔
1131
  }
1132

1133
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,519,454✔
1134
  while (stb != NULL) {
2,756,150✔
1135
    size_t keyLen = 0;
236,696✔
1136
    void  *key = taosHashGetKey(stb, &keyLen);
236,696✔
1137
    size += sizeof(int32_t);
236,696✔
1138
    size += keyLen;
236,696✔
1139

1140
    size_t valueLen = 0;
236,696✔
1141
    valueLen = strlen(stb) + 1;
236,696✔
1142
    size += sizeof(int32_t);
236,696✔
1143
    size += valueLen;
236,696✔
1144
    stb = taosHashIterate(pUser->alterTbs, stb);
236,696✔
1145
  }
1146

1147
  stb = taosHashIterate(pUser->readViews, NULL);
2,519,454✔
1148
  while (stb != NULL) {
2,569,869✔
1149
    size_t keyLen = 0;
50,415✔
1150
    void  *key = taosHashGetKey(stb, &keyLen);
50,415✔
1151
    size += sizeof(int32_t);
50,415✔
1152
    size += keyLen;
50,415✔
1153

1154
    size_t valueLen = 0;
50,415✔
1155
    valueLen = strlen(stb) + 1;
50,415✔
1156
    size += sizeof(int32_t);
50,415✔
1157
    size += valueLen;
50,415✔
1158
    stb = taosHashIterate(pUser->readViews, stb);
50,415✔
1159
  }
1160

1161
  stb = taosHashIterate(pUser->writeViews, NULL);
2,519,454✔
1162
  while (stb != NULL) {
2,558,386✔
1163
    size_t keyLen = 0;
38,932✔
1164
    void  *key = taosHashGetKey(stb, &keyLen);
38,932✔
1165
    size += sizeof(int32_t);
38,932✔
1166
    size += keyLen;
38,932✔
1167

1168
    size_t valueLen = 0;
38,932✔
1169
    valueLen = strlen(stb) + 1;
38,932✔
1170
    size += sizeof(int32_t);
38,932✔
1171
    size += valueLen;
38,932✔
1172
    stb = taosHashIterate(pUser->writeViews, stb);
38,932✔
1173
  }
1174

1175
  stb = taosHashIterate(pUser->alterViews, NULL);
2,519,454✔
1176
  while (stb != NULL) {
2,557,818✔
1177
    size_t keyLen = 0;
38,364✔
1178
    void  *key = taosHashGetKey(stb, &keyLen);
38,364✔
1179
    size += sizeof(int32_t);
38,364✔
1180
    size += keyLen;
38,364✔
1181

1182
    size_t valueLen = 0;
38,364✔
1183
    valueLen = strlen(stb) + 1;
38,364✔
1184
    size += sizeof(int32_t);
38,364✔
1185
    size += valueLen;
38,364✔
1186
    stb = taosHashIterate(pUser->alterViews, stb);
38,364✔
1187
  }
1188

1189
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,519,454✔
1190
  while (useDb != NULL) {
3,086,424✔
1191
    size_t keyLen = 0;
566,970✔
1192
    void  *key = taosHashGetKey(useDb, &keyLen);
566,970✔
1193
    size += sizeof(int32_t);
566,970✔
1194
    size += keyLen;
566,970✔
1195
    size += sizeof(int32_t);
566,970✔
1196
    useDb = taosHashIterate(pUser->useDbs, useDb);
566,970✔
1197
  }
1198

1199
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
2,519,454✔
1200
  if (pRaw == NULL) {
2,519,454✔
1201
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1202
  }
1203

1204
  int32_t dataPos = 0;
2,519,454✔
1205
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
2,519,454✔
1206

1207
  dropOldPasswords(pUser);
2,519,454✔
1208
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,519,454✔
1209
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
5,070,012✔
1210
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,550,558✔
1211
    SDB_SET_INT64(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,550,558✔
1212
  }
1213
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,519,454✔
1214

1215
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
2,519,454✔
1216
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
2,519,454✔
1217
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
2,519,454✔
1218
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
2,519,454✔
1219
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
2,519,454✔
1220
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
2,519,454✔
1221
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
2,519,454✔
1222
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
2,519,454✔
1223
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
2,519,454✔
1224
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
2,519,454✔
1225
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
2,519,454✔
1226
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
2,519,454✔
1227

1228
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,519,454✔
1229
  while (db != NULL) {
2,853,489✔
1230
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
334,035✔
1231
    db = taosHashIterate(pUser->readDbs, db);
334,035✔
1232
  }
1233

1234
  db = taosHashIterate(pUser->writeDbs, NULL);
2,519,454✔
1235
  while (db != NULL) {
2,843,496✔
1236
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
324,042✔
1237
    db = taosHashIterate(pUser->writeDbs, db);
324,042✔
1238
  }
1239

1240
  char *topic = taosHashIterate(pUser->topics, NULL);
2,519,454✔
1241
  while (topic != NULL) {
2,536,466✔
1242
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
17,012✔
1243
    topic = taosHashIterate(pUser->topics, topic);
17,012✔
1244
  }
1245

1246
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
2,519,454✔
1247
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
2,519,454✔
1248
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
2,519,454✔
1249
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
2,519,454✔
1250
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
2,519,454✔
1251
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
2,519,454✔
1252
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
2,519,454✔
1253

1254
  stb = taosHashIterate(pUser->readTbs, NULL);
2,519,454✔
1255
  while (stb != NULL) {
2,958,987✔
1256
    size_t keyLen = 0;
439,533✔
1257
    void  *key = taosHashGetKey(stb, &keyLen);
439,533✔
1258
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
439,533✔
1259
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
439,533✔
1260

1261
    size_t valueLen = 0;
439,533✔
1262
    valueLen = strlen(stb) + 1;
439,533✔
1263
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
439,533✔
1264
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
439,533✔
1265
    stb = taosHashIterate(pUser->readTbs, stb);
439,533✔
1266
  }
1267

1268
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,519,454✔
1269
  while (stb != NULL) {
2,978,458✔
1270
    size_t keyLen = 0;
459,004✔
1271
    void  *key = taosHashGetKey(stb, &keyLen);
459,004✔
1272
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
459,004✔
1273
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
459,004✔
1274

1275
    size_t valueLen = 0;
459,004✔
1276
    valueLen = strlen(stb) + 1;
459,004✔
1277
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
459,004✔
1278
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
459,004✔
1279
    stb = taosHashIterate(pUser->writeTbs, stb);
459,004✔
1280
  }
1281

1282
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,519,454✔
1283
  while (stb != NULL) {
2,756,150✔
1284
    size_t keyLen = 0;
236,696✔
1285
    void  *key = taosHashGetKey(stb, &keyLen);
236,696✔
1286
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
236,696✔
1287
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
236,696✔
1288

1289
    size_t valueLen = 0;
236,696✔
1290
    valueLen = strlen(stb) + 1;
236,696✔
1291
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
236,696✔
1292
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
236,696✔
1293
    stb = taosHashIterate(pUser->alterTbs, stb);
236,696✔
1294
  }
1295

1296
  stb = taosHashIterate(pUser->readViews, NULL);
2,519,454✔
1297
  while (stb != NULL) {
2,569,869✔
1298
    size_t keyLen = 0;
50,415✔
1299
    void  *key = taosHashGetKey(stb, &keyLen);
50,415✔
1300
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
50,415✔
1301
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
50,415✔
1302

1303
    size_t valueLen = 0;
50,415✔
1304
    valueLen = strlen(stb) + 1;
50,415✔
1305
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
50,415✔
1306
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
50,415✔
1307
    stb = taosHashIterate(pUser->readViews, stb);
50,415✔
1308
  }
1309

1310
  stb = taosHashIterate(pUser->writeViews, NULL);
2,519,454✔
1311
  while (stb != NULL) {
2,558,386✔
1312
    size_t keyLen = 0;
38,932✔
1313
    void  *key = taosHashGetKey(stb, &keyLen);
38,932✔
1314
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
38,932✔
1315
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
38,932✔
1316

1317
    size_t valueLen = 0;
38,932✔
1318
    valueLen = strlen(stb) + 1;
38,932✔
1319
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
38,932✔
1320
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
38,932✔
1321
    stb = taosHashIterate(pUser->writeViews, stb);
38,932✔
1322
  }
1323

1324
  stb = taosHashIterate(pUser->alterViews, NULL);
2,519,454✔
1325
  while (stb != NULL) {
2,557,818✔
1326
    size_t keyLen = 0;
38,364✔
1327
    void  *key = taosHashGetKey(stb, &keyLen);
38,364✔
1328
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
38,364✔
1329
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
38,364✔
1330

1331
    size_t valueLen = 0;
38,364✔
1332
    valueLen = strlen(stb) + 1;
38,364✔
1333
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
38,364✔
1334
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
38,364✔
1335
    stb = taosHashIterate(pUser->alterViews, stb);
38,364✔
1336
  }
1337

1338
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,519,454✔
1339
  while (useDb != NULL) {
3,086,424✔
1340
    size_t keyLen = 0;
566,970✔
1341
    void  *key = taosHashGetKey(useDb, &keyLen);
566,970✔
1342
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
566,970✔
1343
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
566,970✔
1344

1345
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
566,970✔
1346
    useDb = taosHashIterate(pUser->useDbs, useDb);
566,970✔
1347
  }
1348

1349
  // save white list
1350
  int32_t num = pUser->pIpWhiteListDual->num;
2,519,454✔
1351
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
2,519,454✔
1352
  if ((buf = taosMemoryCalloc(1, tlen)) == NULL) {
2,519,454✔
1353
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1354
  }
1355
  int32_t len = 0;
2,519,454✔
1356
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
2,519,454✔
1357

1358
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
2,519,454✔
1359
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
2,519,454✔
1360

1361
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
2,519,454✔
1362
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
2,519,454✔
1363

1364
  SDB_SET_BINARY(pRaw, dataPos, pUser->totpsecret, sizeof(pUser->totpsecret), _OVER);
2,519,454✔
1365
  SDB_SET_INT8(pRaw, dataPos, pUser->changePass, _OVER);
2,519,454✔
1366
  SDB_SET_INT32(pRaw, dataPos, pUser->sessionPerUser, _OVER);
2,519,454✔
1367
  SDB_SET_INT32(pRaw, dataPos, pUser->connectTime, _OVER);
2,519,454✔
1368
  SDB_SET_INT32(pRaw, dataPos, pUser->connectIdleTime, _OVER);
2,519,454✔
1369
  SDB_SET_INT32(pRaw, dataPos, pUser->callPerSession, _OVER);
2,519,454✔
1370
  SDB_SET_INT32(pRaw, dataPos, pUser->vnodePerCall, _OVER);
2,519,454✔
1371
  SDB_SET_INT32(pRaw, dataPos, pUser->failedLoginAttempts, _OVER);
2,519,454✔
1372
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLifeTime, _OVER);
2,519,454✔
1373
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseTime, _OVER);
2,519,454✔
1374
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordReuseMax, _OVER);
2,519,454✔
1375
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordLockTime, _OVER);
2,519,454✔
1376
  SDB_SET_INT32(pRaw, dataPos, pUser->passwordGraceTime, _OVER);
2,519,454✔
1377
  SDB_SET_INT32(pRaw, dataPos, pUser->inactiveAccountTime, _OVER);
2,519,454✔
1378
  SDB_SET_INT32(pRaw, dataPos, pUser->allowTokenNum, _OVER);
2,519,454✔
1379

1380
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
2,519,454✔
1381
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
2,519,454✔
1382
    SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1383
    SDB_SET_BOOL(pRaw, dataPos, range->absolute, _OVER);
×
1384
    SDB_SET_BOOL(pRaw, dataPos, range->neg, _OVER);
×
1385
    SDB_SET_INT64(pRaw, dataPos, range->start, _OVER);
×
1386
    SDB_SET_INT32(pRaw, dataPos, range->duration, _OVER);
×
1387
  }
1388

1389
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,519,454✔
1390
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,519,454✔
1391

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

1402
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
2,519,454✔
1403
  return pRaw;
2,519,454✔
1404
}
1405

1406
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
1,641,874✔
1407
  int32_t   code = 0;
1,641,874✔
1408
  int32_t   lino = 0;
1,641,874✔
1409
  SSdbRow  *pRow = NULL;
1,641,874✔
1410
  SUserObj *pUser = NULL;
1,641,874✔
1411
  char     *key = NULL;
1,641,874✔
1412
  char     *value = NULL;
1,641,874✔
1413

1414
  int8_t sver = 0;
1,641,874✔
1415
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
1,641,874✔
1416
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1417
  }
1418

1419
  if (sver < 1 || sver > USER_VER_NUMBER) {
1,641,874✔
1420
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1421
  }
1422

1423
  pRow = sdbAllocRow(sizeof(SUserObj));
1,641,874✔
1424
  if (pRow == NULL) {
1,641,874✔
1425
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1426
  }
1427

1428
  pUser = sdbGetRowObj(pRow);
1,641,874✔
1429
  if (pUser == NULL) {
1,641,874✔
1430
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1431
  }
1432

1433
  int32_t dataPos = 0;
1,641,874✔
1434
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
1,641,874✔
1435

1436
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,641,874✔
1437
    pUser->passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
×
1438
    if (pUser->passwords == NULL) {
×
1439
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1440
    }
1441
    SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[0].pass, TSDB_PASSWORD_LEN, _OVER)
×
1442
    pUser->numOfPasswords = 1;
×
1443
    memset(pUser->salt, 0, sizeof(pUser->salt));
×
1444
  } else {
1445
    SDB_GET_INT32(pRaw, dataPos, &pUser->numOfPasswords, _OVER)
1,641,874✔
1446
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,641,874✔
1447
    if (pUser->passwords == NULL) {
1,641,874✔
1448
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1449
    }
1450
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
3,336,386✔
1451
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
1,694,512✔
1452
      SDB_GET_INT64(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
1,694,512✔
1453
    }
1454
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
1,641,874✔
1455
  }
1456
  
1457
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
1,641,874✔
1458
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
1,641,874✔
1459
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
1,641,874✔
1460
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,641,874✔
1461
    pUser->passwords[0].setTime = pUser->updateTime / 1000;
×
1462
  }
1463

1464
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
1,641,874✔
1465
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
1,641,874✔
1466
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
1,641,874✔
1467
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
1,641,874✔
1468
  if (pUser->superUser) pUser->createdb = 1;
1,641,874✔
1469
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
1,641,874✔
1470
  if (sver >= 4) {
1,641,874✔
1471
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
1,641,874✔
1472
  }
1473

1474
  int32_t numOfReadDbs = 0;
1,641,874✔
1475
  int32_t numOfWriteDbs = 0;
1,641,874✔
1476
  int32_t numOfTopics = 0;
1,641,874✔
1477
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
1,641,874✔
1478
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
1,641,874✔
1479
  if (sver >= 2) {
1,641,874✔
1480
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
1,641,874✔
1481
  }
1482

1483
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1484
  pUser->writeDbs =
1,641,874✔
1485
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1486
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1487
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
1,641,874✔
1488
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1489
    goto _OVER;
×
1490
  }
1491

1492
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
1,963,937✔
1493
    char db[TSDB_DB_FNAME_LEN] = {0};
322,063✔
1494
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
322,063✔
1495
    int32_t len = strlen(db) + 1;
322,063✔
1496
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
322,063✔
1497
  }
1498

1499
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
1,957,007✔
1500
    char db[TSDB_DB_FNAME_LEN] = {0};
315,133✔
1501
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
315,133✔
1502
    int32_t len = strlen(db) + 1;
315,133✔
1503
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
315,133✔
1504
  }
1505

1506
  if (sver >= 2) {
1,641,874✔
1507
    for (int32_t i = 0; i < numOfTopics; ++i) {
1,657,453✔
1508
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
15,579✔
1509
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
15,579✔
1510
      int32_t len = strlen(topic) + 1;
15,579✔
1511
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
15,579✔
1512
    }
1513
  }
1514

1515
  if (sver >= 3) {
1,641,874✔
1516
    int32_t numOfReadTbs = 0;
1,641,874✔
1517
    int32_t numOfWriteTbs = 0;
1,641,874✔
1518
    int32_t numOfAlterTbs = 0;
1,641,874✔
1519
    int32_t numOfReadViews = 0;
1,641,874✔
1520
    int32_t numOfWriteViews = 0;
1,641,874✔
1521
    int32_t numOfAlterViews = 0;
1,641,874✔
1522
    int32_t numOfUseDbs = 0;
1,641,874✔
1523
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
1,641,874✔
1524
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
1,641,874✔
1525
    if (sver >= 6) {
1,641,874✔
1526
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
1,641,874✔
1527
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
1,641,874✔
1528
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
1,641,874✔
1529
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
1,641,874✔
1530
    }
1531
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
1,641,874✔
1532

1533
    pUser->readTbs =
1,641,874✔
1534
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1535
    pUser->writeTbs =
1,641,874✔
1536
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1537
    pUser->alterTbs =
1,641,874✔
1538
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1539

1540
    pUser->readViews =
1,641,874✔
1541
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1542
    pUser->writeViews =
1,641,874✔
1543
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1544
    pUser->alterViews =
1,641,874✔
1545
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1546

1547
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,641,874✔
1548

1549
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
1,641,874✔
1550
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
1,641,874✔
1551
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1552
      goto _OVER;
×
1553
    }
1554

1555
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,056,481✔
1556
      int32_t keyLen = 0;
414,607✔
1557
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
414,607✔
1558

1559
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
414,607✔
1560
      if (key == NULL) {
414,607✔
1561
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1562
      }
1563
      (void)memset(key, 0, keyLen);
414,607✔
1564
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
414,607✔
1565

1566
      int32_t valuelen = 0;
414,607✔
1567
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
414,607✔
1568
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
414,607✔
1569
      if (value == NULL) {
414,607✔
1570
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1571
      }
1572
      (void)memset(value, 0, valuelen);
414,607✔
1573
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
414,607✔
1574

1575
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
414,607✔
1576
    }
1577

1578
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
2,070,036✔
1579
      int32_t keyLen = 0;
428,162✔
1580
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
428,162✔
1581

1582
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
428,162✔
1583
      if (key == NULL) {
428,162✔
1584
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1585
      }
1586
      (void)memset(key, 0, keyLen);
428,162✔
1587
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
428,162✔
1588

1589
      int32_t valuelen = 0;
428,162✔
1590
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
428,162✔
1591
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
428,162✔
1592
      if (value == NULL) {
428,162✔
1593
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1594
      }
1595
      (void)memset(value, 0, valuelen);
428,162✔
1596
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
428,162✔
1597

1598
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
428,162✔
1599
    }
1600

1601
    if (sver >= 6) {
1,641,874✔
1602
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,865,968✔
1603
        int32_t keyLen = 0;
224,094✔
1604
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
224,094✔
1605

1606
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
224,094✔
1607
        if (key == NULL) {
224,094✔
1608
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1609
        }
1610
        (void)memset(key, 0, keyLen);
224,094✔
1611
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
224,094✔
1612

1613
        int32_t valuelen = 0;
224,094✔
1614
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
224,094✔
1615
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
224,094✔
1616
        if (value == NULL) {
224,094✔
1617
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1618
        }
1619
        (void)memset(value, 0, valuelen);
224,094✔
1620
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
224,094✔
1621

1622
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
224,094✔
1623
      }
1624

1625
      for (int32_t i = 0; i < numOfReadViews; ++i) {
1,690,891✔
1626
        int32_t keyLen = 0;
49,017✔
1627
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
49,017✔
1628

1629
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
49,017✔
1630
        if (key == NULL) {
49,017✔
1631
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1632
        }
1633
        (void)memset(key, 0, keyLen);
49,017✔
1634
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
49,017✔
1635

1636
        int32_t valuelen = 0;
49,017✔
1637
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
49,017✔
1638
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
49,017✔
1639
        if (value == NULL) {
49,017✔
1640
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1641
        }
1642
        (void)memset(value, 0, valuelen);
49,017✔
1643
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
49,017✔
1644

1645
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
49,017✔
1646
      }
1647

1648
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
1,679,408✔
1649
        int32_t keyLen = 0;
37,534✔
1650
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
37,534✔
1651

1652
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
37,534✔
1653
        if (key == NULL) {
37,534✔
1654
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1655
        }
1656
        (void)memset(key, 0, keyLen);
37,534✔
1657
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
37,534✔
1658

1659
        int32_t valuelen = 0;
37,534✔
1660
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
37,534✔
1661
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
37,534✔
1662
        if (value == NULL) {
37,534✔
1663
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1664
        }
1665
        (void)memset(value, 0, valuelen);
37,534✔
1666
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
37,534✔
1667

1668
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
37,534✔
1669
      }
1670

1671
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
1,678,840✔
1672
        int32_t keyLen = 0;
36,966✔
1673
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36,966✔
1674

1675
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
36,966✔
1676
        if (key == NULL) {
36,966✔
1677
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1678
        }
1679
        (void)memset(key, 0, keyLen);
36,966✔
1680
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,966✔
1681

1682
        int32_t valuelen = 0;
36,966✔
1683
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
36,966✔
1684
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
36,966✔
1685
        if (value == NULL) {
36,966✔
1686
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1687
        }
1688
        (void)memset(value, 0, valuelen);
36,966✔
1689
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
36,966✔
1690

1691
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterViews, key, keyLen, value, valuelen), &lino, _OVER);
36,966✔
1692
      }
1693
    }
1694

1695
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
2,164,299✔
1696
      int32_t keyLen = 0;
522,425✔
1697
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
522,425✔
1698

1699
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
522,425✔
1700
      if (key == NULL) {
522,425✔
1701
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1702
      }
1703
      (void)memset(key, 0, keyLen);
522,425✔
1704
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
522,425✔
1705

1706
      int32_t ref = 0;
522,425✔
1707
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
522,425✔
1708

1709
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
522,425✔
1710
    }
1711
  }
1712
  // decoder white list
1713
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
1,641,874✔
1714
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,641,874✔
1715
      int32_t len = 0;
×
1716
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
1717

1718
      TAOS_MEMORY_REALLOC(key, len);
×
1719
      if (key == NULL) {
×
1720
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1721
      }
1722
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
1723

1724
      SIpWhiteList *pIpWhiteList = NULL;
×
1725
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
1726

1727
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
1728

1729
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
1730
      if (code != 0) {
×
1731
        taosMemoryFreeClear(pIpWhiteList);
×
1732
      }
1733
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1734

1735
      taosMemoryFreeClear(pIpWhiteList);
×
1736

1737
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,641,874✔
1738
      int32_t len = 0;
1,641,874✔
1739
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
1,641,874✔
1740

1741
      TAOS_MEMORY_REALLOC(key, len);
1,641,874✔
1742
      if (key == NULL) {
1,641,874✔
1743
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1744
      }
1745
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
1,641,874✔
1746

1747
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual, sver >= USER_VER_SUPPORT_ADVANCED_SECURITY), &lino, _OVER);
1,641,874✔
1748
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
1,641,874✔
1749
    }
1750
  }
1751

1752
  if (pUser->pIpWhiteListDual == NULL) {
1,641,874✔
1753
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
1754
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1755
  }
1756

1757
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
1,641,874✔
1758

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

1796
    int32_t num = 0;
1,641,874✔
1797
    SDB_GET_INT32(pRaw, dataPos, &num, _OVER);
1,641,874✔
1798
    pUser->pTimeWhiteList = taosMemCalloc(1, sizeof(SDateTimeWhiteList) + num * sizeof(SDateTimeWhiteListItem));
1,641,874✔
1799
    if (pUser->pTimeWhiteList == NULL) {
1,641,874✔
1800
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1801
    }
1802

1803
    pUser->pTimeWhiteList->num = num;
1,641,874✔
1804
    for (int32_t i = 0; i < num; i++) {
1,641,874✔
1805
      SDateTimeWhiteListItem *range = &pUser->pTimeWhiteList->ranges[i];
×
1806
      SDB_GET_BOOL(pRaw, dataPos, &range->absolute, _OVER);
×
1807
      SDB_GET_BOOL(pRaw, dataPos, &range->neg, _OVER);
×
1808
      SDB_GET_INT64(pRaw, dataPos, &range->start, _OVER);
×
1809
      SDB_GET_INT32(pRaw, dataPos, &range->duration, _OVER);
×
1810
    }
1811
  }
1812

1813
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
1,641,874✔
1814
  taosInitRWLatch(&pUser->lock);
1,641,874✔
1815
  dropOldPasswords(pUser);
1,641,874✔
1816

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

1842
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
1,641,874✔
1843
  return pRow;
1,641,874✔
1844
}
1845

1846
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
623,971✔
1847
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
623,971✔
1848

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

1858
  return 0;
623,971✔
1859
}
1860

1861
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
38,968,668✔
1862
  int32_t code = 0;
38,968,668✔
1863
  *ppNew =
38,968,668✔
1864
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
38,968,668✔
1865
  if (*ppNew == NULL) {
38,968,668✔
1866
    TAOS_RETURN(terrno);
×
1867
  }
1868

1869
  char *tb = taosHashIterate(pOld, NULL);
38,968,354✔
1870
  while (tb != NULL) {
41,341,595✔
1871
    size_t keyLen = 0;
2,372,927✔
1872
    char  *key = taosHashGetKey(tb, &keyLen);
2,372,927✔
1873

1874
    int32_t valueLen = strlen(tb) + 1;
2,372,927✔
1875
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
2,372,927✔
1876
      taosHashCancelIterate(pOld, tb);
×
1877
      taosHashCleanup(*ppNew);
×
1878
      TAOS_RETURN(code);
×
1879
    }
1880
    tb = taosHashIterate(pOld, tb);
2,372,927✔
1881
  }
1882

1883
  TAOS_RETURN(code);
38,968,668✔
1884
}
1885

1886
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
1,686,247✔
1887
  int32_t code = 0;
1,686,247✔
1888
  *ppNew =
1,686,247✔
1889
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,686,247✔
1890
  if (*ppNew == NULL) {
1,686,247✔
1891
    TAOS_RETURN(terrno);
×
1892
  }
1893

1894
  int32_t *db = taosHashIterate(pOld, NULL);
1,686,247✔
1895
  while (db != NULL) {
2,187,850✔
1896
    size_t keyLen = 0;
501,603✔
1897
    char  *key = taosHashGetKey(db, &keyLen);
501,603✔
1898

1899
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
501,603✔
1900
      taosHashCancelIterate(pOld, db);
×
1901
      taosHashCleanup(*ppNew);
×
1902
      TAOS_RETURN(code);
×
1903
    }
1904
    db = taosHashIterate(pOld, db);
501,603✔
1905
  }
1906

1907
  TAOS_RETURN(code);
1,686,247✔
1908
}
1909

1910
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
1,686,247✔
1911
  int32_t code = 0;
1,686,247✔
1912
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
1,686,247✔
1913
  pNew->authVersion++;
1,686,247✔
1914
  pNew->updateTime = taosGetTimestampMs();
1,686,247✔
1915

1916
  pNew->passwords = NULL;
1,686,247✔
1917
  pNew->readDbs = NULL;
1,686,247✔
1918
  pNew->writeDbs = NULL;
1,686,247✔
1919
  pNew->readTbs = NULL;
1,686,247✔
1920
  pNew->writeTbs = NULL;
1,686,247✔
1921
  pNew->alterTbs = NULL;
1,686,247✔
1922
  pNew->readViews = NULL;
1,686,247✔
1923
  pNew->writeViews = NULL;
1,686,247✔
1924
  pNew->alterViews = NULL;
1,686,247✔
1925
  pNew->topics = NULL;
1,686,247✔
1926
  pNew->useDbs = NULL;
1,686,247✔
1927
  pNew->pIpWhiteListDual = NULL;
1,686,247✔
1928
  pNew->pTimeWhiteList = NULL;
1,686,247✔
1929

1930
  taosRLockLatch(&pUser->lock);
1,686,247✔
1931
  pNew->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,686,247✔
1932
  if (pNew->passwords == NULL) {
1,686,247✔
1933
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1934
    goto _OVER;
×
1935
  }
1936
  (void)memcpy(pNew->passwords, pUser->passwords, pUser->numOfPasswords * sizeof(SUserPassword));
1,686,247✔
1937

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

1954
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
1,686,247✔
1955
  if (pNew->pTimeWhiteList == NULL) {
1,686,247✔
1956
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1957
    goto _OVER;
×
1958
  }
1959

1960
_OVER:
1,686,247✔
1961
  taosRUnLockLatch(&pUser->lock);
1,686,247✔
1962
  TAOS_RETURN(code);
1,686,247✔
1963
}
1964

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

1991
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
1,641,325✔
1992
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
1,641,325✔
1993
  mndUserFreeObj(pUser);
1,641,325✔
1994
  return 0;
1,641,325✔
1995
}
1996

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

2008
  pOld->sessionPerUser = pNew->sessionPerUser;
945,148✔
2009
  pOld->connectTime = pNew->connectTime;
945,148✔
2010
  pOld->connectIdleTime = pNew->connectIdleTime;
945,148✔
2011
  pOld->callPerSession = pNew->callPerSession;
945,148✔
2012
  pOld->vnodePerCall = pNew->vnodePerCall;
945,148✔
2013
  pOld->failedLoginAttempts = pNew->failedLoginAttempts;
945,148✔
2014
  pOld->passwordLifeTime = pNew->passwordLifeTime;
945,148✔
2015
  pOld->passwordReuseTime = pNew->passwordReuseTime;
945,148✔
2016
  pOld->passwordReuseMax = pNew->passwordReuseMax;
945,148✔
2017
  pOld->passwordLockTime = pNew->passwordLockTime;
945,148✔
2018
  pOld->passwordGraceTime = pNew->passwordGraceTime;
945,148✔
2019
  pOld->inactiveAccountTime = pNew->inactiveAccountTime;
945,148✔
2020
  pOld->allowTokenNum = pNew->allowTokenNum;
945,148✔
2021

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

2037
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
945,148✔
2038
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
945,148✔
2039
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
945,148✔
2040
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
945,148✔
2041
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
945,148✔
2042

2043
  taosWUnLockLatch(&pOld->lock);
945,148✔
2044

2045
  return 0;
945,148✔
2046
}
2047

2048
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
81,891,846✔
2049
  int32_t code = 0;
81,891,846✔
2050
  SSdb   *pSdb = pMnode->pSdb;
81,891,846✔
2051

2052
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
81,892,109✔
2053
  if (*ppUser == NULL) {
81,892,346✔
2054
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
122,068✔
2055
      code = TSDB_CODE_MND_USER_NOT_EXIST;
122,068✔
2056
    } else {
2057
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2058
    }
2059
  }
2060
  TAOS_RETURN(code);
81,892,320✔
2061
}
2062

2063
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
81,931,030✔
2064
  SSdb *pSdb = pMnode->pSdb;
81,931,030✔
2065
  sdbRelease(pSdb, pUser);
81,931,030✔
2066
}
81,932,466✔
2067

2068

2069

2070
int32_t mndEncryptPass(char *pass, const char* salt, int8_t *algo) {
116,811✔
2071
  int32_t code = 0;
116,811✔
2072
  if (tsiEncryptPassAlgorithm != DND_CA_SM4) {
116,811✔
2073
    return 0;
116,091✔
2074
  }
2075

2076
  if (strlen(tsEncryptKey) == 0) {
720✔
2077
    return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
2078
  }
2079

2080
  if (salt[0] != 0) {
720✔
2081
    char passAndSalt[TSDB_PASSWORD_LEN - 1 + TSDB_PASSWORD_SALT_LEN];
720✔
2082
    (void)memcpy(passAndSalt, pass, TSDB_PASSWORD_LEN - 1);
720✔
2083
    (void)memcpy(passAndSalt + TSDB_PASSWORD_LEN - 1, salt, TSDB_PASSWORD_SALT_LEN);
720✔
2084
    taosEncryptPass_c((uint8_t *)passAndSalt, sizeof(passAndSalt), pass);
2085
  }
2086

2087
  unsigned char packetData[TSDB_PASSWORD_LEN] = {0};
720✔
2088
  SCryptOpts opts = {0};
720✔
2089
  opts.len = TSDB_PASSWORD_LEN;
720✔
2090
  opts.source = pass;
720✔
2091
  opts.result = packetData;
720✔
2092
  opts.unitLen = TSDB_PASSWORD_LEN;
720✔
2093
  tstrncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
720✔
2094
  int newLen = Builtin_CBC_Encrypt(&opts);
720✔
2095

2096
  memcpy(pass, packetData, newLen);
720✔
2097
  if (algo != NULL) {
720✔
2098
    *algo = DND_CA_SM4;
360✔
2099
  }
2100

2101
  return 0;
720✔
2102
}
2103

2104

2105

2106
static void generateSalt(char *salt, size_t len) {
102,995✔
2107
  const char* set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
102,995✔
2108
  int32_t     setLen = 62;
102,995✔
2109
  for (int32_t i = 0; i < len - 1; ++i) {
3,295,840✔
2110
    salt[i] = set[taosSafeRand() % setLen];
3,192,845✔
2111
  }
2112
  salt[len - 1] = 0;
102,995✔
2113
}
102,995✔
2114

2115

2116

2117
static int32_t addDefaultIpToTable(int8_t enableIpv6, SHashObj *pUniqueTab) {
1,078✔
2118
  int32_t code = 0;
1,078✔
2119
  int32_t lino = 0;
1,078✔
2120
  int32_t dummpy = 0;
1,078✔
2121

2122
  SIpRange ipv4 = {0}, ipv6 = {0};
1,078✔
2123
  code = createDefaultIp4Range(&ipv4);
1,078✔
2124
  TSDB_CHECK_CODE(code, lino, _error);
1,078✔
2125

2126
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummpy, sizeof(dummpy));
1,078✔
2127
  TSDB_CHECK_CODE(code, lino, _error);
1,078✔
2128

2129
  if (enableIpv6) {
1,078✔
2130
    code = createDefaultIp6Range(&ipv6);
×
2131
    TSDB_CHECK_CODE(code, lino, _error);
×
2132

2133
    code = taosHashPut(pUniqueTab, &ipv6, sizeof(ipv6), &dummpy, sizeof(dummpy));
×
2134
    TSDB_CHECK_CODE(code, lino, _error);
×
2135
  }
2136
_error:
1,078✔
2137
  if (code != 0) {
1,078✔
2138
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
2139
  }
2140
  return code;
1,078✔
2141
}
2142

2143
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
102,271✔
2144
  int32_t  code = 0;
102,271✔
2145
  int32_t  lino = 0;
102,271✔
2146
  SUserObj userObj = {0};
102,271✔
2147

2148
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
102,271✔
2149
  if (userObj.passwords == NULL) {
102,271✔
2150
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2151
  }
2152
  userObj.numOfPasswords = 1;
102,271✔
2153

2154
  if (pCreate->isImport == 1) {
102,271✔
2155
    memset(userObj.salt, 0, sizeof(userObj.salt));
×
2156
    memcpy(userObj.passwords[0].pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
2157
  } else {
2158
    generateSalt(userObj.salt, sizeof(userObj.salt));
102,271✔
2159
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.passwords[0].pass);
102,271✔
2160
    userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
102,271✔
2161
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino, _OVER);
102,271✔
2162
  }
2163
  userObj.passwords[0].setTime = taosGetTimestampSec();
102,271✔
2164

2165
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
102,271✔
2166
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
102,271✔
2167
  if (pCreate->totpseed[0] != 0) {
102,271✔
2168
    // TODO: generate totp seed
2169
  }
2170

2171
  userObj.createdTime = taosGetTimestampMs();
102,271✔
2172
  userObj.updateTime = userObj.createdTime;
102,271✔
2173
  userObj.superUser = 0;  // pCreate->superUser;
102,271✔
2174
  userObj.sysInfo = pCreate->sysInfo;
102,271✔
2175
  userObj.enable = pCreate->enable;
102,271✔
2176
  userObj.createdb = pCreate->createDb;
102,271✔
2177

2178
  userObj.changePass = pCreate->changepass;
102,271✔
2179
  userObj.sessionPerUser = pCreate->sessionPerUser;
102,271✔
2180
  userObj.connectTime = pCreate->connectTime;
102,271✔
2181
  userObj.connectIdleTime = pCreate->connectIdleTime;
102,271✔
2182
  userObj.callPerSession = pCreate->callPerSession;
102,271✔
2183
  userObj.vnodePerCall = pCreate->vnodePerCall;
102,271✔
2184
  userObj.failedLoginAttempts = pCreate->failedLoginAttempts;
102,271✔
2185
  userObj.passwordLifeTime = pCreate->passwordLifeTime;
102,271✔
2186
  userObj.passwordReuseTime = pCreate->passwordReuseTime;
102,271✔
2187
  userObj.passwordReuseMax = pCreate->passwordReuseMax;
102,271✔
2188
  userObj.passwordLockTime = pCreate->passwordLockTime;
102,271✔
2189
  userObj.passwordGraceTime = pCreate->passwordGraceTime;
102,271✔
2190
  userObj.inactiveAccountTime = pCreate->inactiveAccountTime;
102,271✔
2191
  userObj.allowTokenNum = pCreate->allowTokenNum;
102,271✔
2192

2193
  if (pCreate->numIpRanges == 0) {
102,271✔
2194
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&userObj.pIpWhiteListDual), &lino, _OVER);
101,193✔
2195
  } else {
2196
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
1,078✔
2197
    if (pUniqueTab == NULL) {
1,078✔
2198
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2199
    }
2200
    int32_t dummpy = 0;
1,078✔
2201
    
2202
    for (int i = 0; i < pCreate->numIpRanges; i++) {
2,874✔
2203
      SIpRange range = {0};
1,796✔
2204
      copyIpRange(&range, pCreate->pIpDualRanges + i);
1,796✔
2205
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
1,796✔
2206
        taosHashCleanup(pUniqueTab);
×
2207
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2208
      }
2209
    }
2210

2211
    code = addDefaultIpToTable(tsEnableIpv6, pUniqueTab);
1,078✔
2212
    if (code != 0) {
1,078✔
2213
      taosHashCleanup(pUniqueTab);
×
2214
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2215
    }
2216

2217
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,078✔
2218
      taosHashCleanup(pUniqueTab);
×
2219
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2220
    }
2221

2222
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
1,078✔
2223
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
1,078✔
2224
    if (p == NULL) {
1,078✔
2225
      taosHashCleanup(pUniqueTab);
×
2226
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2227
    }
2228

2229
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
1,078✔
2230
    for (int32_t i = 0; i < numOfRanges; i++) {
3,233✔
2231
      size_t    len = 0;
2,155✔
2232
      SIpRange *key = taosHashGetKey(pIter, &len);
2,155✔
2233
      memcpy(&p->pIpRanges[i], key, sizeof(SIpRange));
2,155✔
2234
      pIter = taosHashIterate(pUniqueTab, pIter);
2,155✔
2235
    }
2236

2237
    taosHashCleanup(pUniqueTab);
1,078✔
2238
    p->num = numOfRanges;
1,078✔
2239
    sortIpWhiteList(p);
1,078✔
2240
    userObj.pIpWhiteListDual = p;
1,078✔
2241
  }
2242

2243
  if (pCreate->numTimeRanges == 0) {
102,271✔
2244
    userObj.pTimeWhiteList = (SDateTimeWhiteList*)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
102,271✔
2245
    if (userObj.pTimeWhiteList == NULL) {
102,271✔
2246
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2247
    }
2248
  } else {
2249
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
2250
    if (pUniqueTab == NULL) {
×
2251
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2252
    }
2253
    int32_t dummpy = 0;
×
2254
    
2255
    for (int i = 0; i < pCreate->numIpRanges; i++) {
×
2256
      SDateTimeRange* src = pCreate->pTimeRanges + i;
×
2257
      SDateTimeWhiteListItem range = {0};
×
2258
      DateTimeRangeToWhiteListItem(&range, src);
×
2259

2260
      // no need to add expired range
2261
      if (isDateTimeWhiteListItemExpired(&range)) {
×
2262
        continue;
×
2263
      }
2264

2265
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
×
2266
        taosHashCleanup(pUniqueTab);
×
2267
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2268
      }
2269
    }
2270

2271
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_TIME_RANGE) {
×
2272
      taosHashCleanup(pUniqueTab);
×
2273
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
2274
    }
2275

2276
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
×
2277
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
2278
    if (p == NULL) {
×
2279
      taosHashCleanup(pUniqueTab);
×
2280
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2281
    }
2282

2283
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
×
2284
    for (int32_t i = 0; i < numOfRanges; i++) {
×
2285
      size_t    len = 0;
×
2286
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
×
2287
      memcpy(p->ranges + i, key, sizeof(SDateTimeWhiteListItem));
×
2288
      pIter = taosHashIterate(pUniqueTab, pIter);
×
2289
    }
2290

2291
    taosHashCleanup(pUniqueTab);
×
2292
    p->num = numOfRanges;
×
2293
    sortTimeWhiteList(p);
×
2294
    userObj.pTimeWhiteList = p;
×
2295
  }
2296

2297
  userObj.ipWhiteListVer = taosGetTimestampMs();
102,271✔
2298
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
102,271✔
2299

2300
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
102,271✔
2301
  if (pTrans == NULL) {
102,271✔
2302
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
2303
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2304
  }
2305
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
102,271✔
2306

2307
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
102,271✔
2308
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
102,271✔
2309
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
2310
    mndTransDrop(pTrans);
×
2311
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2312
  }
2313
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
102,271✔
2314

2315
  if (mndTransPrepare(pMnode, pTrans) != 0) {
102,271✔
2316
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2317
    mndTransDrop(pTrans);
×
2318
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2319
  }
2320

2321
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
102,271✔
2322
    mndTransDrop(pTrans);
×
2323
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2324
  }
2325

2326
  mndTransDrop(pTrans);
102,271✔
2327

2328
_OVER:
102,271✔
2329
  taosMemoryFree(userObj.passwords);
102,271✔
2330
  taosMemoryFree(userObj.pIpWhiteListDual);
102,271✔
2331
  taosMemoryFree(userObj.pTimeWhiteList);
102,271✔
2332
  TAOS_RETURN(code);
102,271✔
2333
}
2334

2335

2336

2337
static int32_t mndCheckPasswordFmt(const char *pwd) {
119,319✔
2338
  if (strcmp(pwd, "taosdata") == 0) {
119,319✔
2339
    return 0;
28,046✔
2340
  }
2341

2342
  if (tsEnableStrongPassword == 0) {
91,273✔
2343
    for (char c = *pwd; c != 0; c = *(++pwd)) {
98,640✔
2344
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
97,560✔
2345
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2346
      }
2347
    }
2348
    return 0;
1,080✔
2349
  }
2350

2351
  int32_t len = strlen(pwd);
90,193✔
2352
  if (len < TSDB_PASSWORD_MIN_LEN) {
90,193✔
2353
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
2354
  }
2355

2356
  if (len > TSDB_PASSWORD_MAX_LEN) {
90,193✔
2357
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
2358
  }
2359

2360
  int32_t upper = 0, lower = 0, number = 0, special = 0;
90,193✔
2361
  for (int32_t i = 0; i < len; ++i) {
1,239,791✔
2362
    if (taosIsBigChar(pwd[i])) {
1,151,752✔
2363
      upper = 1;
113,682✔
2364
    } else if (taosIsSmallChar(pwd[i])) {
1,038,070✔
2365
      lower = 1;
573,050✔
2366
    } else if (taosIsNumberChar(pwd[i])) {
465,020✔
2367
      number = 1;
257,868✔
2368
    } else if (taosIsSpecialChar(pwd[i])) {
207,152✔
2369
      special = 1;
204,998✔
2370
    } else {
2371
      return TSDB_CODE_MND_INVALID_PASS_FORMAT;
2,154✔
2372
    }
2373
  }
2374

2375
  if (upper + lower + number + special < 3) {
88,039✔
2376
    return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2377
  }
2378

2379
  return 0;
88,039✔
2380
}
2381

2382

2383

2384
static int32_t mndProcessGetUserDateTimeWhiteListReq(SRpcMsg *pReq) {
×
2385
  SMnode              *pMnode = pReq->info.node;
×
2386
  int32_t              code = 0;
×
2387
  int32_t              lino = 0;
×
2388
  int32_t              contLen = 0;
×
2389
  void                *pRsp = NULL;
×
2390
  SUserObj            *pUser = NULL;
×
2391
  SGetUserWhiteListReq wlReq = {0};
×
2392
  SUserDateTimeWhiteList wlRsp = {0};
×
2393

2394
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
×
2395
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2396
  }
2397
  mTrace("user: %s, start to get date time whitelist", wlReq.user);
×
2398

2399
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
×
2400
  if (pUser == NULL) {
×
2401
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2402
  }
2403

2404
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
2405

2406
  contLen = tSerializeSUserDateTimeWhiteList(NULL, 0, &wlRsp);
×
2407
  if (contLen < 0) {
×
2408
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2409
  }
2410
  pRsp = rpcMallocCont(contLen);
×
2411
  if (pRsp == NULL) {
×
2412
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2413
  }
2414
  
2415
  contLen = tSerializeSUserDateTimeWhiteList(pRsp, contLen, &wlRsp);
×
2416
  if (contLen < 0) {
×
2417
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2418
  }
2419

2420
_OVER:
×
2421
  mndReleaseUser(pMnode, pUser);
×
2422
  tFreeSUserDateTimeWhiteList(&wlRsp);
×
2423
  if (code < 0) {
×
2424
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
2425
    rpcFreeCont(pRsp);
×
2426
    pRsp = NULL;
×
2427
    contLen = 0;
×
2428
  }
2429
  pReq->code = code;
×
2430
  pReq->info.rsp = pRsp;
×
2431
  pReq->info.rspLen = contLen;
×
2432

2433
  TAOS_RETURN(code);
×
2434
  return 0;
2435
}
2436

2437

2438

2439
static int32_t buildRetrieveDateTimeWhiteListRsp(SRetrieveDateTimeWhiteListRsp *pRsp) {
1,862✔
2440
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,862✔
2441
  
2442
  int32_t count = taosHashGetSize(userCache.users);
1,862✔
2443
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
1,862✔
2444
  if (pRsp->pUsers == NULL) {
1,862✔
2445
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
2446
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2447
  }
2448

2449
  count = 0;
1,862✔
2450
  void   *pIter = taosHashIterate(userCache.users, NULL);
1,862✔
2451
  while (pIter) {
3,724✔
2452
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
1,862✔
2453
    if (wl == NULL || wl->num <= 0) {
1,862✔
2454
      pIter = taosHashIterate(userCache.users, pIter);
1,862✔
2455
      continue;
1,862✔
2456
    }
2457

2458
    SUserDateTimeWhiteList *pUser = &pRsp->pUsers[count];
×
2459
    pUser->ver = userCache.verTime;
×
2460

2461
    size_t klen;
×
2462
    char  *key = taosHashGetKey(pIter, &klen);
×
2463
    (void)memcpy(pUser->user, key, klen);
×
2464

2465
    pUser->numWhiteLists = wl->num;
×
2466
    pUser->pWhiteLists = taosMemoryCalloc(wl->num, sizeof(SDateTimeWhiteListItem));
×
2467
    if (pUser->pWhiteLists == NULL) {
×
2468
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
2469
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2470
    }
2471

2472
    (void)memcpy(pUser->pWhiteLists, wl->ranges, wl->num * sizeof(SDateTimeWhiteListItem));
×
2473
    count++;
×
2474
    pIter = taosHashIterate(userCache.users, pIter);
×
2475
  }
2476

2477
  pRsp->numOfUser = count;
1,862✔
2478
  pRsp->ver = userCache.verTime;
1,862✔
2479
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,862✔
2480
  TAOS_RETURN(0);
1,862✔
2481
}
2482

2483

2484

2485
static int32_t mndProcessRetrieveDateTimeWhiteListReq(SRpcMsg *pReq) {
1,862✔
2486
  int32_t        code = 0;
1,862✔
2487
  int32_t        lino = 0;
1,862✔
2488
  int32_t        len = 0;
1,862✔
2489
  void          *pRsp = NULL;
1,862✔
2490
  SRetrieveDateTimeWhiteListRsp wlRsp = {0};
1,862✔
2491

2492
  // impl later
2493
  SRetrieveWhiteListReq req = {0};
1,862✔
2494
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,862✔
2495
    code = TSDB_CODE_INVALID_MSG;
×
2496
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2497
  }
2498

2499
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
1,862✔
2500

2501
  len = tSerializeSRetrieveDateTimeWhiteListRsp(NULL, 0, &wlRsp);
1,862✔
2502
  if (len < 0) {
1,862✔
2503
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2504
  }
2505

2506
  pRsp = rpcMallocCont(len);
1,862✔
2507
  if (!pRsp) {
1,862✔
2508
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2509
  }
2510
  len = tSerializeSRetrieveDateTimeWhiteListRsp(pRsp, len, &wlRsp);
1,862✔
2511
  if (len < 0) {
1,862✔
2512
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2513
  }
2514

2515
_OVER:
1,862✔
2516
  if (code < 0) {
1,862✔
2517
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2518
    rpcFreeCont(pRsp);
×
2519
    pRsp = NULL;
×
2520
    len = 0;
×
2521
  }
2522
  pReq->code = code;
1,862✔
2523
  pReq->info.rsp = pRsp;
1,862✔
2524
  pReq->info.rspLen = len;
1,862✔
2525

2526
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
1,862✔
2527
  TAOS_RETURN(code);
1,862✔
2528
}
2529

2530

2531

2532
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
104,419✔
2533
  SMnode        *pMnode = pReq->info.node;
104,419✔
2534
  int32_t        code = 0;
104,419✔
2535
  int32_t        lino = 0;
104,419✔
2536
  SUserObj      *pUser = NULL;
104,419✔
2537
  SUserObj      *pOperUser = NULL;
104,419✔
2538
  SCreateUserReq createReq = {0};
104,419✔
2539

2540
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
104,419✔
2541
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2542
  }
2543

2544
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
104,419✔
2545

2546
#ifndef TD_ENTERPRISE
2547
  if (createReq.isImport == 1) {
2548
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
2549
  }
2550
#endif
2551

2552
  if (createReq.isImport != 1) {
104,419✔
2553
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
104,419✔
2554
  } else if (strcmp(pReq->info.conn.user, "root") != 0) {
×
2555
    mError("The operation is not permitted, user:%s", pReq->info.conn.user);
×
2556
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
2557
  }
2558

2559
  if (createReq.user[0] == 0) {
104,419✔
2560
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2561
  }
2562

2563
  if (createReq.isImport != 1) {
104,419✔
2564
    code = mndCheckPasswordFmt(createReq.pass);
104,419✔
2565
    TAOS_CHECK_GOTO(code, &lino, _OVER);
104,419✔
2566
  }
2567

2568
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
102,985✔
2569
  if (pUser != NULL) {
102,985✔
2570
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
714✔
2571
  }
2572

2573
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
102,271✔
2574
  if (pOperUser == NULL) {
102,271✔
2575
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2576
  }
2577

2578
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
102,271✔
2579

2580
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
102,271✔
2581
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
102,271✔
2582

2583
  char detail[1000] = {0};
102,271✔
2584
  (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
102,271✔
2585
                  createReq.superUser, createReq.sysInfo);
102,271✔
2586
  char operation[15] = {0};
102,271✔
2587
  if (createReq.isImport == 1) {
102,271✔
2588
    tstrncpy(operation, "importUser", sizeof(operation));
×
2589
  } else {
2590
    tstrncpy(operation, "createUser", sizeof(operation));
102,271✔
2591
  }
2592

2593
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
102,271✔
2594

2595
_OVER:
104,419✔
2596
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExisting) {
104,419✔
2597
    code = 0;
×
2598
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
104,419✔
2599
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
2,148✔
2600
  }
2601

2602
  mndReleaseUser(pMnode, pUser);
104,419✔
2603
  mndReleaseUser(pMnode, pOperUser);
104,419✔
2604
  tFreeSCreateUserReq(&createReq);
104,419✔
2605

2606
  TAOS_RETURN(code);
104,419✔
2607
}
2608

2609

2610

2611
static int32_t mndProcessGetUserIpWhiteListReq(SRpcMsg *pReq) {
143,602✔
2612
  SMnode              *pMnode = pReq->info.node;
143,602✔
2613
  int32_t              code = 0;
143,602✔
2614
  int32_t              lino = 0;
143,602✔
2615
  int32_t              contLen = 0;
143,602✔
2616
  void                *pRsp = NULL;
143,602✔
2617
  SUserObj            *pUser = NULL;
143,602✔
2618
  SGetUserWhiteListReq wlReq = {0};
143,602✔
2619
  SGetUserIpWhiteListRsp wlRsp = {0};
143,602✔
2620

2621
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
143,602✔
2622
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserIpWhiteListRsp * pRsp) = NULL;
143,602✔
2623

2624
  if (pReq->msgType == TDMT_MND_GET_USER_IP_WHITELIST_DUAL) {
143,602✔
2625
    serialFn = tSerializeSGetUserIpWhiteListDualRsp;
143,602✔
2626
    setRspFn = mndSetUserIpWhiteListDualRsp;
143,602✔
2627
  } else {
2628
    serialFn = tSerializeSGetUserIpWhiteListRsp;
×
2629
    setRspFn = mndSetUserIpWhiteListRsp;
×
2630
  }
2631
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
143,602✔
2632
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2633
  }
2634
  mTrace("user: %s, start to get ip whitelist", wlReq.user);
143,602✔
2635

2636
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
143,602✔
2637
  if (pUser == NULL) {
143,602✔
2638
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2639
  }
2640

2641
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
143,602✔
2642
  contLen = serialFn(NULL, 0, &wlRsp);
143,339✔
2643
  if (contLen < 0) {
143,602✔
2644
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2645
  }
2646
  pRsp = rpcMallocCont(contLen);
143,602✔
2647
  if (pRsp == NULL) {
143,602✔
2648
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2649
  }
2650

2651
  contLen = serialFn(pRsp, contLen, &wlRsp);
143,602✔
2652
  if (contLen < 0) {
143,602✔
2653
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2654
  }
2655

2656
_OVER:
143,602✔
2657
  mndReleaseUser(pMnode, pUser);
143,602✔
2658
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
143,602✔
2659
  if (code < 0) {
143,602✔
2660
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
2661
    rpcFreeCont(pRsp);
×
2662
    pRsp = NULL;
×
2663
    contLen = 0;
×
2664
  }
2665
  pReq->code = code;
143,602✔
2666
  pReq->info.rsp = pRsp;
143,602✔
2667
  pReq->info.rspLen = contLen;
143,602✔
2668

2669
  TAOS_RETURN(code);
143,602✔
2670
}
2671

2672

2673

2674
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
1,862✔
2675
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,862✔
2676

2677
  int32_t count = taosHashGetSize(userCache.users);
1,862✔
2678
  pUpdate->pUserIpWhite = taosMemoryCalloc(count, sizeof(SUpdateUserIpWhite));
1,862✔
2679
  if (pUpdate->pUserIpWhite == NULL) {
1,862✔
2680
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
2681
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2682
  }
2683

2684
  count = 0;
1,862✔
2685
  void   *pIter = taosHashIterate(userCache.users, NULL);
1,862✔
2686
  while (pIter) {
3,724✔
2687
    SIpWhiteListDual   *wl = (*(SCachedUserInfo**)pIter)->wlIp;
1,862✔
2688
    if (wl == NULL || wl->num <= 0) {
1,862✔
2689
      pIter = taosHashIterate(userCache.users, pIter);
×
2690
      continue;
×
2691
    }
2692

2693
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
1,862✔
2694
    pUser->ver = userCache.verIp;
1,862✔
2695

2696
    size_t klen;
1,862✔
2697
    char  *key = taosHashGetKey(pIter, &klen);
1,862✔
2698
    (void)memcpy(pUser->user, key, klen);
1,862✔
2699

2700
    pUser->numOfRange = wl->num;
1,862✔
2701
    pUser->pIpRanges = taosMemoryCalloc(wl->num, sizeof(SIpRange));
1,862✔
2702
    if (pUser->pIpRanges == NULL) {
1,862✔
2703
      (void)taosThreadRwlockUnlock(&userCache.rw);
×
2704
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2705
    }
2706

2707
    (void)memcpy(pUser->pIpRanges, wl->pIpRanges, wl->num * sizeof(SIpRange));
1,862✔
2708
    count++;
1,862✔
2709
    pIter = taosHashIterate(userCache.users, pIter);
1,862✔
2710
  }
2711

2712
  pUpdate->numOfUser = count;
1,862✔
2713
  pUpdate->ver = userCache.verIp;
1,862✔
2714
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,862✔
2715
  TAOS_RETURN(0);
1,862✔
2716
}
2717

2718

2719

2720
int32_t mndProcessRetrieveIpWhiteListReq(SRpcMsg *pReq) {
1,862✔
2721
  int32_t        code = 0;
1,862✔
2722
  int32_t        lino = 0;
1,862✔
2723
  int32_t        len = 0;
1,862✔
2724
  void          *pRsp = NULL;
1,862✔
2725
  SUpdateIpWhite ipWhite = {0};
1,862✔
2726

2727
  // impl later
2728
  SRetrieveWhiteListReq req = {0};
1,862✔
2729
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,862✔
2730
    code = TSDB_CODE_INVALID_MSG;
×
2731
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2732
  }
2733

2734
  int32_t (*fn)(void *, int32_t, SUpdateIpWhite *) = NULL;
1,862✔
2735
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST) {
1,862✔
2736
    fn = tSerializeSUpdateIpWhite;
×
2737
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL) {
1,862✔
2738
    fn = tSerializeSUpdateIpWhiteDual;
1,862✔
2739
  }
2740

2741
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
1,862✔
2742

2743
  len = fn(NULL, 0, &ipWhite);
1,862✔
2744
  if (len < 0) {
1,862✔
2745
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2746
  }
2747

2748
  pRsp = rpcMallocCont(len);
1,862✔
2749
  if (!pRsp) {
1,862✔
2750
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2751
  }
2752
  len = fn(pRsp, len, &ipWhite);
1,862✔
2753
  if (len < 0) {
1,862✔
2754
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2755
  }
2756

2757
_OVER:
1,862✔
2758
  if (code < 0) {
1,862✔
2759
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2760
    rpcFreeCont(pRsp);
×
2761
    pRsp = NULL;
×
2762
    len = 0;
×
2763
  }
2764
  pReq->code = code;
1,862✔
2765
  pReq->info.rsp = pRsp;
1,862✔
2766
  pReq->info.rspLen = len;
1,862✔
2767

2768
  tFreeSUpdateIpWhiteReq(&ipWhite);
1,862✔
2769
  TAOS_RETURN(code);
1,862✔
2770
}
2771

2772

2773

2774
void mndUpdateUser(SMnode *pMnode, SUserObj *pUser, SRpcMsg *pReq) {
×
2775
  int32_t code = 0;
×
2776
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "update-user");
×
2777
  if (pTrans == NULL) {
×
2778
    mError("user:%s, failed to update since %s", pUser->user, terrstr());
×
2779
    return;
×
2780
  }
2781
  mInfo("trans:%d, used to update user:%s", pTrans->id, pUser->user);
×
2782

2783
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
×
2784
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
2785
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2786
    mndTransDrop(pTrans);
×
2787
    return;
×
2788
  }
2789
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
2790
  if (code < 0) {
×
2791
    mndTransDrop(pTrans);
×
2792
    return;
×
2793
  }
2794

2795
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
2796
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2797
    mndTransDrop(pTrans);
×
2798
    return;
×
2799
  }
2800

2801
  mndTransDrop(pTrans);
×
2802
}
2803

2804

2805

2806
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
914,950✔
2807
  int32_t code = 0;
914,950✔
2808
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
914,950✔
2809
  if (pTrans == NULL) {
914,950✔
2810
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
×
2811
    TAOS_RETURN(terrno);
×
2812
  }
2813
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
914,950✔
2814

2815
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
914,950✔
2816
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
914,950✔
2817
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2818
    mndTransDrop(pTrans);
×
2819
    TAOS_RETURN(terrno);
×
2820
  }
2821
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
914,950✔
2822
  if (code < 0) {
914,950✔
2823
    mndTransDrop(pTrans);
×
2824
    TAOS_RETURN(code);
×
2825
  }
2826

2827
  if (mndTransPrepare(pMnode, pTrans) != 0) {
914,950✔
2828
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2829
    mndTransDrop(pTrans);
×
2830
    TAOS_RETURN(terrno);
×
2831
  }
2832
  if ((code = userCacheUpdateWhiteList(pMnode, pNew)) != 0) {
914,950✔
2833
    mndTransDrop(pTrans);
×
2834
    TAOS_RETURN(code);
×
2835
  }
2836
  mndTransDrop(pTrans);
914,950✔
2837
  return 0;
914,950✔
2838
}
2839

2840
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
13,301,937✔
2841
  int32_t code = 0;
13,301,937✔
2842

2843
  *ppNew =
13,301,623✔
2844
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
13,301,937✔
2845
  if (*ppNew == NULL) {
13,301,623✔
2846
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2847
    TAOS_RETURN(code);
×
2848
  }
2849

2850
  char *db = taosHashIterate(pOld, NULL);
13,301,623✔
2851
  while (db != NULL) {
14,450,566✔
2852
    int32_t len = strlen(db) + 1;
1,148,629✔
2853
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
1,148,629✔
2854
      taosHashCancelIterate(pOld, db);
×
2855
      taosHashCleanup(*ppNew);
×
2856
      TAOS_RETURN(code);
×
2857
    }
2858
    db = taosHashIterate(pOld, db);
1,148,629✔
2859
  }
2860

2861
  TAOS_RETURN(code);
13,301,937✔
2862
}
2863

2864
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
11,615,690✔
2865

2866
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
1,686,247✔
2867

2868
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
445,935✔
2869
                                  SSdb *pSdb) {
2870
  void *pIter = NULL;
445,935✔
2871
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
445,935✔
2872

2873
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
445,935✔
2874
  int32_t len = strlen(tbFName) + 1;
445,935✔
2875

2876
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
519,931✔
2877
    char *value = taosHashGet(hash, tbFName, len);
73,996✔
2878
    if (value != NULL) {
73,996✔
2879
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2880
    }
2881

2882
    int32_t condLen = alterReq->tagCondLen;
73,996✔
2883
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
73,996✔
2884
  } else {
2885
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
371,939✔
2886
  }
2887

2888
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
445,935✔
2889
  int32_t  ref = 1;
445,935✔
2890
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
445,935✔
2891
  if (NULL != currRef) {
445,935✔
2892
    ref = (*currRef) + 1;
259,533✔
2893
  }
2894
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
445,935✔
2895

2896
  TAOS_RETURN(0);
445,935✔
2897
}
2898

2899
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
408,242✔
2900
                                        SSdb *pSdb) {
2901
  void *pIter = NULL;
408,242✔
2902
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
408,242✔
2903
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
408,242✔
2904
  int32_t len = strlen(tbFName) + 1;
408,242✔
2905

2906
  if (taosHashRemove(hash, tbFName, len) != 0) {
408,242✔
2907
    TAOS_RETURN(0);  // not found
1,365✔
2908
  }
2909

2910
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
406,877✔
2911
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
406,877✔
2912
  if (NULL == currRef) {
406,877✔
2913
    return 0;
×
2914
  }
2915

2916
  if (1 == *currRef) {
406,877✔
2917
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
172,862✔
2918
      TAOS_RETURN(0);  // not found
×
2919
    }
2920
    return 0;
172,862✔
2921
  }
2922
  int32_t ref = (*currRef) - 1;
234,015✔
2923
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
234,015✔
2924

2925
  return 0;
234,015✔
2926
}
2927

2928
static char *mndUserAuditTypeStr(int32_t type) {
×
2929
  #if 0
2930
  if (type == TSDB_ALTER_USER_PASSWD) {
2931
    return "changePassword";
2932
  }
2933
  if (type == TSDB_ALTER_USER_SUPERUSER) {
2934
    return "changeSuperUser";
2935
  }
2936
  if (type == TSDB_ALTER_USER_ENABLE) {
2937
    return "enableUser";
2938
  }
2939
  if (type == TSDB_ALTER_USER_SYSINFO) {
2940
    return "userSysInfo";
2941
  }
2942
  if (type == TSDB_ALTER_USER_CREATEDB) {
2943
    return "userCreateDB";
2944
  }
2945
    #endif
2946
  return "error";
×
2947
}
2948

2949
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
879,850✔
2950
  SSdb   *pSdb = pMnode->pSdb;
879,850✔
2951
  void   *pIter = NULL;
879,850✔
2952
  int32_t code = 0;
879,850✔
2953
  int32_t lino = 0;
879,850✔
2954

2955
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
879,850✔
2956
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
753,037✔
2957
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
126,813✔
2958
      int32_t len = strlen(pAlterReq->objname) + 1;
123,234✔
2959
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
123,234✔
2960
      if (pDb == NULL) {
123,234✔
2961
        mndReleaseDb(pMnode, pDb);
2,839✔
2962
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
2,839✔
2963
      }
2964
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
120,395✔
2965
          0) {
2966
        mndReleaseDb(pMnode, pDb);
×
2967
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2968
      }
2969
      mndReleaseDb(pMnode, pDb);
120,395✔
2970
    } else {
2971
      while (1) {
5,739✔
2972
        SDbObj *pDb = NULL;
9,318✔
2973
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
9,318✔
2974
        if (pIter == NULL) break;
9,318✔
2975
        int32_t len = strlen(pDb->name) + 1;
5,739✔
2976
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,739✔
2977
          sdbRelease(pSdb, pDb);
×
2978
          sdbCancelFetch(pSdb, pIter);
×
2979
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2980
        }
2981
        sdbRelease(pSdb, pDb);
5,739✔
2982
      }
2983
    }
2984
  }
2985

2986
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
877,011✔
2987
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
754,538✔
2988
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
122,473✔
2989
      int32_t len = strlen(pAlterReq->objname) + 1;
119,251✔
2990
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
119,251✔
2991
      if (pDb == NULL) {
119,251✔
2992
        mndReleaseDb(pMnode, pDb);
360✔
2993
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
360✔
2994
      }
2995
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
118,891✔
2996
          0) {
2997
        mndReleaseDb(pMnode, pDb);
×
2998
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2999
      }
3000
      mndReleaseDb(pMnode, pDb);
118,891✔
3001
    } else {
3002
      while (1) {
5,382✔
3003
        SDbObj *pDb = NULL;
8,604✔
3004
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
8,604✔
3005
        if (pIter == NULL) break;
8,604✔
3006
        int32_t len = strlen(pDb->name) + 1;
5,382✔
3007
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,382✔
3008
          sdbRelease(pSdb, pDb);
×
3009
          sdbCancelFetch(pSdb, pIter);
×
3010
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3011
        }
3012
        sdbRelease(pSdb, pDb);
5,382✔
3013
      }
3014
    }
3015
  }
3016

3017
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,651✔
3018
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
763,650✔
3019
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
113,001✔
3020
      int32_t len = strlen(pAlterReq->objname) + 1;
109,422✔
3021
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
109,422✔
3022
      if (pDb == NULL) {
109,422✔
3023
        mndReleaseDb(pMnode, pDb);
357✔
3024
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
357✔
3025
      }
3026
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
109,065✔
3027
      if (code < 0) {
109,065✔
3028
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3029
      }
3030
      mndReleaseDb(pMnode, pDb);
109,065✔
3031
    } else {
3032
      taosHashClear(pNewUser->readDbs);
3,579✔
3033
    }
3034
  }
3035

3036
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3037
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
763,603✔
3038
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
112,691✔
3039
      int32_t len = strlen(pAlterReq->objname) + 1;
108,755✔
3040
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
108,755✔
3041
      if (pDb == NULL) {
108,755✔
3042
        mndReleaseDb(pMnode, pDb);
×
3043
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
3044
      }
3045
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
108,755✔
3046
      if (code < 0) {
108,755✔
3047
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3048
      }
3049
      mndReleaseDb(pMnode, pDb);
108,755✔
3050
    } else {
3051
      taosHashClear(pNewUser->writeDbs);
3,936✔
3052
    }
3053
  }
3054

3055
  SHashObj *pReadTbs = pNewUser->readTbs;
876,294✔
3056
  SHashObj *pWriteTbs = pNewUser->writeTbs;
876,294✔
3057
  SHashObj *pAlterTbs = pNewUser->alterTbs;
876,294✔
3058

3059
#ifdef TD_ENTERPRISE
3060
  if (pAlterReq->isView) {
876,294✔
3061
    pReadTbs = pNewUser->readViews;
14,117✔
3062
    pWriteTbs = pNewUser->writeViews;
14,117✔
3063
    pAlterTbs = pNewUser->alterViews;
14,117✔
3064
  }
3065
#endif
3066

3067
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3068
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
701,272✔
3069
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
175,022✔
3070
  }
3071

3072
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3073
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
698,770✔
3074
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
177,524✔
3075
  }
3076

3077
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3078
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
782,905✔
3079
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
93,389✔
3080
  }
3081

3082
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3083
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
714,887✔
3084
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
161,407✔
3085
  }
3086

3087
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3088
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
710,460✔
3089
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
165,834✔
3090
  }
3091

3092
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,294✔
3093
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
795,293✔
3094
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
81,001✔
3095
  }
3096

3097
#ifdef USE_TOPIC
3098
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
876,294✔
3099
    int32_t      len = strlen(pAlterReq->objname) + 1;
6,238✔
3100
    SMqTopicObj *pTopic = NULL;
6,238✔
3101
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
6,238✔
3102
      mndReleaseTopic(pMnode, pTopic);
×
3103
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3104
    }
3105
    if ((code = taosHashPut(pNewUser->topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN)) != 0) {
6,238✔
3106
      mndReleaseTopic(pMnode, pTopic);
×
3107
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3108
    }
3109
    mndReleaseTopic(pMnode, pTopic);
6,238✔
3110
  }
3111

3112
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
876,294✔
3113
    int32_t      len = strlen(pAlterReq->objname) + 1;
4,086✔
3114
    SMqTopicObj *pTopic = NULL;
4,086✔
3115
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
4,086✔
3116
      mndReleaseTopic(pMnode, pTopic);
359✔
3117
      TAOS_CHECK_GOTO(code, &lino, _OVER);
359✔
3118
    }
3119
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
3,727✔
3120
    if (code < 0) {
3,727✔
3121
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
3122
    }
3123
    mndReleaseTopic(pMnode, pTopic);
3,727✔
3124
  }
3125
#endif
3126
_OVER:
879,850✔
3127
  if (code < 0) {
879,850✔
3128
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
3,915✔
3129
  }
3130
  TAOS_RETURN(code);
879,850✔
3131
}
3132

3133
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
947,068✔
3134
  SMnode       *pMnode = pReq->info.node;
947,068✔
3135
  SSdb         *pSdb = pMnode->pSdb;
947,068✔
3136
  void         *pIter = NULL;
947,068✔
3137
  int32_t       code = 0;
947,068✔
3138
  int32_t       lino = 0;
947,068✔
3139
  SUserObj     *pUser = NULL;
947,068✔
3140
  SUserObj     *pOperUser = NULL;
947,068✔
3141
  SUserObj      newUser = {0};
947,068✔
3142
  SAlterUserReq alterReq = {0};
947,068✔
3143
  TAOS_CHECK_GOTO(tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq), &lino, _OVER);
947,068✔
3144

3145
  mInfo("user:%s, start to alter", alterReq.user);
947,068✔
3146

3147
  if (alterReq.user[0] == 0) {
947,068✔
3148
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3149
  }
3150

3151
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
947,068✔
3152

3153
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
938,896✔
3154
  if (pOperUser == NULL) {
938,896✔
3155
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3156
  }
3157

3158
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
938,896✔
3159
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
919,947✔
3160

3161
  if (alterReq.hasPassword) {
919,947✔
3162
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(alterReq.pass), &lino, _OVER);
14,900✔
3163
    if (newUser.salt[0] == 0) {
14,180✔
3164
      generateSalt(newUser.salt, sizeof(newUser.salt));
724✔
3165
    }
3166
    char pass[TSDB_PASSWORD_LEN] = {0};
14,180✔
3167
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
14,180✔
3168
    pass[sizeof(pass) - 1] = 0;
14,180✔
3169
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
14,180✔
3170

3171
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
23,802✔
3172
      for(int32_t i = 0; i < newUser.numOfPasswords; ++i) {
29,686✔
3173
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
20,064✔
3174
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
362✔
3175
        }
3176
      }
3177
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
9,622✔
3178
      if (passwords == NULL) {
9,622✔
3179
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3180
      }
3181
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
9,622✔
3182
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
9,622✔
3183
      passwords[0].setTime = taosGetTimestampSec();
9,622✔
3184
      taosMemoryFree(newUser.passwords);
9,622✔
3185
      newUser.passwords = passwords;
9,622✔
3186
      ++newUser.numOfPasswords;
9,622✔
3187
      ++newUser.passVersion;
9,622✔
3188
      if (strcmp(newUser.user, pOperUser->user) == 0) {
9,622✔
3189
        // if user change own password, set changePass to 2 so that user won't be
3190
        // forced to change password at next login
3191
        newUser.changePass = 2;
6,382✔
3192
      }
3193
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
4,196✔
3194
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
2,861✔
3195
      newUser.passwords[0].setTime = taosGetTimestampSec();
2,861✔
3196
      ++newUser.passVersion;
2,861✔
3197
      if (strcmp(newUser.user, pOperUser->user) == 0) {
2,861✔
3198
        newUser.changePass = 2;
356✔
3199
      }
3200
    }
3201
  }
3202

3203
  if (alterReq.hasTotpseed) {
918,865✔
3204
    // TODO: totp seed to secret
3205
  }
3206

3207
  if (alterReq.hasEnable) {
918,865✔
3208
    newUser.enable = alterReq.enable; // lock or unlock user manually
2,415✔
3209
    if (newUser.enable) {
2,415✔
3210
      // reset login info to allow login immediately
3211
      userCacheResetLoginInfo(newUser.user);
1,341✔
3212
    }
3213
  }
3214

3215
  if (alterReq.hasSysinfo) newUser.sysInfo = alterReq.sysinfo;
918,865✔
3216
  if (alterReq.hasCreatedb) newUser.createdb = alterReq.createdb;
918,865✔
3217
  if (alterReq.hasChangepass) newUser.changePass = alterReq.changepass;
918,865✔
3218
  if (alterReq.hasSessionPerUser) newUser.sessionPerUser = alterReq.sessionPerUser;
918,865✔
3219
  if (alterReq.hasConnectTime) newUser.connectTime = alterReq.connectTime;
918,865✔
3220
  if (alterReq.hasConnectIdleTime) newUser.connectIdleTime = alterReq.connectIdleTime;
918,865✔
3221
  if (alterReq.hasCallPerSession) newUser.callPerSession = alterReq.callPerSession;
918,865✔
3222
  if (alterReq.hasVnodePerCall) newUser.vnodePerCall = alterReq.vnodePerCall;
918,865✔
3223
  if (alterReq.hasFailedLoginAttempts) newUser.failedLoginAttempts = alterReq.failedLoginAttempts;
918,865✔
3224
  if (alterReq.hasPasswordLifeTime) newUser.passwordLifeTime = alterReq.passwordLifeTime;
918,865✔
3225
  if (alterReq.hasPasswordReuseTime) newUser.passwordReuseTime = alterReq.passwordReuseTime;
918,865✔
3226
  if (alterReq.hasPasswordReuseMax) newUser.passwordReuseMax = alterReq.passwordReuseMax;
918,865✔
3227
  if (alterReq.hasPasswordLockTime) newUser.passwordLockTime = alterReq.passwordLockTime;
918,865✔
3228
  if (alterReq.hasPasswordGraceTime) newUser.passwordGraceTime = alterReq.passwordGraceTime;
918,865✔
3229
  if (alterReq.hasInactiveAccountTime) newUser.inactiveAccountTime = alterReq.inactiveAccountTime;
918,865✔
3230
  if (alterReq.hasAllowTokenNum) newUser.allowTokenNum = alterReq.allowTokenNum;
918,865✔
3231

3232

3233
  if (alterReq.numDropIpRanges > 0 || alterReq.numIpRanges > 0) {
918,865✔
3234
    int32_t dummy = 0;
718✔
3235

3236
    // put previous ip whitelist into hash table
3237
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
718✔
3238
    if (m == NULL) {
718✔
3239
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3240
    }
3241

3242
    for (int32_t i = 0; i < newUser.pIpWhiteListDual->num; i++) {
2,513✔
3243
      SIpRange range;
1,795✔
3244
      copyIpRange(&range, newUser.pIpWhiteListDual->pIpRanges + i);
1,795✔
3245
      code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
1,795✔
3246
      if (code != 0) {
1,795✔
3247
        taosHashCleanup(m);
×
3248
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3249
      }
3250
    }
3251

3252
    if (alterReq.numDropIpRanges > 0) {
718✔
3253
      for (int32_t i = 0; i < alterReq.numDropIpRanges; i++) {
718✔
3254
        if (taosHashGetSize(m) == 0) {
359✔
3255
          break;
×
3256
        }
3257

3258
        SIpRange range;
359✔
3259
        copyIpRange(&range, alterReq.pDropIpRanges + i);
359✔
3260

3261
        // for white list, drop default ip ranges is allowed, otherwise, we can never
3262
        // convert white list to black list.
3263

3264
        code = taosHashRemove(m, &range, sizeof(range));
359✔
3265
        if (code == TSDB_CODE_NOT_FOUND) {
359✔
3266
          // treat not exist as success
3267
          code = 0;
359✔
3268
        }
3269
        if (code != 0) {
359✔
3270
          taosHashCleanup(m);
×
3271
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3272
        }
3273
      }
3274
    }
3275

3276
    if (alterReq.numIpRanges > 0) {
718✔
3277
      for (int32_t i = 0; i < alterReq.numIpRanges; i++) {
718✔
3278
        SIpRange range;
359✔
3279
        copyIpRange(&range, alterReq.pIpRanges + i);
359✔
3280
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
359✔
3281
        if (code != 0) {
359✔
3282
          taosHashCleanup(m);
×
3283
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3284
        }
3285
      }
3286
    }
3287

3288
    int32_t numOfRanges = taosHashGetSize(m);
718✔
3289
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
718✔
3290
      taosHashCleanup(m);
×
3291
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
3292
    }
3293

3294
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
718✔
3295
    if (p == NULL) {
718✔
3296
      taosHashCleanup(m);
×
3297
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3298
    }
3299

3300
    void *pIter = taosHashIterate(m, NULL);
718✔
3301
    int32_t i = 0;
718✔
3302
    while (pIter) {
2,872✔
3303
      size_t len = 0;
2,154✔
3304
      SIpRange *key = taosHashGetKey(pIter, &len);
2,154✔
3305
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
2,154✔
3306
      pIter = taosHashIterate(m, pIter);
2,154✔
3307
      i++;
2,154✔
3308
    }
3309

3310
    taosHashCleanup(m);
718✔
3311
    p->num = numOfRanges;
718✔
3312
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
718✔
3313
    sortIpWhiteList(p);
718✔
3314
    newUser.pIpWhiteListDual = p;
718✔
3315

3316
    newUser.ipWhiteListVer++;
718✔
3317
  }
3318

3319

3320
  if (alterReq.numTimeRanges > 0 || alterReq.numDropTimeRanges) {
918,865✔
3321
    int32_t dummy = 0;
×
3322

3323
    // put previous ip whitelist into hash table
3324
    SHashObj *m = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
3325
    if (m == NULL) {
×
3326
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3327
    }
3328

3329
    for (int32_t i = 0; i < newUser.pTimeWhiteList->num; i++) {
×
3330
      SDateTimeWhiteListItem *range = &newUser.pTimeWhiteList->ranges[i];
×
3331
      if (isDateTimeWhiteListItemExpired(range)) {
×
3332
        continue;
×
3333
      }
3334
      code = taosHashPut(m, range, sizeof(*range), &dummy, sizeof(dummy));
×
3335
      if (code != 0) {
×
3336
        taosHashCleanup(m);
×
3337
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3338
      }
3339
    }
3340

3341
    if (alterReq.numDropTimeRanges > 0) {
×
3342
      for (int32_t i = 0; i < alterReq.numDropTimeRanges; i++) {
×
3343
        if (taosHashGetSize(m) == 0) {
×
3344
          break;
×
3345
        }
3346
        SDateTimeWhiteListItem range = { 0 };
×
3347
        DateTimeRangeToWhiteListItem(&range, alterReq.pDropTimeRanges + i);
×
3348

3349
        code = taosHashRemove(m, &range, sizeof(range));
×
3350
        if (code == TSDB_CODE_NOT_FOUND) {
×
3351
          // treat not exist as success
3352
          code = 0;
×
3353
        }
3354
        if (code != 0) {
×
3355
          taosHashCleanup(m);
×
3356
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3357
        }
3358
      }
3359
    }
3360

3361
    if (alterReq.numTimeRanges > 0) {
×
3362
      for (int32_t i = 0; i < alterReq.numTimeRanges; i++) {
×
3363
        SDateTimeWhiteListItem range = { 0 };
×
3364
        DateTimeRangeToWhiteListItem(&range, alterReq.pTimeRanges + i);
×
3365
        if (isDateTimeWhiteListItemExpired(&range)) {
×
3366
          continue;
×
3367
        }
3368
        code = taosHashPut(m, &range, sizeof(range), &dummy, sizeof(dummy));
×
3369
        if (code != 0) {
×
3370
          taosHashCleanup(m);
×
3371
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3372
        }
3373
      }
3374
    }
3375

3376
    int32_t numOfRanges = taosHashGetSize(m);
×
3377
    if (numOfRanges > MND_MAX_USER_TIME_RANGE) {
×
3378
      taosHashCleanup(m);
×
3379
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_TIME_RANGE, &lino, _OVER);
×
3380
    }
3381

3382
    SDateTimeWhiteList *p = taosMemoryCalloc(1, sizeof(SDateTimeWhiteList) + numOfRanges * sizeof(SDateTimeWhiteListItem));
×
3383
    if (p == NULL) {
×
3384
      taosHashCleanup(m);
×
3385
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3386
    }
3387

3388
    void *pIter = taosHashIterate(m, NULL);
×
3389
    int32_t i = 0;
×
3390
    while (pIter) {
×
3391
      size_t len = 0;
×
3392
      SDateTimeWhiteListItem *key = taosHashGetKey(pIter, &len);
×
3393
      memcpy(&p->ranges[i], key, sizeof(SDateTimeWhiteListItem));
×
3394
      pIter = taosHashIterate(m, pIter);
×
3395
      i++;
×
3396
    }
3397

3398
    taosHashCleanup(m);
×
3399
    p->num = numOfRanges;
×
3400
    taosMemoryFreeClear(newUser.pTimeWhiteList);
×
3401
    sortTimeWhiteList(p);
×
3402
    newUser.pTimeWhiteList = p;
×
3403
    newUser.timeWhiteListVer++;
×
3404
  }
3405

3406

3407
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
918,865✔
3408
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
879,850✔
3409
  }
3410

3411
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
914,950✔
3412
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
914,950✔
3413

3414
#if 0
3415
  if (alterReq.passIsMd5 == 0) {
3416
    if (TSDB_ALTER_USER_PASSWD == alterReq.alterType) {
3417
      code = mndCheckPasswordFmt(alterReq.pass, len);
3418
      TAOS_CHECK_GOTO(code, &lino, _OVER);
3419
    }
3420
  }
3421

3422
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
3423

3424
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
3425
  if (pOperUser == NULL) {
3426
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
3427
  }
3428

3429
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
3430

3431
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3432

3433
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
3434
    if (alterReq.passIsMd5 == 1) {
3435
      (void)memcpy(newUser.pass, alterReq.pass, TSDB_PASSWORD_LEN);
3436
    } else {
3437
      taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), newUser.pass);
3438
    }
3439

3440
    TAOS_CHECK_GOTO(mndEncryptPass(newUser.pass, &newUser.passEncryptAlgorithm), &lino, _OVER);
3441

3442
    if (0 != strncmp(pUser->pass, newUser.pass, TSDB_PASSWORD_LEN)) {
3443
      ++newUser.passVersion;
3444
    }
3445
  }
3446

3447
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
3448
    newUser.superUser = alterReq.superUser;
3449
  }
3450

3451
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
3452
    newUser.enable = alterReq.enable;
3453
  }
3454

3455
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
3456
    newUser.sysInfo = alterReq.sysInfo;
3457
  }
3458

3459
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3460
    newUser.createdb = alterReq.createdb;
3461
  }
3462

3463
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
3464
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
3465
  }
3466

3467
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_ALLOWED_HOST) {
3468
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3469

3470
    int32_t           num = pUser->pIpWhiteListDual->num + alterReq.numIpRanges;
3471
    int32_t           idx = pUser->pIpWhiteListDual->num;
3472
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3473

3474
    if (pNew == NULL) {
3475
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3476
    }
3477

3478
    bool exist = false;
3479
    (void)memcpy(pNew->pIpRanges, pUser->pIpWhiteListDual->pIpRanges, sizeof(SIpRange) * idx);
3480
    for (int i = 0; i < alterReq.numIpRanges; i++) {
3481
      SIpRange range = {0};
3482
      if (alterReq.pIpDualRanges == NULL) {
3483
        range.type = 0;
3484
        memcpy(&range.ipV4, &alterReq.pIpRanges[i], sizeof(SIpV4Range));
3485
      } else {
3486
        memcpy(&range, &alterReq.pIpDualRanges[i], sizeof(SIpRange));
3487
        range = alterReq.pIpDualRanges[i];
3488
      }
3489
      if (!isRangeInIpWhiteList(pUser->pIpWhiteListDual, &range)) {
3490
        // already exist, just ignore;
3491
        (void)memcpy(&pNew->pIpRanges[idx], &range, sizeof(SIpRange));
3492
        idx++;
3493
        continue;
3494
      } else {
3495
        exist = true;
3496
      }
3497
    }
3498
    if (exist) {
3499
      taosMemoryFree(pNew);
3500
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_EXIST, &lino, _OVER);
3501
    }
3502
    pNew->num = idx;
3503
    newUser.pIpWhiteListDual = pNew;
3504
    newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3505

3506
    if (pNew->num > MND_MAX_USER_IP_RANGE) {
3507
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
3508
    }
3509
  }
3510
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_ALLOWED_HOST) {
3511
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3512

3513
    int32_t           num = pUser->pIpWhiteListDual->num;
3514
    bool              noexist = true;
3515
    bool              localHost = false;
3516
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3517

3518
    if (pNew == NULL) {
3519
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3520
    }
3521

3522
    if (pUser->pIpWhiteListDual->num > 0) {
3523
      int idx = 0;
3524
      for (int i = 0; i < pUser->pIpWhiteListDual->num; i++) {
3525
        SIpRange *oldRange = &pUser->pIpWhiteListDual->pIpRanges[i];
3526

3527
        bool found = false;
3528
        for (int j = 0; j < alterReq.numIpRanges; j++) {
3529
          SIpRange range = {0};
3530
          if (alterReq.pIpDualRanges == NULL) {
3531
            SIpV4Range *trange = &alterReq.pIpRanges[j];
3532
            memcpy(&range.ipV4, trange, sizeof(SIpV4Range));
3533
          } else {
3534
            memcpy(&range, &alterReq.pIpDualRanges[j], sizeof(SIpRange));
3535
          }
3536

3537
          if (isDefaultRange(&range)) {
3538
            localHost = true;
3539
            break;
3540
          }
3541
          if (isIpRangeEqual(oldRange, &range)) {
3542
            found = true;
3543
            break;
3544
          }
3545
        }
3546
        if (localHost) break;
3547

3548
        if (found == false) {
3549
          (void)memcpy(&pNew->pIpRanges[idx], oldRange, sizeof(SIpRange));
3550
          idx++;
3551
        } else {
3552
          noexist = false;
3553
        }
3554
      }
3555
      pNew->num = idx;
3556
      newUser.pIpWhiteListDual = pNew;
3557
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3558

3559
    } else {
3560
      pNew->num = 0;
3561
      newUser.pIpWhiteListDual = pNew;
3562
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3563
    }
3564

3565
    if (localHost) {
3566
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
3567
    }
3568
    if (noexist) {
3569
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
3570
    }
3571
  }
3572

3573
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
3574
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
3575

3576
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
3577
    char detail[1000] = {0};
3578
    (void)tsnprintf(detail, sizeof(detail),
3579
                    "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx",
3580
                    mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
3581
                    alterReq.createdb ? 1 : 0, alterReq.tabName);
3582
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
3583
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
3584
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3585
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3586
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3587
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3588
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3589
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3590
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
3591
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
3592
    if (strcmp(alterReq.objname, "1.*") != 0) {
3593
      SName name = {0};
3594
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3595
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
3596
                  alterReq.sqlLen);
3597
    } else {
3598
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3599
    }
3600
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
3601
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
3602
                alterReq.sqlLen);
3603
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
3604
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
3605
                alterReq.sqlLen);
3606
  } else {
3607
    if (strcmp(alterReq.objname, "1.*") != 0) {
3608
      SName name = {0};
3609
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
3610
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
3611
                  alterReq.sqlLen);
3612
    } else {
3613
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
3614
    }
3615
  }
3616

3617
#endif
3618

3619
_OVER:
947,068✔
3620
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
947,068✔
3621
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
32,118✔
3622
  }
3623

3624
  tFreeSAlterUserReq(&alterReq);
947,068✔
3625
  mndReleaseUser(pMnode, pOperUser);
947,068✔
3626
  mndReleaseUser(pMnode, pUser);
947,068✔
3627
  mndUserFreeObj(&newUser);
947,068✔
3628
  TAOS_RETURN(code);
947,068✔
3629
}
3630

3631
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
60,915✔
3632
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
60,915✔
3633
  if (pTrans == NULL) {
60,915✔
3634
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
3635
    TAOS_RETURN(terrno);
×
3636
  }
3637
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
60,915✔
3638

3639
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
60,915✔
3640
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
60,915✔
3641
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
3642
    mndTransDrop(pTrans);
×
3643
    TAOS_RETURN(terrno);
×
3644
  }
3645
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
60,915✔
3646
    mndTransDrop(pTrans);
×
3647
    TAOS_RETURN(terrno);
×
3648
  }
3649

3650
  if (mndTransPrepare(pMnode, pTrans) != 0) {
60,915✔
3651
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
3652
    mndTransDrop(pTrans);
×
3653
    TAOS_RETURN(terrno);
×
3654
  }
3655

3656
  userCacheRemoveUser(pUser->user);
60,915✔
3657

3658
  mndTransDrop(pTrans);
60,915✔
3659
  TAOS_RETURN(0);
60,915✔
3660
}
3661

3662
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
61,271✔
3663
  SMnode      *pMnode = pReq->info.node;
61,271✔
3664
  int32_t      code = 0;
61,271✔
3665
  int32_t      lino = 0;
61,271✔
3666
  SUserObj    *pUser = NULL;
61,271✔
3667
  SDropUserReq dropReq = {0};
61,271✔
3668

3669
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
61,271✔
3670

3671
  mInfo("user:%s, start to drop", dropReq.user);
61,271✔
3672
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
61,271✔
3673

3674
  if (dropReq.user[0] == 0) {
61,271✔
3675
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3676
  }
3677

3678
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
61,271✔
3679

3680
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
60,915✔
3681
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
60,915✔
3682

3683
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
60,915✔
3684

3685
_OVER:
61,271✔
3686
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
61,271✔
3687
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
356✔
3688
  }
3689

3690
  mndReleaseUser(pMnode, pUser);
61,271✔
3691
  tFreeSDropUserReq(&dropReq);
61,271✔
3692
  TAOS_RETURN(code);
61,271✔
3693
}
3694

3695
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
3,346,124✔
3696
  SMnode         *pMnode = pReq->info.node;
3,346,124✔
3697
  int32_t         code = 0;
3,346,124✔
3698
  int32_t         lino = 0;
3,346,124✔
3699
  int32_t         contLen = 0;
3,346,124✔
3700
  void           *pRsp = NULL;
3,346,124✔
3701
  SUserObj       *pUser = NULL;
3,346,124✔
3702
  SGetUserAuthReq authReq = {0};
3,346,124✔
3703
  SGetUserAuthRsp authRsp = {0};
3,346,124✔
3704

3705
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
3,346,124✔
3706
  mTrace("user:%s, start to get auth", authReq.user);
3,346,124✔
3707

3708
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
3,346,124✔
3709

3710
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,344,722✔
3711

3712
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
3,344,722✔
3713
  if (contLen < 0) {
3,343,641✔
3714
    TAOS_CHECK_EXIT(contLen);
×
3715
  }
3716
  pRsp = rpcMallocCont(contLen);
3,343,641✔
3717
  if (pRsp == NULL) {
3,344,722✔
3718
    TAOS_CHECK_EXIT(terrno);
×
3719
  }
3720

3721
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
3,344,722✔
3722
  if (contLen < 0) {
3,344,722✔
3723
    TAOS_CHECK_EXIT(contLen);
×
3724
  }
3725

3726
_exit:
3,346,124✔
3727
  mndReleaseUser(pMnode, pUser);
3,346,124✔
3728
  tFreeSGetUserAuthRsp(&authRsp);
3,346,124✔
3729
  if (code < 0) {
3,345,357✔
3730
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
1,402✔
3731
    rpcFreeCont(pRsp);
1,402✔
3732
    pRsp = NULL;
1,402✔
3733
    contLen = 0;
1,402✔
3734
  }
3735
  pReq->info.rsp = pRsp;
3,345,357✔
3736
  pReq->info.rspLen = contLen;
3,346,124✔
3737
  pReq->code = code;
3,346,124✔
3738

3739
  TAOS_RETURN(code);
3,345,810✔
3740
}
3741

3742
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
20,286✔
3743
  SMnode   *pMnode = pReq->info.node;
20,286✔
3744
  SSdb     *pSdb = pMnode->pSdb;
20,286✔
3745
  int32_t   code = 0;
20,286✔
3746
  int32_t   lino = 0;
20,286✔
3747
  int32_t   numOfRows = 0;
20,286✔
3748
  SUserObj *pUser = NULL;
20,286✔
3749
  int32_t   cols = 0;
20,286✔
3750
  int8_t    flag = 0;
20,286✔
3751
  char     *pWrite = NULL;
20,286✔
3752
  char     *buf = NULL;
20,286✔
3753
  char     *varstr = NULL;
20,286✔
3754

3755
  while (numOfRows < rows) {
73,624✔
3756
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
73,624✔
3757
    if (pShow->pIter == NULL) break;
73,624✔
3758

3759
    cols = 0;
53,338✔
3760
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3761
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
53,338✔
3762
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
53,338✔
3763
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
53,338✔
3764

3765
    cols++;
53,338✔
3766
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3767
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
53,338✔
3768

3769
    cols++;
53,338✔
3770
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3771
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, pShow->pIter, _exit);
53,338✔
3772

3773
    cols++;
53,338✔
3774
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3775
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, pShow->pIter, _exit);
53,338✔
3776

3777
    cols++;
53,338✔
3778
    flag = pUser->createdb ? 1 : 0;
53,338✔
3779
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3780
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
53,338✔
3781

3782
    cols++;
53,338✔
3783
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3784
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
53,338✔
3785

3786
    cols++;
53,338✔
3787

3788
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
53,338✔
3789
    if (tlen != 0) {
53,338✔
3790
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
53,338✔
3791
      if (varstr == NULL) {
53,338✔
3792
        sdbRelease(pSdb, pUser);
×
3793
        sdbCancelFetch(pSdb, pShow->pIter);
×
3794
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3795
      }
3796
      varDataSetLen(varstr, tlen);
53,338✔
3797
      (void)memcpy(varDataVal(varstr), buf, tlen);
53,338✔
3798

3799
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3800
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
53,338✔
3801

3802
      taosMemoryFreeClear(buf);
53,338✔
3803
    } else {
3804
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3805
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3806
    }
3807

3808
    cols++;
53,338✔
3809
    tlen = convertTimeRangesToStr(pUser, &buf);
53,338✔
3810
    if (tlen != 0) {
53,338✔
3811
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
53,338✔
3812
      if (varstr == NULL) {
53,338✔
3813
        sdbRelease(pSdb, pUser);
×
3814
        sdbCancelFetch(pSdb, pShow->pIter);
×
3815
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3816
      }
3817
      varDataSetLen(varstr, tlen);
53,338✔
3818
      (void)memcpy(varDataVal(varstr), buf, tlen);
53,338✔
3819

3820
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
53,338✔
3821
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
53,338✔
3822

3823
      taosMemoryFreeClear(buf);
53,338✔
3824
    } else {
3825
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3826
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3827
    }
3828

3829
    numOfRows++;
53,338✔
3830
    sdbRelease(pSdb, pUser);
53,338✔
3831
  }
3832

3833
  pShow->numOfRows += numOfRows;
20,286✔
3834
_exit:
20,286✔
3835
  taosMemoryFreeClear(buf);
20,286✔
3836
  taosMemoryFreeClear(varstr);
20,286✔
3837
  if (code < 0) {
20,286✔
3838
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3839
    TAOS_RETURN(code);
×
3840
  }
3841
  return numOfRows;
20,286✔
3842
}
3843

3844
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
3845
  int32_t numOfRows = 0;
×
3846
#ifdef TD_ENTERPRISE
3847
  SMnode   *pMnode = pReq->info.node;
×
3848
  SSdb     *pSdb = pMnode->pSdb;
×
3849
  SUserObj *pUser = NULL;
×
3850
  int32_t   code = 0;
×
3851
  int32_t   lino = 0;
×
3852
  int32_t   cols = 0;
×
3853
  int8_t    flag = 0;
×
3854
  char     *pWrite = NULL;
×
3855
  char     *buf = NULL;
×
3856
  char     *varstr = NULL;
×
3857

3858
  while (numOfRows < rows) {
×
3859
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
3860
    if (pShow->pIter == NULL) break;
×
3861

3862
    cols = 0;
×
3863
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3864
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
3865
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
3866
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, pShow->pIter, _exit);
×
3867

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

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

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

3880
    cols++;
×
3881
    flag = pUser->createdb ? 1 : 0;
×
3882
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3883
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
×
3884

3885
    cols++;
×
3886
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3887
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
×
3888

3889
    cols++;
×
3890
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3891
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->changePass, false, pUser, pShow->pIter, _exit);
×
3892

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

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

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

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

3911
    cols++;
×
3912
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3913
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->callPerSession, false, pUser, pShow->pIter, _exit);
×
3914

3915
    /* not supported yet
3916
    cols++;
3917
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
3918
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerSession, false, pUser, pShow->pIter, _exit);
3919
*/
3920

3921
    cols++;
×
3922
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3923
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
×
3924

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

3929
    cols++;
×
3930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3931
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseTime, false, pUser, pShow->pIter, _exit);
×
3932

3933
    cols++;
×
3934
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3935
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->passwordReuseMax, false, pUser, pShow->pIter, _exit);
×
3936

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

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

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

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

3953
    cols++;
×
3954
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
×
3955
    if (tlen != 0) {
×
3956
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
3957
      if (varstr == NULL) {
×
3958
        sdbRelease(pSdb, pUser);
×
3959
        sdbCancelFetch(pSdb, pShow->pIter);
×
3960
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3961
      }
3962
      varDataSetLen(varstr, tlen);
×
3963
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
3964

3965
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3966
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
3967

3968
      taosMemoryFreeClear(buf);
×
3969
    } else {
3970
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3971
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3972
    }
3973

3974
    cols++;
×
3975
    tlen = convertTimeRangesToStr(pUser, &buf);
×
3976
    if (tlen != 0) {
×
3977
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
3978
      if (varstr == NULL) {
×
3979
        sdbRelease(pSdb, pUser);
×
3980
        sdbCancelFetch(pSdb, pShow->pIter);
×
3981
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3982
      }
3983
      varDataSetLen(varstr, tlen);
×
3984
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
3985

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

3989
      taosMemoryFreeClear(buf);
×
3990
    } else {
3991
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3992
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3993
    }
3994

3995
    numOfRows++;
×
3996
    sdbRelease(pSdb, pUser);
×
3997
  }
3998

3999
  pShow->numOfRows += numOfRows;
×
4000
_exit:
×
4001
  taosMemoryFreeClear(buf);
×
4002
  taosMemoryFreeClear(varstr);
×
4003
  if (code < 0) {
×
4004
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4005
    TAOS_RETURN(code);
×
4006
  }
4007
#endif
4008
  return numOfRows;
×
4009
}
4010

4011
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
4012
  SSdb *pSdb = pMnode->pSdb;
×
4013
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4014
}
×
4015

4016
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
615,732✔
4017
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
4018
  char   *value = taosHashIterate(hash, NULL);
615,732✔
4019
  char   *user = pUser->user;
615,732✔
4020
  int32_t code = 0;
615,732✔
4021
  int32_t lino = 0;
615,732✔
4022
  int32_t cols = 0;
615,732✔
4023
  int32_t numOfRows = *pNumOfRows;
615,732✔
4024

4025
  while (value != NULL) {
703,810✔
4026
    cols = 0;
88,078✔
4027
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
88,078✔
4028
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
88,078✔
4029
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
88,078✔
4030
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
88,078✔
4031

4032
    char privilege[20] = {0};
88,078✔
4033
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
88,078✔
4034
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
88,078✔
4035
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
88,078✔
4036

4037
    size_t keyLen = 0;
88,078✔
4038
    void  *key = taosHashGetKey(value, &keyLen);
88,078✔
4039

4040
    char dbName[TSDB_DB_NAME_LEN] = {0};
88,078✔
4041
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
88,078✔
4042
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
88,078✔
4043
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
88,078✔
4044
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
88,078✔
4045
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
88,078✔
4046

4047
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
88,078✔
4048
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
88,078✔
4049
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
88,078✔
4050
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
88,078✔
4051
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
88,078✔
4052
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
88,078✔
4053

4054
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
150,028✔
4055
      SNode  *pAst = NULL;
61,950✔
4056
      int32_t sqlLen = 0;
61,950✔
4057
      size_t  bufSz = strlen(value) + 1;
61,950✔
4058
      if (bufSz < 6) bufSz = 6;
61,950✔
4059
      TAOS_MEMORY_REALLOC(*sql, bufSz);
61,950✔
4060
      if (*sql == NULL) {
61,950✔
4061
        code = terrno;
×
4062
        goto _exit;
×
4063
      }
4064
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
61,950✔
4065
      if ((*condition) == NULL) {
61,950✔
4066
        code = terrno;
×
4067
        goto _exit;
×
4068
      }
4069

4070
      if (nodesStringToNode(value, &pAst) == 0) {
61,950✔
4071
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
61,950✔
4072
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
4073
        }
4074
        nodesDestroyNode(pAst);
61,950✔
4075
      }
4076

4077
      if (sqlLen == 0) {
61,950✔
4078
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
4079
      }
4080

4081
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
61,950✔
4082

4083
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
61,950✔
4084
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
61,950✔
4085

4086
      char notes[2] = {0};
61,950✔
4087
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
61,950✔
4088
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
61,950✔
4089
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
61,950✔
4090
    } else {
4091
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
26,128✔
4092
      if ((*condition) == NULL) {
26,128✔
4093
        code = terrno;
×
4094
        goto _exit;
×
4095
      }
4096
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
26,128✔
4097
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
26,128✔
4098
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
26,128✔
4099

4100
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
26,128✔
4101
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
26,128✔
4102
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
26,128✔
4103
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
26,128✔
4104
    }
4105

4106
    numOfRows++;
88,078✔
4107
    value = taosHashIterate(hash, value);
88,078✔
4108
  }
4109
  *pNumOfRows = numOfRows;
615,732✔
4110
_exit:
615,732✔
4111
  if (code < 0) {
615,732✔
4112
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4113
    sdbRelease(pSdb, pUser);
×
4114
    sdbCancelFetch(pSdb, pShow->pIter);
×
4115
  }
4116
  TAOS_RETURN(code);
615,732✔
4117
}
4118

4119
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
20,179✔
4120
  int32_t   code = 0;
20,179✔
4121
  int32_t   lino = 0;
20,179✔
4122
  SMnode   *pMnode = pReq->info.node;
20,179✔
4123
  SSdb     *pSdb = pMnode->pSdb;
20,179✔
4124
  int32_t   numOfRows = 0;
20,179✔
4125
  SUserObj *pUser = NULL;
20,179✔
4126
  int32_t   cols = 0;
20,179✔
4127
  char     *pWrite = NULL;
20,179✔
4128
  char     *condition = NULL;
20,179✔
4129
  char     *sql = NULL;
20,179✔
4130

4131
  bool fetchNextUser = pShow->restore ? false : true;
20,179✔
4132
  pShow->restore = false;
20,179✔
4133

4134
  while (numOfRows < rows) {
122,801✔
4135
    if (fetchNextUser) {
122,801✔
4136
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
122,801✔
4137
      if (pShow->pIter == NULL) break;
122,801✔
4138
    } else {
4139
      fetchNextUser = true;
×
4140
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
4141
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
4142
      if (!pUser) {
×
4143
        continue;
×
4144
      }
4145
    }
4146

4147
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
102,622✔
4148
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
102,622✔
4149
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
102,622✔
4150
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
102,622✔
4151
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
102,622✔
4152
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
102,622✔
4153
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
102,622✔
4154
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
102,622✔
4155
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
102,622✔
4156
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
102,622✔
4157
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
102,622✔
4158
        rows) {
4159
      mInfo(
×
4160
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
4161
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
4162
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
4163
          numOfReadViews, numOfWriteViews, numOfAlterViews);
4164
      pShow->restore = true;
×
4165
      sdbRelease(pSdb, pUser);
×
4166
      break;
×
4167
    }
4168

4169
    if (pUser->superUser) {
102,622✔
4170
      cols = 0;
20,179✔
4171
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
20,179✔
4172
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
20,179✔
4173
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4174
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
20,179✔
4175

4176
      char privilege[20] = {0};
20,179✔
4177
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
20,179✔
4178
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4179
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
20,179✔
4180

4181
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
20,179✔
4182
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
20,179✔
4183
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4184
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
20,179✔
4185

4186
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
20,179✔
4187
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
20,179✔
4188
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4189
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
20,179✔
4190

4191
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
20,179✔
4192
      if (condition == NULL) {
20,179✔
4193
        sdbRelease(pSdb, pUser);
×
4194
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4195
      }
4196
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
20,179✔
4197
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4198
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
20,179✔
4199

4200
      char notes[2] = {0};
20,179✔
4201
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
20,179✔
4202
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,179✔
4203
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
20,179✔
4204

4205
      numOfRows++;
20,179✔
4206
    }
4207

4208
    char *db = taosHashIterate(pUser->readDbs, NULL);
102,622✔
4209
    while (db != NULL) {
122,024✔
4210
      cols = 0;
19,402✔
4211
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19,402✔
4212
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19,402✔
4213
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4214
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
19,402✔
4215

4216
      char privilege[20] = {0};
19,402✔
4217
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
19,402✔
4218
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4219
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
19,402✔
4220

4221
      SName name = {0};
19,402✔
4222
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,402✔
4223
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19,402✔
4224
      if (code < 0) {
19,402✔
4225
        sdbRelease(pSdb, pUser);
×
4226
        sdbCancelFetch(pSdb, pShow->pIter);
×
4227
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4228
      }
4229
      (void)tNameGetDbName(&name, varDataVal(objName));
19,402✔
4230
      varDataSetLen(objName, strlen(varDataVal(objName)));
19,402✔
4231
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4232
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
19,402✔
4233

4234
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,402✔
4235
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19,402✔
4236
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4237
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
19,402✔
4238

4239
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19,402✔
4240
      if (condition == NULL) {
19,402✔
4241
        sdbRelease(pSdb, pUser);
×
4242
        sdbCancelFetch(pSdb, pShow->pIter);
×
4243
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4244
      }
4245
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19,402✔
4246
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4247
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
19,402✔
4248

4249
      char notes[2] = {0};
19,402✔
4250
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19,402✔
4251
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,402✔
4252
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
19,402✔
4253

4254
      numOfRows++;
19,402✔
4255
      db = taosHashIterate(pUser->readDbs, db);
19,402✔
4256
    }
4257

4258
    db = taosHashIterate(pUser->writeDbs, NULL);
102,622✔
4259
    while (db != NULL) {
119,830✔
4260
      cols = 0;
17,208✔
4261
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
17,208✔
4262
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
17,208✔
4263
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4264
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
17,208✔
4265

4266
      char privilege[20] = {0};
17,208✔
4267
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
17,208✔
4268
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4269
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
17,208✔
4270

4271
      SName name = {0};
17,208✔
4272
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
17,208✔
4273
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
17,208✔
4274
      if (code < 0) {
17,208✔
4275
        sdbRelease(pSdb, pUser);
×
4276
        sdbCancelFetch(pSdb, pShow->pIter);
×
4277
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
4278
      }
4279
      (void)tNameGetDbName(&name, varDataVal(objName));
17,208✔
4280
      varDataSetLen(objName, strlen(varDataVal(objName)));
17,208✔
4281
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4282
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
17,208✔
4283

4284
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
17,208✔
4285
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
17,208✔
4286
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4287
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
17,208✔
4288

4289
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
17,208✔
4290
      if (condition == NULL) {
17,208✔
4291
        sdbRelease(pSdb, pUser);
×
4292
        sdbCancelFetch(pSdb, pShow->pIter);
×
4293
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4294
      }
4295
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
17,208✔
4296
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4297
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
17,208✔
4298

4299
      char notes[2] = {0};
17,208✔
4300
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
17,208✔
4301
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,208✔
4302
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
17,208✔
4303

4304
      numOfRows++;
17,208✔
4305
      db = taosHashIterate(pUser->writeDbs, db);
17,208✔
4306
    }
4307

4308
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4309

4310
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4311

4312
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4313

4314
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4315

4316
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4317

4318
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
102,622✔
4319

4320
    char *topic = taosHashIterate(pUser->topics, NULL);
102,622✔
4321
    while (topic != NULL) {
105,991✔
4322
      cols = 0;
3,369✔
4323
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
3,369✔
4324
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
3,369✔
4325
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4326
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
3,369✔
4327

4328
      char privilege[20] = {0};
3,369✔
4329
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
3,369✔
4330
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4331
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
3,369✔
4332

4333
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
3,369✔
4334
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
3,369✔
4335
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
3,369✔
4336
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4337
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, pShow->pIter, _exit);
3,369✔
4338

4339
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
3,369✔
4340
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
3,369✔
4341
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4342
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
3,369✔
4343

4344
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
3,369✔
4345
      if (condition == NULL) {
3,369✔
4346
        sdbRelease(pSdb, pUser);
×
4347
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
4348
      }
4349
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
3,369✔
4350
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4351
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
3,369✔
4352

4353
      char notes[2] = {0};
3,369✔
4354
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
3,369✔
4355
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,369✔
4356
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
3,369✔
4357

4358
      numOfRows++;
3,369✔
4359
      topic = taosHashIterate(pUser->topics, topic);
3,369✔
4360
    }
4361

4362
    sdbRelease(pSdb, pUser);
102,622✔
4363
  }
4364

4365
  pShow->numOfRows += numOfRows;
20,179✔
4366
_exit:
20,179✔
4367
  taosMemoryFreeClear(condition);
20,179✔
4368
  taosMemoryFreeClear(sql);
20,179✔
4369
  if (code < 0) {
20,179✔
4370
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4371
    TAOS_RETURN(code);
×
4372
  }
4373
  return numOfRows;
20,179✔
4374
}
4375

4376
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
4377
  SSdb *pSdb = pMnode->pSdb;
×
4378
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4379
}
×
4380

4381
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
13,596,305✔
4382
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
4383
  int32_t           code = 0;
13,596,305✔
4384
  int32_t           lino = 0;
13,596,305✔
4385
  int32_t           rspLen = 0;
13,597,019✔
4386
  void             *pRsp = NULL;
13,597,019✔
4387
  SUserAuthBatchRsp batchRsp = {0};
13,597,019✔
4388

4389
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
13,597,019✔
4390
  if (batchRsp.pArray == NULL) {
13,597,019✔
4391
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4392
  }
4393

4394
  for (int32_t i = 0; i < numOfUses; ++i) {
27,533,472✔
4395
    SUserObj *pUser = NULL;
13,936,453✔
4396
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
13,936,453✔
4397
    if (pUser == NULL) {
13,936,453✔
4398
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
7,367✔
4399
        SGetUserAuthRsp rsp = {.dropped = 1};
7,367✔
4400
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
7,367✔
4401
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
14,734✔
4402
      }
4403
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
7,367✔
4404
      code = 0;
7,367✔
4405
      continue;
33,699✔
4406
    }
4407

4408
    pUsers[i].version = ntohl(pUsers[i].version);
13,929,086✔
4409
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
13,929,086✔
4410
      mndReleaseUser(pMnode, pUser);
13,151,511✔
4411
      continue;
13,152,210✔
4412
    }
4413

4414
    SGetUserAuthRsp rsp = {0};
777,575✔
4415
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
776,876✔
4416
    if (code) {
776,876✔
4417
      mndReleaseUser(pMnode, pUser);
×
4418
      tFreeSGetUserAuthRsp(&rsp);
×
4419
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4420
    }
4421

4422
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
1,553,752✔
4423
      code = terrno;
×
4424
      mndReleaseUser(pMnode, pUser);
×
4425
      tFreeSGetUserAuthRsp(&rsp);
×
4426
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4427
    }
4428
    mndReleaseUser(pMnode, pUser);
776,876✔
4429
  }
4430

4431
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
13,597,019✔
4432
    *ppRsp = NULL;
12,827,900✔
4433
    *pRspLen = 0;
12,827,900✔
4434

4435
    tFreeSUserAuthBatchRsp(&batchRsp);
12,827,900✔
4436
    return 0;
12,827,201✔
4437
  }
4438

4439
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
769,119✔
4440
  if (rspLen < 0) {
768,448✔
4441
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4442
  }
4443
  pRsp = taosMemoryMalloc(rspLen);
768,448✔
4444
  if (pRsp == NULL) {
768,817✔
4445
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4446
  }
4447
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
768,817✔
4448
  if (rspLen < 0) {
768,817✔
4449
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4450
  }
4451
_OVER:
768,817✔
4452
  tFreeSUserAuthBatchRsp(&batchRsp);
768,817✔
4453
  if (code < 0) {
768,817✔
4454
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4455
    taosMemoryFreeClear(pRsp);
×
4456
    rspLen = 0;
×
4457
  }
4458
  *ppRsp = pRsp;
768,817✔
4459
  *pRspLen = rspLen;
769,119✔
4460

4461
  TAOS_RETURN(code);
768,448✔
4462
}
4463

4464
static int32_t mndRemoveDbPrivileges(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, int32_t *nRemoved) {
30,882✔
4465
  void *pVal = NULL;
30,882✔
4466
  while ((pVal = taosHashIterate(pHash, pVal))) {
54,362✔
4467
    size_t keyLen = 0;
23,480✔
4468
    char  *pKey = (char *)taosHashGetKey(pVal, &keyLen);
23,480✔
4469
    if (pKey == NULL || keyLen <= dbFNameLen) continue;
23,480✔
4470
    if ((*(pKey + dbFNameLen) == '.') && strncmp(pKey, dbFName, dbFNameLen) == 0) {
23,480✔
4471
      TAOS_CHECK_RETURN(taosHashRemove(pHash, pKey, keyLen));
11,929✔
4472
      if (nRemoved) ++(*nRemoved);
11,929✔
4473
    }
4474
  }
4475
  TAOS_RETURN(0);
30,882✔
4476
}
4477

4478
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers) {
730,998✔
4479
  int32_t    code = 0, lino = 0;
730,998✔
4480
  SSdb      *pSdb = pMnode->pSdb;
730,998✔
4481
  int32_t    dbLen = strlen(pDb->name);
730,998✔
4482
  void      *pIter = NULL;
730,998✔
4483
  SUserObj  *pUser = NULL;
730,998✔
4484
  SUserObj   newUser = {0};
730,998✔
4485
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
730,998✔
4486
  bool       output = (ppUsers != NULL);
730,998✔
4487

4488
  while (1) {
794,261✔
4489
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,525,259✔
4490
    if (pIter == NULL) break;
1,525,259✔
4491

4492
    bool update = false;
794,261✔
4493
    bool inReadDb = (taosHashGet(pUser->readDbs, pDb->name, dbLen + 1) != NULL);
794,261✔
4494
    bool inWriteDb = (taosHashGet(pUser->writeDbs, pDb->name, dbLen + 1) != NULL);
794,261✔
4495
    bool inUseDb = (taosHashGet(pUser->useDbs, pDb->name, dbLen + 1) != NULL);
794,261✔
4496
    bool inReadTbs = taosHashGetSize(pUser->readTbs) > 0;
794,261✔
4497
    bool inWriteTbs = taosHashGetSize(pUser->writeTbs) > 0;
794,261✔
4498
    bool inAlterTbs = taosHashGetSize(pUser->alterTbs) > 0;
794,261✔
4499
    bool inReadViews = taosHashGetSize(pUser->readViews) > 0;
794,261✔
4500
    bool inWriteViews = taosHashGetSize(pUser->writeViews) > 0;
794,261✔
4501
    bool inAlterViews = taosHashGetSize(pUser->alterViews) > 0;
794,261✔
4502
    // no need remove pUser->topics since topics must be dropped ahead of db
4503
    if (!inReadDb && !inWriteDb && !inReadTbs && !inWriteTbs && !inAlterTbs && !inReadViews && !inWriteViews &&
794,261✔
4504
        !inAlterViews) {
783,361✔
4505
      sdbRelease(pSdb, pUser);
783,361✔
4506
      continue;
783,361✔
4507
    }
4508
    SUserObj *pTargetUser = &newUser;
10,900✔
4509
    if (output) {
10,900✔
4510
      if (!pUsers) {
1,318✔
4511
        TSDB_CHECK_NULL(pUsers = tSimpleHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY)), code, lino,
659✔
4512
                        _exit, TSDB_CODE_OUT_OF_MEMORY);
4513
        *ppUsers = pUsers;
659✔
4514
      }
4515
      void   *pVal = NULL;
1,318✔
4516
      int32_t userLen = strlen(pUser->user) + 1;
1,318✔
4517
      if ((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)) != NULL) {
1,318✔
4518
        pTargetUser = (SUserObj *)pVal;
659✔
4519
      } else {
4520
        TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
659✔
4521
        TAOS_CHECK_EXIT(tSimpleHashPut(pUsers, pUser->user, userLen, &newUser, sizeof(SUserObj)));
659✔
4522
        TSDB_CHECK_NULL((pVal = tSimpleHashGet(pUsers, pUser->user, userLen)), code, lino, _exit,
659✔
4523
                        TSDB_CODE_OUT_OF_MEMORY);
4524
        pTargetUser = (SUserObj *)pVal;
659✔
4525
      }
4526
    } else {
4527
      TAOS_CHECK_EXIT(mndUserDupObj(pUser, &newUser));
9,582✔
4528
    }
4529
    if (inReadDb) {
10,900✔
4530
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->readDbs, pDb->name, dbLen + 1));
2,644✔
4531
    }
4532
    if (inWriteDb) {
10,900✔
4533
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->writeDbs, pDb->name, dbLen + 1));
2,284✔
4534
    }
4535
    if (inUseDb) {
10,900✔
4536
      TAOS_CHECK_EXIT(taosHashRemove(pTargetUser->useDbs, pDb->name, dbLen + 1));
1,678✔
4537
    }
4538
    update = inReadDb || inWriteDb || inUseDb;
10,900✔
4539

4540
    int32_t nRemovedReadTbs = 0;
10,900✔
4541
    int32_t nRemovedWriteTbs = 0;
10,900✔
4542
    int32_t nRemovedAlterTbs = 0;
10,900✔
4543
    if (inReadTbs || inWriteTbs || inAlterTbs) {
10,900✔
4544
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readTbs, pDb->name, dbLen, &nRemovedReadTbs));
9,934✔
4545
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeTbs, pDb->name, dbLen, &nRemovedWriteTbs));
9,934✔
4546
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterTbs, pDb->name, dbLen, &nRemovedAlterTbs));
9,934✔
4547
      if (!update) update = nRemovedReadTbs > 0 || nRemovedWriteTbs > 0 || nRemovedAlterTbs > 0;
9,934✔
4548
    }
4549

4550
    int32_t nRemovedReadViews = 0;
10,900✔
4551
    int32_t nRemovedWriteViews = 0;
10,900✔
4552
    int32_t nRemovedAlterViews = 0;
10,900✔
4553
    if (inReadViews || inWriteViews || inAlterViews) {
10,900✔
4554
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readViews, pDb->name, dbLen, &nRemovedReadViews));
360✔
4555
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeViews, pDb->name, dbLen, &nRemovedWriteViews));
360✔
4556
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterViews, pDb->name, dbLen, &nRemovedAlterViews));
360✔
4557
      if (!update) update = nRemovedReadViews > 0 || nRemovedWriteViews > 0 || nRemovedAlterViews > 0;
360✔
4558
    }
4559

4560
    if (!output) {
10,900✔
4561
      if (update) {
9,582✔
4562
        SSdbRaw *pCommitRaw = mndUserActionEncode(pTargetUser);
1,326✔
4563
        if (pCommitRaw == NULL) {
1,326✔
4564
          TAOS_CHECK_EXIT(terrno);
×
4565
        }
4566
        TAOS_CHECK_EXIT(mndTransAppendCommitlog(pTrans, pCommitRaw));
1,326✔
4567
        TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,326✔
4568
      }
4569
      mndUserFreeObj(&newUser);
9,582✔
4570
    }
4571
    sdbRelease(pSdb, pUser);
10,900✔
4572
  }
4573

4574
_exit:
730,998✔
4575
  if (code < 0) {
730,998✔
4576
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4577
    mndUserFreeObj(&newUser);
×
4578
  }
4579
  if (pUser != NULL) sdbRelease(pSdb, pUser);
730,998✔
4580
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
730,998✔
4581
  if (!output) mndUserFreeObj(&newUser);
730,998✔
4582
  TAOS_RETURN(code);
730,998✔
4583
}
4584

4585
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
507,854✔
4586
  int32_t   code = 0;
507,854✔
4587
  SSdb     *pSdb = pMnode->pSdb;
507,854✔
4588
  int32_t   len = strlen(stb) + 1;
507,854✔
4589
  void     *pIter = NULL;
507,854✔
4590
  SUserObj *pUser = NULL;
507,854✔
4591
  SUserObj  newUser = {0};
507,854✔
4592

4593
  while (1) {
507,854✔
4594
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,015,708✔
4595
    if (pIter == NULL) break;
1,015,708✔
4596

4597
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
507,854✔
4598
      break;
×
4599
    }
4600

4601
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
507,854✔
4602
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
507,854✔
4603
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
507,854✔
4604
    if (inRead || inWrite || inAlter) {
507,854✔
4605
      code = taosHashRemove(newUser.readTbs, stb, len);
×
4606
      if (code < 0) {
×
4607
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
4608
      }
4609
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
4610
      if (code < 0) {
×
4611
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
4612
      }
4613
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
4614
      if (code < 0) {
×
4615
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
4616
      }
4617

4618
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
4619
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
4620
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4621
        break;
×
4622
      }
4623
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
4624
      if (code != 0) {
×
4625
        mndUserFreeObj(&newUser);
×
4626
        sdbRelease(pSdb, pUser);
×
4627
        TAOS_RETURN(code);
×
4628
      }
4629
    }
4630

4631
    mndUserFreeObj(&newUser);
507,854✔
4632
    sdbRelease(pSdb, pUser);
507,854✔
4633
  }
4634

4635
  if (pUser != NULL) sdbRelease(pSdb, pUser);
507,854✔
4636
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
507,854✔
4637
  mndUserFreeObj(&newUser);
507,854✔
4638
  TAOS_RETURN(code);
507,854✔
4639
}
4640

4641
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
180,924✔
4642
  int32_t   code = 0;
180,924✔
4643
  SSdb     *pSdb = pMnode->pSdb;
180,924✔
4644
  int32_t   len = strlen(view) + 1;
180,924✔
4645
  void     *pIter = NULL;
180,924✔
4646
  SUserObj *pUser = NULL;
180,924✔
4647
  SUserObj  newUser = {0};
180,924✔
4648

4649
  while (1) {
188,315✔
4650
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
369,239✔
4651
    if (pIter == NULL) break;
369,239✔
4652

4653
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
188,315✔
4654
      break;
×
4655
    }
4656

4657
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
188,315✔
4658
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
188,315✔
4659
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
188,315✔
4660
    if (inRead || inWrite || inAlter) {
188,315✔
4661
      code = taosHashRemove(newUser.readViews, view, len);
4,587✔
4662
      if (code < 0) {
4,587✔
4663
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
910✔
4664
      }
4665
      code = taosHashRemove(newUser.writeViews, view, len);
4,587✔
4666
      if (code < 0) {
4,587✔
4667
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
2,312✔
4668
      }
4669
      code = taosHashRemove(newUser.alterViews, view, len);
4,587✔
4670
      if (code < 0) {
4,587✔
4671
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
1,402✔
4672
      }
4673

4674
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
4,587✔
4675
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
4,587✔
4676
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4677
        break;
×
4678
      }
4679
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
4,587✔
4680
      if (code < 0) {
4,587✔
4681
        mndUserFreeObj(&newUser);
×
4682
        sdbRelease(pSdb, pUser);
×
4683
        TAOS_RETURN(code);
×
4684
      }
4685
    }
4686

4687
    mndUserFreeObj(&newUser);
188,315✔
4688
    sdbRelease(pSdb, pUser);
188,315✔
4689
  }
4690

4691
  if (pUser != NULL) sdbRelease(pSdb, pUser);
180,924✔
4692
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
180,924✔
4693
  mndUserFreeObj(&newUser);
180,924✔
4694
  TAOS_RETURN(code);
180,924✔
4695
}
4696

4697
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
33,488✔
4698
  int32_t   code = 0;
33,488✔
4699
  SSdb     *pSdb = pMnode->pSdb;
33,488✔
4700
  int32_t   len = strlen(topic) + 1;
33,488✔
4701
  void     *pIter = NULL;
33,488✔
4702
  SUserObj *pUser = NULL;
33,488✔
4703
  SUserObj  newUser = {0};
33,488✔
4704

4705
  while (1) {
48,464✔
4706
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
81,952✔
4707
    if (pIter == NULL) {
81,952✔
4708
      break;
33,488✔
4709
    }
4710

4711
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
48,464✔
4712
      break;
×
4713
    }
4714

4715
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
48,464✔
4716
    if (inTopic) {
48,464✔
4717
      code = taosHashRemove(newUser.topics, topic, len);
360✔
4718
      if (code < 0) {
360✔
4719
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
4720
      }
4721
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
360✔
4722
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
360✔
4723
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4724
        break;
×
4725
      }
4726
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
360✔
4727
      if (code < 0) {
360✔
4728
        mndUserFreeObj(&newUser);
×
4729
        sdbRelease(pSdb, pUser);
×
4730
        TAOS_RETURN(code);
×
4731
      }
4732
    }
4733

4734
    mndUserFreeObj(&newUser);
48,464✔
4735
    sdbRelease(pSdb, pUser);
48,464✔
4736
  }
4737

4738
  if (pUser != NULL) sdbRelease(pSdb, pUser);
33,488✔
4739
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
33,488✔
4740
  mndUserFreeObj(&newUser);
33,488✔
4741
  TAOS_RETURN(code);
33,488✔
4742
}
4743

4744
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4745
  // ver = 0, disable ip white list
4746
  // ver > 0, enable ip white list
4747
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
4748
}
4749

4750
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4751
  // ver = 0, disable datetime white list
4752
  // ver > 0, enable datetime white list
4753
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
4754
}
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