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