From 920cfc5981d839fa1df6877a5adf50b873b37c39 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 17 Aug 2020 08:41:55 +0000 Subject: [PATCH] TD-1154 #1112 How to modify root's password when I have forgot it? --- src/client/src/taos.def | 1 + src/client/src/tscSql.c | 91 ++++++++++++++++++++++---------- src/client/src/tscSystem.c | 8 ++- src/common/inc/tglobal.h | 1 + src/common/src/tglobal.c | 1 + src/dnode/src/dnodeSystem.c | 2 + src/kit/shell/inc/shell.h | 1 + src/kit/shell/src/shellDarwin.c | 6 +-- src/kit/shell/src/shellEngine.c | 9 +++- src/kit/shell/src/shellLinux.c | 10 ++-- src/kit/shell/src/shellWindows.c | 15 ++++-- src/kit/taosdemo/taosdemo.c | 8 +-- src/mnode/src/mnodeUser.c | 32 +++++++++++ 13 files changed, 137 insertions(+), 48 deletions(-) diff --git a/src/client/src/taos.def b/src/client/src/taos.def index df84ac657e..49d7290ce7 100644 --- a/src/client/src/taos.def +++ b/src/client/src/taos.def @@ -3,6 +3,7 @@ taos_init taos_cleanup taos_options taos_connect +taos_connect_auth taos_close taos_stmt_init taos_stmt_prepare diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 29c8aa0a56..7046bc7725 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -16,6 +16,7 @@ #include "hash.h" #include "os.h" #include "qAst.h" +#include "tkey.h" #include "tcache.h" #include "tnote.h" #include "trpc.h" @@ -47,18 +48,37 @@ static bool validPassword(const char* passwd) { return validImpl(passwd, TSDB_PASSWORD_LEN - 1); } -SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, const char *db, uint16_t port, - void (*fp)(void *, TAOS_RES *, int), void *param, void **taos) { +SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, const char *auth, const char *db, + uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, void **taos) { taos_init(); - + if (!validUserName(user)) { terrno = TSDB_CODE_TSC_INVALID_USER_LENGTH; return NULL; } - if (!validPassword(pass)) { - terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH; - return NULL; + char secretEncrypt[32] = {0}; + int secretEncryptLen = 0; + if (auth == NULL) { + if (!validPassword(pass)) { + terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH; + return NULL; + } + taosEncryptPass((uint8_t *)pass, strlen(pass), secretEncrypt); + } else { + int outlen = 0; + int len = strlen(auth); + char *base64 = (char *)base64_decode(auth, len, &outlen); + if (base64 == NULL || outlen == 0) { + tscError("invalid auth info:%s", auth); + free(base64); + terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH; + return NULL; + } else { + memcpy(secretEncrypt, base64, outlen); + free(base64); + } + secretEncryptLen = outlen; } if (ip) { @@ -67,7 +87,7 @@ SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, con } void *pDnodeConn = NULL; - if (tscInitRpc(user, pass, &pDnodeConn) != 0) { + if (tscInitRpc(user, secretEncrypt, &pDnodeConn) != 0) { terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL; return NULL; } @@ -82,7 +102,8 @@ SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, con pObj->signature = pObj; tstrncpy(pObj->user, user, sizeof(pObj->user)); - taosEncryptPass((uint8_t *)pass, strlen(pass), pObj->pass); + secretEncryptLen = MIN(secretEncryptLen, sizeof(pObj->pass)); + memcpy(pObj->pass, secretEncrypt, secretEncryptLen); if (db) { int32_t len = (int32_t)strlen(db); @@ -144,20 +165,17 @@ static void syncConnCallback(void *param, TAOS_RES *tres, int code) { tsem_post(&pSql->rspSem); } -TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) { - tscDebug("try to create a connection to %s:%u, user:%s db:%s", ip, port, user, db); - if (user == NULL) user = TSDB_DEFAULT_USER; - if (pass == NULL) pass = TSDB_DEFAULT_PASS; - - STscObj* pObj = NULL; - SSqlObj *pSql = taosConnectImpl(ip, user, pass, db, port, syncConnCallback, NULL, (void**) &pObj); +TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, + uint16_t port) { + STscObj *pObj = NULL; + SSqlObj *pSql = taosConnectImpl(ip, user, pass, auth, db, port, syncConnCallback, NULL, (void **)&pObj); if (pSql != NULL) { pSql->fp = syncConnCallback; pSql->param = pSql; - + tscProcessSql(pSql); tsem_wait(&pSql->rspSem); - + if (pSql->res.code != TSDB_CODE_SUCCESS) { terrno = pSql->res.code; taos_free_result(pSql); @@ -182,23 +200,38 @@ TAOS *taos_connect(const char *ip, const char *user, const char *pass, const cha return NULL; } -TAOS *taos_connect_c(const char *ip, uint8_t ipLen, const char *user, uint8_t userLen, - const char *pass, uint8_t passLen, const char *db, uint8_t dbLen, uint16_t port) { - char ipBuf[TSDB_EP_LEN] = {0}; - char userBuf[TSDB_USER_LEN] = {0}; - char passBuf[TSDB_PASSWORD_LEN] = {0}; - char dbBuf[TSDB_DB_NAME_LEN] = {0}; - strncpy(ipBuf, ip, MIN(TSDB_EP_LEN - 1, ipLen)); - strncpy(userBuf, user, MIN(TSDB_USER_LEN - 1, userLen)); - strncpy(passBuf, pass, MIN(TSDB_PASSWORD_LEN - 1,passLen)); - strncpy(dbBuf, db, MIN(TSDB_DB_NAME_LEN - 1, dbLen)); - return taos_connect(ipBuf, userBuf, passBuf, dbBuf, port); +TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) { + tscDebug("try to create a connection to %s:%u, user:%s db:%s", ip, port, user, db); + if (user == NULL) user = TSDB_DEFAULT_USER; + if (pass == NULL) pass = TSDB_DEFAULT_PASS; + + return taos_connect_internal(ip, user, pass, NULL, db, port); +} + +TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port) { + tscDebug("try to create a connection to %s:%u by auth, user:%s db:%s", ip, port, user, db); + if (user == NULL) user = TSDB_DEFAULT_USER; + if (auth == NULL) return NULL; + + return taos_connect_internal(ip, user, NULL, auth, db, port); } +TAOS *taos_connect_c(const char *ip, uint8_t ipLen, const char *user, uint8_t userLen, const char *pass, + uint8_t passLen, const char *db, uint8_t dbLen, uint16_t port) { + char ipBuf[TSDB_EP_LEN] = {0}; + char userBuf[TSDB_USER_LEN] = {0}; + char passBuf[TSDB_PASSWORD_LEN] = {0}; + char dbBuf[TSDB_DB_NAME_LEN] = {0}; + strncpy(ipBuf, ip, MIN(TSDB_EP_LEN - 1, ipLen)); + strncpy(userBuf, user, MIN(TSDB_USER_LEN - 1, userLen)); + strncpy(passBuf, pass, MIN(TSDB_PASSWORD_LEN - 1, passLen)); + strncpy(dbBuf, db, MIN(TSDB_DB_NAME_LEN - 1, dbLen)); + return taos_connect(ipBuf, userBuf, passBuf, dbBuf, port); +} TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, void **taos) { - SSqlObj* pSql = taosConnectImpl(ip, user, pass, db, port, fp, param, taos); + SSqlObj* pSql = taosConnectImpl(ip, user, pass, NULL, db, port, fp, param, taos); if (pSql == NULL) { return NULL; } diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 211e673754..72f23881d2 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -47,10 +47,8 @@ void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) { taosTmrReset(tscCheckDiskUsage, 1000, NULL, tscTmr, &tscCheckDiskUsageTmr); } -int32_t tscInitRpc(const char *user, const char *secret, void** pDnodeConn) { +int32_t tscInitRpc(const char *user, const char *secretEncrypt, void **pDnodeConn) { SRpcInit rpcInit; - char secretEncrypt[32] = {0}; - taosEncryptPass((uint8_t *)secret, strlen(secret), secretEncrypt); if (*pDnodeConn == NULL) { memset(&rpcInit, 0, sizeof(rpcInit)); @@ -60,11 +58,11 @@ int32_t tscInitRpc(const char *user, const char *secret, void** pDnodeConn) { rpcInit.cfp = tscProcessMsgFromServer; rpcInit.sessions = tsMaxConnections; rpcInit.connType = TAOS_CONN_CLIENT; - rpcInit.user = (char*)user; + rpcInit.user = (char *)user; rpcInit.idleTime = 2000; rpcInit.ckey = "key"; rpcInit.spi = 1; - rpcInit.secret = secretEncrypt; + rpcInit.secret = (char *)secretEncrypt; *pDnodeConn = rpcOpen(&rpcInit); if (*pDnodeConn == NULL) { diff --git a/src/common/inc/tglobal.h b/src/common/inc/tglobal.h index a04b5f32b9..4ffc631566 100644 --- a/src/common/inc/tglobal.h +++ b/src/common/inc/tglobal.h @@ -113,6 +113,7 @@ extern char tsInternalPass[]; extern int32_t tsMonitorInterval; // internal +extern int32_t tsPrintAuth; extern int32_t tscEmbedded; extern char configDir[]; extern char tsVnodeDir[]; diff --git a/src/common/src/tglobal.c b/src/common/src/tglobal.c index 391e5f5935..e9d7a71477 100644 --- a/src/common/src/tglobal.c +++ b/src/common/src/tglobal.c @@ -146,6 +146,7 @@ char tsInternalPass[] = "secretkey"; int32_t tsMonitorInterval = 30; // seconds // internal +int32_t tsPrintAuth = 0; int32_t tscEmbedded = 0; char configDir[TSDB_FILENAME_LEN] = {0}; char tsVnodeDir[TSDB_FILENAME_LEN] = {0}; diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c index 543e1c9639..6f32bc0f7a 100644 --- a/src/dnode/src/dnodeSystem.c +++ b/src/dnode/src/dnodeSystem.c @@ -52,6 +52,8 @@ int32_t main(int32_t argc, char *argv[]) { } else if (strcmp(argv[i], "-k") == 0) { grantParseParameter(); exit(EXIT_SUCCESS); + } else if (strcmp(argv[i], "-A") == 0) { + tsPrintAuth = 1; } #ifdef TAOS_MEM_CHECK else if (strcmp(argv[i], "--alloc-random-fail") == 0) { diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index 54ac45583c..d47f87965b 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -39,6 +39,7 @@ typedef struct SShellArguments { char* host; char* password; char* user; + char* auth; char* database; char* timezone; bool is_raw_time; diff --git a/src/kit/shell/src/shellDarwin.c b/src/kit/shell/src/shellDarwin.c index ce41827462..ffe537dd91 100644 --- a/src/kit/shell/src/shellDarwin.c +++ b/src/kit/shell/src/shellDarwin.c @@ -32,16 +32,16 @@ void insertChar(Command *cmd, char *c, int size); void printHelp() { char indent[10] = " "; - printf("taos shell is used to test the TDEngine database\n"); + printf("taos shell is used to test the TDengine database\n"); printf("%s%s\n", indent, "-h"); - printf("%s%s%s\n", indent, indent, "TDEngine server IP address to connect. The default host is localhost."); + printf("%s%s%s\n", indent, indent, "TDengine server IP address to connect. The default host is localhost."); printf("%s%s\n", indent, "-p"); printf("%s%s%s\n", indent, indent, "The password to use when connecting to the server."); printf("%s%s\n", indent, "-P"); printf("%s%s%s\n", indent, indent, "The TCP/IP port number to use for the connection"); printf("%s%s\n", indent, "-u"); - printf("%s%s%s\n", indent, indent, "The TDEngine user name to use when connecting to the server."); + printf("%s%s%s\n", indent, indent, "The user name to use when connecting to the server."); printf("%s%s\n", indent, "-c"); printf("%s%s%s\n", indent, indent, "Configuration directory."); printf("%s%s\n", indent, "-s"); diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index 692b5e49a0..d82ddf8871 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -38,6 +38,7 @@ SShellHistory history; #define DEFAULT_MAX_BINARY_DISPLAY_WIDTH 30 extern int32_t tsMaxBinaryDisplayWidth; +extern TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port); /* * FUNCTION: Initialize the shell. @@ -70,7 +71,13 @@ TAOS *shellInit(SShellArguments *args) { tsTableMetaKeepTimer = 3000; // Connect to the database. - TAOS *con = taos_connect(args->host, args->user, args->password, args->database, args->port); + TAOS *con = NULL; + if (args->auth == NULL) { + con = taos_connect(args->host, args->user, args->password, args->database, args->port); + } else { + con = taos_connect_auth(args->host, args->user, args->auth, args->database, args->port); + } + if (con == NULL) { printf("taos connect failed, reason: %s.\n\n", tstrerror(terrno)); fflush(stdout); diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index 963afe346d..c74eeb7f59 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -33,10 +33,11 @@ const char *argp_program_bug_address = ""; static char doc[] = ""; static char args_doc[] = ""; static struct argp_option options[] = { - {"host", 'h', "HOST", 0, "TDEngine server IP address to connect. The default host is localhost."}, + {"host", 'h', "HOST", 0, "TDengine server IP address to connect. The default host is localhost."}, {"password", 'p', "PASSWORD", OPTION_ARG_OPTIONAL, "The password to use when connecting to the server."}, {"port", 'P', "PORT", 0, "The TCP/IP port number to use for the connection."}, - {"user", 'u', "USER", 0, "The TDEngine user name to use when connecting to the server."}, + {"user", 'u', "USER", 0, "The user name to use when connecting to the server."}, + {"user", 'A', "Auth", 0, "The user auth to use when connecting to the server."}, {"config-dir", 'c', "CONFIG_DIR", 0, "Configuration directory."}, {"commands", 's', "COMMANDS", 0, "Commands to run without enter the shell."}, {"raw-time", 'r', 0, 0, "Output time as uint64_t."}, @@ -76,11 +77,14 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { case 'u': arguments->user = arg; break; + case 'A': + arguments->auth = arg; + break; case 'c': if (wordexp(arg, &full_path, 0) != 0) { fprintf(stderr, "Invalid path %s\n", arg); return -1; - } + } if (strlen(full_path.we_wordv[0]) >= TSDB_FILENAME_LEN) { fprintf(stderr, "config file path: %s overflow max len %d\n", full_path.we_wordv[0], TSDB_FILENAME_LEN - 1); wordfree(&full_path); diff --git a/src/kit/shell/src/shellWindows.c b/src/kit/shell/src/shellWindows.c index 279d3b9cdd..67174827d4 100644 --- a/src/kit/shell/src/shellWindows.c +++ b/src/kit/shell/src/shellWindows.c @@ -21,16 +21,18 @@ extern char configDir[]; void printHelp() { char indent[10] = " "; - printf("taos shell is used to test the TDEngine database\n"); + printf("taos shell is used to test the TDengine database\n"); printf("%s%s\n", indent, "-h"); - printf("%s%s%s\n", indent, indent, "TDEngine server IP address to connect. The default host is localhost."); + printf("%s%s%s\n", indent, indent, "TDengine server IP address to connect. The default host is localhost."); printf("%s%s\n", indent, "-p"); printf("%s%s%s\n", indent, indent, "The password to use when connecting to the server."); printf("%s%s\n", indent, "-P"); printf("%s%s%s\n", indent, indent, "The TCP/IP port number to use for the connection"); printf("%s%s\n", indent, "-u"); - printf("%s%s%s\n", indent, indent, "The TDEngine user name to use when connecting to the server."); + printf("%s%s%s\n", indent, indent, "The user name to use when connecting to the server."); + printf("%s%s\n", indent, "-A"); + printf("%s%s%s\n", indent, indent, "The user auth to use when connecting to the server."); printf("%s%s\n", indent, "-c"); printf("%s%s%s\n", indent, indent, "Configuration directory."); printf("%s%s\n", indent, "-s"); @@ -79,6 +81,13 @@ void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { fprintf(stderr, "option -u requires an argument\n"); exit(EXIT_FAILURE); } + } else if (strcmp(argv[i], "-A") == 0) { + if (i < argc - 1) { + arguments->auth = argv[++i]; + } else { + fprintf(stderr, "option -A requires an argument\n"); + exit(EXIT_FAILURE); + } } else if (strcmp(argv[i], "-c") == 0) { if (i < argc - 1) { if (strlen(argv[++i]) >= TSDB_FILENAME_LEN) { diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 192cb3145c..74bdf6f074 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -85,9 +85,9 @@ typedef struct DemoArguments { #ifdef LINUX /* The options we understand. */ static struct argp_option options[] = { - {0, 'h', "host", 0, "The host to connect to TDEngine. Default is localhost.", 0}, + {0, 'h', "host", 0, "The host to connect to TDengine. Default is localhost.", 0}, {0, 'p', "port", 0, "The TCP/IP port number to use for the connection. Default is 0.", 1}, - {0, 'u', "user", 0, "The TDEngine user name to use when connecting to the server. Default is 'root'.", 2}, + {0, 'u', "user", 0, "The TDengine user name to use when connecting to the server. Default is 'root'.", 2}, {0, 'P', "password", 0, "The password to use when connecting to the server. Default is 'taosdata'.", 3}, {0, 'd', "database", 0, "Destination database. Default is 'test'.", 3}, {0, 'm', "table_prefix", 0, "Table prefix name. Default is 't'.", 3}, @@ -264,11 +264,11 @@ typedef struct DemoArguments { void printHelp() { char indent[10] = " "; printf("%s%s\n", indent, "-h"); - printf("%s%s%s\n", indent, indent, "host, The host to connect to TDEngine. Default is localhost."); + printf("%s%s%s\n", indent, indent, "host, The host to connect to TDengine. Default is localhost."); printf("%s%s\n", indent, "-p"); printf("%s%s%s\n", indent, indent, "port, The TCP/IP port number to use for the connection. Default is 0."); printf("%s%s\n", indent, "-u"); - printf("%s%s%s\n", indent, indent, "user, The TDEngine user name to use when connecting to the server. Default is 'root'."); + printf("%s%s%s\n", indent, indent, "user, The user name to use when connecting to the server. Default is 'root'."); printf("%s%s\n", indent, "-p"); printf("%s%s%s\n", indent, indent, "password, The password to use when connecting to the server. Default is 'taosdata'."); printf("%s%s\n", indent, "-d"); diff --git a/src/mnode/src/mnodeUser.c b/src/mnode/src/mnodeUser.c index 765661ac71..6e63a8f2de 100644 --- a/src/mnode/src/mnodeUser.c +++ b/src/mnode/src/mnodeUser.c @@ -20,6 +20,7 @@ #include "tglobal.h" #include "tgrant.h" #include "tdataformat.h" +#include "tkey.h" #include "mnode.h" #include "dnode.h" #include "mnodeDef.h" @@ -100,6 +101,32 @@ static int32_t mnodeUserActionDecode(SSdbOper *pOper) { return TSDB_CODE_SUCCESS; } +static void mnodePrintUserAuth() { + FILE *fp = fopen("auth.txt", "w"); + if (!fp) { + mDebug("failed to auth.txt for write"); + return; + } + + void * pIter = NULL; + SUserObj *pUser = NULL; + + while (1) { + pIter = mnodeGetNextUser(pIter, &pUser); + if (pUser == NULL) break; + + char *base64 = base64_encode((const unsigned char *)pUser->pass, TSDB_KEY_LEN * 2); + fprintf(fp, "user:%24s auth:%s\n", pUser->user, base64); + free(base64); + + mnodeDecUserRef(pUser); + } + + fflush(fp); + sdbFreeIter(pIter); + fclose(fp); +} + static int32_t mnodeUserActionRestored() { int32_t numOfRows = sdbGetNumOfRows(tsUserSdb); if (numOfRows <= 0 && dnodeIsFirstDeploy()) { @@ -111,6 +138,11 @@ static int32_t mnodeUserActionRestored() { mnodeDecAcctRef(pAcct); } + if (tsPrintAuth != 0) { + mInfo("print user auth, for -A parameter is set"); + mnodePrintUserAuth(); + } + return TSDB_CODE_SUCCESS; } -- GitLab