break out monkeysphere-{host,authentication} subcommands into seperate
[monkeysphere.git] / src / subcommands / ma / add-certifier
1 #!/usr/bin/env bash
2
3 # Monkeysphere authentication add-certifier 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 # retrieve key from web of trust, import it into the host keyring, and
14 # ltsign the key in the host keyring so that it may certify other keys
15
16 local domain
17 local trust
18 local depth
19 local keyID
20 local fingerprint
21 local ltsignCommand
22 local trustval
23
24 # set default values for trust depth and domain
25 domain=
26 trust=full
27 depth=1
28
29 # get options
30 while true ; do
31     case "$1" in
32         -n|--domain)
33             domain="$2"
34             shift 2
35             ;;
36         -t|--trust)
37             trust="$2"
38             shift 2
39             ;;
40         -d|--depth)
41             depth="$2"
42             shift 2
43             ;;
44         *)
45             if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
46                 failure "Unknown option '$1'.
47 Type '$PGRM help' for usage."
48             fi
49             break
50             ;;
51     esac
52 done
53
54 keyID="$1"
55 if [ -z "$keyID" ] ; then
56     failure "You must specify the key ID of a key to add, or specify a file to read the key from."
57 fi
58 if [ -f "$keyID" ] ; then
59     echo "Reading key from file '$keyID':"
60     importinfo=$(gpg_authentication "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
61     # FIXME: if this is tried when the key database is not
62     # up-to-date, i got these errors (using set -x):
63
64     # ++ su -m monkeysphere -c '\''gpg --import'\''
65     # Warning: using insecure memory!
66     # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
67     # gpg: Total number processed: 1
68     # gpg:               imported: 1  (RSA: 1)
69     # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
70     # gpg: failed to rebuild keyring cache: Permission denied
71     # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
72     # gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
73     # gpg: next trustdb check due at 2009-01-10'
74     # + failure 'could not read key from '\''/root/dkg.gpg'\'''
75     # + echo 'could not read key from '\''/root/dkg.gpg'\'''
76
77     keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
78     if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
79         failure "Expected there to be a single gpg key in the file."
80     fi
81 else
82     # get the key from the key server
83     gpg_authentication "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
84 fi
85
86 export keyID
87
88 # get the full fingerprint of a key ID
89 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint 0x${keyID}!" | \
90     grep '^fpr:' | grep "$keyID" | cut -d: -f10)
91
92 if [ -z "$fingerprint" ] ; then
93     failure "Key '$keyID' not found."
94 fi
95
96 echo
97 echo "key found:"
98 gpg_authentication "--fingerprint 0x${fingerprint}!"
99
100 echo "Are you sure you want to add the above key as a"
101 read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
102 if [ "${OK/y/Y}" != 'Y' ] ; then
103     failure "Identity certifier not added."
104 fi
105
106 # export the key to the host keyring
107 gpg_authentication "--export 0x${fingerprint}!" | gpg_host --import
108
109 if [ "$trust" = marginal ]; then
110     trustval=1
111 elif [ "$trust" = full ]; then
112     trustval=2
113 else
114     failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
115 fi
116
117 # ltsign command
118 # NOTE: *all* user IDs will be ltsigned
119 ltsignCommand=$(cat <<EOF
120 ltsign
121 y
122 $trustval
123 $depth
124 $domain
125 y
126 save
127 EOF
128     )
129
130 # ltsign the key
131 if echo "$ltsignCommand" | \
132     gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
133
134     # update the trustdb for the authentication keyring
135     gpg_authentication "--check-trustdb"
136
137     echo
138     echo "Identity certifier added."
139 else
140     failure "Problem adding identify certifier."
141 fi