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 ########################################################################
17 # set the pipefail option so pipelines fail on first command failure
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
24 . "${SYSSHAREDIR}/common" || exit 1
26 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
29 # sharedir for host functions
30 MHSHAREDIR="${SYSSHAREDIR}/mh"
32 # datadir for host functions
33 MHDATADIR="${SYSDATADIR}/host"
35 # UTC date in ISO 8601 format if needed
36 DATE=$(date -u '+%FT%T')
38 # unset some environment variables that could screw things up
44 ########################################################################
46 ########################################################################
50 usage: $PGRM <subcommand> [options] [args]
51 Monkeysphere host admin tool.
54 show-key (s) output all host key information
55 set-expire (e) EXPIRE set host key expiration
56 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
57 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
58 add-revoker (o) FINGERPRINT add a revoker to the host key
59 revoke-key (r) revoke host key
60 publish-key (p) publish host key to keyserver
62 expert <expert-subcommand> run expert command
63 expert help expert command help
65 version (v) show version number
71 # function to run command as monkeysphere user
72 su_monkeysphere_user() {
73 # if the current user is the monkeysphere user, then just eval
75 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
78 # otherwise su command as monkeysphere user
80 su "$MONKEYSPHERE_USER" -c "$@"
84 # function to interact with the gpg keyring
86 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
89 # command to list the info about the host key, in colon format
91 gpg_host --list-keys --with-colons --fixed-list-mode \
92 --with-fingerprint --with-fingerprint \
93 "0x${HOST_FINGERPRINT}!"
97 # command for edit key scripts, takes scripts on stdin
99 gpg_host --quiet --command-fd 0 --edit-key \
100 "0x${HOST_FINGERPRINT}!" "$@"
103 # export the host key to stdout
105 gpg_host --export --armor --export-options export-minimal \
106 "0x${HOST_FINGERPRINT}!"
109 # export the host key to the monkeysphere host file key
110 gpg_host_export_to_ssh_file() {
111 log debug "exporting openpgp public key..."
113 gpg_host_export > "$HOST_KEY_PUB_GPG"
114 log info "SSH host public key in OpenPGP form: $HOST_KEY_PUB_GPG"
117 # output just key fingerprint
118 fingerprint_host_key() {
119 gpg_host --list-secret-keys --fingerprint \
120 --with-colons --fixed-list-mode 2> /dev/null | \
121 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
124 # output the index of a user ID on the host key
125 # return 1 if user ID not found
131 # match to only ultimately trusted user IDs
132 tmpuidMatch="u:$(echo $userID | gpg_escape)"
134 # find the index of the requsted user ID
135 # NOTE: this is based on circumstantial evidence that the order of
136 # this output is the appropriate index
137 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
138 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
140 if [ "$line" ] ; then
148 # function to check for host secret key
150 [ "$HOST_FINGERPRINT" ] || \
151 failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
154 # show info about the host key
158 # FIXME: should not have to be priviledged user to see this info.
159 # should be taken from publicly accessible key files, instead of
162 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
163 "0x${HOST_FINGERPRINT}!" 2>/dev/null
165 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
167 if [ -f "$HOST_KEY_PUB" ] ; then
168 fingerprintSSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" | \
169 awk '{ print $1, $2, $4 }')
171 echo "ssh fingerprint: $fingerprintSSH"
173 log error "SSH host key not found."
176 # FIXME: show expiration date
177 # FIXME: other relevant key parameters?
180 ########################################################################
182 ########################################################################
184 # unset variables that should be defined only in config file
186 unset MONKEYSPHERE_USER
188 # load configuration file
189 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
191 # set empty config variable with ones from the environment, or with
193 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
194 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
195 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
196 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
197 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
200 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
201 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
203 # host key fingerprint
204 HOST_FINGERPRINT=$(fingerprint_host_key)
207 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
208 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
210 # export variables needed in su invocation
214 export MONKEYSPHERE_USER
216 export GNUPGHOME_HOST
218 export HOST_FINGERPRINT
222 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
227 'show-key'|'show'|'s')
232 'set-expire'|'extend-key'|'e')
234 source "${MHSHAREDIR}/set_expire"
238 'add-hostname'|'add-name'|'n+')
240 source "${MHSHAREDIR}/add_hostname"
244 'revoke-hostname'|'revoke-name'|'n-')
246 source "${MHSHAREDIR}/revoke_hostname"
252 source "${MHSHAREDIR}/add_revoker"
258 source "${MHSHAREDIR}/revoke_key"
262 'publish-key'|'publish'|'p')
264 source "${MHSHAREDIR}/publish_key"
271 case "$SUBCOMMAND" in
274 usage: $PGRM expert <subcommand> [options] [args]
277 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
278 gen-key (g) [NAME[:PORT]] generate gpg key for the host
279 --length (-l) BITS key length in bits (2048)
280 diagnostics (d) monkeysphere host status
286 source "${MHSHAREDIR}/import_key"
291 source "${MHSHAREDIR}/gen_key"
296 source "${MHSHAREDIR}/diagnostics"
301 failure "Unknown expert subcommand: '$COMMAND'
302 Type '$PGRM help' for usage."
311 '--help'|'help'|'-h'|'h'|'?')
316 failure "Unknown command: '$COMMAND'
317 Type '$PGRM help' for usage."