2014-06-30 22:43:00 -07:00
#!/bin/bash
2014-06-04 11:13:59 -07:00
#
# Get an OpenVPN client configuration file
#
2015-02-28 02:45:31 -08:00
if [ "$DEBUG" == "1" ]; then
2015-03-13 00:32:40 +01:00
set -x
2015-02-28 02:45:31 -08:00
fi
set -e
2014-06-04 11:13:59 -07:00
2015-03-13 00:32:40 +01:00
if [ -z "$OPENVPN" ]; then
2015-03-13 00:43:50 +01:00
export OPENVPN="$PWD"
2015-03-13 00:32:40 +01:00
fi
2015-03-13 00:43:50 +01:00
if ! source "$OPENVPN/ovpn_env.sh"; then
echo "Could not source $OPENVPN/ovpn_env.sh."
exit 1
fi
if [ -z "$EASYRSA_PKI" ]; then
export EASYRSA_PKI="$OPENVPN/pki"
fi
2015-03-13 00:32:40 +01:00
cn="$1"
parm="$2"
2014-06-30 22:43:00 -07:00
2014-07-05 18:51:58 -07:00
if [ ! -f "$EASYRSA_PKI/private/${cn}.key" ]; then
2015-03-14 13:22:28 +01:00
echo "Unable to find \"${cn}\", please try again or generate the key first" >&2
2014-07-10 09:53:24 -07:00
exit 1
2014-06-04 11:13:59 -07:00
fi
2015-03-13 00:32:40 +01:00
get_client_config() {
mode="$1"
echo "
2014-06-04 11:13:59 -07:00
client
nobind
dev tun
2014-06-04 15:38:49 -07:00
remote-cert-tls server
2014-06-04 11:13:59 -07:00
2015-03-13 00:32:40 +01:00
remote $OVPN_CN $OVPN_PORT $OVPN_PROTO
"
2015-03-14 13:22:28 +01:00
if [ "$mode" == "combined" ]; then
echo "
2014-06-04 11:13:59 -07:00
<key>
2014-07-05 18:51:58 -07:00
$(cat $EASYRSA_PKI/private/${cn}.key)
2014-06-04 11:13:59 -07:00
</key>
<cert>
2015-07-05 21:07:04 -07:00
$(openssl x509 -in $EASYRSA_PKI/issued/${cn}.crt)
2014-06-04 11:13:59 -07:00
</cert>
<ca>
$(cat $EASYRSA_PKI/ca.crt)
</ca>
2014-06-04 15:34:42 -07:00
<tls-auth>
$(cat $EASYRSA_PKI/ta.key)
</tls-auth>
key-direction 1
2015-03-13 00:32:40 +01:00
"
2015-03-14 13:22:28 +01:00
elif [ "$mode" == "separated" ]; then
echo "
2015-03-13 00:32:40 +01:00
key ${cn}.key
ca ca.crt
cert ${cn}.crt
tls-auth ta.key 1
2015-05-30 23:03:17 +02:00
$OVPN_ADDITIONAL_CLIENT_CONFIG
2015-03-13 00:32:40 +01:00
"
2015-03-14 13:22:28 +01:00
fi
2014-07-06 00:25:14 -07:00
2015-03-14 13:22:28 +01:00
if [ "$OVPN_DEFROUTE" != "0" ];then
echo "redirect-gateway def1"
fi
2015-01-17 01:07:52 -08:00
2015-03-14 13:22:28 +01:00
if [ -n "$OVPN_MTU" ]; then
echo "tun-mtu $OVPN_MTU"
fi
2015-03-13 00:32:40 +01:00
}
dir="$OPENVPN/clients/$cn"
case "$parm" in
"separated")
mkdir -p "$dir"
get_client_config "$parm" > "$dir/${cn}.ovpn"
cp "$EASYRSA_PKI/private/${cn}.key" "$dir/${cn}.key"
cp "$EASYRSA_PKI/ca.crt" "$dir/ca.crt"
cp "$EASYRSA_PKI/issued/${cn}.crt" "$dir/${cn}.crt"
cp "$EASYRSA_PKI/ta.key" "$dir/ta.key"
;;
2015-03-14 13:22:28 +01:00
"" | "combined")
2015-03-13 00:32:40 +01:00
get_client_config "combined"
;;
"combined-save")
get_client_config "combined" > "$dir/${cn}-combined.ovpn"
;;
*)
2015-03-14 13:22:28 +01:00
echo "This script can produce the client configuration in to formats:" >&2
echo " 1. combined (default): All needed configuration and cryptographic material is in one file (Use \"combined-save\" to write the configuration file in the same path as the separated parameter does)." >&2
echo " 2. separated: Separated files." >&2
echo "Please specific one of those options as second parameter." >&2
2015-03-13 00:32:40 +01:00
;;
esac