2014-11-09 00:40:40 +01:00
|
|
|
#!/bin/bash
|
2015-05-12 09:30:55 +02:00
|
|
|
#
|
|
|
|
# Wifi Hotspot app for YunoHost
|
2014-12-26 20:18:03 +01:00
|
|
|
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
|
2015-07-22 23:54:56 +02:00
|
|
|
# Contribute at https://github.com/labriqueinternet/hotspot_ynh
|
2015-05-12 09:30:55 +02:00
|
|
|
#
|
2014-12-26 20:18:03 +01:00
|
|
|
# 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.
|
2015-05-12 09:30:55 +02:00
|
|
|
#
|
2014-12-26 20:18:03 +01:00
|
|
|
# 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.
|
2015-05-12 09:30:55 +02:00
|
|
|
#
|
2014-12-26 20:18:03 +01:00
|
|
|
# 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/>.
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
# Functions
|
|
|
|
## State functions
|
|
|
|
|
2014-11-16 23:27:36 +01:00
|
|
|
has_vpnclient_app() {
|
2021-11-16 00:20:13 +01:00
|
|
|
[ -e /tmp/.ynh-vpnclient-started ]
|
2014-11-16 23:27:36 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:27:59 +01:00
|
|
|
has_ip6delegatedprefix() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
[[ -n "${ip6_net[${i}]}" ]] && [[ "${ip6_net[${i}]}" != "none" ]]
|
2014-11-10 22:27:59 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 00:04:34 +02:00
|
|
|
ip6addrfromdelegatedprefix() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2021-10-18 00:04:34 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "${ip6_net[${i}]}${i}001"
|
2021-10-18 00:04:34 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
is_nat_set() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local gateway_interface=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is_ip4nataddr_set() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2021-11-16 00:46:09 +01:00
|
|
|
local dev=$(devfromid "${i}")
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
ip address show dev "${dev}" 2>/dev/null | grep -q "${ip4_nat_prefix[${i}]}.1/24"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is_ip6addr_set() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2021-11-16 00:46:09 +01:00
|
|
|
local dev=$(devfromid "${i}")
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
ip address show dev "${dev}" 2>/dev/null | grep -q "$(ip6addrfromdelegatedprefix $i)/64"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 23:48:24 +02:00
|
|
|
is_ip6firewall_set() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2021-11-16 00:46:09 +01:00
|
|
|
local dev=$(devfromid "${i}")
|
2015-07-24 23:48:24 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
ip6tables -w -nvL FORWARD | grep DROP | grep -q "${dev}"
|
2015-07-24 23:48:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
is_forwarding_set() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
|
|
|
|
local ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:29:00 +01:00
|
|
|
[ "${ip6}" -eq 1 ] && [ "${ip4}" -eq 1 ]
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
is_dhcpd6_running() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2021-11-16 00:29:00 +01:00
|
|
|
ps aux | grep "dhcpdv6-ssid${i}" | grep -qv grep
|
2015-05-02 17:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
is_dhcpd4_running() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2015-05-02 17:41:54 +02:00
|
|
|
|
2021-11-16 00:29:00 +01:00
|
|
|
ps aux | grep "dhcpdv4-ssid${i}" | grep -qv grep
|
2014-12-26 18:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
is_hostapd_running() {
|
2021-11-16 00:20:13 +01:00
|
|
|
systemctl is-active hostapd &>/dev/null
|
2015-04-26 21:34:11 +02:00
|
|
|
}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
is_running() {
|
2021-11-16 00:20:13 +01:00
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
|
|
|
(has_ip6delegatedprefix ${i} && is_ip6addr_set ${i} \
|
|
|
|
&& ([ "${ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i} || [ "${ip6_firewall[${i}]}" -eq 0 ]) \
|
|
|
|
&& is_dhcpd6_running ${i} || ! has_ip6delegatedprefix ${i}) \
|
|
|
|
&& is_ip4nataddr_set ${i} && is_dhcpd4_running ${i}
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
if [ ! $? -eq 0 ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
is_hostapd_running && is_forwarding_set && ([ -z "${new_gateway_interface}" ] || is_nat_set "${new_gateway_interface}")
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Setters
|
|
|
|
|
|
|
|
set_nat() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local gateway_interface=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
set_ipaddr() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
|
|
|
local dev=$(devfromid "${i}")
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
if ! is_ip4nataddr_set ${i}; then
|
|
|
|
echo "hotspot${i}: Set IPv4 NAT address"
|
|
|
|
ip address add "${ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
|
|
|
|
fi
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
|
|
|
|
echo "hotspot${i}: Set IPv6 address"
|
|
|
|
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev tun0 &>/dev/null
|
|
|
|
ip address add "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}"
|
|
|
|
fi
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
set_ipfirewall() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
|
|
|
local dev=$(devfromid "${i}")
|
2015-07-24 23:48:24 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
# Set ipv6 firewalling
|
|
|
|
if has_ip6delegatedprefix ${i} && [ "${ip6_firewall[${i}]}" -eq 1 ] && ! is_ip6firewall_set ${i}; then
|
|
|
|
echo "hotspot${i}: Set IPv6 firewalling"
|
|
|
|
ip6tables -w -A FORWARD -i "${dev}" -j ACCEPT
|
|
|
|
ip6tables -w -A FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
ip6tables -w -A FORWARD -o "${dev}" -j DROP
|
|
|
|
fi
|
|
|
|
|
2015-07-24 23:48:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
set_forwarding() {
|
2021-11-16 00:20:13 +01:00
|
|
|
sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
|
|
|
|
sysctl -w net.ipv4.conf.all.forwarding=1 >/dev/null
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
start_dhcpd() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
|
|
|
local dev=$(devfromid "${i}")
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
# Run DHCPv4 server
|
|
|
|
if ! is_dhcpd4_running ${i}; then
|
|
|
|
echo "hotspot${i}: Start the DHCPv4 server (dnsmasq)"
|
2015-05-02 17:41:54 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
sed "s|__WIFI_DEVICE__|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
|
|
|
sed "s|__IP4_DNS__|${ip4_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
|
|
|
sed "s|__IP4_NAT_PREFIX__|${ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
2015-05-02 17:41:54 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0
|
|
|
|
fi
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
# Run DHCPv6 server
|
|
|
|
if has_ip6delegatedprefix ${i} && ! is_dhcpd6_running ${i}; then
|
|
|
|
echo "hotspot${i}: Start the NDP and DHCPv6 server (dnsmasq)"
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
cp /etc/dnsmasq.dhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf}
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
sed "s|__WIFI_DEVICE__|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
sed "s|__IP6_DNS__|${ip6_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
sed "s|__IP6_NET__|${ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
|
|
|
|
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0
|
|
|
|
fi
|
2014-12-26 18:58:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:45:27 +01:00
|
|
|
configure_hostapd() {
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
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}"
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
cp /etc/hostapd/hostapd.base.conf /etc/hostapd/hostapd.conf
|
|
|
|
sed "s|__WIFI_DEVICE__|${wifi_device}|g" -i /etc/hostapd/hostapd.conf
|
|
|
|
sed "s|__WIFI_CHANNEL__|${wifi_channel}|g" -i /etc/hostapd/hostapd.conf
|
|
|
|
sed "s|__N_COMMENT__||g" -i /etc/hostapd/hostapd.conf
|
2014-11-09 23:49:06 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
2021-11-15 23:45:27 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
[ "${wifi_secure[${i}]}" -eq 1 ] && local sec_comment="" || local sec_comment="#"
|
|
|
|
[ "${i}" -eq 0 ] && local bss_comment="#" || local bss_comment=""
|
2021-11-15 23:45:27 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
cp /etc/hostapd/hostapd.accesspoint.conf /etc/hostapd/hostapd.conf.tmp
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
sed "s|__WIFI_INTERFACE__|hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
sed "s|__WIFI_SSID__|${wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
sed "s|__WIFI_PASSPHRASE__|${wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
sed "s|__SEC_COMMENT__|${sec_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
sed "s|__BSS_COMMENT__|${bss_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
|
2015-05-02 12:32:19 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
cat /etc/hostapd/hostapd.conf.tmp >>/etc/hostapd/hostapd.conf
|
|
|
|
rm /etc/hostapd/hostapd.conf.tmp
|
|
|
|
done
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Unsetters
|
|
|
|
|
|
|
|
unset_nat() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local gateway_interface=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
unset_ipaddr() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
|
|
|
local dev=$(devfromid "${i}")
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
if is_ip4nataddr_set ${i}; then
|
|
|
|
echo "hotspot${i}: Unset IPv4 NAT address"
|
|
|
|
ip address delete "${ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
|
|
|
|
fi
|
2015-05-02 14:54:08 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
|
|
|
|
echo "hotspot${i}: Unset IPv6 address"
|
|
|
|
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}"
|
|
|
|
fi
|
2014-12-26 18:58:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
unset_ipfirewall() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
|
|
|
local dev=$(devfromid "${i}")
|
2015-07-24 23:48:24 +02:00
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
if has_ip6delegatedprefix ${i} && [ "${ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i}; then
|
|
|
|
echo "hotspot${i}: Unset IPv6 firewalling"
|
|
|
|
ip6tables -w -D FORWARD -i "${dev}" -j ACCEPT
|
|
|
|
ip6tables -w -D FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
ip6tables -w -D FORWARD -o "${dev}" -j DROP
|
|
|
|
fi
|
2015-07-24 23:48:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
unset_forwarding() {
|
2021-11-16 00:20:13 +01:00
|
|
|
sysctl -w net.ipv6.conf.all.forwarding=0 >/dev/null
|
|
|
|
sysctl -w net.ipv4.conf.all.forwarding=0 >/dev/null
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
stop_dhcpd() {
|
|
|
|
local i=${1}
|
|
|
|
|
|
|
|
if is_dhcpd6_running ${i}; then
|
|
|
|
echo "hotspot${i}: Stop the NDP and DHCPv6 server (dnsmasq)"
|
|
|
|
kill $(ps aux | grep 'dhcpdv6-ssid' | grep -v grep | awk '{ print $2 }')
|
|
|
|
rm -f /etc/dnsmasq.d/dhcpdv6-ssid*.conf
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_dhcpd4_running ${i}; then
|
|
|
|
echo "hotspot${i}: Stop the DHCPv4 server (dnsmasq)"
|
|
|
|
kill $(ps aux | grep 'dhcpdv4-ssid' | grep -v grep | awk '{ print $2 }')
|
|
|
|
rm -f /etc/dnsmasq.d/dhcpdv4-ssid*.conf
|
|
|
|
fi
|
2015-05-02 17:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_dhcpd4() {
|
2015-04-26 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
stop_hostapd() {
|
2021-11-16 00:20:13 +01:00
|
|
|
systemctl stop hostapd
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Tools
|
|
|
|
|
2015-07-24 20:56:19 +02:00
|
|
|
ynh_setting_get() {
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
APP="$1" KEY="$2" python3 - <<EOF
|
2021-11-15 22:07:24 +01:00
|
|
|
import os, yaml, sys
|
|
|
|
app = os.environ['APP']
|
|
|
|
key = os.environ['KEY']
|
|
|
|
setting_file = "/etc/yunohost/apps/%s/settings.yml" % app
|
|
|
|
assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file
|
|
|
|
with open(setting_file) as f:
|
|
|
|
settings = yaml.safe_load(f)
|
|
|
|
if key in settings:
|
|
|
|
print(settings[key])
|
|
|
|
EOF
|
|
|
|
|
2015-05-12 09:30:55 +02:00
|
|
|
}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2015-07-24 20:56:19 +02:00
|
|
|
ynh_setting_set() {
|
2014-11-17 23:44:18 +01:00
|
|
|
|
2020-07-07 17:06:58 +02:00
|
|
|
# This is a partial copypasta of the official ynh_app_setting internal helper
|
|
|
|
# In particular, we do this instead of relying on 'yunohost app setting' for
|
|
|
|
# performance reasons (it takes a few second to run every yunohost commands)
|
|
|
|
# and to remove the need for the infamous '--need-lock' option/issue.
|
|
|
|
|
2021-11-15 22:07:24 +01:00
|
|
|
APP="$1" KEY="$2" VALUE="${3:-}" python3 - <<EOF
|
2020-07-07 17:06:58 +02:00
|
|
|
import os, yaml, sys
|
2021-11-15 22:07:24 +01:00
|
|
|
app = os.environ['APP']
|
|
|
|
key, value = os.environ['KEY'], os.environ.get('VALUE', None)
|
2020-07-07 17:06:58 +02:00
|
|
|
setting_file = "/etc/yunohost/apps/%s/settings.yml" % app
|
|
|
|
assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file
|
|
|
|
with open(setting_file) as f:
|
|
|
|
settings = yaml.load(f)
|
|
|
|
settings[key] = value
|
|
|
|
with open(setting_file, "w") as f:
|
|
|
|
yaml.safe_dump(settings, f, default_flow_style=False)
|
|
|
|
EOF
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 23:48:24 +02:00
|
|
|
devfromid() {
|
2021-11-16 00:20:13 +01:00
|
|
|
local i=${1}
|
2015-07-24 23:48:24 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
echo "${wifi_device}"
|
|
|
|
else
|
|
|
|
echo "hotspot${i}"
|
|
|
|
fi
|
2015-07-24 23:48:24 +02:00
|
|
|
}
|
|
|
|
|
2014-12-28 23:27:39 +01:00
|
|
|
if [ "$1" != restart ]; then
|
2014-11-25 21:05:24 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
# Variables
|
|
|
|
|
|
|
|
echo -n "Retrieving Yunohost settings... "
|
|
|
|
|
|
|
|
service_enabled=$(systemctl is-enabled ynh-hotspot)
|
|
|
|
wifi_device=$(ynh_setting_get hotspot wifi_device)
|
|
|
|
wifi_channel=$(ynh_setting_get hotspot wifi_channel)
|
|
|
|
multissid=$(ynh_setting_get hotspot multissid)
|
|
|
|
|
|
|
|
IFS='|' read -a wifi_ssid <<<"$(ynh_setting_get hotspot wifi_ssid)"
|
|
|
|
IFS='|' read -a wifi_secure <<<"$(ynh_setting_get hotspot wifi_secure)"
|
|
|
|
IFS='|' read -a wifi_passphrase <<<"$(ynh_setting_get hotspot wifi_passphrase)"
|
|
|
|
IFS='|' read -a ip6_firewall <<<"$(ynh_setting_get hotspot ip6_firewall)"
|
|
|
|
IFS='|' read -a ip6_net <<<"$(ynh_setting_get hotspot ip6_net)"
|
|
|
|
IFS='|' read -a dns <<<"$(ynh_setting_get hotspot dns)"
|
|
|
|
IFS='|' read -a ip4_nat_prefix <<<"$(ynh_setting_get hotspot ip4_nat_prefix)"
|
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
|
|
|
ip6_dns[${i}]=""
|
|
|
|
ip4_dns[${i}]=""
|
|
|
|
for ip in $(echo "${dns[${i}]}" | tr ',' ' '); do
|
|
|
|
if [[ "$ip" == *":"* ]]; then
|
|
|
|
ip6_dns[${i}]+="[$ip],"
|
|
|
|
else
|
|
|
|
ip4_dns[${i}]+="$ip,"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# Remove trailing ,
|
|
|
|
ip6_dns[${i}]="${ip6_dns[${i}]%%,}"
|
|
|
|
ip4_dns[${i}]="${ip4_dns[${i}]%%,}"
|
2021-09-20 18:01:28 +02:00
|
|
|
done
|
2015-07-24 20:56:19 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
old_gateway_interface=$(ynh_setting_get hotspot gateway_interface)
|
|
|
|
new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
|
2015-05-12 09:30:55 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
# Switch the NAT interface if there is a VPN
|
|
|
|
ip link show dev tun0 &>/dev/null
|
|
|
|
if [ "$?" -eq 0 ]; then
|
|
|
|
new_gateway_interface=tun0
|
|
|
|
fi
|
2015-05-12 09:30:55 +02:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "OK"
|
2014-11-17 23:44:18 +01:00
|
|
|
fi
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
# Script
|
|
|
|
|
|
|
|
case "$1" in
|
2021-11-16 00:20:13 +01:00
|
|
|
start)
|
2014-11-09 00:40:40 +01:00
|
|
|
if is_running; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Already started"
|
|
|
|
exit 0
|
2021-11-16 00:16:13 +01:00
|
|
|
elif [ "${service_enabled}" != "enabled" ]; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Not starting because hotspod service is disabled"
|
|
|
|
exit 1
|
2021-11-16 00:16:13 +01:00
|
|
|
fi
|
2020-09-24 00:07:43 +02:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
if [ -z "${wifi_device}" ]; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
|
|
|
|
exit 1
|
2021-11-16 00:16:13 +01:00
|
|
|
fi
|
2020-09-24 00:07:43 +02:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
echo "[hotspot] Starting..."
|
|
|
|
touch /tmp/.ynh-hotspot-started
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
# Check old state of the ipv4 NAT settings
|
2021-11-16 00:22:23 +01:00
|
|
|
if [ -n "${old_gateway_interface}" ] && [ "${new_gateway_interface}" != "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
unset_nat "${old_gateway_interface}"
|
2021-11-16 00:16:13 +01:00
|
|
|
fi
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
# Set ipv4 NAT
|
2021-11-16 00:22:23 +01:00
|
|
|
if [ -n "${new_gateway_interface}" ] && ! is_nat_set "${new_gateway_interface}"; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Set NAT"
|
|
|
|
set_nat "${new_gateway_interface}"
|
2021-11-16 00:16:13 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Set forwarding for ipv6 and ipv4
|
2021-11-16 00:46:09 +01:00
|
|
|
echo "Set forwarding"
|
|
|
|
set_forwarding
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
# Run hostapd
|
|
|
|
if ! is_hostapd_running; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Configuring hostapd"
|
|
|
|
configure_hostapd
|
2021-11-16 00:16:13 +01:00
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Starting hostapd..."
|
|
|
|
if ! systemctl start hostapd; then
|
|
|
|
journalctl -u hostapd -n 100 --no-hostname --no-pager
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
sleep 1
|
2021-11-16 00:20:13 +01:00
|
|
|
|
|
|
|
i=0
|
|
|
|
while ! ip link show dev hotspot1 &>/dev/null; do
|
|
|
|
sleep 1
|
|
|
|
if [ ${i} -gt 20 ]; then
|
|
|
|
echo "Failed to see hotspot interface showing up in 'ip a'"
|
|
|
|
stop_hostapd
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
i=$(($i + 1))
|
|
|
|
done
|
2021-11-16 00:16:13 +01:00
|
|
|
fi
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
# For each registred ssid
|
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
2021-11-16 00:46:09 +01:00
|
|
|
set_ipaddr ${i}
|
|
|
|
set_ipfirewall ${i}
|
|
|
|
start_dhcpd ${i}
|
2021-11-16 00:16:13 +01:00
|
|
|
done
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
# Update dynamic settings
|
|
|
|
ynh_setting_set hotspot gateway_interface "${new_gateway_interface}"
|
2021-11-16 00:20:13 +01:00
|
|
|
;;
|
|
|
|
stop)
|
2014-11-16 23:27:36 +01:00
|
|
|
echo "[hotspot] Stopping..."
|
2014-12-26 18:58:58 +01:00
|
|
|
rm -f /tmp/.ynh-hotspot-started
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2021-11-16 00:22:23 +01:00
|
|
|
if [ -n "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Unset NAT"
|
|
|
|
unset_nat "${old_gateway_interface}"
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:46:09 +01:00
|
|
|
echo "Unset forwarding"
|
|
|
|
unset_forwarding
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
2021-11-16 00:46:09 +01:00
|
|
|
unset_ipaddr ${i}
|
|
|
|
unset_ipfirewall ${i}
|
|
|
|
stop_dhcpd ${i}
|
2015-04-26 21:34:11 +02:00
|
|
|
done
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
if is_hostapd_running; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "Stop hostapd"
|
|
|
|
stop_hostapd
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-05-25 02:23:28 +02:00
|
|
|
# Fix configuration
|
2014-11-16 23:27:36 +01:00
|
|
|
if has_vpnclient_app; then
|
2021-11-16 00:20:13 +01:00
|
|
|
ynh-vpnclient start
|
2014-11-16 23:27:36 +01:00
|
|
|
fi
|
2021-11-16 00:20:13 +01:00
|
|
|
;;
|
|
|
|
restart)
|
2014-12-28 15:38:05 +01:00
|
|
|
$0 stop
|
|
|
|
$0 start
|
2021-11-16 00:20:13 +01:00
|
|
|
;;
|
|
|
|
status)
|
2014-11-09 00:40:40 +01:00
|
|
|
exitcode=0
|
2014-11-20 20:03:24 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
if [ "${service_enabled}" != "enabled" ]; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] Hotspot Service disabled"
|
|
|
|
exit 1
|
2015-03-14 15:44:48 +01:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
if [ -z "${wifi_device}" ]; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
|
|
|
|
exit 1
|
2020-09-24 00:07:43 +02:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
echo "[INFO] Autodetected internet interface: ${new_gateway_interface} (last start: ${old_gateway_interface})"
|
2014-11-20 20:03:24 +01:00
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
if is_nat_set "${new_gateway_interface}"; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[ OK ] IPv4 NAT set"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
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
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if is_forwarding_set; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[ OK ] IPv6/IPv4 forwarding set"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] No IPv6/IPv4 forwarding set"
|
|
|
|
exitcode=1
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
|
|
|
|
2014-12-26 18:58:58 +01:00
|
|
|
if is_hostapd_running; then
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[ OK ] Hostapd is running"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] Hostapd is not running"
|
|
|
|
exitcode=1
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:16:13 +01:00
|
|
|
for i in $(seq 0 $((${multissid} - 1))); do
|
2021-11-16 00:20:13 +01:00
|
|
|
if has_ip6delegatedprefix ${i}; then
|
|
|
|
echo "[INFO] hotspot${i}: IPv6 delegated prefix found"
|
|
|
|
echo "[INFO] hotspot${i}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix $i)"
|
|
|
|
|
|
|
|
if is_ip6addr_set ${i}; then
|
|
|
|
echo "[ OK ] hotspot${i}: IPv6 address set"
|
|
|
|
else
|
|
|
|
echo "[FAIL] hotspot${i}: No IPv6 address set"
|
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_ip6firewall_set ${i}; then
|
|
|
|
echo "[ OK ] hotspot${i}: IPv6 firewalling set"
|
|
|
|
else
|
|
|
|
if [ "${ip6_firewall[${i}]}" -eq 1 ]; then
|
|
|
|
echo "[FAIL] hotspot${i}: No IPv6 firewalling set"
|
|
|
|
else
|
|
|
|
echo "[INFO] hotspot${i}: No IPv6 firewalling set"
|
|
|
|
fi
|
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_dhcpd6_running ${i}; then
|
|
|
|
echo "[ OK ] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are running"
|
|
|
|
else
|
|
|
|
echo "[FAIL] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are not running"
|
|
|
|
exitcode=1
|
|
|
|
fi
|
2015-05-02 17:41:54 +02:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[INFO] hotspot${i}: No IPv6 delegated prefix found"
|
2015-05-02 17:41:54 +02:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
if is_dhcpd4_running ${i}; then
|
|
|
|
echo "[ OK ] hotspot${i}: DHCPv4 server (dnsmasq) is running"
|
2015-07-24 23:48:24 +02:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] hotspot${i}: DHCPv4 (dnsmasq) is not running"
|
|
|
|
exitcode=1
|
2015-07-24 23:48:24 +02:00
|
|
|
fi
|
|
|
|
|
2021-11-16 00:20:13 +01:00
|
|
|
if is_ip4nataddr_set ${i}; then
|
|
|
|
echo "[ OK ] hotspot${i}: IPv4 NAT address set"
|
2015-04-26 21:34:11 +02:00
|
|
|
else
|
2021-11-16 00:20:13 +01:00
|
|
|
echo "[FAIL] hotspot${i}: No IPv4 NAT address set"
|
|
|
|
exitcode=1
|
2015-04-26 21:34:11 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
exit ${exitcode}
|
2021-11-16 00:20:13 +01:00
|
|
|
;;
|
|
|
|
*)
|
2014-12-28 15:38:05 +01:00
|
|
|
echo "Usage: $0 {start|stop|restart|status}"
|
2014-11-09 00:40:40 +01:00
|
|
|
exit 1
|
2021-11-16 00:20:13 +01:00
|
|
|
;;
|
2014-11-09 00:40:40 +01:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|