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 if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
213 if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
216 if (chmod(name, mode) != 0) /* (3) */
219 if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
221 if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
224 if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
227 if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
228 LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
233 if (oacl) free(oacl);
234 if (nacl) free(nacl);
235 if (cacl) free(cacl);
241 static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
243 int ret, i, nfsv4_ace_count, tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
244 ace_t *old_aces, *new_aces = NULL;
248 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
250 /* Get no of ACEs the client put on the wire */
251 ace_count = htonl(*((uint32_t *)ibuf));
252 ibuf += 8; /* skip ACL flags (see acls.h) */
255 /* inherited + trivial ACEs */
256 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
258 /* only trivial ACEs */
259 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
261 /* Get existing ACL and count ACEs which have to be copied */
262 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
264 for ( i=0; i < nfsv4_ace_count; i++) {
265 if (old_aces[i].a_flags & flags)
269 /* Now malloc buffer exactly sized to fit all new ACEs */
270 new_aces = malloc( (ace_count + tocopy_aces_count) * sizeof(ace_t) );
271 if (new_aces == NULL) {
272 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
277 /* Start building new ACL */
279 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
280 inherited ACEs first. */
282 for (i=0; i < nfsv4_ace_count; i++) {
283 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
284 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
289 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
291 /* Now the ACEs from the client */
292 ret = map_acl(DARWIN_2_SOLARIS, &new_aces[new_aces_count], (darwin_ace_t *)ibuf, ace_count);
297 new_aces_count += ace_count;
298 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
300 /* Now copy the trivial ACEs */
301 for (i=0; i < nfsv4_ace_count; i++) {
302 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
303 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
308 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
310 /* Ressourcefork first.
311 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
312 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
313 if ( (ret = vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
314 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
315 if (errno == (EACCES | EPERM))
317 else if (errno == ENOENT)
323 if ( (ret = acl(name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
324 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
325 if (errno == (EACCES | EPERM))
327 else if (errno == ENOENT)
340 LOG(log_debug9, logtype_afpd, "set_acl: END");
345 #endif /* HAVE_NFSv4_ACLS */