Modify/cleanup add_certifier and add_revoker, so that their code base
[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 domain=
19 local trust=full
20 local depth=1
21 local keyID
22 local importinfo
23 local fingerprint
24 local ltsignCommand
25 local trustval
26
27 keyID="$1"
28
29 # check that key ID or file is specified
30 if [ -z "$keyID" ] ; then
31     failure "You must specify the key ID of a revoker key, or specify a file to read the key from."
32 fi
33
34 # if file is specified
35 if [ -f "$keyID" -o "$keyID" = '-' ] ; then
36     # load the key from stdin
37     if [ "$keyID" = '-' ] ; then
38         local keyID=$(msmktempfile)
39         trap "rm -f $keyID" EXIT
40         log verbose "reading key from stdin..."
41         cat > "$keyID"
42
43     # load the key from the file
44     elif [ -f "$keyID" ] ; then
45         log verbose "reading key from file '$keyID'..."
46     fi
47
48     # check the key is ok as monkeysphere user before loading
49     fingerprint=$(su_monkeysphere_user \
50         ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
51
52     if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
53         failure "There was not exactly one gpg key in the file."
54     fi
55
56     # load the key
57     gpg_host --import <"$keyID" \
58         || failure "could not read key from '$keyID'"
59
60     keyID="$fingerprint"
61
62 # else, get the key from the keyserver
63 else
64     # create a temporary directory for storing the downloaded key
65     local TMPLOC=$(msmktempdir)
66     chmod 0700 "$GNUPGHOME"
67     chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_USER" "$GNUPGHOME"
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 \
72         "GNUPGHOME=$TMPLOC gpg --quiet --keyserver $KEYSERVER --recv-key 0x${keyID}!" \
73         || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
74
75     # export the new key to the host keyring
76     log verbose "loading key $keyID..."
77     su_monkeysphere_user "GNUPGHOME=$TMPLOC gpg --quiet --export 0x${keyID}!" \
78         | gpg_host --import
79 fi
80
81 # get the full fingerprint of new revoker key
82 log debug "getting fingerprint of revoker key..."
83 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "0x${keyID}!" \
84     | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
85
86 if [ -z "$fingerprint" ] ; then
87     failure "Key '$keyID' not found."
88 fi
89
90 log info "key found:"
91 gpg_host --fingerprint "0x${fingerprint}!"
92
93 if [ "$PROMPT" = "true" ] ; then
94     echo "Are you sure you want to add the above key as a"
95     read -p "revoker of the host key? (Y/n) " OK; OK=${OK:-Y}
96     if [ "${OK/y/Y}" != 'Y' ] ; then
97         failure "revoker not added."
98     fi
99 else
100     log debug "adding revoker without prompting."
101 fi
102
103 # edit-key script to add revoker
104 addrevokerCommand=$(cat <<EOF
105 addrevoker
106
107 EOF
108     )
109
110 # FIXME: implement!
111 failure "not implemented yet!"
112
113 # core ltsigns the newly imported revoker key
114 if echo "$addrevokerCommand" | \
115     gpg_core_edit ; then
116
117     update_gpg_pub_file
118
119     log info "Revoker added."
120 else
121     failure "Problem adding revoker."
122 fi
123
124 }