-
Notifications
You must be signed in to change notification settings - Fork 0
/
wipi
executable file
·588 lines (523 loc) · 18.8 KB
/
wipi
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/bin/bash
#Show help
showHelp() {
echo "Usage: $(basename $0) [stop|start|restart] [OPTIONS] <interface>"
echo "Used to create wireless access points with ease"
echo
echo "OPTIONS:"
echo -e " -c --channel <channel>\tChannel the access point is broadcasted on"
echo -e " -e --essid <ESSID>\t\tESSID of the access point (The default is 'WiPi AP')"
echo -e " -d --domain <domain>\tDomain name of the local network created by dnsmasq"
echo -e " -h --help\t\t\tShows this menu"
echo -e " -i --interface <interface>\tThe interface used to route traffic out of (default is eth0)"
echo -e " -n --nat <static ip>\tThe static ip set to the NAT (The subnet mask is a /24)"
echo -e " -p --password <password>\tThe password of the access point (WPA2/PSK)"
echo -e " -v --verbose\t\t\tVerbose mode. Shows much more output about what is going on"
echo
echo "Examples:"
echo " $(basename $0) start -c 1 -d foo.net --essid Foo -p Bar wlan0"
echo " $(basename $0) restart -e 'Free WiFi' -c11 -i enp2s0 wlp3s0"
echo " $(basename $0) stop wlan1"
}
#Used to print verbose statements
verbose() {
if [ $verbosity = true ]; then
for i in "$@"; do
echo $i
done
fi
}
#Used to check if two files are the same by grabbing their md5 sums
checkFiles() {
if [ "`md5sum $1 | cut -d ' ' -f 1`" != "`md5sum $2 | cut -d ' ' -f 1`" ]; then
echo 1
else
echo 0
fi
}
#Check to see if a service is active
checkActive() {
echo `systemctl status $1 | grep Active | cut -d ':' -f 2 | xargs | cut -d ' ' -f 1`
}
#Check to see if a service is running
checkRunning() {
echo `systemctl status $1 | grep Active | cut -d ':' -f 2 | xargs | cut -d ' ' -f 2 | tr '()' ' ' | xargs`
}
#Check to see if a service is loaded
checkLoaded() {
echo `systemctl status $1 | grep Loaded | cut -d ':' -f 2 | xargs | cut -d ' ' -f 1`
}
#TODO: Clean this whole thing up
#Used to start the services
startServices() {
#What the function returns
local output=0
#Check through all of the services
for service in "$@"; do
#Attempt to start the services
if [ "`checkActive $service`" != "active" ]; then
if [ "`checkLoaded $service`" != "masked" ]; then
verbose "Starting $service"
systemctl start $service
else
#Check to see if service is running as a process
if [ -n "`pgrep $service`" ]; then
output=1
verbose "Failed to start $service because it is already running"
break
elif [ "$service" = "dnsmasq" ]; then
verbose "Starting $service"
dnsmasq
elif [ "$service" = "hostapd" ]; then
verbose "Starting $service"
hostapd -d /etc/hostapd/hostapd.conf &> /dev/null &
else
output=1
verbose "Failed to start $service"
break
fi
fi
#Check to see if the service isn't running and isn't actually running as a process
elif [ "`checkRunning $service`" != "running" ] && [ -z "`pgrep $service`" ]; then
if [ "$service" = "dnsmasq" ]; then
verbose "Starting $service"
dnsmasq
elif [ "$service" = "hostapd" ]; then
verbose "Starting $service"
hostapd -d /etc/hostapd/hostapd.conf &> /dev/null &
fi
else
output=1
verbose "Failed to start $service because it is already running"
break
fi
done
#Return of the function
echo $output
}
#Used to kill services
killServices() {
#What the function returns
local output=0
#Check through all of the services
for service in "$@"; do
#Check to see if the service is active
if [ "`checkActive $service`" = "active" ] && [ "`checkRunning $service`" = "running" ]; then
verbose "Stopping $service"
systemctl stop $service
#If the service is a process, kill it
elif [ -n "`pgrep $service`" ]; then
verbose "Stopping $service"
pkill $service
#Otherwise error
else
systemctl stop $service
output=1
verbose "Failed to stop $service because it isn't running"
break
fi
done
#Make sure the services are inactive
for service in "$@"; do
if [ $output -eq 1 ]; then
if [ -n "`pgrep $service`" ]; then
pkill $service
fi
elif [ "`checkActive $service`" = "active" ]; then
systemctl stop $service
elif [ -n "`pgrep $service`" ]; then
pkill $service
else
break
fi
done
#Return of the function
echo $output
}
#Check to see if the interface exists
checkInterface() {
if [ -z "$interface" ]; then
echo "No interface specified. Please specify a wireless interface"
exit 1;
elif [ -z "`ip l | grep $interface`" ]; then
echo "Interface $interface does not exist. Please choose an interface that exists"
exit 1;
elif [ -z "`iw dev | grep $interface`" ]; then
echo "Interface $interface isn't a wireless card. Please choose an interface that is"
exit 1;
fi
}
#Create new hostapd config file
hostapdConfigure() {
#Used to check if the config exists already
hostapdConfig=false
#Config file
echo "interface=$interface" > /etc/hostapd/hostapd.conf.temp
echo "driver=nl80211" >> /etc/hostapd/hostapd.conf.temp
echo "ssid=$name" >> /etc/hostapd/hostapd.conf.temp
echo "hw_mode=g" >> /etc/hostapd/hostapd.conf.temp
echo "channel=$channel" >> /etc/hostapd/hostapd.conf.temp
echo "wmm_enabled=0" >> /etc/hostapd/hostapd.conf.temp
echo "macaddr_acl=0" >> /etc/hostapd/hostapd.conf.temp
echo "auth_algs=1" >> /etc/hostapd/hostapd.conf.temp
echo "ignore_broadcast_ssid=0" >> /etc/hostapd/hostapd.conf.temp
#If access point is closed or not
if [ -n "$password" ]; then
echo "wpa=2" >> /etc/hostapd/hostapd.conf.temp
echo "wpa_passphrase=$password" >> /etc/hostapd/hostapd.conf.temp
echo "wpa_key_mgmt=WPA-PSK" >> /etc/hostapd/hostapd.conf.temp
echo "wpa_pairwise=TKIP" >> /etc/hostapd/hostapd.conf.temp
echo "rsn_pairwise=CCMP" >> /etc/hostapd/hostapd.conf.temp
fi
#Check to see if a hostapd config file exists
if [ -s /etc/hostapd/hostapd.conf ]; then
hostapdConfig=true
fi
#If config exists, leave it. Otherwise create a new one
if [ $hostapdConfig = true ]; then
if [ "`checkFiles /etc/hostapd/hostapd.conf.temp /etc/hostapd/hostapd.conf`" = "1" ]; then
verbose "hostapd config file found. Creating a backup of the old one"
mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.bkp
verbose "Backup file located at /etc/hostapd/hostapd.conf.bkp"
fi
else
verbose "hostapd config file not found. Creating a new one"
fi
mv /etc/hostapd/hostapd.conf.temp /etc/hostapd/hostapd.conf
#Make sure our config file gets loaded
sed -ie 's/DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd
sed -ie 's/#DAEMON_CONF/DAEMON_CONF/g' /etc/default/hostapd
}
#Create new dnsmasq config file
dnsmasqConfigure() {
#Used to check if the config exists already
dnsmasqConfig=false
#Check if the dnsmasq.conf file exists
if [ -s /etc/dnsmasq.conf ]; then
dnsmasqConfig=true
fi
echo "domain-needed" > /etc/dnsmasq.conf.temp #Blocks incomplete requests from leaving the network
echo "bogus-priv" >> /etc/dnsmasq.conf.temp #Prevents private addresses from being forwarded
echo "no-resolv" >> /etc/dnsmasq.conf.temp #Don't read /etc/resolv.conf for DNS servers
echo "domain=$domain" >> /etc/dnsmasq.conf.temp #Set the local domain
echo "expand-hosts" >> /etc/dnsmasq.conf.temp #Allow hosts to use this domain
echo "local=/$domain/ " >> /etc/dnsmasq.conf.temp #Ensures the domain is private
echo "listen-address=127.0.0.1" >> /etc/dnsmasq.conf.temp #Make sure dnsmasq listens on the right networks
echo "listen-address=$staticIP" >> /etc/dnsmasq.conf.temp #Make sure dnsmasq listens on the right networks
echo "" >> /etc/dnsmasq.conf.temp
echo "interface=$interface" >> /etc/dnsmasq.conf.temp #Set the interface to route on
echo " dhcp-range=$staticIPRange.2,$staticIPRange.254,255.255.255.0,5m" >> /etc/dnsmasq.conf.temp #Set the ip range
echo " dhcp-option=3,$staticIP" >> /etc/dnsmasq.conf.temp #Set the router IP
echo " dhcp-option=6,$staticIP" >> /etc/dnsmasq.conf.temp #Set the DHCP server IP
echo " server=8.8.8.8" >> /etc/dnsmasq.conf.temp #Set upstream DNS server to Google
echo " server=8.8.4.4" >> /etc/dnsmasq.conf.temp #Set upstream DNS server to Google
echo "" >> /etc/dnsmasq.conf.temp
echo "bind-interfaces" >> /etc/dnsmasq.conf.temp #Makes sure dnsmasq only listens on specified interfaces
echo "log-facility=/var/log/dnsmasq.log" >> /etc/dnsmasq.conf.temp #Set the logs file
echo "log-queries" >> /etc/dnsmasq.conf.temp #Logs queries
echo "log-dhcp" >> /etc/dnsmasq.conf.temp #Logs dhcp
#Check to see if the files are the same
if [ $dnsmasqConfig = true ]; then
if [ "`checkFiles /etc/dnsmasq.conf.temp /etc/dnsmasq.conf`" = "1" ]; then
#Make a backup on failure
verbose "dnsmasq config file found. Backing up old one"
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bkp
else
#Remove the new config
rm /etc/dnsmasq.conf.temp
verbose "dnsmasq config file not found. Created a new one"
fi
else
#Make the new configs
mv /etc/dnsmasq.conf.temp /etc/dnsmasq.conf
fi
echo "$staticIP $domain" > /etc/dnsmasq.hosts.temp #Creates a dns entry to allow the domain to be pinged
if [ "`checkFiles /etc/dnsmasq.hosts.temp /etc/dnsmasq.hosts`" = "1" ]; then
mv /etc/dnsmasq.hosts /etc/dnsmasq.hosts.bkp
mv /etc/dnsmasq.hosts.temp /etc/dnsmasq.hosts
else
rm /etc/dnsmasq.hosts.temp
fi
}
#Check to make sure the dependencies are installed
checkDepends() {
depend="iptables hostapd dnsmasq"
#Check for each dependency
for item in $depend; do
#Check in the installed programs lists
programList="`dpkg -l | grep $item | sed -e 's/ */ /g' | cut -d ' ' -f 2`"
realProgram=""
#Go through each program found in the list and make sure it's actually correct
for program in $programList; do
#Check to make sure the program has the exact same name
if [ $program = $item ]; then
realProgram="$item"
fi
done
if [ -z $realProgram ]; then
#Read input
read -p "$item not installed. Would you like to install it now? (y/n) " input
#Check to make sure the user agrees
#If so install it. It not, exit the program. Otherwise, wait for a valid option
while true; do
if [ "$input" = "y" ] || [ "$input" = "Y" ]; then
apt install $item -y
systemctl stop $item
break
elif [ "$input" = "n" ] || [ "$input" = "N" ]; then
echo "Exiting"
exit 0
else
read -p "Please enter a valid option. (y/n) " input
fi
done
fi
done
dnsmasqConfigure
hostapdConfigure
}
startAP() {
#Check to see if the interface exists
checkInterface
#Check to make sure the dependencies are installed
checkDepends
#Check to see if the status file exists
if [ -e /etc/wipi.status ]; then
#Check if the access point is running via status file
if [ "`cat /etc/wipi.status`" = "running" ]; then
echo "WiPi is already running. If this is a mistake, please remove /etc/wipi.status"
exit 1
#Error if in the wrong format
elif [ "`cat /etc/wipi.status`" != "stopped" ]; then
echo "Error reading /etc/wipi.status. Please remove it"
exit 1
else
#Proceed
echo "running" > /etc/wipi.status
fi
else
#Proceed
echo "running" > /etc/wipi.status
fi
#Set up iptables rules
verbose "Saving existing iptables rules to /etc/iptables.ipv4.nat"
sh -c "iptables-save" > /etc/iptables.ipv4.nat
verbose "Creating new routing rules"
iptables -F
iptables -t nat -A POSTROUTING -o $ipInterface -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
#iptables -i $interface -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
#iptables -i $interface -A INPUT -p tcp --dport 443 -j ACCEPT
#iptables -i $interface -A INPUT -p tcp --dport 80 -j ACCEPT
#iptables -i $interface -A INPUT -p udp --dport 67:68 -j ACCEPT
#iptables -i $interface -A INPUT -p udp --dport 53 -j ACCEPT
#iptables -i $interface -A INPUT -p tcp --dport 53 -j ACCEPT
#iptables -i $interface -A INPUT -p tcp --dport 22 -j ACCEPT
#iptables -i $interface -A INPUT -j DROP
#Enable ip forwarding
if [ "`cat /proc/sys/net/ipv4/ip_forward`" -eq 0 ]; then
verbose "Enabling ip forwarding"
echo 1 > /proc/sys/net/ipv4/ip_forward
fi
#Killing conflicting processes
if [ -n "`pgrep NetworkManager`" ]; then
verbose "Killing Network Manager"
pkill NetworkManager
fi
if [ -n "`pgrep wpa_supplicant`" ]; then
verbose "Killing wpa supplicant"
pkill wpa_supplicant
fi
#Configure hostapd
hostapdConfigure
#Bring the interface up
verbose "Bringing the interface up"
ip l set $interface up
#Add an ip address
if [ -z "`ip a show $interface | grep $staticIP/24`" ]; then
verbose "Adding static ip $staticIP to interface $interface"
ip a add $staticIP/24 dev $interface
fi
#sleep 1
#Remove old leases
if [ -e /var/lib/misc/dnsmasq.leases ];then
rm /var/lib/misc/dnsmasq.leases
fi
#Try to start services
if [ "`startServices hostapd dnsmasq`" = "1" ]; then
echo "Failed to start the access point (it's most likely already running)"
ip a del $staticIP/24 dev $interface
echo "stopped" > /etc/wipi.status
else
#Start the access point
if [ "$mode" = "start" ]; then
echo "Started access point $name"
fi
fi
}
stopAP() {
#Check to see if the interface exists
checkInterface
#Check to make sure the dependencies are installed
checkDepends
#Check to see if the status file exists
if [ -e /etc/wipi.status ]; then
#Check if the access point is running via status file
if [ "`cat /etc/wipi.status`" = "stopped" ] && [ "$mode" = "stop" ]; then
echo "WiPi is not running. If this is a mistake, please remove /etc/wipi.status"
exit 1
#Error if in the wrong format
elif [ "`cat /etc/wipi.status`" != "running" ] && [ "`cat /etc/wipi.status`" != "stopped" ]; then
echo "Error reading /etc/wipi.status. Please remove it"
exit 1
else
#Proceed
echo "stopped" > /etc/wipi.status
fi
else
#Proceed
echo "stopped" > /etc/wipi.status
fi
verbose "Removing static ip"
#Remove static IP
for ip in "`ip a show $interface | grep inet | grep [.] | sed 's/\ \ */\ /g' | cut -d ' ' -f 3`"; do
ip a del $ip dev $interface
done
local output=`killServices hostapd dnsmasq`
#Kill services
if [ "$output" = "1" ] && [ "$mode" = "stop" ]; then
echo "Couldn't stop access point (it wasn't running)"
exit 1
fi
#Restore iptables rules
verbose "Restoring old iptables rules"
iptables-restore < /etc/iptables.ipv4.nat
#Print access point stopped if the mode was told to stop
#This is here so if you restart it doesn't print the access point is stopped
if [ "$mode" = "stop" ]; then
echo "Access point stopped"
fi
}
#Make sure user is root
if [ "$EUID" != "0" ]; then
echo "You must be root in order to run this program"
exit 1
fi
#Set default parameters
if [ -z "$1" ]; then
mode=""
else
mode="$1"
fi
verbosity=false
interface=""
ipInterface="eth0"
staticIP="192.168.4.1"
staticIPRange="192.168.4"
name="WiPi AP"
channel=1
domain="mydomain.net"
password=""
#Read the options and set them
OPTIONS=`getopt -o hvc:e:d:i:n:p: --long help,verbose,channel:,essid:,domain:,interface:,nat:,password: -n $(basename $0) -- "$@"`
eval set -- "$OPTIONS"
#Extract options and their arguments into variables.
while true ; do
case "$1" in
-h|--help) showHelp; exit 0 ;; #Show help
-v|--verbose) verbosity=true; shift ;; #Set verbosity flag to true
-c|--channel) #Set the channel
shift
if [ $1 -gt 11 ] || [ $1 -lt 1 ]; then
echo "The channel specified must be between 1 and 11"
exit 1
else
channel=$1
fi
shift
;;
-e|--essid) #Set the name
shift
#Checks character length
if [ `echo $1 | wc -c` -gt 32 ]; then
echo "Name must be shorter than 33 characters"
exit 1
else
name=$1
fi
shift
;;
-d|--domain) shift; domain=$1; shift ;; #Set the domain
-i|--interface) shift; ipInterface=$1; shift ;; #Set the domain
-n|--nat) #Set the domain
shift
#Replace dots with spaces
parts=`echo $1 | cut -d ' ' -f 1 | sed -e 's/\./\ /g'`
#Make sure there are 4 parts to the ip
if [ `echo $parts | wc -w` -eq 4 ]; then
count=0
#For each part in the array
for part in ${parts[@]}; do
count=`expr $count + 1`
#Make sure the number isn't too high
if [ $part -gt 254 ]; then
echo "IP is in an invalid format. Please use standard IPV4 notation"
exit 1
#Make sure the number isn't too low
elif [ $part -lt 0 ] && [ $count -lt 4 ]; then
echo "IP is in an invalid format. Please use standard IPV4 notation"
exit 1
#Make sure the number isn't too low
elif [ "$part" != "1" ] && [ $count -eq 4 ]; then
echo "Last digit in the static IP must be 1"
exit 1
fi
done
#If IP is not the right size
else
echo "IP is in an invalid format. Please use standard IPV4 notation"
exit 1
fi
#Set IP to the right size
staticIP=`echo $1 | cut -d ' ' -f 1`
staticIPRange=${staticIP::-2}
shift
;;
-p|--password) #Sets the password to the access point
shift
#Checks character length
if [ `echo $1 | wc -c` -lt 8 ]; then
echo "Password must be longer than 7 characters"
exit 1
elif [ `echo $1 | wc -c` -gt 32 ]; then
echo "Password must be shorter than 33 characters"
exit 1
else
password=$1
fi
shift
;;
--) shift ; break ;; #Break to parse non opts
*) echo "Internal error!"; exit 1 ;; #SHTF! EXIT NOW!
esac
done
interface="$2"
#Check to see if the access point should be restarted
if [ "$mode" = "restart" ];then
echo "Attempting to restart the access point"
stopAP
startAP
echo "Restarted the access point"
echo "New name is $name"
#Check to see if the access point should be started
elif [ "$mode" = "start" ]; then
echo "Attempting to start the access point"
startAP
#Check to see if the access point should be stopped
elif [ "$mode" = "stop" ]; then
echo "Attempting to stop the access point"
stopAP
#Message for help if the command is used wrong
else
showHelp
fi