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
127 if [ -f "$HOST_KEY_PUB_GPG" ] ; then
128 HOST_FINGERPRINT=$( \
129 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
130 && gpg --quiet --import \
131 && gpg --quiet --list-keys --with-colons --with-fingerprint \
132 && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
133 | grep '^fpr:' | cut -d: -f10 )
139 # load the host fingerprint into the fingerprint variable, using the
140 # gpg host secret key
141 load_fingerprint_secret() {
142 HOST_FINGERPRINT=$( \
143 gpg_host --quiet --list-secret-key \
144 --with-colons --with-fingerprint \
145 | grep '^fpr:' | cut -d: -f10 )
148 # output host key ssh fingerprint
149 load_ssh_fingerprint() {
150 [ -f "$HOST_KEY_PUB" ] || return 0
151 HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
152 | awk '{ print $1, $2, $4 }')
155 # fail if host key present
157 [ -z "$HOST_FINGERPRINT" ] \
158 || failure "An OpenPGP host key already exists."
161 # fail if host key not present
162 check_host_no_key() {
163 [ "$HOST_FINGERPRINT" ] \
164 || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
167 # output the index of a user ID on the host key
168 # return 1 if user ID not found
174 # match to only ultimately trusted user IDs
175 tmpuidMatch="u:$(echo $userID | gpg_escape)"
177 # find the index of the requsted user ID
178 # NOTE: this is based on circumstantial evidence that the order of
179 # this output is the appropriate index
180 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
181 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
183 if [ "$line" ] ; then
191 # show info about the host key
193 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
194 "0x${HOST_FINGERPRINT}!" 2>/dev/null
195 # FIXME: make sure expiration date is shown
197 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
201 if [ "$HOST_FINGERPRINT_SSH" ] ; then
202 echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
204 log error "SSH host key not found."
207 # FIXME: other relevant key parameters?
210 ########################################################################
212 ########################################################################
214 # unset variables that should be defined only in config file
216 unset MONKEYSPHERE_USER
218 # load configuration file
219 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
221 # set empty config variable with ones from the environment, or with
223 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
224 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
225 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
226 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
227 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
230 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
231 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
233 # export variables needed in su invocation
237 export MONKEYSPHERE_USER
239 export GNUPGHOME_HOST
241 export HOST_FINGERPRINT=
242 export HOST_FINGERPRINT_SSH=
246 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
250 'show-key'|'show'|'s')
256 'set-expire'|'extend-key'|'e')
259 source "${MHSHAREDIR}/set_expire"
263 'add-hostname'|'add-name'|'n+')
266 source "${MHSHAREDIR}/add_hostname"
270 'revoke-hostname'|'revoke-name'|'n-')
273 source "${MHSHAREDIR}/revoke_hostname"
280 source "${MHSHAREDIR}/add_revoker"
287 source "${MHSHAREDIR}/revoke_key"
291 'publish-key'|'publish'|'p')
294 source "${MHSHAREDIR}/publish_key"
301 case "$SUBCOMMAND" in
304 usage: $PGRM expert <subcommand> [options] [args]
307 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
308 gen-key (g) [NAME[:PORT]] generate gpg key for the host
309 --length (-l) BITS key length in bits (2048)
310 diagnostics (d) monkeysphere host status
318 source "${MHSHAREDIR}/import_key"
325 source "${MHSHAREDIR}/gen_key"
330 source "${MHSHAREDIR}/diagnostics"
335 failure "Unknown expert subcommand: '$COMMAND'
336 Type '$PGRM help' for usage."
345 '--help'|'help'|'-h'|'h'|'?')
350 failure "Unknown command: '$COMMAND'
351 Type '$PGRM help' for usage."