3 # monkeysphere-host: Monkeysphere host admin tool
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>
11 # They are Copyright 2008-2010, and are all released under the GPL,
14 ########################################################################
17 # set the pipefail option so pipelines fail on first command failure
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
24 . "${SYSSHAREDIR}/defaultenv"
25 . "${SYSSHAREDIR}/common"
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
30 # sharedir for host functions
31 MHSHAREDIR="${SYSSHAREDIR}/mh"
33 # datadir for host functions
34 MHDATADIR="${SYSDATADIR}/host"
37 HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.pgp"
39 # UTC date in ISO 8601 format if needed
40 DATE=$(date -u '+%FT%T')
42 # unset some environment variables that could screw things up
45 ########################################################################
47 ########################################################################
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere host admin tool.
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]
65 revoke-key [KEYID] generate and/or publish revocation
68 version (v) show version number
71 See ${PGRM}(8) for more info.
75 # function to interact with the gpg keyring
77 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
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 \
87 # edit key scripts, takes scripts on stdin, and keyID as first input
89 gpg_host --command-fd 0 --edit-key "$@"
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:) \
100 # check that the service name is well formed
101 check_service_name() {
103 log error "FIX ME: check service name"
106 # fail if host key not present
108 [ -s "$HOST_KEY_FILE" ] \
109 || failure "You don't appear to have a Monkeysphere host key on this server.
110 Please run 'monkeysphere-host import-key' import a key."
113 # key input to functions, outputs full fingerprint of specified key if
117 # array of fingerprints
118 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
122 failure "You don't appear to have any Monkeysphere host keys.
123 Please run 'monkeysphere-host import-key' to import a key."
129 if [ -z "$keyID" ] ; then
130 failure "Your host keyring contains multiple keys.
131 Please specify one to act on (see 'monkeysphere-host show-keys')."
135 printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
136 || failure "Host key '$keyID' not found."
139 # return 0 if user ID was found.
140 # return 1 if user ID not found.
146 # match to only "unknown" user IDs (host has no need for ultimate trust)
147 tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
149 # See whether the requsted user ID is present
150 gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
151 grep -q -x -F "$tmpuidMatch" 2>/dev/null
154 # run command looped over keys
160 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
165 if [[ -z "$1" || "$1" == '--all' ]] ; then
169 for key in $keys ; do
170 if (( i++ > 0 )) ; then
171 echo "##############################"
177 # show info about the a key
186 export GNUPGHOME=$(msmktempdir)
188 # trap to remove tmp dir if break
189 trap "rm -rf $GNUPGHOME" EXIT
191 # import the host key into the tmp dir
192 gpg --quiet --import <"$HOST_KEY_FILE"
194 # get the gpg fingerprint
195 if gpg --quiet --list-keys \
196 --with-colons --with-fingerprint "$id" \
197 | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
198 fingerprint=$(cat "$GNUPGHOME"/fingerprint)
200 failure "ID '$id' not found."
204 tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
205 gpg --export "$fingerprint" 2>/dev/null \
206 | openpgp2ssh 2>/dev/null >"$tmpssh"
208 # list the host key info
209 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
210 # FIXME: can we show uid validity somehow?
211 gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
212 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
215 # list revokers, if there are any
216 revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
217 | awk -F: '/^rvk:/{ print $10 }' )
218 if [ "$revokers" ] ; then
219 echo "The following keys are allowed to revoke this host key:"
220 for key in $revokers ; do
226 # list the pgp fingerprint
227 echo "OpenPGP fingerprint: $fingerprint"
229 # list the ssh fingerprint
230 echo -n "ssh fingerprint: "
231 ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
233 # remove the tmp file
238 ########################################################################
240 ########################################################################
242 # load configuration file
243 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
244 && . "$MONKEYSPHERE_HOST_CONFIG"
246 # set empty config variable with ones from the environment, or with
248 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
249 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
250 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
251 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
252 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
253 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
256 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
257 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
259 # export variables needed in su invocation
263 export CHECK_KEYSERVER
264 export MONKEYSPHERE_USER
265 export MONKEYSPHERE_GROUP
267 export GNUPGHOME_HOST
269 export HOST_FINGERPRINT
272 if [ "$#" -eq 0 ] ; then
274 failure "Please supply a subcommand."
283 source "${MHSHAREDIR}/import_key"
287 'show-keys'|'show-key'|'show'|'s')
288 multi_key show_key "$@"
291 'set-expire'|'extend-key'|'e')
292 source "${MHSHAREDIR}/set_expire"
296 'add-servicename'|'add-hostname'|'add-name'|'n+')
297 source "${MHSHAREDIR}/add_name"
301 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
302 source "${MHSHAREDIR}/revoke_name"
307 source "${MHSHAREDIR}/add_revoker"
312 source "${MHSHAREDIR}/revoke_key"
316 'publish-keys'|'publish-key'|'publish'|'p')
317 source "${MHSHAREDIR}/publish_key"
318 multi_key publish_key "$@"
322 source "${MHSHAREDIR}/diagnostics"
326 'update-pgp-pub-file')
334 '--help'|'help'|'-h'|'h'|'?')
339 failure "Unknown command: '$COMMAND'
340 Try '$PGRM help' for usage."