1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hotspot_ynh.git synced 2024-09-03 19:25:53 +02:00

Merge pull request #92 from YunoHost-Apps/testing

Testing
This commit is contained in:
ljf (zamentur) 2021-12-04 00:54:33 +01:00 committed by GitHub
commit b9d264811f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 378 additions and 456 deletions

View file

@ -1,25 +1,10 @@
# Wifi Hotspot app for YunoHost # 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/>.
# Do DHCP for this subnet. # Do DHCP for this subnet.
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP4_NAT_PREFIX>.2,<TPL:IP4_NAT_PREFIX>.254,4h dhcp-range=interface:__WIFI_DEVICE__,__IP4_NAT_PREFIX__.2,__IP4_NAT_PREFIX__.254,4h
# Send DHCPv4 option. # Send DHCPv4 option.
dhcp-option=option:dns-server,<TPL:IP4_DNS> dhcp-option=option:dns-server,__IP4_DNS__
# Set the DHCP server to authoritative mode. In this mode it will barge in # Set the DHCP server to authoritative mode. In this mode it will barge in
# and take over the lease for any client which broadcasts on the network, # and take over the lease for any client which broadcasts on the network,

View file

@ -1,23 +1,6 @@
# 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/>.
# Do DHCP and Router Advertisements for this subnet. Set the A bit in the RA # Do DHCP and Router Advertisements for this subnet. Set the A bit in the RA
# so that clients can use SLAAC addresses as well as DHCP ones. # so that clients can use SLAAC addresses as well as DHCP ones.
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP6_NET>,slaac,64,4h dhcp-range=interface:__WIFI_DEVICE__,__IP6_NET__,slaac,64,4h
# Send DHCPv6 option. Note [] around IPv6 addresses. # Send DHCPv6 option. Note [] around IPv6 addresses.
dhcp-option=option6:dns-server,<TPL:IP6_DNS> dhcp-option=option6:dns-server,__IP6_DNS__

View file

@ -0,0 +1,7 @@
__BSS_COMMENT__bss=__WIFI_INTERFACE__
ssid=__WIFI_SSID__
__SEC_COMMENT__wpa=2
__SEC_COMMENT__wpa_passphrase=__WIFI_PASSPHRASE__
__SEC_COMMENT__wpa_key_mgmt=WPA-PSK
__SEC_COMMENT__wpa_pairwise=TKIP
__SEC_COMMENT__rsn_pairwise=CCMP

8
conf/hostapd.base.conf Normal file
View file

@ -0,0 +1,8 @@
interface=__WIFI_DEVICE__
hw_mode=g
__N_COMMENT__ieee80211n=1
__N_COMMENT__wmm_enabled=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
channel=__WIFI_CHANNEL__

View file

@ -1,26 +0,0 @@
# 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/>.
interface=<TPL:WIFI_DEVICE>
hw_mode=g
<TPL:N_COMMENT>ieee80211n=1
<TPL:N_COMMENT>wmm_enabled=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
channel=<TPL:WIFI_CHANNEL>

View file

@ -1,8 +0,0 @@
<TPL:BSS_COMMENT>bss=<TPL:WIFI_INTERFACE>
ssid=<TPL:WIFI_SSID>
<TPL:SEC_COMMENT>wpa=2
<TPL:SEC_COMMENT>wpa_passphrase=<TPL:WIFI_PASSPHRASE>
<TPL:SEC_COMMENT>wpa_key_mgmt=WPA-PSK
<TPL:SEC_COMMENT>wpa_pairwise=TKIP
<TPL:SEC_COMMENT>rsn_pairwise=CCMP

View file

@ -25,72 +25,72 @@ has_vpnclient_app() {
} }
has_ip6delegatedprefix() { has_ip6delegatedprefix() {
i=${1} local i=${1}
[[ -n "${ynh_ip6_net[${i}]}" ]] && [[ "${ynh_ip6_net[${i}]}" != "none" ]] [[ -n "${ip6_net[${i}]}" ]] && [[ "${ip6_net[${i}]}" != "none" ]]
} }
ip6addrfromdelegatedprefix() { ip6addrfromdelegatedprefix() {
i=${1} local i=${1}
echo "${ynh_ip6_net[${i}]}${i}001" echo "${ip6_net[${i}]}${i}001"
} }
is_nat_set() { is_nat_set() {
internet_device=${1} local gateway_interface=${1}
iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}" iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
} }
is_ip4nataddr_set() { is_ip4nataddr_set() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
ip address show dev "${dev}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix[${i}]}.1/24" ip address show dev "${dev}" 2>/dev/null | grep -q "${ip4_nat_prefix[${i}]}.1/24"
} }
is_ip6addr_set() { is_ip6addr_set() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
ip address show dev "${dev}" 2> /dev/null | grep -q "$(ip6addrfromdelegatedprefix $i)/64" ip address show dev "${dev}" 2>/dev/null | grep -q "$(ip6addrfromdelegatedprefix $i)/64"
} }
is_ip6firewall_set() { is_ip6firewall_set() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
ip6tables -w -nvL FORWARD | grep DROP | grep -q "${dev}" ip6tables -w -nvL FORWARD | grep DROP | grep -q "${dev}"
} }
is_forwarding_set() { is_forwarding_set() {
ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }') local ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }') local ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
[ "${ip6}" -eq 1 -a "${ip4}" -eq 1 ] [ "${ip6}" -eq 1 ] && [ "${ip4}" -eq 1 ]
} }
is_dhcpd6_running() { is_dhcpd6_running() {
i=${1} local i=${1}
$(ps aux | grep "dhcpdv6-ssid${i}" | grep -qv grep) ps aux | grep "dhcpdv6-ssid${i}" | grep -qv grep
} }
is_dhcpd4_running() { is_dhcpd4_running() {
i=${1} local i=${1}
$(ps aux | grep "dhcpdv4-ssid${i}" | grep -qv grep) ps aux | grep "dhcpdv4-ssid${i}" | grep -qv grep
} }
is_hostapd_running() { is_hostapd_running() {
systemctl is-active hostapd &> /dev/null systemctl is-active hostapd &>/dev/null
} }
is_running() { is_running() {
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
( has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}\ (has_ip6delegatedprefix ${i} && is_ip6addr_set ${i} \
&& ( [ "${ynh_ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i} || [ "${ynh_ip6_firewall[${i}]}" -eq 0 ] )\ && ([ "${ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i} || [ "${ip6_firewall[${i}]}" -eq 0 ]) \
&& is_dhcpd6_running ${i} || ! has_ip6delegatedprefix ${i} )\ && is_dhcpd6_running ${i} || ! has_ip6delegatedprefix ${i}) \
&& is_ip4nataddr_set ${i} && is_dhcpd4_running ${i} && is_ip4nataddr_set ${i} && is_dhcpd4_running ${i}
if [ ! $? -eq 0 ]; then if [ ! $? -eq 0 ]; then
@ -98,152 +98,169 @@ is_running() {
fi fi
done done
is_hostapd_running && is_forwarding_set && ( [ -z "${new_internet_device}" ] || is_nat_set "${new_internet_device}" ) is_hostapd_running && is_forwarding_set && ([ -z "${new_gateway_interface}" ] || is_nat_set "${new_gateway_interface}")
} }
## Setters ## Setters
set_nat() { set_nat() {
internet_device=${1} local gateway_interface=${1}
iptables -w -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE iptables -w -t nat -A POSTROUTING -o "${gateway_interface}" -j MASQUERADE
} }
set_ip4nataddr() { set_ipaddr() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
ip address add "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}" 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
set_ip6addr() { if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
i=${1} echo "hotspot${i}: Set IPv6 address"
dev=$(devfromid "${i}") ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev tun0 &>/dev/null
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev tun0 &> /dev/null
ip address add "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}" ip address add "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}"
fi
} }
set_ip6firewall() { set_ipfirewall() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
# 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 -i "${dev}" -j ACCEPT
ip6tables -w -A FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ip6tables -w -A FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -A FORWARD -o "${dev}" -j DROP ip6tables -w -A FORWARD -o "${dev}" -j DROP
fi
} }
set_forwarding() { set_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null sysctl -w net.ipv4.conf.all.forwarding=1 >/dev/null
} }
start_dhcpd6() { start_dhcpd() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
cp /etc/dnsmasq.dhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf} # Run DHCPv4 server
if ! is_dhcpd4_running ${i}; then
sed "s|<TPL:WIFI_DEVICE>|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf echo "hotspot${i}: Start the DHCPv4 server (dnsmasq)"
sed "s|<TPL:IP6_NET>|${ynh_ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|<TPL:IP6_DNS>|${ynh_ip6_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0
}
start_dhcpd4() {
i=${1}
dev=$(devfromid "${i}")
cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf} cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
sed "s|<TPL:IP4_DNS>|${ynh_ip4_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf sed "s|__WIFI_DEVICE__|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|<TPL: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|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix[${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
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0 dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0
fi
# Run DHCPv6 server
if has_ip6delegatedprefix ${i} && ! is_dhcpd6_running ${i}; then
echo "hotspot${i}: Start the NDP and DHCPv6 server (dnsmasq)"
cp /etc/dnsmasq.dhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf}
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
} }
start_hostapd() { configure_hostapd() {
cp /etc/hostapd/hostapd.conf{.tpl1,}
ethaddr=$(ip link show dev "${ynh_wifi_device}" | grep link/ether | awk -F: '{ printf "02:%s:%s:%s:%s:00", $2, $3, $4, $5 }') 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 "${ynh_wifi_device}" ip link set addr "${ethaddr}" dev "${wifi_device}"
sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf cp /etc/hostapd/hostapd.base.conf /etc/hostapd/hostapd.conf
sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf sed "s|__WIFI_DEVICE__|${wifi_device}|g" -i /etc/hostapd/hostapd.conf
sed "s|<TPL:N_COMMENT>||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
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
cp /etc/hostapd/hostapd.conf{.tpl2,.tmp}
sed "s|<TPL:WIFI_INTERFACE>|hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp [ "${wifi_secure[${i}]}" -eq 1 ] && local sec_comment="" || local sec_comment="#"
sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp [ "${i}" -eq 0 ] && local bss_comment="#" || local bss_comment=""
sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
if [ "${ynh_wifi_secure[${i}]}" -eq 1 ]; then cp /etc/hostapd/hostapd.accesspoint.conf /etc/hostapd/hostapd.conf.tmp
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
if [ "${i}" -eq 0 ]; then sed "s|__WIFI_INTERFACE__|hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|<TPL:BSS_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf.tmp sed "s|__WIFI_SSID__|${wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
else sed "s|__WIFI_PASSPHRASE__|${wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|<TPL:BSS_COMMENT>||g" -i /etc/hostapd/hostapd.conf.tmp sed "s|__SEC_COMMENT__|${sec_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
fi sed "s|__BSS_COMMENT__|${bss_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
cat /etc/hostapd/hostapd.conf.tmp >> /etc/hostapd/hostapd.conf cat /etc/hostapd/hostapd.conf.tmp >>/etc/hostapd/hostapd.conf
rm /etc/hostapd/hostapd.conf.tmp rm /etc/hostapd/hostapd.conf.tmp
done done
systemctl start hostapd
} }
## Unsetters ## Unsetters
unset_nat() { unset_nat() {
internet_device=${1} local gateway_interface=${1}
iptables -w -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE iptables -w -t nat -D POSTROUTING -o "${gateway_interface}" -j MASQUERADE
} }
unset_ip4nataddr() { unset_ipaddr() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}" if is_ip4nataddr_set ${i}; then
} echo "hotspot${i}: Unset IPv4 NAT address"
ip address delete "${ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
unset_ip6addr() { fi
i=${1}
dev=$(devfromid "${i}")
if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
echo "hotspot${i}: Unset IPv6 address"
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}" ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}"
fi
} }
unset_ip6firewall() { unset_ipfirewall() {
i=${1} local i=${1}
dev=$(devfromid "${i}") local dev=$(devfromid "${i}")
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 -i "${dev}" -j ACCEPT
ip6tables -w -D FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ip6tables -w -D FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -D FORWARD -o "${dev}" -j DROP ip6tables -w -D FORWARD -o "${dev}" -j DROP
fi
} }
unset_forwarding() { unset_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null sysctl -w net.ipv6.conf.all.forwarding=0 >/dev/null
sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null sysctl -w net.ipv4.conf.all.forwarding=0 >/dev/null
} }
stop_dhcpd6() { 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 }') kill $(ps aux | grep 'dhcpdv6-ssid' | grep -v grep | awk '{ print $2 }')
rm -f /etc/dnsmasq.d/dhcpdv6-ssid*.conf 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
} }
stop_dhcpd4() { stop_dhcpd4() {
kill $(ps aux | grep 'dhcpdv4-ssid' | grep -v grep | awk '{ print $2 }') :
rm -f /etc/dnsmasq.d/dhcpdv4-ssid*.conf
} }
stop_hostapd() { stop_hostapd() {
@ -253,11 +270,19 @@ stop_hostapd() {
## Tools ## Tools
ynh_setting_get() { ynh_setting_get() {
app=${1}
setting=${2}
grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$// APP="$1" KEY="$2" python3 - <<EOF
# '" 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
} }
ynh_setting_set() { ynh_setting_set() {
@ -267,10 +292,10 @@ ynh_setting_set() {
# performance reasons (it takes a few second to run every yunohost commands) # performance reasons (it takes a few second to run every yunohost commands)
# and to remove the need for the infamous '--need-lock' option/issue. # and to remove the need for the infamous '--need-lock' option/issue.
app="$1" key="$2" value="${3:-}" python3 - <<EOF APP="$1" KEY="$2" VALUE="${3:-}" python3 - <<EOF
import os, yaml, sys import os, yaml, sys
app = os.environ['app'] app = os.environ['APP']
key, value = os.environ['key'], os.environ.get('value', None) key, value = os.environ['KEY'], os.environ.get('VALUE', None)
setting_file = "/etc/yunohost/apps/%s/settings.yml" % app setting_file = "/etc/yunohost/apps/%s/settings.yml" % app
assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file
with open(setting_file) as f: with open(setting_file) as f:
@ -283,10 +308,10 @@ EOF
} }
devfromid() { devfromid() {
i=${1} local i=${1}
if [ "${i}" -eq 0 ]; then if [ "${i}" -eq 0 ]; then
echo "${ynh_wifi_device}" echo "${wifi_device}"
else else
echo "hotspot${i}" echo "hotspot${i}"
fi fi
@ -298,41 +323,40 @@ if [ "$1" != restart ]; then
echo -n "Retrieving Yunohost settings... " echo -n "Retrieving Yunohost settings... "
ynh_service_enabled=$(systemctl is-enabled ynh-hotspot) service_enabled=$(systemctl is-enabled ynh-hotspot)
ynh_wifi_device=$(ynh_setting_get hotspot wifi_device) wifi_device=$(ynh_setting_get hotspot wifi_device)
ynh_wifi_channel=$(ynh_setting_get hotspot wifi_channel) wifi_channel=$(ynh_setting_get hotspot wifi_channel)
ynh_multissid=$(ynh_setting_get hotspot multissid) multissid=$(ynh_setting_get hotspot multissid)
IFS='|' read -a ynh_wifi_ssid <<< "$(ynh_setting_get hotspot wifi_ssid)" IFS='|' read -a wifi_ssid <<<"$(ynh_setting_get hotspot wifi_ssid)"
IFS='|' read -a ynh_wifi_secure <<< "$(ynh_setting_get hotspot wifi_secure)" IFS='|' read -a wifi_secure <<<"$(ynh_setting_get hotspot wifi_secure)"
IFS='|' read -a ynh_wifi_passphrase <<< "$(ynh_setting_get hotspot wifi_passphrase)" IFS='|' read -a wifi_passphrase <<<"$(ynh_setting_get hotspot wifi_passphrase)"
IFS='|' read -a ynh_ip6_firewall <<< "$(ynh_setting_get hotspot ip6_firewall)" IFS='|' read -a ip6_firewall <<<"$(ynh_setting_get hotspot ip6_firewall)"
IFS='|' read -a ynh_ip6_net <<< "$(ynh_setting_get hotspot ip6_net)" IFS='|' read -a ip6_net <<<"$(ynh_setting_get hotspot ip6_net)"
IFS='|' read -a ynh_dns <<< "$(ynh_setting_get hotspot dns)" IFS='|' read -a dns <<<"$(ynh_setting_get hotspot dns)"
IFS='|' read -a ynh_ip4_nat_prefix <<< "$(ynh_setting_get hotspot ip4_nat_prefix)" IFS='|' read -a ip4_nat_prefix <<<"$(ynh_setting_get hotspot ip4_nat_prefix)"
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
ynh_ip6_dns[${i}]="" ip6_dns[${i}]=""
ynh_ip4_dns[${i}]="" ip4_dns[${i}]=""
for ip in $(echo "${ynh_dns[${i}]}" | tr ',' ' '); do for ip in $(echo "${dns[${i}]}" | tr ',' ' '); do
if [[ "$ip" == *":"* ]] if [[ "$ip" == *":"* ]]; then
then ip6_dns[${i}]+="[$ip],"
ynh_ip6_dns[${i}]+="[$ip],"
else else
ynh_ip4_dns[${i}]+="$ip," ip4_dns[${i}]+="$ip,"
fi fi
done done
# Remove trailing , # Remove trailing ,
ynh_ip6_dns[${i}]="${ynh_ip6_dns[${i}]%%,}" ip6_dns[${i}]="${ip6_dns[${i}]%%,}"
ynh_ip4_dns[${i}]="${ynh_ip4_dns[${i}]%%,}" ip4_dns[${i}]="${ip4_dns[${i}]%%,}"
done done
old_internet_device=$(ynh_setting_get hotspot internet_device) old_gateway_interface=$(ynh_setting_get hotspot gateway_interface)
new_internet_device=$(ip route get 1.2.3.4 | awk '{ print $5; }') new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
# Switch the NAT interface if there is a VPN # Switch the NAT interface if there is a VPN
ip link show dev tun0 &> /dev/null ip link show dev tun0 &>/dev/null
if [ "$?" -eq 0 ]; then if [ "$?" -eq 0 ]; then
new_internet_device=tun0 new_gateway_interface=tun0
fi fi
echo "OK" echo "OK"
@ -341,145 +365,94 @@ fi
# Script # Script
case "$1" in case "$1" in
start) start)
if is_running; then if is_running; then
echo "Already started" echo "Already started"
elif [ "${ynh_service_enabled}" != "enabled" ]; then exit 0
echo "Disabled service" elif [ "${service_enabled}" != "enabled" ]; then
else echo "Not starting because hotspod service is disabled"
exit 1
fi
if [ -z "${ynh_wifi_device}" ]; then if [ -z "${wifi_device}" ]; then
echo "[ERR] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin" echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exitcode=1 exit 1
fi fi
echo "[hotspot] Starting..." echo "[hotspot] Starting..."
touch /tmp/.ynh-hotspot-started touch /tmp/.ynh-hotspot-started
if [ "${new_internet_device}" == tun0 ]; then
ynh_setting_set hotspot vpnclient yes
else
ynh_setting_set hotspot vpnclient no
fi
# Check old state of the ipv4 NAT settings # Check old state of the ipv4 NAT settings
if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\ if [ -n "${old_gateway_interface}" ] && [ "${new_gateway_interface}" != "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then
&& is_nat_set "${old_internet_device}"; then
unset_nat "${old_internet_device}" unset_nat "${old_gateway_interface}"
fi fi
# Set ipv4 NAT # Set ipv4 NAT
if [ ! -z "${new_internet_device}" ] && ! is_nat_set "${new_internet_device}"; then if [ -n "${new_gateway_interface}" ] && ! is_nat_set "${new_gateway_interface}"; then
echo "Set NAT" echo "Set NAT"
set_nat "${new_internet_device}" set_nat "${new_gateway_interface}"
fi fi
# Set forwarding for ipv6 and ipv4 # Set forwarding for ipv6 and ipv4
if ! is_forwarding_set; then
echo "Set forwarding" echo "Set forwarding"
set_forwarding set_forwarding
fi
# Run hostapd # Run hostapd
if ! is_hostapd_running; then if ! is_hostapd_running; then
echo "Run hostapd" echo "Configuring hostapd"
start_hostapd ${i} configure_hostapd
if [ ! $? -eq 0 ]; then echo "Starting hostapd..."
if ! systemctl start hostapd; then
journalctl -u hostapd -n 100 --no-hostname --no-pager
exit 1 exit 1
fi fi
if [ "${ynh_multissid}" -gt 1 ]; then
i=0; false || while [ $? -ne 0 ]; do
sleep 1 && (( i++ ))
[ ${i} -gt 20 ] && stop_hostapd
[ ${i} -gt 20 ] && exit 1
ip link show dev hotspot1 &> /dev/null
done
else
sleep 1 sleep 1
# On single SSID, the hotspot interface will be wlan0 (or similar)
# in multissid, we additionally want to make sure that at least hotspot1 started
if [ "${multissid}" -gt 1 ]; then
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
fi fi
fi fi
# For each registred ssid # For each registred ssid
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
set_ipaddr ${i}
# Set ipv4 NAT address set_ipfirewall ${i}
if ! is_ip4nataddr_set ${i}; then start_dhcpd ${i}
echo "hotspot${i}: Set IPv4 NAT address"
set_ip4nataddr ${i}
fi
# Set the ipv6 address
if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
echo "hotspot${i}: Set IPv6 address"
set_ip6addr ${i}
fi
# Set ipv6 firewalling
if has_ip6delegatedprefix ${i} && [ "${ynh_ip6_firewall[${i}]}" -eq 1 ] && ! is_ip6firewall_set ${i}; then
echo "hotspot${i}: Set IPv6 firewalling"
set_ip6firewall ${i}
fi
# 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}
fi
# Run DHCPv4 server
if ! is_dhcpd4_running ${i}; then
echo "hotspot${i}: Start the DHCPv4 server (dnsmasq)"
start_dhcpd4 ${i}
fi
done done
# Update dynamic settings # Update dynamic settings
ynh_setting_set hotspot internet_device "${new_internet_device}" ynh_setting_set hotspot gateway_interface "${new_gateway_interface}"
fi
;; ;;
stop) stop)
echo "[hotspot] Stopping..." echo "[hotspot] Stopping..."
rm -f /tmp/.ynh-hotspot-started rm -f /tmp/.ynh-hotspot-started
if [ ! -z "${old_internet_device}" ] && is_nat_set "${old_internet_device}"; then if [ -n "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then
echo "Unset NAT" echo "Unset NAT"
unset_nat "${old_internet_device}" unset_nat "${old_gateway_interface}"
fi fi
if is_forwarding_set; then
echo "Unset forwarding" echo "Unset forwarding"
unset_forwarding unset_forwarding
fi
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
if is_ip4nataddr_set ${i}; then unset_ipaddr ${i}
echo "hotspot${i}: Unset IPv4 NAT address" unset_ipfirewall ${i}
unset_ip4nataddr ${i} stop_dhcpd ${i}
fi
if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
echo "hotspot${i}: Unset IPv6 address"
unset_ip6addr ${i}
fi
if has_ip6delegatedprefix ${i} && [ "${ynh_ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i}; then
echo "hotspot${i}: Unset IPv6 firewalling"
unset_ip6firewall ${i}
fi
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}
fi
done done
if is_hostapd_running; then if is_hostapd_running; then
@ -492,67 +465,67 @@ case "$1" in
ynh-vpnclient start ynh-vpnclient start
fi fi
;; ;;
restart) restart)
$0 stop $0 stop
$0 start $0 start
;; ;;
status) status)
exitcode=0 exitcode=0
if [ "${ynh_service_enabled}" != "enabled" ]; then if [ "${service_enabled}" != "enabled" ]; then
echo "[ERR] Hotspot Service disabled" echo "[FAIL] Hotspot Service disabled"
exitcode=1 exit 1
fi fi
if [ -z "${ynh_wifi_device}" ]; then if [ -z "${wifi_device}" ]; then
echo "[ERR] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin" echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exitcode=1 exit 1
fi fi
echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})" echo "[INFO] Autodetected internet interface: ${new_gateway_interface} (last start: ${old_gateway_interface})"
if is_nat_set "${new_internet_device}"; then if is_nat_set "${new_gateway_interface}"; then
echo "[OK] IPv4 NAT set" echo "[ OK ] IPv4 NAT set"
else else
if [ -z "${new_internet_device}" ]; then if [ -z "${new_gateway_interface}" ]; then
echo "[INFO] No IPv4 NAT set (no internet interface)" echo "[INFO] No IPv4 NAT set (no internet interface)"
else else
echo "[ERR] No IPv4 NAT set" echo "[FAIL] No IPv4 NAT set"
fi fi
exitcode=1 exitcode=1
fi fi
if is_forwarding_set; then if is_forwarding_set; then
echo "[OK] IPv6/IPv4 forwarding set" echo "[ OK ] IPv6/IPv4 forwarding set"
else else
echo "[ERR] No IPv6/IPv4 forwarding set" echo "[FAIL] No IPv6/IPv4 forwarding set"
exitcode=1 exitcode=1
fi fi
if is_hostapd_running; then if is_hostapd_running; then
echo "[OK] Hostapd is running" echo "[ OK ] Hostapd is running"
else else
echo "[ERR] Hostapd is not running" echo "[FAIL] Hostapd is not running"
exitcode=1 exitcode=1
fi fi
for i in $(seq 0 $((${ynh_multissid} - 1))); do for i in $(seq 0 $((${multissid} - 1))); do
if has_ip6delegatedprefix ${i}; then if has_ip6delegatedprefix ${i}; then
echo "[INFO] hotspot${i}: IPv6 delegated prefix found" echo "[INFO] hotspot${i}: IPv6 delegated prefix found"
echo "[INFO] hotspot${i}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix $i)" echo "[INFO] hotspot${i}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix $i)"
if is_ip6addr_set ${i}; then if is_ip6addr_set ${i}; then
echo "[OK] hotspot${i}: IPv6 address set" echo "[ OK ] hotspot${i}: IPv6 address set"
else else
echo "[ERR] hotspot${i}: No IPv6 address set" echo "[FAIL] hotspot${i}: No IPv6 address set"
exitcode=1 exitcode=1
fi fi
if is_ip6firewall_set ${i}; then if is_ip6firewall_set ${i}; then
echo "[OK] hotspot${i}: IPv6 firewalling set" echo "[ OK ] hotspot${i}: IPv6 firewalling set"
else else
if [ "${ynh_ip6_firewall[${i}]}" -eq 1 ]; then if [ "${ip6_firewall[${i}]}" -eq 1 ]; then
echo "[ERR] hotspot${i}: No IPv6 firewalling set" echo "[FAIL] hotspot${i}: No IPv6 firewalling set"
else else
echo "[INFO] hotspot${i}: No IPv6 firewalling set" echo "[INFO] hotspot${i}: No IPv6 firewalling set"
fi fi
@ -560,9 +533,9 @@ case "$1" in
fi fi
if is_dhcpd6_running ${i}; then if is_dhcpd6_running ${i}; then
echo "[OK] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are running" echo "[ OK ] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are running"
else else
echo "[ERR] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are not running" echo "[FAIL] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are not running"
exitcode=1 exitcode=1
fi fi
else else
@ -570,23 +543,23 @@ case "$1" in
fi fi
if is_dhcpd4_running ${i}; then if is_dhcpd4_running ${i}; then
echo "[OK] hotspot${i}: DHCPv4 server (dnsmasq) is running" echo "[ OK ] hotspot${i}: DHCPv4 server (dnsmasq) is running"
else else
echo "[ERR] hotspot${i}: DHCPv4 (dnsmasq) is not running" echo "[FAIL] hotspot${i}: DHCPv4 (dnsmasq) is not running"
exitcode=1 exitcode=1
fi fi
if is_ip4nataddr_set ${i}; then if is_ip4nataddr_set ${i}; then
echo "[OK] hotspot${i}: IPv4 NAT address set" echo "[ OK ] hotspot${i}: IPv4 NAT address set"
else else
echo "[ERR] hotspot${i}: No IPv4 NAT address set" echo "[FAIL] hotspot${i}: No IPv4 NAT address set"
exitcode=1 exitcode=1
fi fi
done done
exit ${exitcode} exit ${exitcode}
;; ;;
*) *)
echo "Usage: $0 {start|stop|restart|status}" echo "Usage: $0 {start|stop|restart|status}"
exit 1 exit 1
;; ;;

View file

@ -40,10 +40,11 @@ ynh_print_info --message="Declaring files to be backed up..."
# BACKUP THE APP MAIN DIR # BACKUP THE APP MAIN DIR
#================================================= #=================================================
for FILE in $(ls /etc/hostapd/hostapd.conf{.tpl?,} 2>/dev/null) for FILE in $(ls /etc/hostapd/hostapd.*.conf 2>/dev/null)
do do
ynh_backup --src_path="$FILE" ynh_backup --src_path="$FILE"
done done
ynh_backup --src_path="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl" ynh_backup --src_path="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl"
ynh_backup --src_path="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl" ynh_backup --src_path="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl"

View file

@ -141,9 +141,8 @@ ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}"
ynh_app_setting_set --app=$app --key=wifi_channel --value=6 ynh_app_setting_set --app=$app --key=wifi_channel --value=6
ynh_app_setting_set --app=$app --key=ip6_firewall --value=1 ynh_app_setting_set --app=$app --key=ip6_firewall --value=1
ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}" ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}"
ynh_app_setting_set --app=$app --key=dns --value="2001:913::8,2001:910:800::12,80.67.188.188,80.67.169.12" ynh_app_setting_set --app=$app --key=dns --value="10.0.242.1"
ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value=10.0.242 ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value=10.0.242
ynh_app_setting_set --app=$app --key=vpnclient --value=no
if [[ -z $wifi_device ]]; then if [[ -z $wifi_device ]]; then
ynh_app_setting_set --app=$app --key=service_enabled --value=0 ynh_app_setting_set --app=$app --key=service_enabled --value=0
@ -159,7 +158,7 @@ ynh_script_progression --message="Copying configuration files..."
mkdir -pm 0755 /etc/dnsmasq.dhcpd/ mkdir -pm 0755 /etc/dnsmasq.dhcpd/
chown root: /etc/dnsmasq.dhcpd/ chown root: /etc/dnsmasq.dhcpd/
install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/ install -b -o root -g root -m 0644 ../conf/hostapd.*.conf /etc/hostapd/
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl

View file

@ -65,7 +65,7 @@ done
# Remove confs # Remove confs
ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl" ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl"
ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl" ynh_secure_remove --file="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl"
for FILE in $(ls /etc/hostapd/hostapd.conf{.tpl?,} 2>/dev/null) for FILE in $(ls /etc/hostapd/hostapd.*.conf 2>/dev/null)
do do
ynh_secure_remove --file="$FILE" ynh_secure_remove --file="$FILE"
done done

View file

@ -134,7 +134,7 @@ ynh_script_progression --message="Copying configuration..."
mkdir -pm 0755 /etc/dnsmasq.dhcpd/ mkdir -pm 0755 /etc/dnsmasq.dhcpd/
chown root: /etc/dnsmasq.dhcpd/ chown root: /etc/dnsmasq.dhcpd/
install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/ install -b -o root -g root -m 0644 ../conf/hostapd.*.conf /etc/hostapd/
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl