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