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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/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() {
×
142
  ipWhiteMgt.pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
×
143
  if (ipWhiteMgt.pIpWhiteTab == NULL) {
×
144
    TAOS_RETURN(terrno);
×
145
  }
146
  ipWhiteMgt.ver = 0;
×
147
  (void)taosThreadRwlockInit(&ipWhiteMgt.rw, NULL);
×
148
  TAOS_RETURN(0);
×
149
}
150
void ipWhiteMgtCleanup() {
×
151
  destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
×
152
  (void)taosThreadRwlockDestroy(&ipWhiteMgt.rw);
×
153
}
×
154

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

163
  if (ppList == NULL || *ppList == NULL) {
×
164
    SIpWhiteList *p = cloneIpWhiteList(pNew);
×
165
    if (p == NULL) {
×
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) {
×
170
      update = false;
×
171
      taosMemoryFree(p);
×
172
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
173
    }
174
  } else {
175
    SIpWhiteList *pOld = *ppList;
×
176
    if (isIpWhiteListEqual(pOld, pNew)) {
×
177
      update = false;
×
178
    } else {
179
      taosMemoryFree(pOld);
×
180
      SIpWhiteList *p = cloneIpWhiteList(pNew);
×
181
      if (p == NULL) {
×
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) {
×
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
×
194
  if (fqdns == NULL) {
×
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++) {
×
200
    char *fqdn = taosArrayGetP(fqdns, i);
×
201
    bool  upd = false;
×
202
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, TSDB_DEFAULT_USER, fqdn, IP_WHITE_ADD, &upd), &lino,
×
203
                    _OVER);
204
    update |= upd;
×
205
    TAOS_CHECK_GOTO(mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, IP_WHITE_ADD, &upd), &lino, _OVER);
×
206
    update |= upd;
×
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++;
×
215

216
_OVER:
×
217
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
218
  taosArrayDestroyP(fqdns, NULL);
×
219
  if (code < 0) {
×
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);
×
223
}
224
int32_t ipWhiteMgtRemove(char *user) {
×
225
  bool    update = true;
×
226
  int32_t code = 0;
×
227
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
×
228
  SIpWhiteList **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
×
229
  if (ppList == NULL || *ppList == NULL) {
×
230
    update = false;
×
231
  } else {
232
    taosMemoryFree(*ppList);
×
233
    code = taosHashRemove(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
×
234
    if (code != 0) {
×
235
      update = false;
×
236
    }
237
  }
238

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

244
bool isRangeInWhiteList(SIpWhiteList *pList, SIpV4Range *range) {
×
245
  for (int i = 0; i < pList->num; i++) {
×
246
    if (isIpRangeEqual(&pList->pIpRange[i], range)) {
×
247
      return true;
×
248
    }
249
  }
250
  return false;
×
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) {
×
300
  SHashObj *pNew = NULL;
×
301
  TAOS_CHECK_RETURN(mndFetchAllIpWhite(pMnode, &pNew));
×
302

303
  SHashObj *pOld = ipWhiteMgt.pIpWhiteTab;
×
304

305
  ipWhiteMgt.pIpWhiteTab = pNew;
×
306
  ipWhiteMgt.ver++;
×
307

308
  destroyIpWhiteTab(pOld);
×
309
  TAOS_RETURN(0);
×
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) {
×
323
  int64_t ver = 0;
×
324
  int32_t code = 0;
×
325
  (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
×
326
  if (ipWhiteMgt.ver == 0) {
×
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;
×
336
  (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
337

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

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

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

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

386
        pNewList->num = pList->num + 1;
×
387

388
        if ((code = taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *))) != 0) {
×
389
          taosMemoryFree(pNewList);
×
390
          TAOS_CHECK_GOTO(code, &lino, _OVER);
×
391
        }
392
        taosMemoryFree(pList);
×
393
        update = true;
×
394
      }
395
    }
396
  } else if (type == IP_WHITE_DROP) {
×
397
    if (pList != NULL) {
×
398
      if (isRangeInWhiteList(pList, &range)) {
×
399
        if (pList->num == 1) {
×
400
          if (taosHashRemove(pIpWhiteTab, user, strlen(user)) < 0) {
×
401
            mError("failed to remove ip-white-list for user: %s at line %d", user, lino);
×
402
          }
403
          taosMemoryFree(pList);
×
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;
×
427
      }
428
    }
429
  }
430
  if (update) {
×
431
    mDebug("ip-white-list update for user: %s, fqdn: %s", user, fqdn);
×
432
  }
433

434
_OVER:
×
435
  if (pUpdate) *pUpdate = update;
×
436
  if (code < 0) {
×
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);
×
441
}
442

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

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

454
  TAOS_RETURN(code);
×
455
}
456

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

462
  if (lock) {
×
463
    (void)taosThreadRwlockWrlock(&ipWhiteMgt.rw);
×
464
    if (ipWhiteMgt.ver == 0) {
×
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);
×
472

473
  void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
×
474
  while (pIter) {
×
475
    size_t klen = 0;
×
476
    char  *key = taosHashGetKey(pIter, &klen);
×
477

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

492
    pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
×
493
  }
494

495
_OVER:
×
496
  if (update) ipWhiteMgt.ver++;
×
497
  if (lock) (void)taosThreadRwlockUnlock(&ipWhiteMgt.rw);
×
498
  if (code < 0) {
×
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);
×
504
}
505

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

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

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

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

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

547
void destroyIpWhiteTab(SHashObj *pIpWhiteTab) {
×
548
  if (pIpWhiteTab == NULL) return;
×
549

550
  void *pIter = taosHashIterate(pIpWhiteTab, NULL);
×
551
  while (pIter) {
×
552
    SIpWhiteList *list = *(SIpWhiteList **)pIter;
×
553
    taosMemoryFree(list);
×
554
    pIter = taosHashIterate(pIpWhiteTab, pIter);
×
555
  }
556

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

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

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

582
    SIpWhiteList *pWhiteList = cloneIpWhiteList(pUser->pIpWhiteList);
×
583
    if (pWhiteList == NULL) {
×
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) {
×
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);
×
596
    if (name == NULL) {
×
597
      sdbRelease(pSdb, pUser);
×
598
      sdbCancelFetch(pSdb, pIter);
×
599
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
600
    }
601
    if (taosArrayPush(pUserNames, &name) == NULL) {
×
602
      taosMemoryFree(name);
×
603
      sdbRelease(pSdb, pUser);
×
604
      sdbCancelFetch(pSdb, pIter);
×
605
      TAOS_CHECK_GOTO(terrno, &lino, _OVER);
×
606
    }
607

608
    sdbRelease(pSdb, pUser);
×
609
  }
610

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

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

635
  for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
×
636
    char *fqdn = taosArrayGetP(fqdns, i);
×
637

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

644
_OVER:
×
645
  taosArrayDestroyP(fqdns, NULL);
×
646
  taosArrayDestroyP(pUserNames, NULL);
×
647

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

657
int32_t mndInitUser(SMnode *pMnode) {
×
658
  TAOS_CHECK_RETURN(ipWhiteMgtInit());
×
659

660
  SSdbTable table = {
×
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);
×
672
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
×
673
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
×
674
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
×
675
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST, mndProcessGetUserWhiteListReq);
×
676

677
  mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE, mndProcesSRetrieveIpWhiteReq);
×
678

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

688
void mndCleanupUser(SMnode *pMnode) { ipWhiteMgtCleanup(); }
×
689

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

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

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

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

764
  TAOS_CHECK_GOTO(tStartEncode(&encoder), &lino, _OVER);
×
765
  TAOS_CHECK_GOTO(tEncodeI32(&encoder, pList->num), &lino, _OVER);
×
766

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

773
  tEndEncode(&encoder);
×
774

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

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

791
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
792
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &pList->num), &lino, _OVER);
×
793

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

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

807
  TAOS_RETURN(code);
×
808
}
809

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

818
  TAOS_CHECK_GOTO(tStartDecode(&decoder), &lino, _OVER);
×
819
  TAOS_CHECK_GOTO(tDecodeI32(&decoder, &num), &lino, _OVER);
×
820

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

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

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

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

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

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

879
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
×
880
  if (pTrans == NULL) {
×
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);
×
886

887
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
×
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);
×
893

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1177
  SDB_SET_INT32(pRaw, dataPos, len, _OVER);
×
1178
  SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
×
1179

1180
  SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
×
1181

1182
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
×
1183
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
×
1184

1185
_OVER:
×
1186
  taosMemoryFree(buf);
×
1187
  if (code < 0) {
×
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);
×
1196
  return pRaw;
×
1197
}
1198

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1323
    for (int32_t i = 0; i < numOfReadTbs; ++i) {
×
1324
      int32_t keyLen = 0;
×
1325
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1326

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

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

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

1346
    for (int32_t i = 0; i < numOfWriteTbs; ++i) {
×
1347
      int32_t keyLen = 0;
×
1348
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1349

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

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

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

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

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

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

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

1393
      for (int32_t i = 0; i < numOfReadViews; ++i) {
×
1394
        int32_t keyLen = 0;
×
1395
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1396

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

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

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

1416
      for (int32_t i = 0; i < numOfWriteViews; ++i) {
×
1417
        int32_t keyLen = 0;
×
1418
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1419

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

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

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

1439
      for (int32_t i = 0; i < numOfAlterViews; ++i) {
×
1440
        int32_t keyLen = 0;
×
1441
        SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1442

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

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

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

1463
    for (int32_t i = 0; i < numOfUseDbs; ++i) {
×
1464
      int32_t keyLen = 0;
×
1465
      SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
×
1466

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

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

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

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

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

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

1496
  if (pUser->pIpWhiteList == NULL) {
×
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)
×
1502
  taosInitRWLatch(&pUser->lock);
×
1503

1504
_OVER:
×
1505
  taosMemoryFree(key);
×
1506
  taosMemoryFree(value);
×
1507
  if (code < 0) {
×
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);
×
1529
  return pRow;
×
1530
}
1531

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

1535
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
×
1536
  if (pAcct == NULL) {
×
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;
×
1542
  sdbRelease(pSdb, pAcct);
×
1543

1544
  return 0;
×
1545
}
1546

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

1555
  char *tb = taosHashIterate(pOld, NULL);
×
1556
  while (tb != NULL) {
×
1557
    size_t keyLen = 0;
×
1558
    char  *key = taosHashGetKey(tb, &keyLen);
×
1559

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

1569
  TAOS_RETURN(code);
×
1570
}
1571

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

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

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

1593
  TAOS_RETURN(code);
×
1594
}
1595

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

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

1618
_OVER:
×
1619
  taosRUnLockLatch(&pUser->lock);
×
1620
  TAOS_RETURN(code);
×
1621
}
1622

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

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

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

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

1683
  taosWUnLockLatch(&pOld->lock);
×
1684

1685
  return 0;
×
1686
}
1687

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

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

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

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

1713
  if (pCreate->passIsMd5 == 1) {
×
1714
    memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
×
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);
×
1725
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
×
1726
  userObj.createdTime = taosGetTimestampMs();
×
1727
  userObj.updateTime = userObj.createdTime;
×
1728
  userObj.superUser = 0;  // pCreate->superUser;
×
1729
  userObj.sysInfo = pCreate->sysInfo;
×
1730
  userObj.enable = pCreate->enable;
×
1731
  userObj.createdb = pCreate->createDb;
×
1732

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

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

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

1773
      i++;
×
1774
    }
1775

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

1781
  userObj.ipWhiteListVer = taosGetTimestampMs();
×
1782

1783
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
×
1784
  if (pTrans == NULL) {
×
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);
×
1790

1791
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
×
1792
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
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);
×
1798

1799
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
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) {
×
1805
    mndTransDrop(pTrans);
×
1806
    TAOS_CHECK_GOTO(code, &lino, _OVER);
×
1807
  }
1808

1809
  taosMemoryFree(userObj.pIpWhiteList);
×
1810
  mndTransDrop(pTrans);
×
1811
  return 0;
×
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) {
×
1865
  SMnode        *pMnode = pReq->info.node;
×
1866
  int32_t        code = 0;
×
1867
  int32_t        lino = 0;
×
1868
  SUserObj      *pUser = NULL;
×
1869
  SUserObj      *pOperUser = NULL;
×
1870
  SCreateUserReq createReq = {0};
×
1871

1872
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
×
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);
×
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) {
×
1885
    TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER), &lino, _OVER);
×
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) {
×
1894
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
1895
  }
1896

1897
  if(createReq.passIsMd5 == 0){
×
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);
×
1913
  if (pUser != NULL) {
×
1914
    TAOS_CHECK_GOTO(TSDB_CODE_MND_USER_ALREADY_EXIST, &lino, _OVER);
×
1915
  }
1916

1917
  code = mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
×
1918
  if (pOperUser == NULL) {
×
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);
×
1923

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

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

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

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

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

1948
  TAOS_RETURN(code);
×
1949
}
1950

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

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

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

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

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

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

1987
_OVER:
×
1988
  mndReleaseUser(pMnode, pUser);
×
1989
  tFreeSGetUserWhiteListRsp(&wlRsp);
×
1990
  if (code < 0) {
×
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;
×
1997
  pReq->info.rsp = pRsp;
×
1998
  pReq->info.rspLen = contLen;
×
1999

2000
  TAOS_RETURN(code);
×
2001
}
2002

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

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

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

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

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

2033
_OVER:
×
2034
  if (code < 0) {
×
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;
×
2041
  pReq->info.rsp = pRsp;
×
2042
  pReq->info.rspLen = len;
×
2043

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

2048
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
×
2049
  int32_t code = 0;
×
2050
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
×
2051
  if (pTrans == NULL) {
×
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);
×
2056

2057
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
×
2058
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
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);
×
2064
  if (code < 0) {
×
2065
    mndTransDrop(pTrans);
×
2066
    TAOS_RETURN(code);
×
2067
  }
2068

2069
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
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) {
×
2075
    mndTransDrop(pTrans);
×
2076
    TAOS_RETURN(code);
×
2077
  }
2078
  mndTransDrop(pTrans);
×
2079
  return 0;
×
2080
}
2081

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

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

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

2103
  TAOS_RETURN(code);
×
2104
}
2105

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

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

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

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

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

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

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

2138
  TAOS_RETURN(0);
×
2139
}
2140

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

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

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

2158
  if (1 == *currRef) {
×
2159
    if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
×
2160
      TAOS_RETURN(0);  // not found
×
2161
    }
2162
    return 0;
×
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) {
×
2171
  if (type == TSDB_ALTER_USER_PASSWD) {
×
2172
    return "changePassword";
×
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) {
×
2190
  SSdb   *pSdb = pMnode->pSdb;
×
2191
  void   *pIter = NULL;
×
2192
  int32_t code = 0;
×
2193
  int32_t lino = 0;
×
2194

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

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

2257
  if (ALTER_USER_DEL_READ_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
2258
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
2259
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
2260
      int32_t len = strlen(pAlterReq->objname) + 1;
×
2261
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
2262
      if (pDb == NULL) {
×
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);
×
2267
      if (code < 0) {
×
2268
        mError("read db:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2269
      }
2270
      mndReleaseDb(pMnode, pDb);
×
2271
    } else {
2272
      taosHashClear(pNewUser->readDbs);
×
2273
    }
2274
  }
2275

2276
  if (ALTER_USER_DEL_WRITE_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
2277
      ALTER_USER_DEL_ALL_DB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
2278
    if (strcmp(pAlterReq->objname, "1.*") != 0) {
×
2279
      int32_t len = strlen(pAlterReq->objname) + 1;
×
2280
      SDbObj *pDb = mndAcquireDb(pMnode, pAlterReq->objname);
×
2281
      if (pDb == NULL) {
×
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);
×
2286
      if (code < 0) {
×
2287
        mError("user:%s, failed to remove db:%s since %s", pNewUser->user, pAlterReq->objname, terrstr());
×
2288
      }
2289
      mndReleaseDb(pMnode, pDb);
×
2290
    } else {
2291
      taosHashClear(pNewUser->writeDbs);
×
2292
    }
2293
  }
2294

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

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

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

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

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

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

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

2332
  if (ALTER_USER_DEL_ALTER_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName) ||
×
2333
      ALTER_USER_DEL_ALL_TB_PRIV(pAlterReq->alterType, pAlterReq->privileges, pAlterReq->tabName)) {
×
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)) {
×
2339
    int32_t      len = strlen(pAlterReq->objname) + 1;
×
2340
    SMqTopicObj *pTopic = NULL;
×
2341
    if ((code = mndAcquireTopic(pMnode, pAlterReq->objname, &pTopic)) != 0) {
×
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) {
×
2346
      mndReleaseTopic(pMnode, pTopic);
×
2347
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
2348
    }
2349
    mndReleaseTopic(pMnode, pTopic);
×
2350
  }
2351

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

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

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

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

2388
  if (alterReq.user[0] == 0) {
×
2389
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_USER_FORMAT, &lino, _OVER);
×
2390
  }
2391
  if(alterReq.passIsMd5 == 0){
×
2392
    if (TSDB_ALTER_USER_PASSWD == alterReq.alterType) {
×
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);
×
2407

2408
  (void)mndAcquireUser(pMnode, pReq->info.conn.user, &pOperUser);
×
2409
  if (pOperUser == NULL) {
×
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);
×
2414

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

2417
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
×
2418
    if (alterReq.passIsMd5 == 1) {
×
2419
      (void)memcpy(newUser.pass, alterReq.pass, TSDB_PASSWORD_LEN);
×
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)) {
×
2425
      ++newUser.passVersion;
×
2426
    }
2427
  }
2428

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

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

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

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

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

2449
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WHITE_LIST) {
×
2450
    taosMemoryFreeClear(newUser.pIpWhiteList);
×
2451

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

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

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

2481
    if (pNew->num > MND_MAX_USE_HOST) {
×
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) {
×
2486
    taosMemoryFreeClear(newUser.pIpWhiteList);
×
2487

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

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

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

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

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

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

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

2543
  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
×
2544
    char detail[1000] = {0};
×
2545
    (void)tsnprintf(detail, sizeof(detail),
×
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,
×
2548
              alterReq.createdb ? 1 : 0, alterReq.tabName);
×
2549
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, detail, strlen(detail));
×
2550
  } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
×
2551
             alterReq.alterType == TSDB_ALTER_USER_SYSINFO || alterReq.alterType == TSDB_ALTER_USER_CREATEDB) {
×
2552
    auditRecord(pReq, pMnode->clusterId, "alterUser", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
2553
  } else if (ALTER_USER_ADD_READ_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
2554
             ALTER_USER_ADD_WRITE_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
2555
             ALTER_USER_ADD_ALL_DB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
2556
             ALTER_USER_ADD_READ_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
2557
             ALTER_USER_ADD_WRITE_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName) ||
×
2558
             ALTER_USER_ADD_ALL_TB_PRIV(alterReq.alterType, alterReq.privileges, alterReq.tabName)) {
×
2559
    if (strcmp(alterReq.objname, "1.*") != 0) {
×
2560
      SName name = {0};
×
2561
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
×
2562
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", name.dbname, alterReq.user, alterReq.sql,
×
2563
                  alterReq.sqlLen);
2564
    } else {
2565
      auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
2566
    }
2567
  } else if (ALTER_USER_ADD_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
×
2568
    auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.objname, alterReq.user, alterReq.sql,
×
2569
                alterReq.sqlLen);
2570
  } else if (ALTER_USER_DEL_SUBSCRIBE_TOPIC_PRIV(alterReq.alterType, alterReq.privileges)) {
×
2571
    auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.objname, alterReq.user, alterReq.sql,
×
2572
                alterReq.sqlLen);
2573
  } else {
2574
    if (strcmp(alterReq.objname, "1.*") != 0) {
×
2575
      SName name = {0};
×
2576
      TAOS_CHECK_GOTO(tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
×
2577
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", name.dbname, alterReq.user, alterReq.sql,
×
2578
                  alterReq.sqlLen);
2579
    } else {
2580
      auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", "", alterReq.user, alterReq.sql, alterReq.sqlLen);
×
2581
    }
2582
  }
2583

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

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

2594
  TAOS_RETURN(code);
×
2595
}
2596

2597
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
×
2598
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
×
2599
  if (pTrans == NULL) {
×
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);
×
2604

2605
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
×
2606
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
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) {
×
2612
    mndTransDrop(pTrans);
×
2613
    TAOS_RETURN(terrno);
×
2614
  }
2615

2616
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
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);
×
2622

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

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

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

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

2639
  if (dropReq.user[0] == 0) {
×
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);
×
2644

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

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

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

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

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

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

2673
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, authReq.user, &pUser));
×
2674

2675
  TAOS_CHECK_EXIT(mndSetUserAuthRsp(pMnode, pUser, &authRsp));
×
2676

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

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

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

2704
  TAOS_RETURN(code);
×
2705
}
2706

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

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

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

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

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

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

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

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

2751
    cols++;
×
2752

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

2764
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
2765
      COL_DATA_SET_VAL_GOTO((const char *)varstr, false, pUser, _exit);
×
2766

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

2773
    numOfRows++;
×
2774
    sdbRelease(pSdb, pUser);
×
2775
  }
2776

2777
  pShow->numOfRows += numOfRows;
×
2778
_exit:
×
2779
  taosMemoryFreeClear(buf);
×
2780
  taosMemoryFreeClear(varstr);
×
2781
  if (code < 0) {
×
2782
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2783
    TAOS_RETURN(code);
×
2784
  }
2785
  return numOfRows;
×
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,
×
2880
                           SUserObj *pUser, SShowObj *pShow, char **condition, char **sql) {
2881
  char   *value = taosHashIterate(hash, NULL);
×
2882
  char   *user = pUser->user;
×
2883
  int32_t code = 0;
×
2884
  int32_t lino = 0;
×
2885
  int32_t cols = 0;
×
2886
  int32_t numOfRows = *pNumOfRows;
×
2887

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

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

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

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

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

2917
    if (strcmp("t", value) != 0 && strcmp("v", value) != 0) {
×
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);
×
2955
      if ((*condition) == NULL) {
×
2956
        code = terrno;
×
2957
        goto _exit;
×
2958
      }
2959
      STR_WITH_MAXSIZE_TO_VARSTR((*condition), "", pShow->pMeta->pSchemas[cols].bytes);
×
2960
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2961
      COL_DATA_SET_VAL_GOTO((const char *)(*condition), false, NULL, _exit);
×
2962

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

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

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

2993
  bool fetchNextUser = pShow->restore ? false : true;
×
2994
  pShow->restore = false;
×
2995

2996
  while (numOfRows < rows) {
×
2997
    if (fetchNextUser) {
×
2998
      pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
×
2999
      if (pShow->pIter == NULL) break;
×
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);
×
3010
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
×
3011
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
×
3012
    int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
×
3013
    int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
×
3014
    int32_t numOfAlterTbs = taosHashGetSize(pUser->alterTbs);
×
3015
    int32_t numOfReadViews = taosHashGetSize(pUser->readViews);
×
3016
    int32_t numOfWriteViews = taosHashGetSize(pUser->writeViews);
×
3017
    int32_t numOfAlterViews = taosHashGetSize(pUser->alterViews);
×
3018
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs + numOfAlterTbs +
×
3019
            numOfReadViews + numOfWriteViews + numOfAlterViews >=
×
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) {
×
3032
      cols = 0;
×
3033
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
×
3034
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
×
3035
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
3036
      COL_DATA_SET_VAL_GOTO((const char *)userName, false, pUser, _exit);
×
3037

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

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

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

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

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

3067
      numOfRows++;
×
3068
    }
3069

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3220
    sdbRelease(pSdb, pUser);
×
3221
  }
3222

3223
  pShow->numOfRows += numOfRows;
×
3224
_exit:
×
3225
  taosMemoryFreeClear(condition);
×
3226
  taosMemoryFreeClear(sql);
×
3227
  if (code < 0) {
×
3228
    uError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3229
    TAOS_RETURN(code);
×
3230
  }
3231
  return numOfRows;
×
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,
×
3240
                                int32_t *pRspLen, int64_t ipWhiteListVer) {
3241
  int32_t           code = 0;
×
3242
  int32_t           lino = 0;
×
3243
  int32_t           rspLen = 0;
×
3244
  void             *pRsp = NULL;
×
3245
  SUserAuthBatchRsp batchRsp = {0};
×
3246

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

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

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

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

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

3289
  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
×
3290
    *ppRsp = NULL;
×
3291
    *pRspLen = 0;
×
3292

3293
    tFreeSUserAuthBatchRsp(&batchRsp);
×
3294
    return 0;
×
3295
  }
3296

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

3319
  TAOS_RETURN(code);
×
3320
}
3321

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

3331
  while (1) {
×
3332
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
×
3333
    if (pIter == NULL) break;
×
3334

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

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

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

3359
    mndUserFreeObj(&newUser);
×
3360
    sdbRelease(pSdb, pUser);
×
3361
  }
3362

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

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

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

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

3386
    bool inRead = (taosHashGet(newUser.readTbs, stb, len) != NULL);
×
3387
    bool inWrite = (taosHashGet(newUser.writeTbs, stb, len) != NULL);
×
3388
    bool inAlter = (taosHashGet(newUser.alterTbs, stb, len) != NULL);
×
3389
    if (inRead || inWrite || inAlter) {
×
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);
×
3417
    sdbRelease(pSdb, pUser);
×
3418
  }
3419

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

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

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

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

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

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

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

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

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

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

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

3500
    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
×
3501
    if (inTopic) {
×
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);
×
3520
    sdbRelease(pSdb, pUser);
×
3521
  }
3522

3523
  if (pUser != NULL) sdbRelease(pSdb, pUser);
×
3524
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
×
3525
  mndUserFreeObj(&newUser);
×
3526
  TAOS_RETURN(code);
×
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