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