Major rework to remove all caching. Everything processed straight
[monkeysphere.git] / src / monkeysphere-server
index fd7b58330ae6fc58a5696958ccbb122713e99bdd..560d249867d780183f9b070ed341c099bf948963 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 # monkeysphere-server: MonkeySphere server admin tool
 #
@@ -28,36 +28,64 @@ GREP_OPTIONS=
 usage() {
 cat <<EOF
 usage: $PGRM <subcommand> [args]
-Monkeysphere server admin tool.
+MonkeySphere server admin tool.
 
 subcommands:
-  update-users (s) [USER]...                 update user authorized_keys file
-  gen-key (g)                                generate gpg key for the server
-  publish-key (p)                            publish server gpg to keyserver
-  trust-key (t) KEYID [KEYID]...             mark keyid as trusted
-  update-user-userids (u) USER UID [UID]...  add/update userid for user
-  help (h,?)                                 this help
+  update-users (s) [USER]...            update users authorized_keys files
+  gen-key (g) [HOSTNAME]                generate gpg key for the server
+  publish-key (p)                       publish server key to keyserver
+  trust-keys (t) KEYID...               mark keyids as trusted
+  update-user-userids (u) USER UID...   add/update user IDs for a user
+  remove-user-userids (r) USER UID...   remove user IDs for a user
+  help (h,?)                            this help
 
 EOF
 }
 
 # generate server gpg key
 gen_key() {
-    KEY_TYPE=${KEY_TYPE:-RSA}
-    KEY_LENGTH=${KEY_LENGTH:-2048}
-    KEY_USAGE=${KEY_USAGE:-encrypt,auth}
-    SERVICE=${SERVICE:-ssh}
-    HOSTNAME_FQDN=${HOSTNAME_FQDN:-$(hostname -f)}
+    local hostName
 
-    USERID=${USERID:-"$SERVICE"://"$HOSTNAME_FQDN"}
+    hostName=${1:-$(hostname --fqdn)}
 
-    echo "key parameters:"
+    # set key defaults
+    KEY_TYPE=${KEY_TYPE:-"RSA"}
+    KEY_LENGTH=${KEY_LENGTH:-"2048"}
+    KEY_USAGE=${KEY_USAGE:-"auth,encrypt"}
     cat <<EOF
+Please specify how long the key should be valid.
+         0 = key does not expire
+      <n>  = key expires in n days
+      <n>w = key expires in n weeks
+      <n>m = key expires in n months
+      <n>y = key expires in n years
+EOF
+    read -p "Key is valid for? ($EXPIRE) " EXPIRE; EXPIRE=${EXPIRE:-"0"}
+
+    SERVICE=${SERVICE:-"ssh"}
+    USERID=${USERID:-"$SERVICE"://"$hostName"}
+
+    # set key parameters
+    keyParameters=$(cat <<EOF
 Key-Type: $KEY_TYPE
 Key-Length: $KEY_LENGTH
 Key-Usage: $KEY_USAGE
 Name-Real: $USERID
+Expire-Date: $EXPIRE
+EOF
+)
+
+    # add the revoker field if requested
+    if [ "$REVOKER" ] ; then
+       keyParameters="${keyParameters}"$(cat <<EOF
+
+Revoker: 1:$REVOKER sensitive
 EOF
+)
+    fi
+
+    echo "The following key parameters will be used:"
+    echo "$keyParameters"
 
     read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
     if [ ${OK/y/Y} != 'Y' ] ; then
@@ -68,29 +96,17 @@ EOF
        failure "key for '$USERID' already exists"
     fi
 
-    echo "generating server key..."
-    gpg --batch --gen-key <<EOF
-Key-Type: $KEY_TYPE
-Key-Length: $KEY_LENGTH
-Key-Usage: $KEY_USAGE
-Name-Real: $USERID
+    # add commit command
+    keyParameters="${keyParameters}"$(cat <<EOF
+
 %commit
+%echo done
 EOF
-}
-
-# publish server key to keyserver
-publish_key() {
-    read -p "publish key to $KEYSERVER? [Y|n]: " OK; OK=${OK:=Y}
-    if [ ${OK/y/Y} != 'Y' ] ; then
-       failure "aborting."
-    fi
+)
 
-    keyID=$(gpg --list-key --with-colons ="$USERID" 2> /dev/null | grep '^pub:' | cut -d: -f5)
-
-    # dummy command so as not to publish fakes keys during testing
-    # eventually:
-    #gpg --send-keys --keyserver "$KEYSERVER" "$keyID"
-    echo "gpg --send-keys --keyserver $KEYSERVER $keyID"
+    log -n "generating server key... "
+    echo "$keyParameters" | gpg --batch --gen-key
+    loge "done."
 }
 
 ########################################################################
@@ -109,96 +125,139 @@ MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
 [ -e "$MS_CONF" ] && . "$MS_CONF"
 
 # set empty config variable with defaults
-GNUPGHOME=${GNUPGHOME:-"$MS_HOME"/gnupg}
-KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
-REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
-USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-%h/.ssh/authorized_keys}
-STAGING_AREA=${STAGING_AREA:-"$LIB"/stage}
+GNUPGHOME=${GNUPGHOME:-"${MS_HOME}/gnupg"}
+KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
+CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
+REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
+USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
 
 export GNUPGHOME
 
+# make sure the monkeysphere home directory exists
+mkdir -p "${MS_HOME}/authorized_user_ids"
 # make sure gpg home exists with proper permissions
 mkdir -p -m 0700 "$GNUPGHOME"
+# make sure the authorized_keys directory exists
+mkdir -p "${CACHE}/authorized_keys"
 
 case $COMMAND in
-    'update-users'|'s')
+    'update-users'|'update-user'|'s')
        if [ "$1" ] ; then
            unames="$@"
        else
-           unames=$(ls -1 "$MS_HOME"/authorized_user_ids)
+           unames=$(ls -1 "${MS_HOME}/authorized_user_ids")
        fi
 
        for uname in $unames ; do
            MODE="authorized_keys"
-           authorizedUserIDs="$MS_HOME"/authorized_user_ids/"$uname"
-           cacheDir="$STAGING_AREA"/"$uname"/user_keys
-           msAuthorizedKeys="$STAGING_AREA"/"$uname"/authorized_keys
 
-            # make sure authorized_user_ids file exists
-           if [ ! -s "$authorizedUserIDs" ] ; then
-               log "authorized_user_ids file for '$uname' is empty or does not exist."
-               continue
-           fi
+           log "----- user: $uname -----"
 
-           log "processing authorized_keys for user '$uname'..."
+           # set variables for the user
+           AUTHORIZED_USER_IDS="${MS_HOME}/authorized_user_ids/${uname}"
+           # temporary authorized_keys file
+           AUTHORIZED_KEYS="${CACHE}/authorized_keys/${uname}.tmp"
 
-           process_authorized_ids "$authorizedUserIDs" "$cacheDir"
+            # make sure user's authorized_user_ids file exists
+           touch "$AUTHORIZED_USER_IDS"
+           # make sure the authorized_keys file exists and is clear
+           > "$AUTHORIZED_KEYS"
 
-            # write output key file
-           log "writing monkeysphere authorized_keys file... "
-           touch "$msAuthorizedKeys"
-           if [ "$(ls "$cacheDir")" ] ; then
-               log -n "adding gpg keys... "
-               cat "$cacheDir"/* > "$msAuthorizedKeys"
-               echo "done."
-           else
-               log "no gpg keys to add."
+           # skip if the user's authorized_user_ids file is empty
+           if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
+               log "authorized_user_ids file for '$uname' is empty."
+               continue
            fi
-           if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then
+
+           # process authorized_user_ids file
+           log "processing authorized_user_ids file..."
+           process_authorized_user_ids
+
+           # add user-controlled authorized_keys file path if specified
+           if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
                userHome=$(getent passwd "$uname" | cut -d: -f6)
                userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$userHome"}
-               if [ -s "$userAuthorizedKeys" ] ; then
-                   log -n "adding user authorized_keys file... "
-                   cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
-                   echo "done."
-               fi
+               log -n "adding user's authorized_keys file... "
+               cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
+               loge "done."
            fi
-           log "monkeysphere authorized_keys file generated:"
-           log "$msAuthorizedKeys"
+
+           # move the temp authorized_keys file into place
+           mv -f "${CACHE}/authorized_keys/${uname}.tmp" "${CACHE}/authorized_keys/${uname}"
+
+           log "authorized_keys file updated."
        done
+
+       log "----- done. -----"
        ;;
 
     'gen-key'|'g')
-       gen_key
+       gen_key "$1"
        ;;
 
     'publish-key'|'p')
-       publish_key
+       publish_server_key
        ;;
 
-    'trust-key'|'t')
+    'trust-keys'|'trust-key'|'t')
        if [ -z "$1" ] ; then
-           failure "you must specify at least one key to trust."
+           failure "You must specify at least one key to trust."
        fi
+
+       # process key IDs
        for keyID ; do
            trust_key "$keyID"
        done
        ;;
 
-    'update-user-userids'|'u')
+    'update-user-userids'|'update-user-userid'|'u')
        uname="$1"
        shift
        if [ -z "$uname" ] ; then
-           failure "you must specify user."
+           failure "You must specify user."
        fi
        if [ -z "$1" ] ; then
-           failure "you must specify at least one userid."
+           failure "You must specify at least one user ID."
        fi
+
+       # set variables for the user
        AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
-       userKeysCacheDir="$STAGING_AREA"/"$uname"/user_keys
+
+        # make sure user's authorized_user_ids file exists
+       touch "$AUTHORIZED_USER_IDS"
+
+       # process the user IDs
        for userID ; do
-           update_userid "$userID" "$userKeysCacheDir"
+           update_userid "$userID"
        done
+
+       log "Run the following to update user's authorized_keys file:"
+       log "$PGRM update-users $uname"
+       ;;
+
+    'remove-user-userids'|'remove-user-userid'|'r')
+       uname="$1"
+       shift
+       if [ -z "$uname" ] ; then
+           failure "You must specify user."
+       fi
+       if [ -z "$1" ] ; then
+           failure "You must specify at least one user ID."
+       fi
+
+       # set variables for the user
+       AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
+
+        # make sure user's authorized_user_ids file exists
+       touch "$AUTHORIZED_USER_IDS"
+
+       # process the user IDs
+       for userID ; do
+           remove_userid "$userID"
+       done
+
+       log "Run the following to update user's authorized_keys file:"
+       log "$PGRM update-users $uname"
        ;;
 
     'help'|'h'|'?')
@@ -207,6 +266,6 @@ case $COMMAND in
 
     *)
         failure "Unknown command: '$COMMAND'
-Type 'cereal-admin help' for usage."
+Type '$PGRM help' for usage."
         ;;
 esac