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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

22.94
/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
#include <uv.h>
19
#include "mndUser.h"
20
#include "audit.h"
21
#include "mndDb.h"
22
#include "mndPrivilege.h"
23
#include "mndShow.h"
24
#include "mndStb.h"
25
#include "mndTopic.h"
26
#include "mndTrans.h"
27
#include "tbase64.h"
28

29
// clang-format on
30

31
#define USER_VER_NUMBER   6
32
#define USER_RESERVE_SIZE 64
33

34
#define BIT_FLAG_MASK(n)              (1 << n)
35
#define BIT_FLAG_SET_MASK(val, mask)  ((val) |= (mask))
36
#define BIT_FLAG_TEST_MASK(val, mask) (((val) & (mask)) != 0)
37

38
#define PRIVILEGE_TYPE_ALL       BIT_FLAG_MASK(0)
39
#define PRIVILEGE_TYPE_READ      BIT_FLAG_MASK(1)
40
#define PRIVILEGE_TYPE_WRITE     BIT_FLAG_MASK(2)
41
#define PRIVILEGE_TYPE_SUBSCRIBE BIT_FLAG_MASK(3)
42
#define PRIVILEGE_TYPE_ALTER     BIT_FLAG_MASK(4)
43

44
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
45
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
46

47
#define ALTER_USER_ALL_PRIV(_priv) (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
48
#define ALTER_USER_READ_PRIV(_priv) \
49
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_READ) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
50
#define ALTER_USER_WRITE_PRIV(_priv) \
51
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_WRITE) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
52
#define ALTER_USER_ALTER_PRIV(_priv) \
53
  (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALTER) || BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_ALL))
54
#define ALTER_USER_SUBSCRIBE_PRIV(_priv) (BIT_FLAG_TEST_MASK((_priv), PRIVILEGE_TYPE_SUBSCRIBE))
55

56
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
57
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
58

59
#define ALTER_USER_ADD_READ_DB_PRIV(_type, _priv, _tbname) \
60
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
61
#define ALTER_USER_DEL_READ_DB_PRIV(_type, _priv, _tbname) \
62
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
63
#define ALTER_USER_ADD_WRITE_DB_PRIV(_type, _priv, _tbname) \
64
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
65
#define ALTER_USER_DEL_WRITE_DB_PRIV(_type, _priv, _tbname) \
66
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
67
#define ALTER_USER_ADD_ALTER_DB_PRIV(_type, _priv, _tbname) \
68
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
69
#define ALTER_USER_DEL_ALTER_DB_PRIV(_type, _priv, _tbname) \
70
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
71
#define ALTER_USER_ADD_ALL_DB_PRIV(_type, _priv, _tbname) \
72
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
73
#define ALTER_USER_DEL_ALL_DB_PRIV(_type, _priv, _tbname) \
74
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_DB(_tbname))
75

76
#define ALTER_USER_ADD_READ_TB_PRIV(_type, _priv, _tbname) \
77
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
78
#define ALTER_USER_DEL_READ_TB_PRIV(_type, _priv, _tbname) \
79
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_READ_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
80
#define ALTER_USER_ADD_WRITE_TB_PRIV(_type, _priv, _tbname) \
81
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
82
#define ALTER_USER_DEL_WRITE_TB_PRIV(_type, _priv, _tbname) \
83
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_WRITE_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
84
#define ALTER_USER_ADD_ALTER_TB_PRIV(_type, _priv, _tbname) \
85
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
86
#define ALTER_USER_DEL_ALTER_TB_PRIV(_type, _priv, _tbname) \
87
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALTER_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
88
#define ALTER_USER_ADD_ALL_TB_PRIV(_type, _priv, _tbname) \
89
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
90
#define ALTER_USER_DEL_ALL_TB_PRIV(_type, _priv, _tbname) \
91
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_ALL_PRIV(_priv) && ALTER_USER_TARGET_TB(_tbname))
92

93
#define ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
94
  (ALTER_USER_ADD_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
95
#define ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(_type, _priv) \
96
  (ALTER_USER_DEL_PRIVS(_type) && ALTER_USER_SUBSCRIBE_PRIV(_priv))
97

98
static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList);
99
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteList **ppWhiteList);
100
static bool    updateIpWhiteList(SIpWhiteList *pOld, SIpWhiteList *pNew);
101
static bool    isIpWhiteListEqual(SIpWhiteList *a, SIpWhiteList *b);
102
static bool    isIpRangeEqual(SIpV4Range *a, SIpV4Range *b);
103

104
void destroyIpWhiteTab(SHashObj *pIpWhiteTab);
105

106
#define MND_MAX_USE_HOST (TSDB_PRIVILEDGE_HOST_LEN / 24)
107

108
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
109
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
110
static int32_t  mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
111
static int32_t  mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
112
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
113
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
114
static int32_t  mndProcessCreateUserReq(SRpcMsg *pReq);
115
static int32_t  mndProcessAlterUserReq(SRpcMsg *pReq);
116
static int32_t  mndProcessDropUserReq(SRpcMsg *pReq);
117
static int32_t  mndProcessGetUserAuthReq(SRpcMsg *pReq);
118
static int32_t  mndProcessGetUserWhiteListReq(SRpcMsg *pReq);
119
static int32_t  mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
120
static int32_t  mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
121
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
122
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
123
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
124
static int32_t  mndFetchAllIpWhite(SMnode *pMnode, SHashObj **ppIpWhiteTab);
125
static int32_t  mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq);
126
static int32_t  mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t type, bool *pUpdate);
127

128
static int32_t ipWhiteMgtUpdateAll(SMnode *pMnode);
129
typedef struct {
130
  SHashObj      *pIpWhiteTab;
131
  int64_t        ver;
132
  TdThreadRwlock rw;
133
} SIpWhiteMgt;
134

135
static SIpWhiteMgt ipWhiteMgt;
136

137
const static SIpV4Range defaultIpRange = {.ip = 16777343, .mask = 32};
138

139
static int32_t ipWhiteMgtInit() {
13✔
140
  ipWhiteMgt.pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
13✔
141
  if (ipWhiteMgt.pIpWhiteTab == NULL) {
13!
142
    TAOS_RETURN(terrno);
×
143
  }
144
  ipWhiteMgt.ver = 0;
13✔
145
  (void)taosThreadRwlockInit(&ipWhiteMgt.rw, NULL);
13✔
146
  TAOS_RETURN(0);
13✔
147
}
148
void ipWhiteMgtCleanup() {
13✔
149
  destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
13✔
150
  (void)taosThreadRwlockDestroy(&ipWhiteMgt.rw);
13✔
151
}
13✔
152

153
int32_t ipWhiteMgtUpdate(SMnode *pMnode, char *user, SIpWhiteList *pNew) {
3✔
154
  int32_t code = 0;
3✔
155
  int32_t lino = 0;
3✔
156
  bool    update = true;
3✔
157
  SArray *fqdns = NULL;
3✔
158
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
3✔
159
  SIpWhiteList **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
3✔
160

161
  if (ppList == NULL || *ppList == NULL) {
6!
162
    SIpWhiteList *p = cloneIpWhiteList(pNew);
3✔
163
    if (p == NULL) {
3!
164
      update = false;
×
165
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
166
    }
167
    if ((code = taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *))) != 0) {
3!
168
      update = false;
×
169
      taosMemoryFree(p);
×
170
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
171
    }
172
  } else {
UNCOV
173
    SIpWhiteList *pOld = *ppList;
×
UNCOV
174
    if (isIpWhiteListEqual(pOld, pNew)) {
×
UNCOV
175
      update = false;
×
176
    } else {
UNCOV
177
      taosMemoryFree(pOld);
×
UNCOV
178
      SIpWhiteList *p = cloneIpWhiteList(pNew);
×
UNCOV
179
      if (p == NULL) {
×
180
        update = false;
×
181
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
182
      }
UNCOV
183
      if ((code = taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *))) != 0) {
×
184
        update = false;
×
185
        taosMemoryFree(p);
×
186
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
187
      }
188
    }
189
  }
190

191
  fqdns = mndGetAllDnodeFqdns(pMnode);  // TODO: update this line after refactor api
3✔
192
  if (fqdns == NULL) {
3!
193
    update = false;
×
194
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
195
  }
196

197
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
6✔
198
    char *fqdn = taosArrayGetP(fqdns, i);
3✔
199
    bool  upd = false;
3✔
200
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, TSDB_DEFAULT_USER, fqdn, IP_WHITE_ADD, &upd), &lino,
3!
201
                    _OVER);
202
    update |= upd;
3✔
203
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, IP_WHITE_ADD, &upd), &lino, _OVER);
3!
204
    update |= upd;
3✔
205
  }
206

207
  // for (int i = 0; i < taosArrayGetSize(pUserNames); i++) {
208
  //   taosMemoryFree(taosArrayGetP(pUserNames, i));
209
  // }
210
  // taosArrayDestroy(pUserNames);
211

212
  if (update) ipWhiteMgt.ver++;
3!
213

UNCOV
214
_OVER:
×
215
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
3✔
216
  taosArrayDestroyP(fqdns, NULL);
3✔
217
  if (code < 0) {
3!
218
    mError("failed to update ip white list for user: %s at line %d since %s", user, lino, tstrerror(code));
×
219
  }
220
  TAOS_RETURN(code);
3✔
221
}
UNCOV
222
int32_t ipWhiteMgtRemove(char *user) {
×
UNCOV
223
  bool    update = true;
×
UNCOV
224
  int32_t code = 0;
×
UNCOV
225
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
×
UNCOV
226
  SIpWhiteList **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
×
UNCOV
227
  if (ppList == NULL || *ppList == NULL) {
×
228
    update = false;
×
229
  } else {
UNCOV
230
    taosMemoryFree(*ppList);
×
UNCOV
231
    code = taosHashRemove(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
×
UNCOV
232
    if (code != 0) {
×
233
      update = false;
×
234
    }
235
  }
236

UNCOV
237
  if (update) ipWhiteMgt.ver++;
×
UNCOV
238
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
UNCOV
239
  return 0;
×
240
}
241

242
bool isRangeInWhiteList(SIpWhiteList *pList, SIpV4Range *range) {
76✔
243
  for (int i = 0; i < pList->num; i++) {
76!
244
    if (isIpRangeEqual(&pList->pIpRange[i], range)) {
76!
245
      return true;
76✔
246
    }
247
  }
UNCOV
248
  return false;
×
249
}
250
#if 0
251
int32_t ipWhiteUpdateForAllUser(SIpWhiteList *pList) {
252
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
253

254
  SHashObj *pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
255
  void     *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
256

257
  while (pIter) {
258
    SIpWhiteList *p = *(SIpWhiteList **)pIter;
259
    SIpWhiteList *clone = cloneIpWhiteList(pList);
260
    int32_t       idx = 0;
261
    for (int i = 0; i < pList->num; i++) {
262
      SIpV4Range *e = &pList->pIpRange[i];
263
      if (!isRangeInWhiteList(p, e)) {
264
        clone->pIpRange[idx] = *e;
265
        idx++;
266
      }
267
    }
268
    clone->num = idx;
269

270
    SIpWhiteList *val = NULL;
271
    if (clone->num != 0) {
272
      int32_t sz = clone->num + p->num;
273
      val = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sz * sizeof(SIpV4Range));
274
      (void)memcpy(val->pIpRange, p->pIpRange, sizeof(SIpV4Range) * p->num);
275
      (void)memcpy(((char *)val->pIpRange) + sizeof(SIpV4Range) * p->num, (char *)clone->pIpRange,
276
             sizeof(SIpV4Range) * clone->num);
277

278
    } else {
279
      val = cloneIpWhiteList(p);
280
    }
281
    taosMemoryFree(clone);
282

283
    size_t klen;
284
    void  *key = taosHashGetKey(pIter, &klen);
285
    taosHashPut(pIpWhiteTab, key, klen, val, sizeof(void *));
286
  }
287

288
  destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
289

290
  ipWhiteMgt.pIpWhiteTab = pIpWhiteTab;
291
  ipWhiteMgt.ver++;
292
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
293
  return 0;
294
}
295
#endif
296

297
static int32_t ipWhiteMgtUpdateAll(SMnode *pMnode) {
13✔
298
  SHashObj *pNew = NULL;
13✔
299
  TAOS_CHECK_RETURN(mndFetchAllIpWhite(pMnode, &pNew));
13!
300

301
  SHashObj *pOld = ipWhiteMgt.pIpWhiteTab;
13✔
302

303
  ipWhiteMgt.pIpWhiteTab = pNew;
13✔
304
  ipWhiteMgt.ver++;
13✔
305

306
  destroyIpWhiteTab(pOld);
13✔
307
  TAOS_RETURN(0);
13✔
308
}
309

310
#if 0
311
void ipWhiteMgtUpdate2(SMnode *pMnode) {
312
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
313

314
  ipWhiteMgtUpdateAll(pMnode);
315

316
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
317
}
318
#endif
319

320
int64_t mndGetIpWhiteVer(SMnode *pMnode) {
6,827✔
321
  int64_t ver = 0;
6,827✔
322
  int32_t code = 0;
6,827✔
323
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
6,827✔
324
  if (ipWhiteMgt.ver == 0) {
6,827!
325
    // get user and dnode ip white list
326
    if ((code = ipWhiteMgtUpdateAll(pMnode)) != 0) {
×
327
      (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
328
      mError("%s failed to update ip white list since %s", __func__, tstrerror(code));
×
329
      return ver;
×
330
    }
331
    ipWhiteMgt.ver = taosGetTimestampMs();
×
332
  }
333
  ver = ipWhiteMgt.ver;
6,827✔
334
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
6,827✔
335

336
  if (mndEnableIpWhiteList(pMnode) == 0 || tsEnableWhiteList == false) {
6,827!
337
    ver = 0;
6,827✔
338
  }
339
  mDebug("ip-white-list on mnode ver: %" PRId64 "", ver);
6,827✔
340
  return ver;
6,827✔
341
}
342

343
int32_t mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t type, bool *pUpdate) {
89✔
344
  int32_t    lino = 0;
89✔
345
  bool       update = false;
89✔
346
  SIpV4Range range = {.ip = 0, .mask = 32};
89✔
347
  int32_t    code = taosGetIpv4FromFqdn(fqdn, &range.ip);
89✔
348
  if (code) {
89!
349
    mError("failed to get ip from fqdn: %s at line %d since %s", fqdn, lino, tstrerror(code));
×
350
    TAOS_RETURN(TSDB_CODE_TSC_INVALID_FQDN);
×
351
  }
352
  mDebug("ip-white-list may update for user: %s, fqdn: %s", user, fqdn);
89✔
353
  SIpWhiteList **ppList = taosHashGet(pIpWhiteTab, user, strlen(user));
89✔
354
  SIpWhiteList  *pList = NULL;
89✔
355
  if (ppList != NULL && *ppList != NULL) {
89!
356
    pList = *ppList;
76✔
357
  }
358

359
  if (type == IP_WHITE_ADD) {
89✔
360
    if (pList == NULL) {
87✔
361
      SIpWhiteList *pNewList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range));
12!
362
      if (pNewList == NULL) {
12!
363
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
364
      }
365
      (void)memcpy(pNewList->pIpRange, &range, sizeof(SIpV4Range));
12✔
366
      pNewList->num = 1;
12✔
367

368
      if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *))) != 0) {
12!
369
        taosMemoryFree(pNewList);
×
370
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
371
      }
372
      update = true;
12✔
373
    } else {
374
      if (!isRangeInWhiteList(pList, &range)) {
75!
UNCOV
375
        int32_t       sz = sizeof(SIpWhiteList) + sizeof(SIpV4Range) * (pList->num + 1);
×
UNCOV
376
        SIpWhiteList *pNewList = taosMemoryCalloc(1, sz);
×
UNCOV
377
        if (pNewList == NULL) {
×
378
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
379
        }
UNCOV
380
        (void)memcpy(pNewList->pIpRange, pList->pIpRange, sizeof(SIpV4Range) * (pList->num));
×
UNCOV
381
        pNewList->pIpRange[pList->num].ip = range.ip;
×
UNCOV
382
        pNewList->pIpRange[pList->num].mask = range.mask;
×
383

UNCOV
384
        pNewList->num = pList->num + 1;
×
385

UNCOV
386
        if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *))) != 0) {
×
387
          taosMemoryFree(pNewList);
×
388
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
389
        }
UNCOV
390
        taosMemoryFree(pList);
×
UNCOV
391
        update = true;
×
392
      }
393
    }
394
  } else if (type == IP_WHITE_DROP) {
2!
395
    if (pList != NULL) {
2✔
396
      if (isRangeInWhiteList(pList, &range)) {
1!
397
        if (pList->num == 1) {
1!
398
          if (taosHashRemove(pIpWhiteTab, user, strlen(user)) < 0) {
1!
399
            mError("failed to remove ip-white-list for user: %s at line %d", user, lino);
×
400
          }
401
          taosMemoryFree(pList);
1!
402
        } else {
403
          int32_t       idx = 0;
×
404
          int32_t       sz = sizeof(SIpWhiteList) + sizeof(SIpV4Range) * (pList->num - 1);
×
405
          SIpWhiteList *pNewList = taosMemoryCalloc(1, sz);
×
406
          if (pNewList == NULL) {
×
407
            TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
408
          }
409
          for (int i = 0; i < pList->num; i++) {
×
410
            SIpV4Range *e = &pList->pIpRange[i];
×
411
            if (!isIpRangeEqual(e, &range)) {
×
412
              pNewList->pIpRange[idx].ip = e->ip;
×
413
              pNewList->pIpRange[idx].mask = e->mask;
×
414
              idx++;
×
415
            }
416
          }
417
          pNewList->num = idx;
×
418
          if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *)) != 0)) {
×
419
            taosMemoryFree(pNewList);
×
420
            TAOS_CHECK_GOTO(code, &lino, _OVER);
×
421
          }
422
          taosMemoryFree(pList);
×
423
        }
424
        update = true;
1✔
425
      }
426
    }
427
  }
428
  if (update) {
89✔
429
    mDebug("ip-white-list update for user: %s, fqdn: %s", user, fqdn);
13✔
430
  }
431

432
_OVER:
78✔
433
  if (pUpdate) *pUpdate = update;
89✔
434
  if (code < 0) {
89!
435
    mError("failed to update ip-white-list for user: %s, fqdn: %s at line %d since %s", user, fqdn, lino,
×
436
           tstrerror(code));
437
  }
438
  TAOS_RETURN(code);
89✔
439
}
440

441
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
13✔
442
  int32_t code = 0;
13✔
443
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
13✔
444

445
  if ((code = ipWhiteMgtUpdateAll(pMnode)) != 0) {
13!
446
    (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
447
    TAOS_RETURN(code);
×
448
  }
449
  ipWhiteMgt.ver = taosGetTimestampMs();
13✔
450
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
13✔
451

452
  TAOS_RETURN(code);
13✔
453
}
454

455
int32_t mndUpdateIpWhiteForAllUser(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock) {
34✔
456
  int32_t code = 0;
34✔
457
  int32_t lino = 0;
34✔
458
  bool    update = false;
34✔
459

460
  if (lock) {
34!
461
    (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
34✔
462
    if (ipWhiteMgt.ver == 0) {
34!
463
      TAOS_CHECK_GOTO(ipWhiteMgtUpdateAll(pMnode), &lino, _OVER);
×
464
      ipWhiteMgt.ver = taosGetTimestampMs();
×
465
      mInfo("update ip-white-list, user: %s, ver: %" PRId64, user, ipWhiteMgt.ver);
×
466
    }
467
  }
468

469
  TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, type, &update), &lino, _OVER);
34!
470

471
  void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
34✔
472
  while (pIter) {
82✔
473
    size_t klen = 0;
48✔
474
    char  *key = taosHashGetKey(pIter, &klen);
48✔
475

476
    char *keyDup = taosMemoryCalloc(1, klen + 1);
48!
477
    if (keyDup == NULL) {
48!
478
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
479
    }
480
    (void)memcpy(keyDup, key, klen);
48✔
481
    bool upd = false;
48✔
482
    code = mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, keyDup, fqdn, type, &upd);
48✔
483
    update |= upd;
48✔
484
    if (code < 0) {
48!
485
      taosMemoryFree(keyDup);
×
486
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
487
    }
488
    taosMemoryFree(keyDup);
48!
489

490
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
48✔
491
  }
492

493
_OVER:
34✔
494
  if (update) ipWhiteMgt.ver++;
34✔
495
  if (lock) (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
34!
496
  if (code < 0) {
34!
497
    mError("failed to update ip-white-list for user: %s, fqdn: %s at line %d since %s", user, fqdn, lino,
×
498
           tstrerror(code));
499
  }
500

501
  TAOS_RETURN(code);
34✔
502
}
503

UNCOV
504
static int64_t ipWhiteMgtFillMsg(SUpdateIpWhite *pUpdate) {
×
UNCOV
505
  int64_t ver = 0;
×
UNCOV
506
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
×
UNCOV
507
  ver = ipWhiteMgt.ver;
×
UNCOV
508
  int32_t num = taosHashGetSize(ipWhiteMgt.pIpWhiteTab);
×
509

UNCOV
510
  pUpdate->pUserIpWhite = taosMemoryCalloc(1, num * sizeof(SUpdateUserIpWhite));
×
UNCOV
511
  if (pUpdate->pUserIpWhite == NULL) {
×
512
    (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
513
    TAOS_RETURN(terrno);
×
514
  }
515

UNCOV
516
  void   *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
×
UNCOV
517
  int32_t i = 0;
×
UNCOV
518
  while (pIter) {
×
UNCOV
519
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[i];
×
UNCOV
520
    SIpWhiteList       *list = *(SIpWhiteList **)pIter;
×
521

522
    size_t klen;
UNCOV
523
    char  *key = taosHashGetKey(pIter, &klen);
×
UNCOV
524
    if (list->num != 0) {
×
UNCOV
525
      pUser->ver = ver;
×
UNCOV
526
      (void)memcpy(pUser->user, key, klen);
×
UNCOV
527
      pUser->numOfRange = list->num;
×
UNCOV
528
      pUser->pIpRanges = taosMemoryCalloc(1, list->num * sizeof(SIpV4Range));
×
UNCOV
529
      if (pUser->pIpRanges == NULL) {
×
530
        (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
531
        TAOS_RETURN(terrno);
×
532
      }
UNCOV
533
      (void)memcpy(pUser->pIpRanges, list->pIpRange, list->num * sizeof(SIpV4Range));
×
UNCOV
534
      i++;
×
535
    }
UNCOV
536
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
×
537
  }
UNCOV
538
  pUpdate->numOfUser = i;
×
UNCOV
539
  pUpdate->ver = ver;
×
540

UNCOV
541
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
UNCOV
542
  TAOS_RETURN(0);
×
543
}
544

545
void destroyIpWhiteTab(SHashObj *pIpWhiteTab) {
26✔
546
  if (pIpWhiteTab == NULL) return;
26!
547

548
  void *pIter = taosHashIterate(pIpWhiteTab, NULL);
26✔
549
  while (pIter) {
41✔
550
    SIpWhiteList *list = *(SIpWhiteList **)pIter;
15✔
551
    taosMemoryFree(list);
15!
552
    pIter = taosHashIterate(pIpWhiteTab, pIter);
15✔
553
  }
554

555
  taosHashCleanup(pIpWhiteTab);
26✔
556
}
557
int32_t mndFetchAllIpWhite(SMnode *pMnode, SHashObj **ppIpWhiteTab) {
13✔
558
  int32_t   code = 0;
13✔
559
  int32_t   lino = 0;
13✔
560
  SSdb     *pSdb = pMnode->pSdb;
13✔
561
  void     *pIter = NULL;
13✔
562
  SHashObj *pIpWhiteTab = NULL;
13✔
563
  SArray   *pUserNames = NULL;
13✔
564
  SArray   *fqdns = NULL;
13✔
565

566
  pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
13✔
567
  if (pIpWhiteTab == NULL) {
13!
568
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
569
  }
570
  pUserNames = taosArrayInit(8, sizeof(void *));
13✔
571
  if (pUserNames == NULL) {
13!
572
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
573
  }
574

575
  while (1) {
1✔
576
    SUserObj *pUser = NULL;
14✔
577
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
14✔
578
    if (pIter == NULL) break;
14✔
579

580
    SIpWhiteList *pWhiteList = cloneIpWhiteList(pUser->pIpWhiteList);
1✔
581
    if (pWhiteList == NULL) {
1!
582
      sdbRelease(pSdb, pUser);
×
583
      sdbCancelFetch(pSdb, pIter);
×
584
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
585
    }
586
    if ((code = taosHashPut(pIpWhiteTab, pUser->user, strlen(pUser->user), &pWhiteList, sizeof(void *))) != 0) {
1!
587
      taosMemoryFree(pWhiteList);
×
588
      sdbRelease(pSdb, pUser);
×
589
      sdbCancelFetch(pSdb, pIter);
×
590
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
591
    }
592

593
    char *name = taosStrdup(pUser->user);
1!
594
    if (name == NULL) {
1!
595
      sdbRelease(pSdb, pUser);
×
596
      sdbCancelFetch(pSdb, pIter);
×
597
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
598
    }
599
    if (taosArrayPush(pUserNames, &name) == NULL) {
1!
600
      taosMemoryFree(name);
×
601
      sdbRelease(pSdb, pUser);
×
602
      sdbCancelFetch(pSdb, pIter);
×
603
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
604
    }
605

606
    sdbRelease(pSdb, pUser);
1✔
607
  }
608

609
  bool found = false;
13✔
610
  for (int i = 0; i < taosArrayGetSize(pUserNames); i++) {
13✔
611
    char *name = taosArrayGetP(pUserNames, i);
1✔
612
    if (strlen(name) == strlen(TSDB_DEFAULT_USER) && strncmp(name, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER)) == 0) {
1!
613
      found = true;
1✔
614
      break;
1✔
615
    }
616
  }
617
  if (found == false) {
13✔
618
    char *name = taosStrdup(TSDB_DEFAULT_USER);
12!
619
    if (name == NULL) {
12!
620
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
621
    }
622
    if (taosArrayPush(pUserNames, &name) == NULL) {
12!
623
      taosMemoryFree(name);
×
624
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
625
    }
626
  }
627

628
  fqdns = mndGetAllDnodeFqdns(pMnode);  // TODO: refactor this line after refactor api
13✔
629
  if (fqdns == NULL) {
13!
630
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
631
  }
632

633
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
14✔
634
    char *fqdn = taosArrayGetP(fqdns, i);
1✔
635

636
    for (int j = 0; j < taosArrayGetSize(pUserNames); j++) {
2✔
637
      char *name = taosArrayGetP(pUserNames, j);
1✔
638
      TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(pIpWhiteTab, name, fqdn, IP_WHITE_ADD, NULL), &lino, _OVER);
1!
639
    }
640
  }
641

642
_OVER:
13✔
643
  taosArrayDestroyP(fqdns, NULL);
13✔
644
  taosArrayDestroyP(pUserNames, NULL);
13✔
645

646
  if (code < 0) {
13!
647
    mError("failed to fetch all ip white list at line %d since %s", lino, tstrerror(code));
×
648
    destroyIpWhiteTab(pIpWhiteTab);
×
649
    pIpWhiteTab = NULL;
×
650
  }
651
  *ppIpWhiteTab = pIpWhiteTab;
13✔
652
  TAOS_RETURN(code);
13✔
653
}
654

655
int32_t mndInitUser(SMnode *pMnode) {
13✔
656
  TAOS_CHECK_RETURN(ipWhiteMgtInit());
13!
657

658
  SSdbTable table = {
13✔
659
      .sdbType = SDB_USER,
660
      .keyType = SDB_KEY_BINARY,
661
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
662
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
663
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
664
      .insertFp = (SdbInsertFp)mndUserActionInsert,
665
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
666
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
667
  };
668

669
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
13✔
670
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
13✔
671
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
13✔
672
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
13✔
673
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST, mndProcessGetUserWhiteListReq);
13✔
674

675
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE, mndProcesSRetrieveIpWhiteReq);
13✔
676

677
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
13✔
678
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
13✔
679
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndRetrieveUsersFull);
13✔
680
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndCancelGetNextUser);
13✔
681
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
13✔
682
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
13✔
683
  return sdbSetTable(pMnode->pSdb, table);
13✔
684
}
685

686
void mndCleanupUser(SMnode *pMnode) { ipWhiteMgtCleanup(); }
13✔
687

UNCOV
688
static void ipRangeToStr(SIpV4Range *range, char *buf) {
×
689
  struct in_addr addr;
UNCOV
690
  addr.s_addr = range->ip;
×
691

UNCOV
692
  (void)uv_inet_ntop(AF_INET, &addr, buf, 32);
×
UNCOV
693
  if (range->mask != 32) {
×
UNCOV
694
    (void)tsnprintf(buf + strlen(buf), 36 - strlen(buf), "/%d", range->mask);
×
695
  }
UNCOV
696
  return;
×
697
}
UNCOV
698
static bool isDefaultRange(SIpV4Range *pRange) {
×
699
  static SIpV4Range val = {.ip = 16777343, .mask = 32};
UNCOV
700
  return pRange->ip == val.ip && pRange->mask == val.mask;
×
701
}
UNCOV
702
static int32_t ipRangeListToStr(SIpV4Range *range, int32_t num, char *buf, int64_t bufLen) {
×
UNCOV
703
  int32_t len = 0;
×
UNCOV
704
  for (int i = 0; i < num; i++) {
×
UNCOV
705
    char        tbuf[36] = {0};
×
UNCOV
706
    SIpV4Range *pRange = &range[i];
×
707

UNCOV
708
    ipRangeToStr(&range[i], tbuf);
×
UNCOV
709
    len += tsnprintf(buf + len, bufLen - len, "%s,", tbuf);
×
710
  }
UNCOV
711
  if (len > 0) buf[len - 1] = 0;
×
UNCOV
712
  return len;
×
713
}
714

715
static bool isIpRangeEqual(SIpV4Range *a, SIpV4Range *b) {
76✔
716
  // equal or not
717
  return a->ip == b->ip && a->mask == b->mask;
76!
718
}
UNCOV
719
static bool isRangeInIpWhiteList(SIpWhiteList *pList, SIpV4Range *tgt) {
×
UNCOV
720
  for (int i = 0; i < pList->num; i++) {
×
UNCOV
721
    if (isIpRangeEqual(&pList->pIpRange[i], tgt)) return true;
×
722
  }
UNCOV
723
  return false;
×
724
}
UNCOV
725
static bool isIpWhiteListEqual(SIpWhiteList *a, SIpWhiteList *b) {
×
UNCOV
726
  if (a->num != b->num) {
×
UNCOV
727
    return false;
×
728
  }
UNCOV
729
  for (int i = 0; i < a->num; i++) {
×
UNCOV
730
    if (!isIpRangeEqual(&a->pIpRange[i], &b->pIpRange[i])) {
×
731
      return false;
×
732
    }
733
  }
UNCOV
734
  return true;
×
735
}
UNCOV
736
int32_t convertIpWhiteListToStr(SIpWhiteList *pList, char **buf) {
×
UNCOV
737
  if (pList->num == 0) {
×
738
    *buf = NULL;
×
739
    return 0;
×
740
  }
UNCOV
741
  int64_t bufLen = pList->num * 36;
×
UNCOV
742
  *buf = taosMemoryCalloc(1, bufLen);
×
UNCOV
743
  if (*buf == NULL) {
×
744
    return 0;
×
745
  }
746

UNCOV
747
  int32_t len = ipRangeListToStr(pList->pIpRange, pList->num, *buf, bufLen);
×
UNCOV
748
  if (len == 0) {
×
749
    taosMemoryFreeClear(*buf);
×
750
    return 0;
×
751
  }
UNCOV
752
  return strlen(*buf);
×
753
}
754
int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteList *pList, uint32_t *pLen) {
44✔
755
  int32_t  code = 0;
44✔
756
  int32_t  lino = 0;
44✔
757
  int32_t  tlen = 0;
44✔
758
  SEncoder encoder = {0};
44✔
759
  tEncoderInit(&encoder, buf, len);
44✔
760

761
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
44!
762
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
88!
763

764
  for (int i = 0; i < pList->num; i++) {
88✔
765
    SIpV4Range *pRange = &(pList->pIpRange[i]);
44✔
766
    TAOS_CHECK_GOTO(tEncodeU32(&encoder, pRange->ip), &lino, _OVER);
88!
767
    TAOS_CHECK_GOTO(tEncodeU32(&encoder, pRange->mask), &lino, _OVER);
88!
768
  }
769

770
  tEndEncode(&encoder);
44✔
771

772
  tlen = encoder.pos;
44✔
773
_OVER:
44✔
774
  tEncoderClear(&encoder);
44✔
775
  if (code < 0) {
44!
776
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
777
  }
778
  if (pLen) *pLen = tlen;
44!
779
  TAOS_RETURN(code);
44✔
780
}
781

782
int32_t tDerializeIpWhileList(void *buf, int32_t len, SIpWhiteList *pList) {
16✔
783
  int32_t  code = 0;
16✔
784
  int32_t  lino = 0;
16✔
785
  SDecoder decoder = {0};
16✔
786
  tDecoderInit(&decoder, buf, len);
16✔
787

788
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
16!
789
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
32!
790

791
  for (int i = 0; i < pList->num; i++) {
32✔
792
    SIpV4Range *pRange = &(pList->pIpRange[i]);
16✔
793
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pRange->ip), &lino, _OVER);
32!
794
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pRange->mask), &lino, _OVER);
32!
795
  }
796

797
_OVER:
16✔
798
  tEndDecode(&decoder);
16✔
799
  tDecoderClear(&decoder);
16✔
800
  if (code < 0) {
16!
801
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
802
  }
803

804
  TAOS_RETURN(code);
16✔
805
}
806

807
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteList **ppList) {
16✔
808
  int32_t       code = 0;
16✔
809
  int32_t       lino = 0;
16✔
810
  int32_t       num = 0;
16✔
811
  SIpWhiteList *p = NULL;
16✔
812
  SDecoder      decoder = {0};
16✔
813
  tDecoderInit(&decoder, buf, len);
16✔
814

815
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
16!
816
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
16!
817

818
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
16!
819
  if (p == NULL) {
16!
820
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
821
  }
822
  TAOS_CHECK_GOTO(tDerializeIpWhileList(buf, len, p), &lino, _OVER);
16!
823

824
_OVER:
16✔
825
  tEndDecode(&decoder);
16✔
826
  tDecoderClear(&decoder);
16✔
827
  if (code < 0) {
16!
828
    taosMemoryFreeClear(p);
×
829
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
830
  }
831
  *ppList = p;
16✔
832
  TAOS_RETURN(code);
16✔
833
}
834

835
static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList) {
15✔
836
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1);
15!
837
  if (*ppWhiteList == NULL) {
15!
838
    TAOS_RETURN(terrno);
×
839
  }
840
  (*ppWhiteList)->num = 1;
15✔
841
  SIpV4Range *range = &((*ppWhiteList)->pIpRange[0]);
15✔
842

843
  struct in_addr addr;
844
  if (uv_inet_pton(AF_INET, "127.0.0.1", &addr) == 0) {
15!
845
    range->ip = addr.s_addr;
15✔
846
    range->mask = 32;
15✔
847
  }
848
  return 0;
15✔
849
}
850

851
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
12✔
852
  int32_t  code = 0;
12✔
853
  int32_t  lino = 0;
12✔
854
  SUserObj userObj = {0};
12✔
855
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
12✔
856
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
12✔
857
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
12✔
858
  userObj.createdTime = taosGetTimestampMs();
12✔
859
  userObj.updateTime = userObj.createdTime;
12✔
860
  userObj.sysInfo = 1;
12✔
861
  userObj.enable = 1;
12✔
862
  userObj.ipWhiteListVer = taosGetTimestampMs();
12✔
863
  TAOS_CHECK_RETURN(createDefaultIpWhiteList(&userObj.pIpWhiteList));
12!
864
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
12!
865
    userObj.superUser = 1;
12✔
866
    userObj.createdb = 1;
12✔
867
  }
868

869
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
12✔
870
  if (pRaw == NULL) goto _ERROR;
12!
871
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
12!
872

873
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
12!
874

875
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
12✔
876
  if (pTrans == NULL) {
12!
877
    sdbFreeRaw(pRaw);
×
878
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
879
    goto _ERROR;
×
880
  }
881
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
12!
882

883
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
12!
884
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
885
    mndTransDrop(pTrans);
×
886
    goto _ERROR;
×
887
  }
888
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
12!
889

890
  if (mndTransPrepare(pMnode, pTrans) != 0) {
12!
891
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
892
    mndTransDrop(pTrans);
×
893
    goto _ERROR;
×
894
  }
895

896
  mndTransDrop(pTrans);
12✔
897
  taosMemoryFree(userObj.pIpWhiteList);
12!
898
  return 0;
12✔
899
_ERROR:
×
900
  taosMemoryFree(userObj.pIpWhiteList);
×
901
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
902
}
903

904
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
12✔
905
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
12✔
906
}
907

908
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
44✔
909
  int32_t code = 0;
44✔
910
  int32_t lino = 0;
44✔
911
  int32_t ipWhiteReserve =
×
912
      pUser->pIpWhiteList ? (sizeof(SIpV4Range) * pUser->pIpWhiteList->num + sizeof(SIpWhiteList) + 4) : 16;
44!
913
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
44✔
914
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
44✔
915
  int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
44✔
916
  int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
44✔
917
  int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
44✔
918
  int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
44✔
919
  int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
44✔
920
  int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
44✔
921
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
44✔
922
  int32_t numOfUseDbs = taosHashGetSize(pUser->useDbs);
44✔
923
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
44✔
924
                 numOfTopics * TSDB_TOPIC_FNAME_LEN + ipWhiteReserve;
44✔
925
  char    *buf = NULL;
44✔
926
  SSdbRaw *pRaw = NULL;
44✔
927

928
  char *stb = taosHashIterate(pUser->readTbs, NULL);
44✔
929
  while (stb != NULL) {
44!
UNCOV
930
    size_t keyLen = 0;
×
UNCOV
931
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
932
    size += sizeof(int32_t);
×
UNCOV
933
    size += keyLen;
×
934

UNCOV
935
    size_t valueLen = 0;
×
UNCOV
936
    valueLen = strlen(stb) + 1;
×
UNCOV
937
    size += sizeof(int32_t);
×
UNCOV
938
    size += valueLen;
×
UNCOV
939
    stb = taosHashIterate(pUser->readTbs, stb);
×
940
  }
941

942
  stb = taosHashIterate(pUser->writeTbs, NULL);
44✔
943
  while (stb != NULL) {
44!
UNCOV
944
    size_t keyLen = 0;
×
UNCOV
945
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
946
    size += sizeof(int32_t);
×
UNCOV
947
    size += keyLen;
×
948

UNCOV
949
    size_t valueLen = 0;
×
UNCOV
950
    valueLen = strlen(stb) + 1;
×
UNCOV
951
    size += sizeof(int32_t);
×
UNCOV
952
    size += valueLen;
×
UNCOV
953
    stb = taosHashIterate(pUser->writeTbs, stb);
×
954
  }
955

956
  stb = taosHashIterate(pUser->alterTbs, NULL);
44✔
957
  while (stb != NULL) {
44!
UNCOV
958
    size_t keyLen = 0;
×
UNCOV
959
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
960
    size += sizeof(int32_t);
×
UNCOV
961
    size += keyLen;
×
962

UNCOV
963
    size_t valueLen = 0;
×
UNCOV
964
    valueLen = strlen(stb) + 1;
×
UNCOV
965
    size += sizeof(int32_t);
×
UNCOV
966
    size += valueLen;
×
UNCOV
967
    stb = taosHashIterate(pUser->alterTbs, stb);
×
968
  }
969

970
  stb = taosHashIterate(pUser->readViews, NULL);
44✔
971
  while (stb != NULL) {
44!
UNCOV
972
    size_t keyLen = 0;
×
UNCOV
973
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
974
    size += sizeof(int32_t);
×
UNCOV
975
    size += keyLen;
×
976

UNCOV
977
    size_t valueLen = 0;
×
UNCOV
978
    valueLen = strlen(stb) + 1;
×
UNCOV
979
    size += sizeof(int32_t);
×
UNCOV
980
    size += valueLen;
×
UNCOV
981
    stb = taosHashIterate(pUser->readViews, stb);
×
982
  }
983

984
  stb = taosHashIterate(pUser->writeViews, NULL);
44✔
985
  while (stb != NULL) {
44!
UNCOV
986
    size_t keyLen = 0;
×
UNCOV
987
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
988
    size += sizeof(int32_t);
×
UNCOV
989
    size += keyLen;
×
990

UNCOV
991
    size_t valueLen = 0;
×
UNCOV
992
    valueLen = strlen(stb) + 1;
×
UNCOV
993
    size += sizeof(int32_t);
×
UNCOV
994
    size += valueLen;
×
UNCOV
995
    stb = taosHashIterate(pUser->writeViews, stb);
×
996
  }
997

998
  stb = taosHashIterate(pUser->alterViews, NULL);
44✔
999
  while (stb != NULL) {
44!
UNCOV
1000
    size_t keyLen = 0;
×
UNCOV
1001
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1002
    size += sizeof(int32_t);
×
UNCOV
1003
    size += keyLen;
×
1004

UNCOV
1005
    size_t valueLen = 0;
×
UNCOV
1006
    valueLen = strlen(stb) + 1;
×
UNCOV
1007
    size += sizeof(int32_t);
×
UNCOV
1008
    size += valueLen;
×
UNCOV
1009
    stb = taosHashIterate(pUser->alterViews, stb);
×
1010
  }
1011

1012
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
44✔
1013
  while (useDb != NULL) {
44!
UNCOV
1014
    size_t keyLen = 0;
×
UNCOV
1015
    void  *key = taosHashGetKey(useDb, &keyLen);
×
UNCOV
1016
    size += sizeof(int32_t);
×
UNCOV
1017
    size += keyLen;
×
UNCOV
1018
    size += sizeof(int32_t);
×
UNCOV
1019
    useDb = taosHashIterate(pUser->useDbs, useDb);
×
1020
  }
1021

1022
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
44✔
1023
  if (pRaw == NULL) {
44!
1024
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1025
  }
1026

1027
  int32_t dataPos = 0;
44✔
1028
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
44!
1029
  SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
44!
1030
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
44!
1031
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
44!
1032
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
44!
1033
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
44!
1034
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
44!
1035
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
44!
1036
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
44!
1037
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
44!
1038
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
44!
1039
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
44!
1040
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
44!
1041
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
44!
1042

1043
  char *db = taosHashIterate(pUser->readDbs, NULL);
44✔
1044
  while (db != NULL) {
44!
UNCOV
1045
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
×
UNCOV
1046
    db = taosHashIterate(pUser->readDbs, db);
×
1047
  }
1048

1049
  db = taosHashIterate(pUser->writeDbs, NULL);
44✔
1050
  while (db != NULL) {
44!
UNCOV
1051
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
×
UNCOV
1052
    db = taosHashIterate(pUser->writeDbs, db);
×
1053
  }
1054

1055
  char *topic = taosHashIterate(pUser->topics, NULL);
44✔
1056
  while (topic != NULL) {
44!
UNCOV
1057
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
×
UNCOV
1058
    topic = taosHashIterate(pUser->topics, topic);
×
1059
  }
1060

1061
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
44!
1062
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
44!
1063
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
44!
1064
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
44!
1065
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
44!
1066
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
44!
1067
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
44!
1068

1069
  stb = taosHashIterate(pUser->readTbs, NULL);
44✔
1070
  while (stb != NULL) {
44!
UNCOV
1071
    size_t keyLen = 0;
×
UNCOV
1072
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1073
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1074
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1075

UNCOV
1076
    size_t valueLen = 0;
×
UNCOV
1077
    valueLen = strlen(stb) + 1;
×
UNCOV
1078
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1079
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1080
    stb = taosHashIterate(pUser->readTbs, stb);
×
1081
  }
1082

1083
  stb = taosHashIterate(pUser->writeTbs, NULL);
44✔
1084
  while (stb != NULL) {
44!
UNCOV
1085
    size_t keyLen = 0;
×
UNCOV
1086
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1087
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1088
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1089

UNCOV
1090
    size_t valueLen = 0;
×
UNCOV
1091
    valueLen = strlen(stb) + 1;
×
UNCOV
1092
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1093
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1094
    stb = taosHashIterate(pUser->writeTbs, stb);
×
1095
  }
1096

1097
  stb = taosHashIterate(pUser->alterTbs, NULL);
44✔
1098
  while (stb != NULL) {
44!
UNCOV
1099
    size_t keyLen = 0;
×
UNCOV
1100
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1101
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1102
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1103

UNCOV
1104
    size_t valueLen = 0;
×
UNCOV
1105
    valueLen = strlen(stb) + 1;
×
UNCOV
1106
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1107
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1108
    stb = taosHashIterate(pUser->alterTbs, stb);
×
1109
  }
1110

1111
  stb = taosHashIterate(pUser->readViews, NULL);
44✔
1112
  while (stb != NULL) {
44!
UNCOV
1113
    size_t keyLen = 0;
×
UNCOV
1114
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1115
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1116
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1117

UNCOV
1118
    size_t valueLen = 0;
×
UNCOV
1119
    valueLen = strlen(stb) + 1;
×
UNCOV
1120
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1121
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1122
    stb = taosHashIterate(pUser->readViews, stb);
×
1123
  }
1124

1125
  stb = taosHashIterate(pUser->writeViews, NULL);
44✔
1126
  while (stb != NULL) {
44!
UNCOV
1127
    size_t keyLen = 0;
×
UNCOV
1128
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1129
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1130
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1131

UNCOV
1132
    size_t valueLen = 0;
×
UNCOV
1133
    valueLen = strlen(stb) + 1;
×
UNCOV
1134
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1135
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1136
    stb = taosHashIterate(pUser->writeViews, stb);
×
1137
  }
1138

1139
  stb = taosHashIterate(pUser->alterViews, NULL);
44✔
1140
  while (stb != NULL) {
44!
UNCOV
1141
    size_t keyLen = 0;
×
UNCOV
1142
    void  *key = taosHashGetKey(stb, &keyLen);
×
UNCOV
1143
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1144
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1145

UNCOV
1146
    size_t valueLen = 0;
×
UNCOV
1147
    valueLen = strlen(stb) + 1;
×
UNCOV
1148
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
×
UNCOV
1149
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
×
UNCOV
1150
    stb = taosHashIterate(pUser->alterViews, stb);
×
1151
  }
1152

1153
  useDb = taosHashIterate(pUser->useDbs, NULL);
44✔
1154
  while (useDb != NULL) {
44!
UNCOV
1155
    size_t keyLen = 0;
×
UNCOV
1156
    void  *key = taosHashGetKey(useDb, &keyLen);
×
UNCOV
1157
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
×
UNCOV
1158
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1159

UNCOV
1160
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
×
UNCOV
1161
    useDb = taosHashIterate(pUser->useDbs, useDb);
×
1162
  }
1163

1164
  // save white list
1165
  int32_t num = pUser->pIpWhiteList->num;
44✔
1166
  int32_t tlen = sizeof(SIpWhiteList) + num * sizeof(SIpV4Range) + 4;
44✔
1167
  if ((buf = taosMemoryCalloc(1, tlen)) == NULL) {
44!
1168
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1169
  }
1170
  int32_t len = 0;
44✔
1171
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteList, &len), &lino, _OVER);
44!
1172

1173
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
44!
1174
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
44!
1175

1176
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
44!
1177

1178
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
44!
1179
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
44!
1180

1181
_OVER:
44✔
1182
  taosMemoryFree(buf);
44!
1183
  if (code < 0) {
44!
1184
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1185
           tstrerror(code));
1186
    sdbFreeRaw(pRaw);
×
1187
    pRaw = NULL;
×
1188
    terrno = code;
×
1189
  }
1190

1191
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
44✔
1192
  return pRaw;
44✔
1193
}
1194

1195
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
16✔
1196
  int32_t   code = 0;
16✔
1197
  int32_t   lino = 0;
16✔
1198
  SSdbRow  *pRow = NULL;
16✔
1199
  SUserObj *pUser = NULL;
16✔
1200
  char     *key = NULL;
16✔
1201
  char     *value = NULL;
16✔
1202

1203
  int8_t sver = 0;
16✔
1204
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
16!
1205
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1206
  }
1207

1208
  if (sver < 1 || sver > USER_VER_NUMBER) {
16!
1209
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1210
  }
1211

1212
  pRow = sdbAllocRow(sizeof(SUserObj));
16✔
1213
  if (pRow == NULL) {
16!
1214
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1215
  }
1216

1217
  pUser = sdbGetRowObj(pRow);
16✔
1218
  if (pUser == NULL) {
16!
1219
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1220
  }
1221

1222
  int32_t dataPos = 0;
16✔
1223
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
16!
1224
  SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
16!
1225
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
16!
1226
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
16!
1227
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
16!
1228
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
16!
1229
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
16!
1230
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
16!
1231
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
16!
1232
  if (pUser->superUser) pUser->createdb = 1;
16✔
1233
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
16!
1234
  if (sver >= 4) {
16!
1235
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
16!
1236
  }
1237

1238
  int32_t numOfReadDbs = 0;
16✔
1239
  int32_t numOfWriteDbs = 0;
16✔
1240
  int32_t numOfTopics = 0;
16✔
1241
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
16!
1242
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
16!
1243
  if (sver >= 2) {
16!
1244
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
16!
1245
  }
1246

1247
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1248
  pUser->writeDbs =
16✔
1249
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1250
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1251
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
16!
1252
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1253
    goto _OVER;
×
1254
  }
1255

1256
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
16!
UNCOV
1257
    char db[TSDB_DB_FNAME_LEN] = {0};
×
UNCOV
1258
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
UNCOV
1259
    int32_t len = strlen(db) + 1;
×
UNCOV
1260
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
1261
  }
1262

1263
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
16!
UNCOV
1264
    char db[TSDB_DB_FNAME_LEN] = {0};
×
UNCOV
1265
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
×
UNCOV
1266
    int32_t len = strlen(db) + 1;
×
UNCOV
1267
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
×
1268
  }
1269

1270
  if (sver >= 2) {
16!
1271
    for (int32_t i = 0; i < numOfTopics; ++i) {
16!
UNCOV
1272
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
×
UNCOV
1273
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
×
UNCOV
1274
      int32_t len = strlen(topic) + 1;
×
UNCOV
1275
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
×
1276
    }
1277
  }
1278

1279
  if (sver >= 3) {
16!
1280
    int32_t numOfReadTbs = 0;
16✔
1281
    int32_t numOfWriteTbs = 0;
16✔
1282
    int32_t numOfAlterTbs = 0;
16✔
1283
    int32_t numOfReadViews = 0;
16✔
1284
    int32_t numOfWriteViews = 0;
16✔
1285
    int32_t numOfAlterViews = 0;
16✔
1286
    int32_t numOfUseDbs = 0;
16✔
1287
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
16!
1288
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
16!
1289
    if (sver >= 6) {
16!
1290
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
16!
1291
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
16!
1292
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
16!
1293
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
16!
1294
    }
1295
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
16!
1296

1297
    pUser->readTbs =
16✔
1298
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1299
    pUser->writeTbs =
16✔
1300
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1301
    pUser->alterTbs =
16✔
1302
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1303

1304
    pUser->readViews =
16✔
1305
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1306
    pUser->writeViews =
16✔
1307
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1308
    pUser->alterViews =
16✔
1309
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1310

1311
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16✔
1312

1313
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
16!
1314
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
16!
1315
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1316
      goto _OVER;
×
1317
    }
1318

1319
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
16!
UNCOV
1320
      int32_t keyLen = 0;
×
UNCOV
1321
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1322

UNCOV
1323
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1324
      if (key == NULL) {
×
1325
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1326
      }
UNCOV
1327
      (void)memset(key, 0, keyLen);
×
UNCOV
1328
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1329

UNCOV
1330
      int32_t valuelen = 0;
×
UNCOV
1331
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1332
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1333
      if (value == NULL) {
×
1334
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1335
      }
UNCOV
1336
      (void)memset(value, 0, valuelen);
×
UNCOV
1337
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1338

UNCOV
1339
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
1340
    }
1341

1342
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
16!
UNCOV
1343
      int32_t keyLen = 0;
×
UNCOV
1344
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1345

UNCOV
1346
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1347
      if (key == NULL) {
×
1348
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1349
      }
UNCOV
1350
      (void)memset(key, 0, keyLen);
×
UNCOV
1351
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1352

UNCOV
1353
      int32_t valuelen = 0;
×
UNCOV
1354
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1355
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1356
      if (value == NULL) {
×
1357
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1358
      }
UNCOV
1359
      (void)memset(value, 0, valuelen);
×
UNCOV
1360
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1361

UNCOV
1362
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
1363
    }
1364

1365
    if (sver >= 6) {
16!
1366
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
16!
UNCOV
1367
        int32_t keyLen = 0;
×
UNCOV
1368
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1369

UNCOV
1370
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1371
        if (key == NULL) {
×
1372
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1373
        }
UNCOV
1374
        (void)memset(key, 0, keyLen);
×
UNCOV
1375
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1376

UNCOV
1377
        int32_t valuelen = 0;
×
UNCOV
1378
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1379
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1380
        if (value == NULL) {
×
1381
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1382
        }
UNCOV
1383
        (void)memset(value, 0, valuelen);
×
UNCOV
1384
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1385

UNCOV
1386
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
×
1387
      }
1388

1389
      for (int32_t i = 0; i < numOfReadViews; ++i) {
16!
UNCOV
1390
        int32_t keyLen = 0;
×
UNCOV
1391
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1392

UNCOV
1393
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1394
        if (key == NULL) {
×
1395
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1396
        }
UNCOV
1397
        (void)memset(key, 0, keyLen);
×
UNCOV
1398
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1399

UNCOV
1400
        int32_t valuelen = 0;
×
UNCOV
1401
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1402
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1403
        if (value == NULL) {
×
1404
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1405
        }
UNCOV
1406
        (void)memset(value, 0, valuelen);
×
UNCOV
1407
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1408

UNCOV
1409
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
×
1410
      }
1411

1412
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
16!
UNCOV
1413
        int32_t keyLen = 0;
×
UNCOV
1414
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1415

UNCOV
1416
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1417
        if (key == NULL) {
×
1418
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1419
        }
UNCOV
1420
        (void)memset(key, 0, keyLen);
×
UNCOV
1421
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1422

UNCOV
1423
        int32_t valuelen = 0;
×
UNCOV
1424
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1425
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1426
        if (value == NULL) {
×
1427
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1428
        }
UNCOV
1429
        (void)memset(value, 0, valuelen);
×
UNCOV
1430
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1431

UNCOV
1432
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
×
1433
      }
1434

1435
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
16!
UNCOV
1436
        int32_t keyLen = 0;
×
UNCOV
1437
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1438

UNCOV
1439
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1440
        if (key == NULL) {
×
1441
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1442
        }
UNCOV
1443
        (void)memset(key, 0, keyLen);
×
UNCOV
1444
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1445

UNCOV
1446
        int32_t valuelen = 0;
×
UNCOV
1447
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
×
UNCOV
1448
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
×
UNCOV
1449
        if (value == NULL) {
×
1450
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1451
        }
UNCOV
1452
        (void)memset(value, 0, valuelen);
×
UNCOV
1453
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
×
1454

UNCOV
1455
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterViews, key, keyLen, value, valuelen), &lino, _OVER);
×
1456
      }
1457
    }
1458

1459
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
16!
UNCOV
1460
      int32_t keyLen = 0;
×
UNCOV
1461
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1462

UNCOV
1463
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
×
UNCOV
1464
      if (key == NULL) {
×
1465
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1466
      }
UNCOV
1467
      (void)memset(key, 0, keyLen);
×
UNCOV
1468
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
×
1469

UNCOV
1470
      int32_t ref = 0;
×
UNCOV
1471
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
×
1472

UNCOV
1473
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
×
1474
    }
1475
  }
1476
  // decoder white list
1477
  if (sver >= 5) {
16!
1478
    int32_t len = 0;
16✔
1479
    SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
16!
1480

1481
    TAOS_MEMORY_REALLOC(key, len);
16!
1482
    if (key == NULL) {
16!
1483
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1484
    }
1485
    SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
16!
1486

1487
    TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteList), &lino, _OVER);
16!
1488

1489
    SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
16!
1490
  }
1491

1492
  if (pUser->pIpWhiteList == NULL) {
16!
1493
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteList), &lino, _OVER);
×
1494
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1495
  }
1496

1497
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
16!
1498
  taosInitRWLatch(&pUser->lock);
16✔
1499

1500
_OVER:
16✔
1501
  taosMemoryFree(key);
16!
1502
  taosMemoryFree(value);
16!
1503
  if (code < 0) {
16!
1504
    terrno = code;
×
1505
    mError("user:%s, failed to decode at line %d from raw:%p since %s", pUser == NULL ? "null" : pUser->user, lino,
×
1506
           pRaw, tstrerror(code));
1507
    if (pUser != NULL) {
×
1508
      taosHashCleanup(pUser->readDbs);
×
1509
      taosHashCleanup(pUser->writeDbs);
×
1510
      taosHashCleanup(pUser->topics);
×
1511
      taosHashCleanup(pUser->readTbs);
×
1512
      taosHashCleanup(pUser->writeTbs);
×
1513
      taosHashCleanup(pUser->alterTbs);
×
1514
      taosHashCleanup(pUser->readViews);
×
1515
      taosHashCleanup(pUser->writeViews);
×
1516
      taosHashCleanup(pUser->alterViews);
×
1517
      taosHashCleanup(pUser->useDbs);
×
1518
      taosMemoryFreeClear(pUser->pIpWhiteList);
×
1519
    }
1520
    taosMemoryFreeClear(pRow);
×
1521
    return NULL;
×
1522
  }
1523

1524
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
16✔
1525
  return pRow;
16✔
1526
}
1527

1528
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
16✔
1529
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
16✔
1530

1531
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
16✔
1532
  if (pAcct == NULL) {
16!
1533
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
1534
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
1535
    TAOS_RETURN(terrno);
×
1536
  }
1537
  pUser->acctId = pAcct->acctId;
16✔
1538
  sdbRelease(pSdb, pAcct);
16✔
1539

1540
  return 0;
16✔
1541
}
1542

1543
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
8,524✔
1544
  int32_t code = 0;
8,524✔
1545
  *ppNew =
8,524✔
1546
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
8,524✔
1547
  if (*ppNew == NULL) {
8,524!
1548
    TAOS_RETURN(terrno);
×
1549
  }
1550

1551
  char *tb = taosHashIterate(pOld, NULL);
8,524✔
1552
  while (tb != NULL) {
8,524!
UNCOV
1553
    size_t keyLen = 0;
×
UNCOV
1554
    char  *key = taosHashGetKey(tb, &keyLen);
×
1555

UNCOV
1556
    int32_t valueLen = strlen(tb) + 1;
×
UNCOV
1557
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
×
1558
      taosHashCancelIterate(pOld, tb);
×
1559
      taosHashCleanup(*ppNew);
×
1560
      TAOS_RETURN(code);
×
1561
    }
UNCOV
1562
    tb = taosHashIterate(pOld, tb);
×
1563
  }
1564

1565
  TAOS_RETURN(code);
8,524✔
1566
}
1567

1568
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
9✔
1569
  int32_t code = 0;
9✔
1570
  *ppNew =
9✔
1571
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
9✔
1572
  if (*ppNew == NULL) {
9!
1573
    TAOS_RETURN(terrno);
×
1574
  }
1575

1576
  int32_t *db = taosHashIterate(pOld, NULL);
9✔
1577
  while (db != NULL) {
9!
UNCOV
1578
    size_t keyLen = 0;
×
UNCOV
1579
    char  *key = taosHashGetKey(db, &keyLen);
×
1580

UNCOV
1581
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
×
1582
      taosHashCancelIterate(pOld, db);
×
1583
      taosHashCleanup(*ppNew);
×
1584
      TAOS_RETURN(code);
×
1585
    }
UNCOV
1586
    db = taosHashIterate(pOld, db);
×
1587
  }
1588

1589
  TAOS_RETURN(code);
9✔
1590
}
1591

1592
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
9✔
1593
  int32_t code = 0;
9✔
1594
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
9✔
1595
  pNew->authVersion++;
9✔
1596
  pNew->updateTime = taosGetTimestampMs();
9✔
1597

1598
  taosRLockLatch(&pUser->lock);
9✔
1599
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->readDbs, &pNew->readDbs), NULL, _OVER);
9!
1600
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->writeDbs, &pNew->writeDbs), NULL, _OVER);
9!
1601
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readTbs, &pNew->readTbs), NULL, _OVER);
9!
1602
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeTbs, &pNew->writeTbs), NULL, _OVER);
9!
1603
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterTbs, &pNew->alterTbs), NULL, _OVER);
9!
1604
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readViews, &pNew->readViews), NULL, _OVER);
9!
1605
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeViews, &pNew->writeViews), NULL, _OVER);
9!
1606
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterViews, &pNew->alterViews), NULL, _OVER);
9!
1607
  TAOS_CHECK_GOTO(mndDupTopicHash(pUser->topics, &pNew->topics), NULL, _OVER);
9!
1608
  TAOS_CHECK_GOTO(mndDupUseDbHash(pUser->useDbs, &pNew->useDbs), NULL, _OVER);
9!
1609
  pNew->pIpWhiteList = cloneIpWhiteList(pUser->pIpWhiteList);
9✔
1610
  if (pNew->pIpWhiteList == NULL) {
9!
1611
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1612
  }
1613

1614
_OVER:
9✔
1615
  taosRUnLockLatch(&pUser->lock);
9✔
1616
  TAOS_RETURN(code);
9✔
1617
}
1618

1619
void mndUserFreeObj(SUserObj *pUser) {
56✔
1620
  taosHashCleanup(pUser->readDbs);
56✔
1621
  taosHashCleanup(pUser->writeDbs);
56✔
1622
  taosHashCleanup(pUser->topics);
56✔
1623
  taosHashCleanup(pUser->readTbs);
56✔
1624
  taosHashCleanup(pUser->writeTbs);
56✔
1625
  taosHashCleanup(pUser->alterTbs);
56✔
1626
  taosHashCleanup(pUser->readViews);
56✔
1627
  taosHashCleanup(pUser->writeViews);
56✔
1628
  taosHashCleanup(pUser->alterViews);
56✔
1629
  taosHashCleanup(pUser->useDbs);
56✔
1630
  taosMemoryFreeClear(pUser->pIpWhiteList);
56!
1631
  pUser->readDbs = NULL;
56✔
1632
  pUser->writeDbs = NULL;
56✔
1633
  pUser->topics = NULL;
56✔
1634
  pUser->readTbs = NULL;
56✔
1635
  pUser->writeTbs = NULL;
56✔
1636
  pUser->alterTbs = NULL;
56✔
1637
  pUser->readViews = NULL;
56✔
1638
  pUser->writeViews = NULL;
56✔
1639
  pUser->alterViews = NULL;
56✔
1640
  pUser->useDbs = NULL;
56✔
1641
}
56✔
1642

1643
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
16✔
1644
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
16✔
1645
  mndUserFreeObj(pUser);
16✔
1646
  return 0;
16✔
1647
}
1648

UNCOV
1649
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
×
UNCOV
1650
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
×
UNCOV
1651
  taosWLockLatch(&pOld->lock);
×
UNCOV
1652
  pOld->updateTime = pNew->updateTime;
×
UNCOV
1653
  pOld->authVersion = pNew->authVersion;
×
UNCOV
1654
  pOld->passVersion = pNew->passVersion;
×
UNCOV
1655
  pOld->sysInfo = pNew->sysInfo;
×
UNCOV
1656
  pOld->enable = pNew->enable;
×
UNCOV
1657
  pOld->flag = pNew->flag;
×
UNCOV
1658
  (void)memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
×
UNCOV
1659
  TSWAP(pOld->readDbs, pNew->readDbs);
×
UNCOV
1660
  TSWAP(pOld->writeDbs, pNew->writeDbs);
×
UNCOV
1661
  TSWAP(pOld->topics, pNew->topics);
×
UNCOV
1662
  TSWAP(pOld->readTbs, pNew->readTbs);
×
UNCOV
1663
  TSWAP(pOld->writeTbs, pNew->writeTbs);
×
UNCOV
1664
  TSWAP(pOld->alterTbs, pNew->alterTbs);
×
UNCOV
1665
  TSWAP(pOld->readViews, pNew->readViews);
×
UNCOV
1666
  TSWAP(pOld->writeViews, pNew->writeViews);
×
UNCOV
1667
  TSWAP(pOld->alterViews, pNew->alterViews);
×
UNCOV
1668
  TSWAP(pOld->useDbs, pNew->useDbs);
×
1669

UNCOV
1670
  int32_t sz = sizeof(SIpWhiteList) + pNew->pIpWhiteList->num * sizeof(SIpV4Range);
×
UNCOV
1671
  TAOS_MEMORY_REALLOC(pOld->pIpWhiteList, sz);
×
UNCOV
1672
  if (pOld->pIpWhiteList == NULL) {
×
1673
    taosWUnLockLatch(&pOld->lock);
×
1674
    return terrno;
×
1675
  }
UNCOV
1676
  (void)memcpy(pOld->pIpWhiteList, pNew->pIpWhiteList, sz);
×
UNCOV
1677
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
×
1678

UNCOV
1679
  taosWUnLockLatch(&pOld->lock);
×
1680

UNCOV
1681
  return 0;
×
1682
}
1683

1684
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
24,234✔
1685
  int32_t code = 0;
24,234✔
1686
  SSdb   *pSdb = pMnode->pSdb;
24,234✔
1687

1688
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
24,234✔
1689
  if (*ppUser == NULL) {
24,234✔
1690
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
3!
1691
      code = TSDB_CODE_MND_USER_NOT_EXIST;
3✔
1692
    } else {
1693
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
1694
    }
1695
  }
1696
  TAOS_RETURN(code);
24,234✔
1697
}
1698

1699
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
24,234✔
1700
  SSdb *pSdb = pMnode->pSdb;
24,234✔
1701
  sdbRelease(pSdb, pUser);
24,234✔
1702
}
24,234✔
1703

1704
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
3✔
1705
  int32_t  code = 0;
3✔
1706
  int32_t  lino = 0;
3✔
1707
  SUserObj userObj = {0};
3✔
1708
  if (pCreate->isImport != 1) {
3!
1709
    taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
3✔
1710
  } else {
1711
    // mInfo("pCreate->pass:%s", pCreate->eass)
1712
    memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
1713
  }
1714
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
3✔
1715
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
3✔
1716
  userObj.createdTime = taosGetTimestampMs();
3✔
1717
  userObj.updateTime = userObj.createdTime;
3✔
1718
  userObj.superUser = 0;  // pCreate->superUser;
3✔
1719
  userObj.sysInfo = pCreate->sysInfo;
3✔
1720
  userObj.enable = pCreate->enable;
3✔
1721
  userObj.createdb = pCreate->createDb;
3✔
1722

1723
  if (pCreate->numIpRanges == 0) {
3!
1724
    TAOS_CHECK_RETURN(createDefaultIpWhiteList(&userObj.pIpWhiteList));
3!
1725
  } else {
UNCOV
1726
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
×
UNCOV
1727
    if (pUniqueTab == NULL) {
×
1728
      TAOS_RETURN(terrno);
×
1729
    }
UNCOV
1730
    int32_t dummpy = 0;
×
UNCOV
1731
    for (int i = 0; i < pCreate->numIpRanges; i++) {
×
UNCOV
1732
      SIpV4Range range = {.ip = pCreate->pIpRanges[i].ip, .mask = pCreate->pIpRanges[i].mask};
×
UNCOV
1733
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
×
1734
        taosHashCleanup(pUniqueTab);
×
1735
        TAOS_RETURN(code);
×
1736
      }
1737
    }
UNCOV
1738
    if ((code = taosHashPut(pUniqueTab, &defaultIpRange, sizeof(defaultIpRange), &dummpy, sizeof(dummpy))) != 0) {
×
1739
      taosHashCleanup(pUniqueTab);
×
1740
      TAOS_RETURN(code);
×
1741
    }
1742

UNCOV
1743
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USE_HOST) {
×
1744
      taosHashCleanup(pUniqueTab);
×
1745
      TAOS_RETURN(TSDB_CODE_MND_TOO_MANY_USER_HOST);
×
1746
    }
1747

UNCOV
1748
    int32_t       numOfRanges = taosHashGetSize(pUniqueTab);
×
UNCOV
1749
    SIpWhiteList *p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + numOfRanges * sizeof(SIpV4Range));
×
UNCOV
1750
    if (p == NULL) {
×
1751
      taosHashCleanup(pUniqueTab);
×
1752
      TAOS_RETURN(terrno);
×
1753
    }
UNCOV
1754
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
×
UNCOV
1755
    int32_t i = 0;
×
UNCOV
1756
    while (pIter) {
×
UNCOV
1757
      size_t      len = 0;
×
UNCOV
1758
      SIpV4Range *key = taosHashGetKey(pIter, &len);
×
UNCOV
1759
      p->pIpRange[i].ip = key->ip;
×
UNCOV
1760
      p->pIpRange[i].mask = key->mask;
×
UNCOV
1761
      pIter = taosHashIterate(pUniqueTab, pIter);
×
1762

UNCOV
1763
      i++;
×
1764
    }
1765

UNCOV
1766
    taosHashCleanup(pUniqueTab);
×
UNCOV
1767
    p->num = numOfRanges;
×
UNCOV
1768
    userObj.pIpWhiteList = p;
×
1769
  }
1770

1771
  userObj.ipWhiteListVer = taosGetTimestampMs();
3✔
1772

1773
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
3✔
1774
  if (pTrans == NULL) {
3!
1775
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
1776
    taosMemoryFree(userObj.pIpWhiteList);
×
1777
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1778
  }
1779
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
3!
1780

1781
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
3✔
1782
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
3!
1783
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1784
    mndTransDrop(pTrans);
×
1785
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1786
  }
1787
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
3!
1788

1789
  if (mndTransPrepare(pMnode, pTrans) != 0) {
3!
1790
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1791
    mndTransDrop(pTrans);
×
1792
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1793
  }
1794
  if ((code = ipWhiteMgtUpdate(pMnode, userObj.user, userObj.pIpWhiteList)) != 0) {
3!
1795
    mndTransDrop(pTrans);
×
1796
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1797
  }
1798

1799
  taosMemoryFree(userObj.pIpWhiteList);
3!
1800
  mndTransDrop(pTrans);
3✔
1801
  return 0;
3✔
1802
_OVER:
×
1803
  taosMemoryFree(userObj.pIpWhiteList);
×
1804

1805
  TAOS_RETURN(code);
×
1806
}
1807

1808
static int32_t mndCheckPasswordFmt(const char *pwd) {
3✔
1809
  int32_t len = strlen(pwd);
3✔
1810
  if (len < TSDB_PASSWORD_MIN_LEN || len > TSDB_PASSWORD_MAX_LEN) {
3!
UNCOV
1811
    return -1;
×
1812
  }
1813

1814
  if (strcmp(pwd, "taosdata") == 0) {
3!
1815
    return 0;
3✔
1816
  }
1817

UNCOV
1818
  bool charTypes[4] = {0};
×
UNCOV
1819
  for (int32_t i = 0; i < len; ++i) {
×
UNCOV
1820
    if (taosIsBigChar(pwd[i])) {
×
UNCOV
1821
      charTypes[0] = true;
×
UNCOV
1822
    } else if (taosIsSmallChar(pwd[i])) {
×
UNCOV
1823
      charTypes[1] = true;
×
UNCOV
1824
    } else if (taosIsNumberChar(pwd[i])) {
×
UNCOV
1825
      charTypes[2] = true;
×
UNCOV
1826
    } else if (taosIsSpecialChar(pwd[i])) {
×
UNCOV
1827
      charTypes[3] = true;
×
1828
    } else {
UNCOV
1829
      return -1;
×
1830
    }
1831
  }
1832

UNCOV
1833
  int32_t numOfTypes = 0;
×
UNCOV
1834
  for (int32_t i = 0; i < 4; ++i) {
×
UNCOV
1835
    numOfTypes += charTypes[i];
×
1836
  }
1837

UNCOV
1838
  if (numOfTypes < 3) {
×
UNCOV
1839
    return -1;
×
1840
  }
1841

UNCOV
1842
  return 0;
×
1843
}
1844

1845
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
3✔
1846
  SMnode        *pMnode = pReq->info.node;
3✔
1847
  int32_t        code = 0;
3✔
1848
  int32_t        lino = 0;
3✔
1849
  SUserObj      *pUser = NULL;
3✔
1850
  SUserObj      *pOperUser = NULL;
3✔
1851
  SCreateUserReq createReq = {0};
3✔
1852

1853
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
3!
1854
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
1855
  }
1856

1857
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.isImport, createReq.createDb);
3!
1858

1859
#ifndef TD_ENTERPRISE
1860
  if (createReq.isImport == 1) {
1861
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
1862
  }
1863
#endif
1864

1865
  if (createReq.isImport != 1) {
3!
1866
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
3!
1867
  } else {
1868
    if (strcmp(pReq->info.conn.user, "root") != 0) {
×
1869
      mError("The operation is not permitted, user:%s", pReq->info.conn.user);
×
1870
      TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
1871
    }
1872
  }
1873

1874
  if (createReq.user[0] == 0) {
3!
1875
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
1876
  }
1877

1878
  if (mndCheckPasswordFmt(createReq.pass) != 0) {
3!
UNCOV
1879
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
1880
  }
1881

1882
  if (createReq.isImport != 1) {
3!
1883
    if (strlen(createReq.pass) >= TSDB_PASSWORD_LEN) {
3!
1884
      TAOS_CHECK_GOTO(TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG, &lino, _OVER);
×
1885
    }
1886
  }
1887

1888
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
3✔
1889
  if (pUser != NULL) {
3!
1890
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
×
1891
  }
1892

1893
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
3✔
1894
  if (pOperUser == NULL) {
3!
1895
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
1896
  }
1897

1898
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
3!
1899

1900
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
3✔
1901
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
3!
1902

1903
  char detail[1000] = {0};
3✔
1904
  (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
3✔
1905
            createReq.superUser, createReq.sysInfo);
3✔
1906
  char operation[15] = {0};
3✔
1907
  if (createReq.isImport == 1) {
3!
1908
    tstrncpy(operation, "importUser", sizeof(operation));
×
1909
  } else {
1910
    tstrncpy(operation, "createUser", sizeof(operation));
3✔
1911
  }
1912

1913
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
3✔
1914

1915
_OVER:
3✔
1916
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
3!
UNCOV
1917
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
×
1918
  }
1919

1920
  mndReleaseUser(pMnode, pUser);
3✔
1921
  mndReleaseUser(pMnode, pOperUser);
3✔
1922
  tFreeSCreateUserReq(&createReq);
3✔
1923

1924
  TAOS_RETURN(code);
3✔
1925
}
1926

UNCOV
1927
int32_t mndProcessGetUserWhiteListReq(SRpcMsg *pReq) {
×
UNCOV
1928
  SMnode              *pMnode = pReq->info.node;
×
UNCOV
1929
  int32_t              code = 0;
×
UNCOV
1930
  int32_t              lino = 0;
×
UNCOV
1931
  int32_t              contLen = 0;
×
UNCOV
1932
  void                *pRsp = NULL;
×
UNCOV
1933
  SUserObj            *pUser = NULL;
×
UNCOV
1934
  SGetUserWhiteListReq wlReq = {0};
×
UNCOV
1935
  SGetUserWhiteListRsp wlRsp = {0};
×
1936

UNCOV
1937
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
×
1938
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
1939
  }
UNCOV
1940
  mTrace("user: %s, start to get whitelist", wlReq.user);
×
1941

UNCOV
1942
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
×
UNCOV
1943
  if (pUser == NULL) {
×
1944
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
1945
  }
1946

UNCOV
1947
  TAOS_CHECK_GOTO(mndSetUserWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
×
1948

UNCOV
1949
  contLen = tSerializeSGetUserWhiteListRsp(NULL, 0, &wlRsp);
×
UNCOV
1950
  if (contLen < 0) {
×
1951
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1952
  }
UNCOV
1953
  pRsp = rpcMallocCont(contLen);
×
UNCOV
1954
  if (pRsp == NULL) {
×
1955
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1956
  }
1957

UNCOV
1958
  contLen = tSerializeSGetUserWhiteListRsp(pRsp, contLen, &wlRsp);
×
UNCOV
1959
  if (contLen < 0) {
×
1960
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1961
  }
1962

UNCOV
1963
_OVER:
×
UNCOV
1964
  mndReleaseUser(pMnode, pUser);
×
UNCOV
1965
  tFreeSGetUserWhiteListRsp(&wlRsp);
×
UNCOV
1966
  if (code < 0) {
×
1967
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
1968
    rpcFreeCont(pRsp);
×
1969
    pRsp = NULL;
×
1970
    contLen = 0;
×
1971
  }
UNCOV
1972
  pReq->code = code;
×
UNCOV
1973
  pReq->info.rsp = pRsp;
×
UNCOV
1974
  pReq->info.rspLen = contLen;
×
1975

UNCOV
1976
  TAOS_RETURN(code);
×
1977
}
1978

UNCOV
1979
int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
×
UNCOV
1980
  int32_t        code = 0;
×
UNCOV
1981
  int32_t        lino = 0;
×
UNCOV
1982
  int32_t        len = 0;
×
UNCOV
1983
  void          *pRsp = NULL;
×
UNCOV
1984
  SUpdateIpWhite ipWhite = {0};
×
1985

1986
  // impl later
UNCOV
1987
  SRetrieveIpWhiteReq req = {0};
×
UNCOV
1988
  if (tDeserializeRetrieveIpWhite(pReq->pCont, pReq->contLen, &req) != 0) {
×
1989
    code = TSDB_CODE_INVALID_MSG;
×
1990
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1991
  }
1992

UNCOV
1993
  TAOS_CHECK_GOTO(ipWhiteMgtFillMsg(&ipWhite), &lino, _OVER);
×
1994

UNCOV
1995
  len = tSerializeSUpdateIpWhite(NULL, 0, &ipWhite);
×
UNCOV
1996
  if (len < 0) {
×
1997
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
1998
  }
1999

UNCOV
2000
  pRsp = rpcMallocCont(len);
×
UNCOV
2001
  if (!pRsp) {
×
2002
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2003
  }
UNCOV
2004
  len = tSerializeSUpdateIpWhite(pRsp, len, &ipWhite);
×
UNCOV
2005
  if (len < 0) {
×
2006
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2007
  }
2008

UNCOV
2009
_OVER:
×
UNCOV
2010
  if (code < 0) {
×
2011
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2012
    rpcFreeCont(pRsp);
×
2013
    pRsp = NULL;
×
2014
    len = 0;
×
2015
  }
UNCOV
2016
  pReq->code = code;
×
UNCOV
2017
  pReq->info.rsp = pRsp;
×
UNCOV
2018
  pReq->info.rspLen = len;
×
2019

UNCOV
2020
  tFreeSUpdateIpWhiteReq(&ipWhite);
×
UNCOV
2021
  TAOS_RETURN(code);
×
2022
}
2023

UNCOV
2024
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
×
UNCOV
2025
  int32_t code = 0;
×
UNCOV
2026
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
×
UNCOV
2027
  if (pTrans == NULL) {
×
2028
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
×
2029
    TAOS_RETURN(terrno);
×
2030
  }
UNCOV
2031
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
×
2032

UNCOV
2033
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
×
UNCOV
2034
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
2035
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2036
    mndTransDrop(pTrans);
×
2037
    TAOS_RETURN(terrno);
×
2038
  }
UNCOV
2039
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
UNCOV
2040
  if (code < 0) {
×
2041
    mndTransDrop(pTrans);
×
2042
    TAOS_RETURN(code);
×
2043
  }
2044

UNCOV
2045
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
2046
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2047
    mndTransDrop(pTrans);
×
2048
    TAOS_RETURN(terrno);
×
2049
  }
UNCOV
2050
  if ((code = ipWhiteMgtUpdate(pMnode, pNew->user, pNew->pIpWhiteList)) != 0) {
×
2051
    mndTransDrop(pTrans);
×
2052
    TAOS_RETURN(code);
×
2053
  }
UNCOV
2054
  mndTransDrop(pTrans);
×
UNCOV
2055
  return 0;
×
2056
}
2057

2058
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
2,447✔
2059
  int32_t code = 0;
2,447✔
2060

2061
  *ppNew =
2,447✔
2062
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,447✔
2063
  if (*ppNew == NULL) {
2,447!
2064
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2065
    TAOS_RETURN(code);
×
2066
  }
2067

2068
  char *db = taosHashIterate(pOld, NULL);
2,447✔
2069
  while (db != NULL) {
2,447!
UNCOV
2070
    int32_t len = strlen(db) + 1;
×
UNCOV
2071
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
×
2072
      taosHashCancelIterate(pOld, db);
×
2073
      taosHashCleanup(*ppNew);
×
2074
      TAOS_RETURN(code);
×
2075
    }
UNCOV
2076
    db = taosHashIterate(pOld, db);
×
2077
  }
2078

2079
  TAOS_RETURN(code);
2,447✔
2080
}
2081

2082
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
2,438✔
2083

2084
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
9✔
2085

UNCOV
2086
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
2087
                                  SSdb *pSdb) {
UNCOV
2088
  void *pIter = NULL;
×
UNCOV
2089
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
2090

UNCOV
2091
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
×
UNCOV
2092
  int32_t len = strlen(tbFName) + 1;
×
2093

UNCOV
2094
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
×
UNCOV
2095
    char *value = taosHashGet(hash, tbFName, len);
×
UNCOV
2096
    if (value != NULL) {
×
2097
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2098
    }
2099

UNCOV
2100
    int32_t condLen = alterReq->tagCondLen;
×
UNCOV
2101
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
×
2102
  } else {
UNCOV
2103
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
×
2104
  }
2105

UNCOV
2106
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
UNCOV
2107
  int32_t  ref = 1;
×
UNCOV
2108
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
UNCOV
2109
  if (NULL != currRef) {
×
UNCOV
2110
    ref = (*currRef) + 1;
×
2111
  }
UNCOV
2112
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
2113

UNCOV
2114
  TAOS_RETURN(0);
×
2115
}
2116

UNCOV
2117
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
×
2118
                                        SSdb *pSdb) {
UNCOV
2119
  void *pIter = NULL;
×
UNCOV
2120
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
×
UNCOV
2121
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
×
UNCOV
2122
  int32_t len = strlen(tbFName) + 1;
×
2123

UNCOV
2124
  if (taosHashRemove(hash, tbFName, len) != 0) {
×
2125
    TAOS_RETURN(0);  // not found
×
2126
  }
2127

UNCOV
2128
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
×
UNCOV
2129
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
×
UNCOV
2130
  if (NULL == currRef) {
×
2131
    return 0;
×
2132
  }
2133

UNCOV
2134
  if (1 == *currRef) {
×
UNCOV
2135
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
×
2136
      TAOS_RETURN(0);  // not found
×
2137
    }
UNCOV
2138
    return 0;
×
2139
  }
2140
  int32_t ref = (*currRef) - 1;
×
2141
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
2142

2143
  return 0;
×
2144
}
2145

UNCOV
2146
static char *mndUserAuditTypeStr(int32_t type) {
×
UNCOV
2147
  if (type == TSDB_ALTER_USER_PASSWD) {
×
UNCOV
2148
    return "changePassword";
×
2149
  }
2150
  if (type == TSDB_ALTER_USER_SUPERUSER) {
×
2151
    return "changeSuperUser";
×
2152
  }
2153
  if (type == TSDB_ALTER_USER_ENABLE) {
×
2154
    return "enableUser";
×
2155
  }
2156
  if (type == TSDB_ALTER_USER_SYSINFO) {
×
2157
    return "userSysInfo";
×
2158
  }
2159
  if (type == TSDB_ALTER_USER_CREATEDB) {
×
2160
    return "userCreateDB";
×
2161
  }
2162
  return "error";
×
2163
}
2164

UNCOV
2165
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
×
UNCOV
2166
  SSdb   *pSdb = pMnode->pSdb;
×
UNCOV
2167
  void   *pIter = NULL;
×
UNCOV
2168
  int32_t code = 0;
×
UNCOV
2169
  int32_t lino = 0;
×
2170

UNCOV
2171
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2172
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2173
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
UNCOV
2174
      int32_t len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2175
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
UNCOV
2176
      if (pDb == NULL) {
×
UNCOV
2177
        mndReleaseDb(pMnode, pDb);
×
UNCOV
2178
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2179
      }
UNCOV
2180
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
×
2181
          0) {
2182
        mndReleaseDb(pMnode, pDb);
×
2183
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2184
      }
UNCOV
2185
      mndReleaseDb(pMnode, pDb);
×
2186
    } else {
UNCOV
2187
      while (1) {
×
UNCOV
2188
        SDbObj *pDb = NULL;
×
UNCOV
2189
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
×
UNCOV
2190
        if (pIter == NULL) break;
×
UNCOV
2191
        int32_t len = strlen(pDb->name) + 1;
×
UNCOV
2192
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
×
2193
          sdbRelease(pSdb, pDb);
×
2194
          sdbCancelFetch(pSdb, pIter);
×
2195
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2196
        }
UNCOV
2197
        sdbRelease(pSdb, pDb);
×
2198
      }
2199
    }
2200
  }
2201

UNCOV
2202
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2203
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2204
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
UNCOV
2205
      int32_t len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2206
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
UNCOV
2207
      if (pDb == NULL) {
×
UNCOV
2208
        mndReleaseDb(pMnode, pDb);
×
UNCOV
2209
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2210
      }
UNCOV
2211
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
×
2212
          0) {
2213
        mndReleaseDb(pMnode, pDb);
×
2214
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2215
      }
UNCOV
2216
      mndReleaseDb(pMnode, pDb);
×
2217
    } else {
UNCOV
2218
      while (1) {
×
UNCOV
2219
        SDbObj *pDb = NULL;
×
UNCOV
2220
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
×
UNCOV
2221
        if (pIter == NULL) break;
×
UNCOV
2222
        int32_t len = strlen(pDb->name) + 1;
×
UNCOV
2223
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
×
2224
          sdbRelease(pSdb, pDb);
×
2225
          sdbCancelFetch(pSdb, pIter);
×
2226
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2227
        }
UNCOV
2228
        sdbRelease(pSdb, pDb);
×
2229
      }
2230
    }
2231
  }
2232

UNCOV
2233
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2234
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2235
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
UNCOV
2236
      int32_t len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2237
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
UNCOV
2238
      if (pDb == NULL) {
×
2239
        mndReleaseDb(pMnode, pDb);
×
2240
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2241
      }
UNCOV
2242
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
×
UNCOV
2243
      if (code < 0) {
×
2244
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2245
      }
UNCOV
2246
      mndReleaseDb(pMnode, pDb);
×
2247
    } else {
UNCOV
2248
      taosHashClear(pNewUser->readDbs);
×
2249
    }
2250
  }
2251

UNCOV
2252
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2253
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2254
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
UNCOV
2255
      int32_t len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2256
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
UNCOV
2257
      if (pDb == NULL) {
×
2258
        mndReleaseDb(pMnode, pDb);
×
2259
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2260
      }
UNCOV
2261
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
×
UNCOV
2262
      if (code < 0) {
×
2263
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2264
      }
UNCOV
2265
      mndReleaseDb(pMnode, pDb);
×
2266
    } else {
UNCOV
2267
      taosHashClear(pNewUser->writeDbs);
×
2268
    }
2269
  }
2270

UNCOV
2271
  SHashObj *pReadTbs = pNewUser->readTbs;
×
UNCOV
2272
  SHashObj *pWriteTbs = pNewUser->writeTbs;
×
UNCOV
2273
  SHashObj *pAlterTbs = pNewUser->alterTbs;
×
2274

2275
#ifdef TD_ENTERPRISE
UNCOV
2276
  if (pAlterReq->isView) {
×
UNCOV
2277
    pReadTbs = pNewUser->readViews;
×
UNCOV
2278
    pWriteTbs = pNewUser->writeViews;
×
UNCOV
2279
    pAlterTbs = pNewUser->alterViews;
×
2280
  }
2281
#endif
2282

UNCOV
2283
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2284
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2285
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2286
  }
2287

UNCOV
2288
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2289
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2290
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2291
  }
2292

UNCOV
2293
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2294
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2295
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2296
  }
2297

UNCOV
2298
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2299
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2300
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2301
  }
2302

UNCOV
2303
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2304
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
UNCOV
2305
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2306
  }
2307

UNCOV
2308
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
UNCOV
2309
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
2310
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2311
  }
2312

UNCOV
2313
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
×
UNCOV
2314
    int32_t      len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2315
    SMqTopicObj *pTopic = NULL;
×
UNCOV
2316
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
×
2317
      mndReleaseTopic(pMnode, pTopic);
×
2318
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2319
    }
UNCOV
2320
    if ((code = taosHashPut(pNewUser->topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN)) != 0) {
×
2321
      mndReleaseTopic(pMnode, pTopic);
×
2322
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2323
    }
UNCOV
2324
    mndReleaseTopic(pMnode, pTopic);
×
2325
  }
2326

UNCOV
2327
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
×
UNCOV
2328
    int32_t      len = strlen(pAlterReq->objname) + 1;
×
UNCOV
2329
    SMqTopicObj *pTopic = NULL;
×
UNCOV
2330
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
×
UNCOV
2331
      mndReleaseTopic(pMnode, pTopic);
×
UNCOV
2332
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2333
    }
UNCOV
2334
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
×
UNCOV
2335
    if (code < 0) {
×
2336
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
2337
    }
UNCOV
2338
    mndReleaseTopic(pMnode, pTopic);
×
2339
  }
2340

UNCOV
2341
_OVER:
×
UNCOV
2342
  if (code < 0) {
×
UNCOV
2343
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
×
2344
  }
UNCOV
2345
  TAOS_RETURN(code);
×
2346
}
2347

UNCOV
2348
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
×
UNCOV
2349
  SMnode       *pMnode = pReq->info.node;
×
UNCOV
2350
  SSdb         *pSdb = pMnode->pSdb;
×
UNCOV
2351
  void         *pIter = NULL;
×
UNCOV
2352
  int32_t       code = 0;
×
UNCOV
2353
  int32_t       lino = 0;
×
UNCOV
2354
  SUserObj     *pUser = NULL;
×
UNCOV
2355
  SUserObj     *pOperUser = NULL;
×
UNCOV
2356
  SUserObj      newUser = {0};
×
UNCOV
2357
  SAlterUserReq alterReq = {0};
×
2358

UNCOV
2359
  TAOS_CHECK_GOTO(tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq), &lino, _OVER);
×
2360

UNCOV
2361
  mInfo("user:%s, start to alter", alterReq.user);
×
2362

UNCOV
2363
  if (alterReq.user[0] == 0) {
×
2364
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2365
  }
2366

UNCOV
2367
  if (TSDB_ALTER_USER_PASSWD == alterReq.alterType && mndCheckPasswordFmt(alterReq.pass) != 0) {
×
UNCOV
2368
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
2369
  }
2370

UNCOV
2371
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
×
2372

UNCOV
2373
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
×
UNCOV
2374
  if (pOperUser == NULL) {
×
2375
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2376
  }
2377

UNCOV
2378
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
×
2379

UNCOV
2380
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
×
2381

UNCOV
2382
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
×
UNCOV
2383
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
×
UNCOV
2384
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
×
UNCOV
2385
    (void)memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
×
UNCOV
2386
    if (0 != strncmp(pUser->pass, pass, TSDB_PASSWORD_LEN)) {
×
UNCOV
2387
      ++newUser.passVersion;
×
2388
    }
2389
  }
2390

UNCOV
2391
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
×
2392
    newUser.superUser = alterReq.superUser;
×
2393
  }
2394

UNCOV
2395
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
×
UNCOV
2396
    newUser.enable = alterReq.enable;
×
2397
  }
2398

UNCOV
2399
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
×
UNCOV
2400
    newUser.sysInfo = alterReq.sysInfo;
×
2401
  }
2402

UNCOV
2403
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
×
UNCOV
2404
    newUser.createdb = alterReq.createdb;
×
2405
  }
2406

UNCOV
2407
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
×
UNCOV
2408
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
×
2409
  }
2410

UNCOV
2411
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WHITE_LIST) {
×
UNCOV
2412
    taosMemoryFreeClear(newUser.pIpWhiteList);
×
2413

UNCOV
2414
    int32_t       num = pUser->pIpWhiteList->num + alterReq.numIpRanges;
×
UNCOV
2415
    int32_t       idx = pUser->pIpWhiteList->num;
×
UNCOV
2416
    SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
×
2417

UNCOV
2418
    if (pNew == NULL) {
×
2419
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2420
    }
2421

UNCOV
2422
    bool exist = false;
×
UNCOV
2423
    (void)memcpy(pNew->pIpRange, pUser->pIpWhiteList->pIpRange, sizeof(SIpV4Range) * idx);
×
UNCOV
2424
    for (int i = 0; i < alterReq.numIpRanges; i++) {
×
UNCOV
2425
      SIpV4Range *range = &(alterReq.pIpRanges[i]);
×
UNCOV
2426
      if (!isRangeInIpWhiteList(pUser->pIpWhiteList, range)) {
×
2427
        // already exist, just ignore;
UNCOV
2428
        (void)memcpy(&pNew->pIpRange[idx], range, sizeof(SIpV4Range));
×
UNCOV
2429
        idx++;
×
UNCOV
2430
        continue;
×
2431
      } else {
2432
        exist = true;
×
2433
      }
2434
    }
UNCOV
2435
    if (exist) {
×
2436
      taosMemoryFree(pNew);
×
2437
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_EXIST, &lino, _OVER);
×
2438
    }
UNCOV
2439
    pNew->num = idx;
×
UNCOV
2440
    newUser.pIpWhiteList = pNew;
×
UNCOV
2441
    newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
×
2442

UNCOV
2443
    if (pNew->num > MND_MAX_USE_HOST) {
×
2444
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_HOST, &lino, _OVER);
×
2445
    }
2446
  }
UNCOV
2447
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_WHITE_LIST) {
×
UNCOV
2448
    taosMemoryFreeClear(newUser.pIpWhiteList);
×
2449

UNCOV
2450
    int32_t       num = pUser->pIpWhiteList->num;
×
UNCOV
2451
    bool          noexist = true;
×
UNCOV
2452
    bool          localHost = false;
×
UNCOV
2453
    SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
×
2454

UNCOV
2455
    if (pNew == NULL) {
×
2456
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2457
    }
2458

UNCOV
2459
    if (pUser->pIpWhiteList->num > 0) {
×
UNCOV
2460
      int idx = 0;
×
UNCOV
2461
      for (int i = 0; i < pUser->pIpWhiteList->num; i++) {
×
UNCOV
2462
        SIpV4Range *oldRange = &pUser->pIpWhiteList->pIpRange[i];
×
UNCOV
2463
        bool        found = false;
×
UNCOV
2464
        for (int j = 0; j < alterReq.numIpRanges; j++) {
×
UNCOV
2465
          SIpV4Range *range = &alterReq.pIpRanges[j];
×
UNCOV
2466
          if (isDefaultRange(range)) {
×
2467
            localHost = true;
×
2468
            break;
×
2469
          }
UNCOV
2470
          if (isIpRangeEqual(oldRange, range)) {
×
2471
            found = true;
×
2472
            break;
×
2473
          }
2474
        }
UNCOV
2475
        if (localHost) break;
×
2476

UNCOV
2477
        if (found == false) {
×
UNCOV
2478
          (void)memcpy(&pNew->pIpRange[idx], oldRange, sizeof(SIpV4Range));
×
UNCOV
2479
          idx++;
×
2480
        } else {
2481
          noexist = false;
×
2482
        }
2483
      }
UNCOV
2484
      pNew->num = idx;
×
UNCOV
2485
      newUser.pIpWhiteList = pNew;
×
UNCOV
2486
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
×
2487

2488
    } else {
2489
      pNew->num = 0;
×
2490
      newUser.pIpWhiteList = pNew;
×
2491
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
×
2492
    }
2493

UNCOV
2494
    if (localHost) {
×
2495
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
×
2496
    }
UNCOV
2497
    if (noexist) {
×
UNCOV
2498
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
×
2499
    }
2500
  }
2501

UNCOV
2502
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
×
UNCOV
2503
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2504

UNCOV
2505
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
×
UNCOV
2506
    char detail[1000] = {0};
×
UNCOV
2507
    (void)tsnprintf(detail, sizeof(detail),
×
2508
              "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx",
UNCOV
2509
              mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
×
UNCOV
2510
              alterReq.createdb ? 1 : 0, alterReq.tabName);
×
UNCOV
2511
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
×
UNCOV
2512
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
×
UNCOV
2513
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
×
UNCOV
2514
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
UNCOV
2515
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
UNCOV
2516
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
UNCOV
2517
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
UNCOV
2518
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
UNCOV
2519
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
UNCOV
2520
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
×
UNCOV
2521
    if (strcmp(alterReq.objname, "1.*") != 0) {
×
UNCOV
2522
      SName name = {0};
×
UNCOV
2523
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
×
UNCOV
2524
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
×
2525
                  alterReq.sqlLen);
2526
    } else {
UNCOV
2527
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
2528
    }
UNCOV
2529
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
×
UNCOV
2530
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
×
2531
                alterReq.sqlLen);
UNCOV
2532
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
×
UNCOV
2533
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
×
2534
                alterReq.sqlLen);
2535
  } else {
UNCOV
2536
    if (strcmp(alterReq.objname, "1.*") != 0) {
×
UNCOV
2537
      SName name = {0};
×
UNCOV
2538
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
×
UNCOV
2539
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
×
2540
                  alterReq.sqlLen);
2541
    } else {
UNCOV
2542
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
2543
    }
2544
  }
2545

UNCOV
2546
_OVER:
×
UNCOV
2547
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
UNCOV
2548
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
×
2549
  }
2550

UNCOV
2551
  tFreeSAlterUserReq(&alterReq);
×
UNCOV
2552
  mndReleaseUser(pMnode, pOperUser);
×
UNCOV
2553
  mndReleaseUser(pMnode, pUser);
×
UNCOV
2554
  mndUserFreeObj(&newUser);
×
2555

UNCOV
2556
  TAOS_RETURN(code);
×
2557
}
2558

UNCOV
2559
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
×
UNCOV
2560
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
×
UNCOV
2561
  if (pTrans == NULL) {
×
2562
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
2563
    TAOS_RETURN(terrno);
×
2564
  }
UNCOV
2565
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
×
2566

UNCOV
2567
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
×
UNCOV
2568
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
2569
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2570
    mndTransDrop(pTrans);
×
2571
    TAOS_RETURN(terrno);
×
2572
  }
UNCOV
2573
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
×
2574
    mndTransDrop(pTrans);
×
2575
    TAOS_RETURN(terrno);
×
2576
  }
2577

UNCOV
2578
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
2579
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2580
    mndTransDrop(pTrans);
×
2581
    TAOS_RETURN(terrno);
×
2582
  }
UNCOV
2583
  (void)ipWhiteMgtRemove(pUser->user);
×
2584

UNCOV
2585
  mndTransDrop(pTrans);
×
UNCOV
2586
  TAOS_RETURN(0);
×
2587
}
2588

UNCOV
2589
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
×
UNCOV
2590
  SMnode      *pMnode = pReq->info.node;
×
UNCOV
2591
  int32_t      code = 0;
×
UNCOV
2592
  int32_t      lino = 0;
×
UNCOV
2593
  SUserObj    *pUser = NULL;
×
UNCOV
2594
  SDropUserReq dropReq = {0};
×
2595

UNCOV
2596
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
×
2597

UNCOV
2598
  mInfo("user:%s, start to drop", dropReq.user);
×
UNCOV
2599
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
×
2600

UNCOV
2601
  if (dropReq.user[0] == 0) {
×
2602
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2603
  }
2604

UNCOV
2605
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
×
2606

UNCOV
2607
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
×
UNCOV
2608
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2609

UNCOV
2610
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
×
2611

UNCOV
2612
_OVER:
×
UNCOV
2613
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2614
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
×
2615
  }
2616

UNCOV
2617
  mndReleaseUser(pMnode, pUser);
×
UNCOV
2618
  tFreeSDropUserReq(&dropReq);
×
UNCOV
2619
  TAOS_RETURN(code);
×
2620
}
2621

2622
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
1,195✔
2623
  SMnode         *pMnode = pReq->info.node;
1,195✔
2624
  int32_t         code = 0;
1,195✔
2625
  int32_t         lino = 0;
1,195✔
2626
  int32_t         contLen = 0;
1,195✔
2627
  void           *pRsp = NULL;
1,195✔
2628
  SUserObj       *pUser = NULL;
1,195✔
2629
  SGetUserAuthReq authReq = {0};
1,195✔
2630
  SGetUserAuthRsp authRsp = {0};
1,195✔
2631

2632
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
1,195!
2633
  mTrace("user:%s, start to get auth", authReq.user);
1,195!
2634

2635
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
1,195!
2636

2637
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
1,195!
2638

2639
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
1,195✔
2640
  if (contLen < 0) {
1,195!
2641
    TAOS_CHECK_EXIT(contLen);
×
2642
  }
2643
  pRsp = rpcMallocCont(contLen);
1,195✔
2644
  if (pRsp == NULL) {
1,195!
2645
    TAOS_CHECK_EXIT(terrno);
×
2646
  }
2647

2648
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
1,195✔
2649
  if (contLen < 0) {
1,195!
2650
    TAOS_CHECK_EXIT(contLen);
×
2651
  }
2652

2653
_exit:
1,195✔
2654
  mndReleaseUser(pMnode, pUser);
1,195✔
2655
  tFreeSGetUserAuthRsp(&authRsp);
1,195✔
2656
  if (code < 0) {
1,195!
UNCOV
2657
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
×
UNCOV
2658
    rpcFreeCont(pRsp);
×
UNCOV
2659
    pRsp = NULL;
×
UNCOV
2660
    contLen = 0;
×
2661
  }
2662
  pReq->info.rsp = pRsp;
1,195✔
2663
  pReq->info.rspLen = contLen;
1,195✔
2664
  pReq->code = code;
1,195✔
2665

2666
  TAOS_RETURN(code);
1,195✔
2667
}
2668

UNCOV
2669
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
UNCOV
2670
  SMnode   *pMnode = pReq->info.node;
×
UNCOV
2671
  SSdb     *pSdb = pMnode->pSdb;
×
UNCOV
2672
  int32_t   code = 0;
×
UNCOV
2673
  int32_t   lino = 0;
×
UNCOV
2674
  int32_t   numOfRows = 0;
×
UNCOV
2675
  SUserObj *pUser = NULL;
×
UNCOV
2676
  int32_t   cols = 0;
×
UNCOV
2677
  int8_t    flag = 0;
×
UNCOV
2678
  char     *pWrite = NULL;
×
UNCOV
2679
  char     *buf = NULL;
×
UNCOV
2680
  char     *varstr = NULL;
×
2681

UNCOV
2682
  while (numOfRows < rows) {
×
UNCOV
2683
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
UNCOV
2684
    if (pShow->pIter == NULL) break;
×
2685

UNCOV
2686
    cols = 0;
×
UNCOV
2687
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2688
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2689
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2690
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
×
2691

UNCOV
2692
    cols++;
×
UNCOV
2693
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2694
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
×
2695

UNCOV
2696
    cols++;
×
UNCOV
2697
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2698
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
×
2699

UNCOV
2700
    cols++;
×
UNCOV
2701
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2702
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
×
2703

UNCOV
2704
    cols++;
×
UNCOV
2705
    flag = pUser->createdb ? 1 : 0;
×
UNCOV
2706
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2707
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
×
2708

UNCOV
2709
    cols++;
×
UNCOV
2710
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2711
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, _exit);
×
2712

UNCOV
2713
    cols++;
×
2714

UNCOV
2715
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf);
×
2716
    // int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf);
UNCOV
2717
    if (tlen != 0) {
×
UNCOV
2718
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
UNCOV
2719
      if (varstr == NULL) {
×
2720
        sdbRelease(pSdb, pUser);
×
2721
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
2722
      }
UNCOV
2723
      varDataSetLen(varstr, tlen);
×
UNCOV
2724
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
2725

UNCOV
2726
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
UNCOV
2727
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
×
2728

UNCOV
2729
      taosMemoryFreeClear(buf);
×
2730
    } else {
2731
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2732
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2733
    }
2734

UNCOV
2735
    numOfRows++;
×
UNCOV
2736
    sdbRelease(pSdb, pUser);
×
2737
  }
2738

UNCOV
2739
  pShow->numOfRows += numOfRows;
×
UNCOV
2740
_exit:
×
UNCOV
2741
  taosMemoryFreeClear(buf);
×
UNCOV
2742
  taosMemoryFreeClear(varstr);
×
UNCOV
2743
  if (code < 0) {
×
2744
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2745
    TAOS_RETURN(code);
×
2746
  }
UNCOV
2747
  return numOfRows;
×
2748
}
2749

2750
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2751
  int32_t numOfRows = 0;
×
2752
#ifdef TD_ENTERPRISE
2753
  SMnode   *pMnode = pReq->info.node;
×
2754
  SSdb     *pSdb = pMnode->pSdb;
×
2755
  SUserObj *pUser = NULL;
×
2756
  int32_t   code = 0;
×
2757
  int32_t   lino = 0;
×
2758
  int32_t   cols = 0;
×
2759
  int8_t    flag = 0;
×
2760
  char     *pWrite = NULL;
×
2761
  char     *buf = NULL;
×
2762
  char     *varstr = NULL;
×
2763

2764
  while (numOfRows < rows) {
×
2765
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
2766
    if (pShow->pIter == NULL) break;
×
2767

2768
    cols = 0;
×
2769
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2770
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
2771
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
2772
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
×
2773

2774
    cols++;
×
2775
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2776
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
×
2777

2778
    cols++;
×
2779
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2780
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
×
2781

2782
    cols++;
×
2783
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2784
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
×
2785

2786
    cols++;
×
2787
    flag = pUser->createdb ? 1 : 0;
×
2788
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2789
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
×
2790

2791
    // mInfo("pUser->pass:%s", pUser->pass);
2792
    cols++;
×
2793
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2794
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
×
2795
    STR_WITH_MAXSIZE_TO_VARSTR(pass, pUser->pass, pShow->pMeta->pSchemas[cols].bytes);
×
2796
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, _exit);
×
2797

2798
    cols++;
×
2799

2800
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf);
×
2801
    // int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf);
2802
    if (tlen != 0) {
×
2803
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
2804
      if (varstr == NULL) {
×
2805
        sdbRelease(pSdb, pUser);
×
2806
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
2807
      }
2808
      varDataSetLen(varstr, tlen);
×
2809
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
2810

2811
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2812
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
×
2813

2814
      taosMemoryFreeClear(buf);
×
2815
    } else {
2816
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2817
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2818
    }
2819

2820
    numOfRows++;
×
2821
    sdbRelease(pSdb, pUser);
×
2822
  }
2823

2824
  pShow->numOfRows += numOfRows;
×
2825
_exit:
×
2826
  taosMemoryFreeClear(buf);
×
2827
  taosMemoryFreeClear(varstr);
×
2828
  if (code < 0) {
×
2829
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2830
    TAOS_RETURN(code);
×
2831
  }
2832
#endif
2833
  return numOfRows;
×
2834
}
2835

2836
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
2837
  SSdb *pSdb = pMnode->pSdb;
×
2838
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
2839
}
×
2840

UNCOV
2841
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
×
2842
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
UNCOV
2843
  char   *value = taosHashIterate(hash, NULL);
×
UNCOV
2844
  char   *user = pUser->user;
×
UNCOV
2845
  int32_t code = 0;
×
UNCOV
2846
  int32_t lino = 0;
×
UNCOV
2847
  int32_t cols = 0;
×
UNCOV
2848
  int32_t numOfRows = *pNumOfRows;
×
2849

UNCOV
2850
  while (value != NULL) {
×
UNCOV
2851
    cols = 0;
×
UNCOV
2852
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2853
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2854
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2855
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, _exit);
×
2856

UNCOV
2857
    char privilege[20] = {0};
×
UNCOV
2858
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2859
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2860
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, _exit);
×
2861

UNCOV
2862
    size_t keyLen = 0;
×
UNCOV
2863
    void  *key = taosHashGetKey(value, &keyLen);
×
2864

UNCOV
2865
    char dbName[TSDB_DB_NAME_LEN] = {0};
×
UNCOV
2866
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
×
UNCOV
2867
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2868
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2869
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2870
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, _exit);
×
2871

UNCOV
2872
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
×
UNCOV
2873
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
×
UNCOV
2874
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2875
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2876
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2877
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, _exit);
×
2878

UNCOV
2879
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
×
2880
      SNode  *pAst = NULL;
×
2881
      int32_t sqlLen = 0;
×
2882
      size_t  bufSz = strlen(value) + 1;
×
2883
      if (bufSz < 6) bufSz = 6;
×
2884
      TAOS_MEMORY_REALLOC(*sql, bufSz);
×
2885
      if (*sql == NULL) {
×
2886
        code = terrno;
×
2887
        goto _exit;
×
2888
      }
2889
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
2890
      if ((*condition) == NULL) {
×
2891
        code = terrno;
×
2892
        goto _exit;
×
2893
      }
2894

2895
      if (nodesStringToNode(value, &pAst) == 0) {
×
2896
        if (nodesNodeToSQL(pAst, *sql, bufSz, &sqlLen) != 0) {
×
2897
          sqlLen = 5;
×
2898
          (void)tsnprintf(*sql, bufSz, "error");
×
2899
        }
2900
        nodesDestroyNode(pAst);
×
2901
      } else {
2902
        sqlLen = 5;
×
2903
        (void)tsnprintf(*sql, bufSz, "error");
×
2904
      }
2905

2906
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), (*sql), pShow->pMeta->pSchemas[cols].bytes);
×
2907

2908
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2909
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
×
2910

2911
      char notes[2] = {0};
×
2912
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
2913
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2914
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
×
2915
    } else {
UNCOV
2916
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
UNCOV
2917
      if ((*condition) == NULL) {
×
2918
        code = terrno;
×
2919
        goto _exit;
×
2920
      }
UNCOV
2921
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2922
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2923
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
×
2924

UNCOV
2925
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2926
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
×
UNCOV
2927
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2928
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
×
2929
    }
2930

UNCOV
2931
    numOfRows++;
×
UNCOV
2932
    value = taosHashIterate(hash, value);
×
2933
  }
UNCOV
2934
  *pNumOfRows = numOfRows;
×
UNCOV
2935
_exit:
×
UNCOV
2936
  if (code < 0) {
×
2937
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2938
    sdbRelease(pSdb, pUser);
×
2939
  }
UNCOV
2940
  TAOS_RETURN(code);
×
2941
}
2942

UNCOV
2943
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
UNCOV
2944
  int32_t   code = 0;
×
UNCOV
2945
  int32_t   lino = 0;
×
UNCOV
2946
  SMnode   *pMnode = pReq->info.node;
×
UNCOV
2947
  SSdb     *pSdb = pMnode->pSdb;
×
UNCOV
2948
  int32_t   numOfRows = 0;
×
UNCOV
2949
  SUserObj *pUser = NULL;
×
UNCOV
2950
  int32_t   cols = 0;
×
UNCOV
2951
  char     *pWrite = NULL;
×
UNCOV
2952
  char     *condition = NULL;
×
UNCOV
2953
  char     *sql = NULL;
×
2954

UNCOV
2955
  bool fetchNextUser = pShow->restore ? false : true;
×
UNCOV
2956
  pShow->restore = false;
×
2957

UNCOV
2958
  while (numOfRows < rows) {
×
UNCOV
2959
    if (fetchNextUser) {
×
UNCOV
2960
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
UNCOV
2961
      if (pShow->pIter == NULL) break;
×
2962
    } else {
2963
      fetchNextUser = true;
×
2964
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
2965
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
2966
      if (!pUser) {
×
2967
        continue;
×
2968
      }
2969
    }
2970

UNCOV
2971
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
×
UNCOV
2972
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
×
UNCOV
2973
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
×
UNCOV
2974
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
×
UNCOV
2975
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
×
UNCOV
2976
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
×
UNCOV
2977
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
×
UNCOV
2978
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
×
UNCOV
2979
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
×
UNCOV
2980
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
×
UNCOV
2981
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
×
2982
        rows) {
2983
      mInfo(
×
2984
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
2985
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
2986
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
2987
          numOfReadViews, numOfWriteViews, numOfAlterViews);
2988
      pShow->restore = true;
×
2989
      sdbRelease(pSdb, pUser);
×
2990
      break;
×
2991
    }
2992

UNCOV
2993
    if (pUser->superUser) {
×
UNCOV
2994
      cols = 0;
×
UNCOV
2995
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2996
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2997
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2998
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
×
2999

UNCOV
3000
      char privilege[20] = {0};
×
UNCOV
3001
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3002
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3003
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
×
3004

UNCOV
3005
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3006
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3007
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3008
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
×
3009

UNCOV
3010
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3011
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3012
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3013
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
×
3014

UNCOV
3015
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
UNCOV
3016
      if (condition == NULL) {
×
3017
        sdbRelease(pSdb, pUser);
×
3018
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3019
      }
UNCOV
3020
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3021
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3022
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
×
3023

UNCOV
3024
      char notes[2] = {0};
×
UNCOV
3025
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
UNCOV
3026
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3027
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
×
3028

UNCOV
3029
      numOfRows++;
×
3030
    }
3031

UNCOV
3032
    char *db = taosHashIterate(pUser->readDbs, NULL);
×
UNCOV
3033
    while (db != NULL) {
×
UNCOV
3034
      cols = 0;
×
UNCOV
3035
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3036
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3037
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3038
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
×
3039

UNCOV
3040
      char privilege[20] = {0};
×
UNCOV
3041
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3042
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3043
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
×
3044

UNCOV
3045
      SName name = {0};
×
UNCOV
3046
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3047
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
×
UNCOV
3048
      if (code < 0) {
×
3049
        sdbRelease(pSdb, pUser);
×
3050
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3051
      }
UNCOV
3052
      (void)tNameGetDbName(&name, varDataVal(objName));
×
UNCOV
3053
      varDataSetLen(objName, strlen(varDataVal(objName)));
×
UNCOV
3054
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3055
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
×
3056

UNCOV
3057
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3058
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3059
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3060
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
×
3061

UNCOV
3062
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
UNCOV
3063
      if (condition == NULL) {
×
3064
        sdbRelease(pSdb, pUser);
×
3065
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3066
      }
UNCOV
3067
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3068
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3069
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
×
3070

UNCOV
3071
      char notes[2] = {0};
×
UNCOV
3072
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
UNCOV
3073
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3074
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
×
3075

UNCOV
3076
      numOfRows++;
×
UNCOV
3077
      db = taosHashIterate(pUser->readDbs, db);
×
3078
    }
3079

UNCOV
3080
    db = taosHashIterate(pUser->writeDbs, NULL);
×
UNCOV
3081
    while (db != NULL) {
×
UNCOV
3082
      cols = 0;
×
UNCOV
3083
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3084
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3085
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3086
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
×
3087

UNCOV
3088
      char privilege[20] = {0};
×
UNCOV
3089
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3090
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3091
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
×
3092

UNCOV
3093
      SName name = {0};
×
UNCOV
3094
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3095
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
×
UNCOV
3096
      if (code < 0) {
×
3097
        sdbRelease(pSdb, pUser);
×
3098
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3099
      }
UNCOV
3100
      (void)tNameGetDbName(&name, varDataVal(objName));
×
UNCOV
3101
      varDataSetLen(objName, strlen(varDataVal(objName)));
×
UNCOV
3102
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3103
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
×
3104

UNCOV
3105
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3106
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3107
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3108
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
×
3109

UNCOV
3110
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
UNCOV
3111
      if (condition == NULL) {
×
3112
        sdbRelease(pSdb, pUser);
×
3113
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3114
      }
UNCOV
3115
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3116
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3117
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
×
3118

UNCOV
3119
      char notes[2] = {0};
×
UNCOV
3120
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
UNCOV
3121
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3122
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
×
3123

UNCOV
3124
      numOfRows++;
×
UNCOV
3125
      db = taosHashIterate(pUser->writeDbs, db);
×
3126
    }
3127

UNCOV
3128
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3129

UNCOV
3130
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3131

UNCOV
3132
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3133

UNCOV
3134
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3135

UNCOV
3136
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3137

UNCOV
3138
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
×
3139

UNCOV
3140
    char *topic = taosHashIterate(pUser->topics, NULL);
×
UNCOV
3141
    while (topic != NULL) {
×
UNCOV
3142
      cols = 0;
×
UNCOV
3143
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3144
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3145
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3146
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
×
3147

UNCOV
3148
      char privilege[20] = {0};
×
UNCOV
3149
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3150
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3151
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
×
3152

UNCOV
3153
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
×
UNCOV
3154
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
×
UNCOV
3155
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
×
UNCOV
3156
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3157
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, _exit);
×
3158

UNCOV
3159
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
3160
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3161
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3162
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
×
3163

UNCOV
3164
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
UNCOV
3165
      if (condition == NULL) {
×
3166
        sdbRelease(pSdb, pUser);
×
3167
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3168
      }
UNCOV
3169
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
3170
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3171
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
×
3172

UNCOV
3173
      char notes[2] = {0};
×
UNCOV
3174
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
UNCOV
3175
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
3176
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
×
3177

UNCOV
3178
      numOfRows++;
×
UNCOV
3179
      topic = taosHashIterate(pUser->topics, topic);
×
3180
    }
3181

UNCOV
3182
    sdbRelease(pSdb, pUser);
×
3183
  }
3184

UNCOV
3185
  pShow->numOfRows += numOfRows;
×
UNCOV
3186
_exit:
×
UNCOV
3187
  taosMemoryFreeClear(condition);
×
UNCOV
3188
  taosMemoryFreeClear(sql);
×
UNCOV
3189
  if (code < 0) {
×
3190
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3191
    TAOS_RETURN(code);
×
3192
  }
UNCOV
3193
  return numOfRows;
×
3194
}
3195

3196
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
3197
  SSdb *pSdb = pMnode->pSdb;
×
3198
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
3199
}
×
3200

3201
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
1,065✔
3202
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
3203
  int32_t           code = 0;
1,065✔
3204
  int32_t           lino = 0;
1,065✔
3205
  int32_t           rspLen = 0;
1,065✔
3206
  void             *pRsp = NULL;
1,065✔
3207
  SUserAuthBatchRsp batchRsp = {0};
1,065✔
3208

3209
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
1,065✔
3210
  if (batchRsp.pArray == NULL) {
1,065!
3211
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3212
  }
3213

3214
  for (int32_t i = 0; i < numOfUses; ++i) {
2,130✔
3215
    SUserObj *pUser = NULL;
1,065✔
3216
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
1,065✔
3217
    if (pUser == NULL) {
1,065!
UNCOV
3218
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
×
UNCOV
3219
        SGetUserAuthRsp rsp = {.dropped = 1};
×
UNCOV
3220
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
×
UNCOV
3221
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
×
3222
      }
UNCOV
3223
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
×
UNCOV
3224
      code = 0;
×
3225
      continue;
1,050✔
3226
    }
3227

3228
    pUsers[i].version = ntohl(pUsers[i].version);
1,065✔
3229
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
1,065!
3230
      mndReleaseUser(pMnode, pUser);
1,050✔
3231
      continue;
1,050✔
3232
    }
3233

3234
    SGetUserAuthRsp rsp = {0};
15✔
3235
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
15✔
3236
    if (code) {
15!
3237
      mndReleaseUser(pMnode, pUser);
×
3238
      tFreeSGetUserAuthRsp(&rsp);
×
3239
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3240
    }
3241

3242
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
30!
3243
      code = terrno;
×
3244
      mndReleaseUser(pMnode, pUser);
×
3245
      tFreeSGetUserAuthRsp(&rsp);
×
3246
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3247
    }
3248
    mndReleaseUser(pMnode, pUser);
15✔
3249
  }
3250

3251
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
1,065✔
3252
    *ppRsp = NULL;
1,050✔
3253
    *pRspLen = 0;
1,050✔
3254

3255
    tFreeSUserAuthBatchRsp(&batchRsp);
1,050✔
3256
    return 0;
1,050✔
3257
  }
3258

3259
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
15✔
3260
  if (rspLen < 0) {
15!
3261
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3262
  }
3263
  pRsp = taosMemoryMalloc(rspLen);
15!
3264
  if (pRsp == NULL) {
15!
3265
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3266
  }
3267
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
15✔
3268
  if (rspLen < 0) {
15!
3269
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3270
  }
3271
_OVER:
15✔
3272
  tFreeSUserAuthBatchRsp(&batchRsp);
15✔
3273
  if (code < 0) {
15!
3274
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3275
    taosMemoryFreeClear(pRsp);
×
3276
    rspLen = 0;
×
3277
  }
3278
  *ppRsp = pRsp;
15✔
3279
  *pRspLen = rspLen;
15✔
3280

3281
  TAOS_RETURN(code);
15✔
3282
}
3283

3284
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
8✔
3285
  int32_t   code = 0;
8✔
3286
  int32_t   lino = 0;
8✔
3287
  SSdb     *pSdb = pMnode->pSdb;
8✔
3288
  int32_t   len = strlen(db) + 1;
8✔
3289
  void     *pIter = NULL;
8✔
3290
  SUserObj *pUser = NULL;
8✔
3291
  SUserObj  newUser = {0};
8✔
3292

3293
  while (1) {
8✔
3294
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
16✔
3295
    if (pIter == NULL) break;
16✔
3296

3297
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
8!
3298
      break;
×
3299
    }
3300

3301
    bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
8✔
3302
    bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
8✔
3303
    if (inRead || inWrite) {
8!
UNCOV
3304
      code = taosHashRemove(newUser.readDbs, db, len);
×
UNCOV
3305
      if (code < 0) {
×
3306
        mError("failed to remove readDbs:%s from user:%s", db, pUser->user);
×
3307
      }
UNCOV
3308
      code = taosHashRemove(newUser.writeDbs, db, len);
×
UNCOV
3309
      if (code < 0) {
×
3310
        mError("failed to remove writeDbs:%s from user:%s", db, pUser->user);
×
3311
      }
3312

UNCOV
3313
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
UNCOV
3314
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3315
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3316
        break;
×
3317
      }
UNCOV
3318
      TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
×
3319
    }
3320

3321
    mndUserFreeObj(&newUser);
8✔
3322
    sdbRelease(pSdb, pUser);
8✔
3323
  }
3324

3325
_OVER:
8✔
3326
  if (pUser != NULL) sdbRelease(pSdb, pUser);
8!
3327
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
8!
3328
  mndUserFreeObj(&newUser);
8✔
3329
  TAOS_RETURN(code);
8✔
3330
}
3331

3332
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
1✔
3333
  int32_t   code = 0;
1✔
3334
  SSdb     *pSdb = pMnode->pSdb;
1✔
3335
  int32_t   len = strlen(stb) + 1;
1✔
3336
  void     *pIter = NULL;
1✔
3337
  SUserObj *pUser = NULL;
1✔
3338
  SUserObj  newUser = {0};
1✔
3339

3340
  while (1) {
1✔
3341
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
2✔
3342
    if (pIter == NULL) break;
2✔
3343

3344
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
1!
3345
      break;
×
3346
    }
3347

3348
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
1✔
3349
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
1✔
3350
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
1✔
3351
    if (inRead || inWrite || inAlter) {
1!
3352
      code = taosHashRemove(newUser.readTbs, stb, len);
×
3353
      if (code < 0) {
×
3354
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
3355
      }
3356
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
3357
      if (code < 0) {
×
3358
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
3359
      }
3360
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
3361
      if (code < 0) {
×
3362
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
3363
      }
3364

3365
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3366
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3367
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3368
        break;
×
3369
      }
3370
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3371
      if (code != 0) {
×
3372
        mndUserFreeObj(&newUser);
×
3373
        sdbRelease(pSdb, pUser);
×
3374
        TAOS_RETURN(code);
×
3375
      }
3376
    }
3377

3378
    mndUserFreeObj(&newUser);
1✔
3379
    sdbRelease(pSdb, pUser);
1✔
3380
  }
3381

3382
  if (pUser != NULL) sdbRelease(pSdb, pUser);
1!
3383
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
1!
3384
  mndUserFreeObj(&newUser);
1✔
3385
  TAOS_RETURN(code);
1✔
3386
}
3387

UNCOV
3388
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
×
UNCOV
3389
  int32_t   code = 0;
×
UNCOV
3390
  SSdb     *pSdb = pMnode->pSdb;
×
UNCOV
3391
  int32_t   len = strlen(view) + 1;
×
UNCOV
3392
  void     *pIter = NULL;
×
UNCOV
3393
  SUserObj *pUser = NULL;
×
UNCOV
3394
  SUserObj  newUser = {0};
×
3395

UNCOV
3396
  while (1) {
×
UNCOV
3397
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
×
UNCOV
3398
    if (pIter == NULL) break;
×
3399

UNCOV
3400
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
×
3401
      break;
×
3402
    }
3403

UNCOV
3404
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
×
UNCOV
3405
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
×
UNCOV
3406
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
×
UNCOV
3407
    if (inRead || inWrite || inAlter) {
×
UNCOV
3408
      code = taosHashRemove(newUser.readViews, view, len);
×
UNCOV
3409
      if (code < 0) {
×
3410
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
×
3411
      }
UNCOV
3412
      code = taosHashRemove(newUser.writeViews, view, len);
×
UNCOV
3413
      if (code < 0) {
×
UNCOV
3414
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
×
3415
      }
UNCOV
3416
      code = taosHashRemove(newUser.alterViews, view, len);
×
UNCOV
3417
      if (code < 0) {
×
UNCOV
3418
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
×
3419
      }
3420

UNCOV
3421
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
UNCOV
3422
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3423
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3424
        break;
×
3425
      }
UNCOV
3426
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
UNCOV
3427
      if (code < 0) {
×
3428
        mndUserFreeObj(&newUser);
×
3429
        sdbRelease(pSdb, pUser);
×
3430
        TAOS_RETURN(code);
×
3431
      }
3432
    }
3433

UNCOV
3434
    mndUserFreeObj(&newUser);
×
UNCOV
3435
    sdbRelease(pSdb, pUser);
×
3436
  }
3437

UNCOV
3438
  if (pUser != NULL) sdbRelease(pSdb, pUser);
×
UNCOV
3439
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
×
UNCOV
3440
  mndUserFreeObj(&newUser);
×
UNCOV
3441
  TAOS_RETURN(code);
×
3442
}
3443

UNCOV
3444
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
×
UNCOV
3445
  int32_t   code = 0;
×
UNCOV
3446
  SSdb     *pSdb = pMnode->pSdb;
×
UNCOV
3447
  int32_t   len = strlen(topic) + 1;
×
UNCOV
3448
  void     *pIter = NULL;
×
UNCOV
3449
  SUserObj *pUser = NULL;
×
UNCOV
3450
  SUserObj  newUser = {0};
×
3451

UNCOV
3452
  while (1) {
×
UNCOV
3453
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
×
UNCOV
3454
    if (pIter == NULL) {
×
UNCOV
3455
      break;
×
3456
    }
3457

UNCOV
3458
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
×
3459
      break;
×
3460
    }
3461

UNCOV
3462
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
×
UNCOV
3463
    if (inTopic) {
×
3464
      code = taosHashRemove(newUser.topics, topic, len);
×
3465
      if (code < 0) {
×
3466
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
3467
      }
3468
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3469
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3470
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3471
        break;
×
3472
      }
3473
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3474
      if (code < 0) {
×
3475
        mndUserFreeObj(&newUser);
×
3476
        sdbRelease(pSdb, pUser);
×
3477
        TAOS_RETURN(code);
×
3478
      }
3479
    }
3480

UNCOV
3481
    mndUserFreeObj(&newUser);
×
UNCOV
3482
    sdbRelease(pSdb, pUser);
×
3483
  }
3484

UNCOV
3485
  if (pUser != NULL) sdbRelease(pSdb, pUser);
×
UNCOV
3486
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
×
UNCOV
3487
  mndUserFreeObj(&newUser);
×
UNCOV
3488
  TAOS_RETURN(code);
×
3489
}
3490

3491
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
3492
  // ver = 0, disable ip white list
3493
  // ver > 0, enable ip white list
3494
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
3495
}
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