2 Copyright (c) 2008, 2009, 2010 Frank Lahm <franklahm@gmail.com>
3 Copyright (c) 2011 Laura Mueller <laura-mueller@uni-duesseldorf.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 #endif /* HAVE_CONFIG_H */
27 #ifdef HAVE_SOLARIS_ACLS
30 #ifdef HAVE_POSIX_ACLS
33 #ifdef HAVE_ACL_LIBACL_H
34 #include <acl/libacl.h>
37 #include <atalk/errchk.h>
38 #include <atalk/adouble.h>
39 #include <atalk/vfs.h>
40 #include <atalk/afp.h>
41 #include <atalk/util.h>
42 #include <atalk/cnid.h>
43 #include <atalk/logger.h>
44 #include <atalk/uuid.h>
45 #include <atalk/acl.h>
46 #include <atalk/bstrlib.h>
47 #include <atalk/bstradd.h>
48 #include <atalk/unix.h>
49 #include <atalk/netatalk_conf.h>
51 #include "directory.h"
57 #include "acl_mappings.h"
61 #define SOLARIS_2_DARWIN 1
62 #define DARWIN_2_SOLARIS 2
63 #define POSIX_DEFAULT_2_DARWIN 3
64 #define POSIX_ACCESS_2_DARWIN 4
65 #define DARWIN_2_POSIX_DEFAULT 5
66 #define DARWIN_2_POSIX_ACCESS 6
71 /* bit flags for set_acl() and map_aces_darwin_to_posix() */
72 #define HAS_DEFAULT_ACL 0x01
73 #define HAS_EXT_DEFAULT_ACL 0x02
75 /********************************************************
77 ********************************************************/
79 #ifdef HAVE_SOLARIS_ACLS
82 * Compile access rights for a user to one file-system object
84 * This combines combines all access rights for a user to one fs-object and
85 * returns the result as a Darwin allowed rights ACE.
86 * This must honor trivial ACEs which are a mode_t mapping.
88 * @param obj (r) handle
89 * @param path (r) path to filesystem object
90 * @param sb (rw) struct stat of path
91 * @param ma (rw) UARights struct
92 * @param rights_out (w) mapped Darwin ACL rights
94 * @returns 0 or -1 on error
96 static int solaris_acl_rights(const AFPObj *obj,
100 uint32_t *rights_out)
103 int i, ace_count, checkgroup;
106 uint16_t flags, type;
107 uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
109 /* Get ACL from file/dir */
110 EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
115 /* Now check requested rights */
117 do { /* Loop through ACEs */
119 flags = aces[i].a_flags;
120 type = aces[i].a_type;
121 rights = aces[i].a_access_mask;
123 if (flags & ACE_INHERIT_ONLY_ACE)
126 /* Now the tricky part: decide if ACE effects our user. I'll explain:
127 if its a dedicated (non trivial) ACE for the user
129 if its a ACE for a group we're member of
131 if its a trivial ACE_OWNER ACE and requested UUID is the owner
133 if its a trivial ACE_GROUP ACE and requested UUID is group
135 if its a trivial ACE_EVERYONE ACE
138 if (((who == obj->uid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
140 ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who, obj->ngroups, obj->groups))
142 ((flags & ACE_OWNER) && (obj->uid == sb->st_uid))
144 ((flags & ACE_GROUP) && !(obj->uid == sb->st_uid) && gmem(sb->st_gid, obj->ngroups, obj->groups))
146 (flags & ACE_EVERYONE && !(obj->uid == sb->st_uid) && !gmem(sb->st_gid, obj->ngroups, obj->groups))
148 /* Found an applicable ACE */
149 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
150 allowed_rights |= rights;
151 else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
152 /* Only or to denied rights if not previously allowed !! */
153 denied_rights |= ((!allowed_rights) & rights);
155 } while (++i < ace_count);
158 /* Darwin likes to ask for "delete_child" on dir,
159 "write_data" is actually the same, so we add that for dirs */
160 if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
161 allowed_rights |= ACE_DELETE_CHILD;
163 /* Remove denied from allowed rights */
164 allowed_rights &= ~denied_rights;
168 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
169 if (allowed_rights & nfsv4_to_darwin_rights[i].from)
170 darwin_rights |= nfsv4_to_darwin_rights[i].to;
173 LOG(log_maxdebug, logtype_afpd, "rights: 0x%08x", darwin_rights);
176 *rights_out = darwin_rights;
178 if (ma && obj->options.flags & OPTION_ACL2MACCESS) {
179 if (darwin_rights & DARWIN_ACE_READ_DATA)
180 ma->ma_user |= AR_UREAD;
181 if (darwin_rights & DARWIN_ACE_WRITE_DATA)
182 ma->ma_user |= AR_UWRITE;
183 if (darwin_rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
184 ma->ma_user |= AR_USEARCH;
187 if (sb && obj->options.flags & OPTION_ACL2MODE) {
188 if (darwin_rights & DARWIN_ACE_READ_DATA)
189 sb->st_mode |= S_IRUSR;
190 if (darwin_rights & DARWIN_ACE_WRITE_DATA)
191 sb->st_mode |= S_IWUSR;
192 if (darwin_rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
193 sb->st_mode |= S_IXUSR;
197 if (aces) free(aces);
203 Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
204 Return numer of mapped ACEs or -1 on error.
205 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
207 static int map_aces_solaris_to_darwin(const ace_t *aces,
208 darwin_ace_t *darwin_aces,
215 struct passwd *pwd = NULL;
216 struct group *grp = NULL;
218 LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
221 LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
222 /* if its a ACE resulting from nfsv4 mode mapping, discard it */
223 if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
224 LOG(log_debug, logtype_afpd, "trivial ACE");
229 if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
230 LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
231 EC_NULL_LOG(pwd = getpwuid(aces->a_who));
232 LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
233 EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
235 darwin_aces->darwin_ace_uuid));
236 } else { /* group ace */
237 LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
238 EC_NULL_LOG(grp = getgrgid(aces->a_who));
239 LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
240 EC_ZERO_LOG(getuuidfromname(grp->gr_name,
242 darwin_aces->darwin_ace_uuid));
246 if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
247 flags = DARWIN_ACE_FLAGS_PERMIT;
248 else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
249 flags = DARWIN_ACE_FLAGS_DENY;
250 else { /* unsupported type */
254 for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
255 if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
256 flags |= nfsv4_to_darwin_flags[i].to;
258 darwin_aces->darwin_ace_flags = htonl(flags);
262 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
263 if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
264 rights |= nfsv4_to_darwin_rights[i].to;
266 darwin_aces->darwin_ace_rights = htonl(rights);
279 Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
280 Return numer of mapped ACEs or -1 on error.
281 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
283 static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
288 int i, mapped_aces = 0;
289 uint32_t darwin_ace_flags;
290 uint32_t darwin_ace_rights;
291 uint16_t nfsv4_ace_flags;
292 uint32_t nfsv4_ace_rights;
300 nfsv4_ace_rights = 0;
303 EC_ZERO(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
306 EC_NULL_LOG(pwd = getpwnam(name));
307 nfsv4_aces->a_who = pwd->pw_uid;
310 EC_NULL_LOG(grp = getgrnam(name));
311 nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
312 nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
315 LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: unkown uuidtype");
321 /* now type: allow/deny */
322 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
323 if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
324 nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
325 else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
326 nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
327 else { /* unsupported type */
332 for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
333 if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
334 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
338 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
339 for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
340 if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
341 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
344 LOG(log_debug9, logtype_afpd,
345 "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x",
346 darwin_ace_flags, nfsv4_ace_flags);
347 LOG(log_debug9, logtype_afpd,
348 "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x",
349 darwin_ace_rights, nfsv4_ace_rights);
351 nfsv4_aces->a_flags = nfsv4_ace_flags;
352 nfsv4_aces->a_access_mask = nfsv4_ace_rights;
365 #endif /* HAVE_SOLARIS_ACLS */
367 /********************************************************
369 ********************************************************/
371 #ifdef HAVE_POSIX_ACLS
373 static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
377 acl_permset_t permset;
379 EC_ZERO_LOG(acl_get_permset(e, &permset));
381 #ifdef HAVE_ACL_GET_PERM_NP
382 if (acl_get_perm_np(permset, ACL_READ))
384 if (acl_get_perm(permset, ACL_READ))
386 rights = DARWIN_ACE_READ_DATA
387 | DARWIN_ACE_READ_EXTATTRIBUTES
388 | DARWIN_ACE_READ_ATTRIBUTES
389 | DARWIN_ACE_READ_SECURITY;
390 #ifdef HAVE_ACL_GET_PERM_NP
391 if (acl_get_perm_np(permset, ACL_WRITE)) {
393 if (acl_get_perm(permset, ACL_WRITE)) {
395 rights |= DARWIN_ACE_WRITE_DATA
396 | DARWIN_ACE_APPEND_DATA
397 | DARWIN_ACE_WRITE_EXTATTRIBUTES
398 | DARWIN_ACE_WRITE_ATTRIBUTES;
400 rights |= DARWIN_ACE_DELETE_CHILD;
402 #ifdef HAVE_ACL_GET_PERM_NP
403 if (acl_get_perm_np(permset, ACL_EXECUTE))
405 if (acl_get_perm(permset, ACL_EXECUTE))
407 rights |= DARWIN_ACE_EXECUTE;
410 LOG(log_maxdebug, logtype_afpd, "mapped rights: 0x%08x", rights);
415 * Compile access rights for a user to one file-system object
417 * This combines combines all access rights for a user to one fs-object and
418 * returns the result as a Darwin allowed rights ACE.
419 * This must honor trivial ACEs which are a mode_t mapping.
421 * @param path (r) path to filesystem object
422 * @param sb (r) struct stat of path
423 * @param result (rw) resulting Darwin allow ACE
425 * @returns 0 or -1 on error
428 static int posix_acl_rights(const AFPObj *obj,
430 const struct stat *sb,
434 int entry_id = ACL_FIRST_ENTRY;
435 uint32_t rights = 0; /* rights which do not depend on ACL_MASK */
436 uint32_t acl_rights = 0; /* rights which are subject to limitations imposed by ACL_MASK */
437 uint32_t mask_rights = 0xffffffff;
444 EC_NULL_LOGSTR(acl = acl_get_file(path, ACL_TYPE_ACCESS),
445 "acl_get_file(\"%s\"): %s", fullpathname(path), strerror(errno));
447 /* Iterate through all ACEs. If we apply mask_rights later there is no need to iterate twice. */
448 while (acl_get_entry(acl, entry_id, &e) == 1) {
449 entry_id = ACL_NEXT_ENTRY;
450 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
454 if (sb->st_uid == obj->uid) {
455 LOG(log_maxdebug, logtype_afpd, "ACL_USER_OBJ: %u", sb->st_uid);
456 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
461 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
463 if (*uid == obj->uid) {
464 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
465 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
472 if (!(sb->st_uid == obj->uid) && gmem(sb->st_gid, obj->ngroups, obj->groups)) {
473 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
474 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
479 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
481 if (gmem(*gid, obj->ngroups, obj->groups)) {
482 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
483 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
490 mask_rights = posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
491 LOG(log_maxdebug, logtype_afpd, "maskrights: 0x%08x", mask_rights);
495 if (!(sb->st_uid == obj->uid) && !gmem(sb->st_gid, obj->ngroups, obj->groups)) {
496 LOG(log_maxdebug, logtype_afpd, "ACL_OTHER");
497 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
506 /* apply the mask and collect the rights */
507 rights |= (acl_rights & mask_rights);
512 if (acl) acl_free(acl);
513 if (uid) acl_free(uid);
514 if (gid) acl_free(gid);
519 * Helper function for posix_acls_to_uaperms() to convert Posix ACL permissions
520 * into access rights needed to fill ua_permissions of a FPUnixPrivs structure.
522 * @param entry (r) Posix ACL entry
524 * @returns access rights
526 static u_char acl_permset_to_uarights(acl_entry_t entry) {
527 acl_permset_t permset;
530 if (acl_get_permset(entry, &permset) == -1)
533 #ifdef HAVE_ACL_GET_PERM_NP
534 if (acl_get_perm_np(permset, ACL_READ))
536 if (acl_get_perm(permset, ACL_READ))
540 #ifdef HAVE_ACL_GET_PERM_NP
541 if (acl_get_perm_np(permset, ACL_WRITE))
543 if (acl_get_perm(permset, ACL_WRITE))
547 #ifdef HAVE_ACL_GET_PERM_NP
548 if (acl_get_perm_np(permset, ACL_EXECUTE))
550 if (acl_get_perm(permset, ACL_EXECUTE))
552 rights |= AR_USEARCH;
558 * Update FPUnixPrivs for a file-system object on a volume supporting ACLs
560 * Checks permissions granted by ACLS for a user to one fs-object and
561 * updates user and group permissions in given struct maccess. As OS X
562 * doesn't conform to Posix 1003.1e Draft 17 it expects proper group
563 * permissions in st_mode of struct stat even if the fs-object has an
564 * ACL_MASK entry, st_mode gets modified to properly reflect group
567 * @param path (r) path to filesystem object
568 * @param sb (rw) struct stat of path
569 * @param maccess (rw) struct maccess of path
571 * @returns 0 or -1 on error
573 static int posix_acls_to_uaperms(const AFPObj *obj, const char *path, struct stat *sb, struct maccess *ma) {
576 int entry_id = ACL_FIRST_ENTRY;
582 uid_t whoami = geteuid();
584 u_char group_rights = 0x00;
585 u_char acl_rights = 0x00;
588 EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
590 /* iterate through all ACEs */
591 while (acl_get_entry(acl, entry_id, &entry) == 1) {
592 entry_id = ACL_NEXT_ENTRY;
593 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
597 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(entry));
599 if (*uid == obj->uid && !(whoami == sb->st_uid)) {
600 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
601 acl_rights |= acl_permset_to_uarights(entry);
607 group_rights = acl_permset_to_uarights(entry);
608 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
610 if (gmem(sb->st_gid, obj->ngroups, obj->groups) && !(whoami == sb->st_uid))
611 acl_rights |= group_rights;
615 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(entry));
617 if (gmem(*gid, obj->ngroups, obj->groups) && !(whoami == sb->st_uid)) {
618 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
619 acl_rights |= acl_permset_to_uarights(entry);
625 mask = acl_permset_to_uarights(entry);
626 LOG(log_maxdebug, logtype_afpd, "ACL_MASK: 0x%02x", mask);
634 if (obj->options.flags & OPTION_ACL2MACCESS) {
635 /* apply the mask and adjust user and group permissions */
636 ma->ma_user |= (acl_rights & mask);
637 ma->ma_group = (group_rights & mask);
640 if (obj->options.flags & OPTION_ACL2MODE) {
641 /* update st_mode to properly reflect group permissions */
642 sb->st_mode &= ~S_IRWXG;
643 if (ma->ma_group & AR_USEARCH)
644 sb->st_mode |= S_IXGRP;
645 if (ma->ma_group & AR_UWRITE)
646 sb->st_mode |= S_IWGRP;
647 if (ma->ma_group & AR_UREAD)
648 sb->st_mode |= S_IRGRP;
652 if (acl) acl_free(acl);
659 * Add entries of one acl to another acl
661 * @param aclp (rw) destination acl where new aces will be added
662 * @param acl (r) source acl where aces will be copied from
664 * @returns 0 on success, -1 on error
666 static int acl_add_acl(acl_t *aclp, const acl_t acl)
672 for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
673 EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
674 EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
683 * Map Darwin ACE rights to POSIX 1e perm
685 * We can only map few rights:
686 * DARWIN_ACE_READ_DATA -> ACL_READ
687 * DARWIN_ACE_WRITE_DATA -> ACL_WRITE
688 * DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
689 * DARWIN_ACE_EXECUTE -> ACL_EXECUTE
691 * @param entry (rw) result of the mapping
692 * @param is_dir (r) 1 for dirs, 0 for files
694 * @returns mapping result as acl_perm_t, -1 on error
696 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
700 if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
703 if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (is_dir ? DARWIN_ACE_DELETE_CHILD : 0)))
706 if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
713 * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
715 * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
716 * for existing permissions for this type/id, if there is one add the perm,
717 * otherwise create a new ACL entry.
718 * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.
720 * @param aclp (rw) pointer to ACL
721 * @param type (r) acl_tag_t of ACL_USER or ACL_GROUP
722 * @param id (r) uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
723 * @param perm (r) acl_perm_t permissions to add
725 * @returns 0 on success, -1 on failure
727 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
733 int entry_id = ACL_FIRST_ENTRY;
734 acl_permset_t permset;
737 for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
738 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
739 if (tag != ACL_USER && tag != ACL_GROUP)
741 EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
742 if ((*eid == id) && (type == tag)) {
743 /* found an ACE for this type/id */
745 EC_ZERO_LOG(acl_get_permset(e, &permset));
746 EC_ZERO_LOG(acl_add_perm(permset, perm));
754 /* there was no existing ACE for this type/id */
755 EC_ZERO_LOG(acl_create_entry(aclp, &e));
756 EC_ZERO_LOG(acl_set_tag_type(e, type));
757 EC_ZERO_LOG(acl_set_qualifier(e, &id));
758 EC_ZERO_LOG(acl_get_permset(e, &permset));
759 EC_ZERO_LOG(acl_clear_perms(permset));
760 EC_ZERO_LOG(acl_add_perm(permset, perm));
761 EC_ZERO_LOG(acl_set_permset(e, permset));
765 if (eid) acl_free(eid);
771 * Map Darwin ACL to POSIX ACL.
773 * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
774 * Mapping pecularities:
775 * - we create a default ace (which inherits to files and dirs) if either
776 DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
777 * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
780 * @param darwin_aces (r) pointer to darwin_aces buffer
781 * @param def_aclp (rw) directories: pointer to an initialized acl_t with
782 the default acl files: *def_aclp will be NULL
783 * @param acc_aclp (rw) pointer to an initialized acl_t with the access acl
784 * @param ace_count (r) number of ACEs in darwin_aces buffer
785 * @param default_acl_flags (rw) flags to indicate if the object has a basic default
786 * acl or an extended default acl.
788 * @returns 0 on success storing the result in aclp, -1 on error. default_acl_flags
789 * is set to HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL in case there is at least one
790 * extended default ace. Otherwise default_acl_flags is left unchanged.
792 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
796 uint32_t *default_acl_flags)
804 uint32_t darwin_ace_flags, darwin_ace_rights;
808 for ( ; ace_count != 0; ace_count--, darwin_aces++) {
809 /* type: allow/deny, posix only has allow */
810 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
811 if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
814 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
815 perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
817 continue; /* dont add empty perm */
819 LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
820 ace_count, darwin_ace_flags, darwin_ace_rights, perm);
823 EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
826 EC_NULL_LOG(pwd = getpwnam(name));
829 LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
832 EC_NULL_LOG(grp = getgrnam(name));
834 id = (uid_t)(grp->gr_gid);
835 LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
843 if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
844 if (*def_aclp == NULL) {
845 /* ace request inheritane but we haven't got a default acl pointer */
846 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
850 /* add it as default ace */
851 EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
852 *default_acl_flags = (HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL);
854 if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
855 /* if it not a "inherit only" ace, it must be added as access aces too */
856 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
858 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
870 * Map ACEs from POSIX to Darwin.
871 * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
872 * Return number of mapped ACES, -1 on error.
874 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
878 int entry_id = ACL_FIRST_ENTRY;
883 struct passwd *pwd = NULL;
884 struct group *grp = NULL;
886 uint32_t rights, maskrights = 0;
887 darwin_ace_t *saved_darwin_aces = darwin_aces;
889 LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
890 (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
891 "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
893 /* itereate through all ACEs */
894 while (acl_get_entry(acl, entry_id, &e) == 1) {
895 entry_id = ACL_NEXT_ENTRY;
898 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
900 /* we return user and group ACE */
903 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
904 EC_NULL_LOG(pwd = getpwuid(*uid));
905 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
907 EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
913 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
914 EC_NULL_LOG(grp = getgrgid(*gid));
915 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
917 EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
923 maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
931 flags = DARWIN_ACE_FLAGS_PERMIT;
932 if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
933 flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
934 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
935 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
936 darwin_aces->darwin_ace_flags = htonl(flags);
939 rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
940 darwin_aces->darwin_ace_rights = htonl(rights);
946 /* Loop through the mapped ACE buffer once again, applying the mask */
947 for (int i = mapped_aces; i > 0; i--) {
948 saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
952 EC_STATUS(mapped_aces);
955 if (uid) acl_free(uid);
956 if (gid) acl_free(gid);
962 * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
963 * Reads from 'aces' buffer, writes to 'rbuf' buffer.
964 * Caller must provide buffer.
965 * Darwin ACEs are read and written in network byte order.
966 * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
967 * Ignores trivial ACEs.
968 * Return no of mapped ACEs or -1 on error.
970 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
974 LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
976 switch (type & MAP_MASK) {
978 #ifdef HAVE_SOLARIS_ACLS
979 case SOLARIS_2_DARWIN:
980 mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
983 case DARWIN_2_SOLARIS:
984 mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
986 #endif /* HAVE_SOLARIS_ACLS */
988 #ifdef HAVE_POSIX_ACLS
989 case POSIX_DEFAULT_2_DARWIN:
990 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
993 case POSIX_ACCESS_2_DARWIN:
994 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
997 case DARWIN_2_POSIX_DEFAULT:
1000 case DARWIN_2_POSIX_ACCESS:
1002 #endif /* HAVE_POSIX_ACLS */
1009 LOG(log_debug9, logtype_afpd, "map_acl: END");
1013 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
1014 store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
1015 Returns 0 on success, -1 on error. */
1016 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
1019 int mapped_aces = 0;
1021 char *darwin_ace_count = rbuf;
1022 #ifdef HAVE_SOLARIS_ACLS
1026 #ifdef HAVE_POSIX_ACLS
1029 LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
1031 /* Skip length and flags */
1036 #ifdef HAVE_SOLARIS_ACLS
1037 EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
1038 EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
1039 #endif /* HAVE_SOLARIS_ACLS */
1041 #ifdef HAVE_POSIX_ACLS
1042 acl_t defacl = NULL , accacl = NULL;
1044 /* stat to check if its a dir */
1045 EC_ZERO_LOG(lstat(name, &st));
1047 /* if its a dir, check for default acl too */
1049 if (S_ISDIR(st.st_mode)) {
1051 EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
1052 EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
1054 (darwin_ace_t *)rbuf,
1058 EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
1061 EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
1063 (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
1066 #endif /* HAVE_POSIX_ACLS */
1068 LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
1070 *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
1071 mapped_aces = htonl(mapped_aces);
1072 memcpy(darwin_ace_count, &mapped_aces, sizeof(uint32_t));
1077 #ifdef HAVE_SOLARIS_ACLS
1078 if (aces) free(aces);
1080 #ifdef HAVE_POSIX_ACLS
1081 if (defacl) acl_free(defacl);
1082 if (accacl) acl_free(accacl);
1083 #endif /* HAVE_POSIX_ACLS */
1085 LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
1090 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
1091 static int remove_acl(const struct vol *vol,const char *path, int dir)
1095 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
1096 /* Ressource etc. first */
1097 if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
1099 /* now the data fork or dir */
1100 ret = remove_acl_vfs(path);
1106 Set ACL. Subtleties:
1107 - the client sends a complete list of ACEs, not only new ones. So we dont need to do
1108 any combination business (one exception being 'kFileSec_Inherit': see next)
1109 - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
1110 We will store inherited ACEs first, which is Darwins canonical order.
1111 - returns AFPerror code
1113 #ifdef HAVE_SOLARIS_ACLS
1114 static int set_acl(const struct vol *vol,
1117 darwin_ace_t *daces,
1121 int i, nfsv4_ace_count;
1122 int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
1123 ace_t *old_aces, *new_aces = NULL;
1126 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
1129 /* inherited + trivial ACEs */
1130 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1132 /* only trivial ACEs */
1133 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1135 /* Get existing ACL and count ACEs which have to be copied */
1136 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
1138 for ( i=0; i < nfsv4_ace_count; i++) {
1139 if (old_aces[i].a_flags & flags)
1140 tocopy_aces_count++;
1143 /* Now malloc buffer exactly sized to fit all new ACEs */
1144 if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
1145 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
1146 EC_STATUS(AFPERR_MISC);
1150 /* Start building new ACL */
1152 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
1153 inherited ACEs first. */
1155 for (i=0; i < nfsv4_ace_count; i++) {
1156 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
1157 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1162 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
1164 /* Now the ACEs from the client */
1165 if ((ret = (map_acl(DARWIN_2_SOLARIS,
1166 &new_aces[new_aces_count],
1168 ace_count))) == -1) {
1169 EC_STATUS(AFPERR_PARAM);
1172 new_aces_count += ret;
1173 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
1175 /* Now copy the trivial ACEs */
1176 for (i=0; i < nfsv4_ace_count; i++) {
1177 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
1178 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1180 trivial_ace_count++;
1183 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
1185 /* Ressourcefork first */
1186 if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1187 LOG(log_debug, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1191 EC_STATUS(AFPERR_ACCESS);
1197 EC_STATUS(AFPERR_MISC);
1202 if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1203 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1204 if (errno == (EACCES | EPERM))
1205 EC_STATUS(AFPERR_ACCESS);
1206 else if (errno == ENOENT)
1207 EC_STATUS(AFPERR_NOITEM);
1209 EC_STATUS(AFPERR_MISC);
1216 if (old_aces) free(old_aces);
1217 if (new_aces) free(new_aces);
1219 LOG(log_debug9, logtype_afpd, "set_acl: END");
1222 #endif /* HAVE_SOLARIS_ACLS */
1224 #ifdef HAVE_POSIX_ACLS
1225 #ifndef HAVE_ACL_FROM_MODE
1226 static acl_t acl_from_mode(mode_t mode)
1230 acl_permset_t permset;
1232 if (!(acl = acl_init(3)))
1235 if (acl_create_entry(&acl, &entry) != 0)
1237 acl_set_tag_type(entry, ACL_USER_OBJ);
1238 acl_get_permset(entry, &permset);
1239 acl_clear_perms(permset);
1241 acl_add_perm(permset, ACL_READ);
1243 acl_add_perm(permset, ACL_WRITE);
1245 acl_add_perm(permset, ACL_EXECUTE);
1246 acl_set_permset(entry, permset);
1248 if (acl_create_entry(&acl, &entry) != 0)
1250 acl_set_tag_type(entry, ACL_GROUP_OBJ);
1251 acl_get_permset(entry, &permset);
1252 acl_clear_perms(permset);
1254 acl_add_perm(permset, ACL_READ);
1256 acl_add_perm(permset, ACL_WRITE);
1258 acl_add_perm(permset, ACL_EXECUTE);
1259 acl_set_permset(entry, permset);
1261 if (acl_create_entry(&acl, &entry) != 0)
1263 acl_set_tag_type(entry, ACL_OTHER);
1264 acl_get_permset(entry, &permset);
1265 acl_clear_perms(permset);
1267 acl_add_perm(permset, ACL_READ);
1269 acl_add_perm(permset, ACL_WRITE);
1271 acl_add_perm(permset, ACL_EXECUTE);
1272 acl_set_permset(entry, permset);
1282 static int set_acl(const struct vol *vol,
1285 darwin_ace_t *daces,
1290 acl_t default_acl = NULL;
1291 acl_t access_acl = NULL;
1294 int entry_id = ACL_FIRST_ENTRY;
1295 /* flags to indicate if the object has a minimal default acl and/or an extended
1298 uint32_t default_acl_flags = 0;
1300 LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1302 EC_NULL_LOG_ERR(access_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1304 /* Iterate through acl and remove all extended acl entries. */
1305 while (acl_get_entry(access_acl, entry_id, &entry) == 1) {
1306 entry_id = ACL_NEXT_ENTRY;
1307 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
1309 if ((tag == ACL_USER) || (tag == ACL_GROUP) || (tag == ACL_MASK)) {
1310 EC_ZERO_LOG_ERR(acl_delete_entry(access_acl, entry), AFPERR_MISC);
1314 /* In case we are acting on a directory prepare a default acl. For files default_acl will be NULL.
1315 * If the directory already has a default acl it will be preserved.
1317 EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1319 if (S_ISDIR(st.st_mode)) {
1320 default_acl = acl_get_file(name, ACL_TYPE_DEFAULT);
1323 /* If default_acl is not empty then the dir has a default acl. */
1324 if (acl_get_entry(default_acl, ACL_FIRST_ENTRY, &entry) == 1)
1325 default_acl_flags = HAS_DEFAULT_ACL;
1327 acl_free(default_acl);
1329 default_acl = acl_dup(access_acl);
1331 /* adds the clients aces */
1332 EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &default_acl, &access_acl, ace_count, &default_acl_flags), AFPERR_MISC);
1334 /* calcuate ACL mask */
1335 EC_ZERO_LOG_ERR(acl_calc_mask(&access_acl), AFPERR_MISC);
1338 EC_ZERO_LOG_ERR(acl_valid(access_acl), AFPERR_MISC);
1341 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, access_acl), AFPERR_MISC);
1342 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, access_acl), AFPERR_MISC);
1345 /* If the dir has an extended default acl it's ACL_MASK must be updated.*/
1346 if (default_acl_flags & HAS_EXT_DEFAULT_ACL)
1347 EC_ZERO_LOG_ERR(acl_calc_mask(&default_acl), AFPERR_MISC);
1349 if (default_acl_flags) {
1350 EC_ZERO_LOG_ERR(acl_valid(default_acl), AFPERR_MISC);
1351 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, default_acl), AFPERR_MISC);
1352 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, default_acl), AFPERR_MISC);
1357 if (access_acl) acl_free(access_acl);
1358 if (default_acl) acl_free(default_acl);
1360 LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1363 #endif /* HAVE_POSIX_ACLS */
1366 * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1368 * Note: this gets called frequently and is a good place for optimizations !
1370 * @param vol (r) volume
1371 * @param dir (rw) directory
1372 * @param path (r) path to filesystem object
1373 * @param uuid (r) UUID of user
1374 * @param requested_rights (r) requested Darwin ACE
1376 * @returns AFP result code
1378 static int check_acl_access(const AFPObj *obj,
1379 const struct vol *vol,
1383 uint32_t requested_rights)
1386 uint32_t allowed_rights = 0;
1387 char *username = NULL;
1389 bstring parent = NULL;
1392 LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1393 cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1397 /* This check is not used anymore, as OS X Server seems to be ignoring too */
1399 /* Get uid or gid from UUID */
1400 EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1405 LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1406 EC_STATUS(AFPERR_MISC);
1409 EC_STATUS(AFPERR_MISC);
1414 EC_ZERO_LOG_ERR(ostat(path, &st, vol_syml_opt(vol)), AFPERR_PARAM);
1416 is_dir = !strcmp(path, ".");
1418 if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1419 /* its a dir and the cache value is valid */
1420 allowed_rights = curdir->d_rights_cache;
1421 LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1423 #ifdef HAVE_SOLARIS_ACLS
1424 EC_ZERO_LOG(solaris_acl_rights(obj, path, &st, NULL, &allowed_rights));
1426 #ifdef HAVE_POSIX_ACLS
1427 EC_ZERO_LOG(posix_acl_rights(obj, path, &st, &allowed_rights));
1430 * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1431 * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1432 * allowed where a delete would succeed because the parent dir gives write perms.
1433 * So we check the parent dir for write access and set the right accordingly.
1434 * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1435 * there to do this extra check -- afaict.
1437 if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1439 uint32_t parent_rights = 0;
1441 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1442 /* use volume path */
1443 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1445 /* build path for parent */
1446 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1447 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1448 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1449 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1450 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1453 LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1454 EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1456 #ifdef HAVE_SOLARIS_ACLS
1457 EC_ZERO_LOG(solaris_acl_rights(obj, cfrombstr(parent), &st, NULL, &parent_rights));
1459 #ifdef HAVE_POSIX_ACLS
1460 EC_ZERO_LOG(posix_acl_rights(obj, path, &st, &parent_rights));
1462 if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1463 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1467 /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1470 if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1471 allowed_rights |= DARWIN_ACE_DELETE;
1473 curdir->d_rights_cache = allowed_rights;
1475 LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1478 if ((requested_rights & allowed_rights) != requested_rights) {
1479 LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1480 requested_rights, allowed_rights);
1481 EC_STATUS(AFPERR_ACCESS);
1483 LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1489 if (username) free(username);
1490 if (parent) bdestroy(parent);
1495 /********************************************************
1497 ********************************************************/
1499 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1504 uint32_t did, darwin_ace_rights;
1506 struct path *s_path;
1512 memcpy(&vid, ibuf, sizeof( vid ));
1513 ibuf += sizeof(vid);
1514 if (NULL == ( vol = getvolbyvid( vid ))) {
1515 LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1516 return AFPERR_NOOBJ;
1519 memcpy(&did, ibuf, sizeof( did ));
1520 ibuf += sizeof( did );
1521 if (NULL == ( dir = dirlookup( vol, did ))) {
1522 LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1529 /* Store UUID address */
1530 uuid = (uuidp_t)ibuf;
1531 ibuf += UUID_BINSIZE;
1533 /* Store ACE rights */
1534 memcpy(&darwin_ace_rights, ibuf, 4);
1535 darwin_ace_rights = ntohl(darwin_ace_rights);
1538 /* get full path and handle file/dir subtleties in netatalk code*/
1539 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1540 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1541 return AFPERR_NOOBJ;
1543 if (!s_path->st_valid)
1544 of_statdir(vol, s_path);
1545 if ( s_path->st_errno != 0 ) {
1546 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1547 return AFPERR_NOOBJ;
1550 ret = check_acl_access(obj, vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1555 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1561 uint16_t vid, bitmap;
1562 struct path *s_path;
1566 LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1570 memcpy(&vid, ibuf, sizeof( vid ));
1571 ibuf += sizeof(vid);
1572 if (NULL == ( vol = getvolbyvid( vid ))) {
1573 LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1574 return AFPERR_NOOBJ;
1577 memcpy(&did, ibuf, sizeof( did ));
1578 ibuf += sizeof( did );
1579 if (NULL == ( dir = dirlookup( vol, did ))) {
1580 LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1584 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1585 memcpy(rbuf, ibuf, sizeof( bitmap ));
1586 bitmap = ntohs( bitmap );
1587 ibuf += sizeof( bitmap );
1588 rbuf += sizeof( bitmap );
1589 *rbuflen += sizeof( bitmap );
1591 /* skip maxreplysize */
1594 /* get full path and handle file/dir subtleties in netatalk code*/
1595 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1596 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1597 return AFPERR_NOOBJ;
1599 if (!s_path->st_valid)
1600 of_statdir(vol, s_path);
1601 if ( s_path->st_errno != 0 ) {
1602 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1603 return AFPERR_NOOBJ;
1606 /* Shall we return owner UUID ? */
1607 if (bitmap & kFileSec_UUID) {
1608 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1609 if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1610 LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1611 localuuid_from_id((unsigned char *)rbuf, UUID_USER, s_path->st.st_uid);
1613 LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1614 if ((ret = getuuidfromname(pw->pw_name, UUID_USER, (unsigned char *)rbuf)) != 0)
1617 rbuf += UUID_BINSIZE;
1618 *rbuflen += UUID_BINSIZE;
1621 /* Shall we return group UUID ? */
1622 if (bitmap & kFileSec_GRPUUID) {
1623 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1624 if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1625 LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1626 localuuid_from_id((unsigned char *)rbuf, UUID_GROUP, s_path->st.st_gid);
1628 LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1629 if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, (unsigned char *)rbuf)) != 0)
1632 rbuf += UUID_BINSIZE;
1633 *rbuflen += UUID_BINSIZE;
1636 /* Shall we return ACL ? */
1637 if (bitmap & kFileSec_ACL) {
1638 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1639 if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1640 LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1641 getcwdpath(), s_path->u_name);
1646 LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1650 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1656 uint16_t vid, bitmap;
1657 struct path *s_path;
1659 LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1663 memcpy(&vid, ibuf, sizeof( vid ));
1664 ibuf += sizeof(vid);
1665 if (NULL == ( vol = getvolbyvid( vid ))) {
1666 LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1667 return AFPERR_NOOBJ;
1670 memcpy(&did, ibuf, sizeof( did ));
1671 ibuf += sizeof( did );
1672 if (NULL == ( dir = dirlookup( vol, did ))) {
1673 LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1677 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1678 bitmap = ntohs( bitmap );
1679 ibuf += sizeof( bitmap );
1681 /* get full path and handle file/dir subtleties in netatalk code*/
1682 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1683 LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1684 return AFPERR_NOOBJ;
1686 if (!s_path->st_valid)
1687 of_statdir(vol, s_path);
1688 if ( s_path->st_errno != 0 ) {
1689 LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1690 return AFPERR_NOOBJ;
1692 LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1695 if ((unsigned long)ibuf & 1)
1698 /* Start processing request */
1700 /* Change owner: dont even try */
1701 if (bitmap & kFileSec_UUID) {
1702 LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1703 ret = AFPERR_ACCESS;
1704 ibuf += UUID_BINSIZE;
1707 /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1708 if (bitmap & kFileSec_UUID) {
1709 LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1711 ibuf += UUID_BINSIZE;
1715 if (bitmap & kFileSec_REMOVEACL) {
1716 LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1717 if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1718 LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1722 if (bitmap & kFileSec_ACL) {
1723 LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1724 /* Get no of ACEs the client put on the wire */
1726 memcpy(&ace_count, ibuf, sizeof(uint32_t));
1727 ace_count = htonl(ace_count);
1728 ibuf += 8; /* skip ACL flags (see acls.h) */
1732 (bitmap & kFileSec_Inherit),
1733 (darwin_ace_t *)ibuf,
1738 LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1739 getcwdpath(), s_path->u_name);
1744 LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1748 /********************************************************************
1749 * ACL funcs interfacing with other parts
1750 ********************************************************************/
1753 * map ACL to user maccess
1755 * This is the magic function that makes ACLs usable by calculating
1756 * the access granted by ACEs to the logged in user.
1758 int acltoownermode(const AFPObj *obj, const struct vol *vol, char *path, struct stat *st, struct maccess *ma)
1762 if ( ! (obj->options.flags & (OPTION_ACL2MACCESS | OPTION_ACL2MODE))
1763 || ! (vol->v_flags & AFPVOL_ACLS))
1766 LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
1767 getcwdpath(), path, ma->ma_user);
1769 #ifdef HAVE_SOLARIS_ACLS
1770 EC_ZERO_LOG(solaris_acl_rights(obj, path, st, ma, NULL));
1773 #ifdef HAVE_POSIX_ACLS
1774 EC_ZERO_LOG(posix_acls_to_uaperms(obj, path, st, ma));
1777 LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x group maccess: 0x%02x", ma->ma_user, ma->ma_group);