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 interact with the gpg keyring
73 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
76 # command to list the info about the host key, in colon format
78 gpg_host --list-keys --with-colons --fixed-list-mode \
79 --with-fingerprint --with-fingerprint \
80 "0x${HOST_FINGERPRINT}!"
84 # command for edit key scripts, takes scripts on stdin
86 gpg_host --quiet --command-fd 0 --edit-key \
87 "0x${HOST_FINGERPRINT}!" "$@"
90 # export the host key to stdout
92 gpg_host --export --armor --export-options export-minimal \
93 "0x${HOST_FINGERPRINT}!"
96 # export the host key to the monkeysphere host file key
97 gpg_host_export_to_ssh_file() {
98 log debug "exporting openpgp public key..."
100 gpg_host_export > "$HOST_KEY_PUB_GPG"
101 log info "SSH host public key in OpenPGP form: $HOST_KEY_PUB_GPG"
104 # output just key fingerprint
105 fingerprint_host_key() {
106 gpg_host --list-secret-keys --fingerprint \
107 --with-colons --fixed-list-mode 2> /dev/null | \
108 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
111 # output the index of a user ID on the host key
112 # return 1 if user ID not found
118 # match to only ultimately trusted user IDs
119 tmpuidMatch="u:$(echo $userID | gpg_escape)"
121 # find the index of the requsted user ID
122 # NOTE: this is based on circumstantial evidence that the order of
123 # this output is the appropriate index
124 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
125 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
127 if [ "$line" ] ; then
135 # function to check for host secret key
137 [ "$HOST_FINGERPRINT" ] || \
138 failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
141 # show info about the host key
145 # FIXME: should not have to be priviledged user to see this info.
146 # should be taken from publicly accessible key files, instead of
149 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
150 "0x${HOST_FINGERPRINT}!" 2>/dev/null
152 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
154 if [ -f "$HOST_KEY_PUB" ] ; then
155 fingerprintSSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" | \
156 awk '{ print $1, $2, $4 }')
158 echo "ssh fingerprint: $fingerprintSSH"
160 log error "SSH host key not found."
163 # FIXME: show expiration date
164 # FIXME: other relevant key parameters?
167 ########################################################################
169 ########################################################################
171 # unset variables that should be defined only in config file
173 unset MONKEYSPHERE_USER
175 # load configuration file
176 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
178 # set empty config variable with ones from the environment, or with
180 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
181 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
182 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
183 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
184 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
187 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
188 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
190 # host key fingerprint
191 HOST_FINGERPRINT=$(fingerprint_host_key)
194 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
195 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
197 # export variables needed in su invocation
201 export MONKEYSPHERE_USER
203 export GNUPGHOME_HOST
205 export HOST_FINGERPRINT
209 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
214 'show-key'|'show'|'s')
219 'set-expire'|'extend-key'|'e')
221 source "${MHSHAREDIR}/set_expire"
225 'add-hostname'|'add-name'|'n+')
227 source "${MHSHAREDIR}/add_hostname"
231 'revoke-hostname'|'revoke-name'|'n-')
233 source "${MHSHAREDIR}/revoke_hostname"
239 source "${MHSHAREDIR}/add_revoker"
245 source "${MHSHAREDIR}/revoke_key"
249 'publish-key'|'publish'|'p')
251 source "${MHSHAREDIR}/publish_key"
258 case "$SUBCOMMAND" in
261 usage: $PGRM expert <subcommand> [options] [args]
264 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
265 gen-key (g) [NAME[:PORT]] generate gpg key for the host
266 --length (-l) BITS key length in bits (2048)
267 diagnostics (d) monkeysphere host status
273 source "${MHSHAREDIR}/import_key"
278 source "${MHSHAREDIR}/gen_key"
283 source "${MHSHAREDIR}/diagnostics"
288 failure "Unknown expert subcommand: '$COMMAND'
289 Type '$PGRM help' for usage."
298 '--help'|'help'|'-h'|'h'|'?')
303 failure "Unknown command: '$COMMAND'
304 Type '$PGRM help' for usage."