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) [NAME[:PORT]] import existing ssh key to gpg
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 version (v) show version number
69 See ${PGRM}(8) for more info.
73 # function to interact with the gpg keyring
75 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
78 # command to list the info about the host key, in colon format, to
81 gpg_host --list-keys --with-colons --fixed-list-mode \
82 --with-fingerprint --with-fingerprint \
83 "0x${HOST_FINGERPRINT}!"
87 # command for edit key scripts, takes scripts on stdin
88 # FIXME: should we supress all the edit script spew? or pipe it
91 gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
94 # export the host public key to the monkeysphere gpg pub key file
95 update_gpg_pub_file() {
96 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
97 gpg_host --export --armor --export-options export-minimal \
98 "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
101 # load the host fingerprint into the fingerprint variable, using the
102 # export gpg pub key file
103 # FIXME: this seems much less than ideal, with all this temp keyring
104 # stuff. is there a way we can do this without having to create temp
105 # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
107 if [ -f "$HOST_KEY_FILE" ] ; then
108 HOST_FINGERPRINT=$( \
109 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
110 && gpg --quiet --import \
111 && gpg --quiet --list-keys --with-colons --with-fingerprint \
112 && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
113 | grep '^fpr:' | cut -d: -f10 )
119 # load the host fingerprint into the fingerprint variable, using the
120 # gpg host secret key
121 load_fingerprint_secret() {
122 HOST_FINGERPRINT=$( \
123 gpg_host --list-secret-key --with-colons --with-fingerprint \
124 | grep '^fpr:' | cut -d: -f10 )
127 # fail if host key present
129 [ ! -s "$HOST_KEY_FILE" ] \
130 || failure "An OpenPGP host key already exists."
133 # fail if host key not present
134 check_host_no_key() {
135 [ -s "$HOST_KEY_FILE" ] \
136 || failure "You don't appear to have a Monkeysphere host key on this server.
137 Please run 'monkeysphere-host import-key...' first."
140 # output the index of a user ID on the host key
141 # return 1 if user ID not found
147 # match to only ultimately trusted user IDs
148 tmpuidMatch="u:$(echo $userID | gpg_escape)"
150 # find the index of the requsted user ID
151 # NOTE: this is based on circumstantial evidence that the order of
152 # this output is the appropriate index
153 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
154 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
156 if [ "$line" ] ; then
164 # show info about the host key
169 export GNUPGHOME=$(msmktempdir)
171 # trap to remove tmp dir if break
172 trap "rm -rf $GNUPGHOME" EXIT
174 # import the host key into the tmp dir
175 gpg --quiet --import <"$HOST_KEY_FILE"
177 HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
178 | grep '^fpr:' | cut -d: -f10 )
180 # list the host key info
181 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
182 # FIXME: can we show uid validity somehow?
183 gpg --list-keys --fingerprint \
184 --list-options show-unusable-uids 2>/dev/null \
185 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
188 # list the pgp fingerprint
189 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
191 # list the ssh fingerprint
192 echo -n "ssh fingerprint: "
193 ssh-keygen -l -f /dev/stdin \
194 <<<$(openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null) \
195 | awk '{ print $1, $2, $4 }'
197 # remove the tmp file
202 ########################################################################
204 ########################################################################
206 # load configuration file
207 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
208 && . "$MONKEYSPHERE_HOST_CONFIG"
210 # set empty config variable with ones from the environment, or with
212 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
213 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
214 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
215 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
216 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
219 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
221 # export variables needed in su invocation
225 export CHECK_KEYSERVER
226 export MONKEYSPHERE_USER
228 export GNUPGHOME_HOST
230 export HOST_FINGERPRINT
234 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
238 'show-key'|'show'|'s')
243 'set-expire'|'extend-key'|'e')
246 source "${MHSHAREDIR}/set_expire"
250 'add-hostname'|'add-name'|'n+')
253 source "${MHSHAREDIR}/add_hostname"
257 'revoke-hostname'|'revoke-name'|'n-')
260 source "${MHSHAREDIR}/revoke_hostname"
267 source "${MHSHAREDIR}/add_revoker"
274 source "${MHSHAREDIR}/revoke_key"
278 'publish-key'|'publish'|'p')
281 source "${MHSHAREDIR}/publish_key"
287 source "${MHSHAREDIR}/import_key"
293 source "${MHSHAREDIR}/diagnostics"
297 'update-gpg-pub-file')
305 '--help'|'help'|'-h'|'h'|'?')
310 failure "Unknown command: '$COMMAND'
311 Type '$PGRM help' for usage."