systemd: migrate the SySv init script to a simple bash script
This commit is contained in:
396
conf/ynh-vpnclient
Normal file
396
conf/ynh-vpnclient
Normal file
@@ -0,0 +1,396 @@
|
||||
#!/bin/bash
|
||||
### BEGIN INIT INFO
|
||||
# Provides: ynh-vpnclient
|
||||
# Required-Start: $network $remote_fs $syslog yunohost-api
|
||||
# Required-Stop: $network $remote_fs $syslog
|
||||
# Should-Start: ynh-hotspot
|
||||
# Should-Stop: ynh-hotspot
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop: 0 1 6
|
||||
# Short-Description: Start VPN client.
|
||||
# Description: Start VPN client.
|
||||
### END INIT INFO
|
||||
|
||||
# VPN Client app for YunoHost
|
||||
# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
|
||||
# Contribute at https://github.com/jvaubourg/vpnclient_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/>.
|
||||
|
||||
# Functions
|
||||
## State functions
|
||||
|
||||
has_nativeip6() {
|
||||
ip -6 route | grep -q default\ via
|
||||
}
|
||||
|
||||
has_ip6delegatedprefix() {
|
||||
[ "${ynh_ip6_addr}" != none ]
|
||||
}
|
||||
|
||||
has_hotspot_app() {
|
||||
[ -e /tmp/.ynh-hotspot-started ]
|
||||
}
|
||||
|
||||
is_hotspot_knowme() {
|
||||
value=$(yunohost app setting hotspot vpnclient)
|
||||
|
||||
if [[ "${value}" =~ "An instance is already running" ]]; then
|
||||
echo "${value}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
[ "${value}" == yes ]
|
||||
}
|
||||
|
||||
is_ip6addr_set() {
|
||||
ip address show dev tun0 2> /dev/null | grep -q "${ynh_ip6_addr}/128"
|
||||
}
|
||||
|
||||
is_serverip6route_set() {
|
||||
server_ip6=${1}
|
||||
|
||||
if [ -z "${server_ip6}" ]; then
|
||||
false
|
||||
else
|
||||
ip -6 route | grep -q "${server_ip6}/"
|
||||
fi
|
||||
}
|
||||
|
||||
is_openvpn_running() {
|
||||
systemctl is-active openvpn@client.service --quiet &> /dev/null
|
||||
}
|
||||
|
||||
is_running() {
|
||||
((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
|
||||
&& ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app || ! has_ip6delegatedprefix)\
|
||||
&& is_openvpn_running
|
||||
}
|
||||
|
||||
## Setters
|
||||
|
||||
set_ip6addr() {
|
||||
ip address add "${ynh_ip6_addr}/128" dev tun0
|
||||
}
|
||||
|
||||
set_serverip6route() {
|
||||
server_ip6=${1}
|
||||
ip6_gw=${2}
|
||||
wired_device=${3}
|
||||
|
||||
ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
|
||||
}
|
||||
|
||||
start_openvpn() {
|
||||
ip6_gw=${1}
|
||||
server_ip6=${2}
|
||||
|
||||
if [ ! -z "${ip6_gw}" -a ! -z "${server_ip6}" ]; then
|
||||
proto=udp6
|
||||
[ "${ynh_server_proto}" == tcp ] && proto=tcp6-client
|
||||
else
|
||||
proto=udp
|
||||
[ "${ynh_server_proto}" == tcp ] && proto=tcp-client
|
||||
fi
|
||||
|
||||
cp /etc/openvpn/client.conf{.tpl,}
|
||||
|
||||
sed "s|<TPL:SERVER_NAME>|${ynh_server_name}|g" -i /etc/openvpn/client.conf
|
||||
sed "s|<TPL:SERVER_PORT>|${ynh_server_port}|g" -i /etc/openvpn/client.conf
|
||||
sed "s|<TPL:PROTO>|${proto}|g" -i /etc/openvpn/client.conf
|
||||
|
||||
if [ -e /etc/openvpn/keys/user.key ]; then
|
||||
sed 's|^<TPL:CERT_COMMENT>||' -i /etc/openvpn/client.conf
|
||||
else
|
||||
sed 's|^<TPL:CERT_COMMENT>|;|' -i /etc/openvpn/client.conf
|
||||
fi
|
||||
|
||||
if [[ "${proto}" =~ udp ]]; then
|
||||
sed 's|^<TPL:UDP_COMMENT>||' -i /etc/openvpn/client.conf
|
||||
else
|
||||
sed 's|^<TPL:UDP_COMMENT>|;|' -i /etc/openvpn/client.conf
|
||||
fi
|
||||
|
||||
if [ -z "${ynh_login_user}" ]; then
|
||||
sed 's|^<TPL:LOGIN_COMMENT>|;|' -i /etc/openvpn/client.conf
|
||||
else
|
||||
sed 's|^<TPL:LOGIN_COMMENT>||' -i /etc/openvpn/client.conf
|
||||
fi
|
||||
|
||||
systemctl start openvpn@client.service --quiet
|
||||
}
|
||||
|
||||
## Unsetters
|
||||
|
||||
unset_ip6addr() {
|
||||
ip address delete "${ynh_ip6_addr}/128" dev tun0
|
||||
}
|
||||
|
||||
unset_serverip6route() {
|
||||
server_ip6=${1}
|
||||
ip6_gw=${2}
|
||||
wired_device=${3}
|
||||
|
||||
ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
|
||||
}
|
||||
|
||||
stop_openvpn() {
|
||||
systemctl stop openvpn.service --quiet
|
||||
}
|
||||
|
||||
## Tools
|
||||
|
||||
moulinette_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 vpnclient "${var}" -v "${value}")
|
||||
|
||||
if [ ! $? -eq 0 ]; then
|
||||
echo "${msg}" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "$1" != restart ]; then
|
||||
|
||||
# Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
|
||||
if [ ! -e /tmp/.ynh-vpnclient-boot ]; then
|
||||
touch /tmp/.ynh-vpnclient-boot
|
||||
systemctl restart php5-fpm --quiet
|
||||
fi
|
||||
|
||||
# Check configuration consistency
|
||||
|
||||
if [[ ! "${1}" =~ stop ]]; then
|
||||
exitcode=0
|
||||
|
||||
if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then
|
||||
echo "[WARN] You need a CA server (you can add it through the web admin)"
|
||||
exitcode=1
|
||||
fi
|
||||
|
||||
empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l)
|
||||
if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then
|
||||
echo "[WARN] You need either a client certificate, either a username, or both (you can add one through the web admin)"
|
||||
exitcode=1
|
||||
fi
|
||||
|
||||
[ "${exitcode}" -ne 0 ] && exit ${exitcode}
|
||||
fi
|
||||
|
||||
# Variables
|
||||
|
||||
echo -n "Retrieving Yunohost settings... "
|
||||
|
||||
ynh_service_enabled=$(moulinette_get service_enabled)
|
||||
ynh_server_name=$(moulinette_get server_name)
|
||||
ynh_server_port=$(moulinette_get server_port)
|
||||
ynh_server_proto=$(moulinette_get server_proto)
|
||||
ynh_ip6_addr=$(moulinette_get ip6_addr)
|
||||
ynh_login_user=$(moulinette_get login_user)
|
||||
|
||||
old_ip6_gw=$(moulinette_get ip6_gw)
|
||||
old_wired_device=$(moulinette_get wired_device)
|
||||
old_server_ip6=$(moulinette_get server_ip6)
|
||||
|
||||
new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
|
||||
new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
|
||||
new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }')
|
||||
|
||||
if [ -z "${new_server_ip6}" ]; then
|
||||
new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }')
|
||||
fi
|
||||
|
||||
echo "OK"
|
||||
|
||||
fi
|
||||
|
||||
# Script
|
||||
|
||||
case "${1}" in
|
||||
start)
|
||||
if is_running; then
|
||||
echo "Already started"
|
||||
elif [ "${ynh_service_enabled}" -eq 0 ]; then
|
||||
echo "Disabled service"
|
||||
else
|
||||
echo "[vpnclient] Starting..."
|
||||
touch /tmp/.ynh-vpnclient-started
|
||||
|
||||
# Run openvpn
|
||||
if ! is_openvpn_running; then
|
||||
echo "Run openvpn"
|
||||
|
||||
start_openvpn "${new_ip6_gw}" "${new_server_ip6}"
|
||||
|
||||
if [ ! $? -eq 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
i=0; false || while [ $? -ne 0 ]; do
|
||||
sleep 1 && (( i++ ))
|
||||
[ ${i} -gt 20 ] && stop_openvpn
|
||||
[ ${i} -gt 20 ] && exit 1
|
||||
ip link show dev tun0 &> /dev/null
|
||||
done
|
||||
fi
|
||||
|
||||
# Check old state of the server ipv6 route
|
||||
if [ ! -z "${old_server_ip6}" -a ! -z "${old_ip6_gw}" -a ! -z "${old_wired_device}"\
|
||||
-a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
|
||||
-o "${new_wired_device}" != "${old_wired_device}" \) ]\
|
||||
&& is_serverip6route_set "${old_server_ip6}"; then
|
||||
|
||||
unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
|
||||
fi
|
||||
|
||||
# Set the new server ipv6 route
|
||||
if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"; then
|
||||
echo "Set IPv6 server route"
|
||||
set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
|
||||
fi
|
||||
|
||||
# Set the ipv6 address
|
||||
if ! has_hotspot_app && has_ip6delegatedprefix && ! is_ip6addr_set; then
|
||||
echo "Set IPv6 address"
|
||||
set_ip6addr
|
||||
fi
|
||||
|
||||
# Update dynamic settings
|
||||
moulinette_set server_ip6 "${new_server_ip6}"
|
||||
moulinette_set ip6_gw "${new_ip6_gw}"
|
||||
moulinette_set wired_device "${new_wired_device}"
|
||||
|
||||
# Restart dhcpd
|
||||
systemctl stop bind9 --quiet &> /dev/null
|
||||
systemctl restart dnsmasq --quiet
|
||||
|
||||
# Restart hotspot if needed
|
||||
if has_hotspot_app && ! is_hotspot_knowme; then
|
||||
systemctl start ynh-hotspot --quiet
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
stop)
|
||||
echo "[vpnclient] Stopping..."
|
||||
rm -f /tmp/.ynh-vpnclient-started
|
||||
|
||||
if ! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set; then
|
||||
echo "Unset IPv6 address"
|
||||
unset_ip6addr
|
||||
fi
|
||||
|
||||
if is_serverip6route_set "${old_server_ip6}"; then
|
||||
echo "Unset IPv6 server route"
|
||||
unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
|
||||
fi
|
||||
|
||||
if is_openvpn_running; then
|
||||
echo "Stop openvpn"
|
||||
stop_openvpn
|
||||
|
||||
i=0; true && while [ $? -eq 0 ]; do
|
||||
sleep 1 && (( i++ ))
|
||||
[ ${i} -gt 20 ] && exit 1
|
||||
ip link show dev tun0 &> /dev/null
|
||||
done
|
||||
fi
|
||||
|
||||
if has_hotspot_app && is_hotspot_knowme; then
|
||||
systemctl start ynh-hotspot --quiet
|
||||
fi
|
||||
|
||||
systemctl restart dnsmasq --quiet
|
||||
;;
|
||||
restart)
|
||||
$0 stop
|
||||
$0 start
|
||||
;;
|
||||
status)
|
||||
exitcode=0
|
||||
|
||||
if [ "${ynh_service_enabled}" -eq 0 ]; then
|
||||
echo "[ERR] VPN Client Service disabled"
|
||||
exitcode=1
|
||||
fi
|
||||
|
||||
echo "[INFO] Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
|
||||
echo "[INFO] Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
|
||||
|
||||
if has_ip6delegatedprefix; then
|
||||
echo "[INFO] IPv6 delegated prefix found"
|
||||
echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
|
||||
|
||||
if ! has_hotspot_app; then
|
||||
echo "[INFO] No Hotspot app detected"
|
||||
|
||||
if is_ip6addr_set; then
|
||||
echo "[OK] IPv6 address correctly set"
|
||||
else
|
||||
echo "[ERR] No IPv6 address set"
|
||||
exitcode=1
|
||||
fi
|
||||
else
|
||||
echo "[INFO] Hotspot app detected"
|
||||
echo "[INFO] No IPv6 address to set"
|
||||
fi
|
||||
else
|
||||
echo "[INFO] No IPv6 delegated prefix found"
|
||||
fi
|
||||
|
||||
if has_nativeip6; then
|
||||
echo "[INFO] Native IPv6 detected"
|
||||
echo "[INFO] Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
|
||||
|
||||
if is_serverip6route_set "${new_server_ip6}"; then
|
||||
echo "[OK] IPv6 server route correctly set"
|
||||
else
|
||||
echo "[ERR] No IPv6 server route set"
|
||||
exitcode=1
|
||||
fi
|
||||
else
|
||||
echo "[INFO] No native IPv6 detected"
|
||||
echo "[INFO] No IPv6 server route to set"
|
||||
fi
|
||||
|
||||
if is_openvpn_running; then
|
||||
echo "[OK] Openvpn is running"
|
||||
else
|
||||
echo "[ERR] Openvpn is not running"
|
||||
exitcode=1
|
||||
fi
|
||||
|
||||
exit ${exitcode}
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|litestop|restart|status}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
Reference in New Issue
Block a user