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
OPENVPN="$PWD"
fi
2014-07-05 18:51:58 -07:00
source "$OPENVPN/ovpn_env.sh"
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-13 00:32:40 +01:00
>&2 "Unable to find \"${cn}\", please try again or generate the key first" 1>&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
"
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>
2014-07-05 18:51:58 -07:00
$(cat $EASYRSA_PKI/issued/${cn}.crt)
2014-06-04 11:13:59 -07:00
</cert>
<ca>
$(cat $EASYRSA_PKI/ca.crt)
</ca>
<dh>
$(cat $EASYRSA_PKI/dh.pem)
</dh>
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
"
else
echo "
key ${cn}.key
ca ca.crt
cert ${cn}.crt
dh dh.pem
tls-auth ta.key 1
"
fi
2014-07-06 00:25:14 -07:00
if [ "$OVPN_DEFROUTE" != "0" ];then
echo "redirect-gateway def1"
fi
2015-01-17 01:07:52 -08:00
2015-02-21 02:46:50 +07: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/dh.pem" "$dir/dh.pem"
cp "$EASYRSA_PKI/ta.key" "$dir/ta.key"
;;
"combined")
get_client_config "combined"
;;
"combined-save")
get_client_config "combined" > "$dir/${cn}-combined.ovpn"
;;
*)
>&2 echo "This script can produce the client configuration in to formats."
>&2 echo " 1. combined: 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."
;;
esac