]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.wpa_supplicant
Use autotools.
[people/stevee/network.git] / src / functions / functions.wpa_supplicant
CommitLineData
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
22WPA_SUPPLICANT_SOCKET_DIR="${RUN_DIR}/wpa_supplicant/ctrl"
23
6d4eec4c
MT
24function wpa_supplicant_config_write() {
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
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"
6d4eec4c 49 ;;
22a61046
MT
50 --ssid=*)
51 ssid=$(cli_get_val ${arg})
6d4eec4c
MT
52 ;;
53 --key=*)
22a61046
MT
54 key=$(cli_get_val ${arg})
55 ;;
56 *)
57 error "Unrecognized argument: ${arg}"
58 return ${EXIT_ERROR}
6d4eec4c
MT
59 ;;
60 esac
6d4eec4c
MT
61 done
62
22a61046
MT
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
6d4eec4c 162
22a61046
MT
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}
6d4eec4c
MT
183}
184
185function wpa_supplicant_config_dir() {
186 local device=${1}
6d4eec4c
MT
187 assert isset device
188
22a61046 189 echo "${RUN_DIR}/wpa_supplicant/${device}"
6d4eec4c
MT
190}
191
192function wpa_supplicant_start() {
193 local device=${1}
22a61046 194 assert isset device
6d4eec4c 195
22a61046
MT
196 service_start "wpa_supplicant@${device}.service"
197}
6d4eec4c 198
22a61046
MT
199function wpa_supplicant_stop() {
200 local device=${1}
201 assert isset device
6d4eec4c 202
22a61046
MT
203 service_stop "wpa_supplicant@${device}.service"
204}
205
206function wpa_supplicant_client() {
207 local device=${1}
208 assert isset device
209 shift
6d4eec4c 210
22a61046
MT
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}
6d4eec4c
MT
216}
217
22a61046 218function wpa_cli_status() {
6d4eec4c 219 local device=${1}
22a61046
MT
220 assert isset device
221
222 wpa_supplicant_client ${device} status verbose
223}
6d4eec4c 224
22a61046
MT
225function wpa_cli_status_get() {
226 local device=${1}
6d4eec4c
MT
227 assert isset device
228
22a61046
MT
229 local arg=${2}
230 assert isset arg
6d4eec4c 231
22a61046
MT
232 local line key
233 while read -r line; do
234 key=$(cli_get_key ${line})
6d4eec4c 235
22a61046
MT
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}
6d4eec4c
MT
243}
244
22a61046 245function wpa_cli_bss() {
6d4eec4c 246 local device=${1}
22a61046
MT
247 assert isset device
248
249 local bss=${2}
250 assert isset bss
6d4eec4c 251
22a61046
MT
252 wpa_supplicant_client ${device} bss ${bss}
253}
254
255function wpa_cli_bss_get() {
256 local device=${1}
6d4eec4c
MT
257 assert isset device
258
22a61046
MT
259 local bss=${2}
260 assert isset bss
6d4eec4c 261
22a61046
MT
262 local arg=${3}
263 assert isset arg
6d4eec4c 264
22a61046
MT
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}
6d4eec4c
MT
276}
277
22a61046 278function wpa_cli_bss_get_frequency() {
6d4eec4c 279 local device=${1}
6d4eec4c
MT
280 assert isset device
281
22a61046
MT
282 local bssid=${2}
283 assert isset bssid
6d4eec4c 284
22a61046
MT
285 wpa_cli_bss_get ${device} ${bssid} freq
286}
6d4eec4c 287
22a61046
MT
288function 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
6d4eec4c
MT
296}
297
22a61046
MT
298function wpa_cli_bss_get_quality() {
299 local device=${1}
300 assert isset device
6d4eec4c 301
22a61046
MT
302 local bssid=${2}
303 assert isset bssid
304
305 wpa_cli_bss_get ${device} ${bssid} qual
6d4eec4c
MT
306}
307
22a61046
MT
308function wpa_cli_bss_get_flags() {
309 local device=${1}
310 assert isset device
311
312 local bssid=${2}
313 assert isset bssid
6d4eec4c 314
22a61046 315 wpa_cli_bss_get ${device} ${bssid} flags
6d4eec4c 316}