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_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
38 # UTC date in ISO 8601 format if needed
39 DATE=$(date -u '+%FT%T')
41 # unset some environment variables that could screw things up
47 ########################################################################
49 ########################################################################
53 usage: $PGRM <subcommand> [options] [args]
54 Monkeysphere host admin tool.
57 import-key (i) FILE [NAME[:PORT]] import existing ssh key to gpg
58 show-key (s) output all host key information
59 publish-key (p) publish host key to keyserver
60 set-expire (e) [EXPIRE] set host key expiration
61 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
62 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
63 add-revoker (r+) [KEYID|FILE] add a revoker to the host key
64 revoke-key generate and/or publish revocation
65 certificate for host key
67 version (v) show version number
70 See ${PGRM}(8) for more info.
74 # function to interact with the gpg keyring
76 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
79 # command to list the info about the host key, in colon format, to
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 --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
93 # export the host public key to the monkeysphere gpg pub key file
94 update_gpg_pub_file() {
95 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
96 gpg_host --export --armor --export-options export-minimal \
97 "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
100 # load the host fingerprint into the fingerprint variable, using the
101 # export gpg pub key file
102 # FIXME: this seems much less than ideal, with all this temp keyring
103 # stuff. is there a way we can do this without having to create temp
104 # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
106 if [ -f "$HOST_KEY_FILE" ] ; then
107 HOST_FINGERPRINT=$( \
108 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
109 && gpg --quiet --import \
110 && gpg --quiet --list-keys --with-colons --with-fingerprint \
111 && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
112 | grep '^fpr:' | cut -d: -f10 )
114 failure "host key gpg pub file not found."
118 # load the host fingerprint into the fingerprint variable, using the
119 # gpg host secret key
120 load_fingerprint_secret() {
121 HOST_FINGERPRINT=$( \
122 gpg_host --list-secret-key --with-colons --with-fingerprint \
123 | grep '^fpr:' | cut -d: -f10 )
126 # fail if host key present
128 [ ! -s "$HOST_KEY_FILE" ] \
129 || failure "An OpenPGP host key already exists."
132 # fail if host key not present
133 check_host_no_key() {
134 [ -s "$HOST_KEY_FILE" ] \
135 || failure "You don't appear to have a Monkeysphere host key on this server.
136 Please run 'monkeysphere-host import-key...' first."
139 # output the index of a user ID on the host key
140 # return 1 if user ID not found
146 # match to only ultimately trusted user IDs
147 tmpuidMatch="u:$(echo $userID | gpg_escape)"
149 # find the index of the requsted user ID
150 # NOTE: this is based on circumstantial evidence that the order of
151 # this output is the appropriate index
152 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
153 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
155 if [ "$line" ] ; then
163 # show info about the host key
168 export GNUPGHOME=$(msmktempdir)
170 # trap to remove tmp dir if break
171 trap "rm -rf $GNUPGHOME" EXIT
173 # import the host key into the tmp dir
174 gpg --quiet --import <"$HOST_KEY_FILE"
177 TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
178 openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null >"$TMPSSH"
180 # get the gpg fingerprint
181 HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
182 | grep '^fpr:' | cut -d: -f10 )
184 # list the host key info
185 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
186 # FIXME: can we show uid validity somehow?
187 gpg --list-keys --fingerprint \
188 --list-options show-unusable-uids 2>/dev/null \
189 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
192 # list the pgp fingerprint
193 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
195 # list the ssh fingerprint
196 echo -n "ssh fingerprint: "
197 ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
199 # remove the tmp file
204 ########################################################################
206 ########################################################################
208 # load configuration file
209 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
210 && . "$MONKEYSPHERE_HOST_CONFIG"
212 # set empty config variable with ones from the environment, or with
214 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
215 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
216 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
217 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
218 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
221 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
223 # export variables needed in su invocation
227 export CHECK_KEYSERVER
228 export MONKEYSPHERE_USER
230 export GNUPGHOME_HOST
232 export HOST_FINGERPRINT
236 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
242 source "${MHSHAREDIR}/import_key"
246 'show-key'|'show'|'s')
251 'set-expire'|'extend-key'|'e')
254 source "${MHSHAREDIR}/set_expire"
258 'add-hostname'|'add-name'|'n+')
261 source "${MHSHAREDIR}/add_hostname"
265 'revoke-hostname'|'revoke-name'|'n-')
268 source "${MHSHAREDIR}/revoke_hostname"
275 source "${MHSHAREDIR}/add_revoker"
282 source "${MHSHAREDIR}/revoke_key"
286 'publish-key'|'publish'|'p')
289 source "${MHSHAREDIR}/publish_key"
295 source "${MHSHAREDIR}/diagnostics"
299 'update-gpg-pub-file')
300 load_fingerprint_secret
308 '--help'|'help'|'-h'|'h'|'?')
313 failure "Unknown command: '$COMMAND'
314 Type '$PGRM help' for usage."