Modify/cleanup add_certifier and add_revoker, so that their code base
[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
66 # check that key ID or file is specified
67 if [ -z "$keyID" ] ; then
68     failure "You must specify the key ID of a key to add, or specify a file to read the key from."
69 fi
70
71 # if file is specified
72 if [ -f "$keyID" -o "$keyID" = '-' ] ; then
73     # load the key from stdin
74     if [ "$keyID" = '-' ] ; then
75         local keyID=$(msmktempfile)
76         trap "rm -f $keyID" EXIT
77         log verbose "reading key from stdin..."
78         cat > "$keyID"
79
80     # load the key from the file
81     elif [ -f "$keyID" ] ; then
82         log verbose "reading key from file '$keyID'..."
83     fi
84
85     # check the key is ok as monkeysphere user before loading
86     fingerprint=$(su_monkeysphere_user \
87         ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
88
89     if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
90         failure "There was not exactly one gpg key in the file."
91     fi
92
93     # load the key
94     gpg_sphere "--import" <"$keyID" \
95         || failure "could not read key from '$keyID'"
96
97     keyID="$fingerprint"
98
99 # else, get the key from the keyserver
100 else
101     log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
102     gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" \
103         || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
104 fi
105
106 # get the full fingerprint of new certifier key
107 log debug "getting fingerprint of certifier key..."
108 fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
109     | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
110
111 if [ -z "$fingerprint" ] ; then
112     failure "Key '$keyID' not found."
113 fi
114
115 log info "key found:"
116 gpg_sphere "--fingerprint 0x${fingerprint}!"
117
118 if [ "$PROMPT" = "true" ] ; then
119     echo "Are you sure you want to add the above key as a"
120     read -p "certifier of users on this system? (Y/n) " OK; OK=${OK:-Y}
121     if [ "${OK/y/Y}" != 'Y' ] ; then
122         failure "Identity certifier not added."
123     fi
124 else
125     log debug "adding key without prompting."
126 fi
127
128 # export the key to the core keyring so that the core can sign the
129 # new certifier key
130 log debug "exporting retrieved certifier key to core keyring..."
131 gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
132
133 case "$trust" in
134     'marginal')
135         trustval=1
136         ;;
137     'full')
138         trustval=2
139         ;;
140     *)
141         failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
142         ;;
143 esac
144
145 # edit-key script to ltsign key
146 # NOTE: *all* user IDs will be ltsigned
147 ltsignCommand=$(cat <<EOF
148 ltsign
149 y
150 $trustval
151 $depth
152 $domain
153 y
154 save
155 EOF
156     )
157
158 # core ltsigns the newly imported certifier key
159 log debug "executing core ltsign script..."
160 if echo "$ltsignCommand" | \
161     gpg_core --command-fd 0 --edit-key "0x${fingerprint}!" ; then
162
163     # transfer the new sigs back to the sphere keyring
164     gpg_core_sphere_sig_transfer
165
166     # update the sphere trustdb
167     log debug "updating sphere trustdb..."
168     gpg_sphere "--check-trustdb" 2>&1 | log debug
169
170     log info "Identity certifier added."
171 else
172     failure "Problem adding identify certifier."
173 fi
174
175 }