2014-11-09 00:40:40 +01:00
|
|
|
#!/bin/bash
|
|
|
|
### BEGIN INIT INFO
|
|
|
|
# Provides: ynh-hotspot
|
2015-04-24 11:04:50 +02:00
|
|
|
# Required-Start: $network $remote_fs $syslog yunohost-api
|
2014-11-09 00:40:40 +01:00
|
|
|
# Required-Stop: $network $remote_fs $syslog
|
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
|
|
|
# Short-Description: Set prerequisites for wifi hotspot.
|
|
|
|
# Description: Set prerequisites for wifi hotspot.
|
|
|
|
### END INIT INFO
|
|
|
|
|
2014-12-26 20:18:03 +01:00
|
|
|
# Wifi Hotspot app for YunoHost
|
|
|
|
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
|
|
|
|
# Contribute at https://github.com/jvaubourg/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/>.
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
# Functions
|
|
|
|
## State functions
|
|
|
|
|
2014-11-16 23:27:36 +01:00
|
|
|
has_vpnclient_app() {
|
|
|
|
[ -e /tmp/.ynh-vpnclient-started ]
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:27:59 +01:00
|
|
|
has_ip6delegatedprefix() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
|
|
|
[ "${ynh_ip6_net[${i}]}" != none ]
|
2014-11-10 22:27:59 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
is_nat_set() {
|
2014-11-09 22:49:07 +01:00
|
|
|
internet_device=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}"
|
|
|
|
}
|
|
|
|
|
|
|
|
is_ip4nataddr_set() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip address show dev "${dev}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix[${i}]}.1/24"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is_ip6addr_set() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip address show dev "${dev}" 2> /dev/null | grep -q "${ynh_ip6_addr[${i}]}/64"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is_forwarding_set() {
|
|
|
|
ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
|
|
|
|
ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
|
|
|
|
|
|
|
|
[ "${ip6}" -eq 1 -a "${ip4}" -eq 1 ]
|
|
|
|
}
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
is_dhcpd6_running() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
$(ps aux | grep "dhcpdv6-ssid${i}" | grep -qv grep)
|
|
|
|
}
|
|
|
|
|
|
|
|
is_dhcpd4_running() {
|
|
|
|
i=${1}
|
|
|
|
|
|
|
|
$(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() {
|
2015-05-12 09:16:53 +02:00
|
|
|
systemctl is-active hostapd --quiet &> /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() {
|
|
|
|
for i in $(seq 0 $((${ynh_multissid} - 1))); do
|
|
|
|
( has_ip6delegatedprefix ${i} && is_ip6addr_set ${i} || ! has_ip6delegatedprefix ${i} )\
|
2015-05-02 17:41:54 +02:00
|
|
|
&& is_ip4nataddr_set ${i} && is_dhcpd6_running ${i} && is_dhcpd4_running ${i}
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
if [ ! $? -eq 0 ]; then
|
2014-12-26 18:58:58 +01:00
|
|
|
return 1
|
|
|
|
fi
|
2015-04-26 21:34:11 +02:00
|
|
|
done
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
is_hostapd_running && is_forwarding_set && is_nat_set "${new_internet_device}"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Setters
|
|
|
|
|
|
|
|
set_nat() {
|
2014-11-09 22:49:07 +01:00
|
|
|
internet_device=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
iptables -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE
|
|
|
|
}
|
|
|
|
|
|
|
|
set_ip4nataddr() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip address add "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_ip6addr() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
ip address delete "${ynh_ip6_addr[${i}]}/64" dev tun0 &> /dev/null
|
2015-05-02 14:54:08 +02:00
|
|
|
ip address add "${ynh_ip6_addr[${i}]}/64" dev "${dev}"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_forwarding() {
|
|
|
|
sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
|
|
|
|
sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
|
|
|
|
}
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
start_dhcpd6() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
rm -f /etc/dnsmasq.dhcpd/dhcpdv6-ssid*.conf
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2015-05-08 01:08:32 +02:00
|
|
|
cp /etc/dnsmasq.dhcp/dhcpdv6{.conf.tpl,-ssid${i}.conf}
|
2015-05-02 17:41:54 +02:00
|
|
|
|
|
|
|
sed "s|<TPL:WIFI_DEVICE>|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
sed "s|<TPL:IP6_NET>|${ynh_ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
|
|
|
sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
|
2015-04-26 21:34:11 +02:00
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0
|
|
|
|
}
|
|
|
|
|
|
|
|
start_dhcpd4() {
|
|
|
|
i=${1}
|
|
|
|
|
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
2014-12-26 18:58:58 +01:00
|
|
|
fi
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
rm -f /etc/dnsmasq.dhcpd/dhcpdv4-ssid*.conf
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
|
|
|
|
|
|
|
|
sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
|
|
|
sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
2015-05-02 14:54:08 +02:00
|
|
|
sed "s|<TPL:WIFI_DEVICE>|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
2015-04-26 21:34:11 +02:00
|
|
|
sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0
|
2014-12-26 18:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
start_hostapd() {
|
2015-04-26 21:34:11 +02:00
|
|
|
cp /etc/hostapd/hostapd.conf{.tpl1,}
|
|
|
|
|
2015-05-02 12:51:57 +02:00
|
|
|
ethaddr=$(ip link show dev "${ynh_wifi_device}" | grep link/ether | awk -F: '{ printf "02:%s:%s:%s:%s:00", $2, $3, $4, $5 }')
|
|
|
|
ip link set addr "${ethaddr}" dev "${ynh_wifi_device}"
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
|
2014-11-09 22:49:07 +01:00
|
|
|
sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
|
2015-05-02 14:54:08 +02:00
|
|
|
sed "s|<TPL:N_COMMENT>||g" -i /etc/hostapd/hostapd.conf
|
2014-11-09 23:49:06 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
for i in $(seq 0 $((${ynh_multissid} - 1))); do
|
|
|
|
cp /etc/hostapd/hostapd.conf{.tpl2,.tmp}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
sed "s|<TPL:WIFI_INTERFACE>|hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp
|
2015-04-26 21:34:11 +02:00
|
|
|
sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
|
|
|
|
if [ "${ynh_wifi_secure[${i}]}" -eq 1 ]; then
|
|
|
|
sed "s|<TPL:SEC_COMMENT>||g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
else
|
|
|
|
sed "s|<TPL:SEC_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
fi
|
|
|
|
|
2015-05-02 12:32:19 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
sed "s|<TPL:BSS_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
else
|
|
|
|
sed "s|<TPL:BSS_COMMENT>||g" -i /etc/hostapd/hostapd.conf.tmp
|
|
|
|
fi
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
cat /etc/hostapd/hostapd.conf.tmp >> /etc/hostapd/hostapd.conf
|
|
|
|
rm /etc/hostapd/hostapd.conf.tmp
|
|
|
|
done
|
2015-03-14 15:44:48 +01:00
|
|
|
|
2015-05-12 09:16:53 +02:00
|
|
|
systemctl start hostapd --quiet
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Unsetters
|
|
|
|
|
|
|
|
unset_nat() {
|
2014-11-09 22:49:07 +01:00
|
|
|
internet_device=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
|
|
|
|
}
|
|
|
|
|
|
|
|
unset_ip4nataddr() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unset_ip6addr() {
|
2015-04-26 21:34:11 +02:00
|
|
|
i=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
2015-05-02 14:54:08 +02:00
|
|
|
if [ "${i}" -eq 0 ]; then
|
|
|
|
dev=${ynh_wifi_device}
|
|
|
|
else
|
|
|
|
dev="hotspot${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip address delete "${ynh_ip6_addr[${i}]}/64" dev "${dev}"
|
2014-12-26 18:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
unset_forwarding() {
|
|
|
|
sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
|
|
|
|
sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
|
|
|
|
}
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
stop_dhcpd6() {
|
|
|
|
kill $(ps aux | grep 'dhcpdv6-ssid' | grep -v grep | awk '{ print $2 }')
|
|
|
|
rm -f /etc/dnsmasq.d/dhcpdv6-ssid*.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
stop_dhcpd4() {
|
|
|
|
kill $(ps aux | grep 'dhcpdv4-ssid' | grep -v grep | awk '{ print $2 }')
|
|
|
|
rm -f /etc/dnsmasq.d/dhcpdv4-ssid*.conf
|
2015-04-26 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
stop_hostapd() {
|
2015-05-12 09:16:53 +02:00
|
|
|
systemctl stop hostapd --quiet
|
2014-11-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## Tools
|
|
|
|
|
|
|
|
moulinette_get() {
|
2014-11-09 22:49:07 +01:00
|
|
|
var=${1}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
value=$(yunohost app setting hotspot "${var}")
|
|
|
|
|
|
|
|
if [[ "${value}" =~ "An instance is already running" ]]; then
|
|
|
|
echo "${value}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "${value}"
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:18 +01:00
|
|
|
moulinette_vpnclient_get() {
|
|
|
|
var=${1}
|
|
|
|
|
|
|
|
value=$(yunohost app setting vpnclient "${var}")
|
|
|
|
|
|
|
|
if [[ "${value}" =~ "An instance is already running" ]]; then
|
|
|
|
echo "${value}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "${value}"
|
|
|
|
}
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
moulinette_set() {
|
2014-11-09 22:49:07 +01:00
|
|
|
var=${1}
|
|
|
|
value=${2}
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
msg=$(yunohost app setting hotspot "${var}" -v "${value}")
|
|
|
|
|
|
|
|
if [ ! $? -eq 0 ]; then
|
|
|
|
echo "${msg}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:27:39 +01:00
|
|
|
if [ "$1" != restart ]; then
|
2014-11-25 21:05:24 +01:00
|
|
|
|
2014-12-28 23:27:39 +01:00
|
|
|
# Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
|
|
|
|
if [ ! -e /tmp/.ynh-hotspot-boot ]; then
|
|
|
|
touch /tmp/.ynh-hotspot-boot
|
2015-05-12 09:16:53 +02:00
|
|
|
systemctl restart php5-fpm --quiet
|
2014-12-28 23:27:39 +01:00
|
|
|
fi
|
2014-11-25 21:05:24 +01:00
|
|
|
|
2014-12-28 23:27:39 +01:00
|
|
|
# Variables
|
|
|
|
|
|
|
|
echo -n "Retrieving Yunohost settings... "
|
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
ynh_service_enabled=$(moulinette_get service_enabled)
|
2014-12-28 23:27:39 +01:00
|
|
|
ynh_wifi_device=$(moulinette_get wifi_device)
|
|
|
|
ynh_wifi_channel=$(moulinette_get wifi_channel)
|
2015-04-26 21:34:11 +02:00
|
|
|
ynh_multissid=$(moulinette_get multissid)
|
|
|
|
|
|
|
|
IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_get wifi_ssid)"
|
|
|
|
IFS='|' read -a ynh_wifi_secure <<< "$(moulinette_get wifi_secure)"
|
|
|
|
IFS='|' read -a ynh_wifi_passphrase <<< "$(moulinette_get wifi_passphrase)"
|
|
|
|
IFS='|' read -a ynh_ip6_addr <<< "$(moulinette_get ip6_addr)"
|
|
|
|
IFS='|' read -a ynh_ip6_net <<< "$(moulinette_get ip6_net)"
|
|
|
|
IFS='|' read -a ynh_ip6_dns0 <<< "$(moulinette_get ip6_dns0)"
|
|
|
|
IFS='|' read -a ynh_ip6_dns1 <<< "$(moulinette_get ip6_dns1)"
|
|
|
|
IFS='|' read -a ynh_ip4_dns0 <<< "$(moulinette_get ip4_dns0)"
|
|
|
|
IFS='|' read -a ynh_ip4_dns1 <<< "$(moulinette_get ip4_dns1)"
|
|
|
|
IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_get ip4_nat_prefix)"
|
|
|
|
|
2014-12-28 23:27:39 +01:00
|
|
|
old_internet_device=$(moulinette_get internet_device)
|
|
|
|
new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
|
|
|
|
|
|
|
|
# Switch the NAT interface if there is a VPN
|
|
|
|
ip link show dev tun0 &> /dev/null
|
|
|
|
if [ "$?" -eq 0 ]; then
|
|
|
|
new_internet_device=tun0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "OK"
|
|
|
|
|
|
|
|
# Check IPv6 delegated prefix from vpnclient
|
2015-04-26 21:34:11 +02:00
|
|
|
# vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
|
|
|
|
#
|
|
|
|
# if [ ! -z "${vpnclient_ip6_addr}" ]; then
|
|
|
|
# if [ "${ynh_ip6_net}" == none ]; then
|
|
|
|
# ynh_ip6_net=$vpnclient_ip6_net
|
|
|
|
# ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
|
|
|
|
#
|
|
|
|
# moulinette_set ip6_net "${ynh_ip6_net}"
|
|
|
|
# moulinette_set ip6_addr "${ynh_ip6_addr}"
|
|
|
|
# else
|
|
|
|
# if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
|
|
|
|
# echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
|
|
|
|
# fi
|
|
|
|
# fi
|
|
|
|
# fi
|
2014-12-28 23:27:39 +01:00
|
|
|
|
2014-11-17 23:44:18 +01:00
|
|
|
fi
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
# Script
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
start)
|
|
|
|
if is_running; then
|
|
|
|
echo "Already started"
|
2015-03-14 15:44:48 +01:00
|
|
|
elif [ "${ynh_service_enabled}" -eq 0 ]; then
|
|
|
|
echo "Disabled service"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2014-11-16 23:27:36 +01:00
|
|
|
echo "[hotspot] Starting..."
|
|
|
|
touch /tmp/.ynh-hotspot-started
|
|
|
|
|
|
|
|
if [ "${new_internet_device}" == tun0 ]; then
|
|
|
|
moulinette_set vpnclient yes
|
|
|
|
else
|
|
|
|
moulinette_set vpnclient no
|
|
|
|
fi
|
2014-11-09 00:40:40 +01:00
|
|
|
|
|
|
|
# Check old state of the ipv4 NAT settings
|
|
|
|
if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
|
|
|
|
&& is_nat_set "${old_internet_device}"; then
|
|
|
|
|
|
|
|
unset_nat "${old_internet_device}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set ipv4 NAT
|
|
|
|
if ! is_nat_set "${new_internet_device}"; then
|
|
|
|
echo "Set NAT"
|
|
|
|
set_nat "${new_internet_device}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set forwarding for ipv6 and ipv4
|
|
|
|
if ! is_forwarding_set; then
|
|
|
|
echo "Set forwarding"
|
|
|
|
set_forwarding
|
|
|
|
fi
|
|
|
|
|
2015-03-17 00:35:34 +01:00
|
|
|
# Run hostapd
|
|
|
|
if ! is_hostapd_running; then
|
|
|
|
echo "Run hostapd"
|
2015-04-26 21:34:11 +02:00
|
|
|
start_hostapd ${i}
|
|
|
|
|
|
|
|
if [ ! $? -eq 0 ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
i=0; false || while [ $? -ne 0 ]; do
|
|
|
|
sleep 1 && (( i++ ))
|
|
|
|
[ ${i} -gt 20 ] && stop_hostapd
|
|
|
|
[ ${i} -gt 20 ] && exit 1
|
2015-05-02 12:32:19 +02:00
|
|
|
ip link show dev "mon.${ynh_wifi_device}" &> /dev/null
|
2015-04-26 21:34:11 +02:00
|
|
|
done
|
2015-03-17 00:35:34 +01:00
|
|
|
fi
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
# For each registred ssid
|
|
|
|
for i in $(seq 0 $((${ynh_multissid} - 1))); do
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
# Set ipv4 NAT address
|
|
|
|
if ! is_ip4nataddr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "hotspot${i}: Set IPv4 NAT address"
|
2015-04-26 21:34:11 +02:00
|
|
|
set_ip4nataddr ${i}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set the ipv6 address
|
|
|
|
if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "hotspot${i}: Set IPv6 address"
|
2015-04-26 21:34:11 +02:00
|
|
|
set_ip6addr ${i}
|
|
|
|
fi
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
# Run DHCPv6 server
|
|
|
|
if has_ip6delegatedprefix ${i} && ! is_dhcpd6_running ${i}; then
|
|
|
|
echo "hotspot${i}: Start the NDP and DHCPv6 server (dnsmasq)"
|
|
|
|
start_dhcpd6 ${i}
|
2015-04-26 21:34:11 +02:00
|
|
|
fi
|
2015-05-02 17:41:54 +02:00
|
|
|
|
|
|
|
# Run DHCPv4 server
|
|
|
|
if ! is_dhcpd4_running ${i}; then
|
|
|
|
echo "hotspot${i}: Start the DHCPv4 server (dnsmasq)"
|
|
|
|
start_dhcpd4 ${i}
|
|
|
|
fi
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
done
|
2014-11-16 23:27:36 +01:00
|
|
|
|
|
|
|
# Update dynamic settings
|
|
|
|
moulinette_set internet_device "${new_internet_device}"
|
2014-11-09 00:40:40 +01:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
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
|
|
|
|
|
|
|
if is_nat_set "${old_internet_device}"; then
|
|
|
|
echo "Unset NAT"
|
|
|
|
unset_nat "${old_internet_device}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_forwarding_set; then
|
|
|
|
echo "Unset forwarding"
|
|
|
|
unset_forwarding
|
|
|
|
fi
|
2015-04-26 21:34:11 +02:00
|
|
|
|
|
|
|
for i in $(seq 0 $((${ynh_multissid} - 1))); do
|
|
|
|
if is_ip4nataddr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "hotspot${i}: Unset IPv4 NAT address"
|
2015-04-26 21:34:11 +02:00
|
|
|
unset_ip4nataddr ${i}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "hotspot${i}: Unset IPv6 address"
|
2015-04-26 21:34:11 +02:00
|
|
|
unset_ip6addr ${i}
|
|
|
|
fi
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
if is_dhcpd6_running ${i}; then
|
|
|
|
echo "hotspot${i}: Stop the NDP and DHCPv6 server (dnsmasq)"
|
|
|
|
stop_dhcpd6 ${i}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_dhcpd4_running ${i}; then
|
|
|
|
echo "hotspot${i}: Stop the DHCPv4 server (dnsmasq)"
|
|
|
|
stop_dhcpd4 ${i}
|
2015-04-26 21:34:11 +02:00
|
|
|
fi
|
|
|
|
done
|
2014-12-26 18:58:58 +01:00
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
if is_hostapd_running; then
|
|
|
|
echo "Stop hostapd"
|
|
|
|
stop_hostapd
|
|
|
|
fi
|
2014-11-16 23:27:36 +01:00
|
|
|
|
|
|
|
if has_vpnclient_app; then
|
2015-05-12 09:16:53 +02:00
|
|
|
systemctl start ynh-vpnclient --quiet
|
2014-11-16 23:27:36 +01:00
|
|
|
fi
|
2014-11-09 00:40:40 +01:00
|
|
|
;;
|
2014-12-28 15:38:05 +01:00
|
|
|
restart)
|
|
|
|
$0 stop
|
|
|
|
$0 start
|
|
|
|
;;
|
2014-11-09 00:40:40 +01:00
|
|
|
status)
|
|
|
|
exitcode=0
|
2014-11-20 20:03:24 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if [ "${ynh_service_enabled}" -eq 0 ]; then
|
|
|
|
echo "[ERR] Hotspot Service disabled"
|
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
2014-11-20 20:03:24 +01:00
|
|
|
echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
if is_nat_set "${new_internet_device}"; then
|
2014-11-17 23:44:18 +01:00
|
|
|
echo "[OK] IPv4 NAT set"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2014-11-17 23:44:18 +01:00
|
|
|
echo "[ERR] No IPv4 NAT set"
|
2014-11-09 00:40:40 +01:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_forwarding_set; then
|
2014-11-17 23:44:18 +01:00
|
|
|
echo "[OK] IPv6/IPv4 forwarding set"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2014-11-17 23:44:18 +01:00
|
|
|
echo "[ERR] No IPv6/IPv4 forwarding set"
|
2014-11-09 00:40:40 +01:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 18:58:58 +01:00
|
|
|
if is_hostapd_running; then
|
|
|
|
echo "[OK] Hostapd is running"
|
2014-11-09 00:40:40 +01:00
|
|
|
else
|
2014-12-26 18:58:58 +01:00
|
|
|
echo "[ERR] Hostapd is not running"
|
2014-11-09 00:40:40 +01:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
2015-04-26 21:34:11 +02:00
|
|
|
for i in $(seq 0 $((${ynh_multissid} - 1))); do
|
|
|
|
if has_ip6delegatedprefix ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[INFO] hotspot${i}: IPv6 delegated prefix found"
|
|
|
|
echo "[INFO] hotspot${i}: IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
|
2015-04-26 21:34:11 +02:00
|
|
|
|
|
|
|
if is_ip6addr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[OK] hotspot${i}: IPv6 address set"
|
|
|
|
else
|
|
|
|
echo "[ERR] hotspot${i}: No IPv6 address set"
|
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_dhcpd6_running ${i}; then
|
|
|
|
echo "[OK] hotspot${i}: NDP and DHCPv6 server (dnsmasq) is running"
|
2015-04-26 21:34:11 +02:00
|
|
|
else
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[ERR] hotspot${i}: NDP and DHCPv6 server (dnsmasq) is not running"
|
2015-04-26 21:34:11 +02:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
else
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[INFO] hotspot${i}: No IPv6 delegated prefix found"
|
2015-04-26 21:34:11 +02:00
|
|
|
fi
|
|
|
|
|
2015-05-02 17:41:54 +02:00
|
|
|
if is_dhcpd4_running ${i}; then
|
|
|
|
echo "[OK] hotspot${i}: DHCPv4 server (dnsmasq) is running"
|
2015-04-26 21:34:11 +02:00
|
|
|
else
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[ERR] hotspot${i}: NDP and DHCPv4 (dnsmasq) is not running"
|
2015-04-26 21:34:11 +02:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_ip4nataddr_set ${i}; then
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[OK] hotspot${i}: IPv4 NAT address set"
|
2015-04-26 21:34:11 +02:00
|
|
|
else
|
2015-05-02 17:41:54 +02:00
|
|
|
echo "[ERR] hotspot${i}: No IPv4 NAT address set"
|
2015-04-26 21:34:11 +02:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-11-09 00:40:40 +01:00
|
|
|
exit ${exitcode}
|
|
|
|
;;
|
|
|
|
*)
|
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
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|