]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/bsdinstall/scripts/wlanconfig
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / bsdinstall / scripts / wlanconfig
1 #!/bin/sh
2 #-
3 # Copyright (c) 2011 Nathan Whitehorn
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28
29 echo -n > $BSDINSTALL_TMPETC/wpa_supplicant.conf
30 chmod 0600 $BSDINSTALL_TMPETC/wpa_supplicant.conf
31
32 echo "ctrl_interface=/var/run/wpa_supplicant" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
33 echo "eapol_version=2" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
34 echo "ap_scan=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
35 echo "fast_reauth=1" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
36 echo >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
37
38 # Try to reach wpa_supplicant. If it isn't running and we can modify the
39 # existing system, start it. Otherwise, fail.
40 (wpa_cli ping >/dev/null 2>/dev/null || ([ ! -z $BSDINSTALL_CONFIGCURRENT ] && \
41         wpa_supplicant -B -i $1 -c $BSDINSTALL_TMPETC/wpa_supplicant.conf)) || \
42         (dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
43         "Could not start wpa_supplicant!" 0 0; exit 1) || exit 1
44
45 # See if we succeeded
46 wpa_cli ping >/dev/null 2>/dev/null
47 if [ $? -ne 0 -a -z $BSDINSTALL_CONFIGCURRENT ]; then
48         dialog --backtitle "FreeBSD Installer" --title "Error" --msgbox \
49         "Wireless cannot be configured without making changes to the local system!" \ 0 0
50         exit 1
51 fi
52
53 wpa_cli scan >>$BSDINSTALL_LOG
54 dialog --backtitle "FreeBSD Installer" --title "Scanning" --ok-label "Skip" \
55         --pause "Waiting 5 seconds to scan for wireless networks..." \
56         9 40 5 || exit 1
57
58 SCAN_RESULTS=`wpa_cli scan_results`
59 NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
60    '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
61    sort | uniq`
62
63 if [ -z "$NETWORKS" ]; then
64         dialog --backtitle "FreeBSD Installer" --title "Error" \
65             --yesno "No wireless networks were found. Rescan?" 0 0 && \
66             exec $0 $@
67         exit 1
68 fi
69
70 exec 3>&1
71 NETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
72     --backtitle \"FreeBSD Installer\" --title \"Network Selection\" --menu \
73     \"Select a wireless network to connect to.\" 0 0 0 \
74     $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
75 case $? in
76 0)      # OK
77         ;;
78 1)      # Cancel
79         exit 1
80         ;;
81 3)      # Rescan
82         exec $0 $@
83         ;;
84 esac
85 exec 3>&-
86
87 ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
88     "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`
89
90 if echo $ENCRYPTION | grep -q 'PSK'; then
91         exec 3>&1
92         PASS=`dialog --insecure --backtitle "FreeBSD Installer" \
93             --title "WPA Setup" --mixedform "" 0 0 0 \
94                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
95                 "Password" 2 0 "" 2 12 15 63 1 \
96                 2>&1 1>&3` \
97         || exec $0 $@
98         exec 3>&-
99 echo "network={
100         ssid=\"$NETWORK\"
101         psk=\"$PASS\"
102         priority=5
103 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
104 elif echo $ENCRYPTION | grep -q WEP; then
105         echo FOO
106         exec 3>&1
107         WEPKEY=`dialog --insecure --backtitle "FreeBSD Installer" \
108             --title "WEP Setup" --mixedform "" 0 0 0 \
109                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
110                 "WEP Key 0" 2 0 "" 2 12 15 0 1 \
111                 2>&1 1>&3` \
112         || exec $0 $@
113 echo "network={
114         ssid=\"$NETWORK\"
115         key_mgmt=NONE
116         wep_key0=\"$WEPKEY\"
117         wep_tx_keyidx=0
118         priority=5
119 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
120 else    # Open
121 echo "network={
122         ssid=\"$NETWORK\"
123         key_mgmt=NONE
124         priority=5
125 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
126 fi
127
128 # Connect to any open networks policy
129 echo "network={
130         priority=5
131         key_mgmt=NONE
132 }" >> $BSDINSTALL_TMPETC/wpa_supplicant.conf
133
134 # Bring up new network
135 test ! -z $BSDINSTALL_CONFIGCURRENT && wpa_cli reconfigure >>$BSDINSTALL_LOG
136
137 exit 0