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