mnodeUser.c 16.4 KB
Newer Older
H
hzcheng 已提交
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
slguan 已提交
16
#define _DEFAULT_SOURCE
17
#include "os.h"
S
slguan 已提交
18
#include "trpc.h"
19
#include "ttime.h"
S
slguan 已提交
20
#include "tutil.h"
S
slguan 已提交
21
#include "tglobal.h"
22
#include "tgrant.h"
H
hjxilinx 已提交
23
#include "tdataformat.h"
24
#include "mnode.h"
S
slguan 已提交
25
#include "dnode.h"
S
Shengliang Guan 已提交
26 27 28 29 30
#include "mnodeDef.h"
#include "mnodeInt.h"
#include "mnodeAcct.h"
#include "mnodeMnode.h"
#include "mnodeSdb.h"
31
#include "mnodeShow.h"
S
Shengliang Guan 已提交
32
#include "mnodeUser.h"
33 34
#include "mnodeWrite.h"
#include "mnodePeer.h"
S
slguan 已提交
35

36
static void *  tsUserSdb = NULL;
S
slguan 已提交
37
static int32_t tsUserUpdateSize = 0;
38 39 40 41 42 43 44 45
static int32_t mnodeGetUserMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
static int32_t mnodeRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn);
static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessAlterUserMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessDropUserMsg(SMnodeMsg *pMsg);
static int32_t mnodeProcessAuthMsg(SMnodeMsg *pMsg);

static int32_t mnodeUserActionDestroy(SSdbOper *pOper) {
S
slguan 已提交
46
  tfree(pOper->pObj);
S
slguan 已提交
47 48 49
  return TSDB_CODE_SUCCESS;
}

50
static int32_t mnodeUserActionInsert(SSdbOper *pOper) {
S
slguan 已提交
51
  SUserObj *pUser = pOper->pObj;
52
  SAcctObj *pAcct = mnodeGetAcct(pUser->acct);
53 54

  if (pAcct != NULL) {
55 56
    mnodeAddUserToAcct(pAcct, pUser);
    mnodeDecAcctRef(pAcct);
57
  } else {
58
    mError("user:%s, acct:%s info not exist in sdb", pUser->user, pUser->acct);
59
    return TSDB_CODE_MND_INVALID_ACCT;
60 61
  }

S
slguan 已提交
62 63 64
  return TSDB_CODE_SUCCESS;
}

65
static int32_t mnodeUserActionDelete(SSdbOper *pOper) {
S
slguan 已提交
66
  SUserObj *pUser = pOper->pObj;
67
  SAcctObj *pAcct = mnodeGetAcct(pUser->acct);
S
slguan 已提交
68

S
[TD-16]  
slguan 已提交
69
  if (pAcct != NULL) {
70 71
    mnodeDropUserFromAcct(pAcct, pUser);
    mnodeDecAcctRef(pAcct);
S
[TD-16]  
slguan 已提交
72
  }
S
slguan 已提交
73 74 75 76

  return TSDB_CODE_SUCCESS;
}

77
static int32_t mnodeUserActionUpdate(SSdbOper *pOper) {
S
slguan 已提交
78
  SUserObj *pUser = pOper->pObj;
79
  SUserObj *pSaved = mnodeGetUser(pUser->user);
S
slguan 已提交
80
  if (pUser != pSaved) {
S
slguan 已提交
81
    memcpy(pSaved, pUser, tsUserUpdateSize);
S
slguan 已提交
82 83
    free(pUser);
  }
84
  mnodeDecUserRef(pSaved);
S
slguan 已提交
85 86 87
  return TSDB_CODE_SUCCESS;
}

88
static int32_t mnodeUserActionEncode(SSdbOper *pOper) {
S
slguan 已提交
89
  SUserObj *pUser = pOper->pObj;
S
slguan 已提交
90 91 92
  memcpy(pOper->rowData, pUser, tsUserUpdateSize);
  pOper->rowSize = tsUserUpdateSize;
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
93 94
}

95
static int32_t mnodeUserActionDecode(SSdbOper *pOper) {
96
  SUserObj *pUser = (SUserObj *)calloc(1, sizeof(SUserObj));
97
  if (pUser == NULL) return TSDB_CODE_MND_OUT_OF_MEMORY;
S
slguan 已提交
98

S
slguan 已提交
99 100 101
  memcpy(pUser, pOper->rowData, tsUserUpdateSize);
  pOper->pObj = pUser;
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
102
}
H
hzcheng 已提交
103

104
static int32_t mnodeUserActionRestored() {
105 106
  int32_t numOfRows = sdbGetNumOfRows(tsUserSdb);
  if (numOfRows <= 0 && dnodeIsFirstDeploy()) {
107
    mInfo("dnode first deploy, create root user");
108 109
    SAcctObj *pAcct = mnodeGetAcct(TSDB_DEFAULT_USER);
    mnodeCreateUser(pAcct, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS, NULL);
110
    mnodeCreateUser(pAcct, "monitor", tsInternalPass, NULL);
111
    mnodeCreateUser(pAcct, "_"TSDB_DEFAULT_USER, tsInternalPass, NULL);
112
    mnodeDecAcctRef(pAcct);
S
slguan 已提交
113
  }
S
slguan 已提交
114

115
  return TSDB_CODE_SUCCESS;
S
slguan 已提交
116 117
}

118
int32_t mnodeInitUsers() {
S
slguan 已提交
119
  SUserObj tObj;
S
slguan 已提交
120
  tsUserUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj;
S
slguan 已提交
121

S
slguan 已提交
122
  SSdbTableDesc tableDesc = {
S
slguan 已提交
123
    .tableId      = SDB_TABLE_USER,
S
slguan 已提交
124
    .tableName    = "users",
S
Shengliang Guan 已提交
125
    .hashSessions = TSDB_DEFAULT_USERS_HASH_SIZE,
S
slguan 已提交
126
    .maxRowSize   = tsUserUpdateSize,
S
slguan 已提交
127
    .refCountPos  = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj,
S
slguan 已提交
128
    .keyType      = SDB_KEY_STRING,
129 130 131 132 133 134 135
    .insertFp     = mnodeUserActionInsert,
    .deleteFp     = mnodeUserActionDelete,
    .updateFp     = mnodeUserActionUpdate,
    .encodeFp     = mnodeUserActionEncode,
    .decodeFp     = mnodeUserActionDecode,
    .destroyFp    = mnodeUserActionDestroy,
    .restoredFp   = mnodeUserActionRestored
S
slguan 已提交
136 137 138
  };

  tsUserSdb = sdbOpenTable(&tableDesc);
139
  if (tsUserSdb == NULL) {
140
    mError("table:%s, failed to create hash", tableDesc.tableName);
H
hzcheng 已提交
141 142 143
    return -1;
  }

144 145 146 147 148 149
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_CREATE_USER, mnodeProcessCreateUserMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_ALTER_USER, mnodeProcessAlterUserMsg);
  mnodeAddWriteMsgHandle(TSDB_MSG_TYPE_CM_DROP_USER, mnodeProcessDropUserMsg);
  mnodeAddShowMetaHandle(TSDB_MGMT_TABLE_USER, mnodeGetUserMeta);
  mnodeAddShowRetrieveHandle(TSDB_MGMT_TABLE_USER, mnodeRetrieveUsers);
  mnodeAddPeerMsgHandle(TSDB_MSG_TYPE_DM_AUTH, mnodeProcessAuthMsg);
S
Shengliang Guan 已提交
150
   
151
  mDebug("table:%s, hash is created", tableDesc.tableName);
H
hzcheng 已提交
152 153 154
  return 0;
}

155
void mnodeCleanupUsers() {
S
slguan 已提交
156
  sdbCloseTable(tsUserSdb);
157
  tsUserSdb = NULL;
S
slguan 已提交
158 159
}

160
SUserObj *mnodeGetUser(char *name) {
161 162
  return (SUserObj *)sdbGetRow(tsUserSdb, name);
}
H
hzcheng 已提交
163

164
void *mnodeGetNextUser(void *pIter, SUserObj **pUser) { 
S
Shengliang Guan 已提交
165
  return sdbFetchRow(tsUserSdb, pIter, (void **)pUser); 
166 167
}

168
void mnodeIncUserRef(SUserObj *pUser) { 
S
slguan 已提交
169 170 171
  return sdbIncRef(tsUserSdb, pUser); 
}

172
void mnodeDecUserRef(SUserObj *pUser) { 
S
slguan 已提交
173 174 175
  return sdbDecRef(tsUserSdb, pUser); 
}

176
static int32_t mnodeUpdateUser(SUserObj *pUser, void *pMsg) {
S
slguan 已提交
177
  SSdbOper oper = {
178
    .type  = SDB_OPER_GLOBAL,
S
slguan 已提交
179
    .table = tsUserSdb,
180 181
    .pObj  = pUser,
    .pMsg  = pMsg
S
slguan 已提交
182 183 184
  };

  int32_t code = sdbUpdateRow(&oper);
185
  if (code == TSDB_CODE_SUCCESS) {
186
    mLInfo("user:%s, is altered by %s", pUser->user, mnodeGetUserFromMsg(pMsg));
187
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
188
  }
189 190

  return code;
191
}
H
hzcheng 已提交
192

193
int32_t mnodeCreateUser(SAcctObj *pAcct, char *name, char *pass, void *pMsg) {
S
slguan 已提交
194
  int32_t code = acctCheck(pAcct, ACCT_GRANT_USER);
195
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
196
    return code;
H
hzcheng 已提交
197 198
  }

199
  if (name[0] == 0) {
200
    return TSDB_CODE_MND_INVALID_USER_FORMAT;
201 202 203
  }

  if (pass[0] == 0) {
204
    return TSDB_CODE_MND_INVALID_PASS_FORMAT;
S
slguan 已提交
205 206
  }

207
  SUserObj *pUser = mnodeGetUser(name);
H
hzcheng 已提交
208
  if (pUser != NULL) {
209
    mDebug("user:%s, is already there", name);
210
    mnodeDecUserRef(pUser);
211
    return TSDB_CODE_MND_USER_ALREADY_EXIST;
H
hzcheng 已提交
212 213
  }

214 215
  code = grantCheck(TSDB_GRANT_USER);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
216 217 218
    return code;
  }

S
slguan 已提交
219
  pUser = calloc(1, sizeof(SUserObj));
S
Shengliang Guan 已提交
220
  tstrncpy(pUser->user, name, TSDB_USER_LEN);
S
slguan 已提交
221
  taosEncryptPass((uint8_t*) pass, strlen(pass), pUser->pass);
H
hzcheng 已提交
222 223 224 225
  strcpy(pUser->acct, pAcct->user);
  pUser->createdTime = taosGetTimestampMs();
  pUser->superAuth = 0;
  pUser->writeAuth = 1;
226
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0 || strcmp(pUser->user, pUser->acct) == 0) {
H
hzcheng 已提交
227 228 229
    pUser->superAuth = 1;
  }

S
slguan 已提交
230
  SSdbOper oper = {
231 232 233
    .type    = SDB_OPER_GLOBAL,
    .table   = tsUserSdb,
    .pObj    = pUser,
234
    .rowSize = sizeof(SUserObj),
235
    .pMsg    = pMsg
S
slguan 已提交
236 237 238 239
  };

  code = sdbInsertRow(&oper);
  if (code != TSDB_CODE_SUCCESS) {
H
hzcheng 已提交
240
    tfree(pUser);
241
  } else {
242
    mLInfo("user:%s, is created by %s", pUser->user, mnodeGetUserFromMsg(pMsg));
243
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
H
hzcheng 已提交
244
  }
245 246

  return code;
H
hzcheng 已提交
247 248
}

249
static int32_t mnodeDropUser(SUserObj *pUser, void *pMsg) {
S
slguan 已提交
250
  SSdbOper oper = {
251
    .type  = SDB_OPER_GLOBAL,
S
slguan 已提交
252
    .table = tsUserSdb,
253 254
    .pObj  = pUser,
    .pMsg  = pMsg
S
slguan 已提交
255
  };
H
hzcheng 已提交
256

S
slguan 已提交
257
  int32_t code = sdbDeleteRow(&oper);
258
  if (code == TSDB_CODE_SUCCESS) {
259
    mLInfo("user:%s, is dropped by %s", pUser->user, mnodeGetUserFromMsg(pMsg));
260
    if (pMsg != NULL) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
slguan 已提交
261
  }
262 263

  return code;
H
hzcheng 已提交
264 265
}

266 267
static int32_t mnodeGetUserMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
  SUserObj *pUser = mnodeGetUserFromConn(pConn);
S
slguan 已提交
268
  if (pUser == NULL) {
269
    return TSDB_CODE_MND_NO_USER_FROM_CONN;
S
slguan 已提交
270 271
  }

272
  int32_t cols = 0;
H
hjxilinx 已提交
273
  SSchema *pSchema = pMeta->schema;
S
slguan 已提交
274

H
hjxilinx 已提交
275
  pShow->bytes[cols] = TSDB_USER_LEN + VARSTR_HEADER_SIZE;
S
slguan 已提交
276 277 278 279 280
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

H
hjxilinx 已提交
281
  pShow->bytes[cols] = 8 + VARSTR_HEADER_SIZE;
S
slguan 已提交
282 283 284 285 286 287 288
  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;
H
hjxilinx 已提交
289
  strcpy(pSchema[cols].name, "create_time");
S
slguan 已提交
290 291 292
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

293 294 295 296 297 298
  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++;

S
slguan 已提交
299 300 301 302 303 304 305 306 307 308 309
  pMeta->numOfColumns = htons(cols);
  strcpy(pMeta->tableId, "show users");
  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 = pUser->pAcct->acctInfo.numOfUsers;
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
H
hzcheng 已提交
310

311
  mnodeDecUserRef(pUser);
H
hzcheng 已提交
312 313 314
  return 0;
}

315
static int32_t mnodeRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
316 317 318 319 320 321
  int32_t  numOfRows = 0;
  SUserObj *pUser    = NULL;
  int32_t  cols      = 0;
  char     *pWrite;

  while (numOfRows < rows) {
322
    pShow->pIter = mnodeGetNextUser(pShow->pIter, &pUser);
S
slguan 已提交
323
    if (pUser == NULL) break;
S
slguan 已提交
324
    
S
slguan 已提交
325 326 327
    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Hui Li 已提交
328
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pUser->user, pShow->bytes[cols]);
S
slguan 已提交
329 330 331 332
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    if (pUser->superAuth) {
sangshuduo's avatar
sangshuduo 已提交
333 334
      const char *src = "super";
      STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
S
slguan 已提交
335
    } else if (pUser->writeAuth) {
sangshuduo's avatar
sangshuduo 已提交
336 337
      const char *src = "writable";
      STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
S
slguan 已提交
338
    } else {
sangshuduo's avatar
sangshuduo 已提交
339 340
      const char *src = "readable";
      STR_WITH_SIZE_TO_VARSTR(pWrite, src, strlen(src));
S
slguan 已提交
341 342 343 344 345 346 347
    }
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pUser->createdTime;
    cols++;

348
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
H
Hui Li 已提交
349
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pUser->acct, pShow->bytes[cols]);
350 351
    cols++;

S
slguan 已提交
352
    numOfRows++;
353
    mnodeDecUserRef(pUser);
S
slguan 已提交
354
  }
355

S
slguan 已提交
356
  pShow->numOfReads += numOfRows;
H
hzcheng 已提交
357 358 359
  return numOfRows;
}

360
SUserObj *mnodeGetUserFromConn(void *pConn) {
361
  SRpcConnInfo connInfo = {0};
362
  if (rpcGetConnInfo(pConn, &connInfo) == 0) {
363
    return mnodeGetUser(connInfo.user);
364 365 366
  } else {
    mError("can not get user from conn:%p", pConn);
    return NULL;
367
  }
S
slguan 已提交
368
}
S
slguan 已提交
369

370 371
char *mnodeGetUserFromMsg(void *pMsg) {
  SMnodeMsg *pMnodeMsg = pMsg;
S
Shengliang Guan 已提交
372
  if (pMnodeMsg != NULL && pMnodeMsg->pUser != NULL) {
373 374 375 376 377 378
    return pMnodeMsg->pUser->user;
  } else {
    return "system";
  }
}

379
static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg) {
380
  SUserObj *pOperUser = pMsg->pUser;
381
  
382
  if (pOperUser->superAuth) {
S
Shengliang Guan 已提交
383
    SCMCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;
384
    return mnodeCreateUser(pOperUser->pAcct, pCreate->user, pCreate->pass, pMsg);
S
slguan 已提交
385
  } else {
S
slguan 已提交
386
    mError("user:%s, no rights to create user", pOperUser->user);
387
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
388 389 390
  }
}

391
static int32_t mnodeProcessAlterUserMsg(SMnodeMsg *pMsg) {
392 393 394
  int32_t code;
  SUserObj *pOperUser = pMsg->pUser;
  
S
Shengliang Guan 已提交
395
  SCMAlterUserMsg *pAlter = pMsg->rpcMsg.pCont;
396
  SUserObj *pUser = mnodeGetUser(pAlter->user);
S
slguan 已提交
397
  if (pUser == NULL) {
398
    return TSDB_CODE_MND_INVALID_USER;
S
slguan 已提交
399 400 401
  }

  if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) {
402
    mnodeDecUserRef(pUser);
403
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
404 405 406 407
  }

  if ((pAlter->flag & TSDB_ALTER_USER_PASSWD) != 0) {
    bool hasRight = false;
408
    if (strcmp(pOperUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
409 410 411 412
      hasRight = true;
    } else if (strcmp(pUser->user, pOperUser->user) == 0) {
      hasRight = true;
    } else if (pOperUser->superAuth) {
413
      if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
414 415 416 417 418 419 420 421 422 423 424
        hasRight = false;
      } else if (strcmp(pOperUser->acct, pUser->acct) != 0) {
        hasRight = false;
      } else {
        hasRight = true;
      }
    }

    if (hasRight) {
      memset(pUser->pass, 0, sizeof(pUser->pass));
      taosEncryptPass((uint8_t*)pAlter->pass, strlen(pAlter->pass), pUser->pass);
425
      code = mnodeUpdateUser(pUser, pMsg);
S
slguan 已提交
426
    } else {
C
chang 已提交
427
      mError("user:%s, no rights to alter user", pOperUser->user);
428
      code = TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
429
    }
S
slguan 已提交
430
  } else if ((pAlter->flag & TSDB_ALTER_USER_PRIVILEGES) != 0) {
S
slguan 已提交
431 432
    bool hasRight = false;

433
    if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
434 435 436
      hasRight = false;
    } else if (strcmp(pUser->user, pUser->acct) == 0) {
      hasRight = false;
437
    } else if (strcmp(pOperUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
438 439 440 441
      hasRight = true;
    } else if (strcmp(pUser->user, pOperUser->user) == 0) {
      hasRight = false;
    } else if (pOperUser->superAuth) {
442
      if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        hasRight = false;
      } else if (strcmp(pOperUser->acct, pUser->acct) != 0) {
        hasRight = false;
      } else {
        hasRight = true;
      }
    }

    if (pAlter->privilege == 1) { // super
      hasRight = false;
    }

    if (hasRight) {
      if (pAlter->privilege == 2) {  // read
        pUser->superAuth = 0;
        pUser->writeAuth = 0;
      }
      if (pAlter->privilege == 3) {  // write
        pUser->superAuth = 0;
        pUser->writeAuth = 1;
      }

465
      code = mnodeUpdateUser(pUser, pMsg);
S
slguan 已提交
466
    } else {
C
chang 已提交
467
      mError("user:%s, no rights to alter user", pOperUser->user);
468
      code = TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
469
    }
S
slguan 已提交
470
  } else {
C
chang 已提交
471
    mError("user:%s, no rights to alter user", pOperUser->user);
472
    code = TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
473 474
  }

475 476
  mnodeDecUserRef(pUser);
  return code;
S
slguan 已提交
477 478
}

479
static int32_t mnodeProcessDropUserMsg(SMnodeMsg *pMsg) {
480 481
  int32_t code;
  SUserObj *pOperUser = pMsg->pUser;
S
slguan 已提交
482

S
Shengliang Guan 已提交
483
  SCMDropUserMsg *pDrop = pMsg->rpcMsg.pCont;
484
  SUserObj *pUser = mnodeGetUser(pDrop->user);
S
slguan 已提交
485
  if (pUser == NULL) {
486
    return TSDB_CODE_MND_INVALID_USER;
S
slguan 已提交
487 488
  }

S
[TD-16]  
slguan 已提交
489 490
  if (strcmp(pUser->user, "monitor") == 0 || strcmp(pUser->user, pUser->acct) == 0 ||
    (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) {
491
    mnodeDecUserRef(pUser);
492
    return TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
493 494 495
  }

  bool hasRight = false;
496
  if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
497
    hasRight = false;
498
  } else if (strcmp(pOperUser->user, TSDB_DEFAULT_USER) == 0) {
S
slguan 已提交
499 500 501 502
    hasRight = true;
  } else if (strcmp(pUser->user, pOperUser->user) == 0) {
    hasRight = false;
  } else if (pOperUser->superAuth) {
S
slguan 已提交
503
    if (strcmp(pOperUser->acct, pUser->acct) != 0) {
S
slguan 已提交
504 505 506 507 508 509 510
      hasRight = false;
    } else {
      hasRight = true;
    }
  }

  if (hasRight) {
511
    code = mnodeDropUser(pUser, pMsg);
S
slguan 已提交
512
  } else {
513
    code = TSDB_CODE_MND_NO_RIGHTS;
S
slguan 已提交
514 515
  }

516 517
  mnodeDecUserRef(pUser);
  return code;
S
slguan 已提交
518
}
S
[TD-61]  
slguan 已提交
519

520
void mnodeDropAllUsers(SAcctObj *pAcct)  {
S
Shengliang Guan 已提交
521
  void *    pIter = NULL;
S
slguan 已提交
522 523
  int32_t   numOfUsers = 0;
  int32_t   acctNameLen = strlen(pAcct->user);
S
[TD-61]  
slguan 已提交
524 525 526
  SUserObj *pUser = NULL;

  while (1) {
527
    pIter = mnodeGetNextUser(pIter, &pUser);
S
[TD-61]  
slguan 已提交
528 529 530
    if (pUser == NULL) break;

    if (strncmp(pUser->acct, pAcct->user, acctNameLen) == 0) {
S
slguan 已提交
531
      SSdbOper oper = {
S
slguan 已提交
532
        .type = SDB_OPER_LOCAL,
S
[TD-61]  
slguan 已提交
533 534 535 536 537 538
        .table = tsUserSdb,
        .pObj = pUser,
      };
      sdbDeleteRow(&oper);
      numOfUsers++;
    }
S
slguan 已提交
539

540
    mnodeDecUserRef(pUser);
S
[TD-61]  
slguan 已提交
541 542
  }

S
Shengliang Guan 已提交
543 544
  sdbFreeIter(pIter);

545
  mDebug("acct:%s, all users:%d is dropped from sdb", pAcct->user, numOfUsers);
sangshuduo's avatar
sangshuduo 已提交
546
}
S
Shengliang Guan 已提交
547

548
int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
Shengliang Guan 已提交
549 550
  if (!sdbIsMaster()) {
    *secret = 0;
551
    mDebug("user:%s, failed to auth user, reason:%s", user, tstrerror(TSDB_CODE_RPC_NOT_READY));
552
    return TSDB_CODE_RPC_NOT_READY;
S
Shengliang Guan 已提交
553 554
  }

555
  SUserObj *pUser = mnodeGetUser(user);
S
Shengliang Guan 已提交
556 557
  if (pUser == NULL) {
    *secret = 0;
558 559
    mError("user:%s, failed to auth user, reason:%s", user, tstrerror(TSDB_CODE_MND_INVALID_USER));
    return TSDB_CODE_MND_INVALID_USER;
S
Shengliang Guan 已提交
560 561 562 563 564 565
  } else {
    *spi = 1;
    *encrypt = 0;
    *ckey = 0;

    memcpy(secret, pUser->pass, TSDB_KEY_LEN);
566
    mnodeDecUserRef(pUser);
567
    mDebug("user:%s, auth info is returned", user);
S
Shengliang Guan 已提交
568 569 570 571
    return TSDB_CODE_SUCCESS;
  }
}

572
static int32_t mnodeProcessAuthMsg(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
573
  SDMAuthMsg *pAuthMsg = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
574 575
  SDMAuthRsp *pAuthRsp = rpcMallocCont(sizeof(SDMAuthRsp));
  
576 577
  pMsg->rpcRsp.rsp = pAuthRsp;
  pMsg->rpcRsp.len = sizeof(SDMAuthRsp);
S
Shengliang Guan 已提交
578
  
579
  return mnodeRetriveAuth(pAuthMsg->user, &pAuthRsp->spi, &pAuthRsp->encrypt, pAuthRsp->secret, pAuthRsp->ckey);
S
Shengliang Guan 已提交
580
}