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-2009, and are all released under the GPL,
14 ########################################################################
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
21 . "${SYSSHAREDIR}/common" || exit 1
23 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
26 # sharedir for host functions
27 MHSHAREDIR="${SYSSHAREDIR}/mh"
29 # datadir for host functions
30 MHDATADIR="${SYSDATADIR}/host"
32 # UTC date in ISO 8601 format if needed
33 DATE=$(date -u '+%FT%T')
35 # unset some environment variables that could screw things up
41 ########################################################################
43 ########################################################################
47 usage: $PGRM <subcommand> [options] [args]
48 Monkeysphere host admin tool.
51 show-key (s) output all host key information
52 set-expire (e) EXPIRE set host key expiration
53 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
54 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
55 add-revoker (o) FINGERPRINT add a revoker to the host key
56 revoke-key (r) revoke host key
57 publish-key (p) publish host key to keyserver
59 expert <expert-subcommand> run expert command
60 expert help expert command help
62 version (v) show version number
68 # function to run command as monkeysphere user
69 su_monkeysphere_user() {
70 # if the current user is the monkeysphere user, then just eval
72 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
75 # otherwise su command as monkeysphere user
77 su "$MONKEYSPHERE_USER" -c "$@"
81 # function to interact with the gpg keyring
83 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
86 # command to list the info about the host key, in colon format
88 gpg_host --list-keys --with-colons --fixed-list-mode \
89 --with-fingerprint --with-fingerprint \
90 "0x${HOST_FINGERPRINT}!"
94 # command for edit key scripts, takes scripts on stdin
96 gpg_host --quiet --command-fd 0 --edit-key \
97 "0x${HOST_FINGERPRINT}!" "$@"
100 # output just key fingerprint
101 fingerprint_host_key() {
102 # set the pipefail option so functions fails if can't read sec key
105 gpg_host --list-secret-keys --fingerprint \
106 --with-colons --fixed-list-mode 2> /dev/null | \
107 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
110 # output the index of a user ID on the host key
111 # return 1 if user ID not found
117 # match to only ultimately trusted user IDs
118 tmpuidMatch="u:$(echo $userID | gpg_escape)"
120 # find the index of the requsted user ID
121 # NOTE: this is based on circumstantial evidence that the order of
122 # this output is the appropriate index
123 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
124 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
126 if [ "$line" ] ; then
134 # function to check for host secret key
136 [ "$HOST_FINGERPRINT" ] || \
137 failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
140 # show info about the host key
144 # FIXME: should not have to be priviledged user to see this info.
145 # should be taken from publicly accessible key files, instead of
148 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
149 "0x${HOST_FINGERPRINT}!" 2>/dev/null
151 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
153 if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
154 fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
155 awk '{ print $1, $2, $4 }')
156 echo "ssh fingerprint: $fingerprintSSH"
158 log info "SSH host key not found."
161 # FIXME: show expiration date
162 # FIXME: other relevant key parameters?
165 ########################################################################
167 ########################################################################
169 # unset variables that should be defined only in config file
171 unset MONKEYSPHERE_USER
173 # load configuration file
174 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
176 # set empty config variable with ones from the environment, or with
178 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
179 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
180 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
181 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
182 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
185 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
186 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
188 # host key fingerprint
189 HOST_FINGERPRINT=$(fingerprint_host_key)
191 # export variables needed in su invocation
195 export MONKEYSPHERE_USER
197 export GNUPGHOME_HOST
199 export HOST_FINGERPRINT
203 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
208 'show-key'|'show'|'s')
213 'set-expire'|'extend-key'|'e')
215 source "${MHSHAREDIR}/set_expire"
219 'add-hostname'|'add-name'|'n+')
221 source "${MHSHAREDIR}/add_hostname"
225 'revoke-hostname'|'revoke-name'|'n-')
227 source "${MHSHAREDIR}/revoke_hostname"
233 source "${MHSHAREDIR}/add_revoker"
239 source "${MHSHAREDIR}/revoke_key"
243 'publish-key'|'publish'|'p')
245 source "${MHSHAREDIR}/publish_key"
252 case "$SUBCOMMAND" in
255 usage: $PGRM expert <subcommand> [options] [args]
258 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
259 gen-key (g) [NAME[:PORT]] generate gpg key for the host
260 --length (-l) BITS key length in bits (2048)
261 diagnostics (d) monkeysphere host status
267 source "${MHSHAREDIR}/import_key"
272 source "${MHSHAREDIR}/gen_key"
277 source "${MHSHAREDIR}/diagnostics"
282 failure "Unknown expert subcommand: '$COMMAND'
283 Type '$PGRM help' for usage."
292 '--help'|'help'|'-h'|'h'|'?')
297 failure "Unknown command: '$COMMAND'
298 Type '$PGRM help' for usage."