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