enhance: continue coding
This commit is contained in:
parent
f280da26da
commit
33966cf856
|
@ -884,6 +884,11 @@ typedef struct {
|
|||
int32_t tSerializeSDropUserReq(void* buf, int32_t bufLen, SDropUserReq* pReq);
|
||||
int32_t tDeserializeSDropUserReq(void* buf, int32_t bufLen, SDropUserReq* pReq);
|
||||
|
||||
typedef struct SIpV4Range{
|
||||
uint32_t ip;
|
||||
uint32_t mask;
|
||||
} SIpV4Range;
|
||||
|
||||
typedef struct {
|
||||
int8_t createType;
|
||||
int8_t superUser; // denote if it is a super user or not
|
||||
|
@ -893,10 +898,13 @@ typedef struct {
|
|||
char pass[TSDB_USET_PASSWORD_LEN];
|
||||
int32_t sqlLen;
|
||||
char* sql;
|
||||
int32_t numIpRanges;
|
||||
SIpV4Range* pIpRanges;
|
||||
} SCreateUserReq;
|
||||
|
||||
int32_t tSerializeSCreateUserReq(void* buf, int32_t bufLen, SCreateUserReq* pReq);
|
||||
int32_t tDeserializeSCreateUserReq(void* buf, int32_t bufLen, SCreateUserReq* pReq);
|
||||
void tFreeSCreateUserReq(SCreateUserReq* pReq);
|
||||
|
||||
typedef struct {
|
||||
int8_t alterType;
|
||||
|
@ -911,6 +919,8 @@ typedef struct {
|
|||
int32_t tagCondLen;
|
||||
int32_t sqlLen;
|
||||
char* sql;
|
||||
int32_t numIpRanges;
|
||||
SIpV4Range* pIpRanges;
|
||||
} SAlterUserReq;
|
||||
|
||||
int32_t tSerializeSAlterUserReq(void* buf, int32_t bufLen, SAlterUserReq* pReq);
|
||||
|
|
|
@ -222,6 +222,8 @@ typedef struct SCreateUserStmt {
|
|||
char userName[TSDB_USER_LEN];
|
||||
char password[TSDB_USET_PASSWORD_LEN];
|
||||
int8_t sysinfo;
|
||||
int32_t numIpRanges;
|
||||
SIpV4Range* pIpRanges;
|
||||
} SCreateUserStmt;
|
||||
|
||||
typedef struct SAlterUserStmt {
|
||||
|
@ -231,6 +233,8 @@ typedef struct SAlterUserStmt {
|
|||
char password[TSDB_USET_PASSWORD_LEN];
|
||||
int8_t enable;
|
||||
int8_t sysinfo;
|
||||
int32_t numIpRanges;
|
||||
SIpV4Range* pIpRanges;
|
||||
} SAlterUserStmt;
|
||||
|
||||
typedef struct SDropUserStmt {
|
||||
|
|
|
@ -710,6 +710,7 @@ int32_t* taosGetErrno();
|
|||
#define TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED_FUNC TAOS_DEF_ERROR_CODE(0, 0x2668)
|
||||
#define TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED TAOS_DEF_ERROR_CODE(0, 0x2669)
|
||||
#define TSDB_CODE_PAR_INVALID_VARBINARY TAOS_DEF_ERROR_CODE(0, 0x266A)
|
||||
#define TSDB_CODE_PAR_INVALID_IP_RANGE TAOS_DEF_ERROR_CODE(0, 0x266B)
|
||||
#define TSDB_CODE_PAR_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x26FF)
|
||||
|
||||
//planner
|
||||
|
|
|
@ -1380,6 +1380,11 @@ int32_t tSerializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pReq
|
|||
if (tEncodeI8(&encoder, pReq->enable) < 0) return -1;
|
||||
if (tEncodeCStr(&encoder, pReq->user) < 0) return -1;
|
||||
if (tEncodeCStr(&encoder, pReq->pass) < 0) return -1;
|
||||
if (tEncodeI32(&encoder, pReq->numIpRanges) < 0) return -1;
|
||||
for (int32_t i = 0; i < pReq->numIpRanges; ++i) {
|
||||
if (tEncodeU32(&encoder, pReq->pIpRanges[i].ip) < 0) return -1;
|
||||
if (tEncodeU32(&encoder, pReq->pIpRanges[i].mask) < 0) return -1;
|
||||
}
|
||||
tEndEncode(&encoder);
|
||||
|
||||
int32_t tlen = encoder.pos;
|
||||
|
@ -1398,12 +1403,23 @@ int32_t tDeserializeSCreateUserReq(void *buf, int32_t bufLen, SCreateUserReq *pR
|
|||
if (tDecodeI8(&decoder, &pReq->enable) < 0) return -1;
|
||||
if (tDecodeCStrTo(&decoder, pReq->user) < 0) return -1;
|
||||
if (tDecodeCStrTo(&decoder, pReq->pass) < 0) return -1;
|
||||
if (tDecodeI32(&decoder, &pReq->numIpRanges) < 0) return -1;
|
||||
pReq->pIpRanges = taosMemoryMalloc(pReq->numIpRanges * sizeof(SIpV4Range));
|
||||
if (pReq->pIpRanges == NULL) return -1;
|
||||
for (int32_t i = 0; i < pReq->numIpRanges; ++i) {
|
||||
if (tDecodeU32(&decoder, &(pReq->pIpRanges[i].ip)) < 0) return -1;
|
||||
if (tDecodeU32(&decoder, &(pReq->pIpRanges[i].mask)) < 0) return -1;
|
||||
}
|
||||
tEndDecode(&decoder);
|
||||
|
||||
tDecoderClear(&decoder);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tFreeSCreateUserReq(SCreateUserReq* pReq) {
|
||||
taosMemoryFree(pReq->pIpRanges);
|
||||
}
|
||||
|
||||
int32_t tSerializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq) {
|
||||
SEncoder encoder = {0};
|
||||
tEncoderInit(&encoder, buf, bufLen);
|
||||
|
@ -1422,6 +1438,11 @@ int32_t tSerializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq)
|
|||
if (tEncodeCStr(&encoder, pReq->tabName) < 0) return -1;
|
||||
}
|
||||
if (tEncodeBinary(&encoder, pReq->tagCond, pReq->tagCondLen) < 0) return -1;
|
||||
if (tEncodeI32(&encoder, pReq->numIpRanges) < 0) return -1;
|
||||
for (int32_t i = 0; i < pReq->numIpRanges; ++i) {
|
||||
if (tEncodeU32(&encoder, pReq->pIpRanges[i].ip) < 0) return -1;
|
||||
if (tEncodeU32(&encoder, pReq->pIpRanges[i].mask) < 0) return -1;
|
||||
}
|
||||
tEndEncode(&encoder);
|
||||
|
||||
int32_t tlen = encoder.pos;
|
||||
|
@ -1451,13 +1472,23 @@ int32_t tDeserializeSAlterUserReq(void *buf, int32_t bufLen, SAlterUserReq *pReq
|
|||
if (tDecodeBinaryAlloc(&decoder, (void **)&pReq->tagCond, &tagCondLen) < 0) return -1;
|
||||
pReq->tagCondLen = tagCondLen;
|
||||
}
|
||||
if (tDecodeI32(&decoder, &pReq->numIpRanges) < 0) return -1;
|
||||
pReq->pIpRanges = taosMemoryMalloc(pReq->numIpRanges * sizeof(SIpV4Range));
|
||||
if (pReq->pIpRanges == NULL) return -1;
|
||||
for (int32_t i = 0; i < pReq->numIpRanges; ++i) {
|
||||
if (tDecodeU32(&decoder, &(pReq->pIpRanges[i].ip)) < 0) return -1;
|
||||
if (tDecodeU32(&decoder, &(pReq->pIpRanges[i].mask)) < 0) return -1;
|
||||
}
|
||||
tEndDecode(&decoder);
|
||||
|
||||
tDecoderClear(&decoder);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tFreeSAlterUserReq(SAlterUserReq *pReq) { taosMemoryFreeClear(pReq->tagCond); }
|
||||
void tFreeSAlterUserReq(SAlterUserReq *pReq) {
|
||||
taosMemoryFreeClear(pReq->tagCond);
|
||||
taosMemoryFree(pReq->pIpRanges);
|
||||
}
|
||||
|
||||
int32_t tSerializeSGetUserAuthReq(void *buf, int32_t bufLen, SGetUserAuthReq *pReq) {
|
||||
SEncoder encoder = {0};
|
||||
|
|
|
@ -665,7 +665,7 @@ _OVER:
|
|||
|
||||
mndReleaseUser(pMnode, pUser);
|
||||
mndReleaseUser(pMnode, pOperUser);
|
||||
|
||||
tFreeSCreateUserReq(&createReq);
|
||||
return code;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include "parAst.h"
|
||||
|
@ -1665,16 +1666,56 @@ SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const ST
|
|||
return (SNode*)pStmt;
|
||||
}
|
||||
|
||||
static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange) {
|
||||
struct in_addr addr;
|
||||
|
||||
char* ipCopy = strdup(ipRange);
|
||||
char* slash = strchr(ipCopy, '/');
|
||||
if (slash) {
|
||||
*slash = '\0';
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
return TSDB_CODE_PAR_INVALID_IP_RANGE;
|
||||
}
|
||||
} else {
|
||||
if (inet_pton(AF_INET, ipCopy, &addr) == 1) {
|
||||
pIpRange->ip = addr.s_addr;
|
||||
pIpRange->mask = 0xFFFFFFFF;
|
||||
} else {
|
||||
return TSDB_CODE_PAR_INVALID_IP_RANGE;
|
||||
}
|
||||
}
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) {
|
||||
if (pIpRangesNodeList == NULL) {
|
||||
return pCreateUserStmt;
|
||||
}
|
||||
SCreateUserStmt* pCreateUser = (SCreateUserStmt*)pCreateUserStmt;
|
||||
pCreateUser->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
|
||||
pCreateUser->pIpRanges = taosMemoryMalloc(pCreateUser->numIpRanges * sizeof(SIpV4Range));
|
||||
int32_t i = 0;
|
||||
int32_t code = 0;
|
||||
|
||||
SNode* pNode = NULL;
|
||||
FOREACH(pNode, pIpRangesNodeList) {
|
||||
char* pStr = NULL;
|
||||
nodesNodeToString(pNode, false, &pStr, NULL);
|
||||
printf("%s\n", pStr);
|
||||
taosMemoryFree(pStr);
|
||||
SValueNode* pValNode = (SValueNode*)(pNode);
|
||||
code = getIpV4RangeFromWhitelistItem(pValNode->literal, pCreateUser->pIpRanges + i);
|
||||
if (code != TSDB_CODE_SUCCESS) {
|
||||
//TODO: see check user name/pass to return error no
|
||||
taosMemoryFree(pCreateUser->pIpRanges);
|
||||
nodesDestroyNode(pCreateUserStmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return pCreateUserStmt;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue