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