]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsdinstall/scripts/wlanconfig
MFV r309299:
[FreeBSD/FreeBSD.git] / usr.sbin / bsdinstall / scripts / wlanconfig
1 #!/bin/sh
2 #-
3 # Copyright (c) 2011 Nathan Whitehorn
4 # Copyright (c) 2013-2015 Devin Teske
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29 #
30 ############################################################ INCLUDES
31
32 BSDCFG_SHARE="/usr/share/bsdconfig"
33 . $BSDCFG_SHARE/common.subr || exit 1
34 f_include $BSDCFG_SHARE/dialog.subr
35
36 ############################################################ MAIN
37
38 echo -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
39 chmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
40
41 echo "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
42 echo "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
43 echo "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
44 echo "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
45 echo >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
46
47 # Try to reach wpa_supplicant. If it isn't running and we can modify the
48 # existing system, start it. Otherwise, fail.
49 (wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
50         wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
51         (dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
52         "Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
53
54 # See if we succeeded
55 wpa_cli ping >/dev/null 2>/dev/null
56 if [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
57         dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
58         "Wireless cannot be configured without making changes to the local system!" \ 0 0
59         exit 1
60 fi
61
62 country_set()
63 {
64         local error_str=
65         local iface_up=
66         local ifconfig_args=
67
68         # Setup what was selected
69         # NB: do not change order of arguments (or regdomain will be ignored)
70         if [ ! -z "$2" ]; then
71                 ifconfig_args="${ifconfig_args}country $2"
72         fi
73         if [ ! -z "$1" ]; then
74                 if [ ! -z "$2" ]; then
75                         ifconfig_args="${ifconfig_args} "
76                 fi
77                 ifconfig_args="${ifconfig_args}regdomain $1"
78         fi
79         if [ -z "$ifconfig_args" ]; then
80                 # Nothing to do (everything was skipped)
81                 return $SUCCESS
82         fi
83
84         # Regdomain/country cannot be applied while interface is running
85         iface_up=`ifconfig -lu | grep -w $WLAN_IFACE`
86         if [ ! -z "$iface_up" ]; then
87                 ifconfig $WLAN_IFACE down
88         fi
89         error_str=`ifconfig $WLAN_IFACE $ifconfig_args 2>&1 | \
90                 sed 's/ifconfig: //'`
91         if [ ! -z "$iface_up" ]; then
92                 # Restart wpa_supplicant(8) (should not fail).
93                 wpa_supplicant -B -i $WLAN_IFACE -c \
94                     $BSDINSTALL_TMPETC/wpa_supplicant.conf
95         fi
96         if [ ! -z "$error_str" ]; then
97                 dialog --backtitle "FreeBSD Installer" --title "Error" \
98                     --yes-label Change --no-label Ignore --yesno \
99                     "Error while applying chosen settings ($error_str)" 0 0
100                 if [ $? -eq $DIALOG_OK ]; then
101                         return $FAILURE         # Restart
102                 else
103                         return $SUCCESS         # Skip
104                 fi
105         else
106                 : > $BSDINSTALL_TMPETC/rc.conf.net.wlan
107                 echo create_args_$WLAN_IFACE=\"$ifconfig_args\" >> \
108                     $BSDINSTALL_TMPETC/rc.conf.net.wlan
109         fi
110
111         return $SUCCESS
112 }
113
114 dialog_country_select()
115 {
116         local input=
117         local def_item_regdomain=
118         local def_item_country=
119         local regdomains=
120         local countries=
121         local regdomain=
122         local country=
123
124         # Parse available countries/regdomains
125         input=`ifconfig $WLAN_IFACE list countries | sed 's/DEBUG//gi'`
126         regdomains=`echo $input | sed 's/.*domains://' | tr ' ' '\n' | \
127                 sort | tr '\n' ' '`
128         countries=`echo $input | sed 's/Country codes://' | \
129             sed 's/Regulatory.*//' | awk '{
130                 for (i = 1; i <= NF; i++) {
131                         printf "%s", $i
132                         if (match($i, "[[:lower:]]"))
133                                 if (match($(i+1), "[[:lower:]]"))
134                                         printf "\\\\\\ "
135                                 else
136                                         printf "\n"
137                         else
138                                 printf " "
139                 }
140             }' | sort -k 2 | tr '\n' ' '`
141
142         # Change default cursor position (if required).
143         if [ "$1" != "<not selected>" ]; then
144                 def_item_regdomain="--default-item $1"
145         fi
146         if [ "$2" != "<not selected>" ]; then
147                 def_item_country="--default-item $2"
148         fi
149
150         f_dialog_menu_size height width rows \"Regdomain selection\" \
151             \"FreeBSD Installer\" \"Select your regdomain.\" \
152             \"\" $regdomains
153         regdomain=`sh -c "dialog \
154             --backtitle \"FreeBSD Installer\" \
155             --title \"Regdomain selection\" \
156             --cancel-label \"Skip\" \
157             $def_item_regdomain \
158             --no-items \
159             --stdout \
160             --menu \"Select your regdomain.\" \
161             $height $width $rows $regdomains"`
162
163         f_dialog_menu_size height width rows \"Country selection\" \
164             \"FreeBSD Installer\" \"Select your country.\" \
165             \"\" $countries
166         country=`sh -c "dialog \
167             --backtitle \"FreeBSD Installer\" \
168             --title \"Country selection\" \
169             --cancel-label \"Skip\" \
170             $def_item_country \
171             --stdout \
172             --menu \"Select your country.\" \
173             $height $width $rows $countries"`
174
175         country_set "$regdomain" "$country"
176
177         return $?
178 }
179
180 # There is no way to check country/regdomain without (possible)
181 # interface state modification
182 if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
183         # Get current country/regdomain for selected interface
184         WLAN_IFACE=`wpa_cli ifname | tail -n 1`
185         INPUT=`ifconfig $WLAN_IFACE list regdomain | head -n 1`
186         DEF_REGDOMAIN=`echo $INPUT | cut -w -f 2`
187         if [ "$DEF_REGDOMAIN" = "0" ]; then
188                 DEF_REGDOMAIN="<not selected>"
189         fi
190         DEF_COUNTRY=`echo $INPUT | cut -w -f 4`
191         if [ "$DEF_COUNTRY" = "0" ]; then
192                 DEF_COUNTRY="<not selected>"
193         fi
194         dialog --backtitle "FreeBSD Installer" --title "Regdomain/country" \
195             --yesno "Change regdomain/country (now \
196             $DEF_REGDOMAIN/$DEF_COUNTRY)?" 0 0
197         if [ $? -eq 0 ]; then
198                 while :
199                 do
200                         dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY"
201                         if [ $? -eq $SUCCESS ]; then
202                                 break
203                         fi
204                 done
205         fi
206 fi
207
208 while :
209 do
210         output=$( wpa_cli scan 2>&1 )
211         f_dprintf "%s" "$output"
212         dialog --backtitle "FreeBSD Installer" --title "Scanning" \
213             --ok-label "Skip" \
214             --pause "Waiting 5 seconds to scan for wireless networks..." \
215             9 40 5 || exit 1
216
217         SCAN_RESULTS=`wpa_cli scan_results`
218         NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
219            '/..:..:..:..:..:../ {if (length($5) > 0) \
220            printf("\"%s\"\t%s\n", $5, $4);}' | sort | uniq`
221
222         if [ -z "$NETWORKS" ]; then
223                 dialog --backtitle "FreeBSD Installer" --title "Error" \
224                     --yesno "No wireless networks were found. Rescan?" 0 0 && \
225                     continue
226                 exit 1
227         fi
228
229         exec 3>&1
230         NETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
231             --backtitle \"FreeBSD Installer\" --title \"Network Selection\" \
232             --menu \"Select a wireless network to connect to.\" 0 0 0 \
233             $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
234         case $? in
235         0)      # OK
236                 break
237                 ;;
238         1)      # Cancel
239                 exit 1
240                 ;;
241         3)      # Rescan
242                 ;;
243         esac
244         exec 3>&-
245 done
246
247 ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
248     "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
249
250 if echo $ENCRYPTION | grep -q 'PSK'; then
251         exec 3>&1
252         PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
253             --title "WPA Setup" --mixedform "" 0 0 0 \
254                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
255                 "Password" 2 0 "" 2 12 15 63 1 \
256                 2>&1 1>&3` \
257         || exec $0 $@
258         exec 3>&-
259 echo "network={
260         ssid=\"$NETWORK\"
261         psk=\"$PASS\"
262         priority=5
263 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
264 elif echo $ENCRYPTION | grep -q EAP; then
265         exec 3>&1
266         USERPASS=`dialog --insecure --backtitle "FreeBSD Installer" \
267             --title "WPA-Enterprise Setup" --mixedform "" 0 0 0 \
268                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
269                 "Username" 2 0 "" 2 12 25 63 0 \
270                 "Password" 3 0 "" 3 12 25 63 1 \
271                 2>&1 1>&3` \
272         || exec $0 $@
273         exec 3>&-
274 echo "network={
275         ssid=\"$NETWORK\"
276         key_mgmt=WPA-EAP" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
277 echo "$USERPASS" | awk '
278 {
279         if (NR == 1) {
280                 printf "        identity=\"%s\"\n", $1;
281         } else if (NR == 2) {
282                 printf "        password=\"%s\"\n", $1;
283         }
284 }' >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
285 echo "  priority=5
286 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
287 elif echo $ENCRYPTION | grep -q WEP; then
288         exec 3>&1
289         WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
290             --title "WEP Setup" --mixedform "" 0 0 0 \
291                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
292                 "WEP Key 0" 2 0 "" 2 12 15 0 1 \
293                 2>&1 1>&3` \
294         || exec $0 $@
295 echo "network={
296         ssid=\"$NETWORK\"
297         key_mgmt=NONE
298         wep_key0=\"$WEPKEY\"
299         wep_tx_keyidx=0
300         priority=5
301 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
302 else    # Open
303 echo "network={
304         ssid=\"$NETWORK\"
305         key_mgmt=NONE
306         priority=5
307 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
308 fi
309
310 # Connect to any open networks policy
311 echo "network={
312         priority=0
313         key_mgmt=NONE
314 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
315
316 # Bring up new network
317 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
318         output=$( wpa_cli reconfigure 2>&1 )
319         f_dprintf "%s" "$output"
320 fi
321
322 exit $SUCCESS
323
324 ################################################################################
325 # END
326 ################################################################################