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

taosdata / TDengine / #3778

28 Mar 2025 06:51AM UTC coverage: 63.374% (+0.4%) from 62.934%
#3778

push

travis-ci

web-flow
fix(tdb): disable page recycling (#30529)

155771 of 313582 branches covered (49.67%)

Branch coverage included in aggregate %.

241569 of 313390 relevant lines covered (77.08%)

20705705.27 hits per line

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

59.34
/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   6
34
#define USER_RESERVE_SIZE 64
35

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

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

46
#define ALTER_USER_ADD_PRIVS(_type) ((_type) == TSDB_ALTER_USER_ADD_PRIVILEGES)
47
#define ALTER_USER_DEL_PRIVS(_type) ((_type) == TSDB_ALTER_USER_DEL_PRIVILEGES)
48

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

58
#define ALTER_USER_TARGET_DB(_tbname) (0 == (_tbname)[0])
59
#define ALTER_USER_TARGET_TB(_tbname) (0 != (_tbname)[0])
60

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

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

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

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

106
void destroyIpWhiteTab(SHashObj *pIpWhiteTab);
107

108
#define MND_MAX_USE_HOST (TSDB_PRIVILEDGE_HOST_LEN / 24)
109

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

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

137
static SIpWhiteMgt ipWhiteMgt;
138

139
const static SIpV4Range defaultIpRange = {.ip = 16777343, .mask = 32};
140

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

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

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

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

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

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

214
  if (update) ipWhiteMgt.ver++;
387✔
215

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

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

244
bool isRangeInWhiteList(SIpWhiteList *pList, SIpV4Range *range) {
5,400✔
245
  for (int i = 0; i < pList->num; i++) {
6,991✔
246
    if (isIpRangeEqual(&pList->pIpRange[i], range)) {
6,658✔
247
      return true;
5,067✔
248
    }
249
  }
250
  return false;
333✔
251
}
252
#if 0
253
int32_t ipWhiteUpdateForAllUser(SIpWhiteList *pList) {
254
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
255

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

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

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

280
    } else {
281
      val = cloneIpWhiteList(p);
282
    }
283
    taosMemoryFree(clone);
284

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

290
  destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
291

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

299
static int32_t ipWhiteMgtUpdateAll(SMnode *pMnode) {
2,189✔
300
  SHashObj *pNew = NULL;
2,189✔
301
  TAOS_CHECK_RETURN(mndFetchAllIpWhite(pMnode, &pNew));
2,189✔
302

303
  SHashObj *pOld = ipWhiteMgt.pIpWhiteTab;
2,188✔
304

305
  ipWhiteMgt.pIpWhiteTab = pNew;
2,188✔
306
  ipWhiteMgt.ver++;
2,188✔
307

308
  destroyIpWhiteTab(pOld);
2,188✔
309
  TAOS_RETURN(0);
2,188✔
310
}
311

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

316
  ipWhiteMgtUpdateAll(pMnode);
317

318
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
319
}
320
#endif
321

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

338
  if (mndEnableIpWhiteList(pMnode) == 0 || tsEnableWhiteList == false) {
193,952!
339
    ver = 0;
193,940✔
340
  }
341
  mDebug("ip-white-list on mnode ver: %" PRId64, ver);
193,952✔
342
  return ver;
193,952✔
343
}
344

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

361
  if (type == IP_WHITE_ADD) {
6,847✔
362
    if (pList == NULL) {
6,826✔
363
      SIpWhiteList *pNewList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range));
1,445!
364
      if (pNewList == NULL) {
1,445!
365
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
366
      }
367
      (void)memcpy(pNewList->pIpRange, &range, sizeof(SIpV4Range));
1,445✔
368
      pNewList->num = 1;
1,445✔
369

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

386
        pNewList->num = pList->num + 1;
333✔
387

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

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

443
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
2,189✔
444
  int32_t code = 0;
2,189✔
445
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2,189✔
446

447
  if ((code = ipWhiteMgtUpdateAll(pMnode)) != 0) {
2,189✔
448
    (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
1✔
449
    TAOS_RETURN(code);
1✔
450
  }
451
  ipWhiteMgt.ver = taosGetTimestampMs();
2,188✔
452
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
2,188✔
453

454
  TAOS_RETURN(code);
2,188✔
455
}
456

457
int32_t mndUpdateIpWhiteForAllUser(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock) {
1,917✔
458
  int32_t code = 0;
1,917✔
459
  int32_t lino = 0;
1,917✔
460
  bool    update = false;
1,917✔
461

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

471
  TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, type, &update), &lino, _OVER);
1,917!
472

473
  void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
1,917✔
474
  while (pIter) {
3,839✔
475
    size_t klen = 0;
1,922✔
476
    char  *key = taosHashGetKey(pIter, &klen);
1,922✔
477

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

492
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
1,922✔
493
  }
494

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

503
  TAOS_RETURN(code);
1,917✔
504
}
505

506
static int64_t ipWhiteMgtFillMsg(SUpdateIpWhite *pUpdate) {
6✔
507
  int64_t ver = 0;
6✔
508
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
6✔
509
  ver = ipWhiteMgt.ver;
6✔
510
  int32_t num = taosHashGetSize(ipWhiteMgt.pIpWhiteTab);
6✔
511

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

518
  void   *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
6✔
519
  int32_t i = 0;
6✔
520
  while (pIter) {
12✔
521
    SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[i];
6✔
522
    SIpWhiteList       *list = *(SIpWhiteList **)pIter;
6✔
523

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

543
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
6✔
544
  TAOS_RETURN(0);
6✔
545
}
546

547
void destroyIpWhiteTab(SHashObj *pIpWhiteTab) {
4,127✔
548
  if (pIpWhiteTab == NULL) return;
4,127!
549

550
  void *pIter = taosHashIterate(pIpWhiteTab, NULL);
4,127✔
551
  while (pIter) {
6,313✔
552
    SIpWhiteList *list = *(SIpWhiteList **)pIter;
2,186✔
553
    taosMemoryFree(list);
2,186!
554
    pIter = taosHashIterate(pIpWhiteTab, pIter);
2,186✔
555
  }
556

557
  taosHashCleanup(pIpWhiteTab);
4,127✔
558
}
559
int32_t mndFetchAllIpWhite(SMnode *pMnode, SHashObj **ppIpWhiteTab) {
2,189✔
560
  int32_t   code = 0;
2,189✔
561
  int32_t   lino = 0;
2,189✔
562
  SSdb     *pSdb = pMnode->pSdb;
2,189✔
563
  void     *pIter = NULL;
2,189✔
564
  SHashObj *pIpWhiteTab = NULL;
2,189✔
565
  SArray   *pUserNames = NULL;
2,189✔
566
  SArray   *fqdns = NULL;
2,189✔
567

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

577
  while (1) {
661✔
578
    SUserObj *pUser = NULL;
2,850✔
579
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
2,850✔
580
    if (pIter == NULL) break;
2,850✔
581

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

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

608
    sdbRelease(pSdb, pUser);
661✔
609
  }
610

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

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

635
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
3,991✔
636
    char *fqdn = taosArrayGetP(fqdns, i);
1,803✔
637

638
    for (int j = 0; j < taosArrayGetSize(pUserNames); j++) {
3,625✔
639
      char *name = taosArrayGetP(pUserNames, j);
1,823✔
640
      TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(pIpWhiteTab, name, fqdn, IP_WHITE_ADD, NULL), &lino, _OVER);
1,823✔
641
    }
642
  }
643

644
_OVER:
2,188✔
645
  taosArrayDestroyP(fqdns, NULL);
2,189✔
646
  taosArrayDestroyP(pUserNames, NULL);
2,189✔
647

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

657
int32_t mndInitUser(SMnode *pMnode) {
1,939✔
658
  TAOS_CHECK_RETURN(ipWhiteMgtInit());
1,939!
659

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

671
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
1,939✔
672
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
1,939✔
673
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
1,939✔
674
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
1,939✔
675
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST, mndProcessGetUserWhiteListReq);
1,939✔
676

677
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE, mndProcesSRetrieveIpWhiteReq);
1,939✔
678

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

688
void mndCleanupUser(SMnode *pMnode) { ipWhiteMgtCleanup(); }
1,938✔
689

690
static void ipRangeToStr(SIpV4Range *range, char *buf) {
5,123✔
691
  struct in_addr addr;
692
  addr.s_addr = range->ip;
5,123✔
693
#ifndef TD_ASTRA
694
  (void)uv_inet_ntop(AF_INET, &addr, buf, 32);
5,123✔
695
  if (range->mask != 32) {
5,125✔
696
    (void)tsnprintf(buf + strlen(buf), 36 - strlen(buf), "/%d", range->mask);
6✔
697
  }
698
#endif
699
  return;
5,124✔
700
}
701
static bool isDefaultRange(SIpV4Range *pRange) {
2✔
702
  static SIpV4Range val = {.ip = 16777343, .mask = 32};
703
  return pRange->ip == val.ip && pRange->mask == val.mask;
2!
704
}
705
static int32_t ipRangeListToStr(SIpV4Range *range, int32_t num, char *buf, int64_t bufLen) {
5,116✔
706
  int32_t len = 0;
5,116✔
707
  for (int i = 0; i < num; i++) {
10,248✔
708
    char        tbuf[36] = {0};
5,121✔
709
    SIpV4Range *pRange = &range[i];
5,121✔
710

711
    ipRangeToStr(&range[i], tbuf);
5,121✔
712
    len += tsnprintf(buf + len, bufLen - len, "%s,", tbuf);
5,124✔
713
  }
714
  if (len > 0) buf[len - 1] = 0;
5,127!
715
  return len;
5,127✔
716
}
717

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

750
  int32_t len = ipRangeListToStr(pList->pIpRange, pList->num, *buf, bufLen);
5,124✔
751
  if (len == 0) {
5,125!
752
    taosMemoryFreeClear(*buf);
×
753
    return 0;
×
754
  }
755
  return strlen(*buf);
5,125✔
756
}
757
int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteList *pList, uint32_t *pLen) {
5,590✔
758
  int32_t  code = 0;
5,590✔
759
  int32_t  lino = 0;
5,590✔
760
  int32_t  tlen = 0;
5,590✔
761
  SEncoder encoder = {0};
5,590✔
762
  tEncoderInit(&encoder, buf, len);
5,590✔
763

764
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
5,590!
765
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
11,180!
766

767
  for (int i = 0; i < pList->num; i++) {
11,190✔
768
    SIpV4Range *pRange = &(pList->pIpRange[i]);
5,600✔
769
    TAOS_CHECK_GOTO(tEncodeU32(&encoder, pRange->ip), &lino, _OVER);
11,200!
770
    TAOS_CHECK_GOTO(tEncodeU32(&encoder, pRange->mask), &lino, _OVER);
11,200!
771
  }
772

773
  tEndEncode(&encoder);
5,590✔
774

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

785
int32_t tDerializeIpWhileList(void *buf, int32_t len, SIpWhiteList *pList) {
2,551✔
786
  int32_t  code = 0;
2,551✔
787
  int32_t  lino = 0;
2,551✔
788
  SDecoder decoder = {0};
2,551✔
789
  tDecoderInit(&decoder, buf, len);
2,551✔
790

791
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
2,551!
792
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
5,102!
793

794
  for (int i = 0; i < pList->num; i++) {
5,128✔
795
    SIpV4Range *pRange = &(pList->pIpRange[i]);
2,577✔
796
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pRange->ip), &lino, _OVER);
5,154!
797
    TAOS_CHECK_GOTO(tDecodeU32(&decoder, &pRange->mask), &lino, _OVER);
5,154!
798
  }
799

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

807
  TAOS_RETURN(code);
2,551✔
808
}
809

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

818
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
2,551!
819
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
2,551!
820

821
  p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
2,551!
822
  if (p == NULL) {
2,551!
823
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
824
  }
825
  TAOS_CHECK_GOTO(tDerializeIpWhileList(buf, len, p), &lino, _OVER);
2,551!
826

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

838
static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList) {
1,585✔
839
  *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1);
1,585!
840
  if (*ppWhiteList == NULL) {
1,585!
841
    TAOS_RETURN(terrno);
×
842
  }
843
  (*ppWhiteList)->num = 1;
1,585✔
844
  SIpV4Range *range = &((*ppWhiteList)->pIpRange[0]);
1,585✔
845
#ifndef TD_ASTRA
846
  struct in_addr addr;
847
  if (uv_inet_pton(AF_INET, "127.0.0.1", &addr) == 0) {
1,585!
848
    range->ip = addr.s_addr;
1,585✔
849
    range->mask = 32;
1,585✔
850
  }
851
#endif
852
  return 0;
1,585✔
853
}
854

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

873
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
1,439✔
874
  if (pRaw == NULL) goto _ERROR;
1,439!
875
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
1,439!
876

877
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
1,439!
878

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

887
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
1,439!
888
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
889
    mndTransDrop(pTrans);
×
890
    goto _ERROR;
×
891
  }
892
  TAOS_CHECK_GOTO(sdbSetRawStatus(pRaw, SDB_STATUS_READY), &lino, _ERROR);
1,439!
893

894
  if (mndTransPrepare(pMnode, pTrans) != 0) {
1,439!
895
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
896
    mndTransDrop(pTrans);
×
897
    goto _ERROR;
×
898
  }
899

900
  mndTransDrop(pTrans);
1,439✔
901
  taosMemoryFree(userObj.pIpWhiteList);
1,439!
902
  return 0;
1,439✔
903
_ERROR:
×
904
  taosMemoryFree(userObj.pIpWhiteList);
×
905
  TAOS_RETURN(terrno ? terrno : TSDB_CODE_APP_ERROR);
×
906
}
907

908
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
1,439✔
909
  return mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS);
1,439✔
910
}
911

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

932
  char *stb = taosHashIterate(pUser->readTbs, NULL);
5,590✔
933
  while (stb != NULL) {
5,643✔
934
    size_t keyLen = 0;
53✔
935
    void  *key = taosHashGetKey(stb, &keyLen);
53✔
936
    size += sizeof(int32_t);
53✔
937
    size += keyLen;
53✔
938

939
    size_t valueLen = 0;
53✔
940
    valueLen = strlen(stb) + 1;
53✔
941
    size += sizeof(int32_t);
53✔
942
    size += valueLen;
53✔
943
    stb = taosHashIterate(pUser->readTbs, stb);
53✔
944
  }
945

946
  stb = taosHashIterate(pUser->writeTbs, NULL);
5,590✔
947
  while (stb != NULL) {
5,655✔
948
    size_t keyLen = 0;
65✔
949
    void  *key = taosHashGetKey(stb, &keyLen);
65✔
950
    size += sizeof(int32_t);
65✔
951
    size += keyLen;
65✔
952

953
    size_t valueLen = 0;
65✔
954
    valueLen = strlen(stb) + 1;
65✔
955
    size += sizeof(int32_t);
65✔
956
    size += valueLen;
65✔
957
    stb = taosHashIterate(pUser->writeTbs, stb);
65✔
958
  }
959

960
  stb = taosHashIterate(pUser->alterTbs, NULL);
5,590✔
961
  while (stb != NULL) {
5,603✔
962
    size_t keyLen = 0;
13✔
963
    void  *key = taosHashGetKey(stb, &keyLen);
13✔
964
    size += sizeof(int32_t);
13✔
965
    size += keyLen;
13✔
966

967
    size_t valueLen = 0;
13✔
968
    valueLen = strlen(stb) + 1;
13✔
969
    size += sizeof(int32_t);
13✔
970
    size += valueLen;
13✔
971
    stb = taosHashIterate(pUser->alterTbs, stb);
13✔
972
  }
973

974
  stb = taosHashIterate(pUser->readViews, NULL);
5,590✔
975
  while (stb != NULL) {
5,671✔
976
    size_t keyLen = 0;
81✔
977
    void  *key = taosHashGetKey(stb, &keyLen);
81✔
978
    size += sizeof(int32_t);
81✔
979
    size += keyLen;
81✔
980

981
    size_t valueLen = 0;
81✔
982
    valueLen = strlen(stb) + 1;
81✔
983
    size += sizeof(int32_t);
81✔
984
    size += valueLen;
81✔
985
    stb = taosHashIterate(pUser->readViews, stb);
81✔
986
  }
987

988
  stb = taosHashIterate(pUser->writeViews, NULL);
5,590✔
989
  while (stb != NULL) {
5,659✔
990
    size_t keyLen = 0;
69✔
991
    void  *key = taosHashGetKey(stb, &keyLen);
69✔
992
    size += sizeof(int32_t);
69✔
993
    size += keyLen;
69✔
994

995
    size_t valueLen = 0;
69✔
996
    valueLen = strlen(stb) + 1;
69✔
997
    size += sizeof(int32_t);
69✔
998
    size += valueLen;
69✔
999
    stb = taosHashIterate(pUser->writeViews, stb);
69✔
1000
  }
1001

1002
  stb = taosHashIterate(pUser->alterViews, NULL);
5,590✔
1003
  while (stb != NULL) {
5,662✔
1004
    size_t keyLen = 0;
72✔
1005
    void  *key = taosHashGetKey(stb, &keyLen);
72✔
1006
    size += sizeof(int32_t);
72✔
1007
    size += keyLen;
72✔
1008

1009
    size_t valueLen = 0;
72✔
1010
    valueLen = strlen(stb) + 1;
72✔
1011
    size += sizeof(int32_t);
72✔
1012
    size += valueLen;
72✔
1013
    stb = taosHashIterate(pUser->alterViews, stb);
72✔
1014
  }
1015

1016
  int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
5,590✔
1017
  while (useDb != NULL) {
5,772✔
1018
    size_t keyLen = 0;
182✔
1019
    void  *key = taosHashGetKey(useDb, &keyLen);
182✔
1020
    size += sizeof(int32_t);
182✔
1021
    size += keyLen;
182✔
1022
    size += sizeof(int32_t);
182✔
1023
    useDb = taosHashIterate(pUser->useDbs, useDb);
182✔
1024
  }
1025

1026
  pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
5,590✔
1027
  if (pRaw == NULL) {
5,590!
1028
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1029
  }
1030

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

1047
  char *db = taosHashIterate(pUser->readDbs, NULL);
5,590✔
1048
  while (db != NULL) {
5,715✔
1049
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
125!
1050
    db = taosHashIterate(pUser->readDbs, db);
125✔
1051
  }
1052

1053
  db = taosHashIterate(pUser->writeDbs, NULL);
5,590✔
1054
  while (db != NULL) {
5,709✔
1055
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
119!
1056
    db = taosHashIterate(pUser->writeDbs, db);
119✔
1057
  }
1058

1059
  char *topic = taosHashIterate(pUser->topics, NULL);
5,590✔
1060
  while (topic != NULL) {
5,627✔
1061
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
37!
1062
    topic = taosHashIterate(pUser->topics, topic);
37✔
1063
  }
1064

1065
  SDB_SET_INT32(pRaw, dataPos, numOfReadTbs, _OVER)
5,590!
1066
  SDB_SET_INT32(pRaw, dataPos, numOfWriteTbs, _OVER)
5,590!
1067
  SDB_SET_INT32(pRaw, dataPos, numOfAlterTbs, _OVER)
5,590!
1068
  SDB_SET_INT32(pRaw, dataPos, numOfReadViews, _OVER)
5,590!
1069
  SDB_SET_INT32(pRaw, dataPos, numOfWriteViews, _OVER)
5,590!
1070
  SDB_SET_INT32(pRaw, dataPos, numOfAlterViews, _OVER)
5,590!
1071
  SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
5,590!
1072

1073
  stb = taosHashIterate(pUser->readTbs, NULL);
5,590✔
1074
  while (stb != NULL) {
5,643✔
1075
    size_t keyLen = 0;
53✔
1076
    void  *key = taosHashGetKey(stb, &keyLen);
53✔
1077
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
53!
1078
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
53!
1079

1080
    size_t valueLen = 0;
53✔
1081
    valueLen = strlen(stb) + 1;
53✔
1082
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
53!
1083
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
53!
1084
    stb = taosHashIterate(pUser->readTbs, stb);
53✔
1085
  }
1086

1087
  stb = taosHashIterate(pUser->writeTbs, NULL);
5,590✔
1088
  while (stb != NULL) {
5,655✔
1089
    size_t keyLen = 0;
65✔
1090
    void  *key = taosHashGetKey(stb, &keyLen);
65✔
1091
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
65!
1092
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
65!
1093

1094
    size_t valueLen = 0;
65✔
1095
    valueLen = strlen(stb) + 1;
65✔
1096
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
65!
1097
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
65!
1098
    stb = taosHashIterate(pUser->writeTbs, stb);
65✔
1099
  }
1100

1101
  stb = taosHashIterate(pUser->alterTbs, NULL);
5,590✔
1102
  while (stb != NULL) {
5,603✔
1103
    size_t keyLen = 0;
13✔
1104
    void  *key = taosHashGetKey(stb, &keyLen);
13✔
1105
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
13!
1106
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
13!
1107

1108
    size_t valueLen = 0;
13✔
1109
    valueLen = strlen(stb) + 1;
13✔
1110
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
13!
1111
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
13!
1112
    stb = taosHashIterate(pUser->alterTbs, stb);
13✔
1113
  }
1114

1115
  stb = taosHashIterate(pUser->readViews, NULL);
5,590✔
1116
  while (stb != NULL) {
5,671✔
1117
    size_t keyLen = 0;
81✔
1118
    void  *key = taosHashGetKey(stb, &keyLen);
81✔
1119
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
81!
1120
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
81!
1121

1122
    size_t valueLen = 0;
81✔
1123
    valueLen = strlen(stb) + 1;
81✔
1124
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
81!
1125
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
81!
1126
    stb = taosHashIterate(pUser->readViews, stb);
81✔
1127
  }
1128

1129
  stb = taosHashIterate(pUser->writeViews, NULL);
5,590✔
1130
  while (stb != NULL) {
5,659✔
1131
    size_t keyLen = 0;
69✔
1132
    void  *key = taosHashGetKey(stb, &keyLen);
69✔
1133
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
69!
1134
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
69!
1135

1136
    size_t valueLen = 0;
69✔
1137
    valueLen = strlen(stb) + 1;
69✔
1138
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
69!
1139
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
69!
1140
    stb = taosHashIterate(pUser->writeViews, stb);
69✔
1141
  }
1142

1143
  stb = taosHashIterate(pUser->alterViews, NULL);
5,590✔
1144
  while (stb != NULL) {
5,662✔
1145
    size_t keyLen = 0;
72✔
1146
    void  *key = taosHashGetKey(stb, &keyLen);
72✔
1147
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
72!
1148
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
72!
1149

1150
    size_t valueLen = 0;
72✔
1151
    valueLen = strlen(stb) + 1;
72✔
1152
    SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
72!
1153
    SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
72!
1154
    stb = taosHashIterate(pUser->alterViews, stb);
72✔
1155
  }
1156

1157
  useDb = taosHashIterate(pUser->useDbs, NULL);
5,590✔
1158
  while (useDb != NULL) {
5,772✔
1159
    size_t keyLen = 0;
182✔
1160
    void  *key = taosHashGetKey(useDb, &keyLen);
182✔
1161
    SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
182!
1162
    SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
182!
1163

1164
    SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
182!
1165
    useDb = taosHashIterate(pUser->useDbs, useDb);
182✔
1166
  }
1167

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

1177
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
5,590!
1178
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
5,590!
1179

1180
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
5,590!
1181

1182
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
5,590!
1183
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
5,590!
1184

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

1195
  mTrace("user:%s, encode user action to raw:%p, row:%p", pUser->user, pRaw, pUser);
5,590✔
1196
  return pRaw;
5,590✔
1197
}
1198

1199
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
2,551✔
1200
  int32_t   code = 0;
2,551✔
1201
  int32_t   lino = 0;
2,551✔
1202
  SSdbRow  *pRow = NULL;
2,551✔
1203
  SUserObj *pUser = NULL;
2,551✔
1204
  char     *key = NULL;
2,551✔
1205
  char     *value = NULL;
2,551✔
1206

1207
  int8_t sver = 0;
2,551✔
1208
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
2,551!
1209
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PTR, &lino, _OVER);
×
1210
  }
1211

1212
  if (sver < 1 || sver > USER_VER_NUMBER) {
2,551!
1213
    TAOS_CHECK_GOTO(TSDB_CODE_SDB_INVALID_DATA_VER, &lino, _OVER);
×
1214
  }
1215

1216
  pRow = sdbAllocRow(sizeof(SUserObj));
2,551✔
1217
  if (pRow == NULL) {
2,551!
1218
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1219
  }
1220

1221
  pUser = sdbGetRowObj(pRow);
2,551✔
1222
  if (pUser == NULL) {
2,551!
1223
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1224
  }
1225

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

1242
  int32_t numOfReadDbs = 0;
2,551✔
1243
  int32_t numOfWriteDbs = 0;
2,551✔
1244
  int32_t numOfTopics = 0;
2,551✔
1245
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
2,551!
1246
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
2,551!
1247
  if (sver >= 2) {
2,551!
1248
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
2,551!
1249
  }
1250

1251
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1252
  pUser->writeDbs =
2,551✔
1253
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1254
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1255
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) {
2,551!
1256
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1257
    goto _OVER;
×
1258
  }
1259

1260
  for (int32_t i = 0; i < numOfReadDbs; ++i) {
2,658✔
1261
    char db[TSDB_DB_FNAME_LEN] = {0};
107✔
1262
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
107!
1263
    int32_t len = strlen(db) + 1;
107✔
1264
    TAOS_CHECK_GOTO(taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
107!
1265
  }
1266

1267
  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
2,651✔
1268
    char db[TSDB_DB_FNAME_LEN] = {0};
100✔
1269
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
100!
1270
    int32_t len = strlen(db) + 1;
100✔
1271
    TAOS_CHECK_GOTO(taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN), &lino, _OVER);
100!
1272
  }
1273

1274
  if (sver >= 2) {
2,551!
1275
    for (int32_t i = 0; i < numOfTopics; ++i) {
2,585✔
1276
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
34✔
1277
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
34!
1278
      int32_t len = strlen(topic) + 1;
34✔
1279
      TAOS_CHECK_GOTO(taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN), &lino, _OVER);
34!
1280
    }
1281
  }
1282

1283
  if (sver >= 3) {
2,551!
1284
    int32_t numOfReadTbs = 0;
2,551✔
1285
    int32_t numOfWriteTbs = 0;
2,551✔
1286
    int32_t numOfAlterTbs = 0;
2,551✔
1287
    int32_t numOfReadViews = 0;
2,551✔
1288
    int32_t numOfWriteViews = 0;
2,551✔
1289
    int32_t numOfAlterViews = 0;
2,551✔
1290
    int32_t numOfUseDbs = 0;
2,551✔
1291
    SDB_GET_INT32(pRaw, dataPos, &numOfReadTbs, _OVER)
2,551!
1292
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteTbs, _OVER)
2,551!
1293
    if (sver >= 6) {
2,551!
1294
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterTbs, _OVER)
2,551!
1295
      SDB_GET_INT32(pRaw, dataPos, &numOfReadViews, _OVER)
2,551!
1296
      SDB_GET_INT32(pRaw, dataPos, &numOfWriteViews, _OVER)
2,551!
1297
      SDB_GET_INT32(pRaw, dataPos, &numOfAlterViews, _OVER)
2,551!
1298
    }
1299
    SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
2,551!
1300

1301
    pUser->readTbs =
2,551✔
1302
        taosHashInit(numOfReadTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1303
    pUser->writeTbs =
2,551✔
1304
        taosHashInit(numOfWriteTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1305
    pUser->alterTbs =
2,551✔
1306
        taosHashInit(numOfAlterTbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1307

1308
    pUser->readViews =
2,551✔
1309
        taosHashInit(numOfReadViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1310
    pUser->writeViews =
2,551✔
1311
        taosHashInit(numOfWriteViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1312
    pUser->alterViews =
2,551✔
1313
        taosHashInit(numOfAlterViews, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1314

1315
    pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,551✔
1316

1317
    if (pUser->readTbs == NULL || pUser->writeTbs == NULL || pUser->alterTbs == NULL || pUser->readViews == NULL ||
2,551!
1318
        pUser->writeViews == NULL || pUser->alterViews == NULL || pUser->useDbs == NULL) {
2,551!
1319
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1320
      goto _OVER;
×
1321
    }
1322

1323
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
2,581✔
1324
      int32_t keyLen = 0;
30✔
1325
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
30!
1326

1327
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
30!
1328
      if (key == NULL) {
30!
1329
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1330
      }
1331
      (void)memset(key, 0, keyLen);
30✔
1332
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
30!
1333

1334
      int32_t valuelen = 0;
30✔
1335
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
30!
1336
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
30!
1337
      if (value == NULL) {
30!
1338
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1339
      }
1340
      (void)memset(value, 0, valuelen);
30✔
1341
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
30!
1342

1343
      TAOS_CHECK_GOTO(taosHashPut(pUser->readTbs, key, keyLen, value, valuelen), &lino, _OVER);
30!
1344
    }
1345

1346
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
2,587✔
1347
      int32_t keyLen = 0;
36✔
1348
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
36!
1349

1350
      TAOS_MEMORY_REALLOC(key, keyLen * sizeof(char));
36!
1351
      if (key == NULL) {
36!
1352
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1353
      }
1354
      (void)memset(key, 0, keyLen);
36✔
1355
      SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
36!
1356

1357
      int32_t valuelen = 0;
36✔
1358
      SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
36!
1359
      TAOS_MEMORY_REALLOC(value, valuelen * sizeof(char));
36!
1360
      if (value == NULL) {
36!
1361
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1362
      }
1363
      (void)memset(value, 0, valuelen);
36✔
1364
      SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
36!
1365

1366
      TAOS_CHECK_GOTO(taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen), &lino, _OVER);
36!
1367
    }
1368

1369
    if (sver >= 6) {
2,551!
1370
      for (int32_t i = 0; i < numOfAlterTbs; ++i) {
2,563✔
1371
        int32_t keyLen = 0;
12✔
1372
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
12!
1373

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

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

1390
        TAOS_CHECK_GOTO(taosHashPut(pUser->alterTbs, key, keyLen, value, valuelen), &lino, _OVER);
12!
1391
      }
1392

1393
      for (int32_t i = 0; i < numOfReadViews; ++i) {
2,629✔
1394
        int32_t keyLen = 0;
78✔
1395
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
78!
1396

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

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

1413
        TAOS_CHECK_GOTO(taosHashPut(pUser->readViews, key, keyLen, value, valuelen), &lino, _OVER);
78!
1414
      }
1415

1416
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
2,617✔
1417
        int32_t keyLen = 0;
66✔
1418
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
66!
1419

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

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

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

1439
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
2,620✔
1440
        int32_t keyLen = 0;
69✔
1441
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
69!
1442

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

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

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

1463
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
2,680✔
1464
      int32_t keyLen = 0;
129✔
1465
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
129!
1466

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

1474
      int32_t ref = 0;
129✔
1475
      SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
129!
1476

1477
      TAOS_CHECK_GOTO(taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref)), &lino, _OVER);
129!
1478
    }
1479
  }
1480
  // decoder white list
1481
  if (sver >= 5) {
2,551!
1482
    int32_t len = 0;
2,551✔
1483
    SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
2,551!
1484

1485
    TAOS_MEMORY_REALLOC(key, len);
2,551!
1486
    if (key == NULL) {
2,551!
1487
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1488
    }
1489
    SDB_GET_BINARY(pRaw, dataPos, key, len, _OVER);
2,551!
1490

1491
    TAOS_CHECK_GOTO(createIpWhiteList(key, len, &pUser->pIpWhiteList), &lino, _OVER);
2,551!
1492

1493
    SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
2,551!
1494
  }
1495

1496
  if (pUser->pIpWhiteList == NULL) {
2,551!
1497
    TAOS_CHECK_GOTO(createDefaultIpWhiteList(&pUser->pIpWhiteList), &lino, _OVER);
×
1498
    pUser->ipWhiteListVer = taosGetTimestampMs();
×
1499
  }
1500

1501
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2,551!
1502
  taosInitRWLatch(&pUser->lock);
2,551✔
1503

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

1528
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
2,551✔
1529
  return pRow;
2,551✔
1530
}
1531

1532
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
2,145✔
1533
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
2,145✔
1534

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

1544
  return 0;
2,145✔
1545
}
1546

1547
int32_t mndDupTableHash(SHashObj *pOld, SHashObj **ppNew) {
2,942,571✔
1548
  int32_t code = 0;
2,942,571✔
1549
  *ppNew =
2,942,566✔
1550
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2,942,571✔
1551
  if (*ppNew == NULL) {
2,942,566!
1552
    TAOS_RETURN(terrno);
×
1553
  }
1554

1555
  char *tb = taosHashIterate(pOld, NULL);
2,942,566✔
1556
  while (tb != NULL) {
2,943,504✔
1557
    size_t keyLen = 0;
939✔
1558
    char  *key = taosHashGetKey(tb, &keyLen);
939✔
1559

1560
    int32_t valueLen = strlen(tb) + 1;
939✔
1561
    if ((code = taosHashPut(*ppNew, key, keyLen, tb, valueLen)) != 0) {
939!
1562
      taosHashCancelIterate(pOld, tb);
×
1563
      taosHashCleanup(*ppNew);
×
1564
      TAOS_RETURN(code);
×
1565
    }
1566
    tb = taosHashIterate(pOld, tb);
939✔
1567
  }
1568

1569
  TAOS_RETURN(code);
2,942,565✔
1570
}
1571

1572
int32_t mndDupUseDbHash(SHashObj *pOld, SHashObj **ppNew) {
4,386✔
1573
  int32_t code = 0;
4,386✔
1574
  *ppNew =
4,386✔
1575
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
4,386✔
1576
  if (*ppNew == NULL) {
4,386!
1577
    TAOS_RETURN(terrno);
×
1578
  }
1579

1580
  int32_t *db = taosHashIterate(pOld, NULL);
4,386✔
1581
  while (db != NULL) {
4,587✔
1582
    size_t keyLen = 0;
201✔
1583
    char  *key = taosHashGetKey(db, &keyLen);
201✔
1584

1585
    if ((code = taosHashPut(*ppNew, key, keyLen, db, sizeof(*db))) != 0) {
201!
1586
      taosHashCancelIterate(pOld, db);
×
1587
      taosHashCleanup(*ppNew);
×
1588
      TAOS_RETURN(code);
×
1589
    }
1590
    db = taosHashIterate(pOld, db);
201✔
1591
  }
1592

1593
  TAOS_RETURN(code);
4,386✔
1594
}
1595

1596
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
4,386✔
1597
  int32_t code = 0;
4,386✔
1598
  (void)memcpy(pNew, pUser, sizeof(SUserObj));
4,386✔
1599
  pNew->authVersion++;
4,386✔
1600
  pNew->updateTime = taosGetTimestampMs();
4,386✔
1601

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

1618
_OVER:
4,386✔
1619
  taosRUnLockLatch(&pUser->lock);
4,386✔
1620
  TAOS_RETURN(code);
4,386✔
1621
}
1622

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

1647
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
2,550✔
1648
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
2,550✔
1649
  mndUserFreeObj(pUser);
2,550✔
1650
  return 0;
2,550✔
1651
}
1652

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

1674
  int32_t sz = sizeof(SIpWhiteList) + pNew->pIpWhiteList->num * sizeof(SIpV4Range);
306✔
1675
  TAOS_MEMORY_REALLOC(pOld->pIpWhiteList, sz);
306!
1676
  if (pOld->pIpWhiteList == NULL) {
306!
1677
    taosWUnLockLatch(&pOld->lock);
×
1678
    return terrno;
×
1679
  }
1680
  (void)memcpy(pOld->pIpWhiteList, pNew->pIpWhiteList, sz);
306✔
1681
  pOld->ipWhiteListVer = pNew->ipWhiteListVer;
306✔
1682

1683
  taosWUnLockLatch(&pOld->lock);
306✔
1684

1685
  return 0;
306✔
1686
}
1687

1688
int32_t mndAcquireUser(SMnode *pMnode, const char *userName, SUserObj **ppUser) {
5,171,910✔
1689
  int32_t code = 0;
5,171,910✔
1690
  SSdb   *pSdb = pMnode->pSdb;
5,171,910✔
1691

1692
  *ppUser = sdbAcquire(pSdb, SDB_USER, userName);
5,171,910✔
1693
  if (*ppUser == NULL) {
5,171,955✔
1694
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
196!
1695
      code = TSDB_CODE_MND_USER_NOT_EXIST;
196✔
1696
    } else {
1697
      code = TSDB_CODE_MND_USER_NOT_AVAILABLE;
×
1698
    }
1699
  }
1700
  TAOS_RETURN(code);
5,171,955✔
1701
}
1702

1703
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
5,172,241✔
1704
  SSdb *pSdb = pMnode->pSdb;
5,172,241✔
1705
  sdbRelease(pSdb, pUser);
5,172,241✔
1706
}
5,172,643✔
1707

1708
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
149✔
1709
  int32_t  code = 0;
149✔
1710
  int32_t  lino = 0;
149✔
1711
  SUserObj userObj = {0};
149✔
1712

1713
  if (pCreate->passIsMd5 == 1) {
149!
1714
    memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
149✔
1715
  } else {
1716
    if (pCreate->isImport != 1) {
×
1717
      taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
×
1718
    } else {
1719
      // mInfo("pCreate->pass:%s", pCreate->eass)
1720
      memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
1721
    }
1722
  }
1723

1724
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
149✔
1725
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
149✔
1726
  userObj.createdTime = taosGetTimestampMs();
149✔
1727
  userObj.updateTime = userObj.createdTime;
149✔
1728
  userObj.superUser = 0;  // pCreate->superUser;
149✔
1729
  userObj.sysInfo = pCreate->sysInfo;
149✔
1730
  userObj.enable = pCreate->enable;
149✔
1731
  userObj.createdb = pCreate->createDb;
149✔
1732

1733
  if (pCreate->numIpRanges == 0) {
149✔
1734
    TAOS_CHECK_RETURN(createDefaultIpWhiteList(&userObj.pIpWhiteList));
146!
1735
  } else {
1736
    SHashObj *pUniqueTab = taosHashInit(64, MurmurHash3_32, true, HASH_NO_LOCK);
3✔
1737
    if (pUniqueTab == NULL) {
3!
1738
      TAOS_RETURN(terrno);
×
1739
    }
1740
    int32_t dummpy = 0;
3✔
1741
    for (int i = 0; i < pCreate->numIpRanges; i++) {
8✔
1742
      SIpV4Range range = {.ip = pCreate->pIpRanges[i].ip, .mask = pCreate->pIpRanges[i].mask};
5✔
1743
      if ((code = taosHashPut(pUniqueTab, &range, sizeof(range), &dummpy, sizeof(dummpy))) != 0) {
5!
1744
        taosHashCleanup(pUniqueTab);
×
1745
        TAOS_RETURN(code);
×
1746
      }
1747
    }
1748
    if ((code = taosHashPut(pUniqueTab, &defaultIpRange, sizeof(defaultIpRange), &dummpy, sizeof(dummpy))) != 0) {
3!
1749
      taosHashCleanup(pUniqueTab);
×
1750
      TAOS_RETURN(code);
×
1751
    }
1752

1753
    if (taosHashGetSize(pUniqueTab) > MND_MAX_USE_HOST) {
3!
1754
      taosHashCleanup(pUniqueTab);
×
1755
      TAOS_RETURN(TSDB_CODE_MND_TOO_MANY_USER_HOST);
×
1756
    }
1757

1758
    int32_t       numOfRanges = taosHashGetSize(pUniqueTab);
3✔
1759
    SIpWhiteList *p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + numOfRanges * sizeof(SIpV4Range));
3!
1760
    if (p == NULL) {
3!
1761
      taosHashCleanup(pUniqueTab);
×
1762
      TAOS_RETURN(terrno);
×
1763
    }
1764
    void   *pIter = taosHashIterate(pUniqueTab, NULL);
3✔
1765
    int32_t i = 0;
3✔
1766
    while (pIter) {
9✔
1767
      size_t      len = 0;
6✔
1768
      SIpV4Range *key = taosHashGetKey(pIter, &len);
6✔
1769
      p->pIpRange[i].ip = key->ip;
6✔
1770
      p->pIpRange[i].mask = key->mask;
6✔
1771
      pIter = taosHashIterate(pUniqueTab, pIter);
6✔
1772

1773
      i++;
6✔
1774
    }
1775

1776
    taosHashCleanup(pUniqueTab);
3✔
1777
    p->num = numOfRanges;
3✔
1778
    userObj.pIpWhiteList = p;
3✔
1779
  }
1780

1781
  userObj.ipWhiteListVer = taosGetTimestampMs();
149✔
1782

1783
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
149✔
1784
  if (pTrans == NULL) {
149!
1785
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
×
1786
    taosMemoryFree(userObj.pIpWhiteList);
×
1787
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1788
  }
1789
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
149!
1790

1791
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
149✔
1792
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
149!
1793
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
1794
    mndTransDrop(pTrans);
×
1795
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1796
  }
1797
  TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
149!
1798

1799
  if (mndTransPrepare(pMnode, pTrans) != 0) {
149!
1800
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
1801
    mndTransDrop(pTrans);
×
1802
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1803
  }
1804
  if ((code = ipWhiteMgtUpdate(pMnode, userObj.user, userObj.pIpWhiteList)) != 0) {
149!
1805
    mndTransDrop(pTrans);
×
1806
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1807
  }
1808

1809
  taosMemoryFree(userObj.pIpWhiteList);
149!
1810
  mndTransDrop(pTrans);
149✔
1811
  return 0;
149✔
1812
_OVER:
×
1813
  taosMemoryFree(userObj.pIpWhiteList);
×
1814

1815
  TAOS_RETURN(code);
×
1816
}
1817

1818
static int32_t mndCheckPasswordMinLen(const char *pwd, int32_t len) {
×
1819
  if (len < TSDB_PASSWORD_MIN_LEN) {
×
1820
    return -1;
×
1821
  }
1822
  return 0;
×
1823
}
1824

1825
static int32_t mndCheckPasswordMaxLen(const char *pwd, int32_t len) {
×
1826
  if (len > TSDB_PASSWORD_MAX_LEN) {
×
1827
    return -1;
×
1828
  }
1829
  return 0;
×
1830
}
1831

1832
static int32_t mndCheckPasswordFmt(const char *pwd, int32_t len) {
×
1833
  if (strcmp(pwd, "taosdata") == 0) {
×
1834
    return 0;
×
1835
  }
1836

1837
  bool charTypes[4] = {0};
×
1838
  for (int32_t i = 0; i < len; ++i) {
×
1839
    if (taosIsBigChar(pwd[i])) {
×
1840
      charTypes[0] = true;
×
1841
    } else if (taosIsSmallChar(pwd[i])) {
×
1842
      charTypes[1] = true;
×
1843
    } else if (taosIsNumberChar(pwd[i])) {
×
1844
      charTypes[2] = true;
×
1845
    } else if (taosIsSpecialChar(pwd[i])) {
×
1846
      charTypes[3] = true;
×
1847
    } else {
1848
      return -1;
×
1849
    }
1850
  }
1851

1852
  int32_t numOfTypes = 0;
×
1853
  for (int32_t i = 0; i < 4; ++i) {
×
1854
    numOfTypes += charTypes[i];
×
1855
  }
1856

1857
  if (numOfTypes < 3) {
×
1858
    return -1;
×
1859
  }
1860

1861
  return 0;
×
1862
}
1863

1864
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
149✔
1865
  SMnode        *pMnode = pReq->info.node;
149✔
1866
  int32_t        code = 0;
149✔
1867
  int32_t        lino = 0;
149✔
1868
  SUserObj      *pUser = NULL;
149✔
1869
  SUserObj      *pOperUser = NULL;
149✔
1870
  SCreateUserReq createReq = {0};
149✔
1871

1872
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
149!
1873
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
1874
  }
1875

1876
  mInfo("user:%s, start to create, createdb:%d, is_import:%d", createReq.user, createReq.isImport, createReq.createDb);
149!
1877

1878
#ifndef TD_ENTERPRISE
1879
  if (createReq.isImport == 1) {
1880
    TAOS_CHECK_GOTO(TSDB_CODE_OPS_NOT_SUPPORT, &lino, _OVER);  // enterprise feature
1881
  }
1882
#endif
1883

1884
  if (createReq.isImport != 1) {
149!
1885
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
149!
1886
  } else {
1887
    if (strcmp(pReq->info.conn.user, "root") != 0) {
×
1888
      mError("The operation is not permitted, user:%s", pReq->info.conn.user);
×
1889
      TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_RIGHTS, &lino, _OVER);
×
1890
    }
1891
  }
1892

1893
  if (createReq.user[0] == 0) {
149!
1894
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
1895
  }
1896

1897
  if(createReq.passIsMd5 == 0){
149!
1898
    int32_t len = strlen(createReq.pass);
×
1899
    if (createReq.isImport != 1) {
×
1900
      if (mndCheckPasswordMinLen(createReq.pass, len) != 0) {
×
1901
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY, &lino, _OVER);
×
1902
      }
1903
      if (mndCheckPasswordMaxLen(createReq.pass, len) != 0) {
×
1904
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG, &lino, _OVER);
×
1905
      }
1906
      if (mndCheckPasswordFmt(createReq.pass, len) != 0) {
×
1907
        TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
1908
      }
1909
    }
1910
  }
1911

1912
  code = mndAcquireUser(pMnode, createReq.user, &pUser);
149✔
1913
  if (pUser != NULL) {
149!
1914
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
×
1915
  }
1916

1917
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
149✔
1918
  if (pOperUser == NULL) {
149!
1919
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
1920
  }
1921

1922
  TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
149!
1923

1924
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
149✔
1925
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
149!
1926

1927
  char detail[1000] = {0};
149✔
1928
  (void)tsnprintf(detail, sizeof(detail), "enable:%d, superUser:%d, sysInfo:%d, password:xxx", createReq.enable,
149✔
1929
            createReq.superUser, createReq.sysInfo);
149✔
1930
  char operation[15] = {0};
149✔
1931
  if (createReq.isImport == 1) {
149!
1932
    tstrncpy(operation, "importUser", sizeof(operation));
×
1933
  } else {
1934
    tstrncpy(operation, "createUser", sizeof(operation));
149✔
1935
  }
1936

1937
  auditRecord(pReq, pMnode->clusterId, operation, "", createReq.user, detail, strlen(detail));
149✔
1938

1939
_OVER:
149✔
1940
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
149!
1941
    mError("user:%s, failed to create at line %d since %s", createReq.user, lino, tstrerror(code));
×
1942
  }
1943

1944
  mndReleaseUser(pMnode, pUser);
149✔
1945
  mndReleaseUser(pMnode, pOperUser);
149✔
1946
  tFreeSCreateUserReq(&createReq);
149✔
1947

1948
  TAOS_RETURN(code);
149✔
1949
}
1950

1951
int32_t mndProcessGetUserWhiteListReq(SRpcMsg *pReq) {
418✔
1952
  SMnode              *pMnode = pReq->info.node;
418✔
1953
  int32_t              code = 0;
418✔
1954
  int32_t              lino = 0;
418✔
1955
  int32_t              contLen = 0;
418✔
1956
  void                *pRsp = NULL;
418✔
1957
  SUserObj            *pUser = NULL;
418✔
1958
  SGetUserWhiteListReq wlReq = {0};
418✔
1959
  SGetUserWhiteListRsp wlRsp = {0};
418✔
1960

1961
  if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
418!
1962
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
1963
  }
1964
  mTrace("user: %s, start to get whitelist", wlReq.user);
418✔
1965

1966
  code = mndAcquireUser(pMnode, wlReq.user, &pUser);
418✔
1967
  if (pUser == NULL) {
418!
1968
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_NOT_EXIST, &lino, _OVER);
×
1969
  }
1970

1971
  TAOS_CHECK_GOTO(mndSetUserWhiteListRsp(pMnode, pUser, &wlRsp), &lino, _OVER);
418!
1972

1973
  contLen = tSerializeSGetUserWhiteListRsp(NULL, 0, &wlRsp);
418✔
1974
  if (contLen < 0) {
418!
1975
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1976
  }
1977
  pRsp = rpcMallocCont(contLen);
418✔
1978
  if (pRsp == NULL) {
418!
1979
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
1980
  }
1981

1982
  contLen = tSerializeSGetUserWhiteListRsp(pRsp, contLen, &wlRsp);
418✔
1983
  if (contLen < 0) {
418!
1984
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
×
1985
  }
1986

1987
_OVER:
418✔
1988
  mndReleaseUser(pMnode, pUser);
418✔
1989
  tFreeSGetUserWhiteListRsp(&wlRsp);
418✔
1990
  if (code < 0) {
418!
1991
    mError("user:%s, failed to get whitelist at line %d since %s", wlReq.user, lino, tstrerror(code));
×
1992
    rpcFreeCont(pRsp);
×
1993
    pRsp = NULL;
×
1994
    contLen = 0;
×
1995
  }
1996
  pReq->code = code;
418✔
1997
  pReq->info.rsp = pRsp;
418✔
1998
  pReq->info.rspLen = contLen;
418✔
1999

2000
  TAOS_RETURN(code);
418✔
2001
}
2002

2003
int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
6✔
2004
  int32_t        code = 0;
6✔
2005
  int32_t        lino = 0;
6✔
2006
  int32_t        len = 0;
6✔
2007
  void          *pRsp = NULL;
6✔
2008
  SUpdateIpWhite ipWhite = {0};
6✔
2009

2010
  // impl later
2011
  SRetrieveIpWhiteReq req = {0};
6✔
2012
  if (tDeserializeRetrieveIpWhite(pReq->pCont, pReq->contLen, &req) != 0) {
6!
2013
    code = TSDB_CODE_INVALID_MSG;
×
2014
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2015
  }
2016

2017
  TAOS_CHECK_GOTO(ipWhiteMgtFillMsg(&ipWhite), &lino, _OVER);
6!
2018

2019
  len = tSerializeSUpdateIpWhite(NULL, 0, &ipWhite);
6✔
2020
  if (len < 0) {
6!
2021
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2022
  }
2023

2024
  pRsp = rpcMallocCont(len);
6✔
2025
  if (!pRsp) {
6!
2026
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2027
  }
2028
  len = tSerializeSUpdateIpWhite(pRsp, len, &ipWhite);
6✔
2029
  if (len < 0) {
6!
2030
    TAOS_CHECK_GOTO(len, &lino, _OVER);
×
2031
  }
2032

2033
_OVER:
6✔
2034
  if (code < 0) {
6!
2035
    mError("failed to process retrieve ip white request at line %d since %s", lino, tstrerror(code));
×
2036
    rpcFreeCont(pRsp);
×
2037
    pRsp = NULL;
×
2038
    len = 0;
×
2039
  }
2040
  pReq->code = code;
6✔
2041
  pReq->info.rsp = pRsp;
6✔
2042
  pReq->info.rspLen = len;
6✔
2043

2044
  tFreeSUpdateIpWhiteReq(&ipWhite);
6✔
2045
  TAOS_RETURN(code);
6✔
2046
}
2047

2048
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
238✔
2049
  int32_t code = 0;
238✔
2050
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
238✔
2051
  if (pTrans == NULL) {
238!
2052
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
×
2053
    TAOS_RETURN(terrno);
×
2054
  }
2055
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
238!
2056

2057
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
238✔
2058
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
238!
2059
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2060
    mndTransDrop(pTrans);
×
2061
    TAOS_RETURN(terrno);
×
2062
  }
2063
  code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
238✔
2064
  if (code < 0) {
238!
2065
    mndTransDrop(pTrans);
×
2066
    TAOS_RETURN(code);
×
2067
  }
2068

2069
  if (mndTransPrepare(pMnode, pTrans) != 0) {
238!
2070
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2071
    mndTransDrop(pTrans);
×
2072
    TAOS_RETURN(terrno);
×
2073
  }
2074
  if ((code = ipWhiteMgtUpdate(pMnode, pNew->user, pNew->pIpWhiteList)) != 0) {
238!
2075
    mndTransDrop(pTrans);
×
2076
    TAOS_RETURN(code);
×
2077
  }
2078
  mndTransDrop(pTrans);
238✔
2079
  return 0;
238✔
2080
}
2081

2082
static int32_t mndDupObjHash(SHashObj *pOld, int32_t dataLen, SHashObj **ppNew) {
846,382✔
2083
  int32_t code = 0;
846,382✔
2084

2085
  *ppNew =
846,383✔
2086
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
846,382✔
2087
  if (*ppNew == NULL) {
846,383!
2088
    code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2089
    TAOS_RETURN(code);
×
2090
  }
2091

2092
  char *db = taosHashIterate(pOld, NULL);
846,383✔
2093
  while (db != NULL) {
846,846✔
2094
    int32_t len = strlen(db) + 1;
465✔
2095
    if ((code = taosHashPut(*ppNew, db, len, db, dataLen)) != 0) {
465!
2096
      taosHashCancelIterate(pOld, db);
×
2097
      taosHashCleanup(*ppNew);
×
2098
      TAOS_RETURN(code);
×
2099
    }
2100
    db = taosHashIterate(pOld, db);
465✔
2101
  }
2102

2103
  TAOS_RETURN(code);
846,381✔
2104
}
2105

2106
int32_t mndDupDbHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN, ppNew); }
841,996✔
2107

2108
int32_t mndDupTopicHash(SHashObj *pOld, SHashObj **ppNew) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN, ppNew); }
4,386✔
2109

2110
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
86✔
2111
                                  SSdb *pSdb) {
2112
  void *pIter = NULL;
86✔
2113
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
86✔
2114

2115
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
86✔
2116
  int32_t len = strlen(tbFName) + 1;
86✔
2117

2118
  if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
109!
2119
    char *value = taosHashGet(hash, tbFName, len);
23✔
2120
    if (value != NULL) {
23!
2121
      TAOS_RETURN(TSDB_CODE_MND_PRIVILEDGE_EXIST);
×
2122
    }
2123

2124
    int32_t condLen = alterReq->tagCondLen;
23✔
2125
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen));
23!
2126
  } else {
2127
    TAOS_CHECK_RETURN(taosHashPut(hash, tbFName, len, alterReq->isView ? "v" : "t", 2));
63!
2128
  }
2129

2130
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
86✔
2131
  int32_t  ref = 1;
86✔
2132
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
86✔
2133
  if (NULL != currRef) {
86✔
2134
    ref = (*currRef) + 1;
30✔
2135
  }
2136
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
86!
2137

2138
  TAOS_RETURN(0);
86✔
2139
}
2140

2141
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
11✔
2142
                                        SSdb *pSdb) {
2143
  void *pIter = NULL;
11✔
2144
  char  tbFName[TSDB_TABLE_FNAME_LEN] = {0};
11✔
2145
  (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
11✔
2146
  int32_t len = strlen(tbFName) + 1;
11✔
2147

2148
  if (taosHashRemove(hash, tbFName, len) != 0) {
11!
2149
    TAOS_RETURN(0);  // not found
×
2150
  }
2151

2152
  int32_t  dbKeyLen = strlen(alterReq->objname) + 1;
11✔
2153
  int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
11✔
2154
  if (NULL == currRef) {
11!
2155
    return 0;
×
2156
  }
2157

2158
  if (1 == *currRef) {
11!
2159
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
11!
2160
      TAOS_RETURN(0);  // not found
×
2161
    }
2162
    return 0;
11✔
2163
  }
2164
  int32_t ref = (*currRef) - 1;
×
2165
  TAOS_CHECK_RETURN(taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)));
×
2166

2167
  return 0;
×
2168
}
2169

2170
static char *mndUserAuditTypeStr(int32_t type) {
35✔
2171
  if (type == TSDB_ALTER_USER_PASSWD) {
35!
2172
    return "changePassword";
35✔
2173
  }
2174
  if (type == TSDB_ALTER_USER_SUPERUSER) {
×
2175
    return "changeSuperUser";
×
2176
  }
2177
  if (type == TSDB_ALTER_USER_ENABLE) {
×
2178
    return "enableUser";
×
2179
  }
2180
  if (type == TSDB_ALTER_USER_SYSINFO) {
×
2181
    return "userSysInfo";
×
2182
  }
2183
  if (type == TSDB_ALTER_USER_CREATEDB) {
×
2184
    return "userCreateDB";
×
2185
  }
2186
  return "error";
×
2187
}
2188

2189
static int32_t mndProcessAlterUserPrivilegesReq(SAlterUserReq *pAlterReq, SMnode *pMnode, SUserObj *pNewUser) {
186✔
2190
  SSdb   *pSdb = pMnode->pSdb;
186✔
2191
  void   *pIter = NULL;
186✔
2192
  int32_t code = 0;
186✔
2193
  int32_t lino = 0;
186✔
2194

2195
  if (ALTER_USER_ADD_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
186✔
2196
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
140!
2197
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
46✔
2198
      int32_t len = strlen(pAlterReq->objname) + 1;
43✔
2199
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
43✔
2200
      if (pDb == NULL) {
43✔
2201
        mndReleaseDb(pMnode, pDb);
6✔
2202
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
6!
2203
      }
2204
      if ((code = taosHashPut(pNewUser->readDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
37!
2205
          0) {
2206
        mndReleaseDb(pMnode, pDb);
×
2207
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2208
      }
2209
      mndReleaseDb(pMnode, pDb);
37✔
2210
    } else {
2211
      while (1) {
9✔
2212
        SDbObj *pDb = NULL;
12✔
2213
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
12✔
2214
        if (pIter == NULL) break;
12✔
2215
        int32_t len = strlen(pDb->name) + 1;
9✔
2216
        if ((code = taosHashPut(pNewUser->readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
9!
2217
          sdbRelease(pSdb, pDb);
×
2218
          sdbCancelFetch(pSdb, pIter);
×
2219
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2220
        }
2221
        sdbRelease(pSdb, pDb);
9✔
2222
      }
2223
    }
2224
  }
2225

2226
  if (ALTER_USER_ADD_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
180✔
2227
      ALTER_USER_ADD_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
140!
2228
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
40✔
2229
      int32_t len = strlen(pAlterReq->objname) + 1;
37✔
2230
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
37✔
2231
      if (pDb == NULL) {
37✔
2232
        mndReleaseDb(pMnode, pDb);
1✔
2233
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
1!
2234
      }
2235
      if ((code = taosHashPut(pNewUser->writeDbs, pAlterReq->objname, len, pAlterReq->objname, TSDB_DB_FNAME_LEN)) !=
36!
2236
          0) {
2237
        mndReleaseDb(pMnode, pDb);
×
2238
        TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2239
      }
2240
      mndReleaseDb(pMnode, pDb);
36✔
2241
    } else {
2242
      while (1) {
9✔
2243
        SDbObj *pDb = NULL;
12✔
2244
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
12✔
2245
        if (pIter == NULL) break;
12✔
2246
        int32_t len = strlen(pDb->name) + 1;
9✔
2247
        if ((code = taosHashPut(pNewUser->writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN)) != 0) {
9!
2248
          sdbRelease(pSdb, pDb);
×
2249
          sdbCancelFetch(pSdb, pIter);
×
2250
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2251
        }
2252
        sdbRelease(pSdb, pDb);
9✔
2253
      }
2254
    }
2255
  }
2256

2257
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2258
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
155!
2259
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
24✔
2260
      int32_t len = strlen(pAlterReq->objname) + 1;
21✔
2261
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
21✔
2262
      if (pDb == NULL) {
21!
2263
        mndReleaseDb(pMnode, pDb);
×
2264
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2265
      }
2266
      code = taosHashRemove(pNewUser->readDbs, pAlterReq->objname, len);
21✔
2267
      if (code < 0) {
21!
2268
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2269
      }
2270
      mndReleaseDb(pMnode, pDb);
21✔
2271
    } else {
2272
      taosHashClear(pNewUser->readDbs);
3✔
2273
    }
2274
  }
2275

2276
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2277
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
157!
2278
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
22✔
2279
      int32_t len = strlen(pAlterReq->objname) + 1;
19✔
2280
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
19✔
2281
      if (pDb == NULL) {
19!
2282
        mndReleaseDb(pMnode, pDb);
×
2283
        TAOS_CHECK_GOTO(terrno, &lino, _OVER);  // TODO: refactor the terrno to code
×
2284
      }
2285
      code = taosHashRemove(pNewUser->writeDbs, pAlterReq->objname, len);
19✔
2286
      if (code < 0) {
19!
2287
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2288
      }
2289
      mndReleaseDb(pMnode, pDb);
19✔
2290
    } else {
2291
      taosHashClear(pNewUser->writeDbs);
3✔
2292
    }
2293
  }
2294

2295
  SHashObj *pReadTbs = pNewUser->readTbs;
179✔
2296
  SHashObj *pWriteTbs = pNewUser->writeTbs;
179✔
2297
  SHashObj *pAlterTbs = pNewUser->alterTbs;
179✔
2298

2299
#ifdef TD_ENTERPRISE
2300
  if (pAlterReq->isView) {
179✔
2301
    pReadTbs = pNewUser->readViews;
18✔
2302
    pWriteTbs = pNewUser->writeViews;
18✔
2303
    pAlterTbs = pNewUser->alterViews;
18✔
2304
  }
2305
#endif
2306

2307
  if (ALTER_USER_ADD_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2308
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
143!
2309
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
36!
2310
  }
2311

2312
  if (ALTER_USER_ADD_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2313
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
144!
2314
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
35!
2315
  }
2316

2317
  if (ALTER_USER_ADD_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2318
      ALTER_USER_ADD_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
164!
2319
    TAOS_CHECK_GOTO(mndTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
15!
2320
  }
2321

2322
  if (ALTER_USER_DEL_READ_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2323
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
173!
2324
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pReadTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
6!
2325
  }
2326

2327
  if (ALTER_USER_DEL_WRITE_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179✔
2328
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
174!
2329
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pWriteTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
5!
2330
  }
2331

2332
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
179!
2333
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
179!
2334
    TAOS_CHECK_GOTO(mndRemoveTablePriviledge(pMnode, pAlterTbs, pNewUser->useDbs, pAlterReq, pSdb), &lino, _OVER);
×
2335
  }
2336

2337
#ifdef USE_TOPIC
2338
  if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
179✔
2339
    int32_t      len = strlen(pAlterReq->objname) + 1;
15✔
2340
    SMqTopicObj *pTopic = NULL;
15✔
2341
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
15!
2342
      mndReleaseTopic(pMnode, pTopic);
×
2343
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2344
    }
2345
    if ((code = taosHashPut(pNewUser->topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN)) != 0) {
15!
2346
      mndReleaseTopic(pMnode, pTopic);
×
2347
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2348
    }
2349
    mndReleaseTopic(pMnode, pTopic);
15✔
2350
  }
2351

2352
  if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(pAlterReq->alterType, pAlterReq->privileges)) {
179✔
2353
    int32_t      len = strlen(pAlterReq->objname) + 1;
11✔
2354
    SMqTopicObj *pTopic = NULL;
11✔
2355
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
11✔
2356
      mndReleaseTopic(pMnode, pTopic);
1✔
2357
      TAOS_CHECK_GOTO(code, &lino, _OVER);
1!
2358
    }
2359
    code = taosHashRemove(pNewUser->topics, pAlterReq->objname, len);
10✔
2360
    if (code < 0) {
10!
2361
      mError("user:%s, failed to remove topic:%s since %s", pNewUser->user, pAlterReq->objname, tstrerror(code));
×
2362
    }
2363
    mndReleaseTopic(pMnode, pTopic);
10✔
2364
  }
2365
#endif
2366
_OVER:
168✔
2367
  if (code < 0) {
186✔
2368
    mError("user:%s, failed to alter user privileges at line %d since %s", pAlterReq->user, lino, tstrerror(code));
8!
2369
  }
2370
  TAOS_RETURN(code);
186✔
2371
}
2372

2373
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
317✔
2374
  SMnode       *pMnode = pReq->info.node;
317✔
2375
  SSdb         *pSdb = pMnode->pSdb;
317✔
2376
  void         *pIter = NULL;
317✔
2377
  int32_t       code = 0;
317✔
2378
  int32_t       lino = 0;
317✔
2379
  SUserObj     *pUser = NULL;
317✔
2380
  SUserObj     *pOperUser = NULL;
317✔
2381
  SUserObj      newUser = {0};
317✔
2382
  SAlterUserReq alterReq = {0};
317✔
2383

2384
  TAOS_CHECK_GOTO(tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq), &lino, _OVER);
317!
2385

2386
  mInfo("user:%s, start to alter", alterReq.user);
317!
2387

2388
  if (alterReq.user[0] == 0) {
317!
2389
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2390
  }
2391
  if(alterReq.passIsMd5 == 0){
317✔
2392
    if (TSDB_ALTER_USER_PASSWD == alterReq.alterType) {
281!
2393
      int32_t len = strlen(alterReq.pass);
×
2394
      if (mndCheckPasswordMinLen(alterReq.pass, len) != 0) {
×
2395
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY, &lino, _OVER);
×
2396
      }
2397
      if (mndCheckPasswordMaxLen(alterReq.pass, len) != 0) {
×
2398
        TAOS_CHECK_GOTO(TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG, &lino, _OVER);
×
2399
      }
2400
      if (mndCheckPasswordFmt(alterReq.pass, len) != 0) {
×
2401
        TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_PASS_FORMAT, &lino, _OVER);
×
2402
      }
2403
    }
2404
  }
2405

2406
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, alterReq.user, &pUser), &lino, _OVER);
317✔
2407

2408
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
295✔
2409
  if (pOperUser == NULL) {
295!
2410
    TAOS_CHECK_GOTO(TSDB_CODE_MND_NO_USER_FROM_CONN, &lino, _OVER);
×
2411
  }
2412

2413
  TAOS_CHECK_GOTO(mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq), &lino, _OVER);
295✔
2414

2415
  TAOS_CHECK_GOTO(mndUserDupObj(pUser, &newUser), &lino, _OVER);
247!
2416

2417
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
247✔
2418
    if (alterReq.passIsMd5 == 1) {
35!
2419
      (void)memcpy(newUser.pass, alterReq.pass, TSDB_PASSWORD_LEN);
35✔
2420
    } else {
2421
      taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), newUser.pass);
×
2422
    }
2423

2424
    if (0 != strncmp(pUser->pass, newUser.pass, TSDB_PASSWORD_LEN)) {
35✔
2425
      ++newUser.passVersion;
31✔
2426
    }
2427
  }
2428

2429
  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
247!
2430
    newUser.superUser = alterReq.superUser;
×
2431
  }
2432

2433
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
247✔
2434
    newUser.enable = alterReq.enable;
5✔
2435
  }
2436

2437
  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
247✔
2438
    newUser.sysInfo = alterReq.sysInfo;
14✔
2439
  }
2440

2441
  if (alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
247✔
2442
    newUser.createdb = alterReq.createdb;
5✔
2443
  }
2444

2445
  if (ALTER_USER_ADD_PRIVS(alterReq.alterType) || ALTER_USER_DEL_PRIVS(alterReq.alterType)) {
247✔
2446
    TAOS_CHECK_GOTO(mndProcessAlterUserPrivilegesReq(&alterReq, pMnode, &newUser), &lino, _OVER);
186✔
2447
  }
2448

2449
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WHITE_LIST) {
239✔
2450
    taosMemoryFreeClear(newUser.pIpWhiteList);
1!
2451

2452
    int32_t       num = pUser->pIpWhiteList->num + alterReq.numIpRanges;
1✔
2453
    int32_t       idx = pUser->pIpWhiteList->num;
1✔
2454
    SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
1!
2455

2456
    if (pNew == NULL) {
1!
2457
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2458
    }
2459

2460
    bool exist = false;
1✔
2461
    (void)memcpy(pNew->pIpRange, pUser->pIpWhiteList->pIpRange, sizeof(SIpV4Range) * idx);
1✔
2462
    for (int i = 0; i < alterReq.numIpRanges; i++) {
2✔
2463
      SIpV4Range *range = &(alterReq.pIpRanges[i]);
1✔
2464
      if (!isRangeInIpWhiteList(pUser->pIpWhiteList, range)) {
1!
2465
        // already exist, just ignore;
2466
        (void)memcpy(&pNew->pIpRange[idx], range, sizeof(SIpV4Range));
1✔
2467
        idx++;
1✔
2468
        continue;
1✔
2469
      } else {
2470
        exist = true;
×
2471
      }
2472
    }
2473
    if (exist) {
1!
2474
      taosMemoryFree(pNew);
×
2475
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_EXIST, &lino, _OVER);
×
2476
    }
2477
    pNew->num = idx;
1✔
2478
    newUser.pIpWhiteList = pNew;
1✔
2479
    newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
1✔
2480

2481
    if (pNew->num > MND_MAX_USE_HOST) {
1!
2482
      TAOS_CHECK_GOTO(TSDB_CODE_MND_TOO_MANY_USER_HOST, &lino, _OVER);
×
2483
    }
2484
  }
2485
  if (alterReq.alterType == TSDB_ALTER_USER_DROP_WHITE_LIST) {
239✔
2486
    taosMemoryFreeClear(newUser.pIpWhiteList);
1!
2487

2488
    int32_t       num = pUser->pIpWhiteList->num;
1✔
2489
    bool          noexist = true;
1✔
2490
    bool          localHost = false;
1✔
2491
    SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
1!
2492

2493
    if (pNew == NULL) {
1!
2494
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
2495
    }
2496

2497
    if (pUser->pIpWhiteList->num > 0) {
1!
2498
      int idx = 0;
1✔
2499
      for (int i = 0; i < pUser->pIpWhiteList->num; i++) {
3✔
2500
        SIpV4Range *oldRange = &pUser->pIpWhiteList->pIpRange[i];
2✔
2501
        bool        found = false;
2✔
2502
        for (int j = 0; j < alterReq.numIpRanges; j++) {
4✔
2503
          SIpV4Range *range = &alterReq.pIpRanges[j];
2✔
2504
          if (isDefaultRange(range)) {
2!
2505
            localHost = true;
×
2506
            break;
×
2507
          }
2508
          if (isIpRangeEqual(oldRange, range)) {
2!
2509
            found = true;
×
2510
            break;
×
2511
          }
2512
        }
2513
        if (localHost) break;
2!
2514

2515
        if (found == false) {
2!
2516
          (void)memcpy(&pNew->pIpRange[idx], oldRange, sizeof(SIpV4Range));
2✔
2517
          idx++;
2✔
2518
        } else {
2519
          noexist = false;
×
2520
        }
2521
      }
2522
      pNew->num = idx;
1✔
2523
      newUser.pIpWhiteList = pNew;
1✔
2524
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
1✔
2525

2526
    } else {
2527
      pNew->num = 0;
×
2528
      newUser.pIpWhiteList = pNew;
×
2529
      newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
×
2530
    }
2531

2532
    if (localHost) {
1!
2533
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_LOCAL_HOST_NOT_DROP, &lino, _OVER);
×
2534
    }
2535
    if (noexist) {
1!
2536
      TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_HOST_NOT_EXIST, &lino, _OVER);
1!
2537
    }
2538
  }
2539

2540
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
238✔
2541
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
238!
2542

2543
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
238✔
2544
    char detail[1000] = {0};
35✔
2545
    (void)tsnprintf(detail, sizeof(detail),
35✔
2546
              "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, createdb:%d, tabName:%s, password:xxx",
2547
              mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
35✔
2548
              alterReq.createdb ? 1 : 0, alterReq.tabName);
35✔
2549
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
35✔
2550
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
203!
2551
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
198✔
2552
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
24✔
2553
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
179✔
2554
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
139✔
2555
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
126!
2556
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
126!
2557
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
90!
2558
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
67!
2559
    if (strcmp(alterReq.objname, "1.*") != 0) {
112✔
2560
      SName name = {0};
108✔
2561
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
108!
2562
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
108✔
2563
                  alterReq.sqlLen);
2564
    } else {
2565
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
4✔
2566
    }
2567
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
67✔
2568
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
15✔
2569
                alterReq.sqlLen);
2570
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
52✔
2571
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
10✔
2572
                alterReq.sqlLen);
2573
  } else {
2574
    if (strcmp(alterReq.objname, "1.*") != 0) {
42✔
2575
      SName name = {0};
38✔
2576
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
38✔
2577
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
37✔
2578
                  alterReq.sqlLen);
2579
    } else {
2580
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
4✔
2581
    }
2582
  }
2583

2584
_OVER:
317✔
2585
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
317✔
2586
    mError("user:%s, failed to alter at line %d since %s", alterReq.user, lino, tstrerror(code));
80!
2587
  }
2588

2589
  tFreeSAlterUserReq(&alterReq);
317✔
2590
  mndReleaseUser(pMnode, pOperUser);
317✔
2591
  mndReleaseUser(pMnode, pUser);
317✔
2592
  mndUserFreeObj(&newUser);
317✔
2593

2594
  TAOS_RETURN(code);
317✔
2595
}
2596

2597
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
50✔
2598
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
50✔
2599
  if (pTrans == NULL) {
50!
2600
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
×
2601
    TAOS_RETURN(terrno);
×
2602
  }
2603
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
50!
2604

2605
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
50✔
2606
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
50!
2607
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
2608
    mndTransDrop(pTrans);
×
2609
    TAOS_RETURN(terrno);
×
2610
  }
2611
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
50!
2612
    mndTransDrop(pTrans);
×
2613
    TAOS_RETURN(terrno);
×
2614
  }
2615

2616
  if (mndTransPrepare(pMnode, pTrans) != 0) {
50!
2617
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
2618
    mndTransDrop(pTrans);
×
2619
    TAOS_RETURN(terrno);
×
2620
  }
2621
  (void)ipWhiteMgtRemove(pUser->user);
50✔
2622

2623
  mndTransDrop(pTrans);
50✔
2624
  TAOS_RETURN(0);
50✔
2625
}
2626

2627
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
50✔
2628
  SMnode      *pMnode = pReq->info.node;
50✔
2629
  int32_t      code = 0;
50✔
2630
  int32_t      lino = 0;
50✔
2631
  SUserObj    *pUser = NULL;
50✔
2632
  SDropUserReq dropReq = {0};
50✔
2633

2634
  TAOS_CHECK_GOTO(tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
50!
2635

2636
  mInfo("user:%s, start to drop", dropReq.user);
50!
2637
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
50!
2638

2639
  if (dropReq.user[0] == 0) {
50!
2640
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2641
  }
2642

2643
  TAOS_CHECK_GOTO(mndAcquireUser(pMnode, dropReq.user, &pUser), &lino, _OVER);
50!
2644

2645
  TAOS_CHECK_GOTO(mndDropUser(pMnode, pReq, pUser), &lino, _OVER);
50!
2646
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
50!
2647

2648
  auditRecord(pReq, pMnode->clusterId, "dropUser", "", dropReq.user, dropReq.sql, dropReq.sqlLen);
50✔
2649

2650
_OVER:
50✔
2651
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
50!
2652
    mError("user:%s, failed to drop at line %d since %s", dropReq.user, lino, tstrerror(code));
×
2653
  }
2654

2655
  mndReleaseUser(pMnode, pUser);
50✔
2656
  tFreeSDropUserReq(&dropReq);
50✔
2657
  TAOS_RETURN(code);
50✔
2658
}
2659

2660
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
400,937✔
2661
  SMnode         *pMnode = pReq->info.node;
400,937✔
2662
  int32_t         code = 0;
400,937✔
2663
  int32_t         lino = 0;
400,937✔
2664
  int32_t         contLen = 0;
400,937✔
2665
  void           *pRsp = NULL;
400,937✔
2666
  SUserObj       *pUser = NULL;
400,937✔
2667
  SGetUserAuthReq authReq = {0};
400,937✔
2668
  SGetUserAuthRsp authRsp = {0};
400,937✔
2669

2670
  TAOS_CHECK_EXIT(tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq));
400,937!
2671
  mTrace("user:%s, start to get auth", authReq.user);
400,938✔
2672

2673
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
400,938✔
2674

2675
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
400,933!
2676

2677
  contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
400,935✔
2678
  if (contLen < 0) {
400,922!
2679
    TAOS_CHECK_EXIT(contLen);
×
2680
  }
2681
  pRsp = rpcMallocCont(contLen);
400,922✔
2682
  if (pRsp == NULL) {
400,929!
2683
    TAOS_CHECK_EXIT(terrno);
×
2684
  }
2685

2686
  contLen = tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
400,929✔
2687
  if (contLen < 0) {
400,932!
2688
    TAOS_CHECK_EXIT(contLen);
×
2689
  }
2690

2691
_exit:
400,932✔
2692
  mndReleaseUser(pMnode, pUser);
400,935✔
2693
  tFreeSGetUserAuthRsp(&authRsp);
400,938✔
2694
  if (code < 0) {
400,938✔
2695
    mError("user:%s, failed to get auth at line %d since %s", authReq.user, lino, tstrerror(code));
3!
2696
    rpcFreeCont(pRsp);
3✔
2697
    pRsp = NULL;
3✔
2698
    contLen = 0;
3✔
2699
  }
2700
  pReq->info.rsp = pRsp;
400,938✔
2701
  pReq->info.rspLen = contLen;
400,938✔
2702
  pReq->code = code;
400,938✔
2703

2704
  TAOS_RETURN(code);
400,938✔
2705
}
2706

2707
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
5,062✔
2708
  SMnode   *pMnode = pReq->info.node;
5,062✔
2709
  SSdb     *pSdb = pMnode->pSdb;
5,062✔
2710
  int32_t   code = 0;
5,062✔
2711
  int32_t   lino = 0;
5,062✔
2712
  int32_t   numOfRows = 0;
5,062✔
2713
  SUserObj *pUser = NULL;
5,062✔
2714
  int32_t   cols = 0;
5,062✔
2715
  int8_t    flag = 0;
5,062✔
2716
  char     *pWrite = NULL;
5,062✔
2717
  char     *buf = NULL;
5,062✔
2718
  char     *varstr = NULL;
5,062✔
2719

2720
  while (numOfRows < rows) {
10,192!
2721
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
10,194✔
2722
    if (pShow->pIter == NULL) break;
10,204✔
2723

2724
    cols = 0;
5,128✔
2725
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,128✔
2726
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
5,126✔
2727
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
5,126✔
2728
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
5,126!
2729

2730
    cols++;
5,118✔
2731
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,118✔
2732
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
5,116!
2733

2734
    cols++;
5,121✔
2735
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,121✔
2736
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
5,119!
2737

2738
    cols++;
5,120✔
2739
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,120✔
2740
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
5,116!
2741

2742
    cols++;
5,117✔
2743
    flag = pUser->createdb ? 1 : 0;
5,117✔
2744
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,117✔
2745
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
5,117!
2746

2747
    cols++;
5,120✔
2748
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,120✔
2749
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->createdTime, false, pUser, _exit);
5,119!
2750

2751
    cols++;
5,122✔
2752

2753
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf);
5,122✔
2754
    // int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf);
2755
    if (tlen != 0) {
5,124!
2756
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
5,125!
2757
      if (varstr == NULL) {
5,125!
2758
        sdbRelease(pSdb, pUser);
×
2759
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
2760
      }
2761
      varDataSetLen(varstr, tlen);
5,125✔
2762
      (void)memcpy(varDataVal(varstr), buf, tlen);
5,125✔
2763

2764
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
5,125✔
2765
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
5,124!
2766

2767
      taosMemoryFreeClear(buf);
5,124!
2768
    } else {
2769
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2770
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2771
    }
2772

2773
    numOfRows++;
5,128✔
2774
    sdbRelease(pSdb, pUser);
5,128✔
2775
  }
2776

2777
  pShow->numOfRows += numOfRows;
5,074✔
2778
_exit:
5,074✔
2779
  taosMemoryFreeClear(buf);
5,074!
2780
  taosMemoryFreeClear(varstr);
5,074!
2781
  if (code < 0) {
5,073!
2782
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2783
    TAOS_RETURN(code);
×
2784
  }
2785
  return numOfRows;
5,073✔
2786
}
2787

2788
static int32_t mndRetrieveUsersFull(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2789
  int32_t numOfRows = 0;
×
2790
#ifdef TD_ENTERPRISE
2791
  SMnode   *pMnode = pReq->info.node;
×
2792
  SSdb     *pSdb = pMnode->pSdb;
×
2793
  SUserObj *pUser = NULL;
×
2794
  int32_t   code = 0;
×
2795
  int32_t   lino = 0;
×
2796
  int32_t   cols = 0;
×
2797
  int8_t    flag = 0;
×
2798
  char     *pWrite = NULL;
×
2799
  char     *buf = NULL;
×
2800
  char     *varstr = NULL;
×
2801

2802
  while (numOfRows < rows) {
×
2803
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
2804
    if (pShow->pIter == NULL) break;
×
2805

2806
    cols = 0;
×
2807
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2808
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
2809
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
2810
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pUser, _exit);
×
2811

2812
    cols++;
×
2813
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2814
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->superUser, false, pUser, _exit);
×
2815

2816
    cols++;
×
2817
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2818
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->enable, false, pUser, _exit);
×
2819

2820
    cols++;
×
2821
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2822
    COL_DATA_SET_VAL_GOTO((const char *)&pUser->sysInfo, false, pUser, _exit);
×
2823

2824
    cols++;
×
2825
    flag = pUser->createdb ? 1 : 0;
×
2826
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2827
    COL_DATA_SET_VAL_GOTO((const char *)&flag, false, pUser, _exit);
×
2828

2829
    // mInfo("pUser->pass:%s", pUser->pass);
2830
    cols++;
×
2831
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2832
    char pass[TSDB_PASSWORD_LEN + VARSTR_HEADER_SIZE] = {0};
×
2833
    STR_WITH_MAXSIZE_TO_VARSTR(pass, pUser->pass, pShow->pMeta->pSchemas[cols].bytes);
×
2834
    COL_DATA_SET_VAL_GOTO((const char *)pass, false, pUser, _exit);
×
2835

2836
    cols++;
×
2837

2838
    int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf);
×
2839
    // int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf);
2840
    if (tlen != 0) {
×
2841
      TAOS_MEMORY_REALLOC(varstr, VARSTR_HEADER_SIZE + tlen);
×
2842
      if (varstr == NULL) {
×
2843
        sdbRelease(pSdb, pUser);
×
2844
        TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
2845
      }
2846
      varDataSetLen(varstr, tlen);
×
2847
      (void)memcpy(varDataVal(varstr), buf, tlen);
×
2848

2849
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2850
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
×
2851

2852
      taosMemoryFreeClear(buf);
×
2853
    } else {
2854
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2855
      COL_DATA_SET_VAL_GOTO((const char *)NULL, true, pUser, _exit);
×
2856
    }
2857

2858
    numOfRows++;
×
2859
    sdbRelease(pSdb, pUser);
×
2860
  }
2861

2862
  pShow->numOfRows += numOfRows;
×
2863
_exit:
×
2864
  taosMemoryFreeClear(buf);
×
2865
  taosMemoryFreeClear(varstr);
×
2866
  if (code < 0) {
×
2867
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2868
    TAOS_RETURN(code);
×
2869
  }
2870
#endif
2871
  return numOfRows;
×
2872
}
2873

2874
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
×
2875
  SSdb *pSdb = pMnode->pSdb;
×
2876
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
2877
}
×
2878

2879
static int32_t mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *pNumOfRows, SSdb *pSdb,
26,362✔
2880
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
2881
  char   *value = taosHashIterate(hash, NULL);
26,362✔
2882
  char   *user = pUser->user;
26,374✔
2883
  int32_t code = 0;
26,374✔
2884
  int32_t lino = 0;
26,374✔
2885
  int32_t cols = 0;
26,374✔
2886
  int32_t numOfRows = *pNumOfRows;
26,374✔
2887

2888
  while (value != NULL) {
26,377✔
2889
    cols = 0;
3✔
2890
    char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
3✔
2891
    STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
3✔
2892
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2893
    COL_DATA_SET_VAL_GOTO((const char *)userName, false, NULL, _exit);
3!
2894

2895
    char privilege[20] = {0};
3✔
2896
    STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
3✔
2897
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2898
    COL_DATA_SET_VAL_GOTO((const char *)privilege, false, NULL, _exit);
3!
2899

2900
    size_t keyLen = 0;
3✔
2901
    void  *key = taosHashGetKey(value, &keyLen);
3✔
2902

2903
    char dbName[TSDB_DB_NAME_LEN] = {0};
3✔
2904
    (void)mndExtractShortDbNameFromStbFullName(key, dbName);
3✔
2905
    char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
3✔
2906
    STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
3✔
2907
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2908
    COL_DATA_SET_VAL_GOTO((const char *)dbNameContent, false, NULL, _exit);
3!
2909

2910
    char tableName[TSDB_TABLE_NAME_LEN] = {0};
3✔
2911
    mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
3✔
2912
    char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
3✔
2913
    STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
3✔
2914
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2915
    COL_DATA_SET_VAL_GOTO((const char *)tableNameContent, false, NULL, _exit);
3!
2916

2917
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
3!
2918
      SNode  *pAst = NULL;
×
2919
      int32_t sqlLen = 0;
×
2920
      size_t  bufSz = strlen(value) + 1;
×
2921
      if (bufSz < 6) bufSz = 6;
×
2922
      TAOS_MEMORY_REALLOC(*sql, bufSz);
×
2923
      if (*sql == NULL) {
×
2924
        code = terrno;
×
2925
        goto _exit;
×
2926
      }
2927
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
×
2928
      if ((*condition) == NULL) {
×
2929
        code = terrno;
×
2930
        goto _exit;
×
2931
      }
2932

2933
      if (nodesStringToNode(value, &pAst) == 0) {
×
2934
        if (nodesNodeToSQL(pAst, *sql, bufSz, &sqlLen) != 0) {
×
2935
          sqlLen = 5;
×
2936
          (void)tsnprintf(*sql, bufSz, "error");
×
2937
        }
2938
        nodesDestroyNode(pAst);
×
2939
      } else {
2940
        sqlLen = 5;
×
2941
        (void)tsnprintf(*sql, bufSz, "error");
×
2942
      }
2943

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

2946
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2947
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
×
2948

2949
      char notes[2] = {0};
×
2950
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
×
2951
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2952
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
×
2953
    } else {
2954
      TAOS_MEMORY_REALLOC(*condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
3!
2955
      if ((*condition) == NULL) {
3!
2956
        code = terrno;
×
2957
        goto _exit;
×
2958
      }
2959
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
3✔
2960
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2961
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
3!
2962

2963
      char notes[64 + VARSTR_HEADER_SIZE] = {0};
3✔
2964
      STR_WITH_MAXSIZE_TO_VARSTR(notes, value[0] == 'v' ? "view" : "", sizeof(notes));
3!
2965
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3✔
2966
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, NULL, _exit);
3!
2967
    }
2968

2969
    numOfRows++;
3✔
2970
    value = taosHashIterate(hash, value);
3✔
2971
  }
2972
  *pNumOfRows = numOfRows;
26,374✔
2973
_exit:
26,374✔
2974
  if (code < 0) {
26,374!
2975
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2976
    sdbRelease(pSdb, pUser);
×
2977
  }
2978
  TAOS_RETURN(code);
26,374✔
2979
}
2980

2981
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
4,336✔
2982
  int32_t   code = 0;
4,336✔
2983
  int32_t   lino = 0;
4,336✔
2984
  SMnode   *pMnode = pReq->info.node;
4,336✔
2985
  SSdb     *pSdb = pMnode->pSdb;
4,336✔
2986
  int32_t   numOfRows = 0;
4,336✔
2987
  SUserObj *pUser = NULL;
4,336✔
2988
  int32_t   cols = 0;
4,336✔
2989
  char     *pWrite = NULL;
4,336✔
2990
  char     *condition = NULL;
4,336✔
2991
  char     *sql = NULL;
4,336✔
2992

2993
  bool fetchNextUser = pShow->restore ? false : true;
4,336✔
2994
  pShow->restore = false;
4,336✔
2995

2996
  while (numOfRows < rows) {
8,742!
2997
    if (fetchNextUser) {
8,742!
2998
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
8,742✔
2999
      if (pShow->pIter == NULL) break;
8,748✔
3000
    } else {
3001
      fetchNextUser = true;
×
3002
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
3003
      pUser = sdbAcquire(pSdb, SDB_USER, pKey);
×
3004
      if (!pUser) {
×
3005
        continue;
×
3006
      }
3007
    }
3008

3009
    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
4,407✔
3010
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
4,406✔
3011
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
4,405✔
3012
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
4,405✔
3013
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
4,404✔
3014
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
4,403✔
3015
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
4,403✔
3016
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
4,404✔
3017
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
4,405✔
3018
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
4,404✔
3019
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
4,404!
3020
        rows) {
3021
      mInfo(
×
3022
          "will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
3023
          "%d, alter tables %d, read views %d, write views %d, alter views %d",
3024
          numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs, numOfAlterTbs,
3025
          numOfReadViews, numOfWriteViews, numOfAlterViews);
3026
      pShow->restore = true;
×
3027
      sdbRelease(pSdb, pUser);
×
3028
      break;
×
3029
    }
3030

3031
    if (pUser->superUser) {
4,404✔
3032
      cols = 0;
4,338✔
3033
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
4,338✔
3034
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
4,338✔
3035
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,338✔
3036
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
4,333!
3037

3038
      char privilege[20] = {0};
4,337✔
3039
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
4,337✔
3040
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,337✔
3041
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
4,338!
3042

3043
      char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4,331✔
3044
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
4,331✔
3045
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,331✔
3046
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
4,332!
3047

3048
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4,335✔
3049
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
4,335✔
3050
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,335✔
3051
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
4,337!
3052

3053
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
4,338!
3054
      if (condition == NULL) {
4,340!
3055
        sdbRelease(pSdb, pUser);
×
3056
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3057
      }
3058
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
4,340✔
3059
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,340✔
3060
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
4,338!
3061

3062
      char notes[2] = {0};
4,338✔
3063
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
4,338✔
3064
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4,338✔
3065
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
4,336!
3066

3067
      numOfRows++;
4,335✔
3068
    }
3069

3070
    char *db = taosHashIterate(pUser->readDbs, NULL);
4,401✔
3071
    while (db != NULL) {
4,420✔
3072
      cols = 0;
19✔
3073
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3074
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19✔
3075
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3076
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
19!
3077

3078
      char privilege[20] = {0};
19✔
3079
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
19✔
3080
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3081
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
19!
3082

3083
      SName name = {0};
19✔
3084
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3085
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19✔
3086
      if (code < 0) {
19!
3087
        sdbRelease(pSdb, pUser);
×
3088
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3089
      }
3090
      (void)tNameGetDbName(&name, varDataVal(objName));
19✔
3091
      varDataSetLen(objName, strlen(varDataVal(objName)));
19✔
3092
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3093
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
19!
3094

3095
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3096
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3097
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3098
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
19!
3099

3100
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19!
3101
      if (condition == NULL) {
19!
3102
        sdbRelease(pSdb, pUser);
×
3103
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3104
      }
3105
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3106
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3107
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
19!
3108

3109
      char notes[2] = {0};
19✔
3110
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19✔
3111
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3112
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
19!
3113

3114
      numOfRows++;
19✔
3115
      db = taosHashIterate(pUser->readDbs, db);
19✔
3116
    }
3117

3118
    db = taosHashIterate(pUser->writeDbs, NULL);
4,401✔
3119
    while (db != NULL) {
4,419✔
3120
      cols = 0;
19✔
3121
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3122
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
19✔
3123
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3124
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
19!
3125

3126
      char privilege[20] = {0};
19✔
3127
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
19✔
3128
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3129
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
19!
3130

3131
      SName name = {0};
19✔
3132
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3133
      code = tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
19✔
3134
      if (code < 0) {
19!
3135
        sdbRelease(pSdb, pUser);
×
3136
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3137
      }
3138
      (void)tNameGetDbName(&name, varDataVal(objName));
19✔
3139
      varDataSetLen(objName, strlen(varDataVal(objName)));
19✔
3140
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3141
      COL_DATA_SET_VAL_GOTO((const char *)objName, false, pUser, _exit);
19!
3142

3143
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
19✔
3144
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3145
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3146
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
19!
3147

3148
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
19!
3149
      if (condition == NULL) {
19!
3150
        sdbRelease(pSdb, pUser);
×
3151
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3152
      }
3153
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
19✔
3154
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3155
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
19!
3156

3157
      char notes[2] = {0};
19✔
3158
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
19✔
3159
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
19✔
3160
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
19!
3161

3162
      numOfRows++;
19✔
3163
      db = taosHashIterate(pUser->writeDbs, db);
19✔
3164
    }
3165

3166
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,400!
3167

3168
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,396!
3169

3170
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterTbs, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,401!
3171

3172
    TAOS_CHECK_EXIT(mndLoopHash(pUser->readViews, "read", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,401!
3173

3174
    TAOS_CHECK_EXIT(mndLoopHash(pUser->writeViews, "write", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,401!
3175

3176
    TAOS_CHECK_EXIT(mndLoopHash(pUser->alterViews, "alter", pBlock, &numOfRows, pSdb, pUser, pShow, &condition, &sql));
4,402!
3177

3178
    char *topic = taosHashIterate(pUser->topics, NULL);
4,402✔
3179
    while (topic != NULL) {
4,409✔
3180
      cols = 0;
8✔
3181
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
8✔
3182
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
8✔
3183
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3184
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
8!
3185

3186
      char privilege[20] = {0};
8✔
3187
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
8✔
3188
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3189
      COL_DATA_SET_VAL_GOTO((const char *)privilege, false, pUser, _exit);
8!
3190

3191
      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
8✔
3192
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
8✔
3193
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
8✔
3194
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3195
      COL_DATA_SET_VAL_GOTO((const char *)topicName, false, pUser, _exit);
8!
3196

3197
      char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
8✔
3198
      STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
8✔
3199
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3200
      COL_DATA_SET_VAL_GOTO((const char *)tableName, false, pUser, _exit);
8!
3201

3202
      TAOS_MEMORY_REALLOC(condition, TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
8!
3203
      if (condition == NULL) {
8!
3204
        sdbRelease(pSdb, pUser);
×
3205
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
3206
      }
3207
      STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
8✔
3208
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3209
      COL_DATA_SET_VAL_GOTO((const char *)condition, false, pUser, _exit);
8!
3210

3211
      char notes[2] = {0};
8✔
3212
      STR_WITH_MAXSIZE_TO_VARSTR(notes, "", sizeof(notes));
8✔
3213
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
8✔
3214
      COL_DATA_SET_VAL_GOTO((const char *)notes, false, pUser, _exit);
8!
3215

3216
      numOfRows++;
8✔
3217
      topic = taosHashIterate(pUser->topics, topic);
8✔
3218
    }
3219

3220
    sdbRelease(pSdb, pUser);
4,401✔
3221
  }
3222

3223
  pShow->numOfRows += numOfRows;
4,341✔
3224
_exit:
4,341✔
3225
  taosMemoryFreeClear(condition);
4,341!
3226
  taosMemoryFreeClear(sql);
4,341!
3227
  if (code < 0) {
4,341!
3228
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3229
    TAOS_RETURN(code);
×
3230
  }
3231
  return numOfRows;
4,341✔
3232
}
3233

3234
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
3235
  SSdb *pSdb = pMnode->pSdb;
×
3236
  sdbCancelFetchByType(pSdb, pIter, SDB_USER);
×
3237
}
×
3238

3239
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
244,995✔
3240
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
3241
  int32_t           code = 0;
244,995✔
3242
  int32_t           lino = 0;
244,995✔
3243
  int32_t           rspLen = 0;
244,995✔
3244
  void             *pRsp = NULL;
244,995✔
3245
  SUserAuthBatchRsp batchRsp = {0};
244,995✔
3246

3247
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
244,995✔
3248
  if (batchRsp.pArray == NULL) {
244,991!
3249
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3250
  }
3251

3252
  for (int32_t i = 0; i < numOfUses; ++i) {
490,208✔
3253
    SUserObj *pUser = NULL;
245,210✔
3254
    code = mndAcquireUser(pMnode, pUsers[i].user, &pUser);
245,210✔
3255
    if (pUser == NULL) {
245,208✔
3256
      if (TSDB_CODE_MND_USER_NOT_EXIST == code) {
20!
3257
        SGetUserAuthRsp rsp = {.dropped = 1};
20✔
3258
        (void)memcpy(rsp.user, pUsers[i].user, TSDB_USER_LEN);
20✔
3259
        TSDB_CHECK_NULL(taosArrayPush(batchRsp.pArray, &rsp), code, lino, _OVER, TSDB_CODE_OUT_OF_MEMORY);
40!
3260
      }
3261
      mError("user:%s, failed to auth user since %s", pUsers[i].user, tstrerror(code));
20!
3262
      code = 0;
20✔
3263
      continue;
229,539✔
3264
    }
3265

3266
    pUsers[i].version = ntohl(pUsers[i].version);
245,188✔
3267
    if (pUser->authVersion <= pUsers[i].version && ipWhiteListVer == pMnode->ipWhiteVer) {
245,188✔
3268
      mndReleaseUser(pMnode, pUser);
229,516✔
3269
      continue;
229,519✔
3270
    }
3271

3272
    SGetUserAuthRsp rsp = {0};
15,672✔
3273
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
15,672✔
3274
    if (code) {
15,678!
3275
      mndReleaseUser(pMnode, pUser);
×
3276
      tFreeSGetUserAuthRsp(&rsp);
×
3277
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3278
    }
3279

3280
    if (!(taosArrayPush(batchRsp.pArray, &rsp))) {
31,356!
3281
      code = terrno;
×
3282
      mndReleaseUser(pMnode, pUser);
×
3283
      tFreeSGetUserAuthRsp(&rsp);
×
3284
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
3285
    }
3286
    mndReleaseUser(pMnode, pUser);
15,678✔
3287
  }
3288

3289
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
244,998✔
3290
    *ppRsp = NULL;
229,497✔
3291
    *pRspLen = 0;
229,497✔
3292

3293
    tFreeSUserAuthBatchRsp(&batchRsp);
229,497✔
3294
    return 0;
229,496✔
3295
  }
3296

3297
  rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
15,506✔
3298
  if (rspLen < 0) {
15,506!
3299
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3300
  }
3301
  pRsp = taosMemoryMalloc(rspLen);
15,506!
3302
  if (pRsp == NULL) {
15,506!
3303
    TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
3304
  }
3305
  rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
15,506✔
3306
  if (rspLen < 0) {
15,506!
3307
    TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
×
3308
  }
3309
_OVER:
15,506✔
3310
  tFreeSUserAuthBatchRsp(&batchRsp);
15,506✔
3311
  if (code < 0) {
15,506!
3312
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3313
    taosMemoryFreeClear(pRsp);
×
3314
    rspLen = 0;
×
3315
  }
3316
  *ppRsp = pRsp;
15,506✔
3317
  *pRspLen = rspLen;
15,506✔
3318

3319
  TAOS_RETURN(code);
15,506✔
3320
}
3321

3322
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
2,258✔
3323
  int32_t   code = 0;
2,258✔
3324
  int32_t   lino = 0;
2,258✔
3325
  SSdb     *pSdb = pMnode->pSdb;
2,258✔
3326
  int32_t   len = strlen(db) + 1;
2,258✔
3327
  void     *pIter = NULL;
2,258✔
3328
  SUserObj *pUser = NULL;
2,258✔
3329
  SUserObj  newUser = {0};
2,258✔
3330

3331
  while (1) {
2,447✔
3332
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
4,705✔
3333
    if (pIter == NULL) break;
4,705✔
3334

3335
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
2,447!
3336
      break;
×
3337
    }
3338

3339
    bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
2,447✔
3340
    bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
2,447✔
3341
    if (inRead || inWrite) {
2,447!
3342
      code = taosHashRemove(newUser.readDbs, db, len);
2✔
3343
      if (code < 0) {
2!
3344
        mError("failed to remove readDbs:%s from user:%s", db, pUser->user);
×
3345
      }
3346
      code = taosHashRemove(newUser.writeDbs, db, len);
2✔
3347
      if (code < 0) {
2!
3348
        mError("failed to remove writeDbs:%s from user:%s", db, pUser->user);
×
3349
      }
3350

3351
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
2✔
3352
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
2!
3353
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3354
        break;
×
3355
      }
3356
      TAOS_CHECK_GOTO(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY), &lino, _OVER);
2!
3357
    }
3358

3359
    mndUserFreeObj(&newUser);
2,447✔
3360
    sdbRelease(pSdb, pUser);
2,447✔
3361
  }
3362

3363
_OVER:
2,258✔
3364
  if (pUser != NULL) sdbRelease(pSdb, pUser);
2,258!
3365
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
2,258!
3366
  mndUserFreeObj(&newUser);
2,258✔
3367
  TAOS_RETURN(code);
2,258✔
3368
}
3369

3370
int32_t mndUserRemoveStb(SMnode *pMnode, STrans *pTrans, char *stb) {
1,150✔
3371
  int32_t   code = 0;
1,150✔
3372
  SSdb     *pSdb = pMnode->pSdb;
1,150✔
3373
  int32_t   len = strlen(stb) + 1;
1,150✔
3374
  void     *pIter = NULL;
1,150✔
3375
  SUserObj *pUser = NULL;
1,150✔
3376
  SUserObj  newUser = {0};
1,150✔
3377

3378
  while (1) {
1,150✔
3379
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
2,300✔
3380
    if (pIter == NULL) break;
2,300✔
3381

3382
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
1,150!
3383
      break;
×
3384
    }
3385

3386
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
1,150✔
3387
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
1,150✔
3388
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
1,150✔
3389
    if (inRead || inWrite || inAlter) {
1,150!
3390
      code = taosHashRemove(newUser.readTbs, stb, len);
×
3391
      if (code < 0) {
×
3392
        mError("failed to remove readTbs:%s from user:%s", stb, pUser->user);
×
3393
      }
3394
      code = taosHashRemove(newUser.writeTbs, stb, len);
×
3395
      if (code < 0) {
×
3396
        mError("failed to remove writeTbs:%s from user:%s", stb, pUser->user);
×
3397
      }
3398
      code = taosHashRemove(newUser.alterTbs, stb, len);
×
3399
      if (code < 0) {
×
3400
        mError("failed to remove alterTbs:%s from user:%s", stb, pUser->user);
×
3401
      }
3402

3403
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3404
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3405
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3406
        break;
×
3407
      }
3408
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3409
      if (code != 0) {
×
3410
        mndUserFreeObj(&newUser);
×
3411
        sdbRelease(pSdb, pUser);
×
3412
        TAOS_RETURN(code);
×
3413
      }
3414
    }
3415

3416
    mndUserFreeObj(&newUser);
1,150✔
3417
    sdbRelease(pSdb, pUser);
1,150✔
3418
  }
3419

3420
  if (pUser != NULL) sdbRelease(pSdb, pUser);
1,150!
3421
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
1,150!
3422
  mndUserFreeObj(&newUser);
1,150✔
3423
  TAOS_RETURN(code);
1,150✔
3424
}
3425

3426
int32_t mndUserRemoveView(SMnode *pMnode, STrans *pTrans, char *view) {
172✔
3427
  int32_t   code = 0;
172✔
3428
  SSdb     *pSdb = pMnode->pSdb;
172✔
3429
  int32_t   len = strlen(view) + 1;
172✔
3430
  void     *pIter = NULL;
172✔
3431
  SUserObj *pUser = NULL;
172✔
3432
  SUserObj  newUser = {0};
172✔
3433

3434
  while (1) {
181✔
3435
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
353✔
3436
    if (pIter == NULL) break;
353✔
3437

3438
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
181!
3439
      break;
×
3440
    }
3441

3442
    bool inRead = (taosHashGet(newUser.readViews, view, len) != NULL);
181✔
3443
    bool inWrite = (taosHashGet(newUser.writeViews, view, len) != NULL);
181✔
3444
    bool inAlter = (taosHashGet(newUser.alterViews, view, len) != NULL);
181✔
3445
    if (inRead || inWrite || inAlter) {
181!
3446
      code = taosHashRemove(newUser.readViews, view, len);
3✔
3447
      if (code < 0) {
3!
3448
        mError("failed to remove readViews:%s from user:%s", view, pUser->user);
×
3449
      }
3450
      code = taosHashRemove(newUser.writeViews, view, len);
3✔
3451
      if (code < 0) {
3!
3452
        mError("failed to remove writeViews:%s from user:%s", view, pUser->user);
3!
3453
      }
3454
      code = taosHashRemove(newUser.alterViews, view, len);
3✔
3455
      if (code < 0) {
3!
3456
        mError("failed to remove alterViews:%s from user:%s", view, pUser->user);
3!
3457
      }
3458

3459
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
3✔
3460
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
3!
3461
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3462
        break;
×
3463
      }
3464
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
3✔
3465
      if (code < 0) {
3!
3466
        mndUserFreeObj(&newUser);
×
3467
        sdbRelease(pSdb, pUser);
×
3468
        TAOS_RETURN(code);
×
3469
      }
3470
    }
3471

3472
    mndUserFreeObj(&newUser);
181✔
3473
    sdbRelease(pSdb, pUser);
181✔
3474
  }
3475

3476
  if (pUser != NULL) sdbRelease(pSdb, pUser);
172!
3477
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
172!
3478
  mndUserFreeObj(&newUser);
172✔
3479
  TAOS_RETURN(code);
172✔
3480
}
3481

3482
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
339✔
3483
  int32_t   code = 0;
339✔
3484
  SSdb     *pSdb = pMnode->pSdb;
339✔
3485
  int32_t   len = strlen(topic) + 1;
339✔
3486
  void     *pIter = NULL;
339✔
3487
  SUserObj *pUser = NULL;
339✔
3488
  SUserObj  newUser = {0};
339✔
3489

3490
  while (1) {
342✔
3491
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
681✔
3492
    if (pIter == NULL) {
681✔
3493
      break;
339✔
3494
    }
3495

3496
    if ((code = mndUserDupObj(pUser, &newUser)) != 0) {
342!
3497
      break;
×
3498
    }
3499

3500
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
342✔
3501
    if (inTopic) {
342!
3502
      code = taosHashRemove(newUser.topics, topic, len);
×
3503
      if (code < 0) {
×
3504
        mError("failed to remove topic:%s from user:%s", topic, pUser->user);
×
3505
      }
3506
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
×
3507
      if (pCommitRaw == NULL || (code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
3508
        code = TSDB_CODE_OUT_OF_MEMORY;
×
3509
        break;
×
3510
      }
3511
      code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
×
3512
      if (code < 0) {
×
3513
        mndUserFreeObj(&newUser);
×
3514
        sdbRelease(pSdb, pUser);
×
3515
        TAOS_RETURN(code);
×
3516
      }
3517
    }
3518

3519
    mndUserFreeObj(&newUser);
342✔
3520
    sdbRelease(pSdb, pUser);
342✔
3521
  }
3522

3523
  if (pUser != NULL) sdbRelease(pSdb, pUser);
339!
3524
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
339!
3525
  mndUserFreeObj(&newUser);
339✔
3526
  TAOS_RETURN(code);
339✔
3527
}
3528

3529
int64_t mndGetUserIpWhiteListVer(SMnode *pMnode, SUserObj *pUser) {
×
3530
  // ver = 0, disable ip white list
3531
  // ver > 0, enable ip white list
3532
  return tsEnableWhiteList ? pUser->ipWhiteListVer : 0;
×
3533
}
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