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