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 f_eval_catch -dk error_str wlanconfig ifconfig "ifconfig %s %s" \
56 "$WLAN_IFACE" "$ifconfig_args"
57 error_str="${error_str#ifconfig: }"
58 # Restart wpa_supplicant(8) (should not fail).
59 [ "$iface_up" ] && f_eval_catch -d wlanconfig wpa_supplicant \
60 'wpa_supplicant -B -i "%s" -c "%s/wpa_supplicant.conf"' \
61 "$WLAN_IFACE" "$BSDINSTALL_TMPETC"
62 if [ "$error_str" ]; then
64 --title "$msg_error" \
65 --backtitle "$DIALOG_BACKTITLE" \
69 "Error while applying chosen settings ($error_str)" \
70 0 0 || return $SUCCESS # Skip
71 return $FAILURE # Restart
73 awk 'sub(/^\t\t/,"")||1' \
74 > "$BSDINSTALL_TMPETC/rc.conf.net.wlan" <<-EOF
75 create_args_$WLAN_IFACE="$ifconfig_args"
82 dialog_country_select()
84 local input regdomains countries regdomain country prompt
85 local no_default="<not selected>"
86 local default_regdomain="${1:-$no_default}"
87 local default_country="${2:-$no_default}"
90 # Parse available countries/regdomains
92 input=$( ifconfig "$WLAN_IFACE" list countries | sed -e 's/DEBUG//gi' )
93 regdomains=$( echo "$input" | awk '
94 sub(/.*domains:/, ""), /[^[:alnum:][[:space:]]/ {
95 n = split($0, domains)
96 for (i = 1; i <= n; i++)
97 printf "'\''%s'\'' '\'\''", domains[i]
100 countries=$( echo "$input" | awk '
101 sub(/Country codes:/, ""), sub(/Regulatory.*/, "") {
102 while (match($0, /[[:upper:]][[:upper:][:digit:]] /)) {
103 country = substr($0, RSTART)
104 sub(/ [[:upper:]][[:upper:][:digit:]].*/, "", country)
105 code = substr(country, 1, 2)
106 desc = substr(country, 4)
107 sub(/[[:space:]]*$/, "", desc)
108 printf "'\''%s'\'' '\''%s'\''\n", code, desc
109 $0 = substr($0, RSTART + RLENGTH)
114 f_dialog_title "Regdomain selection"
115 prompt="Select your regdomain."
116 eval f_dialog_menu_size height width rows \
117 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \
118 \"\$prompt\" \"\" $regdomains
119 regdomain=$( eval $DIALOG \
120 --title \"\$DIALOG_TITLE\" \
121 --backtitle \"\$DIALOG_BACKTITLE\" \
122 --cancel-label \"\$msg_skip\" \
123 --default-item \"\$default_regdomain\" \
124 --menu \"\$prompt\" \
125 $height $width $rows \
127 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
129 f_dialog_data_sanitize regdomain
131 f_dialog_title "Country selection"
132 prompt="Select your country."
133 eval f_dialog_menu_size height width rows \
134 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \
135 \"\$prompt\" \"\" $countries
136 country=$( eval $DIALOG \
137 --title \"\$DIALOG_TITLE\" \
138 --backtitle \"\$DIALOG_BACKTITLE\" \
139 --cancel-label \"\$msg_skip\" \
140 --default-item \"\$default_country\" \
141 --menu \"\$prompt\" \
142 $height $width $rows \
144 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
146 f_dialog_data_sanitize country
148 country_set "$regdomain" "$country"
151 ############################################################ MAIN
153 : > "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
154 chmod 0600 "$BSDINSTALL_TMPETC/wpa_supplicant.conf"
156 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
157 ctrl_interface=/var/run/wpa_supplicant
165 # Try to reach wpa_supplicant. If it isn't running and we can modify the
166 # existing system, start it. Otherwise, fail.
168 if ! f_eval_catch -d wlanconfig wpa_cli "wpa_cli ping"; then
169 if [ ! "$BSDINSTALL_CONFIGCURRENT" ]; then
170 f_show_err "Wireless cannot be configured without %s" \
171 "making changes to the local system!"
174 f_eval_catch wlanconfig wpa_supplicant \
175 'wpa_supplicant -B -i "%s" -c "%s/wpa_supplicant.conf"' \
176 "$1" "$BSDINSTALL_TMPETC" || exit 1
178 # See if we succeeded
179 f_eval_catch wlanconfig wpa_cli "wpa_cli ping" || exit 1
183 # There is no way to check country/regdomain without (possible)
184 # interface state modification
186 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
187 # Get current country/regdomain for selected interface
188 WLAN_IFACE=$( wpa_cli ifname | tail -n 1 )
189 INPUT=$( ifconfig "$WLAN_IFACE" list regdomain | head -n 1 )
190 DEF_REGDOMAIN=$( echo "$INPUT" | cut -w -f 2 )
191 DEF_COUNTRY=$( echo "$INPUT" | cut -w -f 4 )
192 [ "$DEF_REGDOMAIN" = 0 ] && DEF_REGDOMAIN="<not selected>"
193 [ "$DEF_COUNTRY" = 0 ] && DEF_COUNTRY="<not selected>"
194 f_dialog_title "Regdomain/country"
195 if f_yesno "Change regdomain/country ($DEF_REGDOMAIN/$DEF_COUNTRY)?"
197 while ! dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY"
204 f_eval_catch -d wlanconfig wpa_cli "wpa_cli scan"
205 f_dialog_title "Scanning"
206 f_dialog_pause "Waiting 5 seconds to scan for wireless networks..." 5 ||
209 f_eval_catch -dk SCAN_RESULTS wlanconfig wpa_cli "wpa_cli scan_results"
210 NETWORKS=$( echo "$SCAN_RESULTS" | awk -F '\t' '
211 /..:..:..:..:..:../ && $5 { printf "\"%s\"\t\"%s\"\n", $5, $4 }
214 if [ ! "$NETWORKS" ]; then
215 f_dialog_title "$msg_error"
216 f_yesno "No wireless networks were found. Rescan?" && continue
220 f_dialog_title "Network Selection"
221 prompt="Select a wireless network to connect to."
222 f_dialog_menu_size height width rows "$DIALOG_TITLE" \
223 "$DIALOG_BACKTITLE" "$prompt" "" $menu_list
224 NETWORK=$( eval $DIALOG \
225 --title \"\$DIALOG_TITLE\" \
226 --backtitle \"\$DIALOG_BACKTITLE\" \
228 --extra-label \"Rescan\" \
229 --menu \"\$prompt\" \
230 $height $width $rows \
232 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
235 f_dialog_data_sanitize NETWORK
239 # Ask if the user wants to select network manually
240 f_dialog_title "Network Selection"
241 f_yesno "Do you want to select the network manually?" || exit 1
242 f_dialog_input NETWORK "Enter SSID" || exit 1
243 prompt="Select encryption type"
250 eval f_dialog_menu_size height width rows \"\$DIALOG_TITLE\" \
251 \"\$DIALOG_BACKTITLE\" \"\$prompt\" \"\" $menu_list
252 ENCRYPTION=$( eval $DIALOG \
253 --title \"\$DIALOG_TITLE\" \
254 --backtitle \"\$DIALOG_BACKTITLE\" \
255 --menu \"\$prompt\" \
256 $height $width $rows \
258 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
263 $DIALOG_EXTRA) # Rescan
268 [ "$ENCRYPTION" ] || ENCRYPTION=$( echo "$NETWORKS" |
269 awk -F '\t' "/^\"$NETWORK\"\t/ { print \$2 }" )
271 if echo "$ENCRYPTION" | grep -q PSK; then
273 --title "WPA Setup" \
274 --backtitle "$DIALOG_BACKTITLE" \
278 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
279 "Password" 2 0 "" 2 12 15 63 1 \
280 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
282 awk 'sub(/^\t/,"")||1' \
283 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<EOF
291 elif echo "$ENCRYPTION" | grep -q EAP; then
292 USERPASS=$( $DIALOG \
293 --title "WPA-Enterprise Setup" \
294 --backtitle "$DIALOG_BACKTITLE" \
298 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
299 "Username" 2 0 "" 2 12 25 63 0 \
300 "Password" 3 0 "" 3 12 25 63 1 \
301 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
303 awk 'sub(/^\t/,"")||1' \
304 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<EOF
309 echo "$USERPASS" | awk '
310 NR == 1 { printf "\n\t\tidentity=\"%s\"", $1 }
311 NR == 2 { printf "\n\t\tpassword=\"%s\"", $1 }
316 elif echo "$ENCRYPTION" | grep -q WEP; then
318 --title "WEP Setup" \
319 --backtitle "$DIALOG_BACKTITLE" \
323 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
324 "WEP Key 0" 2 0 "" 2 12 15 0 1 \
325 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
327 awk 'sub(/^\t/,"")||1' \
328 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<EOF
339 awk 'sub(/^\t/,"")||1' \
340 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<EOF
350 # Connect to any open networks policy
351 cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF
358 # Bring up new network
359 [ "$BSDINSTALL_CONFIGCURRENT" ] &&
360 f_eval_catch -d wlanconfig wpa_cli "wpa_cli reconfigure"
364 ################################################################################
366 ################################################################################