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}/defaultenv"
25 . "${SYSSHAREDIR}/common"
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
30 # sharedir for host functions
31 MHSHAREDIR="${SYSSHAREDIR}/mh"
33 # datadir for host functions
34 MHDATADIR="${SYSDATADIR}/host"
37 HOST_KEY_FILE="${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
45 ########################################################################
47 ########################################################################
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere host admin tool.
55 import-key (i) FILE NAME[:PORT] import existing ssh key to gpg
56 show-key (s) output all host key information
57 publish-key (p) publish host key to keyserver
58 set-expire (e) [EXPIRE] set host key expiration
59 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
60 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
61 add-revoker (r+) KEYID|FILE add a revoker to the host key
62 revoke-key generate and/or publish revocation
63 certificate for host key
65 version (v) show version number
68 See ${PGRM}(8) for more info.
72 # function to interact with the gpg keyring
74 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
77 # command to list the info about the host key, in colon format, to
80 gpg_host --list-keys --with-colons --fixed-list-mode \
81 --with-fingerprint --with-fingerprint \
82 "0x${HOST_FINGERPRINT}!"
86 # command for edit key scripts, takes scripts on stdin
88 gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
91 # export the host public key to the monkeysphere gpg pub key file
92 update_gpg_pub_file() {
93 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
94 gpg_host --export --armor --export-options export-minimal \
95 "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
98 # load the host fingerprint into the fingerprint variable, using the
99 # export gpg pub key file
100 # FIXME: this seems much less than ideal, with all this temp keyring
101 # stuff. is there a way we can do this without having to create temp
102 # files? what if we stored the fingerprint in MHDATADIR/fingerprint?
104 if [ -f "$HOST_KEY_FILE" ] ; then
105 HOST_FINGERPRINT=$( \
106 (FUBAR=$(msmktempdir) && export GNUPGHOME="$FUBAR" \
107 && gpg --quiet --import \
108 && gpg --quiet --list-keys --with-colons --with-fingerprint \
109 && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
110 | grep '^fpr:' | cut -d: -f10 )
112 failure "host key gpg pub file not found."
116 # load the host fingerprint into the fingerprint variable, using the
117 # gpg host secret key
118 load_fingerprint_secret() {
119 HOST_FINGERPRINT=$( \
120 gpg_host --list-secret-key --with-colons --with-fingerprint \
121 | grep '^fpr:' | cut -d: -f10 )
124 # fail if host key present
126 [ ! -s "$HOST_KEY_FILE" ] \
127 || failure "An OpenPGP host key already exists."
130 # fail if host key not present
131 check_host_no_key() {
132 [ -s "$HOST_KEY_FILE" ] \
133 || failure "You don't appear to have a Monkeysphere host key on this server.
134 Please run 'monkeysphere-host import-key...' first."
137 # return 0 if user ID was found.
138 # return 1 if user ID not found.
143 # match to only "unknown" user IDs (host has no need for ultimate trust)
144 tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
146 # See whether the requsted user ID is present
147 gpg_host_list | cut -f1,2,10 -d: | \
148 grep -q -x -F "$tmpuidMatch" 2>/dev/null
151 # show info about the host key
158 export GNUPGHOME=$(msmktempdir)
160 # trap to remove tmp dir if break
161 trap "rm -rf $GNUPGHOME" EXIT
163 # import the host key into the tmp dir
164 gpg --quiet --import <"$HOST_KEY_FILE"
167 TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
168 gpg --export | openpgp2ssh 2>/dev/null >"$TMPSSH"
170 # get the gpg fingerprint
171 HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
172 | grep '^fpr:' | cut -d: -f10 )
174 # list the host key info
175 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
176 # FIXME: can we show uid validity somehow?
177 gpg --list-keys --fingerprint \
178 --list-options show-unusable-uids 2>/dev/null \
179 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
182 # list revokers, if there are any
183 revokers=$(gpg --list-keys --with-colons --fixed-list-mode \
184 | awk -F: '/^rvk:/{ print $10 }' )
185 if [ "$revokers" ] ; then
186 echo "The following keys are allowed to revoke this host key:"
187 for key in $revokers ; do
193 # list the pgp fingerprint
194 echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
196 # list the ssh fingerprint
197 echo -n "ssh fingerprint: "
198 ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
200 # remove the tmp file
205 ########################################################################
207 ########################################################################
209 # load configuration file
210 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
211 && . "$MONKEYSPHERE_HOST_CONFIG"
213 # set empty config variable with ones from the environment, or with
215 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
216 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
217 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
218 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
219 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
220 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
223 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
224 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
226 # export variables needed in su invocation
230 export CHECK_KEYSERVER
231 export MONKEYSPHERE_USER
232 export MONKEYSPHERE_GROUP
234 export GNUPGHOME_HOST
236 export HOST_FINGERPRINT
241 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
247 source "${MHSHAREDIR}/import_key"
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"
299 source "${MHSHAREDIR}/diagnostics"
303 'update-gpg-pub-file')
304 load_fingerprint_secret
312 '--help'|'help'|'-h'|'h'|'?')
317 failure "Unknown command: '$COMMAND'
318 Type '$PGRM help' for usage."