| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- #include "radius.h"
- #include "paras.h"
- #include "common.h"
- #include "file.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "elog.h"
- #include "string.h"
- #include "stdio.h"
- enum{
- FILE_OK = 0, //文件存在并且有内容
- FILE_NO_CONTENT, //文件存在没有内容
- FILE_NO_EXSIT //文件不存在
- };
- #if 1
- #define LOGE log_e
- #define LOGW log_w
- #define LOGD log_d
- #else
- #define LOGD printf
- #define LOGW printf
- #define LOGE printf
- #endif
- #define RADIUS_CONF "/etc/eth0_radius.conf"
- static inline MiscInfo_t* get_misc(void)
- {
- return &__globalDeviceManage._globalDevInfo.misc;
- }
- static radius_config_t radius={
- .ap_scan=0,
- .key_mgmt="IEEE8021X",
- .eap="PEAP",
- .user="admin",
- .password="XXXXXXXXX",
- .peaplable=0,
- .auth="MSCHAPV2",
- };
- static int check_file(const char * path)
- {
- struct stat sta;
- int fd = stat(path,&sta);
- if(fd < 0)
- {
- return FILE_NO_EXSIT;
- }
- if(sta.st_size == 0)
- {
- return FILE_NO_CONTENT;
- }
- return FILE_OK;
- }
- static void clear_ch(char *buff,int lenth,char ch)
- {
- for(int i= 0; i < lenth;i++)
- {
- if(buff[i] == ch)
- {
- buff[i] = 0;
- }
- }
- }
- static void radius_get_config(char *buff,radius_config_t *radius)
- {
-
- char temp[64] = {0};
- ////get ap_scan=
- char *p_str = strstr(buff,"ap_scan=");
- char *p_endle = strchr(p_str,'\n');
- p_str +=8;
- memcpy(temp,p_str,p_endle-p_str);
- radius->ap_scan = atoi(temp);
-
- //get key_mgmt
- memset(temp,0,64);
- p_str = strstr(buff,"key_mgmt=");
- p_endle = strchr(p_str,'\n');
- p_str +=9;
- memcpy(temp,p_str,p_endle-p_str);
- memcpy(radius->key_mgmt,temp,p_endle-p_str);
- //get eap
- memset(temp,0,64);
- p_str = strstr(buff,"eap=");
- p_endle = strchr(p_str,'\n');
- p_str +=4;
- memcpy(temp,p_str,p_endle-p_str);
- memcpy(radius->eap,temp,p_endle-p_str);
- //get user
- memset(temp,0,64);
- p_str = strstr(buff,"identity=\"");
- p_endle = strchr(p_str,'\n');
- p_str +=10;
-
- memcpy(temp,p_str,p_endle-p_str);
- clear_ch(temp,64,'\"');
- memcpy(radius->user,temp,p_endle-p_str);
- //get password
- memset(temp,0,64);
- p_str = strstr(buff,"password=\"");
- p_endle = strchr(p_str,'\n');
- p_str +=10;
- memcpy(temp,p_str,p_endle-p_str);
- clear_ch(temp,64,'\"');
- memcpy(radius->password,temp,p_endle-p_str);
-
- ////get peaplabel
- memset(temp,0,64);
- p_str = strstr(buff,"peaplabel=");
- p_endle = strchr(p_str,'\n');
- p_str +=10;
- memcpy(temp,p_str,p_endle-p_str);
- clear_ch(temp,64,'\"');
- radius->peaplable = atoi(temp);
- //get auth
- memset(temp,0,64);
- p_str = strstr(buff,"auth=");
- p_endle = strchr(p_str,'\n');
- p_str +=5;
- memcpy(temp,p_str,p_endle-p_str);
- clear_ch(temp,64,'\"');
- memcpy(radius->auth,temp,p_endle-p_str);
- }
- #define CONFIG_FILE "ctrl_interface=/var/run/wpa_supplicant\n"\
- "ap_scan=%d\n"\
- "update_config=1\n"\
- "eapol_version=1\n"\
- "network={\n"\
- " ssid=\"T113-i\"\n"\
- " key_mgmt=%s\n"\
- " eap=%s\n"\
- " identity=\"%s\"\n"\
- " password=\"%s\"\n"\
- " phase1=\"peaplabel=%d\"\n"\
- " phase2=\"auth=%s\"\n"\
- "}\n"
- static radius_auth_t radius_client;
- #define RADIUS_CLIENT_CONFIG_PATH "/etc/radiusclient/radiusclient.conf"
- #define RSDIUS_SEARVEAR_CONFIG_PATH "/etc/radiusclient/servers"
- radius_auth_t get_radius_auth()
- {
- return radius_client;
- }
- void set_radius_auth(radius_auth_t *auth)
- {
- char buffer[512] = {0};
- char temp[300] = {0};
- if(memcmp(auth,&radius_client,sizeof(radius_auth_t)) != 0)
- {
- struct in_addr ipv4_addr;
- if(inet_pton(AF_INET,&auth->authserver[0],&ipv4_addr)==1 && memcmp(&radius_client.authserver[0],auth->authserver,64) != 0)
- {
- sprintf(buffer, "sed -e /authserver/d -i %s", RADIUS_CLIENT_CONFIG_PATH);
- system(buffer);
- memset(buffer,0,512);
- sprintf(temp,"%s %s","authserver",auth->authserver);
- sprintf(buffer, "sed -e \"$ a %s\" -i %s", temp, RADIUS_CLIENT_CONFIG_PATH);
- system(buffer);
- LOGW("acctserver replace %s %s !!!!\n",auth->authserver,radius_client.authserver);
-
- memset(buffer,0,512);
- sprintf(buffer, "sed -e /%s/d -i %s",radius_client.authserver,RSDIUS_SEARVEAR_CONFIG_PATH);
- system(buffer);
-
- memset(buffer,0,512);
- sprintf(temp,"%s %s",auth->authserver,auth->key);
- sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RSDIUS_SEARVEAR_CONFIG_PATH);
- system(buffer);
-
- memset(radius_client.authserver,0,sizeof(radius_client.authserver));
- memcpy(radius_client.authserver,auth->authserver,strlen(auth->authserver));
- memset(&radius_client.key[0],0,sizeof(radius_client.key));
- memcpy(&radius_client.key[0],auth->key,strlen(auth->key));
- }
- memset(buffer,0,512);
- memset(temp,0,300);
- if(inet_pton(AF_INET,&auth->acctserver[0],&ipv4_addr)==1 && memcmp(&radius_client.acctserver[0],auth->acctserver,64) != 0)
- {
- sprintf(buffer, "sed -e /acctserver/d -i %s", RADIUS_CLIENT_CONFIG_PATH);
- system(buffer);
- sprintf(temp,"%s %s","acctserver",auth->acctserver);
- sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RADIUS_CLIENT_CONFIG_PATH);
- system(buffer);
- LOGW("acctserver replace %s %s !!!!\n",auth->acctserver,radius_client.acctserver);
- memset(radius_client.acctserver,0,sizeof(radius_client.acctserver));
- memcpy(radius_client.acctserver,auth->acctserver,strlen(auth->acctserver));
- }
- memset(buffer,0,512);
- memset(temp,0,300);
- if(memcmp(&radius_client.key[0],auth->key,64) != 0)
- {
- sprintf(buffer, "sed -e /%s/d -i %s",radius_client.authserver,RSDIUS_SEARVEAR_CONFIG_PATH);
- system(buffer);
-
- memset(buffer,0,512);
- sprintf(temp,"%s %s",auth->authserver,auth->key);
- sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RSDIUS_SEARVEAR_CONFIG_PATH);
- system(buffer);
-
- memset(&radius_client.key[0],0,sizeof(radius_client.key));
- memcpy(&radius_client.key[0],auth->key,strlen(auth->key));
- }
- sync();
- }
- //auth
- }
- static void check_client_str(char *buffer)
- {
- char *p= NULL;
- p = buffer;
- if(*p == '#')
- return;
- p = strstr(buffer,"authserver");
- if(p)
- {
- p += strlen("authserver");
- while (isspace(*p))
- p++;
- memcpy(radius_client.authserver,p,strlen(p));
- //clean '\n'
- p=&radius_client.authserver[0];
- do{
- if(isspace(*p))
- *p= 0;
- }while(*p++);
- printf("authserver = %s\n",radius_client.authserver);
- return;
- }
- p = strstr(buffer,"acctserver");
- if(p)
- {
- p += strlen("acctserver");
- while (isspace(*p))
- p++;
- memcpy(radius_client.acctserver,p,strlen(p));
- p=&radius_client.acctserver[0];
- do{
- if(isspace(*p))
- *p= 0;
- }while(*p++);
- printf("acctserver = %s\n",radius_client.acctserver);
- return;
- }
- return;
- }
- static void check_server_str(char *buffer)
- {
- char *p= NULL;
- p = buffer;
- if(*p =='#')
- return;
- p = strstr(buffer,radius_client.authserver);
-
- if(p)
- {
- p += strlen(radius_client.authserver);
- while (isspace(*p))
- p++;
- memcpy(radius_client.key,p,strlen(p));
- p=&radius_client.key[0];
- do{
- if(isspace(*p))
- *p= 0;
- }while(*p++);
- printf("key = %s\n",radius_client.key);
- return;
- }
- }
- static int get_radius_cs()
- {
- FILE *file = fopen(RADIUS_CLIENT_CONFIG_PATH,"r");
- char buffer[200] = {0};
- if(file)
- {
- while(fgets(buffer,sizeof(buffer),file) != NULL)
- {
- check_client_str(buffer);
- memset(buffer,0,sizeof(buffer));
- }
- fclose(file);
- file = NULL;
- }else
- {
- printf("%s not exsit!!!\n",RADIUS_CLIENT_CONFIG_PATH);
- }
- file = fopen(RSDIUS_SEARVEAR_CONFIG_PATH,"r");
- if(file)
- {
- while(fgets(buffer,sizeof(buffer),file) != NULL)
- {
- check_server_str(buffer);
- memset(buffer,0,sizeof(buffer));
- }
- fclose(file);
- file = NULL;
- }else
- {
- printf("%s not exsit!!!\n",RSDIUS_SEARVEAR_CONFIG_PATH);
- }
- return -1;
- }
- int radius_init()
- {
- MiscInfo_t *misc=get_misc();
- //检查文件
- int fd = 0;
- char temp[1024] = {0};
- radius.status = misc->radius;
- int ret = check_file(RADIUS_CONF);
- if(FILE_OK != ret)
- {
- sprintf(temp,CONFIG_FILE,radius.ap_scan,
- radius.key_mgmt,radius.eap,radius.user,radius.password,radius.peaplable,radius.auth);
- fd = open(RADIUS_CONF,O_CREAT|O_TRUNC|O_RDWR,0777);
- if(fd < 0)
- {
- LOGE("radius create conf failed!!\n");
- goto failed;
- }
- int size = write(fd,temp,strlen(temp));
- close (fd);
- sync();
- }
- memset(temp,0,1024);
- fd = open(RADIUS_CONF,O_RDONLY,0777);
- if(fd < 0)
- {
- LOGE("radius open conf failed!!\n");
- goto failed;
- }
- int size = read(fd,temp,1024);
- close(fd);
- radius_get_config(temp,&radius);
- get_radius_cs();
- if(misc->radius == 0)
- {
- LOGD("radius status is close!!\n");
- goto failed;
- }
- ////////////////////////////////////////////////////////////////
- sprintf(temp,"wpa_supplicant -i eth0 -c %s -D wired -B",RADIUS_CONF);
- ret = system(temp);
- if(ret)
- {
- LOGE("radius init failed!!\n");
- }
- return 0;
- failed:
- return -1;
- }
- int radius_deinit()
- {
- FILE *fp;
- int pid=0;
- char buf[20]={0};
- char command[200];
- snprintf(command, sizeof(command), "ps -ef | grep -v grep | grep -w -c %s", "wpa_supplicant");
- fp = popen(command, "r");
- if (fp == NULL) {
- LOGE("execute %s failed: %s", command, strerror(errno));
- return 0;
- }
- if (fgets(buf, sizeof(buf), fp)) {
- pid = atoi(buf);
- }
- pclose(fp);
- if(pid)
- {
- #if (CHIP_TYPE == CHIP_T113i)
- system("killall wpa_supplicant");
- #endif
- }
- return 0;
- }
- int radius_config(radius_config_t* cfg)
- {
- MiscInfo_t *misc=get_misc();
- radius.status = cfg->status;
- misc->radius = radius.status;
- paras_save();
- if(cfg->status == 0)
- {
- system("killall wpa_supplicant");
- return 0;
- }
- if(memcmp(cfg->user,&radius.user,sizeof(radius.user)) != 0 || memcmp(cfg->password,&radius.password,sizeof(radius.password)) != 0 )
- {
- char temp[1024];
- memset(radius.user,0,sizeof(radius.user));
- memset(radius.password,0,sizeof(radius.password));
- memcpy(radius.user,cfg->user,strlen(cfg->user));
- memcpy(radius.password,cfg->password,strlen(cfg->password));
- sprintf(temp,CONFIG_FILE,radius.ap_scan,
- radius.key_mgmt,radius.eap,radius.user,radius.password,radius.peaplable,radius.auth);
- int fd = open(RADIUS_CONF,O_CREAT|O_TRUNC|O_RDWR,0777);
- if(fd < 0)
- {
- LOGE("radius create conf failed!!\n");
- return -1;
- }
- int size = write(fd,temp,strlen(temp));
- close (fd);
- sync();
- //kill
- #if (CHIP_TYPE == CHIP_T113i)
- system("killall wpa_supplicant");
- sleep(2);
- memset(temp,0,1024);
- sprintf(temp,"wpa_supplicant -i eth0 -c %s -D wired -B",RADIUS_CONF);
- int ret = system(temp);
- if(ret)
- {
- LOGE("radius init failed!!\n");
- }
- #endif
- }
- return 0;
- }
- radius_config_t radius_get()
- {
- return radius;
- }
|