3 # Copyright (c) 2011 Nathan Whitehorn
4 # Copyright (c) 2013-2016 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
35 f_dialog_backtitle "FreeBSD Installer"
37 ############################################################ FUNCTIONS
41 local error_str iface_up ifconfig_args=
44 # Setup what was selected
45 # NB: Do not change order of arguments (or regdomain will be ignored)
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# }"
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"
62 if [ "$error_str" ]; then
64 --backtitle "$DIALOG_BACKTITLE" \
69 "Error while applying chosen settings ($error_str)" \
71 if [ $? -eq $DIALOG_OK ]; then
72 return $FAILURE # Restart
74 return $SUCCESS # Skip
77 awk 'sub(/^\t\t/,"")||1' \
78 > "$BSDINSTALL_TMPETC/rc.conf.net.wlan" <<-EOF
79 create_args_$WLAN_IFACE="$ifconfig_args"
86 dialog_country_select()
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}"
94 # Parse available countries/regdomains
96 input=$( ifconfig $WLAN_IFACE list countries | sed -e 's/DEBUG//gi' )
97 regdomains=$( echo $input | sed -e 's/.*domains://' | tr ' ' '\n' |
99 countries=$( echo $input | awk '{
100 sub(/Country codes:/, "")
101 sub(/Regulatory.*/, "")
102 for (i = 1; i <= NF; i++) {
104 if ($i ~ /[[:lower:]]/)
105 printf $(i+1) ~ /[[:lower:]]/ ? "\\\\\\ " : "\n"
109 }' | sort -k 2 | tr '\n' ' ' )
111 f_dialog_menu_size height width rows \"Regdomain selection\" \
112 \"$DIALOG_BACKTITLE\" \"Select your regdomain.\" \
114 regdomain=$( sh -c "$DIALOG \
115 --backtitle \"$DIALOG_BACKTITLE\" \
116 --title \"Regdomain selection\" \
117 --cancel-label \"Skip\" \
118 --default-item \"$default_regdomain\" \
121 --menu \"Select your regdomain.\" \
122 $height $width $rows $regdomains"
125 f_dialog_menu_size height width rows \"Country selection\" \
126 \"$DIALOG_BACKTITLE\" \"Select your country.\" \
128 country=$( sh -c "$DIALOG \
129 --backtitle \"$DIALOG_BACKTITLE\" \
130 --title \"Country selection\" \
131 --cancel-label \"Skip\" \
132 --default-item \"$default_country\" \
134 --menu \"Select your country.\" \
135 $height $width $rows $countries"
138 country_set "$regdomain" "$country"
141 ############################################################ MAIN
143 : > "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
144 chmod 0600 "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
146 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
147 ctrl_interface=/var/run/wpa_supplicant
155 # Try to reach wpa_supplicant. If it isn't running and we can modify the
156 # existing system, start it. Otherwise, fail.
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
163 # See if we succeeded
164 wpa_cli ping >/dev/null 2>/dev/null
165 if [ $? -ne 0 -a ! "$BSDINSTALL_CONFIGCURRENT" ]; then
167 --backtitle "$DIALOG_BACKTITLE" \
169 --msgbox "Wireless cannot be configured without making changes to the local system!" \
175 # There is no way to check country/regdomain without (possible)
176 # interface state modification
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>"
187 --backtitle "$DIALOG_BACKTITLE" \
188 --title "Regdomain/country" \
189 --yesno "Change regdomain/country (now $DEF_REGDOMAIN/$DEF_COUNTRY)?" \
191 if [ $? -eq 0 ]; then
194 dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY"
195 if [ $? -eq $SUCCESS ]; then
204 output=$( wpa_cli scan 2>&1 )
205 f_dprintf "%s" "$output"
207 --backtitle "$DIALOG_BACKTITLE" \
210 --pause "Waiting 5 seconds to scan for wireless networks..." \
213 SCAN_RESULTS=$( wpa_cli scan_results )
214 NETWORKS=$( echo "$SCAN_RESULTS" | awk -F '\t' '
215 /..:..:..:..:..:../ {
217 printf("\"%s\"\t%s\n", $5, $4);
221 if [ ! "$NETWORKS" ]; then
223 --backtitle "$DIALOG_BACKTITLE" \
225 --yesno "No wireless networks were found. Rescan?" \
231 NETWORK=$( sh -c "$DIALOG \
233 --extra-label \"Rescan\" \
234 --backtitle \"$DIALOG_BACKTITLE\" \
235 --title \"Network Selection\" \
236 --menu \"Select a wireless network to connect to.\" \
238 $( echo $NETWORKS | tr '\n' ' ' )" \
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" \
255 "1 WPA/WPA2 PSK" "" \
256 "2 WPA/WPA2 EAP" "" \
262 f_dialog_title_restore
265 $DIALOG_EXTRA) # Rescan
271 [ "$ENCRYPTION" ] || ENCRYPTION=$( echo "$NETWORKS" |
272 awk -F '\t' "/^\"$NETWORK\"\t/ { printf(\"%s\n\", \\\$2 ); }" )
274 if echo $ENCRYPTION | grep -q 'PSK'; then
278 --backtitle "$DIALOG_BACKTITLE" \
279 --title "WPA Setup" \
282 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
283 "Password" 2 0 "" 2 12 15 63 1 \
287 awk 'sub(/^\t/,"")||1' \
288 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
296 elif echo $ENCRYPTION | grep -q EAP; then
298 USERPASS=$( $DIALOG \
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 \
309 awk 'sub(/^\t/,"")||1' \
310 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
315 echo "$USERPASS" | awk '
318 printf " identity=\"%s\"\n", $1;
319 } else if (NR == 2) {
320 printf " password=\"%s\"\n", $1;
326 elif echo $ENCRYPTION | grep -q WEP; then
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 \
337 awk 'sub(/^\t/,"")||1' \
338 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
349 awk 'sub(/^\t/,"")||1' \
350 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF
360 # Connect to any open networks policy
361 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
368 # Bring up new network
369 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
370 output=$( wpa_cli reconfigure 2>&1 )
371 f_dprintf "%s" "$output"
376 ################################################################################
378 ################################################################################