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 users 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 ('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
61 su_monkeysphere_user() {
62 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
65 # function to interact with the host gnupg keyring
69 GNUPGHOME="$GNUPGHOME_HOST"
72 # NOTE: we supress this warning because we need the monkeysphere
73 # user to be able to read the host pubring. we realize this might
74 # be problematic, but it's the simplest solution, without too much
76 gpg --no-permission-warning "$@"
79 # always reset the permissions on the host pubring so that the
80 # monkeysphere user can read the trust signatures
81 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
82 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
87 # function to interact with the authentication gnupg keyring
88 # FIXME: this function requires basically accepts only a single
89 # argument because of problems with quote expansion. this needs to be
91 gpg_authentication() {
92 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
95 su_monkeysphere_user "gpg $@"
98 # update authorized_keys for users
101 # get users from command line
104 # or just look at all users if none specified
105 unames=$(getent passwd | cut -d: -f1)
109 MODE="authorized_keys"
112 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
114 # check to see if the gpg trust database has been initialized
115 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
116 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
119 # make sure the authorized_keys directory exists
120 mkdir -p "${VARLIB}/authorized_keys"
123 for uname in $unames ; do
124 # check all specified users exist
125 if ! getent passwd "$uname" >/dev/null ; then
126 log "----- unknown user '$uname' -----"
130 # set authorized_user_ids and raw authorized_keys variables,
131 # translating ssh-style path variables
132 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
133 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
135 # if neither is found, skip user
136 if [ ! -s "$authorizedUserIDs" ] ; then
137 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
142 log "----- user: $uname -----"
144 if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
145 log "Improper permissions on authorized_user_ids file."
149 if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
150 log "Improper permissions on authorized_keys file."
154 # make temporary directory
157 # trap to delete temporary directory on exit
158 trap "rm -rf $TMPDIR" EXIT
160 # create temporary authorized_user_ids file
161 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
162 touch "$TMP_AUTHORIZED_USER_IDS"
164 # create temporary authorized_keys file
165 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
166 touch "$AUTHORIZED_KEYS"
168 # set restrictive permissions on the temporary files
169 # FIXME: is there a better way to do this?
171 chmod 0600 "$AUTHORIZED_KEYS"
172 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
173 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
175 # if the authorized_user_ids file exists...
176 if [ -s "$authorizedUserIDs" ] ; then
177 # copy user authorized_user_ids file to temporary
179 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
181 # export needed variables
182 export AUTHORIZED_KEYS
183 export TMP_AUTHORIZED_USER_IDS
185 # process authorized_user_ids file, as monkeysphere
187 su_monkeysphere_user \
188 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
192 # add user-controlled authorized_keys file path if specified
193 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
194 log -n "adding raw authorized_keys file... "
195 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
199 # openssh appears to check the contents of the
200 # authorized_keys file as the user in question, so the
201 # file must be readable by that user at least.
202 # FIXME: is there a better way to do this?
203 chown root "$AUTHORIZED_KEYS"
204 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
205 chmod g+r "$AUTHORIZED_KEYS"
207 # if the resulting authorized_keys file is not empty, move
209 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
211 # destroy temporary directory
216 # generate server gpg key
223 hostName=${1:-$(hostname --fqdn)}
224 userID="ssh://${hostName}"
226 # check for presense of key with user ID
227 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
228 failure "Key for '$userID' already exists"
233 KEY_LENGTH=${KEY_LENGTH:="2048"}
235 # prompt about key expiration if not specified
236 if [ -z "$KEY_EXPIRE" ] ; then
238 Please specify how long the key should be valid.
239 0 = key does not expire
240 <n> = key expires in n days
241 <n>w = key expires in n weeks
242 <n>m = key expires in n months
243 <n>y = key expires in n years
245 while [ -z "$KEY_EXPIRE" ] ; do
246 read -p "Key is valid for? (0) " KEY_EXPIRE
247 if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
252 elif ! test_gpg_expire "$KEY_EXPIRE" ; then
253 failure "invalid key expiration value '$KEY_EXPIRE'."
257 keyParameters=$(cat <<EOF
259 Key-Length: $KEY_LENGTH
260 Key-Usage: $KEY_USAGE
262 Expire-Date: $KEY_EXPIRE
266 # add the revoker field if specified
267 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
268 # FIXME: key is marked "sensitive"? is this appropriate?
269 if [ "$REVOKER" ] ; then
270 keyParameters="${keyParameters}"$(cat <<EOF
271 Revoker: 1:$REVOKER sensitive
276 echo "The following key parameters will be used for the host private key:"
277 echo "$keyParameters"
279 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
280 if [ ${OK/y/Y} != 'Y' ] ; then
285 keyParameters="${keyParameters}"$(cat <<EOF
292 log "generating server key..."
293 echo "$keyParameters" | gpg_host --batch --gen-key
295 # output the server fingerprint
296 fingerprint_server_key "=${userID}"
298 # find the key fingerprint of the server primary key
299 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
300 grep '^fpr:' | head -1 | cut -d: -f10)
302 # translate the private key to ssh format, and export to a file
304 # NOTE: assumes that the primary key is the proper key to use
306 gpg_host --export-secret-key "$fingerprint" | \
307 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
308 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
311 # gpg output key fingerprint
312 fingerprint_server_key() {
313 gpg_host --fingerprint --list-secret-keys
316 # publish server key to keyserver
317 publish_server_key() {
318 read -p "really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
319 if [ ${OK/y/Y} != 'Y' ] ; then
324 # FIXME: need to figure out better way to identify host key
325 # dummy command so as not to publish fakes keys during testing
327 #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
328 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
331 # retrieve key from web of trust, import it into the host keyring, and
332 # ltsign the key in the host keyring so that it may certify other keys
341 # export host ownertrust to authentication keyring
342 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
344 # get the key from the key server
345 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'" || failure
347 # get the full fingerprint of a key ID
348 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
349 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
352 gpg_authentication "--fingerprint $fingerprint"
354 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}
355 if [ "${OK/y/Y}" != 'Y' ] ; then
359 # export the key to the host keyring
360 gpg_authentication "--export $keyID" | gpg_host --import
362 # default values for trust depth and domain
368 # NOTE: *all* user IDs will be ltsigned
369 ltsignCommand=$(cat <<EOF
381 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
383 # update the trustdb for the authentication keyring
384 gpg_authentication "--check-trustdb"
387 # delete a certifiers key from the host keyring
394 # delete the requested key (with prompting)
395 gpg_host --delete-key "$keyID"
397 # update the trustdb for the authentication keyring
398 gpg_authentication "--check-trustdb"
401 # list the host certifiers
406 ########################################################################
408 ########################################################################
410 # unset variables that should be defined only in config file
412 unset AUTHORIZED_USER_IDS
413 unset RAW_AUTHORIZED_KEYS
414 unset MONKEYSPHERE_USER
416 # load configuration file
417 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
419 # set empty config variable with ones from the environment, or with
421 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
422 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
423 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
424 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
427 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
428 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
429 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
430 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
432 # export variables needed in su invocation
435 export MONKEYSPHERE_USER
437 export CHECK_KEYSERVER
438 export REQUIRED_USER_KEY_CAPABILITY
439 export GNUPGHOME_HOST
440 export GNUPGHOME_AUTHENTICATION
445 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
448 # unset option variables
456 # get options for key generation and add-certifier functions
457 TEMP=$(getopt -o l:e:r:n:t:d: -l length:,expire:,revoker:,domain:,trust:,depth: -n "$PGRM" -- "$@")
459 if [ $? != 0 ] ; then
464 # Note the quotes around `$TEMP': they are essential!
503 'update-users'|'update-user'|'u')
511 'show-fingerprint'|'f')
512 fingerprint_server_key
519 'add-identity-certifier'|'add-certifier'|'a')
520 if [ -z "$1" ] ; then
521 failure "You must specify a key ID."
526 'remove-identity-certifier'|'remove-certifier'|'r')
527 if [ -z "$1" ] ; then
528 failure "You must specify a key ID."
530 remove_certifier "$1"
533 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
542 failure "Unknown command: '$COMMAND'
543 Type '$PGRM help' for usage."