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