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

463 lines
10 KiB
Text
Raw Normal View History

#!/bin/bash
### BEGIN INIT INFO
# Provides: ynh-hotspot
# Required-Start: $network $remote_fs $syslog $all
# 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
# Functions
## State functions
has_vpnclient_app() {
[ -e /tmp/.ynh-vpnclient-started ]
}
2014-11-10 22:27:59 +01:00
has_ip6delegatedprefix() {
[ "${ynh_ip6_net}" != none ]
}
is_ndproxy_set() {
proxy=$(ip -6 neighbour show proxy)
[ ! -z "${proxy}" ]
}
is_nat_set() {
internet_device=${1}
iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}"
}
is_ip4nataddr_set() {
ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix}.1/24"
}
is_ip6addr_set() {
ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip6_addr}/64"
}
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 ]
}
is_hostapd_running() {
service hostapd status &> /dev/null
}
is_radvd_running() {
service radvd status &> /dev/null
}
is_dhcpd_running() {
service isc-dhcp-server status &> /dev/null
}
is_running() {
2014-11-10 22:27:59 +01:00
( has_ip6delegatedprefix && is_ip6addr_set && is_radvd_running && is_ndproxy_set || ! has_ip6delegatedprefix )\
&& is_nat_set "${new_internet_device}" && is_ip4nataddr_set && is_forwarding_set && is_hostapd_running\
&& is_dhcpd_running
}
## Setters
set_ndproxy() {
ip -6 neighbour add proxy "${ynh_ip6_addr}" dev "${ynh_wifi_device}"
}
set_nat() {
internet_device=${1}
iptables -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE
}
set_ip4nataddr() {
ip address add "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
}
set_ip6addr() {
ip address delete "${ynh_ip6_addr}/64" dev tun0 &> /dev/null
ip address add "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
}
set_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
}
start_hostapd() {
cp /etc/hostapd/hostapd.conf{.tpl,}
sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid}|g" -i /etc/hostapd/hostapd.conf
sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase}|g" -i /etc/hostapd/hostapd.conf
sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
2014-11-09 23:49:06 +01:00
if [ "${ynh_wifi_n}" -eq 1 ]; then
sed "s|<TPL:N_COMMENT>||g" -i /etc/hostapd/hostapd.conf
else
sed "s|<TPL:N_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
fi
service hostapd start
}
start_radvd() {
cp /etc/radvd.conf{.tpl,}
sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/radvd.conf
sed "s|<TPL:IP6_NET>|${ynh_ip6_net}|g" -i /etc/radvd.conf
sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0}|g" -i /etc/radvd.conf
sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1}|g" -i /etc/radvd.conf
service radvd start
}
start_dhcpd() {
cp /etc/dhcp/dhcpd.conf{.tpl,}
sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0}|g" -i /etc/dhcp/dhcpd.conf
sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1}|g" -i /etc/dhcp/dhcpd.conf
sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/dhcp/dhcpd.conf
sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix}|g" -i /etc/dhcp/dhcpd.conf
service isc-dhcp-server start
}
## Unsetters
unset_ndproxy() {
ip -6 neighbour delete proxy "${ynh_ip6_addr}" dev "${ynh_wifi_device}"
}
unset_nat() {
internet_device=${1}
iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
}
unset_ip4nataddr() {
ip address delete "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
}
unset_ip6addr() {
ip address delete "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
}
unset_forwarding() {
sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
}
stop_hostapd() {
service hostapd stop
}
stop_radvd() {
service radvd stop
}
stop_dhcpd() {
service isc-dhcp-server stop
}
## Tools
moulinette_get() {
var=${1}
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}"
}
moulinette_set() {
var=${1}
value=${2}
msg=$(yunohost app setting hotspot "${var}" -v "${value}")
if [ ! $? -eq 0 ]; then
echo "${msg}" >&2
exit 1
fi
}
2014-11-17 23:44:18 +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
service php5-fpm restart
fi
# Variables
echo -n "Retrieving Yunohost settings... "
ynh_wifi_device=$(moulinette_get wifi_device)
ynh_wifi_ssid=$(moulinette_get wifi_ssid)
ynh_wifi_passphrase=$(moulinette_get wifi_passphrase)
ynh_wifi_channel=$(moulinette_get wifi_channel)
2014-11-09 23:49:06 +01:00
ynh_wifi_n=$(moulinette_get wifi_n)
ynh_ip6_addr=$(moulinette_get ip6_addr)
ynh_ip6_net=$(moulinette_get ip6_net)
ynh_ip6_dns0=$(moulinette_get ip6_dns0)
ynh_ip6_dns1=$(moulinette_get ip6_dns1)
ynh_ip4_dns0=$(moulinette_get ip4_dns0)
ynh_ip4_dns1=$(moulinette_get ip4_dns1)
ynh_ip4_nat_prefix=$(moulinette_get ip4_nat_prefix)
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"
2014-11-17 23:44:18 +01:00
# Check IPv6 delegated prefix from vpnclient
2014-11-25 21:05:24 +01: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
2014-11-17 23:44:18 +01:00
fi
fi
# Script
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "[hotspot] Starting..."
touch /tmp/.ynh-hotspot-started
if [ "${new_internet_device}" == tun0 ]; then
moulinette_set vpnclient yes
else
moulinette_set vpnclient no
fi
# Set NDP proxy
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && ! is_ndproxy_set; then
echo "Set NDP proxy"
set_ndproxy
fi
# 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 ipv4 NAT address
if ! is_ip4nataddr_set; then
echo "Set IPv4 NAT address"
set_ip4nataddr
fi
# Set the ipv6 address
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && ! is_ip6addr_set; then
echo "Set IPv6 address"
set_ip6addr
fi
# Set forwarding for ipv6 and ipv4
if ! is_forwarding_set; then
echo "Set forwarding"
set_forwarding
fi
# Run hostapd
if ! is_hostapd_running; then
echo "Run hostapd"
start_hostapd
sleep 1
fi
# Run radvd
# must be running after hostapd
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && ! is_radvd_running; then
echo "Run radvd"
start_radvd
fi
# Run dhcpd
# "options routers" addr (is_ip6addr_set) must be set before
if ! is_dhcpd_running; then
echo "Run dhcpd"
start_dhcpd
fi
# Update dynamic settings
moulinette_set internet_device "${new_internet_device}"
fi
;;
stop)
echo "[hotspot] Stopping..."
rm /tmp/.ynh-hotspot-started
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && is_ndproxy_set; then
echo "Unset NDP proxy"
unset_ndproxy
fi
if is_nat_set "${old_internet_device}"; then
echo "Unset NAT"
unset_nat "${old_internet_device}"
fi
if is_ip4nataddr_set; then
echo "Unset IPv4 NAT address"
unset_ip4nataddr
fi
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && is_ip6addr_set; then
echo "Unset IPv6 address"
unset_ip6addr
fi
if is_forwarding_set; then
echo "Unset forwarding"
unset_forwarding
fi
if is_hostapd_running; then
echo "Stop hostapd"
stop_hostapd
fi
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix && is_radvd_running; then
echo "Stop radvd"
stop_radvd
fi
if is_dhcpd_running; then
echo "Stop dhcpd"
stop_dhcpd
fi
if has_vpnclient_app; then
service ynh-vpnclient start
fi
;;
status)
exitcode=0
2014-11-20 20:03:24 +01:00
echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
2014-11-10 22:27:59 +01:00
if has_ip6delegatedprefix; then
2014-11-17 23:44:18 +01:00
echo "[INFO] IPv6 delegated prefix found"
2014-11-20 20:03:24 +01:00
echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
2014-11-17 23:44:18 +01:00
2014-11-10 22:27:59 +01:00
if is_ndproxy_set; then
2014-11-17 23:44:18 +01:00
echo "[OK] NDP proxy set"
2014-11-10 22:27:59 +01:00
else
2014-11-17 23:44:18 +01:00
echo "[ERR] No NDP proxy set"
2014-11-10 22:27:59 +01:00
exitcode=1
fi
if is_ip6addr_set; then
2014-11-17 23:44:18 +01:00
echo "[OK] IPv6 address set"
2014-11-10 22:27:59 +01:00
else
2014-11-17 23:44:18 +01:00
echo "[ERR] No IPv6 address set"
2014-11-10 22:27:59 +01:00
exitcode=1
fi
if is_radvd_running; then
2014-11-17 23:44:18 +01:00
echo "[OK] Radvd is running"
2014-11-10 22:27:59 +01:00
else
2014-11-17 23:44:18 +01:00
echo "[ERR] Radvd is not running"
2014-11-10 22:27:59 +01:00
exitcode=1
fi
else
2014-11-17 23:44:18 +01:00
echo "[INFO] No IPv6 delegated prefix found"
fi
if is_nat_set "${new_internet_device}"; then
2014-11-17 23:44:18 +01:00
echo "[OK] IPv4 NAT set"
else
2014-11-17 23:44:18 +01:00
echo "[ERR] No IPv4 NAT set"
exitcode=1
fi
if is_ip4nataddr_set; then
2014-11-17 23:44:18 +01:00
echo "[OK] IPv4 NAT address set"
else
2014-11-17 23:44:18 +01:00
echo "[ERR] No IPv4 NAT address set"
exitcode=1
fi
if is_forwarding_set; then
2014-11-17 23:44:18 +01:00
echo "[OK] IPv6/IPv4 forwarding set"
else
2014-11-17 23:44:18 +01:00
echo "[ERR] No IPv6/IPv4 forwarding set"
exitcode=1
fi
if is_hostapd_running; then
2014-11-17 23:44:18 +01:00
echo "[OK] Hostapd is running"
else
2014-11-17 23:44:18 +01:00
echo "[ERR] Hostapd is not running"
exitcode=1
fi
if is_dhcpd_running; then
2014-11-17 23:44:18 +01:00
echo "[OK] Dhcpd is running"
else
2014-11-17 23:44:18 +01:00
echo "[ERR] Dhcpd is not running"
exitcode=1
fi
exit ${exitcode}
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
exit 0