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

239 lines
7.2 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-11-11 03:11:14 +00:00
#include "mnodeSync.h"
2021-10-16 07:16:05 +00:00
#include "os.h"
2021-10-18 06:00:35 +00:00
#include "tglobal.h"
2021-11-11 03:11:14 +00:00
#include "tkey.h"
2021-11-19 06:46:06 +00:00
#include "mnodeTrans.h"
2021-10-17 03:42:05 +00:00
2021-11-12 07:06:58 +00:00
#define SDB_USER_VER 1
2021-10-18 06:00:35 +00:00
2021-11-11 03:11:14 +00:00
static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) {
2021-11-12 07:06:58 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SAcctObj));
if (pRaw == NULL) return NULL;
int32_t dataPos = 0;
SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN)
SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_KEY_LEN)
2021-11-12 13:35:29 +00:00
SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
2021-11-12 07:06:58 +00:00
SDB_SET_INT64(pRaw, dataPos, pUser->createdTime)
SDB_SET_INT64(pRaw, dataPos, pUser->updateTime)
SDB_SET_INT8(pRaw, dataPos, pUser->rootAuth)
SDB_SET_DATALEN(pRaw, dataPos);
2021-11-09 03:37:58 +00:00
return pRaw;
2021-10-18 06:00:35 +00:00
}
2021-11-12 07:06:58 +00:00
static SSdbRow *mnodeUserActionDecode(SSdbRaw *pRaw) {
int8_t sver = 0;
if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
if (sver != SDB_USER_VER) {
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
2021-11-09 03:37:58 +00:00
return NULL;
}
2021-10-18 06:00:35 +00:00
2021-11-12 13:35:29 +00:00
SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
2021-11-12 07:06:58 +00:00
SUserObj *pUser = sdbGetRowObj(pRow);
if (pUser == NULL) return NULL;
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->user, TSDB_USER_LEN)
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->pass, TSDB_KEY_LEN)
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->acct, TSDB_USER_LEN)
SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->createdTime)
SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->updateTime)
SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->rootAuth)
2021-10-18 06:00:35 +00:00
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-09 03:37:58 +00:00
static int32_t mnodeUserActionInsert(SUserObj *pUser) {
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-11-11 03:11:14 +00:00
return -1;
2021-10-18 06:00:35 +00:00
}
2021-11-09 03:37:58 +00:00
pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct);
if (pUser->pAcct == NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
return -1;
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-09 03:37:58 +00:00
static int32_t mnodeUserActionDelete(SUserObj *pUser) {
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
if (pUser->acct != NULL) {
2021-11-09 06:27:17 +00:00
sdbRelease(pUser->pAcct);
2021-11-09 03:37:58 +00:00
pUser->pAcct = NULL;
2021-10-18 06:00:35 +00:00
}
2021-11-09 03:37:58 +00:00
return 0;
}
static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) {
2021-11-12 13:35:29 +00:00
SUserObj tObj;
int32_t len = (int32_t)((int8_t *)tObj.prohibitDbHash - (int8_t *)&tObj);
memcpy(pDstUser, pSrcUser, len);
2021-11-09 03:37:58 +00:00
return 0;
}
static int32_t mnodeCreateDefaultUser(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();
2021-11-09 06:27:17 +00:00
userObj.updateTime = userObj.createdTime;
2021-11-09 03:37:58 +00:00
if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
userObj.rootAuth = 1;
2021-10-18 06:00:35 +00:00
}
2021-11-11 03:11:14 +00:00
SSdbRaw *pRaw = mnodeUserActionEncode(&userObj);
2021-11-12 07:06:58 +00:00
if (pRaw == NULL) return -1;
2021-11-12 11:11:35 +00:00
sdbSetRawStatus(pRaw, SDB_STATUS_READY);
2021-10-18 06:00:35 +00:00
2021-11-11 03:11:14 +00:00
return sdbWrite(pRaw);
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-11-09 03:37:58 +00:00
static int32_t mnodeCreateDefaultUsers() {
2021-11-11 03:11:14 +00:00
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
2021-11-09 03:37:58 +00:00
2021-11-11 03:11:14 +00:00
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) {
return -1;
}
2021-11-09 03:37:58 +00:00
2021-11-11 03:11:14 +00:00
if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) {
return -1;
}
2021-11-09 03:37:58 +00:00
2021-11-11 03:11:14 +00:00
return 0;
2021-10-18 06:00:35 +00:00
}
2021-11-11 09:31:52 +00:00
static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
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.rootAuth = 0;
2021-11-19 06:46:06 +00:00
STrans *pTrans = trnCreate(TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
2021-11-11 03:11:14 +00:00
if (pTrans == NULL) return -1;
SSdbRaw *pRedoRaw = mnodeUserActionEncode(&userObj);
if (pRedoRaw == NULL || trnAppendRedoLog(pTrans, pRedoRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append redo log since %s", terrstr());
2021-11-11 03:11:14 +00:00
trnDrop(pTrans);
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 07:06:58 +00:00
sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
2021-11-09 06:27:17 +00:00
2021-11-11 03:11:14 +00:00
SSdbRaw *pUndoRaw = mnodeUserActionEncode(&userObj);
if (pUndoRaw == NULL || trnAppendUndoLog(pTrans, pUndoRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append undo log since %s", terrstr());
2021-11-11 03:11:14 +00:00
trnDrop(pTrans);
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 11:11:35 +00:00
sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
2021-11-09 06:27:17 +00:00
2021-11-11 03:11:14 +00:00
SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj);
if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append commit log since %s", terrstr());
2021-11-11 03:11:14 +00:00
trnDrop(pTrans);
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 07:06:58 +00:00
sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
2021-11-09 06:27:17 +00:00
2021-11-11 03:11:14 +00:00
if (trnPrepare(pTrans, mnodeSyncPropose) != 0) {
2021-11-09 06:27:17 +00:00
trnDrop(pTrans);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-11 03:11:14 +00:00
trnDrop(pTrans);
return 0;
2021-11-09 06:27:17 +00:00
}
2021-11-27 14:46:12 +00:00
static int32_t mnodeProcessCreateUserMsg(SMnode *pMnode, SMnodeMsg *pMsg) {
2021-11-09 06:27:17 +00:00
SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;
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
}
SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user);
if (pUser != NULL) {
sdbRelease(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
}
2021-11-11 09:31:52 +00:00
SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->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;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-11 03:11:14 +00:00
int32_t code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
2021-11-09 06:27:17 +00:00
sdbRelease(pOperUser);
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;
}
2021-10-18 06:00:35 +00:00
int32_t mnodeInitUser() {
2021-11-11 11:02:38 +00:00
SSdbTable table = {.sdbType = SDB_USER,
.keyType = SDB_KEY_BINARY,
.deployFp = (SdbDeployFp)mnodeCreateDefaultUsers,
.encodeFp = (SdbEncodeFp)mnodeUserActionEncode,
.decodeFp = (SdbDecodeFp)mnodeUserActionDecode,
.insertFp = (SdbInsertFp)mnodeUserActionInsert,
.updateFp = (SdbUpdateFp)mnodeUserActionUpdate,
.deleteFp = (SdbDeleteFp)mnodeUserActionDelete};
sdbSetTable(table);
2021-11-09 03:37:58 +00:00
2021-11-27 14:46:12 +00:00
mnodeSetMsgHandle(NULL, TSDB_MSG_TYPE_CREATE_USER, mnodeProcessCreateUserMsg);
2021-11-19 06:46:06 +00:00
2021-10-18 06:00:35 +00:00
return 0;
}
void mnodeCleanupUser() {}