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