]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/wlanclient
unbound: Drop certificates for local control connection
[ipfire-2.x.git] / src / initscripts / system / wlanclient
1 #!/bin/sh
2 ########################################################################
3 # Begin $rc_base/init.d/wlan_client
4 #
5 # Description : Wireless client initscript
6 #
7 ########################################################################
8
9 . /etc/sysconfig/rc
10 . ${rc_functions}
11
12 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
13
14 WIRELESS_CONFIG="/var/ipfire/ethernet/wireless"
15
16 function device_is_wireless() {
17 local device=${1}
18
19 if [ -d "/sys/class/net/${device}/wireless" ]; then
20 return 0
21 fi
22
23 return 1
24 }
25
26 function wpa_supplicant_make_config() {
27 local device=${1}
28 local config=${2}
29 shift 2
30
31 # Check if device is wireless.
32 local wireless="false"
33 if device_is_wireless ${device}; then
34 wireless="true"
35 fi
36
37 # Write a configuration file header.
38 (
39 echo "#"
40 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND"
41 echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!"
42 echo "#"
43 echo
44 echo "ctrl_interface=/var/run/wpa_supplicant"
45 echo
46 ) > ${config}
47
48 local items=0
49
50 local line
51 while IFS="," read -ra line; do
52 # Skip commented lines.
53 [ "${line:0:1}" = "#" ] && continue
54
55 # Skip disabled entries.
56 [ "${line[2]}" = "on" ] || continue
57
58 wpa_supplicant_config_line \
59 ${device} ${config} \
60 --wireless="${wireless}" \
61 --mode="${line[3]}" \
62 --wpa-mode="${line[4]}" \
63 --ssid="${line[5]}" \
64 --psk="${line[6]}" \
65 --priority="${line[7]}" \
66 --auth-mode="${line[8]}" \
67 --anonymous-identity="${line[9]}" \
68 --identity="${line[10]}" \
69 --password="${line[11]}"
70
71 items=$(( ${items} + 1 ))
72
73 done < ${WIRELESS_CONFIG}
74
75 # Return exit code 2, when there are no entries in the
76 # configuration file.
77 if [ "${items}" = "0" ]; then
78 return 2
79 fi
80
81 return 0
82 }
83
84 function wpa_supplicant_config_line() {
85 local device=${1}
86 local config=${2}
87 shift 2
88
89 local anonymous_identity
90 local auth_alg
91 local auth_mode
92 local identity
93 local proto
94 local key_mgmt
95 local pairwise
96 local group
97 local mode
98 local password
99 local priority
100 local psk
101 local ssid
102 local wep_tx_keyidx
103 local wep_key0
104 local wireless="true"
105 local wpa_mode
106
107 while [ $# -gt 0 ]; do
108 case "${1}" in
109 --anonymous-identity=*)
110 anonymous_identity=${1#--anonymous-identity=}
111 ;;
112 --auth-mode=*)
113 auth_mode=${1#--auth-mode=}
114 ;;
115 --identity=*)
116 identity=${1#--identity=}
117 ;;
118 --mode=*)
119 mode=${1#--mode=}
120 ;;
121 --password=*)
122 password=${1#--password=}
123 ;;
124 --priority=*)
125 priority=${1#--priority=}
126 ;;
127 --psk=*)
128 psk=${1#--psk=}
129 ;;
130 --ssid=*)
131 ssid=${1#--ssid=}
132 ;;
133 --wireless=*)
134 wireless=${1#--wireless=}
135 ;;
136 --wpa-mode=*)
137 wpa_mode=${1#--wpa-mode=}
138 ;;
139 esac
140 shift
141 done
142
143 case "${mode}" in
144 EAP)
145 key_mgmt="WPA-EAP"
146 ;;
147 WPA2)
148 auth_alg="OPEN"
149 proto="RSN"
150 key_mgmt="WPA-PSK"
151 ;;
152 WPA)
153 auth_alg="OPEN"
154 proto="WPA"
155 key_mgmt="WPA-PSK"
156 ;;
157 WEP)
158 auth_alg="SHARED"
159 key_mgmt="NONE"
160
161 wep_tx_keyidx=0
162 wep_key0=${psk}
163 psk=""
164 ;;
165 NONE)
166 auth_alg="OPEN"
167 key_mgmt="NONE"
168 ;;
169 *)
170 # Unsupported mode.
171 return 1
172 ;;
173 esac
174
175 if [ "${mode}" = "EAP" -o "${mode}" = "WPA" -o "${mode}" = "WPA2" ]; then
176 case "${wpa_mode}" in
177 CCMP-CCMP)
178 pairwise="CCMP"
179 group="CCMP"
180 ;;
181 CCMP-TKIP)
182 pairwise="CCMP"
183 group="TKIP"
184 ;;
185 TKIP-TKIP)
186 pairwise="TKIP"
187 group="TKIP"
188 ;;
189 *)
190 pairwise="CCMP TKIP"
191 group="CCMP TKIP"
192 ;;
193 esac
194 fi
195
196 (
197 echo "network={"
198
199 if [ -n "${ssid}" ]; then
200 echo " ssid=\"${ssid}\""
201 fi
202 if [ "${wireless}" = "true" ]; then
203 echo " scan_ssid=1"
204 fi
205 if [ -n "${auth_alg}" ]; then
206 echo " auth_alg=${auth_alg}"
207 fi
208 if [ -n "${key_mgmt}" ]; then
209 echo " key_mgmt=${key_mgmt}"
210 fi
211 if [ -n "${psk}" ]; then
212 echo " psk=\"${psk}\""
213 fi
214 if [ -n "${wep_tx_keyidx}" ]; then
215 echo " wep_tx_keyidx=${wep_tx_keyidx}"
216 fi
217 if [ -n "${wep_key0}" ]; then
218 echo " wep_key0=\"${wep_key0}\""
219 fi
220 if [ -n "${proto}" ]; then
221 echo " proto=${proto}"
222 fi
223 if [ -n "${pairwise}" -a -n "${group}" ]; then
224 echo " pairwise=${pairwise}"
225 echo " group=${group}"
226 fi
227 if [ -n "${priority}" ]; then
228 echo " priority=${priority}"
229 fi
230
231 # EAP
232 if [ "${mode}" = "EAP" ]; then
233 if [ -n "${auth_mode}" ]; then
234 echo " eap=${auth_mode}"
235 else
236 echo " eap=PEAP TTLS"
237 fi
238
239 if [ "${auth_mode}" = "TTLS" -a -n "${anonymous_identity}" ]; then
240 echo " anonymous_identity=\"${anonymous_identity}\""
241 fi
242
243 if [ -n "${identity}" -a -n "${password}" ]; then
244 echo " identity=\"${identity}\""
245 echo " password=\"${password}\""
246 fi
247 fi
248
249 echo "}"
250 echo
251 ) >> ${config}
252 }
253
254 function wpa_supplicant_start() {
255 local device=${1}
256 local config="/etc/wpa_supplicant.conf"
257
258 # Write configuration file.
259 wpa_supplicant_make_config ${device} ${config}
260 [ $? -eq 0 ] || return 0
261
262 # Build wpa_supplicant command line.
263 local wpa_suppl_cmd="wpa_supplicant -B -qqq -i${device} -c${config}"
264
265 if device_is_wireless ${device}; then
266 wpa_suppl_cmd="${wpa_suppl_cmd} -Dwext"
267 else
268 wpa_suppl_cmd="${wpa_suppl_cmd} -Dwired"
269 fi
270
271 # Run the shiz.
272 boot_mesg "Starting wireless client on ${RED_DEV}..."
273 loadproc ${wpa_suppl_cmd}
274
275 # Run wpa_cli to handle reconnection events.
276 boot_mesg "Starting wireless event handler on ${RED_DEV}..."
277 wpa_cli -B -a /etc/rc.d/init.d/networking/wpa_supplicant.exe
278 }
279
280 function wpa_supplicant_stop() {
281 boot_mesg "Stopping wireless event handler on ${RED_DEV}..."
282 killproc wpa_cli
283
284 # wpa_cli does not send a disconnect event when get stopped.
285 # So we manually have to send it to the wpa_supplicant.exe.
286 /etc/rc.d/init.d/networking/wpa_supplicant.exe "${RED_DEV}" DISCONNECTED
287
288 boot_mesg "Stopping wireless client on ${RED_DEV}..."
289 killproc wpa_supplicant
290
291 # Tidy up /tmp directory.
292 rm -f /tmp/wpa_ctrl_*
293 }
294
295 case "${1}" in
296 start)
297 if [ -n "${RED_DEV}" ] && device_is_wireless ${RED_DEV}; then
298 wpa_supplicant_start ${RED_DEV}
299 fi
300 ;;
301
302 stop)
303 if [ -n "${RED_DEV}" ] && device_is_wireless ${RED_DEV}; then
304 wpa_supplicant_stop
305 fi
306 ;;
307
308 restart)
309 ${0} stop
310 sleep 1
311 ${0} start
312 ;;
313
314 status)
315 statusproc wpa_supplicant
316 ;;
317
318 *)
319 echo "Usage: ${0} {start|stop|restart|status}"
320 exit 1
321 ;;
322 esac
323
324 # End $rc_base/init.d/wlan_client