mndUser.c 18.1 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 18
#include "mndUser.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
19 20
#include "mndSync.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
21
#include "tkey.h"
S
Shengliang Guan 已提交
22

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

S
Shengliang Guan 已提交
25 26 27 28 29
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);
S
Shengliang Guan 已提交
30
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOldUser, SUserObj *pNewUser);
S
Shengliang Guan 已提交
31 32 33 34
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg);
static int32_t  mndProcessCreateUserMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessAlterUserMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropUserMsg(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
35 36 37
static int32_t  mndGetUserMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t  mndRetrieveUsers(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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};

  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_USER, mndProcessCreateUserMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_USER, mndProcessAlterUserMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_USER, mndProcessDropUserMsg);

S
Shengliang Guan 已提交
53 54 55
  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_USER, mndGetUserMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
S
Shengliang Guan 已提交
56 57 58 59 60 61 62 63 64 65 66 67
  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;
S
Shengliang Guan 已提交
68 69
  userObj.readAuth = 1;
  userObj.writeAuth = 1;
S
Shengliang Guan 已提交
70 71 72 73 74 75 76 77 78

  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
    userObj.superAuth = 1;
  }

  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

S
Shengliang Guan 已提交
79
  mDebug("user:%s, will be created while deploy sdb", userObj.user);
S
Shengliang Guan 已提交
80 81 82 83 84 85 86 87
  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;
  }

S
Shengliang Guan 已提交
88
#if 0
S
Shengliang Guan 已提交
89 90 91
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }
S
Shengliang Guan 已提交
92
#endif  
S
Shengliang Guan 已提交
93 94 95 96

  return 0;
}

S
Shengliang Guan 已提交
97
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
S
Shengliang Guan 已提交
98
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SUserObj));
S
Shengliang Guan 已提交
99 100 101 102 103
  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 已提交
104
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
S
Shengliang Guan 已提交
105 106
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime)
S
Shengliang Guan 已提交
107
  SDB_SET_INT8(pRaw, dataPos, pUser->superAuth)
S
Shengliang Guan 已提交
108 109
  SDB_SET_INT8(pRaw, dataPos, pUser->readAuth)
  SDB_SET_INT8(pRaw, dataPos, pUser->writeAuth)
S
Shengliang Guan 已提交
110
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
111 112

  return pRaw;
S
Shengliang Guan 已提交
113 114
}

S
Shengliang Guan 已提交
115
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
116 117
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
118

S
Shengliang Guan 已提交
119
  if (sver != SDB_USER_VER) {
S
Shengliang Guan 已提交
120
    mError("failed to decode user since %s", terrstr());
S
Shengliang Guan 已提交
121
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
122 123
    return NULL;
  }
S
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125
  SSdbRow  *pRow = sdbAllocRow(sizeof(SUserObj));
S
Shengliang Guan 已提交
126 127
  SUserObj *pUser = sdbGetRowObj(pRow);
  if (pUser == NULL) return NULL;
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130 131 132 133 134
  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)
S
Shengliang Guan 已提交
135
  SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->superAuth)
S
Shengliang Guan 已提交
136 137
  SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->readAuth)
  SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->writeAuth)
S
Shengliang Guan 已提交
138

S
Shengliang Guan 已提交
139
  return pRow;
S
Shengliang Guan 已提交
140
}
S
Shengliang Guan 已提交
141

S
Shengliang Guan 已提交
142
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
143
  mTrace("user:%s, perform insert action", pUser->user);
S
Shengliang Guan 已提交
144 145
  pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->prohibitDbHash == NULL) {
S
Shengliang Guan 已提交
146
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
147
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
148
    return -1;
S
Shengliang Guan 已提交
149 150
  }

S
Shengliang Guan 已提交
151 152
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
153
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
154
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
155
    return -1;
S
Shengliang Guan 已提交
156
  }
S
Shengliang Guan 已提交
157 158
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160 161
  return 0;
}
S
Shengliang Guan 已提交
162

S
Shengliang Guan 已提交
163
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
164
  mTrace("user:%s, perform delete action", pUser->user);
S
Shengliang Guan 已提交
165 166 167
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
168 169
  }

S
Shengliang Guan 已提交
170 171 172
  return 0;
}

S
Shengliang Guan 已提交
173 174 175 176 177 178 179 180 181 182
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOldUser, SUserObj *pNewUser) {
  mTrace("user:%s, perform update action", pOldUser->user);
  memcpy(pOldUser->user, pNewUser->user, TSDB_USER_LEN);
  memcpy(pOldUser->pass, pNewUser->pass, TSDB_KEY_LEN);
  memcpy(pOldUser->acct, pNewUser->acct, TSDB_USER_LEN);
  pOldUser->createdTime = pNewUser->createdTime;
  pOldUser->updateTime = pNewUser->updateTime;
  pOldUser->superAuth = pNewUser->superAuth;
  pOldUser->readAuth = pNewUser->readAuth;
  pOldUser->writeAuth = pNewUser->writeAuth;
S
Shengliang Guan 已提交
183 184 185
  return 0;
}

S
Shengliang Guan 已提交
186
SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) {
S
Shengliang Guan 已提交
187
  SSdb *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
188
  return sdbAcquire(pSdb, SDB_USER, userName);
S
Shengliang Guan 已提交
189
}
S
Shengliang Guan 已提交
190

S
Shengliang Guan 已提交
191 192 193
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
194 195
}

S
Shengliang Guan 已提交
196
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
197 198 199 200 201 202
  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;
S
Shengliang Guan 已提交
203
  userObj.superAuth = 0;
S
Shengliang Guan 已提交
204 205
  userObj.readAuth = 1;
  userObj.writeAuth = 1;
S
Shengliang Guan 已提交
206

S
Shengliang Guan 已提交
207
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
S
Shengliang Guan 已提交
208 209 210 211 212
  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);
S
Shengliang Guan 已提交
213

S
Shengliang Guan 已提交
214
  SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
215
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
S
Shengliang Guan 已提交
216
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
217
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
218
    return -1;
S
Shengliang Guan 已提交
219
  }
S
Shengliang Guan 已提交
220
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
S
Shengliang Guan 已提交
221

S
Shengliang Guan 已提交
222
  SSdbRaw *pUndoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
223
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
S
Shengliang Guan 已提交
224
    mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
225
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
226
    return -1;
S
Shengliang Guan 已提交
227
  }
S
Shengliang Guan 已提交
228
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
229

S
Shengliang Guan 已提交
230
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
231
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
S
Shengliang Guan 已提交
232
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
233
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
234
    return -1;
S
Shengliang Guan 已提交
235
  }
S
Shengliang Guan 已提交
236
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
237

S
Shengliang Guan 已提交
238 239
  if (mndTransPrepare(pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
240
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
241
    return -1;
S
Shengliang Guan 已提交
242 243
  }

S
Shengliang Guan 已提交
244
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
245
  return 0;
S
Shengliang Guan 已提交
246 247
}

S
Shengliang Guan 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int32_t mndUpdateUser(SMnode *pMnode, SUserObj *pOldUser, SUserObj *pNewUser, SMnodeMsg *pMsg) {
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
  if (pTrans == NULL) {
    mError("user:%s, failed to update since %s", pOldUser->user, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to update user:%s", pTrans->id, pOldUser->user);

  SSdbRaw *pRedoRaw = mndUserActionEncode(pNewUser);
  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);

  SSdbRaw *pUndoRaw = mndUserActionEncode(pOldUser);
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);

  if (mndTransPrepare(pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
282
static int32_t mndDropUser(SMnode *pMnode, SMnodeMsg *pMsg, SUserObj *pUser) {
S
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
  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_DROPPING);

  SSdbRaw *pUndoRaw = mndUserActionEncode(pUser);
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);

  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);

  if (mndTransPrepare(pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

324 325
static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg) {
  SMnode         *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
326 327
  SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;

S
Shengliang Guan 已提交
328 329
  mDebug("user:%s, start to create", pCreate->user);

S
Shengliang Guan 已提交
330
  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
331 332 333
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
334 335 336
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
337 338 339
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
340 341
  }

S
Shengliang Guan 已提交
342
  SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pCreate->user);
S
Shengliang Guan 已提交
343
  if (pUser != NULL) {
S
Shengliang Guan 已提交
344
    sdbRelease(pMnode->pSdb, pUser);
S
Shengliang Guan 已提交
345 346 347
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
348 349
  }

S
Shengliang Guan 已提交
350
  SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
S
Shengliang Guan 已提交
351
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
352 353 354
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
355 356
  }

S
Shengliang Guan 已提交
357 358
  int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
  sdbRelease(pMnode->pSdb, pOperUser);
S
Shengliang Guan 已提交
359 360

  if (code != 0) {
S
Shengliang Guan 已提交
361 362
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
363 364 365 366 367
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
368
static int32_t mndProcessAlterUserMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  SMnode        *pMnode = pMsg->pMnode;
  SAlterUserMsg *pAlter = pMsg->rpcMsg.pCont;

  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;
  }

  SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pAlter->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
    return -1;
  }

  SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
  if (pOperUser == NULL) {
    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);

  int32_t code = mndUpdateUser(pMnode, pUser, &newUser, pMsg);
  sdbRelease(pMnode->pSdb, pOperUser);

  if (code != 0) {
    mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
414 415
}

S
Shengliang Guan 已提交
416
static int32_t mndProcessDropUserMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  SMnode       *pMnode = pMsg->pMnode;
  SDropUserMsg *pDrop = pMsg->rpcMsg.pCont;

  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;
  }

  SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pDrop->user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
    return -1;
  }

  SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
442
  int32_t code = mndDropUser(pMnode, pMsg, pUser);
S
Shengliang Guan 已提交
443 444 445 446 447 448 449 450
  sdbRelease(pMnode->pSdb, pOperUser);

  if (code != 0) {
    mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
451 452 453 454 455 456 457
}

static int32_t mndGetUserMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
458
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

  pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "privilege");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
S
Shengliang Guan 已提交
474
  strcpy(pSchema[cols].name, "create time");
S
Shengliang Guan 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "account");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  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];
494
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
495

S
Shengliang Guan 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  return 0;
}

static int32_t mndRetrieveUsers(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode   *pMnode = pMsg->pMnode;
  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->superAuth) {
      const char *src = "super";
      STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
    } else {
S
Shengliang Guan 已提交
522
      const char *src = "normal";
S
Shengliang Guan 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
      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);
  }

  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
547
}