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

1549 lines
51 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
2021-12-03 12:52:44 +00:00
#include "mndUser.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-04-12 05:09:52 +00:00
#define USER_VER_NUMBER 4
2022-04-26 09:42:57 +00:00
#define USER_RESERVE_SIZE 64
2021-10-18 06:00:35 +00:00
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 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);
2021-12-03 12:52:44 +00:00
int32_t mndInitUser(SMnode *pMnode) {
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);
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);
}
void mndCleanupUser(SMnode *pMnode) {}
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;
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);
if (pRaw == NULL) return -1;
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());
return -1;
}
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);
return -1;
}
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);
return -1;
}
mndTransDrop(pTrans);
return 0;
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;
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 +
(numOfReadDbs + numOfWriteDbs + numOfUseDbs) * TSDB_DB_FNAME_LEN + numOfTopics * TSDB_TOPIC_FNAME_LEN;
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
}
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
}
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);
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);
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);
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);
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
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());
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);
2021-11-11 03:11:14 +00:00
return -1;
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);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return 0;
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-07-19 07:04:39 +00:00
if (strlen(createReq.pass) >= TSDB_PASSWORD_LEN){
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
_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);
return code;
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;
}
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;
}
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-06-29 10:14:35 +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-07-18 03:27:01 +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-07-18 03:27:01 +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);
}
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
_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;
}
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
_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
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;
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 {
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;
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-07-31 15:27:35 +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);
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);
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);
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);
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;
}