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