2014-07-01 05:43:00 +00:00
#!/bin/bash
2014-06-04 18:13:59 +00:00
#
# Get an OpenVPN client configuration file
#
2015-02-28 10:45:31 +00:00
if [ "$DEBUG" == "1" ]; then
2015-03-12 23:32:40 +00:00
set -x
2015-02-28 10:45:31 +00:00
fi
set -e
2014-06-04 18:13:59 +00:00
2015-03-12 23:32:40 +00:00
if [ -z "$OPENVPN" ]; then
2015-03-12 23:43:50 +00:00
export OPENVPN="$PWD"
2015-03-12 23:32:40 +00:00
fi
2015-03-12 23:43:50 +00: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-12 23:32:40 +00:00
cn="$1"
parm="$2"
2014-07-01 05:43:00 +00:00
2014-07-06 01:51:58 +00:00
if [ ! -f "$EASYRSA_PKI/private/${cn}.key" ]; then
2015-03-14 12:22:28 +00:00
echo "Unable to find \"${cn}\", please try again or generate the key first" >&2
2014-07-10 16:53:24 +00:00
exit 1
2014-06-04 18:13:59 +00:00
fi
2015-03-12 23:32:40 +00:00
get_client_config() {
mode="$1"
echo "
2014-06-04 18:13:59 +00:00
client
nobind
2015-08-18 22:46:07 +00:00
dev $OVPN_DEVICE
2016-06-13 10:09:54 +00:00
key-direction 1
2014-06-04 22:38:49 +00:00
remote-cert-tls server
2014-06-04 18:13:59 +00:00
2015-03-12 23:32:40 +00:00
remote $OVPN_CN $OVPN_PORT $OVPN_PROTO
"
2015-03-14 12:22:28 +00:00
if [ "$mode" == "combined" ]; then
echo "
2014-06-04 18:13:59 +00:00
<key>
2014-07-06 01:51:58 +00:00
$(cat $EASYRSA_PKI/private/${cn}.key)
2014-06-04 18:13:59 +00:00
</key>
<cert>
2015-07-06 04:07:04 +00:00
$(openssl x509 -in $EASYRSA_PKI/issued/${cn}.crt)
2014-06-04 18:13:59 +00:00
</cert>
<ca>
$(cat $EASYRSA_PKI/ca.crt)
</ca>
2014-06-04 22:34:42 +00:00
<tls-auth>
$(cat $EASYRSA_PKI/ta.key)
</tls-auth>
key-direction 1
2015-03-12 23:32:40 +00:00
"
2015-03-14 12:22:28 +00:00
elif [ "$mode" == "separated" ]; then
echo "
2015-03-12 23:32:40 +00:00
key ${cn}.key
ca ca.crt
cert ${cn}.crt
tls-auth ta.key 1
2015-05-30 21:03:17 +00:00
$OVPN_ADDITIONAL_CLIENT_CONFIG
2015-03-12 23:32:40 +00:00
"
2015-03-14 12:22:28 +00:00
fi
2014-07-06 07:25:14 +00:00
2015-03-14 12:22:28 +00:00
if [ "$OVPN_DEFROUTE" != "0" ];then
echo "redirect-gateway def1"
fi
2015-01-17 09:07:52 +00:00
2015-03-14 12:22:28 +00:00
if [ -n "$OVPN_MTU" ]; then
echo "tun-mtu $OVPN_MTU"
fi
2015-08-26 10:43:25 +00:00
if [ -n "$OVPN_TLS_CIPHER" ]; then
echo "tls-cipher $OVPN_TLS_CIPHER"
fi
if [ -n "$OVPN_CIPHER" ]; then
echo "cipher $OVPN_CIPHER"
fi
if [ -n "$OVPN_AUTH" ]; then
echo "auth $OVPN_AUTH"
fi
2015-11-27 14:03:35 +00:00
2016-02-06 20:40:11 +00:00
if [ -n "$OVPN_OTP_AUTH" ]; then
echo "auth-user-pass"
2016-02-07 01:53:43 +00:00
echo "auth-nocache"
2016-02-06 20:40:11 +00:00
fi
2015-11-27 14:03:35 +00:00
if [ -n "$OVPN_COMP_LZO" ]; then
echo "comp-lzo"
fi
2017-01-24 14:37:48 +00:00
if [ "$OVPN_OTP_AUTH" = "1" ]; then
echo reneg-sec 0
fi
2015-03-12 23:32:40 +00: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 12:22:28 +00:00
"" | "combined")
2015-03-12 23:32:40 +00:00
get_client_config "combined"
;;
"combined-save")
2017-01-12 11:49:24 +00:00
mkdir -p "$dir"
2015-03-12 23:32:40 +00:00
get_client_config "combined" > "$dir/${cn}-combined.ovpn"
;;
*)
2015-03-14 12:22:28 +00: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-12 23:32:40 +00:00
;;
esac