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 domain of certifier ()
51 -t|--trust TRUST trust level of certifier (2)
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 execute a gpg command to the
57 authentication keyring as the
65 su_monkeysphere_user() {
66 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
69 # function to interact with the host gnupg keyring
73 GNUPGHOME="$GNUPGHOME_HOST"
76 # NOTE: we supress this warning because we need the monkeysphere
77 # user to be able to read the host pubring. we realize this might
78 # be problematic, but it's the simplest solution, without too much
80 gpg --no-permission-warning "$@"
83 # always reset the permissions on the host pubring so that the
84 # monkeysphere user can read the trust signatures
85 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
86 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
91 # function to interact with the authentication gnupg keyring
92 # FIXME: this function requires basically accepts only a single
93 # argument because of problems with quote expansion. this needs to be
95 gpg_authentication() {
96 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
99 su_monkeysphere_user "gpg $@"
102 # update authorized_keys for users
105 # get users from command line
108 # or just look at all users if none specified
109 unames=$(getent passwd | cut -d: -f1)
113 MODE="authorized_keys"
116 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
118 # check to see if the gpg trust database has been initialized
119 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
120 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
123 # make sure the authorized_keys directory exists
124 mkdir -p "${VARLIB}/authorized_keys"
127 for uname in $unames ; do
128 # check all specified users exist
129 if ! getent passwd "$uname" >/dev/null ; then
130 log "----- unknown user '$uname' -----"
134 # set authorized_user_ids and raw authorized_keys variables,
135 # translating ssh-style path variables
136 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
137 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
139 # if neither is found, skip user
140 if [ ! -s "$authorizedUserIDs" ] ; then
141 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
146 log "----- user: $uname -----"
148 if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
149 log "Improper permissions on authorized_user_ids file."
153 if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
154 log "Improper permissions on authorized_keys file."
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 # if the resulting authorized_keys file is not empty, move
213 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
215 # destroy temporary directory
220 # generate server gpg key
232 # set default key parameter values
240 TEMP=$(getopt -o l:e:r: -l length:,expire:,revoker: -n "$PGRM" -- "$@")
242 if [ $? != 0 ] ; then
246 # Note the quotes around `$TEMP': they are essential!
272 hostName=${1:-$(hostname --fqdn)}
273 userID="ssh://${hostName}"
275 # check for presense of key with user ID
276 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
277 failure "Key for '$userID' already exists"
280 # prompt about key expiration if not specified
281 if [ -z "$keyExpire" ] ; then
283 Please specify how long the key should be valid.
284 0 = key does not expire
285 <n> = key expires in n days
286 <n>w = key expires in n weeks
287 <n>m = key expires in n months
288 <n>y = key expires in n years
290 while [ -z "$keyExpire" ] ; do
291 read -p "Key is valid for? (0) " keyExpire
292 if ! test_gpg_expire ${keyExpire:=0} ; then
297 elif ! test_gpg_expire "$keyExpire" ; then
298 failure "invalid key expiration value '$keyExpire'."
302 keyParameters=$(cat <<EOF
304 Key-Length: $keyLength
307 Expire-Date: $keyExpire
311 # add the revoker field if specified
312 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
313 # FIXME: key is marked "sensitive"? is this appropriate?
314 if [ "$revoker" ] ; then
315 keyParameters="${keyParameters}"$(cat <<EOF
316 Revoker: 1:$revoker sensitive
321 echo "The following key parameters will be used for the host private key:"
322 echo "$keyParameters"
324 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
325 if [ ${OK/y/Y} != 'Y' ] ; then
330 keyParameters="${keyParameters}"$(cat <<EOF
337 log "generating server key..."
338 echo "$keyParameters" | gpg_host --batch --gen-key
340 # output the server fingerprint
341 fingerprint_server_key "=${userID}"
343 # find the key fingerprint of the server primary key
344 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
345 grep '^fpr:' | head -1 | cut -d: -f10)
347 # translate the private key to ssh format, and export to a file
349 # NOTE: assumes that the primary key is the proper key to use
351 gpg_host --export-secret-key "$fingerprint" | \
352 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
353 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
356 # gpg output key fingerprint
357 fingerprint_server_key() {
358 gpg_host --fingerprint --list-secret-keys
361 # publish server key to keyserver
362 publish_server_key() {
363 read -p "really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
364 if [ ${OK/y/Y} != 'Y' ] ; then
369 # FIXME: need to figure out better way to identify host key
370 # dummy command so as not to publish fakes keys during testing
372 #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
373 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
376 # retrieve key from web of trust, import it into the host keyring, and
377 # ltsign the key in the host keyring so that it may certify other keys
386 # set default values for trust depth and domain
392 TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
394 if [ $? != 0 ] ; then
398 # Note the quotes around `$TEMP': they are essential!
427 # export host ownertrust to authentication keyring
428 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
430 # get the key from the key server
431 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
433 # get the full fingerprint of a key ID
434 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
435 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
438 gpg_authentication "--fingerprint $fingerprint"
440 read -p "Are you sure you want to add this key as a certifier of users on this system? (y/N) " OK; OK=${OK:-N}
441 if [ "${OK/y/Y}" != 'Y' ] ; then
445 # export the key to the host keyring
446 gpg_authentication "--export $keyID" | gpg_host --import
449 # NOTE: *all* user IDs will be ltsigned
450 ltsignCommand=$(cat <<EOF
462 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
464 # update the trustdb for the authentication keyring
465 gpg_authentication "--check-trustdb"
468 # delete a certifiers key from the host keyring
475 # delete the requested key (with prompting)
476 gpg_host --delete-key "$keyID"
478 # update the trustdb for the authentication keyring
479 gpg_authentication "--check-trustdb"
482 # list the host certifiers
487 # issue command to gpg-authentication keyring
488 gpg_authentication_cmd() {
489 gpg_authentication "$@"
492 ########################################################################
494 ########################################################################
496 # unset variables that should be defined only in config file
498 unset AUTHORIZED_USER_IDS
499 unset RAW_AUTHORIZED_KEYS
500 unset MONKEYSPHERE_USER
502 # load configuration file
503 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
505 # set empty config variable with ones from the environment, or with
507 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
508 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
509 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
510 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
513 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
514 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
515 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
516 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
518 # export variables needed in su invocation
521 export MONKEYSPHERE_USER
523 export CHECK_KEYSERVER
524 export REQUIRED_USER_KEY_CAPABILITY
525 export GNUPGHOME_HOST
526 export GNUPGHOME_AUTHENTICATION
531 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
535 'update-users'|'update-user'|'u')
543 'show-fingerprint'|'f')
544 fingerprint_server_key
551 'add-identity-certifier'|'add-certifier'|'a')
552 if [ -z "$1" ] ; then
553 failure "You must specify a key ID."
558 'remove-identity-certifier'|'remove-certifier'|'r')
559 if [ -z "$1" ] ; then
560 failure "You must specify a key ID."
562 remove_certifier "$1"
565 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
569 'gpg-authentication-cmd')
570 gpg_authentication_cmd "$@"
578 failure "Unknown command: '$COMMAND'
579 Type '$PGRM help' for usage."