mgmtShell.c 36.8 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 19 20 21 22
#include "taosmsg.h"
#include "tlog.h"
#include "trpc.h"
#include "tstatus.h"
#include "tsched.h"
H
hzcheng 已提交
23
#include "dnodeSystem.h"
S
slguan 已提交
24
#include "mnode.h"
S
slguan 已提交
25 26
#include "mgmtAcct.h"
#include "mgmtBalance.h"
S
slguan 已提交
27
#include "mgmtChildTable.h"
S
slguan 已提交
28 29 30
#include "mgmtConn.h"
#include "mgmtDb.h"
#include "mgmtDnode.h"
S
#1177  
slguan 已提交
31
#include "mgmtGrant.h"
S
#1177  
slguan 已提交
32
#include "mgmtMnode.h"
S
slguan 已提交
33
#include "mgmtNormalTable.h"
H
hzcheng 已提交
34
#include "mgmtProfile.h"
S
#1177  
slguan 已提交
35
#include "mgmtShell.h"
S
slguan 已提交
36 37
#include "mgmtStreamTable.h"
#include "mgmtSuperTable.h"
S
slguan 已提交
38
#include "mgmtTable.h"
S
#1177  
slguan 已提交
39 40
#include "mgmtUser.h"
#include "mgmtVgroup.h"
H
hjliao 已提交
41

S
slguan 已提交
42

S
slguan 已提交
43
#define MAX_LEN_OF_METER_META (sizeof(SMultiMeterMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS + sizeof(SSchema) * TSDB_MAX_TAGS + TSDB_MAX_TAGS_LEN)
H
hzcheng 已提交
44

S
slguan 已提交
45 46
typedef int32_t (*GetMateFp)(SMeterMeta *pMeta, SShowObj *pShow, void *pConn);
typedef int32_t (*RetrieveMetaFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn);
S
slguan 已提交
47 48 49 50
static GetMateFp* mgmtGetMetaFp;
static RetrieveMetaFp* mgmtRetrieveFp;
static void mgmtInitShowMsgFp();

51
void *tsShellConnServer = NULL;
S
slguan 已提交
52

53
static void mgmtInitProcessShellMsg();
S
slguan 已提交
54 55
static void mgmtProcessMsgFromShell(char type, void *pCont, int contLen, void *ahandle, int32_t code);
static int32_t (*mgmtProcessShellMsg[TSDB_MSG_TYPE_MAX])(void *pCont, int32_t contLen, void *ahandle);
56
static int32_t mgmtProcessUnSupportMsg(void *pCont, int32_t contLen, void *ahandle);
S
slguan 已提交
57 58
static int32_t mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey);

S
slguan 已提交
59 60
void mgmtProcessTranRequest(SSchedMsg *sched) {
  int8_t  msgType = *(int8_t *) (sched->msg);
61
  int32_t contLen = *(int32_t *) (sched->msg + sizeof(int8_t));
S
slguan 已提交
62 63
  int8_t  *pCont  = sched->msg + sizeof(int32_t) + sizeof(int8_t);
  void    *pConn  = sched->thandle;
H
hzcheng 已提交
64

S
slguan 已提交
65 66 67 68 69
  (*mgmtProcessShellMsg[msgType])(pCont, contLen, pConn);
  if (sched->msg) {
    free(sched->msg);
  }
}
H
hzcheng 已提交
70

S
slguan 已提交
71 72
void mgmtAddToTranRequest(int8_t type, void *pCont, int contLen, void *ahandle) {
  SSchedMsg schedMsg;
73 74 75
  schedMsg.msg     = malloc(contLen + sizeof(int32_t) + sizeof(int8_t));
  schedMsg.fp      = mgmtProcessTranRequest;
  schedMsg.tfp     = NULL;
S
slguan 已提交
76 77 78
  schedMsg.thandle = ahandle;
  *(int8_t *) (schedMsg.msg) = type;
  *(int32_t *) (schedMsg.msg + sizeof(int8_t)) = contLen;
S
slguan 已提交
79
  memcpy(schedMsg.msg + sizeof(int32_t) + sizeof(int8_t), pCont, contLen);
S
slguan 已提交
80 81

  taosScheduleTask(tsMgmtTranQhandle, &schedMsg);
H
hzcheng 已提交
82 83
}

S
slguan 已提交
84
int32_t mgmtInitShell() {
H
hzcheng 已提交
85 86
  SRpcInit rpcInit;
  mgmtInitProcessShellMsg();
S
slguan 已提交
87
  mgmtInitShowMsgFp();
H
hzcheng 已提交
88

S
slguan 已提交
89
  int32_t numOfThreads = tsNumOfCores * tsNumOfThreadsPerCore / 4.0;
90 91 92
  if (numOfThreads < 1) {
    numOfThreads = 1;
  }
S
slguan 已提交
93

S
slguan 已提交
94
  memset(&rpcInit, 0, sizeof(rpcInit));
95 96 97
  rpcInit.localIp      = tsAnyIp ? "0.0.0.0" : tsPrivateIp;;
  rpcInit.localPort    = tsMgmtShellPort;
  rpcInit.label        = "MND-shell";
H
hzcheng 已提交
98
  rpcInit.numOfThreads = numOfThreads;
99 100 101 102 103 104 105 106
  rpcInit.cfp          = mgmtProcessMsgFromShell;
  rpcInit.sessions     = tsMaxShellConns;
  rpcInit.connType     = TAOS_CONN_SERVER;
  rpcInit.idleTime     = tsShellActivityTimer * 2000;
  rpcInit.afp          = mgmtRetriveUserAuthInfo;

  tsShellConnServer = rpcOpen(&rpcInit);
  if (tsShellConnServer == NULL) {
H
hzcheng 已提交
107 108 109 110 111 112 113 114
    mError("failed to init tcp connection to shell");
    return -1;
  }

  return 0;
}

void mgmtCleanUpShell() {
115 116 117
  if (tsShellConnServer) {
    rpcClose(tsShellConnServer);
    tsShellConnServer = NULL;
S
slguan 已提交
118
  }
H
hzcheng 已提交
119 120
}

S
slguan 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
int32_t mgmtProcessMeterMetaMsg(void *pCont, int32_t contLen, void *ahandle) {
  SRpcConnInfo connInfo;
  rpcGetConnInfo(ahandle, &connInfo);

  bool usePublicIp = (connInfo.serverIp == tsPublicIpInt);
  SUserObj *pUser = mgmtGetUser(connInfo.user);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  STableInfoMsg *pInfo = pCont;
  pInfo->createFlag = htons(pInfo->createFlag);

  SDbObj *pDb = mgmtGetDb(pInfo->db);
  if (pDb == NULL || pDb->dropStatus != TSDB_DB_STATUS_READY) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_DB, NULL, 0);
    return TSDB_CODE_INVALID_DB;
H
hzcheng 已提交
139 140
  }

S
slguan 已提交
141
  STableInfo *pTable = mgmtGetTable(pInfo->tableId);
H
hzcheng 已提交
142

S
slguan 已提交
143 144 145 146 147 148
  // on demand create table from super table if meter does not exists
  if (pTable == NULL && pInfo->createFlag == 1) {
    // write operation needs to redirect to master mnode
    if (mgmtCheckRedirectMsg(ahandle) != 0) {
      return TSDB_CODE_REDIRECT;
    }
H
hzcheng 已提交
149

S
slguan 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    SCreateTableMsg *pCreateMsg = calloc(1, sizeof(SCreateTableMsg) + sizeof(STagData));
    if (pCreateMsg == NULL) {
      rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0);
      return TSDB_CODE_SERV_OUT_OF_MEMORY;
    }

    memcpy(pCreateMsg->schema, pInfo->tags, sizeof(STagData));
    strcpy(pCreateMsg->tableId, pInfo->tableId);

    int32_t code = mgmtCreateTable(pDb, pCreateMsg);

    char stableName[TSDB_TABLE_ID_LEN] = {0};
    strncpy(stableName, pInfo->tags, TSDB_TABLE_ID_LEN);
    mTrace("table:%s is auto created by %s from %s, code:%d", pCreateMsg->tableId, pUser->user, stableName, code);

    tfree(pCreateMsg);

    if (code != TSDB_CODE_SUCCESS) {
      rpcSendResponse(ahandle, code, NULL, 0);
      return code;
    }

    pTable = mgmtGetTable(pInfo->tableId);
H
hzcheng 已提交
173 174
  }

S
slguan 已提交
175 176 177 178
  if (pTable == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_TABLE, NULL, 0);
    return TSDB_CODE_INVALID_TABLE;
  }
H
hzcheng 已提交
179

S
slguan 已提交
180 181 182 183 184 185 186 187 188 189 190 191
  SMeterMeta *pMeta = rpcMallocCont(sizeof(SMeterMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS);
  int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp);

  if (code == TSDB_CODE_SUCCESS) {
    rpcFreeCont(pMeta);
    rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, NULL, 0);
  } else {
    pMeta->contLen = htons(pMeta->contLen);
    rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, pMeta, pMeta->contLen);
  }

  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
192 193
}

S
slguan 已提交
194
int32_t mgmtProcessMultiMeterMetaMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
195 196
  SRpcConnInfo connInfo;
  rpcGetConnInfo(ahandle, &connInfo);
S
slguan 已提交
197

S
slguan 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  bool usePublicIp = (connInfo.serverIp == tsPublicIpInt);
  SUserObj *pUser = mgmtGetUser(connInfo.user);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  SMultiTableInfoMsg *pInfo = pCont;
  pInfo->numOfTables = htonl(pInfo->numOfTables);

  int32_t totalMallocLen = 4*1024*1024; // first malloc 4 MB, subsequent reallocation as twice
  SMultiMeterMeta *pMultiMeta = rpcMallocCont(totalMallocLen);
  if (pMultiMeta == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0);
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
  }

  pMultiMeta->contLen = sizeof(SMultiMeterMeta);
  pMultiMeta->numOfTables = 0;

  for (int t = 0; t < pInfo->numOfTables; ++t) {
    char *tableId = (char*)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN);
    STableInfo *pTable = mgmtGetTable(tableId);
    if (pTable == NULL) continue;

    SDbObj *pDb = mgmtGetDbByTableId(tableId);
    if (pDb == NULL) continue;

    int availLen = totalMallocLen - pMultiMeta->contLen;
    if (availLen <= sizeof(SMeterMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS) {
      //TODO realloc
      //totalMallocLen *= 2;
      //pMultiMeta = rpcReMalloc(pMultiMeta, totalMallocLen);
      //if (pMultiMeta == NULL) {
      ///  rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0);
      //  return TSDB_CODE_SERV_OUT_OF_MEMORY;
      //} else {
      //  t--;
      //  continue;
      //}
    }

    SMeterMeta *pMeta = (SMeterMeta *)(pMultiMeta->metas + pMultiMeta->contLen);
    int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp);
    if (code == TSDB_CODE_SUCCESS) {
      pMultiMeta->numOfTables ++;
      pMultiMeta->contLen += pMeta->contLen;
    }
  }

  rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, pMultiMeta, pMultiMeta->contLen);
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
250 251
}

S
slguan 已提交
252
int32_t mgmtProcessCreateDbMsg(void *pCont, int32_t contLen, void *ahandle) {
253 254 255
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
256

257 258 259 260 261
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }
H
hzcheng 已提交
262

S
slguan 已提交
263
  SCreateDbMsg *pCreate = (SCreateDbMsg *) pCont;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

  pCreate->maxSessions     = htonl(pCreate->maxSessions);
  pCreate->cacheBlockSize  = htonl(pCreate->cacheBlockSize);
  pCreate->daysPerFile     = htonl(pCreate->daysPerFile);
  pCreate->daysToKeep      = htonl(pCreate->daysToKeep);
  pCreate->daysToKeep1     = htonl(pCreate->daysToKeep1);
  pCreate->daysToKeep2     = htonl(pCreate->daysToKeep2);
  pCreate->commitTime      = htonl(pCreate->commitTime);
  pCreate->blocksPerMeter  = htons(pCreate->blocksPerMeter);
  pCreate->rowsInFileBlock = htonl(pCreate->rowsInFileBlock);
  // pCreate->cacheNumOfBlocks = htonl(pCreate->cacheNumOfBlocks);

  int32_t code;
  if (mgmtCheckExpired()) {
    code = TSDB_CODE_GRANT_EXPIRED;
  } else if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtCreateDb(pUser->pAcct, pCreate);
    if (code == TSDB_CODE_SUCCESS) {
      mLPrint("DB:%s is created by %s", pCreate->db, pUser->user);
    }
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
S
slguan 已提交
290 291
}

S
slguan 已提交
292
int32_t mgmtProcessAlterDbMsg(void *pCont, int32_t contLen, void *ahandle) {
293 294 295
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
296

297 298 299 300 301 302
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
303
  SAlterDbMsg *pAlter = (SAlterDbMsg *) pCont;
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  pAlter->daysPerFile = htonl(pAlter->daysPerFile);
  pAlter->daysToKeep  = htonl(pAlter->daysToKeep);
  pAlter->maxSessions = htonl(pAlter->maxSessions) + 1;

  int32_t code;
  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtAlterDb(pUser->pAcct, pAlter);
    if (code == TSDB_CODE_SUCCESS) {
      mLPrint("DB:%s is altered by %s", pAlter->db, pUser->user);
    }
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
320 321
}

S
slguan 已提交
322
int32_t mgmtProcessKillQueryMsg(void *pCont, int32_t contLen, void *ahandle) {
323 324 325
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
326

327 328 329 330 331 332
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
333
  SKillQueryMsg *pKill = (SKillQueryMsg *) pCont;
334 335 336 337 338 339 340 341 342 343
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtKillQuery(pKill->queryId, ahandle);
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
344 345
}

S
slguan 已提交
346
int32_t mgmtProcessKillStreamMsg(void *pCont, int32_t contLen, void *ahandle) {
347 348 349
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
350

351 352 353 354 355 356
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
357
  SKillStreamMsg *pKill = (SKillStreamMsg *) pCont;
358 359 360 361 362 363 364 365 366 367
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtKillStream(pKill->queryId, ahandle);
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
368 369
}

S
slguan 已提交
370
int32_t mgmtProcessKillConnectionMsg(void *pCont, int32_t contLen, void *ahandle) {
371 372 373
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  SKillConnectionMsg *pKill = (SKillConnectionMsg *) pCont;
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtKillConnection(pKill->queryId, ahandle);
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
392 393
}

S
slguan 已提交
394
int32_t mgmtProcessCreateUserMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
395 396 397
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
398

S
slguan 已提交
399 400 401 402 403 404 405 406
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  int32_t code;
  if (pUser->superAuth) {
S
slguan 已提交
407
    SCreateUserMsg *pCreate = pCont;
S
slguan 已提交
408 409 410 411 412 413 414 415 416 417
    code = mgmtCreateUser(pUser->pAcct, pCreate->user, pCreate->pass);
    if (code == TSDB_CODE_SUCCESS) {
      mLPrint("user:%s is created by %s", pCreate->user, pUser->user);
    }
  } else {
    code = TSDB_CODE_NO_RIGHTS;
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
418 419
}

S
slguan 已提交
420
int32_t mgmtProcessAlterUserMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
421 422 423 424 425 426 427 428 429 430
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }

  SUserObj *pOperUser = mgmtGetUserFromConn(ahandle);
  if (pOperUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
431
  SAlterUserMsg *pAlter = pCont;
S
slguan 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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 522 523 524
  SUserObj *pUser = mgmtGetUser(pAlter->user);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) {
    rpcSendResponse(ahandle, TSDB_CODE_NO_RIGHTS, NULL, 0);
    return TSDB_CODE_NO_RIGHTS;
  }

  int code;
  if ((pAlter->flag & TSDB_ALTER_USER_PASSWD) != 0) {
    bool hasRight = false;
    if (strcmp(pOperUser->user, "root") == 0) {
      hasRight = true;
    } else if (strcmp(pUser->user, pOperUser->user) == 0) {
      hasRight = true;
    } else if (pOperUser->superAuth) {
      if (strcmp(pUser->user, "root") == 0) {
        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);
      code = mgmtUpdateUser(pUser);
      mLPrint("user:%s password is altered by %s, code:%d", pAlter->user, pUser->user, code);
    } else {
      code = TSDB_CODE_NO_RIGHTS;
    }

    rpcSendResponse(ahandle, code, NULL, 0);
    return code;
  }

  if ((pAlter->flag & TSDB_ALTER_USER_PRIVILEGES) != 0) {
    bool hasRight = false;

    if (strcmp(pUser->user, "root") == 0) {
      hasRight = false;
    } else if (strcmp(pUser->user, pUser->acct) == 0) {
      hasRight = false;
    } else if (strcmp(pOperUser->user, "root") == 0) {
      hasRight = true;
    } else if (strcmp(pUser->user, pOperUser->user) == 0) {
      hasRight = false;
    } else if (pOperUser->superAuth) {
      if (strcmp(pUser->user, "root") == 0) {
        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 == 1) {  // super
      //  pUser->superAuth = 1;
      //  pUser->writeAuth = 1;
      //}
      if (pAlter->privilege == 2) {  // read
        pUser->superAuth = 0;
        pUser->writeAuth = 0;
      }
      if (pAlter->privilege == 3) {  // write
        pUser->superAuth = 0;
        pUser->writeAuth = 1;
      }

      code = mgmtUpdateUser(pUser);
      mLPrint("user:%s privilege is altered by %s, code:%d", pAlter->user, pUser->user, code);
    } else {
      code = TSDB_CODE_NO_RIGHTS;
    }

    rpcSendResponse(ahandle, code, NULL, 0);
    return code;
  }

  code = TSDB_CODE_NO_RIGHTS;
  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
525 526
}

S
slguan 已提交
527
int32_t mgmtProcessDropUserMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
528 529 530 531 532 533 534 535 536 537
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }

  SUserObj *pOperUser = mgmtGetUserFromConn(ahandle);
  if (pOperUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
538
  SDropUserMsg *pDrop = pCont;
S
slguan 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  SUserObj *pUser = mgmtGetUser(pDrop->user);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) {
    rpcSendResponse(ahandle, TSDB_CODE_NO_RIGHTS, NULL, 0);
    return TSDB_CODE_NO_RIGHTS;
  }

  bool hasRight = false;
  if (strcmp(pUser->user, "root") == 0) {
    hasRight = false;
  } else if (strcmp(pOperUser->user, "root") == 0) {
    hasRight = true;
  } else if (strcmp(pUser->user, pOperUser->user) == 0) {
    hasRight = false;
  } else if (pOperUser->superAuth) {
    if (strcmp(pUser->user, "root") == 0) {
      hasRight = false;
    } else if (strcmp(pOperUser->acct, pUser->acct) != 0) {
      hasRight = false;
    } else {
      hasRight = true;
    }
  }

  int32_t code;
  if (hasRight) {
    code = mgmtDropUser(pUser->pAcct, pDrop->user);
    if (code == TSDB_CODE_SUCCESS) {
      mLPrint("user:%s is dropped by %s", pDrop->user, pUser->user);
    }
  } else {
    code = TSDB_CODE_NO_RIGHTS;
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
579 580
}

S
slguan 已提交
581
int32_t mgmtProcessDropDbMsg(void *pCont, int32_t contLen, void *ahandle) {
582 583 584
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
585

586 587 588 589 590
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }
H
hzcheng 已提交
591

592 593
  int32_t code;
  if (pUser->superAuth) {
S
slguan 已提交
594
    SDropDbMsg *pDrop = pCont;
595 596 597 598 599 600 601
    code = mgmtDropDbByName(pUser->pAcct, pDrop->db, pDrop->ignoreNotExists);
    if (code == TSDB_CODE_SUCCESS) {
      mLPrint("DB:%s is dropped by %s", pDrop->db, pUser->user);
    }
  } else {
    code = TSDB_CODE_NO_RIGHTS;
  }
H
hzcheng 已提交
602

603 604
  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
605 606
}

S
slguan 已提交
607 608
static void mgmtInitShowMsgFp() {
  mgmtGetMetaFp = (GetMateFp *)malloc(TSDB_MGMT_TABLE_MAX * sizeof(GetMateFp));
S
slguan 已提交
609 610 611 612 613 614 615 616 617
  mgmtGetMetaFp[TSDB_MGMT_TABLE_ACCT]    = mgmtGetAcctMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_USER]    = mgmtGetUserMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_DB]      = mgmtGetDbMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_TABLE]   = mgmtGetShowTableMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_DNODE]   = mgmtGetDnodeMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_MNODE]   = mgmtGetMnodeMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_VGROUP]  = mgmtGetVgroupMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_METRIC]  = mgmtGetShowSuperTableMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_MODULE]  = mgmtGetModuleMeta;
S
slguan 已提交
618 619 620
  mgmtGetMetaFp[TSDB_MGMT_TABLE_QUERIES] = mgmtGetQueryMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_STREAMS] = mgmtGetStreamMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtGetConfigMeta;
S
slguan 已提交
621 622 623 624
  mgmtGetMetaFp[TSDB_MGMT_TABLE_CONNS]   = mgmtGetConnsMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_SCORES]  = mgmtGetScoresMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_GRANTS]  = mgmtGetGrantsMeta;
  mgmtGetMetaFp[TSDB_MGMT_TABLE_VNODES]  = mgmtGetVnodeMeta;
S
slguan 已提交
625 626

  mgmtRetrieveFp = (RetrieveMetaFp *)malloc(TSDB_MGMT_TABLE_MAX * sizeof(RetrieveMetaFp));
S
slguan 已提交
627 628 629 630 631 632 633 634 635
  mgmtRetrieveFp[TSDB_MGMT_TABLE_ACCT]    = mgmtRetrieveAccts;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_USER]    = mgmtRetrieveUsers;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_DB]      = mgmtRetrieveDbs;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_TABLE]   = mgmtRetrieveShowTables;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_DNODE]   = mgmtRetrieveDnodes;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_MNODE]   = mgmtRetrieveMnodes;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_VGROUP]  = mgmtRetrieveVgroups;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_METRIC]  = mgmtRetrieveShowSuperTables;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_MODULE]  = mgmtRetrieveModules;
S
slguan 已提交
636 637 638
  mgmtRetrieveFp[TSDB_MGMT_TABLE_QUERIES] = mgmtRetrieveQueries;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_STREAMS] = mgmtRetrieveStreams;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtRetrieveConfigs;
S
slguan 已提交
639 640 641 642
  mgmtRetrieveFp[TSDB_MGMT_TABLE_CONNS]   = mgmtRetrieveConns;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_SCORES]  = mgmtRetrieveScores;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_GRANTS]  = mgmtRetrieveGrants;
  mgmtRetrieveFp[TSDB_MGMT_TABLE_VNODES]  = mgmtRetrieveVnodes;
S
slguan 已提交
643
}
H
hzcheng 已提交
644

S
slguan 已提交
645
int32_t mgmtProcessShowMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
646
  SShowMsg *pShowMsg = pCont;
S
slguan 已提交
647
  if (pShowMsg->type == TSDB_MGMT_TABLE_DNODE || TSDB_MGMT_TABLE_GRANTS || TSDB_MGMT_TABLE_SCORES) {
S
slguan 已提交
648 649
    if (mgmtCheckRedirectMsg(ahandle) != 0) {
      return TSDB_CODE_REDIRECT;
S
slguan 已提交
650 651 652
    }
  }

S
slguan 已提交
653 654 655 656 657
  int32_t  size = sizeof(SShowRsp) + sizeof(SSchema) * TSDB_MAX_COLUMNS + TSDB_EXTRA_PAYLOAD_SIZE;
  SShowRsp *pShowRsp = rpcMallocCont(size);
  if (pShowRsp == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0);
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
S
slguan 已提交
658 659
  }

S
slguan 已提交
660
  int32_t code;
S
slguan 已提交
661
  if (pShowMsg->type >= TSDB_MGMT_TABLE_MAX) {
S
slguan 已提交
662
    code = TSDB_CODE_INVALID_MSG_TYPE;
S
slguan 已提交
663
  } else {
S
slguan 已提交
664
    SShowObj *pShow = (SShowObj *) calloc(1, sizeof(SShowObj) + htons(pShowMsg->payloadLen));
S
slguan 已提交
665
    pShow->signature = pShow;
S
slguan 已提交
666 667
    pShow->type      = pShowMsg->type;
    strcpy(pShow->db, pShow->db);
S
slguan 已提交
668 669 670 671 672 673
    mTrace("pShow:%p is allocated", pShow);

    // set the table name query condition
    pShow->payloadLen = htons(pShowMsg->payloadLen);
    memcpy(pShow->payload, pShowMsg->payload, pShow->payloadLen);

S
slguan 已提交
674 675 676
    mgmtSaveQhandle(pShow);
    pShowRsp->qhandle = htobe64((uint64_t) pShow);
    code = (*mgmtGetMetaFp[(uint8_t) pShowMsg->type])(&pShowRsp->meterMeta, pShow, ahandle);
S
slguan 已提交
677
    if (code == 0) {
S
slguan 已提交
678
      size = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns;
S
slguan 已提交
679
    } else {
S
slguan 已提交
680 681
      mError("pShow:%p, type:%d %s, failed to get Meta, code:%d", pShow, pShowMsg->type,
             taosMsg[(uint8_t) pShowMsg->type], code);
S
slguan 已提交
682 683 684 685
      free(pShow);
    }
  }

S
slguan 已提交
686 687
  rpcSendResponse(ahandle, code, pShowRsp, size);
  return code;
H
hzcheng 已提交
688 689
}

S
slguan 已提交
690
int32_t mgmtProcessRetrieveMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  int32_t rowsToRead = 0;
  int32_t size = 0;
  int32_t rowsRead = 0;
  SRetrieveMeterMsg *pRetrieve = (SRetrieveMeterMsg *)pCont;
  pRetrieve->qhandle = htobe64(pRetrieve->qhandle);

  /*
   * in case of server restart, apps may hold qhandle created by server before
   * restart, which is actually invalid, therefore, signature check is required.
   */
  if (!mgmtCheckQhandle(pRetrieve->qhandle)) {
    mError("retrieve:%p, qhandle:%p is invalid", pRetrieve, pRetrieve->qhandle);
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_QHANDLE, NULL, 0);
    return TSDB_CODE_INVALID_QHANDLE;
  }

  SShowObj *pShow = (SShowObj *)pRetrieve->qhandle;
  if (pShow->signature != (void *)pShow) {
    mError("pShow:%p, signature:%p, query memory is corrupted", pShow, pShow->signature);
    rpcSendResponse(ahandle, TSDB_CODE_MEMORY_CORRUPTED, NULL, 0);
    return TSDB_CODE_MEMORY_CORRUPTED;
  } else {
    if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) {
      rowsToRead = pShow->numOfRows - pShow->numOfReads;
    }

    /* return no more than 100 meters in one round trip */
    if (rowsToRead > 100) rowsToRead = 100;

    /*
     * the actual number of table may be larger than the value of pShow->numOfRows, if a query is
     * issued during a continuous create table operation. Therefore, rowToRead may be less than 0.
     */
    if (rowsToRead < 0) rowsToRead = 0;
    size = pShow->rowSize * rowsToRead;
  }

  size += 100;
  SRetrieveMeterRsp *pRsp = rpcMallocCont(size);

  // if free flag is set, client wants to clean the resources
  if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE)
    rowsRead = (*mgmtRetrieveFp[(uint8_t) pShow->type])(pShow, pRsp->data, rowsToRead, ahandle);

  if (rowsRead < 0) {
    rowsRead = 0;  // TSDB_CODE_ACTION_IN_PROGRESS;
    rpcFreeCont(pRsp);
    return TSDB_CODE_ACTION_IN_PROGRESS;
  }

  pRsp->numOfRows = htonl(rowsRead);
  pRsp->precision = htonl(TSDB_TIME_PRECISION_MILLI);  // millisecond time precision

  rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, pRsp, size);

  if (rowsToRead == 0) {
    mgmtFreeQhandle(pShow);
  }

  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
751 752
}

S
slguan 已提交
753
int32_t mgmtProcessCreateTableMsg(void *pCont, int32_t contLen, void *ahandle) {
754 755 756
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
757

758 759 760 761 762 763
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
764 765
  SCreateTableMsg *pCreate = (SCreateTableMsg *) pCont;
  SSchema *pSchema;
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    pCreate->numOfColumns = htons(pCreate->numOfColumns);
    pCreate->numOfTags = htons(pCreate->numOfTags);
    pCreate->sqlLen = htons(pCreate->sqlLen);
    pSchema = pCreate->schema;
    for (int32_t i = 0; i < pCreate->numOfColumns + pCreate->numOfTags; ++i) {
      pSchema->bytes = htons(pSchema->bytes);
      pSchema->colId = i;
      pSchema++;
    }

    SDbObj *pDb = mgmtGetDb(pCreate->db);
    if (pDb) {
      code = mgmtCreateTable(pDb, pCreate);
      if (code == TSDB_CODE_TABLE_ALREADY_EXIST) {
        if (pCreate->igExists) {
          code = TSDB_CODE_SUCCESS;
        }
      }
    } else {
      code = TSDB_CODE_DB_NOT_SELECTED;
    }
  }

  if (code != TSDB_CODE_SUCCESS) {
    rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, NULL, 0);
  }

  return code;
H
hzcheng 已提交
799 800
}

S
slguan 已提交
801
int32_t mgmtProcessDropTableMsg(void *pCont, int32_t contLen, void *ahandle) {
802 803 804
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  SDropTableMsg *pDrop = (SDropTableMsg *) pCont;
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    SDbObj *pDb = mgmtGetDb(pDrop->db);
    if (pDb) {
S
slguan 已提交
820
      code = mgmtDropTable(pDb, pDrop->tableId, pDrop->igNotExists);
821
      if (code == TSDB_CODE_SUCCESS) {
S
slguan 已提交
822
        mTrace("table:%s is dropped by user:%s", pDrop->tableId, pUser->user);
823 824 825 826 827 828 829 830 831 832
      }
    } else {
      code = TSDB_CODE_DB_NOT_SELECTED;
    }
  }

  if (code != TSDB_CODE_SUCCESS) {
    rpcSendResponse(ahandle, code, NULL, 0);
  }
  return code;
H
hzcheng 已提交
833 834
}

S
slguan 已提交
835
int32_t mgmtProcessAlterTableMsg(void *pCont, int32_t contLen, void *ahandle) {
836 837 838
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }
H
hzcheng 已提交
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

  SAlterTableMsg *pAlter = (SAlterTableMsg *) pCont;
  int32_t code;

  if (!pUser->writeAuth) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    pAlter->type      = htons(pAlter->type);
    pAlter->numOfCols = htons(pAlter->numOfCols);

    if (pAlter->numOfCols > 2) {
      mError("table:%s error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols);
      code = TSDB_CODE_APP_ERROR;
    } else {
      SDbObj *pDb = mgmtGetDb(pAlter->db);
      if (pDb) {
        for (int32_t i = 0; i < pAlter->numOfCols; ++i) {
          pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes);
        }

        code = mgmtAlterTable(pDb, pAlter);
        if (code == 0) {
          mLPrint("table:%s is altered by %s", pAlter->tableId, pUser->user);
        }
      } else {
        code = TSDB_CODE_DB_NOT_SELECTED;
      }
    }
  }

  if (code != TSDB_CODE_SUCCESS) {
    rpcSendResponse(ahandle, code, NULL, 0);
  }
  return code;
H
hzcheng 已提交
879 880
}

S
slguan 已提交
881
int32_t mgmtProcessCfgDnodeMsg(void *pCont, int32_t contLen, void *ahandle) {
882 883 884 885 886 887 888 889 890 891
  if (mgmtCheckRedirectMsg(ahandle) != 0) {
    return TSDB_CODE_REDIRECT;
  }

  SUserObj *pUser = mgmtGetUserFromConn(ahandle);
  if (pUser == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_INVALID_USER, NULL, 0);
    return TSDB_CODE_INVALID_USER;
  }

S
slguan 已提交
892
  SCfgDnodeMsg *pCfg = (SCfgDnodeMsg *)pCont;
893 894 895 896 897 898 899 900 901 902 903 904 905 906
  int32_t code;

  if (strcmp(pUser->pAcct->user, "root") != 0) {
    code = TSDB_CODE_NO_RIGHTS;
  } else {
    code = mgmtSendCfgDnodeMsg(pCont);
  }

  if (code == TSDB_CODE_SUCCESS) {
    mTrace("dnode:%s is configured by %s", pCfg->ip, pUser->user);
  }

  rpcSendResponse(ahandle, code, NULL, 0);
  return code;
H
hzcheng 已提交
907 908
}

S
slguan 已提交
909
int32_t mgmtProcessHeartBeatMsg(void *pCont, int32_t contLen, void *ahandle) {
S
slguan 已提交
910
  SHeartBeatMsg *pHBMsg = (SHeartBeatMsg *) pCont;
S
slguan 已提交
911 912
  mgmtSaveQueryStreamList(pHBMsg);

S
slguan 已提交
913
  SHeartBeatRsp *pHBRsp = (SHeartBeatRsp *) rpcMallocCont(contLen);
S
slguan 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  if (pHBRsp == NULL) {
    rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0);
    rpcFreeCont(pCont);
    return TSDB_CODE_SERV_OUT_OF_MEMORY;
  }

  SRpcConnInfo connInfo;
  rpcGetConnInfo(ahandle, &connInfo);

  pHBRsp->ipList.index = 0;
  pHBRsp->ipList.port = htons(tsMgmtShellPort);
  pHBRsp->ipList.numOfIps = 0;
  if (pSdbPublicIpList != NULL && pSdbIpList != NULL) {
    pHBRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps);
    if (connInfo.serverIp == tsPublicIpInt) {
      for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) {
        pHBRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]);
      }
    } else {
      for (int i = 0; i < pSdbIpList->numOfIps; ++i) {
        pHBRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]);
      }
    }
  }

  /*
   * TODO
   * Dispose kill stream or kill query message
   */
  pHBRsp->queryId = 0;
  pHBRsp->streamId = 0;
  pHBRsp->killConnection = 0;

S
slguan 已提交
947
  rpcSendResponse(ahandle, TSDB_CODE_SUCCESS, pHBRsp, sizeof(SHeartBeatMsg));
S
slguan 已提交
948
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
949 950
}

S
slguan 已提交
951
int32_t mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) {
S
slguan 已提交
952 953 954 955
  *spi = 0;
  *encrypt = 0;
  *ckey = 0;

S
slguan 已提交
956 957 958 959
  SUserObj *pUser = mgmtGetUser(user);
  if (pUser == NULL) {
    *secret = 0;
    return TSDB_CODE_INVALID_USER;
S
slguan 已提交
960 961 962
  } else {
    memcpy(secret, pUser->pass, TSDB_KEY_LEN);
    return TSDB_CODE_SUCCESS;
S
slguan 已提交
963
  }
H
hzcheng 已提交
964 965
}

S
slguan 已提交
966
static int32_t mgmtProcessConnectMsg(void *pCont, int32_t contLen, void *thandle) {
S
slguan 已提交
967
  SConnectMsg *pConnectMsg = (SConnectMsg *) pCont;
S
slguan 已提交
968
  SRpcConnInfo connInfo;
S
slguan 已提交
969
  rpcGetConnInfo(thandle, &connInfo);
S
slguan 已提交
970
  int32_t code;
S
slguan 已提交
971

S
slguan 已提交
972
  SUserObj *pUser = mgmtGetUser(connInfo.user);
S
slguan 已提交
973
  if (pUser == NULL) {
S
slguan 已提交
974 975
    code = TSDB_CODE_INVALID_USER;
    goto connect_over;
S
slguan 已提交
976 977 978
  }

  if (mgmtCheckExpired()) {
S
slguan 已提交
979 980
    code = TSDB_CODE_GRANT_EXPIRED;
    goto connect_over;
S
slguan 已提交
981 982 983 984
  }

  SAcctObj *pAcct = mgmtGetAcct(pUser->acct);
  if (pAcct == NULL) {
S
slguan 已提交
985 986
    code = TSDB_CODE_INVALID_ACCT;
    goto connect_over;
S
slguan 已提交
987 988 989 990
  }

  code = taosCheckVersion(pConnectMsg->clientVersion, version, 3);
  if (code != TSDB_CODE_SUCCESS) {
S
slguan 已提交
991
    goto connect_over;
S
slguan 已提交
992 993 994 995 996 997 998
  }

  if (pConnectMsg->db[0]) {
    char dbName[TSDB_TABLE_ID_LEN] = {0};
    sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db);
    SDbObj *pDb = mgmtGetDb(dbName);
    if (pDb == NULL) {
S
slguan 已提交
999 1000
      code = TSDB_CODE_INVALID_DB;
      goto connect_over;
S
slguan 已提交
1001 1002 1003
    }
  }

S
slguan 已提交
1004
  SConnectRsp *pConnectRsp = rpcMallocCont(sizeof(SConnectRsp));
S
slguan 已提交
1005
  if (pConnectRsp == NULL) {
S
slguan 已提交
1006 1007
    code = TSDB_CODE_SERV_OUT_OF_MEMORY;
    goto connect_over;
S
slguan 已提交
1008 1009 1010 1011
  }

  sprintf(pConnectRsp->acctId, "%x", pAcct->acctId);
  strcpy(pConnectRsp->serverVersion, version);
S
slguan 已提交
1012 1013
  pConnectRsp->writeAuth = pUser->writeAuth;
  pConnectRsp->superAuth = pUser->superAuth;
S
slguan 已提交
1014 1015 1016
  pConnectRsp->ipList.index = 0;
  pConnectRsp->ipList.port = htons(tsMgmtShellPort);
  pConnectRsp->ipList.numOfIps = 0;
S
slguan 已提交
1017
  if (pSdbPublicIpList != NULL && pSdbIpList != NULL) {
S
slguan 已提交
1018
    pConnectRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps);
S
slguan 已提交
1019
    if (connInfo.serverIp == tsPublicIpInt) {
S
slguan 已提交
1020
      for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) {
S
slguan 已提交
1021
        pConnectRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]);
S
slguan 已提交
1022 1023 1024
      }
    } else {
      for (int i = 0; i < pSdbIpList->numOfIps; ++i) {
S
slguan 已提交
1025
        pConnectRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]);
S
slguan 已提交
1026 1027
      }
    }
S
slguan 已提交
1028 1029 1030 1031 1032
  }

connect_over:
  if (code != TSDB_CODE_SUCCESS) {
    mLError("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code);
S
slguan 已提交
1033
    rpcSendResponse(thandle, code, NULL, 0);
S
slguan 已提交
1034
  } else {
S
slguan 已提交
1035
    mLPrint("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code);
S
slguan 已提交
1036
    rpcSendResponse(thandle, code, pConnectRsp, sizeof(SConnectRsp));
S
slguan 已提交
1037 1038
  }

S
slguan 已提交
1039
  return code;
H
hzcheng 已提交
1040 1041
}

S
slguan 已提交
1042 1043 1044 1045
/**
 * check if we need to add mgmtProcessMeterMetaMsg into tranQueue, which will be executed one-by-one.
 */
static bool mgmtCheckMeterMetaMsgType(void *pMsg) {
S
slguan 已提交
1046
  STableInfoMsg *pInfo = (STableInfoMsg *) pMsg;
S
slguan 已提交
1047
  int16_t autoCreate = htons(pInfo->createFlag);
S
slguan 已提交
1048
  STableInfo *pTable = mgmtGetTable(pInfo->tableId);
S
slguan 已提交
1049 1050 1051 1052

  // If table does not exists and autoCreate flag is set, we add the handler into task queue
  bool addIntoTranQueue = (pTable == NULL && autoCreate == 1);
  if (addIntoTranQueue) {
S
slguan 已提交
1053
    mTrace("table:%s auto created task added", pInfo->tableId);
S
slguan 已提交
1054 1055 1056 1057 1058 1059 1060
  }

  return addIntoTranQueue;
}

static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont) {
  if ((type == TSDB_MSG_TYPE_TABLE_META && (!mgmtCheckMeterMetaMsgType(pCont)))  ||
S
slguan 已提交
1061
       type == TSDB_MSG_TYPE_STABLE_META || type == TSDB_MSG_TYPE_RETRIEVE ||
S
slguan 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
       type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_MULTI_TABLE_META      ||
       type == TSDB_MSG_TYPE_CONNECT) {
    return true;
  }

  return false;
}

static void mgmtProcessMsgFromShell(char type, void *pCont, int contLen, void *ahandle, int32_t code) {
  if (sdbGetRunStatus() != SDB_STATUS_SERVING) {
    mTrace("shell msg is ignored since SDB is not ready");
    rpcSendResponse(ahandle, TSDB_CODE_NOT_READY, NULL, 0);
S
slguan 已提交
1074
    rpcFreeCont(pCont);
S
slguan 已提交
1075 1076 1077 1078 1079 1080 1081
    return;
  }

  if (mgmtCheckMsgReadOnly(type, pCont)) {
    (*mgmtProcessShellMsg[(int8_t)type])(pCont, contLen, ahandle);
  } else {
    if (mgmtProcessShellMsg[(int8_t)type]) {
S
slguan 已提交
1082
      mgmtAddToTranRequest((int8_t)type, pCont, contLen, ahandle);
S
slguan 已提交
1083 1084 1085 1086
    } else {
      mError("%s from shell is not processed", taosMsg[(int8_t)type]);
    }
  }
S
slguan 已提交
1087
  rpcFreeCont(pCont);
H
hzcheng 已提交
1088 1089 1090
}

void mgmtInitProcessShellMsg() {
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CONNECT]          = mgmtProcessConnectMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_HEARTBEAT]        = mgmtProcessHeartBeatMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DB]        = mgmtProcessCreateDbMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_DB]         = mgmtProcessAlterDbMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DB]          = mgmtProcessDropDbMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_USE_DB]           = mgmtProcessUnSupportMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_USER]      = mgmtProcessCreateUserMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_USER]       = mgmtProcessAlterUserMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_USER]        = mgmtProcessDropUserMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_ACCT]      = mgmtProcessCreateAcctMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_ACCT]        = mgmtProcessDropAcctMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_ACCT]       = mgmtProcessAlterAcctMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_TABLE]     = mgmtProcessCreateTableMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_TABLE]       = mgmtProcessDropTableMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_TABLE]      = mgmtProcessAlterTableMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DNODE]     = mgmtProcessCreateDnodeMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DNODE]       = mgmtProcessDropDnodeMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DNODE_CFG]        = mgmtProcessCfgDnodeMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_MNODE]     = mgmtProcessUnSupportMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_MNODE]       = mgmtProcessDropMnodeMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_CFG_MNODE]        = mgmtProcessCfgMnodeMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_QUERY]       = mgmtProcessKillQueryMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_STREAM]      = mgmtProcessKillStreamMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_CONNECTION]  = mgmtProcessKillConnectionMsg;
  mgmtProcessShellMsg[TSDB_MSG_TYPE_SHOW]             = mgmtProcessShowMsg;
S
slguan 已提交
1116
  mgmtProcessShellMsg[TSDB_MSG_TYPE_RETRIEVE]         = mgmtProcessRetrieveMsg;
1117
  mgmtProcessShellMsg[TSDB_MSG_TYPE_TABLE_META]       = mgmtProcessMeterMetaMsg;
S
slguan 已提交
1118
  mgmtProcessShellMsg[TSDB_MSG_TYPE_MULTI_TABLE_META] = mgmtProcessMultiMeterMetaMsg;
S
slguan 已提交
1119
  mgmtProcessShellMsg[TSDB_MSG_TYPE_STABLE_META]      = mgmtProcessUnSupportMsg;
H
hzcheng 已提交
1120
}
S
#1177  
slguan 已提交
1121

1122
static int32_t mgmtCheckRedirectMsgImp(void *pConn) {
S
#1177  
slguan 已提交
1123 1124
  return 0;
}
1125

S
slguan 已提交
1126
int32_t (*mgmtCheckRedirectMsg)(void *pConn) = mgmtCheckRedirectMsgImp;
S
#1177  
slguan 已提交
1127

S
slguan 已提交
1128 1129
static int32_t mgmtProcessUnSupportMsg(void *pCont, int32_t contLen, void *ahandle) {
  rpcSendResponse(ahandle, TSDB_CODE_OPS_NOT_SUPPORT, NULL, 0);
1130
  return TSDB_CODE_OPS_NOT_SUPPORT;
S
#1177  
slguan 已提交
1131 1132
}

1133
int32_t (*mgmtProcessAlterAcctMsg)(void *pCont, int32_t contLen, void *ahandle)   = mgmtProcessUnSupportMsg;
S
slguan 已提交
1134
int32_t (*mgmtProcessCreateDnodeMsg)(void *pCont, int32_t contLen, void *ahandle) = mgmtProcessUnSupportMsg;
1135 1136 1137 1138 1139
int32_t (*mgmtProcessCfgMnodeMsg)(void *pCont, int32_t contLen, void *ahandle)    = mgmtProcessUnSupportMsg;
int32_t (*mgmtProcessDropMnodeMsg)(void *pCont, int32_t contLen, void *ahandle)   = mgmtProcessUnSupportMsg;
int32_t (*mgmtProcessDropDnodeMsg)(void *pCont, int32_t contLen, void *ahandle)   = mgmtProcessUnSupportMsg;
int32_t (*mgmtProcessDropAcctMsg)(void *pCont, int32_t contLen, void *ahandle)    = mgmtProcessUnSupportMsg;
int32_t (*mgmtProcessCreateAcctMsg)(void *pCont, int32_t contLen, void *ahandle)  = mgmtProcessUnSupportMsg;