2 $Id: acl.c,v 1.2 2009-11-26 18:17:12 franklahm Exp $
3 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 #endif /* HAVE_CONFIG_H */
26 #include <atalk/afp.h>
27 #include <atalk/util.h>
28 #include <atalk/logger.h>
30 /* Get ACL. Allocates storage as needed. Caller must free.
31 * Returns no of ACEs or -1 on error. */
32 int get_nfsv4_acl(const char *name, ace_t **retAces)
38 ace_count = acl(name, ACE_GETACLCNT, 0, NULL);
40 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACLCNT) error: ace_count %i, error: %s",
41 ace_count, strerror(errno));
45 aces = malloc(ace_count * sizeof(ace_t));
47 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
51 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
52 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
57 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
63 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
64 int remove_acl(const char *name)
66 int ret,i, ace_count, trivial_aces, new_aces_count;
67 ace_t *old_aces = NULL;
68 ace_t *new_aces = NULL;
70 LOG(log_debug9, logtype_afpd, "remove_acl: BEGIN");
72 /* Get existing ACL and count trivial ACEs */
73 if ((ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
76 for ( i=0; i < ace_count; i++) {
77 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))
81 /* malloc buffer for new ACL */
82 if ((new_aces = malloc(trivial_aces * sizeof(ace_t))) == NULL) {
83 LOG(log_error, logtype_afpd, "remove_acl: malloc %s", strerror(errno));
88 /* Now copy the trivial ACEs */
90 for (i=0; i < ace_count; i++) {
91 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
92 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
97 if ( (acl(name, ACE_SETACL, trivial_aces, new_aces)) == 0)
100 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
101 if (errno == (EACCES | EPERM))
103 else if (errno == ENOENT)
113 LOG(log_debug9, logtype_afpd, "remove_acl: END");