add_revoker fully working. also cleanup of add_certifier.
[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 fingerprint
35 local ltsignCommand
36 local trustval
37
38 # get options
39 while true ; do
40     case "$1" in
41         -n|--domain)
42             domain="$2"
43             shift 2
44             ;;
45         -t|--trust)
46             trust="$2"
47             shift 2
48             ;;
49         -d|--depth)
50             depth="$2"
51             shift 2
52             ;;
53         -)
54             break
55             ;;
56         *)
57             if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
58                 failure "Unknown option '$1'.
59 Type '$PGRM help' for usage."
60             fi
61             break
62             ;;
63     esac
64 done
65
66 keyID="$1"
67
68 # check that key ID or file is specified
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
73 # check the trust value
74 case "$trust" in
75     'marginal')
76         trustval=1
77         ;;
78     'full')
79         trustval=2
80         ;;
81     *)
82         failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
83         ;;
84 esac
85
86 # if file is specified
87 if [ -f "$keyID" -o "$keyID" = '-' ] ; then
88     # load the key from stdin
89     if [ "$keyID" = '-' ] ; then
90         # make a temporary file to hold the key from stdin
91         keyID=$(msmktempfile)
92         trap "rm -f $keyID" EXIT
93         log verbose "reading key from stdin..."
94         cat > "$keyID"
95
96     # load the key from the file
97     elif [ -f "$keyID" ] ; then
98         log verbose "reading key from file '$keyID'..."
99     fi
100
101     # check the key is ok as monkeysphere user before loading
102     log debug "checking keys in file..."
103     fingerprint=$(su_monkeysphere_user \
104         ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
105
106     if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
107         failure "There was not exactly one gpg key in the file."
108     fi
109
110     # load the key
111     gpg_sphere "--import" <"$keyID" \
112         || failure "could not read key from '$keyID'"
113
114 # else, get the key from the keyserver
115 else
116     log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
117     gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" \
118         || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
119
120     # get the full fingerprint of new certifier key
121     log debug "getting fingerprint of certifier key..."
122     fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
123         | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
124
125     log info "key found:"
126     gpg_sphere "--fingerprint 0x${fingerprint}!"
127
128     if [ "$PROMPT" = "true" ] ; then
129         echo "Are you sure you want to add the above key as a"
130         read -p "certifier of users on this system? (Y/n) " OK; OK=${OK:-Y}
131         if [ "${OK/y/Y}" != 'Y' ] ; then
132             failure "Identity certifier not added."
133         fi
134     else
135         log debug "adding key without prompting."
136     fi
137 fi
138
139 # export the key to the core keyring so that the core can sign the
140 # new certifier key
141 log debug "loading key into core keyring..."
142 gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
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 --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" 2>&1 | log debug
168
169     log info "Identity certifier added."
170 else
171     failure "Problem adding identify certifier."
172 fi
173
174 }