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

529 lines
17 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"
#include "mndShow.h"
2021-11-27 14:56:18 +00:00
#include "mndTrans.h"
2021-12-01 09:27:31 +00:00
#include "tkey.h"
2021-10-17 03:42:05 +00:00
2021-12-20 12:22:58 +00:00
#define TSDB_USER_VER_NUMBER 1
2021-12-15 02:48:49 +00:00
#define TSDB_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 SSdbRaw *mndUserActionEncode(SUserObj *pUser);
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-01-05 12:18:56 +00:00
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pReq);
static int32_t mndProcessCreateUserReq(SMnodeMsg *pReq);
static int32_t mndProcessAlterUserReq(SMnodeMsg *pReq);
static int32_t mndProcessDropUserReq(SMnodeMsg *pReq);
2022-01-10 12:44:11 +00:00
static int32_t mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
2022-01-05 12:18:56 +00:00
static int32_t mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
2021-12-07 12:14:22 +00:00
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter);
2021-12-03 12:52:44 +00:00
int32_t mndInitUser(SMnode *pMnode) {
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};
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);
2021-12-03 12:52:44 +00:00
2021-12-07 12:14:22 +00:00
mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_USER, mndGetUserMeta);
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
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};
tstrncpy(userObj.user, user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
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;
sdbSetRawStatus(pRaw, SDB_STATUS_READY);
2021-12-30 15:40:05 +00:00
mDebug("user:%s, will be created while deploy sdb, raw:%p", userObj.user, pRaw);
2021-12-03 12:52:44 +00:00
return sdbWrite(pMnode->pSdb, pRaw);
}
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
2021-12-13 10:19:09 +00:00
#if 0
2021-12-03 12:52:44 +00:00
if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
2021-12-14 07:16:41 +00:00
#endif
2021-12-03 12:52:44 +00:00
return 0;
}
2021-11-29 05:19:00 +00:00
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2021-12-20 12:22:58 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, TSDB_USER_VER_NUMBER, sizeof(SUserObj) + TSDB_USER_RESERVE_SIZE);
2021-12-31 06:22:50 +00:00
if (pRaw == NULL) goto USER_ENCODE_OVER;
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
2021-12-31 06:22:50 +00:00
SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, USER_ENCODE_OVER)
SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, USER_ENCODE_OVER)
SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, USER_ENCODE_OVER)
SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, USER_ENCODE_OVER)
SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, USER_ENCODE_OVER)
SDB_SET_INT8(pRaw, dataPos, pUser->superUser, USER_ENCODE_OVER)
SDB_SET_RESERVE(pRaw, dataPos, TSDB_USER_RESERVE_SIZE, USER_ENCODE_OVER)
SDB_SET_DATALEN(pRaw, dataPos, USER_ENCODE_OVER)
terrno = 0;
USER_ENCODE_OVER:
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;
2021-11-12 07:06:58 +00:00
int8_t sver = 0;
2021-12-31 06:22:50 +00:00
if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto USER_DECODE_OVER;
2021-10-18 06:00:35 +00:00
2021-12-20 12:22:58 +00:00
if (sver != TSDB_USER_VER_NUMBER) {
2021-11-12 07:06:58 +00:00
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
2021-12-31 06:22:50 +00:00
goto USER_DECODE_OVER;
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-12-31 06:22:50 +00:00
SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
if (pRow == NULL) goto USER_DECODE_OVER;
2021-11-12 07:06:58 +00:00
SUserObj *pUser = sdbGetRowObj(pRow);
2021-12-31 06:22:50 +00:00
if (pUser == NULL) goto USER_DECODE_OVER;
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
2021-12-31 06:22:50 +00:00
SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, USER_DECODE_OVER)
SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, USER_DECODE_OVER)
SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, USER_DECODE_OVER)
SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, USER_DECODE_OVER)
SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, USER_DECODE_OVER)
SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, USER_DECODE_OVER)
SDB_GET_RESERVE(pRaw, dataPos, TSDB_USER_RESERVE_SIZE, USER_DECODE_OVER)
terrno = 0;
USER_DECODE_OVER:
if (terrno != 0) {
mError("user:%s, failed to decode from raw:%p since %s", pUser->user, pRaw, terrstr());
tfree(pRow);
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);
2021-11-09 03:37:58 +00:00
pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pUser->prohibitDbHash == NULL) {
2021-11-11 09:31:52 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
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
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
2021-11-29 07:53:02 +00:00
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
2021-12-30 15:40:05 +00:00
mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
2021-11-09 03:37:58 +00:00
if (pUser->prohibitDbHash) {
taosHashCleanup(pUser->prohibitDbHash);
pUser->prohibitDbHash = NULL;
2021-10-18 06:00:35 +00:00
}
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-01-04 12:04:23 +00:00
memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
pOld->updateTime = pNew->updateTime;
2021-11-09 03:37:58 +00:00
return 0;
}
2021-12-04 02:23:39 +00:00
SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) {
2021-12-20 12:30:55 +00:00
SSdb *pSdb = pMnode->pSdb;
SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
if (pUser == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_EXIST;
}
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-01-05 12:18:56 +00:00
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pReq) {
2021-11-09 06:27:17 +00:00
SUserObj userObj = {0};
tstrncpy(userObj.user, user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
userObj.superUser = 0;
2021-11-09 06:27:17 +00:00
2022-01-05 12:18:56 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
2021-12-07 12:14:22 +00:00
if (pTrans == NULL) {
mError("user:%s, failed to create since %s", user, terrstr());
return -1;
}
mDebug("trans:%d, used to create user:%s", pTrans->id, user);
2021-11-11 03:11:14 +00:00
2021-11-29 05:19:00 +00:00
SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
2021-11-29 08:02:37 +00:00
if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
2021-12-07 12:14:22 +00:00
mError("trans:%d, failed to append 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
}
2021-12-07 13:10:36 +00:00
sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY);
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-01-05 12:18:56 +00:00
static int32_t mndProcessCreateUserReq(SMnodeMsg *pReq) {
SMnode *pMnode = pReq->pMnode;
SCreateUserReq *pCreate = pReq->rpcMsg.pCont;
2021-11-09 06:27:17 +00:00
2021-12-07 12:14:22 +00:00
mDebug("user:%s, start to create", pCreate->user);
2021-11-09 06:27:17 +00:00
if (pCreate->user[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
if (pCreate->pass[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2021-12-30 15:47:01 +00:00
SUserObj *pUser = mndAcquireUser(pMnode, pCreate->user);
2021-11-09 06:27:17 +00:00
if (pUser != NULL) {
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pUser);
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2022-01-05 12:18:56 +00:00
SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->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;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2022-01-05 12:18:56 +00:00
int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pReq);
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pOperUser);
2021-11-09 06:27:17 +00:00
if (code != 0) {
2021-11-11 03:11:14 +00:00
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
2022-01-05 12:18:56 +00:00
static int32_t mndUpdateUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SMnodeMsg *pReq) {
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
2021-12-14 11:29:37 +00:00
if (pTrans == NULL) {
2022-01-04 12:04:23 +00:00
mError("user:%s, failed to update since %s", pOld->user, terrstr());
2021-12-14 11:29:37 +00:00
return -1;
}
2022-01-04 12:04:23 +00:00
mDebug("trans:%d, used to update user:%s", pTrans->id, pOld->user);
2021-12-14 11:29:37 +00:00
2022-01-04 12:04:23 +00:00
SSdbRaw *pRedoRaw = mndUserActionEncode(pNew);
2021-12-14 11:29:37 +00:00
if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
sdbSetRawStatus(pRedoRaw, 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;
}
2022-01-05 12:18:56 +00:00
static int32_t mndProcessAlterUserReq(SMnodeMsg *pReq) {
SMnode *pMnode = pReq->pMnode;
SAlterUserReq *pAlter = pReq->rpcMsg.pCont;
2021-12-07 13:10:36 +00:00
mDebug("user:%s, start to alter", pAlter->user);
if (pAlter->user[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
return -1;
}
if (pAlter->pass[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
return -1;
}
2021-12-30 15:47:01 +00:00
SUserObj *pUser = mndAcquireUser(pMnode, pAlter->user);
2021-12-07 13:10:36 +00:00
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
return -1;
}
2022-01-05 12:18:56 +00:00
SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user);
2021-12-07 13:10:36 +00:00
if (pOperUser == NULL) {
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pUser);
2021-12-07 13:10:36 +00:00
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
return -1;
}
SUserObj newUser = {0};
memcpy(&newUser, pUser, sizeof(SUserObj));
memset(pUser->pass, 0, sizeof(pUser->pass));
taosEncryptPass((uint8_t *)pAlter->pass, strlen(pAlter->pass), pUser->pass);
2021-12-15 05:22:04 +00:00
newUser.updateTime = taosGetTimestampMs();
2021-12-07 13:10:36 +00:00
2022-01-05 12:18:56 +00:00
int32_t code = mndUpdateUser(pMnode, pUser, &newUser, pReq);
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pOperUser);
mndReleaseUser(pMnode, pUser);
2021-12-07 13:10:36 +00:00
if (code != 0) {
mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
return -1;
}
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
2021-12-01 09:27:31 +00:00
}
2022-01-05 12:18:56 +00:00
static int32_t mndDropUser(SMnode *pMnode, SMnodeMsg *pReq, SUserObj *pUser) {
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
2021-12-14 11:29:37 +00:00
if (pTrans == NULL) {
mError("user:%s, failed to drop since %s", pUser->user, terrstr());
return -1;
}
mDebug("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
SSdbRaw *pRedoRaw = mndUserActionEncode(pUser);
if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
mndTransDrop(pTrans);
return -1;
}
sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPED);
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-01-05 12:18:56 +00:00
static int32_t mndProcessDropUserReq(SMnodeMsg *pReq) {
SMnode *pMnode = pReq->pMnode;
SDropUserReq *pDrop = pReq->rpcMsg.pCont;
2021-12-07 13:10:36 +00:00
mDebug("user:%s, start to drop", pDrop->user);
if (pDrop->user[0] == 0) {
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
return -1;
}
2021-12-30 15:47:01 +00:00
SUserObj *pUser = mndAcquireUser(pMnode, pDrop->user);
2021-12-07 13:10:36 +00:00
if (pUser == NULL) {
terrno = TSDB_CODE_MND_USER_NOT_EXIST;
mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
return -1;
}
2022-01-05 12:18:56 +00:00
SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user);
2021-12-07 13:10:36 +00:00
if (pOperUser == NULL) {
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pUser);
2021-12-07 13:10:36 +00:00
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
return -1;
}
2022-01-05 12:18:56 +00:00
int32_t code = mndDropUser(pMnode, pReq, pUser);
2021-12-30 15:40:05 +00:00
mndReleaseUser(pMnode, pOperUser);
mndReleaseUser(pMnode, pUser);
2021-12-07 13:10:36 +00:00
if (code != 0) {
mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
return -1;
}
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
2021-12-07 12:14:22 +00:00
}
2022-01-10 12:44:11 +00:00
static int32_t mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
2022-01-05 12:18:56 +00:00
SMnode *pMnode = pReq->pMnode;
2021-12-07 12:14:22 +00:00
SSdb *pSdb = pMnode->pSdb;
int32_t cols = 0;
2021-12-10 06:12:11 +00:00
SSchema *pSchema = pMeta->pSchema;
2021-12-07 12:14:22 +00:00
pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
strcpy(pSchema[cols].name, "name");
2021-12-17 03:22:31 +00:00
pSchema[cols].bytes = htonl(pShow->bytes[cols]);
2021-12-07 12:14:22 +00:00
cols++;
pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
strcpy(pSchema[cols].name, "privilege");
2021-12-17 03:22:31 +00:00
pSchema[cols].bytes = htonl(pShow->bytes[cols]);
2021-12-07 12:14:22 +00:00
cols++;
pShow->bytes[cols] = 8;
pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
2021-12-17 06:20:32 +00:00
strcpy(pSchema[cols].name, "create_time");
2021-12-17 03:22:31 +00:00
pSchema[cols].bytes = htonl(pShow->bytes[cols]);
2021-12-07 12:14:22 +00:00
cols++;
pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
strcpy(pSchema[cols].name, "account");
2021-12-17 03:22:31 +00:00
pSchema[cols].bytes = htonl(pShow->bytes[cols]);
2021-12-07 12:14:22 +00:00
cols++;
pMeta->numOfColumns = htonl(cols);
2021-12-07 12:14:22 +00:00
pShow->numOfColumns = cols;
pShow->offset[0] = 0;
for (int32_t i = 1; i < cols; ++i) {
pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
}
pShow->numOfRows = sdbGetSize(pSdb, SDB_USER);
pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
2021-12-12 06:46:31 +00:00
strcpy(pMeta->tbFname, mndShowStr(pShow->type));
2021-12-08 10:50:52 +00:00
2021-12-07 12:14:22 +00:00
return 0;
}
2022-01-05 12:18:56 +00:00
static int32_t mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
SMnode *pMnode = pReq->pMnode;
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;
pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pUser->user, pShow->bytes[cols]);
cols++;
pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
if (pUser->superUser) {
2021-12-07 12:14:22 +00:00
const char *src = "super";
STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
} else {
2021-12-13 10:19:09 +00:00
const char *src = "normal";
2021-12-07 12:14:22 +00:00
STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
}
cols++;
pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
*(int64_t *)pWrite = pUser->createdTime;
cols++;
pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pUser->acct, pShow->bytes[cols]);
cols++;
numOfRows++;
sdbRelease(pSdb, pUser);
}
2021-12-15 07:35:18 +00:00
mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
2021-12-07 12:14:22 +00:00
pShow->numOfReads += numOfRows;
return numOfRows;
}
static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetch(pSdb, pIter);
2021-12-03 12:52:44 +00:00
}