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