mndUser.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 "mndSync.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
#include "mndTrans.h"
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23
#define SDB_USER_VER 1
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) {
S
Shengliang Guan 已提交
26 27 28 29 30 31
  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)
S
Shengliang Guan 已提交
32
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
S
Shengliang Guan 已提交
33 34 35 36
  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);
S
Shengliang Guan 已提交
37 38

  return pRaw;
S
Shengliang Guan 已提交
39 40
}

S
Shengliang Guan 已提交
41 42 43
static SSdbRow *mnodeUserActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
44

S
Shengliang Guan 已提交
45 46
  if (sver != SDB_USER_VER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
47 48
    return NULL;
  }
S
Shengliang Guan 已提交
49

S
Shengliang Guan 已提交
50
  SSdbRow  *pRow = sdbAllocRow(sizeof(SUserObj));
S
Shengliang Guan 已提交
51 52
  SUserObj *pUser = sdbGetRowObj(pRow);
  if (pUser == NULL) return NULL;
S
Shengliang Guan 已提交
53

S
Shengliang Guan 已提交
54 55 56 57 58 59 60
  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)
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
  return pRow;
S
Shengliang Guan 已提交
63
}
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65 66 67
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 已提交
68
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
69
    return -1;
S
Shengliang Guan 已提交
70 71
  }

S
Shengliang Guan 已提交
72 73
  pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct);
  if (pUser->pAcct == NULL) {
S
Shengliang Guan 已提交
74 75
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
    return -1;
S
Shengliang Guan 已提交
76 77
  }

S
Shengliang Guan 已提交
78 79
  return 0;
}
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81 82 83 84
static int32_t mnodeUserActionDelete(SUserObj *pUser) {
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
85 86
  }

S
Shengliang Guan 已提交
87
  if (pUser->acct != NULL) {
S
Shengliang Guan 已提交
88
    sdbRelease(pUser->pAcct);
S
Shengliang Guan 已提交
89
    pUser->pAcct = NULL;
S
Shengliang Guan 已提交
90 91
  }

S
Shengliang Guan 已提交
92 93 94 95
  return 0;
}

static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) {
S
Shengliang Guan 已提交
96 97 98
  SUserObj tObj;
  int32_t  len = (int32_t)((int8_t *)tObj.prohibitDbHash - (int8_t *)&tObj);
  memcpy(pDstUser, pSrcUser, len);
S
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107
  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 已提交
108
  userObj.updateTime = userObj.createdTime;
S
Shengliang Guan 已提交
109 110 111

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

S
Shengliang Guan 已提交
114
  SSdbRaw *pRaw = mnodeUserActionEncode(&userObj);
S
Shengliang Guan 已提交
115
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
116
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
117

S
Shengliang Guan 已提交
118
  return sdbWrite(pRaw);
S
Shengliang Guan 已提交
119
}
S
Shengliang Guan 已提交
120

S
Shengliang Guan 已提交
121
static int32_t mnodeCreateDefaultUsers() {
S
Shengliang Guan 已提交
122 123 124
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126 127 128
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
129

S
Shengliang Guan 已提交
130 131 132
  if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
133

S
Shengliang Guan 已提交
134
  return 0;
S
Shengliang Guan 已提交
135 136
}

S
Shengliang Guan 已提交
137
static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
138 139 140 141 142 143 144 145
  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 已提交
146
  STrans *pTrans = trnCreate(TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
S
Shengliang Guan 已提交
147 148 149 150
  if (pTrans == NULL) return -1;

  SSdbRaw *pRedoRaw = mnodeUserActionEncode(&userObj);
  if (pRedoRaw == NULL || trnAppendRedoLog(pTrans, pRedoRaw) != 0) {
S
Shengliang Guan 已提交
151
    mError("failed to append redo log since %s", terrstr());
S
Shengliang Guan 已提交
152 153
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
154
  }
S
Shengliang Guan 已提交
155
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
S
Shengliang Guan 已提交
156

S
Shengliang Guan 已提交
157 158
  SSdbRaw *pUndoRaw = mnodeUserActionEncode(&userObj);
  if (pUndoRaw == NULL || trnAppendUndoLog(pTrans, pUndoRaw) != 0) {
S
Shengliang Guan 已提交
159
    mError("failed to append undo log since %s", terrstr());
S
Shengliang Guan 已提交
160 161
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
162
  }
S
Shengliang Guan 已提交
163
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
164

S
Shengliang Guan 已提交
165 166
  SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj);
  if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) {
S
Shengliang Guan 已提交
167
    mError("failed to append commit log since %s", terrstr());
S
Shengliang Guan 已提交
168 169
    trnDrop(pTrans);
    return -1;
S
Shengliang Guan 已提交
170
  }
S
Shengliang Guan 已提交
171
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
172

S
Shengliang Guan 已提交
173
  if (trnPrepare(pTrans, mnodeSyncPropose) != 0) {
S
Shengliang Guan 已提交
174
    trnDrop(pTrans);
S
Shengliang Guan 已提交
175
    return -1;
S
Shengliang Guan 已提交
176 177
  }

S
Shengliang Guan 已提交
178 179
  trnDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
180 181
}

S
Shengliang Guan 已提交
182
static int32_t mnodeProcessCreateUserMsg(SMnode *pMnode, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
183 184 185
  SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;

  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
186 187 188
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
189 190 191
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
192 193 194
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
195 196 197 198 199
  }

  SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user);
  if (pUser != NULL) {
    sdbRelease(pUser);
S
Shengliang Guan 已提交
200 201 202
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
203 204
  }

S
Shengliang Guan 已提交
205
  SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->conn.user);
S
Shengliang Guan 已提交
206
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
207 208 209
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
210 211
  }

S
Shengliang Guan 已提交
212
  int32_t code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
S
Shengliang Guan 已提交
213 214 215
  sdbRelease(pOperUser);

  if (code != 0) {
S
Shengliang Guan 已提交
216 217
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
218 219 220 221 222
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

223
int32_t mndInitUser() {
S
Shengliang Guan 已提交
224 225 226 227 228 229 230 231 232
  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);
S
Shengliang Guan 已提交
233

234
  mndSetMsgHandle(NULL, TSDB_MSG_TYPE_CREATE_USER, mnodeProcessCreateUserMsg);
S
Shengliang Guan 已提交
235

S
Shengliang Guan 已提交
236 237 238
  return 0;
}

239
void mndCleanupUser() {}