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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

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

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 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() {
492,175✔
158
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
492,175✔
159

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

165
  userCache.users = users;
492,175✔
166
  userCache.verIp = 0;
492,175✔
167
  userCache.verTime = 0;
492,175✔
168

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

173

174

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

180
  void *pIter = taosHashIterate(userCache.users, NULL);
491,404✔
181
  while (pIter) {
992,981✔
182
    SCachedUserInfo *pInfo = *(SCachedUserInfo **)pIter;
501,577✔
183
    if (pInfo != NULL) {
501,577✔
184
      taosMemoryFree(pInfo->wlIp);
501,577✔
185
      taosMemoryFree(pInfo->wlTime);
501,577✔
186
      taosMemoryFree(pInfo);
501,577✔
187
    }
188
    pIter = taosHashIterate(userCache.users, pIter);
501,577✔
189
  }
190
  taosHashCleanup(userCache.users);
491,404✔
191

192
  (void)taosThreadRwlockDestroy(&userCache.rw);
491,404✔
193
}
194

195

196

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

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

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

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

219

220

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

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

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

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

236

237

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

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

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

255
  return pInfo;
559,902✔
256
}
257

258

259

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

263
  (void)taosThreadRwlockRdlock(&userCache.rw);
2,981,433✔
264

265
  SCachedUserInfo **ppInfo = taosHashGet(userCache.users, user, userLen);
2,981,433✔
266
  if (ppInfo != NULL && *ppInfo != NULL) {
2,981,193✔
267
    pLoginInfo->lastLoginTime = (*ppInfo)->loginInfo.lastLoginTime;
2,664,708✔
268
    pLoginInfo->failedLoginCount = (*ppInfo)->loginInfo.failedLoginCount;
2,663,789✔
269
    pLoginInfo->lastFailedLoginTime = (*ppInfo)->loginInfo.lastFailedLoginTime;
2,664,269✔
270
  } else {
271
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
316,485✔
272
    pLoginInfo->failedLoginCount = 0;
316,485✔
273
    pLoginInfo->lastFailedLoginTime = 0;
316,485✔
274
  }
275

276
  (void)taosThreadRwlockUnlock(&userCache.rw);
2,981,433✔
277

278
  if (pLoginInfo->lastLoginTime == 0) {
2,981,433✔
279
    pLoginInfo->lastLoginTime = taosGetTimestampSec();
45,064✔
280
  }
281
}
2,981,433✔
282

283

284

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

288
  (void)taosThreadRwlockWrlock(&userCache.rw);
2,981,193✔
289

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

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

300

301

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

307
  if (a == NULL || b == NULL) {
944,369✔
308
    return false;
97,729✔
309
  }
310

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

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

327

328

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

332
  (void)taosThreadRwlockWrlock(&userCache.rw);
944,369✔
333

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

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

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

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

367

368

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

372
  SSdb     *pSdb = pMnode->pSdb;
596,462✔
373
  void     *pIter = NULL;
596,462✔
374
  while (1) {
254,217✔
375
    SUserObj *pUser = NULL;
850,679✔
376
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
850,679✔
377
    if (pIter == NULL) {
850,679✔
378
      break;
596,462✔
379
    }
380

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

398
    sdbRelease(pSdb, pUser);
254,217✔
399
  }
400

401
  userCache.verIp++;
596,462✔
402

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

410

411

412
int64_t mndGetIpWhiteListVersion(SMnode *pMnode) {
41,496,007✔
413
  int64_t ver = 0;
41,496,007✔
414
  int32_t code = 0;
41,496,007✔
415

416
  if (mndEnableIpWhiteList(pMnode) != 0 && tsEnableWhiteList) {
41,496,007✔
417
    (void)taosThreadRwlockWrlock(&userCache.rw);
12,369✔
418

419
    if (userCache.verIp == 0) {
12,369✔
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,369✔
429

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

433
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
41,496,007✔
434
  return ver;
41,496,007✔
435
}
436

437

438

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

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

450
  TAOS_RETURN(code);
596,462✔
451
}
452

453

454

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

458
  SSdb     *pSdb = pMnode->pSdb;
584,982✔
459
  void     *pIter = NULL;
584,982✔
460
  while (1) {
242,737✔
461
    SUserObj *pUser = NULL;
827,719✔
462
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
827,719✔
463
    if (pIter == NULL) {
827,719✔
464
      break;
584,982✔
465
    }
466

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

484
    sdbRelease(pSdb, pUser);
242,737✔
485
  }
486

487
  userCache.verTime++;
584,982✔
488

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

496

497

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

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

509
  TAOS_RETURN(code);
584,982✔
510
}
511

512

513

514
int64_t mndGetTimeWhiteListVersion(SMnode *pMnode) {
41,496,007✔
515
  int64_t ver = 0;
41,496,007✔
516
  int32_t code = 0;
41,496,007✔
517

518
  if (mndEnableTimeWhiteList(pMnode) != 0 && tsEnableWhiteList) {
41,496,007✔
519
    (void)taosThreadRwlockWrlock(&userCache.rw);
12,369✔
520

521
    if (userCache.verIp == 0) {
12,369✔
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,369✔
531

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

535
  mDebug("datetime-white-list on mnode ver: %" PRId64, ver);
41,496,007✔
536
  return ver;
41,496,007✔
537
}
538

539

540

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

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

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

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

576

577

578
void mndCleanupUser(SMnode *pMnode) {
491,404✔
579
  userCacheCleanup();
491,404✔
580
}
491,404✔
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,968✔
607
  int32_t len = 0;
51,968✔
608
  for (int i = 0; i < num; i++) {
157,312✔
609
    SIpRange *pRange = &range[i];
105,344✔
610
    SIpAddr   addr = {0};
105,344✔
611
    (void)tIpUintToStr(pRange, &addr);
105,344✔
612

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

619

620

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

626
  if (a->type == 0) {
1,692,576✔
627
    SIpV4Range *a4 = &a->ipV4;
846,640✔
628
    SIpV4Range *b4 = &b->ipV4;
846,640✔
629
    return (a4->ip == b4->ip && a4->mask == b4->mask);
846,640✔
630
  }
631
  
632
  SIpV6Range *a6 = &a->ipV6;
845,936✔
633
  SIpV6Range *b6 = &b->ipV6;
845,936✔
634
  return (a6->addr[0] == b6->addr[0] && a6->addr[1] == b6->addr[1] && a6->mask == b6->mask);
845,936✔
635
}
636

637

638

639
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
944,369✔
640
  if (a == NULL && b == NULL) {
944,369✔
UNCOV
641
    return true;
×
642
  }
643
  
644
  if (a == NULL || b == NULL) {
944,369✔
645
    return false;
97,729✔
646
  }
647

648
  if (a->num != b->num) {
846,640✔
649
    return false;
352✔
650
  }
651
  for (int i = 0; i < a->num; i++) {
2,538,864✔
652
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
1,692,576✔
UNCOV
653
      return false;
×
654
    }
655
  }
656
  return true;
846,288✔
657
}
658

659

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

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

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

672
  if (ra->type == 0) {
3,872✔
673
    if (ra->ipV4.ip != rb->ipV4.ip) {
3,872✔
674
      return (ra->ipV4.ip < rb->ipV4.ip) ? -1 : 1;
3,168✔
675
    }
676
    return (ra->ipV4.mask < rb->ipV4.mask) ? -1 : 1;
704✔
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,762✔
689
  (void)taosqsort(pList->pIpRanges, pList->num, sizeof(SIpRange), NULL, compareIpRange);
1,762✔
690
}
1,762✔
691

692

693

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

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

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

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

715

716

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

724
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
2,409,022✔
725
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
4,818,044✔
726

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

732
  tEndEncode(&encoder);
2,409,022✔
733

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

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

750
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
1,552,696✔
751
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
3,105,392✔
752

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

758
_OVER:
1,552,696✔
759
  tEndDecode(&decoder);
1,552,696✔
760
  tDecoderClear(&decoder);
1,552,696✔
761
  if (code < 0) {
1,552,696✔
UNCOV
762
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
763
  }
764
  TAOS_RETURN(code);
1,552,696✔
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,552,696✔
792
  int32_t           code = 0;
1,552,696✔
793
  int32_t           lino = 0;
1,552,696✔
794
  int32_t           num = 0;
1,552,696✔
795
  SIpWhiteListDual *p = NULL;
1,552,696✔
796
  SDecoder          decoder = {0};
1,552,696✔
797
  tDecoderInit(&decoder, buf, len);
1,552,696✔
798

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

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

808
_OVER:
1,552,696✔
809
  tEndDecode(&decoder);
1,552,696✔
810
  tDecoderClear(&decoder);
1,552,696✔
811
  if (code < 0) {
1,552,696✔
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,552,696✔
816
  TAOS_RETURN(code);
1,552,696✔
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) {
417,783✔
848
  int32_t code = 0;
417,783✔
849
  int32_t lino = 0;
417,783✔
850
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
417,783✔
851
  if (*ppWhiteList == NULL) {
417,783✔
UNCOV
852
    TAOS_RETURN(terrno);
×
853
  }
854
  (*ppWhiteList)->num = 2;
417,783✔
855

856
  SIpRange v4 = {0};
417,783✔
857
  SIpRange v6 = {0};
417,783✔
858

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

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

866
#endif
867

868
_error:
417,783✔
869
  if (code != 0) {
417,783✔
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));
417,783✔
875
    memcpy(&(*ppWhiteList)->pIpRanges[1], &v6, sizeof(SIpRange));
417,783✔
876
  }
877
  return 0;
417,783✔
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,968✔
884
  int32_t bufLen = pUser->pTimeWhiteList->num * 32 + 8;
51,968✔
885
  *buf = taosMemoryCalloc(1, bufLen);
51,968✔
886
  if (*buf == NULL) {
51,968✔
UNCOV
887
    return 0;
×
888
  }
889

890
  int32_t pos = 0;
51,968✔
891
  if (pUser->pTimeWhiteList->num == 0) {
51,968✔
892
    pos += tsnprintf(*buf + pos, bufLen - pos, "+ALL");
51,968✔
893
    return pos;
51,968✔
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) {
3,961,718✔
951
  if (pUser->numOfPasswords <= pUser->passwordReuseMax) {
3,961,718✔
952
    return;
3,912,193✔
953
  }
954

955
  int32_t reuseMax = pUser->passwordReuseMax;
49,525✔
956
  if (reuseMax == 0) {
49,525✔
957
    reuseMax = 1; // keep at least one password
45,985✔
958
  }
959

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

970
  if (index == pUser->numOfPasswords) {
49,525✔
971
    return;
49,525✔
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,469✔
982
  int32_t  code = 0;
321,469✔
983
  int32_t  lino = 0;
321,469✔
984
  SUserObj userObj = {0};
321,469✔
985

986
  userObj.passwords = taosMemCalloc(1, sizeof(SUserPassword));
321,469✔
987
  if (userObj.passwords == NULL) {
321,469✔
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,469✔
991
  userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
321,469✔
992
  if (tsiEncryptPassAlgorithm == DND_CA_SM4 && strlen(tsEncryptKey) > 0) {
321,469✔
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,469✔
998
  userObj.numOfPasswords = 1;
321,469✔
999

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

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

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

1047
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
321,469✔
1048
  if (pTrans == NULL) {
321,469✔
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,469✔
1054

1055
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
321,469✔
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,469✔
1061

1062
  if (mndTransPrepare(pMnode, pTrans) != 0) {
321,469✔
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,469✔
1069
  taosMemoryFree(userObj.passwords);
321,469✔
1070
  taosMemoryFree(userObj.pIpWhiteListDual);
321,469✔
1071
  taosMemoryFree(userObj.pTimeWhiteList);
321,469✔
1072
  return 0;
321,469✔
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,469✔
1082
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
321,469✔
1083
}
1084

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

1106
  char *stb = taosHashIterate(pUser->readTbs, NULL);
2,409,022✔
1107
  while (stb != NULL) {
2,834,092✔
1108
    size_t keyLen = 0;
425,070✔
1109
    void  *key = taosHashGetKey(stb, &keyLen);
425,070✔
1110
    size += sizeof(int32_t);
425,070✔
1111
    size += keyLen;
425,070✔
1112

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

1120
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,409,022✔
1121
  while (stb != NULL) {
2,838,565✔
1122
    size_t keyLen = 0;
429,543✔
1123
    void  *key = taosHashGetKey(stb, &keyLen);
429,543✔
1124
    size += sizeof(int32_t);
429,543✔
1125
    size += keyLen;
429,543✔
1126

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

1134
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,409,022✔
1135
  while (stb != NULL) {
2,633,188✔
1136
    size_t keyLen = 0;
224,166✔
1137
    void  *key = taosHashGetKey(stb, &keyLen);
224,166✔
1138
    size += sizeof(int32_t);
224,166✔
1139
    size += keyLen;
224,166✔
1140

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

1148
  stb = taosHashIterate(pUser->readViews, NULL);
2,409,022✔
1149
  while (stb != NULL) {
2,457,837✔
1150
    size_t keyLen = 0;
48,815✔
1151
    void  *key = taosHashGetKey(stb, &keyLen);
48,815✔
1152
    size += sizeof(int32_t);
48,815✔
1153
    size += keyLen;
48,815✔
1154

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

1162
  stb = taosHashIterate(pUser->writeViews, NULL);
2,409,022✔
1163
  while (stb != NULL) {
2,446,680✔
1164
    size_t keyLen = 0;
37,658✔
1165
    void  *key = taosHashGetKey(stb, &keyLen);
37,658✔
1166
    size += sizeof(int32_t);
37,658✔
1167
    size += keyLen;
37,658✔
1168

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

1176
  stb = taosHashIterate(pUser->alterViews, NULL);
2,409,022✔
1177
  while (stb != NULL) {
2,446,107✔
1178
    size_t keyLen = 0;
37,085✔
1179
    void  *key = taosHashGetKey(stb, &keyLen);
37,085✔
1180
    size += sizeof(int32_t);
37,085✔
1181
    size += keyLen;
37,085✔
1182

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

1190
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
2,409,022✔
1191
  while (useDb != NULL) {
2,943,326✔
1192
    size_t keyLen = 0;
534,304✔
1193
    void  *key = taosHashGetKey(useDb, &keyLen);
534,304✔
1194
    size += sizeof(int32_t);
534,304✔
1195
    size += keyLen;
534,304✔
1196
    size += sizeof(int32_t);
534,304✔
1197
    useDb = taosHashIterate(pUser->useDbs, useDb);
534,304✔
1198
  }
1199

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

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

1208
  dropOldPasswords(pUser);
2,409,022✔
1209
  SDB_SET_INT32(pRaw, dataPos, pUser->numOfPasswords, _OVER)
2,409,022✔
1210
  for (int32_t i = 0; i < pUser->numOfPasswords; i++) {
4,848,197✔
1211
    SDB_SET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER)
2,439,175✔
1212
    SDB_SET_INT32(pRaw, dataPos, pUser->passwords[i].setTime, _OVER)
2,439,175✔
1213
  }
1214
  SDB_SET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
2,409,022✔
1215

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

1229
  char *db = taosHashIterate(pUser->readDbs, NULL);
2,409,022✔
1230
  while (db != NULL) {
2,719,193✔
1231
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
310,171✔
1232
    db = taosHashIterate(pUser->readDbs, db);
310,171✔
1233
  }
1234

1235
  db = taosHashIterate(pUser->writeDbs, NULL);
2,409,022✔
1236
  while (db != NULL) {
2,710,175✔
1237
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
301,153✔
1238
    db = taosHashIterate(pUser->writeDbs, db);
301,153✔
1239
  }
1240

1241
  char *topic = taosHashIterate(pUser->topics, NULL);
2,409,022✔
1242
  while (topic != NULL) {
2,425,661✔
1243
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
16,639✔
1244
    topic = taosHashIterate(pUser->topics, topic);
16,639✔
1245
  }
1246

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

1255
  stb = taosHashIterate(pUser->readTbs, NULL);
2,409,022✔
1256
  while (stb != NULL) {
2,834,092✔
1257
    size_t keyLen = 0;
425,070✔
1258
    void  *key = taosHashGetKey(stb, &keyLen);
425,070✔
1259
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
425,070✔
1260
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
425,070✔
1261

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

1269
  stb = taosHashIterate(pUser->writeTbs, NULL);
2,409,022✔
1270
  while (stb != NULL) {
2,838,565✔
1271
    size_t keyLen = 0;
429,543✔
1272
    void  *key = taosHashGetKey(stb, &keyLen);
429,543✔
1273
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
429,543✔
1274
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
429,543✔
1275

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

1283
  stb = taosHashIterate(pUser->alterTbs, NULL);
2,409,022✔
1284
  while (stb != NULL) {
2,633,188✔
1285
    size_t keyLen = 0;
224,166✔
1286
    void  *key = taosHashGetKey(stb, &keyLen);
224,166✔
1287
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
224,166✔
1288
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
224,166✔
1289

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

1297
  stb = taosHashIterate(pUser->readViews, NULL);
2,409,022✔
1298
  while (stb != NULL) {
2,457,837✔
1299
    size_t keyLen = 0;
48,815✔
1300
    void  *key = taosHashGetKey(stb, &keyLen);
48,815✔
1301
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
48,815✔
1302
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
48,815✔
1303

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

1311
  stb = taosHashIterate(pUser->writeViews, NULL);
2,409,022✔
1312
  while (stb != NULL) {
2,446,680✔
1313
    size_t keyLen = 0;
37,658✔
1314
    void  *key = taosHashGetKey(stb, &keyLen);
37,658✔
1315
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
37,658✔
1316
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
37,658✔
1317

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

1325
  stb = taosHashIterate(pUser->alterViews, NULL);
2,409,022✔
1326
  while (stb != NULL) {
2,446,107✔
1327
    size_t keyLen = 0;
37,085✔
1328
    void  *key = taosHashGetKey(stb, &keyLen);
37,085✔
1329
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
37,085✔
1330
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
37,085✔
1331

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

1339
  useDb = taosHashIterate(pUser->useDbs, NULL);
2,409,022✔
1340
  while (useDb != NULL) {
2,943,326✔
1341
    size_t keyLen = 0;
534,304✔
1342
    void  *key = taosHashGetKey(useDb, &keyLen);
534,304✔
1343
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
534,304✔
1344
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
534,304✔
1345

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

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

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

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

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

1381
  SDB_SET_INT32(pRaw, dataPos, pUser->pTimeWhiteList->num, _OVER);
2,409,022✔
1382
  for (int32_t i = 0; i < pUser->pTimeWhiteList->num; i++) {
2,409,022✔
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,409,022✔
1391
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2,409,022✔
1392

1393
_OVER:
2,409,022✔
1394
  taosMemoryFree(buf);
2,409,022✔
1395
  if (code < 0) {
2,409,022✔
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,409,022✔
1404
  return pRaw;
2,409,022✔
1405
}
1406

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

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

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

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

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

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

1437
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,552,696✔
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,552,696✔
1447
    pUser->passwords = taosMemoryCalloc(pUser->numOfPasswords, sizeof(SUserPassword));
1,552,696✔
1448
    if (pUser->passwords == NULL) {
1,552,696✔
UNCOV
1449
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1450
    }
1451
    for (int32_t i = 0; i < pUser->numOfPasswords; ++i) {
3,156,178✔
1452
      SDB_GET_BINARY(pRaw, dataPos, pUser->passwords[i].pass, sizeof(pUser->passwords[i].pass), _OVER);
1,603,482✔
1453
      SDB_GET_INT32(pRaw, dataPos, &pUser->passwords[i].setTime, _OVER);
1,603,482✔
1454
    }
1455
    SDB_GET_BINARY(pRaw, dataPos, pUser->salt, sizeof(pUser->salt), _OVER)
1,552,696✔
1456
  }
1457
  
1458
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
1,552,696✔
1459
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
1,552,696✔
1460
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
1,552,696✔
1461
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,552,696✔
NEW
1462
    pUser->passwords[0].setTime = (int32_t)(pUser->updateTime / 1000);
×
1463
  }
1464

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

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

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

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

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

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

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

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

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

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

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

1556
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
1,949,976✔
1557
      int32_t keyLen = 0;
397,280✔
1558
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
397,280✔
1559

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

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

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

1579
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
1,956,914✔
1580
      int32_t keyLen = 0;
404,218✔
1581
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
404,218✔
1582

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

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

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

1602
    if (sver >= 6) {
1,552,696✔
1603
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
1,765,560✔
1604
        int32_t keyLen = 0;
212,864✔
1605
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
212,864✔
1606

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

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

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

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

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

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

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

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

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

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

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

1672
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
1,588,431✔
1673
        int32_t keyLen = 0;
35,735✔
1674
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
35,735✔
1675

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

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

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

1696
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
2,044,510✔
1697
      int32_t keyLen = 0;
491,814✔
1698
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
491,814✔
1699

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

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

1710
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
491,814✔
1711
    }
1712
  }
1713
  // decoder white list
1714
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
1,552,696✔
1715
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
1,552,696✔
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,552,696✔
1739
      int32_t len = 0;
1,552,696✔
1740
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
1,552,696✔
1741

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

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

1753
  if (pUser->pIpWhiteListDual == NULL) {
1,552,696✔
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,552,696✔
1759

1760
  if (sver < USER_VER_SUPPORT_ADVANCED_SECURITY) {
1,552,696✔
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,552,696✔
1782
    SDB_GET_INT8(pRaw, dataPos, &pUser->changePass, _OVER);
1,552,696✔
1783
    SDB_GET_INT32(pRaw, dataPos, &pUser->sessionPerUser, _OVER);
1,552,696✔
1784
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectTime, _OVER);
1,552,696✔
1785
    SDB_GET_INT32(pRaw, dataPos, &pUser->connectIdleTime, _OVER);
1,552,696✔
1786
    SDB_GET_INT32(pRaw, dataPos, &pUser->callPerSession, _OVER);
1,552,696✔
1787
    SDB_GET_INT32(pRaw, dataPos, &pUser->vnodePerCall, _OVER);
1,552,696✔
1788
    SDB_GET_INT32(pRaw, dataPos, &pUser->failedLoginAttempts, _OVER);
1,552,696✔
1789
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLifeTime, _OVER);
1,552,696✔
1790
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseTime, _OVER);
1,552,696✔
1791
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordReuseMax, _OVER);
1,552,696✔
1792
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordLockTime, _OVER);
1,552,696✔
1793
    SDB_GET_INT32(pRaw, dataPos, &pUser->passwordGraceTime, _OVER);
1,552,696✔
1794
    SDB_GET_INT32(pRaw, dataPos, &pUser->inactiveAccountTime, _OVER);
1,552,696✔
1795
    SDB_GET_INT32(pRaw, dataPos, &pUser->allowTokenNum, _OVER);
1,552,696✔
1796

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

1804
    pUser->pTimeWhiteList->num = num;
1,552,696✔
1805
    for (int32_t i = 0; i < num; i++) {
1,552,696✔
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,552,696✔
1815
  taosInitRWLatch(&pUser->lock);
1,552,696✔
1816
  dropOldPasswords(pUser);
1,552,696✔
1817

1818
_OVER:
1,552,696✔
1819
  taosMemoryFree(key);
1,552,696✔
1820
  taosMemoryFree(value);
1,552,696✔
1821
  if (code < 0) {
1,552,696✔
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,552,696✔
1844
  return pRow;
1,552,696✔
1845
}
1846

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

1850
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
606,890✔
1851
  if (pAcct == NULL) {
606,890✔
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;
606,890✔
1857
  sdbRelease(pSdb, pAcct);
606,890✔
1858

1859
  return 0;
606,890✔
1860
}
1861

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

1870
  char *tb = taosHashIterate(pOld, NULL);
40,956,235✔
1871
  while (tb != NULL) {
43,213,851✔
1872
    size_t keyLen = 0;
2,257,616✔
1873
    char  *key = taosHashGetKey(tb, &keyLen);
2,257,616✔
1874

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

1884
  TAOS_RETURN(code);
40,956,235✔
1885
}
1886

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

1895
  int32_t *db = taosHashIterate(pOld, NULL);
1,597,661✔
1896
  while (db != NULL) {
2,069,024✔
1897
    size_t keyLen = 0;
471,363✔
1898
    char  *key = taosHashGetKey(db, &keyLen);
471,363✔
1899

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2044
  taosWUnLockLatch(&pOld->lock);
876,131✔
2045

2046
  return 0;
876,131✔
2047
}
2048

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

2053
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
83,969,864✔
2054
  if (*ppUser == NULL) {
83,970,735✔
2055
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
115,932✔
2056
      code = TSDB_CODE_MND_USER_NOT_EXIST;
115,932✔
2057
    } else {
UNCOV
2058
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
2059
    }
2060
  }
2061
  TAOS_RETURN(code);
83,970,735✔
2062
}
2063

2064
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
84,009,278✔
2065
  SSdb *pSdb = pMnode->pSdb;
84,009,278✔
2066
  sdbRelease(pSdb, pUser);
84,008,468✔
2067
}
84,011,903✔
2068

2069

2070

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

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

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

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

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

2102
  return 0;
710✔
2103
}
2104

2105

2106

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

2116

2117

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

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

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

2130
  if (enableIpv6) {
1,058✔
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,058✔
2138
  if (code != 0) {
1,058✔
UNCOV
2139
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
2140
  }
2141
  return code;
1,058✔
2142
}
2143

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

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

2155
  if (pCreate->isImport == 1) {
97,372✔
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));
97,372✔
2160
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.passwords[0].pass);
97,372✔
2161
    userObj.passwords[0].pass[sizeof(userObj.passwords[0].pass) - 1] = 0;
97,372✔
2162
    TAOS_CHECK_GOTO(mndEncryptPass(userObj.passwords[0].pass, userObj.salt, &userObj.passEncryptAlgorithm), &lino, _OVER);
97,372✔
2163
  }
2164
  userObj.passwords[0].setTime = taosGetTimestampSec();
97,372✔
2165

2166
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
97,372✔
2167
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
97,372✔
2168
  if (pCreate->totpseed[0] != 0) {
97,372✔
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();
97,372✔
2176
  userObj.updateTime = userObj.createdTime;
97,372✔
2177
  userObj.superUser = 0;  // pCreate->superUser;
97,372✔
2178
  userObj.sysInfo = pCreate->sysInfo;
97,372✔
2179
  userObj.enable = pCreate->enable;
97,372✔
2180
  userObj.createdb = pCreate->createDb;
97,372✔
2181

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

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

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

2221
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USER_IP_RANGE) {
1,058✔
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,058✔
2227
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
1,058✔
2228
    if (p == NULL) {
1,058✔
UNCOV
2229
      taosHashCleanup(pUniqueTab);
×
UNCOV
2230
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2231
    }
2232

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

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

2247
  if (pCreate->numTimeRanges == 0) {
97,372✔
2248
    userObj.pTimeWhiteList = (SDateTimeWhiteList*)taosMemoryCalloc(1, sizeof(SDateTimeWhiteList));
97,372✔
2249
    if (userObj.pTimeWhiteList == NULL) {
97,372✔
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();
97,372✔
2302
  userObj.timeWhiteListVer = userObj.ipWhiteListVer;
97,372✔
2303

2304
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
97,372✔
2305
  if (pTrans == NULL) {
97,372✔
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);
97,372✔
2310

2311
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
97,372✔
2312
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
97,372✔
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);
97,372✔
2318

2319
  if (mndTransPrepare(pMnode, pTrans) != 0) {
97,372✔
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) {
97,372✔
2326
    mndTransDrop(pTrans);
×
UNCOV
2327
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2328
  }
2329

2330
  mndTransDrop(pTrans);
97,372✔
2331

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

2339

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

2345
  if (tsEnableStrongPassword == 0) {
84,372✔
2346
    for (char c = *pwd; c != 0; c = *(++pwd)) {
96,996✔
2347
      if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
95,934✔
UNCOV
2348
        return TSDB_CODE_MND_INVALID_PASS_FORMAT;
×
2349
      }
2350
    }
2351
    return 0;
1,062✔
2352
  }
2353

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

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

2363
  if (taosIsComplexString(pwd)) {
83,310✔
2364
    return 0;
83,310✔
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,763✔
2443
  (void)taosThreadRwlockWrlock(&userCache.rw);
1,763✔
2444
  
2445
  int32_t count = taosHashGetSize(userCache.users);
1,763✔
2446
  pRsp->pUsers = taosMemoryCalloc(count, sizeof(SUserDateTimeWhiteList));
1,763✔
2447
  if (pRsp->pUsers == NULL) {
1,763✔
UNCOV
2448
    (void)taosThreadRwlockUnlock(&userCache.rw);
×
UNCOV
2449
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2450
  }
2451

2452
  count = 0;
1,763✔
2453
  void   *pIter = taosHashIterate(userCache.users, NULL);
1,763✔
2454
  while (pIter) {
3,526✔
2455
    SDateTimeWhiteList *wl = (*(SCachedUserInfo **)pIter)->wlTime;
1,763✔
2456
    if (wl == NULL || wl->num <= 0) {
1,763✔
2457
      pIter = taosHashIterate(userCache.users, pIter);
1,763✔
2458
      continue;
1,763✔
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,763✔
2481
  pRsp->ver = userCache.verTime;
1,763✔
2482
  (void)taosThreadRwlockUnlock(&userCache.rw);
1,763✔
2483
  TAOS_RETURN(0);
1,763✔
2484
}
2485

2486

2487

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

2495
  // impl later
2496
  SRetrieveWhiteListReq req = {0};
1,763✔
2497
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,763✔
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,763✔
2503

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

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

2518
_OVER:
1,763✔
2519
  if (code < 0) {
1,763✔
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,763✔
2526
  pReq->info.rsp = pRsp;
1,763✔
2527
  pReq->info.rspLen = len;
1,763✔
2528

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

2533

2534

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

2543
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
98,056✔
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,056✔
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,056✔
2556
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
98,056✔
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,056✔
UNCOV
2563
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2564
  }
2565

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

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

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

2581
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
97,372✔
2582
  if (pOperUser == NULL) {
97,372✔
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);
97,372✔
2587

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

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

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

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

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

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

2617

2618

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

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

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

2644
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
136,769✔
2645
  if (pUser == NULL) {
137,022✔
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);
137,022✔
2650
  contLen = serialFn(NULL, 0, &wlRsp);
136,819✔
2651
  if (contLen < 0) {
137,022✔
UNCOV
2652
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2653
  }
2654
  pRsp = rpcMallocCont(contLen);
137,022✔
2655
  if (pRsp == NULL) {
136,597✔
UNCOV
2656
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2657
  }
2658

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

2664
_OVER:
137,022✔
2665
  mndReleaseUser(pMnode, pUser);
137,022✔
2666
  tFreeSGetUserIpWhiteListDualRsp(&wlRsp);
137,022✔
2667
  if (code < 0) {
137,022✔
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;
137,022✔
2674
  pReq->info.rsp = pRsp;
137,022✔
2675
  pReq->info.rspLen = contLen;
137,022✔
2676

2677
  TAOS_RETURN(code);
137,022✔
2678
}
2679

2680

2681

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

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

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

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

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

2708
    pUser->numOfRange = wl->num;
1,763✔
2709
    pUser->pIpRanges = taosMemoryCalloc(wl->num, sizeof(SIpRange));
1,763✔
2710
    if (pUser->pIpRanges == NULL) {
1,763✔
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,763✔
2716
    count++;
1,763✔
2717
    pIter = taosHashIterate(userCache.users, pIter);
1,763✔
2718
  }
2719

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

2726

2727

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

2735
  // impl later
2736
  SRetrieveWhiteListReq req = {0};
1,763✔
2737
  if (tDeserializeRetrieveWhiteListReq(pReq->pCont, pReq->contLen, &req) != 0) {
1,763✔
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,763✔
2743
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST) {
1,763✔
UNCOV
2744
    fn = tSerializeSUpdateIpWhite;
×
2745
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL) {
1,763✔
2746
    fn = tSerializeSUpdateIpWhiteDual;
1,763✔
2747
  }
2748

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

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

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

2765
_OVER:
1,763✔
2766
  if (code < 0) {
1,763✔
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,763✔
2773
  pReq->info.rsp = pRsp;
1,763✔
2774
  pReq->info.rspLen = len;
1,763✔
2775

2776
  tFreeSUpdateIpWhiteReq(&ipWhite);
1,763✔
2777
  TAOS_RETURN(code);
1,763✔
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) {
846,997✔
2815
  int32_t code = 0;
846,997✔
2816
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
846,997✔
2817
  if (pTrans == NULL) {
846,997✔
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);
846,997✔
2822

2823
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
846,997✔
2824
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
846,997✔
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);
846,997✔
2830
  if (code < 0) {
846,997✔
UNCOV
2831
    mndTransDrop(pTrans);
×
UNCOV
2832
    TAOS_RETURN(code);
×
2833
  }
2834

2835
  if (mndTransPrepare(pMnode, pTrans) != 0) {
846,997✔
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) {
846,997✔
UNCOV
2841
    mndTransDrop(pTrans);
×
UNCOV
2842
    TAOS_RETURN(code);
×
2843
  }
2844
  mndTransDrop(pTrans);
846,997✔
2845
  return 0;
846,997✔
2846
}
2847

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

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

2858
  char *db = taosHashIterate(pOld, NULL);
13,755,917✔
2859
  while (db != NULL) {
14,836,553✔
2860
    int32_t len = strlen(db) + 1;
1,080,636✔
2861
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
1,080,636✔
2862
      taosHashCancelIterate(pOld, db);
×
2863
      taosHashCleanup(*ppNew);
×
UNCOV
2864
      TAOS_RETURN(code);
×
2865
    }
2866
    db = taosHashIterate(pOld, db);
1,080,636✔
2867
  }
2868

2869
  TAOS_RETURN(code);
13,755,917✔
2870
}
2871

2872
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
12,158,256✔
2873

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

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

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

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

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

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

2904
  TAOS_RETURN(0);
418,171✔
2905
}
2906

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

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

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

2924
  if (1 == *currRef) {
380,518✔
2925
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
158,277✔
UNCOV
2926
      TAOS_RETURN(0);  // not found
×
2927
    }
2928
    return 0;
158,277✔
2929
  }
2930
  int32_t ref = (*currRef) - 1;
222,241✔
2931
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
222,241✔
2932

2933
  return 0;
222,241✔
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) {
812,921✔
2958
  SSdb   *pSdb = pMnode->pSdb;
812,921✔
2959
  void   *pIter = NULL;
812,921✔
2960
  int32_t code = 0;
812,921✔
2961
  int32_t lino = 0;
812,921✔
2962

2963
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
812,921✔
2964
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
698,325✔
2965
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
114,596✔
2966
      int32_t len = strlen(pAlterReq->objname) + 1;
111,146✔
2967
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
111,146✔
2968
      if (pDb == NULL) {
111,146✔
2969
        mndReleaseDb(pMnode, pDb);
2,751✔
2970
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
2,751✔
2971
      }
2972
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
108,395✔
2973
          0) {
UNCOV
2974
        mndReleaseDb(pMnode, pDb);
×
UNCOV
2975
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2976
      }
2977
      mndReleaseDb(pMnode, pDb);
108,395✔
2978
    } else {
2979
      while (1) {
5,562✔
2980
        SDbObj *pDb = NULL;
9,012✔
2981
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
9,012✔
2982
        if (pIter == NULL) break;
9,012✔
2983
        int32_t len = strlen(pDb->name) + 1;
5,562✔
2984
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,562✔
UNCOV
2985
          sdbRelease(pSdb, pDb);
×
UNCOV
2986
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
2987
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2988
        }
2989
        sdbRelease(pSdb, pDb);
5,562✔
2990
      }
2991
    }
2992
  }
2993

2994
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
810,170✔
2995
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
699,774✔
2996
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
110,396✔
2997
      int32_t len = strlen(pAlterReq->objname) + 1;
107,288✔
2998
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
107,288✔
2999
      if (pDb == NULL) {
107,288✔
3000
        mndReleaseDb(pMnode, pDb);
352✔
3001
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
352✔
3002
      }
3003
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
106,936✔
3004
          0) {
UNCOV
3005
        mndReleaseDb(pMnode, pDb);
×
UNCOV
3006
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3007
      }
3008
      mndReleaseDb(pMnode, pDb);
106,936✔
3009
    } else {
3010
      while (1) {
5,220✔
3011
        SDbObj *pDb = NULL;
8,328✔
3012
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
8,328✔
3013
        if (pIter == NULL) break;
8,328✔
3014
        int32_t len = strlen(pDb->name) + 1;
5,220✔
3015
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
5,220✔
UNCOV
3016
          sdbRelease(pSdb, pDb);
×
UNCOV
3017
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
3018
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3019
        }
3020
        sdbRelease(pSdb, pDb);
5,220✔
3021
      }
3022
    }
3023
  }
3024

3025
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,818✔
3026
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
708,562✔
3027
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
101,256✔
3028
      int32_t len = strlen(pAlterReq->objname) + 1;
97,806✔
3029
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
97,806✔
3030
      if (pDb == NULL) {
97,806✔
3031
        mndReleaseDb(pMnode, pDb);
342✔
3032
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
342✔
3033
      }
3034
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
97,464✔
3035
      if (code < 0) {
97,464✔
UNCOV
3036
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3037
      }
3038
      mndReleaseDb(pMnode, pDb);
97,464✔
3039
    } else {
3040
      taosHashClear(pNewUser->readDbs);
3,450✔
3041
    }
3042
  }
3043

3044
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3045
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
708,540✔
3046
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
100,936✔
3047
      int32_t len = strlen(pAlterReq->objname) + 1;
97,144✔
3048
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
97,144✔
3049
      if (pDb == NULL) {
97,144✔
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);
97,144✔
3054
      if (code < 0) {
97,144✔
UNCOV
3055
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
3056
      }
3057
      mndReleaseDb(pMnode, pDb);
97,144✔
3058
    } else {
3059
      taosHashClear(pNewUser->writeDbs);
3,792✔
3060
    }
3061
  }
3062

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

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

3075
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3076
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
642,144✔
3077
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
167,332✔
3078
  }
3079

3080
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3081
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
645,921✔
3082
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
163,555✔
3083
  }
3084

3085
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3086
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
722,192✔
3087
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
87,284✔
3088
  }
3089

3090
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3091
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
655,252✔
3092
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
154,224✔
3093
  }
3094

3095
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3096
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
657,172✔
3097
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
152,304✔
3098
  }
3099

3100
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
809,476✔
3101
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
734,151✔
3102
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
75,325✔
3103
  }
3104

3105
#ifdef USE_TOPIC
3106
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
809,476✔
3107
    int32_t      len = strlen(pAlterReq->objname) + 1;
6,092✔
3108
    SMqTopicObj *pTopic = NULL;
6,092✔
3109
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
6,092✔
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,092✔
UNCOV
3114
      mndReleaseTopic(pMnode, pTopic);
×
UNCOV
3115
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3116
    }
3117
    mndReleaseTopic(pMnode, pTopic);
6,092✔
3118
  }
3119

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

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

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

3155
  if (alterReq.user[0] == 0) {
877,679✔
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);
877,679✔
3160

3161
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
869,667✔
3162
  if (pOperUser == NULL) {
869,667✔
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);
869,667✔
3167
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
851,151✔
3168

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

3179
    if (newUser.passwordReuseMax > 0 || newUser.passwordReuseTime > 0) {
22,966✔
3180
      for(int32_t i = 0; i < newUser.numOfPasswords; ++i) {
28,815✔
3181
        if (0 == strncmp(newUser.passwords[i].pass, pass, TSDB_PASSWORD_LEN)) {
19,542✔
3182
          TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_PASSWORD_REUSE, &lino, _OVER);
357✔
3183
        }
3184
      }
3185
      SUserPassword *passwords = taosMemoryCalloc(newUser.numOfPasswords + 1, sizeof(SUserPassword));
9,273✔
3186
      if (passwords == NULL) {
9,273✔
UNCOV
3187
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
3188
      }
3189
      memcpy(passwords + 1, newUser.passwords, newUser.numOfPasswords * sizeof(SUserPassword));
9,273✔
3190
      memcpy(passwords[0].pass, pass, TSDB_PASSWORD_LEN);
9,273✔
3191
      passwords[0].setTime = taosGetTimestampSec();
9,273✔
3192
      taosMemoryFree(newUser.passwords);
9,273✔
3193
      newUser.passwords = passwords;
9,273✔
3194
      ++newUser.numOfPasswords;
9,273✔
3195
      ++newUser.passVersion;
9,273✔
3196
      if (strcmp(newUser.user, pOperUser->user) == 0) {
9,273✔
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,087✔
3200
      }
3201
    } else if (0 != strncmp(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN)) {
4,063✔
3202
      memcpy(newUser.passwords[0].pass, pass, TSDB_PASSWORD_LEN);
2,757✔
3203
      newUser.passwords[0].setTime = taosGetTimestampSec();
2,757✔
3204
      ++newUser.passVersion;
2,757✔
3205
      if (strcmp(newUser.user, pOperUser->user) == 0) {
2,757✔
3206
        newUser.changePass = 2;
339✔
3207
      }
3208
    }
3209
  }
3210

3211
  if (alterReq.hasTotpseed) {
850,794✔
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) {
850,794✔
3220
    newUser.enable = alterReq.enable; // lock or unlock user manually
2,350✔
3221
    if (newUser.enable) {
2,350✔
3222
      // reset login info to allow login immediately
3223
      userCacheResetLoginInfo(newUser.user);
1,315✔
3224
    }
3225
  }
3226

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

3244

3245
  if (alterReq.numDropIpRanges > 0 || alterReq.numIpRanges > 0) {
850,794✔
3246
    int32_t dummy = 0;
704✔
3247

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

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

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

3270
        SIpRange range;
352✔
3271
        copyIpRange(&range, alterReq.pDropIpRanges + i);
352✔
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));
352✔
3277
        if (code == TSDB_CODE_NOT_FOUND) {
352✔
3278
          // treat not exist as success
3279
          code = 0;
352✔
3280
        }
3281
        if (code != 0) {
352✔
UNCOV
3282
          taosHashCleanup(m);
×
UNCOV
3283
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3284
        }
3285
      }
3286
    }
3287

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

3300
    int32_t numOfRanges = taosHashGetSize(m);
704✔
3301
    if (numOfRanges > MND_MAX_USER_IP_RANGE) {
704✔
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));
704✔
3307
    if (p == NULL) {
704✔
UNCOV
3308
      taosHashCleanup(m);
×
UNCOV
3309
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3310
    }
3311

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

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

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

3331

3332
  if (alterReq.numTimeRanges > 0 || alterReq.numDropTimeRanges) {
850,794✔
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)) {
850,794✔
3420
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
812,921✔
3421
  }
3422

3423
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
846,997✔
3424
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
846,997✔
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:
877,679✔
3632
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
877,679✔
3633
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
30,682✔
3634
  }
3635

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

3643
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
58,325✔
3644
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
58,325✔
3645
  if (pTrans == NULL) {
58,325✔
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,325✔
3650

3651
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
58,325✔
3652
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
58,325✔
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,325✔
3658
    mndTransDrop(pTrans);
×
3659
    TAOS_RETURN(terrno);
×
3660
  }
3661

3662
  if (mndTransPrepare(pMnode, pTrans) != 0) {
58,325✔
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,325✔
3669

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

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

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

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

3686
  if (dropReq.user[0] == 0) {
58,664✔
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,664✔
3691

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

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

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

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

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

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

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

3722
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
3,651,838✔
3723

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

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

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

3751
  TAOS_RETURN(code);
3,653,191✔
3752
}
3753

3754

3755
bool mndIsTotpEnabledUser(SUserObj *pUser) {
3,033,401✔
3756
  for (int32_t i = 0; i < sizeof(pUser->totpsecret); i++) {
100,043,314✔
3757
    if (pUser->totpsecret[i] != 0) {
97,011,072✔
NEW
3758
      return true;
×
3759
    }
3760
  }
3761
  return false;
3,032,242✔
3762
}
3763

3764

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

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

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

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

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

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

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

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

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

3814
    cols++;
51,968✔
3815

3816
    int32_t tlen = convertIpWhiteListToStr(pUser, &buf);
51,968✔
3817
    if (tlen != 0) {
51,968✔
3818
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
51,968✔
3819
      if (varstr == NULL) {
51,968✔
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,968✔
3825
      (void)memcpy(varDataVal(varstr), buf, tlen);
51,968✔
3826

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

3830
      taosMemoryFreeClear(buf);
51,968✔
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,968✔
3837
    tlen = convertTimeRangesToStr(pUser, &buf);
51,968✔
3838
    if (tlen != 0) {
51,968✔
3839
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
51,968✔
3840
      if (varstr == NULL) {
51,968✔
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,968✔
3846
      (void)memcpy(varDataVal(varstr), buf, tlen);
51,968✔
3847

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

3851
      taosMemoryFreeClear(buf);
51,968✔
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,968✔
3858
    sdbRelease(pSdb, pUser);
51,968✔
3859
  }
3860

3861
  pShow->numOfRows += numOfRows;
19,789✔
3862
_exit:
19,789✔
3863
  taosMemoryFreeClear(buf);
19,789✔
3864
  taosMemoryFreeClear(varstr);
19,789✔
3865
  if (code < 0) {
19,789✔
UNCOV
3866
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
3867
    TAOS_RETURN(code);
×
3868
  }
3869
  return numOfRows;
19,789✔
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,
593,766✔
4050
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
4051
  char   *value = taosHashIterate(hash, NULL);
593,766✔
4052
  char   *user = pUser->user;
593,766✔
4053
  int32_t code = 0;
593,766✔
4054
  int32_t lino = 0;
593,766✔
4055
  int32_t cols = 0;
593,766✔
4056
  int32_t numOfRows = *pNumOfRows;
593,766✔
4057

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

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

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

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

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

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

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

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

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

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

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

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

4139
    numOfRows++;
82,910✔
4140
    value = taosHashIterate(hash, value);
82,910✔
4141
  }
4142
  *pNumOfRows = numOfRows;
593,766✔
4143
_exit:
593,766✔
4144
  if (code < 0) {
593,766✔
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);
593,766✔
4150
}
4151

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

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

4167
  while (numOfRows < rows) {
118,518✔
4168
    if (fetchNextUser) {
118,518✔
4169
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
118,518✔
4170
      if (pShow->pIter == NULL) break;
118,518✔
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);
98,961✔
4181
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
98,961✔
4182
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
98,961✔
4183
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
98,961✔
4184
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
98,961✔
4185
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
98,961✔
4186
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
98,961✔
4187
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
98,961✔
4188
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
98,961✔
4189
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
98,961✔
4190
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
98,961✔
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) {
98,961✔
4203
      cols = 0;
19,557✔
4204
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19,557✔
4205
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19,557✔
4206
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,557✔
4207
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, pShow->pIter, _exit);
19,557✔
4208

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

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

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

4224
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19,557✔
4225
      if (condition == NULL) {
19,557✔
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,557✔
4230
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19,557✔
4231
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
19,557✔
4232

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

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

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

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

4254
      SName name = {0};
18,867✔
4255
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
18,867✔
4256
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
18,867✔
4257
      if (code < 0) {
18,867✔
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,867✔
4263
      varDataSetLen(objName, strlen(varDataVal(objName)));
18,867✔
4264
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,867✔
4265
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
18,867✔
4266

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

4272
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
18,867✔
4273
      if (condition == NULL) {
18,867✔
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,867✔
4279
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,867✔
4280
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
18,867✔
4281

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

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

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

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

4304
      SName name = {0};
16,745✔
4305
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
16,745✔
4306
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
16,745✔
4307
      if (code < 0) {
16,745✔
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,745✔
4313
      varDataSetLen(objName, strlen(varDataVal(objName)));
16,745✔
4314
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,745✔
4315
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, pShow->pIter, _exit);
16,745✔
4316

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

4322
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
16,745✔
4323
      if (condition == NULL) {
16,745✔
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,745✔
4329
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,745✔
4330
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
16,745✔
4331

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

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

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

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

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

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

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

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

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

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

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

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

4377
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
3,289✔
4378
      if (condition == NULL) {
3,289✔
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,289✔
4383
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3,289✔
4384
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, pShow->pIter, _exit);
3,289✔
4385

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

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

4395
    sdbRelease(pSdb, pUser);
98,961✔
4396
  }
4397

4398
  pShow->numOfRows += numOfRows;
19,557✔
4399
_exit:
19,557✔
4400
  taosMemoryFreeClear(condition);
19,557✔
4401
  taosMemoryFreeClear(sql);
19,557✔
4402
  if (code < 0) {
19,557✔
UNCOV
4403
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
4404
    TAOS_RETURN(code);
×
4405
  }
4406
  return numOfRows;
19,557✔
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,
14,128,820✔
4415
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
4416
  int32_t           code = 0;
14,128,820✔
4417
  int32_t           lino = 0;
14,128,820✔
4418
  int32_t           rspLen = 0;
14,129,514✔
4419
  void             *pRsp = NULL;
14,129,514✔
4420
  SUserAuthBatchRsp batchRsp = {0};
14,129,514✔
4421

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

4427
  for (int32_t i = 0; i < numOfUses; ++i) {
28,594,770✔
4428
    SUserObj *pUser = NULL;
14,465,256✔
4429
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
14,465,256✔
4430
    if (pUser == NULL) {
14,464,550✔
4431
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
6,495✔
4432
        SGetUserAuthRsp rsp = {.dropped = 1};
6,495✔
4433
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
6,495✔
4434
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
12,990✔
4435
      }
4436
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
6,495✔
4437
      code = 0;
6,495✔
4438
      continue;
31,692✔
4439
    }
4440

4441
    pUsers[i].version = ntohl(pUsers[i].version);
14,458,055✔
4442
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
14,458,761✔
4443
      mndReleaseUser(pMnode, pUser);
13,628,482✔
4444
      continue;
13,629,132✔
4445
    }
4446

4447
    SGetUserAuthRsp rsp = {0};
829,585✔
4448
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
829,629✔
4449
    if (code) {
829,629✔
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,659,258✔
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);
829,629✔
4462
  }
4463

4464
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
14,129,514✔
4465
    *ppRsp = NULL;
13,307,146✔
4466
    *pRspLen = 0;
13,307,146✔
4467

4468
    tFreeSUserAuthBatchRsp(&batchRsp);
13,307,146✔
4469
    return 0;
13,307,146✔
4470
  }
4471

4472
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
822,368✔
4473
  if (rspLen < 0) {
822,368✔
UNCOV
4474
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4475
  }
4476
  pRsp = taosMemoryMalloc(rspLen);
822,368✔
4477
  if (pRsp == NULL) {
822,368✔
UNCOV
4478
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
4479
  }
4480
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
822,368✔
4481
  if (rspLen < 0) {
822,368✔
UNCOV
4482
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
4483
  }
4484
_OVER:
822,368✔
4485
  tFreeSUserAuthBatchRsp(&batchRsp);
822,368✔
4486
  if (code < 0) {
822,368✔
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;
822,368✔
4492
  *pRspLen = rspLen;
822,368✔
4493

4494
  TAOS_RETURN(code);
822,368✔
4495
}
4496

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

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

4521
  while (1) {
770,285✔
4522
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1,482,320✔
4523
    if (pIter == NULL) break;
1,482,320✔
4524

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

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

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

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

4607
_exit:
712,035✔
4608
  if (code < 0) {
712,035✔
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);
712,035✔
4613
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
712,035✔
4614
  if (!output) mndUserFreeObj(&newUser);
712,035✔
4615
  TAOS_RETURN(code);
712,035✔
4616
}
4617

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

4626
  while (1) {
498,324✔
4627
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
996,648✔
4628
    if (pIter == NULL) break;
996,648✔
4629

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

4634
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
498,324✔
4635
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
498,324✔
4636
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
498,324✔
4637
    if (inRead || inWrite || inAlter) {
498,324✔
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);
498,324✔
4665
    sdbRelease(pSdb, pUser);
498,324✔
4666
  }
4667

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

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

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

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

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

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

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

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

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

4738
  while (1) {
44,529✔
4739
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
74,742✔
4740
    if (pIter == NULL) {
74,742✔
4741
      break;
30,213✔
4742
    }
4743

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

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

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

4771
  if (pUser != NULL) sdbRelease(pSdb, pUser);
30,213✔
4772
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
30,213✔
4773
  mndUserFreeObj(&newUser);
30,213✔
4774
  TAOS_RETURN(code);
30,213✔
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