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