-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpa_config
executable file
·241 lines (225 loc) · 6.12 KB
/
wpa_config
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/sh
#
# wpa_config script by Isaac Dunham
# written and released into public domain under CC0 in 2014
#
# I expressly disclaim ANY WARANTEE IMPLIED OR STATED, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARANTEES OF MERCHANTABILITY OR
# FITNESS FOR A PARTICULAR PURPOSE.
#
# wpa_config aims to be a dialog-based script for configuring wpa_supplicant
# with some aspects of a network manager thrown in.
# However, it stops short at starting/stopping/restarting wpa_supplicant
# itself; we leave that to system services.
mkdir -m 0700 -p /tmp/wpaconf || rm /tmp/wpaconf &&\
mkdir -m 0700 -p /tmp/wpaconf || {
echo "Cannot obtain private tempdir!"
exit 1
}
#DIALOG must implement:
# --insecure --output-fd --passwordbox --menu --inputbox --msgbox --fselect
# --insecure means "show asterisks"; I figure most people want that much acknowledgement.
# --fselect support is only needed for a couple features (user specified config files)
test -n "$DISPLAY" && DIALOG=`command -v Xdialog`
test -z "$DIALOG" && DIALOG=`command -v dialog`
# Unfortunately I have to live with whiptail...
test -z "$DIALOG" && DIALOG=`command -v whiptail`
newconf(){
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=0" >$NEWCF
}
loadconfig(){
true >$CTRL
LOADCONFS="`[ -e /etc/wpa_supplicant/wpa_supplicant.conf ] && echo global /etc/wpa_supplicant/wpa_supplicant.conf`"
LOADCONFS="$LOADCONFS `[ -e /etc/wpa/$IFCUR.conf ] && echo local /etc/wpa/$IFCUR.conf`"
$DIALOG --output-fd 3 --menu "Which config file do you want to load?" 0 0 0 \
$LOADCONFS other "browse for file" new "no file" 3>$CTRL
read conf <$CTRL
case $conf in
new) newconf
;;
global) cat /etc/wpa_supplicant/wpa_supplicant.conf >$NEWCF
;;
local) cat /etc/wpa/$IFCUR.conf >$NEWCF
;;
other) true >$CTRL
$DIALOG --output-fd 3 --fselect /etc 20 60 3>$CTRL
read conf <$CTRL
case "$conf" in
*.conf) true >$NEWCF
grep ^ctrl_interface "$conf" >/dev/null || newconf
cat "$conf" >>$NEWCF
;;
*) $DIALOG --msgbox "No valid config file specified!" 0 0
;;
esac
;;
esac
}
saveconfig(){
true >$CTRL
$DIALOG --output-fd 3 --menu \
"Choose a config file to write. WARNING! This WILL IMMEDIATELY OVERWRITE the file!" 0 0 0 \
global /etc/wpa_supplicant/wpa_supplicant.conf \
local /etc/wpa/$IFCUR.conf other "browse for file" 3>$CTRL
read conf <$CTRL
case $conf in
global) cat $NEWCF >/etc/wpa_supplicant/wpa_supplicant.conf
;;
local) cat $NEWCF >/etc/wpa/$IFCUR.conf
wpa_cli -i $IFCUR reconfigure
;;
other) true >$CTRL
$DIALOG --output-fd 3 --fselect /etc 20 60 3>$CTRL
read conf <$CTRL
case "$conf" in
*.conf) cat $NEWCF >"$conf"
;;
*) $DIALOG --msgbox "No valid config file specified!" 0 0
;;
esac
;;
esac
test "$conf" = "local" || for IF in `ls /var/run/wpa_supplicant/ 2>/dev/null`
do wpa_cli -i $IF reconfigure; done
}
setif(){
true >$CTRL
$DIALOG --output-fd 3 --menu "Select an interface:" 0 0 0 `ls /sys/class/net |sed -e 's/^\(.*\)$/\1 \1/g'` 3>$CTRL
read net <$CTRL
export IFCUR=${net:-$IFCUR}
unset net
}
net_add_open() {
true >$CTRL
test -z "$1" && $DIALOG --output-fd 3 --inputbox "Enter the SSID you wish to connect to.\nIf you enter 'any', the computer will\nconnect to any open network." 0 0 any 3>$CTRL
read net <$CTRL
test -n "$1" && net=$1
test -n "$net" || return
echo "network={" >> $NEWCF
case $net in
any) ;;
*) echo ' ssid="'"$net"'"' >>$NEWCF
;;
esac
echo ' key_mgmt=NONE' >>$NEWCF
echo '}' >>$NEWCF
}
net_add_wep(){
true >$CTRL
$DIALOG --output-fd 3 --insecure --passwordbox "Enter the WEP key for SSID: $1" 0 0 3>$CTRL
test `wc -c <$CTRL` -gt 0 || return
cat >>$NEWCF <<EOF
network={
ssid="$1"
key_mgmt=IEEE8021X
eap=MD5
wep_key0=`cat $CTRL`
}
EOF
echo done >$CTRL
}
net_add_wpa() {
true >$CTRL
$DIALOG --output-fd 3 --insecure --passwordbox "Enter the passphrase for SSID: $1" 0 0 3>$CTRL
test `wc -c <$CTRL` -gt 0 || return
wpa_passphrase $1 <$CTRL >>$NEWCF
echo done >$CTRL
}
network_add() {
true >$CTRL
$DIALOG --output-fd 3 --inputbox "Enter the SSID you wish to connect to." 0 0 3>$CTRL
read net <$CTRL
test -n "$net" || return
true >$CTRL
$DIALOG --output-fd 3 --menu "Choose the network encryption:" 0 0 0 \
No encryption WEP encryption WPA encryption 3>$CTRL || return
case `cat $CTRL` in
No) net_add_open $net
;;
WEP) net_add_wep $net
;;
WPA) net_add_wpa $net
;;
esac
}
#scanner:
# scan
# save results to file
# parse from file
#Buttons:
#Refresh/Add/Leave
# Usage: IFCUR=if0 scanner
scanner() (
if test OK != "`wpa_cli -i "$IFCUR" scan `"
then
$DIALOG --msgbox "wpa_cll status for $IFCUR failed!" 0 0
return
fi
net=Rescan
while test -n "$net"
do
case $net in
Rescan)
SCAN=`mktemp /tmp/wpaconf/scanXXXXXX`
SCAN2=`mktemp /tmp/wpaconf/scanXXXXXX`
wpa_cli -i "$IFCUR" scan_results >$SCAN
echo '$DIALOG --output-fd 3 --menu "Select a network to connect to, or rescan." 0 0 0 \' >$SCAN2
cat $SCAN |awk '{if (NF==5) print $1 "\t\""$5"\" \\";
else if (NF==4) print $1 "\t\"" $4"\" \\"; }' >>$SCAN2
echo 'Rescan "for more networks" 3>$CTRL' >>$SCAN2
true >$CTRL
. $SCAN2
read net <$CTRL
;;
*:*:*:*)
NET="`awk '{if ($1 == "'$net'") print $NF}' <$SCAN`"
#network_add $NET
case `awk '{if ($1 == "'$net'") if (NF == 5) print $4; else print "open"}' <$SCAN` in
*WPA*PSK*) net_add_wpa $NET
;;
*WEP*) net_add_wep $NET
;;
open) net_add_open $NET
esac
net=Rescan
;;
esac
done
rm -f $SCAN $SCAN2
)
#initialize before main loop
CTRL=`mktemp /tmp/wpaconf/ctrlXXXXXX`
cmd=maindummy
NEWCF=`mktemp /tmp/wpaconf/confXXXXXX`
loadconfig
setif
test -n "$IFCUR" || exit 1
#Now, main loop:
while test -n "$cmd"
do true >$CTRL
exec 3>$CTRL
$DIALOG --output-fd 3 --menu "Select an operation:" 0 0 0 \
1 "change interface ($IFCUR)" \
2 disconnect 3 reassociate 4 scan 5 "add network" \
6 "load new config file" 7 "save config file" 8 "view config file"
exec 3<&-
read cmd <$CTRL
case $cmd in
1) setif
;;
2) wpa_cli -i $IFCUR disconnect
;;
3) wpa_cli -i $IFCUR reassociate
;;
4) scanner
;;
5) network_add
;;
6) loadconfig
;;
7) saveconfig
;;
8) $DIALOG --scrollbar --textbox $NEWCF 0 0
;;
esac
done