X-Git-Url: https://iankelling.org/git/?p=vpn-setup;a=blobdiff_plain;f=vpn-mk-client-cert;h=1aa9b4166aca033059b65b25328c829a68677b15;hp=440cfe919049d2d1389cbedda3d9a8fe49c3a9e8;hb=HEAD;hpb=b6a5ea4d79dbed159eaa7ba9050fcfe1fcb6d274 diff --git a/vpn-mk-client-cert b/vpn-mk-client-cert index 440cfe9..7bad544 100755 --- a/vpn-mk-client-cert +++ b/vpn-mk-client-cert @@ -1,5 +1,12 @@ #!/bin/bash -# Copyright (C) 2016 Ian Kelling +# I, Ian Kelling, follow the GNU license recommendations at +# https://www.gnu.org/licenses/license-recommendations.en.html. They +# recommend that small programs, < 300 lines, be licensed under the +# Apache License 2.0. This file contains or is part of one or more small +# programs. If a small program grows beyond 300 lines, I plan to switch +# its license to GPL. + +# Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,28 +20,41 @@ # See the License for the specific language governing permissions and # limitations under the License. + set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" +readonly this_file="$(readlink -f -- "${BASH_SOURCE[0]}")" +this_dir="${this_file%/*}" + usage() { - cat </dev/null - { echo -e '\n\n\n\n\n'$common_name'\n\n\n\n\n'; sleep 2; echo -e 'y\ny\n'; } | ./build-key $name &>/dev/null +if ! $force; then + cert_to_test=$f + if [[ $client_host ]]; then + cert_to_test=$(mktemp) + ssh $ssh_arg root@$client_host cat $f 2>/dev/null >$cert_to_test ||: + fi + if openssl x509 -checkend $(( 60 * 60 * 24 * 30 )) -noout -in $cert_to_test &>/dev/null; then + if [[ $client_host ]]; then + prefix="$shell" + fi + if $prefix test -s $keydir/ta-$name.key -a -s $keydir/ca-$name.crt; then + echo "$0: cert already exists. exiting early" + exit 0 + fi + fi fi -d=\$(mktemp -d) -cp /etc/openvpn/easy-rsa/keys/ca.crt \$d/$name-ca.crt -cp /etc/openvpn/easy-rsa/keys/$name.{crt,key} \$d - -cp \$server_dir/ta.key \$d/$name-ta.key - -tar cz -C \$d . -rm -rf \$d -EOF - -f=/etc/openvpn/client/$name.crt -if ! $shell "test -s $f"; then - # if common name is not unique, you get empty file. and if we didn't silence - # build-key, you'd see an error "TXT_DB error number 2" - echo "$0: error: $f is empty or otherwise bad. is this common name unique?" - exit 1 -fi +port=$(echo '/^port/ {print $2}' | ssh $ssh_arg root@$host awk -f - /etc/openvpn/server/$name.conf | tail -n1) -$shell "dd of=/etc/openvpn/client/$name.conf" <