-
Notifications
You must be signed in to change notification settings - Fork 19
/
ynh-hotspot
431 lines (349 loc) · 12.6 KB
/
ynh-hotspot
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
#!/bin/bash
#
# Wifi Hotspot app for YunoHost
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
# Contribute at https://github.com/labriqueinternet/hotspot_ynh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
source /usr/share/yunohost/helpers
# Functions
## State functions
has_ip6delegatedprefix() {
[[ -n "${ip6_net}" ]] && [[ "${ip6_net}" != "none" ]]
}
ip6addrfromdelegatedprefix() {
echo "${ip6_net}1"
}
is_nat_set() {
local gateway_interface=${1}
iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
}
is_ip4nataddr_set() {
ip address show dev "${wifi_device}" 2>/dev/null | grep -q "${ip4_nat_prefix}.1/24"
}
is_ip6addr_set() {
ip address show dev "${wifi_device}" 2>/dev/null | grep -q "$(ip6addrfromdelegatedprefix)/64"
}
is_ip6firewall_set() {
ip6tables -w -nvL FORWARD | grep DROP | grep -q "${wifi_device}"
}
is_forwarding_set() {
local ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
local ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
[[ "${ip6}" -eq 1 ]] && [[ "${ip4}" -eq 1 ]]
}
is_dhcpd6_running() {
[[ -e "/run/dnsmasq/dnsmasq-dhcpdv6-$app.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv6-$app.pid") > /dev/null
}
is_dhcpd4_running() {
[[ -e "/run/dnsmasq/dnsmasq-dhcpdv4-$app.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv4-$app.pid") > /dev/null
}
is_hostapd_running() {
systemctl is-active "hostapd@${app}" &>/dev/null
}
is_other_hostapd_running() {
other_hostapd_services=$(systemctl list-units --state=running hostapd@*.service | grep -v "^hostapd@$app.service")
[[ -n "${other_hostapd_service}" ]]
}
is_running() {
if has_ip6delegatedprefix; then
if ! is_ip6addr_set; then
return 1
fi
if [[ "${ip6_firewall}" -eq 1 ]] && ! is_ip6firewall_set; then
return 1
fi
if ! is_dhcpd6_running; then
return 1
fi
fi
if ! is_ip4nataddr_set; then
return 1
fi
if ! is_dhcpd4_running; then
return 1
fi
if ! is_hostapd_running; then
return 1
fi
if ! is_forwarding_set; then
return 1
fi
if [[ -n ${new_gateway_interface} ]] && ! is_nat_set "${new_gateway_interface}"; then
return 1
fi
return 0
}
## Setters
set_nat() {
local gateway_interface=${1}
iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
}
set_ipaddr() {
if ! is_ip4nataddr_set; then
echo "hotspot ${wifi_device}: Set IPv4 NAT address"
ip address add "${ip4_nat_prefix}.1/24" dev "${wifi_device}"
fi
if has_ip6delegatedprefix && ! is_ip6addr_set; then
echo "hotspot ${wifi_device}: Set IPv6 address"
ip address delete "$(ip6addrfromdelegatedprefix)/64" dev tun0 &>/dev/null
ip address add "$(ip6addrfromdelegatedprefix)/64" dev "${wifi_device}"
fi
}
set_ipfirewall() {
# Set ipv6 firewalling
if has_ip6delegatedprefix && [[ "${ip6_firewall}" -eq 1 ]] && ! is_ip6firewall_set; then
echo "hotspot ${wifi_device}: Set IPv6 firewalling"
ip6tables -w -A FORWARD -i "${wifi_device}" -j ACCEPT
ip6tables -w -A FORWARD -o "${wifi_device}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -A FORWARD -o "${wifi_device}" -j DROP
fi
}
set_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
sysctl -w net.ipv4.conf.all.forwarding=1 >/dev/null
}
start_dhcpd() {
# Run DHCPv4 server
if ! is_dhcpd4_running; then
echo "hotspot ${wifi_device}: Start the DHCPv4 server (dnsmasq)"
dnsmasq -C /etc/dnsmasq.$app/dhcpdv4.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpdv4-$app.pid
fi
# Run DHCPv6 server
if has_ip6delegatedprefix && ! is_dhcpd6_running; then
echo "hotspot ${wifi_device}: Start the NDP and DHCPv6 server (dnsmasq)"
dnsmasq -C /etc/dnsmasq.$app/dhcpdv6.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpdv6-$app.pid
fi
}
configure_hostapd() {
local ethaddr=$(ip link show dev "${wifi_device}" | grep link/ether | awk -F: '{ printf "02:%s:%s:%s:%s:00", $2, $3, $4, $5 }')
ip link set addr "${ethaddr}" dev "${wifi_device}"
}
## Unsetters
unset_nat() {
local gateway_interface=${1}
iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
}
unset_ipaddr() {
if is_ip4nataddr_set; then
echo "hotspot ${wifi_device}: Unset IPv4 NAT address"
ip address delete "${ip4_nat_prefix}.1/24" dev "${wifi_device}"
fi
if has_ip6delegatedprefix && is_ip6addr_set; then
echo "hotspot ${wifi_device}: Unset IPv6 address"
ip address delete "$(ip6addrfromdelegatedprefix)/64" dev "${wifi_device}"
fi
}
unset_ipfirewall() {
if has_ip6delegatedprefix && [[ "${ip6_firewall}" -eq 1 ]] && is_ip6firewall_set; then
echo "hotspot ${wifi_device}: Unset IPv6 firewalling"
ip6tables -w -D FORWARD -i "${wifi_device}" -j ACCEPT
ip6tables -w -D FORWARD -o "${wifi_device}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -D FORWARD -o "${wifi_device}" -j DROP
fi
}
unset_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=0 >/dev/null
sysctl -w net.ipv4.conf.all.forwarding=0 >/dev/null
}
stop_dhcpd() {
if is_dhcpd6_running; then
echo "hotspot ${wifi_device}: Stop the NDP and DHCPv6 server (dnsmasq)"
kill $(cat /run/dnsmasq/dnsmasq-dhcpdv6-$app.pid)
rm -f /run/dnsmasq/dnsmasq-dhcpdv6-$app.pid
fi
if is_dhcpd4_running; then
echo "hotspot ${wifi_device}: Stop the DHCPv4 server (dnsmasq)"
kill $(cat /run/dnsmasq/dnsmasq-dhcpdv4-$app.pid)
rm -f /run/dnsmasq/dnsmasq-dhcpdv4-$app.pid
fi
}
stop_hostapd() {
systemctl stop "hostapd@${app}"
}
if [ "$1" != restart ]; then
# Variables
echo -n "Retrieving Yunohost settings... "
app=__APP__
service_enabled=$(ynh_app_setting_get --app=$app --key=service_enabled)
wifi_device=$(ynh_app_setting_get --app=$app --key=wifi_device)
wifi_channel=$(ynh_app_setting_get --app=$app --key=wifi_channel)
wifi_ssid=$(ynh_app_setting_get --app=$app --key=wifi_ssid)
wifi_secure=$(ynh_app_setting_get --app=$app --key=wifi_secure)
wifi_passphrase=$(ynh_app_setting_get --app=$app --key=wifi_passphrase)
ip6_firewall=$(ynh_app_setting_get --app=$app --key=ip6_firewall)
ip6_dns=$(ynh_app_setting_get --app=$app --key=ip6_dns)
ip6_net=$(ynh_app_setting_get --app=$app --key=ip6_net)
ip4_dns=$(ynh_app_setting_get --app=$app --key=ip4_dns)
ip4_nat_prefix=$(ynh_app_setting_get --app=$app --key=ip4_nat_prefix)
old_gateway_interface=$(ynh_app_setting_get --app=$app --key=gateway_interface)
# The awk syntax is to accomodate to the fact that the ip route output may look like:
# 1.2.3.4 via 192.168.1.254 dev end0 src 192.168.1.35 uid 0
# 1.2.3.4 dev vpn_iloth table 51820 src 5.6.7.8 uid 0
new_gateway_interface=$(ip route get 1.2.3.4 | awk '$2 ~ /^dev$/ { print $3; } $4 ~ /^dev$/ { print $5; }')
echo "OK"
fi
# Script
case "$1" in
start)
if is_running; then
echo "Already started"
exit 0
elif [[ "${service_enabled}" -eq 0 ]]; then
echo "Not starting because hotspot service is disabled"
exit 1
fi
if [[ -z "${wifi_device}" ]]; then
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exit 1
fi
echo "[$app] Starting..."
touch /tmp/.${service_name}-started
# Check old state of the ipv4 NAT settings
if [[ -n "${old_gateway_interface}" ]] && [[ "${new_gateway_interface}" != "${old_gateway_interface}" ]] && is_nat_set "${old_gateway_interface}"; then
unset_nat "${old_gateway_interface}"
fi
# Set ipv4 NAT
if [[ -n "${new_gateway_interface}" ]] && ! is_nat_set "${new_gateway_interface}"; then
echo "Set NAT"
set_nat "${new_gateway_interface}"
fi
# Set forwarding for ipv6 and ipv4
echo "Set forwarding"
set_forwarding
# Run hostapd
if ! is_hostapd_running; then
echo "Configuring hostapd"
configure_hostapd
echo "Starting hostapd..."
if ! systemctl start "hostapd@${app}"; then
journalctl -u hostapd -n 100 --no-hostname --no-pager
exit 1
fi
sleep 1
fi
set_ipaddr
set_ipfirewall
start_dhcpd
# Update dynamic settings
ynh_app_setting_set --app=$app --key=gateway_interface --value="${new_gateway_interface}"
# Regen-conf dnsmasq to enable dns resolution on dnsmasq for the new interface
yunohost tools regen-conf dnsmasq
;;
stop)
echo "[$app] Stopping..."
rm -f /tmp/.${service_name}-started
if ! is_other_hostapd_running; then
if [[ -n "${old_gateway_interface}" ]] && is_nat_set "${old_gateway_interface}"; then
echo "Unset NAT"
unset_nat "${old_gateway_interface}"
fi
echo "Unset forwarding"
unset_forwarding
fi
unset_ipaddr
unset_ipfirewall
stop_dhcpd
if is_hostapd_running; then
echo "Stop hostapd"
stop_hostapd
fi
# Regen-conf dnsmasq to disable dns resolution on dnsmasq for the previous interface
yunohost tools regen-conf dnsmasq
;;
restart)
$0 stop
$0 start
;;
status)
exitcode=0
if [[ "${service_enabled}" -eq 0 ]]; then
echo "[FAIL] Hotspot Service disabled"
exit 1
fi
if [[ -z "${wifi_device}" ]]; then
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exit 1
fi
echo "[INFO] Autodetected internet interface: ${new_gateway_interface} (last start: ${old_gateway_interface})"
if is_nat_set "${new_gateway_interface}"; then
echo "[ OK ] IPv4 NAT set"
else
if [[ -z "${new_gateway_interface}" ]]; then
echo "[INFO] No IPv4 NAT set (no internet interface)"
else
echo "[FAIL] No IPv4 NAT set"
fi
exitcode=1
fi
if is_forwarding_set; then
echo "[ OK ] IPv6/IPv4 forwarding set"
else
echo "[FAIL] No IPv6/IPv4 forwarding set"
exitcode=1
fi
if is_hostapd_running; then
echo "[ OK ] Hostapd is running"
else
echo "[FAIL] Hostapd is not running"
exitcode=1
fi
if has_ip6delegatedprefix; then
echo "[INFO] hotspot ${wifi_device}: IPv6 delegated prefix found"
echo "[INFO] hotspot ${wifi_device}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix)"
if is_ip6addr_set; then
echo "[ OK ] hotspot ${wifi_device}: IPv6 address set"
else
echo "[FAIL] hotspot ${wifi_device}: No IPv6 address set"
exitcode=1
fi
if is_ip6firewall_set; then
echo "[ OK ] hotspot ${wifi_device}: IPv6 firewalling set"
else
if [[ "${ip6_firewall}" -eq 1 ]]; then
echo "[FAIL] hotspot ${wifi_device}: No IPv6 firewalling set"
else
echo "[INFO] hotspot ${wifi_device}: No IPv6 firewalling set"
fi
exitcode=1
fi
if is_dhcpd6_running; then
echo "[ OK ] hotspot ${wifi_device}: NDP and DHCPv6 server (dnsmasq) are running"
else
echo "[FAIL] hotspot ${wifi_device}: NDP and DHCPv6 server (dnsmasq) are not running"
exitcode=1
fi
else
echo "[INFO] hotspot ${wifi_device}: No IPv6 delegated prefix found"
fi
if is_dhcpd4_running; then
echo "[ OK ] hotspot ${wifi_device}: DHCPv4 server (dnsmasq) is running"
else
echo "[FAIL] hotspot ${zifi_device}: DHCPv4 (dnsmasq) is not running"
exitcode=1
fi
if is_ip4nataddr_set; then
echo "[ OK ] hotspot ${wifi_device}: IPv4 NAT address set"
else
echo "[FAIL] hotspot ${wifi_device}: No IPv4 NAT address set"
exitcode=1
fi
exit ${exitcode}
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0