Modify/cleanup add_certifier and add_revoker, so that their code base
[monkeysphere.git] / src / monkeysphere-host
1 #!/usr/bin/env bash
2
3 # monkeysphere-host: Monkeysphere host admin tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@finestructure.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 ########################################################################
15 set -e
16
17 # set the pipefail option so pipelines fail on first command failure
18 set -o pipefail
19
20 PGRM=$(basename $0)
21
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
23 export SYSSHAREDIR
24 . "${SYSSHAREDIR}/common" || exit 1
25
26 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
27 export SYSDATADIR
28
29 # sharedir for host functions
30 MHSHAREDIR="${SYSSHAREDIR}/mh"
31
32 # datadir for host functions
33 MHDATADIR="${SYSDATADIR}/host"
34
35 # host pub key files
36 HOST_KEY_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
37
38 # UTC date in ISO 8601 format if needed
39 DATE=$(date -u '+%FT%T')
40
41 # unset some environment variables that could screw things up
42 unset GREP_OPTIONS
43
44 # default return code
45 RETURN=0
46
47 ########################################################################
48 # FUNCTIONS
49 ########################################################################
50
51 usage() {
52     cat <<EOF >&2
53 usage: $PGRM <subcommand> [options] [args]
54 Monkeysphere host admin tool.
55
56 subcommands:
57  import-key (i) FILE [NAME[:PORT]]   import existing ssh key to gpg
58  show-key (s)                        output all host key information
59  set-expire (e) [EXPIRE]             set host key expiration
60  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
61  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
62  add-revoker (o) [KEYID|FILE]        add a revoker to the host key
63  revoke-key (r)                      revoke host key
64  publish-key (p)                     publish host key to keyserver
65
66  version (v)                         show version number
67  help (h,?)                          this help
68
69 See ${PGRM}(8) for more info.
70 EOF
71 }
72
73 # function to interact with the gpg keyring
74 gpg_host() {
75     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
76 }
77
78 # command to list the info about the host key, in colon format, to
79 # stdout
80 gpg_host_list() {
81     gpg_host --list-keys --with-colons --fixed-list-mode \
82         --with-fingerprint --with-fingerprint \
83         "0x${HOST_FINGERPRINT}!"
84
85 }
86
87 # command for edit key scripts, takes scripts on stdin
88 gpg_host_edit() {
89     gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
90 }
91
92 # export the host public key to the monkeysphere gpg pub key file
93 update_gpg_pub_file() {
94     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
95     gpg_host --export --armor --export-options export-minimal \
96         "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
97 }
98
99 # load the host fingerprint into the fingerprint variable, using the
100 # export gpg pub key file
101 # FIXME: this seems much less than ideal, with all this temp keyring
102 # stuff.  is there a way we can do this without having to create temp
103 # files?  what if we stored the fingerprint in MHDATADIR/fingerprint?
104 load_fingerprint() {
105     if [ -f "$HOST_KEY_FILE" ] ; then
106         HOST_FINGERPRINT=$( \
107             (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
108             && gpg --quiet --import \
109             && gpg --quiet --list-keys --with-colons --with-fingerprint \
110             && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
111             | grep '^fpr:' | cut -d: -f10 )
112     else
113         failure "host key gpg pub file not found."
114     fi
115 }
116
117 # load the host fingerprint into the fingerprint variable, using the
118 # gpg host secret key
119 load_fingerprint_secret() {
120     HOST_FINGERPRINT=$( \
121         gpg_host --list-secret-key --with-colons --with-fingerprint \
122         | grep '^fpr:' | cut -d: -f10 )
123 }
124
125 # fail if host key present
126 check_host_key() {
127     [ ! -s "$HOST_KEY_FILE" ] \
128         || failure "An OpenPGP host key already exists."
129 }
130
131 # fail if host key not present
132 check_host_no_key() {
133     [ -s "$HOST_KEY_FILE" ] \
134         || failure "You don't appear to have a Monkeysphere host key on this server.
135 Please run 'monkeysphere-host import-key...' first."
136 }
137
138 # output the index of a user ID on the host key
139 # return 1 if user ID not found
140 find_host_userid() {
141     local userID="$1"
142     local tmpuidMatch
143     local line
144
145     # match to only ultimately trusted user IDs
146     tmpuidMatch="u:$(echo $userID | gpg_escape)"
147
148     # find the index of the requsted user ID
149     # NOTE: this is based on circumstantial evidence that the order of
150     # this output is the appropriate index
151     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
152         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
153
154     if [ "$line" ] ; then
155         echo ${line%%:*}
156         return 0
157     else
158         return 1
159     fi
160 }
161
162 # show info about the host key
163 show_key() {
164     local GNUPGHOME
165
166     # tmp gpghome dir
167     export GNUPGHOME=$(msmktempdir)
168
169     # trap to remove tmp dir if break
170     trap "rm -rf $GNUPGHOME" EXIT
171
172     # import the host key into the tmp dir
173     gpg --quiet --import <"$HOST_KEY_FILE"
174
175     # create the ssh key
176     TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
177     openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null >"$TMPSSH"
178
179     # get the gpg fingerprint
180     HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
181         | grep '^fpr:' | cut -d: -f10 )
182
183     # list the host key info
184     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
185     # FIXME: can we show uid validity somehow?
186     gpg --list-keys --fingerprint \
187         --list-options show-unusable-uids 2>/dev/null \
188         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
189         | egrep -v '^-+$'
190
191     # list the pgp fingerprint
192     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
193
194     # list the ssh fingerprint
195     echo -n "ssh fingerprint: "
196     ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
197
198     # remove the tmp file
199     trap - EXIT
200     rm -rf "$GNUPGHOME"
201 }
202
203 ########################################################################
204 # MAIN
205 ########################################################################
206
207 # load configuration file
208 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
209     && . "$MONKEYSPHERE_HOST_CONFIG"
210
211 # set empty config variable with ones from the environment, or with
212 # defaults
213 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
214 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
215 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
216 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
217 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
218
219 # other variables
220 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
221
222 # export variables needed in su invocation
223 export DATE
224 export LOG_LEVEL
225 export KEYSERVER
226 export CHECK_KEYSERVER
227 export MONKEYSPHERE_USER
228 export PROMPT
229 export GNUPGHOME_HOST
230 export GNUPGHOME
231 export HOST_FINGERPRINT
232
233 # get subcommand
234 COMMAND="$1"
235 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
236 shift
237
238 case $COMMAND in
239     'import-key'|'i')
240         check_host_key
241         source "${MHSHAREDIR}/import_key"
242         import_key "$@"
243         ;;
244
245     'show-key'|'show'|'s')
246         check_host_no_key
247         show_key
248         ;;
249
250     'set-expire'|'extend-key'|'e')
251         check_host_no_key
252         load_fingerprint
253         source "${MHSHAREDIR}/set_expire"
254         set_expire "$@"
255         ;;
256
257     'add-hostname'|'add-name'|'n+')
258         check_host_no_key
259         load_fingerprint
260         source "${MHSHAREDIR}/add_hostname"
261         add_hostname "$@"
262         ;;
263
264     'revoke-hostname'|'revoke-name'|'n-')
265         check_host_no_key
266         load_fingerprint
267         source "${MHSHAREDIR}/revoke_hostname"
268         revoke_hostname "$@"
269         ;;
270
271     'add-revoker'|'o')
272         check_host_no_key
273         load_fingerprint
274         source "${MHSHAREDIR}/add_revoker"
275         add_revoker "$@"
276         ;;
277
278     'revoke-key'|'r')
279         check_host_no_key
280         load_fingerprint
281         source "${MHSHAREDIR}/revoke_key"
282         revoke_key "$@"
283         ;;
284
285     'publish-key'|'publish'|'p')
286         check_host_no_key
287         load_fingerprint
288         source "${MHSHAREDIR}/publish_key"
289         publish_key
290         ;;
291
292     'diagnostics'|'d')
293         load_fingerprint
294         source "${MHSHAREDIR}/diagnostics"
295         diagnostics
296         ;;
297
298     'update-gpg-pub-file')
299         load_fingerprint_secret
300         update_gpg_pub_file
301         ;;
302
303     'version'|'v')
304         echo "$VERSION"
305         ;;
306
307     '--help'|'help'|'-h'|'h'|'?')
308         usage
309         ;;
310
311     *)
312         failure "Unknown command: '$COMMAND'
313 Type '$PGRM help' for usage."
314         ;;
315 esac
316
317 exit "$RETURN"