X-Git-Url: https://iankelling.org/git/?p=vpn-setup;a=blobdiff_plain;f=vpn-mk-client-cert;h=1aa9b4166aca033059b65b25328c829a68677b15;hp=f480d02b8e4cf7bbcac1ac400d99253ca41bf381;hb=HEAD;hpb=dfbfe58ae8f9e2a80fde7279fc1608ef6e32c6fa diff --git a/vpn-mk-client-cert b/vpn-mk-client-cert index f480d02..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,25 +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 +host=$1 +[[ $host ]] || usage 1 - { 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 -fi +####### end command line parsing and checking ############## -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 -tar cz -C \$d . -rm -rf \$d -EOF +if $rel; then + f=$name.crt + keydir=. +else + f=/etc/openvpn/client/$name.crt + keydir=/etc/openvpn/client +fi + -f=/etc/openvpn/client/$name.crt -if [[ ! -s $f ]]; then - echo "$0: error: $f is empty or otherwise bad. is this common name unique?" - exit 1 +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 -$shell "dd of=/etc/openvpn/client/$name.conf" <