radius.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include "radius.h"
  2. #include "paras.h"
  3. #include "common.h"
  4. #include "file.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include "elog.h"
  9. #include "string.h"
  10. #include "stdio.h"
  11. enum{
  12. FILE_OK = 0, //文件存在并且有内容
  13. FILE_NO_CONTENT, //文件存在没有内容
  14. FILE_NO_EXSIT //文件不存在
  15. };
  16. #if 1
  17. #define LOGE log_e
  18. #define LOGW log_w
  19. #define LOGD log_d
  20. #else
  21. #define LOGD printf
  22. #define LOGW printf
  23. #define LOGE printf
  24. #endif
  25. #define RADIUS_CONF "/etc/eth0_radius.conf"
  26. static inline MiscInfo_t* get_misc(void)
  27. {
  28. return &__globalDeviceManage._globalDevInfo.misc;
  29. }
  30. static radius_config_t radius={
  31. .ap_scan=0,
  32. .key_mgmt="IEEE8021X",
  33. .eap="PEAP",
  34. .user="admin",
  35. .password="XXXXXXXXX",
  36. .peaplable=0,
  37. .auth="MSCHAPV2",
  38. };
  39. static int check_file(const char * path)
  40. {
  41. struct stat sta;
  42. int fd = stat(path,&sta);
  43. if(fd < 0)
  44. {
  45. return FILE_NO_EXSIT;
  46. }
  47. if(sta.st_size == 0)
  48. {
  49. return FILE_NO_CONTENT;
  50. }
  51. return FILE_OK;
  52. }
  53. static void clear_ch(char *buff,int lenth,char ch)
  54. {
  55. for(int i= 0; i < lenth;i++)
  56. {
  57. if(buff[i] == ch)
  58. {
  59. buff[i] = 0;
  60. }
  61. }
  62. }
  63. static void radius_get_config(char *buff,radius_config_t *radius)
  64. {
  65. char temp[64] = {0};
  66. ////get ap_scan=
  67. char *p_str = strstr(buff,"ap_scan=");
  68. char *p_endle = strchr(p_str,'\n');
  69. p_str +=8;
  70. memcpy(temp,p_str,p_endle-p_str);
  71. radius->ap_scan = atoi(temp);
  72. //get key_mgmt
  73. memset(temp,0,64);
  74. p_str = strstr(buff,"key_mgmt=");
  75. p_endle = strchr(p_str,'\n');
  76. p_str +=9;
  77. memcpy(temp,p_str,p_endle-p_str);
  78. memcpy(radius->key_mgmt,temp,p_endle-p_str);
  79. //get eap
  80. memset(temp,0,64);
  81. p_str = strstr(buff,"eap=");
  82. p_endle = strchr(p_str,'\n');
  83. p_str +=4;
  84. memcpy(temp,p_str,p_endle-p_str);
  85. memcpy(radius->eap,temp,p_endle-p_str);
  86. //get user
  87. memset(temp,0,64);
  88. p_str = strstr(buff,"identity=\"");
  89. p_endle = strchr(p_str,'\n');
  90. p_str +=10;
  91. memcpy(temp,p_str,p_endle-p_str);
  92. clear_ch(temp,64,'\"');
  93. memcpy(radius->user,temp,p_endle-p_str);
  94. //get password
  95. memset(temp,0,64);
  96. p_str = strstr(buff,"password=\"");
  97. p_endle = strchr(p_str,'\n');
  98. p_str +=10;
  99. memcpy(temp,p_str,p_endle-p_str);
  100. clear_ch(temp,64,'\"');
  101. memcpy(radius->password,temp,p_endle-p_str);
  102. ////get peaplabel
  103. memset(temp,0,64);
  104. p_str = strstr(buff,"peaplabel=");
  105. p_endle = strchr(p_str,'\n');
  106. p_str +=10;
  107. memcpy(temp,p_str,p_endle-p_str);
  108. clear_ch(temp,64,'\"');
  109. radius->peaplable = atoi(temp);
  110. //get auth
  111. memset(temp,0,64);
  112. p_str = strstr(buff,"auth=");
  113. p_endle = strchr(p_str,'\n');
  114. p_str +=5;
  115. memcpy(temp,p_str,p_endle-p_str);
  116. clear_ch(temp,64,'\"');
  117. memcpy(radius->auth,temp,p_endle-p_str);
  118. }
  119. #define CONFIG_FILE "ctrl_interface=/var/run/wpa_supplicant\n"\
  120. "ap_scan=%d\n"\
  121. "update_config=1\n"\
  122. "eapol_version=1\n"\
  123. "network={\n"\
  124. " ssid=\"T113-i\"\n"\
  125. " key_mgmt=%s\n"\
  126. " eap=%s\n"\
  127. " identity=\"%s\"\n"\
  128. " password=\"%s\"\n"\
  129. " phase1=\"peaplabel=%d\"\n"\
  130. " phase2=\"auth=%s\"\n"\
  131. "}\n"
  132. static radius_auth_t radius_client;
  133. #define RADIUS_CLIENT_CONFIG_PATH "/etc/radiusclient/radiusclient.conf"
  134. #define RSDIUS_SEARVEAR_CONFIG_PATH "/etc/radiusclient/servers"
  135. radius_auth_t get_radius_auth()
  136. {
  137. return radius_client;
  138. }
  139. void set_radius_auth(radius_auth_t *auth)
  140. {
  141. char buffer[512] = {0};
  142. char temp[300] = {0};
  143. if(memcmp(auth,&radius_client,sizeof(radius_auth_t)) != 0)
  144. {
  145. struct in_addr ipv4_addr;
  146. if(inet_pton(AF_INET,&auth->authserver[0],&ipv4_addr)==1 && memcmp(&radius_client.authserver[0],auth->authserver,64) != 0)
  147. {
  148. sprintf(buffer, "sed -e /authserver/d -i %s", RADIUS_CLIENT_CONFIG_PATH);
  149. system(buffer);
  150. memset(buffer,0,512);
  151. sprintf(temp,"%s %s","authserver",auth->authserver);
  152. sprintf(buffer, "sed -e \"$ a %s\" -i %s", temp, RADIUS_CLIENT_CONFIG_PATH);
  153. system(buffer);
  154. LOGW("acctserver replace %s %s !!!!\n",auth->authserver,radius_client.authserver);
  155. memset(buffer,0,512);
  156. sprintf(buffer, "sed -e /%s/d -i %s",radius_client.authserver,RSDIUS_SEARVEAR_CONFIG_PATH);
  157. system(buffer);
  158. memset(buffer,0,512);
  159. sprintf(temp,"%s %s",auth->authserver,auth->key);
  160. sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RSDIUS_SEARVEAR_CONFIG_PATH);
  161. system(buffer);
  162. memset(radius_client.authserver,0,sizeof(radius_client.authserver));
  163. memcpy(radius_client.authserver,auth->authserver,strlen(auth->authserver));
  164. memset(&radius_client.key[0],0,sizeof(radius_client.key));
  165. memcpy(&radius_client.key[0],auth->key,strlen(auth->key));
  166. }
  167. memset(buffer,0,512);
  168. memset(temp,0,300);
  169. if(inet_pton(AF_INET,&auth->acctserver[0],&ipv4_addr)==1 && memcmp(&radius_client.acctserver[0],auth->acctserver,64) != 0)
  170. {
  171. sprintf(buffer, "sed -e /acctserver/d -i %s", RADIUS_CLIENT_CONFIG_PATH);
  172. system(buffer);
  173. sprintf(temp,"%s %s","acctserver",auth->acctserver);
  174. sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RADIUS_CLIENT_CONFIG_PATH);
  175. system(buffer);
  176. LOGW("acctserver replace %s %s !!!!\n",auth->acctserver,radius_client.acctserver);
  177. memset(radius_client.acctserver,0,sizeof(radius_client.acctserver));
  178. memcpy(radius_client.acctserver,auth->acctserver,strlen(auth->acctserver));
  179. }
  180. memset(buffer,0,512);
  181. memset(temp,0,300);
  182. if(memcmp(&radius_client.key[0],auth->key,64) != 0)
  183. {
  184. sprintf(buffer, "sed -e /%s/d -i %s",radius_client.authserver,RSDIUS_SEARVEAR_CONFIG_PATH);
  185. system(buffer);
  186. memset(buffer,0,512);
  187. sprintf(temp,"%s %s",auth->authserver,auth->key);
  188. sprintf(buffer,"sed -e \"$ a %s\" -i %s",temp,RSDIUS_SEARVEAR_CONFIG_PATH);
  189. system(buffer);
  190. memset(&radius_client.key[0],0,sizeof(radius_client.key));
  191. memcpy(&radius_client.key[0],auth->key,strlen(auth->key));
  192. }
  193. sync();
  194. }
  195. //auth
  196. }
  197. static void check_client_str(char *buffer)
  198. {
  199. char *p= NULL;
  200. p = buffer;
  201. if(*p == '#')
  202. return;
  203. p = strstr(buffer,"authserver");
  204. if(p)
  205. {
  206. p += strlen("authserver");
  207. while (isspace(*p))
  208. p++;
  209. memcpy(radius_client.authserver,p,strlen(p));
  210. //clean '\n'
  211. p=&radius_client.authserver[0];
  212. do{
  213. if(isspace(*p))
  214. *p= 0;
  215. }while(*p++);
  216. printf("authserver = %s\n",radius_client.authserver);
  217. return;
  218. }
  219. p = strstr(buffer,"acctserver");
  220. if(p)
  221. {
  222. p += strlen("acctserver");
  223. while (isspace(*p))
  224. p++;
  225. memcpy(radius_client.acctserver,p,strlen(p));
  226. p=&radius_client.acctserver[0];
  227. do{
  228. if(isspace(*p))
  229. *p= 0;
  230. }while(*p++);
  231. printf("acctserver = %s\n",radius_client.acctserver);
  232. return;
  233. }
  234. return;
  235. }
  236. static void check_server_str(char *buffer)
  237. {
  238. char *p= NULL;
  239. p = buffer;
  240. if(*p =='#')
  241. return;
  242. p = strstr(buffer,radius_client.authserver);
  243. if(p)
  244. {
  245. p += strlen(radius_client.authserver);
  246. while (isspace(*p))
  247. p++;
  248. memcpy(radius_client.key,p,strlen(p));
  249. p=&radius_client.key[0];
  250. do{
  251. if(isspace(*p))
  252. *p= 0;
  253. }while(*p++);
  254. printf("key = %s\n",radius_client.key);
  255. return;
  256. }
  257. }
  258. static int get_radius_cs()
  259. {
  260. FILE *file = fopen(RADIUS_CLIENT_CONFIG_PATH,"r");
  261. char buffer[200] = {0};
  262. if(file)
  263. {
  264. while(fgets(buffer,sizeof(buffer),file) != NULL)
  265. {
  266. check_client_str(buffer);
  267. memset(buffer,0,sizeof(buffer));
  268. }
  269. fclose(file);
  270. file = NULL;
  271. }else
  272. {
  273. printf("%s not exsit!!!\n",RADIUS_CLIENT_CONFIG_PATH);
  274. }
  275. file = fopen(RSDIUS_SEARVEAR_CONFIG_PATH,"r");
  276. if(file)
  277. {
  278. while(fgets(buffer,sizeof(buffer),file) != NULL)
  279. {
  280. check_server_str(buffer);
  281. memset(buffer,0,sizeof(buffer));
  282. }
  283. fclose(file);
  284. file = NULL;
  285. }else
  286. {
  287. printf("%s not exsit!!!\n",RSDIUS_SEARVEAR_CONFIG_PATH);
  288. }
  289. return -1;
  290. }
  291. int radius_init()
  292. {
  293. MiscInfo_t *misc=get_misc();
  294. //检查文件
  295. int fd = 0;
  296. char temp[1024] = {0};
  297. radius.status = misc->radius;
  298. int ret = check_file(RADIUS_CONF);
  299. if(FILE_OK != ret)
  300. {
  301. sprintf(temp,CONFIG_FILE,radius.ap_scan,
  302. radius.key_mgmt,radius.eap,radius.user,radius.password,radius.peaplable,radius.auth);
  303. fd = open(RADIUS_CONF,O_CREAT|O_TRUNC|O_RDWR,0777);
  304. if(fd < 0)
  305. {
  306. LOGE("radius create conf failed!!\n");
  307. goto failed;
  308. }
  309. int size = write(fd,temp,strlen(temp));
  310. close (fd);
  311. sync();
  312. }
  313. memset(temp,0,1024);
  314. fd = open(RADIUS_CONF,O_RDONLY,0777);
  315. if(fd < 0)
  316. {
  317. LOGE("radius open conf failed!!\n");
  318. goto failed;
  319. }
  320. int size = read(fd,temp,1024);
  321. close(fd);
  322. radius_get_config(temp,&radius);
  323. get_radius_cs();
  324. if(misc->radius == 0)
  325. {
  326. LOGD("radius status is close!!\n");
  327. goto failed;
  328. }
  329. ////////////////////////////////////////////////////////////////
  330. sprintf(temp,"wpa_supplicant -i eth0 -c %s -D wired -B",RADIUS_CONF);
  331. ret = system(temp);
  332. if(ret)
  333. {
  334. LOGE("radius init failed!!\n");
  335. }
  336. return 0;
  337. failed:
  338. return -1;
  339. }
  340. int radius_deinit()
  341. {
  342. FILE *fp;
  343. int pid=0;
  344. char buf[20]={0};
  345. char command[200];
  346. snprintf(command, sizeof(command), "ps -ef | grep -v grep | grep -w -c %s", "wpa_supplicant");
  347. fp = popen(command, "r");
  348. if (fp == NULL) {
  349. LOGE("execute %s failed: %s", command, strerror(errno));
  350. return 0;
  351. }
  352. if (fgets(buf, sizeof(buf), fp)) {
  353. pid = atoi(buf);
  354. }
  355. pclose(fp);
  356. if(pid)
  357. {
  358. #if (CHIP_TYPE == CHIP_T113i)
  359. system("killall wpa_supplicant");
  360. #endif
  361. }
  362. return 0;
  363. }
  364. int radius_config(radius_config_t* cfg)
  365. {
  366. MiscInfo_t *misc=get_misc();
  367. radius.status = cfg->status;
  368. misc->radius = radius.status;
  369. paras_save();
  370. if(cfg->status == 0)
  371. {
  372. system("killall wpa_supplicant");
  373. return 0;
  374. }
  375. if(memcmp(cfg->user,&radius.user,sizeof(radius.user)) != 0 || memcmp(cfg->password,&radius.password,sizeof(radius.password)) != 0 )
  376. {
  377. char temp[1024];
  378. memset(radius.user,0,sizeof(radius.user));
  379. memset(radius.password,0,sizeof(radius.password));
  380. memcpy(radius.user,cfg->user,strlen(cfg->user));
  381. memcpy(radius.password,cfg->password,strlen(cfg->password));
  382. sprintf(temp,CONFIG_FILE,radius.ap_scan,
  383. radius.key_mgmt,radius.eap,radius.user,radius.password,radius.peaplable,radius.auth);
  384. int fd = open(RADIUS_CONF,O_CREAT|O_TRUNC|O_RDWR,0777);
  385. if(fd < 0)
  386. {
  387. LOGE("radius create conf failed!!\n");
  388. return -1;
  389. }
  390. int size = write(fd,temp,strlen(temp));
  391. close (fd);
  392. sync();
  393. //kill
  394. #if (CHIP_TYPE == CHIP_T113i)
  395. system("killall wpa_supplicant");
  396. sleep(2);
  397. memset(temp,0,1024);
  398. sprintf(temp,"wpa_supplicant -i eth0 -c %s -D wired -B",RADIUS_CONF);
  399. int ret = system(temp);
  400. if(ret)
  401. {
  402. LOGE("radius init failed!!\n");
  403. }
  404. #endif
  405. }
  406. return 0;
  407. }
  408. radius_config_t radius_get()
  409. {
  410. return radius;
  411. }