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"
36 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
37 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
39 # UTC date in ISO 8601 format if needed
40 DATE=$(date -u '+%FT%T')
42 # unset some environment variables that could screw things up
48 ########################################################################
50 ########################################################################
54 usage: $PGRM <subcommand> [options] [args]
55 Monkeysphere host admin tool.
58 show-key (s) output all host key information
59 set-expire (e) EXPIRE set host key expiration
60 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
61 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
62 add-revoker (o) FINGERPRINT add a revoker to the host key
63 revoke-key (r) revoke host key
64 publish-key (p) publish host key to keyserver
66 expert <expert-subcommand> run expert command
67 expert help expert command help
69 version (v) show version number
75 # function to interact with the gpg keyring
77 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
80 # command to list the info about the host key, in colon format
82 gpg_host --list-keys --with-colons --fixed-list-mode \
83 --with-fingerprint --with-fingerprint \
84 "0x${HOST_FINGERPRINT}!"
88 # command for edit key scripts, takes scripts on stdin
90 gpg_host --quiet --command-fd 0 --edit-key \
91 "0x${HOST_FINGERPRINT}!" "$@"
94 # export the host key to stdout
96 gpg_host --export --armor --export-options export-minimal \
97 "0x${HOST_FINGERPRINT}!"
100 # export the host secret key to the monkeysphere ssh sec key file
101 # NOTE: assumes that the primary key is the proper key to use
102 create_ssh_sec_file() {
103 log debug "creating ssh secret key file..."
105 gpg_host --export-secret-key "$HOST_FINGERPRINT" | \
106 openpgp2ssh "$HOST_FINGERPRINT" > "${MHDATADIR}/ssh_host_rsa_key")
107 log info "SSH host secret key file: ${MHDATADIR}/ssh_host_rsa_key"
110 # export the host public key to the monkeysphere ssh pub key file
111 create_ssh_pub_file() {
112 log debug "creating ssh public key file..."
113 ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "$HOST_KEY_PUB"
114 log info "SSH host public key file: $HOST_KEY_PUB"
117 # export the host public key to the monkeysphere gpg pub key file
118 create_gpg_pub_file() {
119 log debug "creating openpgp public key file..."
120 gpg_host_export > "$HOST_KEY_PUB_GPG"
121 log info "GPG host public key file: $HOST_KEY_PUB_GPG"
124 # load the host fingerprint into the fingerprint variable, using the
125 # export gpg pub key file
126 # FIXME: this seems much less than ideal, with all this temp keyring
127 # stuff. is there a way we can do this without having to create temp
130 if [ -f "$HOST_KEY_PUB_GPG" ] ; then
131 HOST_FINGERPRINT=$( \
132 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
133 && gpg --quiet --import \
134 && gpg --quiet --list-keys --with-colons --with-fingerprint \
135 && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
136 | grep '^fpr:' | cut -d: -f10 )
142 # load the host fingerprint into the fingerprint variable, using the
143 # gpg host secret key
144 load_fingerprint_secret() {
145 HOST_FINGERPRINT=$( \
146 gpg_host --quiet --list-secret-key \
147 --with-colons --with-fingerprint \
148 | grep '^fpr:' | cut -d: -f10 )
151 # output host key ssh fingerprint
152 load_ssh_fingerprint() {
153 [ -f "$HOST_KEY_PUB" ] || return 0
154 HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
155 | awk '{ print $1, $2, $4 }')
158 # fail if host key present
160 [ -z "$HOST_FINGERPRINT" ] \
161 || failure "An OpenPGP host key already exists."
164 # fail if host key not present
165 check_host_no_key() {
166 [ "$HOST_FINGERPRINT" ] \
167 || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
170 # output the index of a user ID on the host key
171 # return 1 if user ID not found
177 # match to only ultimately trusted user IDs
178 tmpuidMatch="u:$(echo $userID | gpg_escape)"
180 # find the index of the requsted user ID
181 # NOTE: this is based on circumstantial evidence that the order of
182 # this output is the appropriate index
183 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
184 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
186 if [ "$line" ] ; then
194 # show info about the host key
196 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
197 "0x${HOST_FINGERPRINT}!" 2>/dev/null
198 # FIXME: make sure expiration date is shown
200 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
204 if [ "$HOST_FINGERPRINT_SSH" ] ; then
205 echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
207 log error "SSH host key not found."
210 # FIXME: other relevant key parameters?
213 ########################################################################
215 ########################################################################
217 # unset variables that should be defined only in config file
219 unset MONKEYSPHERE_USER
221 # load configuration file
222 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
224 # set empty config variable with ones from the environment, or with
226 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
227 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
228 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
229 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
230 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
233 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
234 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
236 # export variables needed in su invocation
240 export MONKEYSPHERE_USER
242 export GNUPGHOME_HOST
244 export HOST_FINGERPRINT=
245 export HOST_FINGERPRINT_SSH=
249 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
253 'show-key'|'show'|'s')
259 'set-expire'|'extend-key'|'e')
262 source "${MHSHAREDIR}/set_expire"
266 'add-hostname'|'add-name'|'n+')
269 source "${MHSHAREDIR}/add_hostname"
273 'revoke-hostname'|'revoke-name'|'n-')
276 source "${MHSHAREDIR}/revoke_hostname"
283 source "${MHSHAREDIR}/add_revoker"
290 source "${MHSHAREDIR}/revoke_key"
294 'publish-key'|'publish'|'p')
297 source "${MHSHAREDIR}/publish_key"
304 case "$SUBCOMMAND" in
307 usage: $PGRM expert <subcommand> [options] [args]
310 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
311 gen-key (g) [NAME[:PORT]] generate gpg key for the host
312 --length (-l) BITS key length in bits (2048)
313 diagnostics (d) monkeysphere host status
321 source "${MHSHAREDIR}/import_key"
328 source "${MHSHAREDIR}/gen_key"
333 source "${MHSHAREDIR}/diagnostics"
338 failure "Unknown expert subcommand: '$COMMAND'
339 Type '$PGRM help' for usage."
348 '--help'|'help'|'-h'|'h'|'?')
353 failure "Unknown command: '$COMMAND'
354 Type '$PGRM help' for usage."