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