]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/acls.c
Merge from branch-2-1
[netatalk.git] / etc / afpd / acls.c
index 5c657e195d5f53dd63e9d2f4d28f532e9d04bd5f..76d87c12645e60e618e28256df65b5faaa6acc97 100644 (file)
 #include <atalk/logger.h>
 #include <atalk/uuid.h>
 #include <atalk/acl.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
 
 #include "directory.h"
 #include "desktop.h"
 #include "volume.h"
 #include "fork.h"
-
+#include "unix.h"
 #include "acls.h"
 #include "acl_mappings.h"
+#include "auth.h"
 
 /* for map_acl() */
 #define SOLARIS_2_DARWIN       1
 #define IS_DIR                 32
 
 /********************************************************
- * Basic and helper funcs
+ * Solaris funcs
  ********************************************************/
 
-/*
-  Takes a users name, uid and primary gid and checks if user is member of any group
-  Returns -1 if no or error, 0 if yes
-*/
-static int check_group(char *name, uid_t uid _U_, gid_t pgid, gid_t path_gid)
+#ifdef HAVE_SOLARIS_ACLS
+
+/*! 
+ * Compile access rights for a user to one file-system object
+ *
+ * This combines combines all access rights for a user to one fs-object and
+ * returns the result as a Darwin allowed rights ACE.
+ * This must honor trivial ACEs which are a mode_t mapping.
+ *
+ * @param path           (r) path to filesystem object
+ * @param sb             (r) struct stat of path
+ * @param result         (w) resulting Darwin allow ACE
+ *
+ * @returns                  0 or -1 on error
+ */
+static int solaris_acl_rights(const char *path,
+                              const struct stat *sb,
+                              uint32_t *result)
 {
     EC_INIT;
-    int i;
-    struct group *grp;
+    int      i, ace_count, checkgroup;
+    ace_t    *aces = NULL;
+    uid_t    who;
+    uint16_t flags, type;
+    uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
 
-    if (pgid == path_gid)
-        return 0;
+    /* Get ACL from file/dir */
+    EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
 
-    EC_NULL(grp = getgrgid(path_gid));
+    if (ace_count == 0) {
+        LOG(log_warning, logtype_afpd, "Zero ACEs from get_nfsv4_acl");
+        EC_FAIL;
+    }
 
+    /* Now check requested rights */
     i = 0;
-    while (grp->gr_mem[i] != NULL) {
-        if ( (strcmp(grp->gr_mem[i], name)) == 0 ) {
-            LOG(log_debug, logtype_afpd, "check_group: requested user:%s is member of: %s", name, grp->gr_name);
-            return 0;
+    do { /* Loop through ACEs */
+        who = aces[i].a_who;
+        flags = aces[i].a_flags;
+        type = aces[i].a_type;
+        rights = aces[i].a_access_mask;
+
+        if (flags & ACE_INHERIT_ONLY_ACE)
+            continue;
+
+        /* Now the tricky part: decide if ACE effects our user. I'll explain:
+           if its a dedicated (non trivial) ACE for the user
+           OR
+           if its a ACE for a group we're member of
+           OR
+           if its a trivial ACE_OWNER ACE and requested UUID is the owner
+           OR
+           if its a trivial ACE_GROUP ACE and requested UUID is group
+           OR
+           if its a trivial ACE_EVERYONE ACE
+           THEN
+           process ACE */
+        if (((who == uuid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
+            ||
+            ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who))
+            ||
+            ((flags & ACE_OWNER) && (uuid == sb->st_uid))
+            ||
+            ((flags & ACE_GROUP) && gmem(sb->st_gid))
+            ||
+            (flags & ACE_EVERYONE)
+            ) {
+            /* Found an applicable ACE */
+            if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
+                allowed_rights |= rights;
+            else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
+                /* Only or to denied rights if not previously allowed !! */
+                denied_rights |= ((!allowed_rights) & rights);
         }
-        i++;
+    } while (++i < ace_count);
+
+
+    /* Darwin likes to ask for "delete_child" on dir,
+       "write_data" is actually the same, so we add that for dirs */
+    if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
+        allowed_rights |= ACE_DELETE_CHILD;
+
+    /* Remove denied from allowed rights */
+    allowed_rights &= ~denied_rights;
+
+    /* map rights */
+    darwin_rights = 0;
+    for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
+        if (allowed_rights & nfsv4_to_darwin_rights[i].from)
+            darwin_rights |= nfsv4_to_darwin_rights[i].to;
     }
 
-    return -1;
+    *result |= darwin_rights;
+
 EC_CLEANUP:
+    if (aces) free(aces);
+
     EC_EXIT;
 }
 
-/********************************************************
- * Solaris funcs
- ********************************************************/
-
-#ifdef HAVE_SOLARIS_ACLS
-
 /*
   Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
   Return numer of mapped ACEs or -1 on error.
   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
 */
-static int map_aces_solaris_to_darwin(ace_t *aces, darwin_ace_t *darwin_aces, int ace_count)
+static int map_aces_solaris_to_darwin(const ace_t *aces,
+                                      darwin_ace_t *darwin_aces,
+                                      int ace_count)
 {
     EC_INIT;
     int i, count = 0;
@@ -113,32 +183,31 @@ static int map_aces_solaris_to_darwin(ace_t *aces, darwin_ace_t *darwin_aces, in
     struct passwd *pwd = NULL;
     struct group *grp = NULL;
 
-    LOG(log_debug7, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
+    LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
 
     while(ace_count--) {
-        LOG(log_debug7, logtype_afpd, "map_aces_solaris_to_darwin: parsing ACE No. %d", ace_count + 1);
+        LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
         /* if its a ACE resulting from nfsv4 mode mapping, discard it */
         if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
-            LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: trivial ACE");
+            LOG(log_debug, logtype_afpd, "trivial ACE");
             aces++;
             continue;
         }
 
-        if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) {
-            /* its a user ace */
-            LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: found user ACE with uid: %d", aces->a_who);
-
+        if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
+            LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
             EC_NULL_LOG(pwd = getpwuid(aces->a_who));
-            LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: uid: %d -> name: %s", aces->a_who, pwd->pw_name);
-
-            EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
-        } else {
-            /* its a group ace */
-            LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: found group ACE with gid: %d", aces->a_who);
-
+            LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
+            EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
+                                        UUID_USER,
+                                        darwin_aces->darwin_ace_uuid));
+        } else { /* group ace */
+            LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
             EC_NULL_LOG(grp = getgrgid(aces->a_who));
-            LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: gid: %d -> name: %s", aces->a_who, grp->gr_name);
-            EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
+            LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
+            EC_ZERO_LOG(getuuidfromname(grp->gr_name,
+                                        UUID_GROUP,
+                                        darwin_aces->darwin_ace_uuid));
         }
 
         /* map flags */
@@ -179,7 +248,9 @@ EC_CLEANUP:
   Return numer of mapped ACEs or -1 on error.
   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
 */
-int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces, ace_t *nfsv4_aces, int ace_count)
+static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
+                                      ace_t *nfsv4_aces,
+                                      int ace_count)
 {
     EC_INIT;
     int i, mapped_aces = 0;
@@ -265,6 +336,133 @@ EC_CLEANUP:
 
 #ifdef HAVE_POSIX_ACLS
 
+static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
+{
+    EC_INIT;
+    uint32_t rights = 0;
+    acl_permset_t permset;
+
+    EC_ZERO_LOG(acl_get_permset(e, &permset));
+
+    if (acl_get_perm(permset, ACL_READ))
+        rights = DARWIN_ACE_READ_DATA
+            | DARWIN_ACE_READ_EXTATTRIBUTES
+            | DARWIN_ACE_READ_ATTRIBUTES
+            | DARWIN_ACE_READ_SECURITY;
+    if (acl_get_perm(permset, ACL_WRITE)) {
+        rights |= DARWIN_ACE_WRITE_DATA
+            | DARWIN_ACE_APPEND_DATA
+            | DARWIN_ACE_WRITE_EXTATTRIBUTES
+            | DARWIN_ACE_WRITE_ATTRIBUTES;
+        if (is_dir)
+            rights |= DARWIN_ACE_DELETE_CHILD;
+    }
+    if (acl_get_perm(permset, ACL_EXECUTE))
+        rights |= DARWIN_ACE_EXECUTE;
+
+EC_CLEANUP:
+    LOG(log_maxdebug, logtype_afpd, "mapped rights: 0x%08x", rights);
+    return rights;
+}
+
+/*! 
+ * Compile access rights for a user to one file-system object
+ *
+ * This combines combines all access rights for a user to one fs-object and
+ * returns the result as a Darwin allowed rights ACE.
+ * This must honor trivial ACEs which are a mode_t mapping.
+ *
+ * @param path           (r) path to filesystem object
+ * @param sb             (r) struct stat of path
+ * @param result         (rw) resulting Darwin allow ACE
+ *
+ * @returns                  0 or -1 on error
+ */
+static int posix_acl_rights(const char *path,
+                            const struct stat *sb,
+                            uint32_t *result)
+{
+    EC_INIT;
+    int havemask = 0;
+    int entry_id = ACL_FIRST_ENTRY;
+    uint32_t rights = 0, maskrights = 0;
+    uid_t *uid = NULL;
+    gid_t *gid = NULL;
+    acl_t acl = NULL;
+    acl_entry_t e;
+    acl_tag_t tag;
+
+    EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
+
+    /* itereate through all ACEs to get the mask */
+    while (!havemask && acl_get_entry(acl, entry_id, &e) == 1) {
+        entry_id = ACL_NEXT_ENTRY;
+        EC_ZERO_LOG(acl_get_tag_type(e, &tag));
+        switch (tag) {
+        case ACL_MASK:
+            maskrights = posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
+            LOG(log_maxdebug, logtype_afpd, "maskrights: 0x%08x", maskrights);
+            havemask = 1;
+            break;
+        default:
+            continue;
+        }
+    }
+
+    /* itereate through all ACEs */
+    entry_id = ACL_FIRST_ENTRY;
+    while (acl_get_entry(acl, entry_id, &e) == 1) {
+        entry_id = ACL_NEXT_ENTRY;
+        EC_ZERO_LOG(acl_get_tag_type(e, &tag));
+        switch (tag) {
+        case ACL_USER:
+            EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
+            if (*uid == uuid) {
+                LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
+                rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
+            }
+            acl_free(uid);
+            uid = NULL;
+            break;
+        case ACL_USER_OBJ:
+            if (sb->st_uid == uuid) {
+                LOG(log_maxdebug, logtype_afpd, "ACL_USER_OBJ: %u", sb->st_uid);
+                rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
+            }
+            break;
+        case ACL_GROUP:
+            EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
+            if (gmem(*gid)) {
+                LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
+                rights |= (posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode)) & maskrights);
+            }
+            acl_free(gid);
+            gid = NULL;
+            break;
+        case ACL_GROUP_OBJ:
+            if (gmem(sb->st_gid)) {
+                LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
+                rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));            
+            }
+            break;
+        case ACL_OTHER:
+            LOG(log_maxdebug, logtype_afpd, "ACL_OTHER");
+            rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
+            break;
+        default:
+            continue;
+        }
+    } /* while */
+
+    *result |= rights;
+
+EC_CLEANUP:
+    if (acl) acl_free(acl);
+    if (uid) acl_free(uid);
+    if (gid) acl_free(gid);
+    EC_EXIT;
+}
+
 /*!
  * Add entries of one acl to another acl
  *
@@ -481,17 +679,15 @@ static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darw
 {
     EC_INIT;
     int mapped_aces = 0;
-    int havemask = 0;
     int entry_id = ACL_FIRST_ENTRY;
     acl_entry_t e;
     acl_tag_t tag;
-    acl_permset_t permset, mask;
     uid_t *uid = NULL;
     gid_t *gid = NULL;
     struct passwd *pwd = NULL;
     struct group *grp = NULL;
     uint32_t flags;
-    uint32_t rights;
+    uint32_t rights, maskrights = 0;
     darwin_ace_t *saved_darwin_aces = darwin_aces;
 
     LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
@@ -527,10 +723,8 @@ static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darw
             gid = NULL;
             break;
 
-            /* store mask so we can apply it later in a second loop */
         case ACL_MASK:
-            EC_ZERO_LOG(acl_get_permset(e, &mask));
-            havemask = 1;
+            maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
             continue;
 
         default:
@@ -546,55 +740,19 @@ static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darw
         darwin_aces->darwin_ace_flags = htonl(flags);
 
         /* rights */
-        EC_ZERO_LOG(acl_get_permset(e, &permset));
-
-        rights = 0;
-        if (acl_get_perm(permset, ACL_READ))
-            rights = DARWIN_ACE_READ_DATA
-                | DARWIN_ACE_READ_EXTATTRIBUTES
-                | DARWIN_ACE_READ_ATTRIBUTES
-                | DARWIN_ACE_READ_SECURITY;
-        if (acl_get_perm(permset, ACL_WRITE)) {
-            rights |= DARWIN_ACE_WRITE_DATA
-                | DARWIN_ACE_APPEND_DATA
-                | DARWIN_ACE_WRITE_EXTATTRIBUTES
-                | DARWIN_ACE_WRITE_ATTRIBUTES;
-            if ((type & ~MAP_MASK) == IS_DIR)
-                rights |= DARWIN_ACE_DELETE;
-        }
-        if (acl_get_perm(permset, ACL_EXECUTE))
-            rights |= DARWIN_ACE_EXECUTE;
-
+        rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
         darwin_aces->darwin_ace_rights = htonl(rights);
 
         darwin_aces++;
         mapped_aces++;
     } /* while */
 
-    if (havemask) {
-        /* Loop through the mapped ACE buffer once again, applying the mask */
-        /* Map the mask to Darwin ACE rights first */
-        rights = 0;
-        if (acl_get_perm(mask, ACL_READ))
-            rights = DARWIN_ACE_READ_DATA
-                | DARWIN_ACE_READ_EXTATTRIBUTES
-                | DARWIN_ACE_READ_ATTRIBUTES
-                | DARWIN_ACE_READ_SECURITY;
-        if (acl_get_perm(mask, ACL_WRITE)) {
-            rights |= DARWIN_ACE_WRITE_DATA
-                | DARWIN_ACE_APPEND_DATA
-                | DARWIN_ACE_WRITE_EXTATTRIBUTES
-                | DARWIN_ACE_WRITE_ATTRIBUTES;
-            if ((type & ~MAP_MASK) == IS_DIR)
-                rights |= DARWIN_ACE_DELETE;
-        }
-        if (acl_get_perm(mask, ACL_EXECUTE))
-            rights |= DARWIN_ACE_EXECUTE;
-        for (int i = mapped_aces; i > 0; i--) {
-            saved_darwin_aces->darwin_ace_rights &= htonl(rights);
-            saved_darwin_aces++;
-        }
+    /* Loop through the mapped ACE buffer once again, applying the mask */
+    for (int i = mapped_aces; i > 0; i--) {
+        saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
+        saved_darwin_aces++;
     }
+
     EC_STATUS(mapped_aces);
 
 EC_CLEANUP:
@@ -688,7 +846,7 @@ static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
     acl_t defacl = NULL , accacl = NULL;
 
     /* stat to check if its a dir */
-    EC_ZERO_LOG(stat(name, &st));
+    EC_ZERO_LOG(lstat(name, &st));
 
     /* if its a dir, check for default acl too */
     dirflag = 0;
@@ -814,8 +972,8 @@ static int set_acl(const struct vol *vol,
         EC_STATUS(AFPERR_PARAM);
         goto EC_CLEANUP;
     }
-    new_aces_count += ace_count;
-    LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
+    new_aces_count += ret;
+    LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
 
     /* Now copy the trivial ACEs */
     for (i=0; i < nfsv4_ace_count; i++) {
@@ -876,7 +1034,7 @@ static int set_acl(const struct vol *vol,
     LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
 
     struct stat st;
-    EC_ZERO_LOG_ERR(stat(name, &st), AFPERR_NOOBJ);
+    EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
 
     /* seed default ACL with access ACL */
     if (S_ISDIR(st.st_mode))
@@ -914,190 +1072,114 @@ EC_CLEANUP:
 }
 #endif /* HAVE_POSIX_ACLS */
 
-/*
-  Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
-  Note: this gets called frequently and is a good place for optimizations !
+/*!
+ * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
+ *
+ * Note: this gets called frequently and is a good place for optimizations !
+ *
+ * @param vol              (r) volume
+ * @param dir              (r) directory
+ * @param path             (r) path to filesystem object
+ * @param uuid             (r) UUID of user
+ * @param requested_rights (r) requested Darwin ACE
+ *
+ * @returns                    AFP result code
 */
-#ifdef HAVE_SOLARIS_ACLS
-static int check_acl_access(const char *path, const uuidp_t uuid, uint32_t requested_darwin_rights)
+static int check_acl_access(const struct vol *vol,
+                            const struct dir *dir,
+                            const char *path,
+                            const uuidp_t uuid,
+                            uint32_t requested_rights)
 {
-    int                 ret, i, ace_count, dir, checkgroup;
-    char                *username = NULL; /* might be group too */
-    uuidtype_t          uuidtype;
-    uid_t               uid;
-    gid_t               pgid;
-    uint32_t            requested_rights = 0, allowed_rights = 0, denied_rights = 0;
-    ace_t               *aces;
-    struct passwd       *pwd;
-    struct stat         st;
-    int                 check_user_trivace = 0, check_group_trivace = 0;
-    uid_t               who;
-    uint16_t            flags;
-    uint16_t            type;
-    uint32_t            rights;
-
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "check_access: BEGIN. Request: %08x", requested_darwin_rights);
-#endif
-    /* Get uid or gid from UUID */
-    if ( (getnamefromuuid(uuid, &username, &uuidtype)) != 0) {
-        LOG(log_error, logtype_afpd, "check_access: error getting name from UUID");
-        return AFPERR_PARAM;
-    }
+    int            ret;
+    uint32_t       allowed_rights = 0;
+    char           *username = NULL;
+    uuidtype_t     uuidtype;
+    struct stat    st;
+    bstring        parent = NULL;
 
-    /* File or dir */
-    if ((lstat(path, &st)) != 0) {
-        LOG(log_error, logtype_afpd, "check_access: stat: %s", strerror(errno));
-        ret = AFPERR_PARAM;
-        goto exit;
-    }
-    dir = S_ISDIR(st.st_mode);
+    LOG(log_maxdebug, logtype_afpd, "check_access: Request: 0x%08x", requested_rights);
 
-    if (uuidtype == UUID_USER) {
-        pwd = getpwnam(username);
-        if (!pwd) {
-            LOG(log_error, logtype_afpd, "check_access: getpwnam: %s", strerror(errno));
-            ret = AFPERR_MISC;
-            goto exit;
-        }
-        uid = pwd->pw_uid;
-        pgid = pwd->pw_gid;
-
-        /* If user is file/dir owner we must check the user trivial ACE */
-        if (uid == st.st_uid) {
-            LOG(log_debug, logtype_afpd, "check_access: user: %s is files owner. Must check trivial user ACE", username);
-            check_user_trivace = 1;
-        }
-
-        /* Now check if requested user is files owning group. If yes we must check the group trivial ACE */
-        if ( (check_group(username, uid, pgid, st.st_gid)) == 0) {
-            LOG(log_debug, logtype_afpd, "check_access: user: %s is in group: %d. Must check trivial group ACE", username, st.st_gid);
-            check_group_trivace = 1;
-        }
-    } else { /* hopefully UUID_GROUP*/
-        LOG(log_error, logtype_afpd, "check_access: afp_access for UUID of groups not supported!");
-#if 0
-        grp = getgrnam(username);
-        if (!grp) {
-            LOG(log_error, logtype_afpd, "check_access: getgrnam: %s", strerror(errno));
-            return -1;
-        }
-        if (st.st_gid == grp->gr_gid )
-            check_group_trivace = 1;
-#endif
-    }
+    /* Get uid or gid from UUID */
+    EC_ZERO_LOG_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
+    EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
 
-    /* Map requested rights to Solaris style. */
-    for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
-        if (requested_darwin_rights & darwin_to_nfsv4_rights[i].from)
-            requested_rights |= darwin_to_nfsv4_rights[i].to;
-    }
+    switch (uuidtype) {
+    case UUID_USER:
+        break;
+    case UUID_GROUP:
+        LOG(log_warning, logtype_afpd, "check_access: afp_access not supported for groups");
+        EC_STATUS(AFPERR_MISC);
+        goto EC_CLEANUP;
 
-    /* Get ACL from file/dir */
-    if ( (ace_count = get_nfsv4_acl(path, &aces)) == -1) {
-        LOG(log_error, logtype_afpd, "check_access: error getting ACEs");
-        ret = AFPERR_MISC;
-        goto exit;
-    }
-    if (ace_count == 0) {
-        LOG(log_debug, logtype_afpd, "check_access: 0 ACEs from get_nfsv4_acl");
-        ret = AFPERR_MISC;
-        goto exit;
+    case UUID_LOCAL:
+        LOG(log_warning, logtype_afpd, "check_access: local UUID");
+        EC_STATUS(AFPERR_MISC);
+        goto EC_CLEANUP;
     }
 
-    /* Now check requested rights */
-    ret = AFPERR_ACCESS;
-    i = 0;
-    do { /* Loop through ACEs */
-        who = aces[i].a_who;
-        flags = aces[i].a_flags;
-        type = aces[i].a_type;
-        rights = aces[i].a_access_mask;
+#ifdef HAVE_SOLARIS_ACLS
+    EC_ZERO_LOG(solaris_acl_rights(path, &st, &allowed_rights));
+#endif
+#ifdef HAVE_POSIX_ACLS
+    EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
+#endif
 
-        if (flags & ACE_INHERIT_ONLY_ACE)
-            continue;
+    LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
 
-        /* Check if its a group ACE and set checkgroup to 1 if yes */
-        checkgroup = 0;
-        if ( (flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) ) {
-            if ( (check_group(username, uid, pgid, who)) == 0)
-                checkgroup = 1;
-            else
-                continue;
-        }
+    /*
+     * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
+     * directory. As it seems the 10.6 AFP client is puzzled when this right is not
+     * allowed where a delete would succeed because the parent dir gives write perms.
+     * So we check the parent dir for write access and set the right accordingly.
+     * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
+     * there to do this extra check -- afaict.
+     */
+    if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
+        int i;
+        uint32_t parent_rights = 0;
 
-        /* Now the tricky part: decide if ACE effects our user. I'll explain:
-           if its a dedicated (non trivial) ACE for the user
-           OR
-           if its a ACE for a group we're member of
-           OR
-           if its a trivial ACE_OWNER ACE and requested UUID is the owner
-           OR
-           if its a trivial ACE_GROUP ACE and requested UUID is group
-           OR
-           if its a trivial ACE_EVERYONE ACE
-           THEN
-           process ACE */
-        if (
-            ( (who == uid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)) ) ||
-            (checkgroup) ||
-            ( (flags & ACE_OWNER) && check_user_trivace ) ||
-            ( (flags & ACE_GROUP) && check_group_trivace ) ||
-            ( flags & ACE_EVERYONE )
-            ) {
-            /* Found an applicable ACE */
-            if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
-                allowed_rights |= rights;
-            else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
-                /* Only or to denied rights if not previously allowed !! */
-                denied_rights |= ((!allowed_rights) & rights);
+        if (dir->d_did == DIRDID_ROOT_PARENT) {
+            /* use volume path */
+            EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
+        } else {
+            /* build path for parent */
+            EC_NULL_LOG_ERR(parent = bstrcpy(dir->d_fullpath), AFPERR_MISC);
+            EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
+            EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
+            EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
+            EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
         }
-    } while (++i < ace_count);
 
+        LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
+        EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
 
-    /* Darwin likes to ask for "delete_child" on dir,
-       "write_data" is actually the same, so we add that for dirs */
-    if (dir && (allowed_rights & ACE_WRITE_DATA))
-        allowed_rights |= ACE_DELETE_CHILD;
+#ifdef HAVE_SOLARIS_ACLS
+        EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, &parent_rights));
+#endif
+#ifdef HAVE_POSIX_ACLS
+    EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
+#endif
+        if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
+            allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
+    }
 
-    if (requested_rights & denied_rights) {
-        LOG(log_debug, logtype_afpd, "check_access: some requested right was denied:");
-        ret = AFPERR_ACCESS;
-    } else if ((requested_rights & allowed_rights) != requested_rights) {
-        LOG(log_debug, logtype_afpd, "check_access: some requested right wasn't allowed:");
-        ret = AFPERR_ACCESS;
+    if ((requested_rights & allowed_rights) != requested_rights) {
+        LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
+            requested_rights, allowed_rights);
+        EC_STATUS(AFPERR_ACCESS);
     } else {
-        LOG(log_debug, logtype_afpd, "check_access: all requested rights are allowed:");
-        ret = AFP_OK;
+        LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
+            requested_rights);
+        EC_STATUS(AFP_OK);
     }
 
-    LOG(log_debug, logtype_afpd, "check_access: Requested rights: %08x, allowed_rights: %08x, denied_rights: %08x, Result: %d",
-        requested_rights, allowed_rights, denied_rights, ret);
-
-exit:
-    free(aces);
-    free(username);
-#ifdef DEBUG
-    LOG(log_debug9, logtype_afpd, "check_access: END");
-#endif
-    return ret;
-}
-#endif /* HAVE_SOLARIS_ACLS */
+EC_CLEANUP:
+    if (username) free(username);
+    if (parent) bdestroy(parent);
 
-#ifdef HAVE_POSIX_ACLS
-static int check_acl_access(const char *path, const uuidp_t uuid, uint32_t requested_darwin_rights)
-{
-    /*
-     * FIXME: for OS X >= 10.6 it seems fp_access isn't called anymore, instead
-     * the client just tries to perform any action, relying on the server
-     * to enforce permission (which the OS does for us), returning appropiate
-     * error codes in case the action failed.
-     * So to summarize: I think it's safe to not implement this function and
-     * just always return AFP_OK.
-     */
-    return AFP_OK;
+    EC_EXIT;
 }
-#endif /* HAVE_POSIX_ACLS */
 
 /********************************************************
  * Interface
@@ -1113,8 +1195,6 @@ int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size
     struct path         *s_path;
     uuidp_t             uuid;
 
-    LOG(log_debug9, logtype_afpd, "afp_access: BEGIN");
-
     *rbuflen = 0;
     ibuf += 2;
 
@@ -1156,9 +1236,8 @@ int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size
         return AFPERR_NOOBJ;
     }
 
-    ret = check_acl_access(s_path->u_name, uuid, darwin_ace_rights);
+    ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
 
-    LOG(log_debug9, logtype_afpd, "afp_access: END");
     return ret;
 }
 
@@ -1240,7 +1319,11 @@ int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size
     /* Shall we return ACL ? */
     if (bitmap & kFileSec_ACL) {
         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
-        get_and_map_acl(s_path->u_name, rbuf, rbuflen);
+        if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
+            LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
+                getcwdpath(), s_path->u_name);
+            return AFPERR_MISC;
+        }
     }
 
     LOG(log_debug9, logtype_afpd, "afp_getacl: END");
@@ -1345,124 +1428,81 @@ int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size
     return ret;
 }
 
-/*
-  unix.c/accessmode calls this: map ACL to OS 9 mode
-*/
-void acltoownermode(char *path, struct stat *st, uid_t uid, struct maccess *ma)
-{
-    struct passwd *pw;
-    atalk_uuid_t uuid;
-    int r_ok, w_ok, x_ok;
+/********************************************************************
+ * ACL funcs interfacing with other parts
+ ********************************************************************/
 
-    if ( ! (AFPobj->options.flags & OPTION_UUID) || ! (AFPobj->options.flags & OPTION_ACL2UARIGHTS))
-        return;
+/*!
+ * map ACL to user maccess
+ *
+ * This is the magic function that makes ACLs usable by calculating
+ * the access granted by ACEs to the logged in user.
+ */
+int acltoownermode(char *path, struct stat *st, struct maccess *ma)
+{
+    EC_INIT;
+    uint32_t rights = 0;
 
-    LOG(log_maxdebug, logtype_afpd, "acltoownermode('%s')", path);
+    if ( ! (AFPobj->options.flags & OPTION_ACL2MACCESS)
+         || (current_vol == NULL)
+         || ! (current_vol->v_flags & AFPVOL_ACLS))
+         return 0;
 
-    if ((pw = getpwuid(uid)) == NULL) {
-        LOG(log_error, logtype_afpd, "acltoownermode: %s", strerror(errno));
-        return;
-    }
+    LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
+        getcwdpath(), path, ma->ma_user);
 
-    /* We need the UUID for check_acl_access */
-    if ((getuuidfromname(pw->pw_name, UUID_USER, uuid)) != 0)
-        return;
+#ifdef HAVE_SOLARIS_ACLS
+    EC_ZERO_LOG(solaris_acl_rights(path, st, &rights));
+#endif
+#ifdef HAVE_POSIX_ACLS
+    EC_ZERO_LOG(posix_acl_rights(path, st, &rights));
+#endif
 
-    /* These work for files and dirs */
-    r_ok = check_acl_access(path, uuid, DARWIN_ACE_READ_DATA);
-    w_ok = check_acl_access(path, uuid, (DARWIN_ACE_WRITE_DATA|DARWIN_ACE_APPEND_DATA));
-    x_ok = check_acl_access(path, uuid, DARWIN_ACE_EXECUTE);
+    LOG(log_maxdebug, logtype_afpd, "rights: 0x%08x", rights);
 
-    LOG(log_debug7, logtype_afpd, "acltoownermode: ma_user before: %04o",ma->ma_user);
-    if (r_ok == 0)
+    if (rights & DARWIN_ACE_READ_DATA)
         ma->ma_user |= AR_UREAD;
-    if (w_ok == 0)
+    if (rights & DARWIN_ACE_WRITE_DATA)
         ma->ma_user |= AR_UWRITE;
-    if (x_ok == 0)
+    if (rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
         ma->ma_user |= AR_USEARCH;
-    LOG(log_debug7, logtype_afpd, "acltoownermode: ma_user after: %04o", ma->ma_user);
 
-    return;
+    LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x", ma->ma_user);
+
+EC_CLEANUP:
+    EC_EXIT;
 }
 
-/*
-  We're being called at the end of afp_createdir. We're (hopefully) inside dir
-  and ".AppleDouble" and ".AppleDouble/.Parent" should have already been created.
-  We then inherit any explicit ACE from "." to ".AppleDouble" and ".AppleDouble/.Parent".
-  FIXME: add to VFS layer ?
-*/
-#ifdef HAVE_SOLARIS_ACLS
-void addir_inherit_acl(const struct vol *vol)
+/*!
+ * Check whether a volume supports ACLs
+ *
+ * @param vol  (r) volume
+ *
+ * @returns        0 if not, 1 if yes
+ */
+int check_vol_acl_support(const struct vol *vol)
 {
-    ace_t *diraces = NULL, *adaces = NULL, *combinedaces = NULL;
-    int diracecount, adacecount;
-
-    LOG(log_debug9, logtype_afpd, "addir_inherit_acl: BEGIN");
-
-    /* Check if ACLs are enabled for the volume */
-    if (vol->v_flags & AFPVOL_ACLS) {
-
-        if ((diracecount = get_nfsv4_acl(".", &diraces)) <= 0)
-            goto cleanup;
-        /* Remove any trivial ACE from "." */
-        if ((diracecount = strip_trivial_aces(&diraces, diracecount)) <= 0)
-            goto cleanup;
-
-        /*
-          Inherit to ".AppleDouble"
-        */
-
-        if ((adacecount = get_nfsv4_acl(".AppleDouble", &adaces)) <= 0)
-            goto cleanup;
-        /* Remove any non-trivial ACE from ".AppleDouble" */
-        if ((adacecount = strip_nontrivial_aces(&adaces, adacecount)) <= 0)
-            goto cleanup;
-
-        /* Combine ACEs */
-        if ((combinedaces = concat_aces(diraces, diracecount, adaces, adacecount)) == NULL)
-            goto cleanup;
-
-        /* Now set new acl */
-        if ((acl(".AppleDouble", ACE_SETACL, diracecount + adacecount, combinedaces)) != 0)
-            LOG(log_error, logtype_afpd, "addir_inherit_acl: acl: %s", strerror(errno));
-
-        free(adaces);
-        adaces = NULL;
-        free(combinedaces);
-        combinedaces = NULL;
-
-        /*
-          Inherit to ".AppleDouble/.Parent"
-        */
-
-        if ((adacecount = get_nfsv4_acl(".AppleDouble/.Parent", &adaces)) <= 0)
-            goto cleanup;
-        if ((adacecount = strip_nontrivial_aces(&adaces, adacecount)) <= 0)
-            goto cleanup;
-
-        /* Combine ACEs */
-        if ((combinedaces = concat_aces(diraces, diracecount, adaces, adacecount)) == NULL)
-            goto cleanup;
-
-        /* Now set new acl */
-        if ((acl(".AppleDouble/.Parent", ACE_SETACL, diracecount + adacecount, combinedaces)) != 0)
-            LOG(log_error, logtype_afpd, "addir_inherit_acl: acl: %s", strerror(errno));
-
+    int ret = 1;
 
-    }
-
-cleanup:
-    LOG(log_debug9, logtype_afpd, "addir_inherit_acl: END");
-
-    free(diraces);
-    free(adaces);
-    free(combinedaces);
-}
-#endif /* HAVE_SOLARIS_ACLS */
+#ifdef HAVE_SOLARIS_ACLS
+    ace_t *aces = NULL;
+    if (get_nfsv4_acl(vol->v_path, &aces) == -1)
+        ret = 0;
+#endif
+#ifdef HAVE_POSIX_ACLS
+    acl_t acl = NULL;
+    if ((acl = acl_get_file(vol->v_path, ACL_TYPE_ACCESS)) == NULL)
+        ret = 0;
+#endif
 
+#ifdef HAVE_SOLARIS_ACLS
+    if (aces) free(aces);
+#endif
 #ifdef HAVE_POSIX_ACLS
-void addir_inherit_acl(const struct vol *vol)
-{
-    return;
-}
+    if (acl) acl_free(acl);
 #endif /* HAVE_POSIX_ACLS */
+
+    LOG(log_debug, logtype_afpd, "Volume \"%s\" ACL support: %s",
+        vol->v_path, ret ? "yes" : "no");
+    return ret;
+}