mnodeUser.c 7.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mnodeSync.h"
S
Shengliang Guan 已提交
18
#include "os.h"
S
Shengliang Guan 已提交
19
#include "tglobal.h"
S
Shengliang Guan 已提交
20
#include "tkey.h"
S
Shengliang Guan 已提交
21

S
Shengliang Guan 已提交
22
#define USER_VER 1
S
Shengliang Guan 已提交
23

S
Shengliang Guan 已提交
24 25
static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) {
  SSdbRaw *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRaw));
S
Shengliang Guan 已提交
26 27 28
  if (pRaw == NULL) {
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
    return NULL;
S
Shengliang Guan 已提交
29 30
  }

S
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
  int32_t dataLen = 0;
  char   *pData = pRaw->data;
  SDB_SET_BINARY_VAL(pData, dataLen, pUser->user, TSDB_USER_LEN)
  SDB_SET_BINARY_VAL(pData, dataLen, pUser->pass, TSDB_KEY_LEN)
  SDB_SET_BINARY_VAL(pData, dataLen, pUser->acct, TSDB_KEY_LEN)
  SDB_SET_INT64_VAL(pData, dataLen, pUser->createdTime)
  SDB_SET_INT64_VAL(pData, dataLen, pUser->updateTime)
  SDB_SET_INT32_VAL(pData, dataLen, pUser->rootAuth)

  pRaw->dataLen = dataLen;
  pRaw->type = SDB_USER;
  pRaw->sver = USER_VER;
  return pRaw;
S
Shengliang Guan 已提交
44 45
}

S
Shengliang Guan 已提交
46
static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
47 48 49 50
  if (pRaw->sver != USER_VER) {
    terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
    return NULL;
  }
S
Shengliang Guan 已提交
51

S
Shengliang Guan 已提交
52 53 54 55 56
  SUserObj *pUser = calloc(1, sizeof(SUserObj));
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
57

S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66
  int32_t code = 0;
  int32_t dataLen = pRaw->dataLen;
  char   *pData = pRaw->data;
  SDB_GET_BINARY_VAL(pData, dataLen, pUser->user, TSDB_USER_LEN, code)
  SDB_GET_BINARY_VAL(pData, dataLen, pUser->pass, TSDB_KEY_LEN, code)
  SDB_GET_BINARY_VAL(pData, dataLen, pUser->acct, TSDB_USER_LEN, code)
  SDB_GET_INT64_VAL(pData, dataLen, pUser->createdTime, code)
  SDB_GET_INT64_VAL(pData, dataLen, pUser->updateTime, code)
  SDB_GET_INT32_VAL(pData, dataLen, pUser->rootAuth, code)
S
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68 69 70 71 72
  if (code != 0) {
    tfree(pUser);
    terrno = code;
    return NULL;
  }
S
Shengliang Guan 已提交
73

S
Shengliang Guan 已提交
74 75
  return pUser;
}
S
Shengliang Guan 已提交
76

S
Shengliang Guan 已提交
77 78 79
static int32_t mnodeUserActionInsert(SUserObj *pUser) {
  pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->prohibitDbHash == NULL) {
S
Shengliang Guan 已提交
80 81
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
    return -1;
S
Shengliang Guan 已提交
82 83
  }

S
Shengliang Guan 已提交
84 85
  pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct);
  if (pUser->pAcct == NULL) {
S
Shengliang Guan 已提交
86 87
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
    return -1;
S
Shengliang Guan 已提交
88 89
  }

S
Shengliang Guan 已提交
90 91
  return 0;
}
S
Shengliang Guan 已提交
92

S
Shengliang Guan 已提交
93 94 95 96
static int32_t mnodeUserActionDelete(SUserObj *pUser) {
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
97 98
  }

S
Shengliang Guan 已提交
99
  if (pUser->acct != NULL) {
S
Shengliang Guan 已提交
100
    sdbRelease(pUser->pAcct);
S
Shengliang Guan 已提交
101
    pUser->pAcct = NULL;
S
Shengliang Guan 已提交
102 103
  }

S
Shengliang Guan 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117
  return 0;
}

static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) {
  memcpy(pDstUser, pSrcUser, (int32_t)((char *)&pDstUser->prohibitDbHash - (char *)&pDstUser));
  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();
S
Shengliang Guan 已提交
118
  userObj.updateTime = userObj.createdTime;
S
Shengliang Guan 已提交
119 120 121

  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
    userObj.rootAuth = 1;
S
Shengliang Guan 已提交
122 123
  }

S
Shengliang Guan 已提交
124 125 126
  SSdbRaw *pRaw = mnodeUserActionEncode(&userObj);
  if (pRaw == NULL) {
    return -1;
S
Shengliang Guan 已提交
127 128
  }

S
Shengliang Guan 已提交
129
  return sdbWrite(pRaw);
S
Shengliang Guan 已提交
130
}
S
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132
static int32_t mnodeCreateDefaultUsers() {
S
Shengliang Guan 已提交
133 134 135
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
136

S
Shengliang Guan 已提交
137 138 139
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
140

S
Shengliang Guan 已提交
141 142 143
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
144

S
Shengliang Guan 已提交
145
  return 0;
S
Shengliang Guan 已提交
146 147
}

S
Shengliang Guan 已提交
148 149 150 151 152 153 154 155 156
static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) {
  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;

S
Shengliang Guan 已提交
157 158 159 160 161 162 163
  STrans *pTrans = trnCreate(TRN_POLICY_ROLLBACK);
  if (pTrans == NULL) return -1;

  SSdbRaw *pRedoRaw = mnodeUserActionEncode(&userObj);
  if (pRedoRaw == NULL || trnAppendRedoLog(pTrans, pRedoRaw) != 0) {
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
164
  }
S
Shengliang Guan 已提交
165
  pRedoRaw->status = SDB_STATUS_CREATING;
S
Shengliang Guan 已提交
166 167
  pRedoRaw->action = SDB_ACTION_INSERT;

S
Shengliang Guan 已提交
168 169 170 171
  SSdbRaw *pUndoRaw = mnodeUserActionEncode(&userObj);
  if (pUndoRaw == NULL || trnAppendUndoLog(pTrans, pUndoRaw) != 0) {
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
172 173 174 175
  }
  pUndoRaw->status = SDB_STATUS_DROPPING;
  pUndoRaw->action = SDB_ACTION_DELETE;

S
Shengliang Guan 已提交
176 177 178 179
  SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj);
  if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) {
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
180
  }
S
Shengliang Guan 已提交
181 182
  pCommitRaw->status = SDB_STATUS_READY;
  pCommitRaw->action = SDB_ACTION_UPDATE;
S
Shengliang Guan 已提交
183

S
Shengliang Guan 已提交
184
  trnSetRpcHandle(pTrans, pMsg->rpcMsg.handle);
S
Shengliang Guan 已提交
185

S
Shengliang Guan 已提交
186
  if (trnPrepare(pTrans, mnodeSyncPropose) != 0) {
S
Shengliang Guan 已提交
187
    trnDrop(pTrans);
S
Shengliang Guan 已提交
188
    return -1;
S
Shengliang Guan 已提交
189 190
  }

S
Shengliang Guan 已提交
191 192
  trnDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
193 194 195 196 197 198
}

static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) {
  SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;

  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
199 200 201
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
202 203 204
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
205 206 207
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
208 209 210 211 212
  }

  SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user);
  if (pUser != NULL) {
    sdbRelease(pUser);
S
Shengliang Guan 已提交
213 214 215
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
216 217 218 219
  }

  SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->user);
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
220 221 222
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
223 224
  }

S
Shengliang Guan 已提交
225
  int32_t code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
S
Shengliang Guan 已提交
226 227 228
  sdbRelease(pOperUser);

  if (code != 0) {
S
Shengliang Guan 已提交
229 230
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
231 232 233 234 235
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
236
int32_t mnodeInitUser() {
S
Shengliang Guan 已提交
237 238 239 240 241 242 243 244 245 246
  SSdbDesc desc = {.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};
  sdbSetHandler(desc);

S
Shengliang Guan 已提交
247 248 249 250
  return 0;
}

void mnodeCleanupUser() {}