2014-07-05 21:39:50 -07:00
|
|
|
#!/bin/bash
|
2014-06-04 11:13:59 -07:00
|
|
|
|
|
|
|
#
|
|
|
|
# Run the OpenVPN server normally
|
|
|
|
#
|
|
|
|
|
2015-02-28 02:45:31 -08:00
|
|
|
if [ "$DEBUG" == "1" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
2014-06-04 11:13:59 -07:00
|
|
|
|
2016-05-28 21:39:47 -05:00
|
|
|
cd $OPENVPN
|
|
|
|
|
2015-07-27 20:20:46 -07:00
|
|
|
# Build runtime arguments array based on environment
|
|
|
|
ARGS=("--config" "$OPENVPN/openvpn.conf")
|
|
|
|
|
2014-07-05 21:39:50 -07:00
|
|
|
source "$OPENVPN/ovpn_env.sh"
|
|
|
|
|
2014-06-04 11:13:59 -07:00
|
|
|
mkdir -p /dev/net
|
|
|
|
if [ ! -c /dev/net/tun ]; then
|
|
|
|
mknod /dev/net/tun c 10 200
|
|
|
|
fi
|
|
|
|
|
2015-07-27 20:20:46 -07:00
|
|
|
if [ -d "$OPENVPN/ccd" ]; then
|
|
|
|
ARGS+=("--client-config-dir" "$OPENVPN/ccd")
|
2014-06-29 23:22:03 -07:00
|
|
|
fi
|
|
|
|
|
2015-08-24 17:19:40 +02:00
|
|
|
# When using --net=host, use this to specify nat device.
|
|
|
|
[ -z "$OVPN_NATDEVICE" ] && OVPN_NATDEVICE=eth0
|
|
|
|
|
2014-07-05 21:39:50 -07:00
|
|
|
# Setup NAT forwarding if requested
|
2015-01-17 01:00:18 -08:00
|
|
|
if [ "$OVPN_DEFROUTE" != "0" ] || [ "$OVPN_NAT" == "1" ] ; then
|
2015-08-24 17:19:40 +02:00
|
|
|
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
|
2014-10-23 09:16:51 -04:00
|
|
|
}
|
2014-07-09 10:34:39 -07:00
|
|
|
for i in "${OVPN_ROUTES[@]}"; do
|
2015-08-24 17:19:40 +02:00
|
|
|
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE || {
|
|
|
|
iptables -t nat -A POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE
|
2014-10-23 09:16:51 -04:00
|
|
|
}
|
2014-07-05 21:39:50 -07:00
|
|
|
done
|
|
|
|
fi
|
2014-06-30 22:56:26 -07:00
|
|
|
|
2015-05-12 00:59:43 -07:00
|
|
|
# Use a hacky hardlink as the CRL Needs to be readable by the user/group
|
|
|
|
# OpenVPN is running as. Only pass arguments to OpenVPN if it's found.
|
|
|
|
if [ -r "$EASYRSA_PKI/crl.pem" ]; then
|
|
|
|
if [ ! -r "$OPENVPN/crl.pem" ]; then
|
|
|
|
ln "$EASYRSA_PKI/crl.pem" "$OPENVPN/crl.pem"
|
|
|
|
chmod 644 "$OPENVPN/crl.pem"
|
|
|
|
fi
|
2015-07-27 20:20:46 -07:00
|
|
|
ARGS+=("--crl-verify" "$OPENVPN/crl.pem")
|
2015-05-12 00:59:43 -07:00
|
|
|
fi
|
|
|
|
|
2015-07-05 21:07:06 -07: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
|
|
|
|
|
2015-12-29 13:33:55 -08: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-05 21:07:06 -07:00
|
|
|
fi
|
|
|
|
|
2015-05-09 15:17:17 -07:00
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
exec openvpn "$@"
|
|
|
|
else
|
2015-07-27 20:20:46 -07:00
|
|
|
exec openvpn ${ARGS[@]}
|
2015-05-09 15:17:17 -07:00
|
|
|
fi
|