]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsdinstall/scripts/wlanconfig
Use provided API (change "dialog" to "$DIALOG")
[FreeBSD/FreeBSD.git] / usr.sbin / bsdinstall / scripts / wlanconfig
1 #!/bin/sh
2 #-
3 # Copyright (c) 2011 Nathan Whitehorn
4 # Copyright (c) 2013-2016 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 f_dialog_backtitle "FreeBSD Installer"
36
37 ############################################################ FUNCTIONS
38
39 country_set()
40 {
41         local error_str iface_up ifconfig_args=
42
43         #
44         # Setup what was selected
45         # NB: Do not change order of arguments (or regdomain will be ignored)
46         #
47         [ "$2" ] && ifconfig_args="$ifconfig_args country $2"
48         [ "$1" ] && ifconfig_args="$ifconfig_args regdomain $1"
49         [ "$ifconfig_args" ] || return $SUCCESS # Nothing to do
50         ifconfig_args="${ifconfig_args# }"
51
52         # Regdomain/country cannot be applied while interface is running
53         iface_up=$( ifconfig -lu | grep -w $WLAN_IFACE )
54         [ "$iface_up" ] && ifconfig $WLAN_IFACE down
55         error_str=$( ifconfig $WLAN_IFACE $ifconfig_args 2>&1 |
56                 sed -e 's/ifconfig: //' )
57         if [ "$iface_up" ]; then
58                 # Restart wpa_supplicant(8) (should not fail).
59                 wpa_supplicant -B -i $WLAN_IFACE -c \
60                         "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
61         fi
62         if [ "$error_str" ]; then
63                 $DIALOG \
64                         --backtitle "$DIALOG_BACKTITLE" \
65                         --title "Error" \
66                         --yes-label Change \
67                         --no-label Ignore \
68                         --yesno \
69                         "Error while applying chosen settings ($error_str)" \
70                         0 0
71                 if [ $? -eq $DIALOG_OK ]; then
72                         return $FAILURE # Restart
73                 else
74                         return $SUCCESS # Skip
75                 fi
76         else
77                 awk 'sub(/^\t\t/,"")||1' \
78                         > "$BSDINSTALL_TMPETC/rc.conf.net.wlan" <<-EOF
79                 create_args_$WLAN_IFACE="$ifconfig_args"
80                 EOF
81         fi
82
83         return $SUCCESS
84 }
85
86 dialog_country_select()
87 {
88         local input regdomains countries regdomain country
89         local no_default="<not selected>"
90         local default_regdomain="${1:-$no_default}"
91         local default_country="${2:-$no_default}"
92
93         #
94         # Parse available countries/regdomains
95         #
96         input=$( ifconfig $WLAN_IFACE list countries | sed -e 's/DEBUG//gi' )
97         regdomains=$( echo $input | sed -e 's/.*domains://' | tr ' ' '\n' |
98                 sort | tr '\n' ' ' )
99         countries=$( echo $input | awk '{
100                 sub(/Country codes:/, "")
101                 sub(/Regulatory.*/, "")
102                 for (i = 1; i <= NF; i++) {
103                         printf "%s", $i
104                         if ($i ~ /[[:lower:]]/)
105                                 printf $(i+1) ~ /[[:lower:]]/ ? "\\\\\\ " : "\n"
106                         else
107                                 printf " "
108                 }
109         }' | sort -k 2 | tr '\n' ' ' )
110
111         f_dialog_menu_size height width rows \"Regdomain selection\" \
112                 \"$DIALOG_BACKTITLE\" \"Select your regdomain.\" \
113                 \"\" $regdomains
114         regdomain=$( sh -c "$DIALOG \
115                 --backtitle \"$DIALOG_BACKTITLE\" \
116                 --title \"Regdomain selection\" \
117                 --cancel-label \"Skip\" \
118                 --default-item \"$default_regdomain\" \
119                 --no-items \
120                 --stdout \
121                 --menu \"Select your regdomain.\" \
122                 $height $width $rows $regdomains"
123         )
124
125         f_dialog_menu_size height width rows \"Country selection\" \
126             \"$DIALOG_BACKTITLE\" \"Select your country.\" \
127             \"\" $countries
128         country=$( sh -c "$DIALOG \
129                 --backtitle \"$DIALOG_BACKTITLE\" \
130                 --title \"Country selection\" \
131                 --cancel-label \"Skip\" \
132                 --default-item \"$default_country\" \
133                 --stdout \
134                 --menu \"Select your country.\" \
135                 $height $width $rows $countries"
136         )
137
138         country_set "$regdomain" "$country"
139 }
140
141 ############################################################ MAIN
142
143 : > "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
144 chmod 0600 "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
145
146 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
147 ctrl_interface=/var/run/wpa_supplicant
148 eapol_version=2
149 ap_scan=1
150 fast_reauth=1
151
152 EOF
153
154 #
155 # Try to reach wpa_supplicant. If it isn't running and we can modify the
156 # existing system, start it. Otherwise, fail.
157 #
158 (wpa_cli ping >/dev/null 2>/dev/null || ([ "$BSDINSTALL_CONFIGCURRENT" ] &&
159         wpa_supplicant -B -i $1 -c "$BSDINSTALL_TMPETC/wpa_supplicant.conf")) ||
160         ($DIALOG --backtitle "$DIALOG_BACKTITLE" --title "Error" --msgbox \
161         "Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
162
163 # See if we succeeded
164 wpa_cli ping >/dev/null 2>/dev/null
165 if [ $? -ne 0 -a ! "$BSDINSTALL_CONFIGCURRENT" ]; then
166         $DIALOG \
167                 --backtitle "$DIALOG_BACKTITLE" \
168                 --title "Error" \
169                 --msgbox "Wireless cannot be configured without making changes to the local system!" \
170                 0 0
171         exit 1
172 fi
173
174 #
175 # There is no way to check country/regdomain without (possible)
176 # interface state modification
177 #
178 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
179         # Get current country/regdomain for selected interface
180         WLAN_IFACE=$( wpa_cli ifname | tail -1 )
181         INPUT=$( ifconfig $WLAN_IFACE list regdomain | head -1 )
182         DEF_REGDOMAIN=$( echo $INPUT | cut -w -f 2 )
183         DEF_COUNTRY=$( echo $INPUT | cut -w -f 4 )
184         [ "$DEF_REGDOMAIN" = 0 ] && DEF_REGDOMAIN="<not selected>"
185         [ "$DEF_COUNTRY" = 0 ] && DEF_COUNTRY="<not selected>"
186         $DIALOG \
187                 --backtitle "$DIALOG_BACKTITLE" \
188                 --title "Regdomain/country" \
189                 --yesno "Change regdomain/country (now $DEF_REGDOMAIN/$DEF_COUNTRY)?" \
190                 0 0
191         if [ $? -eq 0 ]; then
192                 while :
193                 do
194                         dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY"
195                         if [ $? -eq $SUCCESS ]; then
196                                 break
197                         fi
198                 done
199         fi
200 fi
201
202 while :; do
203         SCANSSID=0
204         output=$( wpa_cli scan 2>&1 )
205         f_dprintf "%s" "$output"
206         $DIALOG \
207                 --backtitle "$DIALOG_BACKTITLE" \
208                 --title "Scanning" \
209                 --ok-label "Skip" \
210                 --pause "Waiting 5 seconds to scan for wireless networks..." \
211                 9 40 5 || exit 1
212
213         SCAN_RESULTS=$( wpa_cli scan_results )
214         NETWORKS=$( echo "$SCAN_RESULTS" | awk -F '\t' '
215                 /..:..:..:..:..:../ {
216                         if (length($5) > 0)
217                                 printf("\"%s\"\t%s\n", $5, $4);
218                 }
219         ' | sort | uniq )
220
221         if [ ! "$NETWORKS" ]; then
222                 $DIALOG \
223                         --backtitle "$DIALOG_BACKTITLE" \
224                         --title "Error" \
225                         --yesno "No wireless networks were found. Rescan?" \
226                         0 0 && continue
227                 exit 1
228         fi
229
230         exec 3>&1
231         NETWORK=$( sh -c "$DIALOG \
232                 --extra-button \
233                 --extra-label \"Rescan\" \
234                 --backtitle \"$DIALOG_BACKTITLE\" \
235                 --title \"Network Selection\" \
236                 --menu \"Select a wireless network to connect to.\" \
237                 0 0 0 \
238                 $( echo $NETWORKS | tr '\n' ' ' )" \
239                 2>&1 1>&3
240         )
241         case $? in
242         $DIALOG_OK)
243                 break
244                 ;;
245         $DIALOG_CANCEL)
246                 # here we ask if the user wants to select the network manually
247                 f_dialog_title "Network Selection"
248                 f_dialog_yesno "Do you want to select the network manually?" || exit 1
249                 f_dialog_input NETWORK "Enter SSID" || exit 1
250                 ENCRYPTION=$( $DIALOG \
251                         --backtitle "$DIALOG_BACKTITLE" \
252                         --title "$DIALOG_TITLE" \
253                         --menu "Select encryption type" \
254                         0 0 0 \
255                         "1 WPA/WPA2 PSK" "" \
256                         "2 WPA/WPA2 EAP" "" \
257                         "3 WEP" "" \
258                         "0 None" "" \
259                         2>&1 1>&3
260                 ) || exit 1
261                 SCANSSID=1
262                 f_dialog_title_restore
263                 break
264                 ;;
265         $DIALOG_EXTRA) # Rescan
266                 ;;
267         esac
268         exec 3>&-
269 done
270
271 [ "$ENCRYPTION" ] || ENCRYPTION=$( echo "$NETWORKS" |
272         awk -F '\t' "/^\"$NETWORK\"\t/ { printf(\"%s\n\", \\\$2 ); }" )
273
274 if echo $ENCRYPTION | grep -q 'PSK'; then
275         exec 3>&1
276         PASS=$( $DIALOG \
277                 --insecure \
278                 --backtitle "$DIALOG_BACKTITLE" \
279                 --title "WPA Setup" \
280                 --mixedform "" \
281                 0 0 0 \
282                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
283                 "Password" 2 0 "" 2 12 15 63 1 \
284                 2>&1 1>&3
285         ) || exec $0 $@
286         exec 3>&-
287         awk 'sub(/^\t/,"")||1' \
288                 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
289         network={
290                 ssid="$NETWORK"
291                 scan_ssid=$SCANSSID
292                 psk="$PASS"
293                 priority=5
294         }
295         EOF
296 elif echo $ENCRYPTION | grep -q EAP; then
297         exec 3>&1
298         USERPASS=$( $DIALOG \
299                 --insecure \
300                 --backtitle "$DIALOG_BACKTITLE" \
301                 --title "WPA-Enterprise Setup" \
302                 --mixedform "" 0 0 0 \
303                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
304                 "Username" 2 0 "" 2 12 25 63 0 \
305                 "Password" 3 0 "" 3 12 25 63 1 \
306                 2>&1 1>&3
307         ) || exec $0 $@
308         exec 3>&-
309         awk 'sub(/^\t/,"")||1' \
310                 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
311         network={
312                 ssid="$NETWORK"
313                 scan_ssid=$SCANSSID
314                 key_mgmt=WPA-EAP$(
315                 echo "$USERPASS" | awk '
316                 {
317                         if (NR == 1) {
318                                 printf "        identity=\"%s\"\n", $1;
319                         } else if (NR == 2) {
320                                 printf "        password=\"%s\"\n", $1;
321                         }
322                 }' )
323                 priority=5
324         }
325         EOF
326 elif echo $ENCRYPTION | grep -q WEP; then
327         exec 3>&1
328         WEPKEY=$( $DIALOG \
329                 --insecure \
330                 --backtitle "$DIALOG_BACKTITLE" \
331                 --title "WEP Setup" \
332                 --mixedform "" 0 0 0 \
333                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
334                 "WEP Key 0" 2 0 "" 2 12 15 0 1 \
335                 2>&1 1>&3
336         ) || exec $0 $@
337         awk 'sub(/^\t/,"")||1' \
338                 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
339         network={
340                 ssid="$NETWORK"
341                 scan_ssid=$SCANSSID
342                 key_mgmt=NONE
343                 wep_key0="$WEPKEY"
344                 wep_tx_keyidx=0
345                 priority=5
346         }
347         EOF
348 else # Open
349         awk 'sub(/^\t/,"")||1' \
350                 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
351         network={
352                 ssid="$NETWORK"
353                 scan_ssid=$SCANSSID
354                 key_mgmt=NONE
355                 priority=5
356         }
357         EOF
358 fi
359
360 # Connect to any open networks policy
361 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
362 network={
363         priority=0
364         key_mgmt=NONE
365 }
366 EOF
367
368 # Bring up new network
369 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
370         output=$( wpa_cli reconfigure 2>&1 )
371         f_dprintf "%s" "$output"
372 fi
373
374 exit $SUCCESS
375
376 ################################################################################
377 # END
378 ################################################################################