Merge commit 'jrollins/master'
[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     log debug "checking keys in file..."
50     fingerprint=$(su_monkeysphere_user \
51         ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
52
53     if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
54         failure "There was not exactly one gpg key in the file."
55     fi
56
57     # load the key
58     gpg_host --import <"$keyID" \
59         || failure "could not read key from '$keyID'"
60
61     keyID="$fingerprint"
62
63 # else, get the key from the keyserver
64 else
65     # create a temporary directory for storing the downloaded key
66     local TMPLOC=$(msmktempdir)
67     chmod 0700 "$GNUPGHOME"
68     chown "$MONKEYSPHERE_USER":"$MONKEYSPHERE_USER" "$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 \
73         "GNUPGHOME=$TMPLOC gpg --quiet --keyserver $KEYSERVER --recv-key 0x${keyID}!" \
74         || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
75
76     # export the new key to the host keyring
77     log verbose "loading key $keyID..."
78     su_monkeysphere_user "GNUPGHOME=$TMPLOC gpg --quiet --export 0x${keyID}!" \
79         | gpg_host --import
80 fi
81
82 # get the full fingerprint of new revoker key
83 log debug "getting fingerprint of revoker key..."
84 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "0x${keyID}!" \
85     | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
86
87 if [ -z "$fingerprint" ] ; then
88     failure "Key '$keyID' not found."
89 fi
90
91 log info "key found:"
92 gpg_host --fingerprint "0x${fingerprint}!"
93
94 if [ "$PROMPT" = "true" ] ; then
95     echo "Are you sure you want to add the above key as a"
96     read -p "revoker of the host key? (Y/n) " OK; OK=${OK:-Y}
97     if [ "${OK/y/Y}" != 'Y' ] ; then
98         failure "revoker not added."
99     fi
100 else
101     log debug "adding revoker without prompting."
102 fi
103
104 # edit-key script to add revoker
105 addrevokerCommand=$(cat <<EOF
106 addrevoker
107
108 EOF
109     )
110
111 # FIXME: implement!
112 failure "not implemented yet!"
113
114 # core ltsigns the newly imported revoker key
115 if echo "$addrevokerCommand" | \
116     gpg_core_edit ; then
117
118     update_gpg_pub_file
119
120     log info "Revoker added."
121 else
122     failure "Problem adding revoker."
123 fi
124
125 }