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 # temp directory for temp gnupghome directories for add_revoker
36 MHTMPDIR="${MHDATADIR}/tmp"
40 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
41 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
43 # UTC date in ISO 8601 format if needed
44 DATE=$(date -u '+%FT%T')
46 # unset some environment variables that could screw things up
52 ########################################################################
54 ########################################################################
58 usage: $PGRM <subcommand> [options] [args]
59 Monkeysphere host admin tool.
62 show-key (s) output all host key information
63 set-expire (e) EXPIRE set host key expiration
64 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
65 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
66 add-revoker (o) FINGERPRINT add a revoker to the host key
67 revoke-key (r) revoke host key
68 publish-key (p) publish host key to keyserver
70 expert <expert-subcommand> run expert command
71 expert help expert command help
73 version (v) show version number
79 # function to interact with the gpg keyring
81 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
84 # command to list the info about the host key, in colon format
86 gpg_host --list-keys --with-colons --fixed-list-mode \
87 --with-fingerprint --with-fingerprint \
88 "0x${HOST_FINGERPRINT}!"
92 # command for edit key scripts, takes scripts on stdin
94 gpg_host --quiet --command-fd 0 --edit-key \
95 "0x${HOST_FINGERPRINT}!" "$@"
98 # export the host key to stdout
100 gpg_host --export --armor --export-options export-minimal \
101 "0x${HOST_FINGERPRINT}!"
104 # export the host public key to the monkeysphere gpg pub key file
105 create_gpg_pub_file() {
106 log debug "creating openpgp public key file..."
107 gpg_host_export > "$HOST_KEY_PUB_GPG"
108 log info "GPG host public key file: $HOST_KEY_PUB_GPG"
111 # load the host fingerprint into the fingerprint variable, using the
112 # export gpg pub key file
113 # FIXME: this seems much less than ideal, with all this temp keyring
114 # stuff. is there a way we can do this without having to create temp
117 if [ -f "$HOST_KEY_PUB_GPG" ] ; then
118 HOST_FINGERPRINT=$( \
119 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
120 && gpg --quiet --import \
121 && gpg --quiet --list-keys --with-colons --with-fingerprint \
122 && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
123 | grep '^fpr:' | cut -d: -f10 )
129 # load the host fingerprint into the fingerprint variable, using the
130 # gpg host secret key
131 load_fingerprint_secret() {
132 HOST_FINGERPRINT=$( \
133 gpg_host --quiet --list-secret-key \
134 --with-colons --with-fingerprint \
135 | grep '^fpr:' | cut -d: -f10 )
138 # output host key ssh fingerprint
139 load_ssh_fingerprint() {
140 [ -f "$HOST_KEY_PUB" ] || return 0
141 HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
142 | awk '{ print $1, $2, $4 }')
145 # fail if host key present
147 [ -z "$HOST_FINGERPRINT" ] \
148 || failure "An OpenPGP host key already exists."
151 # fail if host key not present
152 check_host_no_key() {
153 [ "$HOST_FINGERPRINT" ] \
154 || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
157 # output the index of a user ID on the host key
158 # return 1 if user ID not found
164 # match to only ultimately trusted user IDs
165 tmpuidMatch="u:$(echo $userID | gpg_escape)"
167 # find the index of the requsted user ID
168 # NOTE: this is based on circumstantial evidence that the order of
169 # this output is the appropriate index
170 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
171 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
173 if [ "$line" ] ; then
181 # show info about the host key
183 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
184 "0x${HOST_FINGERPRINT}!" 2>/dev/null || true
185 # FIXME: make sure expiration date is shown
187 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
191 if [ "$HOST_FINGERPRINT_SSH" ] ; then
192 echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
194 log error "SSH host key not found."
197 # FIXME: other relevant key parameters?
200 ########################################################################
202 ########################################################################
204 # unset variables that should be defined only in config file
206 unset MONKEYSPHERE_USER
208 # load configuration file
209 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
211 # set empty config variable with ones from the environment, or with
213 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
214 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
215 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
216 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
217 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
220 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
221 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
223 # export variables needed in su invocation
227 export MONKEYSPHERE_USER
229 export GNUPGHOME_HOST
231 export HOST_FINGERPRINT=
232 export HOST_FINGERPRINT_SSH=
236 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
240 'show-key'|'show'|'s')
246 'set-expire'|'extend-key'|'e')
249 source "${MHSHAREDIR}/set_expire"
253 'add-hostname'|'add-name'|'n+')
256 source "${MHSHAREDIR}/add_hostname"
260 'revoke-hostname'|'revoke-name'|'n-')
263 source "${MHSHAREDIR}/revoke_hostname"
270 source "${MHSHAREDIR}/add_revoker"
277 source "${MHSHAREDIR}/revoke_key"
281 'publish-key'|'publish'|'p')
284 source "${MHSHAREDIR}/publish_key"
291 case "$SUBCOMMAND" in
294 usage: $PGRM expert <subcommand> [options] [args]
297 import-key (i) FILE [NAME[:PORT]] import existing ssh key to gpg
298 gen-key (g) [NAME[:PORT]] generate gpg key for the host
299 --length (-l) BITS key length in bits (2048)
300 diagnostics (d) monkeysphere host status
308 source "${MHSHAREDIR}/import_key"
315 source "${MHSHAREDIR}/gen_key"
320 source "${MHSHAREDIR}/diagnostics"
325 failure "Unknown expert subcommand: '$COMMAND'
326 Type '$PGRM help' for usage."
335 '--help'|'help'|'-h'|'h'|'?')
340 failure "Unknown command: '$COMMAND'
341 Type '$PGRM help' for usage."