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

taosdata / TDengine / #4880

11 Dec 2025 02:43AM UTC coverage: 64.544%. Remained the same
#4880

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3380 existing lines in 25 files now uncovered.

163565 of 253417 relevant lines covered (64.54%)

105600506.39 hits per line

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

72.24
/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() {
493,644✔
158
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
493,644✔
159

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

165
  userCache.users = users;
493,644✔
166
  userCache.verIp = 0;
493,644✔
167
  userCache.verTime = 0;
493,644✔
168

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

173

174

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

180
  void *pIter = taosHashIterate(userCache.users, NULL);
492,862✔
181
  while (pIter) {
997,164✔
182
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
504,302✔
183
    if (pInfo != NULL) {
504,302✔
184
      taosMemoryFree(pInfo->wlIp);
504,302✔
185
      taosMemoryFree(pInfo->wlTime);
504,302✔
186
      taosMemoryFree(pInfo);
504,302✔
187
    }
188
    pIter = taosHashIterate(userCache.users, pIter);
504,302✔
189
  }
190
  taosHashCleanup(userCache.users);
492,862✔
191

192
  (void)taosThreadRwlockDestroy(&userCache.rw);
492,862✔
193
}
194

195

196

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

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

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

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

219

220

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

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

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

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

236

237

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

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

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

255
  return pInfo;
562,379✔
256
}
257

258

259

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

263
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,502,719✔
264

265
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,504,311✔
266
  if (ppInfo != NULL && *ppInfo != NULL) {
2,504,311✔
267
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,187,006✔
268
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,184,920✔
269
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,186,249✔
270
  } else {
271
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
317,541✔
272
    pLoginInfo->failedLoginCount = 0;
317,305✔
273
    pLoginInfo->lastFailedLoginTime = 0;
317,305✔
274
  }
275

276
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,502,483✔
277

278
  if (pLoginInfo->lastLoginTime == 0) {
2,504,311✔
279
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
45,656✔
280
  }
281
}
2,503,316✔
282

283

284

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

288
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,503,309✔
289

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

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

300

301

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

307
  if (a == NULL || b == NULL) {
1,005,388✔
308
    return false;
98,525✔
309
  }
310

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

315
  for (int i = 0; i < a->num; i++) {
906,863✔
UNCOV
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;
906,863✔
325
}
326

327

328

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

332
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,005,388✔
333

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

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

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

359
_OVER:
1,005,388✔
360
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,005,388✔
361
  if (code < 0) {
1,005,388✔
UNCOV
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,005,388✔
365
}
366

367

368

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

372
  SSdb     *pSdb = pMnode->pSdb;
599,136✔
373
  void     *pIter = NULL;
599,136✔
374
  while (1) {
256,910✔
375
    SUserObj *pUser = NULL;
856,046✔
376
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
856,046✔
377
    if (pIter == NULL) {
856,046✔
378
      break;
599,136✔
379
    }
380

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

398
    sdbRelease(pSdb, pUser);
256,910✔
399
  }
400

401
  userCache.verIp++;
599,136✔
402

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

410

411

412
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
38,982,745✔
413
  int64_t ver = 0;
38,982,745✔
414
  int32_t code = 0;
38,982,745✔
415

416
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
38,982,745✔
417
    (void)taosThreadRwlockWrlock(&userCache.rw);
12,705✔
418

419
    if (userCache.verIp == 0) {
12,705✔
420
      // get user and dnode ip white list
UNCOV
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
      }
UNCOV
426
      userCache.verIp = taosGetTimestampMs();
×
427
    }
428
    ver = userCache.verIp;
12,705✔
429

430
    (void)taosThreadRwlockUnlock(&userCache.rw);
12,705✔
431
  }
432

433
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
38,982,745✔
434
  return ver;
38,982,745✔
435
}
436

437

438

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

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

450
  TAOS_RETURN(code);
599,136✔
451
}
452

453

454

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

458
  SSdb     *pSdb = pMnode->pSdb;
587,565✔
459
  void     *pIter = NULL;
587,565✔
460
  while (1) {
245,339✔
461
    SUserObj *pUser = NULL;
832,904✔
462
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
832,904✔
463
    if (pIter == NULL) {
832,904✔
464
      break;
587,565✔
465
    }
466

467
    SCachedUserInfo *pInfo = getCachedUserInfo(pUser->user);
245,339✔
468
    if (pInfo == NULL) {
245,339✔
UNCOV
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);
245,339✔
475
    if (wl == NULL) {
245,339✔
UNCOV
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);
245,339✔
482
    pInfo->wlTime = wl;
245,339✔
483

484
    sdbRelease(pSdb, pUser);
245,339✔
485
  }
486

487
  userCache.verTime++;
587,565✔
488

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

496

497

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

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

509
  TAOS_RETURN(code);
587,565✔
510
}
511

512

513

514
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
38,982,745✔
515
  int64_t ver = 0;
38,982,745✔
516
  int32_t code = 0;
38,982,745✔
517

518
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
38,982,745✔
519
    (void)taosThreadRwlockWrlock(&userCache.rw);
12,705✔
520

521
    if (userCache.verIp == 0) {
12,705✔
522
      // get user and dnode datetime white list
UNCOV
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
      }
UNCOV
528
      userCache.verTime = taosGetTimestampMs();
×
529
    }
530
    ver = userCache.verTime;
12,705✔
531

532
    (void)taosThreadRwlockUnlock(&userCache.rw);
12,705✔
533
  }
534

535
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
38,982,745✔
536
  return ver;
38,982,745✔
537
}
538

539

540

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

544
  SSdbTable table = {
493,644✔
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);
493,644✔
556
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
493,644✔
557
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
493,644✔
558
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
493,644✔
559

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

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

576

577

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

582

583

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

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

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

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

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

604

605

606
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
51,210✔
607
  int32_t len = 0;
51,210✔
608
  for (int i = 0; i < num; i++) {
155,042✔
609
    SIpRange *pRange = &range[i];
103,832✔
610
    SIpAddr   addr = {0};
103,832✔
611
    (void)tIpUintToStr(pRange, &addr);
103,832✔
612

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

619

620

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

626
  if (a->type == 0) {
1,813,020✔
627
    SIpV4Range *a4 = &a->ipV4;
906,863✔
628
    SIpV4Range *b4 = &b->ipV4;
906,863✔
629
    return (a4->ip == b4->ip && a4->mask == b4->mask);
906,863✔
630
  }
631
  
632
  SIpV6Range *a6 = &a->ipV6;
906,157✔
633
  SIpV6Range *b6 = &b->ipV6;
906,157✔
634
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
906,157✔
635
}
636

637

638

639
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
1,005,388✔
640
  if (a == NULL && b == NULL) {
1,005,388✔
UNCOV
641
    return true;
×
642
  }
643
  
644
  if (a == NULL || b == NULL) {
1,005,388✔
645
    return false;
98,525✔
646
  }
647

648
  if (a->num != b->num) {
906,863✔
649
    return false;
353✔
650
  }
651
  for (int i = 0; i < a->num; i++) {
2,719,530✔
652
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,813,020✔
UNCOV
653
      return false;
×
654
    }
655
  }
656
  return true;
906,510✔
657
}
658

659

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

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

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

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

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

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

692

693

694
static int32_t convertIpWhiteListToStr(SUserObj *pUser, char **buf) {
51,210✔
695
  SIpWhiteListDual *pList = pUser->pIpWhiteListDual;
51,210✔
696

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

703
  if (pList->num == 0) {
51,210✔
UNCOV
704
    return tsnprintf(*buf, bufLen, "+ALL");
×
705
  }
706

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

715

716

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

724
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,473,623✔
725
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
4,947,246✔
726

727
  for (int i = 0; i < pList->num; i++) {
7,421,920✔
728
    SIpRange *pRange = &(pList->pIpRanges[i]);
4,948,297✔
729
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
4,948,297✔
730
  }
731

732
  tEndEncode(&encoder);
2,473,623✔
733

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

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

750
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,615,515✔
751
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
3,231,030✔
752

753
  for (int i = 0; i < pList->num; i++) {
4,847,247✔
754
    SIpRange *pRange = &(pList->pIpRanges[i]);
3,231,732✔
755
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange, supportNeg), &lino, _OVER);
3,231,732✔
756
  }
757

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

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

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

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

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

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

799
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,615,515✔
800
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
1,615,515✔
801

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

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

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

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

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

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

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

856
  SIpRange v4 = {0};
418,881✔
857
  SIpRange v6 = {0};
418,881✔
858

859
#ifndef TD_ASTRA
860
  code = createDefaultIp4Range(&v4);
418,881✔
861
  TSDB_CHECK_CODE(code, lino, _error);
418,881✔
862

863
  code = createDefaultIp6Range(&v6);
418,881✔
864
  TSDB_CHECK_CODE(code, lino, _error);
418,881✔
865

866
#endif
867

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

880

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

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

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

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

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

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

UNCOV
916
  return pos;
×
917
}
918

919

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

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

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

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

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

UNCOV
940
  return 0;
×
941
}
942

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

947

948

949

950
static void dropOldPasswords(SUserObj *pUser) {
4,089,138✔
951
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
4,089,138✔
952
    return;
4,038,690✔
953
  }
954

955
  int32_t reuseMax = pUser->passwordReuseMax;
50,448✔
956
  if (reuseMax == 0) {
50,448✔
957
    reuseMax = 1; // keep at least one password
46,878✔
958
  }
959

960
  int32_t now = taosGetTimestampSec();
50,448✔
961
  int32_t index = reuseMax;
50,448✔
962
  while(index < pUser->numOfPasswords) {
60,444✔
963
    SUserPassword *pPass = &pUser->passwords[index];
9,996✔
964
    if (now - pPass->setTime >= pUser->passwordReuseTime) {
9,996✔
UNCOV
965
      break;
×
966
    }
967
    index++;
9,996✔
968
  }
969

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

978

979

980

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

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

997
  userObj.passwords[0].setTime = taosGetTimestampSec();
321,777✔
998
  userObj.numOfPasswords = 1;
321,777✔
999

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

1041
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
321,777✔
1042
  if (pRaw == NULL) goto _ERROR;
321,777✔
1043
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
321,777✔
1044

1045
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
321,777✔
1046

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

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

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

1068
  mndTransDrop(pTrans);
321,777✔
1069
  taosMemoryFree(userObj.passwords);
321,777✔
1070
  taosMemoryFree(userObj.pIpWhiteListDual);
321,777✔
1071
  taosMemoryFree(userObj.pTimeWhiteList);
321,777✔
1072
  return 0;
321,777✔
1073

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

1081
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
321,777✔
1082
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
321,777✔
1083
}
1084

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

1106
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,473,623✔
1107
  while (stb != NULL) {
2,917,499✔
1108
    size_t keyLen = 0;
443,876✔
1109
    void  *key = taosHashGetKey(stb, &keyLen);
443,876✔
1110
    size += sizeof(int32_t);
443,876✔
1111
    size += keyLen;
443,876✔
1112

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

1120
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,473,623✔
1121
  while (stb != NULL) {
2,927,485✔
1122
    size_t keyLen = 0;
453,862✔
1123
    void  *key = taosHashGetKey(stb, &keyLen);
453,862✔
1124
    size += sizeof(int32_t);
453,862✔
1125
    size += keyLen;
453,862✔
1126

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

1134
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,473,623✔
1135
  while (stb != NULL) {
2,714,604✔
1136
    size_t keyLen = 0;
240,981✔
1137
    void  *key = taosHashGetKey(stb, &keyLen);
240,981✔
1138
    size += sizeof(int32_t);
240,981✔
1139
    size += keyLen;
240,981✔
1140

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

1148
  stb = taosHashIterate(pUser->readViews, NULL);
2,473,623✔
1149
  while (stb != NULL) {
2,521,490✔
1150
    size_t keyLen = 0;
47,867✔
1151
    void  *key = taosHashGetKey(stb, &keyLen);
47,867✔
1152
    size += sizeof(int32_t);
47,867✔
1153
    size += keyLen;
47,867✔
1154

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

1162
  stb = taosHashIterate(pUser->writeViews, NULL);
2,473,623✔
1163
  while (stb != NULL) {
2,510,234✔
1164
    size_t keyLen = 0;
36,611✔
1165
    void  *key = taosHashGetKey(stb, &keyLen);
36,611✔
1166
    size += sizeof(int32_t);
36,611✔
1167
    size += keyLen;
36,611✔
1168

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

1176
  stb = taosHashIterate(pUser->alterViews, NULL);
2,473,623✔
1177
  while (stb != NULL) {
2,509,634✔
1178
    size_t keyLen = 0;
36,011✔
1179
    void  *key = taosHashGetKey(stb, &keyLen);
36,011✔
1180
    size += sizeof(int32_t);
36,011✔
1181
    size += keyLen;
36,011✔
1182

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

1190
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,473,623✔
1191
  while (useDb != NULL) {
3,032,664✔
1192
    size_t keyLen = 0;
559,041✔
1193
    void  *key = taosHashGetKey(useDb, &keyLen);
559,041✔
1194
    size += sizeof(int32_t);
559,041✔
1195
    size += keyLen;
559,041✔
1196
    size += sizeof(int32_t);
559,041✔
1197
    useDb = taosHashIterate(pUser->useDbs, useDb);
559,041✔
1198
  }
1199

1200
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
2,473,623✔
1201
  if (pRaw == NULL) {
2,473,623✔
UNCOV
1202
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1203
  }
1204

1205
  int32_t dataPos = 0;
2,473,623✔
1206
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
2,473,623✔
1207

1208
  dropOldPasswords(pUser);
2,473,623✔
1209
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,473,623✔
1210
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
4,977,589✔
1211
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,503,966✔
1212
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,503,966✔
1213
  }
1214
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,473,623✔
1215

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

1229
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,473,623✔
1230
  while (db != NULL) {
2,803,082✔
1231
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
329,459✔
1232
    db = taosHashIterate(pUser->readDbs, db);
329,459✔
1233
  }
1234

1235
  db = taosHashIterate(pUser->writeDbs, NULL);
2,473,623✔
1236
  while (db != NULL) {
2,794,643✔
1237
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
321,020✔
1238
    db = taosHashIterate(pUser->writeDbs, db);
321,020✔
1239
  }
1240

1241
  char *topic = taosHashIterate(pUser->topics, NULL);
2,473,623✔
1242
  while (topic != NULL) {
2,490,383✔
1243
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
16,760✔
1244
    topic = taosHashIterate(pUser->topics, topic);
16,760✔
1245
  }
1246

1247
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
2,473,623✔
1248
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
2,473,623✔
1249
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
2,473,623✔
1250
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
2,473,623✔
1251
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
2,473,623✔
1252
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
2,473,623✔
1253
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
2,473,623✔
1254

1255
  stb = taosHashIterate(pUser->readTbs, NULL);
2,473,623✔
1256
  while (stb != NULL) {
2,917,499✔
1257
    size_t keyLen = 0;
443,876✔
1258
    void  *key = taosHashGetKey(stb, &keyLen);
443,876✔
1259
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
443,876✔
1260
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
443,876✔
1261

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

1269
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,473,623✔
1270
  while (stb != NULL) {
2,927,485✔
1271
    size_t keyLen = 0;
453,862✔
1272
    void  *key = taosHashGetKey(stb, &keyLen);
453,862✔
1273
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
453,862✔
1274
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
453,862✔
1275

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

1283
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,473,623✔
1284
  while (stb != NULL) {
2,714,604✔
1285
    size_t keyLen = 0;
240,981✔
1286
    void  *key = taosHashGetKey(stb, &keyLen);
240,981✔
1287
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
240,981✔
1288
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
240,981✔
1289

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

1297
  stb = taosHashIterate(pUser->readViews, NULL);
2,473,623✔
1298
  while (stb != NULL) {
2,521,490✔
1299
    size_t keyLen = 0;
47,867✔
1300
    void  *key = taosHashGetKey(stb, &keyLen);
47,867✔
1301
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
47,867✔
1302
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
47,867✔
1303

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

1311
  stb = taosHashIterate(pUser->writeViews, NULL);
2,473,623✔
1312
  while (stb != NULL) {
2,510,234✔
1313
    size_t keyLen = 0;
36,611✔
1314
    void  *key = taosHashGetKey(stb, &keyLen);
36,611✔
1315
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
36,611✔
1316
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,611✔
1317

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

1325
  stb = taosHashIterate(pUser->alterViews, NULL);
2,473,623✔
1326
  while (stb != NULL) {
2,509,634✔
1327
    size_t keyLen = 0;
36,011✔
1328
    void  *key = taosHashGetKey(stb, &keyLen);
36,011✔
1329
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
36,011✔
1330
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36,011✔
1331

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

1339
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,473,623✔
1340
  while (useDb != NULL) {
3,032,664✔
1341
    size_t keyLen = 0;
559,041✔
1342
    void  *key = taosHashGetKey(useDb, &keyLen);
559,041✔
1343
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
559,041✔
1344
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
559,041✔
1345

1346
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
559,041✔
1347
    useDb = taosHashIterate(pUser->useDbs, useDb);
559,041✔
1348
  }
1349

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

1359
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
2,473,623✔
1360
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
2,473,623✔
1361

1362
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
2,473,623✔
1363
  SDB_SET_INT8(pRaw, dataPos, pUser->passEncryptAlgorithm, _OVER);
2,473,623✔
1364

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

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

1390
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,473,623✔
1391
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,473,623✔
1392

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

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

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

1415
  int8_t sver = 0;
1,615,515✔
1416
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
1,615,515✔
UNCOV
1417
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1418
  }
1419

1420
  if (sver < 1 || sver > USER_VER_NUMBER) {
1,615,515✔
UNCOV
1421
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1422
  }
1423

1424
  pRow = sdbAllocRow(sizeof(SUserObj));
1,615,515✔
1425
  if (pRow == NULL) {
1,615,515✔
UNCOV
1426
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1427
  }
1428

1429
  pUser = sdbGetRowObj(pRow);
1,615,515✔
1430
  if (pUser == NULL) {
1,615,515✔
UNCOV
1431
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1432
  }
1433

1434
  int32_t dataPos = 0;
1,615,515✔
1435
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
1,615,515✔
1436

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

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

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

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

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

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

1507
  if (sver >= 2) {
1,615,515✔
1508
    for (int32_t i = 0; i < numOfTopics; ++i) {
1,630,872✔
1509
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
15,357✔
1510
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
15,357✔
1511
      int32_t len = strlen(topic) + 1;
15,357✔
1512
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
15,357✔
1513
    }
1514
  }
1515

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

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

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

1548
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,615,515✔
1549

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

1556
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,031,192✔
1557
      int32_t keyLen = 0;
415,677✔
1558
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
415,677✔
1559

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

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

1576
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
415,677✔
1577
    }
1578

1579
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
2,040,931✔
1580
      int32_t keyLen = 0;
425,416✔
1581
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
425,416✔
1582

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

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

1599
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
425,416✔
1600
    }
1601

1602
    if (sver >= 6) {
1,615,515✔
1603
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,841,488✔
1604
        int32_t keyLen = 0;
225,973✔
1605
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
225,973✔
1606

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

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

1623
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
225,973✔
1624
      }
1625

1626
      for (int32_t i = 0; i < numOfReadViews; ++i) {
1,663,382✔
1627
        int32_t keyLen = 0;
47,867✔
1628
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
47,867✔
1629

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

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

1646
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
47,867✔
1647
      }
1648

1649
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
1,652,126✔
1650
        int32_t keyLen = 0;
36,611✔
1651
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36,611✔
1652

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

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

1669
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
36,611✔
1670
      }
1671

1672
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
1,651,526✔
1673
        int32_t keyLen = 0;
36,011✔
1674
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36,011✔
1675

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

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

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

1696
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
2,133,590✔
1697
      int32_t keyLen = 0;
518,075✔
1698
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
518,075✔
1699

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

1707
      int32_t ref = 0;
518,075✔
1708
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
518,075✔
1709

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

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

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

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

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

UNCOV
1736
      taosMemoryFreeClear(pIpWhiteList);
×
1737

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

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

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

1753
  if (pUser->pIpWhiteListDual == NULL) {
1,615,515✔
UNCOV
1754
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
1755
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1756
  }
1757

1758
  SDB_GET_INT8(pRaw, dataPos, &pUser->passEncryptAlgorithm, _OVER);
1,615,515✔
1759

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

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

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

1814
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
1,615,515✔
1815
  taosInitRWLatch(&pUser->lock);
1,615,515✔
1816
  dropOldPasswords(pUser);
1,615,515✔
1817

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

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

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

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

1859
  return 0;
609,342✔
1860
}
1861

1862
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
38,338,431✔
1863
  int32_t code = 0;
38,338,431✔
1864
  *ppNew =
38,339,190✔
1865
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
38,338,431✔
1866
  if (*ppNew == NULL) {
38,339,190✔
UNCOV
1867
    TAOS_RETURN(terrno);
×
1868
  }
1869

1870
  char *tb = taosHashIterate(pOld, NULL);
38,339,190✔
1871
  while (tb != NULL) {
40,688,189✔
1872
    size_t keyLen = 0;
2,348,999✔
1873
    char  *key = taosHashGetKey(tb, &keyLen);
2,348,999✔
1874

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

1884
  TAOS_RETURN(code);
38,339,190✔
1885
}
1886

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

1895
  int32_t *db = taosHashIterate(pOld, NULL);
1,661,274✔
1896
  while (db != NULL) {
2,158,808✔
1897
    size_t keyLen = 0;
497,534✔
1898
    char  *key = taosHashGetKey(db, &keyLen);
497,534✔
1899

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

1908
  TAOS_RETURN(code);
1,661,274✔
1909
}
1910

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

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

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

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

1955
  pNew->pTimeWhiteList = cloneDateTimeWhiteList(pUser->pTimeWhiteList);
1,661,274✔
1956
  if (pNew->pTimeWhiteList == NULL) {
1,661,274✔
UNCOV
1957
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1958
    goto _OVER;
×
1959
  }
1960

1961
_OVER:
1,661,274✔
1962
  taosRUnLockLatch(&pUser->lock);
1,661,274✔
1963
  TAOS_RETURN(code);
1,661,274✔
1964
}
1965

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

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

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

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

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

2038
  TSWAP(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual);
936,616✔
2039
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
936,616✔
2040
  TSWAP(pOld->pTimeWhiteList, pNew->pTimeWhiteList);
936,616✔
2041
  pOld->timeWhiteListVer = pNew->timeWhiteListVer;
936,616✔
2042
  pOld->passEncryptAlgorithm = pNew->passEncryptAlgorithm;
936,616✔
2043

2044
  taosWUnLockLatch(&pOld->lock);
936,616✔
2045

2046
  return 0;
936,616✔
2047
}
2048

2049
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
79,776,227✔
2050
  int32_t code = 0;
79,776,227✔
2051
  SSdb   *pSdb = pMnode->pSdb;
79,776,227✔
2052

2053
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
79,776,809✔
2054
  if (*ppUser == NULL) {
79,776,286✔
2055
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
116,475✔
2056
      code = TSDB_CODE_MND_USER_NOT_EXIST;
116,475✔
2057
    } else {
UNCOV
2058
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2059
    }
2060
  }
2061
  TAOS_RETURN(code);
79,776,876✔
2062
}
2063

2064
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
79,815,253✔
2065
  SSdb *pSdb = pMnode->pSdb;
79,815,253✔
2066
  sdbRelease(pSdb, pUser);
79,815,253✔
2067
}
79,816,511✔
2068

2069

2070

2071
int32_t mndEncryptPass(char *pass, const char* salt, int8_t *algo) {
112,332✔
2072
  int32_t code = 0;
112,332✔
2073
  if (tsiEncryptPassAlgorithm != DND_CA_SM4) {
112,332✔
2074
    return 0;
111,618✔
2075
  }
2076

2077
  if (strlen(tsEncryptKey) == 0) {
714✔
UNCOV
2078
    return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
2079
  }
2080

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

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

2097
  memcpy(pass, packetData, newLen);
714✔
2098
  if (algo != NULL) {
714✔
2099
    *algo = DND_CA_SM4;
357✔
2100
  }
2101

2102
  return 0;
714✔
2103
}
2104

2105

2106

2107
static void generateSalt(char *salt, size_t len) {
98,883✔
2108
  const char* set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
98,883✔
2109
  int32_t     setLen = 62;
98,883✔
2110
  for (int32_t i = 0; i < len - 1; ++i) {
3,164,256✔
2111
    salt[i] = set[taosSafeRand() % setLen];
3,065,373✔
2112
  }
2113
  salt[len - 1] = 0;
98,883✔
2114
}
98,883✔
2115

2116

2117

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

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

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

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

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

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

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

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

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

2175
  userObj.createdTime = taosGetTimestampMs();
98,167✔
2176
  userObj.updateTime = userObj.createdTime;
98,167✔
2177
  userObj.superUser = 0;  // pCreate->superUser;
98,167✔
2178
  userObj.sysInfo = pCreate->sysInfo;
98,167✔
2179
  userObj.enable = pCreate->enable;
98,167✔
2180
  userObj.createdb = pCreate->createDb;
98,167✔
2181

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

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

2215
    code = addDefaultIpToTable(tsEnableIpv6, pUniqueTab);
1,063✔
2216
    if (code != 0) {
1,063✔
UNCOV
2217
      taosHashCleanup(pUniqueTab);
×
UNCOV
2218
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2219
    }
2220

2221
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,063✔
2222
      taosHashCleanup(pUniqueTab);
×
UNCOV
2223
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
2224
    }
2225

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

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

2241
    taosHashCleanup(pUniqueTab);
1,063✔
2242
    p->num = numOfRanges;
1,063✔
2243
    sortIpWhiteList(p);
1,063✔
2244
    userObj.pIpWhiteListDual = p;
1,063✔
2245
  }
2246

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

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

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

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

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

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

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

2301
  userObj.ipWhiteListVer = taosGetTimestampMs();
98,167✔
2302
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
98,167✔
2303

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

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

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

2325
  if ((code = userCacheUpdateWhiteList(pMnode, &userObj)) != 0) {
98,167✔
2326
    mndTransDrop(pTrans);
×
UNCOV
2327
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2328
  }
2329

2330
  mndTransDrop(pTrans);
98,167✔
2331

2332
_OVER:
98,167✔
2333
  taosMemoryFree(userObj.passwords);
98,167✔
2334
  taosMemoryFree(userObj.pIpWhiteListDual);
98,167✔
2335
  taosMemoryFree(userObj.pTimeWhiteList);
98,167✔
2336
  TAOS_RETURN(code);
98,167✔
2337
}
2338

2339

2340
static int32_t mndCheckPasswordFmt(const char *pwd) {
112,675✔
2341
  if (strcmp(pwd, "taosdata") == 0) {
112,675✔
2342
    return 0;
27,594✔
2343
  }
2344

2345
  if (tsEnableStrongPassword == 0) {
85,081✔
2346
    for (char c = *pwd; c != 0; c = *(++pwd)) {
97,818✔
2347
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
96,747✔
UNCOV
2348
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2349
      }
2350
    }
2351
    return 0;
1,071✔
2352
  }
2353

2354
  int32_t len = strlen(pwd);
84,010✔
2355
  if (len < TSDB_PASSWORD_MIN_LEN) {
84,010✔
UNCOV
2356
    return TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY;
×
2357
  }
2358

2359
  if (len > TSDB_PASSWORD_MAX_LEN) {
84,010✔
UNCOV
2360
    return TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
×
2361
  }
2362

2363
  if (taosIsComplexString(pwd)) {
84,010✔
2364
    return 0;
84,010✔
2365
  }
2366

NEW
2367
  return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2368
}
2369

2370

2371

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

NEW
2378
  if (taosIsComplexString(seed)) {
×
NEW
2379
    return 0;
×
2380
  }
2381

NEW
2382
  return TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
2383
}
2384

2385

2386

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

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

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

2407
  TAOS_CHECK_GOTO(mndSetUserDateTimeWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
2408

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

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

2436
  TAOS_RETURN(code);
×
2437
  return 0;
2438
}
2439

2440

2441

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

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

UNCOV
2461
    SUserDateTimeWhiteList *pUser = &pRsp->pUsers[count];
×
UNCOV
2462
    pUser->ver = userCache.verTime;
×
2463

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

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

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

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

2486

2487

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

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

2502
  TAOS_CHECK_GOTO(buildRetrieveDateTimeWhiteListRsp(&wlRsp), &lino, _OVER);
1,797✔
2503

2504
  len = tSerializeSRetrieveDateTimeWhiteListRsp(NULL, 0, &wlRsp);
1,797✔
2505
  if (len < 0) {
1,797✔
UNCOV
2506
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2507
  }
2508

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

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

2529
  tFreeSRetrieveDateTimeWhiteListRsp(&wlRsp);
1,797✔
2530
  TAOS_RETURN(code);
1,797✔
2531
}
2532

2533

2534

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

2543
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
98,867✔
UNCOV
2544
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2545
  }
2546

2547
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
98,867✔
2548

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

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

2562
  if (createReq.user[0] == 0) {
98,867✔
UNCOV
2563
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2564
  }
2565

2566
  if (createReq.isImport != 1) {
98,867✔
2567
    code = mndCheckPasswordFmt(createReq.pass);
98,867✔
2568
    TAOS_CHECK_GOTO(code, &lino, _OVER);
98,867✔
2569
  }
2570

2571
  if (createReq.totpseed[0] != 0) {
98,867✔
NEW
2572
    code = mndCheckTotpSeedFmt(createReq.totpseed);
×
NEW
2573
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2574
  }
2575

2576
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
98,867✔
2577
  if (pUser != NULL) {
98,867✔
2578
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
700✔
2579
  }
2580

2581
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
98,167✔
2582
  if (pOperUser == NULL) {
98,167✔
UNCOV
2583
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2584
  }
2585

2586
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
98,167✔
2587

2588
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
98,167✔
2589
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
98,167✔
2590

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

2601
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
98,167✔
2602

2603
_OVER:
98,867✔
2604
  if (code == TSDB_CODE_MND_USER_ALREADY_EXIST && createReq.ignoreExisting) {
98,867✔
UNCOV
2605
    code = 0;
×
2606
  } else if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
98,867✔
2607
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
700✔
2608
  }
2609

2610
  mndReleaseUser(pMnode, pUser);
98,867✔
2611
  mndReleaseUser(pMnode, pOperUser);
98,867✔
2612
  tFreeSCreateUserReq(&createReq);
98,867✔
2613

2614
  TAOS_RETURN(code);
98,867✔
2615
}
2616

2617

2618

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

2629
  int32_t (*serialFn)(void *, int32_t, SGetUserIpWhiteListRsp *) = NULL;
136,693✔
2630
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserIpWhiteListRsp * pRsp) = NULL;
136,693✔
2631

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

2644
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
136,693✔
2645
  if (pUser == NULL) {
136,693✔
UNCOV
2646
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2647
  }
2648

2649
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
136,693✔
2650
  contLen = serialFn(NULL, 0, &wlRsp);
136,693✔
2651
  if (contLen < 0) {
136,693✔
UNCOV
2652
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2653
  }
2654
  pRsp = rpcMallocCont(contLen);
136,693✔
2655
  if (pRsp == NULL) {
136,693✔
UNCOV
2656
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2657
  }
2658

2659
  contLen = serialFn(pRsp, contLen, &wlRsp);
136,693✔
2660
  if (contLen < 0) {
136,693✔
UNCOV
2661
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2662
  }
2663

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

2677
  TAOS_RETURN(code);
136,693✔
2678
}
2679

2680

2681

2682
static int32_t buildRetrieveIpWhiteListRsp(SUpdateIpWhite *pUpdate) {
1,797✔
2683
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,797✔
2684

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

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

2701
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[count];
1,797✔
2702
    pUser->ver = userCache.verIp;
1,797✔
2703

2704
    size_t klen;
1,797✔
2705
    char  *key = taosHashGetKey(pIter, &klen);
1,797✔
2706
    (void)memcpy(pUser->user, key, klen);
1,797✔
2707

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

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

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

2726

2727

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

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

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

2749
  TAOS_CHECK_GOTO(buildRetrieveIpWhiteListRsp(&ipWhite), &lino, _OVER);
1,797✔
2750

2751
  len = fn(NULL, 0, &ipWhite);
1,797✔
2752
  if (len < 0) {
1,797✔
UNCOV
2753
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2754
  }
2755

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

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

2776
  tFreeSUpdateIpWhiteReq(&ipWhite);
1,797✔
2777
  TAOS_RETURN(code);
1,797✔
2778
}
2779

2780

2781

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

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

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

UNCOV
2809
  mndTransDrop(pTrans);
×
2810
}
2811

2812

2813

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

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

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

2848
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
13,089,978✔
2849
  int32_t code = 0;
13,089,978✔
2850

2851
  *ppNew =
13,089,978✔
2852
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
13,089,978✔
2853
  if (*ppNew == NULL) {
13,089,978✔
UNCOV
2854
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2855
    TAOS_RETURN(code);
×
2856
  }
2857

2858
  char *db = taosHashIterate(pOld, NULL);
13,089,978✔
2859
  while (db != NULL) {
14,237,351✔
2860
    int32_t len = strlen(db) + 1;
1,147,373✔
2861
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
1,147,373✔
2862
      taosHashCancelIterate(pOld, db);
×
2863
      taosHashCleanup(*ppNew);
×
UNCOV
2864
      TAOS_RETURN(code);
×
2865
    }
2866
    db = taosHashIterate(pOld, db);
1,147,373✔
2867
  }
2868

2869
  TAOS_RETURN(code);
13,089,978✔
2870
}
2871

2872
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
11,428,704✔
2873

2874
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
1,661,274✔
2875

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

2881
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
448,307✔
2882
  int32_t len = strlen(tbFName) + 1;
448,307✔
2883

2884
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
526,474✔
2885
    char *value = taosHashGet(hash, tbFName, len);
78,167✔
2886
    if (value != NULL) {
78,167✔
UNCOV
2887
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2888
    }
2889

2890
    int32_t condLen = alterReq->tagCondLen;
78,167✔
2891
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
78,167✔
2892
  } else {
2893
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
370,140✔
2894
  }
2895

2896
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
448,307✔
2897
  int32_t  ref = 1;
448,307✔
2898
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
448,307✔
2899
  if (NULL != currRef) {
448,307✔
2900
    ref = (*currRef) + 1;
264,176✔
2901
  }
2902
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
448,307✔
2903

2904
  TAOS_RETURN(0);
448,307✔
2905
}
2906

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

2914
  if (taosHashRemove(hash, tbFName, len) != 0) {
411,662✔
2915
    TAOS_RETURN(0);  // not found
1,338✔
2916
  }
2917

2918
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
410,324✔
2919
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
410,324✔
2920
  if (NULL == currRef) {
410,324✔
UNCOV
2921
    return 0;
×
2922
  }
2923

2924
  if (1 == *currRef) {
410,324✔
2925
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
170,992✔
UNCOV
2926
      TAOS_RETURN(0);  // not found
×
2927
    }
2928
    return 0;
170,992✔
2929
  }
2930
  int32_t ref = (*currRef) - 1;
239,332✔
2931
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
239,332✔
2932

2933
  return 0;
239,332✔
2934
}
2935

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

2957
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
872,878✔
2958
  SSdb   *pSdb = pMnode->pSdb;
872,878✔
2959
  void   *pIter = NULL;
872,878✔
2960
  int32_t code = 0;
872,878✔
2961
  int32_t lino = 0;
872,878✔
2962

2963
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
872,878✔
2964
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
746,960✔
2965
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
125,918✔
2966
      int32_t len = strlen(pAlterReq->objname) + 1;
122,397✔
2967
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
122,397✔
2968
      if (pDb == NULL) {
122,397✔
2969
        mndReleaseDb(pMnode, pDb);
2,785✔
2970
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
2,785✔
2971
      }
2972
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
119,612✔
2973
          0) {
UNCOV
2974
        mndReleaseDb(pMnode, pDb);
×
UNCOV
2975
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2976
      }
2977
      mndReleaseDb(pMnode, pDb);
119,612✔
2978
    } else {
2979
      while (1) {
5,663✔
2980
        SDbObj *pDb = NULL;
9,184✔
2981
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
9,184✔
2982
        if (pIter == NULL) break;
9,184✔
2983
        int32_t len = strlen(pDb->name) + 1;
5,663✔
2984
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,663✔
UNCOV
2985
          sdbRelease(pSdb, pDb);
×
UNCOV
2986
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
2987
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2988
        }
2989
        sdbRelease(pSdb, pDb);
5,663✔
2990
      }
2991
    }
2992
  }
2993

2994
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
870,093✔
2995
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
748,431✔
2996
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
121,662✔
2997
      int32_t len = strlen(pAlterReq->objname) + 1;
118,491✔
2998
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
118,491✔
2999
      if (pDb == NULL) {
118,491✔
3000
        mndReleaseDb(pMnode, pDb);
357✔
3001
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
357✔
3002
      }
3003
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
118,134✔
3004
          0) {
UNCOV
3005
        mndReleaseDb(pMnode, pDb);
×
UNCOV
3006
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3007
      }
3008
      mndReleaseDb(pMnode, pDb);
118,134✔
3009
    } else {
3010
      while (1) {
5,313✔
3011
        SDbObj *pDb = NULL;
8,484✔
3012
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
8,484✔
3013
        if (pIter == NULL) break;
8,484✔
3014
        int32_t len = strlen(pDb->name) + 1;
5,313✔
3015
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,313✔
UNCOV
3016
          sdbRelease(pSdb, pDb);
×
UNCOV
3017
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
3018
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3019
        }
3020
        sdbRelease(pSdb, pDb);
5,313✔
3021
      }
3022
    }
3023
  }
3024

3025
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,736✔
3026
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
757,289✔
3027
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
112,447✔
3028
      int32_t len = strlen(pAlterReq->objname) + 1;
108,926✔
3029
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
108,926✔
3030
      if (pDb == NULL) {
108,926✔
3031
        mndReleaseDb(pMnode, pDb);
350✔
3032
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
350✔
3033
      }
3034
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
108,576✔
3035
      if (code < 0) {
108,576✔
UNCOV
3036
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3037
      }
3038
      mndReleaseDb(pMnode, pDb);
108,576✔
3039
    } else {
3040
      taosHashClear(pNewUser->readDbs);
3,521✔
3041
    }
3042
  }
3043

3044
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3045
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
757,262✔
3046
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
112,124✔
3047
      int32_t len = strlen(pAlterReq->objname) + 1;
108,253✔
3048
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
108,253✔
3049
      if (pDb == NULL) {
108,253✔
UNCOV
3050
        mndReleaseDb(pMnode, pDb);
×
UNCOV
3051
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
3052
      }
3053
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
108,253✔
3054
      if (code < 0) {
108,253✔
UNCOV
3055
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3056
      }
3057
      mndReleaseDb(pMnode, pDb);
108,253✔
3058
    } else {
3059
      taosHashClear(pNewUser->writeDbs);
3,871✔
3060
    }
3061
  }
3062

3063
  SHashObj *pReadTbs = pNewUser->readTbs;
869,386✔
3064
  SHashObj *pWriteTbs = pNewUser->writeTbs;
869,386✔
3065
  SHashObj *pAlterTbs = pNewUser->alterTbs;
869,386✔
3066

3067
#ifdef TD_ENTERPRISE
3068
  if (pAlterReq->isView) {
869,386✔
3069
    pReadTbs = pNewUser->readViews;
13,805✔
3070
    pWriteTbs = pNewUser->writeViews;
13,805✔
3071
    pAlterTbs = pNewUser->alterViews;
13,805✔
3072
  }
3073
#endif
3074

3075
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3076
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
692,590✔
3077
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
176,796✔
3078
  }
3079

3080
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3081
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
693,668✔
3082
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
175,718✔
3083
  }
3084

3085
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3086
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
773,593✔
3087
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
95,793✔
3088
  }
3089

3090
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3091
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
705,823✔
3092
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
163,563✔
3093
  }
3094

3095
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3096
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
705,023✔
3097
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
164,363✔
3098
  }
3099

3100
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
869,386✔
3101
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
785,650✔
3102
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
83,736✔
3103
  }
3104

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

3120
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
869,386✔
3121
    int32_t      len = strlen(pAlterReq->objname) + 1;
4,018✔
3122
    SMqTopicObj *pTopic = NULL;
4,018✔
3123
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
4,018✔
3124
      mndReleaseTopic(pMnode, pTopic);
354✔
3125
      TAOS_CHECK_GOTO(code, &lino, _OVER);
354✔
3126
    }
3127
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
3,664✔
3128
    if (code < 0) {
3,664✔
UNCOV
3129
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
3130
    }
3131
    mndReleaseTopic(pMnode, pTopic);
3,664✔
3132
  }
3133
#endif
3134
_OVER:
872,878✔
3135
  if (code < 0) {
872,878✔
3136
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
3,846✔
3137
  }
3138
  TAOS_RETURN(code);
872,878✔
3139
}
3140

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

3153
  mInfo("user:%s, start to alter", alterReq.user);
938,154✔
3154

3155
  if (alterReq.user[0] == 0) {
938,154✔
UNCOV
3156
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3157
  }
3158

3159
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
938,154✔
3160

3161
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
930,058✔
3162
  if (pOperUser == NULL) {
930,058✔
UNCOV
3163
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
3164
  }
3165

3166
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
930,058✔
3167
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
911,425✔
3168

3169
  if (alterReq.hasPassword) {
911,425✔
3170
    TAOS_CHECK_GOTO(mndCheckPasswordFmt(alterReq.pass), &lino, _OVER);
13,808✔
3171
    if (newUser.salt[0] == 0) {
13,808✔
3172
      generateSalt(newUser.salt, sizeof(newUser.salt));
716✔
3173
    }
3174
    char pass[TSDB_PASSWORD_LEN] = {0};
13,808✔
3175
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
13,808✔
3176
    pass[sizeof(pass) - 1] = 0;
13,808✔
3177
    TAOS_CHECK_GOTO(mndEncryptPass(pass, newUser.salt, &newUser.passEncryptAlgorithm), &lino, _OVER);
13,808✔
3178

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

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

3219
  if (alterReq.hasEnable) {
911,067✔
3220
    newUser.enable = alterReq.enable; // lock or unlock user manually
2,371✔
3221
    if (newUser.enable) {
2,371✔
3222
      // reset login info to allow login immediately
3223
      userCacheResetLoginInfo(newUser.user);
1,323✔
3224
    }
3225
  }
3226

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

3244

3245
  if (alterReq.numDropIpRanges > 0 || alterReq.numIpRanges > 0) {
911,067✔
3246
    int32_t dummy = 0;
706✔
3247

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

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

3264
    if (alterReq.numDropIpRanges > 0) {
706✔
3265
      for (int32_t i = 0; i < alterReq.numDropIpRanges; i++) {
706✔
3266
        if (taosHashGetSize(m) == 0) {
353✔
UNCOV
3267
          break;
×
3268
        }
3269

3270
        SIpRange range;
353✔
3271
        copyIpRange(&range, alterReq.pDropIpRanges + i);
353✔
3272

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

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

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

3300
    int32_t numOfRanges = taosHashGetSize(m);
706✔
3301
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
706✔
UNCOV
3302
      taosHashCleanup(m);
×
UNCOV
3303
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
×
3304
    }
3305

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

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

3322
    taosHashCleanup(m);
706✔
3323
    p->num = numOfRanges;
706✔
3324
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
706✔
3325
    sortIpWhiteList(p);
706✔
3326
    newUser.pIpWhiteListDual = p;
706✔
3327

3328
    newUser.ipWhiteListVer++;
706✔
3329
  }
3330

3331

3332
  if (alterReq.numTimeRanges > 0 || alterReq.numDropTimeRanges) {
911,067✔
UNCOV
3333
    int32_t dummy = 0;
×
3334

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

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

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

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

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

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

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

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

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

3418

3419
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
911,067✔
3420
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
872,878✔
3421
  }
3422

3423
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
907,221✔
3424
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
907,221✔
3425

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

3434
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
3435

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

3441
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
3442

3443
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
3444

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

3452
    TAOS_CHECK_GOTO(mndEncryptPass(newUser.pass, &newUser.passEncryptAlgorithm), &lino, _OVER);
3453

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

3459
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
3460
    newUser.superUser = alterReq.superUser;
3461
  }
3462

3463
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
3464
    newUser.enable = alterReq.enable;
3465
  }
3466

3467
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
3468
    newUser.sysInfo = alterReq.sysInfo;
3469
  }
3470

3471
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
3472
    newUser.createdb = alterReq.createdb;
3473
  }
3474

3475
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
3476
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
3477
  }
3478

3479
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_ALLOWED_HOST) {
3480
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3481

3482
    int32_t           num = pUser->pIpWhiteListDual->num + alterReq.numIpRanges;
3483
    int32_t           idx = pUser->pIpWhiteListDual->num;
3484
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3485

3486
    if (pNew == NULL) {
3487
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3488
    }
3489

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

3518
    if (pNew->num > MND_MAX_USER_IP_RANGE) {
3519
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_IP_RANGE, &lino, _OVER);
3520
    }
3521
  }
3522
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_ALLOWED_HOST) {
3523
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
3524

3525
    int32_t           num = pUser->pIpWhiteListDual->num;
3526
    bool              noexist = true;
3527
    bool              localHost = false;
3528
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
3529

3530
    if (pNew == NULL) {
3531
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
3532
    }
3533

3534
    if (pUser->pIpWhiteListDual->num > 0) {
3535
      int idx = 0;
3536
      for (int i = 0; i < pUser->pIpWhiteListDual->num; i++) {
3537
        SIpRange *oldRange = &pUser->pIpWhiteListDual->pIpRanges[i];
3538

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

3549
          if (isDefaultRange(&range)) {
3550
            localHost = true;
3551
            break;
3552
          }
3553
          if (isIpRangeEqual(oldRange, &range)) {
3554
            found = true;
3555
            break;
3556
          }
3557
        }
3558
        if (localHost) break;
3559

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

3571
    } else {
3572
      pNew->num = 0;
3573
      newUser.pIpWhiteListDual = pNew;
3574
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
3575
    }
3576

3577
    if (localHost) {
3578
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
3579
    }
3580
    if (noexist) {
3581
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
3582
    }
3583
  }
3584

3585
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
3586
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
3587

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

3629
#endif
3630

3631
_OVER:
938,154✔
3632
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
938,154✔
3633
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
30,933✔
3634
  }
3635

3636
  tFreeSAlterUserReq(&alterReq);
938,154✔
3637
  mndReleaseUser(pMnode, pOperUser);
938,154✔
3638
  mndReleaseUser(pMnode, pUser);
938,154✔
3639
  mndUserFreeObj(&newUser);
938,154✔
3640
  TAOS_RETURN(code);
938,154✔
3641
}
3642

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

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

3662
  if (mndTransPrepare(pMnode, pTrans) != 0) {
58,077✔
UNCOV
3663
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
UNCOV
3664
    mndTransDrop(pTrans);
×
3665
    TAOS_RETURN(terrno);
×
3666
  }
3667

3668
  userCacheRemoveUser(pUser->user);
58,077✔
3669

3670
  mndTransDrop(pTrans);
58,077✔
3671
  TAOS_RETURN(0);
58,077✔
3672
}
3673

3674
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
58,422✔
3675
  SMnode      *pMnode = pReq->info.node;
58,422✔
3676
  int32_t      code = 0;
58,422✔
3677
  int32_t      lino = 0;
58,422✔
3678
  SUserObj    *pUser = NULL;
58,422✔
3679
  SDropUserReq dropReq = {0};
58,422✔
3680

3681
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
58,422✔
3682

3683
  mInfo("user:%s, start to drop", dropReq.user);
58,422✔
3684
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
58,422✔
3685

3686
  if (dropReq.user[0] == 0) {
58,422✔
UNCOV
3687
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
3688
  }
3689

3690
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
58,422✔
3691

3692
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
58,077✔
3693
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
58,077✔
3694

3695
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
58,077✔
3696

3697
_OVER:
58,422✔
3698
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
58,422✔
3699
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
345✔
3700
  }
3701

3702
  mndReleaseUser(pMnode, pUser);
58,422✔
3703
  tFreeSDropUserReq(&dropReq);
58,422✔
3704
  TAOS_RETURN(code);
58,422✔
3705
}
3706

3707
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
3,307,211✔
3708
  SMnode         *pMnode = pReq->info.node;
3,307,211✔
3709
  int32_t         code = 0;
3,307,211✔
3710
  int32_t         lino = 0;
3,307,211✔
3711
  int32_t         contLen = 0;
3,307,211✔
3712
  void           *pRsp = NULL;
3,307,211✔
3713
  SUserObj       *pUser = NULL;
3,307,211✔
3714
  SGetUserAuthReq authReq = {0};
3,307,211✔
3715
  SGetUserAuthRsp authRsp = {0};
3,307,211✔
3716

3717
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
3,307,211✔
3718
  mTrace("user:%s, start to get auth", authReq.user);
3,307,211✔
3719

3720
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
3,307,211✔
3721

3722
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,305,847✔
3723

3724
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
3,305,847✔
3725
  if (contLen < 0) {
3,305,847✔
UNCOV
3726
    TAOS_CHECK_EXIT(contLen);
×
3727
  }
3728
  pRsp = rpcMallocCont(contLen);
3,305,847✔
3729
  if (pRsp == NULL) {
3,305,847✔
UNCOV
3730
    TAOS_CHECK_EXIT(terrno);
×
3731
  }
3732

3733
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
3,305,847✔
3734
  if (contLen < 0) {
3,305,847✔
UNCOV
3735
    TAOS_CHECK_EXIT(contLen);
×
3736
  }
3737

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

3751
  TAOS_RETURN(code);
3,307,211✔
3752
}
3753

3754

3755
bool mndIsTotpEnabledUser(SUserObj *pUser) {
2,553,437✔
3756
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
84,217,042✔
3757
    if (pUser->totpsecret[i] != 0) {
81,668,588✔
NEW
3758
      return true;
×
3759
    }
3760
  }
3761
  return false;
2,548,454✔
3762
}
3763

3764

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

3778
  while (numOfRows < rows) {
70,543✔
3779
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
70,543✔
3780
    if (pShow->pIter == NULL) break;
70,543✔
3781

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

3788
    cols++;
51,210✔
3789
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3790
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
51,210✔
3791

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

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

3800
    cols++;
51,210✔
3801
    flag = pUser->createdb ? 1 : 0;
51,210✔
3802
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3803
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
51,210✔
3804

3805
    cols++;
51,210✔
3806
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3807
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, pShow->pIter, _exit);
51,210✔
3808

3809
    cols++;
51,210✔
3810
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3811
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
51,210✔
3812
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
51,210✔
3813

3814
    cols++;
51,210✔
3815

3816
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
51,210✔
3817
    if (tlen != 0) {
51,210✔
3818
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
51,210✔
3819
      if (varstr == NULL) {
51,210✔
UNCOV
3820
        sdbRelease(pSdb, pUser);
×
UNCOV
3821
        sdbCancelFetch(pSdb, pShow->pIter);
×
UNCOV
3822
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3823
      }
3824
      varDataSetLen(varstr, tlen);
51,210✔
3825
      (void)memcpy(varDataVal(varstr), buf, tlen);
51,210✔
3826

3827
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3828
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
51,210✔
3829

3830
      taosMemoryFreeClear(buf);
51,210✔
3831
    } else {
UNCOV
3832
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
3833
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3834
    }
3835

3836
    cols++;
51,210✔
3837
    tlen = convertTimeRangesToStr(pUser, &buf);
51,210✔
3838
    if (tlen != 0) {
51,210✔
3839
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
51,210✔
3840
      if (varstr == NULL) {
51,210✔
UNCOV
3841
        sdbRelease(pSdb, pUser);
×
UNCOV
3842
        sdbCancelFetch(pSdb, pShow->pIter);
×
UNCOV
3843
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
3844
      }
3845
      varDataSetLen(varstr, tlen);
51,210✔
3846
      (void)memcpy(varDataVal(varstr), buf, tlen);
51,210✔
3847

3848
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
51,210✔
3849
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
51,210✔
3850

3851
      taosMemoryFreeClear(buf);
51,210✔
3852
    } else {
UNCOV
3853
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
3854
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
3855
    }
3856

3857
    numOfRows++;
51,210✔
3858
    sdbRelease(pSdb, pUser);
51,210✔
3859
  }
3860

3861
  pShow->numOfRows += numOfRows;
19,333✔
3862
_exit:
19,333✔
3863
  taosMemoryFreeClear(buf);
19,333✔
3864
  taosMemoryFreeClear(varstr);
19,333✔
3865
  if (code < 0) {
19,333✔
UNCOV
3866
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
3867
    TAOS_RETURN(code);
×
3868
  }
3869
  return numOfRows;
19,333✔
3870
}
3871

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

UNCOV
3886
  while (numOfRows < rows) {
×
UNCOV
3887
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
UNCOV
3888
    if (pShow->pIter == NULL) break;
×
3889

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

UNCOV
3896
    cols++;
×
UNCOV
3897
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
3898
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, pShow->pIter, _exit);
×
3899

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

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

3908
    cols++;
×
3909
    flag = pUser->createdb ? 1 : 0;
×
3910
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3911
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
×
3912

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

NEW
3917
    cols++;
×
NEW
3918
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
NEW
3919
    flag = mndIsTotpEnabledUser(pUser) ? 1 : 0;
×
NEW
3920
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, pShow->pIter, _exit);
×
3921

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

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

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

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

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

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

3948
    /* not supported yet
3949
    cols++;
3950
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
3951
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->vnodePerSession, false, pUser, pShow->pIter, _exit);
3952
*/
3953

UNCOV
3954
    cols++;
×
3955
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3956
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->failedLoginAttempts, false, pUser, pShow->pIter, _exit);
×
3957

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

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

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

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

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

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

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

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

UNCOV
3998
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
3999
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
4000

4001
      taosMemoryFreeClear(buf);
×
4002
    } else {
4003
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4004
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4005
    }
4006

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

4019
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4020
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, pShow->pIter, _exit);
×
4021

4022
      taosMemoryFreeClear(buf);
×
4023
    } else {
4024
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
4025
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, pShow->pIter, _exit);
×
4026
    }
4027

4028
    numOfRows++;
×
4029
    sdbRelease(pSdb, pUser);
×
4030
  }
4031

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

4044
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
4045
  SSdb *pSdb = pMnode->pSdb;
×
4046
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4047
}
×
4048

4049
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
597,912✔
4050
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
4051
  char   *value = taosHashIterate(hash, NULL);
597,912✔
4052
  char   *user = pUser->user;
597,912✔
4053
  int32_t code = 0;
597,912✔
4054
  int32_t lino = 0;
597,912✔
4055
  int32_t cols = 0;
597,912✔
4056
  int32_t numOfRows = *pNumOfRows;
597,912✔
4057

4058
  while (value != NULL) {
689,546✔
4059
    cols = 0;
91,634✔
4060
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
91,634✔
4061
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
91,634✔
4062
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
91,634✔
4063
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, NULL, _exit);
91,634✔
4064

4065
    char privilege[20] = {0};
91,634✔
4066
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
91,634✔
4067
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
91,634✔
4068
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, NULL, _exit);
91,634✔
4069

4070
    size_t keyLen = 0;
91,634✔
4071
    void  *key = taosHashGetKey(value, &keyLen);
91,634✔
4072

4073
    char dbName[TSDB_DB_NAME_LEN] = {0};
91,634✔
4074
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
91,634✔
4075
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
91,634✔
4076
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
91,634✔
4077
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
91,634✔
4078
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, NULL, _exit);
91,634✔
4079

4080
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
91,634✔
4081
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
91,634✔
4082
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
91,634✔
4083
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
91,634✔
4084
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
91,634✔
4085
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, NULL, _exit);
91,634✔
4086

4087
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
157,842✔
4088
      SNode  *pAst = NULL;
66,208✔
4089
      int32_t sqlLen = 0;
66,208✔
4090
      size_t  bufSz = strlen(value) + 1;
66,208✔
4091
      if (bufSz < 6) bufSz = 6;
66,208✔
4092
      TAOS_MEMORY_REALLOC(*sql, bufSz);
66,208✔
4093
      if (*sql == NULL) {
66,208✔
UNCOV
4094
        code = terrno;
×
UNCOV
4095
        goto _exit;
×
4096
      }
4097
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
66,208✔
4098
      if ((*condition) == NULL) {
66,208✔
UNCOV
4099
        code = terrno;
×
UNCOV
4100
        goto _exit;
×
4101
      }
4102

4103
      if (nodesStringToNode(value, &pAst) == 0) {
66,208✔
4104
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
66,208✔
UNCOV
4105
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
4106
        }
4107
        nodesDestroyNode(pAst);
66,208✔
4108
      }
4109

4110
      if (sqlLen == 0) {
66,208✔
UNCOV
4111
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
4112
      }
4113

4114
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
66,208✔
4115

4116
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
66,208✔
4117
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
66,208✔
4118

4119
      char notes[2] = {0};
66,208✔
4120
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
66,208✔
4121
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
66,208✔
4122
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, NULL, _exit);
66,208✔
4123
    } else {
4124
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
25,426✔
4125
      if ((*condition) == NULL) {
25,426✔
UNCOV
4126
        code = terrno;
×
4127
        goto _exit;
×
4128
      }
4129
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
25,426✔
4130
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,426✔
4131
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, NULL, _exit);
25,426✔
4132

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

4139
    numOfRows++;
91,634✔
4140
    value = taosHashIterate(hash, value);
91,634✔
4141
  }
4142
  *pNumOfRows = numOfRows;
597,912✔
4143
_exit:
597,912✔
4144
  if (code < 0) {
597,912✔
UNCOV
4145
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
4146
    sdbRelease(pSdb, pUser);
×
UNCOV
4147
    sdbCancelFetch(pSdb, pShow->pIter);
×
4148
  }
4149
  TAOS_RETURN(code);
597,912✔
4150
}
4151

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

4164
  bool fetchNextUser = pShow->restore ? false : true;
19,701✔
4165
  pShow->restore = false;
19,701✔
4166

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

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

4202
    if (pUser->superUser) {
99,652✔
4203
      cols = 0;
19,701✔
4204
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19,701✔
4205
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19,701✔
4206
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,701✔
4207
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
19,701✔
4208

4209
      char privilege[20] = {0};
19,701✔
4210
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
19,701✔
4211
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,701✔
4212
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
19,701✔
4213

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

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

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

4233
      char notes[2] = {0};
19,701✔
4234
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19,701✔
4235
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,701✔
4236
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
19,701✔
4237

4238
      numOfRows++;
19,701✔
4239
    }
4240

4241
    char *db = taosHashIterate(pUser->readDbs, NULL);
99,652✔
4242
    while (db != NULL) {
118,642✔
4243
      cols = 0;
18,990✔
4244
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
18,990✔
4245
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
18,990✔
4246
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,990✔
4247
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
18,990✔
4248

4249
      char privilege[20] = {0};
18,990✔
4250
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
18,990✔
4251
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,990✔
4252
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
18,990✔
4253

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

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

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

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

4287
      numOfRows++;
18,990✔
4288
      db = taosHashIterate(pUser->readDbs, db);
18,990✔
4289
    }
4290

4291
    db = taosHashIterate(pUser->writeDbs, NULL);
99,652✔
4292
    while (db != NULL) {
116,501✔
4293
      cols = 0;
16,849✔
4294
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
16,849✔
4295
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
16,849✔
4296
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,849✔
4297
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
16,849✔
4298

4299
      char privilege[20] = {0};
16,849✔
4300
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
16,849✔
4301
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,849✔
4302
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
16,849✔
4303

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

4317
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
16,849✔
4318
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
16,849✔
4319
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,849✔
4320
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
16,849✔
4321

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

4332
      char notes[2] = {0};
16,849✔
4333
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
16,849✔
4334
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,849✔
4335
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
16,849✔
4336

4337
      numOfRows++;
16,849✔
4338
      db = taosHashIterate(pUser->writeDbs, db);
16,849✔
4339
    }
4340

4341
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4342

4343
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4344

4345
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4346

4347
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4348

4349
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4350

4351
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
99,652✔
4352

4353
    char *topic = taosHashIterate(pUser->topics, NULL);
99,652✔
4354
    while (topic != NULL) {
102,965✔
4355
      cols = 0;
3,313✔
4356
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
3,313✔
4357
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
3,313✔
4358
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,313✔
4359
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
3,313✔
4360

4361
      char privilege[20] = {0};
3,313✔
4362
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
3,313✔
4363
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,313✔
4364
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, pShow->pIter, _exit);
3,313✔
4365

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

4372
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
3,313✔
4373
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
3,313✔
4374
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,313✔
4375
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, pShow->pIter, _exit);
3,313✔
4376

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

4386
      char notes[2] = {0};
3,313✔
4387
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
3,313✔
4388
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,313✔
4389
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, pShow->pIter, _exit);
3,313✔
4390

4391
      numOfRows++;
3,313✔
4392
      topic = taosHashIterate(pUser->topics, topic);
3,313✔
4393
    }
4394

4395
    sdbRelease(pSdb, pUser);
99,652✔
4396
  }
4397

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

UNCOV
4409
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
UNCOV
4410
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
4411
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
4412
}
×
4413

4414
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
13,143,597✔
4415
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
4416
  int32_t           code = 0;
13,143,597✔
4417
  int32_t           lino = 0;
13,143,597✔
4418
  int32_t           rspLen = 0;
13,144,223✔
4419
  void             *pRsp = NULL;
13,144,223✔
4420
  SUserAuthBatchRsp batchRsp = {0};
13,144,223✔
4421

4422
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
13,144,223✔
4423
  if (batchRsp.pArray == NULL) {
13,144,223✔
UNCOV
4424
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4425
  }
4426

4427
  for (int32_t i = 0; i < numOfUses; ++i) {
26,619,528✔
4428
    SUserObj *pUser = NULL;
13,475,305✔
4429
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
13,475,305✔
4430
    if (pUser == NULL) {
13,475,305✔
4431
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
6,105✔
4432
        SGetUserAuthRsp rsp = {.dropped = 1};
6,105✔
4433
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
6,105✔
4434
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
12,210✔
4435
      }
4436
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
6,105✔
4437
      code = 0;
6,105✔
4438
      continue;
31,751✔
4439
    }
4440

4441
    pUsers[i].version = ntohl(pUsers[i].version);
13,469,200✔
4442
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
13,469,200✔
4443
      mndReleaseUser(pMnode, pUser);
12,721,969✔
4444
      continue;
12,721,969✔
4445
    }
4446

4447
    SGetUserAuthRsp rsp = {0};
747,231✔
4448
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
747,231✔
4449
    if (code) {
747,231✔
UNCOV
4450
      mndReleaseUser(pMnode, pUser);
×
UNCOV
4451
      tFreeSGetUserAuthRsp(&rsp);
×
UNCOV
4452
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4453
    }
4454

4455
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
1,494,462✔
UNCOV
4456
      code = terrno;
×
4457
      mndReleaseUser(pMnode, pUser);
×
UNCOV
4458
      tFreeSGetUserAuthRsp(&rsp);
×
UNCOV
4459
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
4460
    }
4461
    mndReleaseUser(pMnode, pUser);
747,231✔
4462
  }
4463

4464
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
13,144,223✔
4465
    *ppRsp = NULL;
12,404,379✔
4466
    *pRspLen = 0;
12,404,379✔
4467

4468
    tFreeSUserAuthBatchRsp(&batchRsp);
12,404,379✔
4469
    return 0;
12,404,379✔
4470
  }
4471

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

4494
  TAOS_RETURN(code);
739,844✔
4495
}
4496

4497
static int32_t mndRemoveDbPrivileges(SHashObj *pHash, const char *dbFName, int32_t dbFNameLen, int32_t *nRemoved) {
29,160✔
4498
  void *pVal = NULL;
29,160✔
4499
  while ((pVal = taosHashIterate(pHash, pVal))) {
51,753✔
4500
    size_t keyLen = 0;
22,593✔
4501
    char  *pKey = (char *)taosHashGetKey(pVal, &keyLen);
22,593✔
4502
    if (pKey == NULL || keyLen <= dbFNameLen) continue;
22,593✔
4503
    if ((*(pKey + dbFNameLen) == '.') && strncmp(pKey, dbFName, dbFNameLen) == 0) {
22,593✔
4504
      TAOS_CHECK_RETURN(taosHashRemove(pHash, pKey, keyLen));
11,670✔
4505
      if (nRemoved) ++(*nRemoved);
11,670✔
4506
    }
4507
  }
4508
  TAOS_RETURN(0);
29,160✔
4509
}
4510

4511
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SSHashObj **ppUsers) {
716,033✔
4512
  int32_t    code = 0, lino = 0;
716,033✔
4513
  SSdb      *pSdb = pMnode->pSdb;
716,033✔
4514
  int32_t    dbLen = strlen(pDb->name);
716,033✔
4515
  void      *pIter = NULL;
716,033✔
4516
  SUserObj  *pUser = NULL;
716,033✔
4517
  SUserObj   newUser = {0};
716,033✔
4518
  SSHashObj *pUsers = ppUsers ? *ppUsers : NULL;
716,033✔
4519
  bool       output = (ppUsers != NULL);
716,033✔
4520

4521
  while (1) {
774,885✔
4522
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,490,918✔
4523
    if (pIter == NULL) break;
1,490,918✔
4524

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

4573
    int32_t nRemovedReadTbs = 0;
10,296✔
4574
    int32_t nRemovedWriteTbs = 0;
10,296✔
4575
    int32_t nRemovedAlterTbs = 0;
10,296✔
4576
    if (inReadTbs || inWriteTbs || inAlterTbs) {
10,296✔
4577
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readTbs, pDb->name, dbLen, &nRemovedReadTbs));
9,363✔
4578
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeTbs, pDb->name, dbLen, &nRemovedWriteTbs));
9,363✔
4579
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterTbs, pDb->name, dbLen, &nRemovedAlterTbs));
9,363✔
4580
      if (!update) update = nRemovedReadTbs > 0 || nRemovedWriteTbs > 0 || nRemovedAlterTbs > 0;
9,363✔
4581
    }
4582

4583
    int32_t nRemovedReadViews = 0;
10,296✔
4584
    int32_t nRemovedWriteViews = 0;
10,296✔
4585
    int32_t nRemovedAlterViews = 0;
10,296✔
4586
    if (inReadViews || inWriteViews || inAlterViews) {
10,296✔
4587
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->readViews, pDb->name, dbLen, &nRemovedReadViews));
357✔
4588
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->writeViews, pDb->name, dbLen, &nRemovedWriteViews));
357✔
4589
      TAOS_CHECK_EXIT(mndRemoveDbPrivileges(pTargetUser->alterViews, pDb->name, dbLen, &nRemovedAlterViews));
357✔
4590
      if (!update) update = nRemovedReadViews > 0 || nRemovedWriteViews > 0 || nRemovedAlterViews > 0;
357✔
4591
    }
4592

4593
    if (!output) {
10,296✔
4594
      if (update) {
9,018✔
4595
        SSdbRaw *pCommitRaw = mndUserActionEncode(pTargetUser);
1,290✔
4596
        if (pCommitRaw == NULL) {
1,290✔
UNCOV
4597
          TAOS_CHECK_EXIT(terrno);
×
4598
        }
4599
        TAOS_CHECK_EXIT(mndTransAppendCommitlog(pTrans, pCommitRaw));
1,290✔
4600
        TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,290✔
4601
      }
4602
      mndUserFreeObj(&newUser);
9,018✔
4603
    }
4604
    sdbRelease(pSdb, pUser);
10,296✔
4605
  }
4606

4607
_exit:
716,033✔
4608
  if (code < 0) {
716,033✔
UNCOV
4609
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
4610
    mndUserFreeObj(&newUser);
×
4611
  }
4612
  if (pUser != NULL) sdbRelease(pSdb, pUser);
716,033✔
4613
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
716,033✔
4614
  if (!output) mndUserFreeObj(&newUser);
716,033✔
4615
  TAOS_RETURN(code);
716,033✔
4616
}
4617

4618
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
499,820✔
4619
  int32_t   code = 0;
499,820✔
4620
  SSdb     *pSdb = pMnode->pSdb;
499,820✔
4621
  int32_t   len = strlen(stb) + 1;
499,820✔
4622
  void     *pIter = NULL;
499,820✔
4623
  SUserObj *pUser = NULL;
499,820✔
4624
  SUserObj  newUser = {0};
499,820✔
4625

4626
  while (1) {
499,820✔
4627
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
999,640✔
4628
    if (pIter == NULL) break;
999,640✔
4629

4630
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
499,820✔
UNCOV
4631
      break;
×
4632
    }
4633

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

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

4664
    mndUserFreeObj(&newUser);
499,820✔
4665
    sdbRelease(pSdb, pUser);
499,820✔
4666
  }
4667

4668
  if (pUser != NULL) sdbRelease(pSdb, pUser);
499,820✔
4669
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
499,820✔
4670
  mndUserFreeObj(&newUser);
499,820✔
4671
  TAOS_RETURN(code);
499,820✔
4672
}
4673

4674
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
176,760✔
4675
  int32_t   code = 0;
176,760✔
4676
  SSdb     *pSdb = pMnode->pSdb;
176,760✔
4677
  int32_t   len = strlen(view) + 1;
176,760✔
4678
  void     *pIter = NULL;
176,760✔
4679
  SUserObj *pUser = NULL;
176,760✔
4680
  SUserObj  newUser = {0};
176,760✔
4681

4682
  while (1) {
183,974✔
4683
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
360,734✔
4684
    if (pIter == NULL) break;
360,734✔
4685

4686
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
183,974✔
4687
      break;
×
4688
    }
4689

4690
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
183,974✔
4691
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
183,974✔
4692
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
183,974✔
4693
    if (inRead || inWrite || inAlter) {
183,974✔
4694
      code = taosHashRemove(newUser.readViews, view, len);
4,486✔
4695
      if (code < 0) {
4,486✔
4696
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
892✔
4697
      }
4698
      code = taosHashRemove(newUser.writeViews, view, len);
4,486✔
4699
      if (code < 0) {
4,486✔
4700
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
2,256✔
4701
      }
4702
      code = taosHashRemove(newUser.alterViews, view, len);
4,486✔
4703
      if (code < 0) {
4,486✔
4704
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
1,364✔
4705
      }
4706

4707
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
4,486✔
4708
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
4,486✔
UNCOV
4709
        code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
4710
        break;
×
4711
      }
4712
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
4,486✔
4713
      if (code < 0) {
4,486✔
UNCOV
4714
        mndUserFreeObj(&newUser);
×
UNCOV
4715
        sdbRelease(pSdb, pUser);
×
UNCOV
4716
        TAOS_RETURN(code);
×
4717
      }
4718
    }
4719

4720
    mndUserFreeObj(&newUser);
183,974✔
4721
    sdbRelease(pSdb, pUser);
183,974✔
4722
  }
4723

4724
  if (pUser != NULL) sdbRelease(pSdb, pUser);
176,760✔
4725
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
176,760✔
4726
  mndUserFreeObj(&newUser);
176,760✔
4727
  TAOS_RETURN(code);
176,760✔
4728
}
4729

4730
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
30,837✔
4731
  int32_t   code = 0;
30,837✔
4732
  SSdb     *pSdb = pMnode->pSdb;
30,837✔
4733
  int32_t   len = strlen(topic) + 1;
30,837✔
4734
  void     *pIter = NULL;
30,837✔
4735
  SUserObj *pUser = NULL;
30,837✔
4736
  SUserObj  newUser = {0};
30,837✔
4737

4738
  while (1) {
45,255✔
4739
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
76,092✔
4740
    if (pIter == NULL) {
76,092✔
4741
      break;
30,837✔
4742
    }
4743

4744
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
45,255✔
UNCOV
4745
      break;
×
4746
    }
4747

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

4767
    mndUserFreeObj(&newUser);
45,255✔
4768
    sdbRelease(pSdb, pUser);
45,255✔
4769
  }
4770

4771
  if (pUser != NULL) sdbRelease(pSdb, pUser);
30,837✔
4772
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
30,837✔
4773
  mndUserFreeObj(&newUser);
30,837✔
4774
  TAOS_RETURN(code);
30,837✔
4775
}
4776

UNCOV
4777
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
4778
  // ver = 0, disable ip white list
4779
  // ver > 0, enable ip white list
UNCOV
4780
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
4781
}
4782

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