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