]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/acl/unix.c
Merge master
[netatalk.git] / libatalk / acl / unix.c
index 3e3b0209e66319c6687257f3b86001beda7d0245..0ff0ea5b6c169597ef5208767231329d0d320354 100644 (file)
@@ -16,7 +16,7 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#ifdef HAVE_NFSv4_ACLS
+#ifdef HAVE_SOLARIS_ACLS
 
 #include <unistd.h>
 #include <sys/types.h>
@@ -30,6 +30,7 @@
 
 #include <atalk/logger.h>
 #include <atalk/afp.h>
+#include <atalk/util.h>
 #include <atalk/acl.h>
 
 /* Get ACL. Allocates storage as needed. Caller must free.
@@ -42,13 +43,24 @@ int get_nfsv4_acl(const char *name, ace_t **retAces)
 
     *retAces = NULL;
     /* Only call acl() for regular files and directories, otherwise just return 0 */
-    if (lstat(name, &st) != 0)
+    if (lstat(name, &st) != 0) {
+        LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): %s", getcwdpath(), name, strerror(errno));
         return -1;
-    if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
+    }
+
+    if (S_ISLNK(st.st_mode))
+        /* sorry, no ACLs for symlinks */
+        return 0;
+
+    if ( ! ((S_ISREG(st.st_mode)) || (S_ISDIR(st.st_mode)))) {
+        LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): special", getcwdpath(), name);
         return 0;
+    }
 
-    if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
+    if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0) {
+        LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): 0 ACEs", getcwdpath(), name);
         return 0;
+    }
 
     if (ace_count == -1) {
         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
@@ -74,6 +86,37 @@ int get_nfsv4_acl(const char *name, ace_t **retAces)
     return ace_count;
 }
 
+/*
+  Concatenate ACEs
+*/
+ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
+{
+    ace_t *new_aces;
+    int i, j;
+
+    /* malloc buffer for new ACL */
+    if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
+        LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
+        return NULL;
+    }
+
+    /* Copy ACEs from buf1 */
+    for (i=0; i < ace1count; ) {
+        memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
+        i++;
+    }
+
+    j = i;
+
+    /* Copy ACEs from buf2 */
+    for (i=0; i < ace2count; ) {
+        memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
+        i++;
+        j++;
+    }
+    return new_aces;
+}
+
 /*
   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
 */
@@ -84,6 +127,9 @@ int strip_trivial_aces(ace_t **saces, int sacecount)
     ace_t *aces = *saces;
     ace_t *new_aces;
 
+    if (aces == NULL || sacecount <= 0)
+        return 0;
+
     /* Count non-trivial ACEs */
     for (i=0; i < sacecount; ) {
         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
@@ -113,37 +159,6 @@ int strip_trivial_aces(ace_t **saces, int sacecount)
     return nontrivaces;
 }
 
-/*
-  Concatenate ACEs
-*/
-ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
-{
-    ace_t *new_aces;
-    int i, j;
-
-    /* malloc buffer for new ACL */
-    if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
-        LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
-        return NULL;
-    }
-
-    /* Copy ACEs from buf1 */
-    for (i=0; i < ace1count; ) {
-        memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
-        i++;
-    }
-
-    j = i;
-
-    /* Copy ACEs from buf2 */
-    for (i=0; i < ace2count; ) {
-        memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
-        i++;
-        j++;
-    }
-    return new_aces;
-}
-
 /*
   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
 */
@@ -200,112 +215,46 @@ int strip_nontrivial_aces(ace_t **saces, int sacecount)
  */
 int nfsv4_chmod(char *name, mode_t mode)
 {
-
-}
-
-#if 0
-static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
-{
-    int ret, i, nfsv4_ace_count, tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
-    ace_t *old_aces, *new_aces = NULL;
-    uint16_t flags;
-    uint32_t ace_count;
-
-    LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
-
-    /*  Get no of ACEs the client put on the wire */
-    ace_count = htonl(*((uint32_t *)ibuf));
-    ibuf += 8;      /* skip ACL flags (see acls.h) */
-
-    if (inherit)
-        /* inherited + trivial ACEs */
-        flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
-    else
-        /* only trivial ACEs */
-        flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
-
-    /* Get existing ACL and count ACEs which have to be copied */
-    if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
-        return AFPERR_MISC;
-    for ( i=0; i < nfsv4_ace_count; i++) {
-        if (old_aces[i].a_flags & flags)
-            tocopy_aces_count++;
-    }
-
-    /* Now malloc buffer exactly sized to fit all new ACEs */
-    new_aces = malloc( (ace_count + tocopy_aces_count) * sizeof(ace_t) );
-    if (new_aces == NULL) {
-        LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
-        ret = AFPERR_MISC;
-        goto cleanup;
-    }
-
-    /* Start building new ACL */
-
-    /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
-       inherited ACEs first. */
-    if (inherit) {
-        for (i=0; i < nfsv4_ace_count; i++) {
-            if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
-                memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
-                new_aces_count++;
-            }
-        }
-    }
-    LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
-
-    /* Now the ACEs from the client */
-    ret = map_acl(DARWIN_2_SOLARIS, &new_aces[new_aces_count], (darwin_ace_t *)ibuf, ace_count);
-    if (ret == -1) {
-        ret = AFPERR_PARAM;
-        goto cleanup;
-    }
-    new_aces_count += ace_count;
-    LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
-
-    /* Now copy the trivial ACEs */
-    for (i=0; i < nfsv4_ace_count; i++) {
-        if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
-            memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
-            new_aces_count++;
-            trivial_ace_count++;
-        }
-    }
-    LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
-
-    /* Ressourcefork first.
-       Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
-       might be strange for ACE_DELETE_CHILD and for inheritance flags. */
-    if ( (ret = vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
-        LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
-        if (errno == (EACCES | EPERM))
-            ret = AFPERR_ACCESS;
-        else if (errno == ENOENT)
-            ret = AFPERR_NOITEM;
-        else
-            ret = AFPERR_MISC;
-        goto cleanup;
-    }
-    if ( (ret = acl(name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
-        LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
-        if (errno == (EACCES | EPERM))
-            ret = AFPERR_ACCESS;
-        else if (errno == ENOENT)
-            ret = AFPERR_NOITEM;
-        else
-            ret = AFPERR_MISC;
-        goto cleanup;
+    int ret = -1;
+    int noaces, nnaces;
+    ace_t *oacl = NULL, *nacl = NULL, *cacl = NULL;
+
+    LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
+        getcwdpath(), name, mode);
+
+    if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
+        goto exit;
+    if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
+        goto exit;
+
+#ifdef chmod
+#undef chmod
+#endif
+    if (chmod(name, mode) != 0) /* (3) */
+        goto exit;
+
+    if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
+        goto exit;
+    if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
+        goto exit;
+
+    if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
+        goto exit;
+
+    if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
+        LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
+        goto exit;
     }
 
-    ret = AFP_OK;
+exit:
+    if (oacl) free(oacl);
+    if (nacl) free(nacl);
+    if (cacl) free(cacl);
 
-cleanup:
-    free(old_aces);
-    free(new_aces);
+    LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
+        getcwdpath(), name, mode, ret);
 
-    LOG(log_debug9, logtype_afpd, "set_acl: END");
     return ret;
 }
-#endif  /* 0 */
 
-#endif /* HAVE_NFSv4_ACLS */
+#endif /* HAVE_SOLARIS_ACLS */