]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/acls.c
Rename dbus daemon path option
[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 EACCES:
1190         case EPERM:
1191             EC_STATUS(AFPERR_ACCESS);
1192             break;
1193         case ENOENT:
1194             EC_STATUS(AFP_OK);
1195             break;
1196         default:
1197             EC_STATUS(AFPERR_MISC);
1198             break;
1199         }
1200         goto EC_CLEANUP;
1201     }
1202     if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1203         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1204         if (errno == (EACCES | EPERM))
1205             EC_STATUS(AFPERR_ACCESS);
1206         else if (errno == ENOENT)
1207             EC_STATUS(AFPERR_NOITEM);
1208         else
1209             EC_STATUS(AFPERR_MISC);
1210         goto EC_CLEANUP;
1211     }
1212
1213     EC_STATUS(AFP_OK);
1214
1215 EC_CLEANUP:
1216     if (old_aces) free(old_aces);
1217     if (new_aces) free(new_aces);
1218
1219     LOG(log_debug9, logtype_afpd, "set_acl: END");
1220     EC_EXIT;
1221 }
1222 #endif /* HAVE_SOLARIS_ACLS */
1223
1224 #ifdef HAVE_POSIX_ACLS
1225 #ifndef HAVE_ACL_FROM_MODE
1226 static acl_t acl_from_mode(mode_t mode)
1227 {
1228     acl_t acl;
1229     acl_entry_t entry;
1230     acl_permset_t permset;
1231
1232     if (!(acl = acl_init(3)))
1233         return NULL;
1234
1235     if (acl_create_entry(&acl, &entry) != 0)
1236         goto error;
1237     acl_set_tag_type(entry, ACL_USER_OBJ);
1238     acl_get_permset(entry, &permset);
1239     acl_clear_perms(permset);
1240     if (mode & S_IRUSR)
1241         acl_add_perm(permset, ACL_READ);
1242     if (mode & S_IWUSR)
1243         acl_add_perm(permset, ACL_WRITE);
1244     if (mode & S_IXUSR)
1245         acl_add_perm(permset, ACL_EXECUTE);
1246     acl_set_permset(entry, permset);
1247
1248     if (acl_create_entry(&acl, &entry) != 0)
1249         goto error;
1250     acl_set_tag_type(entry, ACL_GROUP_OBJ);
1251     acl_get_permset(entry, &permset);
1252     acl_clear_perms(permset);
1253     if (mode & S_IRGRP)
1254         acl_add_perm(permset, ACL_READ);
1255     if (mode & S_IWGRP)
1256         acl_add_perm(permset, ACL_WRITE);
1257     if (mode & S_IXGRP)
1258         acl_add_perm(permset, ACL_EXECUTE);
1259     acl_set_permset(entry, permset);
1260
1261     if (acl_create_entry(&acl, &entry) != 0)
1262         goto error;
1263     acl_set_tag_type(entry, ACL_OTHER);
1264     acl_get_permset(entry, &permset);
1265     acl_clear_perms(permset);
1266     if (mode & S_IROTH)
1267         acl_add_perm(permset, ACL_READ);
1268     if (mode & S_IWOTH)
1269         acl_add_perm(permset, ACL_WRITE);
1270     if (mode & S_IXOTH)
1271         acl_add_perm(permset, ACL_EXECUTE);
1272     acl_set_permset(entry, permset);
1273
1274     return acl;
1275
1276 error:
1277     acl_free(acl);
1278     return NULL;
1279 }
1280 #endif
1281
1282 static int set_acl(const struct vol *vol,
1283                    const char *name,
1284                    int inherit _U_,
1285                    darwin_ace_t *daces,
1286                    uint32_t ace_count)
1287 {
1288     EC_INIT;
1289     struct stat st;
1290     acl_t default_acl = NULL;
1291     acl_t access_acl = NULL;
1292     acl_entry_t entry;
1293     acl_tag_t tag;
1294     int entry_id = ACL_FIRST_ENTRY;
1295     /* flags to indicate if the object has a minimal default acl and/or an extended
1296      * default acl.
1297      */
1298     uint32_t default_acl_flags = 0;
1299
1300     LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1301
1302     EC_NULL_LOG_ERR(access_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1303
1304     /* Iterate through acl and remove all extended acl entries. */
1305     while (acl_get_entry(access_acl, entry_id, &entry) == 1) {
1306         entry_id = ACL_NEXT_ENTRY;
1307         EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
1308
1309         if ((tag == ACL_USER) || (tag == ACL_GROUP) || (tag == ACL_MASK)) {
1310             EC_ZERO_LOG_ERR(acl_delete_entry(access_acl, entry), AFPERR_MISC);
1311         }
1312     } /* while */
1313
1314    /* In case we are acting on a directory prepare a default acl. For files default_acl will be NULL.
1315     * If the directory already has a default acl it will be preserved.
1316     */
1317    EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1318
1319    if (S_ISDIR(st.st_mode)) {
1320        default_acl = acl_get_file(name, ACL_TYPE_DEFAULT);
1321
1322        if (default_acl) {
1323            /* If default_acl is not empty then the dir has a default acl. */
1324            if (acl_get_entry(default_acl, ACL_FIRST_ENTRY, &entry) == 1)
1325                default_acl_flags = HAS_DEFAULT_ACL;
1326
1327            acl_free(default_acl);
1328        }
1329        default_acl = acl_dup(access_acl);
1330     }
1331     /* adds the clients aces */
1332     EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &default_acl, &access_acl, ace_count, &default_acl_flags), AFPERR_MISC);
1333
1334     /* calcuate ACL mask */
1335     EC_ZERO_LOG_ERR(acl_calc_mask(&access_acl), AFPERR_MISC);
1336
1337     /* is it ok? */
1338     EC_ZERO_LOG_ERR(acl_valid(access_acl), AFPERR_MISC);
1339
1340     /* set it */
1341     EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, access_acl), AFPERR_MISC);
1342     EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, access_acl), AFPERR_MISC);
1343
1344     if (default_acl) {
1345         /* If the dir has an extended default acl it's ACL_MASK must be updated.*/
1346         if (default_acl_flags & HAS_EXT_DEFAULT_ACL)
1347             EC_ZERO_LOG_ERR(acl_calc_mask(&default_acl), AFPERR_MISC);
1348
1349         if (default_acl_flags) {
1350             EC_ZERO_LOG_ERR(acl_valid(default_acl), AFPERR_MISC);
1351             EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, default_acl), AFPERR_MISC);
1352             EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, default_acl), AFPERR_MISC);
1353         }
1354     }
1355
1356 EC_CLEANUP:
1357     if (access_acl) acl_free(access_acl);
1358     if (default_acl) acl_free(default_acl);
1359
1360     LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1361     EC_EXIT;
1362 }
1363 #endif /* HAVE_POSIX_ACLS */
1364
1365 /*!
1366  * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1367  *
1368  * Note: this gets called frequently and is a good place for optimizations !
1369  *
1370  * @param vol              (r) volume
1371  * @param dir              (rw) directory
1372  * @param path             (r) path to filesystem object
1373  * @param uuid             (r) UUID of user
1374  * @param requested_rights (r) requested Darwin ACE
1375  *
1376  * @returns                    AFP result code
1377 */
1378 static int check_acl_access(const AFPObj *obj,
1379                             const struct vol *vol,
1380                             struct dir *dir,
1381                             const char *path,
1382                             const uuidp_t uuid,
1383                             uint32_t requested_rights)
1384 {
1385     int            ret;
1386     uint32_t       allowed_rights = 0;
1387     char           *username = NULL;
1388     struct stat    st;
1389     bstring        parent = NULL;
1390     int            is_dir;
1391
1392     LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1393         cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1394
1395     AFP_ASSERT(vol);
1396
1397     /* This check is not used anymore, as OS X Server seems to be ignoring too */
1398 #if 0
1399     /* Get uid or gid from UUID */
1400     EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1401     switch (uuidtype) {
1402     case UUID_USER:
1403         break;
1404     case UUID_GROUP:
1405         LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1406         EC_STATUS(AFPERR_MISC);
1407         goto EC_CLEANUP;
1408     default:
1409         EC_STATUS(AFPERR_MISC);
1410         goto EC_CLEANUP;
1411     }
1412 #endif
1413
1414     EC_ZERO_LOG_ERR(ostat(path, &st, vol_syml_opt(vol)), AFPERR_PARAM);
1415
1416     is_dir = !strcmp(path, ".");
1417
1418     if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1419         /* its a dir and the cache value is valid */
1420         allowed_rights = curdir->d_rights_cache;
1421         LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1422     } else {
1423 #ifdef HAVE_SOLARIS_ACLS
1424         EC_ZERO_LOG(solaris_acl_rights(obj, path, &st, NULL, &allowed_rights));
1425 #endif
1426 #ifdef HAVE_POSIX_ACLS
1427         EC_ZERO_LOG(posix_acl_rights(obj, path, &st, &allowed_rights));
1428 #endif
1429         /*
1430          * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1431          * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1432          * allowed where a delete would succeed because the parent dir gives write perms.
1433          * So we check the parent dir for write access and set the right accordingly.
1434          * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1435          * there to do this extra check -- afaict.
1436          */
1437         if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1438             int i;
1439             uint32_t parent_rights = 0;
1440
1441             if (curdir->d_did == DIRDID_ROOT_PARENT) {
1442                 /* use volume path */
1443                 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1444             } else {
1445                 /* build path for parent */
1446                 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1447                 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1448                 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1449                 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1450                 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1451             }
1452
1453             LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1454             EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1455
1456 #ifdef HAVE_SOLARIS_ACLS
1457             EC_ZERO_LOG(solaris_acl_rights(obj, cfrombstr(parent), &st, NULL, &parent_rights));
1458 #endif
1459 #ifdef HAVE_POSIX_ACLS
1460             EC_ZERO_LOG(posix_acl_rights(obj, path, &st, &parent_rights));
1461 #endif
1462             if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1463                 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1464         }
1465
1466         if (is_dir) {
1467             /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1468              * directory.
1469              */
1470             if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1471                 allowed_rights |= DARWIN_ACE_DELETE;
1472
1473             curdir->d_rights_cache = allowed_rights;
1474         }
1475         LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1476     }
1477
1478     if ((requested_rights & allowed_rights) != requested_rights) {
1479         LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1480             requested_rights, allowed_rights);
1481         EC_STATUS(AFPERR_ACCESS);
1482     } else {
1483         LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1484             requested_rights);
1485         EC_STATUS(AFP_OK);
1486     }
1487
1488 EC_CLEANUP:
1489     if (username) free(username);
1490     if (parent) bdestroy(parent);
1491
1492     EC_EXIT;
1493 }
1494
1495 /********************************************************
1496  * Interface
1497  ********************************************************/
1498
1499 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1500 {
1501     int         ret;
1502     struct vol      *vol;
1503     struct dir      *dir;
1504     uint32_t            did, darwin_ace_rights;
1505     uint16_t        vid;
1506     struct path         *s_path;
1507     uuidp_t             uuid;
1508
1509     *rbuflen = 0;
1510     ibuf += 2;
1511
1512     memcpy(&vid, ibuf, sizeof( vid ));
1513     ibuf += sizeof(vid);
1514     if (NULL == ( vol = getvolbyvid( vid ))) {
1515         LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1516         return AFPERR_NOOBJ;
1517     }
1518
1519     memcpy(&did, ibuf, sizeof( did ));
1520     ibuf += sizeof( did );
1521     if (NULL == ( dir = dirlookup( vol, did ))) {
1522         LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1523         return afp_errno;
1524     }
1525
1526     /* Skip bitmap */
1527     ibuf += 2;
1528
1529     /* Store UUID address */
1530     uuid = (uuidp_t)ibuf;
1531     ibuf += UUID_BINSIZE;
1532
1533     /* Store ACE rights */
1534     memcpy(&darwin_ace_rights, ibuf, 4);
1535     darwin_ace_rights = ntohl(darwin_ace_rights);
1536     ibuf += 4;
1537
1538     /* get full path and handle file/dir subtleties in netatalk code*/
1539     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1540         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1541         return AFPERR_NOOBJ;
1542     }
1543     if (!s_path->st_valid)
1544         of_statdir(vol, s_path);
1545     if ( s_path->st_errno != 0 ) {
1546         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1547         return AFPERR_NOOBJ;
1548     }
1549
1550     ret = check_acl_access(obj, vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1551
1552     return ret;
1553 }
1554
1555 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1556 {
1557     struct vol      *vol;
1558     struct dir      *dir;
1559     int         ret;
1560     uint32_t           did;
1561     uint16_t        vid, bitmap;
1562     struct path         *s_path;
1563     struct passwd       *pw;
1564     struct group        *gr;
1565
1566     LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1567     *rbuflen = 0;
1568     ibuf += 2;
1569
1570     memcpy(&vid, ibuf, sizeof( vid ));
1571     ibuf += sizeof(vid);
1572     if (NULL == ( vol = getvolbyvid( vid ))) {
1573         LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1574         return AFPERR_NOOBJ;
1575     }
1576
1577     memcpy(&did, ibuf, sizeof( did ));
1578     ibuf += sizeof( did );
1579     if (NULL == ( dir = dirlookup( vol, did ))) {
1580         LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1581         return afp_errno;
1582     }
1583
1584     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1585     memcpy(rbuf, ibuf, sizeof( bitmap ));
1586     bitmap = ntohs( bitmap );
1587     ibuf += sizeof( bitmap );
1588     rbuf += sizeof( bitmap );
1589     *rbuflen += sizeof( bitmap );
1590
1591     /* skip maxreplysize */
1592     ibuf += 4;
1593
1594     /* get full path and handle file/dir subtleties in netatalk code*/
1595     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1596         LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1597         return AFPERR_NOOBJ;
1598     }
1599     if (!s_path->st_valid)
1600         of_statdir(vol, s_path);
1601     if ( s_path->st_errno != 0 ) {
1602         LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1603         return AFPERR_NOOBJ;
1604     }
1605
1606     /* Shall we return owner UUID ? */
1607     if (bitmap & kFileSec_UUID) {
1608         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1609         if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1610             LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1611             localuuid_from_id((unsigned char *)rbuf, UUID_USER, s_path->st.st_uid);
1612         } else {
1613             LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1614             if ((ret = getuuidfromname(pw->pw_name, UUID_USER, (unsigned char *)rbuf)) != 0)
1615                 return AFPERR_MISC;
1616         }
1617         rbuf += UUID_BINSIZE;
1618         *rbuflen += UUID_BINSIZE;
1619     }
1620
1621     /* Shall we return group UUID ? */
1622     if (bitmap & kFileSec_GRPUUID) {
1623         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1624         if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1625             LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1626             localuuid_from_id((unsigned char *)rbuf, UUID_GROUP, s_path->st.st_gid);
1627         } else {
1628             LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1629             if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, (unsigned char *)rbuf)) != 0)
1630                 return AFPERR_MISC;
1631         }
1632         rbuf += UUID_BINSIZE;
1633         *rbuflen += UUID_BINSIZE;
1634     }
1635
1636     /* Shall we return ACL ? */
1637     if (bitmap & kFileSec_ACL) {
1638         LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1639         if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1640             LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1641                 getcwdpath(), s_path->u_name);
1642             return AFPERR_MISC;
1643         }
1644     }
1645
1646     LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1647     return AFP_OK;
1648 }
1649
1650 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1651 {
1652     struct vol      *vol;
1653     struct dir      *dir;
1654     int         ret;
1655     uint32_t            did;
1656     uint16_t        vid, bitmap;
1657     struct path         *s_path;
1658
1659     LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1660     *rbuflen = 0;
1661     ibuf += 2;
1662
1663     memcpy(&vid, ibuf, sizeof( vid ));
1664     ibuf += sizeof(vid);
1665     if (NULL == ( vol = getvolbyvid( vid ))) {
1666         LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1667         return AFPERR_NOOBJ;
1668     }
1669
1670     memcpy(&did, ibuf, sizeof( did ));
1671     ibuf += sizeof( did );
1672     if (NULL == ( dir = dirlookup( vol, did ))) {
1673         LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1674         return afp_errno;
1675     }
1676
1677     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1678     bitmap = ntohs( bitmap );
1679     ibuf += sizeof( bitmap );
1680
1681     /* get full path and handle file/dir subtleties in netatalk code*/
1682     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1683         LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1684         return AFPERR_NOOBJ;
1685     }
1686     if (!s_path->st_valid)
1687         of_statdir(vol, s_path);
1688     if ( s_path->st_errno != 0 ) {
1689         LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1690         return AFPERR_NOOBJ;
1691     }
1692     LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1693
1694     /* Padding? */
1695     if ((unsigned long)ibuf & 1)
1696         ibuf++;
1697
1698     /* Start processing request */
1699
1700     /* Change owner: dont even try */
1701     if (bitmap & kFileSec_UUID) {
1702         LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1703         ret = AFPERR_ACCESS;
1704         ibuf += UUID_BINSIZE;
1705     }
1706
1707     /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1708     if (bitmap & kFileSec_UUID) {
1709         LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1710         ret = AFPERR_PARAM;
1711         ibuf += UUID_BINSIZE;
1712     }
1713
1714     /* Remove ACL ? */
1715     if (bitmap & kFileSec_REMOVEACL) {
1716         LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1717         if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1718             LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1719     }
1720
1721     /* Change ACL ? */
1722     if (bitmap & kFileSec_ACL) {
1723         LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1724         /*  Get no of ACEs the client put on the wire */
1725         uint32_t ace_count;
1726         memcpy(&ace_count, ibuf, sizeof(uint32_t));
1727         ace_count = htonl(ace_count);
1728         ibuf += 8;      /* skip ACL flags (see acls.h) */
1729
1730         ret = set_acl(vol,
1731                       s_path->u_name,
1732                       (bitmap & kFileSec_Inherit),
1733                       (darwin_ace_t *)ibuf,
1734                       ace_count);
1735         if (ret == 0)
1736             ret = AFP_OK;
1737         else {
1738             LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1739                 getcwdpath(), s_path->u_name);
1740             ret = AFPERR_MISC;
1741         }
1742     }
1743
1744     LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1745     return ret;
1746 }
1747
1748 /********************************************************************
1749  * ACL funcs interfacing with other parts
1750  ********************************************************************/
1751
1752 /*!
1753  * map ACL to user maccess
1754  *
1755  * This is the magic function that makes ACLs usable by calculating
1756  * the access granted by ACEs to the logged in user.
1757  */
1758 int acltoownermode(const AFPObj *obj, const struct vol *vol, char *path, struct stat *st, struct maccess *ma)
1759 {
1760     EC_INIT;
1761
1762     if ( ! (obj->options.flags & (OPTION_ACL2MACCESS | OPTION_ACL2MODE))
1763          || ! (vol->v_flags & AFPVOL_ACLS))
1764          return 0;
1765
1766     LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
1767         getcwdpath(), path, ma->ma_user);
1768
1769 #ifdef HAVE_SOLARIS_ACLS
1770     EC_ZERO_LOG(solaris_acl_rights(obj, path, st, ma, NULL));
1771 #endif
1772
1773 #ifdef HAVE_POSIX_ACLS
1774     EC_ZERO_LOG(posix_acls_to_uaperms(obj, path, st, ma));
1775 #endif
1776
1777     LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x group maccess: 0x%02x", ma->ma_user, ma->ma_group);
1778
1779 EC_CLEANUP:
1780     EC_EXIT;
1781 }