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

S
Shengliang Guan 已提交
22
#define TSDB_USER_VER_NUMBER 1
S
Shengliang Guan 已提交
23
#define TSDB_USER_RESERVE_SIZE 64
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 *pOld, SUserObj *pNew);
S
Shengliang Guan 已提交
31 32 33 34 35 36
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);
static int32_t  mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t  mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
37
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48

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

S
Shengliang Guan 已提交
49 50 51
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
S
Shengliang Guan 已提交
52

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 68 69
  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) {
70
    userObj.superUser = 1;
S
Shengliang Guan 已提交
71 72 73 74 75 76
  }

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

S
Shengliang Guan 已提交
77
  mDebug("user:%s, will be created while deploy sdb, raw:%p", userObj.user, pRaw);
S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85
  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 已提交
86
#if 0
S
Shengliang Guan 已提交
87 88 89
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }
H
Haojun Liao 已提交
90
#endif
S
Shengliang Guan 已提交
91 92 93 94

  return 0;
}

S
Shengliang Guan 已提交
95
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
96 97
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
98
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, TSDB_USER_VER_NUMBER, sizeof(SUserObj) + TSDB_USER_RESERVE_SIZE);
99
  if (pRaw == NULL) goto USER_ENCODE_OVER;
S
Shengliang Guan 已提交
100 101

  int32_t dataPos = 0;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  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;
  }
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
121
  return pRaw;
S
Shengliang Guan 已提交
122 123
}

S
Shengliang Guan 已提交
124
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
125 126
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
127
  int8_t sver = 0;
128
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto USER_DECODE_OVER;
S
Shengliang Guan 已提交
129

S
Shengliang Guan 已提交
130
  if (sver != TSDB_USER_VER_NUMBER) {
S
Shengliang Guan 已提交
131
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
132
    goto USER_DECODE_OVER;
S
Shengliang Guan 已提交
133
  }
S
Shengliang Guan 已提交
134

135 136 137
  SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
  if (pRow == NULL) goto USER_DECODE_OVER;

S
Shengliang Guan 已提交
138
  SUserObj *pUser = sdbGetRowObj(pRow);
139
  if (pUser == NULL) goto USER_DECODE_OVER;
S
Shengliang Guan 已提交
140

S
Shengliang Guan 已提交
141
  int32_t dataPos = 0;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  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;
  }
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
160
  return pRow;
S
Shengliang Guan 已提交
161
}
S
Shengliang Guan 已提交
162

S
Shengliang Guan 已提交
163
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
164
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
S
Shengliang Guan 已提交
165 166
  pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->prohibitDbHash == NULL) {
S
Shengliang Guan 已提交
167
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
168
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
169
    return -1;
S
Shengliang Guan 已提交
170 171
  }

S
Shengliang Guan 已提交
172 173
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
174
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
175
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
176
    return -1;
S
Shengliang Guan 已提交
177
  }
S
Shengliang Guan 已提交
178 179
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
180

S
Shengliang Guan 已提交
181 182
  return 0;
}
S
Shengliang Guan 已提交
183

S
Shengliang Guan 已提交
184
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
185
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
S
Shengliang Guan 已提交
186 187 188
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
189 190
  }

S
Shengliang Guan 已提交
191 192 193
  return 0;
}

S
Shengliang Guan 已提交
194 195 196 197
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
  mTrace("user:%s, perform update action, old_row:%p new_row:%p", pOld->user, pOld, pNew);
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
  pOld->updateTime = pNew->updateTime;
S
Shengliang Guan 已提交
198 199 200
  return 0;
}

S
Shengliang Guan 已提交
201
SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) {
S
Shengliang Guan 已提交
202 203 204 205 206 207
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_EXIST;
  }
  return pUser;
S
Shengliang Guan 已提交
208
}
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210 211 212
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
213 214
}

S
Shengliang Guan 已提交
215
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
216 217 218 219 220 221
  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;
222
  userObj.superUser = 0;
S
Shengliang Guan 已提交
223

S
Shengliang Guan 已提交
224
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
S
Shengliang Guan 已提交
225 226 227 228 229
  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 已提交
230

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

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

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

S
Shengliang Guan 已提交
249 250 251
static int32_t mndProcessCreateUserReq(SMnodeMsg *pReq) {
  SMnode         *pMnode = pReq->pMnode;
  SCreateUserReq *pCreate = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
252

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

S
Shengliang Guan 已提交
255
  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
256 257 258
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
259 260 261
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
262 263 264
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
265 266
  }

S
Shengliang Guan 已提交
267
  SUserObj *pUser = mndAcquireUser(pMnode, pCreate->user);
S
Shengliang Guan 已提交
268
  if (pUser != NULL) {
S
Shengliang Guan 已提交
269
    mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
270 271 272
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
273 274
  }

S
Shengliang Guan 已提交
275
  SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user);
S
Shengliang Guan 已提交
276
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
277 278 279
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
280 281
  }

S
Shengliang Guan 已提交
282
  int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pReq);
S
Shengliang Guan 已提交
283
  mndReleaseUser(pMnode, pOperUser);
S
Shengliang Guan 已提交
284 285

  if (code != 0) {
S
Shengliang Guan 已提交
286 287
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
288 289 290 291 292
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
293 294
static int32_t mndUpdateUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SMnodeMsg *pReq) {
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
S
Shengliang Guan 已提交
295
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
296
    mError("user:%s, failed to update since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
297 298
    return -1;
  }
S
Shengliang Guan 已提交
299
  mDebug("trans:%d, used to update user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
300

S
Shengliang Guan 已提交
301
  SSdbRaw *pRedoRaw = mndUserActionEncode(pNew);
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  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;
}

S
Shengliang Guan 已提交
319 320 321
static int32_t mndProcessAlterUserReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  SAlterUserReq *pAlter = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

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

S
Shengliang Guan 已提交
337
  SUserObj *pUser = mndAcquireUser(pMnode, pAlter->user);
S
Shengliang Guan 已提交
338 339 340 341 342 343
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    mError("user:%s, failed to alter since %s", pAlter->user, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
344
  SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user);
S
Shengliang Guan 已提交
345
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
346
    mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355
    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);
S
Shengliang Guan 已提交
356
  newUser.updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
357

S
Shengliang Guan 已提交
358
  int32_t code = mndUpdateUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
359 360
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
361 362 363 364 365 366 367

  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 已提交
368 369
}

S
Shengliang Guan 已提交
370 371
static int32_t mndDropUser(SMnode *pMnode, SMnodeMsg *pReq, SUserObj *pUser) {
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
S
Shengliang Guan 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  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;
}

S
Shengliang Guan 已提交
396 397 398
static int32_t mndProcessDropUserReq(SMnodeMsg *pReq) {
  SMnode       *pMnode = pReq->pMnode;
  SDropUserReq *pDrop = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
399 400 401 402 403 404 405 406 407

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

S
Shengliang Guan 已提交
408
  SUserObj *pUser = mndAcquireUser(pMnode, pDrop->user);
S
Shengliang Guan 已提交
409 410 411 412 413 414
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
415
  SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user);
S
Shengliang Guan 已提交
416
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
417
    mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
418 419 420 421 422
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to drop since %s", pDrop->user, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
423
  int32_t code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
424 425
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
426 427 428 429 430 431 432

  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 已提交
433 434
}

S
Shengliang Guan 已提交
435 436
static int32_t mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
437 438 439
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
S
Shengliang Guan 已提交
440
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
441 442 443 444

  pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
H
Haojun Liao 已提交
445
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
446 447 448 449 450
  cols++;

  pShow->bytes[cols] = 10 + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "privilege");
H
Haojun Liao 已提交
451
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
452 453 454 455
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
456
  strcpy(pSchema[cols].name, "create_time");
H
Haojun Liao 已提交
457
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
458 459 460 461 462
  cols++;

  pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "account");
H
Haojun Liao 已提交
463
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
464 465
  cols++;

466
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
467 468 469 470 471 472 473 474 475
  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];
476
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
477

S
Shengliang Guan 已提交
478 479 480
  return 0;
}

S
Shengliang Guan 已提交
481 482
static int32_t mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode   *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  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;
500
    if (pUser->superUser) {
S
Shengliang Guan 已提交
501 502 503
      const char *src = "super";
      STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
    } else {
S
Shengliang Guan 已提交
504
      const char *src = "normal";
S
Shengliang Guan 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
      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);
  }

S
Shengliang Guan 已提交
521
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
522 523 524 525 526 527 528
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

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