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-12 23:32:40 +00:00
>&2 "Unable to find \"${cn}\", please try again or generate the key first" 1>&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
dev tun
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
"
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>
2014-07-06 01:51:58 +00:00
$(cat $EASYRSA_PKI/issued/${cn}.crt)
2014-06-04 18:13:59 +00:00
</cert>
<ca>
$(cat $EASYRSA_PKI/ca.crt)
</ca>
<dh>
$(cat $EASYRSA_PKI/dh.pem)
</dh>
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
"
else
echo "
key ${cn}.key
ca ca.crt
cert ${cn}.crt
dh dh.pem
tls-auth ta.key 1
"
fi
2014-07-06 07:25:14 +00:00
if [ "$OVPN_DEFROUTE" != "0" ];then
echo "redirect-gateway def1"
fi
2015-01-17 09:07:52 +00:00
2015-02-20 19:46:50 +00:00
if [ -n "$OVPN_MTU" ]; then
echo "tun-mtu $OVPN_MTU"
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/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