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

taosdata / TDengine / #4271

10 Jun 2025 09:45AM UTC coverage: 62.985% (+0.002%) from 62.983%
#4271

push

travis-ci

web-flow
Merge pull request #31337 from taosdata/newtest_3.0

fix TD-35057 and TD-35346

158179 of 319671 branches covered (49.48%)

Branch coverage included in aggregate %.

243860 of 318637 relevant lines covered (76.53%)

18624660.26 hits per line

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

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

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

31
// clang-format on
32

33
#define USER_VER_NUMBER                      7
34
#define USER_VER_SUPPORT_WHITELIST           5
35
#define USER_VER_SUPPORT_WHITELIT_DUAL_STACK 7
36
#define USER_RESERVE_SIZE                    64
37

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

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

48
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
49
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
50

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

60
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
61
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
62

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

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

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

102
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList);
103
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppWhiteList);
104

105
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b);
106
static bool isIpRangeEqual(SIpRange *a, SIpRange *b);
107

108
void destroyIpWhiteTab(SHashObj *pIpWhiteTab);
109

110
#define MND_MAX_USE_HOST (TSDB_PRIVILEDGE_HOST_LEN / 24)
111

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

132
static int32_t ipWhiteMgtUpdateAll(SMnode *pMnode);
133
static int32_t ipWhiteMgtRemove(char *user);
134

135
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList);
136
static int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList);
137
typedef struct {
138
  SHashObj      *pIpWhiteTab;
139
  int64_t        ver;
140
  TdThreadRwlock rw;
141
} SIpWhiteMgt;
142

143
static SIpWhiteMgt ipWhiteMgt;
144

145
const static SIpV4Range defaultIpRange = {.ip = 16777343, .mask = 32};
146

147
static int32_t ipWhiteMgtInit() {
2,289✔
148
  ipWhiteMgt.pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
2,289✔
149
  if (ipWhiteMgt.pIpWhiteTab == NULL) {
2,289!
150
    TAOS_RETURN(terrno);
×
151
  }
152
  ipWhiteMgt.ver = 0;
2,289✔
153
  (void)taosThreadRwlockInit(&ipWhiteMgt.rw, NULL);
2,289✔
154
  TAOS_RETURN(0);
2,289✔
155
}
156
void ipWhiteMgtCleanup() {
2,288✔
157
  destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
2,288✔
158
  (void)taosThreadRwlockDestroy(&ipWhiteMgt.rw);
2,288✔
159
}
2,288✔
160

161
int32_t ipWhiteMgtUpdate(SMnode *pMnode, char *user, SIpWhiteListDual *pNew) {
431✔
162
  int32_t code = 0;
431✔
163
  int32_t lino = 0;
431✔
164
  bool    update = true;
431✔
165
  SArray *fqdns = NULL;
431✔
166
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
431✔
167
  SIpWhiteListDual **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
431✔
168

169
  if (ppList == NULL || *ppList == NULL) {
595!
170
    SIpWhiteListDual *p = cloneIpWhiteList(pNew);
164✔
171
    if (p == NULL) {
164!
172
      update = false;
×
173
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
174
    }
175
    if ((code = taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *))) != 0) {
164!
176
      update = false;
×
177
      taosMemoryFree(p);
×
178
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
179
    }
180
  } else {
181
    SIpWhiteListDual *pOld = *ppList;
267✔
182
    if (isIpWhiteListEqual(pOld, pNew)) {
267✔
183
      update = false;
241✔
184
    } else {
185
      taosMemoryFree(pOld);
26!
186
      SIpWhiteListDual *p = cloneIpWhiteList(pNew);
26✔
187
      if (p == NULL) {
26!
188
        update = false;
×
189
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
190
      }
191
      if ((code = taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *))) != 0) {
26!
192
        update = false;
×
193
        taosMemoryFree(p);
×
194
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
195
      }
196
    }
197
  }
198

199
  fqdns = mndGetAllDnodeFqdns(pMnode);  // TODO: update this line after refactor api
431✔
200
  if (fqdns == NULL) {
431!
201
    update = false;
×
202
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
203
  }
204

205
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
1,069✔
206
    char *fqdn = taosArrayGetP(fqdns, i);
638✔
207
    bool  upd = false;
638✔
208
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, TSDB_DEFAULT_USER, fqdn, IP_WHITE_ADD, &upd), &lino,
638!
209
                    _OVER);
210
    update |= upd;
638✔
211
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, IP_WHITE_ADD, &upd), &lino, _OVER);
638!
212
    update |= upd;
638✔
213
  }
214

215
  if (update) ipWhiteMgt.ver++;
431✔
216

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

240
  if (update) ipWhiteMgt.ver++;
59!
241
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
59✔
242
  return 0;
59✔
243
}
244

245
bool isRangeInWhiteList(SIpWhiteListDual *pList, SIpRange *range) {
6,039✔
246
  for (int i = 0; i < pList->num; i++) {
9,287✔
247
    if (isIpRangeEqual(&pList->pIpRanges[i], range)) {
8,941✔
248
      return true;
5,693✔
249
    }
250
  }
251
  return false;
346✔
252
}
253

254
static int32_t ipWhiteMgtUpdateAll(SMnode *pMnode) {
2,613✔
255
  SHashObj *pNew = NULL;
2,613✔
256
  TAOS_CHECK_RETURN(mndFetchAllIpWhite(pMnode, &pNew));
2,613!
257

258
  SHashObj *pOld = ipWhiteMgt.pIpWhiteTab;
2,613✔
259

260
  ipWhiteMgt.pIpWhiteTab = pNew;
2,613✔
261
  ipWhiteMgt.ver++;
2,613✔
262

263
  destroyIpWhiteTab(pOld);
2,613✔
264
  TAOS_RETURN(0);
2,613✔
265
}
266

267
int64_t mndGetIpWhiteVer(SMnode *pMnode) {
116,178✔
268
  int64_t ver = 0;
116,178✔
269
  int32_t code = 0;
116,178✔
270
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
116,178✔
271
  if (ipWhiteMgt.ver == 0) {
116,178!
272
    // get user and dnode ip white list
273
    if ((code = ipWhiteMgtUpdateAll(pMnode)) != 0) {
×
274
      (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
275
      mError("%s failed to update ip white list since %s", __func__, tstrerror(code));
×
276
      return ver;
×
277
    }
278
    ipWhiteMgt.ver = taosGetTimestampMs();
×
279
  }
280
  ver = ipWhiteMgt.ver;
116,178✔
281
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
116,178✔
282

283
  if (mndEnableIpWhiteList(pMnode) == 0 || tsEnableWhiteList == false) {
116,178!
284
    ver = 0;
116,167✔
285
  }
286
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
116,178✔
287
  return ver;
116,178✔
288
}
289

290
int32_t mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t type, bool *pUpdate) {
7,773✔
291
  int32_t lino = 0;
7,773✔
292
  bool    update = false;
7,773✔
293

294
  SIpRange range = {0};
7,773✔
295
  SIpAddr  addr = {0};
7,773✔
296
  int32_t  code = taosGetIpFromFqdn(tsEnableIpv6, fqdn, &addr);
7,773✔
297
  if (code) {
7,773!
298
    mError("failed to get ip from fqdn: %s at line %d since %s", fqdn, lino, tstrerror(code));
×
299
    TAOS_RETURN(TSDB_CODE_TSC_INVALID_FQDN);
×
300
  }
301

302
  code = tIpStrToUint(&addr, &range);
7,773✔
303
  if (code) {
7,773!
304
    TAOS_RETURN(code);
×
305
  }
306

307
  tIpRangeSetMask(&range, 32);
7,773✔
308

309
  mDebug("ip-white-list may update for user: %s, fqdn: %s", user, fqdn);
7,773✔
310
  SIpWhiteListDual **ppList = taosHashGet(pIpWhiteTab, user, strlen(user));
7,773✔
311
  SIpWhiteListDual  *pList = NULL;
7,773✔
312
  if (ppList != NULL && *ppList != NULL) {
7,773!
313
    pList = *ppList;
6,039✔
314
  }
315

316
  if (type == IP_WHITE_ADD) {
7,773!
317
    if (pList == NULL) {
7,773✔
318
      SIpWhiteListDual *pNewList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange));
1,734!
319
      if (pNewList == NULL) {
1,734!
320
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
321
      }
322
      (void)memcpy(pNewList->pIpRanges, &range, sizeof(SIpRange));
1,734✔
323
      pNewList->num = 1;
1,734✔
324

325
      if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *))) != 0) {
1,734!
326
        taosMemoryFree(pNewList);
×
327
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
328
      }
329
      update = true;
1,734✔
330
    } else {
331
      if (!isRangeInWhiteList(pList, &range)) {
6,039✔
332
        int32_t           sz = sizeof(SIpWhiteListDual) + sizeof(SIpRange) * (pList->num + 1);
346✔
333
        SIpWhiteListDual *pNewList = taosMemoryCalloc(1, sz);
346!
334
        if (pNewList == NULL) {
346!
335
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
336
        }
337
        (void)memcpy(pNewList->pIpRanges, pList->pIpRanges, sizeof(SIpRange) * (pList->num));
346✔
338
        memcpy(&pNewList->pIpRanges[pList->num], &range, sizeof(SIpRange));
346✔
339

340
        pNewList->num = pList->num + 1;
346✔
341

342
        if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *))) != 0) {
346!
343
          taosMemoryFree(pNewList);
×
344
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
345
        }
346
        taosMemoryFree(pList);
346!
347
        update = true;
346✔
348
      }
349
    }
350
  } else if (type == IP_WHITE_DROP) {
×
351
    if (pList != NULL) {
×
352
      if (isRangeInWhiteList(pList, &range)) {
×
353
        if (pList->num == 1) {
×
354
          if (taosHashRemove(pIpWhiteTab, user, strlen(user)) < 0) {
×
355
            mError("failed to remove ip-white-list for user: %s at line %d", user, lino);
×
356
          }
357
          taosMemoryFree(pList);
×
358
        } else {
359
          int32_t           idx = 0;
×
360
          int32_t           sz = sizeof(SIpWhiteListDual) + sizeof(SIpRange) * (pList->num - 1);
×
361
          SIpWhiteListDual *pNewList = taosMemoryCalloc(1, sz);
×
362
          if (pNewList == NULL) {
×
363
            TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
364
          }
365
          for (int i = 0; i < pList->num; i++) {
×
366
            SIpRange *e = &pList->pIpRanges[i];
×
367
            if (!isIpRangeEqual(e, &range)) {
×
368
              memcpy(&pNewList->pIpRanges[idx], e, sizeof(SIpRange));
×
369
              idx++;
×
370
            }
371
          }
372
          pNewList->num = idx;
×
373
          if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *)) != 0)) {
×
374
            taosMemoryFree(pNewList);
×
375
            TAOS_CHECK_GOTO(code, &lino, _OVER);
×
376
          }
377
          taosMemoryFree(pList);
×
378
        }
379
        update = true;
×
380
      }
381
    }
382
  }
383
  if (update) {
7,773✔
384
    mDebug("ip-white-list update for user: %s, fqdn: %s", user, fqdn);
2,080✔
385
  }
386

387
_OVER:
5,799✔
388
  if (pUpdate) *pUpdate = update;
7,773✔
389
  if (code < 0) {
7,773!
390
    mError("failed to update ip-white-list for user: %s, fqdn: %s at line %d since %s", user, fqdn, lino,
×
391
           tstrerror(code));
392
  }
393
  TAOS_RETURN(code);
7,773✔
394
}
395

396
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
2,613✔
397
  int32_t code = 0;
2,613✔
398
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2,613✔
399

400
  if ((code = ipWhiteMgtUpdateAll(pMnode)) != 0) {
2,613!
401
    (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
402
    TAOS_RETURN(code);
×
403
  }
404
  ipWhiteMgt.ver = taosGetTimestampMs();
2,613✔
405
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
2,613✔
406

407
  TAOS_RETURN(code);
2,613✔
408
}
409

410
int32_t mndUpdateIpWhiteForAllUser(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock) {
2,223✔
411
  int32_t code = 0;
2,223✔
412
  int32_t lino = 0;
2,223✔
413
  bool    update = false;
2,223✔
414

415
  if (lock) {
2,223!
416
    (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2,223✔
417
    if (ipWhiteMgt.ver == 0) {
2,223!
418
      TAOS_CHECK_GOTO(ipWhiteMgtUpdateAll(pMnode), &lino, _OVER);
×
419
      ipWhiteMgt.ver = taosGetTimestampMs();
×
420
      mInfo("update ip-white-list, user: %s, ver: %" PRId64, user, ipWhiteMgt.ver);
×
421
    }
422
  }
423

424
  TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, type, &update), &lino, _OVER);
2,223!
425

426
  void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
2,223✔
427
  while (pIter) {
4,472✔
428
    size_t klen = 0;
2,249✔
429
    char  *key = taosHashGetKey(pIter, &klen);
2,249✔
430

431
    char *keyDup = taosMemoryCalloc(1, klen + 1);
2,249!
432
    if (keyDup == NULL) {
2,249!
433
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
434
    }
435
    (void)memcpy(keyDup, key, klen);
2,249✔
436
    bool upd = false;
2,249✔
437
    code = mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, keyDup, fqdn, type, &upd);
2,249✔
438
    update |= upd;
2,249✔
439
    if (code < 0) {
2,249!
440
      taosMemoryFree(keyDup);
×
441
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
442
    }
443
    taosMemoryFree(keyDup);
2,249!
444

445
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
2,249✔
446
  }
447

448
_OVER:
2,223✔
449
  if (update) ipWhiteMgt.ver++;
2,223✔
450
  if (lock) (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
2,223!
451
  if (code < 0) {
2,223!
452
    mError("failed to update ip-white-list for user: %s, fqdn: %s at line %d since %s", user, fqdn, lino,
×
453
           tstrerror(code));
454
  }
455

456
  TAOS_RETURN(code);
2,223✔
457
}
458

459
static int64_t ipWhiteMgtFillMsg(SUpdateIpWhite *pUpdate) {
6✔
460
  int64_t ver = 0;
6✔
461
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
6✔
462
  ver = ipWhiteMgt.ver;
6✔
463
  int32_t num = taosHashGetSize(ipWhiteMgt.pIpWhiteTab);
6✔
464

465
  pUpdate->pUserIpWhite = taosMemoryCalloc(1, num * sizeof(SUpdateUserIpWhite));
6!
466
  if (pUpdate->pUserIpWhite == NULL) {
6!
467
    (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
468
    TAOS_RETURN(terrno);
×
469
  }
470

471
  void   *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
6✔
472
  int32_t i = 0;
6✔
473
  while (pIter) {
12✔
474
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[i];
6✔
475
    SIpWhiteListDual   *list = *(SIpWhiteListDual **)pIter;
6✔
476

477
    size_t klen;
478
    char  *key = taosHashGetKey(pIter, &klen);
6✔
479
    if (list->num != 0) {
6!
480
      pUser->ver = ver;
6✔
481
      (void)memcpy(pUser->user, key, klen);
6✔
482
      pUser->numOfRange = list->num;
6✔
483
      pUser->pIpRanges = taosMemoryCalloc(1, list->num * sizeof(SIpRange));
6!
484
      if (pUser->pIpRanges == NULL) {
6!
485
        (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
486
        TAOS_RETURN(terrno);
×
487
      }
488
      (void)memcpy(pUser->pIpRanges, list->pIpRanges, list->num * sizeof(SIpRange));
6✔
489
      i++;
6✔
490
    }
491
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
6✔
492
  }
493
  pUpdate->numOfUser = i;
6✔
494
  pUpdate->ver = ver;
6✔
495

496
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
6✔
497
  TAOS_RETURN(0);
6✔
498
}
499

500
void destroyIpWhiteTab(SHashObj *pIpWhiteTab) {
4,901✔
501
  if (pIpWhiteTab == NULL) return;
4,901!
502

503
  void *pIter = taosHashIterate(pIpWhiteTab, NULL);
4,901✔
504
  while (pIter) {
7,532✔
505
    SIpWhiteListDual *list = *(SIpWhiteListDual **)pIter;
2,631✔
506
    taosMemoryFree(list);
2,631!
507
    pIter = taosHashIterate(pIpWhiteTab, pIter);
2,631✔
508
  }
509

510
  taosHashCleanup(pIpWhiteTab);
4,901✔
511
}
512
int32_t mndFetchAllIpWhite(SMnode *pMnode, SHashObj **ppIpWhiteTab) {
2,613✔
513
  int32_t   code = 0;
2,613✔
514
  int32_t   lino = 0;
2,613✔
515
  SSdb     *pSdb = pMnode->pSdb;
2,613✔
516
  void     *pIter = NULL;
2,613✔
517
  SHashObj *pIpWhiteTab = NULL;
2,613✔
518
  SArray   *pUserNames = NULL;
2,613✔
519
  SArray   *fqdns = NULL;
2,613✔
520

521
  pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
2,613✔
522
  if (pIpWhiteTab == NULL) {
2,613!
523
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
524
  }
525
  pUserNames = taosArrayInit(8, sizeof(void *));
2,613✔
526
  if (pUserNames == NULL) {
2,613!
527
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
528
  }
529

530
  while (1) {
792✔
531
    SUserObj *pUser = NULL;
3,405✔
532
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
3,405✔
533
    if (pIter == NULL) break;
3,405✔
534

535
    SIpWhiteListDual *pWhiteList = cloneIpWhiteList(pUser->pIpWhiteListDual);
792✔
536
    if (pWhiteList == NULL) {
792!
537
      sdbRelease(pSdb, pUser);
×
538
      sdbCancelFetch(pSdb, pIter);
×
539
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
540
    }
541
    if ((code = taosHashPut(pIpWhiteTab, pUser->user, strlen(pUser->user), &pWhiteList, sizeof(void *))) != 0) {
792!
542
      taosMemoryFree(pWhiteList);
×
543
      sdbRelease(pSdb, pUser);
×
544
      sdbCancelFetch(pSdb, pIter);
×
545
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
546
    }
547

548
    char *name = taosStrdup(pUser->user);
792!
549
    if (name == NULL) {
792!
550
      sdbRelease(pSdb, pUser);
×
551
      sdbCancelFetch(pSdb, pIter);
×
552
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
553
    }
554
    if (taosArrayPush(pUserNames, &name) == NULL) {
792!
555
      taosMemoryFree(name);
×
556
      sdbRelease(pSdb, pUser);
×
557
      sdbCancelFetch(pSdb, pIter);
×
558
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
559
    }
560

561
    sdbRelease(pSdb, pUser);
792✔
562
  }
563

564
  bool found = false;
2,613✔
565
  for (int i = 0; i < taosArrayGetSize(pUserNames); i++) {
2,628✔
566
    char *name = taosArrayGetP(pUserNames, i);
788✔
567
    if (strlen(name) == strlen(TSDB_DEFAULT_USER) && strncmp(name, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER)) == 0) {
788!
568
      found = true;
773✔
569
      break;
773✔
570
    }
571
  }
572
  if (found == false) {
2,613✔
573
    char *name = taosStrdup(TSDB_DEFAULT_USER);
1,840!
574
    if (name == NULL) {
1,840!
575
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
576
    }
577
    if (taosArrayPush(pUserNames, &name) == NULL) {
1,840!
578
      taosMemoryFree(name);
×
579
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
580
    }
581
  }
582

583
  fqdns = mndGetAllDnodeFqdns(pMnode);  // TODO: refactor this line after refactor api
2,613✔
584
  if (fqdns == NULL) {
2,613!
585
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
586
  }
587

588
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
4,604✔
589
    char *fqdn = taosArrayGetP(fqdns, i);
1,991✔
590

591
    for (int j = 0; j < taosArrayGetSize(pUserNames); j++) {
4,016✔
592
      char *name = taosArrayGetP(pUserNames, j);
2,025✔
593
      TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(pIpWhiteTab, name, fqdn, IP_WHITE_ADD, NULL), &lino, _OVER);
2,025!
594
    }
595
  }
596

597
_OVER:
2,613✔
598
  taosArrayDestroyP(fqdns, NULL);
2,613✔
599
  taosArrayDestroyP(pUserNames, NULL);
2,613✔
600

601
  if (code < 0) {
2,613!
602
    mError("failed to fetch all ip white list at line %d since %s", lino, tstrerror(code));
×
603
    destroyIpWhiteTab(pIpWhiteTab);
×
604
    pIpWhiteTab = NULL;
×
605
  }
606
  *ppIpWhiteTab = pIpWhiteTab;
2,613✔
607
  TAOS_RETURN(code);
2,613✔
608
}
609

610
int32_t mndInitUser(SMnode *pMnode) {
2,289✔
611
  TAOS_CHECK_RETURN(ipWhiteMgtInit());
2,289!
612

613
  SSdbTable table = {
2,289✔
614
      .sdbType = SDB_USER,
615
      .keyType = SDB_KEY_BINARY,
616
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
617
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
618
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
619
      .insertFp = (SdbInsertFp)mndUserActionInsert,
620
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
621
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
622
  };
623

624
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
2,289✔
625
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
2,289✔
626
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
2,289✔
627
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
2,289✔
628
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST, mndProcessGetUserWhiteListReq);
2,289✔
629
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST_DUAL, mndProcessGetUserWhiteListReq);
2,289✔
630

631
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE, mndProcesSRetrieveIpWhiteReq);
2,289✔
632
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE_DUAL, mndProcesSRetrieveIpWhiteReq);
2,289✔
633

634
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
2,289✔
635
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
2,289✔
636
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndRetrieveUsersFull);
2,289✔
637
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER_FULL, mndCancelGetNextUser);
2,289✔
638
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
2,289✔
639
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
2,289✔
640
  return sdbSetTable(pMnode->pSdb, table);
2,289✔
641
}
642

643
void mndCleanupUser(SMnode *pMnode) { ipWhiteMgtCleanup(); }
2,288✔
644

645
static bool isDefaultRange(SIpRange *pRange) {
5✔
646
  int32_t code = 0;
5✔
647
  int32_t lino = 0;
5✔
648

649
  SIpRange range4 = {0};
5✔
650
  SIpRange range6 = {0};
5✔
651

652
  code = createDefaultIp4Range(&range4);
5✔
653
  TSDB_CHECK_CODE(code, lino, _error);
5!
654

655
  code = createDefaultIp6Range(&range6);
5✔
656
  TSDB_CHECK_CODE(code, lino, _error);
5!
657

658
  if (isIpRangeEqual(pRange, &range4) || (isIpRangeEqual(pRange, &range6))) {
5!
659
    return true;
×
660
  }
661
_error:
5✔
662
  return false;
5✔
663
};
664

665
static int32_t ipRangeListToStr(SIpRange *range, int32_t num, char *buf, int64_t bufLen) {
5,188✔
666
  int32_t len = 0;
5,188✔
667
  for (int i = 0; i < num; i++) {
15,575✔
668
    SIpRange *pRange = &range[i];
10,384✔
669
    SIpAddr   addr = {0};
10,384✔
670
    tIpUintToStr(pRange, &addr);
10,384✔
671

672
    len += tsnprintf(buf + len, bufLen - len, "%s/%d,", IP_ADDR_STR(&addr), addr.mask);
10,378✔
673
  }
674
  if (len > 0) buf[len - 1] = 0;
5,191!
675
  return len;
5,191✔
676
}
677

678
static bool isIpRangeEqual(SIpRange *a, SIpRange *b) {
9,444✔
679
  // equal or not
680
  if (a->type != b->type) {
9,444✔
681
    return false;
1,629✔
682
  }
683
  if (a->type == 0) {
7,815✔
684
    SIpV4Range *aP4 = &a->ipV4;
7,574✔
685
    SIpV4Range *bP4 = &b->ipV4;
7,574✔
686
    if (aP4->ip != bP4->ip || aP4->mask != bP4->mask) {
7,574!
687
      return false;
1,640✔
688
    } else {
689
      return true;
5,934✔
690
    }
691
  } else {
692
    SIpV6Range *aP6 = &a->ipV6;
241✔
693
    SIpV6Range *bP6 = &b->ipV6;
241✔
694
    if (aP6->addr[0] != bP6->addr[0] || aP6->addr[1] != bP6->addr[1] || aP6->mask != bP6->mask) {
241!
695
      return false;
×
696
    } else {
697
      return true;
241✔
698
    }
699
  }
700

701
  return true;
702
}
703
static bool isRangeInIpWhiteList(SIpWhiteListDual *pList, SIpRange *tgt) {
2✔
704
  for (int i = 0; i < pList->num; i++) {
8✔
705
    if (isIpRangeEqual(&pList->pIpRanges[i], tgt)) return true;
6!
706
  }
707
  return false;
2✔
708
}
709
static bool isIpWhiteListEqual(SIpWhiteListDual *a, SIpWhiteListDual *b) {
267✔
710
  if (a->num != b->num) {
267✔
711
    return false;
26✔
712
  }
713
  for (int i = 0; i < a->num; i++) {
723✔
714
    if (!isIpRangeEqual(&a->pIpRanges[i], &b->pIpRanges[i])) {
482!
715
      return false;
×
716
    }
717
  }
718
  return true;
241✔
719
}
720
int32_t convertIpWhiteListToStr(SIpWhiteListDual *pList, char **buf) {
5,178✔
721
  if (pList->num == 0) {
5,178!
722
    *buf = NULL;
×
723
    return 0;
×
724
  }
725
  int64_t bufLen = pList->num * 256;
5,178✔
726
  *buf = taosMemoryCalloc(1, bufLen);
5,178!
727
  if (*buf == NULL) {
5,188!
728
    return 0;
×
729
  }
730

731
  int32_t len = ipRangeListToStr(pList->pIpRanges, pList->num, *buf, bufLen);
5,188✔
732
  if (len == 0) {
5,191!
733
    taosMemoryFreeClear(*buf);
×
734
    return 0;
×
735
  }
736
  return strlen(*buf);
5,191✔
737
}
738
int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteListDual *pList, uint32_t *pLen) {
6,674✔
739
  int32_t  code = 0;
6,674✔
740
  int32_t  lino = 0;
6,674✔
741
  int32_t  tlen = 0;
6,674✔
742
  SEncoder encoder = {0};
6,674✔
743
  tEncoderInit(&encoder, buf, len);
6,674✔
744

745
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
6,674!
746
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
13,348!
747

748
  for (int i = 0; i < pList->num; i++) {
20,034✔
749
    SIpRange *pRange = &(pList->pIpRanges[i]);
13,360✔
750
    TAOS_CHECK_GOTO(tSerializeIpRange(&encoder, pRange), &lino, _OVER);
13,360!
751
  }
752

753
  tEndEncode(&encoder);
6,674✔
754

755
  tlen = encoder.pos;
6,674✔
756
_OVER:
6,674✔
757
  tEncoderClear(&encoder);
6,674✔
758
  if (code < 0) {
6,674!
759
    mError("failed to serialize ip white list at line %d since %s", lino, tstrerror(code));
×
760
  }
761
  if (pLen) *pLen = tlen;
6,674!
762
  TAOS_RETURN(code);
6,674✔
763
}
764

765
int32_t tDerializeIpWhileList(void *buf, int32_t len, SIpWhiteListDual *pList) {
2,960✔
766
  int32_t  code = 0;
2,960✔
767
  int32_t  lino = 0;
2,960✔
768
  SDecoder decoder = {0};
2,960✔
769
  tDecoderInit(&decoder, buf, len);
2,960✔
770

771
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
2,960!
772
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
5,920!
773

774
  for (int i = 0; i < pList->num; i++) {
8,887✔
775
    SIpRange *pRange = &(pList->pIpRanges[i]);
5,927✔
776
    TAOS_CHECK_GOTO(tDeserializeIpRange(&decoder, pRange), &lino, _OVER);
5,927!
777
  }
778

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

788
int32_t tDerializeIpWhileListFromOldVer(void *buf, int32_t len, SIpWhiteList *pList) {
×
789
  int32_t  code = 0;
×
790
  int32_t  lino = 0;
×
791
  SDecoder decoder = {0};
×
792
  tDecoderInit(&decoder, buf, len);
×
793

794
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
795
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
796

797
  for (int i = 0; i < pList->num; i++) {
×
798
    SIpV4Range *pIp4 = &(pList->pIpRange[i]);
×
799
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->ip), &lino, _OVER);
×
800
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pIp4->mask), &lino, _OVER);
×
801
  }
802

803
_OVER:
×
804
  tEndDecode(&decoder);
×
805
  tDecoderClear(&decoder);
×
806
  if (code < 0) {
×
807
    mError("failed to deserialize ip white list at line %d since %s", lino, tstrerror(code));
×
808
  }
809
  TAOS_RETURN(code);
×
810
}
811

812
static int32_t createIpWhiteList(void *buf, int32_t len, SIpWhiteListDual **ppList) {
2,960✔
813
  int32_t           code = 0;
2,960✔
814
  int32_t           lino = 0;
2,960✔
815
  int32_t           num = 0;
2,960✔
816
  SIpWhiteListDual *p = NULL;
2,960✔
817
  SDecoder          decoder = {0};
2,960✔
818
  tDecoderInit(&decoder, buf, len);
2,960✔
819

820
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
2,960!
821
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
2,960!
822

823
  p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + num * sizeof(SIpRange));
2,960!
824
  if (p == NULL) {
2,960!
825
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
826
  }
827
  TAOS_CHECK_GOTO(tDerializeIpWhileList(buf, len, p), &lino, _OVER);
2,960!
828

829
_OVER:
2,960✔
830
  tEndDecode(&decoder);
2,960✔
831
  tDecoderClear(&decoder);
2,960✔
832
  if (code < 0) {
2,960!
833
    taosMemoryFreeClear(p);
×
834
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
835
  }
836
  *ppList = p;
2,960✔
837
  TAOS_RETURN(code);
2,960✔
838
}
839

840
static int32_t createIpWhiteListFromOldVer(void *buf, int32_t len, SIpWhiteList **ppList) {
×
841
  int32_t       code = 0;
×
842
  int32_t       lino = 0;
×
843
  int32_t       num = 0;
×
844
  SIpWhiteList *p = NULL;
×
845
  SDecoder      decoder = {0};
×
846
  tDecoderInit(&decoder, buf, len);
×
847

848
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
849
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
850

851
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
×
852
  if (p == NULL) {
×
853
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
854
  }
855
  TAOS_CHECK_GOTO(tDerializeIpWhileListFromOldVer(buf, len, p), &lino, _OVER);
×
856

857
_OVER:
×
858
  tEndDecode(&decoder);
×
859
  tDecoderClear(&decoder);
×
860
  if (code < 0) {
×
861
    taosMemoryFreeClear(p);
×
862
    mError("failed to create ip white list at line %d since %s", lino, tstrerror(code));
×
863
  }
864
  *ppList = p;
×
865
  TAOS_RETURN(code);
×
866
}
867

868
static int32_t createDefaultIpWhiteList(SIpWhiteListDual **ppWhiteList) {
1,893✔
869
  int32_t code = 0;
1,893✔
870
  int32_t lino = 0;
1,893✔
871
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * 2);
1,893!
872
  if (*ppWhiteList == NULL) {
1,893!
873
    TAOS_RETURN(terrno);
×
874
  }
875
  (*ppWhiteList)->num = 2;
1,893✔
876

877
  SIpRange v4 = {0};
1,893✔
878
  SIpRange v6 = {0};
1,893✔
879

880
#ifndef TD_ASTRA
881
  code = createDefaultIp4Range(&v4);
1,893✔
882
  TSDB_CHECK_CODE(code, lino, _error);
1,893!
883

884
  code = createDefaultIp6Range(&v6);
1,893✔
885
  TSDB_CHECK_CODE(code, lino, _error);
1,893!
886

887
#endif
888

889
_error:
1,893✔
890
  if (code != 0) {
1,893!
891
    taosMemoryFree(*ppWhiteList);
×
892
    *ppWhiteList = NULL;
×
893
    mError("failed to create default ip white list at line %d since %s", __LINE__, tstrerror(code));
×
894
  } else {
895
    memcpy(&(*ppWhiteList)->pIpRanges[0], &v4, sizeof(SIpRange));
1,893✔
896
    memcpy(&(*ppWhiteList)->pIpRanges[1], &v6, sizeof(SIpRange));
1,893✔
897
  }
898
  return 0;
1,893✔
899
}
900

901
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
1,734✔
902
  int32_t  code = 0;
1,734✔
903
  int32_t  lino = 0;
1,734✔
904
  SUserObj userObj = {0};
1,734✔
905
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
1,734✔
906
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
1,734✔
907
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
1,734✔
908
  userObj.createdTime = taosGetTimestampMs();
1,734✔
909
  userObj.updateTime = userObj.createdTime;
1,734✔
910
  userObj.sysInfo = 1;
1,734✔
911
  userObj.enable = 1;
1,734✔
912
  userObj.ipWhiteListVer = taosGetTimestampMs();
1,734✔
913
  TAOS_CHECK_RETURN(createDefaultIpWhiteList(&userObj.pIpWhiteListDual));
1,734!
914
  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
1,734!
915
    userObj.superUser = 1;
1,734✔
916
    userObj.createdb = 1;
1,734✔
917
  }
918

919
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
1,734✔
920
  if (pRaw == NULL) goto _ERROR;
1,734!
921
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
1,734!
922

923
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
1,734!
924

925
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
1,734✔
926
  if (pTrans == NULL) {
1,734!
927
    sdbFreeRaw(pRaw);
×
928
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
×
929
    goto _ERROR;
×
930
  }
931
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
1,734!
932

933
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
1,734!
934
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
935
    mndTransDrop(pTrans);
×
936
    goto _ERROR;
×
937
  }
938
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
1,734!
939

940
  if (mndTransPrepare(pMnode, pTrans) != 0) {
1,734!
941
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
942
    mndTransDrop(pTrans);
×
943
    goto _ERROR;
×
944
  }
945

946
  mndTransDrop(pTrans);
1,734✔
947
  taosMemoryFree(userObj.pIpWhiteListDual);
1,734!
948
  return 0;
1,734✔
949
_ERROR:
×
950
  taosMemoryFree(userObj.pIpWhiteListDual);
×
951
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
952
}
953

954
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
1,734✔
955
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
1,734✔
956
}
957

958
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
6,674✔
959
  int32_t code = 0;
6,674✔
960
  int32_t lino = 0;
6,674✔
961
  int32_t ipWhiteReserve =
×
962
      pUser->pIpWhiteListDual ? (sizeof(SIpRange) * pUser->pIpWhiteListDual->num + sizeof(SIpWhiteListDual) + 4) : 16;
6,674!
963
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
6,674✔
964
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
6,674✔
965
  int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
6,674✔
966
  int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
6,674✔
967
  int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
6,674✔
968
  int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
6,674✔
969
  int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
6,674✔
970
  int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
6,674✔
971
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
6,674✔
972
  int32_t numOfUseDbs = taosHashGetSize(pUser->useDbs);
6,674✔
973
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + (numOfReadDbs + numOfWriteDbs) * TSDB_DB_FNAME_LEN +
6,674✔
974
                 numOfTopics * TSDB_TOPIC_FNAME_LEN + ipWhiteReserve;
6,674✔
975
  char    *buf = NULL;
6,674✔
976
  SSdbRaw *pRaw = NULL;
6,674✔
977

978
  char *stb = taosHashIterate(pUser->readTbs, NULL);
6,674✔
979
  while (stb != NULL) {
6,731✔
980
    size_t keyLen = 0;
57✔
981
    void  *key = taosHashGetKey(stb, &keyLen);
57✔
982
    size += sizeof(int32_t);
57✔
983
    size += keyLen;
57✔
984

985
    size_t valueLen = 0;
57✔
986
    valueLen = strlen(stb) + 1;
57✔
987
    size += sizeof(int32_t);
57✔
988
    size += valueLen;
57✔
989
    stb = taosHashIterate(pUser->readTbs, stb);
57✔
990
  }
991

992
  stb = taosHashIterate(pUser->writeTbs, NULL);
6,674✔
993
  while (stb != NULL) {
6,743✔
994
    size_t keyLen = 0;
69✔
995
    void  *key = taosHashGetKey(stb, &keyLen);
69✔
996
    size += sizeof(int32_t);
69✔
997
    size += keyLen;
69✔
998

999
    size_t valueLen = 0;
69✔
1000
    valueLen = strlen(stb) + 1;
69✔
1001
    size += sizeof(int32_t);
69✔
1002
    size += valueLen;
69✔
1003
    stb = taosHashIterate(pUser->writeTbs, stb);
69✔
1004
  }
1005

1006
  stb = taosHashIterate(pUser->alterTbs, NULL);
6,674✔
1007
  while (stb != NULL) {
6,691✔
1008
    size_t keyLen = 0;
17✔
1009
    void  *key = taosHashGetKey(stb, &keyLen);
17✔
1010
    size += sizeof(int32_t);
17✔
1011
    size += keyLen;
17✔
1012

1013
    size_t valueLen = 0;
17✔
1014
    valueLen = strlen(stb) + 1;
17✔
1015
    size += sizeof(int32_t);
17✔
1016
    size += valueLen;
17✔
1017
    stb = taosHashIterate(pUser->alterTbs, stb);
17✔
1018
  }
1019

1020
  stb = taosHashIterate(pUser->readViews, NULL);
6,674✔
1021
  while (stb != NULL) {
6,781✔
1022
    size_t keyLen = 0;
107✔
1023
    void  *key = taosHashGetKey(stb, &keyLen);
107✔
1024
    size += sizeof(int32_t);
107✔
1025
    size += keyLen;
107✔
1026

1027
    size_t valueLen = 0;
107✔
1028
    valueLen = strlen(stb) + 1;
107✔
1029
    size += sizeof(int32_t);
107✔
1030
    size += valueLen;
107✔
1031
    stb = taosHashIterate(pUser->readViews, stb);
107✔
1032
  }
1033

1034
  stb = taosHashIterate(pUser->writeViews, NULL);
6,674✔
1035
  while (stb != NULL) {
6,765✔
1036
    size_t keyLen = 0;
91✔
1037
    void  *key = taosHashGetKey(stb, &keyLen);
91✔
1038
    size += sizeof(int32_t);
91✔
1039
    size += keyLen;
91✔
1040

1041
    size_t valueLen = 0;
91✔
1042
    valueLen = strlen(stb) + 1;
91✔
1043
    size += sizeof(int32_t);
91✔
1044
    size += valueLen;
91✔
1045
    stb = taosHashIterate(pUser->writeViews, stb);
91✔
1046
  }
1047

1048
  stb = taosHashIterate(pUser->alterViews, NULL);
6,674✔
1049
  while (stb != NULL) {
6,769✔
1050
    size_t keyLen = 0;
95✔
1051
    void  *key = taosHashGetKey(stb, &keyLen);
95✔
1052
    size += sizeof(int32_t);
95✔
1053
    size += keyLen;
95✔
1054

1055
    size_t valueLen = 0;
95✔
1056
    valueLen = strlen(stb) + 1;
95✔
1057
    size += sizeof(int32_t);
95✔
1058
    size += valueLen;
95✔
1059
    stb = taosHashIterate(pUser->alterViews, stb);
95✔
1060
  }
1061

1062
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
6,674✔
1063
  while (useDb != NULL) {
6,885✔
1064
    size_t keyLen = 0;
211✔
1065
    void  *key = taosHashGetKey(useDb, &keyLen);
211✔
1066
    size += sizeof(int32_t);
211✔
1067
    size += keyLen;
211✔
1068
    size += sizeof(int32_t);
211✔
1069
    useDb = taosHashIterate(pUser->useDbs, useDb);
211✔
1070
  }
1071

1072
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
6,674✔
1073
  if (pRaw == NULL) {
6,674!
1074
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1075
  }
1076

1077
  int32_t dataPos = 0;
6,674✔
1078
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
6,674!
1079
  SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
6,674!
1080
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
6,674!
1081
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
6,674!
1082
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
6,674!
1083
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
6,674!
1084
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
6,674!
1085
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
6,674!
1086
  SDB_SET_UINT8(pRaw, dataPos, pUser->flag, _OVER)
6,674!
1087
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
6,674!
1088
  SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
6,674!
1089
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
6,674!
1090
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
6,674!
1091
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
6,674!
1092

1093
  char *db = taosHashIterate(pUser->readDbs, NULL);
6,674✔
1094
  while (db != NULL) {
6,813✔
1095
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
139!
1096
    db = taosHashIterate(pUser->readDbs, db);
139✔
1097
  }
1098

1099
  db = taosHashIterate(pUser->writeDbs, NULL);
6,674✔
1100
  while (db != NULL) {
6,807✔
1101
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
133!
1102
    db = taosHashIterate(pUser->writeDbs, db);
133✔
1103
  }
1104

1105
  char *topic = taosHashIterate(pUser->topics, NULL);
6,674✔
1106
  while (topic != NULL) {
6,711✔
1107
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
37!
1108
    topic = taosHashIterate(pUser->topics, topic);
37✔
1109
  }
1110

1111
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
6,674!
1112
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
6,674!
1113
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
6,674!
1114
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
6,674!
1115
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
6,674!
1116
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
6,674!
1117
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
6,674!
1118

1119
  stb = taosHashIterate(pUser->readTbs, NULL);
6,674✔
1120
  while (stb != NULL) {
6,731✔
1121
    size_t keyLen = 0;
57✔
1122
    void  *key = taosHashGetKey(stb, &keyLen);
57✔
1123
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
57!
1124
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
57!
1125

1126
    size_t valueLen = 0;
57✔
1127
    valueLen = strlen(stb) + 1;
57✔
1128
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
57!
1129
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
57!
1130
    stb = taosHashIterate(pUser->readTbs, stb);
57✔
1131
  }
1132

1133
  stb = taosHashIterate(pUser->writeTbs, NULL);
6,674✔
1134
  while (stb != NULL) {
6,743✔
1135
    size_t keyLen = 0;
69✔
1136
    void  *key = taosHashGetKey(stb, &keyLen);
69✔
1137
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
69!
1138
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
69!
1139

1140
    size_t valueLen = 0;
69✔
1141
    valueLen = strlen(stb) + 1;
69✔
1142
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
69!
1143
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
69!
1144
    stb = taosHashIterate(pUser->writeTbs, stb);
69✔
1145
  }
1146

1147
  stb = taosHashIterate(pUser->alterTbs, NULL);
6,674✔
1148
  while (stb != NULL) {
6,691✔
1149
    size_t keyLen = 0;
17✔
1150
    void  *key = taosHashGetKey(stb, &keyLen);
17✔
1151
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
17!
1152
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
17!
1153

1154
    size_t valueLen = 0;
17✔
1155
    valueLen = strlen(stb) + 1;
17✔
1156
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
17!
1157
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
17!
1158
    stb = taosHashIterate(pUser->alterTbs, stb);
17✔
1159
  }
1160

1161
  stb = taosHashIterate(pUser->readViews, NULL);
6,674✔
1162
  while (stb != NULL) {
6,781✔
1163
    size_t keyLen = 0;
107✔
1164
    void  *key = taosHashGetKey(stb, &keyLen);
107✔
1165
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
107!
1166
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
107!
1167

1168
    size_t valueLen = 0;
107✔
1169
    valueLen = strlen(stb) + 1;
107✔
1170
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
107!
1171
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
107!
1172
    stb = taosHashIterate(pUser->readViews, stb);
107✔
1173
  }
1174

1175
  stb = taosHashIterate(pUser->writeViews, NULL);
6,674✔
1176
  while (stb != NULL) {
6,765✔
1177
    size_t keyLen = 0;
91✔
1178
    void  *key = taosHashGetKey(stb, &keyLen);
91✔
1179
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
91!
1180
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
91!
1181

1182
    size_t valueLen = 0;
91✔
1183
    valueLen = strlen(stb) + 1;
91✔
1184
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
91!
1185
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
91!
1186
    stb = taosHashIterate(pUser->writeViews, stb);
91✔
1187
  }
1188

1189
  stb = taosHashIterate(pUser->alterViews, NULL);
6,674✔
1190
  while (stb != NULL) {
6,769✔
1191
    size_t keyLen = 0;
95✔
1192
    void  *key = taosHashGetKey(stb, &keyLen);
95✔
1193
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
95!
1194
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
95!
1195

1196
    size_t valueLen = 0;
95✔
1197
    valueLen = strlen(stb) + 1;
95✔
1198
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
95!
1199
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
95!
1200
    stb = taosHashIterate(pUser->alterViews, stb);
95✔
1201
  }
1202

1203
  useDb = taosHashIterate(pUser->useDbs, NULL);
6,674✔
1204
  while (useDb != NULL) {
6,885✔
1205
    size_t keyLen = 0;
211✔
1206
    void  *key = taosHashGetKey(useDb, &keyLen);
211✔
1207
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
211!
1208
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
211!
1209

1210
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
211!
1211
    useDb = taosHashIterate(pUser->useDbs, useDb);
211✔
1212
  }
1213

1214
  // save white list
1215
  int32_t num = pUser->pIpWhiteListDual->num;
6,674✔
1216
  int32_t tlen = sizeof(SIpWhiteListDual) + num * sizeof(SIpRange) + 4;
6,674✔
1217
  if ((buf = taosMemoryCalloc(1, tlen)) == NULL) {
6,674!
1218
    TAOS_CHECK_GOTO(terrno, NULL, _OVER);
×
1219
  }
1220
  int32_t len = 0;
6,674✔
1221
  TAOS_CHECK_GOTO(tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteListDual, &len), &lino, _OVER);
6,674!
1222

1223
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
6,674!
1224
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
6,674!
1225

1226
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
6,674!
1227

1228
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
6,674!
1229
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
6,674!
1230

1231
_OVER:
6,674✔
1232
  taosMemoryFree(buf);
6,674!
1233
  if (code < 0) {
6,674!
1234
    mError("user:%s, failed to encode user action to raw:%p at line %d since %s", pUser->user, pRaw, lino,
×
1235
           tstrerror(code));
1236
    sdbFreeRaw(pRaw);
×
1237
    pRaw = NULL;
×
1238
    terrno = code;
×
1239
  }
1240

1241
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
6,674✔
1242
  return pRaw;
6,674✔
1243
}
1244

1245
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
2,960✔
1246
  int32_t   code = 0;
2,960✔
1247
  int32_t   lino = 0;
2,960✔
1248
  SSdbRow  *pRow = NULL;
2,960✔
1249
  SUserObj *pUser = NULL;
2,960✔
1250
  char     *key = NULL;
2,960✔
1251
  char     *value = NULL;
2,960✔
1252

1253
  int8_t sver = 0;
2,960✔
1254
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
2,960!
1255
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1256
  }
1257

1258
  if (sver < 1 || sver > USER_VER_NUMBER) {
2,960!
1259
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1260
  }
1261

1262
  pRow = sdbAllocRow(sizeof(SUserObj));
2,960✔
1263
  if (pRow == NULL) {
2,960!
1264
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1265
  }
1266

1267
  pUser = sdbGetRowObj(pRow);
2,960✔
1268
  if (pUser == NULL) {
2,960!
1269
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1270
  }
1271

1272
  int32_t dataPos = 0;
2,960✔
1273
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
2,960!
1274
  SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
2,960!
1275
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
2,960!
1276
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
2,960!
1277
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
2,960!
1278
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
2,960!
1279
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
2,960!
1280
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
2,960!
1281
  SDB_GET_UINT8(pRaw, dataPos, &pUser->flag, _OVER)
2,960!
1282
  if (pUser->superUser) pUser->createdb = 1;
2,960✔
1283
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
2,960!
1284
  if (sver >= 4) {
2,960!
1285
    SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
2,960!
1286
  }
1287

1288
  int32_t numOfReadDbs = 0;
2,960✔
1289
  int32_t numOfWriteDbs = 0;
2,960✔
1290
  int32_t numOfTopics = 0;
2,960✔
1291
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
2,960!
1292
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
2,960!
1293
  if (sver >= 2) {
2,960!
1294
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
2,960!
1295
  }
1296

1297
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1298
  pUser->writeDbs =
2,960✔
1299
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1300
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1301
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
2,960!
1302
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1303
    goto _OVER;
×
1304
  }
1305

1306
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
3,081✔
1307
    char db[TSDB_DB_FNAME_LEN] = {0};
121✔
1308
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
121!
1309
    int32_t len = strlen(db) + 1;
121✔
1310
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
121!
1311
  }
1312

1313
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
3,074✔
1314
    char db[TSDB_DB_FNAME_LEN] = {0};
114✔
1315
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
114!
1316
    int32_t len = strlen(db) + 1;
114✔
1317
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
114!
1318
  }
1319

1320
  if (sver >= 2) {
2,960!
1321
    for (int32_t i = 0; i < numOfTopics; ++i) {
2,994✔
1322
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
34✔
1323
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
34!
1324
      int32_t len = strlen(topic) + 1;
34✔
1325
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
34!
1326
    }
1327
  }
1328

1329
  if (sver >= 3) {
2,960!
1330
    int32_t numOfReadTbs = 0;
2,960✔
1331
    int32_t numOfWriteTbs = 0;
2,960✔
1332
    int32_t numOfAlterTbs = 0;
2,960✔
1333
    int32_t numOfReadViews = 0;
2,960✔
1334
    int32_t numOfWriteViews = 0;
2,960✔
1335
    int32_t numOfAlterViews = 0;
2,960✔
1336
    int32_t numOfUseDbs = 0;
2,960✔
1337
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
2,960!
1338
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
2,960!
1339
    if (sver >= 6) {
2,960!
1340
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
2,960!
1341
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
2,960!
1342
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
2,960!
1343
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
2,960!
1344
    }
1345
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
2,960!
1346

1347
    pUser->readTbs =
2,960✔
1348
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1349
    pUser->writeTbs =
2,960✔
1350
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1351
    pUser->alterTbs =
2,960✔
1352
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1353

1354
    pUser->readViews =
2,960✔
1355
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1356
    pUser->writeViews =
2,960✔
1357
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1358
    pUser->alterViews =
2,960✔
1359
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1360

1361
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,960✔
1362

1363
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
2,960!
1364
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
2,960!
1365
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1366
      goto _OVER;
×
1367
    }
1368

1369
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,994✔
1370
      int32_t keyLen = 0;
34✔
1371
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
34!
1372

1373
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
34!
1374
      if (key == NULL) {
34!
1375
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1376
      }
1377
      (void)memset(key, 0, keyLen);
34✔
1378
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
34!
1379

1380
      int32_t valuelen = 0;
34✔
1381
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
34!
1382
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
34!
1383
      if (value == NULL) {
34!
1384
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1385
      }
1386
      (void)memset(value, 0, valuelen);
34✔
1387
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
34!
1388

1389
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
34!
1390
    }
1391

1392
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
3,000✔
1393
      int32_t keyLen = 0;
40✔
1394
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
40!
1395

1396
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
40!
1397
      if (key == NULL) {
40!
1398
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1399
      }
1400
      (void)memset(key, 0, keyLen);
40✔
1401
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
40!
1402

1403
      int32_t valuelen = 0;
40✔
1404
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
40!
1405
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
40!
1406
      if (value == NULL) {
40!
1407
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1408
      }
1409
      (void)memset(value, 0, valuelen);
40✔
1410
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
40!
1411

1412
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
40!
1413
    }
1414

1415
    if (sver >= 6) {
2,960!
1416
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
2,976✔
1417
        int32_t keyLen = 0;
16✔
1418
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
16!
1419

1420
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
16!
1421
        if (key == NULL) {
16!
1422
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1423
        }
1424
        (void)memset(key, 0, keyLen);
16✔
1425
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
16!
1426

1427
        int32_t valuelen = 0;
16✔
1428
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
16!
1429
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
16!
1430
        if (value == NULL) {
16!
1431
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1432
        }
1433
        (void)memset(value, 0, valuelen);
16✔
1434
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
16!
1435

1436
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
16!
1437
      }
1438

1439
      for (int32_t i = 0; i < numOfReadViews; ++i) {
3,064✔
1440
        int32_t keyLen = 0;
104✔
1441
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
104!
1442

1443
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
104!
1444
        if (key == NULL) {
104!
1445
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1446
        }
1447
        (void)memset(key, 0, keyLen);
104✔
1448
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
104!
1449

1450
        int32_t valuelen = 0;
104✔
1451
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
104!
1452
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
104!
1453
        if (value == NULL) {
104!
1454
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1455
        }
1456
        (void)memset(value, 0, valuelen);
104✔
1457
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
104!
1458

1459
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
104!
1460
      }
1461

1462
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
3,048✔
1463
        int32_t keyLen = 0;
88✔
1464
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
88!
1465

1466
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
88!
1467
        if (key == NULL) {
88!
1468
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1469
        }
1470
        (void)memset(key, 0, keyLen);
88✔
1471
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
88!
1472

1473
        int32_t valuelen = 0;
88✔
1474
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
88!
1475
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
88!
1476
        if (value == NULL) {
88!
1477
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1478
        }
1479
        (void)memset(value, 0, valuelen);
88✔
1480
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
88!
1481

1482
        TAOS_CHECK_GOTO(taosHashPut(pUser->writeViews, key, keyLen, value, valuelen), &lino, _OVER);
88!
1483
      }
1484

1485
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
3,052✔
1486
        int32_t keyLen = 0;
92✔
1487
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
92!
1488

1489
        TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
92!
1490
        if (key == NULL) {
92!
1491
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1492
        }
1493
        (void)memset(key, 0, keyLen);
92✔
1494
        SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
92!
1495

1496
        int32_t valuelen = 0;
92✔
1497
        SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
92!
1498
        TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
92!
1499
        if (value == NULL) {
92!
1500
          TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1501
        }
1502
        (void)memset(value, 0, valuelen);
92✔
1503
        SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
92!
1504

1505
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterViews, key, keyLen, value, valuelen), &lino, _OVER);
92!
1506
      }
1507
    }
1508

1509
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
3,118✔
1510
      int32_t keyLen = 0;
158✔
1511
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
158!
1512

1513
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
158!
1514
      if (key == NULL) {
158!
1515
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1516
      }
1517
      (void)memset(key, 0, keyLen);
158✔
1518
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
158!
1519

1520
      int32_t ref = 0;
158✔
1521
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
158!
1522

1523
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
158!
1524
    }
1525
  }
1526
  // decoder white list
1527
  if (sver >= USER_VER_SUPPORT_WHITELIST) {
2,960!
1528
    if (sver < USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
2,960!
1529
      int32_t len = 0;
×
1530
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
×
1531

1532
      TAOS_MEMORY_REALLOC(key, len);
×
1533
      if (key == NULL) {
×
1534
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1535
      }
1536
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
×
1537

1538
      SIpWhiteList *pIpWhiteList = NULL;
×
1539
      TAOS_CHECK_GOTO(createIpWhiteListFromOldVer(key, len, &pIpWhiteList), &lino, _OVER);
×
1540

1541
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
×
1542

1543
      code = cvtIpWhiteListToDual(pIpWhiteList, &pUser->pIpWhiteListDual);
×
1544
      if (code != 0) {
×
1545
        taosMemoryFreeClear(pIpWhiteList);
×
1546
      }
1547
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1548

1549
      taosMemoryFreeClear(pIpWhiteList);
×
1550

1551
    } else if (sver >= USER_VER_SUPPORT_WHITELIT_DUAL_STACK) {
2,960!
1552
      int32_t len = 0;
2,960✔
1553
      SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
2,960!
1554

1555
      TAOS_MEMORY_REALLOC(key, len);
2,960!
1556
      if (key == NULL) {
2,960!
1557
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1558
      }
1559
      SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
2,960!
1560

1561
      TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteListDual), &lino, _OVER);
2,960!
1562
      SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
2,960!
1563
    }
1564
  }
1565

1566
  if (pUser->pIpWhiteListDual == NULL) {
2,960!
1567
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteListDual), &lino, _OVER);
×
1568
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1569
  }
1570

1571
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,960!
1572
  taosInitRWLatch(&pUser->lock);
2,960✔
1573

1574
_OVER:
2,960✔
1575
  taosMemoryFree(key);
2,960!
1576
  taosMemoryFree(value);
2,960!
1577
  if (code < 0) {
2,960!
1578
    terrno = code;
×
1579
    mError("user:%s, failed to decode at line %d from raw:%p since %s", pUser == NULL ? "null" : pUser->user, lino,
×
1580
           pRaw, tstrerror(code));
1581
    if (pUser != NULL) {
×
1582
      taosHashCleanup(pUser->readDbs);
×
1583
      taosHashCleanup(pUser->writeDbs);
×
1584
      taosHashCleanup(pUser->topics);
×
1585
      taosHashCleanup(pUser->readTbs);
×
1586
      taosHashCleanup(pUser->writeTbs);
×
1587
      taosHashCleanup(pUser->alterTbs);
×
1588
      taosHashCleanup(pUser->readViews);
×
1589
      taosHashCleanup(pUser->writeViews);
×
1590
      taosHashCleanup(pUser->alterViews);
×
1591
      taosHashCleanup(pUser->useDbs);
×
1592
      taosMemoryFreeClear(pUser->pIpWhiteListDual);
×
1593
    }
1594
    taosMemoryFreeClear(pRow);
×
1595
    return NULL;
×
1596
  }
1597

1598
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
2,960✔
1599
  return pRow;
2,960✔
1600
}
1601

1602
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
2,513✔
1603
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
2,513✔
1604

1605
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
2,513✔
1606
  if (pAcct == NULL) {
2,513!
1607
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
×
1608
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
×
1609
    TAOS_RETURN(terrno);
×
1610
  }
1611
  pUser->acctId = pAcct->acctId;
2,513✔
1612
  sdbRelease(pSdb, pAcct);
2,513✔
1613

1614
  return 0;
2,513✔
1615
}
1616

1617
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
3,021,059✔
1618
  int32_t code = 0;
3,021,059✔
1619
  *ppNew =
3,021,037✔
1620
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
3,021,059✔
1621
  if (*ppNew == NULL) {
3,021,037!
1622
    TAOS_RETURN(terrno);
×
1623
  }
1624

1625
  char *tb = taosHashIterate(pOld, NULL);
3,021,037✔
1626
  while (tb != NULL) {
3,022,255✔
1627
    size_t keyLen = 0;
1,219✔
1628
    char  *key = taosHashGetKey(tb, &keyLen);
1,219✔
1629

1630
    int32_t valueLen = strlen(tb) + 1;
1,219✔
1631
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
1,219!
1632
      taosHashCancelIterate(pOld, tb);
×
1633
      taosHashCleanup(*ppNew);
×
1634
      TAOS_RETURN(code);
×
1635
    }
1636
    tb = taosHashIterate(pOld, tb);
1,219✔
1637
  }
1638

1639
  TAOS_RETURN(code);
3,021,036✔
1640
}
1641

1642
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
9,208✔
1643
  int32_t code = 0;
9,208✔
1644
  *ppNew =
9,208✔
1645
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
9,208✔
1646
  if (*ppNew == NULL) {
9,208!
1647
    TAOS_RETURN(terrno);
×
1648
  }
1649

1650
  int32_t *db = taosHashIterate(pOld, NULL);
9,208✔
1651
  while (db != NULL) {
9,568✔
1652
    size_t keyLen = 0;
360✔
1653
    char  *key = taosHashGetKey(db, &keyLen);
360✔
1654

1655
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
360!
1656
      taosHashCancelIterate(pOld, db);
×
1657
      taosHashCleanup(*ppNew);
×
1658
      TAOS_RETURN(code);
×
1659
    }
1660
    db = taosHashIterate(pOld, db);
360✔
1661
  }
1662

1663
  TAOS_RETURN(code);
9,208✔
1664
}
1665

1666
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
9,208✔
1667
  int32_t code = 0;
9,208✔
1668
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
9,208✔
1669
  pNew->authVersion++;
9,208✔
1670
  pNew->updateTime = taosGetTimestampMs();
9,208✔
1671

1672
  taosRLockLatch(&pUser->lock);
9,208✔
1673
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->readDbs, &pNew->readDbs), NULL, _OVER);
9,208!
1674
  TAOS_CHECK_GOTO(mndDupDbHash(pUser->writeDbs, &pNew->writeDbs), NULL, _OVER);
9,208!
1675
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readTbs, &pNew->readTbs), NULL, _OVER);
9,208!
1676
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeTbs, &pNew->writeTbs), NULL, _OVER);
9,208!
1677
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterTbs, &pNew->alterTbs), NULL, _OVER);
9,208!
1678
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->readViews, &pNew->readViews), NULL, _OVER);
9,208!
1679
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->writeViews, &pNew->writeViews), NULL, _OVER);
9,208!
1680
  TAOS_CHECK_GOTO(mndDupTableHash(pUser->alterViews, &pNew->alterViews), NULL, _OVER);
9,208!
1681
  TAOS_CHECK_GOTO(mndDupTopicHash(pUser->topics, &pNew->topics), NULL, _OVER);
9,208!
1682
  TAOS_CHECK_GOTO(mndDupUseDbHash(pUser->useDbs, &pNew->useDbs), NULL, _OVER);
9,208!
1683
  pNew->pIpWhiteListDual = cloneIpWhiteList(pUser->pIpWhiteListDual);
9,208✔
1684
  if (pNew->pIpWhiteListDual == NULL) {
9,208!
1685
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1686
  }
1687

1688
_OVER:
9,208✔
1689
  taosRUnLockLatch(&pUser->lock);
9,208✔
1690
  TAOS_RETURN(code);
9,208✔
1691
}
1692

1693
void mndUserFreeObj(SUserObj *pUser) {
26,027✔
1694
  taosHashCleanup(pUser->readDbs);
26,027✔
1695
  taosHashCleanup(pUser->writeDbs);
26,027✔
1696
  taosHashCleanup(pUser->topics);
26,027✔
1697
  taosHashCleanup(pUser->readTbs);
26,027✔
1698
  taosHashCleanup(pUser->writeTbs);
26,027✔
1699
  taosHashCleanup(pUser->alterTbs);
26,027✔
1700
  taosHashCleanup(pUser->readViews);
26,027✔
1701
  taosHashCleanup(pUser->writeViews);
26,027✔
1702
  taosHashCleanup(pUser->alterViews);
26,027✔
1703
  taosHashCleanup(pUser->useDbs);
26,027✔
1704
  taosMemoryFreeClear(pUser->pIpWhiteListDual);
26,027!
1705
  pUser->readDbs = NULL;
26,027✔
1706
  pUser->writeDbs = NULL;
26,027✔
1707
  pUser->topics = NULL;
26,027✔
1708
  pUser->readTbs = NULL;
26,027✔
1709
  pUser->writeTbs = NULL;
26,027✔
1710
  pUser->alterTbs = NULL;
26,027✔
1711
  pUser->readViews = NULL;
26,027✔
1712
  pUser->writeViews = NULL;
26,027✔
1713
  pUser->alterViews = NULL;
26,027✔
1714
  pUser->useDbs = NULL;
26,027✔
1715
}
26,027✔
1716

1717
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
2,959✔
1718
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
2,959✔
1719
  mndUserFreeObj(pUser);
2,959✔
1720
  return 0;
2,959✔
1721
}
1722

1723
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
348✔
1724
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
348!
1725
  taosWLockLatch(&pOld->lock);
348✔
1726
  pOld->updateTime = pNew->updateTime;
348✔
1727
  pOld->authVersion = pNew->authVersion;
348✔
1728
  pOld->passVersion = pNew->passVersion;
348✔
1729
  pOld->sysInfo = pNew->sysInfo;
348✔
1730
  pOld->enable = pNew->enable;
348✔
1731
  pOld->flag = pNew->flag;
348✔
1732
  (void)memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
348✔
1733
  TSWAP(pOld->readDbs, pNew->readDbs);
348✔
1734
  TSWAP(pOld->writeDbs, pNew->writeDbs);
348✔
1735
  TSWAP(pOld->topics, pNew->topics);
348✔
1736
  TSWAP(pOld->readTbs, pNew->readTbs);
348✔
1737
  TSWAP(pOld->writeTbs, pNew->writeTbs);
348✔
1738
  TSWAP(pOld->alterTbs, pNew->alterTbs);
348✔
1739
  TSWAP(pOld->readViews, pNew->readViews);
348✔
1740
  TSWAP(pOld->writeViews, pNew->writeViews);
348✔
1741
  TSWAP(pOld->alterViews, pNew->alterViews);
348✔
1742
  TSWAP(pOld->useDbs, pNew->useDbs);
348✔
1743

1744
  int32_t sz = sizeof(SIpWhiteListDual) + pNew->pIpWhiteListDual->num * sizeof(SIpRange);
348✔
1745
  TAOS_MEMORY_REALLOC(pOld->pIpWhiteListDual, sz);
348!
1746
  if (pOld->pIpWhiteListDual == NULL) {
348!
1747
    taosWUnLockLatch(&pOld->lock);
×
1748
    return terrno;
×
1749
  }
1750
  (void)memcpy(pOld->pIpWhiteListDual, pNew->pIpWhiteListDual, sz);
348✔
1751
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
348✔
1752

1753
  taosWUnLockLatch(&pOld->lock);
348✔
1754

1755
  return 0;
348✔
1756
}
1757

1758
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
6,694,750✔
1759
  int32_t code = 0;
6,694,750✔
1760
  SSdb   *pSdb = pMnode->pSdb;
6,694,750✔
1761

1762
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
6,694,750✔
1763
  if (*ppUser == NULL) {
6,694,919✔
1764
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
215!
1765
      code = TSDB_CODE_MND_USER_NOT_EXIST;
215✔
1766
    } else {
1767
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
1768
    }
1769
  }
1770
  TAOS_RETURN(code);
6,694,919✔
1771
}
1772

1773
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
6,695,059✔
1774
  SSdb *pSdb = pMnode->pSdb;
6,695,059✔
1775
  sdbRelease(pSdb, pUser);
6,695,059✔
1776
}
6,695,661✔
1777

1778
static int32_t addDefaultIpToTable(int8_t enableIpv6, SHashObj *pUniqueTab) {
5✔
1779
  int32_t code = 0;
5✔
1780
  int32_t lino = 0;
5✔
1781
  int32_t dummpy = 0;
5✔
1782

1783
  SIpRange ipv4 = {0}, ipv6 = {0};
5✔
1784
  code = createDefaultIp4Range(&ipv4);
5✔
1785
  TSDB_CHECK_CODE(code, lino, _error);
5!
1786

1787
  code = taosHashPut(pUniqueTab, &ipv4, sizeof(ipv4), &dummpy, sizeof(dummpy));
5✔
1788
  TSDB_CHECK_CODE(code, lino, _error);
5!
1789

1790
  if (enableIpv6) {
5!
1791
    code = createDefaultIp6Range(&ipv6);
×
1792
    TSDB_CHECK_CODE(code, lino, _error);
×
1793

1794
    code = taosHashPut(pUniqueTab, &ipv6, sizeof(ipv6), &dummpy, sizeof(dummpy));
×
1795
    TSDB_CHECK_CODE(code, lino, _error);
×
1796
  }
1797
_error:
5✔
1798
  if (code != 0) {
5!
1799
    mError("failed to add default ip range to table since %s", tstrerror(code));
×
1800
  }
1801
  return code;
5✔
1802
}
1803
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
164✔
1804
  int32_t  code = 0;
164✔
1805
  int32_t  lino = 0;
164✔
1806
  SUserObj userObj = {0};
164✔
1807

1808
  if (pCreate->passIsMd5 == 1) {
164!
1809
    memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
164✔
1810
  } else {
1811
    if (pCreate->isImport != 1) {
×
1812
      taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
×
1813
    } else {
1814
      // mInfo("pCreate->pass:%s", pCreate->eass)
1815
      memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
1816
    }
1817
  }
1818

1819
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
164✔
1820
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
164✔
1821
  userObj.createdTime = taosGetTimestampMs();
164✔
1822
  userObj.updateTime = userObj.createdTime;
164✔
1823
  userObj.superUser = 0;  // pCreate->superUser;
164✔
1824
  userObj.sysInfo = pCreate->sysInfo;
164✔
1825
  userObj.enable = pCreate->enable;
164✔
1826
  userObj.createdb = pCreate->createDb;
164✔
1827

1828
  if (pCreate->numIpRanges == 0) {
164✔
1829
    TAOS_CHECK_RETURN(createDefaultIpWhiteList(&userObj.pIpWhiteListDual));
159!
1830
  } else {
1831
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
5✔
1832
    if (pUniqueTab == NULL) {
5!
1833
      TAOS_RETURN(terrno);
×
1834
    }
1835
    int32_t dummpy = 0;
5✔
1836
    for (int i = 0; i < pCreate->numIpRanges; i++) {
14✔
1837
      SIpRange range = {0};
9✔
1838
      if (pCreate->pIpDualRanges == NULL) {
9!
1839
        range.type = 0;
×
1840
        memcpy(&range.ipV4, &(pCreate->pIpRanges[i]), sizeof(SIpV4Range));
×
1841
      } else {
1842
        memcpy(&range, pCreate->pIpDualRanges + i, sizeof(SIpRange));
9✔
1843
      }
1844

1845
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
9!
1846
        taosHashCleanup(pUniqueTab);
×
1847
        TAOS_RETURN(code);
×
1848
      }
1849
    }
1850
    code = addDefaultIpToTable(tsEnableIpv6, pUniqueTab);
5✔
1851
    if (code != 0) {
5!
1852
      taosHashCleanup(pUniqueTab);
×
1853
      TAOS_RETURN(code);
×
1854
    }
1855

1856
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USE_HOST) {
5!
1857
      taosHashCleanup(pUniqueTab);
×
1858
      TAOS_RETURN(TSDB_CODE_MND_TOO_MANY_USER_HOST);
×
1859
    }
1860

1861
    int32_t           numOfRanges = taosHashGetSize(pUniqueTab);
5✔
1862
    SIpWhiteListDual *p = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + numOfRanges * sizeof(SIpRange));
5!
1863
    if (p == NULL) {
5!
1864
      taosHashCleanup(pUniqueTab);
×
1865
      TAOS_RETURN(terrno);
×
1866
    }
1867
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
5✔
1868
    int32_t i = 0;
5✔
1869
    while (pIter) {
18✔
1870
      size_t    len = 0;
13✔
1871
      SIpRange *key = taosHashGetKey(pIter, &len);
13✔
1872
      memcpy(p->pIpRanges + i, key, sizeof(SIpRange));
13✔
1873
      pIter = taosHashIterate(pUniqueTab, pIter);
13✔
1874
      i++;
13✔
1875
    }
1876

1877
    taosHashCleanup(pUniqueTab);
5✔
1878
    p->num = numOfRanges;
5✔
1879
    userObj.pIpWhiteListDual = p;
5✔
1880
  }
1881

1882
  userObj.ipWhiteListVer = taosGetTimestampMs();
164✔
1883

1884
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
164✔
1885
  if (pTrans == NULL) {
164!
1886
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
1887
    taosMemoryFree(userObj.pIpWhiteListDual);
×
1888
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1889
  }
1890
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
164!
1891

1892
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
164✔
1893
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
164!
1894
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1895
    mndTransDrop(pTrans);
×
1896
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1897
  }
1898
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
164!
1899

1900
  if (mndTransPrepare(pMnode, pTrans) != 0) {
164!
1901
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1902
    mndTransDrop(pTrans);
×
1903
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1904
  }
1905
  if ((code = ipWhiteMgtUpdate(pMnode, userObj.user, userObj.pIpWhiteListDual)) != 0) {
164!
1906
    mndTransDrop(pTrans);
×
1907
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1908
  }
1909

1910
  taosMemoryFree(userObj.pIpWhiteListDual);
164!
1911
  mndTransDrop(pTrans);
164✔
1912
  return 0;
164✔
1913
_OVER:
×
1914
  taosMemoryFree(userObj.pIpWhiteListDual);
×
1915

1916
  TAOS_RETURN(code);
×
1917
}
1918

1919
static int32_t mndCheckPasswordMinLen(const char *pwd, int32_t len) {
×
1920
  if (len < TSDB_PASSWORD_MIN_LEN) {
×
1921
    return -1;
×
1922
  }
1923
  return 0;
×
1924
}
1925

1926
static int32_t mndCheckPasswordMaxLen(const char *pwd, int32_t len) {
×
1927
  if (len > TSDB_PASSWORD_MAX_LEN) {
×
1928
    return -1;
×
1929
  }
1930
  return 0;
×
1931
}
1932

1933
static int32_t mndCheckPasswordFmt(const char *pwd, int32_t len) {
×
1934
  if (strcmp(pwd, "taosdata") == 0) {
×
1935
    return 0;
×
1936
  }
1937

1938
  bool charTypes[4] = {0};
×
1939
  for (int32_t i = 0; i < len; ++i) {
×
1940
    if (taosIsBigChar(pwd[i])) {
×
1941
      charTypes[0] = true;
×
1942
    } else if (taosIsSmallChar(pwd[i])) {
×
1943
      charTypes[1] = true;
×
1944
    } else if (taosIsNumberChar(pwd[i])) {
×
1945
      charTypes[2] = true;
×
1946
    } else if (taosIsSpecialChar(pwd[i])) {
×
1947
      charTypes[3] = true;
×
1948
    } else {
1949
      return -1;
×
1950
    }
1951
  }
1952

1953
  int32_t numOfTypes = 0;
×
1954
  for (int32_t i = 0; i < 4; ++i) {
×
1955
    numOfTypes += charTypes[i];
×
1956
  }
1957

1958
  if (numOfTypes < 3) {
×
1959
    return -1;
×
1960
  }
1961

1962
  return 0;
×
1963
}
1964

1965
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
164✔
1966
  SMnode        *pMnode = pReq->info.node;
164✔
1967
  int32_t        code = 0;
164✔
1968
  int32_t        lino = 0;
164✔
1969
  SUserObj      *pUser = NULL;
164✔
1970
  SUserObj      *pOperUser = NULL;
164✔
1971
  SCreateUserReq createReq = {0};
164✔
1972

1973
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
164!
1974
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
1975
  }
1976

1977
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.createDb, createReq.isImport);
164!
1978

1979
#ifndef TD_ENTERPRISE
1980
  if (createReq.isImport == 1) {
1981
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
1982
  }
1983
#endif
1984

1985
  if (createReq.isImport != 1) {
164✔
1986
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
163!
1987
  } else {
1988
    if (strcmp(pReq->info.conn.user, "root") != 0) {
1!
1989
      mError("The operation is not permitted, user:%s", pReq->info.conn.user);
×
1990
      TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
1991
    }
1992
  }
1993

1994
  if (createReq.user[0] == 0) {
164!
1995
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
1996
  }
1997

1998
  if (createReq.passIsMd5 == 0) {
164!
1999
    int32_t len = strlen(createReq.pass);
×
2000
    if (createReq.isImport != 1) {
×
2001
      if (mndCheckPasswordMinLen(createReq.pass, len) != 0) {
×
2002
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY, &lino, _OVER);
×
2003
      }
2004
      if (mndCheckPasswordMaxLen(createReq.pass, len) != 0) {
×
2005
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG, &lino, _OVER);
×
2006
      }
2007
      if (mndCheckPasswordFmt(createReq.pass, len) != 0) {
×
2008
        TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
2009
      }
2010
    }
2011
  }
2012

2013
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
164✔
2014
  if (pUser != NULL) {
164!
2015
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
×
2016
  }
2017

2018
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
164✔
2019
  if (pOperUser == NULL) {
164!
2020
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2021
  }
2022

2023
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
164!
2024

2025
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
164✔
2026
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
164!
2027

2028
  char detail[1000] = {0};
164✔
2029
  (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
164✔
2030
                  createReq.superUser, createReq.sysInfo);
164✔
2031
  char operation[15] = {0};
164✔
2032
  if (createReq.isImport == 1) {
164✔
2033
    tstrncpy(operation, "importUser", sizeof(operation));
1✔
2034
  } else {
2035
    tstrncpy(operation, "createUser", sizeof(operation));
163✔
2036
  }
2037

2038
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
164✔
2039

2040
_OVER:
164✔
2041
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
164!
2042
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
×
2043
  }
2044

2045
  mndReleaseUser(pMnode, pUser);
164✔
2046
  mndReleaseUser(pMnode, pOperUser);
164✔
2047
  tFreeSCreateUserReq(&createReq);
164✔
2048

2049
  TAOS_RETURN(code);
164✔
2050
}
2051

2052
int32_t mndProcessGetUserWhiteListReq(SRpcMsg *pReq) {
231✔
2053
  SMnode              *pMnode = pReq->info.node;
231✔
2054
  int32_t              code = 0;
231✔
2055
  int32_t              lino = 0;
231✔
2056
  int32_t              contLen = 0;
231✔
2057
  void                *pRsp = NULL;
231✔
2058
  SUserObj            *pUser = NULL;
231✔
2059
  SGetUserWhiteListReq wlReq = {0};
231✔
2060
  SGetUserWhiteListRsp wlRsp = {0};
231✔
2061

2062
  int32_t (*serialFn)(void *, int32_t, SGetUserWhiteListRsp *) = NULL;
231✔
2063
  int32_t (*setRspFn)(SMnode * pMnode, SUserObj * pUser, SGetUserWhiteListRsp * pRsp) = NULL;
231✔
2064

2065
  if (pReq->msgType == TDMT_MND_GET_USER_WHITELIST_DUAL) {
231!
2066
    serialFn = tSerializeSGetUserWhiteListDualRsp;
×
2067
    setRspFn = mndSetUserWhiteListDualRsp;
×
2068
  } else {
2069
    serialFn = tSerializeSGetUserWhiteListRsp;
231✔
2070
    setRspFn = mndSetUserWhiteListRsp;
231✔
2071
  }
2072
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
231!
2073
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
2074
  }
2075
  mTrace("user: %s, start to get whitelist", wlReq.user);
231✔
2076

2077
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
231✔
2078
  if (pUser == NULL) {
231!
2079
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
2080
  }
2081

2082
  TAOS_CHECK_GOTO(setRspFn(pMnode, pUser, &wlRsp), &lino, _OVER);
231!
2083

2084
  contLen = serialFn(NULL, 0, &wlRsp);
231✔
2085
  if (contLen < 0) {
231!
2086
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2087
  }
2088
  pRsp = rpcMallocCont(contLen);
231✔
2089
  if (pRsp == NULL) {
231!
2090
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2091
  }
2092

2093
  contLen = serialFn(pRsp, contLen, &wlRsp);
231✔
2094
  if (contLen < 0) {
231!
2095
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
2096
  }
2097

2098
_OVER:
231✔
2099
  mndReleaseUser(pMnode, pUser);
231✔
2100
  tFreeSGetUserWhiteListDualRsp(&wlRsp);
231✔
2101
  if (code < 0) {
231!
2102
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
2103
    rpcFreeCont(pRsp);
×
2104
    pRsp = NULL;
×
2105
    contLen = 0;
×
2106
  }
2107
  pReq->code = code;
231✔
2108
  pReq->info.rsp = pRsp;
231✔
2109
  pReq->info.rspLen = contLen;
231✔
2110

2111
  TAOS_RETURN(code);
231✔
2112
}
2113

2114
int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
6✔
2115
  int32_t        code = 0;
6✔
2116
  int32_t        lino = 0;
6✔
2117
  int32_t        len = 0;
6✔
2118
  void          *pRsp = NULL;
6✔
2119
  SUpdateIpWhite ipWhite = {0};
6✔
2120

2121
  // impl later
2122
  SRetrieveIpWhiteReq req = {0};
6✔
2123
  if (tDeserializeRetrieveIpWhite(pReq->pCont, pReq->contLen, &req) != 0) {
6!
2124
    code = TSDB_CODE_INVALID_MSG;
×
2125
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2126
  }
2127

2128
  int32_t (*fn)(void *, int32_t, SUpdateIpWhite *) = NULL;
6✔
2129
  if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITE) {
6!
2130
    fn = tSerializeSUpdateIpWhite;
×
2131
  } else if (pReq->msgType == TDMT_MND_RETRIEVE_IP_WHITE_DUAL) {
6!
2132
    fn = tSerializeSUpdateIpWhiteDual;
6✔
2133
  }
2134

2135
  TAOS_CHECK_GOTO(ipWhiteMgtFillMsg(&ipWhite), &lino, _OVER);
6!
2136

2137
  len = fn(NULL, 0, &ipWhite);
6✔
2138
  if (len < 0) {
6!
2139
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2140
  }
2141

2142
  pRsp = rpcMallocCont(len);
6✔
2143
  if (!pRsp) {
6!
2144
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2145
  }
2146
  len = fn(pRsp, len, &ipWhite);
6✔
2147
  if (len < 0) {
6!
2148
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2149
  }
2150

2151
_OVER:
6✔
2152
  if (code < 0) {
6!
2153
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2154
    rpcFreeCont(pRsp);
×
2155
    pRsp = NULL;
×
2156
    len = 0;
×
2157
  }
2158
  pReq->code = code;
6✔
2159
  pReq->info.rsp = pRsp;
6✔
2160
  pReq->info.rspLen = len;
6✔
2161

2162
  tFreeSUpdateIpWhiteReq(&ipWhite);
6✔
2163
  TAOS_RETURN(code);
6✔
2164
}
2165

2166
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
267✔
2167
  int32_t code = 0;
267✔
2168
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
267✔
2169
  if (pTrans == NULL) {
267!
2170
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
×
2171
    TAOS_RETURN(terrno);
×
2172
  }
2173
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
267!
2174

2175
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
267✔
2176
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
267!
2177
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2178
    mndTransDrop(pTrans);
×
2179
    TAOS_RETURN(terrno);
×
2180
  }
2181
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
267✔
2182
  if (code < 0) {
267!
2183
    mndTransDrop(pTrans);
×
2184
    TAOS_RETURN(code);
×
2185
  }
2186

2187
  if (mndTransPrepare(pMnode, pTrans) != 0) {
267!
2188
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2189
    mndTransDrop(pTrans);
×
2190
    TAOS_RETURN(terrno);
×
2191
  }
2192
  if ((code = ipWhiteMgtUpdate(pMnode, pNew->user, pNew->pIpWhiteListDual)) != 0) {
267!
2193
    mndTransDrop(pTrans);
×
2194
    TAOS_RETURN(code);
×
2195
  }
2196
  mndTransDrop(pTrans);
267✔
2197
  return 0;
267✔
2198
}
2199

2200
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
875,017✔
2201
  int32_t code = 0;
875,017✔
2202

2203
  *ppNew =
875,022✔
2204
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
875,017✔
2205
  if (*ppNew == NULL) {
875,022!
2206
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2207
    TAOS_RETURN(code);
×
2208
  }
2209

2210
  char *db = taosHashIterate(pOld, NULL);
875,022✔
2211
  while (db != NULL) {
875,609✔
2212
    int32_t len = strlen(db) + 1;
591✔
2213
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
591!
2214
      taosHashCancelIterate(pOld, db);
×
2215
      taosHashCleanup(*ppNew);
×
2216
      TAOS_RETURN(code);
×
2217
    }
2218
    db = taosHashIterate(pOld, db);
591✔
2219
  }
2220

2221
  TAOS_RETURN(code);
875,018✔
2222
}
2223

2224
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
865,811✔
2225

2226
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
9,208✔
2227

2228
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
101✔
2229
                                  SSdb *pSdb) {
2230
  void *pIter = NULL;
101✔
2231
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
101✔
2232

2233
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
101✔
2234
  int32_t len = strlen(tbFName) + 1;
101✔
2235

2236
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
124!
2237
    char *value = taosHashGet(hash, tbFName, len);
23✔
2238
    if (value != NULL) {
23!
2239
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2240
    }
2241

2242
    int32_t condLen = alterReq->tagCondLen;
23✔
2243
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
23!
2244
  } else {
2245
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
78!
2246
  }
2247

2248
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
101✔
2249
  int32_t  ref = 1;
101✔
2250
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
101✔
2251
  if (NULL != currRef) {
101✔
2252
    ref = (*currRef) + 1;
40✔
2253
  }
2254
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
101!
2255

2256
  TAOS_RETURN(0);
101✔
2257
}
2258

2259
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
11✔
2260
                                        SSdb *pSdb) {
2261
  void *pIter = NULL;
11✔
2262
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
11✔
2263
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
11✔
2264
  int32_t len = strlen(tbFName) + 1;
11✔
2265

2266
  if (taosHashRemove(hash, tbFName, len) != 0) {
11!
2267
    TAOS_RETURN(0);  // not found
×
2268
  }
2269

2270
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
11✔
2271
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
11✔
2272
  if (NULL == currRef) {
11!
2273
    return 0;
×
2274
  }
2275

2276
  if (1 == *currRef) {
11!
2277
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
11!
2278
      TAOS_RETURN(0);  // not found
×
2279
    }
2280
    return 0;
11✔
2281
  }
2282
  int32_t ref = (*currRef) - 1;
×
2283
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
2284

2285
  return 0;
×
2286
}
2287

2288
static char *mndUserAuditTypeStr(int32_t type) {
36✔
2289
  if (type == TSDB_ALTER_USER_PASSWD) {
36!
2290
    return "changePassword";
36✔
2291
  }
2292
  if (type == TSDB_ALTER_USER_SUPERUSER) {
×
2293
    return "changeSuperUser";
×
2294
  }
2295
  if (type == TSDB_ALTER_USER_ENABLE) {
×
2296
    return "enableUser";
×
2297
  }
2298
  if (type == TSDB_ALTER_USER_SYSINFO) {
×
2299
    return "userSysInfo";
×
2300
  }
2301
  if (type == TSDB_ALTER_USER_CREATEDB) {
×
2302
    return "userCreateDB";
×
2303
  }
2304
  return "error";
×
2305
}
2306

2307
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
204✔
2308
  SSdb   *pSdb = pMnode->pSdb;
204✔
2309
  void   *pIter = NULL;
204✔
2310
  int32_t code = 0;
204✔
2311
  int32_t lino = 0;
204✔
2312

2313
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
204✔
2314
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
151!
2315
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
53✔
2316
      int32_t len = strlen(pAlterReq->objname) + 1;
50✔
2317
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
50✔
2318
      if (pDb == NULL) {
50✔
2319
        mndReleaseDb(pMnode, pDb);
7✔
2320
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
7!
2321
      }
2322
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
43!
2323
          0) {
2324
        mndReleaseDb(pMnode, pDb);
×
2325
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2326
      }
2327
      mndReleaseDb(pMnode, pDb);
43✔
2328
    } else {
2329
      while (1) {
9✔
2330
        SDbObj *pDb = NULL;
12✔
2331
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
12✔
2332
        if (pIter == NULL) break;
12✔
2333
        int32_t len = strlen(pDb->name) + 1;
9✔
2334
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
9!
2335
          sdbRelease(pSdb, pDb);
×
2336
          sdbCancelFetch(pSdb, pIter);
×
2337
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2338
        }
2339
        sdbRelease(pSdb, pDb);
9✔
2340
      }
2341
    }
2342
  }
2343

2344
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
197✔
2345
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
151!
2346
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
46✔
2347
      int32_t len = strlen(pAlterReq->objname) + 1;
43✔
2348
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
43✔
2349
      if (pDb == NULL) {
43✔
2350
        mndReleaseDb(pMnode, pDb);
1✔
2351
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
1!
2352
      }
2353
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
42!
2354
          0) {
2355
        mndReleaseDb(pMnode, pDb);
×
2356
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2357
      }
2358
      mndReleaseDb(pMnode, pDb);
42✔
2359
    } else {
2360
      while (1) {
9✔
2361
        SDbObj *pDb = NULL;
12✔
2362
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
12✔
2363
        if (pIter == NULL) break;
12✔
2364
        int32_t len = strlen(pDb->name) + 1;
9✔
2365
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
9!
2366
          sdbRelease(pSdb, pDb);
×
2367
          sdbCancelFetch(pSdb, pIter);
×
2368
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2369
        }
2370
        sdbRelease(pSdb, pDb);
9✔
2371
      }
2372
    }
2373
  }
2374

2375
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2376
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
168!
2377
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
28✔
2378
      int32_t len = strlen(pAlterReq->objname) + 1;
25✔
2379
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
25✔
2380
      if (pDb == NULL) {
25!
2381
        mndReleaseDb(pMnode, pDb);
×
2382
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2383
      }
2384
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
25✔
2385
      if (code < 0) {
25!
2386
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2387
      }
2388
      mndReleaseDb(pMnode, pDb);
25✔
2389
    } else {
2390
      taosHashClear(pNewUser->readDbs);
3✔
2391
    }
2392
  }
2393

2394
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2395
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
170!
2396
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
26✔
2397
      int32_t len = strlen(pAlterReq->objname) + 1;
23✔
2398
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
23✔
2399
      if (pDb == NULL) {
23!
2400
        mndReleaseDb(pMnode, pDb);
×
2401
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2402
      }
2403
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
23✔
2404
      if (code < 0) {
23!
2405
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2406
      }
2407
      mndReleaseDb(pMnode, pDb);
23✔
2408
    } else {
2409
      taosHashClear(pNewUser->writeDbs);
3✔
2410
    }
2411
  }
2412

2413
  SHashObj *pReadTbs = pNewUser->readTbs;
196✔
2414
  SHashObj *pWriteTbs = pNewUser->writeTbs;
196✔
2415
  SHashObj *pAlterTbs = pNewUser->alterTbs;
196✔
2416

2417
#ifdef TD_ENTERPRISE
2418
  if (pAlterReq->isView) {
196✔
2419
    pReadTbs = pNewUser->readViews;
24✔
2420
    pWriteTbs = pNewUser->writeViews;
24✔
2421
    pAlterTbs = pNewUser->alterViews;
24✔
2422
  }
2423
#endif
2424

2425
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2426
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
154!
2427
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
42!
2428
  }
2429

2430
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2431
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
157!
2432
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
39!
2433
  }
2434

2435
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2436
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
176!
2437
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
20!
2438
  }
2439

2440
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2441
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
190!
2442
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
6!
2443
  }
2444

2445
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196✔
2446
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
191!
2447
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
5!
2448
  }
2449

2450
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
196!
2451
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
196!
2452
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2453
  }
2454

2455
#ifdef USE_TOPIC
2456
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
196✔
2457
    int32_t      len = strlen(pAlterReq->objname) + 1;
15✔
2458
    SMqTopicObj *pTopic = NULL;
15✔
2459
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
15!
2460
      mndReleaseTopic(pMnode, pTopic);
×
2461
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2462
    }
2463
    if ((code = taosHashPut(pNewUser->topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN)) != 0) {
15!
2464
      mndReleaseTopic(pMnode, pTopic);
×
2465
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2466
    }
2467
    mndReleaseTopic(pMnode, pTopic);
15✔
2468
  }
2469

2470
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
196✔
2471
    int32_t      len = strlen(pAlterReq->objname) + 1;
11✔
2472
    SMqTopicObj *pTopic = NULL;
11✔
2473
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
11✔
2474
      mndReleaseTopic(pMnode, pTopic);
1✔
2475
      TAOS_CHECK_GOTO(code, &lino, _OVER);
1!
2476
    }
2477
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
10✔
2478
    if (code < 0) {
10!
2479
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
2480
    }
2481
    mndReleaseTopic(pMnode, pTopic);
10✔
2482
  }
2483
#endif
2484
_OVER:
185✔
2485
  if (code < 0) {
204✔
2486
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
9!
2487
  }
2488
  TAOS_RETURN(code);
204✔
2489
}
2490

2491
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
371✔
2492
  SMnode       *pMnode = pReq->info.node;
371✔
2493
  SSdb         *pSdb = pMnode->pSdb;
371✔
2494
  void         *pIter = NULL;
371✔
2495
  int32_t       code = 0;
371✔
2496
  int32_t       lino = 0;
371✔
2497
  SUserObj     *pUser = NULL;
371✔
2498
  SUserObj     *pOperUser = NULL;
371✔
2499
  SUserObj      newUser = {0};
371✔
2500
  SAlterUserReq alterReq = {0};
371✔
2501

2502
  TAOS_CHECK_GOTO(tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq), &lino, _OVER);
371!
2503

2504
  mInfo("user:%s, start to alter", alterReq.user);
371!
2505

2506
  if (alterReq.user[0] == 0) {
371!
2507
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2508
  }
2509
  if (alterReq.passIsMd5 == 0) {
371✔
2510
    if (TSDB_ALTER_USER_PASSWD == alterReq.alterType) {
334!
2511
      int32_t len = strlen(alterReq.pass);
×
2512
      if (mndCheckPasswordMinLen(alterReq.pass, len) != 0) {
×
2513
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY, &lino, _OVER);
×
2514
      }
2515
      if (mndCheckPasswordMaxLen(alterReq.pass, len) != 0) {
×
2516
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG, &lino, _OVER);
×
2517
      }
2518
      if (mndCheckPasswordFmt(alterReq.pass, len) != 0) {
×
2519
        TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
2520
      }
2521
    }
2522
  }
2523

2524
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
371✔
2525

2526
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
349✔
2527
  if (pOperUser == NULL) {
349!
2528
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2529
  }
2530

2531
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
349✔
2532

2533
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
278!
2534

2535
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
278✔
2536
    if (alterReq.passIsMd5 == 1) {
36!
2537
      (void)memcpy(newUser.pass, alterReq.pass, TSDB_PASSWORD_LEN);
36✔
2538
    } else {
2539
      taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), newUser.pass);
×
2540
    }
2541

2542
    if (0 != strncmp(pUser->pass, newUser.pass, TSDB_PASSWORD_LEN)) {
36✔
2543
      ++newUser.passVersion;
31✔
2544
    }
2545
  }
2546

2547
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
278!
2548
    newUser.superUser = alterReq.superUser;
×
2549
  }
2550

2551
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
278✔
2552
    newUser.enable = alterReq.enable;
9✔
2553
  }
2554

2555
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
278✔
2556
    newUser.sysInfo = alterReq.sysInfo;
18✔
2557
  }
2558

2559
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
278✔
2560
    newUser.createdb = alterReq.createdb;
7✔
2561
  }
2562

2563
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
278✔
2564
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
204✔
2565
  }
2566

2567
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WHITE_LIST) {
269✔
2568
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
2!
2569

2570
    int32_t           num = pUser->pIpWhiteListDual->num + alterReq.numIpRanges;
2✔
2571
    int32_t           idx = pUser->pIpWhiteListDual->num;
2✔
2572
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
2!
2573

2574
    if (pNew == NULL) {
2!
2575
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2576
    }
2577

2578
    bool exist = false;
2✔
2579
    (void)memcpy(pNew->pIpRanges, pUser->pIpWhiteListDual->pIpRanges, sizeof(SIpRange) * idx);
2✔
2580
    for (int i = 0; i < alterReq.numIpRanges; i++) {
4✔
2581
      SIpRange range = {0};
2✔
2582
      if (alterReq.pIpDualRanges == NULL) {
2!
2583
        range.type = 0;
×
2584
        memcpy(&range.ipV4, &alterReq.pIpRanges[i], sizeof(SIpV4Range));
×
2585
      } else {
2586
        memcpy(&range, &alterReq.pIpDualRanges[i], sizeof(SIpRange));
2✔
2587
        range = alterReq.pIpDualRanges[i];
2✔
2588
      }
2589
      if (!isRangeInIpWhiteList(pUser->pIpWhiteListDual, &range)) {
2!
2590
        // already exist, just ignore;
2591
        (void)memcpy(&pNew->pIpRanges[idx], &range, sizeof(SIpRange));
2✔
2592
        idx++;
2✔
2593
        continue;
2✔
2594
      } else {
2595
        exist = true;
×
2596
      }
2597
    }
2598
    if (exist) {
2!
2599
      taosMemoryFree(pNew);
×
2600
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_EXIST, &lino, _OVER);
×
2601
    }
2602
    pNew->num = idx;
2✔
2603
    newUser.pIpWhiteListDual = pNew;
2✔
2604
    newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
2✔
2605

2606
    if (pNew->num > MND_MAX_USE_HOST) {
2!
2607
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_HOST, &lino, _OVER);
×
2608
    }
2609
  }
2610
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_WHITE_LIST) {
269✔
2611
    taosMemoryFreeClear(newUser.pIpWhiteListDual);
2!
2612

2613
    int32_t           num = pUser->pIpWhiteListDual->num;
2✔
2614
    bool              noexist = true;
2✔
2615
    bool              localHost = false;
2✔
2616
    SIpWhiteListDual *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteListDual) + sizeof(SIpRange) * num);
2!
2617

2618
    if (pNew == NULL) {
2!
2619
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2620
    }
2621

2622
    if (pUser->pIpWhiteListDual->num > 0) {
2!
2623
      int idx = 0;
2✔
2624
      for (int i = 0; i < pUser->pIpWhiteListDual->num; i++) {
7✔
2625
        SIpRange *oldRange = &pUser->pIpWhiteListDual->pIpRanges[i];
5✔
2626

2627
        bool found = false;
5✔
2628
        for (int j = 0; j < alterReq.numIpRanges; j++) {
10✔
2629
          SIpRange range = {0};
5✔
2630
          if (alterReq.pIpDualRanges == NULL) {
5!
2631
            SIpV4Range *trange = &alterReq.pIpRanges[j];
×
2632
            memcpy(&range.ipV4, trange, sizeof(SIpV4Range));
×
2633
          } else {
2634
            memcpy(&range, &alterReq.pIpDualRanges[j], sizeof(SIpRange));
5✔
2635
          }
2636

2637
          if (isDefaultRange(&range)) {
5!
2638
            localHost = true;
×
2639
            break;
×
2640
          }
2641
          if (isIpRangeEqual(oldRange, &range)) {
5!
2642
            found = true;
×
2643
            break;
×
2644
          }
2645
        }
2646
        if (localHost) break;
5!
2647

2648
        if (found == false) {
5!
2649
          (void)memcpy(&pNew->pIpRanges[idx], oldRange, sizeof(SIpRange));
5✔
2650
          idx++;
5✔
2651
        } else {
2652
          noexist = false;
×
2653
        }
2654
      }
2655
      pNew->num = idx;
2✔
2656
      newUser.pIpWhiteListDual = pNew;
2✔
2657
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
2✔
2658

2659
    } else {
2660
      pNew->num = 0;
×
2661
      newUser.pIpWhiteListDual = pNew;
×
2662
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
×
2663
    }
2664

2665
    if (localHost) {
2!
2666
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
×
2667
    }
2668
    if (noexist) {
2!
2669
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
2!
2670
    }
2671
  }
2672

2673
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
267✔
2674
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
267!
2675

2676
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
267✔
2677
    char detail[1000] = {0};
36✔
2678
    (void)tsnprintf(detail, sizeof(detail),
36✔
2679
                    "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx",
2680
                    mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
36✔
2681
                    alterReq.createdb ? 1 : 0, alterReq.tabName);
36✔
2682
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
36✔
2683
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
231!
2684
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
222✔
2685
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
34✔
2686
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
197✔
2687
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
151✔
2688
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
138!
2689
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
138!
2690
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
96!
2691
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
73!
2692
    if (strcmp(alterReq.objname, "1.*") != 0) {
124✔
2693
      SName name = {0};
120✔
2694
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
120!
2695
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
120✔
2696
                  alterReq.sqlLen);
2697
    } else {
2698
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
4✔
2699
    }
2700
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
73✔
2701
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
15✔
2702
                alterReq.sqlLen);
2703
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
58✔
2704
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
10✔
2705
                alterReq.sqlLen);
2706
  } else {
2707
    if (strcmp(alterReq.objname, "1.*") != 0) {
48✔
2708
      SName name = {0};
44✔
2709
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
44✔
2710
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
42✔
2711
                  alterReq.sqlLen);
2712
    } else {
2713
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
4✔
2714
    }
2715
  }
2716

2717
_OVER:
371✔
2718
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
371✔
2719
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
106!
2720
  }
2721

2722
  tFreeSAlterUserReq(&alterReq);
371✔
2723
  mndReleaseUser(pMnode, pOperUser);
371✔
2724
  mndReleaseUser(pMnode, pUser);
371✔
2725
  mndUserFreeObj(&newUser);
371✔
2726

2727
  TAOS_RETURN(code);
371✔
2728
}
2729

2730
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
59✔
2731
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
59✔
2732
  if (pTrans == NULL) {
59!
2733
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
2734
    TAOS_RETURN(terrno);
×
2735
  }
2736
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
59!
2737

2738
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
59✔
2739
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
59!
2740
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2741
    mndTransDrop(pTrans);
×
2742
    TAOS_RETURN(terrno);
×
2743
  }
2744
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
59!
2745
    mndTransDrop(pTrans);
×
2746
    TAOS_RETURN(terrno);
×
2747
  }
2748

2749
  if (mndTransPrepare(pMnode, pTrans) != 0) {
59!
2750
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2751
    mndTransDrop(pTrans);
×
2752
    TAOS_RETURN(terrno);
×
2753
  }
2754
  (void)ipWhiteMgtRemove(pUser->user);
59✔
2755

2756
  mndTransDrop(pTrans);
59✔
2757
  TAOS_RETURN(0);
59✔
2758
}
2759

2760
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
59✔
2761
  SMnode      *pMnode = pReq->info.node;
59✔
2762
  int32_t      code = 0;
59✔
2763
  int32_t      lino = 0;
59✔
2764
  SUserObj    *pUser = NULL;
59✔
2765
  SDropUserReq dropReq = {0};
59✔
2766

2767
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
59!
2768

2769
  mInfo("user:%s, start to drop", dropReq.user);
59!
2770
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
59!
2771

2772
  if (dropReq.user[0] == 0) {
59!
2773
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2774
  }
2775

2776
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
59!
2777

2778
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
59!
2779
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
59!
2780

2781
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
59✔
2782

2783
_OVER:
59✔
2784
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
59!
2785
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
×
2786
  }
2787

2788
  mndReleaseUser(pMnode, pUser);
59✔
2789
  tFreeSDropUserReq(&dropReq);
59✔
2790
  TAOS_RETURN(code);
59✔
2791
}
2792

2793
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
410,138✔
2794
  SMnode         *pMnode = pReq->info.node;
410,138✔
2795
  int32_t         code = 0;
410,138✔
2796
  int32_t         lino = 0;
410,138✔
2797
  int32_t         contLen = 0;
410,138✔
2798
  void           *pRsp = NULL;
410,138✔
2799
  SUserObj       *pUser = NULL;
410,138✔
2800
  SGetUserAuthReq authReq = {0};
410,138✔
2801
  SGetUserAuthRsp authRsp = {0};
410,138✔
2802

2803
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
410,138!
2804
  mTrace("user:%s, start to get auth", authReq.user);
410,145✔
2805

2806
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
410,145✔
2807

2808
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
410,140!
2809

2810
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
410,142✔
2811
  if (contLen < 0) {
410,120!
2812
    TAOS_CHECK_EXIT(contLen);
×
2813
  }
2814
  pRsp = rpcMallocCont(contLen);
410,120✔
2815
  if (pRsp == NULL) {
410,126!
2816
    TAOS_CHECK_EXIT(terrno);
×
2817
  }
2818

2819
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
410,126✔
2820
  if (contLen < 0) {
410,130!
2821
    TAOS_CHECK_EXIT(contLen);
×
2822
  }
2823

2824
_exit:
410,130✔
2825
  mndReleaseUser(pMnode, pUser);
410,134✔
2826
  tFreeSGetUserAuthRsp(&authRsp);
410,146✔
2827
  if (code < 0) {
410,145✔
2828
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
4!
2829
    rpcFreeCont(pRsp);
4✔
2830
    pRsp = NULL;
4✔
2831
    contLen = 0;
4✔
2832
  }
2833
  pReq->info.rsp = pRsp;
410,145✔
2834
  pReq->info.rspLen = contLen;
410,145✔
2835
  pReq->code = code;
410,145✔
2836

2837
  TAOS_RETURN(code);
410,145✔
2838
}
2839

2840
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
5,109✔
2841
  SMnode   *pMnode = pReq->info.node;
5,109✔
2842
  SSdb     *pSdb = pMnode->pSdb;
5,109✔
2843
  int32_t   code = 0;
5,109✔
2844
  int32_t   lino = 0;
5,109✔
2845
  int32_t   numOfRows = 0;
5,109✔
2846
  SUserObj *pUser = NULL;
5,109✔
2847
  int32_t   cols = 0;
5,109✔
2848
  int8_t    flag = 0;
5,109✔
2849
  char     *pWrite = NULL;
5,109✔
2850
  char     *buf = NULL;
5,109✔
2851
  char     *varstr = NULL;
5,109✔
2852

2853
  while (numOfRows < rows) {
10,303!
2854
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
10,309✔
2855
    if (pShow->pIter == NULL) break;
10,314✔
2856

2857
    cols = 0;
5,194✔
2858
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,194✔
2859
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5,185✔
2860
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5,185✔
2861
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
5,185!
2862

2863
    cols++;
5,184✔
2864
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,184✔
2865
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
5,175!
2866

2867
    cols++;
5,174✔
2868
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,174✔
2869
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
5,161!
2870

2871
    cols++;
5,166✔
2872
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,166✔
2873
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
5,157!
2874

2875
    cols++;
5,171✔
2876
    flag = pUser->createdb ? 1 : 0;
5,171✔
2877
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,171✔
2878
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
5,169!
2879

2880
    cols++;
5,174✔
2881
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,174✔
2882
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, _exit);
5,165!
2883

2884
    cols++;
5,175✔
2885

2886
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteListDual, &buf);
5,175✔
2887
    if (tlen != 0) {
5,184!
2888
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
5,186!
2889
      if (varstr == NULL) {
5,191!
2890
        sdbRelease(pSdb, pUser);
×
2891
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
2892
      }
2893
      varDataSetLen(varstr, tlen);
5,191✔
2894
      (void)memcpy(varDataVal(varstr), buf, tlen);
5,191✔
2895

2896
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,191✔
2897
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
5,166!
2898

2899
      taosMemoryFreeClear(buf);
5,177!
2900
    } else {
2901
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2902
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2903
    }
2904

2905
    numOfRows++;
5,189✔
2906
    sdbRelease(pSdb, pUser);
5,189✔
2907
  }
2908

2909
  pShow->numOfRows += numOfRows;
5,114✔
2910
_exit:
5,114✔
2911
  taosMemoryFreeClear(buf);
5,114!
2912
  taosMemoryFreeClear(varstr);
5,114!
2913
  if (code < 0) {
5,114!
2914
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2915
    TAOS_RETURN(code);
×
2916
  }
2917
  return numOfRows;
5,114✔
2918
}
2919

2920
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2921
  int32_t numOfRows = 0;
×
2922
#ifdef TD_ENTERPRISE
2923
  SMnode   *pMnode = pReq->info.node;
×
2924
  SSdb     *pSdb = pMnode->pSdb;
×
2925
  SUserObj *pUser = NULL;
×
2926
  int32_t   code = 0;
×
2927
  int32_t   lino = 0;
×
2928
  int32_t   cols = 0;
×
2929
  int8_t    flag = 0;
×
2930
  char     *pWrite = NULL;
×
2931
  char     *buf = NULL;
×
2932
  char     *varstr = NULL;
×
2933

2934
  while (numOfRows < rows) {
×
2935
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
2936
    if (pShow->pIter == NULL) break;
×
2937

2938
    cols = 0;
×
2939
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2940
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
2941
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
2942
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
×
2943

2944
    cols++;
×
2945
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2946
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
×
2947

2948
    cols++;
×
2949
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2950
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
×
2951

2952
    cols++;
×
2953
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2954
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
×
2955

2956
    cols++;
×
2957
    flag = pUser->createdb ? 1 : 0;
×
2958
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2959
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
×
2960

2961
    // mInfo("pUser->pass:%s", pUser->pass);
2962
    cols++;
×
2963
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2964
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
×
2965
    STR_WITH_MAXSIZE_TO_VARSTR(pass, pUser->pass, pShow->pMeta->pSchemas[cols].bytes);
×
2966
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, _exit);
×
2967

2968
    cols++;
×
2969

2970
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteListDual, &buf);
×
2971
    if (tlen != 0) {
×
2972
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
2973
      if (varstr == NULL) {
×
2974
        sdbRelease(pSdb, pUser);
×
2975
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
2976
      }
2977
      varDataSetLen(varstr, tlen);
×
2978
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
2979

2980
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2981
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
×
2982

2983
      taosMemoryFreeClear(buf);
×
2984
    } else {
2985
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2986
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2987
    }
2988

2989
    numOfRows++;
×
2990
    sdbRelease(pSdb, pUser);
×
2991
  }
2992

2993
  pShow->numOfRows += numOfRows;
×
2994
_exit:
×
2995
  taosMemoryFreeClear(buf);
×
2996
  taosMemoryFreeClear(varstr);
×
2997
  if (code < 0) {
×
2998
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2999
    TAOS_RETURN(code);
×
3000
  }
3001
#endif
3002
  return numOfRows;
×
3003
}
3004

3005
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
3006
  SSdb *pSdb = pMnode->pSdb;
×
3007
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
3008
}
×
3009

3010
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
26,420✔
3011
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
3012
  char   *value = taosHashIterate(hash, NULL);
26,420✔
3013
  char   *user = pUser->user;
26,441✔
3014
  int32_t code = 0;
26,441✔
3015
  int32_t lino = 0;
26,441✔
3016
  int32_t cols = 0;
26,441✔
3017
  int32_t numOfRows = *pNumOfRows;
26,441✔
3018

3019
  while (value != NULL) {
26,445✔
3020
    cols = 0;
4✔
3021
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
3022
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
4✔
3023
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3024
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, _exit);
4!
3025

3026
    char privilege[20] = {0};
4✔
3027
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
4✔
3028
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3029
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, _exit);
4!
3030

3031
    size_t keyLen = 0;
4✔
3032
    void  *key = taosHashGetKey(value, &keyLen);
4✔
3033

3034
    char dbName[TSDB_DB_NAME_LEN] = {0};
4✔
3035
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
4✔
3036
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
3037
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
4✔
3038
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3039
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, _exit);
4!
3040

3041
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
4✔
3042
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
4✔
3043
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
3044
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
4✔
3045
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3046
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, _exit);
4!
3047

3048
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
4!
3049
      SNode  *pAst = NULL;
×
3050
      int32_t sqlLen = 0;
×
3051
      size_t  bufSz = strlen(value) + 1;
×
3052
      if (bufSz < 6) bufSz = 6;
×
3053
      TAOS_MEMORY_REALLOC(*sql, bufSz);
×
3054
      if (*sql == NULL) {
×
3055
        code = terrno;
×
3056
        goto _exit;
×
3057
      }
3058
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
3059
      if ((*condition) == NULL) {
×
3060
        code = terrno;
×
3061
        goto _exit;
×
3062
      }
3063

3064
      if (nodesStringToNode(value, &pAst) == 0) {
×
3065
        if (nodesNodeToSQLFormat(pAst, *sql, bufSz, &sqlLen, true) != 0) {
×
3066
          sqlLen = tsnprintf(*sql, bufSz, "error");
×
3067
        }
3068
        nodesDestroyNode(pAst);
×
3069
      }
3070

3071
      if (sqlLen == 0) {
×
3072
        sqlLen = tsnprintf(*sql, bufSz, "error");
×
3073
      }
3074

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

3077
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
3078
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
×
3079

3080
      char notes[2] = {0};
×
3081
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
3082
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
3083
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
×
3084
    } else {
3085
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
4!
3086
      if ((*condition) == NULL) {
4!
3087
        code = terrno;
×
3088
        goto _exit;
×
3089
      }
3090
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
4✔
3091
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3092
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
4!
3093

3094
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
4✔
3095
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
4!
3096
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
3097
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
4!
3098
    }
3099

3100
    numOfRows++;
4✔
3101
    value = taosHashIterate(hash, value);
4✔
3102
  }
3103
  *pNumOfRows = numOfRows;
26,441✔
3104
_exit:
26,441✔
3105
  if (code < 0) {
26,441!
3106
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3107
    sdbRelease(pSdb, pUser);
×
3108
  }
3109
  TAOS_RETURN(code);
26,441✔
3110
}
3111

3112
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
4,342✔
3113
  int32_t   code = 0;
4,342✔
3114
  int32_t   lino = 0;
4,342✔
3115
  SMnode   *pMnode = pReq->info.node;
4,342✔
3116
  SSdb     *pSdb = pMnode->pSdb;
4,342✔
3117
  int32_t   numOfRows = 0;
4,342✔
3118
  SUserObj *pUser = NULL;
4,342✔
3119
  int32_t   cols = 0;
4,342✔
3120
  char     *pWrite = NULL;
4,342✔
3121
  char     *condition = NULL;
4,342✔
3122
  char     *sql = NULL;
4,342✔
3123

3124
  bool fetchNextUser = pShow->restore ? false : true;
4,342✔
3125
  pShow->restore = false;
4,342✔
3126

3127
  while (numOfRows < rows) {
8,761!
3128
    if (fetchNextUser) {
8,761!
3129
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
8,761✔
3130
      if (pShow->pIter == NULL) break;
8,766✔
3131
    } else {
3132
      fetchNextUser = true;
×
3133
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
3134
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
3135
      if (!pUser) {
×
3136
        continue;
×
3137
      }
3138
    }
3139

3140
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
4,419✔
3141
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
4,417✔
3142
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
4,416✔
3143
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
4,414✔
3144
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
4,414✔
3145
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
4,414✔
3146
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
4,414✔
3147
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
4,414✔
3148
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
4,414✔
3149
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
4,411✔
3150
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
4,411!
3151
        rows) {
3152
      mInfo(
×
3153
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
3154
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
3155
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
3156
          numOfReadViews, numOfWriteViews, numOfAlterViews);
3157
      pShow->restore = true;
×
3158
      sdbRelease(pSdb, pUser);
×
3159
      break;
×
3160
    }
3161

3162
    if (pUser->superUser) {
4,411✔
3163
      cols = 0;
4,340✔
3164
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
4,340✔
3165
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
4,340✔
3166
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,340✔
3167
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
4,316!
3168

3169
      char privilege[20] = {0};
4,329✔
3170
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
4,329✔
3171
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,329✔
3172
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
4,321!
3173

3174
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4,335✔
3175
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
4,335✔
3176
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,335✔
3177
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
4,327!
3178

3179
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4,338✔
3180
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
4,338✔
3181
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,338✔
3182
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
4,328!
3183

3184
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
4,336!
3185
      if (condition == NULL) {
4,344!
3186
        sdbRelease(pSdb, pUser);
×
3187
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3188
      }
3189
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
4,344✔
3190
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,344✔
3191
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
4,331!
3192

3193
      char notes[2] = {0};
4,334✔
3194
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
4,334✔
3195
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,334✔
3196
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
4,321!
3197

3198
      numOfRows++;
4,338✔
3199
    }
3200

3201
    char *db = taosHashIterate(pUser->readDbs, NULL);
4,409✔
3202
    while (db != NULL) {
4,429✔
3203
      cols = 0;
19✔
3204
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3205
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19✔
3206
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3207
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
19!
3208

3209
      char privilege[20] = {0};
19✔
3210
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
19✔
3211
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3212
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
19!
3213

3214
      SName name = {0};
19✔
3215
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3216
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19✔
3217
      if (code < 0) {
19!
3218
        sdbRelease(pSdb, pUser);
×
3219
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3220
      }
3221
      (void)tNameGetDbName(&name, varDataVal(objName));
19✔
3222
      varDataSetLen(objName, strlen(varDataVal(objName)));
19✔
3223
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3224
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
19!
3225

3226
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3227
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3228
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3229
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
19!
3230

3231
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19!
3232
      if (condition == NULL) {
19!
3233
        sdbRelease(pSdb, pUser);
×
3234
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3235
      }
3236
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3237
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3238
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
19!
3239

3240
      char notes[2] = {0};
19✔
3241
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19✔
3242
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3243
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
19!
3244

3245
      numOfRows++;
19✔
3246
      db = taosHashIterate(pUser->readDbs, db);
19✔
3247
    }
3248

3249
    db = taosHashIterate(pUser->writeDbs, NULL);
4,410✔
3250
    while (db != NULL) {
4,426✔
3251
      cols = 0;
19✔
3252
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3253
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19✔
3254
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3255
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
19!
3256

3257
      char privilege[20] = {0};
19✔
3258
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
19✔
3259
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3260
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
19!
3261

3262
      SName name = {0};
19✔
3263
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3264
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19✔
3265
      if (code < 0) {
19!
3266
        sdbRelease(pSdb, pUser);
×
3267
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3268
      }
3269
      (void)tNameGetDbName(&name, varDataVal(objName));
19✔
3270
      varDataSetLen(objName, strlen(varDataVal(objName)));
19✔
3271
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3272
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
19!
3273

3274
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3275
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3276
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3277
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
19!
3278

3279
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19!
3280
      if (condition == NULL) {
19!
3281
        sdbRelease(pSdb, pUser);
×
3282
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3283
      }
3284
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3285
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3286
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
19!
3287

3288
      char notes[2] = {0};
19✔
3289
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19✔
3290
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3291
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
19!
3292

3293
      numOfRows++;
19✔
3294
      db = taosHashIterate(pUser->writeDbs, db);
19✔
3295
    }
3296

3297
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,407!
3298

3299
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,407!
3300

3301
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,415!
3302

3303
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,414!
3304

3305
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,411!
3306

3307
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,415!
3308

3309
    char *topic = taosHashIterate(pUser->topics, NULL);
4,413✔
3310
    while (topic != NULL) {
4,421✔
3311
      cols = 0;
8✔
3312
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
8✔
3313
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
8✔
3314
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3315
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
8!
3316

3317
      char privilege[20] = {0};
8✔
3318
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
8✔
3319
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3320
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
8!
3321

3322
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
8✔
3323
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
8✔
3324
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
8✔
3325
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3326
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, _exit);
8!
3327

3328
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
8✔
3329
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
8✔
3330
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3331
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
8!
3332

3333
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
8!
3334
      if (condition == NULL) {
8!
3335
        sdbRelease(pSdb, pUser);
×
3336
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3337
      }
3338
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
8✔
3339
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3340
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
8!
3341

3342
      char notes[2] = {0};
8✔
3343
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
8✔
3344
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3345
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
8!
3346

3347
      numOfRows++;
8✔
3348
      topic = taosHashIterate(pUser->topics, topic);
8✔
3349
    }
3350

3351
    sdbRelease(pSdb, pUser);
4,413✔
3352
  }
3353

3354
  pShow->numOfRows += numOfRows;
4,347✔
3355
_exit:
4,347✔
3356
  taosMemoryFreeClear(condition);
4,347!
3357
  taosMemoryFreeClear(sql);
4,347!
3358
  if (code < 0) {
4,347!
3359
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3360
    TAOS_RETURN(code);
×
3361
  }
3362
  return numOfRows;
4,347✔
3363
}
3364

3365
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
3366
  SSdb *pSdb = pMnode->pSdb;
×
3367
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
3368
}
×
3369

3370
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
147,357✔
3371
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
3372
  int32_t           code = 0;
147,357✔
3373
  int32_t           lino = 0;
147,357✔
3374
  int32_t           rspLen = 0;
147,357✔
3375
  void             *pRsp = NULL;
147,357✔
3376
  SUserAuthBatchRsp batchRsp = {0};
147,357✔
3377

3378
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
147,357✔
3379
  if (batchRsp.pArray == NULL) {
147,358!
3380
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3381
  }
3382

3383
  for (int32_t i = 0; i < numOfUses; ++i) {
294,950✔
3384
    SUserObj *pUser = NULL;
147,583✔
3385
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
147,583✔
3386
    if (pUser == NULL) {
147,590✔
3387
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
24!
3388
        SGetUserAuthRsp rsp = {.dropped = 1};
24✔
3389
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
24✔
3390
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
48!
3391
      }
3392
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
24!
3393
      code = 0;
24✔
3394
      continue;
134,033✔
3395
    }
3396

3397
    pUsers[i].version = ntohl(pUsers[i].version);
147,566✔
3398
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
147,566✔
3399
      mndReleaseUser(pMnode, pUser);
134,007✔
3400
      continue;
134,009✔
3401
    }
3402

3403
    SGetUserAuthRsp rsp = {0};
13,559✔
3404
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
13,559✔
3405
    if (code) {
13,559!
3406
      mndReleaseUser(pMnode, pUser);
×
3407
      tFreeSGetUserAuthRsp(&rsp);
×
3408
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3409
    }
3410

3411
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
27,118!
3412
      code = terrno;
×
3413
      mndReleaseUser(pMnode, pUser);
×
3414
      tFreeSGetUserAuthRsp(&rsp);
×
3415
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3416
    }
3417
    mndReleaseUser(pMnode, pUser);
13,559✔
3418
  }
3419

3420
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
147,367✔
3421
    *ppRsp = NULL;
133,978✔
3422
    *pRspLen = 0;
133,978✔
3423

3424
    tFreeSUserAuthBatchRsp(&batchRsp);
133,978✔
3425
    return 0;
133,978✔
3426
  }
3427

3428
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
13,386✔
3429
  if (rspLen < 0) {
13,386!
3430
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3431
  }
3432
  pRsp = taosMemoryMalloc(rspLen);
13,386!
3433
  if (pRsp == NULL) {
13,386!
3434
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3435
  }
3436
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
13,386✔
3437
  if (rspLen < 0) {
13,385!
3438
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3439
  }
3440
_OVER:
13,385✔
3441
  tFreeSUserAuthBatchRsp(&batchRsp);
13,385✔
3442
  if (code < 0) {
13,386!
3443
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3444
    taosMemoryFreeClear(pRsp);
×
3445
    rspLen = 0;
×
3446
  }
3447
  *ppRsp = pRsp;
13,386✔
3448
  *pRspLen = rspLen;
13,386✔
3449

3450
  TAOS_RETURN(code);
13,386✔
3451
}
3452

3453
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
2,334✔
3454
  int32_t   code = 0;
2,334✔
3455
  int32_t   lino = 0;
2,334✔
3456
  SSdb     *pSdb = pMnode->pSdb;
2,334✔
3457
  int32_t   len = strlen(db) + 1;
2,334✔
3458
  void     *pIter = NULL;
2,334✔
3459
  SUserObj *pUser = NULL;
2,334✔
3460
  SUserObj  newUser = {0};
2,334✔
3461

3462
  while (1) {
2,527✔
3463
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
4,861✔
3464
    if (pIter == NULL) break;
4,861✔
3465

3466
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
2,527!
3467
      break;
×
3468
    }
3469

3470
    bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
2,527✔
3471
    bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
2,527✔
3472
    if (inRead || inWrite) {
2,527!
3473
      code = taosHashRemove(newUser.readDbs, db, len);
2✔
3474
      if (code < 0) {
2!
3475
        mError("failed to remove readDbs:%s from user:%s", db, pUser->user);
×
3476
      }
3477
      code = taosHashRemove(newUser.writeDbs, db, len);
2✔
3478
      if (code < 0) {
2!
3479
        mError("failed to remove writeDbs:%s from user:%s", db, pUser->user);
×
3480
      }
3481

3482
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
2✔
3483
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
2!
3484
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3485
        break;
×
3486
      }
3487
      TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
2!
3488
    }
3489

3490
    mndUserFreeObj(&newUser);
2,527✔
3491
    sdbRelease(pSdb, pUser);
2,527✔
3492
  }
3493

3494
_OVER:
2,334✔
3495
  if (pUser != NULL) sdbRelease(pSdb, pUser);
2,334!
3496
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
2,334!
3497
  mndUserFreeObj(&newUser);
2,334✔
3498
  TAOS_RETURN(code);
2,334✔
3499
}
3500

3501
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
5,621✔
3502
  int32_t   code = 0;
5,621✔
3503
  SSdb     *pSdb = pMnode->pSdb;
5,621✔
3504
  int32_t   len = strlen(stb) + 1;
5,621✔
3505
  void     *pIter = NULL;
5,621✔
3506
  SUserObj *pUser = NULL;
5,621✔
3507
  SUserObj  newUser = {0};
5,621✔
3508

3509
  while (1) {
5,808✔
3510
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
11,429✔
3511
    if (pIter == NULL) break;
11,429✔
3512

3513
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
5,808!
3514
      break;
×
3515
    }
3516

3517
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
5,808✔
3518
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
5,808✔
3519
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
5,808✔
3520
    if (inRead || inWrite || inAlter) {
5,808!
3521
      code = taosHashRemove(newUser.readTbs, stb, len);
×
3522
      if (code < 0) {
×
3523
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
3524
      }
3525
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
3526
      if (code < 0) {
×
3527
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
3528
      }
3529
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
3530
      if (code < 0) {
×
3531
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
3532
      }
3533

3534
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3535
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3536
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3537
        break;
×
3538
      }
3539
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3540
      if (code != 0) {
×
3541
        mndUserFreeObj(&newUser);
×
3542
        sdbRelease(pSdb, pUser);
×
3543
        TAOS_RETURN(code);
×
3544
      }
3545
    }
3546

3547
    mndUserFreeObj(&newUser);
5,808✔
3548
    sdbRelease(pSdb, pUser);
5,808✔
3549
  }
3550

3551
  if (pUser != NULL) sdbRelease(pSdb, pUser);
5,621!
3552
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
5,621!
3553
  mndUserFreeObj(&newUser);
5,621✔
3554
  TAOS_RETURN(code);
5,621✔
3555
}
3556

3557
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
229✔
3558
  int32_t   code = 0;
229✔
3559
  SSdb     *pSdb = pMnode->pSdb;
229✔
3560
  int32_t   len = strlen(view) + 1;
229✔
3561
  void     *pIter = NULL;
229✔
3562
  SUserObj *pUser = NULL;
229✔
3563
  SUserObj  newUser = {0};
229✔
3564

3565
  while (1) {
241✔
3566
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
470✔
3567
    if (pIter == NULL) break;
470✔
3568

3569
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
241!
3570
      break;
×
3571
    }
3572

3573
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
241✔
3574
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
241✔
3575
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
241✔
3576
    if (inRead || inWrite || inAlter) {
241!
3577
      code = taosHashRemove(newUser.readViews, view, len);
4✔
3578
      if (code < 0) {
4!
3579
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
×
3580
      }
3581
      code = taosHashRemove(newUser.writeViews, view, len);
4✔
3582
      if (code < 0) {
4!
3583
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
4!
3584
      }
3585
      code = taosHashRemove(newUser.alterViews, view, len);
4✔
3586
      if (code < 0) {
4!
3587
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
4!
3588
      }
3589

3590
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
4✔
3591
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
4!
3592
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3593
        break;
×
3594
      }
3595
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
4✔
3596
      if (code < 0) {
4!
3597
        mndUserFreeObj(&newUser);
×
3598
        sdbRelease(pSdb, pUser);
×
3599
        TAOS_RETURN(code);
×
3600
      }
3601
    }
3602

3603
    mndUserFreeObj(&newUser);
241✔
3604
    sdbRelease(pSdb, pUser);
241✔
3605
  }
3606

3607
  if (pUser != NULL) sdbRelease(pSdb, pUser);
229!
3608
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
229!
3609
  mndUserFreeObj(&newUser);
229✔
3610
  TAOS_RETURN(code);
229✔
3611
}
3612

3613
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
327✔
3614
  int32_t   code = 0;
327✔
3615
  SSdb     *pSdb = pMnode->pSdb;
327✔
3616
  int32_t   len = strlen(topic) + 1;
327✔
3617
  void     *pIter = NULL;
327✔
3618
  SUserObj *pUser = NULL;
327✔
3619
  SUserObj  newUser = {0};
327✔
3620

3621
  while (1) {
330✔
3622
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
657✔
3623
    if (pIter == NULL) {
657✔
3624
      break;
327✔
3625
    }
3626

3627
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
330!
3628
      break;
×
3629
    }
3630

3631
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
330✔
3632
    if (inTopic) {
330!
3633
      code = taosHashRemove(newUser.topics, topic, len);
×
3634
      if (code < 0) {
×
3635
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
3636
      }
3637
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3638
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3639
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3640
        break;
×
3641
      }
3642
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3643
      if (code < 0) {
×
3644
        mndUserFreeObj(&newUser);
×
3645
        sdbRelease(pSdb, pUser);
×
3646
        TAOS_RETURN(code);
×
3647
      }
3648
    }
3649

3650
    mndUserFreeObj(&newUser);
330✔
3651
    sdbRelease(pSdb, pUser);
330✔
3652
  }
3653

3654
  if (pUser != NULL) sdbRelease(pSdb, pUser);
327!
3655
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
327!
3656
  mndUserFreeObj(&newUser);
327✔
3657
  TAOS_RETURN(code);
327✔
3658
}
3659

3660
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
3661
  // ver = 0, disable ip white list
3662
  // ver > 0, enable ip white list
3663
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
3664
}
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