diff --git a/include/libs/nodes/cmdnodes.h b/include/libs/nodes/cmdnodes.h index 59b66eafd9..40b9d21503 100644 --- a/include/libs/nodes/cmdnodes.h +++ b/include/libs/nodes/cmdnodes.h @@ -224,6 +224,8 @@ typedef struct SCreateUserStmt { int8_t sysinfo; int32_t numIpRanges; SIpV4Range* pIpRanges; + + SNodeList* pNodeListIpRanges; } SCreateUserStmt; typedef struct SAlterUserStmt { @@ -235,6 +237,8 @@ typedef struct SAlterUserStmt { int8_t sysinfo; int32_t numIpRanges; SIpV4Range* pIpRanges; + + SNodeList* pNodeListIpRanges; } SAlterUserStmt; typedef struct SDropUserStmt { diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index bc2067660a..a5b9f6dd91 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -927,11 +927,13 @@ void nodesDestroyNode(SNode* pNode) { case QUERY_NODE_CREATE_USER_STMT: { SCreateUserStmt* pStmt = (SCreateUserStmt*)pNode; taosMemoryFree(pStmt->pIpRanges); + nodesDestroyList(pStmt->pNodeListIpRanges); break; } case QUERY_NODE_ALTER_USER_STMT: { SAlterUserStmt* pStmt = (SAlterUserStmt*)pNode; taosMemoryFree(pStmt->pIpRanges); + nodesDestroyList(pStmt->pNodeListIpRanges); } case QUERY_NODE_DROP_USER_STMT: // no pointer field case QUERY_NODE_USE_DATABASE_STMT: // no pointer field diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index bc07687022..286477659c 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -1655,33 +1655,39 @@ SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* p } static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange) { - struct in_addr addr; - - char* ipCopy = strdup(ipRange); + int32_t code = TSDB_CODE_SUCCESS; + char* ipCopy = taosStrdup(ipRange); char* slash = strchr(ipCopy, '/'); if (slash) { *slash = '\0'; + struct in_addr addr; if (inet_pton(AF_INET, ipCopy, &addr) == 1) { int prefix = atoi(slash + 1); - pIpRange->ip = addr.s_addr; - uint32_t mask = (1 << (32 - prefix)) - 1; - mask = htonl(~mask); - pIpRange->mask = mask; if (prefix < 0 || prefix > 32) { - return TSDB_CODE_PAR_INVALID_IP_RANGE; + code = TSDB_CODE_PAR_INVALID_IP_RANGE; + } else { + pIpRange->ip = addr.s_addr; + uint32_t mask = (1 << (32 - prefix)) - 1; + mask = htonl(~mask); + pIpRange->mask = mask; + code = TSDB_CODE_SUCCESS; } } else { - return TSDB_CODE_PAR_INVALID_IP_RANGE; + code = TSDB_CODE_PAR_INVALID_IP_RANGE; } } else { + struct in_addr addr; if (inet_pton(AF_INET, ipCopy, &addr) == 1) { pIpRange->ip = addr.s_addr; pIpRange->mask = 0xFFFFFFFF; + code = TSDB_CODE_SUCCESS; } else { - return TSDB_CODE_PAR_INVALID_IP_RANGE; + code = TSDB_CODE_PAR_INVALID_IP_RANGE; } } - return TSDB_CODE_SUCCESS; + + taosMemoryFreeClear(ipCopy); + return code; } static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIpRangesNodeList, SIpV4Range* pIpRanges) { @@ -1706,6 +1712,8 @@ static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIp } SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) { + ((SCreateUserStmt*)pCreateUserStmt)->pNodeListIpRanges = pIpRangesNodeList; + if (pIpRangesNodeList == NULL) { return pCreateUserStmt; } @@ -1773,6 +1781,7 @@ SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t al case TSDB_ALTER_USER_ADD_WHITE_LIST: case TSDB_ALTER_USER_DROP_WHITE_LIST: { SNodeList* pIpRangesNodeList = pAlterInfo; + pStmt->pNodeListIpRanges = pIpRangesNodeList; pStmt->numIpRanges = LIST_LENGTH(pIpRangesNodeList); pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpV4Range)); if (NULL == pStmt->pIpRanges) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 253e605190..7fba30e60f 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -5713,10 +5713,12 @@ static int32_t translateCreateUser(STranslateContext* pCxt, SCreateUserStmt* pSt createReq.enable = 1; strcpy(createReq.pass, pStmt->password); createReq.numIpRanges = pStmt->numIpRanges; - createReq.pIpRanges = taosMemoryMalloc(createReq.numIpRanges); + createReq.pIpRanges = taosMemoryMalloc(createReq.numIpRanges * sizeof(SIpV4Range)); memcpy(createReq.pIpRanges, pStmt->pIpRanges, sizeof(SIpV4Range) * createReq.numIpRanges); - return buildCmdMsg(pCxt, TDMT_MND_CREATE_USER, (FSerializeFunc)tSerializeSCreateUserReq, &createReq); + int32_t code = buildCmdMsg(pCxt, TDMT_MND_CREATE_USER, (FSerializeFunc)tSerializeSCreateUserReq, &createReq); + tFreeSCreateUserReq(&createReq); + return code; } static int32_t translateAlterUser(STranslateContext* pCxt, SAlterUserStmt* pStmt) { @@ -5731,10 +5733,12 @@ static int32_t translateAlterUser(STranslateContext* pCxt, SAlterUserStmt* pStmt snprintf(alterReq.objname, sizeof(alterReq.objname), "%s", pCxt->pParseCxt->db); } alterReq.numIpRanges = pStmt->numIpRanges; - alterReq.pIpRanges = taosMemoryMalloc(alterReq.numIpRanges); + alterReq.pIpRanges = taosMemoryMalloc(alterReq.numIpRanges * sizeof(SIpV4Range)); memcpy(alterReq.pIpRanges, pStmt->pIpRanges, sizeof(SIpV4Range) * alterReq.numIpRanges); - return buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &alterReq); + int32_t code = buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &alterReq); + tFreeSAlterUserReq(&alterReq); + return code; } static int32_t translateDropUser(STranslateContext* pCxt, SDropUserStmt* pStmt) {