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