2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
20 #include <sys/types.h>
27 #include <atalk/afp.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
31 /* Get ACL. Allocates storage as needed. Caller must free.
32 * Returns no of ACEs or -1 on error. */
33 int get_nfsv4_acl(const char *name, ace_t **retAces)
40 /* Only call acl() for regular files and directories, otherwise just return 0 */
41 if (lstat(name, &st) != 0)
43 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
45 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
48 if (ace_count == -1) {
49 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
50 getcwdpath(), name, ace_count, strerror(errno));
54 aces = malloc(ace_count * sizeof(ace_t));
56 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
60 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
61 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
66 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
72 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
73 int remove_acl(const char *name)
75 int ret,i, ace_count, trivial_aces, new_aces_count;
76 ace_t *old_aces = NULL;
77 ace_t *new_aces = NULL;
79 LOG(log_debug9, logtype_afpd, "remove_acl: BEGIN");
81 /* Get existing ACL and count trivial ACEs */
82 if ((ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
85 for ( i=0; i < ace_count; i++) {
86 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))
90 /* malloc buffer for new ACL */
91 if ((new_aces = malloc(trivial_aces * sizeof(ace_t))) == NULL) {
92 LOG(log_error, logtype_afpd, "remove_acl: malloc %s", strerror(errno));
97 /* Now copy the trivial ACEs */
99 for (i=0; i < ace_count; i++) {
100 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
101 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
106 if ( (acl(name, ACE_SETACL, trivial_aces, new_aces)) == 0)
109 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
110 if (errno == (EACCES | EPERM))
112 else if (errno == ENOENT)
122 LOG(log_debug9, logtype_afpd, "remove_acl: END");