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