Merge commit 'mlcastle/master'
[monkeysphere.git] / src / share / mh / revoke_key
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere host revoke-key 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-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 # revoke host key
15
16 revoke_key() {
17
18 # Coming in here, we expect $HOST_FINGERPRINT to be set, and we
19 # believe that there is in fact a key.
20
21     if [ "$PROMPT" = "false" ] ; then
22         publish=N
23     else
24         cat <<EOF >&2
25 This will generate a revocation certificate for your host key
26 (fingerprint: $HOST_FINGERPRINT) and
27 dump the certificate to standard output.
28
29 It can also directly publish the new revocation certificate
30 to the public keyservers via $KEYSERVER if you want it to.
31
32 Publishing this certificate will IMMEDIATELY and PERMANENTLY revoke
33 your host key!
34
35 EOF
36         printf "Publish the certificate after generation? (y/n/Q) " >&2
37         read publish
38         
39         if ! [ "${publish/y/Y}" = 'Y' -o "${publish/n/N}" = 'N' ] ; then
40             failure "aborting at user request"
41         fi
42     fi
43     
44     # our current implementation is very simple: we just want to
45     # generate the revocation certificate on stdout.  This provides
46     # for the two most likely (but hopefully not common) scenarios:
47
48     # an admin wants a revocation certificate for the host which they
49     # can store securely offline.  In this case, the admin can
50     # redirect stdout to a file, or can simply copy/paste or
51     # transcribe from the terminal.
52
53     # Alternately, an admin might want to publish the revocation
54     # certificate immediately, which we can help them do as well.
55
56     if [ "$PROMPT" = 'false' ] ; then
57         # FIXME: allow the end user to choose something other than
58         # "key was compromised" (1) and to supply their own revocation
59         # string.
60
61         local revoke_commands="y
62 1
63 Monkeysphere host key revocation (automated) $(date '+%F_%T%z')
64
65 y
66
67 "
68         revcert=$(GNUPGHOME="$GNUPGHOME_HOST" gpg_host --command-fd 0 --armor --gen-revoke "0x${HOST_FINGERPRINT}!" <<<"$revoke_commands" ) \
69             || failure "Failed to generate revocation certificate!"
70
71
72     else
73     # note: we're not using the gpg_host function because we actually
74     # want to use gpg's UI in this case, so we want to omit --no-tty
75         revcert=$(GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --armor --gen-revoke "0x${HOST_FINGERPRINT}!") \
76             || failure "Failed to generate revocation certificate!"
77     fi
78
79     # if you run gpg --gen-revoke but cancel it or quit in the middle,
80     # it returns success, but emits no revocation certificate:
81     if ! [ "$revcert" ] ; then
82         failure "Revocation canceled."
83     fi
84
85     ## ok, now we have the revocation certificate.  Print it, and
86     ## offer to publish if originally requested:
87     printf "%s\n" "$revcert"
88
89     if [ "${publish/y/Y}" = 'Y' ] ; then
90         printf "\n" >&2
91         printf "Really publish this cert to $KEYSERVER ? (Y/n) " >&2
92         read really
93         if [ "${really/n/N}" = 'N' ] ; then
94             printf "Not publishing.\n" >&2
95         else
96             local newhome=$(mkmstempdir)
97             GNUPGHOME="$newhome" gpg --no-tty --quiet --import < "$HOST_KEY_FILE"
98             GNUPGHOME="$newhome" gpg --no-tty --quiet --import <<< "$revcert"
99             GNUPGHOME="$newhome" gpg --keyserver "$KEYSERVER" --send "0x${HOST_FINGERPRINT}!"
100             rm -rf "$newhome"
101         fi
102     fi
103 }