Browse Source

radius 代码!!

liyi 9 months ago
parent
commit
36a2371f61
8 changed files with 266 additions and 8 deletions
  1. 10 2
      pro/src/app.c
  2. 14 1
      pro/src/appweb_handle.c
  3. 4 0
      pro/src/common.c
  4. 1 0
      pro/src/common.h
  5. 42 0
      pro/src/json_handle.c
  6. 12 0
      pro/src/json_handle.h
  7. 180 3
      pro/src/radius.c
  8. 3 2
      pro/src/radius.h

+ 10 - 2
pro/src/app.c

@@ -36,6 +36,7 @@
 #include "led.h"
 #include "beep.h"
 #include "broadcast.h"
+#include "radius.h"
 
 
 static void* power_thread(void* arg)
@@ -1111,6 +1112,13 @@ static void* power_thread(void* arg)
             }   
 #endif
 
+#ifdef PRJ_BYD_UPS_AC_I1_O1
+            if (_globalPowerMangerTemp->product_ch_id>6 && _globalPowerMangerTemp->product_ch_id<=10)
+            {
+                _globalPowerInfo._power_info.OperAllowFlag=1;
+            }   
+#endif
+
 #ifdef PRJ_2405_AC_I1_O1_OLED
             if (_globalPowerMangerTemp->product_ch_id==4 )
             {
@@ -3455,8 +3463,8 @@ int app_init(void)
         return -1 ;
     }
     log_i("database %s open ok.\n", path);
-
-
+    
+    radius_init();
 #if 1
     if(_globalDeviceManager->_globalDevInfo.product.pwr_type == SmartPDU_AC ||
         _globalDeviceManager->_globalDevInfo.product.pwr_type == SmartPDU_DC)       // single AC

+ 14 - 1
pro/src/appweb_handle.c

@@ -21,6 +21,7 @@
 #include "beep.h"
 #include "led.h"
 #include "broadcast.h"
+#include "radius.h"
 
 #define TOTAL_COUNT 60
 #define FN_MAX  30
@@ -4144,7 +4145,19 @@ static void serviceManagement(void *conn)
     if(strcmp(_serviceManageRequestInfo.type,"listSmtpQuery")==0)
     {
          ack_str = service_mail_ack_to_json(&gdm->mailInfo);
-    }   
+    }else if(strcmp(_serviceManageRequestInfo.type,"listRadius")==0)
+    {
+        radius_config_t conf = radius_get();
+        ack_str = service_radius_ack_to_json(&conf);
+    }else if(strcmp(_serviceManageRequestInfo.type,"radiusSave")==0)
+    {
+        radius_config_t conf = {0};
+        conf.status = atoi(_serviceManageRequestInfo.radius_conf.status);
+        memcpy(conf.user,_serviceManageRequestInfo.radius_conf.user,strlen(_serviceManageRequestInfo.radius_conf.user));
+        memcpy(conf.password,_serviceManageRequestInfo.radius_conf.password,strlen(_serviceManageRequestInfo.radius_conf.password));
+        radius_config(&conf);
+        ack_str = ctrl_sts_ack_to_json();
+    }
     else if(strcmp(_serviceManageRequestInfo.type,"SmtpSave")==0)
     {
         mail_info_t mailInfo=gdm->mailInfo;

+ 4 - 0
pro/src/common.c

@@ -960,6 +960,7 @@ void SetMenuAuthority(int RoleID)
         _GlobalMenuAuthority.ShfwEnabled = true;
         _GlobalMenuAuthority.GysbEnabled = true;
         _GlobalMenuAuthority.BroadcastEnable = true;
+        _GlobalMenuAuthority.RadiusEnabled = true;
     }
     // 管理员
     else if (RoleID == 2)
@@ -1031,6 +1032,7 @@ void SetMenuAuthority(int RoleID)
         _GlobalMenuAuthority.ShfwEnabled = false;
         _GlobalMenuAuthority.GysbEnabled = true;
         _GlobalMenuAuthority.BroadcastEnable = true;
+        _GlobalMenuAuthority.RadiusEnabled = true;
     }
     //普通用户
     else
@@ -1084,6 +1086,8 @@ void SetMenuAuthority(int RoleID)
         _GlobalMenuAuthority.PzdrdcEnabled = false;
         _GlobalMenuAuthority.ShfwEnabled = false;
         _GlobalMenuAuthority.GysbEnabled = false;
+        _GlobalMenuAuthority.BroadcastEnable = false;
+        _GlobalMenuAuthority.RadiusEnabled = false;
     }
     
 }

+ 1 - 0
pro/src/common.h

@@ -1050,6 +1050,7 @@ typedef struct MenuAuthority{
     bool DsszEnabled;
     bool SxszEnabled;
     bool FzszEnabled;
+    bool RadiusEnabled;
      //8、设备维护菜单
     bool SbcqEnabled;
     bool GjsjEnabled;

+ 42 - 0
pro/src/json_handle.c

@@ -10,6 +10,7 @@
 #include "lock.h"
 #include "cascade.h"
 #include "netswitch.h"
+#include "radius.h"
 
 #define TOTAL_COUNT 60 //计数长度
 
@@ -6257,6 +6258,24 @@ cJSON* json_to_service_manage(const char* str,_ServiceManageRequestInfo* _servic
         {
             _serviceManageRequestInfo->productName = cjson_productName->valuestring;
         }
+    }else if(strcmp(_serviceManageRequestInfo->type, "radiusSave") == 0)
+    {
+        cJSON *ret= cJSON_GetObjectItem(cjson, "status");
+        if(ret)
+        {
+            _serviceManageRequestInfo->radius_conf.status = ret->valuestring;
+        }
+        ret= cJSON_GetObjectItem(cjson, "user");
+        if(ret)
+        {
+            _serviceManageRequestInfo->radius_conf.user = ret->valuestring;
+        }       
+
+        ret= cJSON_GetObjectItem(cjson, "password");
+        if(ret)
+        {
+            _serviceManageRequestInfo->radius_conf.password = ret->valuestring;
+        }
     }
     return cjson;
 }
@@ -6432,6 +6451,29 @@ char* service_ntp_ack_to_json(NTPManager* __NTPManager_t)
     return str;
 }
 
+char *service_radius_ack_to_json(radius_config_t *conf)
+{
+    char* str=NULL;
+    char buf[30];
+    cJSON* root = cJSON_CreateObject();   
+    // 添加基本键值对
+    cJSON_AddNumberToObject(root, "code", 200); 
+    cJSON* data_filed = cJSON_CreateObject();
+    memset(buf, 0, sizeof(buf));
+    sprintf(buf, "%d", conf->status);
+    cJSON_AddStringToObject(data_filed, "status", buf);
+    cJSON_AddStringToObject(data_filed, "user", conf->user);
+    cJSON_AddStringToObject(data_filed, "password", conf->password);
+    cJSON_AddItemToObject(root, "data", data_filed);
+    //添加结果
+    cJSON_AddStringToObject(root, "msg", language_Operation_Success[__globalDeviceManage._globalDevInfo.product.language]);
+    //转换
+    str = cJSON_Print(root);    
+    //释放资源
+    cJSON_Delete(root);
+    return str;
+}
+
 char* service_snmp_ack_to_json(SNMPManager* sm)
 {
     char* str=NULL;

+ 12 - 0
pro/src/json_handle.h

@@ -10,6 +10,7 @@
 #include "common.h"
 #include "cJSON.h"
 #include "xlist.h"
+#include "radius.h"
 
 /// @brief 获取用户信息
 typedef struct
@@ -490,6 +491,14 @@ typedef struct
     char* updategroupName;
 }_GROUP_ServiceRequestInfo;
 
+typedef struct 
+{
+    char *status;
+    char *user;
+    char *password;
+}radius_conf_t;
+
+
 
 
 /// @brief 服务信息查询
@@ -505,6 +514,7 @@ typedef struct
     _DS_ServiceRequestInfo _ds_info;
     _POSIX_ServiceRequestInfo _posix_info;
     _GROUP_ServiceRequestInfo _group_info;
+    radius_conf_t              radius_conf;
     char* productName;
 }_ServiceManageRequestInfo;
 
@@ -738,6 +748,8 @@ char *upg_chk_to_json(int r);
 
 char * json_get_chip_type(int type);
 
+char *service_radius_ack_to_json(radius_config_t *conf);
+
 
 #endif
 

+ 180 - 3
pro/src/radius.c

@@ -6,6 +6,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include "elog.h"
+#include "string.h"
 
 
 
@@ -60,16 +61,87 @@ static int check_file(const char * path)
     }
     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);       
 
 }
 
@@ -95,6 +167,8 @@ int radius_init()
     //检查文件
     int fd = 0;
     char temp[1024] = {0};
+
+    radius.status = misc->radius;
     int ret = check_file(RADIUS_CONF);
     if(FILE_OK != ret)
     {
@@ -109,7 +183,6 @@ int radius_init()
         int size = write(fd,temp,strlen(temp));
         close (fd);
         sync();
-
     }
     if(misc->radius == 0)
     {
@@ -117,11 +190,115 @@ int radius_init()
         goto failed;
     }
     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);
+    ////////////////////////////////////////////////////////////////
 
+    memset(temp,0,1024);
 
+    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;
+    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;
+}

+ 3 - 2
pro/src/radius.h

@@ -8,6 +8,7 @@
 
 typedef struct
 {
+    int status;
     int  ap_scan;
     char key_mgmt[64];
     char eap[64];
@@ -21,6 +22,6 @@ typedef struct
 int radius_init();
 int radius_deinit();
 
-int radus_config(radius_config_t* cfg);
-
+int radius_config(radius_config_t* cfg);
+radius_config_t radius_get();
 #endif // !_RADIUS_H