more big cleanup of host/authentication commands to reflect new
[monkeysphere.git] / src / subcommands / mh / revoke-hostname
1 #!/usr/bin/env bash
2
3 # Monkeysphere host revoke-hostname subcommand
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #
10 # They are Copyright 2008, and are all released under the GPL, version 3
11 # or later.
12
13 # revoke hostname user ID from host key
14
15 revoke_hostname() {
16
17 local userID
18 local fingerprint
19 local tmpuidMatch
20 local line
21 local uidIndex
22 local message
23 local revuidCommand
24
25 if [ -z "$1" ] ; then
26     failure "You must specify a hostname to revoke."
27 fi
28
29 echo "WARNING: There is a known bug in this function."
30 echo "This function has been known to occasionally revoke the wrong user ID."
31 echo "Please see the following bug report for more information:"
32 echo "http://web.monkeysphere.info/bugs/revoke-hostname-revoking-wrong-userid/"
33 read -p "Are you sure you would like to proceed? (y/N) " OK; OK=${OK:=N}
34 if [ ${OK/y/Y} != 'Y' ] ; then
35     failure "aborting."
36 fi
37
38 userID="ssh://${1}"
39
40 fingerprint=$(fingerprint_server_key)
41
42 # match to only ultimately trusted user IDs
43 tmpuidMatch="u:$(echo $userID | gpg_escape)"
44
45 # find the index of the requsted user ID
46 # NOTE: this is based on circumstantial evidence that the order of
47 # this output is the appropriate index
48 if line=$(gpg_host --list-keys --with-colons --fixed-list-mode "0x${fingerprint}!" \
49     | egrep '^(uid|uat):' | cut -f2,10 -d: | grep -n -x -F "$tmpuidMatch") ; then
50     uidIndex=${line%%:*}
51 else
52     failure "No non-revoked user ID '$userID' is found."
53 fi
54
55 echo "The following host key user ID will be revoked:"
56 echo "  $userID"
57 read -p "Are you sure you would like to revoke this user ID? (y/N) " OK; OK=${OK:=N}
58 if [ ${OK/y/Y} != 'Y' ] ; then
59     failure "User ID not revoked."
60 fi
61
62 message="Hostname removed by monkeysphere-server $DATE"
63
64 # edit-key script command to revoke user ID
65 revuidCommand=$(cat <<EOF
66 $uidIndex
67 revuid
68 y
69 4
70 $message
71
72 y
73 save
74 EOF
75     )   
76
77 # execute edit-key script
78 if echo "$revuidCommand" | \
79     gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
80
81     show_key
82
83     echo
84     echo "NOTE: User ID revoked, but revocation not published."
85     echo "Run '$PGRM publish-key' to publish the revocation."
86 else
87     failure "Problem revoking user ID."
88 fi
89
90 }