264c2556766d3b77f1ab604ea82b8b35b773c75c
[monkeysphere.git] / src / share / mh / add_revoker
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere host add-revoker subcommand
5 #
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 #
11 # They are Copyright 2008-2010, and are all released under the GPL,
12 # version 3 or later.
13
14 # add a revoker to the host key
15
16 add_revoker() {
17
18 local revokerKeyID
19 local keyID
20 local tmpDir
21 local fingerprint
22 local addrevokerCommand
23
24 # check that key ID or file is specified
25 if [ -z "$1" ] ; then
26     failure "You must specify the key ID of a revoker key, or specify a file to read the key from."
27 fi
28 revokerKeyID="$1"
29 shift
30
31 keyID=$(check_key_input "$@")
32
33 # make a temporary directory for storing keys during import, and set
34 # the trap to delete it on exit
35 tmpDir=$(msmktempdir)
36 trap "rm -rf $tmpDir" EXIT
37
38 # if file is specified
39 if [ -f "$revokerKeyID" -o "$revokerKeyID" = '-' ] ; then
40     # load the key from stdin
41     if [ "$revokerKeyID" = '-' ] ; then
42         # make a temporary file to hold the key from stdin
43         revokerKeyID="$tmpDir"/importkey
44         log verbose "reading revoker key from stdin..."
45         cat > "$revokerKeyID"
46
47     # load the key from the file
48     elif [ -f "$revokerKeyID" ] ; then
49         log verbose "reading revoker key from file '$revokerKeyID'..."
50     fi
51
52     # check the key is ok as monkeysphere user before loading
53     log debug "checking keys in file..."
54     fingerprint=$(su_monkeysphere_user \
55         ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$revokerKeyID")
56
57     if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
58         failure "There was not exactly one gpg key in the file."
59     fi
60
61     # load the key
62     gpg_host --import <"$revokerKeyID" \
63         || failure "could not read revoker key from '$revokerKeyID'"
64
65 # else, get the revoker key from the keyserver
66 else
67     # fix permissions and ownership on temporary directory which will
68     # be used by monkeysphere user for storing the downloaded key
69     chmod 0700 "$tmpDir"
70     chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_GROUP" "$tmpDir"
71
72     # download the key from the keyserver as the monkeysphere user
73     log verbose "searching keyserver $KEYSERVER for revoker keyID $revokerKeyID..."
74     su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --keyserver $KEYSERVER --recv-key 0x${revokerKeyID}!" \
75         || failure "Could not receive a key with this ID from keyserver '$KEYSERVER'."
76
77     # get the full fingerprint of new revoker key
78     log debug "getting fingerprint of revoker key..."
79     fingerprint=$(su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --list-key --with-colons --with-fingerprint ${revokerKeyID}" \
80         | grep '^fpr:' | cut -d: -f10)
81
82     # test that there is only a single fingerprint
83     if (( $(echo "$fingerprint" | wc -l) != 1 )) ; then
84         cat <<EOF
85 More than one fingerprint found:
86 $fingerprint
87 Please use a more specific key ID.
88 EOF
89         failure
90     fi
91
92     log info "revoker key found:"
93     su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --fingerprint 0x${fingerprint}!"
94
95     if [ "$PROMPT" = "true" ] ; then
96         printf "Are you sure you want to add the above key as a revoker\nof the key '$keyID'? (Y/n) " >&2
97         read OK; OK=${OK:-Y}
98         if [ "${OK/y/Y}" != 'Y' ] ; then
99             failure "revoker not added."
100         fi
101     else
102         log debug "adding revoker without prompting."
103     fi
104
105     # export the new key to the host keyring
106     log debug "loading revoker key into host keyring..."
107     su_monkeysphere_user "GNUPGHOME=$tmpDir gpg --quiet --export 0x${fingerprint}!" \
108         | gpg_host --import
109 fi
110
111 # edit-key script to add revoker
112 addrevokerCommand="addrevoker
113 $fingerprint
114 y
115 save
116 "
117 # end script
118
119 # core ltsigns the newly imported revoker key
120 log debug "executing add revoker script..."
121 if echo "$addrevokerCommand" | gpg_host_edit "0x${keyID}!" ; then
122
123     update_gpg_pub_file
124
125     log info "Revoker added."
126 else
127     failure "Problem adding revoker."
128 fi
129
130 # remove the temporary directory
131 trap - EXIT
132 rm -rf "$tmpDir"
133
134 }