2014-07-06 04:39:50 +00:00
|
|
|
#!/bin/bash
|
2014-06-04 18:13:59 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Run the OpenVPN server normally
|
|
|
|
#
|
|
|
|
|
2015-02-28 10:45:31 +00:00
|
|
|
if [ "$DEBUG" == "1" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
2014-06-04 18:13:59 +00:00
|
|
|
|
2016-05-29 02:39:47 +00:00
|
|
|
cd $OPENVPN
|
|
|
|
|
2015-07-28 03:20:46 +00:00
|
|
|
# Build runtime arguments array based on environment
|
2016-05-29 02:36:02 +00:00
|
|
|
USER_ARGS=("${@}")
|
|
|
|
ARGS=()
|
|
|
|
|
|
|
|
# Checks if ARGS already contains the given value
|
|
|
|
function hasArg {
|
|
|
|
local element
|
|
|
|
for element in "${@:2}"; do
|
|
|
|
[ "${element}" == "${1}" ] && return 0
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Adds the given argument if it's not already specified.
|
|
|
|
function addArg {
|
|
|
|
local arg="${1}"
|
|
|
|
[ $# -ge 1 ] && local val="${2}"
|
|
|
|
if ! hasArg "${arg}" "${USER_ARGS[@]}"; then
|
|
|
|
ARGS+=("${arg}")
|
|
|
|
[ $# -ge 1 ] && ARGS+=("${val}")
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:40:40 +00:00
|
|
|
# set up iptables rules and routing
|
|
|
|
# this allows rules/routing to be altered by supplying this function
|
|
|
|
# in an included file, such as ovpn_env.sh
|
2017-02-14 20:34:24 +00:00
|
|
|
function setupIptablesAndRouting {
|
|
|
|
iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE || {
|
|
|
|
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE
|
|
|
|
}
|
|
|
|
for i in "${OVPN_ROUTES[@]}"; do
|
|
|
|
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE || {
|
|
|
|
iptables -t nat -A POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE
|
|
|
|
}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-29 02:36:02 +00:00
|
|
|
addArg "--config" "$OPENVPN/openvpn.conf"
|
2015-07-28 03:20:46 +00:00
|
|
|
|
2014-07-06 04:39:50 +00:00
|
|
|
source "$OPENVPN/ovpn_env.sh"
|
|
|
|
|
2014-06-04 18:13:59 +00:00
|
|
|
mkdir -p /dev/net
|
|
|
|
if [ ! -c /dev/net/tun ]; then
|
|
|
|
mknod /dev/net/tun c 10 200
|
|
|
|
fi
|
|
|
|
|
2015-07-28 03:20:46 +00:00
|
|
|
if [ -d "$OPENVPN/ccd" ]; then
|
2016-05-29 02:36:02 +00:00
|
|
|
addArg "--client-config-dir" "$OPENVPN/ccd"
|
2014-06-30 06:22:03 +00:00
|
|
|
fi
|
|
|
|
|
2015-08-24 15:19:40 +00:00
|
|
|
# When using --net=host, use this to specify nat device.
|
|
|
|
[ -z "$OVPN_NATDEVICE" ] && OVPN_NATDEVICE=eth0
|
|
|
|
|
2014-07-06 04:39:50 +00:00
|
|
|
# Setup NAT forwarding if requested
|
2015-01-17 09:00:18 +00:00
|
|
|
if [ "$OVPN_DEFROUTE" != "0" ] || [ "$OVPN_NAT" == "1" ] ; then
|
2017-02-14 21:40:40 +00:00
|
|
|
# call function to setup iptables rules and routing
|
|
|
|
# this allows rules to be customized by supplying
|
|
|
|
# a replacement function in, for example, ovpn_env.sh
|
2017-02-14 20:34:24 +00:00
|
|
|
setupIptablesAndRouting
|
2014-07-06 04:39:50 +00:00
|
|
|
fi
|
2014-07-01 05:56:26 +00:00
|
|
|
|
2017-05-02 15:17:01 +00:00
|
|
|
# Use a copy of crl.pem as the CRL Needs to be readable by the user/group
|
2015-05-12 07:59:43 +00:00
|
|
|
# OpenVPN is running as. Only pass arguments to OpenVPN if it's found.
|
2017-05-02 15:17:01 +00:00
|
|
|
if [ "$EASYRSA_PKI/crl.pem" -nt "$OPENVPN/crl.pem" ]; then
|
|
|
|
cp -f "$EASYRSA_PKI/crl.pem" "$OPENVPN/crl.pem"
|
|
|
|
chmod 644 "$OPENVPN/crl.pem"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -r "$OPENVPN/crl.pem" ]; then
|
2016-05-29 02:36:02 +00:00
|
|
|
addArg "--crl-verify" "$OPENVPN/crl.pem"
|
2015-05-12 07:59:43 +00:00
|
|
|
fi
|
|
|
|
|
2015-07-06 04:07:06 +00:00
|
|
|
ip -6 route show default 2>/dev/null
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
echo "Enabling IPv6 Forwarding"
|
|
|
|
# If this fails, ensure the docker container is run with --privileged
|
|
|
|
# Could be side stepped with `ip netns` madness to drop privileged flag
|
|
|
|
|
2017-05-03 14:48:15 +00:00
|
|
|
sysctl -w net.ipv6.conf.all.disable_ipv6=0 || echo "Failed to enable IPv6 support"
|
2015-12-29 21:33:55 +00:00
|
|
|
sysctl -w net.ipv6.conf.default.forwarding=1 || echo "Failed to enable IPv6 Forwarding default"
|
|
|
|
sysctl -w net.ipv6.conf.all.forwarding=1 || echo "Failed to enable IPv6 Forwarding"
|
2015-07-06 04:07:06 +00:00
|
|
|
fi
|
|
|
|
|
2016-05-29 02:36:02 +00:00
|
|
|
echo "Running 'openvpn ${ARGS[@]} ${USER_ARGS[@]}'"
|
|
|
|
exec openvpn ${ARGS[@]} ${USER_ARGS[@]}
|