2 Copyright (c) 2010 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 */
19 #ifdef HAVE_NFSv4_ACLS
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
34 #include <atalk/acl.h>
36 /* Get ACL. Allocates storage as needed. Caller must free.
37 * Returns no of ACEs or -1 on error. */
38 int get_nfsv4_acl(const char *name, ace_t **retAces)
45 /* Only call acl() for regular files and directories, otherwise just return 0 */
46 if (lstat(name, &st) != 0)
48 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
51 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
54 if (ace_count == -1) {
55 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
56 getcwdpath(), name, ace_count, strerror(errno));
60 aces = malloc(ace_count * sizeof(ace_t));
62 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
66 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
67 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
72 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
81 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
86 /* malloc buffer for new ACL */
87 if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
88 LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
92 /* Copy ACEs from buf1 */
93 for (i=0; i < ace1count; ) {
94 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
100 /* Copy ACEs from buf2 */
101 for (i=0; i < ace2count; ) {
102 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
110 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
112 int strip_trivial_aces(ace_t **saces, int sacecount)
116 ace_t *aces = *saces;
119 if (aces == NULL || sacecount <= 0)
122 /* Count non-trivial ACEs */
123 for (i=0; i < sacecount; ) {
124 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
128 /* malloc buffer for new ACL */
129 if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
130 LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
134 /* Copy non-trivial ACEs */
135 for (i=0, j=0; i < sacecount; ) {
136 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
137 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
146 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
152 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
154 int strip_nontrivial_aces(ace_t **saces, int sacecount)
158 ace_t *aces = *saces;
161 /* Count trivial ACEs */
162 for (i=0; i < sacecount; ) {
163 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
167 /* malloc buffer for new ACL */
168 if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
169 LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
173 /* Copy trivial ACEs */
174 for (i=0, j=0; i < sacecount; ) {
175 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
176 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
185 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
191 * Change mode of file preserving existing explicit ACEs
194 * (1) reads objects ACL (acl1)
195 * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
196 * leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
197 * (3) calls chmod() with mode
198 * (4) reads the changed ACL (acl2) which
199 * a) might still contain explicit ACEs (up to onnv132)
200 * b) will have any explicit ACE removed (starting with onnv145/Openindiana)
201 * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
202 * (6) merge acl2 and acl2
203 * (7) set the ACL merged ACL on the object
205 int nfsv4_chmod(char *name, mode_t mode)
209 ace_t *oacl = NULL, *nacl = NULL, *cacl;
211 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
212 getcwdpath(), name, mode);
214 if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
216 if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
222 if (chmod(name, mode) != 0) /* (3) */
225 if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
227 if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
230 if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
233 if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
234 LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
239 if (oacl) free(oacl);
240 if (nacl) free(nacl);
241 if (cacl) free(cacl);
243 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
244 ret, getcwdpath(), name, mode);
250 #endif /* HAVE_NFSv4_ACLS */