3 # monkeysphere-server: MonkeySphere server admin tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
11 ########################################################################
14 SHARE=${MONKEYSPHERE_SHARE:="/usr/share/monkeysphere"}
16 . "${SHARE}/common" || exit 1
18 VARLIB="/var/lib/monkeysphere"
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
24 # unset some environment variables that could screw things up
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere server admin tool.
40 update-users (u) [USER]... update user authorized_keys files
42 gen-key (g) [HOSTNAME] generate gpg key for the server
43 -l|--length BITS key length in bits (2048)
44 -e|--expire EXPIRE date to expire
45 -r|--revoker FINGERPRINT add a revoker
46 show-fingerprint (f) show server's host key fingerprint
47 publish-key (p) publish server's host key to keyserver
49 add-identity-certifier (a) KEYID import and tsign a certification key
50 -n|--domain DOMAIN limit ID certifications to IDs in DOMAIN ()
51 -t|--trust TRUST trust level of certifier (full)
52 -d|--depth DEPTH trust depth for certifier (1)
53 remove-identity-certifier (r) KEYID remove a certification key
54 list-identity-certifiers (l) list certification keys
56 gpg-authentication-cmd CMD gnupg-authentication command
63 su_monkeysphere_user() {
64 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
67 # function to interact with the host gnupg keyring
71 GNUPGHOME="$GNUPGHOME_HOST"
74 # NOTE: we supress this warning because we need the monkeysphere
75 # user to be able to read the host pubring. we realize this might
76 # be problematic, but it's the simplest solution, without too much
78 gpg --no-permission-warning "$@"
81 # always reset the permissions on the host pubring so that the
82 # monkeysphere user can read the trust signatures
83 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
84 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
89 # function to interact with the authentication gnupg keyring
90 # FIXME: this function requires basically accepts only a single
91 # argument because of problems with quote expansion. this needs to be
93 gpg_authentication() {
94 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
97 su_monkeysphere_user "gpg $@"
100 # update authorized_keys for users
103 # get users from command line
106 # or just look at all users if none specified
107 unames=$(getent passwd | cut -d: -f1)
111 MODE="authorized_keys"
114 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
116 # check to see if the gpg trust database has been initialized
117 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
118 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
121 # make sure the authorized_keys directory exists
122 mkdir -p "${VARLIB}/authorized_keys"
125 for uname in $unames ; do
126 # check all specified users exist
127 if ! getent passwd "$uname" >/dev/null ; then
128 log "----- unknown user '$uname' -----"
132 # set authorized_user_ids and raw authorized_keys variables,
133 # translating ssh-style path variables
134 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
135 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
137 # if neither is found, skip user
138 if [ ! -s "$authorizedUserIDs" ] ; then
139 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
144 log "----- user: $uname -----"
146 # exit if the authorized_user_ids file is empty
147 if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
148 log "Improper permissions on authorized_user_ids file path."
152 # check permissions on the authorized_keys file path
153 if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
154 log "Improper permissions on authorized_keys file path path."
158 # make temporary directory
161 # trap to delete temporary directory on exit
162 trap "rm -rf $TMPDIR" EXIT
164 # create temporary authorized_user_ids file
165 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
166 touch "$TMP_AUTHORIZED_USER_IDS"
168 # create temporary authorized_keys file
169 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
170 touch "$AUTHORIZED_KEYS"
172 # set restrictive permissions on the temporary files
173 # FIXME: is there a better way to do this?
175 chmod 0600 "$AUTHORIZED_KEYS"
176 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
177 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
179 # if the authorized_user_ids file exists...
180 if [ -s "$authorizedUserIDs" ] ; then
181 # copy user authorized_user_ids file to temporary
183 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
185 # export needed variables
186 export AUTHORIZED_KEYS
187 export TMP_AUTHORIZED_USER_IDS
189 # process authorized_user_ids file, as monkeysphere
191 su_monkeysphere_user \
192 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
196 # add user-controlled authorized_keys file path if specified
197 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
198 log -n "adding raw authorized_keys file... "
199 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
203 # openssh appears to check the contents of the
204 # authorized_keys file as the user in question, so the
205 # file must be readable by that user at least.
206 # FIXME: is there a better way to do this?
207 chown root "$AUTHORIZED_KEYS"
208 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
209 chmod g+r "$AUTHORIZED_KEYS"
211 # move the resulting authorized_keys file into place
212 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
214 # destroy temporary directory
219 # generate server gpg key
231 # set default key parameter values
239 TEMP=$(getopt -o l:e:r: -l length:,expire:,revoker: -n "$PGRM" -- "$@")
241 if [ $? != 0 ] ; then
245 # Note the quotes around `$TEMP': they are essential!
271 hostName=${1:-$(hostname --fqdn)}
272 userID="ssh://${hostName}"
274 # check for presense of key with user ID
275 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
276 failure "Key for '$userID' already exists"
279 # prompt about key expiration if not specified
280 if [ -z "$keyExpire" ] ; then
282 Please specify how long the key should be valid.
283 0 = key does not expire
284 <n> = key expires in n days
285 <n>w = key expires in n weeks
286 <n>m = key expires in n months
287 <n>y = key expires in n years
289 while [ -z "$keyExpire" ] ; do
290 read -p "Key is valid for? (0) " keyExpire
291 if ! test_gpg_expire ${keyExpire:=0} ; then
296 elif ! test_gpg_expire "$keyExpire" ; then
297 failure "invalid key expiration value '$keyExpire'."
301 keyParameters=$(cat <<EOF
303 Key-Length: $keyLength
306 Expire-Date: $keyExpire
310 # add the revoker field if specified
311 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
312 # FIXME: key is marked "sensitive"? is this appropriate?
313 if [ "$revoker" ] ; then
314 keyParameters="${keyParameters}"$(cat <<EOF
315 Revoker: 1:$revoker sensitive
320 echo "The following key parameters will be used for the host private key:"
321 echo "$keyParameters"
323 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
324 if [ ${OK/y/Y} != 'Y' ] ; then
329 keyParameters="${keyParameters}"$(cat <<EOF
336 log "generating server key..."
337 echo "$keyParameters" | gpg_host --batch --gen-key
339 # output the server fingerprint
340 fingerprint_server_key "=${userID}"
342 # find the key fingerprint of the server primary key
343 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
344 grep '^fpr:' | head -1 | cut -d: -f10)
346 # export host ownertrust to authentication keyring
347 log "setting ultimate owner trust for server key..."
348 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
350 # translate the private key to ssh format, and export to a file
352 # NOTE: assumes that the primary key is the proper key to use
354 gpg_host --export-secret-key "$fingerprint" | \
355 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
356 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
359 # gpg output key fingerprint
360 fingerprint_server_key() {
361 gpg_host --fingerprint --list-secret-keys
364 # publish server key to keyserver
365 publish_server_key() {
366 read -p "Really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
367 if [ ${OK/y/Y} != 'Y' ] ; then
372 # FIXME: need to figure out better way to identify host key
373 # dummy command so as not to publish fakes keys during testing
375 #gpg_authentication "--keyserver $KEYSERVER --send-keys $(hostname -f)"
376 echo "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
377 echo "The following command should publish the key:"
378 echo "monkeysphere-server gpg-authentication-cmd '--keyserver $KEYSERVER --send-keys $(hostname -f)'"
382 # retrieve key from web of trust, import it into the host keyring, and
383 # ltsign the key in the host keyring so that it may certify other keys
393 # set default values for trust depth and domain
399 TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
401 if [ $? != 0 ] ; then
405 # Note the quotes around `$TEMP': they are essential!
432 if [ -z "$keyID" ] ; then
433 failure "You must specify the key ID of a key to add."
437 # get the key from the key server
438 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
440 # get the full fingerprint of a key ID
441 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
442 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
445 gpg_authentication "--fingerprint $fingerprint"
447 echo "Are you sure you want to add this key as a certifier of"
448 read -p "users on this system? (y/N) " OK; OK=${OK:-N}
449 if [ "${OK/y/Y}" != 'Y' ] ; then
453 # export the key to the host keyring
454 gpg_authentication "--export $keyID" | gpg_host --import
456 if [ "$trust" == marginal ]; then
458 elif [ "$trust" == full ]; then
461 failure "trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)"
465 # NOTE: *all* user IDs will be ltsigned
466 ltsignCommand=$(cat <<EOF
478 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
480 # update the trustdb for the authentication keyring
481 gpg_authentication "--check-trustdb"
484 # delete a certifiers key from the host keyring
490 if [ -z "$keyID" ] ; then
491 failure "You must specify the key ID of a key to remove."
494 # delete the requested key (with prompting)
495 gpg_host --delete-key "$keyID"
497 # update the trustdb for the authentication keyring
498 gpg_authentication "--check-trustdb"
501 # list the host certifiers
506 # issue command to gpg-authentication keyring
507 gpg_authentication_cmd() {
508 gpg_authentication "$@"
511 ########################################################################
513 ########################################################################
515 # unset variables that should be defined only in config file
517 unset AUTHORIZED_USER_IDS
518 unset RAW_AUTHORIZED_KEYS
519 unset MONKEYSPHERE_USER
521 # load configuration file
522 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
524 # set empty config variable with ones from the environment, or with
526 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
527 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
528 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
529 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
532 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
533 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
534 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
535 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
537 # export variables needed in su invocation
540 export MONKEYSPHERE_USER
542 export CHECK_KEYSERVER
543 export REQUIRED_USER_KEY_CAPABILITY
544 export GNUPGHOME_HOST
545 export GNUPGHOME_AUTHENTICATION
550 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
554 'update-users'|'update-user'|'u')
562 'show-fingerprint'|'f')
563 fingerprint_server_key
570 'add-identity-certifier'|'add-certifier'|'a')
574 'remove-identity-certifier'|'remove-certifier'|'r')
575 remove_certifier "$1"
578 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
582 'gpg-authentication-cmd')
583 gpg_authentication_cmd "$@"
591 failure "Unknown command: '$COMMAND'
592 Type '$PGRM help' for usage."