blob: 443f006eb8b784aa4753e68c8cac0edec7ae40c9 [file] [log] [blame]
developer9afc7332022-08-22 10:12:14 +08001From 019d55137b9c3a0c033675f55cc098bb472d677c Mon Sep 17 00:00:00 2001
2From: "Allen.Ye" <allen.ye@mediatek.com>
3Date: Thu, 18 Aug 2022 15:22:24 +0800
4Subject: [PATCH] HAL: refactor set and get radius server functions
5
6---
7 source/wifi/wifi_hal.c | 137 ++++++++++++++++++++++++++++++++++++++---
8 1 file changed, 127 insertions(+), 10 deletions(-)
9
10diff --git a/source/wifi/wifi_hal.c b/source/wifi/wifi_hal.c
11index 7a137f7..6c00ceb 100644
12--- a/source/wifi/wifi_hal.c
13+++ b/source/wifi/wifi_hal.c
14@@ -5624,35 +5624,152 @@ INT wifi_setApSecurityReset(INT apIndex)
15 //The IP Address and port number of the RADIUS server used for WLAN security. RadiusServerIPAddr is only applicable when ModeEnabled is an Enterprise type (i.e. WPA-Enterprise, WPA2-Enterprise or WPA-WPA2-Enterprise).
16 INT wifi_getApSecurityRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
17 {
18+ char config_file[64] = {0};
19+ char buf[64] = {0};
20+ char cmd[256] = {0};
21+
22+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
23+
24 if(!IP_output || !Port_output || !RadiusSecret_output)
25 return RETURN_ERR;
26- snprintf(IP_output, 64, "75.56.77.78");
27- *Port_output = 123;
28- snprintf(RadiusSecret_output, 64, "12345678");
29
30+ // Read the first matched config
31+ snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
32+ sprintf(cmd, "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
33+ _syscmd(cmd, buf, sizeof(buf));
34+ strncpy(IP_output, buf, 64);
35+
36+ memset(buf, 0, sizeof(buf));
37+ sprintf(cmd, "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
38+ _syscmd(cmd, buf, sizeof(buf));
39+ *Port_output = atoi(buf);
40+
41+ memset(buf, 0, sizeof(buf));
42+ sprintf(cmd, "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | head -n1 | tr -d \"\\n\"", config_file);
43+ _syscmd(cmd, buf, sizeof(buf));
44+ strncpy(RadiusSecret_output, buf, 64);
45+
46+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
47 return RETURN_OK;
48 }
49
50 INT wifi_setApSecurityRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
51 {
52- //store the paramters, and apply instantly
53- return RETURN_ERR;
54+ char config_file[64] = {0};
55+ char port_str[8] = {0};
56+ char cmd[256] = {0};
57+ char buf[128] = {0};
58+
59+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
60+
61+ snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
62+
63+ snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 1'", config_file);
64+ _syscmd(cmd, buf, sizeof(buf));
65+ memset(cmd, 0, sizeof(cmd));
66+
67+ snprintf(port_str, sizeof(port_str), "%d", port);
68+ if (strlen(buf) == 0)
69+ // Append
70+ snprintf(cmd, sizeof(cmd), "echo -e '# radius 1\\n"
71+ "auth_server_addr=%s\\n"
72+ "auth_server_port=%s\\n"
73+ "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
74+ else {
75+ // Delete the three lines setting after the "# radius 1" comment
76+ snprintf(cmd, sizeof(cmd), "sed -i '/# radius 1/{n;N;N;d}' %s", config_file);
77+ _syscmd(cmd, buf, sizeof(buf));
78+ memset(cmd, 0, sizeof(cmd));
79+ // Use "# radius 1" comment to find the location to insert the radius setting
80+ snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 1/"
81+ "# radius 1\\n"
82+ "auth_server_addr=%s\\n"
83+ "auth_server_port=%s\\n"
84+ "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
85+ }
86+ if(_syscmd(cmd, buf, sizeof(buf))) {
87+ wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
88+ return RETURN_ERR;
89+ }
90+
91+ wifi_reloadAp(apIndex);
92+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
93+ return RETURN_OK;
94 }
95
96 INT wifi_getApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IP_output, UINT *Port_output, CHAR *RadiusSecret_output)
97 {
98+ char config_file[64] = {0};
99+ char buf[64] = {0};
100+ char cmd[256] = {0};
101+
102+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
103+
104 if(!IP_output || !Port_output || !RadiusSecret_output)
105 return RETURN_ERR;
106- snprintf(IP_output, 64, "75.56.77.78");
107- *Port_output = 123;
108- snprintf(RadiusSecret_output, 64, "12345678");
109+
110+ // Read the second matched config
111+ snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
112+ sprintf(cmd, "cat %s | grep \"^auth_server_addr=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"", config_file);
113+ _syscmd(cmd, buf, sizeof(buf));
114+ strncpy(IP_output, buf, 64);
115+
116+ memset(buf, 0, sizeof(buf));
117+ sprintf(cmd, "cat %s | grep \"^auth_server_port=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"", config_file);
118+ _syscmd(cmd, buf, sizeof(buf));
119+ *Port_output = atoi(buf);
120+
121+ memset(buf, 0, sizeof(buf));
122+ sprintf(cmd, "cat %s | grep \"^auth_server_shared_secret=\" | cut -d \"=\" -f 2 | tail -n +2 | head -n1 | tr -d \"\\n\"", config_file);
123+ _syscmd(cmd, buf, sizeof(buf));
124+ strncpy(RadiusSecret_output, buf, 64);
125+
126+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
127 return RETURN_OK;
128 }
129
130 INT wifi_setApSecuritySecondaryRadiusServer(INT apIndex, CHAR *IPAddress, UINT port, CHAR *RadiusSecret)
131 {
132- //store the paramters, and apply instantly
133- return RETURN_ERR;
134+ char config_file[64] = {0};
135+ char port_str[8] = {0};
136+ char cmd[256] = {0};
137+ char buf[128] = {0};
138+
139+ WIFI_ENTRY_EXIT_DEBUG("Inside %s:%d\n",__func__, __LINE__);
140+
141+ snprintf(config_file, sizeof(config_file), "%s%d.conf", CONFIG_PREFIX, apIndex);
142+
143+ snprintf(cmd, sizeof(cmd), "cat %s | grep '# radius 2'", config_file);
144+ _syscmd(cmd, buf, sizeof(buf));
145+ memset(cmd, 0, sizeof(cmd));
146+
147+ snprintf(port_str, sizeof(port_str), "%d", port);
148+ if (strlen(buf) == 0)
149+ // Append
150+ snprintf(cmd, sizeof(cmd), "echo -e '# radius 2\\n"
151+ "auth_server_addr=%s\\n"
152+ "auth_server_port=%s\\n"
153+ "auth_server_shared_secret=%s' >> %s", IPAddress, port_str, RadiusSecret, config_file);
154+ else {
155+ // Delete the three lines setting after the "# radius 2" comment
156+ snprintf(cmd, sizeof(cmd), "sed -i '/# radius 2/{n;N;N;d}' %s", config_file);
157+ _syscmd(cmd, buf, sizeof(buf));
158+ memset(cmd, 0, sizeof(cmd));
159+ // Use "# radius 2" comment to find the location to insert the radius setting
160+ snprintf(cmd, sizeof(cmd), "sed -i 's/# radius 2/"
161+ "# radius 2\\n"
162+ "auth_server_addr=%s\\n"
163+ "auth_server_port=%s\\n"
164+ "auth_server_shared_secret=%s/' %s", IPAddress, port_str, RadiusSecret, config_file);
165+ }
166+ if(_syscmd(cmd, buf, sizeof(buf))) {
167+ wifi_dbg_printf("%s: command failed, cmd: %s\n", __func__, cmd);
168+ return RETURN_ERR;
169+ }
170+
171+ wifi_reloadAp(apIndex);
172+ WIFI_ENTRY_EXIT_DEBUG("Exiting %s:%d\n",__func__, __LINE__);
173+ return RETURN_OK;
174 }
175
176 //RadiusSettings
177--
1782.18.0
179