make subcommand subfunction that were missed previously
[monkeysphere.git] / src / subcommands / ma / add-certifier
1 #!/usr/bin/env bash
2
3 # Monkeysphere authentication add-certifier subcommand
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #
10 # They are Copyright 2008, and are all released under the GPL, version 3
11 # or later.
12
13 # retrieve key from web of trust, import it into the host keyring, and
14 # ltsign the key in the host keyring so that it may certify other keys
15
16 add_certifier() {
17
18 local domain
19 local trust
20 local depth
21 local keyID
22 local fingerprint
23 local ltsignCommand
24 local trustval
25
26 # set default values for trust depth and domain
27 domain=
28 trust=full
29 depth=1
30
31 # get options
32 while true ; do
33     case "$1" in
34         -n|--domain)
35             domain="$2"
36             shift 2
37             ;;
38         -t|--trust)
39             trust="$2"
40             shift 2
41             ;;
42         -d|--depth)
43             depth="$2"
44             shift 2
45             ;;
46         *)
47             if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
48                 failure "Unknown option '$1'.
49 Type '$PGRM help' for usage."
50             fi
51             break
52             ;;
53     esac
54 done
55
56 keyID="$1"
57 if [ -z "$keyID" ] ; then
58     failure "You must specify the key ID of a key to add, or specify a file to read the key from."
59 fi
60 if [ -f "$keyID" ] ; then
61     echo "Reading key from file '$keyID':"
62     importinfo=$(gpg_authentication "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
63     # FIXME: if this is tried when the key database is not
64     # up-to-date, i got these errors (using set -x):
65
66     # ++ su -m monkeysphere -c '\''gpg --import'\''
67     # Warning: using insecure memory!
68     # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
69     # gpg: Total number processed: 1
70     # gpg:               imported: 1  (RSA: 1)
71     # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
72     # gpg: failed to rebuild keyring cache: Permission denied
73     # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
74     # gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
75     # gpg: next trustdb check due at 2009-01-10'
76     # + failure 'could not read key from '\''/root/dkg.gpg'\'''
77     # + echo 'could not read key from '\''/root/dkg.gpg'\'''
78
79     keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
80     if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
81         failure "Expected there to be a single gpg key in the file."
82     fi
83 else
84     # get the key from the key server
85     gpg_authentication "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
86 fi
87
88 export keyID
89
90 # get the full fingerprint of a key ID
91 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint 0x${keyID}!" | \
92     grep '^fpr:' | grep "$keyID" | cut -d: -f10)
93
94 if [ -z "$fingerprint" ] ; then
95     failure "Key '$keyID' not found."
96 fi
97
98 echo
99 echo "key found:"
100 gpg_authentication "--fingerprint 0x${fingerprint}!"
101
102 echo "Are you sure you want to add the above key as a"
103 read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
104 if [ "${OK/y/Y}" != 'Y' ] ; then
105     failure "Identity certifier not added."
106 fi
107
108 # export the key to the host keyring
109 gpg_authentication "--export 0x${fingerprint}!" | gpg_host --import
110
111 if [ "$trust" = marginal ]; then
112     trustval=1
113 elif [ "$trust" = full ]; then
114     trustval=2
115 else
116     failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
117 fi
118
119 # ltsign command
120 # NOTE: *all* user IDs will be ltsigned
121 ltsignCommand=$(cat <<EOF
122 ltsign
123 y
124 $trustval
125 $depth
126 $domain
127 y
128 save
129 EOF
130     )
131
132 # ltsign the key
133 if echo "$ltsignCommand" | \
134     gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
135
136     # update the trustdb for the authentication keyring
137     gpg_authentication "--check-trustdb"
138
139     echo
140     echo "Identity certifier added."
141 else
142     failure "Problem adding identify certifier."
143 fi
144
145 }