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>
49 #include "directory.h"
55 #include "acl_mappings.h"
59 #define SOLARIS_2_DARWIN 1
60 #define DARWIN_2_SOLARIS 2
61 #define POSIX_DEFAULT_2_DARWIN 3
62 #define POSIX_ACCESS_2_DARWIN 4
63 #define DARWIN_2_POSIX_DEFAULT 5
64 #define DARWIN_2_POSIX_ACCESS 6
69 /* bit flags for set_acl() and map_aces_darwin_to_posix() */
70 #define HAS_DEFAULT_ACL 0x01
71 #define HAS_EXT_DEFAULT_ACL 0x02
73 /********************************************************
75 ********************************************************/
77 #ifdef HAVE_SOLARIS_ACLS
80 * Compile access rights for a user to one file-system object
82 * This combines combines all access rights for a user to one fs-object and
83 * returns the result as a Darwin allowed rights ACE.
84 * This must honor trivial ACEs which are a mode_t mapping.
86 * @param path (r) path to filesystem object
87 * @param sb (r) struct stat of path
88 * @param result (w) resulting Darwin allow ACE
90 * @returns 0 or -1 on error
92 static int solaris_acl_rights(const char *path,
93 const struct stat *sb,
97 int i, ace_count, checkgroup;
100 uint16_t flags, type;
101 uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
103 /* Get ACL from file/dir */
104 EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
109 /* Now check requested rights */
111 do { /* Loop through ACEs */
113 flags = aces[i].a_flags;
114 type = aces[i].a_type;
115 rights = aces[i].a_access_mask;
117 if (flags & ACE_INHERIT_ONLY_ACE)
120 /* Now the tricky part: decide if ACE effects our user. I'll explain:
121 if its a dedicated (non trivial) ACE for the user
123 if its a ACE for a group we're member of
125 if its a trivial ACE_OWNER ACE and requested UUID is the owner
127 if its a trivial ACE_GROUP ACE and requested UUID is group
129 if its a trivial ACE_EVERYONE ACE
132 if (((who == uuid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
134 ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who))
136 ((flags & ACE_OWNER) && (uuid == sb->st_uid))
138 ((flags & ACE_GROUP) && !(uuid == sb->st_uid) && gmem(sb->st_gid))
140 (flags & ACE_EVERYONE && !(uuid == sb->st_uid) && !gmem(sb->st_gid))
142 /* Found an applicable ACE */
143 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
144 allowed_rights |= rights;
145 else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
146 /* Only or to denied rights if not previously allowed !! */
147 denied_rights |= ((!allowed_rights) & rights);
149 } while (++i < ace_count);
152 /* Darwin likes to ask for "delete_child" on dir,
153 "write_data" is actually the same, so we add that for dirs */
154 if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
155 allowed_rights |= ACE_DELETE_CHILD;
157 /* Remove denied from allowed rights */
158 allowed_rights &= ~denied_rights;
162 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
163 if (allowed_rights & nfsv4_to_darwin_rights[i].from)
164 darwin_rights |= nfsv4_to_darwin_rights[i].to;
167 *result |= darwin_rights;
170 if (aces) free(aces);
176 Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
177 Return numer of mapped ACEs or -1 on error.
178 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
180 static int map_aces_solaris_to_darwin(const ace_t *aces,
181 darwin_ace_t *darwin_aces,
188 struct passwd *pwd = NULL;
189 struct group *grp = NULL;
191 LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
194 LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
195 /* if its a ACE resulting from nfsv4 mode mapping, discard it */
196 if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
197 LOG(log_debug, logtype_afpd, "trivial ACE");
202 if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
203 LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
204 EC_NULL_LOG(pwd = getpwuid(aces->a_who));
205 LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
206 EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
208 darwin_aces->darwin_ace_uuid));
209 } else { /* group ace */
210 LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
211 EC_NULL_LOG(grp = getgrgid(aces->a_who));
212 LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
213 EC_ZERO_LOG(getuuidfromname(grp->gr_name,
215 darwin_aces->darwin_ace_uuid));
219 if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
220 flags = DARWIN_ACE_FLAGS_PERMIT;
221 else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
222 flags = DARWIN_ACE_FLAGS_DENY;
223 else { /* unsupported type */
227 for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
228 if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
229 flags |= nfsv4_to_darwin_flags[i].to;
231 darwin_aces->darwin_ace_flags = htonl(flags);
235 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
236 if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
237 rights |= nfsv4_to_darwin_rights[i].to;
239 darwin_aces->darwin_ace_rights = htonl(rights);
252 Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
253 Return numer of mapped ACEs or -1 on error.
254 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
256 static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
261 int i, mapped_aces = 0;
262 uint32_t darwin_ace_flags;
263 uint32_t darwin_ace_rights;
264 uint16_t nfsv4_ace_flags;
265 uint32_t nfsv4_ace_rights;
273 nfsv4_ace_rights = 0;
276 EC_ZERO(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
279 EC_NULL_LOG(pwd = getpwnam(name));
280 nfsv4_aces->a_who = pwd->pw_uid;
283 EC_NULL_LOG(grp = getgrnam(name));
284 nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
285 nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
288 LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: unkown uuidtype");
294 /* now type: allow/deny */
295 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
296 if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
297 nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
298 else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
299 nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
300 else { /* unsupported type */
305 for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
306 if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
307 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
311 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
312 for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
313 if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
314 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
317 LOG(log_debug9, logtype_afpd,
318 "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x",
319 darwin_ace_flags, nfsv4_ace_flags);
320 LOG(log_debug9, logtype_afpd,
321 "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x",
322 darwin_ace_rights, nfsv4_ace_rights);
324 nfsv4_aces->a_flags = nfsv4_ace_flags;
325 nfsv4_aces->a_access_mask = nfsv4_ace_rights;
338 #endif /* HAVE_SOLARIS_ACLS */
340 /********************************************************
342 ********************************************************/
344 #ifdef HAVE_POSIX_ACLS
346 static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
350 acl_permset_t permset;
352 EC_ZERO_LOG(acl_get_permset(e, &permset));
354 #ifdef HAVE_ACL_GET_PERM_NP
355 if (acl_get_perm_np(permset, ACL_READ))
357 if (acl_get_perm(permset, ACL_READ))
359 rights = DARWIN_ACE_READ_DATA
360 | DARWIN_ACE_READ_EXTATTRIBUTES
361 | DARWIN_ACE_READ_ATTRIBUTES
362 | DARWIN_ACE_READ_SECURITY;
363 #ifdef HAVE_ACL_GET_PERM_NP
364 if (acl_get_perm_np(permset, ACL_WRITE)) {
366 if (acl_get_perm(permset, ACL_WRITE)) {
368 rights |= DARWIN_ACE_WRITE_DATA
369 | DARWIN_ACE_APPEND_DATA
370 | DARWIN_ACE_WRITE_EXTATTRIBUTES
371 | DARWIN_ACE_WRITE_ATTRIBUTES;
373 rights |= DARWIN_ACE_DELETE_CHILD;
375 #ifdef HAVE_ACL_GET_PERM_NP
376 if (acl_get_perm_np(permset, ACL_EXECUTE))
378 if (acl_get_perm(permset, ACL_EXECUTE))
380 rights |= DARWIN_ACE_EXECUTE;
383 LOG(log_maxdebug, logtype_afpd, "mapped rights: 0x%08x", rights);
388 * Compile access rights for a user to one file-system object
390 * This combines combines all access rights for a user to one fs-object and
391 * returns the result as a Darwin allowed rights ACE.
392 * This must honor trivial ACEs which are a mode_t mapping.
394 * @param path (r) path to filesystem object
395 * @param sb (r) struct stat of path
396 * @param result (rw) resulting Darwin allow ACE
398 * @returns 0 or -1 on error
400 static int posix_acl_rights(const char *path,
401 const struct stat *sb,
405 int entry_id = ACL_FIRST_ENTRY;
406 uint32_t rights = 0; /* rights which do not depend on ACL_MASK */
407 uint32_t acl_rights = 0; /* rights which are subject to limitations imposed by ACL_MASK */
408 uint32_t mask_rights = 0xffffffff;
415 EC_NULL_LOGSTR(acl = acl_get_file(path, ACL_TYPE_ACCESS),
416 "acl_get_file(\"%s\"): %s", fullpathname(path), strerror(errno));
418 /* Iterate through all ACEs. If we apply mask_rights later there is no need to iterate twice. */
419 while (acl_get_entry(acl, entry_id, &e) == 1) {
420 entry_id = ACL_NEXT_ENTRY;
421 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
425 if (sb->st_uid == uuid) {
426 LOG(log_maxdebug, logtype_afpd, "ACL_USER_OBJ: %u", sb->st_uid);
427 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
432 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
435 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
436 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
443 if (!(sb->st_uid == uuid) && gmem(sb->st_gid)) {
444 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
445 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
450 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
453 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
454 acl_rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
461 mask_rights = posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
462 LOG(log_maxdebug, logtype_afpd, "maskrights: 0x%08x", mask_rights);
466 if (!(sb->st_uid == uuid) && !gmem(sb->st_gid)) {
467 LOG(log_maxdebug, logtype_afpd, "ACL_OTHER");
468 rights |= posix_permset_to_darwin_rights(e, S_ISDIR(sb->st_mode));
477 /* apply the mask and collect the rights */
478 rights |= (acl_rights & mask_rights);
483 if (acl) acl_free(acl);
484 if (uid) acl_free(uid);
485 if (gid) acl_free(gid);
490 * Helper function for posix_acls_to_uaperms() to convert Posix ACL permissions
491 * into access rights needed to fill ua_permissions of a FPUnixPrivs structure.
493 * @param entry (r) Posix ACL entry
495 * @returns access rights
497 static u_char acl_permset_to_uarights(acl_entry_t entry) {
498 acl_permset_t permset;
501 if (acl_get_permset(entry, &permset) == -1)
504 #ifdef HAVE_ACL_GET_PERM_NP
505 if (acl_get_perm_np(permset, ACL_READ))
507 if (acl_get_perm(permset, ACL_READ))
511 #ifdef HAVE_ACL_GET_PERM_NP
512 if (acl_get_perm_np(permset, ACL_WRITE))
514 if (acl_get_perm(permset, ACL_WRITE))
518 #ifdef HAVE_ACL_GET_PERM_NP
519 if (acl_get_perm_np(permset, ACL_EXECUTE))
521 if (acl_get_perm(permset, ACL_EXECUTE))
523 rights |= AR_USEARCH;
529 * Update FPUnixPrivs for a file-system object on a volume supporting ACLs
531 * Checks permissions granted by ACLS for a user to one fs-object and
532 * updates user and group permissions in given struct maccess. As OS X
533 * doesn't conform to Posix 1003.1e Draft 17 it expects proper group
534 * permissions in st_mode of struct stat even if the fs-object has an
535 * ACL_MASK entry, st_mode gets modified to properly reflect group
538 * @param path (r) path to filesystem object
539 * @param sb (rw) struct stat of path
540 * @param maccess (rw) struct maccess of path
542 * @returns 0 or -1 on error
544 static int posix_acls_to_uaperms(const char *path, struct stat *sb, struct maccess *ma) {
547 int entry_id = ACL_FIRST_ENTRY;
553 uid_t whoami = geteuid();
555 u_char group_rights = 0x00;
556 u_char acl_rights = 0x00;
559 EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
561 /* iterate through all ACEs */
562 while (acl_get_entry(acl, entry_id, &entry) == 1) {
563 entry_id = ACL_NEXT_ENTRY;
564 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
568 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(entry));
570 if (*uid == uuid && !(whoami == sb->st_uid)) {
571 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
572 acl_rights |= acl_permset_to_uarights(entry);
578 group_rights = acl_permset_to_uarights(entry);
579 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
581 if (gmem(sb->st_gid) && !(whoami == sb->st_uid))
582 acl_rights |= group_rights;
586 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(entry));
588 if (gmem(*gid) && !(whoami == sb->st_uid)) {
589 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
590 acl_rights |= acl_permset_to_uarights(entry);
596 mask = acl_permset_to_uarights(entry);
597 LOG(log_maxdebug, logtype_afpd, "ACL_MASK: 0x%02x", mask);
604 /* apply the mask and adjust user and group permissions */
605 ma->ma_user |= (acl_rights & mask);
606 ma->ma_group = (group_rights & mask);
608 /* update st_mode to properly reflect group permissions */
609 sb->st_mode &= ~S_IRWXG;
611 if (ma->ma_group & AR_USEARCH)
612 sb->st_mode |= S_IXGRP;
614 if (ma->ma_group & AR_UWRITE)
615 sb->st_mode |= S_IWGRP;
617 if (ma->ma_group & AR_UREAD)
618 sb->st_mode |= S_IRGRP;
621 if (acl) acl_free(acl);
627 * Add entries of one acl to another acl
629 * @param aclp (rw) destination acl where new aces will be added
630 * @param acl (r) source acl where aces will be copied from
632 * @returns 0 on success, -1 on error
634 static int acl_add_acl(acl_t *aclp, const acl_t acl)
640 for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
641 EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
642 EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
650 * Map Darwin ACE rights to POSIX 1e perm
652 * We can only map few rights:
653 * DARWIN_ACE_READ_DATA -> ACL_READ
654 * DARWIN_ACE_WRITE_DATA -> ACL_WRITE
655 * DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
656 * DARWIN_ACE_EXECUTE -> ACL_EXECUTE
658 * @param entry (rw) result of the mapping
659 * @param is_dir (r) 1 for dirs, 0 for files
661 * @returns mapping result as acl_perm_t, -1 on error
663 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
667 if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
670 if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (is_dir ? DARWIN_ACE_DELETE_CHILD : 0)))
673 if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
680 * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
682 * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
683 * for existing permissions for this type/id, if there is one add the perm,
684 * otherwise create a new ACL entry.
685 * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.
687 * @param aclp (rw) pointer to ACL
688 * @param type (r) acl_tag_t of ACL_USER or ACL_GROUP
689 * @param id (r) uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
690 * @param perm (r) acl_perm_t permissions to add
692 * @returns 0 on success, -1 on failure
694 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
700 int entry_id = ACL_FIRST_ENTRY;
701 acl_permset_t permset;
704 for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
705 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
706 if (tag != ACL_USER && tag != ACL_GROUP)
708 EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
709 if ((*eid == id) && (type == tag)) {
710 /* found an ACE for this type/id */
712 EC_ZERO_LOG(acl_get_permset(e, &permset));
713 EC_ZERO_LOG(acl_add_perm(permset, perm));
721 /* there was no existing ACE for this type/id */
722 EC_ZERO_LOG(acl_create_entry(aclp, &e));
723 EC_ZERO_LOG(acl_set_tag_type(e, type));
724 EC_ZERO_LOG(acl_set_qualifier(e, &id));
725 EC_ZERO_LOG(acl_get_permset(e, &permset));
726 EC_ZERO_LOG(acl_clear_perms(permset));
727 EC_ZERO_LOG(acl_add_perm(permset, perm));
728 EC_ZERO_LOG(acl_set_permset(e, permset));
732 if (eid) acl_free(eid);
738 * Map Darwin ACL to POSIX ACL.
740 * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
741 * Mapping pecularities:
742 * - we create a default ace (which inherits to files and dirs) if either
743 DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
744 * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
747 * @param darwin_aces (r) pointer to darwin_aces buffer
748 * @param def_aclp (rw) directories: pointer to an initialized acl_t with
749 the default acl files: *def_aclp will be NULL
750 * @param acc_aclp (rw) pointer to an initialized acl_t with the access acl
751 * @param ace_count (r) number of ACEs in darwin_aces buffer
752 * @param default_acl_flags (rw) flags to indicate if the object has a basic default
753 * acl or an extended default acl.
755 * @returns 0 on success storing the result in aclp, -1 on error. default_acl_flags
756 * is set to HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL in case there is at least one
757 * extended default ace. Otherwise default_acl_flags is left unchanged.
759 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
763 uint32_t *default_acl_flags)
771 uint32_t darwin_ace_flags, darwin_ace_rights;
775 for ( ; ace_count != 0; ace_count--, darwin_aces++) {
776 /* type: allow/deny, posix only has allow */
777 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
778 if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
781 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
782 perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
784 continue; /* dont add empty perm */
786 LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
787 ace_count, darwin_ace_flags, darwin_ace_rights, perm);
790 EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
793 EC_NULL_LOG(pwd = getpwnam(name));
796 LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
799 EC_NULL_LOG(grp = getgrnam(name));
801 id = (uid_t)(grp->gr_gid);
802 LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
810 if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
811 if (*def_aclp == NULL) {
812 /* ace request inheritane but we haven't got a default acl pointer */
813 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
817 /* add it as default ace */
818 EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
819 *default_acl_flags = (HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL);
821 if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
822 /* if it not a "inherit only" ace, it must be added as access aces too */
823 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
825 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
837 * Map ACEs from POSIX to Darwin.
838 * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
839 * Return number of mapped ACES, -1 on error.
841 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
845 int entry_id = ACL_FIRST_ENTRY;
850 struct passwd *pwd = NULL;
851 struct group *grp = NULL;
853 uint32_t rights, maskrights = 0;
854 darwin_ace_t *saved_darwin_aces = darwin_aces;
856 LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
857 (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
858 "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
860 /* itereate through all ACEs */
861 while (acl_get_entry(acl, entry_id, &e) == 1) {
862 entry_id = ACL_NEXT_ENTRY;
865 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
867 /* we return user and group ACE */
870 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
871 EC_NULL_LOG(pwd = getpwuid(*uid));
872 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
874 EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
880 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
881 EC_NULL_LOG(grp = getgrgid(*gid));
882 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
884 EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
890 maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
898 flags = DARWIN_ACE_FLAGS_PERMIT;
899 if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
900 flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
901 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
902 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
903 darwin_aces->darwin_ace_flags = htonl(flags);
906 rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
907 darwin_aces->darwin_ace_rights = htonl(rights);
913 /* Loop through the mapped ACE buffer once again, applying the mask */
914 for (int i = mapped_aces; i > 0; i--) {
915 saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
919 EC_STATUS(mapped_aces);
922 if (uid) acl_free(uid);
923 if (gid) acl_free(gid);
929 * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
930 * Reads from 'aces' buffer, writes to 'rbuf' buffer.
931 * Caller must provide buffer.
932 * Darwin ACEs are read and written in network byte order.
933 * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
934 * Ignores trivial ACEs.
935 * Return no of mapped ACEs or -1 on error.
937 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
941 LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
943 switch (type & MAP_MASK) {
945 #ifdef HAVE_SOLARIS_ACLS
946 case SOLARIS_2_DARWIN:
947 mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
950 case DARWIN_2_SOLARIS:
951 mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
953 #endif /* HAVE_SOLARIS_ACLS */
955 #ifdef HAVE_POSIX_ACLS
956 case POSIX_DEFAULT_2_DARWIN:
957 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
960 case POSIX_ACCESS_2_DARWIN:
961 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
964 case DARWIN_2_POSIX_DEFAULT:
967 case DARWIN_2_POSIX_ACCESS:
969 #endif /* HAVE_POSIX_ACLS */
976 LOG(log_debug9, logtype_afpd, "map_acl: END");
980 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
981 store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
982 Returns 0 on success, -1 on error. */
983 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
988 uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
989 #ifdef HAVE_SOLARIS_ACLS
993 #ifdef HAVE_POSIX_ACLS
996 LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
998 /* Skip length and flags */
1003 #ifdef HAVE_SOLARIS_ACLS
1004 EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
1005 EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
1006 #endif /* HAVE_SOLARIS_ACLS */
1008 #ifdef HAVE_POSIX_ACLS
1009 acl_t defacl = NULL , accacl = NULL;
1011 /* stat to check if its a dir */
1012 EC_ZERO_LOG(lstat(name, &st));
1014 /* if its a dir, check for default acl too */
1016 if (S_ISDIR(st.st_mode)) {
1018 EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
1019 EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
1021 (darwin_ace_t *)rbuf,
1025 EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
1028 EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
1030 (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
1033 #endif /* HAVE_POSIX_ACLS */
1035 LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
1037 *darwin_ace_count = htonl(mapped_aces);
1038 *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
1043 #ifdef HAVE_SOLARIS_ACLS
1044 if (aces) free(aces);
1046 #ifdef HAVE_POSIX_ACLS
1047 if (defacl) acl_free(defacl);
1048 if (accacl) acl_free(accacl);
1049 #endif /* HAVE_POSIX_ACLS */
1051 LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
1056 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
1057 static int remove_acl(const struct vol *vol,const char *path, int dir)
1061 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
1062 /* Ressource etc. first */
1063 if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
1065 /* now the data fork or dir */
1066 ret = remove_acl_vfs(path);
1072 Set ACL. Subtleties:
1073 - the client sends a complete list of ACEs, not only new ones. So we dont need to do
1074 any combination business (one exception being 'kFileSec_Inherit': see next)
1075 - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
1076 We will store inherited ACEs first, which is Darwins canonical order.
1077 - returns AFPerror code
1079 #ifdef HAVE_SOLARIS_ACLS
1080 static int set_acl(const struct vol *vol,
1083 darwin_ace_t *daces,
1087 int i, nfsv4_ace_count;
1088 int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
1089 ace_t *old_aces, *new_aces = NULL;
1092 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
1095 /* inherited + trivial ACEs */
1096 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1098 /* only trivial ACEs */
1099 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1101 /* Get existing ACL and count ACEs which have to be copied */
1102 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
1104 for ( i=0; i < nfsv4_ace_count; i++) {
1105 if (old_aces[i].a_flags & flags)
1106 tocopy_aces_count++;
1109 /* Now malloc buffer exactly sized to fit all new ACEs */
1110 if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
1111 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
1112 EC_STATUS(AFPERR_MISC);
1116 /* Start building new ACL */
1118 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
1119 inherited ACEs first. */
1121 for (i=0; i < nfsv4_ace_count; i++) {
1122 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
1123 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1128 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
1130 /* Now the ACEs from the client */
1131 if ((ret = (map_acl(DARWIN_2_SOLARIS,
1132 &new_aces[new_aces_count],
1134 ace_count))) == -1) {
1135 EC_STATUS(AFPERR_PARAM);
1138 new_aces_count += ret;
1139 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
1141 /* Now copy the trivial ACEs */
1142 for (i=0; i < nfsv4_ace_count; i++) {
1143 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
1144 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1146 trivial_ace_count++;
1149 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
1151 /* Ressourcefork first.
1152 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
1153 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
1154 if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1155 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1156 if (errno == (EACCES | EPERM))
1157 EC_STATUS(AFPERR_ACCESS);
1158 else if (errno == ENOENT)
1159 EC_STATUS(AFPERR_NOITEM);
1161 EC_STATUS(AFPERR_MISC);
1164 if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1165 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1166 if (errno == (EACCES | EPERM))
1167 EC_STATUS(AFPERR_ACCESS);
1168 else if (errno == ENOENT)
1169 EC_STATUS(AFPERR_NOITEM);
1171 EC_STATUS(AFPERR_MISC);
1178 if (old_aces) free(old_aces);
1179 if (new_aces) free(new_aces);
1181 LOG(log_debug9, logtype_afpd, "set_acl: END");
1184 #endif /* HAVE_SOLARIS_ACLS */
1186 #ifdef HAVE_POSIX_ACLS
1187 #ifndef HAVE_ACL_FROM_MODE
1188 static acl_t acl_from_mode(mode_t mode)
1192 acl_permset_t permset;
1194 if (!(acl = acl_init(3)))
1197 if (acl_create_entry(&acl, &entry) != 0)
1199 acl_set_tag_type(entry, ACL_USER_OBJ);
1200 acl_get_permset(entry, &permset);
1201 acl_clear_perms(permset);
1203 acl_add_perm(permset, ACL_READ);
1205 acl_add_perm(permset, ACL_WRITE);
1207 acl_add_perm(permset, ACL_EXECUTE);
1208 acl_set_permset(entry, permset);
1210 if (acl_create_entry(&acl, &entry) != 0)
1212 acl_set_tag_type(entry, ACL_GROUP_OBJ);
1213 acl_get_permset(entry, &permset);
1214 acl_clear_perms(permset);
1216 acl_add_perm(permset, ACL_READ);
1218 acl_add_perm(permset, ACL_WRITE);
1220 acl_add_perm(permset, ACL_EXECUTE);
1221 acl_set_permset(entry, permset);
1223 if (acl_create_entry(&acl, &entry) != 0)
1225 acl_set_tag_type(entry, ACL_OTHER);
1226 acl_get_permset(entry, &permset);
1227 acl_clear_perms(permset);
1229 acl_add_perm(permset, ACL_READ);
1231 acl_add_perm(permset, ACL_WRITE);
1233 acl_add_perm(permset, ACL_EXECUTE);
1234 acl_set_permset(entry, permset);
1244 static int set_acl(const struct vol *vol,
1247 darwin_ace_t *daces,
1252 acl_t default_acl = NULL;
1253 acl_t access_acl = NULL;
1256 int entry_id = ACL_FIRST_ENTRY;
1257 int has_def_acl = 0;
1258 /* flags to indicate if the object has a minimal default acl and/or an extended
1261 uint32_t default_acl_flags = 0;
1263 LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1265 EC_NULL_LOG_ERR(access_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1267 /* Iterate through acl and remove all extended acl entries. */
1268 while (acl_get_entry(access_acl, entry_id, &entry) == 1) {
1269 entry_id = ACL_NEXT_ENTRY;
1270 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
1272 if ((tag == ACL_USER) || (tag == ACL_GROUP) || (tag == ACL_MASK)) {
1273 EC_ZERO_LOG_ERR(acl_delete_entry(access_acl, entry), AFPERR_MISC);
1277 /* In case we are acting on a directory prepare a default acl. For files default_acl will be NULL.
1278 * If the directory already has a default acl it will be preserved.
1280 EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1282 if (S_ISDIR(st.st_mode)) {
1283 default_acl = acl_get_file(name, ACL_TYPE_DEFAULT);
1286 /* If default_acl is not empty then the dir has a default acl. */
1287 if (acl_get_entry(default_acl, ACL_FIRST_ENTRY, &entry) == 1)
1288 default_acl_flags = HAS_DEFAULT_ACL;
1290 acl_free(default_acl);
1292 default_acl = acl_dup(access_acl);
1294 /* adds the clients aces */
1295 EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &default_acl, &access_acl, ace_count, &default_acl_flags), AFPERR_MISC);
1297 /* calcuate ACL mask */
1298 EC_ZERO_LOG_ERR(acl_calc_mask(&access_acl), AFPERR_MISC);
1301 EC_ZERO_LOG_ERR(acl_valid(access_acl), AFPERR_MISC);
1304 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, access_acl), AFPERR_MISC);
1305 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, access_acl), AFPERR_MISC);
1308 /* If the dir has an extended default acl it's ACL_MASK must be updated.*/
1309 if (default_acl_flags & HAS_EXT_DEFAULT_ACL)
1310 EC_ZERO_LOG_ERR(acl_calc_mask(&default_acl), AFPERR_MISC);
1312 if (default_acl_flags) {
1313 EC_ZERO_LOG_ERR(acl_valid(default_acl), AFPERR_MISC);
1314 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, default_acl), AFPERR_MISC);
1315 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, default_acl), AFPERR_MISC);
1320 if (access_acl) acl_free(access_acl);
1321 if (default_acl) acl_free(default_acl);
1323 LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1326 #endif /* HAVE_POSIX_ACLS */
1329 * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1331 * Note: this gets called frequently and is a good place for optimizations !
1333 * @param vol (r) volume
1334 * @param dir (rw) directory
1335 * @param path (r) path to filesystem object
1336 * @param uuid (r) UUID of user
1337 * @param requested_rights (r) requested Darwin ACE
1339 * @returns AFP result code
1341 static int check_acl_access(const struct vol *vol,
1345 uint32_t requested_rights)
1348 uint32_t allowed_rights = 0;
1349 char *username = NULL;
1350 uuidtype_t uuidtype;
1352 bstring parent = NULL;
1355 LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1356 cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1358 /* This check is not used anymore, as OS X Server seems to be ignoring too */
1360 /* Get uid or gid from UUID */
1361 EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1366 LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1367 EC_STATUS(AFPERR_MISC);
1370 EC_STATUS(AFPERR_MISC);
1375 EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
1377 is_dir = !strcmp(path, ".");
1379 if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1380 /* its a dir and the cache value is valid */
1381 allowed_rights = curdir->d_rights_cache;
1382 LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1384 #ifdef HAVE_SOLARIS_ACLS
1385 EC_ZERO_LOG(solaris_acl_rights(path, &st, &allowed_rights));
1387 #ifdef HAVE_POSIX_ACLS
1388 EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
1391 * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1392 * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1393 * allowed where a delete would succeed because the parent dir gives write perms.
1394 * So we check the parent dir for write access and set the right accordingly.
1395 * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1396 * there to do this extra check -- afaict.
1398 if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1400 uint32_t parent_rights = 0;
1402 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1403 /* use volume path */
1404 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1406 /* build path for parent */
1407 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1408 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1409 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1410 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1411 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1414 LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1415 EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1417 #ifdef HAVE_SOLARIS_ACLS
1418 EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, &parent_rights));
1420 #ifdef HAVE_POSIX_ACLS
1421 EC_ZERO_LOG(posix_acl_rights(path, &st, &parent_rights));
1423 if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1424 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1428 /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1431 if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1432 allowed_rights |= DARWIN_ACE_DELETE;
1434 curdir->d_rights_cache = allowed_rights;
1436 LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1439 if ((requested_rights & allowed_rights) != requested_rights) {
1440 LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1441 requested_rights, allowed_rights);
1442 EC_STATUS(AFPERR_ACCESS);
1444 LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1450 if (username) free(username);
1451 if (parent) bdestroy(parent);
1456 /********************************************************
1458 ********************************************************/
1460 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1465 uint32_t did, darwin_ace_rights;
1467 struct path *s_path;
1473 memcpy(&vid, ibuf, sizeof( vid ));
1474 ibuf += sizeof(vid);
1475 if (NULL == ( vol = getvolbyvid( vid ))) {
1476 LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1477 return AFPERR_NOOBJ;
1480 memcpy(&did, ibuf, sizeof( did ));
1481 ibuf += sizeof( did );
1482 if (NULL == ( dir = dirlookup( vol, did ))) {
1483 LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1490 /* Store UUID address */
1491 uuid = (uuidp_t)ibuf;
1492 ibuf += UUID_BINSIZE;
1494 /* Store ACE rights */
1495 memcpy(&darwin_ace_rights, ibuf, 4);
1496 darwin_ace_rights = ntohl(darwin_ace_rights);
1499 /* get full path and handle file/dir subtleties in netatalk code*/
1500 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1501 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1502 return AFPERR_NOOBJ;
1504 if (!s_path->st_valid)
1505 of_statdir(vol, s_path);
1506 if ( s_path->st_errno != 0 ) {
1507 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1508 return AFPERR_NOOBJ;
1511 ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1516 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1522 uint16_t vid, bitmap;
1523 struct path *s_path;
1527 LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1531 memcpy(&vid, ibuf, sizeof( vid ));
1532 ibuf += sizeof(vid);
1533 if (NULL == ( vol = getvolbyvid( vid ))) {
1534 LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1535 return AFPERR_NOOBJ;
1538 memcpy(&did, ibuf, sizeof( did ));
1539 ibuf += sizeof( did );
1540 if (NULL == ( dir = dirlookup( vol, did ))) {
1541 LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1545 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1546 memcpy(rbuf, ibuf, sizeof( bitmap ));
1547 bitmap = ntohs( bitmap );
1548 ibuf += sizeof( bitmap );
1549 rbuf += sizeof( bitmap );
1550 *rbuflen += sizeof( bitmap );
1552 /* skip maxreplysize */
1555 /* get full path and handle file/dir subtleties in netatalk code*/
1556 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1557 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1558 return AFPERR_NOOBJ;
1560 if (!s_path->st_valid)
1561 of_statdir(vol, s_path);
1562 if ( s_path->st_errno != 0 ) {
1563 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1564 return AFPERR_NOOBJ;
1567 /* Shall we return owner UUID ? */
1568 if (bitmap & kFileSec_UUID) {
1569 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1570 if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1571 LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1572 localuuid_from_id(rbuf, UUID_USER, s_path->st.st_uid);
1574 LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1575 if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1578 rbuf += UUID_BINSIZE;
1579 *rbuflen += UUID_BINSIZE;
1582 /* Shall we return group UUID ? */
1583 if (bitmap & kFileSec_GRPUUID) {
1584 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1585 if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1586 LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1587 localuuid_from_id(rbuf, UUID_GROUP, s_path->st.st_gid);
1589 LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1590 if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1593 rbuf += UUID_BINSIZE;
1594 *rbuflen += UUID_BINSIZE;
1597 /* Shall we return ACL ? */
1598 if (bitmap & kFileSec_ACL) {
1599 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1600 if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1601 LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1602 getcwdpath(), s_path->u_name);
1607 LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1611 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1617 uint16_t vid, bitmap;
1618 struct path *s_path;
1620 LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1624 memcpy(&vid, ibuf, sizeof( vid ));
1625 ibuf += sizeof(vid);
1626 if (NULL == ( vol = getvolbyvid( vid ))) {
1627 LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1628 return AFPERR_NOOBJ;
1631 memcpy(&did, ibuf, sizeof( did ));
1632 ibuf += sizeof( did );
1633 if (NULL == ( dir = dirlookup( vol, did ))) {
1634 LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1638 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1639 bitmap = ntohs( bitmap );
1640 ibuf += sizeof( bitmap );
1642 /* get full path and handle file/dir subtleties in netatalk code*/
1643 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1644 LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1645 return AFPERR_NOOBJ;
1647 if (!s_path->st_valid)
1648 of_statdir(vol, s_path);
1649 if ( s_path->st_errno != 0 ) {
1650 LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1651 return AFPERR_NOOBJ;
1653 LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1656 if ((unsigned long)ibuf & 1)
1659 /* Start processing request */
1661 /* Change owner: dont even try */
1662 if (bitmap & kFileSec_UUID) {
1663 LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1664 ret = AFPERR_ACCESS;
1665 ibuf += UUID_BINSIZE;
1668 /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1669 if (bitmap & kFileSec_UUID) {
1670 LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1672 ibuf += UUID_BINSIZE;
1676 if (bitmap & kFileSec_REMOVEACL) {
1677 LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1678 if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1679 LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1683 if (bitmap & kFileSec_ACL) {
1684 LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1685 /* Get no of ACEs the client put on the wire */
1687 memcpy(&ace_count, ibuf, sizeof(uint32_t));
1688 ace_count = htonl(ace_count);
1689 ibuf += 8; /* skip ACL flags (see acls.h) */
1693 (bitmap & kFileSec_Inherit),
1694 (darwin_ace_t *)ibuf,
1699 LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1700 getcwdpath(), s_path->u_name);
1705 LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1709 /********************************************************************
1710 * ACL funcs interfacing with other parts
1711 ********************************************************************/
1714 * map ACL to user maccess
1716 * This is the magic function that makes ACLs usable by calculating
1717 * the access granted by ACEs to the logged in user.
1719 int acltoownermode(const struct vol *vol, char *path, struct stat *st, struct maccess *ma)
1722 uint32_t rights = 0;
1724 if ( ! (AFPobj->options.flags & OPTION_ACL2MACCESS)
1725 || ! (vol->v_flags & AFPVOL_ACLS))
1728 LOG(log_maxdebug, logtype_afpd, "acltoownermode(\"%s/%s\", 0x%02x)",
1729 getcwdpath(), path, ma->ma_user);
1731 #ifdef HAVE_SOLARIS_ACLS
1732 EC_ZERO_LOG(solaris_acl_rights(path, st, &rights));
1734 LOG(log_maxdebug, logtype_afpd, "rights: 0x%08x", rights);
1736 if (rights & DARWIN_ACE_READ_DATA)
1737 ma->ma_user |= AR_UREAD;
1738 if (rights & DARWIN_ACE_WRITE_DATA)
1739 ma->ma_user |= AR_UWRITE;
1740 if (rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
1741 ma->ma_user |= AR_USEARCH;
1744 #ifdef HAVE_POSIX_ACLS
1745 EC_ZERO_LOG(posix_acls_to_uaperms(path, st, ma));
1748 LOG(log_maxdebug, logtype_afpd, "resulting user maccess: 0x%02x group maccess: 0x%02x", ma->ma_user, ma->ma_group);
1755 * Check whether a volume supports ACLs
1757 * @param vol (r) volume
1759 * @returns 0 if not, 1 if yes
1761 int check_vol_acl_support(const struct vol *vol)
1765 #ifdef HAVE_SOLARIS_ACLS
1768 if (get_nfsv4_acl(vol->v_path, &aces) == -1)
1771 #ifdef HAVE_POSIX_ACLS
1774 if ((acl = acl_get_file(vol->v_path, ACL_TYPE_ACCESS)) == NULL)
1778 #ifdef HAVE_SOLARIS_ACLS
1779 if (aces) free(aces);
1781 #ifdef HAVE_POSIX_ACLS
1782 if (acl) acl_free(acl);
1783 #endif /* HAVE_POSIX_ACLS */
1785 LOG(log_debug, logtype_afpd, "Volume \"%s\" ACL support: %s",
1786 vol->v_path, ret ? "yes" : "no");