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
89 gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
92 # export the host public key to the monkeysphere gpg pub key file
93 update_gpg_pub_file() {
94 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
95 gpg_host --export --armor --export-options export-minimal \
96 "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
99 # load the host fingerprint into the fingerprint variable, using the
100 # export gpg pub key file
101 # FIXME: this seems much less than ideal, with all this temp keyring
102 # stuff. is there a way we can do this without having to create temp
103 # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
105 if [ -f "$HOST_KEY_FILE" ] ; then
106 HOST_FINGERPRINT=$( \
107 (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
108 && gpg --quiet --import \
109 && gpg --quiet --list-keys --with-colons --with-fingerprint \
110 && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
111 | grep '^fpr:' | cut -d: -f10 )
113 failure "host key gpg pub file not found."
117 # load the host fingerprint into the fingerprint variable, using the
118 # gpg host secret key
119 load_fingerprint_secret() {
120 HOST_FINGERPRINT=$( \
121 gpg_host --list-secret-key --with-colons --with-fingerprint \
122 | grep '^fpr:' | cut -d: -f10 )
125 # fail if host key present
127 [ ! -s "$HOST_KEY_FILE" ] \
128 || failure "An OpenPGP host key already exists."
131 # fail if host key not present
132 check_host_no_key() {
133 [ -s "$HOST_KEY_FILE" ] \
134 || failure "You don't appear to have a Monkeysphere host key on this server.
135 Please run 'monkeysphere-host import-key...' first."
138 # output the index of a user ID on the host key
139 # return 1 if user ID not found
145 # match to only ultimately trusted user IDs
146 tmpuidMatch="u:$(echo $userID | gpg_escape)"
148 # find the index of the requsted user ID
149 # NOTE: this is based on circumstantial evidence that the order of
150 # this output is the appropriate index
151 line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
152 grep -n -x -F "$tmpuidMatch" 2>/dev/null)
154 if [ "$line" ] ; then
162 # show info about the host key
167 export GNUPGHOME=$(msmktempdir)
169 # trap to remove tmp dir if break
170 trap "rm -rf $GNUPGHOME" EXIT
172 # import the host key into the tmp dir
173 gpg --quiet --import <"$HOST_KEY_FILE"
175 HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
176 | grep '^fpr:' | cut -d: -f10 )
178 # list the host key info
179 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
180 # FIXME: can we show uid validity somehow?
181 gpg --list-keys --fingerprint \
182 --list-options show-unusable-uids 2>/dev/null \
183 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
186 # list the pgp fingerprint
187 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
189 # list the ssh fingerprint
190 echo -n "ssh fingerprint: "
191 ssh-keygen -l -f /dev/stdin \
192 <<<$(openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null) \
193 | awk '{ print $1, $2, $4 }'
195 # remove the tmp file
200 ########################################################################
202 ########################################################################
204 # load configuration file
205 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
206 && . "$MONKEYSPHERE_HOST_CONFIG"
208 # set empty config variable with ones from the environment, or with
210 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
211 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
212 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
213 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
214 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
217 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
219 # export variables needed in su invocation
223 export CHECK_KEYSERVER
224 export MONKEYSPHERE_USER
226 export GNUPGHOME_HOST
228 export HOST_FINGERPRINT
232 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
236 'show-key'|'show'|'s')
241 'set-expire'|'extend-key'|'e')
244 source "${MHSHAREDIR}/set_expire"
248 'add-hostname'|'add-name'|'n+')
251 source "${MHSHAREDIR}/add_hostname"
255 'revoke-hostname'|'revoke-name'|'n-')
258 source "${MHSHAREDIR}/revoke_hostname"
265 source "${MHSHAREDIR}/add_revoker"
272 source "${MHSHAREDIR}/revoke_key"
276 'publish-key'|'publish'|'p')
279 source "${MHSHAREDIR}/publish_key"
285 source "${MHSHAREDIR}/import_key"
291 source "${MHSHAREDIR}/diagnostics"
295 'update-gpg-pub-file')
296 load_fingerprint_secret
304 '--help'|'help'|'-h'|'h'|'?')
309 failure "Unknown command: '$COMMAND'
310 Type '$PGRM help' for usage."