2014-07-01 05:43:00 +00:00
|
|
|
#!/bin/bash
|
2014-06-04 23:49:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Generate OpenVPN configs
|
|
|
|
#
|
|
|
|
|
2014-07-06 04:16:02 +00:00
|
|
|
# Convert 1.2.3.4/24 -> 255.255.255.0
|
|
|
|
cidr2mask()
|
|
|
|
{
|
|
|
|
local i
|
|
|
|
local subnetmask=""
|
|
|
|
local cidr=${1#*/}
|
|
|
|
local full_octets=$(($cidr/8))
|
|
|
|
local partial_octet=$(($cidr%8))
|
|
|
|
|
|
|
|
for ((i=0;i<4;i+=1)); do
|
|
|
|
if [ $i -lt $full_octets ]; then
|
|
|
|
subnetmask+=255
|
|
|
|
elif [ $i -eq $full_octets ]; then
|
|
|
|
subnetmask+=$((256 - 2**(8-$partial_octet)))
|
|
|
|
else
|
|
|
|
subnetmask+=0
|
|
|
|
fi
|
|
|
|
[ $i -lt 3 ] && subnetmask+=.
|
|
|
|
done
|
|
|
|
echo $subnetmask
|
|
|
|
}
|
|
|
|
|
|
|
|
# Used often enough to justify a function
|
|
|
|
getroute() {
|
|
|
|
echo ${1%/*} $(cidr2mask $1)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "usage: $0 [-d]"
|
|
|
|
echo " -u SERVER_PUBLIC_URL"
|
|
|
|
echo " [-s SERVER_SUBNET]"
|
|
|
|
echo " [-r ROUTE ...]"
|
|
|
|
echo
|
|
|
|
echo "optional arguments:"
|
|
|
|
echo " -d Disable NAT routing and default route"
|
|
|
|
}
|
|
|
|
|
2014-06-04 23:49:13 +00:00
|
|
|
set -ex
|
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
OVPN_ENV=$OPENVPN/ovpn_env.sh
|
2014-07-06 04:16:02 +00:00
|
|
|
OVPN_SERVER=192.168.255.0/24
|
|
|
|
OVPN_DEFROUTE=1
|
2014-06-04 23:49:13 +00:00
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
# Import defaults if present
|
|
|
|
[ -r "$OVPN_ENV" ] && source "$OVPN_ENV"
|
|
|
|
|
2014-07-06 04:56:04 +00:00
|
|
|
ORIG_OVPN_ROUTES=$OVPN_ROUTES
|
|
|
|
OVPN_ROUTES=""
|
|
|
|
|
2014-07-06 04:16:02 +00:00
|
|
|
# Parse arguments
|
|
|
|
while getopts ":r:s:du:" opt; do
|
|
|
|
case $opt in
|
|
|
|
r)
|
2014-07-06 04:56:04 +00:00
|
|
|
if [ -n "$OVPN_ROUTES" ]; then
|
|
|
|
OVPN_ROUTES+=" $OPTARG"
|
|
|
|
else
|
|
|
|
OVPN_ROUTES+="$OPTARG"
|
|
|
|
fi
|
2014-07-06 04:16:02 +00:00
|
|
|
;;
|
|
|
|
s)
|
|
|
|
OVPN_SERVER=$OPTARG
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
OVPN_DEFROUTE=0
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
OVPN_SERVER_URL=$OPTARG
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
set +x
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
set +x
|
|
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
|
|
|
|
# Server name is in the form "udp://vpn.example.com:1194"
|
|
|
|
if [[ "$OVPN_SERVER_URL" =~ ^((udp|tcp)://)?([0-9a-zA-Z\.]+)(:([0-9]+))?$ ]]; then
|
|
|
|
OVPN_PROTO=${BASH_REMATCH[2]};
|
|
|
|
OVPN_CN=${BASH_REMATCH[3]};
|
|
|
|
OVPN_PORT=${BASH_REMATCH[5]};
|
2014-07-01 05:43:00 +00:00
|
|
|
else
|
2014-07-06 04:16:02 +00:00
|
|
|
set +x
|
|
|
|
echo "Common name not specified, see '-u'"
|
|
|
|
usage
|
2014-06-04 23:49:13 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-07-01 05:43:00 +00:00
|
|
|
# Apply defaults
|
2014-07-06 01:51:58 +00:00
|
|
|
[ -z "$OVPN_PROTO" ] && OVPN_PROTO=udp
|
|
|
|
[ -z "$OVPN_PORT" ] && OVPN_PORT=1194
|
2014-07-06 04:56:04 +00:00
|
|
|
|
|
|
|
if [ -z "$OVPN_ROUTES" ]; then
|
|
|
|
if [ -n "$ORIG_OVPN_ROUTES" ]; then
|
|
|
|
OVPN_ROUTES=$ORIG_OVPN_ROUTES
|
|
|
|
else
|
|
|
|
OVPN_ROUTES=192.168.254.0/24
|
|
|
|
fi
|
|
|
|
fi
|
2014-07-06 01:51:58 +00:00
|
|
|
|
2014-07-06 04:16:02 +00:00
|
|
|
export OVPN_SERVER OVPN_ROUTES OVPN_DEFROUTE
|
|
|
|
export OVPN_SERVER_URL OVPN_ENV OVPN_PROTO OVPN_CN OVPN_PORT
|
2014-07-06 01:51:58 +00:00
|
|
|
|
|
|
|
# Preserve config
|
|
|
|
if [ -f "$OVPN_ENV" ]; then
|
|
|
|
bak_env=$OVPN_ENV.$(date +%s).bak
|
|
|
|
echo "Backing up $OVPN_ENV -> $bak_env"
|
|
|
|
mv "$OVPN_ENV" "$bak_env"
|
|
|
|
fi
|
2014-07-06 04:16:02 +00:00
|
|
|
export | grep OVPN_ > "$OVPN_ENV"
|
2014-07-01 05:43:00 +00:00
|
|
|
|
2014-07-01 05:56:26 +00:00
|
|
|
conf=$OPENVPN/openvpn.conf
|
2014-06-30 06:09:18 +00:00
|
|
|
if [ -f "$conf" ]; then
|
|
|
|
bak=$conf.$(date +%s).bak
|
|
|
|
echo "Backing up $conf -> $bak"
|
|
|
|
mv "$conf" "$bak"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat > "$conf" <<EOF
|
2014-07-06 04:16:02 +00:00
|
|
|
server $(getroute $OVPN_SERVER)
|
2014-06-04 23:49:13 +00:00
|
|
|
verb 3
|
|
|
|
#duplicate-cn
|
2014-07-06 04:16:02 +00:00
|
|
|
key $EASYRSA_PKI/private/${OVPN_CN}.key
|
2014-06-04 23:49:13 +00:00
|
|
|
ca $EASYRSA_PKI/ca.crt
|
2014-07-06 04:16:02 +00:00
|
|
|
cert $EASYRSA_PKI/issued/${OVPN_CN}.crt
|
2014-06-04 23:49:13 +00:00
|
|
|
dh $EASYRSA_PKI/dh.pem
|
|
|
|
tls-auth $EASYRSA_PKI/ta.key
|
|
|
|
key-direction 0
|
|
|
|
keepalive 10 60
|
|
|
|
persist-key
|
|
|
|
persist-tun
|
|
|
|
push "dhcp-option DNS 8.8.4.4"
|
|
|
|
push "dhcp-option DNS 8.8.8.8"
|
2014-06-30 07:10:52 +00:00
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
proto $OVPN_PROTO
|
|
|
|
port $OVPN_PORT
|
2014-07-01 05:56:26 +00:00
|
|
|
dev tun0
|
|
|
|
status /tmp/openvpn-status.log
|
2014-07-06 04:16:02 +00:00
|
|
|
|
|
|
|
client-config-dir $OPENVPN/ccd
|
2014-06-04 23:49:13 +00:00
|
|
|
EOF
|
2014-07-01 15:30:28 +00:00
|
|
|
|
2014-07-06 04:16:02 +00:00
|
|
|
# Append Routes
|
|
|
|
for i in ${OVPN_ROUTES[@]}; do
|
|
|
|
echo route $(getroute $i) >> "$conf"
|
|
|
|
done
|
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
# Clean-up duplicate configs (always return success)
|
|
|
|
diff -q "$bak_env" "$OVPN_ENV" 2> /dev/null && rm "$bak_env" || true
|
|
|
|
diff -q "$bak" "$conf" 2> /dev/null && rm "$bak" || true
|