X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=client-cert-helper;h=9961fc64be0ecf6b487e9c86b3c60c68d1d8e6f0;hb=73e9a029aef15f4135a9ed79d334cc7dde68227e;hp=aedc4cd124f50f1e296147b8946addf94a9d3be2;hpb=50a29b33506900a8bc5d87e67ea0d3fd9bd69369;p=vpn-setup diff --git a/client-cert-helper b/client-cert-helper index aedc4cd..9961fc6 100755 --- a/client-cert-helper +++ b/client-cert-helper @@ -1,30 +1,48 @@ #!/bin/bash set -eE -o pipefail +# Outputs the keyfiles to stdout as tar.gz + rm -f /tmp/vpn-mk-client-cert.log exec 2>/tmp/vpn-mk-client-cert.log + +if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi +shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 +set -eE -o pipefail +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?. PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR + +date >&2 +set -x + name=$1 common_name=$2 -echo common_name=$common_name >&2 - server_dir=/etc/openvpn if [[ -e /etc/openvpn/server ]]; then server_dir=/etc/openvpn/server fi -cafile=$server_dir/ca.crt +cafile=$server_dir/ca-$name.crt -new=true -keyfiles=(/etc/openvpn/easy-rsa/pki/{issued/$common_name.crt,private/$common_name.key}) -if [[ -e /etc/openvpn/easy-rsa/build-ca ]]; then +### begin section roughly copied from vpn-server-setup +rsadir=/etc/openvpn/easy-rsa-$name +new=true # newer easy-rsa version +keyfiles=( + $rsadir/pki/private/$common_name.key + $rsadir/pki/issued/$common_name.crt +) +if [[ -e /etc/openvpn/easy-rsa-$name/build-ca ]]; then new=false - keyfiles=(/etc/openvpn/easy-rsa/keys/$name.{crt,key}) + keyfiles=( + $rsadir/keys/$common_name.key + $rsadir/keys/$common_name.crt + ) fi +### end section roughly copied from vpn-server-setup if [[ ! -e $cafile ]]; then - echo: error no cafile found at $cafile >/tmp/errors + echo error: no cafile found at $cafile >&2 exit 1 fi @@ -38,7 +56,7 @@ done if ! $exists; then - cd /etc/openvpn/easy-rsa + cd /etc/openvpn/easy-rsa-$name if $new; then ./easyrsa build-client-full $common_name nopass >/dev/null else @@ -49,10 +67,10 @@ if ! $exists; then fi d=$(mktemp -d) -cp $cafile $d/$name-ca.crt -cp ${keyfiles[@]} $d - -cp $server_dir/ta.key $d/$name-ta.key +cp $server_dir/ta-$name.key $cafile $d +for f in ${keyfiles[@]}; do + cp $f $d/$name.${f##*.} +done tar cz -C $d . rm -rf $d