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 # export the host key to stdout
102 gpg_host --export --armor --export-options export-minimal \
103 "0x${HOST_FINGERPRINT}!"
106 # export the host key to the monkeysphere host file key
107 gpg_host_export_to_ssh_file() {
108 log debug "exporting openpgp public key..."
109 gpg_host_export > "${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
110 log info "SSH host public key in OpenPGP form: ${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
113 # output just key fingerprint
114 fingerprint_host_key() {
115 # set the pipefail option so functions fails if can't read sec key
118 gpg_host --list-secret-keys --fingerprint \
119 --with-colons --fixed-list-mode 2> /dev/null | \
120 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
123 # output the index of a user ID on the host key
124 # return 1 if user ID not found
130 # match to only ultimately trusted user IDs
131 tmpuidMatch="u:$(echo $userID | gpg_escape)"
133 # find the index of the requsted user ID
134 # NOTE: this is based on circumstantial evidence that the order of
135 # this output is the appropriate index
136 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
137 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
139 if [ "$line" ] ; then
147 # function to check for host secret key
149 [ "$HOST_FINGERPRINT" ] || \
150 failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
153 # show info about the host key
157 # FIXME: should not have to be priviledged user to see this info.
158 # should be taken from publicly accessible key files, instead of
161 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
162 "0x${HOST_FINGERPRINT}!" 2>/dev/null
164 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
166 if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
167 fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
168 awk '{ print $1, $2, $4 }')
169 echo "ssh fingerprint: $fingerprintSSH"
171 log info "SSH host key not found."
174 # FIXME: show expiration date
175 # FIXME: other relevant key parameters?
178 ########################################################################
180 ########################################################################
182 # unset variables that should be defined only in config file
184 unset MONKEYSPHERE_USER
186 # load configuration file
187 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
189 # set empty config variable with ones from the environment, or with
191 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
192 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
193 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
194 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
195 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
198 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
199 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
201 # host key fingerprint
202 HOST_FINGERPRINT=$(fingerprint_host_key)
204 # export variables needed in su invocation
208 export MONKEYSPHERE_USER
210 export GNUPGHOME_HOST
212 export HOST_FINGERPRINT
216 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
221 'show-key'|'show'|'s')
226 'set-expire'|'extend-key'|'e')
228 source "${MHSHAREDIR}/set_expire"
232 'add-hostname'|'add-name'|'n+')
234 source "${MHSHAREDIR}/add_hostname"
238 'revoke-hostname'|'revoke-name'|'n-')
240 source "${MHSHAREDIR}/revoke_hostname"
246 source "${MHSHAREDIR}/add_revoker"
252 source "${MHSHAREDIR}/revoke_key"
256 'publish-key'|'publish'|'p')
258 source "${MHSHAREDIR}/publish_key"
265 case "$SUBCOMMAND" in
268 usage: $PGRM expert <subcommand> [options] [args]
271 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
272 gen-key (g) [NAME[:PORT]] generate gpg key for the host
273 --length (-l) BITS key length in bits (2048)
274 diagnostics (d) monkeysphere host status
280 source "${MHSHAREDIR}/import_key"
285 source "${MHSHAREDIR}/gen_key"
290 source "${MHSHAREDIR}/diagnostics"
295 failure "Unknown expert subcommand: '$COMMAND'
296 Type '$PGRM help' for usage."
305 '--help'|'help'|'-h'|'h'|'?')
310 failure "Unknown command: '$COMMAND'
311 Type '$PGRM help' for usage."