improve function to get primary group to make it more portable
[monkeysphere.git] / src / share / common
index 04fe4fe462e9c7a540c5a582f2b50ffa4fbb387f..76b539f4ab89dc9ddd8d585a751f64ed4f514430 100644 (file)
@@ -254,7 +254,7 @@ check_capability() {
 file_hash() {
     if type md5sum &>/dev/null ; then
        md5sum "$1"
-    elif type lockfile &>/dev/null ; then
+    elif type md5 &>/dev/null ; then
        md5 "$1"
     else
        failure "Neither md5sum nor md5 are in the path!"
@@ -390,7 +390,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 +425,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'."
@@ -445,7 +451,7 @@ check_key_file_permissions() {
     # return 2 if path has group or other writability
     if is_write "$gAccess" || is_write "$oAccess" ; then
        log error "improper group or other writability on path '$path':"
-       log error " group: $gAccess, other: $oAcess"
+       log error " group: $gAccess, other: $oAccess"
        return 2
     fi
 
@@ -458,6 +464,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