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