]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/acls.c
Fix regression
[netatalk.git] / etc / afpd / acls.c
1 /*
2   Copyright (c) 2008, 2009, 2010 Frank Lahm <franklahm@gmail.com>
3   Copyright (c) 2011 Laura Mueller <laura-mueller@uni-duesseldorf.de>
4
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.
9
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.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <string.h>
21 #include <strings.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <grp.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #ifdef HAVE_SOLARIS_ACLS
28 #include <sys/acl.h>
29 #endif
30 #ifdef HAVE_POSIX_ACLS
31 #include <sys/acl.h>
32 #endif
33 #ifdef HAVE_ACL_LIBACL_H
34 #include <acl/libacl.h>
35 #endif
36
37 #include <atalk/errchk.h>
38 #include <atalk/adouble.h>
39 #include <atalk/vfs.h>
40 #include <atalk/afp.h>
41 #include <atalk/util.h>
42 #include <atalk/cnid.h>
43 #include <atalk/logger.h>
44 #include <atalk/uuid.h>
45 #include <atalk/acl.h>
46 #include <atalk/bstrlib.h>
47 #include <atalk/bstradd.h>
48
49 #include "directory.h"
50 #include "desktop.h"
51 #include "volume.h"
52 #include "fork.h"
53 #include "unix.h"
54 #include "acls.h"
55 #include "acl_mappings.h"
56 #include "auth.h"
57
58 /* for map_acl() */
59 #define SOLARIS_2_DARWIN       1
60 #define DARWIN_2_SOLARIS       2
61 #define POSIX_DEFAULT_2_DARWIN 3
62 #define POSIX_ACCESS_2_DARWIN  4
63 #define DARWIN_2_POSIX_DEFAULT 5
64 #define DARWIN_2_POSIX_ACCESS  6
65
66 #define MAP_MASK               31
67 #define IS_DIR                 32
68
69 /* bit flags for set_acl() and map_aces_darwin_to_posix() */
70 #define HAS_DEFAULT_ACL 0x01
71 #define HAS_EXT_DEFAULT_ACL 0x02
72
73 /********************************************************
74  * Solaris funcs
75  ********************************************************/
76
77 #ifdef HAVE_SOLARIS_ACLS
78
79 /*! 
80  * Compile access rights for a user to one file-system object
81  *
82  * This combines combines all access rights for a user to one fs-object and
83  * returns the result as a Darwin allowed rights ACE.
84  * This must honor trivial ACEs which are a mode_t mapping.
85  *
86  * @param path           (r) path to filesystem object
87  * @param sb             (r) struct stat of path
88  * @param result         (w) resulting Darwin allow ACE
89  *
90  * @returns                  0 or -1 on error
91  */
92 static int solaris_acl_rights(const char *path,
93                               const struct stat *sb,
94                               uint32_t *result)
95 {
96     EC_INIT;
97     int      i, ace_count, checkgroup;
98     ace_t    *aces = NULL;
99     uid_t    who;
100     uint16_t flags, type;
101     uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
102
103     /* Get ACL from file/dir */
104     EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
105
106     if (ace_count == 0)
107         goto EC_CLEANUP;
108
109     /* Now check requested rights */
110     i = 0;
111     do { /* Loop through ACEs */
112         who = aces[i].a_who;
113         flags = aces[i].a_flags;
114         type = aces[i].a_type;
115         rights = aces[i].a_access_mask;
116
117         if (flags & ACE_INHERIT_ONLY_ACE)
118             continue;
119
120         /* Now the tricky part: decide if ACE effects our user. I'll explain:
121            if its a dedicated (non trivial) ACE for the user
122            OR
123            if its a ACE for a group we're member of
124            OR
125            if its a trivial ACE_OWNER ACE and requested UUID is the owner
126            OR
127            if its a trivial ACE_GROUP ACE and requested UUID is group
128            OR
129            if its a trivial ACE_EVERYONE ACE
130            THEN
131            process ACE */
132         if (((who == uuid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
133             ||
134             ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who))
135             ||
136             ((flags & ACE_OWNER) && (uuid == sb->st_uid))
137             ||
138             ((flags & ACE_GROUP) && !(uuid == sb->st_uid) && gmem(sb->st_gid))
139             ||
140             (flags & ACE_EVERYONE && !(uuid == sb->st_uid) && !gmem(sb->st_gid))
141             ) {
142             /* Found an applicable ACE */
143             if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
144                 allowed_rights |= rights;
145             else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
146                 /* Only or to denied rights if not previously allowed !! */
147                 denied_rights |= ((!allowed_rights) & rights);
148         }
149     } while (++i < ace_count);
150
151
152     /* Darwin likes to ask for "delete_child" on dir,
153        "write_data" is actually the same, so we add that for dirs */
154     if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
155         allowed_rights |= ACE_DELETE_CHILD;
156
157     /* Remove denied from allowed rights */
158     allowed_rights &= ~denied_rights;
159
160     /* map rights */
161     darwin_rights = 0;
162     for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
163         if (allowed_rights & nfsv4_to_darwin_rights[i].from)
164             darwin_rights |= nfsv4_to_darwin_rights[i].to;
165     }
166
167     *result |= darwin_rights;
168
169 EC_CLEANUP:
170     if (aces) free(aces);
171
172     EC_EXIT;
173 }
174
175 /*
176   Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
177   Return numer of mapped ACEs or -1 on error.
178   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
179 */
180 static int map_aces_solaris_to_darwin(const ace_t *aces,
181                                       darwin_ace_t *darwin_aces,
182                                       int ace_count)
183 {
184     EC_INIT;
185     int i, count = 0;
186     uint32_t flags;
187     uint32_t rights;
188     struct passwd *pwd = NULL;
189     struct group *grp = NULL;
190
191     LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
192
193     while(ace_count--) {
194         LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
195         /* if its a ACE resulting from nfsv4 mode mapping, discard it */
196         if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
197             LOG(log_debug, logtype_afpd, "trivial ACE");
198             aces++;
199             continue;
200         }
201
202         if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
203             LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
204             EC_NULL_LOG(pwd = getpwuid(aces->a_who));
205             LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
206             EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
207                                         UUID_USER,
208                                         darwin_aces->darwin_ace_uuid));
209         } else { /* group ace */
210             LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
211             EC_NULL_LOG(grp = getgrgid(aces->a_who));
212             LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
213             EC_ZERO_LOG(getuuidfromname(grp->gr_name,
214                                         UUID_GROUP,
215                                         darwin_aces->darwin_ace_uuid));
216         }
217
218         /* map flags */
219         if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
220             flags = DARWIN_ACE_FLAGS_PERMIT;
221         else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
222             flags = DARWIN_ACE_FLAGS_DENY;
223         else {          /* unsupported type */
224             aces++;
225             continue;
226         }
227         for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
228             if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
229                 flags |= nfsv4_to_darwin_flags[i].to;
230         }
231         darwin_aces->darwin_ace_flags = htonl(flags);
232
233         /* map rights */
234         rights = 0;
235         for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
236             if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
237                 rights |= nfsv4_to_darwin_rights[i].to;
238         }
239         darwin_aces->darwin_ace_rights = htonl(rights);
240
241         count++;
242         aces++;
243         darwin_aces++;
244     }
245
246     return count;
247 EC_CLEANUP:
248     EC_EXIT;
249 }
250
251 /*
252   Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
253   Return numer of mapped ACEs or -1 on error.
254   All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
255 */
256 static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
257                                       ace_t *nfsv4_aces,
258                                       int ace_count)
259 {
260     EC_INIT;
261     int i, mapped_aces = 0;
262     uint32_t darwin_ace_flags;
263     uint32_t darwin_ace_rights;
264     uint16_t nfsv4_ace_flags;
265     uint32_t nfsv4_ace_rights;
266     char *name = NULL;
267     uuidtype_t uuidtype;
268     struct passwd *pwd;
269     struct group *grp;
270
271     while(ace_count--) {
272         nfsv4_ace_flags = 0;
273         nfsv4_ace_rights = 0;
274
275         /* uid/gid first */
276         EC_ZERO(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
277         switch (uuidtype) {
278         case UUID_USER:
279             EC_NULL_LOG(pwd = getpwnam(name));
280             nfsv4_aces->a_who = pwd->pw_uid;
281             break;
282         case UUID_GROUP:
283             EC_NULL_LOG(grp = getgrnam(name));
284             nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
285             nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
286             break;
287         default:
288             LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: unkown uuidtype");
289             EC_FAIL;
290         }
291         free(name);
292         name = NULL;
293
294         /* now type: allow/deny */
295         darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
296         if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
297             nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
298         else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
299             nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
300         else { /* unsupported type */
301             darwin_aces++;
302             continue;
303         }
304         /* map flags */
305         for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
306             if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
307                 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
308         }
309
310         /* map rights */
311         darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
312         for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
313             if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
314                 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
315         }
316
317         LOG(log_debug9, logtype_afpd,
318             "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x",
319             darwin_ace_flags, nfsv4_ace_flags);
320         LOG(log_debug9, logtype_afpd,
321             "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x",
322             darwin_ace_rights, nfsv4_ace_rights);
323
324         nfsv4_aces->a_flags = nfsv4_ace_flags;
325         nfsv4_aces->a_access_mask = nfsv4_ace_rights;
326
327         mapped_aces++;
328         darwin_aces++;
329         nfsv4_aces++;
330     }
331
332     return mapped_aces;
333 EC_CLEANUP:
334     if (name)
335         free(name);
336     EC_EXIT;
337 }
338 #endif /* HAVE_SOLARIS_ACLS */
339
340 /********************************************************
341  * POSIX 1e funcs
342  ********************************************************/
343
344 #ifdef HAVE_POSIX_ACLS
345
346 static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
347 {
348     EC_INIT;
349     uint32_t rights = 0;
350     acl_permset_t permset;
351
352     EC_ZERO_LOG(acl_get_permset(e, &permset));
353
354 #ifdef HAVE_ACL_GET_PERM_NP
355     if (acl_get_perm_np(permset, ACL_READ))
356 #else
357     if (acl_get_perm(permset, ACL_READ))
358 #endif
359         rights = DARWIN_ACE_READ_DATA
360             | DARWIN_ACE_READ_EXTATTRIBUTES
361             | DARWIN_ACE_READ_ATTRIBUTES
362             | DARWIN_ACE_READ_SECURITY;
363 #ifdef HAVE_ACL_GET_PERM_NP
364     if (acl_get_perm_np(permset, ACL_WRITE)) {
365 #else
366     if (acl_get_perm(permset, ACL_WRITE)) {
367 #endif
368         rights |= DARWIN_ACE_WRITE_DATA
369             | DARWIN_ACE_APPEND_DATA
370             | DARWIN_ACE_WRITE_EXTATTRIBUTES
371             | DARWIN_ACE_WRITE_ATTRIBUTES;
372         if (is_dir)
373             rights |= DARWIN_ACE_DELETE_CHILD;
374     }
375 #ifdef HAVE_ACL_GET_PERM_NP
376     if (acl_get_perm_np(permset, ACL_EXECUTE))
377 #else
378     if (acl_get_perm(permset, ACL_EXECUTE))
379 #endif
380         rights |= DARWIN_ACE_EXECUTE;
381
382 EC_CLEANUP:
383     LOG(log_maxdebug, logtype_afpd, "mapped rights: 0x%08x", rights);
384     return rights;
385 }
386
387 /*! 
388  * Compile access rights for a user to one file-system object
389  *
390  * This combines combines all access rights for a user to one fs-object and
391  * returns the result as a Darwin allowed rights ACE.
392  * This must honor trivial ACEs which are a mode_t mapping.
393  *
394  * @param path           (r) path to filesystem object
395  * @param sb             (r) struct stat of path
396  * @param result         (rw) resulting Darwin allow ACE
397  *
398  * @returns                  0 or -1 on error
399  */
400 static int posix_acl_rights(const char *path,
401                             const struct stat *sb,
402                             uint32_t *result)
403 {
404     EC_INIT;
405     int entry_id = ACL_FIRST_ENTRY;
406     uint32_t rights = 0; /* rights which do not depend on ACL_MASK */
407     uint32_t acl_rights = 0; /* rights which are subject to limitations imposed by ACL_MASK */
408     uint32_t mask_rights = 0xffffffff;
409     uid_t *uid = NULL;
410     gid_t *gid = NULL;
411     acl_t acl = NULL;
412     acl_entry_t e;
413     acl_tag_t tag;
414
415     EC_NULL_LOGSTR(acl = acl_get_file(path, ACL_TYPE_ACCESS),
416                    "acl_get_file(\"%s\"): %s", fullpathname(path), strerror(errno));
417
418     /* Iterate through all ACEs. If we apply mask_rights later there is no need to iterate twice. */
419     while (acl_get_entry(acl, entry_id, &e) == 1) {
420         entry_id = ACL_NEXT_ENTRY;
421         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
422
423         switch (tag) {
424             case ACL_USER_OBJ:
425                 if (sb->st_uid == uuid) {
426                     LOG(log_maxdebug, logtype_afpd, "ACL_USER_OBJ: %u", sb->st_uid);
427                     rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
428                 }
429                 break;
430
431             case ACL_USER:
432                 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
433
434                 if (*uid == uuid) {
435                     LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
436                     acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
437                 }
438                 acl_free(uid);
439                 uid = NULL;
440                 break;
441
442             case ACL_GROUP_OBJ:
443                 if (!(sb->st_uid == uuid) && gmem(sb->st_gid)) {
444                     LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
445                     acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
446                 }
447                 break;
448
449             case ACL_GROUP:
450                 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
451
452                 if (gmem(*gid)) {
453                     LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
454                     acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
455                 }
456                 acl_free(gid);
457                 gid = NULL;
458                 break;
459
460             case ACL_MASK:
461                 mask_rights = posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
462                 LOG(log_maxdebug, logtype_afpd, "maskrights: 0x%08x", mask_rights);
463                 break;
464
465             case ACL_OTHER:
466                 if (!(sb->st_uid == uuid) && !gmem(sb->st_gid)) {
467                     LOG(log_maxdebug, logtype_afpd, "ACL_OTHER");
468                     rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
469                 }
470                 break;
471
472             default:
473                 continue;
474         }
475     } /* while */
476
477     /* apply the mask and collect the rights */
478     rights |= (acl_rights & mask_rights);
479
480     *result |= rights;
481
482 EC_CLEANUP:
483     if (acl) acl_free(acl);
484     if (uid) acl_free(uid);
485     if (gid) acl_free(gid);
486     EC_EXIT;
487 }
488
489 /*!
490  * Helper function for posix_acls_to_uaperms() to convert Posix ACL permissions
491  * into access rights needed to fill ua_permissions of a FPUnixPrivs structure.
492  *
493  * @param entry     (r) Posix ACL entry
494  *
495  * @returns         access rights
496  */
497 static u_char acl_permset_to_uarights(acl_entry_t entry) {
498     acl_permset_t permset;
499     u_char rights = 0;
500
501     if (acl_get_permset(entry, &permset) == -1)
502         return rights;
503
504 #ifdef HAVE_ACL_GET_PERM_NP
505     if (acl_get_perm_np(permset, ACL_READ))
506 #else
507     if (acl_get_perm(permset, ACL_READ))
508 #endif
509         rights |= AR_UREAD;
510
511 #ifdef HAVE_ACL_GET_PERM_NP
512     if (acl_get_perm_np(permset, ACL_WRITE))
513 #else
514     if (acl_get_perm(permset, ACL_WRITE))
515 #endif
516         rights |= AR_UWRITE;
517
518 #ifdef HAVE_ACL_GET_PERM_NP
519     if (acl_get_perm_np(permset, ACL_EXECUTE))
520 #else
521     if (acl_get_perm(permset, ACL_EXECUTE))
522 #endif
523         rights |= AR_USEARCH;
524
525     return rights;
526 }
527
528 /*!
529  * Update FPUnixPrivs for a file-system object on a volume supporting ACLs
530  *
531  * Checks permissions granted by ACLS for a user to one fs-object and
532  * updates user and group permissions in given struct maccess. As OS X
533  * doesn't conform to Posix 1003.1e Draft 17 it expects proper group
534  * permissions in st_mode of struct stat even if the fs-object has an
535  * ACL_MASK entry, st_mode gets modified to properly reflect group
536  * permissions.
537  *
538  * @param path           (r) path to filesystem object
539  * @param sb             (rw) struct stat of path
540  * @param maccess        (rw) struct maccess of path
541  *
542  * @returns                  0 or -1 on error
543  */
544 static int posix_acls_to_uaperms(const char *path, struct stat *sb, struct maccess *ma) {
545     EC_INIT;
546
547     int entry_id = ACL_FIRST_ENTRY;
548     acl_entry_t entry;
549     acl_tag_t tag;
550     acl_t acl = NULL;
551     uid_t *uid;
552     gid_t *gid;
553
554     u_char group_rights = 0x00;
555     u_char acl_rights = 0x00;
556     u_char mask = 0xff;
557
558     EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
559
560     /* iterate through all ACEs */
561     while (acl_get_entry(acl, entry_id, &entry) == 1) {
562         entry_id = ACL_NEXT_ENTRY;
563         EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
564
565         switch (tag) {
566             case ACL_USER:
567                 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(entry));
568
569                 if (*uid == uuid) {
570                     LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
571                     acl_rights |= acl_permset_to_uarights(entry);
572                 }
573                 acl_free(uid);
574                 break;
575
576             case ACL_GROUP_OBJ:
577                 group_rights = acl_permset_to_uarights(entry);
578                 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
579
580                 if (gmem(sb->st_gid))
581                     acl_rights |= group_rights;
582                 break;
583
584             case ACL_GROUP:
585                 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(entry));
586
587                 if (gmem(*gid)) {
588                     LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
589                     acl_rights |= acl_permset_to_uarights(entry);
590                 }
591                 acl_free(gid);
592                 break;
593
594             case ACL_MASK:
595                 mask = acl_permset_to_uarights(entry);
596                 LOG(log_maxdebug, logtype_afpd, "ACL_MASK: 0x%02x", mask);
597                 break;
598
599             default:
600                 break;
601         }
602     }
603     /* apply the mask and adjust user and group permissions */
604     ma->ma_user |= (acl_rights & mask);
605     ma->ma_group = (group_rights & mask);
606
607     /* update st_mode to properly reflect group permissions */
608     sb->st_mode &= ~S_IRWXG;
609
610     if (ma->ma_group & AR_USEARCH)
611         sb->st_mode |= S_IXGRP;
612
613     if (ma->ma_group & AR_UWRITE)
614         sb->st_mode |= S_IWGRP;
615
616     if (ma->ma_group & AR_UREAD)
617         sb->st_mode |= S_IRGRP;
618
619 EC_CLEANUP:
620     if (acl) acl_free(acl);
621
622     EC_EXIT;
623 }
624
625 /*!
626  * Add entries of one acl to another acl
627  *
628  * @param aclp   (rw) destination acl where new aces will be added
629  * @param acl    (r)  source acl where aces will be copied from
630  *
631  * @returns 0 on success, -1 on error
632  */
633 static int acl_add_acl(acl_t *aclp, const acl_t acl)
634 {
635     EC_INIT;
636     int id;
637     acl_entry_t se, de;
638
639     for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
640         EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
641         EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
642     }
643
644 EC_CLEANUP:
645     EC_EXIT;
646 }
647
648 /*!
649  * Map Darwin ACE rights to POSIX 1e perm
650  *
651  * We can only map few rights:
652  *   DARWIN_ACE_READ_DATA                    -> ACL_READ
653  *   DARWIN_ACE_WRITE_DATA                   -> ACL_WRITE
654  *   DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
655  *   DARWIN_ACE_EXECUTE                      -> ACL_EXECUTE
656  *
657  * @param entry             (rw) result of the mapping
658  * @param is_dir            (r) 1 for dirs, 0 for files
659  *
660  * @returns mapping result as acl_perm_t, -1 on error
661  */
662 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
663 {
664     acl_perm_t perm = 0;
665
666     if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
667         perm |= ACL_READ;
668
669     if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (is_dir ? DARWIN_ACE_DELETE_CHILD : 0)))
670         perm |= ACL_WRITE;
671
672     if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
673         perm |= ACL_EXECUTE;
674
675     return perm;
676 }
677
678 /*!
679  * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
680  *
681  * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
682  * for existing permissions for this type/id, if there is one add the perm,
683  * otherwise create a new ACL entry.
684  * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.  
685  *
686  * @param aclp     (rw) pointer to ACL
687  * @param type     (r)  acl_tag_t of ACL_USER or ACL_GROUP
688  * @param id       (r)  uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
689  * @param perm     (r)  acl_perm_t permissions to add
690  *
691  * @returns 0 on success, -1 on failure
692  */
693 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
694 {
695     EC_INIT;
696     uid_t *eid = NULL;
697     acl_entry_t e;
698     acl_tag_t tag;
699     int entry_id = ACL_FIRST_ENTRY;
700     acl_permset_t permset;
701
702     int found = 0;
703     for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
704         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
705         if (tag != ACL_USER && tag != ACL_GROUP)
706             continue;
707         EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
708         if ((*eid == id) && (type == tag)) {
709             /* found an ACE for this type/id */
710             found = 1;
711             EC_ZERO_LOG(acl_get_permset(e, &permset));
712             EC_ZERO_LOG(acl_add_perm(permset, perm));
713         }
714
715         acl_free(eid);
716         eid = NULL;
717     }
718
719     if ( ! found) {
720         /* there was no existing ACE for this type/id */
721         EC_ZERO_LOG(acl_create_entry(aclp, &e));
722         EC_ZERO_LOG(acl_set_tag_type(e, type));
723         EC_ZERO_LOG(acl_set_qualifier(e, &id));
724         EC_ZERO_LOG(acl_get_permset(e, &permset));
725         EC_ZERO_LOG(acl_clear_perms(permset));
726         EC_ZERO_LOG(acl_add_perm(permset, perm));
727         EC_ZERO_LOG(acl_set_permset(e, permset));
728     }
729
730 EC_CLEANUP:
731     if (eid) acl_free(eid);
732
733     EC_EXIT;
734 }
735
736 /*!
737  * Map Darwin ACL to POSIX ACL.
738  *
739  * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
740  * Mapping pecularities:
741  * - we create a default ace (which inherits to files and dirs) if either
742      DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
743  * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
744  *   not be limited
745  *
746  * @param darwin_aces        (r)  pointer to darwin_aces buffer
747  * @param def_aclp           (rw) directories: pointer to an initialized acl_t with
748                                   the default acl files: *def_aclp will be NULL
749  * @param acc_aclp           (rw) pointer to an initialized acl_t with the access acl
750  * @param ace_count          (r)  number of ACEs in darwin_aces buffer
751  * @param default_acl_flags  (rw) flags to indicate if the object has a basic default
752  *                                acl or an extended default acl.
753  *
754  * @returns 0 on success storing the result in aclp, -1 on error. default_acl_flags
755  * is set to HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL in case there is at least one
756  * extended default ace. Otherwise default_acl_flags is left unchanged.
757  */
758 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
759                                     acl_t *def_aclp,
760                                     acl_t *acc_aclp,
761                                     int ace_count,
762                                     uint32_t *default_acl_flags)
763 {
764     EC_INIT;
765     char *name = NULL;
766     uuidtype_t uuidtype;
767     struct passwd *pwd;
768     struct group *grp;
769     uid_t id;
770     uint32_t darwin_ace_flags, darwin_ace_rights;
771     acl_tag_t tag;
772     acl_perm_t perm;
773
774     for ( ; ace_count != 0; ace_count--, darwin_aces++) {
775         /* type: allow/deny, posix only has allow */
776         darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
777         if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
778             continue;
779
780         darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
781         perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
782         if (perm == 0)
783             continue;       /* dont add empty perm */
784
785         LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
786             ace_count, darwin_ace_flags, darwin_ace_rights, perm);
787
788          /* uid/gid */
789         EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
790         switch (uuidtype) {
791         case UUID_USER:
792             EC_NULL_LOG(pwd = getpwnam(name));
793             tag = ACL_USER;
794             id = pwd->pw_uid;
795             LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
796             break;
797         case UUID_GROUP:
798             EC_NULL_LOG(grp = getgrnam(name));
799             tag = ACL_GROUP;
800             id = (uid_t)(grp->gr_gid);
801             LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
802             break;
803         default:
804             continue;
805         }
806         free(name);
807         name = NULL;
808
809         if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
810             if (*def_aclp == NULL) {
811                 /* ace request inheritane but we haven't got a default acl pointer */
812                 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
813                     darwin_ace_flags);
814                 EC_FAIL;
815             }
816             /* add it as default ace */
817             EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
818             *default_acl_flags = (HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL);
819
820             if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
821                 /* if it not a "inherit only" ace, it must be added as access aces too */
822                 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
823         } else {
824             EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
825         }
826     }
827
828 EC_CLEANUP:
829     if (name)
830         free(name);
831
832     EC_EXIT;
833 }
834
835 /*
836  * Map ACEs from POSIX to Darwin.
837  * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
838  * Return number of mapped ACES, -1 on error.
839  */
840 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
841 {
842     EC_INIT;
843     int mapped_aces = 0;
844     int entry_id = ACL_FIRST_ENTRY;
845     acl_entry_t e;
846     acl_tag_t tag;
847     uid_t *uid = NULL;
848     gid_t *gid = NULL;
849     struct passwd *pwd = NULL;
850     struct group *grp = NULL;
851     uint32_t flags;
852     uint32_t rights, maskrights = 0;
853     darwin_ace_t *saved_darwin_aces = darwin_aces;
854
855     LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
856         (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
857         "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
858
859     /* itereate through all ACEs */
860     while (acl_get_entry(acl, entry_id, &e) == 1) {
861         entry_id = ACL_NEXT_ENTRY;
862
863         /* get ACE type */
864         EC_ZERO_LOG(acl_get_tag_type(e, &tag));
865
866         /* we return user and group ACE */
867         switch (tag) {
868         case ACL_USER:
869             EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
870             EC_NULL_LOG(pwd = getpwuid(*uid));
871             LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
872                 *uid, pwd->pw_name);
873             EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
874             acl_free(uid);
875             uid = NULL;
876             break;
877
878         case ACL_GROUP:
879             EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
880             EC_NULL_LOG(grp = getgrgid(*gid));
881             LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
882                 *gid, grp->gr_name);
883             EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
884             acl_free(gid);
885             gid = NULL;
886             break;
887
888         case ACL_MASK:
889             maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
890             continue;
891
892         default:
893             continue;
894         }
895
896         /* flags */
897         flags = DARWIN_ACE_FLAGS_PERMIT;
898         if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
899             flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
900                 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
901                 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
902         darwin_aces->darwin_ace_flags = htonl(flags);
903
904         /* rights */
905         rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
906         darwin_aces->darwin_ace_rights = htonl(rights);
907
908         darwin_aces++;
909         mapped_aces++;
910     } /* while */
911
912     /* Loop through the mapped ACE buffer once again, applying the mask */
913     for (int i = mapped_aces; i > 0; i--) {
914         saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
915         saved_darwin_aces++;
916     }
917
918     EC_STATUS(mapped_aces);
919
920 EC_CLEANUP:
921     if (uid) acl_free(uid);
922     if (gid) acl_free(gid);
923     EC_EXIT;
924 }
925 #endif
926
927 /*
928  * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
929  * Reads from 'aces' buffer, writes to 'rbuf' buffer.
930  * Caller must provide buffer.
931  * Darwin ACEs are read and written in network byte order.
932  * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
933  * Ignores trivial ACEs.
934  * Return no of mapped ACEs or -1 on error.
935  */
936 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
937 {
938     int mapped_aces;
939
940     LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
941
942     switch (type & MAP_MASK) {
943
944 #ifdef HAVE_SOLARIS_ACLS
945     case SOLARIS_2_DARWIN:
946         mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
947         break;
948
949     case DARWIN_2_SOLARIS:
950         mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
951         break;
952 #endif /* HAVE_SOLARIS_ACLS */
953
954 #ifdef HAVE_POSIX_ACLS
955     case POSIX_DEFAULT_2_DARWIN:
956         mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
957         break;
958
959     case POSIX_ACCESS_2_DARWIN:
960         mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
961         break;
962
963     case DARWIN_2_POSIX_DEFAULT:
964         break;
965
966     case DARWIN_2_POSIX_ACCESS:
967         break;
968 #endif /* HAVE_POSIX_ACLS */
969
970     default:
971         mapped_aces = -1;
972         break;
973     }
974
975     LOG(log_debug9, logtype_afpd, "map_acl: END");
976     return mapped_aces;
977 }
978
979 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
980    store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
981    Returns 0 on success, -1 on error. */
982 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
983 {
984     EC_INIT;
985     int mapped_aces = 0;
986     int dirflag;
987     uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
988 #ifdef HAVE_SOLARIS_ACLS
989     int ace_count = 0;
990     ace_t *aces = NULL;
991 #endif
992 #ifdef HAVE_POSIX_ACLS
993     struct stat st;
994 #endif
995     LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
996
997     /* Skip length and flags */
998     rbuf += 4;
999     *rbuf = 0;
1000     rbuf += 4;
1001
1002 #ifdef HAVE_SOLARIS_ACLS
1003     EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
1004     EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
1005 #endif /* HAVE_SOLARIS_ACLS */
1006
1007 #ifdef HAVE_POSIX_ACLS
1008     acl_t defacl = NULL , accacl = NULL;
1009
1010     /* stat to check if its a dir */
1011     EC_ZERO_LOG(lstat(name, &st));
1012
1013     /* if its a dir, check for default acl too */
1014     dirflag = 0;
1015     if (S_ISDIR(st.st_mode)) {
1016         dirflag = IS_DIR;
1017         EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
1018         EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
1019                                       defacl,
1020                                       (darwin_ace_t *)rbuf,
1021                                       0));
1022     }
1023
1024     EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
1025
1026     int tmp;
1027     EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
1028                           accacl,
1029                           (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
1030                           0));
1031     mapped_aces += tmp;
1032 #endif /* HAVE_POSIX_ACLS */
1033
1034     LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
1035
1036     *darwin_ace_count = htonl(mapped_aces);
1037     *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
1038
1039     EC_STATUS(0);
1040
1041 EC_CLEANUP:
1042 #ifdef HAVE_SOLARIS_ACLS
1043     if (aces) free(aces);
1044 #endif
1045 #ifdef HAVE_POSIX_ACLS
1046     if (defacl) acl_free(defacl);
1047     if (accacl) acl_free(accacl);
1048 #endif /* HAVE_POSIX_ACLS */
1049
1050     LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
1051
1052     EC_EXIT;
1053 }
1054
1055 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
1056 static int remove_acl(const struct vol *vol,const char *path, int dir)
1057 {
1058     int ret = AFP_OK;
1059
1060 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
1061     /* Ressource etc. first */
1062     if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
1063         return ret;
1064     /* now the data fork or dir */
1065     ret = remove_acl_vfs(path);
1066 #endif
1067     return ret;
1068 }
1069
1070 /*
1071   Set ACL. Subtleties:
1072   - the client sends a complete list of ACEs, not only new ones. So we dont need to do
1073   any combination business (one exception being 'kFileSec_Inherit': see next)
1074   - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
1075   We will store inherited ACEs first, which is Darwins canonical order.
1076   - returns AFPerror code
1077 */
1078 #ifdef HAVE_SOLARIS_ACLS
1079 static int set_acl(const struct vol *vol,
1080                    char *name,
1081                    int inherit,
1082                    darwin_ace_t *daces,
1083                    uint32_t ace_count)
1084 {
1085     EC_INIT;
1086     int i, nfsv4_ace_count;
1087     int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
1088     ace_t *old_aces, *new_aces = NULL;
1089     uint16_t flags;
1090
1091     LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
1092
1093     if (inherit)
1094         /* inherited + trivial ACEs */
1095         flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1096     else
1097         /* only trivial ACEs */
1098         flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1099
1100     /* Get existing ACL and count ACEs which have to be copied */
1101     if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
1102         return AFPERR_MISC;
1103     for ( i=0; i < nfsv4_ace_count; i++) {
1104         if (old_aces[i].a_flags & flags)
1105             tocopy_aces_count++;
1106     }
1107
1108     /* Now malloc buffer exactly sized to fit all new ACEs */
1109     if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
1110         LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
1111         EC_STATUS(AFPERR_MISC);
1112         goto EC_CLEANUP;
1113     }
1114
1115     /* Start building new ACL */
1116
1117     /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
1118        inherited ACEs first. */
1119     if (inherit) {
1120         for (i=0; i < nfsv4_ace_count; i++) {
1121             if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
1122                 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1123                 new_aces_count++;
1124             }
1125         }
1126     }
1127     LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
1128
1129     /* Now the ACEs from the client */
1130     if ((ret = (map_acl(DARWIN_2_SOLARIS,
1131                         &new_aces[new_aces_count],
1132                         daces,
1133                         ace_count))) == -1) {
1134         EC_STATUS(AFPERR_PARAM);
1135         goto EC_CLEANUP;
1136     }
1137     new_aces_count += ret;
1138     LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
1139
1140     /* Now copy the trivial ACEs */
1141     for (i=0; i < nfsv4_ace_count; i++) {
1142         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
1143             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1144             new_aces_count++;
1145             trivial_ace_count++;
1146         }
1147     }
1148     LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
1149
1150     /* Ressourcefork first.
1151        Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
1152        might be strange for ACE_DELETE_CHILD and for inheritance flags. */
1153     if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1154         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1155         if (errno == (EACCES | EPERM))
1156             EC_STATUS(AFPERR_ACCESS);
1157         else if (errno == ENOENT)
1158             EC_STATUS(AFPERR_NOITEM);
1159         else
1160             EC_STATUS(AFPERR_MISC);
1161         goto EC_CLEANUP;
1162     }
1163     if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1164         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1165         if (errno == (EACCES | EPERM))
1166             EC_STATUS(AFPERR_ACCESS);
1167         else if (errno == ENOENT)
1168             EC_STATUS(AFPERR_NOITEM);
1169         else
1170             EC_STATUS(AFPERR_MISC);
1171         goto EC_CLEANUP;
1172     }
1173
1174     EC_STATUS(AFP_OK);
1175
1176 EC_CLEANUP:
1177     if (old_aces) free(old_aces);
1178     if (new_aces) free(new_aces);
1179
1180     LOG(log_debug9, logtype_afpd, "set_acl: END");
1181     EC_EXIT;
1182 }
1183 #endif /* HAVE_SOLARIS_ACLS */
1184
1185 #ifdef HAVE_POSIX_ACLS
1186 #ifndef HAVE_ACL_FROM_MODE
1187 static acl_t acl_from_mode(mode_t mode)
1188 {
1189     acl_t acl;
1190     acl_entry_t entry;
1191     acl_permset_t permset;
1192
1193     if (!(acl = acl_init(3)))
1194         return NULL;
1195
1196     if (acl_create_entry(&acl, &entry) != 0)
1197         goto error;
1198     acl_set_tag_type(entry, ACL_USER_OBJ);
1199     acl_get_permset(entry, &permset);
1200     acl_clear_perms(permset);
1201     if (mode & S_IRUSR)
1202         acl_add_perm(permset, ACL_READ);
1203     if (mode & S_IWUSR)
1204         acl_add_perm(permset, ACL_WRITE);
1205     if (mode & S_IXUSR)
1206         acl_add_perm(permset, ACL_EXECUTE);
1207     acl_set_permset(entry, permset);
1208
1209     if (acl_create_entry(&acl, &entry) != 0)
1210         goto error;
1211     acl_set_tag_type(entry, ACL_GROUP_OBJ);
1212     acl_get_permset(entry, &permset);
1213     acl_clear_perms(permset);
1214     if (mode & S_IRGRP)
1215         acl_add_perm(permset, ACL_READ);
1216     if (mode & S_IWGRP)
1217         acl_add_perm(permset, ACL_WRITE);
1218     if (mode & S_IXGRP)
1219         acl_add_perm(permset, ACL_EXECUTE);
1220     acl_set_permset(entry, permset);
1221
1222     if (acl_create_entry(&acl, &entry) != 0)
1223         goto error;
1224     acl_set_tag_type(entry, ACL_OTHER);
1225     acl_get_permset(entry, &permset);
1226     acl_clear_perms(permset);
1227     if (mode & S_IROTH)
1228         acl_add_perm(permset, ACL_READ);
1229     if (mode & S_IWOTH)
1230         acl_add_perm(permset, ACL_WRITE);
1231     if (mode & S_IXOTH)
1232         acl_add_perm(permset, ACL_EXECUTE);
1233     acl_set_permset(entry, permset);
1234
1235     return acl;
1236
1237 error:
1238     acl_free(acl);
1239     return NULL;
1240 }
1241 #endif
1242
1243 static int set_acl(const struct vol *vol,
1244                    const char *name,
1245                    int inherit _U_,
1246                    darwin_ace_t *daces,
1247                    uint32_t ace_count)
1248 {
1249     EC_INIT;
1250     struct stat st;
1251     acl_t default_acl = NULL;
1252     acl_t access_acl = NULL;
1253     acl_entry_t entry;
1254     acl_tag_t tag;
1255     int entry_id = ACL_FIRST_ENTRY;
1256     int has_def_acl = 0;
1257     /* flags to indicate if the object has a minimal default acl and/or an extended
1258      * default acl.
1259      */
1260     uint32_t default_acl_flags = 0;
1261
1262     LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1263
1264     EC_NULL_LOG_ERR(access_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1265
1266     /* Iterate through acl and remove all extended acl entries. */
1267     while (acl_get_entry(access_acl, entry_id, &entry) == 1) {
1268         entry_id = ACL_NEXT_ENTRY;
1269         EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
1270
1271         if ((tag == ACL_USER) || (tag == ACL_GROUP) || (tag == ACL_MASK)) {
1272             EC_ZERO_LOG_ERR(acl_delete_entry(access_acl, entry), AFPERR_MISC);
1273         }
1274     } /* while */
1275
1276    /* In case we are acting on a directory prepare a default acl. For files default_acl will be NULL.
1277     * If the directory already has a default acl it will be preserved.
1278     */
1279    EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1280
1281    if (S_ISDIR(st.st_mode)) {
1282        default_acl = acl_get_file(name, ACL_TYPE_DEFAULT);
1283
1284        if (default_acl) {
1285            /* If default_acl is not empty then the dir has a default acl. */
1286            if (acl_get_entry(default_acl, ACL_FIRST_ENTRY, &entry) == 1)
1287                default_acl_flags = HAS_DEFAULT_ACL;
1288
1289            acl_free(default_acl);
1290        }
1291        default_acl = acl_dup(access_acl);
1292     }
1293     /* adds the clients aces */
1294     EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &default_acl, &access_acl, ace_count, &default_acl_flags), AFPERR_MISC);
1295
1296     /* calcuate ACL mask */
1297     EC_ZERO_LOG_ERR(acl_calc_mask(&access_acl), AFPERR_MISC);
1298
1299     /* is it ok? */
1300     EC_ZERO_LOG_ERR(acl_valid(access_acl), AFPERR_MISC);
1301
1302     /* set it */
1303     EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, access_acl), AFPERR_MISC);
1304     EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, access_acl), AFPERR_MISC);
1305
1306     if (default_acl) {
1307         /* If the dir has an extended default acl it's ACL_MASK must be updated.*/
1308         if (default_acl_flags & HAS_EXT_DEFAULT_ACL)
1309             EC_ZERO_LOG_ERR(acl_calc_mask(&default_acl), AFPERR_MISC);
1310
1311         if (default_acl_flags) {
1312             EC_ZERO_LOG_ERR(acl_valid(default_acl), AFPERR_MISC);
1313             EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, default_acl), AFPERR_MISC);
1314             EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, default_acl), AFPERR_MISC);
1315         }
1316     }
1317
1318 EC_CLEANUP:
1319     if (access_acl) acl_free(access_acl);
1320     if (default_acl) acl_free(default_acl);
1321
1322     LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1323     EC_EXIT;
1324 }
1325 #endif /* HAVE_POSIX_ACLS */
1326
1327 /*!
1328  * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1329  *
1330  * Note: this gets called frequently and is a good place for optimizations !
1331  *
1332  * @param vol              (r) volume
1333  * @param dir              (rw) directory
1334  * @param path             (r) path to filesystem object
1335  * @param uuid             (r) UUID of user
1336  * @param requested_rights (r) requested Darwin ACE
1337  *
1338  * @returns                    AFP result code
1339 */
1340 static int check_acl_access(const struct vol *vol,
1341                             struct dir *dir,
1342                             const char *path,
1343                             const uuidp_t uuid,
1344                             uint32_t requested_rights)
1345 {
1346     int            ret;
1347     uint32_t       allowed_rights = 0;
1348     char           *username = NULL;
1349     uuidtype_t     uuidtype;
1350     struct stat    st;
1351     bstring        parent = NULL;
1352     int            is_dir;
1353
1354     LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1355         cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1356
1357     /* This check is not used anymore, as OS X Server seems to be ignoring too */
1358 #if 0
1359     /* Get uid or gid from UUID */
1360     EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1361     switch (uuidtype) {
1362     case UUID_USER:
1363         break;
1364     case UUID_GROUP:
1365         LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1366         EC_STATUS(AFPERR_MISC);
1367         goto EC_CLEANUP;
1368     default:
1369         EC_STATUS(AFPERR_MISC);
1370         goto EC_CLEANUP;
1371     }
1372 #endif
1373
1374     EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
1375
1376     is_dir = !strcmp(path, ".");
1377
1378     if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1379         /* its a dir and the cache value is valid */
1380         allowed_rights = curdir->d_rights_cache;
1381         LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1382     } else {
1383 #ifdef HAVE_SOLARIS_ACLS
1384         EC_ZERO_LOG(solaris_acl_rights(path, &st, &allowed_rights));
1385 #endif
1386 #ifdef HAVE_POSIX_ACLS
1387         EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
1388 #endif
1389         /*
1390          * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1391          * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1392          * allowed where a delete would succeed because the parent dir gives write perms.
1393          * So we check the parent dir for write access and set the right accordingly.
1394          * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1395          * there to do this extra check -- afaict.
1396          */
1397         if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1398             int i;
1399             uint32_t parent_rights = 0;
1400
1401             if (curdir->d_did == DIRDID_ROOT_PARENT) {
1402                 /* use volume path */
1403                 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1404             } else {
1405                 /* build path for parent */
1406                 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1407                 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1408                 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1409                 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1410                 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1411             }
1412
1413             LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1414             EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1415
1416 #ifdef HAVE_SOLARIS_ACLS
1417             EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, &parent_rights));
1418 #endif
1419 #ifdef HAVE_POSIX_ACLS
1420             EC_ZERO_LOG(posix_acl_rights(path, &st, &parent_rights));
1421 #endif
1422             if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1423                 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1424         }
1425
1426         if (is_dir) {
1427             /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1428              * directory.
1429              */
1430             if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1431                 allowed_rights |= DARWIN_ACE_DELETE;
1432
1433             curdir->d_rights_cache = allowed_rights;
1434         }
1435         LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1436     }
1437
1438     if ((requested_rights & allowed_rights) != requested_rights) {
1439         LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1440             requested_rights, allowed_rights);
1441         EC_STATUS(AFPERR_ACCESS);
1442     } else {
1443         LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1444             requested_rights);
1445         EC_STATUS(AFP_OK);
1446     }
1447
1448 EC_CLEANUP:
1449     if (username) free(username);
1450     if (parent) bdestroy(parent);
1451
1452     EC_EXIT;
1453 }
1454
1455 /********************************************************
1456  * Interface
1457  ********************************************************/
1458
1459 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1460 {
1461     int         ret;
1462     struct vol      *vol;
1463     struct dir      *dir;
1464     uint32_t            did, darwin_ace_rights;
1465     uint16_t        vid;
1466     struct path         *s_path;
1467     uuidp_t             uuid;
1468
1469     *rbuflen = 0;
1470     ibuf += 2;
1471
1472     memcpy(&vid, ibuf, sizeof( vid ));
1473     ibuf += sizeof(vid);
1474     if (NULL == ( vol = getvolbyvid( vid ))) {
1475         LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1476         return AFPERR_NOOBJ;
1477     }
1478
1479     memcpy(&did, ibuf, sizeof( did ));
1480     ibuf += sizeof( did );
1481     if (NULL == ( dir = dirlookup( vol, did ))) {
1482         LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1483         return afp_errno;
1484     }
1485
1486     /* Skip bitmap */
1487     ibuf += 2;
1488
1489     /* Store UUID address */
1490     uuid = (uuidp_t)ibuf;
1491     ibuf += UUID_BINSIZE;
1492
1493     /* Store ACE rights */
1494     memcpy(&darwin_ace_rights, ibuf, 4);
1495     darwin_ace_rights = ntohl(darwin_ace_rights);
1496     ibuf += 4;
1497
1498     /* get full path and handle file/dir subtleties in netatalk code*/
1499     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1500         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1501         return AFPERR_NOOBJ;
1502     }
1503     if (!s_path->st_valid)
1504         of_statdir(vol, s_path);
1505     if ( s_path->st_errno != 0 ) {
1506         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1507         return AFPERR_NOOBJ;
1508     }
1509
1510     ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1511
1512     return ret;
1513 }
1514
1515 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1516 {
1517     struct vol      *vol;
1518     struct dir      *dir;
1519     int         ret;
1520     uint32_t           did;
1521     uint16_t        vid, bitmap;
1522     struct path         *s_path;
1523     struct passwd       *pw;
1524     struct group        *gr;
1525
1526     LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1527     *rbuflen = 0;
1528     ibuf += 2;
1529
1530     memcpy(&vid, ibuf, sizeof( vid ));
1531     ibuf += sizeof(vid);
1532     if (NULL == ( vol = getvolbyvid( vid ))) {
1533         LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1534         return AFPERR_NOOBJ;
1535     }
1536
1537     memcpy(&did, ibuf, sizeof( did ));
1538     ibuf += sizeof( did );
1539     if (NULL == ( dir = dirlookup( vol, did ))) {
1540         LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1541         return afp_errno;
1542     }
1543
1544     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1545     memcpy(rbuf, ibuf, sizeof( bitmap ));
1546     bitmap = ntohs( bitmap );
1547     ibuf += sizeof( bitmap );
1548     rbuf += sizeof( bitmap );
1549     *rbuflen += sizeof( bitmap );
1550
1551     /* skip maxreplysize */
1552     ibuf += 4;
1553
1554     /* get full path and handle file/dir subtleties in netatalk code*/
1555     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1556         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1557         return AFPERR_NOOBJ;
1558     }
1559     if (!s_path->st_valid)
1560         of_statdir(vol, s_path);
1561     if ( s_path->st_errno != 0 ) {
1562         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1563         return AFPERR_NOOBJ;
1564     }
1565
1566     /* Shall we return owner UUID ? */
1567     if (bitmap & kFileSec_UUID) {
1568         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1569         if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1570             LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1571             localuuid_from_id(rbuf, UUID_USER, s_path->st.st_uid);
1572         } else {
1573             LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1574             if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1575                 return AFPERR_MISC;
1576         }
1577         rbuf += UUID_BINSIZE;
1578         *rbuflen += UUID_BINSIZE;
1579     }
1580
1581     /* Shall we return group UUID ? */
1582     if (bitmap & kFileSec_GRPUUID) {
1583         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1584         if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1585             LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1586             localuuid_from_id(rbuf, UUID_GROUP, s_path->st.st_gid);
1587         } else {
1588             LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1589             if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1590                 return AFPERR_MISC;
1591         }
1592         rbuf += UUID_BINSIZE;
1593         *rbuflen += UUID_BINSIZE;
1594     }
1595
1596     /* Shall we return ACL ? */
1597     if (bitmap & kFileSec_ACL) {
1598         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1599         if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1600             LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1601                 getcwdpath(), s_path->u_name);
1602             return AFPERR_MISC;
1603         }
1604     }
1605
1606     LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1607     return AFP_OK;
1608 }
1609
1610 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1611 {
1612     struct vol      *vol;
1613     struct dir      *dir;
1614     int         ret;
1615     uint32_t            did;
1616     uint16_t        vid, bitmap;
1617     struct path         *s_path;
1618
1619     LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1620     *rbuflen = 0;
1621     ibuf += 2;
1622
1623     memcpy(&vid, ibuf, sizeof( vid ));
1624     ibuf += sizeof(vid);
1625     if (NULL == ( vol = getvolbyvid( vid ))) {
1626         LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1627         return AFPERR_NOOBJ;
1628     }
1629
1630     memcpy(&did, ibuf, sizeof( did ));
1631     ibuf += sizeof( did );
1632     if (NULL == ( dir = dirlookup( vol, did ))) {
1633         LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1634         return afp_errno;
1635     }
1636
1637     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1638     bitmap = ntohs( bitmap );
1639     ibuf += sizeof( bitmap );
1640
1641     /* get full path and handle file/dir subtleties in netatalk code*/
1642     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1643         LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1644         return AFPERR_NOOBJ;
1645     }
1646     if (!s_path->st_valid)
1647         of_statdir(vol, s_path);
1648     if ( s_path->st_errno != 0 ) {
1649         LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1650         return AFPERR_NOOBJ;
1651     }
1652     LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1653
1654     /* Padding? */
1655     if ((unsigned long)ibuf & 1)
1656         ibuf++;
1657
1658     /* Start processing request */
1659
1660     /* Change owner: dont even try */
1661     if (bitmap & kFileSec_UUID) {
1662         LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1663         ret = AFPERR_ACCESS;
1664         ibuf += UUID_BINSIZE;
1665     }
1666
1667     /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1668     if (bitmap & kFileSec_UUID) {
1669         LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1670         ret = AFPERR_PARAM;
1671         ibuf += UUID_BINSIZE;
1672     }
1673
1674     /* Remove ACL ? */
1675     if (bitmap & kFileSec_REMOVEACL) {
1676         LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1677         if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1678             LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1679     }
1680
1681     /* Change ACL ? */
1682     if (bitmap & kFileSec_ACL) {
1683         LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1684         /*  Get no of ACEs the client put on the wire */
1685         uint32_t ace_count;
1686         memcpy(&ace_count, ibuf, sizeof(uint32_t));
1687         ace_count = htonl(ace_count);
1688         ibuf += 8;      /* skip ACL flags (see acls.h) */
1689
1690         ret = set_acl(vol,
1691                       s_path->u_name,
1692                       (bitmap & kFileSec_Inherit),
1693                       (darwin_ace_t *)ibuf,
1694                       ace_count);
1695         if (ret == 0)
1696             ret = AFP_OK;
1697         else {
1698             LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1699                 getcwdpath(), s_path->u_name);
1700             ret = AFPERR_MISC;
1701         }
1702     }
1703
1704     LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1705     return ret;
1706 }
1707
1708 /********************************************************************
1709  * ACL funcs interfacing with other parts
1710  ********************************************************************/
1711
1712 /*!
1713  * map ACL to user maccess
1714  *
1715  * This is the magic function that makes ACLs usable by calculating
1716  * the access granted by ACEs to the logged in user.
1717  */
1718 int acltoownermode(char *path, struct stat *st, struct maccess *ma)
1719 {
1720     EC_INIT;
1721     uint32_t rights = 0;
1722
1723     if ( ! (AFPobj->options.flags & OPTION_ACL2MACCESS)
1724          || (current_vol == NULL)
1725          || ! (current_vol->v_flags & AFPVOL_ACLS))
1726          return 0;
1727
1728     LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
1729         getcwdpath(), path, ma->ma_user);
1730
1731 #ifdef HAVE_SOLARIS_ACLS
1732     EC_ZERO_LOG(solaris_acl_rights(path, st, &rights));
1733
1734     LOG(log_maxdebug, logtype_afpd, "rights: 0x%08x", rights);
1735
1736     if (rights & DARWIN_ACE_READ_DATA)
1737         ma->ma_user |= AR_UREAD;
1738     if (rights & DARWIN_ACE_WRITE_DATA)
1739         ma->ma_user |= AR_UWRITE;
1740     if (rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
1741         ma->ma_user |= AR_USEARCH;
1742 #endif
1743
1744 #ifdef HAVE_POSIX_ACLS
1745     EC_ZERO_LOG(posix_acls_to_uaperms(path, st, ma));
1746 #endif
1747
1748     LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x group maccess: 0x%02x", ma->ma_user, ma->ma_group);
1749
1750 EC_CLEANUP:
1751     EC_EXIT;
1752 }
1753
1754 /*!
1755  * Check whether a volume supports ACLs
1756  *
1757  * @param vol  (r) volume
1758  *
1759  * @returns        0 if not, 1 if yes
1760  */
1761 int check_vol_acl_support(const struct vol *vol)
1762 {
1763     int ret = 0;
1764
1765 #ifdef HAVE_SOLARIS_ACLS
1766     ace_t *aces = NULL;
1767     ret = 1;
1768     if (get_nfsv4_acl(vol->v_path, &aces) == -1)
1769         ret = 0;
1770 #endif
1771 #ifdef HAVE_POSIX_ACLS
1772     acl_t acl = NULL;
1773     ret = 1;
1774     if ((acl = acl_get_file(vol->v_path, ACL_TYPE_ACCESS)) == NULL)
1775         ret = 0;
1776 #endif
1777
1778 #ifdef HAVE_SOLARIS_ACLS
1779     if (aces) free(aces);
1780 #endif
1781 #ifdef HAVE_POSIX_ACLS
1782     if (acl) acl_free(acl);
1783 #endif /* HAVE_POSIX_ACLS */
1784
1785     LOG(log_debug, logtype_afpd, "Volume \"%s\" ACL support: %s",
1786         vol->v_path, ret ? "yes" : "no");
1787     return ret;
1788 }