1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4263956Sdteske# Copyright (c) 2013 Devin Teske
5218799Snwhitehorn# All rights reserved.
6218799Snwhitehorn#
7218799Snwhitehorn# Redistribution and use in source and binary forms, with or without
8218799Snwhitehorn# modification, are permitted provided that the following conditions
9218799Snwhitehorn# are met:
10218799Snwhitehorn# 1. Redistributions of source code must retain the above copyright
11218799Snwhitehorn#    notice, this list of conditions and the following disclaimer.
12218799Snwhitehorn# 2. Redistributions in binary form must reproduce the above copyright
13218799Snwhitehorn#    notice, this list of conditions and the following disclaimer in the
14218799Snwhitehorn#    documentation and/or other materials provided with the distribution.
15218799Snwhitehorn#
16218799Snwhitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17218799Snwhitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18218799Snwhitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19218799Snwhitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20218799Snwhitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21218799Snwhitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22218799Snwhitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23218799Snwhitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24218799Snwhitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25218799Snwhitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26218799Snwhitehorn# SUCH DAMAGE.
27218799Snwhitehorn#
28218799Snwhitehorn# $FreeBSD$
29263956Sdteske#
30263956Sdteske############################################################ INCLUDES
31218799Snwhitehorn
32263956SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
33263956Sdteske. $BSDCFG_SHARE/common.subr || exit 1
34263956Sdteske
35263956Sdteske############################################################ MAIN
36263956Sdteske
37218799Snwhitehornecho -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
38218799Snwhitehornchmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
39218799Snwhitehorn
40218799Snwhitehornecho "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
41218799Snwhitehornecho "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
42218799Snwhitehornecho "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
43218799Snwhitehornecho "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
44218799Snwhitehornecho >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
45218799Snwhitehorn
46218799Snwhitehorn# Try to reach wpa_supplicant. If it isn't running and we can modify the
47218799Snwhitehorn# existing system, start it. Otherwise, fail.
48218799Snwhitehorn(wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
49218799Snwhitehorn	wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
50218799Snwhitehorn	(dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
51218799Snwhitehorn	"Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
52218799Snwhitehorn
53218799Snwhitehorn# See if we succeeded
54218799Snwhitehornwpa_cli ping >/dev/null 2>/dev/null
55218799Snwhitehornif [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
56218799Snwhitehorn	dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
57218799Snwhitehorn	"Wireless cannot be configured without making changes to the local system!" \ 0 0
58218799Snwhitehorn	exit 1
59218799Snwhitehornfi
60218799Snwhitehorn
61263956Sdteskeoutput=$( wpa_cli scan 2>&1 )
62263956Sdteskef_dprintf "%s" "$output"
63218799Snwhitehorndialog --backtitle "FreeBSD Installer" --title "Scanning" --ok-label "Skip" \
64218799Snwhitehorn	--pause "Waiting 5 seconds to scan for wireless networks..." \
65218799Snwhitehorn	9 40 5 || exit 1
66218799Snwhitehorn
67218799SnwhitehornSCAN_RESULTS=`wpa_cli scan_results`
68218799SnwhitehornNETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
69218799Snwhitehorn   '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
70218799Snwhitehorn   sort | uniq`
71218799Snwhitehorn
72248240Snwhitehornif [ -z "$NETWORKS" ]; then
73218799Snwhitehorn	dialog --backtitle "FreeBSD Installer" --title "Error" \
74218799Snwhitehorn	    --yesno "No wireless networks were found. Rescan?" 0 0 && \
75218799Snwhitehorn	    exec $0 $@
76218799Snwhitehorn	exit 1
77218799Snwhitehornfi
78218799Snwhitehorn
79218799Snwhitehornexec 3>&1
80218799SnwhitehornNETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
81218799Snwhitehorn    --backtitle \"FreeBSD Installer\" --title \"Network Selection\" --menu \
82218799Snwhitehorn    \"Select a wireless network to connect to.\" 0 0 0 \
83218799Snwhitehorn    $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
84218799Snwhitehorncase $? in
85218799Snwhitehorn0)	# OK
86218799Snwhitehorn	;;
87218799Snwhitehorn1)	# Cancel
88218799Snwhitehorn	exit 1
89218799Snwhitehorn	;;
90218799Snwhitehorn3)	# Rescan
91218799Snwhitehorn	exec $0 $@
92218799Snwhitehorn	;;
93218799Snwhitehornesac
94218799Snwhitehornexec 3>&-
95218799Snwhitehorn
96218799SnwhitehornENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
97218799Snwhitehorn    "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
98218799Snwhitehorn
99218799Snwhitehornif echo $ENCRYPTION | grep -q 'PSK'; then
100218799Snwhitehorn	exec 3>&1
101218799Snwhitehorn	PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
102218799Snwhitehorn	    --title "WPA Setup" --mixedform "" 0 0 0 \
103218799Snwhitehorn		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
104225539Sbrueffer		"Password" 2 0 "" 2 12 15 63 1 \
105218799Snwhitehorn		2>&1 1>&3` \
106218799Snwhitehorn	|| exec $0 $@
107218799Snwhitehorn	exec 3>&-
108218799Snwhitehornecho "network={
109218799Snwhitehorn	ssid=\"$NETWORK\"
110218799Snwhitehorn	psk=\"$PASS\"
111218799Snwhitehorn	priority=5
112218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
113263956Sdteskeelif echo $ENCRYPTION | grep -q EAP; then
114263956Sdteske	exec 3>&1
115263956Sdteske	USERPASS=`dialog --insecure --backtitle "FreeBSD Installer" \
116263956Sdteske	    --title "WPA-Enterprise Setup" --mixedform "" 0 0 0 \
117263956Sdteske		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
118263956Sdteske		"Username" 2 0 "" 2 12 25 63 0 \
119263956Sdteske		"Password" 3 0 "" 3 12 25 63 1 \
120263956Sdteske		2>&1 1>&3` \
121263956Sdteske	|| exec $0 $@
122263956Sdteske	exec 3>&-
123263956Sdteskeecho "network={
124263956Sdteske	ssid=\"$NETWORK\"
125263956Sdteske	key_mgmt=WPA-EAP" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
126263956Sdteskeecho "$USERPASS" | awk '
127263956Sdteske{
128263956Sdteske	if (NR == 1) {
129263956Sdteske		printf "	identity=\"%s\"\n", $1;
130263956Sdteske	} else if (NR == 2) {
131263956Sdteske		printf "	password=\"%s\"\n", $1;
132263956Sdteske	}
133263956Sdteske}' >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
134263956Sdteskeecho "	priority=5
135263956Sdteske}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
136218799Snwhitehornelif echo $ENCRYPTION | grep -q WEP; then
137218799Snwhitehorn	exec 3>&1
138218799Snwhitehorn	WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
139218799Snwhitehorn	    --title "WEP Setup" --mixedform "" 0 0 0 \
140218799Snwhitehorn		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
141218799Snwhitehorn		"WEP Key 0" 2 0 "" 2 12 15 0 1 \
142218799Snwhitehorn		2>&1 1>&3` \
143218799Snwhitehorn	|| exec $0 $@
144218799Snwhitehornecho "network={
145218799Snwhitehorn	ssid=\"$NETWORK\"
146218799Snwhitehorn	key_mgmt=NONE
147218799Snwhitehorn	wep_key0=\"$WEPKEY\"
148218799Snwhitehorn	wep_tx_keyidx=0
149218799Snwhitehorn	priority=5
150218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
151218799Snwhitehornelse	# Open
152218799Snwhitehornecho "network={
153218799Snwhitehorn	ssid=\"$NETWORK\"
154218799Snwhitehorn	key_mgmt=NONE
155218799Snwhitehorn	priority=5
156218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
157218799Snwhitehornfi
158218799Snwhitehorn
159218799Snwhitehorn# Connect to any open networks policy
160218799Snwhitehornecho "network={
161263956Sdteske	priority=0
162218799Snwhitehorn	key_mgmt=NONE
163218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
164218799Snwhitehorn
165218799Snwhitehorn# Bring up new network
166263956Sdteskeif [ "$BSDINSTALL_CONFIGCURRENT" ]; then
167263956Sdteske	output=$( wpa_cli reconfigure 2>&1 )
168263956Sdteske	f_dprintf "%s" "$output"
169263956Sdteskefi
170218799Snwhitehorn
171218799Snwhitehornexit 0
172263956Sdteske
173263956Sdteske################################################################################
174263956Sdteske# END
175263956Sdteske################################################################################
176