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