diff --git a/Common/config_manager.h b/Common/config_manager.h index 64d418177..f8a9f80d7 100755 --- a/Common/config_manager.h +++ b/Common/config_manager.h @@ -26,6 +26,11 @@ /*nat config */ #define NAT_CONFIG_MODULE 0x00000008 +/*vlan config */ +#define VLAN_CONFIG_MODULE 0x00000005 + +/*DHCP CONFIG*/ +#define DHCP_CONFIG_MODULE 0x00000006 /************************* 模块定义结束 **********************/ /************************ config id定义 **********************/ @@ -51,6 +56,14 @@ #define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1) +#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1) +#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2) +#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3) +#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4) +#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5) +#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6) + + /************************ config id定义 end**********************/ #endif diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index 2fd52fb67..56e28c26c 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -1,79 +1,83 @@ -# target name, the target name must have the same name of c source file -TARGET_NAME=configm - -# target -# for linux module driver: KO -# for application: EXE -# for dynamic library: DLL -TARGET_TYPE = EXE - -# target object -# for application: APP -# for device driver: DRV -TARGET_OBJ = APP - -# custom install dir -TARGET_BOX = - -#debug mode or release mode -DEBUG = TRUE - -PLAT_LINUX ?= TRUE -PLAT_ARM64 ?= TRUE - -VPATH = ../user/configm/config-server -# source code - -# set the source file, don't used .o because of ... - -COMMON_SRCS = configserver.c \ - netconfig/parsefile.c netconfig/ifconfig.c netconfig/netmain.c\ - netconfig/ipconfig/ipconfig.c \ - netconfig/bridge/brconfig.c netconfig/bridge/brnetlink.c \ - netconfig/bridge/libbridge/libbridge_if.c netconfig/bridge/libbridge/libbridge_init.c netconfig/bridge/libbridge/libbridge_devif.c\ - web_config/config-adm/user_authpara.c \ - web_config/config-adm/user_authfree.c \ - web_config/authfree.c web_config/auth_parameters.c\ - user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \ - user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \ - log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c - -# MRS Board Source Files -PLAT_LINUX_SRCS = $(COMMON_SRCS) -PLAT_ARM64_SRCS = $(COMMON_SRCS) - -COMMOM_CFLAGS = -I../user/configm/config-server/include -I../../Common -I../common/redismq -I../common/database -I../common/configm -I../common/rpc -I../common/rpc/hashtable -I../common/ulog -I../user/configm/config-server/netconfig/ -I../user/configm/config-server/netconfig/bridge/include -# gcc CFLAGS -PLAT_ARM64_CFLAGS := $(COMMOM_CFLAGS) -PLAT_LINUX_CFLAGS := $(COMMOM_CFLAGS) - -PLAT_ARM64_LDFLAGS := -PLAT_LINUX_LDFLAGS := - -#gcc libs -ARM64_LIBS := -lopenrpc-arm64 -lnetlinku-arm64 -lredismq-arm64 -lulogapi-arm64 -ldatabase-arm64 -ARM64_LIBS += -lpthread -lm -lcjson -levent -ljson-c -lhiredis -lodbc -lev -ldl - -LINUX_LIBS := -lopenrpc-linux -lnetlinku-linux -lredismq-linux -lulogapi-linux -ldatabase-linux -LINUX_LIBS += -lpthread -lm -lcjson -levent -ljson-c -lhiredis -lodbc -lev -ldl - - -# this line must be at below of thus, because of... -include ../../Common/common.Makefile - -ifneq ($(MAKECMDGOALS), clean) -ifneq ($(MAKECMDGOALS), cleanall) -ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) -$(shell $(CP) $(DEPEND_LIB) ./) -endif -endif -endif - -ifeq ($(MAKECMDGOALS), ) -$(shell find ./ -name "$(TARGET)-*.ko" -delete) -else -ifeq ($(MAKECMDGOALS), all) -$(shell find ./ -name "$(TARGET)-*.ko" -delete) -endif -endif - +# target name, the target name must have the same name of c source file +TARGET_NAME=configm + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = EXE + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = APP + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../user/configm/config-server +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = configserver.c \ + netconfig/parsefile.c netconfig/ifconfig.c netconfig/netmain.c\ + netconfig/ipconfig/ipconfig.c \ + netconfig/bridge/brconfig.c netconfig/bridge/brnetlink.c \ + netconfig/bridge/libbridge/libbridge_if.c netconfig/bridge/libbridge/libbridge_init.c netconfig/bridge/libbridge/libbridge_devif.c\ + web_config/config-adm/user_authpara.c \ + web_config/config-adm/user_authfree.c \ + web_config/authfree.c web_config/auth_parameters.c\ + user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \ + user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \ + log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c \ + nat_config/natconfig.c + + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + + +COMMOM_CFLAGS = -I../user/configm/config-server/include -I../../Common -I../common/redismq -I../common/database -I../common/configm -I../common/rpc -I../common/rpc/hashtable -I../common/ulog -I../user/configm/config-server/netconfig/ -I../user/configm/config-server/netconfig/bridge/include + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := $(COMMOM_CFLAGS) +PLAT_LINUX_CFLAGS := $(COMMOM_CFLAGS) + +PLAT_ARM64_LDFLAGS := +PLAT_LINUX_LDFLAGS := + +#gcc libs +ARM64_LIBS := -lopenrpc-arm64 -lnetlinku-arm64 -lredismq-arm64 -lulogapi-arm64 -ldatabase-arm64 +ARM64_LIBS += -lpthread -lm -lcjson -levent -ljson-c -lhiredis -lodbc -lev -ldl + +LINUX_LIBS := -lopenrpc-linux -lnetlinku-linux -lredismq-linux -lulogapi-linux -ldatabase-linux +LINUX_LIBS += -lpthread -lm -lcjson -levent -ljson-c -lhiredis -lodbc -lev -ldl + + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifneq ($(MAKECMDGOALS), clean) +ifneq ($(MAKECMDGOALS), cleanall) +ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) +$(shell $(CP) $(DEPEND_LIB) ./) +endif +endif +endif + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c new file mode 100644 index 000000000..a03643662 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c @@ -0,0 +1,209 @@ +#include "dhcp_client_config.h" + +ret_code dhcp_client_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + return ret; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate){ + *config_type = CM_CONFIG_SET; + } + return ret; +} + + +ret_code dhcp_client_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *interfaces=NULL; + + int len = 50; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + interfaces = cJSON_GetObjectItem(root, "interfaces"); + if(NULL == interfaces) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + count = cJSON_GetArraySize(interfaces); + if(!strcmp(operate->valuestring, "obtain ip")){ + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(interfaces, i); + if(NULL == one) + { + continue; + } + cJSON *name = cJSON_GetObjectItem(one, "name"); + if(NULL == name) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "dhclient %s", name->valuestring); + system(cmd); + //system("dhclient %s", name->valuestring); + } + } + if(!strcmp(operate->valuestring, "lease ip")){ + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(interfaces, i); + if(NULL == one) + { + continue; + } + cJSON *name = cJSON_GetObjectItem(one, "name"); + if(NULL == name) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "dhclient -r %s", name->valuestring); + system(cmd); + //system("dhclient -r %s", name->valuestring); + } + } + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + + } + return ret; + +} + +ret_code dhcp_client_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *interface=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + interface = cJSON_GetObjectItem(root, "interface"); + if(NULL == interface) + { + return RET_INPUTERR; + } + + //ȡļroot@ubuntu:/var/lib/dhcp# cat dhclient.leases һ + char *fname="/var/lib/dhcp/dhclient.leases"; + FILE *fp; + char *lease_interface, *ip, *router; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + lease_interface = (char *)malloc(20); + memset(lease_interface, 0, 20); + ip = (char *)malloc(20); + memset(ip, 0, 20); + router = (char *)malloc(20); + memset(router, 0, 20); + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "interface", interface->valuestring); + + + while((fgets(line,1000,fp))!=NULL){ + char *startINTER = strstr(line, "interface"); + char *endINTER = strstr(line, ";"); + if((startINTER!=NULL) && (endINTER!=NULL)){ + memcpy(lease_interface,startINTER+11,endINTER-startINTER-12); + if(!strcmp(interface->valuestring, lease_interface)){ + memset(ip, 0, 20); + memset(router, 0, 20); + } + //printf("ip: %s\n", ip); + //json = cJSON_CreateObject(); + //cJSON_AddStringToObject(json , "ip", ip); + } + if(lease_interface != NULL){ + // + if(strcmp(interface->valuestring, lease_interface)){ + continue; + } + } + char *startIP = strstr(line, "fixed-address"); + char *endIP = strstr(line, ";"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+14,endIP-startIP-14); + //printf("ip: %s\n", ip); + //json = cJSON_CreateObject(); + //cJSON_AddStringToObject(json , "ip", ip); + } + char *startROUTER = strstr(line, "option routers"); + char *endROUTER = strstr(line, ";"); + if((startROUTER!=NULL) && (endROUTER!=NULL)){ + memcpy(router,startROUTER+15,endROUTER-startROUTER-15); + //printf("mac: %s\n", mac); + //cJSON_AddStringToObject(json , "mac", mac); + } + } + if(ip != NULL){ + cJSON_AddStringToObject(root, "ip", ip); + memset(ip, 0, 20); + free(ip); + } + if(router != NULL){ + cJSON_AddStringToObject(root, "router", router); + memset(router, 0, 20); + free(router); + } + if(lease_interface != NULL){ + memset(lease_interface, 0, 20); + free(lease_interface); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c b/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c new file mode 100644 index 000000000..cdec7c562 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c @@ -0,0 +1,109 @@ +#include "dhcp_dhcpd_lease.h" + +ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + *config_type = CM_CONFIG_GET_ALL; + return ret; +} + +ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len) +{ + //ip_config_t *ip_conf; + ret_code ret = RET_OK; + + + char *fname="/var/lib/dhcp/dhcpd.leases"; + FILE *fp; + char *ip=NULL, *mac=NULL, *hname=NULL, *time=NULL; + cJSON *root = NULL, *arr = NULL, *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + ip = (char *)malloc(20); + memset(ip, 0, 20); + mac = (char *)malloc(20); + memset(mac, 0, 20); + hname = (char *)malloc(100); + memset(hname, 0, 100); + time = (char *)malloc(100); + memset(time, 0, 100); + root = cJSON_CreateObject(); + cJSON_AddItemToObject(root, "lease", arr = cJSON_CreateArray()); + + + while((fgets(line,1000,fp))!=NULL){ + char *startIP = strstr(line, "lease"); + char *endIP = strstr(line, "{"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+6,endIP-startIP-7); + //printf("ip: %s\n", ip); + json = cJSON_CreateObject(); + cJSON_AddStringToObject(json , "ip", ip); + } + char *startMAC = strstr(line, "hardware ethernet"); + char *endMAC = strstr(line, ";"); + if((startMAC!=NULL) && (endMAC!=NULL)){ + memcpy(mac,startMAC+18,endMAC-startMAC-18); + //printf("mac: %s\n", mac); + cJSON_AddStringToObject(json , "mac", mac); + } + char *startNAME = strstr(line, "client-hostname"); + char *endNAME = strstr(line, ";"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(hname,startNAME+17,endNAME-startNAME-18); + //printf("name: %s\n", hname); + cJSON_AddStringToObject(json , "host-name", hname); + } + char *startTIME = strstr(line, "ends"); + char *endTIME = strstr(line, ";"); + if((startTIME!=NULL) && (endTIME!=NULL)){ + memcpy(time,startTIME+7,endTIME-startTIME-7); + //printf("time: %s\n", time); + cJSON_AddStringToObject(json , "end-time", time); + } + char *end = strstr(line, "}"); + if(end != NULL){ + //printf("\n"); + cJSON_AddItemToArray(arr, json); + memset(ip, 0, 20); + memset(mac, 0, 20); + memset(hname, 0, 100); + memset(time, 0, 100); + } + } + fclose(fp); + if(ip != NULL){ + memset(ip, 0, 20); + free(ip); + } + if(mac != NULL){ + memset(mac, 0, 20); + free(mac); + } + if(hname != NULL){ + memset(hname, 0, 100); + free(hname); + } + if(time != NULL){ + memset(time, 0, 100); + free(time); + } + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + //out = cJSON_Print(root); + //printf("%s\n",out); + return ret; + +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c new file mode 100644 index 000000000..4ac986da2 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c @@ -0,0 +1,316 @@ +#include "dhcp_host_config.h" + +ret_code dhcp_host_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate) { + *config_type = CM_CONFIG_SET; + } + return ret; +} + +/*inputʽ: +{ + ""operate" : "add", + ""host": [ + { + "host-name" : "h1", + "hardware-ethernet" : "00:00:00:00:00:01", + "fixed-address" : "10.0.0.1" + }, + { + "host-name" : "h2", + "hardware-ethernet" : "00:00:00:00:00:02", + "fixed-address" : "10.0.0.2" + }, + ] +} +*/ +ret_code dhcp_host_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *host=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + host = cJSON_GetObjectItem(root, "host"); + if(NULL == host) + { + return RET_INPUTERR; + } + count = cJSON_GetArraySize(host); + int len = 500; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(host, i); + if(NULL == one) + { + continue; + } + cJSON *hostName = cJSON_GetObjectItem(one, "host-name"); + if(NULL == hostName) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check host name + if(!check_name(hostName->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + cJSON *macAddr = cJSON_GetObjectItem(one, "hardware-ethernet"); + cJSON *fixedAddr = cJSON_GetObjectItem(one, "fixed-address"); + + /*дļ*/ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + if((NULL == macAddr) || (NULL == fixedAddr)) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //ж֮ǰǷùhost + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + //check hardware ethernet and fixed address + if((!check_mac(macAddr->valuestring)) || (!check_ip(fixedAddr->valuestring))){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //echo 'xxx' >> targetfile + /* + host h1 { + hardware ethernet 08:00:07:26:c0:a5; + fixed-address 10.0.7.2; + } + */ + //echo յҪ-e ﲻҪ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'host %s {\\n hardware ethernet %s;\\n fixed-address %s;\\n}' >> /etc/dhcp/dhcpd.conf", + hostName->valuestring, macAddr->valuestring, fixedAddr->valuestring); + system(cmd); + } + + if(!strcmp(operate->valuestring, "del")) + { + //ж֮ǰǷùhost + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", hostName->valuestring); + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + //system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")) + { + snprintf(cmd, len, "grep -Pzo 'host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != macAddr) + { + if(file_consist_str("/tmp/subconf.conf", macAddr->valuestring)){ + //printf("mac already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check mac + if(!check_mac(macAddr->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/hardware[ \\t]+ethernet[ \\ta-fA-F0-9:]+;/hardware ethernet %s;/g' /tmp/subconf.conf", macAddr->valuestring); + system(cmd); + } + if(NULL != fixedAddr) + { + if(file_consist_str("/tmp/subconf.conf", fixedAddr->valuestring)){ + //printf("fixed ip already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(!check_ip(fixedAddr->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/fixed-address[ \\t0-9\\.]+;/fixed-address %s;/g' /tmp/subconf.conf", fixedAddr->valuestring); + system(cmd); + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", hostName->valuestring, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + + } + + } + + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + /*ͷڴ*/ + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + //ret = RET_INPUTERR; + return ret; +} + +ret_code dhcp_host_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *host=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + host = cJSON_GetObjectItem(root, "host-name"); + if(NULL == host) + { + return RET_INPUTERR; + } + if(!check_name(host->valuestring)){ + return RET_INPUTERR; + } + + //ȡļ + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *name=NULL, *mac=NULL, *ip=NULL; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + int len; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + len = strlen(host->valuestring); + name = (char *)malloc(len+1); + memset(name, 0, 20); + ip = (char *)malloc(20); + memset(ip, 0, 20); + mac = (char *)malloc(20); + memset(mac, 0, 20); + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "host-name", host->valuestring); + + while((fgets(line,1000,fp))!=NULL){ + char *startNAME = strstr(line, "host"); + char *endNAME = strstr(line, "{"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(name,startNAME+5,endNAME-startNAME-6); + if(!strcmp(host->valuestring, name)){ + memset(ip, 0, 20); + memset(mac, 0, 20); + } + } + if(name != NULL){ + // + if(strcmp(host->valuestring, name)){ + continue; + } + } + char *startIP = strstr(line, "fixed-address"); + char *endIP = strstr(line, ";"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+14,endIP-startIP-14); + } + char *startMAC = strstr(line, "hardware ethernet"); + char *endMAC = strstr(line, ";"); + if((startMAC!=NULL) && (endMAC!=NULL)){ + memcpy(mac,startMAC+18,endMAC-startMAC-18); + } + } + if(ip != NULL){ + cJSON_AddStringToObject(root, "fixed-address", ip); + memset(ip, 0, 20); + free(ip); + } + if(mac != NULL){ + cJSON_AddStringToObject(root, "hardware-ethernet", mac); + memset(mac, 0, 20); + free(mac); + } + if(name != NULL){ + memset(name, 0, 20); + free(name); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c new file mode 100644 index 000000000..c25d43113 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c @@ -0,0 +1,425 @@ +#include "dhcp_lib.h" + + +char *getfileall(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char txt[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize); + str[0]=0; + + rewind(fp); + while((fgets(txt,1000,fp))!=NULL){ + strcat(str,txt); + } + fclose(fp); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +char *getfilefirstline(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char txt[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize); + str[0]=0; + + rewind(fp); + + while((fgets(txt,1000,fp))!=NULL){ + int len = strlen(txt); + if(txt[len-1] == '\n'){ + txt[len-1] = '\0'; + } + strcat(str,txt); + break; + } + fclose(fp); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +char *getfileall_with_linefeed(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char *p; + char line[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize+100); + str[0]=0; + + rewind(fp); + while((fgets(line,1000,fp))!=NULL){ + int len = strlen(line); + if(line[len-1] == '\n'){ + line[len-1] = ' '; + //printf("%s\n", line); + p = line+len; + stpcpy(p, "\\n"); + //printf("%s\n", line); + } + strcat(str,line); + } + fclose(fp); + //printf("%s\n", str); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +int check_name(char *name){ + if(NULL == name){ + return FALSE; + } + //1+ĸ + int i=0; + while(name[i] != '\0'){ + if(((name[i]>='0') && (name[i]<='9')) || ((name[i]>='a') && (name[i]<='z')) || ((name[i]>='A') && (name[i]<='Z'))){ + i++; + continue; + } + return FALSE; + } + //2Ψһaddʱж + + return TRUE; +} + +int check_mac(char *mac){ + if(NULL == mac){ + return FALSE; + } + int status; + const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$"; + const int cflags = REG_EXTENDED | REG_NEWLINE; + + char ebuf[128]; + regmatch_t pmatch[1]; + int nmatch = 10; + regex_t reg; + + + status = regcomp(®, pattern, cflags);//ģʽ + if(status != 0) { + regerror(status, ®, ebuf, sizeof(ebuf)); + //fprintf(stderr, "regcomp fail: %s , pattern '%s' \n",ebuf, pattern); + goto failed; + } + + status = regexec(®, mac, nmatch, pmatch,0);//ִʽͻıȽ, + if(status != 0) { + regerror(status, ®, ebuf, sizeof(ebuf)); + //fprintf(stderr, "regexec fail: %s , mac:\"%s\" \n", ebuf, mac); + goto failed; + } + + //printf("[%s] match success.\n", __FUNCTION__); + regfree(®); + return TRUE; + +failed: + regfree(®); + return FALSE; +} +int file_consist_str(char *fname, char *str){ + //ȡļ + FILE *fp; + //char *name, *mac, *ip; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return FALSE; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + while((fgets(line,1000,fp))!=NULL){ + char *p = strstr(line, str); + if(p){ + return TRUE; + } + } + return FALSE; +} + +int check_range(char *range, char *mask, char *subnet){ + if((NULL==range) || (NULL==mask) || (NULL==subnet)){ + return FALSE; + } + int len = strlen(range); + char rangeARR[len+1]; + strcpy(rangeARR, range); + + //1ֳip & жip + struct in_addr low, high, subnetadd, maskaddr; + char *item; + item = strtok(rangeARR, " "); + int code, lowsub, highsub; + if(NULL == item){ + return FALSE; + } + code = inet_aton(item, &low); + if(!(code)){ + return FALSE; + } + item = strtok(NULL, " "); + if(NULL == item){ + return FALSE; + } + code = inet_aton(item, &high); + if(!(code)){ + return FALSE; + } + item = strtok(NULL, " "); + if(NULL != item){ + return FALSE; + } + + // 2subnetΧ + inet_aton(subnet, &subnetadd); + inet_aton(mask, &maskaddr); + lowsub = low.s_addr & maskaddr.s_addr; + if(lowsub != subnetadd.s_addr){ + return FALSE; + } + highsub = high.s_addr & maskaddr.s_addr; + if(highsub != subnetadd.s_addr){ + return FALSE; + } + + //3һǰһ + char lowARR[15]; + strcpy(lowARR, inet_ntoa(low)); + char highARR[15]; + strcpy(highARR, inet_ntoa(high)); + if(strcmp(lowARR, highARR) >= 0){ + return FALSE; + } + return TRUE; +} + +int check_dns(char *dns){ + if(NULL == dns){ + return FALSE; + } + int len = strlen(dns); + char dnsARR[len+1]; + strcpy(dnsARR, dns); + + //DNSжǷip + char *item; + item = strtok(dnsARR, ","); + if(NULL == item){ + return FALSE; + } + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, ","); + //ֻһdns + if(NULL == item){ + return TRUE; + } + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, ","); + if(NULL != item){ + return FALSE; + } + return TRUE; +} + +int check_lease(char *lease){ + //־Ϳ + int i=0; + while(lease[i] != '\0'){ + if((lease[i]>='0') && (lease[i]<='9')){ + i++; + continue; + } + return FALSE; + } + return TRUE; +} + +int check_ip(char *ip){ + if(NULL == ip){ + return FALSE; + } + struct in_addr ipadd; + //int s_addr; + int code; + code = inet_aton(ip, &ipadd); + /* + int s_addr; + s_addr = htonl(ipadd.s_addr); + printf("[test]ipadd.s_addr=%x, s_addr=%x\n", ipadd.s_addr, s_addr); + ipadd.s_addr = s_addr;*/ + if((1 == code)){ + return TRUE; + } + return FALSE; +} + +char *get_interface_subnet(char *interface) { + if(NULL == interface){ + return NULL; + } + struct in_addr ipadd, maskadd, subnet; + char *ip, *mask; + ip = get_interface_ip(interface); + mask = get_interface_mask(interface); + + if(ip == NULL){ + return NULL; + } + inet_aton(ip, &ipadd); + if(mask == NULL){ + return NULL; + } + inet_aton(mask, &maskadd); + subnet.s_addr = ipadd.s_addr & maskadd.s_addr; + if(ip != NULL){ + free(ip); + ip = NULL; + } + if(mask != NULL){ + free(mask); + mask = NULL; + } + //printf ("%s\n", inet_ntoa(subnet)); + char *str = (char *)malloc(15); + memset(str, 0, 15); + strcpy(str, inet_ntoa(subnet)); + return str; +} + +char *get_interface_ip(char *interface) { + if(NULL == interface){ + return NULL; + } + char *cmd = (char *)malloc(101); + if(!cmd){ + return NULL; + } + memset(cmd, 0, 101); + //snprintf(cmd, 100, "ifconfig %s | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}' > /tmp/ip", interface); + snprintf(cmd, 100, "ifconfig %s | grep \"inet\" | awk '{ print $2}' > /tmp/ip", interface); + system(cmd); + memset(cmd, 0, 101); + free(cmd); + return getfilefirstline("/tmp/ip"); +} + +char *get_interface_mask(char *interface) { + if(NULL == interface){ + return NULL; + } + char *cmd = (char *)malloc(101); + memset(cmd, 0, 101); + //snprintf(cmd, 100, "ifconfig %s | grep \"Mask\" | awk '{ print $4}' | awk -F: '{print $2}' > /tmp/mask", interface); + snprintf(cmd, 100, "ifconfig %s | grep \"netmask\" | awk '{ print $4}' > /tmp/mask", interface); + system(cmd); + memset(cmd, 0, 101); + free(cmd); + return getfilefirstline("/tmp/mask"); +} + +int check_servers(char *servers){ + if(NULL == servers){ + return FALSE; + } + int len = strlen(servers); + char arr[len+1]; + strcpy(arr, servers); + + //serversжǷip + char *item; + item = strtok(arr, " "); + if(NULL == item){ + return FALSE; + } + while(item != NULL){ + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, " "); + } + return TRUE; +} + +int check_segment(char *segment, char *mask){ + //1ipip + struct in_addr segmentadd, maskadd; + + if(check_ip(segment)){ + inet_aton(segment, &segmentadd); + inet_aton(mask, &maskadd); + if((maskadd.s_addr & segmentadd.s_addr) == segmentadd.s_addr){ + return TRUE; + } + } + return FALSE; +} + +int check_mask(char *mask) +{ + if(check_ip(mask)) + { + unsigned int b = 0, i, n[4]; + sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]); + for(i = 0; i < 4; ++i){ //32λ޷ + b += n[i] << (i * 8); + } + b = ~b + 1; + if((b & (b - 1)) == 0){ //жǷΪ2^n + return TRUE; + } + } + return FALSE; +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c new file mode 100644 index 000000000..cca16cb41 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c @@ -0,0 +1,84 @@ +#include "dhcp_relay_config.h" + +ret_code dhcp_relay_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + *config_type = CM_CONFIG_SET; + return ret; +} + + +ret_code dhcp_relay_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + //int i, count; + cJSON *root=NULL, *operate=NULL, *interfaces=NULL, *servers=NULL; + int len = 100; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + ret = RET_ERR; + goto INPUT_ERROR; + } + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + if(!strcmp(operate->valuestring, "close relay")){ + //ɾļ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"\"/g' /etc/default/isc-dhcp-relay"); //\" д\\" + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"\"/g' /etc/default/isc-dhcp-relay"); //\" д\\" + system(cmd); + system("service isc-dhcp-relay stop"); + } + if(!strcmp(operate->valuestring, "start relay")){ + interfaces = cJSON_GetObjectItem(root, "interfaces"); + servers = cJSON_GetObjectItem(root, "servers"); + if((NULL == interfaces) || (NULL == servers)) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //Ȳcheck interface + //check servers + if(!check_servers(servers->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"%s\"/g' /etc/default/isc-dhcp-relay", servers->valuestring); //\" д\\" + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"%s\"/g' /etc/default/isc-dhcp-relay", interfaces->valuestring); //\" д\\" + system(cmd); + /*ļ*/ + system("service isc-dhcp-relay restart"); + } + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + return ret; + +} diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c new file mode 100644 index 000000000..5627a088e --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c @@ -0,0 +1,467 @@ +#include "dhcp_subnet_config.h" + +ret_code dhcp_shared_network_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate){ + *config_type = CM_CONFIG_SET; + } + else { + *config_type = CM_CONFIG_GET_ALL; + } + return ret; +} + +ret_code dhcp_shared_network_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *shared=NULL, *name=NULL, *subnet=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *routers=NULL, *dns=NULL, *lease=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + shared = cJSON_GetObjectItem(root, "shared-network"); + if(NULL == shared) + { + return RET_INPUTERR; + } + + count = cJSON_GetArraySize(shared); + int len = 5000; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + + for(i = 0; i < count; i++) + + { + cJSON *one = cJSON_GetArrayItem(shared, i); + if(NULL == one) + { + continue; + } + name = cJSON_GetObjectItem(one, "name"); + if(NULL == name){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(!check_name(name->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + subnet = cJSON_GetObjectItem(one, "subnet"); + if((!strcmp(operate->valuestring, "add")) || (!strcmp(operate->valuestring, "mod"))){ + if(NULL == subnet){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + segment = cJSON_GetObjectItem(subnet, "segment"); + netmask = cJSON_GetObjectItem(subnet, "netmask"); + range = cJSON_GetObjectItem(subnet, "range"); + routers = cJSON_GetObjectItem(subnet, "routers"); + dns = cJSON_GetObjectItem(subnet, "domain-name-servers"); + //submask = cJSON_GetObjectItem(subnet, "subnet-mask"); + lease = cJSON_GetObjectItem(subnet, "max-lease-time"); + } + //дļ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + if((NULL==segment) || (NULL==netmask) || (NULL==range) || (NULL==routers) || (NULL==dns)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check mask + if(!check_mask(netmask->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check segment + if(!check_segment(segment->valuestring, netmask->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check range + if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check routers + if(!check_ip(routers->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check dns + if(!check_dns(dns->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check lease + if(lease && !check_lease(lease->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //ж֮ǰǷshared-networksubnet + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment->valuestring, netmask->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'shared-network %s {\\n subnet %s netmask %s {\\n range %s;\\n option domain-name-servers %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf", + name->valuestring, segment->valuestring, netmask->valuestring, range->valuestring, dns->valuestring, netmask->valuestring, routers->valuestring); + system(cmd); + if(NULL != lease){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring); + system(cmd); + } + system("echo -e ' }\\n}' >> /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "del")){ + //ж֮ǰǷ + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + //snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{\\n[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}\\n[ \\t]*\\}//g' /etc/dhcp/dhcpd.conf", + //name->valuestring, segment->valuestring, netmask->valuestring); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\{\\-]+\\}[ \\t\\n]*\\}/#/g' /etc/dhcp/dhcpd.conf", name->valuestring); //\\-÷ + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")){ + snprintf(cmd, len, "grep -Pzo 'shared-network[\\s\\t]+%s[\\s\\t]*{[\\n\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}[\\n\\s\\t]*}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", + name->valuestring, segment->valuestring, netmask->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != range) + { + if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(file_consist_str("/tmp/subconf.conf",range->valuestring)){ + //printf("range already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring); + system(cmd); + } + if(NULL != dns) + { + if(!check_dns(dns->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strdns = (char *)malloc(80); + memset(strdns, 0, 80); + snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring); + //printf("strdns: %s\n", strdns); + if(file_consist_str("/tmp/subconf.conf",strdns)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring); + system(cmd); + } + + if(NULL != routers) + { + if(!check_ip(routers->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strrouters = (char *)malloc(40); + memset(strrouters, 0, 40); + snprintf(strrouters, 40, "option routers %s;", routers->valuestring); + //printf("strdns: %s\n", strdns); + if(file_consist_str("/tmp/subconf.conf",strrouters)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strrouters){ + memset(strrouters, 0, 40); + free(strrouters); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strrouters){ + memset(strrouters, 0, 40); + free(strrouters); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring); + system(cmd); + } + if(NULL != lease) + { + if(!check_lease(lease->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strlease = (char *)malloc(1000); + memset(strlease, 0, 1000); + snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring); + if(file_consist_str("/tmp/subconf.conf",strlease)){ + //printf("lease already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + /*lease 0ǻָĬϣҪɾļеmax-lease-time*/ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf"); + system(cmd); + system("sed -r -i '/#/d' /tmp/subconf.conf"); + if(strcmp(lease->valuestring, "0")){ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/\\}[ \\t\\n]*\\}/ max-lease-time %s;\\n \\}\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //Եʱһ滻\\} + //printf("cmd: %s\n", cmd); + system(cmd); + } + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[ \\t\\n]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}[ \\t\\n]*\\}/%s/g' /etc/dhcp/dhcpd.conf", + name->valuestring, segment->valuestring, netmask->valuestring, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + } + } + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + + return ret; +} + +ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len){ + ret_code ret = RET_OK; + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *name=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; + cJSON *root = NULL, *arr=NULL, *json = NULL, *subnet = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + name = (char *)malloc(100); + memset(name, 0, 100); + segment = (char *)malloc(20); + memset(segment, 0, 20); + netmask = (char *)malloc(20); + memset(netmask, 0, 20); + range = (char *)malloc(40); + memset(range, 0, 40); + dns = (char *)malloc(40); + memset(dns, 0, 40); + submask = (char *)malloc(20); + memset(submask, 0, 20); + routers= (char *)malloc(20); + memset(routers, 0, 20); + lease = (char *)malloc(1000); + memset(lease, 0, 1000); + + root = cJSON_CreateObject(); + cJSON_AddItemToObject(root, "shared-network", arr = cJSON_CreateArray()); + int count = 0; //}ĸ + + while((fgets(line,1000,fp))!=NULL){ + char *startNAME = strstr(line, "shared-network"); + char *endNAME = strstr(line, "{"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(name,startNAME+15,endNAME-startNAME-16); + //printf("ip: %s\n", ip); + json = cJSON_CreateObject(); + cJSON_AddStringToObject(json , "name", name); + cJSON_AddItemToObject(json, "subnet", subnet = cJSON_CreateObject()); + memset(segment, 0, 20); + memset(netmask, 0, 20); + memset(range, 0, 40); + memset(dns, 0, 40); + memset(submask, 0, 20); + memset(routers, 0, 20); + memset(lease, 0, 1000); + } + char *startSUB = strstr(line, "subnet"); + char *startMASK = strstr(line, "netmask"); + char *endMASK = strstr(line, "{"); + if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){ + memcpy(segment,startSUB+7,startMASK-startSUB-8); + cJSON_AddStringToObject(subnet, "segment", segment); + memcpy(netmask,startMASK+8,endMASK-startMASK-9); + cJSON_AddStringToObject(subnet, "netmask", netmask); + } + char *startRANGE = strstr(line, "range"); + char *endRANGE = strstr(line, ";"); + if((startRANGE!=NULL) && (endRANGE!=NULL)){ + memcpy(range,startRANGE+6,endRANGE-startRANGE-6); + cJSON_AddStringToObject(subnet, "range", range); + } + char *startDNS = strstr(line, "option domain-name-servers"); + char *endDNS = strstr(line, ";"); + if((startDNS!=NULL) && (endDNS!=NULL)){ + memcpy(dns,startDNS+27,endDNS-startDNS-27); + cJSON_AddStringToObject(subnet, "domain-name-servers", dns); + } + char *startSUNMASK = strstr(line, "option subnet-mask"); + char *endSUBMASK = strstr(line, ";"); + if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){ + memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19); + cJSON_AddStringToObject(subnet, "subnet-mask", submask); + } + char *startROUTERS = strstr(line, "option routers"); + char *endROUTERS = strstr(line, ";"); + if((startROUTERS!=NULL) && (endROUTERS!=NULL)){ + memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15); + cJSON_AddStringToObject(subnet, "routers", routers); + } + char *startLEASE = strstr(line, "max-lease-time"); + char *endLEASE = strstr(line, ";"); + if((startLEASE!=NULL) && (endLEASE!=NULL)){ + memcpy(lease,startLEASE+15,endLEASE-startLEASE-15); + cJSON_AddStringToObject(subnet, "max-lease-time", lease); + } + char *end = strstr(line, "}"); + if(end && strcmp(name, "")){ + count++; + if(count == 2){ + count = 0; + cJSON_AddItemToArray(arr, json); + //printf("=====================\n"); + memset(name, 0, 100); + } + } + } + if(name != NULL){ + memset(name, 0, 100); + free(name); + } + if(range != NULL){ + memset(range, 0, 40); + free(range); + } + if(dns != NULL){ + memset(dns, 0, 40); + free(dns); + } + if(submask != NULL){ + memset(submask, 0, 20); + free(submask); + } + if(routers != NULL){ + memset(routers, 0, 20); + free(routers); + } + if(lease != NULL){; + memset(lease, 0, 1000); + free(lease); + } + if(segment != NULL){ + memset(segment, 0, 20); + free(segment); + } + if(netmask != NULL){ + memset(netmask, 0, 20); + free(netmask); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; + +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c new file mode 100644 index 000000000..e032b4bfc --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c @@ -0,0 +1,452 @@ +#include "dhcp_subnet_config.h" + +ret_code dhcp_subnet_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate) { + *config_type = CM_CONFIG_SET; + } + return ret; +} + +ret_code dhcp_subnet_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *subnet=NULL; + char *segment=NULL, *mask=NULL, *router=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + subnet= cJSON_GetObjectItem(root, "subnet"); + if(NULL == subnet) + { + return RET_INPUTERR; + } + + count = cJSON_GetArraySize(subnet); + int len = 500; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(subnet, i); + if(NULL == one) + { + continue; + } + cJSON *interface = cJSON_GetObjectItem(one, "interface"); + if(NULL == interface){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + cJSON *range = cJSON_GetObjectItem(one, "range"); + //cJSON *routers = cJSON_GetObjectItem(one, "routers"); + cJSON *dns = cJSON_GetObjectItem(one, "domain-name-servers"); + cJSON *lease = cJSON_GetObjectItem(one, "max-lease-time"); + + //ݽӿڻȡipΡ롢ip ֮Է棬ڳʱͷ + segment = get_interface_subnet(interface->valuestring); + if(NULL == segment){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + mask = get_interface_mask(interface->valuestring); + if(NULL == mask){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + router = get_interface_ip(interface->valuestring); + if(NULL == router){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + //дļ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + //жǷѾ + if(file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface is already configured\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL == range){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check range + if(!check_range(range->valuestring, mask,segment)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(lease && !check_lease(lease->valuestring)){ + ret = RET_INPUTERR; + //system("echo '}' >> /etc/dhcp/dhcpd.conf"); + goto INPUT_ERROR; + } + if(dns && !check_dns(dns->valuestring)){ + ret = RET_INPUTERR; + //system("echo '}' >> /etc/dhcp/dhcpd.conf"); + goto INPUT_ERROR; + } + /* + subnet 10.0.7.0 netmask 255.255.255.0 { + range 10.0.7.1 10.0.7.10; + option domain-name-servers 202.206.192.33; + option subnet-mask 255.255.255.0; + option routers 10.0.7.1; + max-lease-time 7200; + } + */ + + //ӽӿ + snprintf(cmd, len, "sed -r -i 's/INTERFACESv4=\"/INTERFACESv4=\"%s /g' /etc/default/isc-dhcp-server", interface->valuestring); + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'subnet %s netmask %s {\\n range %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf", + segment, mask, range->valuestring, mask, router); + system(cmd); + if(NULL != lease){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring); + system(cmd); + } + if(NULL != dns){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' option domain-name-servers %s;' >> /etc/dhcp/dhcpd.conf", dns->valuestring); + system(cmd); + } + system("echo '}' >> /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "del")){ + if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface does not exist\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + snprintf(cmd, len, "sed -r -i 's/%s //g' /etc/default/isc-dhcp-server", interface->valuestring); + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", segment, mask); + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + //system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")){ + if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface does not exist\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + snprintf(cmd, len, "grep -Pzo 'subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment, mask); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + //printf("configuration do not exist\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != range) + { + if(!check_range(range->valuestring, mask,segment)){ + system("rm -rf /tmp/subconf.conf"); + goto INPUT_ERROR; + } + if(file_consist_str("/tmp/subconf.conf",range->valuestring)){ + //printf("range already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring); + system(cmd); + } + if(NULL != dns) + { + if(!check_dns(dns->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strdns = (char *)malloc(80); + memset(strdns, 0, 80); + snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring); + if(file_consist_str("/tmp/subconf.conf",strdns)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring); + system(cmd); + } + /* + if(NULL != subnetMask) + { + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+subnet-mask[ \\t0-9\\.]+;/option subnet-mask %s;/g' /tmp/subconf.conf", subnetMask->valuestring); + system(cmd); + } + if(NULL != routers) + { + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring); + system(cmd); + }*/ + if(NULL != lease) + { + if(!check_lease(lease->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strlease = (char *)malloc(1000); + memset(strlease, 0, 1000); + snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring); + if(file_consist_str("/tmp/subconf.conf",strlease)){ + //printf("lease already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + /*lease 0ǻָĬϣҪɾļеmax-lease-time*/ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf"); + system(cmd); + system("sed -r -i '/#/d' /tmp/subconf.conf"); + if(strcmp(lease->valuestring, "0")){ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*\\}/ max-lease-time %s;\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //Եʱһ滻\\} + system(cmd); + } + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", segment, mask, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + } + } + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + if(segment){ + free(segment); + } + if(mask){ + free(mask); + } + if(router){ + free(router); + } + //ret = RET_INPUTERR; + return ret; +} + +ret_code dhcp_subnet_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *interface=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + interface = cJSON_GetObjectItem(root, "interface"); + if(NULL == interface) + { + return RET_INPUTERR; + } + //ȡӿκ + char *segment = get_interface_subnet(interface->valuestring); + if(NULL == segment){ + return RET_INPUTERR; + } + char *mask = get_interface_mask(interface->valuestring); + if(NULL == mask){ + return RET_INPUTERR; + } + + //ȡļ + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *subnet=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + subnet = (char *)malloc(20); + memset(subnet, 0, 20); + netmask = (char *)malloc(20); + memset(netmask, 0, 20); + range = (char *)malloc(40); + memset(range, 0, 40); + dns = (char *)malloc(40); + memset(dns, 0, 40); + submask = (char *)malloc(20); + memset(submask, 0, 20); + routers= (char *)malloc(20); + memset(routers, 0, 20); + lease = (char *)malloc(1000); + memset(lease, 0, 1000); + + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "segment", segment); + cJSON_AddStringToObject(root, "netmask", mask); + + while((fgets(line,1000,fp))!=NULL){ + char *startSUB = strstr(line, "subnet"); + char *startMASK = strstr(line, "netmask"); + char *endMASK = strstr(line, "{"); + if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){ + memcpy(subnet,startSUB+7,startMASK-startSUB-8); + memcpy(netmask,startMASK+8,endMASK-startMASK-9); + if(!strcmp(subnet, segment) && !strcmp(netmask, mask)){ + memset(range, 0, 40); + memset(dns, 0, 40); + memset(submask, 0, 20); + memset(routers, 0, 20); + memset(lease, 0, 1000); + } + } + if((subnet!= NULL) || (netmask!=NULL)){ + // + if(strcmp(subnet, segment) || strcmp(netmask, mask)){ + continue; + } + } + char *startRANGE = strstr(line, "range"); + char *endRANGE = strstr(line, ";"); + if((startRANGE!=NULL) && (endRANGE!=NULL)){ + memcpy(range,startRANGE+6,endRANGE-startRANGE-6); + } + char *startDNS = strstr(line, "option domain-name-servers"); + char *endDNS = strstr(line, ";"); + if((startDNS!=NULL) && (endDNS!=NULL)){ + memcpy(dns,startDNS+27,endDNS-startDNS-27); + } + char *startSUNMASK = strstr(line, "option subnet-mask"); + char *endSUBMASK = strstr(line, ";"); + if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){ + memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19); + } + char *startROUTERS = strstr(line, "option routers"); + char *endROUTERS = strstr(line, ";"); + if((startROUTERS!=NULL) && (endROUTERS!=NULL)){ + memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15); + } + char *startLEASE = strstr(line, "max-lease-time"); + char *endLEASE = strstr(line, ";"); + if((startLEASE!=NULL) && (endLEASE!=NULL)){ + memcpy(lease,startLEASE+15,endLEASE-startLEASE-15); + } + } + if(range != NULL){ + cJSON_AddStringToObject(root, "range", range); + memset(range, 0, 40); + free(range); + } + if(dns != NULL){ + cJSON_AddStringToObject(root, "domain-name-servers", dns); + memset(dns, 0, 40); + free(dns); + } + if(submask != NULL){ + cJSON_AddStringToObject(root, "subnet-mask", submask); + memset(submask, 0, 20); + free(submask); + } + if(routers != NULL){ + cJSON_AddStringToObject(root, "routers", routers); + memset(routers, 0, 20); + free(routers); + } + if(lease != NULL){ + cJSON_AddStringToObject(root, "max-lease-time", lease); + memset(lease, 0, 1000); + free(lease); + } + if(subnet != NULL){ + memset(subnet, 0, 20); + free(subnet); + } + if(netmask != NULL){ + memset(netmask, 0, 20); + free(netmask); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} + diff --git a/Platform/user/configm/config-server/include/configm.h b/Platform/user/configm/config-server/include/configm.h index 6d0ce19a0..81bd290eb 100755 --- a/Platform/user/configm/config-server/include/configm.h +++ b/Platform/user/configm/config-server/include/configm.h @@ -10,6 +10,8 @@ #include "log_config.h" #include "../web_config/authfree.h" #include "../web_config/auth_parameters.h" +#include "natconfig.h" + #define CONFIG_INIT_ARRAY \ @@ -162,7 +164,16 @@ log_file_config_proc, \ NULL, \ NULL \ - }\ + },\ + {\ + NAT4_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + nat_config_chk, \ + nat_config_proc, \ + NULL, \ + nat_config_get_all \ + }\ } typedef ret_code (*cm_config_init)(); diff --git a/Platform/user/configm/config-server/include/dhcp_client_config.h b/Platform/user/configm/config-server/include/dhcp_client_config.h new file mode 100644 index 000000000..46f59c084 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_client_config.h @@ -0,0 +1,18 @@ +#ifndef DHCP_CLIENT_CONFIG_H_ +#define DHCP_CLIENT_CONFIG_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_client_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_client_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_client_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h b/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h new file mode 100644 index 000000000..d08619260 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h @@ -0,0 +1,13 @@ +#ifndef DHCP_DHCPD_LEASE_H_ +#define DHCP_DHCPD_LEASE_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len); + +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_host_config.h b/Platform/user/configm/config-server/include/dhcp_host_config.h new file mode 100644 index 000000000..4ee9aaaf7 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_host_config.h @@ -0,0 +1,26 @@ +#ifndef DHCP_HOST_CONFIG_H_ +#define DHCP_HOST_CONFIG_H_ +#include "dhcp_lib.h" + + + +/* +struct host { + char *host_name; + char *hardware_ethernet; + char *fixed_address; +}*/ + +ret_code dhcp_host_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_host_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_host_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +#endif diff --git a/Platform/user/configm/config-server/include/dhcp_lib.h b/Platform/user/configm/config-server/include/dhcp_lib.h new file mode 100644 index 000000000..8ee1c12a9 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_lib.h @@ -0,0 +1,62 @@ +#ifndef DHCP_LIB_H_ +#define DHCP_LIB_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "rpc_common.h" +#include +#include "ipconfig.h" + +#include "configm.h" +#include "rpc.h" +#include "netconfig.h" + +char *getfileall(char *fname); + +char *getfilefirstline(char *fname); + + +char *getfileall_with_linefeed(char *fname); + +int check_name(char *name); + +int check_mac(char *mac); + +int file_consist_str(char *fname, char *str); + +int check_range(char *range, char *mask, char *subnet); + +int check_dns(char *dns); + +int check_lease(char *lease); + +int check_ip(char *ip); + + +char *get_interface_subnet(char *interface); + +char *get_interface_ip(char *interface) ; + +char *get_interface_mask(char *interface); + +int check_servers(char *servers); + + +int check_segment(char *segment, char *mask); + +int check_mask(char *mask); + + + +#endif + + diff --git a/Platform/user/configm/config-server/include/dhcp_relay_config.h b/Platform/user/configm/config-server/include/dhcp_relay_config.h new file mode 100644 index 000000000..7eec755df --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_relay_config.h @@ -0,0 +1,17 @@ +#ifndef DHCP_RELAY_CONFIG_H_ +#define DHCP_RELAY_CONFIG_H_ +#include "dhcp_lib.h" + + + + +ret_code dhcp_relay_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_relay_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + + +#endif diff --git a/Platform/user/configm/config-server/include/dhcp_shared_network_config.h b/Platform/user/configm/config-server/include/dhcp_shared_network_config.h new file mode 100644 index 000000000..780b67c9b --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_shared_network_config.h @@ -0,0 +1,17 @@ +#ifndef DHCP_SHARED_NETWORK_CONFIG_H_ +#define DHCP_SHARED_NETWORK_CONFIG_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_shared_network_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_shared_network_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len); + +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_subnet_config.h b/Platform/user/configm/config-server/include/dhcp_subnet_config.h new file mode 100644 index 000000000..3f65d6ca1 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_subnet_config.h @@ -0,0 +1,22 @@ +#ifndef DHCP_SUBNET_CONFIG_H_ +#define DHCP_SUBNET_CONFIG_H_ +#include "dhcp_lib.h" + + + +ret_code dhcp_subnet_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_subnet_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_subnet_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + + +#endif + + diff --git a/Platform/user/configm/config-server/include/natconfig.h b/Platform/user/configm/config-server/include/natconfig.h new file mode 100644 index 000000000..b434971cc --- /dev/null +++ b/Platform/user/configm/config-server/include/natconfig.h @@ -0,0 +1,23 @@ +#ifndef _NAT_CONFIG_H +#define _NAT_CONFIG_H + +#include + +#include "rpc_types.h" +#include "rpc_common.h" + +ret_code nat_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code nat_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code nat_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code nat_config_get_all(uint source, pointer output, int *output_len); + +#endif \ No newline at end of file diff --git a/Platform/user/configm/config-server/nat_config/0x0000000800000001 b/Platform/user/configm/config-server/nat_config/0x0000000800000001 new file mode 100644 index 000000000..9aa3baa12 --- /dev/null +++ b/Platform/user/configm/config-server/nat_config/0x0000000800000001 @@ -0,0 +1 @@ +{"config_type":1,"-t":"nat","action":"-A","chain":"POSTROUTING","-i":"ens33","-s":"192.168.12.12","-j":"DNAT","--to-destionation":"172.18.2.72"} \ No newline at end of file diff --git a/Platform/user/configm/config-server/nat_config/natconfig.c b/Platform/user/configm/config-server/nat_config/natconfig.c index b6ba11db0..784853031 100644 --- a/Platform/user/configm/config-server/nat_config/natconfig.c +++ b/Platform/user/configm/config-server/nat_config/natconfig.c @@ -1,610 +1,40 @@ -#include "configm.h" -#include "ipconfig.h" -#include "rpc.h" -#include "parsefile.h" - -uchar ip_masklen(struct in_addr netmask) -{ - uint tmp = ~ntohl(netmask.s_addr); - if (tmp) - /* clz: count leading zeroes. sadly, the behaviour of this - * builtin - * is undefined for a 0 argument, even though most CPUs give 32 - */ - return __builtin_clz(tmp); - else - return 32; -} - - -/* Convert masklen into IP address's netmask (network byte order). */ -void masklen2ip(const int masklen, struct in_addr *netmask) -{ - if(masklen < 0 || masklen > IPV4_MAX_BITLEN) - { - return; - } - - /* left shift is only defined for less than the size of the type. - * we unconditionally use long long in case the target platform - * has defined behaviour for << 32 (or has a 64-bit left shift) */ - - if (sizeof(unsigned long long) > 4) - netmask->s_addr = htonl(0xffffffffULL << (32 - masklen)); - else - netmask->s_addr = - htonl(masklen ? 0xffffffffU << (32 - masklen) : 0); -} - -void ip_save_file(ip_config_t *ip_conf, uint config_type) -{ - char *addr_name = "address"; - char *mask_name = "netmask"; - struct in_addr netmask; - char addr_buff[IF_BUFF_LEN] = {0}; - char mask_buff[IF_BUFF_LEN] = {0}; - - if(config_type == CM_CONFIG_SET) - { - sprintf(addr_buff, "address %s\n", inet_ntoa(ip_conf->prefix)); - masklen2ip(ip_conf->prefixlen, &netmask); - sprintf(mask_buff, "netmask %s\n", inet_ntoa(netmask)); - rpc_log_info("%s %s",addr_buff, mask_buff); - - ip_conf_file_set(ip_conf->ifname, addr_name, addr_buff); - ip_conf_file_set(ip_conf->ifname, mask_name, mask_buff); - } - else if(config_type == CM_CONFIG_DEL) - { - ip_conf_file_del(ip_conf->ifname, addr_name); - ip_conf_file_del(ip_conf->ifname, mask_name); - } -} - -/* call ioctl system call */ -ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret) -{ - int sock; - int err = 0; - - *ret = 0; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - { - rpc_log_error("Cannot create UDP socket"); - return RET_SYSERR; - } - if ((*ret = ioctl(sock, request, ifreq)) < 0) - { - err = errno; - rpc_log_error("Ioctl error: %s\n", strerror(errno)); - - } - - close(sock); - - if (*ret < 0) - { - errno = err; - *ret = err; - return RET_SYSERR; - } - - return RET_OK; -} - - -ret_code ip_config_json_parse(pointer input, uint *conf_type, ip_config_t *config_buff) -{ - //ip_config_string_t *ip_config; - cJSON *json_obj; - - json_obj = cJSON_Parse(input); - if(!json_obj) - { - return RET_INPUTERR; - } - - rpc_log_info("json input:\n %s \n", cJSON_Print(json_obj)); - - s2j_create_struct_obj(ip_config, ip_config_string_t); - - if(ip_config == NULL) - { - cJSON_Delete(json_obj); - return RET_NOMEM; - } - - s2j_struct_get_basic_element(ip_config, json_obj, int, config_type); - S2J_STRUCT_GET_STRING_ELEMENT_N(ip_config, json_obj, ifname, INTERFACE_NAMSIZ); - s2j_struct_get_basic_element(ip_config, json_obj, int, family); - S2J_STRUCT_GET_STRING_ELEMENT_N(ip_config, json_obj, ipaddr, DOT_IP_STR); - s2j_struct_get_basic_element(ip_config, json_obj, int, prefixlen); - - strncpy(config_buff->ifname, ip_config->ifname, INTERFACE_NAMSIZ - 1); - - config_buff->family = (uchar)ip_config->family; - config_buff->prefixlen = (uchar)ip_config->prefixlen; - config_buff->prefix.s_addr = inet_addr(ip_config->ipaddr); - - *conf_type = ip_config->config_type; - - s2j_delete_struct_obj(ip_config); - cJSON_Delete(json_obj); - - return RET_OK; -} - -ret_code ip_config_json_parse_array(pointer input, uint *conf_type, - ip_config_t *config_buff, int *cnt) -{ - //ip_config_string_t *ip_config; - cJSON *json_obj; - cJSON* pArrayItem; - int iCount = 0, i = 0; - - json_obj = cJSON_Parse(input); - if(!json_obj) - { - return RET_INPUTERR; - } - - rpc_log_info("json input:%s \n", cJSON_Print(json_obj)); - - iCount = cJSON_GetArraySize(json_obj); /*ȡ鳤*/ - - config_buff = rpc_new0(ip_config_t, iCount); - conf_type = rpc_new0(uint, iCount); - if(!config_buff || !conf_type) - { - return RET_NOMEM; - } - - s2j_create_struct_obj(ip_config, ip_config_string_t); - if(ip_config == NULL) - { - cJSON_Delete(json_obj); - rpc_free(config_buff); - return RET_NOMEM; - } - - *cnt = 0; - for(i = 0; i < iCount; i++) - { - pArrayItem = cJSON_GetArrayItem(json_obj, i); - if(pArrayItem) - { - memset(ip_config, 0, sizeof(ip_config_string_t)); - - s2j_struct_get_basic_element(ip_config, json_obj, int, config_type); - s2j_struct_get_basic_element(ip_config, json_obj, string, ifname); - s2j_struct_get_basic_element(ip_config, json_obj, int, family); - s2j_struct_get_basic_element(ip_config, json_obj, string, ipaddr); - s2j_struct_get_basic_element(ip_config, json_obj, int, prefixlen); - - strncpy(config_buff[*cnt].ifname, ip_config->ifname, INTERFACE_NAMSIZ - 1); - config_buff[*cnt].family = (uchar)ip_config->family; - config_buff[*cnt].prefixlen = (uchar)ip_config->prefixlen; - config_buff[*cnt].prefix.s_addr = inet_addr(ip_config->ipaddr); - conf_type[*cnt] = ip_config->config_type; - - (*cnt)++; - } - } - - s2j_delete_struct_obj(ip_config); - cJSON_Delete(json_obj); - - return RET_OK; -} - - -ret_code ip_config_format_json(int config_type, ip_config_t *config_buff, - pointer output, int *outlen) -{ - ip_config_string_t tem_buff = {0}; - ip_config_string_t *ip_config = &tem_buff; - //cJSON *json_obj = NULL; - char *json_ip; - - ip_config->config_type = config_type; - ip_config->family = AF_INET; - ip_config->prefixlen = config_buff->prefixlen; - - strncpy(ip_config->ifname, config_buff->ifname, INTERFACE_NAMSIZ - 1); - strncpy(ip_config->ipaddr, inet_ntoa(config_buff->prefix), DOT_IP_STR - 1); - - /* create Student JSON object */ - s2j_create_json_obj(json_obj); - if(json_obj == NULL) - { - return RET_NOMEM; - } - - s2j_json_set_basic_element(json_obj, ip_config, int, config_type); - s2j_json_set_basic_element(json_obj, ip_config, string, ifname); - s2j_json_set_basic_element(json_obj, ip_config, int, family); - s2j_json_set_basic_element(json_obj, ip_config, string, ipaddr); - s2j_json_set_basic_element(json_obj, ip_config, int, prefixlen); - - json_ip = cJSON_PrintUnformatted(json_obj); - *outlen = strlen(json_ip) + 1; - memcpy(output, json_ip, *outlen); - - free(json_ip); - cJSON_Delete(json_obj); - - return RET_OK; -} - -ret_code ip_config_format_json_array(int config_type, ip_config_t *config_buff, - int count, pointer output, int *outlen) -{ - ip_config_string_t tem_buff = {0}; - ip_config_string_t *ip_config = &tem_buff; - cJSON *json_array = NULL; - //cJSON *json_obj = NULL; - char *json_ip; - int i; - - json_array = cJSON_CreateArray(); - if(json_array == NULL) - { - return RET_NOMEM; - } - - for(i = 0; i < count; i++) - { - memset(ip_config, 0, sizeof(ip_config_string_t)); - - ip_config->config_type = config_type; - ip_config->family = AF_INET; - ip_config->prefixlen = config_buff[i].prefixlen; - strncpy(ip_config->ifname, config_buff[i].ifname, INTERFACE_NAMSIZ - 1); - strncpy(ip_config->ipaddr, inet_ntoa(config_buff[i].prefix), DOT_IP_STR - 1); - - /* create Student JSON object */ - s2j_create_json_obj(json_obj); - if(json_obj == NULL) - { - return RET_NOMEM; - } - - cJSON_AddItemToArray(json_array, json_obj); - - s2j_json_set_basic_element(json_obj, ip_config, int, config_type); - s2j_json_set_basic_element(json_obj, ip_config, string, ifname); - s2j_json_set_basic_element(json_obj, ip_config, int, family); - s2j_json_set_basic_element(json_obj, ip_config, string, ipaddr); - s2j_json_set_basic_element(json_obj, ip_config, int, prefixlen); - } - - json_ip = cJSON_PrintUnformatted(json_array); - *outlen = strlen(json_ip) + 1; - memcpy(output, json_ip, *outlen); - - free(json_ip); - cJSON_Delete(json_array); - - return RET_OK; -} - - -ret_code if_set_prefix(ip_config_t *ip_conf, int *code) -{ - ret_code ret; - struct ifreq ifreq = {0}; - struct sockaddr_in addr; - struct sockaddr_in mask; - - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name) - 1); - addr.sin_addr = ip_conf->prefix; - addr.sin_family = ip_conf->family; - memcpy(&ifreq.ifr_addr, &addr, sizeof(struct sockaddr_in)); - - ret = if_ioctl(SIOCSIFADDR, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - - if(ip_conf->prefix.s_addr != 0) - { - masklen2ip(ip_conf->prefixlen, &mask.sin_addr); - mask.sin_family = ip_conf->family; - memcpy(&ifreq.ifr_netmask, &mask, sizeof(struct sockaddr_in)); - - ret = if_ioctl(SIOCSIFNETMASK, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - } - - - return 0; -} - -ret_code if_get_prefix_all(pointer output, int *output_len, int *code) -{ - struct ifreq ifreq[MAX_IF_NUM]; - struct sockaddr_in *addr; - struct ifreq netmask; - ip_config_t *ip_conf; - struct ifconf ifc; - int if_count = 0; - ret_code ret; - int mask_ret; - int i; - - memset(&ifc, 0, sizeof(struct ifconf)); - memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq)); - - ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq); - ifc.ifc_buf = (char *)ifreq; - - ret = if_ioctl(SIOCGIFCONF, (caddr_t)&ifc, code); - ASSERT_RET(ret); - - if_count = ifc.ifc_len / (sizeof(struct ifreq)); - - rpc_log_info("if num is %d\n", if_count); - - if((if_count * sizeof(ip_config_t) > CM_BUFF_SIZE) - || (if_count == 0)) - { - ret = RET_NOMEM; - ASSERT_RET(ret); - } - - ip_conf = rpc_new(ip_config_t, MAX_IF_NUM); - if(ip_conf == NULL) - { - return RET_NOMEM; - } - memset(ip_conf, 0, MAX_IF_NUM * sizeof(ip_config_t)); - - for(i = 0; i < if_count; i++) - { - rpc_log_info("get interface %s info\n", ifreq[i].ifr_name); - strncpy(ip_conf[i].ifname, ifreq[i].ifr_name, INTERFACE_NAMSIZ - 1); - ip_conf[i].family = AF_INET; - ip_conf[i].prefix = ((struct sockaddr_in *)&(ifreq[i].ifr_addr))->sin_addr; - - memset(&netmask, 0, sizeof(netmask)); - strncpy(netmask.ifr_name, ifreq[i].ifr_name, sizeof(netmask.ifr_name) - 1); - - ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&netmask, &mask_ret); - ASSERT_RET_NO(ret); - - addr = ( struct sockaddr_in * )&(netmask.ifr_netmask ); - ip_conf[i].prefixlen = ip_masklen(addr->sin_addr); - } - - ip_config_format_json_array(CM_CONFIG_GET, ip_conf, if_count, output, output_len); - - rpc_free(ip_conf); - return RET_OK; -} - -ret_code nat_get_prefix(ip_config_t *ip_conf, int *code) -{ - struct sockaddr_in *addr; - struct ifreq netmask; - struct ifreq ifreq; - ret_code ret = RET_OK; - int mask_ret; - int i; - - if(ip_conf->family != AF_INET) - { - ret = RET_INPUTERR; - } - - ASSERT_RET(ret); - - memset(&ifreq, 0, sizeof(struct ifreq)); - - rpc_log_info("get interface %s info\n", ip_conf->ifname); - - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name) - 1); - - ret = if_ioctl(SIOCGIFADDR, (caddr_t)&ifreq, code); - ASSERT_RET(ret); - - - ip_conf->prefix = ((struct sockaddr_in *)&(ifreq.ifr_addr))->sin_addr; - - memset(&ifreq, 0, sizeof(ifreq)); - strncpy(ifreq.ifr_name, ip_conf->ifname, sizeof(ifreq.ifr_name) - 1); - - ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&ifreq, &mask_ret); - ASSERT_RET_NO(ret); - - addr = ( struct sockaddr_in * )&(ifreq.ifr_netmask); - ip_conf->prefixlen = ip_masklen(addr->sin_addr); - - return ret; -} - -ret_code nat_config_set_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - ip_config_t *ip_conf; - - ip_conf = (ip_config_t *)input; - - if(input_len < sizeof(ip_config_t) - || strlen(ip_conf->ifname) == 0) - { - ret = RET_INPUTERR; - } - - if (config_type != CM_CONFIG_DEL && ipv4_martian(&ip_conf->prefix)) - { - ret = RET_IPINVALID; - } - - - ASSERT_RET(ret); - - if(ip_conf->prefixlen == 0 || ip_conf->prefixlen > IPV4_MAX_PREFIXLEN) - { - ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; - } - - return RET_OK; -} - -ret_code nat_config_get_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - ip_config_t *ip_conf; - - ip_conf = (ip_config_t *)input; - - if(input_len < sizeof(ip_config_t) - || strlen(ip_conf->ifname) == 0 ) - { - ret = RET_INPUTERR; - } - - return ret; -} - -ret_code nat_config_getall_chk(uint source,uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - ret_code ret = RET_OK; - - if(*output_len < MAX_IF_NUM * sizeof(ip_config_t)) - { - ret = RET_INPUTERR; - } - - return ret; -} - - -ret_code nat_config_chk(uint source,uint *config_type, - pointer input, int *input_len, - pointer output, int *output_len) - -{ - int config_len = sizeof(ip_config_t); - uint conf_type = CM_CONFIG_GET; - ip_config_t ip_config = {0}; - ret_code ret = RET_OK; - int code = 0; - - ip_config_json_parse(input, &conf_type, &ip_config); - - switch (conf_type) - { - case CM_CONFIG_SET: - case CM_CONFIG_DEL: - ret = ip_config_set_chk(source, conf_type, - &ip_config, config_len, - output, output_len); - break; - case CM_CONFIG_GET: - ret = ip_config_get_chk(source, conf_type, - &ip_config, config_len, - output, output_len); - break; - case CM_CONFIG_GET_ALL: - ret = ip_config_getall_chk(source, conf_type, - &ip_config, config_len, - output, output_len); - break; - default: - ret = RET_NOTSUPPORT; - } - - /* 将传入的json数据转换成结构体,便于后续处理 */ - if(config_len <= CM_BUFF_SIZE) - { - memset(input, 0, *input_len); - memcpy(input, &ip_config, config_len); - *config_type = conf_type; - *input_len = config_len; - } - else - { - ret = RET_NOMEM; - } - - RET_ERR_FORMART(ret, code, output, *output_len); - - return ret; -} - -ret_code nat_config_proc(uint source, uint config_type, - pointer input, int input_len, - pointer output, int *output_len) -{ - uint conf_type = config_type; - ip_config_t conf_buff = {0}; - ip_config_t *ip_conf = &conf_buff; - ret_code ret = RET_OK; - int code; - - ip_conf = (ip_config_t *)input; - - if(conf_type == CM_CONFIG_DEL) - { - ip_conf->prefix.s_addr = 0; - ip_conf->prefixlen = IPV4_DEFAULT_PREFIXLEN; - } - - rpc_log_info("config type is %d, if %s ip %s prefixlen %d\n", - conf_type, ip_conf->ifname, - inet_ntoa(ip_conf->prefix), - ip_conf->prefixlen); - - ret = if_set_prefix(ip_conf, &code); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - ip_save_file(ip_conf, conf_type); - - return RET_OK; -} - -ret_code nat_config_get(uint source, - pointer input, int input_len, - pointer output, int *output_len) -{ - ip_config_t *ip_conf; - ret_code ret = RET_OK; - int code, conf_type; - - ip_conf = (ip_config_t *)input; - - ret = if_get_prefix(ip_conf, &code); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - ip_config_format_json(CM_CONFIG_GET, ip_conf, output, output_len); - - return ret; -} - -ret_code nat_config_get_all(uint source, pointer output, int *output_len) -{ - ret_code ret = RET_OK; - int code = 0; - - *output_len = 0; - ret = if_get_prefix_all(output, output_len, &code); - - rpc_log_info("ip_config_get_all: %s\n", output); - - RET_ERR_FORMART(ret, code, output, *output_len); - ASSERT_RET(ret); - - return RET_OK; - -} +#include "configm.h" +#include "rpc.h" +#include "parsefile.h" + + + +ret_code nat_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) + +{ + return RET_OK; +} + +ret_code nat_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + + + return RET_OK; +} + +ret_code nat_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + + return ret; +} + + +ret_code nat_config_get_all(uint source, pointer output, int *output_len) +{ + + return RET_OK; + +} + diff --git a/Platform/user/configm/config-server/user_manager_config/user_account_config.c b/Platform/user/configm/config-server/user_manager_config/user_account_config.c index 54afa520d..d52a4f410 100644 --- a/Platform/user/configm/config-server/user_manager_config/user_account_config.c +++ b/Platform/user/configm/config-server/user_manager_config/user_account_config.c @@ -21,6 +21,7 @@ ret_code user_config_chk(uint source, uint *config_type, ret_code ret = RET_OK; cJSON *root, *type_json, *data_json; int fun_type; + //*config_type = CM_CONFIG_SET; if(NULL == input || NULL == input_len || NULL == output || NULL == output_len) { @@ -98,7 +99,7 @@ static ret_code user_config_get_detail_proc(uint source, uint config_type, ret_code ret = RET_OK; char * data_detail = NULL; char * ret_char = NULL; - int group_uuid; + int group_uuid, temp_output_len; /* 解析json串 */ cJSON * root = cJSON_Parse(input); @@ -137,15 +138,16 @@ static ret_code user_config_get_detail_proc(uint source, uint config_type, cJSON_AddStringToObject(ret_root, "data", ""); ret_char = cJSON_PrintUnformatted(ret_root); - memcpy(output, ret_char, strlen(ret_char)+1); - *output_len = strlen(ret_char)+1; + temp_output_len = strlen(ret_char)+1; + memcpy(output, ret_char, temp_output_len); + *output_len = temp_output_len; UCHAR_FREE(data_detail); //释放函数内部申请的内存 UCHAR_FREE(ret_char); cJSON_Delete(ret_root); return ret; } - + /* 查询成功 */ cJSON * list_obj = cJSON_Parse(data_detail); if (NULL == list_obj) @@ -172,8 +174,15 @@ static ret_code user_config_get_detail_proc(uint source, uint config_type, cJSON_AddStringToObject(ret_root, "message", "查询成功"); ret_char = cJSON_PrintUnformatted(ret_root); - memcpy(output, ret_char, strlen(ret_char)+1); - *output_len = strlen(ret_char)+1; + temp_output_len = strlen(ret_char)+1; + if (CM_BUFF_SIZE < temp_output_len) + { + ret = RET_ERR; + }else + { + memcpy(output, ret_char, temp_output_len); + *output_len = temp_output_len; + } cJSON_DetachItemFromObjectCaseSensitive(list_obj,"data"); cJSON_DetachItemFromObjectCaseSensitive(ret_root,"data"); @@ -298,7 +307,7 @@ static ret_code user_config_get_list_proc(uint source, uint config_type, ret_code ret = RET_OK; char * data_list = NULL; char * ret_char = NULL; - int group_uuid; + int group_uuid, temp_output_len; /* 解析json串 */ cJSON * root = cJSON_Parse(input); @@ -396,8 +405,15 @@ static ret_code user_config_get_list_proc(uint source, uint config_type, cJSON_AddStringToObject(ret_root, "message", "查询成功"); ret_char = cJSON_PrintUnformatted(ret_root); - memcpy(output, ret_char, strlen(ret_char)+1); - *output_len = strlen(ret_char)+1; + temp_output_len = strlen(ret_char)+1; + if (CM_BUFF_SIZE < temp_output_len) + { + ret = RET_ERR; + }else + { + memcpy(output, ret_char, strlen(ret_char)+1); + *output_len = strlen(ret_char)+1; + } cJSON_DetachItemFromObjectCaseSensitive(list_obj,"data"); cJSON_DetachItemFromObjectCaseSensitive(ret_root,"data"); @@ -445,7 +461,7 @@ static ret_code user_config_add_proc(uint source, uint config_type, USERLIST *user_account; char *ret_char = NULL; char *temp_begin_time, *temp_end_time, *temp_udesp, *temp_pwd; - int temp_multi, temp_valid; + int temp_multi, temp_valid, temp_output_len; char *message[] = {"新增成功","用户组名不存在","用户名长度不符合","用户名中包含特殊字符","用户名重名","用户数已达到最大(最大用户数100)","数据库操作失败","系统错误"}; /* 解析json串 */ @@ -540,24 +556,31 @@ static ret_code user_config_add_proc(uint source, uint config_type, /* 执行操作 */ usermanager_add_user(uname->valuestring, gname->valuestring, temp_udesp, temp_pwd, temp_multi, temp_valid, temp_begin_time, temp_end_time, &add_user_ret); //新增用户 + cJSON_Delete(root); /* 处理返回结果 */ res = cJSON_CreateObject(); if(!res) { - ret = RET_ERR; - // ASSERT_RET(ret); + ret = RET_SYSERR; return ret; } cJSON_AddNumberToObject(res, "retcode", add_user_ret.result); cJSON_AddStringToObject(res, "message", message[add_user_ret.result]); cJSON_AddStringToObject(res, "data", ""); ret_char = cJSON_PrintUnformatted(res); - memcpy(output, ret_char, strlen(ret_char)+1); - *output_len = strlen(ret_char)+1; + temp_output_len = strlen(ret_char)+1; + + if (CM_BUFF_SIZE < temp_output_len) + { + ret = RET_ERR; + }else + { + memcpy(output, ret_char, temp_output_len); + *output_len = temp_output_len; + } /* 释放内存 */ - cJSON_Delete(root); cJSON_Delete(res); UCHAR_FREE(ret_char); @@ -687,7 +710,8 @@ static ret_code user_config_del_proc(uint source, uint config_type, * "GID": 3, * "uname": "xxx", * "gname": "xxx", -* "resetpwd": 1, +* "resetpwd": 1, //0表示重置密码,1表示不重置密码 +* "passwd": "A31as%4Gb", //默认密码 * "udescription": "xxx", * "multi": 0, * "valid": 0, @@ -699,9 +723,12 @@ static ret_code user_config_del_proc(uint source, uint config_type, * multi, 0:允许多用户登陆、1:只允许单个用户登陆 * valid, 0:永久有效、1:在有效期内有效 * -* output格式:{"result":true} -* true:成功 -* false:失败 +* output格式: +{ + "retcode": 0, + "message": "修改成功", + "data": "" //空 +} */ static ret_code user_config_mod_web_proc(uint source, uint config_type, pointer input, int input_len, @@ -728,7 +755,7 @@ static ret_code user_config_mod_web_proc(uint source, uint config_type, s2j_struct_get_basic_element(input_data, data, int, GID); S2J_STRUCT_GET_STRING_ELEMENT_N(input_data, data, uname,UNAMESIZE); S2J_STRUCT_GET_STRING_ELEMENT_N(input_data, data, gname, GNAMESIZE); - //S2J_STRUCT_GET_STRING_ELEMENT_N(input_data, data, passwd, UPWDSIZE); + S2J_STRUCT_GET_STRING_ELEMENT_N(input_data, data, passwd, UPWDSIZE); S2J_STRUCT_GET_STRING_ELEMENT_N(input_data, data, udescription, UDESIZE); s2j_struct_get_basic_element(input_data, data, int, resetpwd); s2j_struct_get_basic_element(input_data, data, int, multi); @@ -738,6 +765,8 @@ static ret_code user_config_mod_web_proc(uint source, uint config_type, /* 执行操作 */ ret_mod_web = mod_user_web(input_data); + cJSON_Delete(root); + s2j_delete_struct_obj(input_data); /* 处理返回结果 */ ret_json = cJSON_CreateObject(); @@ -747,14 +776,23 @@ static ret_code user_config_mod_web_proc(uint source, uint config_type, //ASSERT_RET(ret); return ret; } - cJSON_AddBoolToObject(ret_json, "result", ret_mod_web); + + if(ret_mod_web) + { + cJSON_AddNumberToObject(ret_json, "retcode", 0); + cJSON_AddStringToObject(ret_json, "message", "修改成功"); + }else + { + cJSON_AddNumberToObject(ret_json, "retcode", 1); + cJSON_AddStringToObject(ret_json, "message", "修改失败"); + } + cJSON_AddStringToObject(ret_json, "data", ""); + ret_char = cJSON_PrintUnformatted(ret_json); memcpy(output, ret_char, strlen(ret_char)+1); - - /* 释放内存 */ - cJSON_Delete(root); + *output_len = strlen(ret_char)+1; + cJSON_Delete(ret_json); - s2j_delete_struct_obj(input_data); UCHAR_FREE(ret_char); return ret; } @@ -847,17 +885,26 @@ static ret_code user_config_mod_line_proc(uint source, uint config_type, /* * 修改用户-移动分组 * iuput格式: -* { -* "type": 6, -* "data": { -* "user_id": 1, -* "group_id": 3 -* } -* } +{ + "type": 6, //6表示移动分组 + "data": { + "user_id": [1, 2, 3], + "batch": { //批量全选 + "all": 0, //0:全选、1:不是全选 + "old_group_id": 2 //全选时的用户组id,0是表示 所有用户组 + }, + "new_group_id": 2 + } +} + * -* output格式:{"result":true} -* true:成功 -* false:失败 +* output格式: +{ + "retcode": 0, + "message": "移动成功", + "data": "" //空 +} + */ static ret_code user_config_move_group_proc(uint source, uint config_type, pointer input, int input_len, @@ -865,8 +912,10 @@ static ret_code user_config_move_group_proc(uint source, uint config_type, { ret_code ret = RET_OK; bool ret_move_group; - cJSON *root, *data, *userid_json, *groupid_json, *ret_json; + cJSON *root, *data, *userid_array_json, *userid_json, *batch_json, *batch_all_json, *old_groupid_json, *new_groupid_json, *ret_json; char *ret_char = NULL; + unsigned short temp_old_groupid = 0; + int id_num = 0; /* 解析json串 */ root = cJSON_Parse(input); @@ -878,40 +927,101 @@ static ret_code user_config_move_group_proc(uint source, uint config_type, cJSON_Delete(root); return ret; } - userid_json = cJSON_GetObjectItem(data, "user_id"); - if(!userid_json) + + new_groupid_json = cJSON_GetObjectItem(data, "new_group_id"); + if(!new_groupid_json) { ret = RET_INPUTERR; - //ASSERT_RET(ret); - cJSON_Delete(root); - return ret; - } - groupid_json = cJSON_GetObjectItem(data, "group_id"); - if(!groupid_json) - { - ret = RET_INPUTERR; - //ASSERT_RET(ret); cJSON_Delete(root); return ret; } - /* 执行操作 */ - ret_move_group = move_user_group(userid_json->valueint, groupid_json->valueint); + batch_json = cJSON_GetObjectItem(data, "batch"); + if(!batch_json) + { + ret = RET_INPUTERR; + cJSON_Delete(root); + return ret; + } + + batch_all_json = cJSON_GetObjectItem(batch_json, "all"); + if(!batch_all_json) + { + ret = RET_INPUTERR; + cJSON_Delete(root); + return ret; + } + + int i = 0; + int a[i]; + + switch (batch_all_json->valueint) + { + case 0: + old_groupid_json = cJSON_GetObjectItem(batch_json, "old_group_id"); + if(!old_groupid_json) + { + ret = RET_INPUTERR; + cJSON_Delete(root); + return ret; + } + temp_old_groupid = old_groupid_json->valueint; + ret_move_group = move_user_group(batch_all_json->valueint, new_groupid_json->valueint, temp_old_groupid, NULL, id_num); + break; + case 1: + userid_array_json = cJSON_GetObjectItem(data, "user_id"); + if(!userid_array_json) + { + ret = RET_INPUTERR; + cJSON_Delete(root); + return ret; + } + id_num = cJSON_GetArraySize(userid_array_json); + if(0 < id_num) + { + unsigned short user_ids[id_num]; + for(int i = 0; i < id_num; i++) + { + userid_json = cJSON_GetArrayItem(userid_array_json, i); + + user_ids[i] = userid_json->valueint; + } + ret_move_group = move_user_group(batch_all_json->valueint, new_groupid_json->valueint, temp_old_groupid, user_ids, id_num); + } + break; + default: + ret = RET_INPUTERR; + return ret; + break; + } + + cJSON_Delete(root); /* 处理返回结果 */ ret_json = cJSON_CreateObject(); if(!ret_json) { - ret = RET_ERR; - //ASSERT_RET(ret); + ret = RET_SYSERR; return ret; } - cJSON_AddBoolToObject(ret_json, "result", ret_move_group); + + if (ret_move_group) + { + cJSON_AddNumberToObject(ret_json, "retcode", 0); + cJSON_AddStringToObject(ret_json, "message", "移动成功"); + cJSON_AddStringToObject(ret_json, "data", ""); + }else + { + cJSON_AddNumberToObject(ret_json, "retcode", 1); + cJSON_AddStringToObject(ret_json, "message", "移动失败"); + cJSON_AddStringToObject(ret_json, "data", ""); + } + ret_char = cJSON_PrintUnformatted(ret_json); memcpy(output, ret_char, strlen(ret_char)+1); + *output_len = strlen(ret_char)+1; /* 释放内存 */ - cJSON_Delete(root); cJSON_Delete(ret_json); UCHAR_FREE(ret_char); diff --git a/Platform/user/configm/config-server/user_manager_config/user_group_config.c b/Platform/user/configm/config-server/user_manager_config/user_group_config.c index 1e1e3e279..38cdfec9b 100644 --- a/Platform/user/configm/config-server/user_manager_config/user_group_config.c +++ b/Platform/user/configm/config-server/user_manager_config/user_group_config.c @@ -14,6 +14,146 @@ ret_code usergroup_config_chk(uint source, uint *config_type, pointer output, int *output_len) { ret_code ret = RET_OK; + cJSON *root, *type_json, *data_json; + int fun_type; + //*config_type = CM_CONFIG_SET; + + if(NULL == input || NULL == input_len || NULL == output || NULL == output_len) + { + ret = RET_INPUTERR; + //ASSERT_RET(ret); + return ret; + } + + if(*input_len < strlen(input)) + { + ret = RET_INPUTERR; + return ret; + } + + root = cJSON_Parse(input); + if(!root) + { + ret = RET_INPUTERR; + return ret; + } + + type_json = cJSON_GetObjectItem(root, "type"); + if(!type_json) + { + cJSON_Delete(root); + ret = RET_INPUTERR; + return ret; + } + + data_json = cJSON_GetObjectItem(root, "data"); + if(!data_json) + { + cJSON_Delete(root); + ret = RET_INPUTERR; + return ret; + } + + cJSON_Delete(root); + return ret; +} + +/* +* 根据用户组名查询用户组uuid-处理 +* iuput格式: +{ + "type": 4, //4:表示获得用户组列表 + "data": "" +} + +* output格式: +{ + "retcode": 0, + "message": "查询成功", + "data": [{ + "gid": 1, + "gname": "用户组01", + "gdescription": "这是用户组01", + "useraccount": 3 + }, + { + "gid": 2, + "gname": "用户组02", + "gdescription": "这是用户组01", + "useraccount": 4 + } + ] +} + +* 0:成功、1:失败 +*/ +static ret_code usergroup_config_get_list_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + char * data_list, * ret_char; + bool ret_get = show_group_list(&data_list); + + cJSON * ret_root = cJSON_CreateObject(); + if (NULL == ret_root) + { + UCHAR_FREE(data_list); + ret = RET_SYSERR; + return ret; + } + + if(!ret_get) + { + cJSON_AddNumberToObject(ret_root, "retcode", 1); + cJSON_AddStringToObject(ret_root, "message", "查询失败"); + cJSON_AddStringToObject(ret_root, "data", ""); + + ret_char = cJSON_PrintUnformatted(ret_root); + memcpy(output, ret_char, strlen(ret_char)+1); + *output_len = strlen(ret_char)+1; + + UCHAR_FREE(data_list); + UCHAR_FREE(ret_char); + cJSON_Delete(ret_root); + return ret; + } + + /* 查询成功 */ + cJSON * list_obj = cJSON_Parse(data_list); + if (NULL == list_obj) + { + UCHAR_FREE(data_list); + cJSON_Delete(ret_root); + ret = RET_SYSERR; + return ret; + } + + cJSON * list_data = cJSON_GetObjectItem(list_obj, "data"); + if (NULL == list_data) + { + UCHAR_FREE(data_list); + cJSON_Delete(ret_root); + cJSON_Delete(list_obj); + ret = RET_SYSERR; + return ret; + } + + cJSON_AddItemToObject(ret_root, "data", list_data); + cJSON_AddNumberToObject(ret_root, "retcode", 0); + cJSON_AddStringToObject(ret_root, "message", "查询成功"); + + ret_char = cJSON_PrintUnformatted(ret_root); + memcpy(output, ret_char, strlen(ret_char)+1); + *output_len = strlen(ret_char)+1; + + cJSON_DetachItemFromObjectCaseSensitive(list_obj,"data"); + cJSON_DetachItemFromObjectCaseSensitive(ret_root,"data"); + UCHAR_FREE(data_list); + UCHAR_FREE(ret_char); + cJSON_Delete(list_obj); + cJSON_Delete(ret_root); + return ret; } @@ -74,7 +214,7 @@ static ret_code usergroup_config_get_uuid_proc(uint source, uint config_type, cJSON * ret_data; cJSON_AddItemToObject(ret_root, "data", ret_data = cJSON_CreateObject()); - cJSON_AddNumberToObject(ret_data, "user_id", group_uuid); + cJSON_AddNumberToObject(ret_data, "group_id", group_uuid); }else { cJSON_AddNumberToObject(ret_root, "retcode", 1); @@ -92,12 +232,93 @@ static ret_code usergroup_config_get_uuid_proc(uint source, uint config_type, return ret; } +/* +* 修改用户组描述-处理 +* input格式: +{ + "type": 2, //2:表示修改用户组描述 + "data": { + "gid": 3, //用户组id + "gdescription": "xxx" + } +} +* output格式: +{ + "retcode": 0, + "message": "修改成功", + "data": "" //空 +} +* 0:成功 +* 1:失败 +*/ +static ret_code usergroup_config_mod_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + + cJSON * root = cJSON_Parse(input); + cJSON * data = cJSON_GetObjectItem(root, "data"); + cJSON * gid = cJSON_GetObjectItem(data, "gid"); + if(NULL == gid) + { + cJSON_Delete(root); + ret = RET_INPUTERR; + return ret; + } + + cJSON * gdescription = cJSON_GetObjectItem(data, "gdescription"); + if(NULL == gdescription) + { + cJSON_Delete(root); + ret = RET_INPUTERR; + return ret; + } + + int ret_mod = mod_group_desp(gid->valueint, gdescription->valuestring); + cJSON_Delete(root); + + cJSON * ret_root = cJSON_CreateObject(); + if(NULL == ret_root) + { + ret = RET_SYSERR; + return ret; + } + + if(0 == ret_mod) + { + cJSON_AddNumberToObject(ret_root, "retcode", 0); + cJSON_AddStringToObject(ret_root, "message", "修改成功"); + }else + { + cJSON_AddNumberToObject(ret_root, "retcode", 1); + cJSON_AddStringToObject(ret_root, "message", "修改失败"); + } + + cJSON_AddStringToObject(ret_root, "data", ""); + + + char * ret_char = cJSON_PrintUnformatted(ret_root); + memcpy(output, ret_char, strlen(ret_char)+1); + *output_len = strlen(ret_char)+1; + + UCHAR_FREE(ret_char); + cJSON_Delete(ret_root); + + return ret; +} + /* * 新增用户组-处理 * input格式:{"type": 0,"data": {"gname": "xxx","gdescription": "xxx"}} -* output格式:{"result":0} +* output格式: +{ + "retcode": 0, + "message": "新增成功", + "data": "" //空 +} * 0:成功 -* 1/2/3/4/5:失败,1:用户组已满、2:用户组名或描述长度不符合要求、3:用户组名中包含特殊字符、4:用户组名重名、5:数据库操作失败 +* 1/2/3/4/5/6:失败,1:用户组已满、2:用户组名或描述长度不符合要求、3:用户组名中包含特殊字符、4:用户组名重名、5:数据库操作失败、6:系统错误 */ static ret_code usergroup_config_add_proc(uint source, uint config_type, pointer input, int input_len, @@ -108,7 +329,7 @@ static ret_code usergroup_config_add_proc(uint source, uint config_type, cJSON *root, *data, *name, *description, *res; char *des = NULL; char *ret_char = NULL; - char * message[] = {"新增成功","用户组已满(最大用户组数20)","用户组名或描述长度不符合要求","用户组名中包含特殊字符","用户组名重名","数据库操作失败"}; + char * message[] = {"新增成功","用户组已满(最大用户组数20)","用户组名或描述长度不符合要求","用户组名中包含特殊字符","用户组名重名","数据库操作失败","系统错误"}; /* 解析json串 */ root = cJSON_Parse(input); @@ -141,7 +362,7 @@ static ret_code usergroup_config_add_proc(uint source, uint config_type, res = cJSON_CreateObject(); if(!res) { - ret = RET_ERR; + ret = RET_SYSERR; //ASSERT_RET(ret); return ret; } @@ -279,13 +500,6 @@ ret_code usergroup_config_proc(uint source, uint config_type, int fun_type; cJSON *root, *type_json; - if(NULL == input) - { - ret = RET_INPUTERR; - //ASSERT_RET(ret); - return ret; - } - /* 获取对应的函数type */ FUNCTYPE_GET(input, fun_type); @@ -308,6 +522,16 @@ ret_code usergroup_config_proc(uint source, uint config_type, input, input_len, output, output_len); break; + case USERGROUP_CONFIG_MOD: + ret = usergroup_config_mod_proc(source, config_type, + input, input_len, + output, output_len); + break; + case USERGROUP_CONFIG_GET_ALL: + ret = usergroup_config_get_list_proc(source, config_type, + input, input_len, + output, output_len); + break; case USERGROUP_CONFIG_GET_UUID: ret = usergroup_config_get_uuid_proc(source, config_type, input, input_len, diff --git a/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user.h b/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user.h index 48b73f8c1..b6c62ae90 100644 --- a/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user.h +++ b/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user.h @@ -102,7 +102,7 @@ bool get_user_uuid_by_name(char* UNAME, unsigned short * user_uuid); bool mod_user_line(char* UNAME, const int INTYPE, char* IN); /* 修改用户-移动分组 */ -bool move_user_group(unsigned short user_id, unsigned short group_id); +bool move_user_group(int flag_all, unsigned short new_group_uuid, unsigned short old_group_uuid, unsigned short * user_uuid, int user_uuid_num); /* 删除用户 * id_name:条件,包括用户名、用户ID diff --git a/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user_group.h b/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user_group.h index 2b6d72996..26f6470a8 100644 --- a/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user_group.h +++ b/Platform/user/configm/config-server/user_manager_config/usermanager-server/include/user_group.h @@ -6,6 +6,9 @@ #define GNAMESIZE (64) #define GDESIZE (64) +#define OFFLINE_USER_BY_GID "offuser_by_groupid_channel" +#define USER_GROUP_DATABASE_ID 12 + #define ADDGROUP_SUCCESS 0 #define ADDGROUP_FAIL_FULL 1 #define ADDGROUP_FAIL_LENGTH 2 @@ -53,13 +56,13 @@ int init_group(); int add_group(char* UGNAME, char* UGDES); /* 修改用户组描述 */ -unsigned short mod_group_desp(char* UGNAME, char* UGDES); +unsigned short mod_group_desp(int group_uuid, char* UGDES); /* 获得用户组个数 */ unsigned short get_group_count(); /* 查询用户组列表 */ -bool show_group_list(USERGROUP* UGLIST); +bool show_group_list(char ** group_list); /* 根据用户组名查询用户组 */ bool find_group_by_name(char* UGNAME, USERGROUP* UGRES); @@ -79,5 +82,8 @@ unsigned short del_group_by_name(GROUP_DEL* UGNAME); /* 强制下线用户-按用户组ID */ void offline_force_by_groupid(char *groupid); +/*解析用户组id的json字符串 --> 用户组id*/ +unsigned short id_json2int(char * gid, char * para_name); + #endif diff --git a/Platform/user/configm/config-server/user_manager_config/usermanager-server/user.c b/Platform/user/configm/config-server/user_manager_config/usermanager-server/user.c index f41ca3bf7..2f7102986 100644 --- a/Platform/user/configm/config-server/user_manager_config/usermanager-server/user.c +++ b/Platform/user/configm/config-server/user_manager_config/usermanager-server/user.c @@ -10,6 +10,7 @@ #include "redisMq.h" #include "database.h" #include "cjson/cJSON.h" +#include "s2j/s2j.h" //#include "sg/user/user_manager/user_auth.h" extern ARRAY g_user_index_head; @@ -277,31 +278,34 @@ void get_user_by_id(unsigned short ID, USERLIST* ulist) bool mod_user_web(USERLIST* ulist) { void *moduser_web_hdbc; - USERLIST temp_user; + int ret_release; time_t cur_time; - char *temp_begin_time, *temp_end_time, *temp_pwd; + char *temp_begin_time, *temp_end_time, *temp_pwd, *temp_udes, *temp_user_json; + /* 可以修改的数据:用户组ID、描述、密码、公用账号、永久有效、有效期开始时间、有效期结束时间 */ if (NULL == ulist || INVALID_INDEX == ulist->ID || INVALID_INDEX == ulist->GID) { return false; } - /* 校验描述长度 */ - if (NULL != ulist->udescription) + /* 数据校验 */ + if (NULL == ulist->gname || NULL == ulist->uname) { - if (UDESIZE < strlen(ulist->udescription)) - { - return false; - } + return false; } - - get_user_by_id(ulist->ID, &temp_user); - - if(0 == ulist->resetpwd) + + if (NULL == ulist->udescription) { - temp_pwd = "12345678"; - }else{ - temp_pwd = temp_user.passwd; + temp_udes = ""; + }else + { + temp_udes = ulist->udescription; + } + + /* 校验描述长度 */ + if (UDESIZE < strlen(temp_udes)) + { + return false; } if(1 == ulist->valid) @@ -313,44 +317,138 @@ bool mod_user_web(USERLIST* ulist) temp_end_time = ""; } + if(0 == ulist->resetpwd) + { + if(NULL == ulist->passwd) + { + return false; + } + temp_pwd = ulist->passwd; + } + + //get_user_by_id(ulist->ID, &temp_user); + + /* 获取修改前的数据 - 这里的temp_user_json需要释放 */ + bool ret_getuser = get_user_detail_by_user_uuid(ulist->ID, &temp_user_json); + if (!ret_getuser) + { + UCHAR_FREE(temp_user_json); + return false; + } + + /* 这里的 ret_root 需要释放 */ + cJSON * ret_root = cJSON_Parse(temp_user_json); + if(NULL == ret_root) + { + UCHAR_FREE(temp_user_json); + return false; + } + cJSON * ret_data = cJSON_GetObjectItem(ret_root, "data"); + if(NULL == ret_data) + { + cJSON_Delete(ret_root); + UCHAR_FREE(temp_user_json); + return false; + } + int ret_data_num = cJSON_GetArraySize(ret_data); + if (1 != ret_data_num) + { + cJSON_Delete(ret_root); + UCHAR_FREE(temp_user_json); + return false; + } + cJSON * ret_user = cJSON_GetArrayItem(ret_data, 0); + if(NULL == ret_user) + { + cJSON_Delete(ret_root); + UCHAR_FREE(temp_user_json); + return false; + } + + /* json序列化到obj - 这里的 temp_user 需要释放*/ + s2j_create_struct_obj(temp_user, USERLIST); + s2j_struct_get_basic_element(temp_user, ret_user, int, ID); + s2j_struct_get_basic_element(temp_user, ret_user, int, GID); + s2j_struct_get_basic_element(temp_user, ret_user, int, multi); + s2j_struct_get_basic_element(temp_user, ret_user, int, valid); + s2j_struct_get_basic_element(temp_user, ret_user, string, uname); + s2j_struct_get_basic_element(temp_user, ret_user, string, gname); +// s2j_struct_get_basic_element(temp_user, ret_user, string, passwd); + s2j_struct_get_basic_element(temp_user, ret_user, string, udescription); + s2j_struct_get_basic_element(temp_user, ret_user, string, valid_begin_time); + s2j_struct_get_basic_element(temp_user, ret_user, string, valid_end_time); + + cJSON_Delete(ret_root); + UCHAR_FREE(temp_user_json); + /* 连接数据库,更新user表 */ moduser_web_hdbc = connect_database(USER_ACCOUNT_DATABASE_ID); if(NULL == moduser_web_hdbc) { - /* 记录日志 */ + s2j_delete_struct_obj(temp_user); return false; } - char* moduser_web_sql = "UPDATE `user_account` SET group_id = ?,udescription = ?, password = ?, multi_player = ?, valid_always = ?, valid_begin_time = ?, valid_end_time = ? WHERE id = ?"; - int ret_moduser_web = update_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc, DB_OP_UPDATE, USER_TABLE, moduser_web_sql, 8, - DB_DATA_INT_TYPE, sizeof(ulist->GID), ulist->GID, - DB_DATA_STRING_TYPE, strlen(ulist->udescription)+1, ulist->udescription, - DB_DATA_STRING_TYPE, strlen(temp_pwd)+1, temp_pwd, - DB_DATA_INT_TYPE, sizeof(ulist->multi), ulist->multi, - DB_DATA_INT_TYPE, sizeof(ulist->valid), ulist->valid, - DB_DATA_STRING_TYPE, strlen(temp_begin_time)+1, temp_begin_time, - DB_DATA_STRING_TYPE, strlen(temp_end_time)+1, temp_end_time, - DB_DATA_INT_TYPE, sizeof(ulist->ID), ulist->ID); - int ret_release = disconnect_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc); //ret_release记录日志 + + unsigned short user_id = get_userid_by_name(ulist->uname, moduser_web_hdbc); + unsigned short group_id = get_groupid_by_name(ulist->gname, moduser_web_hdbc); + if(INVALID_INDEX == user_id || INVALID_INDEX == group_id) + { + s2j_delete_struct_obj(temp_user); + ret_release = disconnect_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc); //ret_release记录日志 + return false; + } + + char * moduser_web_sql; + int ret_moduser_web; + if (0 == ulist->resetpwd) + { + moduser_web_sql = "UPDATE `user_account` SET group_id = (SELECT user_group.id FROM user_group WHERE user_group.uuid = ?),\ + udescription = ?, password = ?, multi_player = ?, valid_always = ?, valid_begin_time = ?, valid_end_time = ? WHERE uuid = ?"; + ret_moduser_web = update_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc, DB_OP_UPDATE, USER_TABLE, moduser_web_sql, 8, + DB_DATA_INT_TYPE, sizeof(ulist->GID), ulist->GID, + DB_DATA_STRING_TYPE, strlen(temp_udes)+1, temp_udes, + DB_DATA_STRING_TYPE, strlen(temp_pwd)+1, temp_pwd, + DB_DATA_INT_TYPE, sizeof(ulist->multi), ulist->multi, + DB_DATA_INT_TYPE, sizeof(ulist->valid), ulist->valid, + DB_DATA_STRING_TYPE, strlen(temp_begin_time)+1, temp_begin_time, + DB_DATA_STRING_TYPE, strlen(temp_end_time)+1, temp_end_time, + DB_DATA_INT_TYPE, sizeof(ulist->ID), ulist->ID); + }else + { + moduser_web_sql = "UPDATE `user_account` SET group_id = (SELECT user_group.id FROM user_group WHERE user_group.uuid = ?),\ + udescription = ?, multi_player = ?, valid_always = ?, valid_begin_time = ?, valid_end_time = ? WHERE uuid = ?"; + ret_moduser_web = update_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc, DB_OP_UPDATE, USER_TABLE, moduser_web_sql, 7, + DB_DATA_INT_TYPE, sizeof(ulist->GID), ulist->GID, + DB_DATA_STRING_TYPE, strlen(temp_udes)+1, temp_udes, + DB_DATA_INT_TYPE, sizeof(ulist->multi), ulist->multi, + DB_DATA_INT_TYPE, sizeof(ulist->valid), ulist->valid, + DB_DATA_STRING_TYPE, strlen(temp_begin_time)+1, temp_begin_time, + DB_DATA_STRING_TYPE, strlen(temp_end_time)+1, temp_end_time, + DB_DATA_INT_TYPE, sizeof(ulist->ID), ulist->ID); + } + + ret_release = disconnect_database(USER_ACCOUNT_DATABASE_ID, moduser_web_hdbc); //ret_release记录日志 if(DB_RET_OK != ret_moduser_web) { + s2j_delete_struct_obj(temp_user); return false; } + /* 更新内存数据 */ - strcpy(g_user_table[ulist->ID].udescription, ulist->udescription); + strcpy(g_user_table[user_id].udescription, temp_udes); if(0 == ulist->resetpwd) { - strcpy(g_user_table[ulist->ID].passwd, "12345678"); + strcpy(g_user_table[user_id].passwd, ulist->passwd); } - g_user_table[ulist->ID].GID = ulist->GID; - g_user_table[ulist->ID].multi_valid = MULTI_SET(g_user_table[ulist->ID].multi_valid, ulist->multi); - g_user_table[ulist->ID].multi_valid = VALID_SET(g_user_table[ulist->ID].multi_valid, ulist->valid); + g_user_table[user_id].GID = group_id; + g_user_table[user_id].multi_valid = MULTI_SET(g_user_table[user_id].multi_valid, ulist->multi); + g_user_table[user_id].multi_valid = VALID_SET(g_user_table[user_id].multi_valid, ulist->valid); /* 更新有效日期时间 */ if (1 == ulist->valid) { //if(判断是否符合格式) - STRING2TIME_T(ulist->valid_begin_time, g_user_table[ulist->ID].valid_begin_time); - STRING2TIME_T(ulist->valid_end_time, g_user_table[ulist->ID].valid_end_time); - //return true; + STRING2TIME_T(ulist->valid_begin_time, g_user_table[user_id].valid_begin_time); + STRING2TIME_T(ulist->valid_end_time, g_user_table[user_id].valid_end_time); } /* 查询该用户是否有在线IP */ @@ -363,35 +461,33 @@ bool mod_user_web(USERLIST* ulist) // } /* 用户组发生改变,策略重查 */ - if (temp_user.GID != ulist->GID) + if (temp_user->GID != ulist->GID) { /* 策略重查*/ + printf("修改了用户组-策略重查 .\n"); } /* 密码发生改变,下线用户 */ if (0 == ulist->resetpwd) { - /* 下线用户,return */ - offline_force_one_user(ulist->ID); + offline_force_one_user(user_id); return true; } /* 公用账号改为单用户登陆,下线用户 */ - if (1 == ulist->multi && 0 == temp_user.multi) + if (1 == ulist->multi && 0 == temp_user->multi) { - /* 下线用户,return */ - offline_force_one_user(ulist->ID); + offline_force_one_user(user_id); return true; } /* 当前时间不在有效期内,下线用户 */ - if (1 == VALID_GET(g_user_table[ulist->ID].multi_valid)) + if (1 == ulist->valid) { cur_time = time(NULL); - if (cur_time < g_user_table[ulist->ID].valid_begin_time || cur_time > g_user_table[ulist->ID].valid_end_time) + if (cur_time < g_user_table[user_id].valid_begin_time || cur_time > g_user_table[user_id].valid_end_time) { - /* 下线用户,return */ - offline_force_one_user(ulist->ID); + offline_force_one_user(user_id); return true; } } @@ -516,43 +612,7 @@ unsigned short get_userid_by_name(char* uname, void* hdbc) } ARRAYJSON2INT(uid, "id", &ID_temp); -/* cJSON *root = cJSON_Parse(uid); - if(!root) - { - return ID_temp; - } - cJSON *id_list = cJSON_GetObjectItem(root, "data"); - if(!id_list) - { - cJSON_Delete(root); - return ID_temp; - } - - int id_list_size = cJSON_GetArraySize(id_list); - if(1 != id_list_size) - { - cJSON_Delete(root); - return ID_temp; - } - - cJSON *id_obj = cJSON_GetArrayItem(id_list, 0); - if(!id_obj) - { - cJSON_Delete(root); - return ID_temp; - } - - cJSON *id_json = cJSON_GetObjectItem(id_obj, "id"); - if(!id_json) - { - cJSON_Delete(root); - return ID_temp; - } - - ID_temp = id_json->valueint; - cJSON_Delete(root); -*/ return ID_temp; } @@ -599,20 +659,168 @@ bool mod_user_line(char* uname, const int intype, char* in) } /* 修改用户-移动分组 */ -bool move_user_group(unsigned short user_id, unsigned short group_id) +bool move_user_group(int flag_all, unsigned short new_group_uuid, unsigned short old_group_uuid, unsigned short * user_uuid, int user_uuid_num) { - if(user_id != g_user_table[user_id].ID || group_id != g_group_table[group_id].ID) + void * moveug_hdbc,* selectgid_hdbc;; + char * moveug_sql; + char * selectuid_sql; + char * ret_userids; + bool root_all = false; + int ret_sql, ret_discon, sql_num; + unsigned short temp_userids[USER_INDEX_MAX] = {0}; + unsigned short temp_groupid, temp_userid; + + if (INVALID_INDEX == new_group_uuid || (0 != flag_all && 1 != flag_all)) { return false; } - g_user_table[user_id].GID = group_id; + //连接数据库 + moveug_hdbc = connect_database(USER_ACCOUNT_DATABASE_ID); //更新用户表 + if (NULL == moveug_hdbc || NULL == selectgid_hdbc) + { + return false; + } - /* 连接数据库,修改user表中的用户组id */ - /* UPDATE `user` u SET u.group_id = WHERE u.id = ; */ + //根据新组的uuid获取新组的id + temp_groupid = get_groupid_by_uuid(new_group_uuid, moveug_hdbc); + if (INVALID_INDEX == temp_groupid) + { + return false; + } + + // char * ret_new_groupid_json = select_datebase_by_number(USER_GROUP_DATABASE_ID, selectgid_hdbc, USER_GROUP_TABLE, "SELECT id FROM `user_group` WHERE uuid = ?", + // 1, 0, &sql_num, 1, + // DB_DATA_INT_TYPE, sizeof(new_group_uuid), new_group_uuid); + // if (0 == sql_num || NULL == ret_new_groupid_json) + // { + // ret_discon = disconnect_database(USER_GROUP_DATABASE_ID, selectgid_hdbc); + // return false; + // } + // ARRAYJSON2INT(ret_new_groupid_json, "id", &temp_groupid); + //temp_groupid = id_json2int(temp_test01, "id"); + + //按照group_uuid移动 + if (0 == flag_all || NULL ==user_uuid) + { + if (0 == old_group_uuid) + { + moveug_sql = "UPDATE `user_account` SET group_id = ?"; + root_all = true; + }else + { + moveug_sql = "UPDATE `user_account` \ + SET group_id = ? \ + WHERE group_id = ( \ + SELECT \ + user_group.id \ + FROM \ + user_group \ + WHERE \ + user_group.uuid = ? \ + )"; + selectuid_sql = "SELECT id FROM `user_account` WHERE group_id = (SELECT user_group.id FROM user_group WHERE user_group.uuid = ?)"; + + ret_userids = select_datebase_by_number(USER_ACCOUNT_DATABASE_ID, moveug_hdbc, USER_TABLE, selectuid_sql, 1, 0, &sql_num, 1, + DB_DATA_INT_TYPE, sizeof(old_group_uuid), old_group_uuid); + //解析ret_userids --> temp_userids + if(0 == sql_num || NULL == ret_userids) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + + cJSON * ret_root_json = cJSON_Parse(ret_userids); + if (NULL == ret_root_json) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + + cJSON * ret_data_json = cJSON_GetObjectItem(ret_root_json, "data"); + if(NULL == ret_data_json) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + + int ret_data_num = cJSON_GetArraySize(ret_data_json); + + for (int i = 0; i < ret_data_num; i++) + { + cJSON * ret_id_json = cJSON_GetArrayItem(ret_data_json, i); + if (NULL == ret_id_json) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + temp_userids[ret_id_json->valueint] = ret_id_json->valueint; + } + } + + ret_sql = update_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc, DB_OP_UPDATE, USER_TABLE, moveug_sql, 2, + DB_DATA_INT_TYPE, sizeof(temp_groupid), temp_groupid, + DB_DATA_INT_TYPE, sizeof(old_group_uuid), old_group_uuid); + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + if (DB_RET_OK != ret_sql) + { + return false; + } + } + + //按user_uuid移动 + if (1 == flag_all) + { + if (NULL == user_uuid) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + + moveug_sql = "UPDATE user_account SET user_account.group_id = ? WHERE user_account.uuid = ?"; + selectuid_sql = "SELECT id FROM `user_account` WHERE uuid = ?"; + for (int i = 0; i < user_uuid_num; i++) + { + unsigned short temp_uesr_uuid = 0; + ret_userids = select_datebase_by_number(USER_ACCOUNT_DATABASE_ID, moveug_hdbc, USER_TABLE, selectuid_sql, 1, 0, &sql_num, 1, + DB_DATA_INT_TYPE, sizeof(user_uuid[i]), user_uuid[i]); + if(0 == sql_num || NULL ==ret_userids) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + ARRAYJSON2INT(ret_userids, "id", &temp_userid); + temp_userids[temp_userid] = temp_userid; + + temp_uesr_uuid = user_uuid[i]; + ret_sql = update_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc, DB_OP_UPDATE, USER_TABLE, moveug_sql, 2, + DB_DATA_INT_TYPE, sizeof(temp_groupid), temp_groupid, + DB_DATA_INT_TYPE, sizeof(temp_uesr_uuid), temp_uesr_uuid); + if(DB_RET_OK != ret_sql) + { + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + return false; + } + } + ret_discon = disconnect_database(USER_ACCOUNT_DATABASE_ID, moveug_hdbc); + } + + //修改内存 + for (int i = 0; i < USER_INDEX_MAX; i++) + { + if (root_all) + { + g_user_table[i].GID = temp_groupid; + }else if(INVALID_INDEX != temp_userids[i]) + { + g_user_table[i].GID = temp_groupid; + } + } + /* 发送消息(用户id - 用户组id)到策略模块,重查策略 */ - + printf("发送消息给策略模块,重查策略. new_group_id:%d .\n", temp_groupid); + return true; } diff --git a/Platform/user/configm/config-server/user_manager_config/usermanager-server/user_group.c b/Platform/user/configm/config-server/user_manager_config/usermanager-server/user_group.c index b34ca50f4..0ca31dfb2 100644 --- a/Platform/user/configm/config-server/user_manager_config/usermanager-server/user_group.c +++ b/Platform/user/configm/config-server/user_manager_config/usermanager-server/user_group.c @@ -10,9 +10,6 @@ #include "database.h" #include "cjson/cJSON.h" -#define OFFLINE_USER_BY_GID "offuser_by_groupid_channel" -#define USER_GROUP_DATABASE_ID 12 - extern ARRAY g_group_index_head; extern ARRAY g_user_index_head; extern USERACCOUNT g_user_table[]; @@ -35,6 +32,7 @@ int init_group() int add_group(char* name, char* description) { void *addgroup_hdbc; + int ret_release; if (NULL == name) { return ADDGROUP_ERROR; @@ -90,7 +88,7 @@ int add_group(char* name, char* description) if(DB_RET_OK != ret_addgroup) { free_index(&g_group_index_head, ID); - disconnect_database(USER_GROUP_DATABASE_ID, addgroup_hdbc); // ret_release记录日志 + ret_release = disconnect_database(USER_GROUP_DATABASE_ID, addgroup_hdbc); // ret_release记录日志 return ADDGROUP_FAIL_DATABASE; } @@ -99,18 +97,20 @@ int add_group(char* name, char* description) strcpy(g_group_table[ID].gname, name); strcpy(g_group_table[ID].gdescription, description); - int ret_release = disconnect_database(USER_GROUP_DATABASE_ID, addgroup_hdbc); // ret_release记录日志 + ret_release = disconnect_database(USER_GROUP_DATABASE_ID, addgroup_hdbc); // ret_release记录日志 return ADDGROUP_SUCCESS; } /* 修改用户组描述 */ -unsigned short mod_group_desp(char* gname, char* gdesp) +unsigned short mod_group_desp(int group_uuid, char* gdesp) { void *modgroup_hdbc; - if(NULL == gname || NULL ==gdesp) + int ret_release; + int num; + if(NULL == gdesp) { - return MODGROUP_FAIL_INPUT; + gdesp = ""; } /* 连接数据库 */ @@ -121,28 +121,28 @@ unsigned short mod_group_desp(char* gname, char* gdesp) return MODGROUP_FAIL_DATABASE; } - unsigned short gid = get_groupid_by_name(gname, mod_group_desp); - if(INVALID_INDEX == gid) + unsigned short group_id = get_groupid_by_uuid(group_uuid, modgroup_hdbc); + if(INVALID_INDEX == group_id) { - return MODGROUP_FAIL_NOTEXIST; + ret_release = disconnect_database(USER_GROUP_DATABASE_ID, modgroup_hdbc); //ret_release记录日志 + return MODGROUP_FAIL_INPUT; } /* 修改数据库 */ - char *modgroup_sql = "UPDATE user_group SET gdescription = ? WHERE gname = ? "; + char *modgroup_sql = "UPDATE user_group SET gdescription = ? WHERE uuid = ? "; int ret_modgroup = update_database(1, modgroup_hdbc, DB_OP_UPDATE, USER_GROUP_TABLE, modgroup_sql, 2, DB_DATA_STRING_TYPE, strlen(gdesp), gdesp, - DB_DATA_STRING_TYPE, strlen(gname), gname); + DB_DATA_INT_TYPE, sizeof(group_uuid), group_uuid); + ret_release = disconnect_database(USER_GROUP_DATABASE_ID, modgroup_hdbc); //ret_release记录日志 + if(DB_RET_OK != ret_modgroup) { return MODGROUP_FAIL_DATABASE; } /* 修改内存 */ - strcpy(g_group_table[gid].gdescription, gdesp); + strcpy(g_group_table[group_id].gdescription, gdesp); - /* 释放数据库连接 */ - int ret_release = disconnect_database(USER_GROUP_DATABASE_ID, modgroup_hdbc); //ret_release记录日志 - return MODGROUP_SUCCESS; } @@ -192,38 +192,75 @@ unsigned short get_group_count() } /* 查询用户组列表 */ -bool show_group_list(USERGROUP* grouplist) +bool show_group_list(char ** group_list) { - if (NULL == grouplist) + void * getglist_hdbc; + int num; + if (NULL == group_list) { return false; } - /* 内存查询数据 */ - int j = 0; - for (int i = 0; i < GROUP_INDEX_MAX; i++) + getglist_hdbc = connect_database(USER_GROUP_DATABASE_ID); + if (NULL == getglist_hdbc) { - if (g_group_table[i].ID != 0) - { - grouplist[j].ID = g_group_table[i].ID; - strcpy(grouplist[j].gname, g_group_table[i].gname); - strcpy(grouplist[j].gdescription, g_group_table[i].gdescription); - j++; - } + return false; } - /* 连接数据库,查询表user_group列表 */ - /* SELECT UG.id, UG.gname, UG.gdescription FROM user_group UG */ + char * select_sql = "SELECT\ + user_group.uuid gid,\ + user_group.gname,\ + user_group.gdescription,\ + IFNULL(t1.count1, 0) useraccount\ + FROM\ + user_group\ + LEFT JOIN (\ + SELECT\ + user_account.group_id,\ + COUNT(*) count1\ + FROM\ + user_account\ + GROUP BY\ + user_account.group_id\ + ) t1 ON user_group.id = t1.group_id\ + ORDER BY\ + user_group.uuid"; + char * glist = select_datebase_by_number(USER_ACCOUNT_DATABASE_ID, getglist_hdbc, USER_GROUP_TABLE, select_sql, 1, 0, &num, 0); + + if (0 == num || NULL == glist) //查询结果为空 + { + cJSON * root = cJSON_CreateObject(); + cJSON * array; + if(!root) + { + return false; + } + + cJSON_AddItemToObject(root, "data", array = cJSON_CreateArray()); + /*{"data":[]}*/ + *group_list = cJSON_Print(root); //在函数外面释放 + + return true; + } + + char * point = (char*)malloc(strlen(glist)+1); + if(NULL == point) + { + return false; + } + memset(point, 0, strlen(glist)+1); + memcpy(point, glist, strlen(glist)+1); + *group_list = point; //在函数外面释放 return true; } /*解析用户组id的json字符串 --> 用户组id*/ -unsigned short id_json2int(char * gid) +unsigned short id_json2int(char * gid, char * para_name) { unsigned short GID_temp = INVALID_INDEX; - if (NULL == gid) + if (NULL == gid || NULL ==para_name) { return GID_temp; } @@ -255,14 +292,15 @@ unsigned short id_json2int(char * gid) return GID_temp; } - cJSON *gid_json = cJSON_GetObjectItem(id_obj, "id"); + cJSON *gid_json = cJSON_GetObjectItem(id_obj, para_name); if(!gid_json) { cJSON_Delete(root); return GID_temp; } GID_temp = gid_json->valueint; - + cJSON_Delete(root); + return GID_temp; } @@ -318,7 +356,7 @@ bool get_group_uuid_by_name(char* gname, unsigned short * group_uuid) /* 删除数据连接 */ discon = disconnect_database(USER_GROUP_DATABASE_ID, ghdbc); //discon记录日志 - if(0 == num) + if(0 == num || NULL == gid) { *group_uuid = INVALID_INDEX; return true; @@ -360,42 +398,6 @@ unsigned short get_groupid_by_name(char* gname, void* hdbc) ARRAYJSON2INT(gid, "id", &GID_temp); -/* cJSON *root = cJSON_Parse(gid); - if(!root) - { - return GID_temp; - } - - cJSON *id_list = cJSON_GetObjectItem(root, "data"); - if(!id_list) - { - cJSON_Delete(root); - return GID_temp; - } - - int id_list_size = cJSON_GetArraySize(id_list); - if(1 != id_list_size) - { - cJSON_Delete(root); - return GID_temp; - } - - cJSON *id_obj = cJSON_GetArrayItem(id_list, 0); - if(!id_obj) - { - cJSON_Delete(root); - return GID_temp; - } - - cJSON *gid_json = cJSON_GetObjectItem(id_obj, "id"); - if(!gid_json) - { - cJSON_Delete(root); - return GID_temp; - } - GID_temp = gid_json->valueint; - cJSON_Delete(root); -*/ return GID_temp; } diff --git a/Platform/user/database/database.c b/Platform/user/database/database.c index 4fee34266..c619ca937 100644 --- a/Platform/user/database/database.c +++ b/Platform/user/database/database.c @@ -219,7 +219,7 @@ int update_database(int module_id, void * db_handle, int op_type, char * tabl return DB_RET_PARAM_ERR; } - SQLAllocHandle(SQL_HANDLE_STMT, db_handle, &hstmt); + ret = SQLAllocHandle(SQL_HANDLE_STMT, db_handle, &hstmt); if ((SQL_INVALID_HANDLE == ret) || (SQL_ERROR == ret)) { return DB_RET_ERR; diff --git a/Product/user/user_manager/usermanager-test/test_usermanager.c b/Product/user/user_manager/usermanager-test/test_usermanager.c index 2c44f80cc..6ec8dc1ca 100644 --- a/Product/user/user_manager/usermanager-test/test_usermanager.c +++ b/Product/user/user_manager/usermanager-test/test_usermanager.c @@ -123,87 +123,118 @@ extern USERACCOUNT g_user_table[USER_INDEX_MAX]; // } -void offline_force(char *userid) -{ - bool ret = redisPubInit(); - if (!ret) - { - printf("Init failed.\n"); - return; - } - - ret = redisPubConnect(); - if (!ret) - { - printf("connect failed."); - return; - } - - // while (true) - // { - // redisPublish(OFFLINE_USER_BY_UID, userid); - // sleep(1); - // } - redisPublish(OFFLINE_USER_BY_UID, userid); - //sleep(1); - redisPubDisconnect(); - redisPubUninit(); - return; -} - -int main1(void) -{ - - offline_force("111"); - offline_force("222"); - offline_force("333"); - return 0; -} - -void test_func_recv(struct RecvMsg_t *mmsg) -{ - if (mmsg->msg) - { - printf("%s,%d,%llu,%s,%d\n", __FUNCTION__, __LINE__, pthread_self(), mmsg->msg, mmsg->len); - freeMsg(mmsg); - } -} - - -void sub_example() -{ - bool ret = redisSubInit(8); - if (!ret) - { - printf("Init failed.\n"); - return; - } - redisRegisterChannelFunc("offuser_by_userid_channel",test_func_recv); - - - ret = redisSubConnect(); - if (!ret) - { - printf("Connect failed.\n"); - return; - } - redisSubscriber("offuser_by_userid_channel"); - - while (true) - { - sleep(1); - } - - redisSubDisconnect(); - redisSubUninit(); - - return; - -} int main(void) { -/* + char * testin; //input + char testout[2000]; //output + ret_code ret_pro; //执行结果 + int outlen = 1; //out数据长度 + + printf("************************************start*******************************************\n"); + printf("初始化用户组:%d\n", init_group()); + printf("初始化用户:%d\n", init_user()); + + printf("************************************新增 group*******************************************\n"); + testin = "{\"type\": 0,\"data\": {\"gname\": \"group01\",\"gdescription\": \"描述group01\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"gname\": \"group02\",\"gdescription\": \"描述group02\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"gname\": \"group03\",\"gdescription\": \"描述group03\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"gname\": \"group04\",\"gdescription\": \"描述group04\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"gname\": \"group05\",\"gdescription\": \"描述group05\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"gname\": \"group06\",\"gdescription\": \"描述group06\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************查询 group 列表*******************************************\n"); + testin = "{\"type\": 4,\"data\": \"\"}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************group name -> group uuid*******************************************\n"); + testin = "{\"type\": 7,\"data\": {\"gname\":\"group04\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************修改 group 描述*******************************************\n"); + testin = "{\"type\": 2,\"data\": {\"gid\": 938,\"gdescription\": \"修改用户组的描述\"}}"; + ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************新增 user*******************************************\n"); + testin = "{\"type\": 0,\"data\": {\"uname\": \"user01\",\"gname\": \"group01\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"uname\": \"user00\",\"gname\": \"group01\",\"udesp\": \"用户描述\",\"pwd\": \"A31as%4Gb\",\"multi\":0,\"valid\":1, \"valid_begin_time\": \"2013-07-04 15:04:23\",\"valid_end_time\": \"2020-07-04 15:04:23\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"uname\": \"user02\",\"gname\": \"group02\",\"pwd\": \"123456\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"uname\": \"user03\",\"gname\": \"group03\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + testin = "{\"type\": 0,\"data\": {\"uname\": \"user04\",\"gname\": \"group04\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************修改 user web*******************************************\n"); + testin = "{\"type\": 2,\"data\": {\"ID\":3,\"GID\":938,\"resetpwd\":0,\"uname\": \"user02\",\"gname\": \"group01\",\"udescription\": \"修改用户描述\",\"passwd\": \"A31as%4Gb\",\"multi\":0,\"valid\":1, \"valid_begin_time\": \"2013-07-04 15:04:23\",\"valid_end_time\": \"2020-07-04 15:04:23\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************查询 user list*******************************************\n"); + testin = "{\"type\": 4,\"data\":{\"group_id\": 0, \"user_name\": \"3\",\"page_start\": 1,\"page_counts\": 20}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************user name -> user uuid*******************************************\n"); + testin = "{\"type\": 7,\"data\":{\"uname\": \"user03\"}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************user uuid -> user 详情*******************************************\n"); + testin = "{\"type\": 3,\"data\":{\"user_id\": 2}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + + + printf("\n************************************移动分组*******************************************\n"); + testin = "{\"type\": 6,\"data\":{\"user_id\": [2],\"batch\":{\"all\":1,\"old_group_id\":0},\"new_group_id\":927}}"; + ret_pro = user_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen); + printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout); + +} + +int main2(void) +{ + int ID = 1; int GID_temp = 2; char *uname = "xiaoming"; @@ -233,7 +264,7 @@ int main(void) // DB_DATA_STRING_TYPE, strlen(valid_end_time)+1,valid_end_time); // printf("ret_adduser = %d .\n",ret_adduser); - +/* char * aa = "户"; int len = strlen(aa)+strlen("*")*2+1; char user_name_para[len]; @@ -245,23 +276,67 @@ int main(void) int rett = create_database_table(1, adduser_hdbc, "ctest", "create table ctest ( id bigint, fl double, dt character(10), status character(7))"); - retptr1 = select_datebase_by_number(1, adduser_hdbc, "user_account", "SELECT * FROM `user_account` WHERE user_name LIKE ?", 1, 0, &num1, 1, - DB_DATA_STRING_TYPE, 20, user_name_para); + retptr1 = select_datebase_by_number(1, adduser_hdbc, "user_account", "SELECT id FROM `user_account` WHERE user_name LIKE ?", 1, 0, &num1, 2, + DB_DATA_STRING_TYPE, 20, user_name_para, + DB_DATA_STRING_TYPE, 20, aa); //printf("长度:%d \n",strlen(retptr1)); - disconnect_database(USER_ACCOUNT_DATABASE_ID, adduser_hdbc); - printf("%d\n",rett); - printf("user = %s\n", retptr1); + char * moveug_sql = "UPDATE `user_account` \ + SET group_id = ( \ + SELECT \ + user_group.id \ + FROM \ + user_group \ + WHERE \ + user_group.uuid = ? \ + ) \ + WHERE \ + uuid = ?"; + int new_group_uuid = 712; + int user_uuid[1] = {3}; + unsigned short *user_uuid_1 = user_uuid; + int temp_i = 0; + rett = update_database(1, adduser_hdbc, DB_OP_UPDATE, USER_TABLE, moveug_sql, 2, + DB_DATA_INT_TYPE, sizeof(new_group_uuid), new_group_uuid, + DB_DATA_INT_TYPE, sizeof(user_uuid_1[temp_i]), user_uuid_1[temp_i]); + + printf("更新用户组id结果:%d\n",rett); + + //printf("user = %s\n", retptr1); + + char * sele_sql = "SELECT \ + user_group.uuid gid,\ + user_group.gname,\ + user_group.gdescription,\ + IFNULL(t1.count1, 0) useraccount\ + FROM\ + user_group\ + LEFT JOIN (\ + SELECT\ + user_account.group_id,\ + COUNT(*) count1\ + FROM\ + user_account\ + GROUP BY\ + user_account.group_id\ + ) t1 ON user_group.id = t1.group_id\ + ORDER BY\ + user_group.uuid"; + + retptr1 = select_datebase_by_number(1, adduser_hdbc, "user_account", sele_sql, 1, 0, &num1, 0); + //printf("user = %s\n", retptr1); + disconnect_database(USER_ACCOUNT_DATABASE_ID, adduser_hdbc); return 0; */ - + +int outsize; char output7[5000]; - char *testin7 = "{\"type\": 4,\"data\":{\"group_id\": 693, \"user_name\": \"户0\",\"page_start\": 1,\"page_counts\": 10}}"; + char *testin7 = "{\"type\": 4,\"data\":{\"group_id\": 0, \"user_name\": \"\",\"page_start\": 1,\"page_counts\": 20}}"; int a7 = 1; ret_code c7 = user_config_proc(1, 1, testin7, strlen(testin7)+1, output7, &a7); - +outsize = strlen(output7); printf("*******************************************************************************\n"); - printf("这里是个测试第7次结果,查询:%d\n", c7); + printf("这里是个测试第7次结果,查询用户列表,查询结果:%d\n", c7); printf("%s\n", output7); printf("*******************************************************************************\n"); @@ -271,7 +346,7 @@ int main(void) ret_code c8 = user_config_proc(1, 1, testin8, strlen(testin8)+1, output8, &a8); printf("*******************************************************************************\n"); - printf("这里是个测试第8次结果,查询:%d\n", c8); + printf("这里是个测试第8次结果,根据用户名->用户uuid,查询结果:%d\n", c8); printf("%s\n", output8); printf("*******************************************************************************\n"); @@ -282,7 +357,7 @@ int main(void) ret_code c9 = usergroup_config_proc(1, 1, testin9, strlen(testin9)+1, output9, &a9); printf("*******************************************************************************\n"); - printf("这里是个测试第9次结果,查询:%d\n", c9); + printf("这里是个测试第9次结果,根据用户组名->用户组uuid,查询结果:%d\n", c9); printf("%s\n", output9); printf("*******************************************************************************\n"); @@ -292,10 +367,49 @@ int main(void) ret_code c10 = user_config_proc(1, 1, testin10, strlen(testin10)+1, output10, &a10); printf("*******************************************************************************\n"); - printf("这里是个测试第10次结果,查询:%d\n", c10); + printf("这里是个测试第10次结果,根据用户uuid->用户详情,查询结果:%d\n", c10); printf("%s\n", output10); printf("*******************************************************************************\n"); + char output11[200]; + char *testin11 = "{\"type\": 6,\"data\":{\"user_id\": [3,4,5],\"batch\":{\"all\":1,\"old_group_id\":0},\"new_group_id\":717}}"; + int a11 = 1; + ret_code c11 = user_config_proc(1, 1, testin11, strlen(testin11)+1, output11, &a11); + + printf("*******************************************************************************\n"); + printf("这里是个测试第11次结果,移动分组,结果:%d\n", c11); + printf("%s\n", output11); + printf("*******************************************************************************\n"); + + char output12[2000]; + char *testin12 = "{\"type\": 4,\"data\":\"\"}"; + int a12 = 1; + ret_code c12 = usergroup_config_proc(1, 1, testin12, strlen(testin12)+1, output12, &a12); +outsize = strlen(output12); + printf("*******************************************************************************\n"); + printf("这里是个测试第12次结果,用户组列表,查询结果:%d\n", c12); + printf("%s\n", output12); + printf("*******************************************************************************\n"); + + char output13[100]; + char *testin13 = "{\"type\":2,\"data\":{\"ID\":4,\"GID\":811,\"uname\":\"用户06\",\"gname\":\"aaa03\",\"resetpwd\":1,\"passwd\":\"A31as%4Gb\",\ + \"udescription\":\"测试修改描述\",\"multi\":1,\"valid\":1,\"valid_begin_time\":\"2018-01-01 00:00:00\",\"valid_end_time\":\"2019-01-01 00:00:00\"}}"; + int a13 = 1; + ret_code c13 = user_config_proc(1, 1, testin13, strlen(testin13)+1, output13, &a13); + printf("*******************************************************************************\n"); + printf("这里是个测试第13次结果,修改用户,结果:%d\n", c13); + printf("%s\n", output13); + printf("*******************************************************************************\n"); + + char output14[100]; + char *testin14 = "{\"type\": 2,\"data\":{\"gid\":835,\"gdescription\":\"111\"}}"; + int a14 = 1; + ret_code c14 = usergroup_config_proc(1, 1, testin14, strlen(testin14)+1, output14, &a14); + printf("*******************************************************************************\n"); + printf("这里是个测试第14次结果,修改用户组描述,结果:%d\n", c14); + printf("%s\n", output14); + printf("*******************************************************************************\n"); + return 0; @@ -317,35 +431,35 @@ int main(void) //USERGROUP U[22] = { 0 }; int gsize = get_group_count(); - USERGROUP* U; - U = (USERGROUP*)malloc(sizeof(USERGROUP) * gsize); - if (NULL == U) - { - printf("shibaile"); - } + // USERGROUP* U; + // U = (USERGROUP*)malloc(sizeof(USERGROUP) * gsize); + // if (NULL == U) + // { + // printf("shibaile"); + // } - show_group_list(U); - for (int i = 0; i < gsize; i++) - { - printf("第 %d 用户组 ID : %d, NAME: %s, 描述:%s \n", i, U[i].ID, U[i].gname, U[i].gdescription); - } + //show_group_list(U); + // for (int i = 0; i < gsize; i++) + // { + // printf("第 %d 用户组 ID : %d, NAME: %s, 描述:%s \n", i, U[i].ID, U[i].gname, U[i].gdescription); + // } // char* name1 = { "aaa18" }; // printf("删除一个组:%s, %d\n", name1, del_group_by_name(name1)); // printf("删除一个组:%s, %d\n", "aaa03", del_group_by_name("aaa03")); // printf("删除一个组:%s, %d\n", "aaaa03", del_group_by_name("aaaa03")); - printf("增加一个用户组:%d\n", add_group("aaa30", "就爱看九分阿道夫就卡了阿道夫阿道夫就")); - printf("增加一个用户组:%d\n", add_group("aaa31", "描述31")); - printf("增加一个用户组:%d\n", add_group("aaa32", "描述31")); - printf("增加一个用户组:%d\n", add_group("aaa13", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa14", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa15", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa16", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa17", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa18", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa18", "描述04")); - printf("增加一个用户组:%d\n", add_group("aaa20", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa30", "就爱看九分阿道夫就卡了阿道夫阿道夫就")); + // printf("增加一个用户组:%d\n", add_group("aaa31", "描述31")); + // printf("增加一个用户组:%d\n", add_group("aaa32", "描述31")); + // printf("增加一个用户组:%d\n", add_group("aaa13", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa14", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa15", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa16", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa17", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa18", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa18", "描述04")); + // printf("增加一个用户组:%d\n", add_group("aaa20", "描述04")); //printf("增加一个用户组:%d\n", add_group("aaa21", "描述04")); char *testgroupin = "{\"type\": 0,\"data\": {\"gname\": \"aaa21\",\"gdescription\": \"xxx\"}}"; @@ -373,18 +487,18 @@ int main(void) gsize = get_group_count(); - USERGROUP* U1; - U1 = (USERGROUP*)malloc(sizeof(USERGROUP) * gsize); - if (NULL == U1) - { - printf("shibaile"); - } + // USERGROUP* U1; + // U1 = (USERGROUP*)malloc(sizeof(USERGROUP) * gsize); + // if (NULL == U1) + // { + // printf("shibaile"); + // } - show_group_list(U1); - for (int i = 0; i < gsize; i++) - { - printf("第 %d 用户组 ID : %d, NAME: %s, 描述:%s \n", i, U1[i].ID, U1[i].gname, U1[i].gdescription); - } + //show_group_list(U1); + // for (int i = 0; i < gsize; i++) + // { + // printf("第 %d 用户组 ID : %d, NAME: %s, 描述:%s \n", i, U1[i].ID, U1[i].gname, U1[i].gdescription); + // } @@ -413,7 +527,7 @@ int main(void) USERADD* addUserResullt; addUserResullt = (USERADD*)malloc(sizeof(USERADD)); - printf("sizeof(USERADD): %d\n",sizeof(USERADD)); + printf("sizeof(USERADD): %ld\n",sizeof(USERADD)); memset(addUserResullt, 0, sizeof(USERADD)); if (NULL == addUserResullt) @@ -691,7 +805,7 @@ int main(void) DB_DATA_INT_TYPE, 8, 2); printf("num_sql = %d \n", num_sql); - printf("test_hdbc = %s \n", test_hdbc); + printf("test_hdbc = %p \n", test_hdbc); //printf("ret_sql = %s \n",ret_sql); /* 创建table, 其中列类型包括长整形、浮点型、字符串 */ diff --git a/libs/src/lighttpd-1.4.51/configure.ac b/libs/src/lighttpd-1.4.51/configure.ac index d8397334e..fc2898f79 100644 --- a/libs/src/lighttpd-1.4.51/configure.ac +++ b/libs/src/lighttpd-1.4.51/configure.ac @@ -1324,7 +1324,7 @@ AM_CONDITIONAL([CHECK_WITH_FASTCGI], [test "$fastcgi_found" = yes]) AC_MSG_NOTICE([----------------------------------------]) dnl check for extra compiler options (warning options) if test "${GCC}" = yes; then - TRY_CFLAGS([-Wall -W -Wshadow -pedantic]) + TRY_CFLAGS([-Wall -W]) fi AC_ARG_ENABLE([extra-warnings], diff --git a/libs/src/lighttpd-1.4.51/src/Makefile.am b/libs/src/lighttpd-1.4.51/src/Makefile.am index 684dc71fc..f115a001e 100644 --- a/libs/src/lighttpd-1.4.51/src/Makefile.am +++ b/libs/src/lighttpd-1.4.51/src/Makefile.am @@ -146,7 +146,7 @@ mod_magnet_la_LDFLAGS = $(common_module_ldflags) mod_magnet_la_LIBADD = $(common_libadd) $(LUA_LIBS) -lm endif -if BUILD_WITH_LUA +if BUILD_WITH_LUA lib_LTLIBRARIES += mod_cml.la mod_cml_la_SOURCES = mod_cml.c mod_cml_lua.c mod_cml_funcs.c mod_cml_la_CFLAGS = $(AM_CFLAGS) $(LUA_CFLAGS) @@ -409,6 +409,11 @@ mod_webm_la_SOURCES = mod_webm.c json.c mod_webm_la_LDFLAGS = -module -export-dynamic -avoid-version mod_webm_la_LIBADD = $(common_libadd) +lib_LTLIBRARIES += mod_portal.la +mod_portal_la_SOURCES = mod_portal.c json.c user_hashtable.c user_auth.c +mod_portal_la_LDFLAGS = -module -export-dynamic -avoid-version -L../../../../Platform/build/debug/ +mod_portal_la_LIBADD = $(common_libadd) -ldatabase-linux + hdr = server.h base64.h buffer.h burl.h network.h log.h http_kv.h keyvalue.h \ response.h request.h fastcgi.h chunk.h \ first.h settings.h http_chunk.h \ @@ -423,7 +428,9 @@ hdr = server.h base64.h buffer.h burl.h network.h log.h http_kv.h keyvalue.h \ sys-crypto.h sys-endian.h sys-mmap.h sys-socket.h sys-strings.h \ mod_cml.h mod_cml_funcs.h \ safe_memclear.h sock_addr.h splaytree.h status_counter.h \ - mod_magnet_cache.h + mod_magnet_cache.h \ + user_hashtable.h \ + user_auth.h DEFS= @DEFS@ -DHAVE_VERSIONSTAMP_H -DLIBRARY_DIR="\"$(libdir)\"" -DSBIN_DIR="\"$(sbindir)\"" @@ -465,7 +472,7 @@ lighttpd_SOURCES = \ mod_userdir.c \ mod_usertrack.c \ mod_vhostdb.c \ - mod_webdav.c + mod_webdav.c lighttpd_CPPFLAGS = \ -DLIGHTTPD_STATIC \ $(XML_CFLAGS) $(SQLITE_CFLAGS) \ diff --git a/libs/src/lighttpd-1.4.51/src/hlist.h b/libs/src/lighttpd-1.4.51/src/hlist.h new file mode 100644 index 000000000..aab692980 --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/hlist.h @@ -0,0 +1,140 @@ +#ifndef __HLIST_H +#define __HLIST_H + +struct hlist_head { + struct hlist_node *first; +}; + +struct hlist_node { + struct hlist_node *next, **pprev; +}; + +#ifndef offsetof +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif + +#ifndef container_of +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif + +#define HLIST_HEAD_INIT { .first = NULL } +#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } + +#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) + +static inline void INIT_HLIST_NODE(struct hlist_node *h) +{ + h->next = NULL; + h->pprev = NULL; +} + +static inline int hlist_unhashed(const struct hlist_node *h) +{ + return !h->pprev; +} + +static inline int hlist_empty(const struct hlist_head *h) +{ + return !h->first; +} + +static inline void __hlist_del(struct hlist_node *n) +{ + struct hlist_node *next = n->next; + struct hlist_node **pprev = n->pprev; + *pprev = next; + if (next) + next->pprev = pprev; +} + +static inline void hlist_del(struct hlist_node *n) +{ + __hlist_del(n); + n->next = NULL; + n->pprev = NULL; +} + +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) +{ + struct hlist_node *first = h->first; + n->next = first; + if (first) + first->pprev = &n->next; + h->first = n; + n->pprev = &h->first; +} + + +static inline void hlist_del_init(struct hlist_node *n) +{ + if (!hlist_unhashed(n)) { + __hlist_del(n); + INIT_HLIST_NODE(n); + } +} + +static inline void hlist_add_before(struct hlist_node *n, + struct hlist_node *next) +{ + n->pprev = next->pprev; + n->next = next; + next->pprev = &n->next; + *(n->pprev) = n; +} + +static inline void hlist_add_after(struct hlist_node *n, + struct hlist_node *next) +{ + next->next = n->next; + n->next = next; + next->pprev = &n->next; + + if(next->next) + next->next->pprev = &next->next; +} + +static inline void hlist_move_list(struct hlist_head *old, + struct hlist_head *new) +{ + new->first = old->first; + if (new->first) + new->first->pprev = &new->first; + old->first = NULL; +} + +#define hlist_entry(ptr, type, member) container_of(ptr,type,member) + + +#define hlist_for_each_safe(pos, n, head) \ + for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ + pos = n) + +#define hlist_for_each_entry(tpos, pos, head, member) \ + for (pos = (head)->first; \ + pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_continue(tpos, pos, member) \ + for (pos = (pos)->next; \ + pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_from(tpos, pos, member) \ + for (; pos && ({ prefetch(pos->next); 1;}) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + +#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ + for (pos = (head)->first; \ + pos && ({ n = pos->next; 1; }) && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = n) + + + +#endif + \ No newline at end of file diff --git a/libs/src/lighttpd-1.4.51/src/mod_portal.c b/libs/src/lighttpd-1.4.51/src/mod_portal.c new file mode 100644 index 000000000..79319d5ed --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/mod_portal.c @@ -0,0 +1,471 @@ +#include "first.h" +#include "base_decls.h" +#include "json.h" +#include "user_hashtable.h" +#include "base.h" +#include "plugin.h" +#include "http_auth.h" +#include "http_header.h" +#include "log.h" +#include "dlfcn.h" +#include "connections.h" +#include +#include +#include +#include "user_auth.h" + +#define USERNAME_MAXLEN 65 +#define PASSWORD_MAXLEN 25 + +typedef void* pointer; + +#if 0 +typedef struct user_auth_list +{ + time_t* fail_time; //循环队列存储认证失败时间点 + int front; //循环队列头 + int rear; //循环队列尾 + int max_size; //循环队列的最大存储空间,锁定次数+1(config_fail_num + 1) + + unsigned int online_num; //用户上线数量 + time_t lock_time; //用户锁定时间 + unsigned short group_id; //用户组id + //unsigned int fail_num; //用户认证失败次数 +} USER_AUTH_LIST; + +typedef enum { + AUTH_SUCCESS = 0, + AUTH_FAIL_PASSWD = 1, + AUTH_FAIL_VALID = 2, + AUTH_FAIL_MULTI = 3, + AUTH_FAIL_LOCK = 4, + AUTH_FAIL_LACKINFO = 5, + AUTH_FAIL_INPUT = 6, + AUTH_FAIL_OVER = 7, + AUTH_ERR = 8, + AUTH_FAIL_DATABASE = 9, + +} auth_ret; + +typedef struct user_auth_ret +{ + auth_ret ret; + unsigned short user_id; + unsigned short group_id; + time_t remain_lock_time; +} USER_AUTH_RET; +#endif + +/*输出函数结构体 */ +typedef struct { + auth_ret resultcode; + char *message; /*返回描述用指针表示数组 */ + time_t remain_lock_time; /*锁定剩余时间 */ + int js_location; /*0 location*/ + char *location_url; +}RESULT; + +/*函数指针*/ +typedef void (*mod_portal_cfg_exec_sync)(char* username, char* password, USER_AUTH_RET *auth_result); + +typedef struct { + PLUGIN_DATA; + void *cfgm_lib; + mod_portal_cfg_exec_sync portal_cfg_exec; + USER_AUTH_LIST *portal_auth_list; +} mod_portal_plugin_data; + + +char * mes[]={"SUCCESS", "ErrorUsernameOrpassword", "NotInVaildTime", + "OutMaxOnlineNum", "UserIsLocked", "LackConfigInfo", + "OverMaxOnlineNum", "OtherErr"}; + + +/** + * the basic and digest auth framework + * + * - config handling + * - protocol handling + * + * http_auth.c + * http_auth_digest.c + * + * do the real work + */ +#if 0 +INIT_FUNC(mod_portal_init) { + /* 用户认证 */ + mod_portal_plugin_data *p; + + p = calloc(1, sizeof(*p)); + if (p) + { + if (NULL == (p->cfgm_lib = dlopen("libuserauthapi-linux.so", RTLD_NOW|RTLD_GLOBAL))) + { + free(p); + return NULL; + } + + p->portal_cfg_exec = (void (*)(char* , char* , USER_AUTH_RET*))(intptr_t)dlsym(p->cfgm_lib, "user_auth_login"); + p->portal_auth_list = dlsym(p->cfgm_lib, "g_user_auth_ret_table"); + if (NULL == p->portal_cfg_exec) + { + free(p); + return NULL; + } + } + return p; +} +#endif + +INIT_FUNC(mod_portal_init) { + mod_portal_plugin_data *p; + + p = calloc(1, sizeof(*p)); + + return p; + +} + +/*认证模块释放*/ +FREE_FUNC(mod_portal_free) { + mod_portal_plugin_data *p = p_d; + srv = srv; + if (!p) return HANDLER_GO_ON; + + dlclose(p->cfgm_lib); + free(p); + + return HANDLER_GO_ON; +} + + +/*认证模块处理函数*/ +static handler_t mod_portal_uri_handler(server *srv, connection *con, void* p_d) +{ + p_d = p_d; + //mod_portal_plugin_data *p = p_d; + cJSON *cjson; + USER_AUTH_RET *resultinfo; + char *account = NULL; + char *pwd = NULL; + Init_hash(); /*初始化hash表放在配置恢复处 */ + uint32_t client_ip=10001; /*解析报文拿到用户IP */ + + RESULT *uresult; + uresult = ( RESULT *)malloc(sizeof(RESULT)); + if(NULL == uresult) + { + return HANDLER_ERROR; + } + + resultinfo = (USER_AUTH_RET *)malloc(sizeof(USER_AUTH_RET)); + if (NULL == resultinfo) + { + return HANDLER_ERROR; + } + + /*method=get,return HANDLER_GO_ON*/ + if(con->request.http_method == HTTP_METHOD_GET) + { + USER_INFO *uinfo; + uinfo = (USER_INFO *)malloc(sizeof(USER_INFO)); + if (NULL == uinfo) + { + return HANDLER_ERROR; + } + + if (0 == strcmp(con->uri.path->ptr, "/ISG-authsuccess")) + { + ufind_user(client_ip, uinfo); + if (NULL == uinfo) + { + buffer *return_info = buffer_init(); + + return_info = buffer_init_string(""); + chunkqueue_append_buffer(con->write_queue, return_info); + buffer_free(return_info); + con->http_status = 200; + con->file_finished = 1; + return HANDLER_FINISHED; + } + } + else if (0 == strcmp(con->uri.path->ptr, "/ISG-auth")) + { + ufind_user(client_ip, uinfo); + if (NULL != uinfo) + { + buffer *return_info = buffer_init(); + return_info = buffer_init_string(""); + chunkqueue_append_buffer(con->write_queue, return_info); + buffer_free(return_info); + con->http_status = 200; + con->file_finished = 1; + return HANDLER_FINISHED; + } + } + return HANDLER_GO_ON; + } + + + /*method=post handle*/ + if(con->request.http_method == HTTP_METHOD_POST) + { + /*get payload*/ + handler_t result = connection_handle_read_post_state(srv, con); + if (result != HANDLER_GO_ON) return result ; + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + buffer *b = buffer_init(); + chunkqueue *dst_cq = con->request_content_queue; + chunk *c = dst_cq->first; + if (NULL == c) return HANDLER_ERROR; + + while(c != NULL) + { + buffer_append_string(b, c->mem->ptr + c->offset); + c = c->next; + } + + log_error_write(srv, __FILE__, __LINE__, "sb","test",b); + + /*JSON字符串到JSON格式 */ + cjson = cJSON_Parse(b->ptr); + if(!cjson) + { + return HANDLER_ERROR; + } + + /*get username */ + cJSON *uitem = cJSON_GetObjectItem(cjson , "account"); + if(!uitem) + { + return HANDLER_ERROR; + } + + account= uitem->valuestring; + log_error_write(srv, __FILE__, __LINE__, "ss","test",account); + + if( strlen(account) > USERNAME_MAXLEN ) + { + cJSON_Delete(uitem); + cJSON_Delete(cjson); + free(account); + return HANDLER_ERROR; + } + + /*get password */ + cJSON *pitem = cJSON_GetObjectItem(cjson , "pwd"); + if(!pitem) + { + return HANDLER_ERROR; + } + + pwd =pitem->valuestring; + log_error_write(srv, __FILE__, __LINE__, "ss","test",pwd); + if( strlen(pwd) > PASSWORD_MAXLEN ) + { + cJSON_Delete(pitem); + cJSON_Delete(cjson); + free(pwd); + return HANDLER_ERROR; + } + + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + /*调用认证接口函数 */ + user_auth_login(account, pwd, resultinfo); + + #if 0 + if ( p->portal_cfg_exec) + { + p->portal_cfg_exec(account, pwd, resultinfo); + } + #endif + + //resultinfo->ret = AUTH_SUCCESS; + + /*auth success*/ + if (resultinfo->ret == AUTH_SUCCESS) + { + cJSON *res; + const char *result_str; + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + /*auth success-用户信息保存在本地IP监测表*/ + /*获取下行报文数、字节数、在线时间*/ + uadd_user(client_ip, account, resultinfo->user_id, resultinfo->group_id, 100, 100, 100); + uprintf_users(); + + + uresult->resultcode = resultinfo->ret; + uresult->remain_lock_time = 0; + uresult->message = mes[resultinfo->ret]; + uresult->js_location = 0; + uresult->location_url = ""; + + printf("resultcode:%d remain_lock_time:%ld message:%s\n",uresult->resultcode, + uresult->remain_lock_time, uresult->message ); + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + /*创建json对象*/ + res = cJSON_CreateObject(); + if(!res) return HANDLER_ERROR; + + cJSON_AddNumberToObject(res, "resultcode", uresult->resultcode); + cJSON_AddStringToObject(res, "message", uresult->message); + cJSON_AddNumberToObject(res, "remain_lock_time", uresult->remain_lock_time); + cJSON_AddNumberToObject(res, "js_location", uresult->js_location); + cJSON_AddStringToObject(res, "location_url", uresult->location_url); + + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + /*json对象转换为json字符串*/ + result_str = cJSON_PrintUnformatted(res); + buffer *result_info = buffer_init(); + result_info = buffer_init_string(result_str); + chunkqueue_append_buffer(con->write_queue, result_info); + buffer_free(result_info); + con->http_status = 200; + con->file_finished = 1; + cJSON_Delete(cjson); + cJSON_Delete(res); + return HANDLER_FINISHED; + + + #if 0 + /*1.跳转到认证成功界面*/ + buffer *return_info = buffer_init(); + + return_info = buffer_init_string(""); + chunkqueue_append_buffer(con->write_queue, return_info); + buffer_free(return_info); + + + /*2.跳转到认证之间的界面 con->request.http.host*/ + /**/ + #endif + + log_error_write(srv, __FILE__, __LINE__, "s","test"); + } + + + /*认证锁定*/ + if (resultinfo->ret == AUTH_FAIL_LOCK) + { + log_error_write(srv, __FILE__, __LINE__, "s","test"); + cJSON *res; + const char *result_str; + + uresult->resultcode = resultinfo->ret; + uresult->remain_lock_time = resultinfo->remain_lock_time; + uresult->message = mes[resultinfo->ret]; + uresult->js_location = 1; + uresult->location_url = "NULL"; + printf("resultcode:%d remain_lock_time:%ld message:%s\n",uresult->resultcode, + uresult->remain_lock_time, uresult->message ); + + /*创建json对象*/ + res = cJSON_CreateObject(); + if(!res) return HANDLER_ERROR; + + cJSON_AddNumberToObject(res, "resultcode", uresult->resultcode); + cJSON_AddStringToObject(res, "message", uresult->message); + cJSON_AddNumberToObject(res, "remain_lock_time", uresult->remain_lock_time); + cJSON_AddNumberToObject(res, "js_location", uresult->js_location); + cJSON_AddStringToObject(res, "location_url", uresult->location_url); + + /*json对象转换为json字符串*/ + result_str = cJSON_PrintUnformatted(res); + buffer *result_info = buffer_init(); + result_info = buffer_init_string(result_str); + chunkqueue_append_buffer(con->write_queue, result_info); + buffer_free(result_info); + con->http_status = 200; + con->file_finished = 1; + cJSON_Delete(cjson); + cJSON_Delete(res); + return HANDLER_FINISHED; + + } + + + /*认证失败*/ + if ( (resultinfo->ret != AUTH_SUCCESS) && (resultinfo->ret != AUTH_FAIL_LOCK)) + { + printf("auth fail\n"); + log_error_write(srv, __FILE__, __LINE__, "s","test"); + + cJSON *res; + const char *result_str; + + uresult->resultcode = resultinfo->ret; + uresult->remain_lock_time = 0; + uresult->message = mes[resultinfo->ret]; + uresult->js_location = 1; + uresult->location_url = "NULL"; + printf("resultcode:%d remain_lock_time:%ld message:%s\n",uresult->resultcode, + uresult->remain_lock_time, uresult->message ); + + /*创建json对象*/ + res = cJSON_CreateObject(); + if(!res) return HANDLER_ERROR; + + cJSON_AddNumberToObject(res, "resultcode", uresult->resultcode); + cJSON_AddStringToObject(res, "message", uresult->message); + cJSON_AddNumberToObject(res, "remain_lock_time", uresult->remain_lock_time); + cJSON_AddNumberToObject(res, "js_location", uresult->js_location); + cJSON_AddStringToObject(res, "location_url", uresult->location_url); + + /*json对象转换为json字符串*/ + result_str = cJSON_PrintUnformatted(res); + buffer *result_info = buffer_init(); + result_info = buffer_init_string(result_str); + + chunkqueue_append_buffer(con->write_queue, result_info); + buffer_free(result_info); + con->http_status = 200; + con->file_finished = 1; + cJSON_Delete(cjson); + cJSON_Delete(res); + return HANDLER_FINISHED; + + } + } + + return HANDLER_GO_ON; +} + + +SETDEFAULTS_FUNC(mod_portal_set_defaults) +{ + mod_portal_plugin_data *p = p_d; + p = p;//解决编译告警; + srv = srv;//解决编译告警; + + return HANDLER_GO_ON; +} + + +int mod_portal_plugin_init(plugin *p); +int mod_portal_plugin_init(plugin *p) { + p->version = LIGHTTPD_VERSION_ID; + p->name = buffer_init_string("portal"); + p->init = mod_portal_init; + p->set_defaults = mod_portal_set_defaults; + p->handle_uri_clean = mod_portal_uri_handler; + p->cleanup = mod_portal_free; + p->data = NULL; + + return 0; +} + diff --git a/libs/src/lighttpd-1.4.51/src/user_auth.c b/libs/src/lighttpd-1.4.51/src/user_auth.c new file mode 100644 index 000000000..5051ab13e --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/user_auth.c @@ -0,0 +1,619 @@ +#include +#include +#include +#include +#include +#include +#include "user_auth.h" +#include "../../../Platform/common/database/database.h" + +#define NOT_LOCK 0 +#define DATA_EMPTY 0 +#define AUTH_INIT_FAIL -1 +#define AUTH_INIT_SUCCESS 0 +#define AUTH_USER_INDEX_MAX (100 + 2) + +#define UNAMESIZE (127 + 1) +#define UDESIZE (127 + 1) +#define UPWDSIZE (63 + 1) + +typedef struct user_auth +{ + unsigned short ID; //用户id + char uname[UNAMESIZE]; //用户名 + char udescription[UDESIZE]; //用户描述 + unsigned short GID; //用户组ID + char passwd[UPWDSIZE]; //密码 + unsigned short multi_valid; //多人登陆、永久有效 + time_t valid_begin_time; //有效期开始时间 + time_t valid_end_time; //有效期结束时间 +}USERACCOUNT; + +#define JSON_URL "/nasdata/zhouzian/secogateway/Product/user/user_manager/usermanager-auth/user_json.json" +#define AUTH_RECORD (g_user_auth_ret_table[user_id]) + +#define AUTH_TIME_T2STRING(time_int, time_char) (strftime((time_char), 20, "%Y-%m-%d %H:%M:%S", (localtime(&time_int)))) +#define AUTH_STRING2TIME_T(time_char,time_int) \ +do { \ + struct tm tm_time; \ + int res = sscanf(time_char, "%4d-%2d-%2d %2d:%2d:%2d", \ + &tm_time.tm_year, &tm_time.tm_mon, &tm_time.tm_mday, \ + &tm_time.tm_hour, &tm_time.tm_min, &tm_time.tm_sec); \ + tm_time.tm_year -= 1900; \ + tm_time.tm_mon--; \ + tm_time.tm_isdst = -1; \ + time_int = mktime(&tm_time); \ +} while (0) + +#define AUTH_MULTI_MASK 0x0002 +#define AUTH_VALID_MASK 0x0001 + +#define AUTH_MULTI_GET(element) ((element) >> 1) +#define AUTH_MULTI_SET(element, value) (((element) & AUTH_VALID_MASK) | (((value) << 1) & AUTH_MULTI_MASK)) +#define AUTH_VALID_GET(element) ((element) & AUTH_VALID_MASK) +#define AUTH_VALID_SET(element, value) (((element) & AUTH_MULTI_MASK) | ((value) & AUTH_VALID_MASK)) + +#define xfree(X) \ + if(X){ \ + free(X); \ + X = NULL; \ + }; \ + +/* 定义用户认证结果记录表 */ +USER_AUTH_LIST g_user_auth_ret_table[AUTH_USER_INDEX_MAX] = { 0 }; + +/* +* config_lock_time 锁定后-时间,单位(分钟) +* config_fail_num 锁定前-次数 +* config_fail_time 锁定前-时间,单位(分钟) +*/ +static int g_config_lock_time, g_config_fail_num, g_config_fail_time; + +/* 创建认证失败时间队列 */ +static int init_fail_time_queue(unsigned short user_id, int max_size) +{ + AUTH_RECORD.fail_time = (time_t *)malloc(sizeof(time_t) * max_size); + if (NULL == AUTH_RECORD.fail_time) + { + //记录日志,申请内存失败 + return AUTH_INIT_FAIL; + } + AUTH_RECORD.max_size = max_size; + + return AUTH_INIT_SUCCESS; +} + +/* 清空认证失败时间队列,不释放内存 */ +static void empty_fail_time_queue(unsigned short user_id) +{ + if(NULL != AUTH_RECORD.fail_time) + { + memset(AUTH_RECORD.fail_time, 0, sizeof(AUTH_RECORD.fail_time)); + } + AUTH_RECORD.front = 0; + AUTH_RECORD.rear = 0; + AUTH_RECORD.lock_time = 0; +} + +/* 判断队列为空 */ +static bool queue_is_empty(unsigned short user_id) +{ + if (AUTH_RECORD.front == AUTH_RECORD.rear) + { + return true; + } + return false; +} + +/* 判断队列为满 */ +static bool queue_is_full(unsigned short user_id) +{ + if (AUTH_RECORD.front == (AUTH_RECORD.rear + 1) % AUTH_RECORD.max_size) + { + return true; + } + return false; +} + +/* 认证失败时间队列删除数据 */ +static void de_fail_time_queue(unsigned short user_id) +{ + if (queue_is_empty(user_id)) + { + return; + } + AUTH_RECORD.front = (AUTH_RECORD.front + 1) % AUTH_RECORD.max_size; +} + +/* 认证失败时间队列添加数据 */ +static void en_fail_time_queue(unsigned short user_id, time_t value) +{ + //满了,删front + if (queue_is_full(user_id)) + { + de_fail_time_queue(user_id); + } + AUTH_RECORD.fail_time[AUTH_RECORD.rear] = value; + AUTH_RECORD.rear = (AUTH_RECORD.rear + 1) % AUTH_RECORD.max_size; + return; +} + +/* 认证失败后的处理 */ +static void auth_fail_operate(unsigned short user_id, time_t login_time, int config_fail_time) +{ + time_t time_from_front; //单位:秒 + + //AUTH_RECORD.fail_num++; + //计算当前时间到front下标下的时间段,添加当时失败时间到queueu + if (queue_is_empty(user_id)) + { + time_from_front = 0; + } + else + { + time_from_front = login_time - AUTH_RECORD.fail_time[AUTH_RECORD.front]; + } + en_fail_time_queue(user_id, login_time); + + //队列已经满,且时间小于配置的失败时间,锁定用户 + if (queue_is_full(user_id) && (time_from_front < (int)(60.0 * config_fail_time))) + { + //锁定用户,设置锁定时间 + AUTH_RECORD.lock_time = login_time; + } +} + +/* 查询json文件数据 */ +// static void get_from_json(char *user_name, USERACCOUNT *user_info) +// { +// FILE* f; +// long len; //文件长度 +// char* content; //文件内容 +// cJSON* root, * user_body; +// int array_size; //用户个数 +// time_t time_begin = 0; +// time_t time_end = 0; + +// if (NULL == user_name) +// { +// user_info = NULL; +// return; +// } + +// memset(user_info, 0, sizeof(USERACCOUNT)); + +// f = fopen(JSON_URL, "rb"); +// fseek(f, 0, SEEK_END); +// len = ftell(f); +// fseek(f, 0, SEEK_SET); +// content = (char*)malloc(len + 1); +// fread(content, 1, len, f); +// fclose(f); + +// root = cJSON_Parse(content); +// if (!root) +// { +// printf("Error before: [%s]\n", cJSON_GetErrorPtr()); +// } + +// array_size = cJSON_GetArraySize(root); + +// for (int i = 0; i < array_size; i++) +// { +// user_body = cJSON_GetArrayItem(root, i); +// //获取用户名 +// char* user_name_temp = cJSON_GetObjectItem(user_body, "user_name")->valuestring; +// if (0 == strcmp(user_name, user_name_temp)) +// { +// /* 转存用户信息,返回 */ +// user_info->ID = cJSON_GetObjectItem(user_body, "id")->valueint; +// user_info->GID = cJSON_GetObjectItem(user_body, "group_id")->valueint; +// user_info->multi_valid = cJSON_GetObjectItem(user_body, "multi_valid")->valueint; + +// AUTH_STRING2TIME_T(cJSON_GetObjectItem(user_body, "valid_begin_time")->valuestring, time_begin); +// AUTH_STRING2TIME_T(cJSON_GetObjectItem(user_body, "valid_end_time")->valuestring, time_end); +// user_info->valid_begin_time = time_begin; +// user_info->valid_end_time = time_end; + +// strcpy(user_info->uname, cJSON_GetObjectItem(user_body, "user_name")->valuestring); +// strcpy(user_info->passwd, cJSON_GetObjectItem(user_body, "password")->valuestring); + +// xfree(content); +// cJSON_Delete(root); +// return; +// } +// } + +// /* 未查到用户名,释放内存*/ +// xfree(content); +// cJSON_Delete(root); +// user_info = NULL; +// } + +bool get_user_from_database(char* username, void* hdbc, USERACCOUNT* user_info, int* num_sql) +{ + char * ret_sql = NULL; + + if(NULL == username || NULL == hdbc || NULL == user_info || NULL == num_sql) + { + return false; + } + + char * select_sql = "SELECT id, group_id, multi_player, valid_always, user_name,password, udescription,valid_begin_time,valid_end_time FROM `user_account`WHERE user_name = ?"; + ret_sql = select_datebase_by_number(20, hdbc, "user_account", select_sql, 1, 0, num_sql, 1, + DB_DATA_STRING_TYPE, strlen(username)+1, username); + + if(0 == *num_sql || NULL == ret_sql) + { + return true; + } +/* +{ + "data": [{ + "id": 5, + "group_id": 5, + "multi_player": 0, + "valid_always": 0, + "user_name": "用户07", + "udescription": "", + "valid_begin_time": "", + "valid_end_time": "" + }] +} +*/ + cJSON * root = cJSON_Parse(ret_sql); + if(!root) + { + return false; + } + + cJSON * data = cJSON_GetObjectItem(root, "data"); + if(!data) + { + cJSON_Delete(root); + return false; + } + + int data_num = cJSON_GetArraySize(data); + if (1 != data_num) + { + cJSON_Delete(root); + return false; + } + + cJSON * user_json = cJSON_GetArrayItem(data, 0); + if(!user_json) + { + cJSON_Delete(root); + return false; + } + + /* 解析各个数据项 */ + cJSON * id = cJSON_GetObjectItem(user_json, "id"); + if(!id) + { + cJSON_Delete(root); + return false; + } + user_info->ID = id->valueint; + + cJSON * group_id = cJSON_GetObjectItem(user_json, "group_id"); + if(!group_id) + { + cJSON_Delete(root); + return false; + } + user_info->GID = group_id->valueint; + + cJSON * user_name = cJSON_GetObjectItem(user_json, "user_name"); + if(!user_name) + { + cJSON_Delete(root); + return false; + } + strcpy(user_info->uname, user_name->valuestring); + + + cJSON * udescription = cJSON_GetObjectItem(user_json, "udescription"); + if(!udescription) + { + strcpy(user_info->udescription, ""); + }else + { + strcpy(user_info->udescription, udescription->valuestring); + } + + + cJSON * password = cJSON_GetObjectItem(user_json, "password"); + if(!password) + { + cJSON_Delete(root); + return false; + } + strcpy(user_info->passwd, password->valuestring); + + cJSON * multi_player = cJSON_GetObjectItem(user_json, "multi_player"); + if(!multi_player) + { + cJSON_Delete(root); + return false; + } + user_info->multi_valid = AUTH_MULTI_SET(user_info->multi_valid, multi_player->valueint); + + cJSON * valid_always = cJSON_GetObjectItem(user_json, "valid_always"); + if(!valid_always) + { + cJSON_Delete(root); + return false; + } + user_info->multi_valid = AUTH_VALID_SET(user_info->multi_valid, valid_always->valueint); + + cJSON * valid_begin_time = cJSON_GetObjectItem(user_json, "valid_begin_time"); + if(!valid_begin_time) + { + if (1 == valid_always->valueint) + { + cJSON_Delete(root); + return false; + } + user_info->valid_begin_time = 0; + } + AUTH_TIME_T2STRING(user_info->valid_begin_time, valid_begin_time->valuestring); + + cJSON * valid_end_time = cJSON_GetObjectItem(user_json, "valid_end_time"); + if(!valid_end_time) + { + if (1 == valid_always->valueint) + { + cJSON_Delete(root); + return false; + } + user_info->valid_end_time = 0; + } + AUTH_TIME_T2STRING(user_info->valid_end_time, valid_end_time->valuestring); + + cJSON_Delete(root); + return true; +} + +/* 用户认证 */ +void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result) +{ + unsigned short user_id, group_id; + int init_queue_ret; //初始化循环列表的结果 + int user_valid; //数据库中的数据 + int config_lock_time = 0; //锁定后的锁定时间,锁定后 + int config_fail_num = 0; //规定时间内允许失败的次数,锁定次数,锁定前 + int config_fail_time = 0; //规定时间,失败的时间范围,锁定前 + time_t login_time; //登陆时间 + time_t remain_lock_time; //锁定剩余时间 + USERACCOUNT *user_info; //临时数据,存储登陆用户名对应的用户信息 + void * auth_hdbc; + int sql_num; + + memset(auth_result, 0, sizeof(USER_AUTH_RET)); + login_time = time(NULL); + + //1、校验用户名和密码 + if (NULL == username || NULL == password || 0 == login_time) + { + auth_result->ret = AUTH_FAIL_INPUT; + return; + } + + /* 连接数据库 */ + auth_hdbc = connect_database(20); + if(NULL == auth_hdbc) + { + auth_result->ret = AUTH_FAIL_DATABASE; + return; + } + + //2、数据库查询配置数据 + /*if (false) + { + auth_result->ret = AUTH_FAIL_LACKINFO; + return auth_result; + }*/ + + config_lock_time = 2; + config_fail_num = 5; + config_fail_time = 40; + /* 校验上述的三个参数都要大于0 */ + + //3、根据用户名查询用户信息-用户id和用户组id + user_info = (USERACCOUNT*)malloc(sizeof(USERACCOUNT)); + memset(user_info, 0, sizeof(USERACCOUNT)); + if (NULL == user_info) + { + /* 记录日志 */ + //printf("user_auth()->user_auth->user_info:error. \n"); + auth_result->ret = AUTH_ERR; + return; + } + //读取json文件获取数据 + // get_from_json(username, user_info); + // if (NULL == user_info) + // { + // auth_result->ret = AUTH_FAIL_PASSWD; + // xfree(user_info); + // return; + // } + + /* 数据库查询 */ + bool ret_getuser = get_user_from_database(username, auth_hdbc, user_info, &sql_num); + if(!ret_getuser) + { + auth_result->ret = AUTH_FAIL_DATABASE; + return; + } + + if(0 == sql_num || NULL == user_info) + { + auth_result->ret = AUTH_FAIL_PASSWD; + return; + } + + user_id = user_info->ID; + group_id = user_info->GID; + + //4、初始化用户认证结果记录表对应id内的循环队列 + if (DATA_EMPTY == AUTH_RECORD.max_size) + { + g_config_lock_time = config_lock_time; + g_config_fail_time = config_fail_time; + g_config_fail_num = config_fail_num; + + init_queue_ret = init_fail_time_queue(user_id, config_fail_num + 1); + if (AUTH_INIT_FAIL == init_queue_ret) + { + auth_result->ret = AUTH_ERR; + xfree(user_info); + return; + } + } + + /* 如果用户锁定的配置数据发生修改 */ + if (g_config_lock_time != config_lock_time || + g_config_fail_time != config_fail_time || g_config_fail_num != config_fail_num) + { + xfree(AUTH_RECORD.fail_time); + g_config_lock_time = config_lock_time; + g_config_fail_time = config_fail_time; + g_config_fail_num = config_fail_num; + + init_queue_ret = init_fail_time_queue(user_id, config_fail_num + 1); + if (AUTH_INIT_FAIL == init_queue_ret) + { + auth_result->ret = AUTH_ERR; + xfree(user_info); + return; + } + empty_fail_time_queue(user_id); + } + + //5、判断用户是否锁定 + if (NOT_LOCK != AUTH_RECORD.lock_time)//锁定 + { + remain_lock_time = login_time - AUTH_RECORD.lock_time; + if (remain_lock_time < 0) + { + auth_result->ret = AUTH_FAIL_INPUT; + xfree(user_info); + return; + } + if ((int)(60.0 * config_lock_time) > 60 *remain_lock_time) + { + auth_result->ret = AUTH_FAIL_LOCK; + auth_result->remain_lock_time = remain_lock_time; + xfree(user_info); + return; + } + //锁定时间已过,解锁,清空该队列 + empty_fail_time_queue(user_id); + } + + //6、判断是否在有效期内 + user_valid = AUTH_VALID_GET(user_info->multi_valid); + if (1 == user_valid) + { + if (login_time < user_info->valid_begin_time || login_time > user_info->valid_end_time) + { + auth_result->ret = AUTH_FAIL_VALID; + /* 认证失败处理 */ + auth_fail_operate(user_id, login_time, config_fail_time); + xfree(user_info); + return; + } + } + + //7、判断在线用户是否到最大值 + if (AUTH_USER_INDEX_MAX - 2 <= AUTH_RECORD.online_num) + { + auth_result->ret = AUTH_FAIL_OVER; + + /* 认证失败处理 */ + auth_fail_operate(user_id, login_time, config_fail_time); + xfree(user_info); + return; + } + + //8、匹配密码 + if (0 != strcmp(password, user_info->passwd)) + { + auth_result->ret = AUTH_FAIL_PASSWD; + + /* 认证失败处理 */ + auth_fail_operate(user_id, login_time, config_fail_time); + xfree(user_info); + return; + } + + //9、认证成功处理 + AUTH_RECORD.group_id = group_id; //更新用户组id + empty_fail_time_queue(user_id); + AUTH_RECORD.online_num++; + + auth_result->ret = AUTH_SUCCESS; + auth_result->user_id = user_id; + auth_result->group_id = group_id; + + disconnect_database(20, auth_hdbc); + xfree(user_info); + return; +} + +/* 用户下线数-1 */ +void reduce_online_num(unsigned short user_id) +{ + if(AUTH_RECORD.online_num > 0) + { + AUTH_RECORD.online_num--; + } +} + +/* 用户在线节点重置-按用户id */ +void reset_online_by_userid(int *user_ids, int num) +{ + if(NULL == user_ids || 0 == num) + { + return; + } + + int user_id_temp = 0; + for(int i = 0; i < num; i++) + { + user_id_temp = user_ids[i]; + if(0 != user_ids[i]) + { + empty_fail_time_queue(user_id_temp); + g_user_auth_ret_table[user_id_temp].fail_time = 0; + g_user_auth_ret_table[user_id_temp].group_id = 0; + g_user_auth_ret_table[user_id_temp].max_size = 0; + g_user_auth_ret_table[user_id_temp].online_num = 0; + } + } +} + +/* 用户在线节点重置-按用户组id */ +void reset_online_by_groupid(int *group_ids) +{ + if(NULL == group_ids) + { + return; + } + + unsigned short group_id_temp = 0; + for(int i = 0; i < AUTH_USER_INDEX_MAX; i++) + { + group_id_temp = g_user_auth_ret_table[i].group_id; + if(0 != group_id_temp && group_id_temp == group_ids[group_id_temp]) + { + empty_fail_time_queue(i); + g_user_auth_ret_table[i].fail_time = 0; + g_user_auth_ret_table[i].group_id = 0; + g_user_auth_ret_table[i].max_size = 0; + g_user_auth_ret_table[i].online_num = 0; + } + } +} \ No newline at end of file diff --git a/libs/src/lighttpd-1.4.51/src/user_auth.h b/libs/src/lighttpd-1.4.51/src/user_auth.h new file mode 100644 index 000000000..53bba5eee --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/user_auth.h @@ -0,0 +1,67 @@ +#ifndef USER_AUTH_ +#define USER_AUTH_ + +#include + +typedef enum { + AUTH_SUCCESS = 0, + AUTH_FAIL_PASSWD = 1, + AUTH_FAIL_VALID = 2, + AUTH_FAIL_MULTI = 3, + AUTH_FAIL_LOCK = 4, + AUTH_FAIL_LACKINFO = 5, + AUTH_FAIL_INPUT = 6, + AUTH_FAIL_OVER = 7, + AUTH_ERR = 8, + AUTH_FAIL_DATABASE = 9, + +} auth_ret; + +#define USER_AUTH_RET_ERROR_DISC \ +{ \ + { AUTH_SUCCESS, "SUCCESS" },\ + { AUTH_FAIL_PASSWD, "ErrorUsernameOrPasswd" },\ + { AUTH_FAIL_VALID, "NotInValidTime" },\ + { AUTH_FAIL_MULTI, "OutMaxOnlineNum" },\ + { AUTH_FAIL_LOCK, "UserIsLocked" },\ + { AUTH_FAIL_LACKINFO, "LackConfigInfo" },\ + { AUTH_FAIL_INPUT, "InputError"},\ + { AUTH_FAIL_OVER, "OverMaxOnlineNum"},\ + { AUTH_ERR, "OtherErr"},\ + { AUTH_FAIL_DATABASE, "FailWithDatabase"}\ +} + +typedef struct user_auth_list +{ + time_t* fail_time; //循环队列存储认证失败时间点 + int front; //循环队列头 + int rear; //循环队列尾 + int max_size; //循环队列的最大存储空间,锁定次数+1(config_fail_num + 1) + + unsigned int online_num; //用户上线数量 + time_t lock_time; //用户锁定时间 + unsigned short group_id; //用户组id + //unsigned int fail_num; //用户认证失败次数 +} USER_AUTH_LIST; + +typedef struct user_auth_ret +{ + auth_ret ret; + unsigned short user_id; + unsigned short group_id; + time_t remain_lock_time; +} USER_AUTH_RET; + +/* 用户认证 */ +void user_auth_login(char* username, char* password, USER_AUTH_RET* auth_result); + +/* 用户在线数-1 */ +void reduce_online_num(unsigned short user_id); + +/* 用户在线节点重置-按用户id, num为user_ids数组长度 */ +void reset_online_by_userid(int *user_ids, int num); + +/* 用户在线节点重置-按用户组id */ +void reset_online_by_groupid(int *group_ids); + +#endif \ No newline at end of file diff --git a/libs/src/lighttpd-1.4.51/src/user_hashtable.c b/libs/src/lighttpd-1.4.51/src/user_hashtable.c new file mode 100644 index 000000000..8e663bc17 --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/user_hashtable.c @@ -0,0 +1,199 @@ +#include +#include +#include +#include +#include +#include +#include "hlist.h" +#include "user_hashtable.h" +#include "user_auth.h" + + +extern USER_AUTH_LIST g_user_auth_ret_table[] ; + +/*链表全局变量 */ +struct hlist_head *hash; +USER_INFO *pNode ; + + +/*计算hash值 */ +struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip) +{ + unsigned int val = ip % 100; + //printf("val =%d\n", val); + return &hash[val]; +} + + +/*初始化函数 */ +int Init_hash() +{ + int i = 0; + + /*创建hash头 */ + hash = (struct hlist_head*)malloc(sizeof(*hash)*100); + if(NULL == hash) + { + printf("alloc error\n"); + return -1; + } + + /*初始化hash表头 */ + for(i = 0; i < 100; i++) + INIT_HLIST_HEAD(&hash[i]); + + + /*hsah桶普通节点分配内存 */ + pNode = (struct user_info *)malloc(sizeof(struct user_info)); + if (NULL == pNode) + { + printf("alloc error\n"); + return -1; + } + + /*初始化hash桶的普通结点 */ + memset(pNode,0,sizeof(struct user_info)); + INIT_HLIST_NODE(&pNode->hnode); + + return 0; +} + + +/*查找用户信息*/ +void ufind_user(uint32_t user_ip, USER_INFO *userinfo) +{ + + struct hlist_node *p = NULL, *n = NULL ; + + /* 这个实际上就是一个for循环,从头到尾遍历链表。 + * pos:struct hlist_node类型的一个指针; + * n:struct hlist_node类型的一个指针; + * head:struct hlist_head类型的一个指针,表示hlist链表的头结点。 + */ + + hlist_for_each_safe(p,n,call_hash(hash,user_ip)) + { + + /* p:表示struct hlist_node类型的一个地址。 + * struct user_info:结构体名 + * hnode:type结构体中的hlist_node成员变量的名称 + * 表示得到p所指地址的这个结构体的首地址 + */ + pNode = hlist_entry(p, struct user_info ,hnode); + if(pNode != NULL) + { + userinfo = pNode; + printf("[%d %s %d %d %ld %ld %ld]\n",userinfo->auth_user.user_ip, userinfo->auth_user.user_name, userinfo->auth_user.user_id, + userinfo->auth_user.group_id, userinfo->auth_user.message_num,userinfo->auth_user.byte_num, userinfo->auth_user.online_time); + return; + } + + + return ; + } + +} + + +/*增加用户信息*/ +int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t message_num, uint64_t byte_num, time_t online_time) +{ + struct hlist_node *pos = NULL, *n = NULL ; + + pNode = NULL; + struct hlist_head* pVal = call_hash(hash, user_ip); + //printf("pVal = %p\n", pVal); + + //hlist_for_each_safe(p,n,call_hash(hash, user_ip)) /*查找ip是否存在hash表中 */ + for (pos = pVal->first; pos && ({ n = pos->next; 1; }); pos = n) + { + pNode = hlist_entry(pos, struct user_info ,hnode); + if(pNode != NULL) + printf("IP ALEADY EXISTED\n"); + } + + if (pNode == NULL) + { + pNode = (struct user_info *)malloc(sizeof(struct user_info)); + if (NULL == pNode) + { + return -1; + } + memset(pNode,0,sizeof(struct user_info)); + INIT_HLIST_NODE(&pNode->hnode); + pNode->auth_user.user_ip = user_ip; + hlist_add_head(&pNode->hnode, call_hash(hash, user_ip)); + } + + memcpy(pNode->auth_user.user_name, name, sizeof(char) * 32); + pNode->auth_user.user_id = user_id; + pNode->auth_user.group_id = group_id; + pNode->auth_user.message_num = message_num; + pNode->auth_user.byte_num = byte_num; + pNode->auth_user.online_time = online_time; + + return 0; + +} + +/*删除用户信息 */ +void udelete_user(int user_ip) +{ + + struct hlist_node *p = NULL, *n = NULL ; + unsigned short check_id; + + hlist_for_each_safe(p,n,call_hash(hash,user_ip)) + { + pNode = hlist_entry(p, struct user_info ,hnode); + + /*查找用户ID,确认ID是否存在 */ + check_id = g_user_auth_ret_table[pNode->auth_user.user_id].group_id; + if(check_id != 0) + { + hlist_del(&pNode->hnode); + } + + free(pNode); + } +} + +/*删除所有的hash节点 */ +void udelete_all() +{ + struct hlist_node *p = NULL, *n = NULL ; + int i = 0; + + for(i = 0; i < 100; i++) + { + hlist_for_each_safe(p,n,&hash[i]) + { + pNode = hlist_entry(p, struct user_info ,hnode); + hlist_del(&pNode->hnode); + free(pNode); + } + } +} + +/*打印所有信息信息 */ +void uprintf_users() +{ + struct hlist_node *p = NULL, *n = NULL ; + int i = 0; + + for(i = 0; i < 100; i++) + { + hlist_for_each_safe(p,n,&hash[i]) + { + char str[32]; + pNode = hlist_entry(p, struct user_info ,hnode); + if(pNode != NULL) + { + inet_ntop(AF_INET, (void *)&(pNode->auth_user.user_ip), str, 32); + printf("[%s %s %d %d %ld %ld %ld]\n", str, pNode->auth_user.user_name, pNode->auth_user.user_id, + pNode->auth_user.group_id, pNode->auth_user.message_num,pNode->auth_user.byte_num, pNode->auth_user.online_time); + } + } + } +} + diff --git a/libs/src/lighttpd-1.4.51/src/user_hashtable.h b/libs/src/lighttpd-1.4.51/src/user_hashtable.h new file mode 100644 index 000000000..e1d4332a7 --- /dev/null +++ b/libs/src/lighttpd-1.4.51/src/user_hashtable.h @@ -0,0 +1,44 @@ +#ifndef K_HASHTABLE_H +#define K_HASHTABLE_H +#include +#include "hlist.h" + +typedef struct online_user{ + uint32_t user_ip; /*用户IP*/ + char user_name[32]; /*用户名, 不允许重名*/ + int user_id; /*用户ID,唯一标识用户*/ + int group_id; /* 用户组ID,唯一标识用户组*/ + uint64_t message_num; /*下行报文数*/ + uint64_t byte_num; /*下行字节数*/ + time_t online_time; /* 在线时间*/ +}ONLINE_USER; + + +typedef struct user_info{ + struct hlist_node hnode; + ONLINE_USER auth_user; +}USER_INFO; + + +/*计算hash值 */ +struct hlist_head *call_hash(struct hlist_head *hash, uint32_t ip); + +/*初始化函数 */ +int Init_hash(); + +/*查找用户信息*/ +void ufind_user(uint32_t user_ip, USER_INFO *user_info); + +/*增加用户信息*/ +int uadd_user(uint32_t user_ip, char *name, int user_id, int group_id, uint64_t message_num, uint64_t byte_num, time_t online_time); + +/*删除用户信息 */ +void udelete_user(int user_ip); + +/*删除所有的hash节点 */ +void udelete_all(); + +/*打印所有信息信息 */ +void uprintf_users(); + +#endif \ No newline at end of file