]>
Commit | Line | Data |
---|---|---|
6d4eec4c MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 IPFire Network Development Team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22a61046 MT |
22 | WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl" |
23 | ||
1c6a4e30 | 24 | wpa_supplicant_config_write() { |
6d4eec4c | 25 | local device=${1} |
6d4eec4c MT |
26 | assert isset device |
27 | ||
22a61046 MT |
28 | local file=${2} |
29 | assert isset file | |
6d4eec4c | 30 | |
22a61046 MT |
31 | shift 2 |
32 | ||
31670741 | 33 | local ap_scan=1 mode key ssid |
22a61046 MT |
34 | |
35 | local arg | |
36 | for arg in "$@"; do | |
37 | case "${arg}" in | |
38 | --ap-scan=*) | |
2212045f | 39 | ap_scan=$(cli_get_val "${arg}") |
22a61046 | 40 | ;; |
22a61046 | 41 | --mode=*) |
2212045f | 42 | mode=$(cli_get_val "${arg}") |
22a61046 MT |
43 | |
44 | # Empty signals no encryption. | |
45 | isset mode || mode="NONE" | |
6d4eec4c | 46 | ;; |
22a61046 | 47 | --ssid=*) |
2212045f | 48 | ssid=$(cli_get_val "${arg}") |
6d4eec4c MT |
49 | ;; |
50 | --key=*) | |
2212045f | 51 | key=$(cli_get_val "${arg}") |
22a61046 MT |
52 | ;; |
53 | *) | |
54 | error "Unrecognized argument: ${arg}" | |
55 | return ${EXIT_ERROR} | |
6d4eec4c MT |
56 | ;; |
57 | esac | |
6d4eec4c MT |
58 | done |
59 | ||
22a61046 MT |
60 | assert isinteger ap_scan |
61 | assert isset mode | |
62 | ||
63 | local auth_alg key_mgmt proto ssid psk wep_key0 wep_tx_keyidx | |
31670741 | 64 | local country_code="$(wireless_get_reg_domain)" |
22a61046 MT |
65 | |
66 | case "${mode}" in | |
67 | # Normal WPA. | |
68 | WPA-PSK) | |
69 | auth_alg="OPEN" | |
70 | key_mgmt="WPA-PSK" | |
71 | proto="WPA" | |
72 | pairwise="CCMP TKIP" | |
73 | group="CCMP TKIP WEP104 WEP40" | |
74 | ;; | |
75 | ||
76 | # WPA with stronger algorithms. | |
77 | WPA-PSK-SHA256) | |
78 | auth_alg="OPEN" | |
79 | key_mgmt="WPA-PSK-SHA256" | |
80 | proto="WPA" | |
81 | pairwise="CCMP TKIP" | |
82 | group="CCMP TKIP WEP104 WEP40" | |
83 | ;; | |
84 | ||
85 | # Normal WPA2 (802.11i). | |
86 | WPA2-PSK) | |
87 | auth_alg="OPEN" | |
88 | key_mgmt="WPA-PSK" | |
89 | proto="RSN" | |
90 | pairwise="CCMP TKIP" | |
91 | group="CCMP TKIP WEP104 WEP40" | |
92 | ;; | |
93 | ||
94 | # WPA2 with stronger algorithms. | |
95 | WPA2-PSK-SHA256) | |
96 | auth_alg="OPEN" | |
97 | key_mgmt="WPA-PSK-SHA256" | |
98 | proto="RSN" | |
99 | pairwise="CCMP TKIP" | |
100 | group="CCMP TKIP WEP104 WEP40" | |
101 | ;; | |
102 | ||
103 | # WEP. | |
104 | WEP) | |
105 | auth_alg="SHARED" | |
106 | wep_key0="${key}" | |
107 | wep_tx_keyidx="0" | |
108 | ||
109 | # Reset PSK. | |
110 | psk="" | |
111 | ;; | |
112 | ||
113 | # IEEE 802.1X | |
114 | 8021X) | |
115 | key_mgmt="IEEE8021X" | |
116 | ;; | |
117 | ||
118 | # No encryption. DANGEROUS! | |
119 | NONE) | |
120 | auth_alg="OPEN" | |
121 | key_mgmt="NONE" | |
122 | ;; | |
123 | *) | |
124 | log ERROR "Unknown mode: ${mode}" | |
125 | return ${EXIT_ERROR} | |
126 | ;; | |
127 | esac | |
128 | ||
129 | local config_dir=$(dirname ${file}) | |
130 | mkdir -p ${config_dir} 2>/dev/null | |
131 | ||
132 | config_header "WPA supplicant configuration file" > ${file} | |
133 | ||
134 | # AP scanning/selection | |
135 | print "ap_scan=${ap_scan}" >> ${file} | |
136 | ||
137 | # Set country code, if known. | |
138 | if isset country_code; then | |
139 | print "country=\"${country_code}\"" >> ${file} | |
140 | fi | |
141 | ||
142 | # Set control socket directory. | |
143 | print "ctrl_interface=${WPA_SUPPLICANT_SOCKET_DIR}" >> ${file} | |
144 | ||
145 | ( | |
146 | print # Network section | |
147 | print "network={" | |
148 | ||
149 | if isset auth_alg; then | |
150 | print " auth_alg=${auth_alg}" | |
151 | fi | |
152 | ||
153 | if isset key_mgmt; then | |
154 | print " key_mgmt=${key_mgmt}" | |
155 | fi | |
156 | ||
157 | if isset proto; then | |
158 | print " proto=${proto}" | |
159 | fi | |
6d4eec4c | 160 | |
22a61046 | 161 | if isset ssid; then |
aaf34099 | 162 | print " ssid=\"${ssid}\"" |
22a61046 MT |
163 | fi |
164 | ||
165 | if isset key; then | |
166 | print " psk=\"${key}\"" | |
167 | fi | |
168 | ||
169 | if isset wep_key0; then | |
170 | print " wep_key0=\"${wep_key0}\"" | |
171 | fi | |
172 | ||
173 | if isset wep_tx_keyidx; then | |
174 | print " wep_tx_keyidx=${wep_tx_keyidx}" | |
175 | fi | |
176 | ||
177 | print "}" | |
178 | ) >> ${file} | |
179 | ||
180 | return ${EXIT_OK} | |
6d4eec4c MT |
181 | } |
182 | ||
1c6a4e30 | 183 | wpa_supplicant_config_dir() { |
6d4eec4c | 184 | local device=${1} |
6d4eec4c MT |
185 | assert isset device |
186 | ||
22a61046 | 187 | echo "${RUN_DIR}/wpa_supplicant/${device}" |
6d4eec4c MT |
188 | } |
189 | ||
1c6a4e30 | 190 | wpa_supplicant_start() { |
6d4eec4c | 191 | local device=${1} |
22a61046 | 192 | assert isset device |
6d4eec4c | 193 | |
22a61046 MT |
194 | service_start "wpa_supplicant@${device}.service" |
195 | } | |
6d4eec4c | 196 | |
1c6a4e30 | 197 | wpa_supplicant_stop() { |
22a61046 MT |
198 | local device=${1} |
199 | assert isset device | |
6d4eec4c | 200 | |
22a61046 MT |
201 | service_stop "wpa_supplicant@${device}.service" |
202 | } | |
203 | ||
1c6a4e30 | 204 | wpa_supplicant_client() { |
22a61046 MT |
205 | local device=${1} |
206 | assert isset device | |
207 | shift | |
6d4eec4c | 208 | |
22a61046 MT |
209 | local cmd="$@" |
210 | assert isset cmd | |
211 | ||
212 | # Run the command and return the output. | |
213 | cmd wpa_cli -p${WPA_SUPPLICANT_SOCKET_DIR} -i${device} ${cmd} | |
6d4eec4c MT |
214 | } |
215 | ||
1c6a4e30 | 216 | wpa_cli_status() { |
6d4eec4c | 217 | local device=${1} |
22a61046 MT |
218 | assert isset device |
219 | ||
220 | wpa_supplicant_client ${device} status verbose | |
221 | } | |
6d4eec4c | 222 | |
1c6a4e30 | 223 | wpa_cli_status_get() { |
22a61046 | 224 | local device=${1} |
6d4eec4c MT |
225 | assert isset device |
226 | ||
22a61046 MT |
227 | local arg=${2} |
228 | assert isset arg | |
6d4eec4c | 229 | |
22a61046 MT |
230 | local line key |
231 | while read -r line; do | |
232 | key=$(cli_get_key ${line}) | |
6d4eec4c | 233 | |
22a61046 MT |
234 | if [ "${key}" = "${arg}" ]; then |
235 | cli_get_val "${line}" | |
236 | return ${EXIT_OK} | |
237 | fi | |
238 | done <<< "$(wpa_cli_status ${device})" | |
239 | ||
240 | return ${EXIT_ERROR} | |
6d4eec4c MT |
241 | } |
242 | ||
1c6a4e30 | 243 | wpa_cli_bss() { |
6d4eec4c | 244 | local device=${1} |
22a61046 MT |
245 | assert isset device |
246 | ||
247 | local bss=${2} | |
248 | assert isset bss | |
6d4eec4c | 249 | |
22a61046 MT |
250 | wpa_supplicant_client ${device} bss ${bss} |
251 | } | |
252 | ||
1c6a4e30 | 253 | wpa_cli_bss_get() { |
22a61046 | 254 | local device=${1} |
6d4eec4c MT |
255 | assert isset device |
256 | ||
22a61046 MT |
257 | local bss=${2} |
258 | assert isset bss | |
6d4eec4c | 259 | |
22a61046 MT |
260 | local arg=${3} |
261 | assert isset arg | |
6d4eec4c | 262 | |
22a61046 MT |
263 | local line key |
264 | while read -r line; do | |
265 | key=$(cli_get_key ${line}) | |
266 | ||
267 | if [ "${key}" = "${arg}" ]; then | |
268 | cli_get_val "${line}" | |
269 | return ${EXIT_OK} | |
270 | fi | |
271 | done <<< "$(wpa_cli_bss ${device} ${bss})" | |
272 | ||
273 | return ${EXIT_ERROR} | |
6d4eec4c MT |
274 | } |
275 | ||
1c6a4e30 | 276 | wpa_cli_bss_get_frequency() { |
6d4eec4c | 277 | local device=${1} |
6d4eec4c MT |
278 | assert isset device |
279 | ||
22a61046 MT |
280 | local bssid=${2} |
281 | assert isset bssid | |
6d4eec4c | 282 | |
22a61046 MT |
283 | wpa_cli_bss_get ${device} ${bssid} freq |
284 | } | |
6d4eec4c | 285 | |
1c6a4e30 | 286 | wpa_cli_bss_get_noise() { |
22a61046 MT |
287 | local device=${1} |
288 | assert isset device | |
289 | ||
290 | local bssid=${2} | |
291 | assert isset bssid | |
292 | ||
293 | wpa_cli_bss_get ${device} ${bssid} noise | |
6d4eec4c MT |
294 | } |
295 | ||
1c6a4e30 | 296 | wpa_cli_bss_get_quality() { |
22a61046 MT |
297 | local device=${1} |
298 | assert isset device | |
6d4eec4c | 299 | |
22a61046 MT |
300 | local bssid=${2} |
301 | assert isset bssid | |
302 | ||
324c09bc MT |
303 | local quality=$(wpa_cli_bss_get ${device} ${bssid} qual) |
304 | ||
305 | # Convert to percent | |
306 | print $(( ${quality} * 100 / 70 )) | |
6d4eec4c MT |
307 | } |
308 | ||
1c6a4e30 | 309 | wpa_cli_bss_get_flags() { |
22a61046 MT |
310 | local device=${1} |
311 | assert isset device | |
312 | ||
313 | local bssid=${2} | |
314 | assert isset bssid | |
6d4eec4c | 315 | |
22a61046 | 316 | wpa_cli_bss_get ${device} ${bssid} flags |
6d4eec4c | 317 | } |