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