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;
554 u_char group_rights = 0x00;
555 u_char acl_rights = 0x00;
558 EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
560 /* iterate through all ACEs */
561 while (acl_get_entry(acl, entry_id, &entry) == 1) {
562 entry_id = ACL_NEXT_ENTRY;
563 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
567 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(entry));
570 LOG(log_maxdebug, logtype_afpd, "ACL_USER: %u", *uid);
571 acl_rights |= acl_permset_to_uarights(entry);
577 group_rights = acl_permset_to_uarights(entry);
578 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP_OBJ: %u", sb->st_gid);
580 if (gmem(sb->st_gid))
581 acl_rights |= group_rights;
585 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(entry));
588 LOG(log_maxdebug, logtype_afpd, "ACL_GROUP: %u", *gid);
589 acl_rights |= acl_permset_to_uarights(entry);
595 mask = acl_permset_to_uarights(entry);
596 LOG(log_maxdebug, logtype_afpd, "ACL_MASK: 0x%02x", mask);
603 /* apply the mask and adjust user and group permissions */
604 ma->ma_user |= (acl_rights & mask);
605 ma->ma_group = (group_rights & mask);
607 /* update st_mode to properly reflect group permissions */
608 sb->st_mode &= ~S_IRWXG;
610 if (ma->ma_group & AR_USEARCH)
611 sb->st_mode |= S_IXGRP;
613 if (ma->ma_group & AR_UWRITE)
614 sb->st_mode |= S_IWGRP;
616 if (ma->ma_group & AR_UREAD)
617 sb->st_mode |= S_IRGRP;
620 if (acl) acl_free(acl);
626 * Add entries of one acl to another acl
628 * @param aclp (rw) destination acl where new aces will be added
629 * @param acl (r) source acl where aces will be copied from
631 * @returns 0 on success, -1 on error
633 static int acl_add_acl(acl_t *aclp, const acl_t acl)
639 for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
640 EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
641 EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
649 * Map Darwin ACE rights to POSIX 1e perm
651 * We can only map few rights:
652 * DARWIN_ACE_READ_DATA -> ACL_READ
653 * DARWIN_ACE_WRITE_DATA -> ACL_WRITE
654 * DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
655 * DARWIN_ACE_EXECUTE -> ACL_EXECUTE
657 * @param entry (rw) result of the mapping
658 * @param is_dir (r) 1 for dirs, 0 for files
660 * @returns mapping result as acl_perm_t, -1 on error
662 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
666 if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
669 if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (is_dir ? DARWIN_ACE_DELETE_CHILD : 0)))
672 if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
679 * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
681 * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
682 * for existing permissions for this type/id, if there is one add the perm,
683 * otherwise create a new ACL entry.
684 * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.
686 * @param aclp (rw) pointer to ACL
687 * @param type (r) acl_tag_t of ACL_USER or ACL_GROUP
688 * @param id (r) uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
689 * @param perm (r) acl_perm_t permissions to add
691 * @returns 0 on success, -1 on failure
693 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
699 int entry_id = ACL_FIRST_ENTRY;
700 acl_permset_t permset;
703 for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
704 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
705 if (tag != ACL_USER && tag != ACL_GROUP)
707 EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
708 if ((*eid == id) && (type == tag)) {
709 /* found an ACE for this type/id */
711 EC_ZERO_LOG(acl_get_permset(e, &permset));
712 EC_ZERO_LOG(acl_add_perm(permset, perm));
720 /* there was no existing ACE for this type/id */
721 EC_ZERO_LOG(acl_create_entry(aclp, &e));
722 EC_ZERO_LOG(acl_set_tag_type(e, type));
723 EC_ZERO_LOG(acl_set_qualifier(e, &id));
724 EC_ZERO_LOG(acl_get_permset(e, &permset));
725 EC_ZERO_LOG(acl_clear_perms(permset));
726 EC_ZERO_LOG(acl_add_perm(permset, perm));
727 EC_ZERO_LOG(acl_set_permset(e, permset));
731 if (eid) acl_free(eid);
737 * Map Darwin ACL to POSIX ACL.
739 * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
740 * Mapping pecularities:
741 * - we create a default ace (which inherits to files and dirs) if either
742 DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
743 * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
746 * @param darwin_aces (r) pointer to darwin_aces buffer
747 * @param def_aclp (rw) directories: pointer to an initialized acl_t with
748 the default acl files: *def_aclp will be NULL
749 * @param acc_aclp (rw) pointer to an initialized acl_t with the access acl
750 * @param ace_count (r) number of ACEs in darwin_aces buffer
751 * @param default_acl_flags (rw) flags to indicate if the object has a basic default
752 * acl or an extended default acl.
754 * @returns 0 on success storing the result in aclp, -1 on error. default_acl_flags
755 * is set to HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL in case there is at least one
756 * extended default ace. Otherwise default_acl_flags is left unchanged.
758 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
762 uint32_t *default_acl_flags)
770 uint32_t darwin_ace_flags, darwin_ace_rights;
774 for ( ; ace_count != 0; ace_count--, darwin_aces++) {
775 /* type: allow/deny, posix only has allow */
776 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
777 if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
780 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
781 perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
783 continue; /* dont add empty perm */
785 LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
786 ace_count, darwin_ace_flags, darwin_ace_rights, perm);
789 EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
792 EC_NULL_LOG(pwd = getpwnam(name));
795 LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
798 EC_NULL_LOG(grp = getgrnam(name));
800 id = (uid_t)(grp->gr_gid);
801 LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
809 if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
810 if (*def_aclp == NULL) {
811 /* ace request inheritane but we haven't got a default acl pointer */
812 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
816 /* add it as default ace */
817 EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
818 *default_acl_flags = (HAS_DEFAULT_ACL|HAS_EXT_DEFAULT_ACL);
820 if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
821 /* if it not a "inherit only" ace, it must be added as access aces too */
822 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
824 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
836 * Map ACEs from POSIX to Darwin.
837 * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
838 * Return number of mapped ACES, -1 on error.
840 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
844 int entry_id = ACL_FIRST_ENTRY;
849 struct passwd *pwd = NULL;
850 struct group *grp = NULL;
852 uint32_t rights, maskrights = 0;
853 darwin_ace_t *saved_darwin_aces = darwin_aces;
855 LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
856 (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
857 "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
859 /* itereate through all ACEs */
860 while (acl_get_entry(acl, entry_id, &e) == 1) {
861 entry_id = ACL_NEXT_ENTRY;
864 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
866 /* we return user and group ACE */
869 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
870 EC_NULL_LOG(pwd = getpwuid(*uid));
871 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
873 EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
879 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
880 EC_NULL_LOG(grp = getgrgid(*gid));
881 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
883 EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
889 maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
897 flags = DARWIN_ACE_FLAGS_PERMIT;
898 if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
899 flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
900 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
901 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
902 darwin_aces->darwin_ace_flags = htonl(flags);
905 rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
906 darwin_aces->darwin_ace_rights = htonl(rights);
912 /* Loop through the mapped ACE buffer once again, applying the mask */
913 for (int i = mapped_aces; i > 0; i--) {
914 saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
918 EC_STATUS(mapped_aces);
921 if (uid) acl_free(uid);
922 if (gid) acl_free(gid);
928 * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
929 * Reads from 'aces' buffer, writes to 'rbuf' buffer.
930 * Caller must provide buffer.
931 * Darwin ACEs are read and written in network byte order.
932 * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
933 * Ignores trivial ACEs.
934 * Return no of mapped ACEs or -1 on error.
936 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
940 LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
942 switch (type & MAP_MASK) {
944 #ifdef HAVE_SOLARIS_ACLS
945 case SOLARIS_2_DARWIN:
946 mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
949 case DARWIN_2_SOLARIS:
950 mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
952 #endif /* HAVE_SOLARIS_ACLS */
954 #ifdef HAVE_POSIX_ACLS
955 case POSIX_DEFAULT_2_DARWIN:
956 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
959 case POSIX_ACCESS_2_DARWIN:
960 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
963 case DARWIN_2_POSIX_DEFAULT:
966 case DARWIN_2_POSIX_ACCESS:
968 #endif /* HAVE_POSIX_ACLS */
975 LOG(log_debug9, logtype_afpd, "map_acl: END");
979 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
980 store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
981 Returns 0 on success, -1 on error. */
982 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
987 uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
988 #ifdef HAVE_SOLARIS_ACLS
992 #ifdef HAVE_POSIX_ACLS
995 LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
997 /* Skip length and flags */
1002 #ifdef HAVE_SOLARIS_ACLS
1003 EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
1004 EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
1005 #endif /* HAVE_SOLARIS_ACLS */
1007 #ifdef HAVE_POSIX_ACLS
1008 acl_t defacl = NULL , accacl = NULL;
1010 /* stat to check if its a dir */
1011 EC_ZERO_LOG(lstat(name, &st));
1013 /* if its a dir, check for default acl too */
1015 if (S_ISDIR(st.st_mode)) {
1017 EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
1018 EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
1020 (darwin_ace_t *)rbuf,
1024 EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
1027 EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
1029 (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
1032 #endif /* HAVE_POSIX_ACLS */
1034 LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
1036 *darwin_ace_count = htonl(mapped_aces);
1037 *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
1042 #ifdef HAVE_SOLARIS_ACLS
1043 if (aces) free(aces);
1045 #ifdef HAVE_POSIX_ACLS
1046 if (defacl) acl_free(defacl);
1047 if (accacl) acl_free(accacl);
1048 #endif /* HAVE_POSIX_ACLS */
1050 LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
1055 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
1056 static int remove_acl(const struct vol *vol,const char *path, int dir)
1060 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
1061 /* Ressource etc. first */
1062 if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
1064 /* now the data fork or dir */
1065 ret = remove_acl_vfs(path);
1071 Set ACL. Subtleties:
1072 - the client sends a complete list of ACEs, not only new ones. So we dont need to do
1073 any combination business (one exception being 'kFileSec_Inherit': see next)
1074 - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
1075 We will store inherited ACEs first, which is Darwins canonical order.
1076 - returns AFPerror code
1078 #ifdef HAVE_SOLARIS_ACLS
1079 static int set_acl(const struct vol *vol,
1082 darwin_ace_t *daces,
1086 int i, nfsv4_ace_count;
1087 int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
1088 ace_t *old_aces, *new_aces = NULL;
1091 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
1094 /* inherited + trivial ACEs */
1095 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1097 /* only trivial ACEs */
1098 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
1100 /* Get existing ACL and count ACEs which have to be copied */
1101 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
1103 for ( i=0; i < nfsv4_ace_count; i++) {
1104 if (old_aces[i].a_flags & flags)
1105 tocopy_aces_count++;
1108 /* Now malloc buffer exactly sized to fit all new ACEs */
1109 if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
1110 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
1111 EC_STATUS(AFPERR_MISC);
1115 /* Start building new ACL */
1117 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
1118 inherited ACEs first. */
1120 for (i=0; i < nfsv4_ace_count; i++) {
1121 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
1122 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1127 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
1129 /* Now the ACEs from the client */
1130 if ((ret = (map_acl(DARWIN_2_SOLARIS,
1131 &new_aces[new_aces_count],
1133 ace_count))) == -1) {
1134 EC_STATUS(AFPERR_PARAM);
1137 new_aces_count += ret;
1138 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
1140 /* Now copy the trivial ACEs */
1141 for (i=0; i < nfsv4_ace_count; i++) {
1142 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
1143 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
1145 trivial_ace_count++;
1148 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
1150 /* Ressourcefork first.
1151 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
1152 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
1153 if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1154 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1155 if (errno == (EACCES | EPERM))
1156 EC_STATUS(AFPERR_ACCESS);
1157 else if (errno == ENOENT)
1158 EC_STATUS(AFPERR_NOITEM);
1160 EC_STATUS(AFPERR_MISC);
1163 if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
1164 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
1165 if (errno == (EACCES | EPERM))
1166 EC_STATUS(AFPERR_ACCESS);
1167 else if (errno == ENOENT)
1168 EC_STATUS(AFPERR_NOITEM);
1170 EC_STATUS(AFPERR_MISC);
1177 if (old_aces) free(old_aces);
1178 if (new_aces) free(new_aces);
1180 LOG(log_debug9, logtype_afpd, "set_acl: END");
1183 #endif /* HAVE_SOLARIS_ACLS */
1185 #ifdef HAVE_POSIX_ACLS
1186 #ifndef HAVE_ACL_FROM_MODE
1187 static acl_t acl_from_mode(mode_t mode)
1191 acl_permset_t permset;
1193 if (!(acl = acl_init(3)))
1196 if (acl_create_entry(&acl, &entry) != 0)
1198 acl_set_tag_type(entry, ACL_USER_OBJ);
1199 acl_get_permset(entry, &permset);
1200 acl_clear_perms(permset);
1202 acl_add_perm(permset, ACL_READ);
1204 acl_add_perm(permset, ACL_WRITE);
1206 acl_add_perm(permset, ACL_EXECUTE);
1207 acl_set_permset(entry, permset);
1209 if (acl_create_entry(&acl, &entry) != 0)
1211 acl_set_tag_type(entry, ACL_GROUP_OBJ);
1212 acl_get_permset(entry, &permset);
1213 acl_clear_perms(permset);
1215 acl_add_perm(permset, ACL_READ);
1217 acl_add_perm(permset, ACL_WRITE);
1219 acl_add_perm(permset, ACL_EXECUTE);
1220 acl_set_permset(entry, permset);
1222 if (acl_create_entry(&acl, &entry) != 0)
1224 acl_set_tag_type(entry, ACL_OTHER);
1225 acl_get_permset(entry, &permset);
1226 acl_clear_perms(permset);
1228 acl_add_perm(permset, ACL_READ);
1230 acl_add_perm(permset, ACL_WRITE);
1232 acl_add_perm(permset, ACL_EXECUTE);
1233 acl_set_permset(entry, permset);
1243 static int set_acl(const struct vol *vol,
1246 darwin_ace_t *daces,
1251 acl_t default_acl = NULL;
1252 acl_t access_acl = NULL;
1255 int entry_id = ACL_FIRST_ENTRY;
1256 int has_def_acl = 0;
1257 /* flags to indicate if the object has a minimal default acl and/or an extended
1260 uint32_t default_acl_flags = 0;
1262 LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1264 EC_NULL_LOG_ERR(access_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1266 /* Iterate through acl and remove all extended acl entries. */
1267 while (acl_get_entry(access_acl, entry_id, &entry) == 1) {
1268 entry_id = ACL_NEXT_ENTRY;
1269 EC_ZERO_LOG(acl_get_tag_type(entry, &tag));
1271 if ((tag == ACL_USER) || (tag == ACL_GROUP) || (tag == ACL_MASK)) {
1272 EC_ZERO_LOG_ERR(acl_delete_entry(access_acl, entry), AFPERR_MISC);
1276 /* In case we are acting on a directory prepare a default acl. For files default_acl will be NULL.
1277 * If the directory already has a default acl it will be preserved.
1279 EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1281 if (S_ISDIR(st.st_mode)) {
1282 default_acl = acl_get_file(name, ACL_TYPE_DEFAULT);
1285 /* If default_acl is not empty then the dir has a default acl. */
1286 if (acl_get_entry(default_acl, ACL_FIRST_ENTRY, &entry) == 1)
1287 default_acl_flags = HAS_DEFAULT_ACL;
1289 acl_free(default_acl);
1291 default_acl = acl_dup(access_acl);
1293 /* adds the clients aces */
1294 EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &default_acl, &access_acl, ace_count, &default_acl_flags), AFPERR_MISC);
1296 /* calcuate ACL mask */
1297 EC_ZERO_LOG_ERR(acl_calc_mask(&access_acl), AFPERR_MISC);
1300 EC_ZERO_LOG_ERR(acl_valid(access_acl), AFPERR_MISC);
1303 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, access_acl), AFPERR_MISC);
1304 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, access_acl), AFPERR_MISC);
1307 /* If the dir has an extended default acl it's ACL_MASK must be updated.*/
1308 if (default_acl_flags & HAS_EXT_DEFAULT_ACL)
1309 EC_ZERO_LOG_ERR(acl_calc_mask(&default_acl), AFPERR_MISC);
1311 if (default_acl_flags) {
1312 EC_ZERO_LOG_ERR(acl_valid(default_acl), AFPERR_MISC);
1313 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, default_acl), AFPERR_MISC);
1314 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, default_acl), AFPERR_MISC);
1319 if (access_acl) acl_free(access_acl);
1320 if (default_acl) acl_free(default_acl);
1322 LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1325 #endif /* HAVE_POSIX_ACLS */
1328 * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1330 * Note: this gets called frequently and is a good place for optimizations !
1332 * @param vol (r) volume
1333 * @param dir (rw) directory
1334 * @param path (r) path to filesystem object
1335 * @param uuid (r) UUID of user
1336 * @param requested_rights (r) requested Darwin ACE
1338 * @returns AFP result code
1340 static int check_acl_access(const struct vol *vol,
1344 uint32_t requested_rights)
1347 uint32_t allowed_rights = 0;
1348 char *username = NULL;
1349 uuidtype_t uuidtype;
1351 bstring parent = NULL;
1354 LOG(log_maxdebug, logtype_afpd, "check_acl_access(dir: \"%s\", path: \"%s\", curdir: \"%s\", 0x%08x)",
1355 cfrombstr(dir->d_fullpath), path, getcwdpath(), requested_rights);
1357 /* This check is not used anymore, as OS X Server seems to be ignoring too */
1359 /* Get uid or gid from UUID */
1360 EC_ZERO_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1365 LOG(log_warning, logtype_afpd, "check_acl_access: afp_access not supported for groups");
1366 EC_STATUS(AFPERR_MISC);
1369 EC_STATUS(AFPERR_MISC);
1374 EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
1376 is_dir = !strcmp(path, ".");
1378 if (is_dir && (curdir->d_rights_cache != 0xffffffff)) {
1379 /* its a dir and the cache value is valid */
1380 allowed_rights = curdir->d_rights_cache;
1381 LOG(log_debug, logtype_afpd, "check_access: allowed rights from dircache: 0x%08x", allowed_rights);
1383 #ifdef HAVE_SOLARIS_ACLS
1384 EC_ZERO_LOG(solaris_acl_rights(path, &st, &allowed_rights));
1386 #ifdef HAVE_POSIX_ACLS
1387 EC_ZERO_LOG(posix_acl_rights(path, &st, &allowed_rights));
1390 * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1391 * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1392 * allowed where a delete would succeed because the parent dir gives write perms.
1393 * So we check the parent dir for write access and set the right accordingly.
1394 * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1395 * there to do this extra check -- afaict.
1397 if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1399 uint32_t parent_rights = 0;
1401 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1402 /* use volume path */
1403 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1405 /* build path for parent */
1406 EC_NULL_LOG_ERR(parent = bstrcpy(curdir->d_fullpath), AFPERR_MISC);
1407 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1408 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1409 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1410 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1413 LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1414 EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1416 #ifdef HAVE_SOLARIS_ACLS
1417 EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, &parent_rights));
1419 #ifdef HAVE_POSIX_ACLS
1420 EC_ZERO_LOG(posix_acl_rights(path, &st, &parent_rights));
1422 if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1423 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1427 /* Without DARWIN_ACE_DELETE set OS X 10.6 refuses to rename subdirectories in a
1430 if (allowed_rights & DARWIN_ACE_ADD_SUBDIRECTORY)
1431 allowed_rights |= DARWIN_ACE_DELETE;
1433 curdir->d_rights_cache = allowed_rights;
1435 LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1438 if ((requested_rights & allowed_rights) != requested_rights) {
1439 LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1440 requested_rights, allowed_rights);
1441 EC_STATUS(AFPERR_ACCESS);
1443 LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1449 if (username) free(username);
1450 if (parent) bdestroy(parent);
1455 /********************************************************
1457 ********************************************************/
1459 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1464 uint32_t did, darwin_ace_rights;
1466 struct path *s_path;
1472 memcpy(&vid, ibuf, sizeof( vid ));
1473 ibuf += sizeof(vid);
1474 if (NULL == ( vol = getvolbyvid( vid ))) {
1475 LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1476 return AFPERR_NOOBJ;
1479 memcpy(&did, ibuf, sizeof( did ));
1480 ibuf += sizeof( did );
1481 if (NULL == ( dir = dirlookup( vol, did ))) {
1482 LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1489 /* Store UUID address */
1490 uuid = (uuidp_t)ibuf;
1491 ibuf += UUID_BINSIZE;
1493 /* Store ACE rights */
1494 memcpy(&darwin_ace_rights, ibuf, 4);
1495 darwin_ace_rights = ntohl(darwin_ace_rights);
1498 /* get full path and handle file/dir subtleties in netatalk code*/
1499 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1500 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1501 return AFPERR_NOOBJ;
1503 if (!s_path->st_valid)
1504 of_statdir(vol, s_path);
1505 if ( s_path->st_errno != 0 ) {
1506 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1507 return AFPERR_NOOBJ;
1510 ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1515 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1521 uint16_t vid, bitmap;
1522 struct path *s_path;
1526 LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1530 memcpy(&vid, ibuf, sizeof( vid ));
1531 ibuf += sizeof(vid);
1532 if (NULL == ( vol = getvolbyvid( vid ))) {
1533 LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1534 return AFPERR_NOOBJ;
1537 memcpy(&did, ibuf, sizeof( did ));
1538 ibuf += sizeof( did );
1539 if (NULL == ( dir = dirlookup( vol, did ))) {
1540 LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1544 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1545 memcpy(rbuf, ibuf, sizeof( bitmap ));
1546 bitmap = ntohs( bitmap );
1547 ibuf += sizeof( bitmap );
1548 rbuf += sizeof( bitmap );
1549 *rbuflen += sizeof( bitmap );
1551 /* skip maxreplysize */
1554 /* get full path and handle file/dir subtleties in netatalk code*/
1555 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1556 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1557 return AFPERR_NOOBJ;
1559 if (!s_path->st_valid)
1560 of_statdir(vol, s_path);
1561 if ( s_path->st_errno != 0 ) {
1562 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1563 return AFPERR_NOOBJ;
1566 /* Shall we return owner UUID ? */
1567 if (bitmap & kFileSec_UUID) {
1568 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1569 if (NULL == (pw = getpwuid(s_path->st.st_uid))) {
1570 LOG(log_debug, logtype_afpd, "afp_getacl: local uid: %u", s_path->st.st_uid);
1571 localuuid_from_id(rbuf, UUID_USER, s_path->st.st_uid);
1573 LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1574 if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1577 rbuf += UUID_BINSIZE;
1578 *rbuflen += UUID_BINSIZE;
1581 /* Shall we return group UUID ? */
1582 if (bitmap & kFileSec_GRPUUID) {
1583 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1584 if (NULL == (gr = getgrgid(s_path->st.st_gid))) {
1585 LOG(log_debug, logtype_afpd, "afp_getacl: local gid: %u", s_path->st.st_gid);
1586 localuuid_from_id(rbuf, UUID_GROUP, s_path->st.st_gid);
1588 LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1589 if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1592 rbuf += UUID_BINSIZE;
1593 *rbuflen += UUID_BINSIZE;
1596 /* Shall we return ACL ? */
1597 if (bitmap & kFileSec_ACL) {
1598 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1599 if (get_and_map_acl(s_path->u_name, rbuf, rbuflen) != 0) {
1600 LOG(log_error, logtype_afpd, "afp_getacl(\"%s/%s\"): mapping error",
1601 getcwdpath(), s_path->u_name);
1606 LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1610 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1616 uint16_t vid, bitmap;
1617 struct path *s_path;
1619 LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1623 memcpy(&vid, ibuf, sizeof( vid ));
1624 ibuf += sizeof(vid);
1625 if (NULL == ( vol = getvolbyvid( vid ))) {
1626 LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1627 return AFPERR_NOOBJ;
1630 memcpy(&did, ibuf, sizeof( did ));
1631 ibuf += sizeof( did );
1632 if (NULL == ( dir = dirlookup( vol, did ))) {
1633 LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1637 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1638 bitmap = ntohs( bitmap );
1639 ibuf += sizeof( bitmap );
1641 /* get full path and handle file/dir subtleties in netatalk code*/
1642 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1643 LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1644 return AFPERR_NOOBJ;
1646 if (!s_path->st_valid)
1647 of_statdir(vol, s_path);
1648 if ( s_path->st_errno != 0 ) {
1649 LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1650 return AFPERR_NOOBJ;
1652 LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1655 if ((unsigned long)ibuf & 1)
1658 /* Start processing request */
1660 /* Change owner: dont even try */
1661 if (bitmap & kFileSec_UUID) {
1662 LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1663 ret = AFPERR_ACCESS;
1664 ibuf += UUID_BINSIZE;
1667 /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1668 if (bitmap & kFileSec_UUID) {
1669 LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1671 ibuf += UUID_BINSIZE;
1675 if (bitmap & kFileSec_REMOVEACL) {
1676 LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1677 if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1678 LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1682 if (bitmap & kFileSec_ACL) {
1683 LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1684 /* Get no of ACEs the client put on the wire */
1686 memcpy(&ace_count, ibuf, sizeof(uint32_t));
1687 ace_count = htonl(ace_count);
1688 ibuf += 8; /* skip ACL flags (see acls.h) */
1692 (bitmap & kFileSec_Inherit),
1693 (darwin_ace_t *)ibuf,
1698 LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1699 getcwdpath(), s_path->u_name);
1704 LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1708 /********************************************************************
1709 * ACL funcs interfacing with other parts
1710 ********************************************************************/
1713 * map ACL to user maccess
1715 * This is the magic function that makes ACLs usable by calculating
1716 * the access granted by ACEs to the logged in user.
1718 int acltoownermode(char *path, struct stat *st, struct maccess *ma)
1721 uint32_t rights = 0;
1723 if ( ! (AFPobj->options.flags & OPTION_ACL2MACCESS)
1724 || (current_vol == NULL)
1725 || ! (current_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");