remove reference to HOST_KEY_FPR_FILE
[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-2010, 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}/host_keys.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 SERVICENAME       import PEM-encoded key from file
56  show-keys (s) [KEYID ...]             output host key information
57  publish-keys (p) [KEYID ...]          publish key(s) to keyserver
58  set-expire (e) EXPIRE [KEYID]         set key expiration
59  add-servicename (n+) SERVICENAME [KEYID]
60                                        add a service name to key
61  revoke-servicename (n-) SERVICENAME [KEYID]
62                                        revoke a service name from key
63  add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
64                                        add a revoker to key
65  revoke-key [KEYID]                    generate and/or publish revocation
66                                        certificate for key
67
68  version (v)                           show version number
69  help (h,?)                            this help
70
71 See ${PGRM}(8) for more info.
72 EOF
73 }
74
75 # function to interact with the gpg keyring
76 gpg_host() {
77     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
78 }
79
80 # list the info about the a key, in colon format, to stdout
81 gpg_host_list_keys() {
82     gpg_host --list-keys --with-colons --fixed-list-mode \
83         --with-fingerprint --with-fingerprint \
84         "$1"
85 }
86
87 # edit key scripts, takes scripts on stdin, and keyID as first input
88 gpg_host_edit() {
89     gpg_host --command-fd 0 --edit-key "$@"
90 }
91
92 # export 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 > "$HOST_KEY_FILE"
96     log debug "updating fingerprint file '$HOST_KEY_FPR_FILE'..."
97     gpg_host --list-secret-key --with-colons --with-fingerprint \
98         | awk -F: '/^fpr:/{ print $10 }' > "$HOST_KEY_FPR_FILE"
99 }
100
101 host_fingerprints() {
102     local fprs=($(<"$HOST_KEY_FILE" "$SYSSHAREDIR/keytrans" listfprs))
103
104     log debug "host key fingerprints:"
105     printf '%s\n' "${fprs[@]}" | log debug
106     printf '%s\n' "${fprs[@]}"
107 }
108
109 # check that the service name is well formed
110 check_service_name() {
111     local name="$1"
112     log error "FIX ME: check service name"
113 }
114
115 # fail if host key not present
116 check_no_keys() {
117     [ -s "$HOST_KEY_FILE" ] \
118         || failure "You don't appear to have a Monkeysphere host key on this server.
119 Please run 'monkeysphere-host import-key' import a key."
120 }
121
122 # key input to functions, outputs full fingerprint of specified key if
123 # found
124 check_key_input() {
125     local keyID="$1"
126     # array of fingerprints
127     local fprs=($(host_fingerprints))
128
129     case ${#fprs[@]} in
130         0)
131             failure "You don't appear to have any Monkeysphere host keys.
132 Please run 'monkeysphere-host import-key' to import a key."
133             ;;
134         1)
135             :
136             ;;
137         *)
138             if [ -z "$keyID" ] ; then
139                 failure "Your host keyring contains multiple keys.
140 Please specify one to act on (see 'monkeysphere-host show-key')."
141             fi
142             ;;
143     esac
144     printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
145         || failure "Host key '$keyID' not found."
146 }
147
148 # return 0 if user ID was found.
149 # return 1 if user ID not found.
150 check_key_userid() {
151     local keyID="$1"
152     local userID="$2"
153     local tmpuidMatch
154
155     # match to only "unknown" user IDs (host has no need for ultimate trust)
156     tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
157
158     # See whether the requsted user ID is present
159     gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
160         grep -q -x -F "$tmpuidMatch" 2>/dev/null
161 }
162
163 # run command looped over keys
164 multi_key() {
165     local cmd="$1"
166     shift
167     local keys=$@
168     local i=0
169     local fprs=($(host_fingerprints))
170     local key
171
172     check_no_keys
173
174     if [[ -z "$1" || "$1" == '--all' ]] ; then
175         keys="${fprs[@]}"
176     fi
177
178     for key in $keys ; do
179         if (( i++ > 0 )) ; then
180             echo "##############################"
181         fi
182         eval "$cmd" "$key"
183     done
184 }
185
186 # show info about the a key
187 show_key() {
188     local id="$1"
189     local GNUPGHOME
190     local fingerprint
191     local tmpssh
192     local revokers
193
194     # tmp gpghome dir
195     export GNUPGHOME=$(msmktempdir)
196
197     # trap to remove tmp dir if break
198     trap "rm -rf $GNUPGHOME" EXIT
199
200     # import the host key into the tmp dir
201     gpg --quiet --import <"$HOST_KEY_FILE"
202
203     # get the gpg fingerprint
204     if gpg --quiet --list-keys \
205         --with-colons --with-fingerprint "$id" \
206         | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
207         fingerprint=$(cat "$GNUPGHOME"/fingerprint)
208     else
209         failure "ID '$id' not found."
210     fi
211
212     # create the ssh key
213     tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
214     gpg --export "$fingerprint" 2>/dev/null \
215         | openpgp2ssh 2>/dev/null >"$tmpssh"
216
217     # list the host key info
218     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
219     # FIXME: can we show uid validity somehow?
220     gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
221         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
222         | egrep -v '^-+$'
223
224     # list revokers, if there are any
225     revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
226         | awk -F: '/^rvk:/{ print $10 }' )
227     if [ "$revokers" ] ; then
228         echo "The following keys are allowed to revoke this host key:"
229         for key in $revokers ; do
230             echo "revoker: $key"
231         done
232         echo
233     fi
234
235     # list the pgp fingerprint
236     echo "OpenPGP fingerprint: $fingerprint"
237
238     # list the ssh fingerprint
239     echo -n "ssh fingerprint: "
240     ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
241
242     # remove the tmp file
243     trap - EXIT
244     rm -rf "$GNUPGHOME"
245 }
246
247 ########################################################################
248 # MAIN
249 ########################################################################
250
251 # load configuration file
252 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
253     && . "$MONKEYSPHERE_HOST_CONFIG"
254
255 # set empty config variable with ones from the environment, or with
256 # defaults
257 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
258 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
259 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
260 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
261 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
262 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
263
264 # other variables
265 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
266 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
267
268 # export variables needed in su invocation
269 export DATE
270 export LOG_LEVEL
271 export KEYSERVER
272 export CHECK_KEYSERVER
273 export MONKEYSPHERE_USER
274 export MONKEYSPHERE_GROUP
275 export PROMPT
276 export GNUPGHOME_HOST
277 export GNUPGHOME
278 export HOST_FINGERPRINT
279 export LOG_PREFIX
280
281 if [ "$#" -eq 0 ] ; then 
282     usage
283     failure "Please supply a subcommand."
284 fi
285
286 # get subcommand
287 COMMAND="$1"
288 shift
289
290 case $COMMAND in
291     'import-key'|'i')
292         source "${MHSHAREDIR}/import_key"
293         import_key "$@"
294         ;;
295
296     'show-keys'|'show-key'|'show'|'s')
297         multi_key show_key "$@"
298         ;;
299
300     'set-expire'|'extend-key'|'e')
301         source "${MHSHAREDIR}/set_expire"
302         set_expire "$@"
303         ;;
304
305     'add-servicename'|'add-hostname'|'add-name'|'n+')
306         source "${MHSHAREDIR}/add_name"
307         add_name "$@"
308         ;;
309
310     'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
311         source "${MHSHAREDIR}/revoke_name"
312         revoke_name "$@"
313         ;;
314
315     'add-revoker'|'r+')
316         source "${MHSHAREDIR}/add_revoker"
317         add_revoker "$@"
318         ;;
319
320     'revoke-key')
321         source "${MHSHAREDIR}/revoke_key"
322         revoke_key "$@"
323         ;;
324
325     'publish-keys'|'publish-key'|'publish'|'p')
326         source "${MHSHAREDIR}/publish_key"
327         multi_key publish_key "$@"
328         ;;
329
330     'diagnostics'|'d')
331         source "${MHSHAREDIR}/diagnostics"
332         diagnostics
333         ;;
334
335     'update-gpg-pub-file')
336         update_gpg_pub_file
337         ;;
338
339     'version'|'v')
340         version
341         ;;
342
343     '--help'|'help'|'-h'|'h'|'?')
344         usage
345         ;;
346
347     *)
348         failure "Unknown command: '$COMMAND'
349 Try '$PGRM help' for usage."
350         ;;
351 esac