3 # Copyright (c) 2011 Nathan Whitehorn
4 # Copyright (c) 2013-2015 Devin Teske
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
30 ############################################################ INCLUDES
32 BSDCFG_SHARE="/usr/share/bsdconfig"
33 . $BSDCFG_SHARE/common.subr || exit 1
34 f_include $BSDCFG_SHARE/dialog.subr
36 ############################################################ MAIN
38 echo -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
39 chmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
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
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
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
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"
73 if [ ! -z "$1" ]; then
74 if [ ! -z "$2" ]; then
75 ifconfig_args="${ifconfig_args} "
77 ifconfig_args="${ifconfig_args}regdomain $1"
79 if [ -z "$ifconfig_args" ]; then
80 # Nothing to do (everything was skipped)
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
89 error_str=`ifconfig $WLAN_IFACE $ifconfig_args 2>&1 | \
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
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
103 return $SUCCESS # Skip
106 : > $BSDINSTALL_TMPETC/rc.conf.net.wlan
107 echo create_args_$WLAN_IFACE=\"$ifconfig_args\" >> \
108 $BSDINSTALL_TMPETC/rc.conf.net.wlan
114 dialog_country_select()
117 local def_item_regdomain=
118 local def_item_country=
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' | \
128 countries=`echo $input | sed 's/Country codes://' | \
129 sed 's/Regulatory.*//' | awk '{
130 for (i = 1; i <= NF; i++) {
132 if (match($i, "[[:lower:]]"))
133 if (match($(i+1), "[[:lower:]]"))
140 }' | sort -k 2 | tr '\n' ' '`
142 # Change default cursor position (if required).
143 if [ "$1" != "<not selected>" ]; then
144 def_item_regdomain="--default-item $1"
146 if [ "$2" != "<not selected>" ]; then
147 def_item_country="--default-item $2"
150 f_dialog_menu_size height width rows \"Regdomain selection\" \
151 \"FreeBSD Installer\" \"Select your regdomain.\" \
153 regdomain=`sh -c "dialog \
154 --backtitle \"FreeBSD Installer\" \
155 --title \"Regdomain selection\" \
156 --cancel-label \"Skip\" \
157 $def_item_regdomain \
160 --menu \"Select your regdomain.\" \
161 $height $width $rows $regdomains"`
163 f_dialog_menu_size height width rows \"Country selection\" \
164 \"FreeBSD Installer\" \"Select your country.\" \
166 country=`sh -c "dialog \
167 --backtitle \"FreeBSD Installer\" \
168 --title \"Country selection\" \
169 --cancel-label \"Skip\" \
172 --menu \"Select your country.\" \
173 $height $width $rows $countries"`
175 country_set "$regdomain" "$country"
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>"
190 DEF_COUNTRY=`echo $INPUT | cut -w -f 4`
191 if [ "$DEF_COUNTRY" = "0" ]; then
192 DEF_COUNTRY="<not selected>"
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
200 dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY"
201 if [ $? -eq $SUCCESS ]; then
210 output=$( wpa_cli scan 2>&1 )
211 f_dprintf "%s" "$output"
212 dialog --backtitle "FreeBSD Installer" --title "Scanning" \
214 --pause "Waiting 5 seconds to scan for wireless networks..." \
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`
222 if [ -z "$NETWORKS" ]; then
223 dialog --backtitle "FreeBSD Installer" --title "Error" \
224 --yesno "No wireless networks were found. Rescan?" 0 0 && \
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`
247 ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
248 "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
250 if echo $ENCRYPTION | grep -q 'PSK'; then
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 \
263 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
264 elif echo $ENCRYPTION | grep -q EAP; then
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 \
276 key_mgmt=WPA-EAP" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
277 echo "$USERPASS" | awk '
280 printf " identity=\"%s\"\n", $1;
281 } else if (NR == 2) {
282 printf " password=\"%s\"\n", $1;
284 }' >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
286 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
287 elif echo $ENCRYPTION | grep -q WEP; then
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 \
301 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
307 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
310 # Connect to any open networks policy
314 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
316 # Bring up new network
317 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
318 output=$( wpa_cli reconfigure 2>&1 )
319 f_dprintf "%s" "$output"
324 ################################################################################
326 ################################################################################