]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.wpa_supplicant
Use autotools.
[people/ms/network.git] / src / functions / functions.wpa_supplicant
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
22 WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl"
23
24 function wpa_supplicant_config_write() {
25 local device=${1}
26 assert isset device
27
28 local file=${2}
29 assert isset file
30
31 shift 2
32
33 local ap_scan=1 country_code mode key ssid
34
35 local arg
36 for arg in "$@"; do
37 case "${arg}" in
38 --ap-scan=*)
39 ap_scan=$(cli_get_val ${arg})
40 ;;
41 --country-code=*)
42 country_code=$(cli_get_val ${arg})
43 ;;
44 --mode=*)
45 mode=$(cli_get_val ${arg})
46
47 # Empty signals no encryption.
48 isset mode || mode="NONE"
49 ;;
50 --ssid=*)
51 ssid=$(cli_get_val ${arg})
52 ;;
53 --key=*)
54 key=$(cli_get_val ${arg})
55 ;;
56 *)
57 error "Unrecognized argument: ${arg}"
58 return ${EXIT_ERROR}
59 ;;
60 esac
61 done
62
63 assert isinteger ap_scan
64 assert isset mode
65
66 local auth_alg key_mgmt proto ssid psk wep_key0 wep_tx_keyidx
67
68 case "${mode}" in
69 # Normal WPA.
70 WPA-PSK)
71 auth_alg="OPEN"
72 key_mgmt="WPA-PSK"
73 proto="WPA"
74 pairwise="CCMP TKIP"
75 group="CCMP TKIP WEP104 WEP40"
76 ;;
77
78 # WPA with stronger algorithms.
79 WPA-PSK-SHA256)
80 auth_alg="OPEN"
81 key_mgmt="WPA-PSK-SHA256"
82 proto="WPA"
83 pairwise="CCMP TKIP"
84 group="CCMP TKIP WEP104 WEP40"
85 ;;
86
87 # Normal WPA2 (802.11i).
88 WPA2-PSK)
89 auth_alg="OPEN"
90 key_mgmt="WPA-PSK"
91 proto="RSN"
92 pairwise="CCMP TKIP"
93 group="CCMP TKIP WEP104 WEP40"
94 ;;
95
96 # WPA2 with stronger algorithms.
97 WPA2-PSK-SHA256)
98 auth_alg="OPEN"
99 key_mgmt="WPA-PSK-SHA256"
100 proto="RSN"
101 pairwise="CCMP TKIP"
102 group="CCMP TKIP WEP104 WEP40"
103 ;;
104
105 # WEP.
106 WEP)
107 auth_alg="SHARED"
108 wep_key0="${key}"
109 wep_tx_keyidx="0"
110
111 # Reset PSK.
112 psk=""
113 ;;
114
115 # IEEE 802.1X
116 8021X)
117 key_mgmt="IEEE8021X"
118 ;;
119
120 # No encryption. DANGEROUS!
121 NONE)
122 auth_alg="OPEN"
123 key_mgmt="NONE"
124 ;;
125 *)
126 log ERROR "Unknown mode: ${mode}"
127 return ${EXIT_ERROR}
128 ;;
129 esac
130
131 local config_dir=$(dirname ${file})
132 mkdir -p ${config_dir} 2>/dev/null
133
134 config_header "WPA supplicant configuration file" > ${file}
135
136 # AP scanning/selection
137 print "ap_scan=${ap_scan}" >> ${file}
138
139 # Set country code, if known.
140 if isset country_code; then
141 print "country=\"${country_code}\"" >> ${file}
142 fi
143
144 # Set control socket directory.
145 print "ctrl_interface=${WPA_SUPPLICANT_SOCKET_DIR}" >> ${file}
146
147 (
148 print # Network section
149 print "network={"
150
151 if isset auth_alg; then
152 print " auth_alg=${auth_alg}"
153 fi
154
155 if isset key_mgmt; then
156 print " key_mgmt=${key_mgmt}"
157 fi
158
159 if isset proto; then
160 print " proto=${proto}"
161 fi
162
163 if isset ssid; then
164 print " ssid=${ssid}"
165 fi
166
167 if isset key; then
168 print " psk=\"${key}\""
169 fi
170
171 if isset wep_key0; then
172 print " wep_key0=\"${wep_key0}\""
173 fi
174
175 if isset wep_tx_keyidx; then
176 print " wep_tx_keyidx=${wep_tx_keyidx}"
177 fi
178
179 print "}"
180 ) >> ${file}
181
182 return ${EXIT_OK}
183 }
184
185 function wpa_supplicant_config_dir() {
186 local device=${1}
187 assert isset device
188
189 echo "${RUN_DIR}/wpa_supplicant/${device}"
190 }
191
192 function wpa_supplicant_start() {
193 local device=${1}
194 assert isset device
195
196 service_start "wpa_supplicant@${device}.service"
197 }
198
199 function wpa_supplicant_stop() {
200 local device=${1}
201 assert isset device
202
203 service_stop "wpa_supplicant@${device}.service"
204 }
205
206 function wpa_supplicant_client() {
207 local device=${1}
208 assert isset device
209 shift
210
211 local cmd="$@"
212 assert isset cmd
213
214 # Run the command and return the output.
215 cmd wpa_cli -p${WPA_SUPPLICANT_SOCKET_DIR} -i${device} ${cmd}
216 }
217
218 function wpa_cli_status() {
219 local device=${1}
220 assert isset device
221
222 wpa_supplicant_client ${device} status verbose
223 }
224
225 function wpa_cli_status_get() {
226 local device=${1}
227 assert isset device
228
229 local arg=${2}
230 assert isset arg
231
232 local line key
233 while read -r line; do
234 key=$(cli_get_key ${line})
235
236 if [ "${key}" = "${arg}" ]; then
237 cli_get_val "${line}"
238 return ${EXIT_OK}
239 fi
240 done <<< "$(wpa_cli_status ${device})"
241
242 return ${EXIT_ERROR}
243 }
244
245 function wpa_cli_bss() {
246 local device=${1}
247 assert isset device
248
249 local bss=${2}
250 assert isset bss
251
252 wpa_supplicant_client ${device} bss ${bss}
253 }
254
255 function wpa_cli_bss_get() {
256 local device=${1}
257 assert isset device
258
259 local bss=${2}
260 assert isset bss
261
262 local arg=${3}
263 assert isset arg
264
265 local line key
266 while read -r line; do
267 key=$(cli_get_key ${line})
268
269 if [ "${key}" = "${arg}" ]; then
270 cli_get_val "${line}"
271 return ${EXIT_OK}
272 fi
273 done <<< "$(wpa_cli_bss ${device} ${bss})"
274
275 return ${EXIT_ERROR}
276 }
277
278 function wpa_cli_bss_get_frequency() {
279 local device=${1}
280 assert isset device
281
282 local bssid=${2}
283 assert isset bssid
284
285 wpa_cli_bss_get ${device} ${bssid} freq
286 }
287
288 function wpa_cli_bss_get_noise() {
289 local device=${1}
290 assert isset device
291
292 local bssid=${2}
293 assert isset bssid
294
295 wpa_cli_bss_get ${device} ${bssid} noise
296 }
297
298 function wpa_cli_bss_get_quality() {
299 local device=${1}
300 assert isset device
301
302 local bssid=${2}
303 assert isset bssid
304
305 wpa_cli_bss_get ${device} ${bssid} qual
306 }
307
308 function wpa_cli_bss_get_flags() {
309 local device=${1}
310 assert isset device
311
312 local bssid=${2}
313 assert isset bssid
314
315 wpa_cli_bss_get ${device} ${bssid} flags
316 }