fc1ae9665ec76745ffed24900e72bea31e8e12fb
[monkeysphere.git] / src / subcommands / mh / add-hostname
1 #!/usr/bin/env bash
2
3 # Monkeysphere host add-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 # add hostname user ID to server key
14
15 local userID
16 local fingerprint
17 local tmpuidMatch
18 local line
19 local adduidCommand
20
21 if [ -z "$1" ] ; then
22     failure "You must specify a hostname to add."
23 fi
24
25 userID="ssh://${1}"
26
27 fingerprint=$(fingerprint_server_key)
28
29 # match to only ultimately trusted user IDs
30 tmpuidMatch="u:$(echo $userID | gpg_escape)"
31
32 # find the index of the requsted user ID
33 # NOTE: this is based on circumstantial evidence that the order of
34 # this output is the appropriate index
35 if line=$(gpg_host --list-keys --with-colons --fixed-list-mode "0x${fingerprint}!" \
36     | egrep '^(uid|uat):' | cut -f2,10 -d: | grep -n -x -F "$tmpuidMatch") ; then
37     failure "Host userID '$userID' already exists."
38 fi
39
40 echo "The following user ID will be added to the host key:"
41 echo "  $userID"
42 read -p "Are you sure you would like to add this user ID? (y/N) " OK; OK=${OK:=N}
43 if [ ${OK/y/Y} != 'Y' ] ; then
44     failure "User ID not added."
45 fi
46
47 # edit-key script command to add user ID
48 adduidCommand=$(cat <<EOF
49 adduid
50 $userID
51
52
53 save
54 EOF
55 )
56
57 # execute edit-key script
58 if echo "$adduidCommand" | \
59     gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
60
61     # update the trustdb for the authentication keyring
62     gpg_authentication "--check-trustdb"
63
64     show_server_key
65
66     echo
67     echo "NOTE: User ID added to key, but key not published."
68     echo "Run '$PGRM publish-key' to publish the new user ID."
69 else
70     failure "Problem adding user ID."
71 fi