stupid bug fix
[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     # create a temporary directory for storing the downloaded key
65     local GNUPGHOME="$tmpDir"
66     chmod 0700 "$GNUPGHOME"
67     chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_USER" "$GNUPGHOME"
68     export GNUPGHOME
69
70     # download the key from the keyserver as the monkeysphere user
71     log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
72     su_monkeysphere_user "gpg --quiet --keyserver $KEYSERVER --recv-key 0x${keyID}!" \
73         || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
74
75     # get the full fingerprint of new revoker key
76     log debug "getting fingerprint of revoker key..."
77     fingerprint=$(su_monkeysphere_user "gpg --list-key --with-colons --with-fingerprint 0x${keyID}!" \
78         | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
79
80     log info "key found:"
81     su_monkeysphere_user "gpg --fingerprint 0x${fingerprint}!"
82
83     if [ "$PROMPT" = "true" ] ; then
84         echo "Are you sure you want to add the above key as a"
85         read -p "revoker of the host key? (Y/n) " OK; OK=${OK:-Y}
86         if [ "${OK/y/Y}" != 'Y' ] ; then
87             failure "revoker not added."
88         fi
89     else
90         log debug "adding revoker without prompting."
91     fi
92
93     # export the new key to the host keyring
94     log debug "loading key into host keyring..."
95     su_monkeysphere_user "gpg --quiet --export 0x${fingerprint}!" \
96         | gpg_host --import
97 fi
98
99 # edit-key script to add revoker
100 addrevokerCommand=$(cat <<EOF
101 addrevoker
102 $fingerprint
103 y
104 save
105
106 EOF
107     )
108
109 # core ltsigns the newly imported revoker key
110 log debug "executing add revoker script..."
111 if echo "$addrevokerCommand" | gpg_host_edit ; then
112
113     update_gpg_pub_file
114
115     log info "Revoker added."
116 else
117     failure "Problem adding revoker."
118 fi
119
120 # remove the temporary directory
121 trap - EXIT
122 rm -rf "$tmpDir"
123
124 }