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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

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

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

33
// clang-format on
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

140

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

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

154
static SUserCache userCache;
155

156

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

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

165
  userCache.users = users;
500,198✔
166
  userCache.verIp = 0;
500,198✔
167
  userCache.verTime = 0;
500,198✔
168

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

173

174

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

180
  void *pIter = taosHashIterate(userCache.users, NULL);
500,081✔
181
  while (pIter) {
1,010,442✔
182
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
510,361✔
183
    if (pInfo != NULL) {
510,361✔
184
      taosMemoryFree(pInfo->wlIp);
510,361✔
185
      taosMemoryFree(pInfo->wlTime);
510,361✔
186
      taosMemoryFree(pInfo);
510,361✔
187
    }
188
    pIter = taosHashIterate(userCache.users, pIter);
510,361✔
189
  }
190
  taosHashCleanup(userCache.users);
500,081✔
191

192
  (void)taosThreadRwlockDestroy(&userCache.rw);
500,081✔
193
}
194

195

196

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

200
  (void)taosThreadRwlockWrlock(&userCache.rw);
58,928✔
201

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

216
  (void)taosThreadRwlockUnlock(&userCache.rw);
58,928✔
217
}
58,928✔
218

219

220

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

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

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

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

236

237

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

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

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

255
  return pInfo;
569,289✔
256
}
257

258

259

260
void mndGetUserLoginInfo(const char *user, SLoginInfo *pLoginInfo) {
2,595,523✔
261
  size_t userLen = strlen(user);
2,595,523✔
262

263
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,595,523✔
264

265
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,596,227✔
266
  if (ppInfo != NULL && *ppInfo != NULL) {
2,595,635✔
267
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,276,670✔
268
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,275,830✔
269
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,276,429✔
270
  } else {
271
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
319,205✔
272
    pLoginInfo->failedLoginCount = 0;
318,965✔
273
    pLoginInfo->lastFailedLoginTime = 0;
318,965✔
274
  }
275

276
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,595,178✔
277

278
  if (pLoginInfo->lastLoginTime == 0) {
2,595,489✔
279
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
47,094✔
280
  }
281
}
2,594,785✔
282

283

284

285
void mndSetUserLoginInfo(const char *user, const SLoginInfo *pLoginInfo) {
2,595,523✔
286
  size_t userLen = strlen(user);
2,595,523✔
287

288
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,595,523✔
289

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

297
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,596,227✔
298
}
2,596,227✔
299

300

301

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

307
  if (a == NULL || b == NULL) {
1,014,840✔
308
    return false;
100,241✔
309
  }
310

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

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

324
  return true;
914,599✔
325
}
326

327

328

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

332
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,014,840✔
333

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

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

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

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

367

368

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

372
  SSdb     *pSdb = pMnode->pSdb;
607,234✔
373
  void     *pIter = NULL;
607,234✔
374
  while (1) {
261,082✔
375
    SUserObj *pUser = NULL;
868,316✔
376
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
868,316✔
377
    if (pIter == NULL) {
868,316✔
378
      break;
607,234✔
379
    }
380

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

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

395
    taosMemoryFree(pInfo->wlIp);
261,082✔
396
    pInfo->wlIp = wl;
261,082✔
397

398
    sdbRelease(pSdb, pUser);
261,082✔
399
  }
400

401
  userCache.verIp++;
607,234✔
402

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

410

411

412
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
40,446,741✔
413
  int64_t ver = 0;
40,446,741✔
414
  int32_t code = 0;
40,446,741✔
415

416
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,446,741✔
417
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,945✔
418

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

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

433
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
40,446,741✔
434
  return ver;
40,446,741✔
435
}
436

437

438

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

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

450
  TAOS_RETURN(code);
607,234✔
451
}
452

453

454

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

458
  SSdb     *pSdb = pMnode->pSdb;
595,538✔
459
  void     *pIter = NULL;
595,538✔
460
  while (1) {
249,386✔
461
    SUserObj *pUser = NULL;
844,924✔
462
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
844,924✔
463
    if (pIter == NULL) {
844,924✔
464
      break;
595,538✔
465
    }
466

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

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

481
    taosMemoryFree(pInfo->wlTime);
249,386✔
482
    pInfo->wlTime = wl;
249,386✔
483

484
    sdbRelease(pSdb, pUser);
249,386✔
485
  }
486

487
  userCache.verTime++;
595,538✔
488

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

496

497

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

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

509
  TAOS_RETURN(code);
595,538✔
510
}
511

512

513

514
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
40,446,741✔
515
  int64_t ver = 0;
40,446,741✔
516
  int32_t code = 0;
40,446,741✔
517

518
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
40,446,741✔
519
    (void)taosThreadRwlockWrlock(&userCache.rw);
13,945✔
520

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

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

535
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
40,446,741✔
536
  return ver;
40,446,741✔
537
}
538

539

540

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

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

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

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

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

576

577

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

582

583

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

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

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

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

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

604

605

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

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

622

623

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

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

640

641

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

651
  if (a->num != b->num) {
914,599✔
652
    return false;
357✔
653
  }
654
  for (int i = 0; i < a->num; i++) {
2,742,726✔
655
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,828,484✔
656
      return false;
×
657
    }
658
  }
659
  return true;
914,242✔
660
}
661

662

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

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

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

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

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

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

695

696

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

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

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

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

718

719

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

727
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,498,022✔
728
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
4,996,044✔
729

730
  for (int i = 0; i < pList->num; i++) {
7,495,131✔
731
    SIpRange *pRange = &(pList->pIpRanges[i]);
4,997,109✔
732
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
4,997,109✔
733
  }
734

735
  tEndEncode(&encoder);
2,498,022✔
736

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

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

753
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,632,951✔
754
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
3,265,902✔
755

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

859
  SIpRange v4 = {0};
422,522✔
860
  SIpRange v6 = {0};
422,522✔
861

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

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

869
#endif
870

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

883

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

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

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

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

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

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

919
  return pos;
×
920
}
921

922

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

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

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

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

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

943
  return 0;
×
944
}
945

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

950

951

952

953
static void dropOldPasswords(SUserObj *pUser) {
4,130,973✔
954
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
4,130,973✔
955
    return;
4,080,753✔
956
  }
957

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

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

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

981

982

983

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

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

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

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

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

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

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

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

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

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

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

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

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

1109
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,498,022✔
1110
  while (stb != NULL) {
2,940,346✔
1111
    size_t keyLen = 0;
442,324✔
1112
    void  *key = taosHashGetKey(stb, &keyLen);
442,324✔
1113
    size += sizeof(int32_t);
442,324✔
1114
    size += keyLen;
442,324✔
1115

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

1123
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,498,022✔
1124
  while (stb != NULL) {
2,951,749✔
1125
    size_t keyLen = 0;
453,727✔
1126
    void  *key = taosHashGetKey(stb, &keyLen);
453,727✔
1127
    size += sizeof(int32_t);
453,727✔
1128
    size += keyLen;
453,727✔
1129

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

1137
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,498,022✔
1138
  while (stb != NULL) {
2,731,487✔
1139
    size_t keyLen = 0;
233,465✔
1140
    void  *key = taosHashGetKey(stb, &keyLen);
233,465✔
1141
    size += sizeof(int32_t);
233,465✔
1142
    size += keyLen;
233,465✔
1143

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

1151
  stb = taosHashIterate(pUser->readViews, NULL);
2,498,022✔
1152
  while (stb != NULL) {
2,546,016✔
1153
    size_t keyLen = 0;
47,994✔
1154
    void  *key = taosHashGetKey(stb, &keyLen);
47,994✔
1155
    size += sizeof(int32_t);
47,994✔
1156
    size += keyLen;
47,994✔
1157

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

1165
  stb = taosHashIterate(pUser->writeViews, NULL);
2,498,022✔
1166
  while (stb != NULL) {
2,534,717✔
1167
    size_t keyLen = 0;
36,695✔
1168
    void  *key = taosHashGetKey(stb, &keyLen);
36,695✔
1169
    size += sizeof(int32_t);
36,695✔
1170
    size += keyLen;
36,695✔
1171

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

1179
  stb = taosHashIterate(pUser->alterViews, NULL);
2,498,022✔
1180
  while (stb != NULL) {
2,534,109✔
1181
    size_t keyLen = 0;
36,087✔
1182
    void  *key = taosHashGetKey(stb, &keyLen);
36,087✔
1183
    size += sizeof(int32_t);
36,087✔
1184
    size += keyLen;
36,087✔
1185

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

1193
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,498,022✔
1194
  while (useDb != NULL) {
3,061,604✔
1195
    size_t keyLen = 0;
563,582✔
1196
    void  *key = taosHashGetKey(useDb, &keyLen);
563,582✔
1197
    size += sizeof(int32_t);
563,582✔
1198
    size += keyLen;
563,582✔
1199
    size += sizeof(int32_t);
563,582✔
1200
    useDb = taosHashIterate(pUser->useDbs, useDb);
563,582✔
1201
  }
1202

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

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

1211
  dropOldPasswords(pUser);
2,498,022✔
1212
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,498,022✔
1213
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
5,026,656✔
1214
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,528,634✔
1215
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,528,634✔
1216
  }
1217
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,498,022✔
1218

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

1232
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,498,022✔
1233
  while (db != NULL) {
2,830,303✔
1234
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
332,281✔
1235
    db = taosHashIterate(pUser->readDbs, db);
332,281✔
1236
  }
1237

1238
  db = taosHashIterate(pUser->writeDbs, NULL);
2,498,022✔
1239
  while (db != NULL) {
2,822,466✔
1240
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
324,444✔
1241
    db = taosHashIterate(pUser->writeDbs, db);
324,444✔
1242
  }
1243

1244
  char *topic = taosHashIterate(pUser->topics, NULL);
2,498,022✔
1245
  while (topic != NULL) {
2,514,906✔
1246
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
16,884✔
1247
    topic = taosHashIterate(pUser->topics, topic);
16,884✔
1248
  }
1249

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

1258
  stb = taosHashIterate(pUser->readTbs, NULL);
2,498,022✔
1259
  while (stb != NULL) {
2,940,346✔
1260
    size_t keyLen = 0;
442,324✔
1261
    void  *key = taosHashGetKey(stb, &keyLen);
442,324✔
1262
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
442,324✔
1263
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
442,324✔
1264

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

1272
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,498,022✔
1273
  while (stb != NULL) {
2,951,749✔
1274
    size_t keyLen = 0;
453,727✔
1275
    void  *key = taosHashGetKey(stb, &keyLen);
453,727✔
1276
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
453,727✔
1277
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
453,727✔
1278

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

1286
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,498,022✔
1287
  while (stb != NULL) {
2,731,487✔
1288
    size_t keyLen = 0;
233,465✔
1289
    void  *key = taosHashGetKey(stb, &keyLen);
233,465✔
1290
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
233,465✔
1291
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
233,465✔
1292

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

1300
  stb = taosHashIterate(pUser->readViews, NULL);
2,498,022✔
1301
  while (stb != NULL) {
2,546,016✔
1302
    size_t keyLen = 0;
47,994✔
1303
    void  *key = taosHashGetKey(stb, &keyLen);
47,994✔
1304
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
47,994✔
1305
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
47,994✔
1306

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

1314
  stb = taosHashIterate(pUser->writeViews, NULL);
2,498,022✔
1315
  while (stb != NULL) {
2,534,717✔
1316
    size_t keyLen = 0;
36,695✔
1317
    void  *key = taosHashGetKey(stb, &keyLen);
36,695✔
1318
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
36,695✔
1319
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,695✔
1320

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

1328
  stb = taosHashIterate(pUser->alterViews, NULL);
2,498,022✔
1329
  while (stb != NULL) {
2,534,109✔
1330
    size_t keyLen = 0;
36,087✔
1331
    void  *key = taosHashGetKey(stb, &keyLen);
36,087✔
1332
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
36,087✔
1333
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,087✔
1334

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

1342
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,498,022✔
1343
  while (useDb != NULL) {
3,061,604✔
1344
    size_t keyLen = 0;
563,582✔
1345
    void  *key = taosHashGetKey(useDb, &keyLen);
563,582✔
1346
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
563,582✔
1347
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
563,582✔
1348

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

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

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

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

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

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

1393
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,498,022✔
1394
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,498,022✔
1395

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1559
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,050,044✔
1560
      int32_t keyLen = 0;
417,093✔
1561
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
417,093✔
1562

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

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

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

1582
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
2,059,852✔
1583
      int32_t keyLen = 0;
426,901✔
1584
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
426,901✔
1585

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

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

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

1605
    if (sver >= 6) {
1,632,951✔
1606
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,856,144✔
1607
        int32_t keyLen = 0;
223,193✔
1608
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
223,193✔
1609

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

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

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

1629
      for (int32_t i = 0; i < numOfReadViews; ++i) {
1,680,945✔
1630
        int32_t keyLen = 0;
47,994✔
1631
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
47,994✔
1632

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

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

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

1652
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
1,669,646✔
1653
        int32_t keyLen = 0;
36,695✔
1654
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36,695✔
1655

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1739
      taosMemoryFreeClear(pIpWhiteList);
×
1740

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

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

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

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

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

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

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

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

1815
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
1,632,951✔
1816
  taosInitRWLatch(&pUser->lock);
1,632,951✔
1817
  dropOldPasswords(pUser);
1,632,951✔
1818

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

1844
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
1,632,951✔
1845
  return pRow;
1,632,951✔
1846
}
1847

1848
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
617,920✔
1849
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
617,920✔
1850

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

1860
  return 0;
617,920✔
1861
}
1862

1863
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
37,949,709✔
1864
  int32_t code = 0;
37,949,709✔
1865
  *ppNew =
37,951,291✔
1866
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
37,949,709✔
1867
  if (*ppNew == NULL) {
37,951,291✔
1868
    TAOS_RETURN(terrno);
×
1869
  }
1870

1871
  char *tb = taosHashIterate(pOld, NULL);
37,951,291✔
1872
  while (tb != NULL) {
40,305,718✔
1873
    size_t keyLen = 0;
2,354,427✔
1874
    char  *key = taosHashGetKey(tb, &keyLen);
2,354,427✔
1875

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

1885
  TAOS_RETURN(code);
37,951,291✔
1886
}
1887

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

1896
  int32_t *db = taosHashIterate(pOld, NULL);
1,673,189✔
1897
  while (db != NULL) {
2,175,066✔
1898
    size_t keyLen = 0;
501,877✔
1899
    char  *key = taosHashGetKey(db, &keyLen);
501,877✔
1900

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

1909
  TAOS_RETURN(code);
1,673,189✔
1910
}
1911

1912
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
1,673,189✔
1913
  int32_t code = 0;
1,673,189✔
1914
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
1,673,189✔
1915
  pNew->authVersion++;
1,673,189✔
1916
  pNew->updateTime = taosGetTimestampMs();
1,673,189✔
1917

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

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

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

1956
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
1,673,189✔
1957
  if (pNew->pTimeWhiteList == NULL) {
1,673,189✔
1958
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1959
    goto _OVER;
×
1960
  }
1961

1962
_OVER:
1,673,189✔
1963
  taosRUnLockLatch(&pUser->lock);
1,673,189✔
1964
  TAOS_RETURN(code);
1,673,189✔
1965
}
1966

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

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

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

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

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

2039
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
944,513✔
2040
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
944,513✔
2041
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
944,513✔
2042
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
944,513✔
2043
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
944,513✔
2044

2045
  taosWUnLockLatch(&pOld->lock);
944,513✔
2046

2047
  return 0;
944,513✔
2048
}
2049

2050
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
81,987,105✔
2051
  int32_t code = 0;
81,987,105✔
2052
  SSdb   *pSdb = pMnode->pSdb;
81,987,105✔
2053

2054
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
81,987,767✔
2055
  if (*ppUser == NULL) {
81,987,812✔
2056
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
117,689✔
2057
      code = TSDB_CODE_MND_USER_NOT_EXIST;
117,689✔
2058
    } else {
2059
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2060
    }
2061
  }
2062
  TAOS_RETURN(code);
81,987,812✔
2063
}
2064

2065
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
82,037,636✔
2066
  SSdb *pSdb = pMnode->pSdb;
82,037,636✔
2067
  sdbRelease(pSdb, pUser);
82,037,528✔
2068
}
82,038,643✔
2069

2070

2071

2072
int32_t mndEncryptPass(char *pass, const char* salt, int8_t *algo) {
114,136✔
2073
  int32_t code = 0;
114,136✔
2074
  if (tsiEncryptPassAlgorithm != DND_CA_SM4) {
114,136✔
2075
    return 0;
113,414✔
2076
  }
2077

2078
  if (strlen(tsEncryptKey) == 0) {
722✔
2079
    return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
2080
  }
2081

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

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

2099
  memcpy(pass, packetData, newLen);
722✔
2100
  if (algo != NULL) {
722✔
2101
    *algo = DND_CA_SM4;
361✔
2102
  }
2103

2104
  return 0;
722✔
2105
}
2106

2107

2108

2109
static void generateSalt(char *salt, size_t len) {
100,602✔
2110
  const char* set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
100,602✔
2111
  int32_t     setLen = 62;
100,602✔
2112
  for (int32_t i = 0; i < len - 1; ++i) {
3,219,264✔
2113
    salt[i] = set[taosSafeRand() % setLen];
3,118,662✔
2114
  }
2115
  salt[len - 1] = 0;
100,602✔
2116
}
100,602✔
2117

2118

2119

2120
static int32_t addDefaultIpToTable(int8_t enableIpv6, SHashObj *pUniqueTab) {
1,074✔
2121
  int32_t code = 0;
1,074✔
2122
  int32_t lino = 0;
1,074✔
2123
  int32_t dummpy = 0;
1,074✔
2124

2125
  SIpRange ipv4 = {0}, ipv6 = {0};
1,074✔
2126
  code = createDefaultIp4Range(&ipv4);
1,074✔
2127
  TSDB_CHECK_CODE(code, lino, _error);
1,074✔
2128

2129
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummpy, sizeof(dummpy));
1,074✔
2130
  TSDB_CHECK_CODE(code, lino, _error);
1,074✔
2131

2132
  if (enableIpv6) {
1,074✔
2133
    code = createDefaultIp6Range(&ipv6);
×
2134
    TSDB_CHECK_CODE(code, lino, _error);
×
2135

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

2146
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
99,880✔
2147
  int32_t  code = 0;
99,880✔
2148
  int32_t  lino = 0;
99,880✔
2149
  SUserObj userObj = {0};
99,880✔
2150

2151
  userObj.passwords = taosMemoryCalloc(1, sizeof(SUserPassword));
99,880✔
2152
  if (userObj.passwords == NULL) {
99,880✔
2153
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2154
  }
2155
  userObj.numOfPasswords = 1;
99,880✔
2156

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

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

2177
  userObj.createdTime = taosGetTimestampMs();
99,880✔
2178
  userObj.updateTime = userObj.createdTime;
99,880✔
2179
  userObj.superUser = 0;  // pCreate->superUser;
99,880✔
2180
  userObj.sysInfo = pCreate->sysInfo;
99,880✔
2181
  userObj.enable = pCreate->enable;
99,880✔
2182
  userObj.createdb = pCreate->createDb;
99,880✔
2183

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

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

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

2223
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,074✔
2224
      taosHashCleanup(pUniqueTab);
×
2225
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2226
    }
2227

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

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

2243
    taosHashCleanup(pUniqueTab);
1,074✔
2244
    p->num = numOfRanges;
1,074✔
2245
    sortIpWhiteList(p);
1,074✔
2246
    userObj.pIpWhiteListDual = p;
1,074✔
2247
  }
2248

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

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

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

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

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

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

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

2303
  userObj.ipWhiteListVer = taosGetTimestampMs();
99,880✔
2304
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
99,880✔
2305

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

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

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

2327
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
99,880✔
2328
    mndTransDrop(pTrans);
×
2329
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2330
  }
2331

2332
  mndTransDrop(pTrans);
99,880✔
2333

2334
_OVER:
99,880✔
2335
  taosMemoryFree(userObj.passwords);
99,880✔
2336
  taosMemoryFree(userObj.pIpWhiteListDual);
99,880✔
2337
  taosMemoryFree(userObj.pTimeWhiteList);
99,880✔
2338
  TAOS_RETURN(code);
99,880✔
2339
}
2340

2341

2342
static int32_t mndCheckPasswordFmt(const char *pwd) {
114,467✔
2343
  if (strcmp(pwd, "taosdata") == 0) {
114,467✔
2344
    return 0;
27,719✔
2345
  }
2346

2347
  if (tsEnableStrongPassword == 0) {
86,748✔
2348
    for (char c = *pwd; c != 0; c = *(++pwd)) {
98,640✔
2349
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
97,560✔
2350
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2351
      }
2352
    }
2353
    return 0;
1,080✔
2354
  }
2355

2356
  int32_t len = strlen(pwd);
85,668✔
2357
  if (len < TSDB_PASSWORD_MIN_LEN) {
85,668✔
2358
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
2359
  }
2360

2361
  if (len > TSDB_PASSWORD_MAX_LEN) {
85,668✔
2362
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
2363
  }
2364

2365
  if (taosIsComplexString(pwd)) {
85,668✔
2366
    return 0;
85,668✔
2367
  }
2368

2369
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2370
}
2371

2372

2373

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

2380
  if (taosIsComplexString(seed)) {
×
2381
    return 0;
×
2382
  }
2383

2384
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
2385
}
2386

2387

2388

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

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

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

2409
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
2410

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

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

2438
  TAOS_RETURN(code);
×
2439
  return 0;
2440
}
2441

2442

2443

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

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

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

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

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

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

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

2488

2489

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

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

2504
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
1,793✔
2505

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

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

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

2531
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
1,793✔
2532
  TAOS_RETURN(code);
1,793✔
2533
}
2534

2535

2536

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

2545
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
100,572✔
2546
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2547
  }
2548

2549
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
100,572✔
2550

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

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

2564
  if (createReq.user[0] == 0) {
100,572✔
2565
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2566
  }
2567

2568
  if (createReq.isImport != 1) {
100,572✔
2569
    code = mndCheckPasswordFmt(createReq.pass);
100,572✔
2570
    TAOS_CHECK_GOTO(code, &lino, _OVER);
100,572✔
2571
  }
2572

2573
  if (createReq.totpseed[0] != 0) {
100,572✔
2574
    code = mndCheckTotpSeedFmt(createReq.totpseed);
×
2575
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2576
  }
2577

2578
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
100,572✔
2579
  if (pUser != NULL) {
100,572✔
2580
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
692✔
2581
  }
2582

2583
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
99,880✔
2584
  if (pOperUser == NULL) {
99,880✔
2585
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2586
  }
2587

2588
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
99,880✔
2589

2590
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
99,880✔
2591
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
99,880✔
2592

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

2603
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
99,880✔
2604

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

2612
  mndReleaseUser(pMnode, pUser);
100,572✔
2613
  mndReleaseUser(pMnode, pOperUser);
100,572✔
2614
  tFreeSCreateUserReq(&createReq);
100,572✔
2615

2616
  TAOS_RETURN(code);
100,572✔
2617
}
2618

2619

2620

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

2631
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
137,920✔
2632
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserIpWhiteListRsp * pRsp) = NULL;
137,920✔
2633

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

2646
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
137,920✔
2647
  if (pUser == NULL) {
137,920✔
2648
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2649
  }
2650

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

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

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

2679
  TAOS_RETURN(code);
137,920✔
2680
}
2681

2682

2683

2684
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
1,793✔
2685
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,793✔
2686

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

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

2703
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
1,793✔
2704
    pUser->ver = userCache.verIp;
1,793✔
2705

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

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

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

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

2728

2729

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

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

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

2751
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
1,793✔
2752

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

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

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

2778
  tFreeSUpdateIpWhiteReq(&ipWhite);
1,793✔
2779
  TAOS_RETURN(code);
1,793✔
2780
}
2781

2782

2783

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

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

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

2811
  mndTransDrop(pTrans);
×
2812
}
2813

2814

2815

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

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

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

2850
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
12,994,016✔
2851
  int32_t code = 0;
12,994,016✔
2852

2853
  *ppNew =
12,994,469✔
2854
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
12,994,016✔
2855
  if (*ppNew == NULL) {
12,994,469✔
2856
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2857
    TAOS_RETURN(code);
×
2858
  }
2859

2860
  char *db = taosHashIterate(pOld, NULL);
12,994,469✔
2861
  while (db != NULL) {
14,144,443✔
2862
    int32_t len = strlen(db) + 1;
1,149,974✔
2863
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
1,149,974✔
2864
      taosHashCancelIterate(pOld, db);
×
2865
      taosHashCleanup(*ppNew);
×
2866
      TAOS_RETURN(code);
×
2867
    }
2868
    db = taosHashIterate(pOld, db);
1,149,974✔
2869
  }
2870

2871
  TAOS_RETURN(code);
12,994,469✔
2872
}
2873

2874
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
11,320,827✔
2875

2876
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
1,673,189✔
2877

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

2883
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
442,446✔
2884
  int32_t len = strlen(tbFName) + 1;
442,446✔
2885

2886
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
511,349✔
2887
    char *value = taosHashGet(hash, tbFName, len);
68,903✔
2888
    if (value != NULL) {
68,903✔
2889
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2890
    }
2891

2892
    int32_t condLen = alterReq->tagCondLen;
68,903✔
2893
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
68,903✔
2894
  } else {
2895
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
373,543✔
2896
  }
2897

2898
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
442,446✔
2899
  int32_t  ref = 1;
442,446✔
2900
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
442,446✔
2901
  if (NULL != currRef) {
442,446✔
2902
    ref = (*currRef) + 1;
256,839✔
2903
  }
2904
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
442,446✔
2905

2906
  TAOS_RETURN(0);
442,446✔
2907
}
2908

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

2916
  if (taosHashRemove(hash, tbFName, len) != 0) {
405,652✔
2917
    TAOS_RETURN(0);  // not found
1,347✔
2918
  }
2919

2920
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
404,305✔
2921
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
404,305✔
2922
  if (NULL == currRef) {
404,305✔
2923
    return 0;
×
2924
  }
2925

2926
  if (1 == *currRef) {
404,305✔
2927
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
172,424✔
2928
      TAOS_RETURN(0);  // not found
×
2929
    }
2930
    return 0;
172,424✔
2931
  }
2932
  int32_t ref = (*currRef) - 1;
231,881✔
2933
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
231,881✔
2934

2935
  return 0;
231,881✔
2936
}
2937

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

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

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

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

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

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

3065
  SHashObj *pReadTbs = pNewUser->readTbs;
876,890✔
3066
  SHashObj *pWriteTbs = pNewUser->writeTbs;
876,890✔
3067
  SHashObj *pAlterTbs = pNewUser->alterTbs;
876,890✔
3068

3069
#ifdef TD_ENTERPRISE
3070
  if (pAlterReq->isView) {
876,890✔
3071
    pReadTbs = pNewUser->readViews;
13,852✔
3072
    pWriteTbs = pNewUser->writeViews;
13,852✔
3073
    pAlterTbs = pNewUser->alterViews;
13,852✔
3074
  }
3075
#endif
3076

3077
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3078
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
701,017✔
3079
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
175,873✔
3080
  }
3081

3082
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3083
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
702,089✔
3084
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
174,801✔
3085
  }
3086

3087
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3088
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
785,118✔
3089
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
91,772✔
3090
  }
3091

3092
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3093
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
714,293✔
3094
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
162,597✔
3095
  }
3096

3097
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3098
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
713,496✔
3099
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
163,394✔
3100
  }
3101

3102
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
876,890✔
3103
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
797,229✔
3104
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
79,661✔
3105
  }
3106

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

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

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

3155
  mInfo("user:%s, start to alter", alterReq.user);
946,068✔
3156

3157
  if (alterReq.user[0] == 0) {
946,068✔
3158
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3159
  }
3160

3161
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
946,068✔
3162

3163
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
937,917✔
3164
  if (pOperUser == NULL) {
937,917✔
3165
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3166
  }
3167

3168
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
937,917✔
3169
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
919,172✔
3170

3171
  if (alterReq.hasPassword) {
919,172✔
3172
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(alterReq.pass), &lino, _OVER);
13,895✔
3173
    if (newUser.salt[0] == 0) {
13,895✔
3174
      generateSalt(newUser.salt, sizeof(newUser.salt));
722✔
3175
    }
3176
    char pass[TSDB_PASSWORD_LEN] = {0};
13,895✔
3177
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
13,895✔
3178
    pass[sizeof(pass) - 1] = 0;
13,895✔
3179
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
13,895✔
3180

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

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

3221
  if (alterReq.hasEnable) {
918,811✔
3222
    newUser.enable = alterReq.enable; // lock or unlock user manually
2,381✔
3223
    if (newUser.enable) {
2,381✔
3224
      // reset login info to allow login immediately
3225
      userCacheResetLoginInfo(newUser.user);
1,332✔
3226
    }
3227
  }
3228

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

3246

3247
  if (alterReq.numDropIpRanges > 0 || alterReq.numIpRanges > 0) {
918,811✔
3248
    int32_t dummy = 0;
714✔
3249

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

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

3266
    if (alterReq.numDropIpRanges > 0) {
714✔
3267
      for (int32_t i = 0; i < alterReq.numDropIpRanges; i++) {
714✔
3268
        if (taosHashGetSize(m) == 0) {
357✔
3269
          break;
×
3270
        }
3271

3272
        SIpRange range;
357✔
3273
        copyIpRange(&range, alterReq.pDropIpRanges + i);
357✔
3274

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

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

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

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

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

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

3324
    taosHashCleanup(m);
714✔
3325
    p->num = numOfRanges;
714✔
3326
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
714✔
3327
    sortIpWhiteList(p);
714✔
3328
    newUser.pIpWhiteListDual = p;
714✔
3329

3330
    newUser.ipWhiteListVer++;
714✔
3331
  }
3332

3333

3334
  if (alterReq.numTimeRanges > 0 || alterReq.numDropTimeRanges) {
918,811✔
3335
    int32_t dummy = 0;
×
3336

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

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

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

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

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

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

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

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

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

3420

3421
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
918,811✔
3422
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
880,384✔
3423
  }
3424

3425
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
914,960✔
3426
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
914,960✔
3427

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

3436
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
3437

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

3443
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
3444

3445
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3446

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

3454
    TAOS_CHECK_GOTO(mndEncryptPass(newUser.pass, &newUser.passEncryptAlgorithm), &lino, _OVER);
3455

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

3461
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
3462
    newUser.superUser = alterReq.superUser;
3463
  }
3464

3465
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
3466
    newUser.enable = alterReq.enable;
3467
  }
3468

3469
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
3470
    newUser.sysInfo = alterReq.sysInfo;
3471
  }
3472

3473
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3474
    newUser.createdb = alterReq.createdb;
3475
  }
3476

3477
   
3478

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3633
#endif
3634

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

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

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

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

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

3672
  userCacheRemoveUser(pUser->user);
58,928✔
3673

3674
  mndTransDrop(pTrans);
58,928✔
3675
  TAOS_RETURN(0);
58,928✔
3676
}
3677

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

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

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

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

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

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

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

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

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

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

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

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

3726
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,306,768✔
3727

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

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

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

3755
  TAOS_RETURN(code);
3,307,383✔
3756
}
3757

3758

3759
bool mndIsTotpEnabledUser(SUserObj *pUser) {
2,648,115✔
3760
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
87,335,061✔
3761
    if (pUser->totpsecret[i] != 0) {
84,688,719✔
3762
      return true;
×
3763
    }
3764
  }
3765
  return false;
2,646,342✔
3766
}
3767

3768

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

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

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

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

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

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

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

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

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

3818
    cols++;
52,697✔
3819

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

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

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

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

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

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

3861
    numOfRows++;
52,697✔
3862
    sdbRelease(pSdb, pUser);
52,697✔
3863
  }
3864

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4431
  for (int32_t i = 0; i < numOfUses; ++i) {
28,341,091✔
4432
    SUserObj *pUser = NULL;
14,333,396✔
4433
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
14,333,849✔
4434
    if (pUser == NULL) {
14,333,849✔
4435
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
5,772✔
4436
        SGetUserAuthRsp rsp = {.dropped = 1};
5,772✔
4437
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
5,772✔
4438
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
11,544✔
4439
      }
4440
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
5,772✔
4441
      code = 0;
5,772✔
4442
      continue;
8,101✔
4443
    }
4444

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

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

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

4468
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
14,007,695✔
4469
    *ppRsp = NULL;
13,336,498✔
4470
    *pRspLen = 0;
13,336,498✔
4471

4472
    tFreeSUserAuthBatchRsp(&batchRsp);
13,336,498✔
4473
    return 0;
13,336,498✔
4474
  }
4475

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

4498
  TAOS_RETURN(code);
670,744✔
4499
}
4500

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

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

4525
  while (1) {
764,358✔
4526
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,467,778✔
4527
    if (pIter == NULL) break;
1,467,778✔
4528

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

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

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

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

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

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

4630
  while (1) {
502,892✔
4631
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,005,784✔
4632
    if (pIter == NULL) break;
1,005,784✔
4633

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

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

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

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

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

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

4686
  while (1) {
184,781✔
4687
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
362,321✔
4688
    if (pIter == NULL) break;
362,321✔
4689

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

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

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

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

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

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

4742
  while (1) {
45,497✔
4743
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
76,528✔
4744
    if (pIter == NULL) {
76,528✔
4745
      break;
31,031✔
4746
    }
4747

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

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

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

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

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

4787
int64_t mndGetUserTimeWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4788
  // ver = 0, disable datetime white list
4789
  // ver > 0, enable datetime white list
4790
  return tsEnableWhiteList ? pUser->timeWhiteListVer : 0;
×
4791
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc