TDengine/source/dnode/mnode/impl/src/mndUser.c

2387 lines
77 KiB
C
Raw Normal View History

2021-09-22 08:15:20 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-10-16 07:16:05 +00:00
#define _DEFAULT_SOURCE
2023-09-05 10:50:11 +00:00
// clang-format off
#include <uv.h>
2021-12-03 12:52:44 +00:00
#include "mndUser.h"
2023-09-04 13:15:46 +00:00
#include "audit.h"
2022-02-11 05:13:32 +00:00
#include "mndDb.h"
2022-07-27 07:43:49 +00:00
#include "mndPrivilege.h"
2021-12-03 12:52:44 +00:00
#include "mndShow.h"
2023-04-07 08:39:40 +00:00
#include "mndStb.h"
2022-12-01 03:45:31 +00:00
#include "mndTopic.h"
2021-11-27 14:56:18 +00:00
#include "mndTrans.h"
2022-02-28 02:34:05 +00:00
#include "tbase64.h"
2021-10-17 03:42:05 +00:00
2023-09-05 10:50:11 +00:00
// clang-format on
2023-09-04 13:15:46 +00:00
#define USER_VER_NUMBER 5
2022-04-26 09:42:57 +00:00
#define USER_RESERVE_SIZE 64
2021-10-18 06:00:35 +00:00
2023-09-06 08:24:29 +00:00
static SIpWhiteList *createDefaultIpWhiteList();
SIpWhiteList *createIpWhiteList(void *buf, int32_t len);
static bool updateIpWhiteList(SIpWhiteList *pOld, SIpWhiteList *pNew);
static bool isIpWhiteListEqual(SIpWhiteList *a, SIpWhiteList *b);
static bool isIpRangeEqual(SIpV4Range *a, SIpV4Range *b);
void destroyIpWhiteTab(SHashObj *pIpWhiteTab);
2021-12-03 12:52:44 +00:00
static int32_t mndCreateDefaultUsers(SMnode *pMnode);
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
2022-01-04 12:04:23 +00:00
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
2022-05-16 06:55:31 +00:00
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq);
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq);
static int32_t mndProcessDropUserReq(SRpcMsg *pReq);
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq);
static int32_t mndProcessGetUserWhiteListReq(SRpcMsg *pReq);
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
2021-12-07 12:14:22 +00:00
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter);
2022-12-01 03:45:31 +00:00
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
2023-09-06 08:24:29 +00:00
SHashObj *mndFetchAllIpWhite(SMnode *pMnode);
2023-09-06 13:36:16 +00:00
static int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq);
2023-09-06 08:24:29 +00:00
2023-09-13 11:54:45 +00:00
void ipWhiteMgtUpdateAll(SMnode *pMnode);
2023-09-06 08:24:29 +00:00
typedef struct {
2023-09-07 12:47:10 +00:00
SHashObj *pIpWhiteTab;
2023-09-06 08:24:29 +00:00
int64_t ver;
TdThreadRwlock rw;
} SIpWhiteMgt;
static SIpWhiteMgt ipWhiteMgt;
void ipWhiteMgtInit() {
2023-09-07 12:47:10 +00:00
ipWhiteMgt.pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
2023-09-11 12:48:24 +00:00
ipWhiteMgt.ver = 0;
2023-09-06 08:24:29 +00:00
taosThreadRwlockInit(&ipWhiteMgt.rw, NULL);
}
void ipWhiteMgtCleanup() {
2023-09-07 12:47:10 +00:00
destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
2023-09-06 08:24:29 +00:00
taosThreadRwlockDestroy(&ipWhiteMgt.rw);
}
int32_t ipWhiteMgtUpdate(char *user, SIpWhiteList *pNew) {
bool update = true;
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2023-09-07 12:47:10 +00:00
SIpWhiteList **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
2023-09-06 08:24:29 +00:00
if (ppList == NULL || *ppList == NULL) {
SIpWhiteList *p = cloneIpWhiteList(pNew);
2023-09-07 12:47:10 +00:00
taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *));
2023-09-06 08:24:29 +00:00
} else {
SIpWhiteList *pOld = *ppList;
if (isIpWhiteListEqual(pOld, pNew)) {
update = false;
} else {
taosMemoryFree(pOld);
SIpWhiteList *p = cloneIpWhiteList(pNew);
2023-09-07 12:47:10 +00:00
taosHashPut(ipWhiteMgt.pIpWhiteTab, user, strlen(user), &p, sizeof(void *));
2023-09-06 08:24:29 +00:00
}
}
if (update) ipWhiteMgt.ver++;
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
return 0;
}
2023-09-07 12:47:10 +00:00
int32_t ipWhiteMgtRemove(char *user) {
bool update = true;
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
SIpWhiteList **ppList = taosHashGet(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
if (ppList == NULL || *ppList == NULL) {
update = false;
} else {
taosMemoryFree(*ppList);
taosHashRemove(ipWhiteMgt.pIpWhiteTab, user, strlen(user));
}
if (update) ipWhiteMgt.ver++;
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
return 0;
}
bool isRangeInWhiteList(SIpWhiteList *pList, SIpV4Range *range) {
for (int i = 0; i < pList->num; i++) {
if (isIpRangeEqual(&pList->pIpRange[i], range)) {
return true;
}
}
return false;
}
int32_t ipWhiteUpdateForAllUser(SIpWhiteList *pList) {
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
SHashObj *pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
while (pIter) {
SIpWhiteList *p = *(SIpWhiteList **)pIter;
SIpWhiteList *clone = cloneIpWhiteList(pList);
int32_t idx = 0;
for (int i = 0; i < pList->num; i++) {
SIpV4Range *e = &pList->pIpRange[i];
if (!isRangeInWhiteList(p, e)) {
clone->pIpRange[idx] = *e;
idx++;
}
}
clone->num = idx;
SIpWhiteList *val = NULL;
if (clone->num != 0) {
int32_t sz = clone->num + p->num;
val = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sz * sizeof(SIpV4Range));
memcpy(val->pIpRange, p->pIpRange, sizeof(SIpV4Range) * p->num);
memcpy(((char *)val->pIpRange) + sizeof(SIpV4Range) * p->num, (char *)clone->pIpRange,
sizeof(SIpV4Range) * clone->num);
} else {
val = cloneIpWhiteList(p);
}
taosMemoryFree(clone);
size_t klen;
void *key = taosHashGetKey(pIter, &klen);
taosHashPut(pIpWhiteTab, key, klen, val, sizeof(void *));
}
destroyIpWhiteTab(ipWhiteMgt.pIpWhiteTab);
ipWhiteMgt.pIpWhiteTab = pIpWhiteTab;
ipWhiteMgt.ver++;
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
return 0;
}
2023-09-06 08:24:29 +00:00
void ipWhiteMgtUpdateAll(SMnode *pMnode) {
ipWhiteMgt.ver++;
SHashObj *pNew = mndFetchAllIpWhite(pMnode);
2023-09-07 12:47:10 +00:00
SHashObj *pOld = ipWhiteMgt.pIpWhiteTab;
2023-09-06 08:24:29 +00:00
2023-09-07 12:47:10 +00:00
ipWhiteMgt.pIpWhiteTab = pNew;
2023-09-08 03:49:12 +00:00
2023-09-06 08:24:29 +00:00
destroyIpWhiteTab(pOld);
}
void ipWhiteMgtUpdate2(SMnode *pMnode) {
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
ipWhiteMgtUpdateAll(pMnode);
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
}
2023-09-06 09:22:30 +00:00
int64_t mndGetIpWhiteVer(SMnode *pMnode) {
2023-09-11 13:29:50 +00:00
int64_t ver = 0;
2023-09-06 08:24:29 +00:00
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2023-09-11 13:29:50 +00:00
if (ipWhiteMgt.ver == 0) {
2023-09-13 11:54:45 +00:00
// user and dnode r
2023-09-06 08:24:29 +00:00
ipWhiteMgtUpdateAll(pMnode);
2023-09-11 12:48:24 +00:00
ipWhiteMgt.ver = taosGetTimestampMs();
2023-09-06 08:24:29 +00:00
}
2023-09-11 13:29:50 +00:00
ver = ipWhiteMgt.ver;
2023-09-06 08:24:29 +00:00
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
2023-09-14 01:25:23 +00:00
mDebug("ip-white-list on mnode ver: %" PRId64 "", ver);
2023-09-11 12:48:24 +00:00
2023-09-12 03:36:44 +00:00
// if (mndEnableIpWhiteList(pMnode) == 0 || tsEnableWhiteList == false) {
// return 0;
// }
2023-09-06 08:24:29 +00:00
return ver;
}
2023-09-08 03:49:12 +00:00
bool mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t type) {
bool update = false;
2023-09-11 07:02:35 +00:00
SIpV4Range range = {.ip = taosGetIpv4FromFqdn(fqdn), .mask = 32};
2023-09-13 11:54:45 +00:00
mDebug("ip-white-list may update for user: %s, fqdn: %s", user, fqdn);
2023-09-08 03:49:12 +00:00
SIpWhiteList **ppList = taosHashGet(pIpWhiteTab, user, strlen(user));
SIpWhiteList *pList = NULL;
if (ppList != NULL && *ppList != NULL) {
pList = *ppList;
}
if (type == IP_WHITE_ADD) {
if (pList == NULL) {
SIpWhiteList *pNewList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range));
memcpy(pNewList->pIpRange, &range, sizeof(SIpV4Range));
pNewList->num = 1;
taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *));
update = true;
} else {
if (!isRangeInWhiteList(pList, &range)) {
int32_t sz = sizeof(SIpWhiteList) + sizeof(SIpV4Range) * (pList->num + 1);
SIpWhiteList *pNewList = taosMemoryCalloc(1, sz);
memcpy(pNewList->pIpRange, pList->pIpRange, sizeof(SIpV4Range) * (pList->num));
pNewList->pIpRange[pList->num].ip = range.ip;
pNewList->pIpRange[pList->num].mask = range.mask;
pNewList->num = pList->num + 1;
taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *));
taosMemoryFree(pList);
update = true;
}
}
} else if (type == IP_WHITE_DROP) {
if (pList != NULL) {
if (isRangeInWhiteList(pList, &range)) {
if (pList->num == 1) {
taosHashRemove(pIpWhiteTab, user, strlen(user));
taosMemoryFree(pList);
} else {
int32_t idx = 0;
int32_t sz = sizeof(SIpWhiteList) + sizeof(SIpV4Range) * (pList->num - 1);
SIpWhiteList *pNewList = taosMemoryCalloc(1, sz);
for (int i = 0; i < pList->num; i++) {
SIpV4Range *e = &pList->pIpRange[i];
if (!isIpRangeEqual(e, &range)) {
pNewList->pIpRange[idx].ip = e->ip;
pNewList->pIpRange[idx].mask = e->mask;
idx++;
}
}
pNewList->num = idx;
taosHashPut(pIpWhiteTab, user, strlen(user), &pNewList, sizeof(void *));
taosMemoryFree(pList);
}
update = true;
}
}
}
2023-09-13 11:54:45 +00:00
if (update) {
mDebug("ip-white-list update for user: %s, fqdn: %s", user, fqdn);
}
2023-09-08 03:49:12 +00:00
return update;
}
2023-09-13 11:54:45 +00:00
int32_t mndRefreshUserIpWhiteList(SMnode *pMnode) {
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
ipWhiteMgtUpdateAll(pMnode);
ipWhiteMgt.ver = taosGetTimestampMs();
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
return 0;
}
2023-09-12 03:31:06 +00:00
void mndUpdateIpWhite(SMnode *pMnode, char *user, char *fqdn, int8_t type, int8_t lock) {
if (lock) {
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
if (ipWhiteMgt.ver == 0) {
ipWhiteMgtUpdateAll(pMnode);
ipWhiteMgt.ver = taosGetTimestampMs();
2023-09-13 11:54:45 +00:00
mInfo("ip-white-list, user: %" PRId64 "", ipWhiteMgt.ver);
2023-09-12 03:31:06 +00:00
}
}
2023-09-08 03:49:12 +00:00
2023-09-10 09:02:38 +00:00
bool update = mndUpdateIpWhiteImpl(ipWhiteMgt.pIpWhiteTab, user, fqdn, type);
2023-09-08 03:49:12 +00:00
if (update) ipWhiteMgt.ver++;
if (lock) taosThreadRwlockUnlock(&ipWhiteMgt.rw);
}
2023-09-06 13:36:16 +00:00
int64_t ipWhiteMgtFillMsg(SUpdateIpWhite *pUpdate) {
2023-09-07 08:10:07 +00:00
int64_t ver = 0;
2023-09-06 13:36:16 +00:00
taosThreadRwlockWrlock(&ipWhiteMgt.rw);
2023-09-07 08:10:07 +00:00
ver = ipWhiteMgt.ver;
2023-09-07 12:47:10 +00:00
int32_t num = taosHashGetSize(ipWhiteMgt.pIpWhiteTab);
2023-09-10 06:09:36 +00:00
2023-09-06 13:36:16 +00:00
pUpdate->pUserIpWhite = taosMemoryCalloc(1, num * sizeof(SUpdateUserIpWhite));
2023-09-10 06:09:36 +00:00
2023-09-07 12:47:10 +00:00
void *pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, NULL);
2023-09-06 13:36:16 +00:00
int32_t i = 0;
while (pIter) {
SUpdateUserIpWhite *pUser = &pUpdate->pUserIpWhite[i];
SIpWhiteList *list = *(SIpWhiteList **)pIter;
size_t klen;
char *key = taosHashGetKey(pIter, &klen);
if (list->num != 0) {
2023-09-07 08:10:07 +00:00
pUser->ver = ver;
2023-09-06 13:36:16 +00:00
memcpy(pUser->user, key, klen);
pUser->numOfRange = list->num;
pUser->pIpRanges = taosMemoryCalloc(1, list->num * sizeof(SIpV4Range));
memcpy(pUser->pIpRanges, list->pIpRange, list->num * sizeof(SIpV4Range));
i++;
}
2023-09-07 12:47:10 +00:00
pIter = taosHashIterate(ipWhiteMgt.pIpWhiteTab, pIter);
2023-09-06 13:36:16 +00:00
}
pUpdate->numOfUser = i;
2023-09-10 06:09:36 +00:00
pUpdate->ver = ver;
2023-09-06 08:24:29 +00:00
2023-09-06 13:36:16 +00:00
taosThreadRwlockUnlock(&ipWhiteMgt.rw);
return 0;
}
2023-09-06 08:24:29 +00:00
void destroyIpWhiteTab(SHashObj *pIpWhiteTab) {
if (pIpWhiteTab == NULL) return;
void *pIter = taosHashIterate(pIpWhiteTab, NULL);
while (pIter) {
SIpWhiteList *list = *(SIpWhiteList **)pIter;
taosMemoryFree(list);
2023-09-07 06:24:26 +00:00
pIter = taosHashIterate(pIpWhiteTab, pIter);
2023-09-06 08:24:29 +00:00
}
taosHashCleanup(pIpWhiteTab);
}
SHashObj *mndFetchAllIpWhite(SMnode *pMnode) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
SHashObj *pIpWhiteTab = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 1, HASH_ENTRY_LOCK);
2023-09-07 13:10:05 +00:00
2023-09-10 09:02:38 +00:00
SArray *pUserNames = taosArrayInit(8, sizeof(void *));
2023-09-06 08:24:29 +00:00
while (1) {
SUserObj *pUser = NULL;
pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
if (pIter == NULL) break;
SIpWhiteList *pWhiteList = cloneIpWhiteList(pUser->pIpWhiteList);
taosHashPut(pIpWhiteTab, pUser->user, strlen(pUser->user), &pWhiteList, sizeof(void *));
2023-09-10 09:02:38 +00:00
char *name = taosStrdup(pUser->user);
taosArrayPush(pUserNames, &name);
2023-09-06 08:24:29 +00:00
sdbRelease(pSdb, pUser);
}
2023-09-08 03:49:12 +00:00
2023-09-10 09:02:38 +00:00
bool found = false;
for (int i = 0; i < taosArrayGetSize(pUserNames); i++) {
char *name = taosArrayGetP(pUserNames, i);
if (strlen(name) == strlen(TSDB_DEFAULT_USER) && strncmp(name, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER)) == 0) {
found = true;
break;
}
}
if (found == false) {
char *name = taosStrdup(TSDB_DEFAULT_USER);
taosArrayPush(pUserNames, &name);
}
2023-09-08 03:49:12 +00:00
SArray *fqdns = mndGetAllDnodeFqdns(pMnode);
for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
char *fqdn = taosArrayGetP(fqdns, i);
2023-09-10 09:02:38 +00:00
for (int j = 0; j < taosArrayGetSize(pUserNames); j++) {
char *name = taosArrayGetP(pUserNames, j);
mndUpdateIpWhiteImpl(pIpWhiteTab, name, fqdn, IP_WHITE_ADD);
}
}
for (int i = 0; i < taosArrayGetSize(fqdns); i++) {
char *fqdn = taosArrayGetP(fqdns, i);
2023-09-08 03:49:12 +00:00
taosMemoryFree(fqdn);
}
taosArrayDestroy(fqdns);
2023-09-10 09:02:38 +00:00
for (int i = 0; i < taosArrayGetSize(pUserNames); i++) {
taosMemoryFree(taosArrayGetP(pUserNames, i));
}
taosArrayDestroy(pUserNames);
2023-09-06 08:24:29 +00:00
return pIpWhiteTab;
}
2021-12-03 12:52:44 +00:00
int32_t mndInitUser(SMnode *pMnode) {
2023-09-06 08:24:29 +00:00
ipWhiteMgtInit();
2022-04-27 10:38:31 +00:00
SSdbTable table = {
.sdbType = SDB_USER,
.keyType = SDB_KEY_BINARY,
.deployFp = (SdbDeployFp)mndCreateDefaultUsers,
.encodeFp = (SdbEncodeFp)mndUserActionEncode,
.decodeFp = (SdbDecodeFp)mndUserActionDecode,
.insertFp = (SdbInsertFp)mndUserActionInsert,
.updateFp = (SdbUpdateFp)mndUserActionUpdate,
.deleteFp = (SdbDeleteFp)mndUserActionDelete,
};
2021-12-03 12:52:44 +00:00
2022-01-04 12:04:23 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
2022-02-11 07:20:27 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_WHITELIST, mndProcessGetUserWhiteListReq);
2023-09-06 13:36:16 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_RETRIEVE_IP_WHITE, mndProcesSRetrieveIpWhiteReq);
2021-12-03 12:52:44 +00:00
2021-12-07 12:14:22 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
2022-12-01 05:37:32 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
2021-12-03 12:52:44 +00:00
return sdbSetTable(pMnode->pSdb, table);
}
2023-09-06 08:24:29 +00:00
void mndCleanupUser(SMnode *pMnode) { ipWhiteMgtCleanup(); }
2021-12-03 12:52:44 +00:00
2023-09-04 13:15:46 +00:00
static void ipRangeToStr(SIpV4Range *range, char *buf) {
2023-09-05 10:50:11 +00:00
struct in_addr addr;
addr.s_addr = range->ip;
2023-09-04 13:15:46 +00:00
2023-09-05 10:50:11 +00:00
uv_inet_ntop(AF_INET, &addr, buf, 32);
2023-09-11 07:17:20 +00:00
if (range->mask != 32) {
2023-09-05 10:50:11 +00:00
sprintf(buf + strlen(buf), "/%d", range->mask);
2023-09-04 13:15:46 +00:00
}
2023-09-05 10:50:11 +00:00
return;
2023-09-04 13:15:46 +00:00
}
2023-09-14 07:34:28 +00:00
static bool isDefaultRange(SIpV4Range *pRange) {
2023-09-13 11:54:45 +00:00
static SIpV4Range val = {.ip = 16777343, .mask = 32};
return pRange->ip == val.ip && pRange->mask == val.mask;
}
static int32_t ipRangeListToStr(SIpV4Range *range, int32_t num, char *buf) {
2023-09-04 13:15:46 +00:00
int32_t len = 0;
for (int i = 0; i < num; i++) {
2023-09-13 11:54:45 +00:00
char tbuf[36] = {0};
SIpV4Range *pRange = &range[i];
2023-09-14 07:34:28 +00:00
if (isDefaultRange(pRange)) continue;
2023-09-13 11:54:45 +00:00
2023-09-04 13:15:46 +00:00
ipRangeToStr(&range[i], tbuf);
2023-09-05 10:50:11 +00:00
len += sprintf(buf + len, "%s,", tbuf);
2023-09-04 13:15:46 +00:00
}
2023-09-05 10:50:11 +00:00
if (len > 0) buf[len - 1] = 0;
2023-09-13 11:54:45 +00:00
return len;
2023-09-04 13:15:46 +00:00
}
2023-09-06 08:24:29 +00:00
static bool isIpRangeEqual(SIpV4Range *a, SIpV4Range *b) {
// equal or not
return a->ip == b->ip && a->mask == b->mask;
}
2023-09-06 01:32:26 +00:00
static bool isRangeInIpWhiteList(SIpWhiteList *pList, SIpV4Range *tgt) {
for (int i = 0; i < pList->num; i++) {
2023-09-06 08:24:29 +00:00
if (isIpRangeEqual(&pList->pIpRange[i], tgt)) return true;
2023-09-06 01:32:26 +00:00
}
return false;
}
2023-09-06 08:24:29 +00:00
static bool isIpWhiteListEqual(SIpWhiteList *a, SIpWhiteList *b) {
if (a->num != b->num) {
return false;
}
for (int i = 0; i < a->num; i++) {
if (!isIpRangeEqual(&a->pIpRange[i], &b->pIpRange[i])) {
return false;
}
}
return true;
}
2023-09-04 13:15:46 +00:00
int32_t convertIpWhiteListToStr(SIpWhiteList *pList, char **buf) {
2023-09-05 10:50:11 +00:00
if (pList->num == 0) {
*buf = NULL;
return 0;
}
2023-09-07 03:54:34 +00:00
*buf = taosMemoryCalloc(1, pList->num * 36);
2023-09-13 11:54:45 +00:00
int32_t len = ipRangeListToStr(pList->pIpRange, pList->num, *buf);
if (len == 0) {
taosMemoryFree(*buf);
return 0;
}
2023-09-04 13:15:46 +00:00
return strlen(*buf);
}
int32_t tSerializeIpWhiteList(void *buf, int32_t len, SIpWhiteList *pList) {
SEncoder encoder = {0};
tEncoderInit(&encoder, buf, len);
if (tStartEncode(&encoder) < 0) return -1;
if (tEncodeI32(&encoder, pList->num) < 0) return -1;
for (int i = 0; i < pList->num; i++) {
SIpV4Range *pRange = &(pList->pIpRange[i]);
if (tEncodeU32(&encoder, pRange->ip) < 0) return -1;
if (tEncodeU32(&encoder, pRange->mask) < 0) return -1;
}
tEndEncode(&encoder);
int32_t tlen = encoder.pos;
tEncoderClear(&encoder);
return tlen;
}
int32_t tDerializeIpWhileList(void *buf, int32_t len, SIpWhiteList *pList) {
SDecoder decoder = {0};
tDecoderInit(&decoder, buf, len);
if (tStartDecode(&decoder) < 0) return -1;
if (tDecodeI32(&decoder, &pList->num) < 0) return -1;
for (int i = 0; i < pList->num; i++) {
SIpV4Range *pRange = &(pList->pIpRange[i]);
if (tDecodeU32(&decoder, &pRange->ip) < 0) return -1;
if (tDecodeU32(&decoder, &pRange->mask) < 0) return -1;
}
tEndDecode(&decoder);
tDecoderClear(&decoder);
return 0;
}
SIpWhiteList *createIpWhiteList(void *buf, int32_t len) {
int32_t num = 0;
SDecoder decoder = {0};
tDecoderInit(&decoder, buf, len);
if (tStartDecode(&decoder) < 0) return NULL;
if (tDecodeI32(&decoder, &num) < 0) return NULL;
tEndDecode(&decoder);
tDecoderClear(&decoder);
SIpWhiteList *p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + num * sizeof(SIpV4Range));
tDerializeIpWhileList(buf, len, p);
return p;
}
static SIpWhiteList *createDefaultIpWhiteList() {
SIpWhiteList *pWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1);
pWhiteList->num = 1;
SIpV4Range *range = &(pWhiteList->pIpRange[0]);
2023-09-05 10:50:11 +00:00
struct in_addr addr;
if (uv_inet_pton(AF_INET, "127.0.0.1", &addr) == 0) {
range->ip = addr.s_addr;
2023-09-11 07:02:35 +00:00
range->mask = 32;
2023-09-05 10:50:11 +00:00
}
2023-09-04 13:15:46 +00:00
return pWhiteList;
}
2021-12-03 12:52:44 +00:00
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
SUserObj userObj = {0};
2022-01-23 11:50:18 +00:00
taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
2021-12-03 12:52:44 +00:00
tstrncpy(userObj.user, user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
userObj.sysInfo = 1;
userObj.enable = 1;
2023-09-12 03:31:06 +00:00
userObj.ipWhiteListVer = taosGetTimestampMs();
2023-09-05 10:50:11 +00:00
userObj.pIpWhiteList = createDefaultIpWhiteList();
2021-12-03 12:52:44 +00:00
if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
userObj.superUser = 1;
2021-12-03 12:52:44 +00:00
}
SSdbRaw *pRaw = mndUserActionEncode(&userObj);
2023-09-05 13:00:01 +00:00
if (pRaw == NULL) goto _ERROR;
2022-10-08 03:29:46 +00:00
(void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
2021-12-03 12:52:44 +00:00
2022-09-23 07:42:36 +00:00
mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
if (pTrans == NULL) {
2022-09-29 11:41:54 +00:00
sdbFreeRaw(pRaw);
mError("user:%s, failed to create since %s", userObj.user, terrstr());
2023-09-05 13:00:01 +00:00
goto _ERROR;
}
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
2023-09-05 13:00:01 +00:00
goto _ERROR;
}
2022-10-08 03:29:46 +00:00
(void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
if (mndTransPrepare(pMnode, pTrans) != 0) {
mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
2023-09-05 13:00:01 +00:00
goto _ERROR;
}
mndTransDrop(pTrans);
2023-09-05 13:00:01 +00:00
taosMemoryFree(userObj.pIpWhiteList);
return 0;
2023-09-05 13:00:01 +00:00
_ERROR:
taosMemoryFree(userObj.pIpWhiteList);
return -1;
2021-12-03 12:52:44 +00:00
}
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
return 0;
}
2022-05-03 04:57:55 +00:00
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2023-09-05 10:50:11 +00:00
int32_t ipWhiteReserve =
2023-09-14 04:18:04 +00:00
pUser->pIpWhiteList ? (sizeof(SIpV4Range) * pUser->pIpWhiteList->num + sizeof(SIpWhiteList) + 4) : 16;
2022-02-11 06:09:03 +00:00
int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
2023-03-30 05:59:48 +00:00
int32_t numOfReadStbs = taosHashGetSize(pUser->readTbs);
int32_t numOfWriteStbs = taosHashGetSize(pUser->writeTbs);
2022-12-01 03:45:31 +00:00
int32_t numOfTopics = taosHashGetSize(pUser->topics);
2023-04-11 02:11:25 +00:00
int32_t numOfUseDbs = taosHashGetSize(pUser->useDbs);
int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE +
2023-09-05 10:50:11 +00:00
(numOfReadDbs + numOfWriteDbs + numOfUseDbs) * TSDB_DB_FNAME_LEN + numOfTopics * TSDB_TOPIC_FNAME_LEN +
ipWhiteReserve;
2022-02-11 06:09:03 +00:00
2023-03-30 05:59:48 +00:00
char *stb = taosHashIterate(pUser->readTbs, NULL);
2023-03-29 11:51:49 +00:00
while (stb != NULL) {
size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen);
size += sizeof(int32_t);
size += keyLen;
size_t valueLen = 0;
valueLen = strlen(stb);
size += sizeof(int32_t);
size += valueLen;
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->readTbs, stb);
2023-03-29 11:51:49 +00:00
}
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->writeTbs, NULL);
2023-03-29 11:51:49 +00:00
while (stb != NULL) {
size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen);
size += sizeof(int32_t);
size += keyLen;
2023-04-07 08:39:40 +00:00
2023-03-29 11:51:49 +00:00
size_t valueLen = 0;
valueLen = strlen(stb);
size += sizeof(int32_t);
2023-04-11 07:08:42 +00:00
size += valueLen;
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->writeTbs, stb);
2023-03-29 11:51:49 +00:00
}
2022-02-11 06:09:03 +00:00
2022-04-26 09:42:57 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
if (pRaw == NULL) goto _OVER;
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
2023-04-09 10:44:46 +00:00
SDB_SET_INT32(pRaw, dataPos, pUser->passVersion, _OVER)
SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
2022-12-01 03:45:31 +00:00
SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
2022-02-10 14:49:53 +00:00
char *db = taosHashIterate(pUser->readDbs, NULL);
while (db != NULL) {
SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
2022-02-10 14:49:53 +00:00
db = taosHashIterate(pUser->readDbs, db);
}
db = taosHashIterate(pUser->writeDbs, NULL);
while (db != NULL) {
SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
2022-02-10 14:49:53 +00:00
db = taosHashIterate(pUser->writeDbs, db);
}
2022-12-01 03:45:31 +00:00
char *topic = taosHashIterate(pUser->topics, NULL);
while (topic != NULL) {
SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
topic = taosHashIterate(pUser->topics, topic);
2022-12-01 03:45:31 +00:00
}
2023-04-11 02:11:25 +00:00
SDB_SET_INT32(pRaw, dataPos, numOfReadStbs, _OVER)
SDB_SET_INT32(pRaw, dataPos, numOfWriteStbs, _OVER)
SDB_SET_INT32(pRaw, dataPos, numOfUseDbs, _OVER)
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->readTbs, NULL);
2023-03-29 05:30:32 +00:00
while (stb != NULL) {
2023-03-29 11:51:49 +00:00
size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen);
SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
size_t valueLen = 0;
2023-04-07 08:39:40 +00:00
valueLen = strlen(stb) + 1;
2023-03-29 11:51:49 +00:00
SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->readTbs, stb);
2023-03-29 05:30:32 +00:00
}
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->writeTbs, NULL);
2023-03-29 05:30:32 +00:00
while (stb != NULL) {
2023-03-29 11:51:49 +00:00
size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen);
SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
2023-04-07 08:39:40 +00:00
2023-03-29 11:51:49 +00:00
size_t valueLen = 0;
2023-04-07 08:39:40 +00:00
valueLen = strlen(stb) + 1;
2023-03-29 11:51:49 +00:00
SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
2023-03-30 05:59:48 +00:00
stb = taosHashIterate(pUser->writeTbs, stb);
2023-03-29 05:30:32 +00:00
}
2023-04-11 02:11:25 +00:00
int32_t *useDb = taosHashIterate(pUser->useDbs, NULL);
while (useDb != NULL) {
size_t keyLen = 0;
void *key = taosHashGetKey(useDb, &keyLen);
SDB_SET_INT32(pRaw, dataPos, keyLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
SDB_SET_INT32(pRaw, dataPos, *useDb, _OVER)
2023-05-06 11:53:57 +00:00
useDb = taosHashIterate(pUser->useDbs, useDb);
2023-04-11 02:11:25 +00:00
}
2023-09-04 13:15:46 +00:00
// save white list
int32_t num = pUser->pIpWhiteList->num;
2023-09-05 10:50:11 +00:00
int32_t tlen = sizeof(SIpWhiteList) + num * sizeof(SIpV4Range) + 4;
2023-09-04 13:15:46 +00:00
char *buf = taosMemoryCalloc(1, tlen);
int32_t len = tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteList);
SDB_SET_INT32(pRaw, dataPos, len, _OVER);
SDB_SET_BINARY(pRaw, dataPos, buf, len, _OVER);
taosMemoryFree(buf);
2023-09-12 03:31:06 +00:00
SDB_SET_INT64(pRaw, dataPos, pUser->ipWhiteListVer, _OVER);
SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2021-12-31 06:22:50 +00:00
terrno = 0;
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, terrstr());
sdbFreeRaw(pRaw);
return NULL;
}
2021-11-09 03:37:58 +00:00
2021-12-30 15:40:05 +00:00
mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
2021-11-09 03:37:58 +00:00
return pRaw;
2021-10-18 06:00:35 +00:00
}
2021-11-29 05:19:00 +00:00
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-12-01 08:04:39 +00:00
SSdbRow *pRow = NULL;
SUserObj *pUser = NULL;
2021-12-31 06:22:50 +00:00
2021-11-12 07:06:58 +00:00
int8_t sver = 0;
if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
2021-10-18 06:00:35 +00:00
2023-04-09 10:44:46 +00:00
if (sver < 1 || sver > USER_VER_NUMBER) {
2021-11-12 07:06:58 +00:00
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
goto _OVER;
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2022-12-01 08:04:39 +00:00
pRow = sdbAllocRow(sizeof(SUserObj));
if (pRow == NULL) goto _OVER;
2021-12-31 06:22:50 +00:00
2022-12-01 08:04:39 +00:00
pUser = sdbGetRowObj(pRow);
if (pUser == NULL) goto _OVER;
2022-02-10 14:49:53 +00:00
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
2023-04-12 05:09:52 +00:00
if (sver >= 4) {
2023-04-09 10:44:46 +00:00
SDB_GET_INT32(pRaw, dataPos, &pUser->passVersion, _OVER)
}
2022-02-10 14:49:53 +00:00
int32_t numOfReadDbs = 0;
int32_t numOfWriteDbs = 0;
2022-12-01 03:45:31 +00:00
int32_t numOfTopics = 0;
SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
2022-12-01 03:45:31 +00:00
if (sver >= 2) {
SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
}
2022-04-27 07:08:51 +00:00
pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->writeDbs =
taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2022-12-01 03:45:31 +00:00
pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) goto _OVER;
2022-02-10 14:49:53 +00:00
for (int32_t i = 0; i < numOfReadDbs; ++i) {
char db[TSDB_DB_FNAME_LEN] = {0};
SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
2022-02-10 14:49:53 +00:00
int32_t len = strlen(db) + 1;
taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN);
}
for (int32_t i = 0; i < numOfWriteDbs; ++i) {
char db[TSDB_DB_FNAME_LEN] = {0};
SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
2022-02-10 14:49:53 +00:00
int32_t len = strlen(db) + 1;
taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
}
2022-12-01 03:45:31 +00:00
if (sver >= 2) {
for (int32_t i = 0; i < numOfTopics; ++i) {
char topic[TSDB_TOPIC_FNAME_LEN] = {0};
SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
int32_t len = strlen(topic) + 1;
taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN);
}
}
2023-04-07 08:39:40 +00:00
if (sver >= 3) {
2023-04-11 02:11:25 +00:00
int32_t numOfReadStbs = 0;
int32_t numOfWriteStbs = 0;
int32_t numOfUseDbs = 0;
SDB_GET_INT32(pRaw, dataPos, &numOfReadStbs, _OVER)
SDB_GET_INT32(pRaw, dataPos, &numOfWriteStbs, _OVER)
SDB_GET_INT32(pRaw, dataPos, &numOfUseDbs, _OVER)
pUser->readTbs =
taosHashInit(numOfReadStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->writeTbs =
taosHashInit(numOfWriteStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->useDbs = taosHashInit(numOfUseDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2023-03-29 05:30:32 +00:00
for (int32_t i = 0; i < numOfReadStbs; ++i) {
2023-03-29 11:51:49 +00:00
int32_t keyLen = 0;
SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
2023-03-30 05:59:48 +00:00
char *key = taosMemoryCalloc(keyLen, sizeof(char));
memset(key, 0, keyLen);
2023-03-29 11:51:49 +00:00
SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
int32_t valuelen = 0;
SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
2023-03-30 05:59:48 +00:00
char *value = taosMemoryCalloc(valuelen, sizeof(char));
2023-04-11 02:11:25 +00:00
memset(value, 0, valuelen);
2023-03-29 11:51:49 +00:00
SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
2023-03-30 05:59:48 +00:00
taosHashPut(pUser->readTbs, key, keyLen, value, valuelen);
2023-03-29 11:51:49 +00:00
taosMemoryFree(key);
taosMemoryFree(value);
2023-03-29 05:30:32 +00:00
}
for (int32_t i = 0; i < numOfWriteStbs; ++i) {
2023-03-29 11:51:49 +00:00
int32_t keyLen = 0;
SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
2023-03-30 05:59:48 +00:00
char *key = taosMemoryCalloc(keyLen, sizeof(char));
memset(key, 0, keyLen);
2023-03-29 11:51:49 +00:00
SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
int32_t valuelen = 0;
SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
2023-03-30 05:59:48 +00:00
char *value = taosMemoryCalloc(valuelen, sizeof(char));
2023-04-11 07:08:42 +00:00
memset(value, 0, valuelen);
2023-03-29 11:51:49 +00:00
SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
2023-03-30 05:59:48 +00:00
taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen);
2023-03-29 11:51:49 +00:00
taosMemoryFree(key);
taosMemoryFree(value);
2023-03-29 05:30:32 +00:00
}
2023-04-11 02:11:25 +00:00
for (int32_t i = 0; i < numOfUseDbs; ++i) {
int32_t keyLen = 0;
SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
char *key = taosMemoryCalloc(keyLen, sizeof(char));
memset(key, 0, keyLen);
SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
int32_t ref = 0;
SDB_GET_INT32(pRaw, dataPos, &ref, _OVER);
taosHashPut(pUser->useDbs, key, keyLen, &ref, sizeof(ref));
2023-04-23 08:30:28 +00:00
taosMemoryFree(key);
2023-04-11 02:11:25 +00:00
}
2023-03-29 05:30:32 +00:00
}
2023-09-04 13:15:46 +00:00
// decoder white list
if (sver >= 5) {
int32_t len = 0;
SDB_GET_INT32(pRaw, dataPos, &len, _OVER);
2023-09-05 13:00:01 +00:00
char *buf = taosMemoryMalloc(len);
2023-09-04 13:15:46 +00:00
if (buf == NULL) goto _OVER;
SDB_GET_BINARY(pRaw, dataPos, buf, len, _OVER);
pUser->pIpWhiteList = createIpWhiteList(buf, len);
2023-09-05 13:00:01 +00:00
taosMemoryFree(buf);
2023-09-12 03:31:06 +00:00
SDB_GET_INT64(pRaw, dataPos, &pUser->ipWhiteListVer, _OVER);
2023-09-04 13:15:46 +00:00
}
if (pUser->pIpWhiteList == NULL) {
pUser->pIpWhiteList = createDefaultIpWhiteList();
2023-09-12 03:31:06 +00:00
pUser->ipWhiteListVer = taosGetTimestampMs();
2023-09-04 13:15:46 +00:00
}
2023-03-29 05:30:32 +00:00
SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
2022-05-05 13:52:18 +00:00
taosInitRWLatch(&pUser->lock);
2021-12-31 06:22:50 +00:00
terrno = 0;
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
2022-12-01 08:04:39 +00:00
mError("user:%s, failed to decode from raw:%p since %s", pUser == NULL ? "null" : pUser->user, pRaw, terrstr());
if (pUser != NULL) {
taosHashCleanup(pUser->readDbs);
taosHashCleanup(pUser->writeDbs);
taosHashCleanup(pUser->topics);
2023-03-30 05:59:48 +00:00
taosHashCleanup(pUser->readTbs);
taosHashCleanup(pUser->writeTbs);
2023-04-11 02:11:25 +00:00
taosHashCleanup(pUser->useDbs);
2023-09-12 03:31:06 +00:00
taosMemoryFreeClear(pUser->pIpWhiteList);
2022-12-01 08:04:39 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(pRow);
2021-12-31 06:22:50 +00:00
return NULL;
}
2021-10-18 06:00:35 +00:00
2021-12-30 15:40:05 +00:00
mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
2021-11-12 07:06:58 +00:00
return pRow;
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-11-29 07:53:02 +00:00
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
2021-12-30 15:40:05 +00:00
mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
2022-02-11 05:13:32 +00:00
2021-12-03 06:36:41 +00:00
SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
if (pAcct == NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
2021-12-01 09:27:31 +00:00
mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
2021-11-11 03:11:14 +00:00
return -1;
2021-10-18 06:00:35 +00:00
}
2021-12-03 06:36:41 +00:00
pUser->acctId = pAcct->acctId;
sdbRelease(pSdb, pAcct);
2021-10-18 06:00:35 +00:00
2021-11-09 03:37:58 +00:00
return 0;
}
2021-10-18 06:00:35 +00:00
2023-03-30 07:13:09 +00:00
SHashObj *mndDupTableHash(SHashObj *pOld) {
SHashObj *pNew =
taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pNew == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
char *tb = taosHashIterate(pOld, NULL);
while (tb != NULL) {
size_t keyLen = 0;
2023-04-07 08:39:40 +00:00
char *key = taosHashGetKey(tb, &keyLen);
2023-03-30 07:13:09 +00:00
int32_t valueLen = strlen(tb) + 1;
if (taosHashPut(pNew, key, keyLen, tb, valueLen) != 0) {
taosHashCancelIterate(pOld, tb);
taosHashCleanup(pNew);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
tb = taosHashIterate(pOld, tb);
}
return pNew;
}
2023-04-11 07:08:42 +00:00
SHashObj *mndDupUseDbHash(SHashObj *pOld) {
SHashObj *pNew =
taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pNew == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
int32_t *db = taosHashIterate(pOld, NULL);
while (db != NULL) {
size_t keyLen = 0;
char *key = taosHashGetKey(db, &keyLen);
if (taosHashPut(pNew, key, keyLen, db, sizeof(*db)) != 0) {
taosHashCancelIterate(pOld, db);
taosHashCleanup(pNew);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
db = taosHashIterate(pOld, db);
}
return pNew;
}
int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
memcpy(pNew, pUser, sizeof(SUserObj));
pNew->authVersion++;
pNew->updateTime = taosGetTimestampMs();
taosRLockLatch(&pUser->lock);
pNew->readDbs = mndDupDbHash(pUser->readDbs);
pNew->writeDbs = mndDupDbHash(pUser->writeDbs);
2023-03-30 07:13:09 +00:00
pNew->readTbs = mndDupTableHash(pUser->readTbs);
pNew->writeTbs = mndDupTableHash(pUser->writeTbs);
pNew->topics = mndDupTopicHash(pUser->topics);
2023-04-11 07:08:42 +00:00
pNew->useDbs = mndDupUseDbHash(pUser->useDbs);
2023-09-05 13:00:01 +00:00
pNew->pIpWhiteList = cloneIpWhiteList(pUser->pIpWhiteList);
taosRUnLockLatch(&pUser->lock);
if (pNew->readDbs == NULL || pNew->writeDbs == NULL || pNew->topics == NULL) {
return -1;
}
return 0;
}
void mndUserFreeObj(SUserObj *pUser) {
2022-02-10 14:49:53 +00:00
taosHashCleanup(pUser->readDbs);
taosHashCleanup(pUser->writeDbs);
2022-12-01 03:45:31 +00:00
taosHashCleanup(pUser->topics);
2023-03-30 05:59:48 +00:00
taosHashCleanup(pUser->readTbs);
taosHashCleanup(pUser->writeTbs);
2023-04-11 02:11:25 +00:00
taosHashCleanup(pUser->useDbs);
2023-09-12 03:31:06 +00:00
taosMemoryFreeClear(pUser->pIpWhiteList);
pUser->readDbs = NULL;
pUser->writeDbs = NULL;
2022-12-01 03:45:31 +00:00
pUser->topics = NULL;
2023-03-30 05:59:48 +00:00
pUser->readTbs = NULL;
pUser->writeTbs = NULL;
2023-04-11 02:11:25 +00:00
pUser->useDbs = NULL;
}
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
mndUserFreeObj(pUser);
2021-11-09 03:37:58 +00:00
return 0;
}
2022-01-04 12:04:23 +00:00
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
2022-01-10 08:24:23 +00:00
mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
2022-05-05 13:52:18 +00:00
taosWLockLatch(&pOld->lock);
2022-01-04 12:04:23 +00:00
pOld->updateTime = pNew->updateTime;
2022-05-20 07:37:39 +00:00
pOld->authVersion = pNew->authVersion;
2023-04-09 10:44:46 +00:00
pOld->passVersion = pNew->passVersion;
pOld->sysInfo = pNew->sysInfo;
pOld->enable = pNew->enable;
2022-05-05 13:52:18 +00:00
memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
2022-04-27 09:39:54 +00:00
TSWAP(pOld->readDbs, pNew->readDbs);
TSWAP(pOld->writeDbs, pNew->writeDbs);
2022-12-01 03:45:31 +00:00
TSWAP(pOld->topics, pNew->topics);
2023-03-30 05:59:48 +00:00
TSWAP(pOld->readTbs, pNew->readTbs);
TSWAP(pOld->writeTbs, pNew->writeTbs);
2023-04-11 02:11:25 +00:00
TSWAP(pOld->useDbs, pNew->useDbs);
2023-09-05 10:50:11 +00:00
2023-09-08 03:49:12 +00:00
int32_t sz = sizeof(SIpWhiteList) + pNew->pIpWhiteList->num * sizeof(SIpV4Range);
2023-09-05 10:50:11 +00:00
pOld->pIpWhiteList = taosMemoryRealloc(pOld->pIpWhiteList, sz);
memcpy(pOld->pIpWhiteList, pNew->pIpWhiteList, sz);
2023-09-14 04:18:04 +00:00
pOld->ipWhiteListVer = pNew->ipWhiteListVer;
2023-09-05 10:50:11 +00:00
2022-05-05 13:52:18 +00:00
taosWUnLockLatch(&pOld->lock);
2022-02-10 14:49:53 +00:00
2021-11-09 03:37:58 +00:00
return 0;
}
2022-05-03 04:57:55 +00:00
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
2021-12-20 12:30:55 +00:00
SSdb *pSdb = pMnode->pSdb;
SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
if (pUser == NULL) {
2023-03-10 06:19:03 +00:00
if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
} else {
terrno = TSDB_CODE_MND_USER_NOT_AVAILABLE;
}
2021-12-20 12:30:55 +00:00
}
return pUser;
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-12-03 12:52:44 +00:00
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
SSdb *pSdb = pMnode->pSdb;
sdbRelease(pSdb, pUser);
2021-10-18 06:00:35 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
2021-11-09 06:27:17 +00:00
SUserObj userObj = {0};
2022-02-11 06:09:03 +00:00
taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
2021-11-09 06:27:17 +00:00
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
2022-06-25 01:09:33 +00:00
userObj.superUser = 0; // pCreate->superUser;
userObj.sysInfo = pCreate->sysInfo;
userObj.enable = pCreate->enable;
2021-11-09 06:27:17 +00:00
2023-09-04 13:15:46 +00:00
if (pCreate->numIpRanges == 0) {
userObj.pIpWhiteList = createDefaultIpWhiteList();
} else {
SIpWhiteList *p = taosMemoryCalloc(1, sizeof(SIpWhiteList) + pCreate->numIpRanges * sizeof(SIpV4Range));
for (int i = 0; i < pCreate->numIpRanges; i++) {
p->pIpRange[i].ip = pCreate->pIpRanges[i].ip;
p->pIpRange[i].mask = pCreate->pIpRanges[i].mask;
}
2023-09-07 12:47:10 +00:00
p->num = pCreate->numIpRanges;
2023-09-04 13:15:46 +00:00
userObj.pIpWhiteList = p;
}
2023-09-12 03:31:06 +00:00
userObj.ipWhiteListVer = taosGetTimestampMs();
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
2021-12-07 12:14:22 +00:00
if (pTrans == NULL) {
2022-02-11 06:09:03 +00:00
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
2023-09-04 13:15:46 +00:00
taosMemoryFree(userObj.pIpWhiteList);
2021-12-07 12:14:22 +00:00
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
2021-11-11 03:11:14 +00:00
2022-05-23 13:08:00 +00:00
SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2023-09-04 13:15:46 +00:00
goto _OVER;
2021-11-09 06:27:17 +00:00
}
2022-10-08 03:29:46 +00:00
(void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
2021-12-07 13:10:36 +00:00
2021-12-14 09:13:18 +00:00
if (mndTransPrepare(pMnode, pTrans) != 0) {
2021-12-07 13:10:36 +00:00
mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2023-09-04 13:15:46 +00:00
goto _OVER;
2021-11-09 06:27:17 +00:00
}
2023-09-07 12:47:10 +00:00
ipWhiteMgtUpdate(userObj.user, userObj.pIpWhiteList);
2023-09-12 13:03:35 +00:00
taosMemoryFree(userObj.pIpWhiteList);
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return 0;
2023-09-04 13:15:46 +00:00
_OVER:
taosMemoryFree(userObj.pIpWhiteList);
return -1;
2021-11-09 06:27:17 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-11 05:13:32 +00:00
int32_t code = -1;
SUserObj *pUser = NULL;
SUserObj *pOperUser = NULL;
SCreateUserReq createReq = {0};
2022-05-16 06:55:31 +00:00
if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2021-11-09 06:27:17 +00:00
2022-09-23 07:42:36 +00:00
mInfo("user:%s, start to create", createReq.user);
2022-06-25 01:09:33 +00:00
if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
goto _OVER;
}
2021-12-07 12:14:22 +00:00
2023-07-19 07:04:39 +00:00
if (createReq.user[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
goto _OVER;
2021-11-09 06:27:17 +00:00
}
2022-02-11 05:13:32 +00:00
if (createReq.pass[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
goto _OVER;
2021-11-09 06:27:17 +00:00
}
2023-09-04 13:15:46 +00:00
if (strlen(createReq.pass) >= TSDB_PASSWORD_LEN) {
2023-07-19 07:04:39 +00:00
terrno = TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG;
goto _OVER;
}
2022-02-11 05:13:32 +00:00
pUser = mndAcquireUser(pMnode, createReq.user);
2021-11-09 06:27:17 +00:00
if (pUser != NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
goto _OVER;
2021-11-09 06:27:17 +00:00
}
pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
2021-11-09 06:27:17 +00:00
if (pOperUser == NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
goto _OVER;
2021-11-09 06:27:17 +00:00
}
2022-07-18 05:22:21 +00:00
if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
code = terrno;
goto _OVER;
}
2022-02-11 06:09:03 +00:00
code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2021-11-09 06:27:17 +00:00
2023-08-28 09:44:10 +00:00
char detail[1000] = {0};
2023-09-04 13:15:46 +00:00
sprintf(detail, "createType:%d, enable:%d, superUser:%d, sysInfo:%d", createReq.createType, createReq.enable,
createReq.superUser, createReq.sysInfo);
2023-08-28 09:44:10 +00:00
auditRecord(pReq, pMnode->clusterId, "createUser", createReq.user, "", detail);
2023-08-28 01:15:51 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-11 05:13:32 +00:00
mError("user:%s, failed to create since %s", createReq.user, terrstr());
2021-11-09 06:27:17 +00:00
}
2022-02-11 05:13:32 +00:00
mndReleaseUser(pMnode, pUser);
mndReleaseUser(pMnode, pOperUser);
2023-08-29 09:35:03 +00:00
tFreeSCreateUserReq(&createReq);
2022-02-11 05:13:32 +00:00
return code;
2021-11-09 06:27:17 +00:00
}
int32_t mndProcessGetUserWhiteListReq(SRpcMsg *pReq) {
2023-09-13 06:27:58 +00:00
SMnode *pMnode = pReq->info.node;
int32_t code = -1;
SUserObj *pUser = NULL;
SGetUserWhiteListReq wlReq = {0};
SGetUserWhiteListRsp wlRsp = {0};
if (tDeserializeSGetUserWhiteListReq(pReq->pCont, pReq->contLen, &wlReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
}
mTrace("user: %s, start to get whitelist", wlReq.user);
pUser = mndAcquireUser(pMnode, wlReq.user);
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
goto _OVER;
}
code = mndSetUserWhiteListRsp(pMnode, pUser, &wlRsp);
if (code) {
goto _OVER;
}
int32_t contLen = tSerializeSGetUserWhiteListRsp(NULL, 0, &wlRsp);
void *pRsp = rpcMallocCont(contLen);
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
}
tSerializeSGetUserWhiteListRsp(pRsp, contLen, &wlRsp);
pReq->info.rsp = pRsp;
pReq->info.rspLen = contLen;
code = 0;
_OVER:
mndReleaseUser(pMnode, pUser);
tFreeSGetUserWhiteListRsp(&wlRsp);
return code;
}
2023-09-06 13:36:16 +00:00
int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
// impl later
SRetrieveIpWhiteReq req = {0};
if (tDeserializeRetrieveIpWhite(pReq->pCont, pReq->contLen, &req) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
}
2023-09-07 03:54:34 +00:00
SUpdateIpWhite ipWhite = {0};
ipWhiteMgtFillMsg(&ipWhite);
2023-09-06 13:36:16 +00:00
int32_t len = tSerializeSUpdateIpWhite(NULL, 0, &ipWhite);
void *pRsp = rpcMallocCont(len);
tSerializeSUpdateIpWhite(pRsp, len, &ipWhite);
2023-09-11 02:45:45 +00:00
pReq->info.rsp = pRsp;
pReq->info.rspLen = len;
//}
2023-09-06 13:36:16 +00:00
tFreeSUpdateIpWhiteReq(&ipWhite);
return 0;
_OVER:
return -1;
}
2021-11-09 06:27:17 +00:00
2022-05-16 06:55:31 +00:00
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
2021-12-14 11:29:37 +00:00
if (pTrans == NULL) {
2022-04-27 07:08:51 +00:00
mError("user:%s, failed to alter since %s", pOld->user, terrstr());
2021-12-14 11:29:37 +00:00
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
2021-12-14 11:29:37 +00:00
2022-05-23 13:08:00 +00:00
SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
2021-12-14 11:29:37 +00:00
mndTransDrop(pTrans);
return -1;
}
2022-10-08 03:29:46 +00:00
(void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
2021-12-14 11:29:37 +00:00
if (mndTransPrepare(pMnode, pTrans) != 0) {
mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
2023-09-07 12:47:10 +00:00
ipWhiteMgtUpdate(pNew->user, pNew->pIpWhiteList);
2021-12-14 11:29:37 +00:00
mndTransDrop(pTrans);
return 0;
}
SHashObj *mndDupObjHash(SHashObj *pOld, int32_t dataLen) {
2022-04-27 07:08:51 +00:00
SHashObj *pNew =
taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
2022-02-11 05:13:32 +00:00
if (pNew == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
char *db = taosHashIterate(pOld, NULL);
while (db != NULL) {
int32_t len = strlen(db) + 1;
if (taosHashPut(pNew, db, len, db, dataLen) != 0) {
2022-02-11 05:13:32 +00:00
taosHashCancelIterate(pOld, db);
taosHashCleanup(pNew);
2022-04-27 07:08:51 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-02-11 05:13:32 +00:00
return NULL;
}
db = taosHashIterate(pOld, db);
}
return pNew;
}
SHashObj *mndDupDbHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN); }
SHashObj *mndDupTopicHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN); }
2023-04-11 02:11:25 +00:00
static int32_t mndTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
SSdb *pSdb) {
2023-04-07 08:39:40 +00:00
void *pIter = NULL;
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
2023-03-30 07:56:08 +00:00
2023-03-30 08:17:51 +00:00
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
int32_t len = strlen(tbFName) + 1;
2023-03-30 07:56:08 +00:00
2023-04-07 08:39:40 +00:00
if (alterReq->tagCond != NULL && alterReq->tagCondLen != 0) {
char *value = taosHashGet(hash, tbFName, len);
2023-04-07 08:39:40 +00:00
if (value != NULL) {
terrno = TSDB_CODE_MND_PRIVILEDGE_EXIST;
return -1;
}
2023-04-11 02:11:25 +00:00
int32_t condLen = alterReq->tagCondLen;
if (taosHashPut(hash, tbFName, len, alterReq->tagCond, condLen) != 0) {
return -1;
}
2023-04-07 08:39:40 +00:00
} else {
if (taosHashPut(hash, tbFName, len, "t", 2) != 0) {
return -1;
}
2023-03-30 07:56:08 +00:00
}
2023-03-30 08:17:51 +00:00
2023-04-11 02:11:25 +00:00
int32_t dbKeyLen = strlen(alterReq->objname) + 1;
int32_t ref = 1;
int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
if (NULL != currRef) {
ref = (*currRef) + 1;
}
if (taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)) != 0) {
return -1;
}
2023-03-30 07:56:08 +00:00
return 0;
}
2023-04-11 02:11:25 +00:00
static int32_t mndRemoveTablePriviledge(SMnode *pMnode, SHashObj *hash, SHashObj *useDbHash, SAlterUserReq *alterReq,
SSdb *pSdb) {
2023-04-07 08:39:40 +00:00
void *pIter = NULL;
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
2023-03-30 08:17:51 +00:00
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq->objname, alterReq->tabName);
int32_t len = strlen(tbFName) + 1;
2023-03-30 08:10:34 +00:00
2023-03-30 08:17:51 +00:00
if (taosHashRemove(hash, tbFName, len) != 0) {
return -1;
}
2023-03-30 08:10:34 +00:00
2023-04-11 02:11:25 +00:00
int32_t dbKeyLen = strlen(alterReq->objname) + 1;
int32_t *currRef = taosHashGet(useDbHash, alterReq->objname, dbKeyLen);
if (NULL == currRef || 1 == *currRef) {
if (taosHashRemove(useDbHash, alterReq->objname, dbKeyLen) != 0) {
return -1;
}
return 0;
}
int32_t ref = (*currRef) - 1;
if (taosHashPut(useDbHash, alterReq->objname, dbKeyLen, &ref, sizeof(ref)) != 0) {
return -1;
}
2023-03-30 08:10:34 +00:00
return 0;
}
2023-09-04 13:15:46 +00:00
static char *mndUserAuditTypeStr(int32_t type) {
if (type == TSDB_ALTER_USER_PASSWD) {
2023-08-30 04:06:11 +00:00
return "changePassword";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_SUPERUSER) {
2023-08-30 04:06:11 +00:00
return "changeSuperUser";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_READ_DB) {
2023-08-30 04:06:11 +00:00
return "addReadToDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_READ_DB) {
2023-08-30 04:06:11 +00:00
return "addReadToDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_READ_DB) {
2023-08-30 04:06:11 +00:00
return "removeReadFromDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_WRITE_DB) {
2023-08-30 04:06:11 +00:00
return "addWriteToDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_WRITE_DB) {
2023-08-30 04:06:11 +00:00
return "removeWriteFromDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_ALL_DB) {
2023-08-30 04:06:11 +00:00
return "addToAllDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_ALL_DB) {
2023-08-30 04:06:11 +00:00
return "removeFromAllDB";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ENABLE) {
2023-08-30 04:06:11 +00:00
return "enableUser";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_SYSINFO) {
2023-08-30 04:06:11 +00:00
return "userSysInfo";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
2023-08-30 04:06:11 +00:00
return "addSubscribeTopic";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_SUBSCRIBE_TOPIC) {
2023-08-30 04:06:11 +00:00
return "removeSubscribeTopic";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_READ_TABLE) {
2023-08-30 04:06:11 +00:00
return "addReadToTable";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_READ_TABLE) {
2023-08-30 04:06:11 +00:00
return "removeReadFromTable";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_WRITE_TABLE) {
2023-08-30 04:06:11 +00:00
return "addWriteToTable";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_WRITE_TABLE) {
2023-08-30 04:06:11 +00:00
return "removeWriteFromTable";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_ADD_ALL_TABLE) {
2023-08-30 04:06:11 +00:00
return "addToAllTable";
}
2023-09-04 13:15:46 +00:00
if (type == TSDB_ALTER_USER_REMOVE_ALL_TABLE) {
2023-08-30 04:06:11 +00:00
return "removeFromAllTable";
}
return "error";
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-05-09 08:03:31 +00:00
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
2022-02-11 05:13:32 +00:00
int32_t code = -1;
SUserObj *pUser = NULL;
SUserObj *pOperUser = NULL;
2022-02-11 06:09:03 +00:00
SUserObj newUser = {0};
2022-02-11 05:13:32 +00:00
SAlterUserReq alterReq = {0};
2022-05-16 06:55:31 +00:00
if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2021-12-07 13:10:36 +00:00
2022-09-23 07:42:36 +00:00
mInfo("user:%s, start to alter", alterReq.user);
2021-12-07 13:10:36 +00:00
2022-02-11 05:13:32 +00:00
if (alterReq.user[0] == 0) {
2021-12-07 13:10:36 +00:00
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
goto _OVER;
}
2023-09-04 13:15:46 +00:00
if (TSDB_ALTER_USER_PASSWD == alterReq.alterType &&
2023-07-10 06:58:28 +00:00
(alterReq.pass[0] == 0 || strlen(alterReq.pass) >= TSDB_PASSWORD_LEN)) {
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
goto _OVER;
2021-12-07 13:10:36 +00:00
}
2022-02-11 05:13:32 +00:00
pUser = mndAcquireUser(pMnode, alterReq.user);
2021-12-07 13:10:36 +00:00
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
goto _OVER;
2021-12-07 13:10:36 +00:00
}
pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
2021-12-07 13:10:36 +00:00
if (pOperUser == NULL) {
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
goto _OVER;
2021-12-07 13:10:36 +00:00
}
2022-06-25 01:09:33 +00:00
if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
2022-05-09 08:03:31 +00:00
goto _OVER;
}
if (mndUserDupObj(pUser, &newUser) != 0) goto _OVER;
2022-02-11 05:13:32 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
char pass[TSDB_PASSWORD_LEN + 1] = {0};
taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
2022-04-27 07:35:10 +00:00
memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
2023-04-09 10:44:46 +00:00
if (0 != strncmp(pUser->pass, pass, TSDB_PASSWORD_LEN)) {
++newUser.passVersion;
}
2022-05-09 08:03:31 +00:00
}
if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
2022-02-11 05:13:32 +00:00
newUser.superUser = alterReq.superUser;
2022-05-09 08:03:31 +00:00
}
if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
newUser.enable = alterReq.enable;
}
if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
newUser.sysInfo = alterReq.sysInfo;
}
2022-05-09 08:03:31 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
if (strcmp(alterReq.objname, "1.*") != 0) {
int32_t len = strlen(alterReq.objname) + 1;
SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
2022-05-09 08:03:31 +00:00
if (pDb == NULL) {
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
if (taosHashPut(newUser.readDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
2022-05-09 08:03:31 +00:00
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
mndReleaseDb(pMnode, pDb);
2022-05-09 08:03:31 +00:00
} else {
while (1) {
SDbObj *pDb = NULL;
pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
if (pIter == NULL) break;
int32_t len = strlen(pDb->name) + 1;
taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
sdbRelease(pSdb, pDb);
}
2022-02-11 05:13:32 +00:00
}
2022-05-09 08:03:31 +00:00
}
if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
if (strcmp(alterReq.objname, "1.*") != 0) {
int32_t len = strlen(alterReq.objname) + 1;
SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
2022-05-09 08:03:31 +00:00
if (pDb == NULL) {
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
if (taosHashPut(newUser.writeDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
2022-05-09 08:03:31 +00:00
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
mndReleaseDb(pMnode, pDb);
2022-05-09 08:03:31 +00:00
} else {
while (1) {
SDbObj *pDb = NULL;
pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
if (pIter == NULL) break;
int32_t len = strlen(pDb->name) + 1;
taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
sdbRelease(pSdb, pDb);
}
2022-02-11 05:13:32 +00:00
}
}
2022-05-09 08:03:31 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
if (strcmp(alterReq.objname, "1.*") != 0) {
int32_t len = strlen(alterReq.objname) + 1;
SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
2022-05-09 08:03:31 +00:00
if (pDb == NULL) {
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
taosHashRemove(newUser.readDbs, alterReq.objname, len);
mndReleaseDb(pMnode, pDb);
2022-05-09 08:03:31 +00:00
} else {
taosHashClear(newUser.readDbs);
}
}
2021-12-07 13:10:36 +00:00
2022-05-09 08:03:31 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
if (strcmp(alterReq.objname, "1.*") != 0) {
int32_t len = strlen(alterReq.objname) + 1;
SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
2022-05-09 08:03:31 +00:00
if (pDb == NULL) {
mndReleaseDb(pMnode, pDb);
goto _OVER;
}
taosHashRemove(newUser.writeDbs, alterReq.objname, len);
mndReleaseDb(pMnode, pDb);
2022-05-09 08:03:31 +00:00
} else {
taosHashClear(newUser.writeDbs);
}
2022-02-11 06:09:03 +00:00
}
2023-07-18 03:27:01 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TABLE || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_TABLE) {
2023-04-11 02:11:25 +00:00
if (mndTablePriviledge(pMnode, newUser.readTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
2023-03-29 05:30:32 +00:00
}
2023-07-18 03:27:01 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TABLE || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_TABLE) {
2023-04-11 02:11:25 +00:00
if (mndTablePriviledge(pMnode, newUser.writeTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
2023-03-29 05:30:32 +00:00
}
2023-09-04 13:15:46 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_TABLE) {
2023-04-11 02:11:25 +00:00
if (mndRemoveTablePriviledge(pMnode, newUser.readTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
2023-03-30 05:59:48 +00:00
}
2023-09-04 13:15:46 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_TABLE) {
2023-04-11 02:11:25 +00:00
if (mndRemoveTablePriviledge(pMnode, newUser.writeTbs, newUser.useDbs, &alterReq, pSdb) != 0) goto _OVER;
2023-03-30 05:59:48 +00:00
}
2022-12-01 03:45:31 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
int32_t len = strlen(alterReq.objname) + 1;
SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
if (pTopic == NULL) {
mndReleaseTopic(pMnode, pTopic);
goto _OVER;
}
taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
}
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_SUBSCRIBE_TOPIC) {
int32_t len = strlen(alterReq.objname) + 1;
SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
if (pTopic == NULL) {
mndReleaseTopic(pMnode, pTopic);
goto _OVER;
}
taosHashRemove(newUser.topics, alterReq.objname, len);
}
2023-09-04 13:15:46 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_ADD_WHITE_LIST) {
2023-09-14 07:34:28 +00:00
taosMemoryFreeClear(newUser.pIpWhiteList);
2023-09-10 09:02:38 +00:00
2023-09-04 13:15:46 +00:00
int32_t num = pUser->pIpWhiteList->num + alterReq.numIpRanges;
SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
int32_t idx = pUser->pIpWhiteList->num;
2023-09-14 07:34:28 +00:00
bool exist = false;
2023-09-04 13:15:46 +00:00
memcpy(pNew->pIpRange, pUser->pIpWhiteList->pIpRange, sizeof(SIpV4Range) * idx);
for (int i = 0; i < alterReq.numIpRanges; i++) {
SIpV4Range *range = &(alterReq.pIpRanges[i]);
if (!isRangeInIpWhiteList(pUser->pIpWhiteList, range)) {
// already exist, just ignore;
memcpy(&pNew->pIpRange[idx], range, sizeof(SIpV4Range));
idx++;
continue;
2023-09-14 07:34:28 +00:00
} else {
exist = true;
2023-09-04 13:15:46 +00:00
}
}
2023-09-14 07:34:28 +00:00
if (exist) {
taosMemoryFree(pNew);
terrno = TSDB_CODE_MND_USER_HOST_EXIST;
code = terrno;
goto _OVER;
}
2023-09-05 10:50:11 +00:00
pNew->num = idx;
2023-09-04 13:15:46 +00:00
newUser.pIpWhiteList = pNew;
2023-09-12 03:31:06 +00:00
newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
2023-09-14 07:34:28 +00:00
if (pNew->num >= TSDB_PRIVILEDGE_HOST_LEN / 24) {
terrno = TSDB_CODE_MND_TOO_MANY_USER_HOST;
code = terrno;
goto _OVER;
}
2023-09-04 13:15:46 +00:00
}
if (alterReq.alterType == TSDB_ALTER_USER_DROP_WHITE_LIST) {
2023-09-14 07:34:28 +00:00
taosMemoryFreeClear(newUser.pIpWhiteList);
2023-09-10 09:02:38 +00:00
2023-09-04 13:15:46 +00:00
int32_t num = pUser->pIpWhiteList->num;
SIpWhiteList *pNew = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * num);
2023-09-14 07:34:28 +00:00
bool noexist = true;
2023-09-04 13:15:46 +00:00
2023-09-05 10:50:11 +00:00
if (pUser->pIpWhiteList->num > 0) {
int idx = 0;
2023-09-07 06:24:26 +00:00
for (int i = 0; i < pUser->pIpWhiteList->num; i++) {
SIpV4Range *oldRange = &pUser->pIpWhiteList->pIpRange[i];
bool found = false;
for (int j = 0; j < alterReq.numIpRanges; j++) {
SIpV4Range *range = &alterReq.pIpRanges[j];
2023-09-14 07:34:28 +00:00
if (!isDefaultRange(range) && isIpRangeEqual(oldRange, range)) {
2023-09-07 06:24:26 +00:00
found = true;
break;
}
}
if (found == false) {
memcpy(&pNew->pIpRange[idx], oldRange, sizeof(SIpV4Range));
2023-09-05 10:50:11 +00:00
idx++;
}
2023-09-14 07:34:28 +00:00
if (found == true) {
noexist = false;
}
2023-09-04 13:15:46 +00:00
}
2023-09-05 10:50:11 +00:00
pNew->num = idx;
newUser.pIpWhiteList = pNew;
2023-09-12 03:31:06 +00:00
newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
2023-09-05 10:50:11 +00:00
} else {
pNew->num = 0;
newUser.pIpWhiteList = pNew;
2023-09-12 03:31:06 +00:00
newUser.ipWhiteListVer = pUser->ipWhiteListVer + 1;
2023-09-04 13:15:46 +00:00
}
2023-09-14 07:34:28 +00:00
if (noexist) {
terrno = TSDB_CODE_MND_USER_HOST_NOT_EXIST;
code = terrno;
goto _OVER;
}
2023-09-04 13:15:46 +00:00
}
2022-04-27 07:08:51 +00:00
code = mndAlterUser(pMnode, pUser, &newUser, pReq);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2021-12-07 13:10:36 +00:00
2023-08-28 09:44:10 +00:00
char detail[1000] = {0};
2023-09-04 13:15:46 +00:00
sprintf(detail, "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, tabName:%s, password:",
mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
alterReq.tabName);
2023-08-28 09:44:10 +00:00
2023-09-04 13:15:46 +00:00
if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
sprintf(detail, "alterType:%s, enable:%d, superUser:%d, sysInfo:%d, tabName:%s, password:xxx",
mndUserAuditTypeStr(alterReq.alterType), alterReq.enable, alterReq.superUser, alterReq.sysInfo,
2023-08-31 08:37:24 +00:00
alterReq.tabName);
auditRecord(pReq, pMnode->clusterId, "alterUser", alterReq.user, "", detail);
2023-09-04 13:15:46 +00:00
} else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER || alterReq.alterType == TSDB_ALTER_USER_ENABLE ||
alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
2023-08-29 10:12:08 +00:00
auditRecord(pReq, pMnode->clusterId, "alterUser", alterReq.user, "", detail);
2023-09-04 13:15:46 +00:00
} else if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB ||
alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_TABLE) {
if (strcmp(alterReq.objname, "1.*") != 0) {
2023-08-29 10:12:08 +00:00
SName name = {0};
tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB);
auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.user, name.dbname, detail);
2023-09-04 13:15:46 +00:00
} else {
2023-08-29 10:12:08 +00:00
auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.user, "*", detail);
}
2023-09-04 13:15:46 +00:00
} else if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
2023-08-28 09:44:10 +00:00
auditRecord(pReq, pMnode->clusterId, "GrantPrivileges", alterReq.user, alterReq.objname, detail);
2023-09-04 13:15:46 +00:00
} else if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_SUBSCRIBE_TOPIC) {
2023-08-28 09:44:10 +00:00
auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.user, alterReq.objname, detail);
2023-09-04 13:15:46 +00:00
} else {
if (strcmp(alterReq.objname, "1.*") != 0) {
2023-08-29 10:12:08 +00:00
SName name = {0};
tNameFromString(&name, alterReq.objname, T_NAME_ACCT | T_NAME_DB);
auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.user, name.dbname, detail);
2023-09-04 13:15:46 +00:00
} else {
2023-08-29 10:12:08 +00:00
auditRecord(pReq, pMnode->clusterId, "RevokePrivileges", alterReq.user, "*", detail);
}
2023-08-28 01:15:51 +00:00
}
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-11 05:13:32 +00:00
mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
2021-12-07 13:10:36 +00:00
}
2023-04-23 08:30:28 +00:00
tFreeSAlterUserReq(&alterReq);
2022-02-11 05:13:32 +00:00
mndReleaseUser(pMnode, pOperUser);
mndReleaseUser(pMnode, pUser);
mndUserFreeObj(&newUser);
2022-02-11 05:13:32 +00:00
return code;
2021-12-01 09:27:31 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
2021-12-14 11:29:37 +00:00
if (pTrans == NULL) {
mError("user:%s, failed to drop since %s", pUser->user, terrstr());
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
2021-12-14 11:29:37 +00:00
2022-05-23 13:08:00 +00:00
SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
2021-12-14 11:29:37 +00:00
mndTransDrop(pTrans);
return -1;
}
2022-10-08 03:29:46 +00:00
(void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
2021-12-14 11:29:37 +00:00
if (mndTransPrepare(pMnode, pTrans) != 0) {
mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
2023-09-07 12:47:10 +00:00
ipWhiteMgtRemove(pUser->user);
2021-12-14 11:29:37 +00:00
mndTransDrop(pTrans);
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-11 05:13:32 +00:00
int32_t code = -1;
SUserObj *pUser = NULL;
SDropUserReq dropReq = {0};
2022-05-16 06:55:31 +00:00
if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2021-12-07 13:10:36 +00:00
2022-09-23 07:42:36 +00:00
mInfo("user:%s, start to drop", dropReq.user);
2022-06-25 01:09:33 +00:00
if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
goto _OVER;
}
2021-12-07 13:10:36 +00:00
2022-02-11 05:13:32 +00:00
if (dropReq.user[0] == 0) {
2021-12-07 13:10:36 +00:00
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
goto _OVER;
2021-12-07 13:10:36 +00:00
}
2022-02-11 05:13:32 +00:00
pUser = mndAcquireUser(pMnode, dropReq.user);
2021-12-07 13:10:36 +00:00
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
goto _OVER;
2021-12-07 13:10:36 +00:00
}
2022-02-11 05:13:32 +00:00
code = mndDropUser(pMnode, pReq, pUser);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2021-12-07 13:10:36 +00:00
2023-08-28 09:44:10 +00:00
auditRecord(pReq, pMnode->clusterId, "dropUser", dropReq.user, "", "");
2023-08-28 01:15:51 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-11 05:13:32 +00:00
mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
2021-12-07 13:10:36 +00:00
}
2022-02-11 05:13:32 +00:00
mndReleaseUser(pMnode, pUser);
return code;
2021-12-07 12:14:22 +00:00
}
2022-05-16 06:55:31 +00:00
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-11 07:20:27 +00:00
int32_t code = -1;
SUserObj *pUser = NULL;
SGetUserAuthReq authReq = {0};
SGetUserAuthRsp authRsp = {0};
2022-05-16 06:55:31 +00:00
if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2022-02-11 07:20:27 +00:00
mTrace("user:%s, start to get auth", authReq.user);
pUser = mndAcquireUser(pMnode, authReq.user);
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
goto _OVER;
2022-02-11 07:20:27 +00:00
}
2022-05-06 06:13:56 +00:00
code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
if (code) {
goto _OVER;
2022-02-11 07:20:27 +00:00
}
2022-02-11 09:48:26 +00:00
int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
2022-02-11 07:20:27 +00:00
void *pRsp = rpcMallocCont(contLen);
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
2022-02-11 07:20:27 +00:00
}
2022-02-11 09:48:26 +00:00
tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
2022-02-11 07:20:27 +00:00
2022-05-16 06:55:31 +00:00
pReq->info.rsp = pRsp;
pReq->info.rspLen = contLen;
2022-02-11 07:20:27 +00:00
code = 0;
_OVER:
2022-02-11 07:20:27 +00:00
mndReleaseUser(pMnode, pUser);
2022-04-27 07:08:51 +00:00
tFreeSGetUserAuthRsp(&authRsp);
2022-02-11 07:20:27 +00:00
return code;
}
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SMnode *pMnode = pReq->info.node;
2021-12-07 12:14:22 +00:00
SSdb *pSdb = pMnode->pSdb;
int32_t numOfRows = 0;
SUserObj *pUser = NULL;
int32_t cols = 0;
char *pWrite;
while (numOfRows < rows) {
pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
if (pShow->pIter == NULL) break;
cols = 0;
2022-04-26 09:42:57 +00:00
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
char name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
2022-04-14 07:11:13 +00:00
cols++;
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
cols++;
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->enable, false);
cols++;
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->sysInfo, false);
2022-04-14 07:11:13 +00:00
cols++;
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
2021-12-07 12:14:22 +00:00
2023-09-05 10:50:11 +00:00
cols++;
2023-09-12 03:31:06 +00:00
char *buf = NULL;
int32_t tlen = convertIpWhiteListToStr(pUser->pIpWhiteList, &buf);
// int32_t tlen = mndFetchIpWhiteList(pUser->pIpWhiteList, &buf);
2023-09-05 10:50:11 +00:00
if (tlen != 0) {
char *varstr = taosMemoryCalloc(1, VARSTR_HEADER_SIZE + tlen);
varDataSetLen(varstr, tlen);
memcpy(varDataVal(varstr), buf, tlen);
2023-09-04 13:15:46 +00:00
2023-09-05 10:50:11 +00:00
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
colDataSetVal(pColInfo, numOfRows, (const char *)varstr, false);
2023-09-04 13:15:46 +00:00
2023-09-05 10:50:11 +00:00
taosMemoryFree(varstr);
taosMemoryFree(buf);
} else {
pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
colDataSetVal(pColInfo, numOfRows, (const char *)NULL, true);
}
2023-09-04 13:15:46 +00:00
2021-12-07 12:14:22 +00:00
numOfRows++;
sdbRelease(pSdb, pUser);
}
pShow->numOfRows += numOfRows;
2021-12-07 12:14:22 +00:00
return numOfRows;
}
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetch(pSdb, pIter);
2022-02-11 07:20:27 +00:00
}
2022-05-06 06:13:56 +00:00
2023-04-07 08:39:40 +00:00
static void mndLoopHash(SHashObj *hash, char *priType, SSDataBlock *pBlock, int32_t *numOfRows, char *user,
SShowObj *pShow) {
char *value = taosHashIterate(hash, NULL);
2023-03-30 05:59:48 +00:00
int32_t cols = 0;
2023-04-07 08:39:40 +00:00
2023-03-30 05:59:48 +00:00
while (value != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)userName, false);
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)privilege, false);
2023-04-07 08:39:40 +00:00
2023-03-30 05:59:48 +00:00
size_t keyLen = 0;
void *key = taosHashGetKey(value, &keyLen);
2023-03-30 10:53:50 +00:00
2023-04-07 08:39:40 +00:00
char dbName[TSDB_DB_NAME_LEN] = {0};
2023-03-30 10:53:50 +00:00
mndExtractShortDbNameFromStbFullName(key, dbName);
2023-04-07 08:39:40 +00:00
char dbNameContent[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(dbNameContent, dbName, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)dbNameContent, false);
2023-04-07 08:39:40 +00:00
char tableName[TSDB_TABLE_NAME_LEN] = {0};
2023-03-30 05:59:48 +00:00
mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
2023-04-07 08:39:40 +00:00
char tableNameContent[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(tableNameContent, tableName, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)tableNameContent, false);
2023-03-30 05:59:48 +00:00
2023-04-07 08:39:40 +00:00
if (strcmp("t", value) != 0) {
2023-03-30 05:59:48 +00:00
SNode *pAst = NULL;
int32_t sqlLen = 0;
2023-09-04 13:15:46 +00:00
size_t bufSz = strlen(value) + 1;
char *sql = taosMemoryMalloc(bufSz + 1);
char *obj = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 05:59:48 +00:00
if (sql != NULL && obj != NULL && nodesStringToNode(value, &pAst) == 0) {
nodesNodeToSQL(pAst, sql, bufSz, &sqlLen);
2023-03-30 10:53:50 +00:00
nodesDestroyNode(pAst);
2023-04-07 08:39:40 +00:00
} else {
2023-03-30 05:59:48 +00:00
sqlLen = 5;
sprintf(sql, "error");
}
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(obj, sql, pShow->pMeta->pSchemas[cols].bytes);
2023-03-30 05:59:48 +00:00
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)obj, false);
taosMemoryFree(obj);
taosMemoryFree(sql);
2023-04-07 08:39:40 +00:00
} else {
2023-09-04 13:15:46 +00:00
char *condition = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)condition, false);
taosMemoryFree(condition);
2023-03-30 10:53:50 +00:00
}
2023-04-07 08:39:40 +00:00
2023-03-30 05:59:48 +00:00
(*numOfRows)++;
value = taosHashIterate(hash, value);
}
}
2022-12-01 05:37:32 +00:00
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb;
int32_t numOfRows = 0;
SUserObj *pUser = NULL;
int32_t cols = 0;
char *pWrite;
2023-09-04 13:15:46 +00:00
bool fetchNextUser = pShow->restore ? false : true;
pShow->restore = false;
2022-12-01 05:37:32 +00:00
while (numOfRows < rows) {
if (fetchNextUser) {
pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
if (pShow->pIter == NULL) break;
} else {
fetchNextUser = true;
void *pKey = taosHashGetKey(pShow->pIter, NULL);
pUser = sdbAcquire(pSdb, SDB_USER, pKey);
if (!pUser) {
continue;
}
}
2022-12-01 05:37:32 +00:00
int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
int32_t numOfTopics = taosHashGetSize(pUser->topics);
2023-03-30 10:53:50 +00:00
int32_t numOfReadTbs = taosHashGetSize(pUser->readTbs);
int32_t numOfWriteTbs = taosHashGetSize(pUser->writeTbs);
if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics + numOfReadTbs + numOfWriteTbs >= rows) {
2023-09-04 13:15:46 +00:00
mInfo(
"will restore. current num of rows: %d, read dbs %d, write dbs %d, topics %d, read tables %d, write tables "
"%d",
numOfRows, numOfReadDbs, numOfWriteDbs, numOfTopics, numOfReadTbs, numOfWriteTbs);
pShow->restore = true;
sdbRelease(pSdb, pUser);
break;
}
2022-12-01 05:37:32 +00:00
if (pUser->superUser) {
cols = 0;
2022-12-15 08:18:48 +00:00
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
2022-12-15 08:18:48 +00:00
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
char objName[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
2023-04-07 08:39:40 +00:00
char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);
2023-09-04 13:15:46 +00:00
char *condition = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);
taosMemoryFree(condition);
2023-03-30 10:53:50 +00:00
numOfRows++;
}
2022-12-01 05:37:32 +00:00
char *db = taosHashIterate(pUser->readDbs, NULL);
while (db != NULL) {
cols = 0;
2022-12-15 08:18:48 +00:00
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
2022-12-01 05:37:32 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
2022-12-15 08:18:48 +00:00
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
2022-12-01 05:37:32 +00:00
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
2022-12-01 05:37:32 +00:00
SName name = {0};
char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
tNameGetDbName(&name, varDataVal(objName));
varDataSetLen(objName, strlen(varDataVal(objName)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
2022-12-01 05:37:32 +00:00
char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);
2023-09-04 13:15:46 +00:00
char *condition = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);
taosMemoryFree(condition);
2023-03-30 10:53:50 +00:00
2022-12-01 05:37:32 +00:00
numOfRows++;
db = taosHashIterate(pUser->readDbs, db);
}
db = taosHashIterate(pUser->writeDbs, NULL);
while (db != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
2022-12-01 05:37:32 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
2022-12-01 05:37:32 +00:00
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
2022-12-01 05:37:32 +00:00
SName name = {0};
char objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
tNameGetDbName(&name, varDataVal(objName));
varDataSetLen(objName, strlen(varDataVal(objName)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
2022-12-01 05:37:32 +00:00
char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);
2023-09-04 13:15:46 +00:00
char *condition = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);
taosMemoryFree(condition);
2023-03-30 10:53:50 +00:00
2022-12-01 05:37:32 +00:00
numOfRows++;
db = taosHashIterate(pUser->writeDbs, db);
}
2023-03-30 05:59:48 +00:00
mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pUser->user, pShow);
2023-03-29 05:30:32 +00:00
2023-03-30 05:59:48 +00:00
mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pUser->user, pShow);
2023-03-29 05:30:32 +00:00
2022-12-01 05:37:32 +00:00
char *topic = taosHashIterate(pUser->topics, NULL);
while (topic != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
2022-12-01 05:37:32 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
2022-12-01 05:37:32 +00:00
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
2022-12-01 05:37:32 +00:00
char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
varDataSetLen(topicName, strlen(varDataVal(topicName)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2023-02-20 02:04:08 +00:00
colDataSetVal(pColInfo, numOfRows, (const char *)topicName, false);
2022-12-01 05:37:32 +00:00
char tableName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(tableName, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)tableName, false);
2023-09-04 13:15:46 +00:00
char *condition = taosMemoryMalloc(TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE);
2023-03-30 10:53:50 +00:00
STR_WITH_MAXSIZE_TO_VARSTR(condition, "", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)condition, false);
taosMemoryFree(condition);
2023-03-30 10:53:50 +00:00
2022-12-01 05:37:32 +00:00
numOfRows++;
topic = taosHashIterate(pUser->topics, topic);
2022-12-01 05:37:32 +00:00
}
2022-12-01 03:45:31 +00:00
2022-12-01 05:37:32 +00:00
sdbRelease(pSdb, pUser);
}
pShow->numOfRows += numOfRows;
return numOfRows;
}
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetch(pSdb, pIter);
}
2022-12-01 03:45:31 +00:00
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
int32_t *pRspLen) {
2022-05-06 06:13:56 +00:00
SUserAuthBatchRsp batchRsp = {0};
batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
if (batchRsp.pArray == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
int32_t code = 0;
for (int32_t i = 0; i < numOfUses; ++i) {
SUserObj *pUser = mndAcquireUser(pMnode, pUsers[i].user);
if (pUser == NULL) {
mError("user:%s, failed to auth user since %s", pUsers[i].user, terrstr());
continue;
}
2022-05-20 07:37:39 +00:00
pUsers[i].version = ntohl(pUsers[i].version);
2022-05-06 06:13:56 +00:00
if (pUser->authVersion <= pUsers[i].version) {
mndReleaseUser(pMnode, pUser);
continue;
}
2022-05-06 06:13:56 +00:00
SGetUserAuthRsp rsp = {0};
code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
if (code) {
mndReleaseUser(pMnode, pUser);
tFreeSGetUserAuthRsp(&rsp);
goto _OVER;
}
taosArrayPush(batchRsp.pArray, &rsp);
mndReleaseUser(pMnode, pUser);
}
if (taosArrayGetSize(batchRsp.pArray) <= 0) {
*ppRsp = NULL;
*pRspLen = 0;
2022-05-06 06:13:56 +00:00
tFreeSUserAuthBatchRsp(&batchRsp);
return 0;
}
int32_t rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
void *pRsp = taosMemoryMalloc(rspLen);
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
tFreeSUserAuthBatchRsp(&batchRsp);
return -1;
}
tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
*ppRsp = pRsp;
*pRspLen = rspLen;
tFreeSUserAuthBatchRsp(&batchRsp);
return 0;
_OVER:
*ppRsp = NULL;
*pRspLen = 0;
2022-05-06 06:13:56 +00:00
tFreeSUserAuthBatchRsp(&batchRsp);
return code;
}
int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
int32_t code = 0;
SSdb *pSdb = pMnode->pSdb;
int32_t len = strlen(db) + 1;
void *pIter = NULL;
SUserObj *pUser = NULL;
SUserObj newUser = {0};
while (1) {
pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
if (pIter == NULL) break;
code = -1;
if (mndUserDupObj(pUser, &newUser) != 0) {
break;
}
bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
if (inRead || inWrite) {
(void)taosHashRemove(newUser.readDbs, db, len);
(void)taosHashRemove(newUser.writeDbs, db, len);
SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
break;
}
(void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
}
mndUserFreeObj(&newUser);
sdbRelease(pSdb, pUser);
code = 0;
}
if (pUser != NULL) sdbRelease(pSdb, pUser);
if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
mndUserFreeObj(&newUser);
return code;
}
int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
int32_t code = 0;
SSdb *pSdb = pMnode->pSdb;
int32_t len = strlen(topic) + 1;
void *pIter = NULL;
SUserObj *pUser = NULL;
SUserObj newUser = {0};
while (1) {
pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
2023-02-23 06:09:56 +00:00
if (pIter == NULL) {
break;
}
code = -1;
2023-02-23 06:09:56 +00:00
if (mndUserDupObj(pUser, &newUser) != 0) {
break;
}
bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
if (inTopic) {
(void)taosHashRemove(newUser.topics, topic, len);
SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
break;
}
(void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
}
mndUserFreeObj(&newUser);
sdbRelease(pSdb, pUser);
code = 0;
}
if (pUser != NULL) sdbRelease(pSdb, pUser);
if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
mndUserFreeObj(&newUser);
return code;
}