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 public key to the monkeysphere gpg pub key file
101 create_gpg_pub_file() {
102 log debug "creating openpgp public key file..."
103 gpg_host_export > "$HOST_KEY_PUB_GPG"
104 log info "GPG host public key file: $HOST_KEY_PUB_GPG"
107 # load the host fingerprint into the fingerprint variable, using the
108 # export gpg pub key file
109 # FIXME: this seems much less than ideal, with all this temp keyring
110 # stuff. is there a way we can do this without having to create temp
113 if [ -f "$HOST_KEY_PUB_GPG" ] ; then
114 HOST_FINGERPRINT=$( \
115 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
116 && gpg --quiet --import \
117 && gpg --quiet --list-keys --with-colons --with-fingerprint \
118 && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
119 | grep '^fpr:' | cut -d: -f10 )
125 # load the host fingerprint into the fingerprint variable, using the
126 # gpg host secret key
127 load_fingerprint_secret() {
128 HOST_FINGERPRINT=$( \
129 gpg_host --quiet --list-secret-key \
130 --with-colons --with-fingerprint \
131 | grep '^fpr:' | cut -d: -f10 )
134 # output host key ssh fingerprint
135 load_ssh_fingerprint() {
136 [ -f "$HOST_KEY_PUB" ] || return 0
137 HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
138 | awk '{ print $1, $2, $4 }')
141 # fail if host key present
143 [ -z "$HOST_FINGERPRINT" ] \
144 || failure "An OpenPGP host key already exists."
147 # fail if host key not present
148 check_host_no_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 # output the index of a user ID on the host key
154 # return 1 if user ID not found
160 # match to only ultimately trusted user IDs
161 tmpuidMatch="u:$(echo $userID | gpg_escape)"
163 # find the index of the requsted user ID
164 # NOTE: this is based on circumstantial evidence that the order of
165 # this output is the appropriate index
166 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
167 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
169 if [ "$line" ] ; then
177 # show info about the host key
179 gpg_host --fingerprint --list-key --list-options show-unusable-uids \
180 "0x${HOST_FINGERPRINT}!" 2>/dev/null || true
181 # FIXME: make sure expiration date is shown
183 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
187 if [ "$HOST_FINGERPRINT_SSH" ] ; then
188 echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
190 log error "SSH host key not found."
193 # FIXME: other relevant key parameters?
196 ########################################################################
198 ########################################################################
200 # unset variables that should be defined only in config file
202 unset MONKEYSPHERE_USER
204 # load configuration file
205 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
207 # set empty config variable with ones from the environment, or with
209 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
210 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
211 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
212 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
213 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
216 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
217 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
219 # export variables needed in su invocation
223 export MONKEYSPHERE_USER
225 export GNUPGHOME_HOST
227 export HOST_FINGERPRINT=
228 export HOST_FINGERPRINT_SSH=
232 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
236 'show-key'|'show'|'s')
242 'set-expire'|'extend-key'|'e')
245 source "${MHSHAREDIR}/set_expire"
249 'add-hostname'|'add-name'|'n+')
252 source "${MHSHAREDIR}/add_hostname"
256 'revoke-hostname'|'revoke-name'|'n-')
259 source "${MHSHAREDIR}/revoke_hostname"
266 source "${MHSHAREDIR}/add_revoker"
273 source "${MHSHAREDIR}/revoke_key"
277 'publish-key'|'publish'|'p')
280 source "${MHSHAREDIR}/publish_key"
287 case "$SUBCOMMAND" in
290 usage: $PGRM expert <subcommand> [options] [args]
293 import-key (i) FILE [NAME[:PORT]] import existing ssh key to gpg
294 gen-key (g) [NAME[:PORT]] generate gpg key for the host
295 --length (-l) BITS key length in bits (2048)
296 diagnostics (d) monkeysphere host status
304 source "${MHSHAREDIR}/import_key"
311 source "${MHSHAREDIR}/gen_key"
316 source "${MHSHAREDIR}/diagnostics"
321 failure "Unknown expert subcommand: '$COMMAND'
322 Type '$PGRM help' for usage."
331 '--help'|'help'|'-h'|'h'|'?')
336 failure "Unknown command: '$COMMAND'
337 Type '$PGRM help' for usage."