xref: /freebsd/usr.sbin/bsdinstall/scripts/wlanconfig (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013 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
34
35############################################################ MAIN
36
37echo -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
38chmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
39
40echo "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
41echo "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
42echo "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
43echo "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
44echo >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
45
46# Try to reach wpa_supplicant. If it isn't running and we can modify the
47# existing system, start it. Otherwise, fail.
48(wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
49	wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
50	(dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
51	"Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
52
53# See if we succeeded
54wpa_cli ping >/dev/null 2>/dev/null
55if [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
56	dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
57	"Wireless cannot be configured without making changes to the local system!" \ 0 0
58	exit 1
59fi
60
61output=$( wpa_cli scan 2>&1 )
62f_dprintf "%s" "$output"
63dialog --backtitle "FreeBSD Installer" --title "Scanning" --ok-label "Skip" \
64	--pause "Waiting 5 seconds to scan for wireless networks..." \
65	9 40 5 || exit 1
66
67SCAN_RESULTS=`wpa_cli scan_results`
68NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
69   '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
70   sort | uniq`
71
72if [ -z "$NETWORKS" ]; then
73	dialog --backtitle "FreeBSD Installer" --title "Error" \
74	    --yesno "No wireless networks were found. Rescan?" 0 0 && \
75	    exec $0 $@
76	exit 1
77fi
78
79exec 3>&1
80NETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
81    --backtitle \"FreeBSD Installer\" --title \"Network Selection\" --menu \
82    \"Select a wireless network to connect to.\" 0 0 0 \
83    $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
84case $? in
850)	# OK
86	;;
871)	# Cancel
88	exit 1
89	;;
903)	# Rescan
91	exec $0 $@
92	;;
93esac
94exec 3>&-
95
96ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
97    "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
98
99if echo $ENCRYPTION | grep -q 'PSK'; then
100	exec 3>&1
101	PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
102	    --title "WPA Setup" --mixedform "" 0 0 0 \
103		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
104		"Password" 2 0 "" 2 12 15 63 1 \
105		2>&1 1>&3` \
106	|| exec $0 $@
107	exec 3>&-
108echo "network={
109	ssid=\"$NETWORK\"
110	psk=\"$PASS\"
111	priority=5
112}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
113elif echo $ENCRYPTION | grep -q EAP; then
114	exec 3>&1
115	USERPASS=`dialog --insecure --backtitle "FreeBSD Installer" \
116	    --title "WPA-Enterprise Setup" --mixedform "" 0 0 0 \
117		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
118		"Username" 2 0 "" 2 12 25 63 0 \
119		"Password" 3 0 "" 3 12 25 63 1 \
120		2>&1 1>&3` \
121	|| exec $0 $@
122	exec 3>&-
123echo "network={
124	ssid=\"$NETWORK\"
125	key_mgmt=WPA-EAP" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
126echo "$USERPASS" | awk '
127{
128	if (NR == 1) {
129		printf "	identity=\"%s\"\n", $1;
130	} else if (NR == 2) {
131		printf "	password=\"%s\"\n", $1;
132	}
133}' >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
134echo "	priority=5
135}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
136elif echo $ENCRYPTION | grep -q WEP; then
137	exec 3>&1
138	WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
139	    --title "WEP Setup" --mixedform "" 0 0 0 \
140		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
141		"WEP Key 0" 2 0 "" 2 12 15 0 1 \
142		2>&1 1>&3` \
143	|| exec $0 $@
144echo "network={
145	ssid=\"$NETWORK\"
146	key_mgmt=NONE
147	wep_key0=\"$WEPKEY\"
148	wep_tx_keyidx=0
149	priority=5
150}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
151else	# Open
152echo "network={
153	ssid=\"$NETWORK\"
154	key_mgmt=NONE
155	priority=5
156}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
157fi
158
159# Connect to any open networks policy
160echo "network={
161	priority=0
162	key_mgmt=NONE
163}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
164
165# Bring up new network
166if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
167	output=$( wpa_cli reconfigure 2>&1 )
168	f_dprintf "%s" "$output"
169fi
170
171exit 0
172
173################################################################################
174# END
175################################################################################
176