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