summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdinstall/scripts/wlanconfig
blob: 0a3599ca08137cf775a983993d834f62c042e07c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
#-
# Copyright (c) 2011 Nathan Whitehorn
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$

echo -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
chmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf

echo "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
echo "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
echo "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
echo "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
echo >> $BSDINSTALL_TMPETC/wpa_supplicant.conf

# Try to reach wpa_supplicant. If it isn't running and we can modify the
# existing system, start it. Otherwise, fail.
(wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
	wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
	(dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
	"Could not start wpa_supplicant!" 0 0; exit 1) || exit 1

# See if we succeeded
wpa_cli ping >/dev/null 2>/dev/null
if [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
	dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
	"Wireless cannot be configured without making changes to the local system!" \ 0 0
	exit 1
fi

wpa_cli scan >>$BSDINSTALL_LOG
dialog --backtitle "FreeBSD Installer" --title "Scanning" --ok-label "Skip" \
	--pause "Waiting 5 seconds to scan for wireless networks..." \
	9 40 5 || exit 1

SCAN_RESULTS=`wpa_cli scan_results`
NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
   '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
   sort | uniq`

if [ -z "$NETWORKS" ]; then
	dialog --backtitle "FreeBSD Installer" --title "Error" \
	    --yesno "No wireless networks were found. Rescan?" 0 0 && \
	    exec $0 $@
	exit 1
fi

exec 3>&1
NETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
    --backtitle \"FreeBSD Installer\" --title \"Network Selection\" --menu \
    \"Select a wireless network to connect to.\" 0 0 0 \
    $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
case $? in
0)	# OK
	;;
1)	# Cancel
	exit 1
	;;
3)	# Rescan
	exec $0 $@
	;;
esac
exec 3>&-

ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
    "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`

if echo $ENCRYPTION | grep -q 'PSK'; then
	exec 3>&1
	PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
	    --title "WPA Setup" --mixedform "" 0 0 0 \
		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
		"Password" 2 0 "" 2 12 15 63 1 \
		2>&1 1>&3` \
	|| exec $0 $@
	exec 3>&-
echo "network={
	ssid=\"$NETWORK\"
	psk=\"$PASS\"
	priority=5
}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
elif echo $ENCRYPTION | grep -q WEP; then
	echo FOO
	exec 3>&1
	WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
	    --title "WEP Setup" --mixedform "" 0 0 0 \
		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
		"WEP Key 0" 2 0 "" 2 12 15 0 1 \
		2>&1 1>&3` \
	|| exec $0 $@
echo "network={
	ssid=\"$NETWORK\"
	key_mgmt=NONE
	wep_key0=\"$WEPKEY\"
	wep_tx_keyidx=0
	priority=5
}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
else	# Open
echo "network={
	ssid=\"$NETWORK\"
	key_mgmt=NONE
	priority=5
}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
fi

# Connect to any open networks policy
echo "network={
	priority=5
	key_mgmt=NONE
}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf

# Bring up new network
test ! -z $BSDINSTALL_CONFIGCURRENT && wpa_cli reconfigure >>$BSDINSTALL_LOG

exit 0
OpenPOWER on IntegriCloud