28c229aec25de30120beca3b6c4ef0ed50f9f72a
[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 PROMPT=true
40
41 # get options
42 while true ; do
43     case "$1" in
44         -n|--domain)
45             domain="$2"
46             shift 2
47             ;;
48         -t|--trust)
49             trust="$2"
50             shift 2
51             ;;
52         -d|--depth)
53             depth="$2"
54             shift 2
55             ;;
56         -y)
57             PROMPT=false
58             shift 1
59             ;;
60         *)
61             if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
62                 failure "Unknown option '$1'.
63 Type '$PGRM help' for usage."
64             fi
65             break
66             ;;
67     esac
68 done
69
70 keyID="$1"
71 if [ -z "$keyID" ] ; then
72     failure "You must specify the key ID of a key to add, or specify a file to read the key from."
73 fi
74 if [ -f "$keyID" ] ; then
75     log info "Reading key from file '$keyID':"
76     importinfo=$(gpg_sphere "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
77     # FIXME: if this is tried when the key database is not
78     # up-to-date, i got these errors (using set -x):
79
80     # ++ su -m monkeysphere -c '\''gpg --import'\''
81     # Warning: using insecure memory!
82     # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
83     # gpg: Total number processed: 1
84     # gpg:               imported: 1  (RSA: 1)
85     # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
86     # gpg: failed to rebuild keyring cache: Permission denied
87     # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
88     # gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
89     # gpg: next trustdb check due at 2009-01-10'
90     # + failure 'could not read key from '\''/root/dkg.gpg'\'''
91     # + echo 'could not read key from '\''/root/dkg.gpg'\'''
92
93     keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
94     if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
95         failure "There was not exactly one gpg key in the file."
96     fi
97 else
98     # get the key from the key server
99     log debug "retrieving key from keyserver..."
100     gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
101 fi
102
103 export keyID
104
105 # get the full fingerprint of new certifier key
106 log debug "getting fingerprint of certifier key..."
107 fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
108     | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
109
110 if [ -z "$fingerprint" ] ; then
111     failure "Key '$keyID' not found."
112 fi
113
114 log info "key found:"
115 gpg_sphere "--fingerprint 0x${fingerprint}!"
116
117 if [ "$PROMPT" = true ] ; then
118     echo "Are you sure you want to add the above key as a"
119     read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
120     if [ "${OK/y/Y}" != 'Y' ] ; then
121         failure "Identity certifier not added."
122     fi
123 else
124     log debug "adding key without prompting."
125 fi
126
127 # export the key to the core keyring so that the core can sign the
128 # new certifier key
129 log debug "exporting retrieved certifier key to core keyring..."
130 gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
131
132 case "$trust" in
133     'marginal')
134         trustval=1
135         ;;
136     'full')
137         trustval=2
138         ;;
139     *)
140         failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
141         ;;
142 esac
143
144 # edit-key script to ltsign key
145 # NOTE: *all* user IDs will be ltsigned
146 ltsignCommand=$(cat <<EOF
147 ltsign
148 y
149 $trustval
150 $depth
151 $domain
152 y
153 save
154 EOF
155     )
156
157 # core ltsigns the newly imported certifier key
158 log debug "executing core ltsign script..."
159 if echo "$ltsignCommand" | \
160     gpg_core --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
161
162     # transfer the new sigs back to the sphere keyring
163     gpg_core_sphere_sig_transfer
164
165     # update the sphere trustdb
166     log debug "updating sphere trustdb..."
167     gpg_sphere "--check-trustdb"
168
169     log info "Identity certifier added."
170 else
171     failure "Problem adding identify certifier."
172 fi
173
174 }