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