ca-sign: Add usage statement and more thorough param-checking
[clinton/scripts.git] / ca-install
... / ...
CommitLineData
1#!/bin/bash
2#
3# Install a signed certificate, placing a complimentary copy in the
4# member's homedir. Also grant member domtool permissions for the
5# certificate.
6#
7# If the certificate comes from the member's home directory, then
8# don't place an extra copy there.
9#
10# Run this on deleuze as an admin.
11#
12# Usage: ca-install member domain cert-file.pem [key-file.pem]
13
14function usage () {
15 echo "Usage: ca-install member domain cert-file.pem [key-file.pem]"
16 exit 1
17}
18
19# Check arguments
20if test -n "$5"; then
21 echo "Error: Too many arguments."
22 usage
23elif test -z "$3"; then
24 echo "Error: Not enough arguments."
25 usage
26else
27 MEMBER=$1
28 DOMAIN=$2
29 CERT=$3
30 KEY=$4
31fi
32
33WEBSERVER=mire.hcoop.net
34
35function verify_cert () {
36 if test -z "$2" || test -n "$3"; then
37 echo "Bad programming."
38 exit 1
39 fi
40 local CERT=$1
41 local KEY=$2
42 local MOD1=$(openssl x509 -noout -modulus -in "$CERT" 2>&1)
43 if test $(echo "$MOD1" | wc -c) -lt 500; then
44 echo "Error: Bad x509 part in certificate."
45 exit 1
46 fi
47 local MOD2=$(openssl rsa -noout -modulus -in "$KEY" 2>&1)
48 if test $(echo "$MOD2" | wc -c) -lt 500; then
49 echo "Error: Bad RSA part in certificate or key."
50 exit 1
51 fi
52 if test "$MOD1" != "$MOD2"; then
53 echo "Error: x509 and RSA parts in certificate do not match."
54 exit 1
55 fi
56}
57
58# Make sure we run this from deleuze
59if test "$(hostname -s)" != "deleuze"; then
60 echo "Error: This script must be run from deleuze."
61 exit 1
62fi
63
64# Sanity-check some paths
65if test ! -f "$CERT"; then
66 echo "Error: Nonexistent or unreadable cert $CERT."
67 exit 1
68fi
69if test -n "$KEY" && test ! -f "$KEY"; then
70 echo "Error: Nonexistent or unreadable key $KEY."
71 exit 1
72fi
73
74# Check for valid username
75if ! getent passwd "$MEMBER" > /dev/null; then
76 echo "Error: Invalid user \"$MEMBER\"."
77 exit 1
78fi
79
80# Figure out destination for complimentary copy
81APACHE_DEST=/etc/apache2/ssl/user/$DOMAIN.pem
82MEMBERHOME=$(getent passwd $MEMBER | cut -d':' -f 6)
83if test -n "$KEY"; then
84 DEST="$(dirname $KEY)/$DOMAIN.pem"
85else
86 DEST=
87fi
88
89# Perform complimentary copy
90if test -z "$DEST"; then
91 echo "No key specified, so skipping complimentary copy."
92elif echo "$CERT" | grep "^$MEMBERHOME" > /dev/null; then
93 echo "Member already has a cert, skipping the complimentary copy."
94elif test -f "$DEST"; then
95 echo "Not overwriting existing file $DEST."
96else
97 echo "Copying signed certificate to member's home directory ..."
98 cp "$CERT" "$DEST"
99 chown $MEMBER:nogroup "$DEST"
100fi
101echo
102
103# Determine whether we need to concatenate a private key
104if grep "^-----BEGIN RSA PRIVATE KEY-----" "$CERT" > /dev/null; then
105 KEY=
106else
107 if test -z "$KEY"; then
108 echo "Error: No RSA private key is included with this certificate."
109 exit 1
110 fi
111fi
112
113# Verify certificate and key
114echo "Validating certificate ..."
115if test -z "$KEY"; then
116 verify_cert "$CERT" "$CERT"
117else
118 verify_cert "$CERT" "$KEY"
119fi
120echo "Certificate passed validatation."
121echo
122
123# Copy complete certificate to webserver
124if test -z "$KEY"; then
125 echo "Installing certificate to Apache SSL directory ..."
126 < "$CERT" ssh $WEBSERVER sudo tee "$APACHE_DEST" > /dev/null
127else
128 echo "Installing certificate and key to Apache SSL directory ..."
129 cat "$CERT" "$KEY" | ssh $WEBSERVER sudo tee "$APACHE_DEST" > /dev/null
130fi
131echo
132
133# Grant Domtool permissions
134echo "Granting member Domtool permissions for the certificate ..."
135domtool-admin grant $MEMBER cert "$APACHE_DEST"
136echo
137
138# Tell admin what to do
139echo "Done. Tell $MEMBER that the certificate is available for use at"
140echo " $APACHE_DEST"