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