2013-01-30 20:07:15 +00:00
|
|
|
#!/bin/sh /etc/rc.common
|
2013-10-07 21:28:14 +00:00
|
|
|
# Copyright (C) 2008-2013 OpenWrt.org
|
2013-01-30 20:07:15 +00:00
|
|
|
# Copyright (C) 2008 Jo-Philipp Wich
|
|
|
|
# This is free software, licensed under the GNU General Public License v2.
|
|
|
|
# See /LICENSE for more information.
|
|
|
|
|
2013-10-07 21:28:14 +00:00
|
|
|
START=90
|
|
|
|
STOP=10
|
2013-01-30 20:07:15 +00:00
|
|
|
|
2013-10-07 21:28:14 +00:00
|
|
|
USE_PROCD=1
|
|
|
|
PROG=/usr/sbin/openvpn
|
2013-01-30 20:07:15 +00:00
|
|
|
|
|
|
|
LIST_SEP="
|
|
|
|
"
|
|
|
|
|
2015-02-07 21:01:28 +00:00
|
|
|
UCI_STARTED=
|
|
|
|
UCI_DISABLED=
|
|
|
|
|
2013-01-30 20:07:15 +00:00
|
|
|
append_param() {
|
2013-10-09 11:25:09 +00:00
|
|
|
local s="$1"
|
|
|
|
local v="$2"
|
2013-01-30 20:07:15 +00:00
|
|
|
case "$v" in
|
|
|
|
*_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
|
|
|
|
*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
|
|
|
|
*_*) v=${v%%_*}-${v#*_} ;;
|
|
|
|
esac
|
2013-10-09 11:25:09 +00:00
|
|
|
echo -n "$v" >> "/var/etc/openvpn-$s.conf"
|
2013-01-30 20:07:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
append_bools() {
|
|
|
|
local p; local v; local s="$1"; shift
|
|
|
|
for p in $*; do
|
|
|
|
config_get_bool v "$s" "$p"
|
2014-10-14 12:21:11 +00:00
|
|
|
[ "$v" = 1 ] && append_param "$s" "$p" && echo >> "/var/etc/openvpn-$s.conf"
|
2013-01-30 20:07:15 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
append_params() {
|
|
|
|
local p; local v; local s="$1"; shift
|
|
|
|
for p in $*; do
|
|
|
|
config_get v "$s" "$p"
|
|
|
|
IFS="$LIST_SEP"
|
|
|
|
for v in $v; do
|
2016-12-10 11:02:03 +00:00
|
|
|
[ -n "$v" ] && [ "$p" != "push" ] && append_param "$s" "$p" && echo " $v" >> "/var/etc/openvpn-$s.conf"
|
|
|
|
[ -n "$v" ] && [ "$p" == "push" ] && append_param "$s" "$p" && echo " \"$v\"" >> "/var/etc/openvpn-$s.conf"
|
2013-01-30 20:07:15 +00:00
|
|
|
done
|
|
|
|
unset IFS
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
section_enabled() {
|
|
|
|
config_get_bool enable "$1" 'enable' 0
|
|
|
|
config_get_bool enabled "$1" 'enabled' 0
|
|
|
|
[ $enable -gt 0 ] || [ $enabled -gt 0 ]
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:36:24 +00:00
|
|
|
openvpn_add_instance() {
|
|
|
|
local name="$1"
|
|
|
|
local dir="$2"
|
|
|
|
local conf="$3"
|
|
|
|
|
|
|
|
procd_open_instance
|
|
|
|
procd_set_param command "$PROG" \
|
|
|
|
--syslog "openvpn($name)" \
|
2015-02-07 21:01:28 +00:00
|
|
|
--status "/var/run/openvpn.$name.status" \
|
2013-10-31 17:36:24 +00:00
|
|
|
--cd "$dir" \
|
|
|
|
--config "$conf"
|
|
|
|
procd_set_param file "$dir/$conf"
|
2015-01-08 20:26:41 +00:00
|
|
|
procd_set_param respawn
|
2013-10-31 17:36:24 +00:00
|
|
|
procd_close_instance
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:07:15 +00:00
|
|
|
start_instance() {
|
|
|
|
local s="$1"
|
|
|
|
|
2013-10-09 11:25:09 +00:00
|
|
|
config_get config "$s" config
|
2015-02-07 21:01:28 +00:00
|
|
|
config="${config:+$(readlink -f "$config")}"
|
|
|
|
|
|
|
|
section_enabled "$s" || {
|
|
|
|
append UCI_DISABLED "$config" "$LIST_SEP"
|
|
|
|
return 1
|
|
|
|
}
|
2013-01-30 20:07:15 +00:00
|
|
|
|
2013-10-09 11:25:09 +00:00
|
|
|
[ ! -d "/var/run" ] && mkdir -p "/var/run"
|
|
|
|
|
|
|
|
if [ ! -z "$config" ]; then
|
2015-02-07 21:01:28 +00:00
|
|
|
append UCI_STARTED "$config" "$LIST_SEP"
|
|
|
|
openvpn_add_instance "$s" "${config%/*}" "$config"
|
2013-10-09 11:25:09 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ ! -d "/var/etc" ] && mkdir -p "/var/etc"
|
|
|
|
[ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
|
2013-10-07 21:28:14 +00:00
|
|
|
|
2017-02-26 07:22:04 +00:00
|
|
|
append_bools "$s" $OPENVPN_BOOLS
|
|
|
|
append_params "$s" $OPENVPN_PARAMS
|
2013-01-30 20:07:15 +00:00
|
|
|
|
2013-10-31 17:36:24 +00:00
|
|
|
openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf"
|
2013-01-30 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 21:28:14 +00:00
|
|
|
start_service() {
|
2017-02-26 07:22:04 +00:00
|
|
|
. /usr/share/openvpn/openvpn.options
|
2013-01-30 20:07:15 +00:00
|
|
|
config_load 'openvpn'
|
|
|
|
config_foreach start_instance 'openvpn'
|
2015-02-07 21:01:28 +00:00
|
|
|
|
|
|
|
local path name
|
|
|
|
for path in /etc/openvpn/*.conf; do
|
|
|
|
if [ -f "$path" ]; then
|
|
|
|
name="${path##*/}"; name="${name%.conf}"
|
|
|
|
|
|
|
|
# don't start configs again that are already started by uci
|
|
|
|
if echo "$UCI_STARTED" | grep -qxF "$path"; then
|
|
|
|
continue
|
|
|
|
|
|
|
|
# don't start configs which are set to disabled in uci
|
|
|
|
elif echo "$UCI_DISABLED" | grep -qxF "$path"; then
|
|
|
|
logger -t openvpn "$name.conf is disabled in /etc/config/openvpn"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
openvpn_add_instance "$name" "${path%/*}" "$path"
|
|
|
|
fi
|
|
|
|
done
|
2013-01-30 20:07:15 +00:00
|
|
|
}
|
2016-01-07 21:08:05 +00:00
|
|
|
|
|
|
|
service_triggers() {
|
|
|
|
procd_add_reload_trigger openvpn
|
|
|
|
}
|