wlanconfig revision 258421
1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4258421Sdteske# 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: stable/10/usr.sbin/bsdinstall/scripts/wlanconfig 258421 2013-11-21 03:40:52Z dteske $
29258421Sdteske#
30258421Sdteske############################################################ INCLUDES
31218799Snwhitehorn
32258421SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
33258421Sdteske. $BSDCFG_SHARE/common.subr || exit 1
34258421Sdteske
35258421Sdteske############################################################ MAIN
36258421Sdteske
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
61258421Sdteskeoutput=$( wpa_cli scan 2>&1 )
62258421Sdteskef_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
72245980Snwhitehornif [ -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
113218799Snwhitehornelif echo $ENCRYPTION | grep -q WEP; then
114218799Snwhitehorn	echo FOO
115218799Snwhitehorn	exec 3>&1
116218799Snwhitehorn	WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
117218799Snwhitehorn	    --title "WEP Setup" --mixedform "" 0 0 0 \
118218799Snwhitehorn		"SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
119218799Snwhitehorn		"WEP Key 0" 2 0 "" 2 12 15 0 1 \
120218799Snwhitehorn		2>&1 1>&3` \
121218799Snwhitehorn	|| exec $0 $@
122218799Snwhitehornecho "network={
123218799Snwhitehorn	ssid=\"$NETWORK\"
124218799Snwhitehorn	key_mgmt=NONE
125218799Snwhitehorn	wep_key0=\"$WEPKEY\"
126218799Snwhitehorn	wep_tx_keyidx=0
127218799Snwhitehorn	priority=5
128218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
129218799Snwhitehornelse	# Open
130218799Snwhitehornecho "network={
131218799Snwhitehorn	ssid=\"$NETWORK\"
132218799Snwhitehorn	key_mgmt=NONE
133218799Snwhitehorn	priority=5
134218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
135218799Snwhitehornfi
136218799Snwhitehorn
137218799Snwhitehorn# Connect to any open networks policy
138218799Snwhitehornecho "network={
139218799Snwhitehorn	priority=5
140218799Snwhitehorn	key_mgmt=NONE
141218799Snwhitehorn}" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
142218799Snwhitehorn
143218799Snwhitehorn# Bring up new network
144258421Sdteskeif [ "$BSDINSTALL_CONFIGCURRENT" ]; then
145258421Sdteske	output=$( wpa_cli reconfigure 2>&1 )
146258421Sdteske	f_dprintf "%s" "$output"
147258421Sdteskefi
148218799Snwhitehorn
149218799Snwhitehornexit 0
150258421Sdteske
151258421Sdteske################################################################################
152258421Sdteske# END
153258421Sdteske################################################################################
154