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