Merge commit 'mlcastle/master'
[monkeysphere.git] / src / share / common
index ac0b5d6382a9dd2c345209d0b18d605b1ee84d27..8e2232d659350558e472bd5f37c365d050317b8d 100644 (file)
@@ -291,7 +291,8 @@ Please specify how long the key should be valid.
       <n>y = key expires in n years
 EOF
        while [ -z "$keyExpire" ] ; do
-           read -p "Key is valid for? (0) " keyExpire
+           printf "Key is valid for? (0) " >&2
+           read keyExpire
            if ! test_gpg_expire ${keyExpire:=0} ; then
                echo "invalid value" >&2
                unset keyExpire
@@ -390,7 +391,7 @@ translate_ssh_variables() {
     path="$2"
 
     # get the user's home directory
-    userHome=$(getent passwd "$uname" | cut -d: -f6)
+    userHome=$(get_homedir "$uname")
 
     # translate '%u' to user name
     path=${path/\%u/"$uname"}
@@ -425,6 +426,12 @@ check_key_file_permissions() {
 
     log debug "checking path permission '$path'..."
 
+    # rewrite path if it points to a symlink
+    if [ -h "$path" ] ; then
+       path=$(readlink -f "$path")
+       log debug "checking path symlink '$path'..."
+    fi
+
     # return 255 if cannot stat file
     if ! stat=$(ls -ld "$path" 2>/dev/null) ; then
         log error "could not stat path '$path'."
@@ -458,6 +465,31 @@ check_key_file_permissions() {
     fi
 }
 
+# return a list of all users on the system
+list_users() {
+    if type getent &>/dev/null ; then
+       # for linux and FreeBSD systems
+       getent passwd | cut -d: -f1
+    elif type dscl &>/dev/null ; then
+       # for Darwin systems
+       dscl localhost -list /Search/Users
+    else
+       failure "Neither getent or dscl is in the path!  Could not determine list of users."
+    fi
+}
+
+# return the path to the home directory of a user
+get_homedir() {
+    local uname=${1:-`whoami`}
+    eval "echo ~${uname}"
+}
+
+# return the primary group of a user
+get_primary_group() {
+    local uname=${1:-`whoami`}
+    groups "$uname" | sed 's/^..* : //' | awk '{ print $1 }'
+}
+
 ### CONVERSION UTILITIES
 
 # output the ssh key for a given key ID