summaryrefslogtreecommitdiff
path: root/mailcert.sh
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk@omgwallhack.org>2011-03-27 19:52:49 -0700
committerJoe Rayhawk <jrayhawk@omgwallhack.org>2011-03-27 19:52:49 -0700
commit478e1993e0760ad83668243111be155b3e9e7050 (patch)
tree46d356b1a306d5333dd530115e57b61bcb3ab803 /mailcert.sh
downloadcash-478e1993e0760ad83668243111be155b3e9e7050.tar.gz
cash-478e1993e0760ad83668243111be155b3e9e7050.zip
Initial content; missing some mailcert.sh support
Diffstat (limited to 'mailcert.sh')
-rwxr-xr-xmailcert.sh59
1 files changed, 59 insertions, 0 deletions
diff --git a/mailcert.sh b/mailcert.sh
new file mode 100755
index 0000000..d3301d5
--- /dev/null
+++ b/mailcert.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+# ./mailcert.sh [certfile|certname|serial] (emailaddress) (configfile)
+
+# We need to know what to send, and who to send it to. We aggressively attempt to infer this information as best we can from what arguments are given to us, and what's provided in config files.
+
+set -e
+
+SUPPLEMENTARY_CONFIG="$3"
+
+if [ $2 ]; then
+ if [[ "$2" =~ .+@.+ ]]; then
+ USEREMAIL=$2
+ else
+ echo "Second argument is not a valid email address; proceeding as if it were the config file..."
+ SUPPLEMENTARY_CONFIG="$2"
+ fi
+fi
+
+. ./configure.sh
+
+# attempt to work out where the certificate is, and which CA it is.
+if [ -e "$CA"/certs/"$1".pem ]; then # serial
+ USERCERT="$CA"/certs/"$1".pem
+elif [ -e "$CA"/signed/"$1".crt ]; then # certname
+ USERCERT="$CA"/signed/"$1".crt
+elif [ -e "$1" ]; then # certfile (ugh!)
+# omgca/signed/test.crt
+ USERCERT="$1"
+ if [[ "$1" =~ (.+/|())(.+)/.+/.+ ]]; then
+ CA="${BASH_REMATCH[3]}"
+ fi
+else
+ echo None of "$CA"/certs/"$1".pem, "$1", or "$CA"/signed/"$1".crt exist\!
+ exit 2
+fi
+
+CACERT="$CA"/ca/"$CA".crt
+
+# attempt to work out where to send the certificate
+if ! [ "$USEREMAIL" ]; then # address from cmdline
+ if ! USEREMAIL="$(openssl x509 -in "$USERCERT" -text | sed -ne '{s/.*Subject.\+emailAddress=\(.\+\)/\1/p}' | head -n 1 | grep . )"; then # address from cert
+ if [[ "$USERCERT" =~ (.+/|())(.+)-.+ ]]; then
+ USEREMAIL="${BASH_REMATCH[3]}"@"$EMAIL_DEFAULT_DOMAIN"
+ elif [[ "$USERCERT" =~ (.+/|())(.+)\..+ ]]; then
+ USEREMAIL="${BASH_REMATCH[3]}"@"$EMAIL_DEFAULT_DOMAIN"
+ else
+ echo "Cannot find email address!"
+ exit 3
+ fi
+ fi
+fi
+
+echo CACERT is assumed to be: "$CACERT"
+echo USERCERT is assumed to be: "$USERCERT"
+echo USEREMAIL is assumed to be: "$USEREMAIL"
+echo Press Ctrl-C if any of this looks incorrect.
+sleep 5
+
+# FIXME: implement /usr/lib/sendmail input, including attachment syntax