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_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
42 # UTC date in ISO 8601 format if needed
43 DATE=$(date -u '+%FT%T')
45 # unset some environment variables that could screw things up
51 ########################################################################
53 ########################################################################
57 usage: $PGRM <subcommand> [options] [args]
58 Monkeysphere host admin tool.
61 show-key (s) output all host key information
62 set-expire (e) EXPIRE set host key expiration
63 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
64 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
65 add-revoker (o) FINGERPRINT add a revoker to the host key
66 revoke-key (r) revoke host key
67 publish-key (p) publish host key to keyserver
69 import-key (i) [NAME[:PORT]] import existing ssh key to gpg
71 version (v) show version number
74 See ${PGRM}(8) for more info.
78 # function to interact with the gpg keyring
80 GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
83 # command to list the info about the host key, in colon format
85 gpg_host --list-keys --with-colons --fixed-list-mode \
86 --with-fingerprint --with-fingerprint \
87 "0x${HOST_FINGERPRINT}!"
91 # command for edit key scripts, takes scripts on stdin
92 # FIXME: should we supress all the edit script spew? or pipe it
95 gpg_host --quiet --command-fd 0 --edit-key \
96 "0x${HOST_FINGERPRINT}!" "$@"
99 # export the host public key to the monkeysphere gpg pub key file
100 update_gpg_pub_file() {
101 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
102 gpg_host --export --armor --export-options export-minimal \
103 "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
106 # load the host fingerprint into the fingerprint variable, using the
107 # export gpg pub key file
108 # FIXME: this seems much less than ideal, with all this temp keyring
109 # stuff. is there a way we can do this without having to create temp
110 # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
112 if [ -f "$HOST_KEY_FILE" ] ; then
113 HOST_FINGERPRINT=$( \
114 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
115 && gpg --quiet --import \
116 && gpg --quiet --list-keys --with-colons --with-fingerprint \
117 && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
118 | grep '^fpr:' | cut -d: -f10 )
124 # load the host fingerprint into the fingerprint variable, using the
125 # gpg host secret key
126 load_fingerprint_secret() {
127 HOST_FINGERPRINT=$( \
128 gpg_host --quiet --list-secret-key \
129 --with-colons --with-fingerprint \
130 | grep '^fpr:' | cut -d: -f10 )
133 # fail if host key present
135 [ ! -s "$HOST_KEY_FILE" ] \
136 || failure "An OpenPGP host key already exists."
139 # fail if host key not present
140 check_host_no_key() {
141 [ -s "$HOST_KEY_FILE" ] \
142 || failure "You don't appear to have a Monkeysphere host key on this server.
143 Please run 'monkeysphere-host import-key' first."
146 # output the index of a user ID on the host key
147 # return 1 if user ID not found
153 # match to only ultimately trusted user IDs
154 tmpuidMatch="u:$(echo $userID | gpg_escape)"
156 # find the index of the requsted user ID
157 # NOTE: this is based on circumstantial evidence that the order of
158 # this output is the appropriate index
159 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
160 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
162 if [ "$line" ] ; then
170 # show info about the host key
175 export GNUPGHOME=$(mktemp -d)
177 # trap to remove tmp dir if break
178 trap "rm -rf $GNUPGHOME" EXIT
180 # import the host key into the tmp dir
181 gpg --quiet --import <"$HOST_KEY_FILE"
183 HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
184 | grep '^fpr:' | cut -d: -f10 )
186 # list the host key info
187 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
188 # FIXME: can we show uid validity somehow?
189 gpg --list-keys --fingerprint \
190 --list-options show-unusable-uids 2>/dev/null \
191 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
194 # list the pgp fingerprint
195 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
197 # list the ssh fingerprint
198 echo -n "ssh fingerprint: "
199 ssh-keygen -l -f /dev/stdin \
200 <<<$(openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null) \
201 | awk '{ print $1, $2, $4 }'
203 # remove the tmp file
208 ########################################################################
210 ########################################################################
212 # unset variables that should be defined only in config file or in
213 # MONKEYSPHERE_ variables
216 unset MONKEYSPHERE_USER
219 # load configuration file
220 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
222 # set empty config variable with ones from the environment, or with
224 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
225 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
226 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
227 PROMPT=${MONKEYSPHERE_PROMPT:=${PROMPT:="true"}}
230 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
231 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
233 # export variables needed in su invocation
238 export MONKEYSPHERE_USER
240 export CHECK_KEYSERVER
241 export GNUPGHOME_HOST
243 export HOST_FINGERPRINT=
247 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
251 '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"
300 source "${MHSHAREDIR}/import_key"
306 source "${MHSHAREDIR}/diagnostics"
314 '--help'|'help'|'-h'|'h'|'?')
319 failure "Unknown command: '$COMMAND'
320 Type '$PGRM help' for usage."