wlanconfig revision 245980
1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4218799Snwhitehorn# All rights reserved.
5218799Snwhitehorn#
6218799Snwhitehorn# Redistribution and use in source and binary forms, with or without
7218799Snwhitehorn# modification, are permitted provided that the following conditions
8218799Snwhitehorn# are met:
9218799Snwhitehorn# 1. Redistributions of source code must retain the above copyright
10218799Snwhitehorn#    notice, this list of conditions and the following disclaimer.
11218799Snwhitehorn# 2. Redistributions in binary form must reproduce the above copyright
12218799Snwhitehorn#    notice, this list of conditions and the following disclaimer in the
13218799Snwhitehorn#    documentation and/or other materials provided with the distribution.
14218799Snwhitehorn#
15218799Snwhitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16218799Snwhitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17218799Snwhitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18218799Snwhitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19218799Snwhitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20218799Snwhitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21218799Snwhitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22218799Snwhitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23218799Snwhitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24218799Snwhitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25218799Snwhitehorn# SUCH DAMAGE.
26218799Snwhitehorn#
27218799Snwhitehorn# $FreeBSD: head/usr.sbin/bsdinstall/scripts/wlanconfig 245980 2013-01-27 14:49:54Z nwhitehorn $
28218799Snwhitehorn
29218799Snwhitehornecho -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
30218799Snwhitehornchmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
31218799Snwhitehorn
32218799Snwhitehornecho "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
33218799Snwhitehornecho "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
34218799Snwhitehornecho "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
35218799Snwhitehornecho "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
36218799Snwhitehornecho >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
37218799Snwhitehorn
38218799Snwhitehorn# Try to reach wpa_supplicant. If it isn't running and we can modify the
39218799Snwhitehorn# existing system, start it. Otherwise, fail.
40218799Snwhitehorn(wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
41218799Snwhitehorn	wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
42218799Snwhitehorn	(dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
43218799Snwhitehorn	"Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
44218799Snwhitehorn
45218799Snwhitehorn# See if we succeeded
46218799Snwhitehornwpa_cli ping >/dev/null 2>/dev/null
47218799Snwhitehornif [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
48218799Snwhitehorn	dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
49218799Snwhitehorn	"Wireless cannot be configured without making changes to the local system!" \ 0 0
50218799Snwhitehorn	exit 1
51218799Snwhitehornfi
52218799Snwhitehorn
53218799Snwhitehornwpa_cli scan >>$BSDINSTALL_LOG
54218799Snwhitehorndialog --backtitle "FreeBSD Installer" --title "Scanning" --ok-label "Skip" \
55218799Snwhitehorn	--pause "Waiting 5 seconds to scan for wireless networks..." \
56218799Snwhitehorn	9 40 5 || exit 1
57218799Snwhitehorn
58218799SnwhitehornSCAN_RESULTS=`wpa_cli scan_results`
59218799SnwhitehornNETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
60218799Snwhitehorn   '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
61218799Snwhitehorn   sort | uniq`
62218799Snwhitehorn
63245980Snwhitehornif [ -z "$NETWORKS" ]; then
64218799Snwhitehorn	dialog --backtitle "FreeBSD Installer" --title "Error" \
65218799Snwhitehorn	    --yesno "No wireless networks were found. Rescan?" 0 0 && \
66218799Snwhitehorn	    exec $0 $@
67218799Snwhitehorn	exit 1
68218799Snwhitehornfi
69218799Snwhitehorn
70218799Snwhitehornexec 3>&1
71218799SnwhitehornNETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
72218799Snwhitehorn    --backtitle \"FreeBSD Installer\" --title \"Network Selection\" --menu \
73218799Snwhitehorn    \"Select a wireless network to connect to.\" 0 0 0 \
74218799Snwhitehorn    $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
75218799Snwhitehorncase $? in
76218799Snwhitehorn0)	# OK
77218799Snwhitehorn	;;
78218799Snwhitehorn1)	# Cancel
79218799Snwhitehorn	exit 1
80218799Snwhitehorn	;;
81218799Snwhitehorn3)	# Rescan
82218799Snwhitehorn	exec $0 $@
83218799Snwhitehorn	;;
84218799Snwhitehornesac
85218799Snwhitehornexec 3>&-
86218799Snwhitehorn
87218799SnwhitehornENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
88218799Snwhitehorn    "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
89218799Snwhitehorn
90218799Snwhitehornif echo $ENCRYPTION | grep -q 'PSK'; then
91218799Snwhitehorn	exec 3>&1
92218799Snwhitehorn	PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
93218799Snwhitehorn	    --title "WPA Setup" --mixedform "" 0 0 0 \
94218799Snwhitehorn		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
95225539Sbrueffer		"Password" 2 0 "" 2 12 15 63 1 \
96218799Snwhitehorn		2>&1 1>&3` \
97218799Snwhitehorn	|| exec $0 $@
98218799Snwhitehorn	exec 3>&-
99218799Snwhitehornecho "network={
100218799Snwhitehorn	ssid=\"$NETWORK\"
101218799Snwhitehorn	psk=\"$PASS\"
102218799Snwhitehorn	priority=5
103218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
104218799Snwhitehornelif echo $ENCRYPTION | grep -q WEP; then
105218799Snwhitehorn	echo FOO
106218799Snwhitehorn	exec 3>&1
107218799Snwhitehorn	WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
108218799Snwhitehorn	    --title "WEP Setup" --mixedform "" 0 0 0 \
109218799Snwhitehorn		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
110218799Snwhitehorn		"WEP Key 0" 2 0 "" 2 12 15 0 1 \
111218799Snwhitehorn		2>&1 1>&3` \
112218799Snwhitehorn	|| exec $0 $@
113218799Snwhitehornecho "network={
114218799Snwhitehorn	ssid=\"$NETWORK\"
115218799Snwhitehorn	key_mgmt=NONE
116218799Snwhitehorn	wep_key0=\"$WEPKEY\"
117218799Snwhitehorn	wep_tx_keyidx=0
118218799Snwhitehorn	priority=5
119218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
120218799Snwhitehornelse	# Open
121218799Snwhitehornecho "network={
122218799Snwhitehorn	ssid=\"$NETWORK\"
123218799Snwhitehorn	key_mgmt=NONE
124218799Snwhitehorn	priority=5
125218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
126218799Snwhitehornfi
127218799Snwhitehorn
128218799Snwhitehorn# Connect to any open networks policy
129218799Snwhitehornecho "network={
130218799Snwhitehorn	priority=5
131218799Snwhitehorn	key_mgmt=NONE
132218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
133218799Snwhitehorn
134218799Snwhitehorn# Bring up new network
135218799Snwhitehorntest ! -z $BSDINSTALL_CONFIGCURRENT && wpa_cli reconfigure >>$BSDINSTALL_LOG
136218799Snwhitehorn
137218799Snwhitehornexit 0
138