-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraspberryap
executable file
·361 lines (295 loc) · 9.47 KB
/
raspberryap
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
### BEGIN INIT INFO
# Provides: raspberryap
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
#### END INIT INFO
set -u
readonly F_CONFIG_DIR='/tmp/raspberryap'
readonly F_TMP_DIR="$F_CONFIG_DIR/tmp"
readonly F_CONFIG='/etc/raspberryap.conf'
readonly F_CONFIG_HOSTAPD="$F_CONFIG_DIR/hostapd.conf"
readonly F_CONFIG_DNSMASQ="$F_CONFIG_DIR/dnsmasq.conf"
readonly F_HOSTAPD_LOG="$F_TMP_DIR/hostapd.log"
readonly F_PID="$F_TMP_DIR/pid"
readonly MAX_CHANNEL=13
readonly KEY_WORD=('INTERFACE' 'TYPE_ESSID' 'ESSID' 'WPA' 'CHANNEL' 'ESSID_CHECK' 'RX_MAX' 'FORCE_RESCAN')
INTERFACE=
ESSID_CHECK=()
WPA_FILE=
CHANNEL="$(($(($RANDOM % $MAX_CHANNEL)) + 1))"
ESSID_AP=
TYPE_ESSID=0
WPA=
RX_MAX=68
FORCE_RESCAN=0
function flush_interface() {
rfkill unblock all
dhclient -r $INTERFACE &>/dev/null
killall -g wpa_supplicant &>/dev/null
ifdown $INTERFACE &>/dev/null
ifconfig $INTERFACE down &>/dev/null
ip addr flush dev $INTERFACE &>/dev/null
ifconfig $INTERFACE up &>/dev/null
}
function kill_service() {
service NetworkManager stop &>/dev/null
service wicd stop &>/dev/null
service dnsmasq stop &>/dev/null
service hostapd stop &>/dev/null
killall -g dnsmasq &>/dev/null
killall -g hostapd &>/dev/null
}
function help() {
echo "Usage: $0 check_conf|start|stop"
}
function check_dependencies() {
if ! command -v rfkill &>/dev/null || ! command -v hostapd &>/dev/null || ! command -v dnsmasq &>/dev/null || ! command -v iw &>/dev/null || ! command -v iwconfig &>/dev/null || ! command -v wpa_supplicant &>/dev/null; then
echo "Errore. Mancano alcune dipendenze"
exit 1
fi
}
function check_file_config() {
if [ ! -f $F_CONFIG ]; then
echo "File di configurazione '$F_CONFIG' non trovato"
exit 1
fi
F_TMP="$(grep -Ev '^#|^$' $F_CONFIG | sed 's/^[[:space:]]*//')"
if echo $F_TMP | cut -d'=' -f 1 | grep '^[a-z]*$' &>/dev/null; then
echo "Errore. Le keyword evono essere tutte in maiuscolo"
exit 1
fi
while read key_word; do
keyword="$(echo "$key_word" | cut -d'=' -f1)"
case "$keyword" in
'INTERFACE')
INTERFACE="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2)"
if [ -z $INTERFACE ]; then
echo "Valore nullo per '$keyword'"
exit 1
fi
check_interface
;;
'ESSID')
ESSID_AP="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2)"
if [ -z $ESSID_AP ]; then
echo "Valore nullo per '$keyword'"
exit 1
fi
;;
'TYPE_ESSID')
TYPE_ESSID="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2 | grep -E '^0$|^1$')"
if [ -z $TYPE_ESSID ]; then
echo "Valore nullo per '$keyword'"
exit 1
fi
if [ $TYPE_ESSID -gt 1 ]; then
echo "Valore non valido per '$keyword': accetta '0', '1'"
exit 1
fi
;;
'WPA')
WPA="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2 | grep -E '^.{8,63}$')"
if [ -z $WPA ]; then
echo "Valore non valido in '$keyword'. PS. (almeno 8 caratteri)"
exit 1
fi
;;
'CHANNEL')
CHANNEL="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2 | grep '^[0-9]*$')"
if [ -z $CHANNEL ]; then
echo "Valore non valido in '$keyword'"
exit 1
fi
if [ $CHANNEL -gt $MAX_CHANNEL ]; then
echo "Errore. Il valore CHANNEL deve essere compreso tra 1 e $MAX_CHANNEL"
exit 1
fi
;;
'RX_MAX')
RX_MAX="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2 | grep -E '^(^|[^0-9])[0-9]{1,2}($|[^0-9])')"
if [ -z $RX_MAX ]; then
echo "Valore non valido in '$keyword'"
exit 1
fi
;;
'ESSID_CHECK')
arg_keyword="$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2)"
if [ -z $arg_keyword ]; then
echo "Valore nullo per '$keyword'"
exit 1
fi
ifs=$IFS
IFS=','
for essid in $(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2); do
WPA_FILE="$(echo "$essid" | cut -d':' -f2)"
if [ ! -f "$WPA_FILE" ]; then
echo "File di configurazione wpa '$WPA_FILE' non trovato"
exit 1
fi
ESSID_CHECK+=($essid)
done
IFS=$ifs
;;
'FORCE_RESCAN')
FORCE_RESCAN=$(echo "$key_word" | grep "^\<$keyword\>" | cut -d'=' -f2 | grep '^[0-9]*$')
if [ -z $FORCE_RESCAN ]; then
echo "Valore nullo per '$keyword'"
exit 1
fi
;;
* | ?)
echo "Errore. Keyword '$keyword' non valida"
exit 1
;;
esac
done <<<$F_TMP
if [ -z $INTERFACE ]; then
echo "Manca l'opzione INTERFACE nel file di configurazione"
exit 1
fi
if [ -z $ESSID_AP ]; then
echo "Manca l'opzione ESSID nel file di configurazione"
exit 1
fi
}
function check_interface() {
phy_interface="$(cat /sys/class/net/$INTERFACE/phy80211/name)"
if ! find /sys/class/net/ ! -type d | grep "\<$INTERFACE\>" &>/dev/null; then
echo "L'interfaccia '$INTERFACE' non esiste"
exit 1
fi
iw $phy_interface info | grep 'AP$' &>/dev/null
if [ "$?" != "0" ]; then
echo "L'interfaccia '$INTERFACE' non supporta la modalità AP"
exit 1
fi
}
function connection_ap() {
total_essid_check=${#ESSID_CHECK[@]}
ESSID_CONNECT=
for essid in $(seq 0 $(($total_essid_check - 1))); do
essid_check="$(echo ${ESSID_CHECK[$essid]} | cut -d':' -f1)"
SCAN_RESULT="$(iwlist $INTERFACE scan 2>/dev/null | expand | grep -B2 'ESSID' | grep -B2 "\"\<$essid_check\>\"\$" | sed 's/^[[:space:]]*//')"
if [ ! -z "$SCAN_RESULT" ]; then
echo "* Trovato ESSID '$essid_check'"
signal_cap="$(echo "$SCAN_RESULT" | head -n1 | sed 's/^.*Signal level=//; s/[-a-zA-Z ]//g' | awk '{print $1}')"
if [ $signal_cap -gt $RX_MAX ]; then
echo "[!] Ricezione del segnale debole per l'essid '$essid_check'"
else
WPA_FILE="$(echo ${ESSID_CHECK[$essid]} | cut -d':' -f2)"
ESSID_CONNECT="$essid_check"
break
fi
fi
done
if [ ! -z $ESSID_CONNECT ]; then
wpa_supplicant -i $INTERFACE -c $WPA_FILE -B &>/dev/null && dhclient $INTERFACE
if [ "$?" == "0" ]; then
echo "* Connessione riuscita"
else
echo "[!] Errore nella connessione"
flush_interface
kill_service
return 1
fi
else
return 1
fi
return 0
}
function creation_ap() {
# `hostapd` Configuring
echo -e "interface=$INTERFACE\n" \
"driver=nl80211\n" \
"ssid=$ESSID_AP\n" \
"hw_mode=g\n" \
"channel=$CHANNEL\n" \
"auth_algs=1\n" \
"ignore_broadcast_ssid=$TYPE_ESSID" | sed 's/^[[:space:]]*//' >$F_CONFIG_HOSTAPD
if [ ! -z $WPA ]; then
echo -e "macaddr_acl=0\n" \
"wpa=2\n" \
"wpa_passphrase=$WPA\n" \
"wpa_key_mgmt=WPA-PSK\n" \
"wpa_pairwise=CCMP TKIP\n" \
"rsn_pairwise=CCMP" | sed 's/^[[:space:]]*//' >>$F_CONFIG_HOSTAPD
fi
# `dnsmasq` Configuring
echo -e "interface=$INTERFACE\n" \
"bind-interfaces\n" \
"dhcp-authoritative\n" \
"dhcp-range=192.168.2.2,192.168.2.254,3h\n" \
"dhcp-option=option:router,192.168.2.1\n" \
"server=192.168.2.1" | sed 's/^[[:space:]]*//' >$F_CONFIG_DNSMASQ
rm -rf $F_HOSTAPD_LOG
if hostapd -B $F_CONFIG_HOSTAPD -f $F_HOSTAPD_LOG && ifconfig $INTERFACE 192.168.2.1 netmask 255.255.255.0 && dnsmasq -C $F_CONFIG_DNSMASQ; then
echo "* Creazione AP riuscita"
else
echo "[!] Problema nella creazione dell'AP"
kill_service
flush_interface
return 1
fi
return 0
}
## MAIN ##
if [ $EUID -ne 0 ]; then
echo "Bisogna essere amminstratori per lanciare lo script"
exit 1
fi
check_dependencies
mkdir $F_CONFIG_DIR &>/dev/null
mkdir $F_TMP_DIR &>/dev/null
if [ $# -eq 0 ]; then
help
exit 1
fi
case "$1" in
check_conf)
check_file_config || exit 1
;;
start)
check_file_config || exit 1
kill_service
flush_interface
connection_ap || creation_ap
if [ $FORCE_RESCAN -gt 0 ]; then
(while [ 1 ]; do
echo "+ Riscansione tra $FORCE_RESCAN minuti..."
sleep $(($FORCE_RESCAN * 60))
status=$(iwconfig wlan0 | expand | sed 's/[[:space:]]/\n/g; s/^[[:space:]]*//' | grep 'Mode:' | cut -d':' -f2)
if [ "$status" == "Master" ]; then
if [ $(grep '\-CONNECTED' $F_HOSTAPD_LOG | wc -l) -eq $(grep '\-DISCONNECTED' $F_HOSTAPD_LOG | wc -l) ]; then
echo "+ Spegnimento AP..."
kill_service
flush_interface
ifconfig $INTERFACE down
iwconfig $INTERFACE mode managed
ifconfig $INTERFACE up
connection_ap || creation_ap
fi
else
if [ "$(iwconfig wlan0 | expand | sed 's/[[:space:]]/\n/g; s/^[[:space:]]*//' | grep ESSID | cut -d':' -f 2)" == "off/any" ]; then
killall -g wpa_supplicant &>/dev/null
flush_interface
connection_ap || creation_ap
fi
fi
done) &
echo $! >"$F_PID"
fi
;;
stop)
kill -TERM $(cat "$F_PID") &>/dev/null
kill_service
flush_interface
;;
* | ?)
help
exit 1
;;
esac
exit 0