add ability to bypass prompting with a MONKEYSPHERE_PROMPT variable,
[monkeysphere.git] / src / share / 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 # This function adds a certifier whose signatures will be used to
7 # calculate validity of keys used to connect to user accounts on the
8 # server.  The specified certifier key is first retrieved from the Web
9 # of Trust with the monkeysphere-user-controlled gpg_sphere keyring.
10 # Once then new key is retrieved, it is imported into the core
11 # keyring.  The gpg_core then ltsigns the key with the desired trust
12 # level, and then the key is exported back to the gpg_sphere keyring.
13 # The gpg_sphere has ultimate owner trust of the core key, so the core
14 # ltsigs on the new certifier key can then be used by gpg_sphere
15 # calculate validity for keys inserted in the authorized_keys file.
16 #
17 # This is all to keep the monkeysphere user that connects to the
18 # keyservers from accessing the core secret key.
19 #
20 # The monkeysphere scripts are written by:
21 # Jameson Rollins <jrollins@finestructure.net>
22 # Jamie McClelland <jm@mayfirst.org>
23 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
24 #
25 # They are Copyright 2008-2009, and are all released under the GPL,
26 # version 3 or later.
27
28 add_certifier() {
29
30 local domain=
31 local trust=full
32 local depth=1
33 local keyID
34 local importinfo
35 local fingerprint
36 local ltsignCommand
37 local trustval
38
39 # get options
40 while true ; do
41     case "$1" in
42         -n|--domain)
43             domain="$2"
44             shift 2
45             ;;
46         -t|--trust)
47             trust="$2"
48             shift 2
49             ;;
50         -d|--depth)
51             depth="$2"
52             shift 2
53             ;;
54         *)
55             if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
56                 failure "Unknown option '$1'.
57 Type '$PGRM help' for usage."
58             fi
59             break
60             ;;
61     esac
62 done
63
64 keyID="$1"
65 if [ -z "$keyID" ] ; then
66     failure "You must specify the key ID of a key to add, or specify a file to read the key from."
67 fi
68 if [ -f "$keyID" ] ; then
69     log info "Reading key from file '$keyID':"
70     importinfo=$(gpg_sphere "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
71     # FIXME: if this is tried when the key database is not
72     # up-to-date, i got these errors (using set -x):
73
74     # ++ su -m monkeysphere -c '\''gpg --import'\''
75     # Warning: using insecure memory!
76     # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
77     # gpg: Total number processed: 1
78     # gpg:               imported: 1  (RSA: 1)
79     # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
80     # gpg: failed to rebuild keyring cache: Permission denied
81     # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
82     # gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
83     # gpg: next trustdb check due at 2009-01-10'
84     # + failure 'could not read key from '\''/root/dkg.gpg'\'''
85     # + echo 'could not read key from '\''/root/dkg.gpg'\'''
86
87     keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
88     if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
89         failure "There was not exactly one gpg key in the file."
90     fi
91 else
92     # get the key from the key server
93     log debug "retrieving key from keyserver..."
94     gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
95 fi
96
97 export keyID
98
99 # get the full fingerprint of new certifier key
100 log debug "getting fingerprint of certifier key..."
101 fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
102     | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
103
104 if [ -z "$fingerprint" ] ; then
105     failure "Key '$keyID' not found."
106 fi
107
108 log info "key found:"
109 gpg_sphere "--fingerprint 0x${fingerprint}!"
110
111 if [ "$PROMPT" = "true" ] ; then
112     echo "Are you sure you want to add the above key as a"
113     read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
114     if [ "${OK/y/Y}" != 'Y' ] ; then
115         failure "Identity certifier not added."
116     fi
117 else
118     log debug "adding key without prompting."
119 fi
120
121 # export the key to the core keyring so that the core can sign the
122 # new certifier key
123 log debug "exporting retrieved certifier key to core keyring..."
124 gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
125
126 case "$trust" in
127     'marginal')
128         trustval=1
129         ;;
130     'full')
131         trustval=2
132         ;;
133     *)
134         failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
135         ;;
136 esac
137
138 # edit-key script to ltsign key
139 # NOTE: *all* user IDs will be ltsigned
140 ltsignCommand=$(cat <<EOF
141 ltsign
142 y
143 $trustval
144 $depth
145 $domain
146 y
147 save
148 EOF
149     )
150
151 # core ltsigns the newly imported certifier key
152 log debug "executing core ltsign script..."
153 if echo "$ltsignCommand" | \
154     gpg_core --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
155
156     # transfer the new sigs back to the sphere keyring
157     gpg_core_sphere_sig_transfer
158
159     # update the sphere trustdb
160     log debug "updating sphere trustdb..."
161     gpg_sphere "--check-trustdb"
162
163     log info "Identity certifier added."
164 else
165     failure "Problem adding identify certifier."
166 fi
167
168 }