2 Copyright (c) 2008, 2009, 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
26 #ifdef HAVE_SOLARIS_ACLS
29 #ifdef HAVE_POSIX_ACLS
31 #include <acl/libacl.h>
34 #include <atalk/errchk.h>
35 #include <atalk/adouble.h>
36 #include <atalk/vfs.h>
37 #include <atalk/afp.h>
38 #include <atalk/util.h>
39 #include <atalk/cnid.h>
40 #include <atalk/logger.h>
41 #include <atalk/uuid.h>
42 #include <atalk/acl.h>
43 #include <atalk/bstrlib.h>
44 #include <atalk/bstradd.h>
46 #include "directory.h"
52 #include "acl_mappings.h"
55 #define SOLARIS_2_DARWIN 1
56 #define DARWIN_2_SOLARIS 2
57 #define POSIX_DEFAULT_2_DARWIN 3
58 #define POSIX_ACCESS_2_DARWIN 4
59 #define DARWIN_2_POSIX_DEFAULT 5
60 #define DARWIN_2_POSIX_ACCESS 6
65 /********************************************************
67 ********************************************************/
69 #ifdef HAVE_SOLARIS_ACLS
72 * Compile access rights for a user to one file-system object
74 * This combines combines all access rights for a user to one fs-object and
75 * returns the result as a Darwin allowed rights ACE.
76 * This must honor trivial ACEs which are a mode_t mapping.
78 * @param path (r) path to filesystem object
79 * @param sb (r) struct stat of path
80 * @param pwd (r) struct passwd of user
81 * @param result (w) resulting Darwin allow ACE
83 * @returns 0 or -1 on error
85 static int solaris_acl_rights(const char *path,
86 const struct stat *sb,
87 const struct passwd *pwd,
91 int i, ace_count, checkgroup;
95 uint32_t rights, allowed_rights = 0, denied_rights = 0, darwin_rights;
97 /* Get ACL from file/dir */
98 EC_NEG1_LOG(ace_count = get_nfsv4_acl(path, &aces));
100 if (ace_count == 0) {
101 LOG(log_warning, logtype_afpd, "Zero ACEs from get_nfsv4_acl");
105 /* Now check requested rights */
107 do { /* Loop through ACEs */
109 flags = aces[i].a_flags;
110 type = aces[i].a_type;
111 rights = aces[i].a_access_mask;
113 if (flags & ACE_INHERIT_ONLY_ACE)
116 /* Now the tricky part: decide if ACE effects our user. I'll explain:
117 if its a dedicated (non trivial) ACE for the user
119 if its a ACE for a group we're member of
121 if its a trivial ACE_OWNER ACE and requested UUID is the owner
123 if its a trivial ACE_GROUP ACE and requested UUID is group
125 if its a trivial ACE_EVERYONE ACE
128 if (((who == pwd->pw_uid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)))
130 ((flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) && gmem(who))
132 ((flags & ACE_OWNER) && (pwd->pw_uid == sb->st_uid))
134 ((flags & ACE_GROUP) && gmem(sb->st_gid))
136 (flags & ACE_EVERYONE)
138 /* Found an applicable ACE */
139 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
140 allowed_rights |= rights;
141 else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
142 /* Only or to denied rights if not previously allowed !! */
143 denied_rights |= ((!allowed_rights) & rights);
145 } while (++i < ace_count);
148 /* Darwin likes to ask for "delete_child" on dir,
149 "write_data" is actually the same, so we add that for dirs */
150 if (S_ISDIR(sb->st_mode) && (allowed_rights & ACE_WRITE_DATA))
151 allowed_rights |= ACE_DELETE_CHILD;
153 /* Remove denied from allowed rights */
154 allowed_rights &= ~denied_rights;
158 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
159 if (allowed_rights & nfsv4_to_darwin_rights[i].from)
160 darwin_rights |= nfsv4_to_darwin_rights[i].to;
163 *result |= darwin_rights;
166 if (aces) free(aces);
172 Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
173 Return numer of mapped ACEs or -1 on error.
174 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
176 static int map_aces_solaris_to_darwin(const ace_t *aces,
177 darwin_ace_t *darwin_aces,
184 struct passwd *pwd = NULL;
185 struct group *grp = NULL;
187 LOG(log_maxdebug, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
190 LOG(log_maxdebug, logtype_afpd, "ACE No. %d", ace_count + 1);
191 /* if its a ACE resulting from nfsv4 mode mapping, discard it */
192 if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
193 LOG(log_debug, logtype_afpd, "trivial ACE");
198 if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) { /* user ace */
199 LOG(log_debug, logtype_afpd, "uid: %d", aces->a_who);
200 EC_NULL_LOG(pwd = getpwuid(aces->a_who));
201 LOG(log_debug, logtype_afpd, "uid: %d -> name: %s", aces->a_who, pwd->pw_name);
202 EC_ZERO_LOG(getuuidfromname(pwd->pw_name,
204 darwin_aces->darwin_ace_uuid));
205 } else { /* group ace */
206 LOG(log_debug, logtype_afpd, "gid: %d", aces->a_who);
207 EC_NULL_LOG(grp = getgrgid(aces->a_who));
208 LOG(log_debug, logtype_afpd, "gid: %d -> name: %s", aces->a_who, grp->gr_name);
209 EC_ZERO_LOG(getuuidfromname(grp->gr_name,
211 darwin_aces->darwin_ace_uuid));
215 if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
216 flags = DARWIN_ACE_FLAGS_PERMIT;
217 else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
218 flags = DARWIN_ACE_FLAGS_DENY;
219 else { /* unsupported type */
223 for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
224 if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
225 flags |= nfsv4_to_darwin_flags[i].to;
227 darwin_aces->darwin_ace_flags = htonl(flags);
231 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
232 if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
233 rights |= nfsv4_to_darwin_rights[i].to;
235 darwin_aces->darwin_ace_rights = htonl(rights);
248 Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
249 Return numer of mapped ACEs or -1 on error.
250 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
252 static int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces,
257 int i, mapped_aces = 0;
258 uint32_t darwin_ace_flags;
259 uint32_t darwin_ace_rights;
260 uint16_t nfsv4_ace_flags;
261 uint32_t nfsv4_ace_rights;
269 nfsv4_ace_rights = 0;
272 EC_ZERO(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
280 EC_NULL_LOG(pwd = getpwnam(name));
281 nfsv4_aces->a_who = pwd->pw_uid;
284 EC_NULL_LOG(grp = getgrnam(name));
285 nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
286 nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
292 /* now type: allow/deny */
293 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
294 if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
295 nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
296 else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
297 nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
298 else { /* unsupported type */
303 for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
304 if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
305 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
309 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
310 for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
311 if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
312 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
315 LOG(log_debug9, logtype_afpd, "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x", darwin_ace_flags, nfsv4_ace_flags);
316 LOG(log_debug9, logtype_afpd, "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x", darwin_ace_rights, nfsv4_ace_rights);
318 nfsv4_aces->a_flags = nfsv4_ace_flags;
319 nfsv4_aces->a_access_mask = nfsv4_ace_rights;
332 #endif /* HAVE_SOLARIS_ACLS */
334 /********************************************************
336 ********************************************************/
338 #ifdef HAVE_POSIX_ACLS
340 static uint32_t posix_permset_to_darwin_rights(acl_entry_t e, int is_dir)
344 acl_permset_t permset;
346 EC_ZERO_LOG(acl_get_permset(e, &permset));
348 if (acl_get_perm(permset, ACL_READ))
349 rights = DARWIN_ACE_READ_DATA
350 | DARWIN_ACE_READ_EXTATTRIBUTES
351 | DARWIN_ACE_READ_ATTRIBUTES
352 | DARWIN_ACE_READ_SECURITY;
353 if (acl_get_perm(permset, ACL_WRITE)) {
354 rights |= DARWIN_ACE_WRITE_DATA
355 | DARWIN_ACE_APPEND_DATA
356 | DARWIN_ACE_WRITE_EXTATTRIBUTES
357 | DARWIN_ACE_WRITE_ATTRIBUTES;
359 rights |= DARWIN_ACE_DELETE;
361 if (acl_get_perm(permset, ACL_EXECUTE))
362 rights |= DARWIN_ACE_EXECUTE;
368 * Compile access rights for a user to one file-system object
370 * This combines combines all access rights for a user to one fs-object and
371 * returns the result as a Darwin allowed rights ACE.
372 * This must honor trivial ACEs which are a mode_t mapping.
374 * @param path (r) path to filesystem object
375 * @param sb (r) struct stat of path
376 * @param pwd (r) struct passwd of user
377 * @param result (w) resulting Darwin allow ACE
379 * @returns 0 or -1 on error
381 static int posix_acl_rights(const char *path,
382 const struct stat *sb,
383 const struct passwd *pwd,
387 int entry_id = ACL_FIRST_ENTRY;
389 uint32_t rights = , maskrights = 0;
395 acl_permset_t permset, mask;
397 EC_NULL_LOG(acl = acl_get_file(path, ACL_TYPE_ACCESS));
399 /* itereate through all ACEs */
400 while (acl_get_entry(acl, entry_id, &e) == 1) {
401 entry_id = ACL_NEXT_ENTRY;
402 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
406 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
407 if (*uid == pwd->pw_uid)
408 rights |= posix_permset_to_darwin_rights(e, type & IS_DIR);
415 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
416 if (*gid == pwd->pw_gid || gmem(*gid))
417 rights |= posix_permset_to_darwin_rights(e, type & IS_DIR);
423 rights |= posix_permset_to_darwin_rights(e, type & IS_DIR);
427 maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
434 /* Loop through the mapped ACE buffer once again, applying the mask */
435 for (int i = mapped_aces; i > 0; i--) {
436 saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
441 if (acl) acl_free(acl);
442 if (uid) acl_free(uid);
443 if (gid) acl_free(gid);
448 * Add entries of one acl to another acl
450 * @param aclp (rw) destination acl where new aces will be added
451 * @param acl (r) source acl where aces will be copied from
453 * @returns 0 on success, -1 on error
455 static int acl_add_acl(acl_t *aclp, const acl_t acl)
461 for (id = ACL_FIRST_ENTRY; acl_get_entry(acl, id, &se) == 1; id = ACL_NEXT_ENTRY) {
462 EC_ZERO_LOG_ERR(acl_create_entry(aclp, &de), AFPERR_MISC);
463 EC_ZERO_LOG_ERR(acl_copy_entry(de, se), AFPERR_MISC);
471 * Map Darwin ACE rights to POSIX 1e perm
473 * We can only map few rights:
474 * DARWIN_ACE_READ_DATA -> ACL_READ
475 * DARWIN_ACE_WRITE_DATA -> ACL_WRITE
476 * DARWIN_ACE_DELETE_CHILD & (is_dir == 1) -> ACL_WRITE
477 * DARWIN_ACE_EXECUTE -> ACL_EXECUTE
479 * @param entry (rw) result of the mapping
480 * @param is_dir (r) 1 for dirs, 0 for files
482 * @returns mapping result as acl_perm_t, -1 on error
484 static acl_perm_t map_darwin_right_to_posix_permset(uint32_t darwin_ace_rights, int is_dir)
488 if (darwin_ace_rights & DARWIN_ACE_READ_DATA)
491 if (darwin_ace_rights & (DARWIN_ACE_WRITE_DATA | (DARWIN_ACE_DELETE_CHILD & is_dir)))
494 if (darwin_ace_rights & DARWIN_ACE_EXECUTE)
501 * Add a ACL_USER or ACL_GROUP permission to an ACL, extending existing ACEs
503 * Add a permission of "type" for user or group "id" to an ACL. Scan the ACL
504 * for existing permissions for this type/id, if there is one add the perm,
505 * otherwise create a new ACL entry.
506 * perm can be or'ed ACL_READ, ACL_WRITE and ACL_EXECUTE.
508 * @param aclp (rw) pointer to ACL
509 * @param type (r) acl_tag_t of ACL_USER or ACL_GROUP
510 * @param id (r) uid_t uid for ACL_USER, or gid casted to uid_t for ACL_GROUP
511 * @param perm (r) acl_perm_t permissions to add
513 * @returns 0 on success, -1 on failure
515 static int posix_acl_add_perm(acl_t *aclp, acl_tag_t type, uid_t id, acl_perm_t perm)
521 int entry_id = ACL_FIRST_ENTRY;
522 acl_permset_t permset;
525 for ( ; (! found) && acl_get_entry(*aclp, entry_id, &e) == 1; entry_id = ACL_NEXT_ENTRY) {
526 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
527 if (tag != ACL_USER && tag != ACL_GROUP)
529 EC_NULL_LOG(eid = (uid_t *)acl_get_qualifier(e));
530 if ((*eid == id) && (type == tag)) {
531 /* found an ACE for this type/id */
533 EC_ZERO_LOG(acl_get_permset(e, &permset));
534 EC_ZERO_LOG(acl_add_perm(permset, perm));
542 /* there was no existing ACE for this type/id */
543 EC_ZERO_LOG(acl_create_entry(aclp, &e));
544 EC_ZERO_LOG(acl_set_tag_type(e, type));
545 EC_ZERO_LOG(acl_set_qualifier(e, &id));
546 EC_ZERO_LOG(acl_get_permset(e, &permset));
547 EC_ZERO_LOG(acl_clear_perms(permset));
548 EC_ZERO_LOG(acl_add_perm(permset, perm));
549 EC_ZERO_LOG(acl_set_permset(e, permset));
553 if (eid) acl_free(eid);
559 * Map Darwin ACL to POSIX ACL.
561 * aclp must point to a acl_init'ed acl_t or an acl_t that can eg contain default ACEs.
562 * Mapping pecularities:
563 * - we create a default ace (which inherits to files and dirs) if either
564 DARWIN_ACE_FLAGS_FILE_INHERIT or DARWIN_ACE_FLAGS_DIRECTORY_INHERIT is requested
565 * - we throw away DARWIN_ACE_FLAGS_LIMIT_INHERIT (can't be mapped), thus the ACL will
568 * @param darwin_aces (r) pointer to darwin_aces buffer
569 * @param def_aclp (rw) directories: pointer to an initialized acl_t with the default acl
570 * files: *def_aclp will be NULL
571 * @param acc_aclp (rw) pointer to an initialized acl_t with the access acl
572 * @param ace_count (r) number of ACEs in darwin_aces buffer
574 * @returns 0 on success storing the result in aclp, -1 on error.
576 static int map_aces_darwin_to_posix(const darwin_ace_t *darwin_aces,
587 uint32_t darwin_ace_flags, darwin_ace_rights;
591 for ( ; ace_count != 0; ace_count--, darwin_aces++) {
592 /* type: allow/deny, posix only has allow */
593 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
594 if ( ! (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT))
597 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
598 perm = map_darwin_right_to_posix_permset(darwin_ace_rights, (*def_aclp != NULL));
600 continue; /* dont add empty perm */
602 LOG(log_debug, logtype_afpd, "map_ace: no: %u, flags: %08x, darwin: %08x, posix: %02x",
603 ace_count, darwin_ace_flags, darwin_ace_rights, perm);
606 EC_ZERO_LOG(getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype));
613 EC_NULL_LOG(pwd = getpwnam(name));
616 LOG(log_debug, logtype_afpd, "map_ace: name: %s, uid: %u", name, id);
619 EC_NULL_LOG(grp = getgrnam(name));
621 id = (uid_t)(grp->gr_gid);
622 LOG(log_debug, logtype_afpd, "map_ace: name: %s, gid: %u", name, id);
628 if (darwin_ace_flags & DARWIN_ACE_INHERIT_CONTROL_FLAGS) {
629 if (*def_aclp == NULL) {
630 /* ace request inheritane but we haven't got a default acl pointer */
631 LOG(log_warning, logtype_afpd, "map_acl: unexpected ACE, flags: 0x%04x",
635 /* add it as default ace */
636 EC_ZERO_LOG(posix_acl_add_perm(def_aclp, tag, id, perm));
639 if (! (darwin_ace_flags & DARWIN_ACE_FLAGS_ONLY_INHERIT))
640 /* if it not a "inherit only" ace, it must be added as access aces too */
641 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
643 EC_ZERO_LOG(posix_acl_add_perm(acc_aclp, tag, id, perm));
655 * Map ACEs from POSIX to Darwin.
656 * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
657 * Return number of mapped ACES, -1 on error.
659 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
663 int entry_id = ACL_FIRST_ENTRY;
666 acl_permset_t permset, mask;
669 struct passwd *pwd = NULL;
670 struct group *grp = NULL;
672 uint32_t rights, maskrights = 0;
673 darwin_ace_t *saved_darwin_aces = darwin_aces;
675 LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
676 (type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN ?
677 "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
679 /* itereate through all ACEs */
680 while (acl_get_entry(acl, entry_id, &e) == 1) {
681 entry_id = ACL_NEXT_ENTRY;
684 EC_ZERO_LOG(acl_get_tag_type(e, &tag));
686 /* we return user and group ACE */
689 EC_NULL_LOG(uid = (uid_t *)acl_get_qualifier(e));
690 EC_NULL_LOG(pwd = getpwuid(*uid));
691 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
693 EC_ZERO_LOG(getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid));
699 EC_NULL_LOG(gid = (gid_t *)acl_get_qualifier(e));
700 EC_NULL_LOG(grp = getgrgid(*gid));
701 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
703 EC_ZERO_LOG(getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid));
709 maskrights = posix_permset_to_darwin_rights(e, type & IS_DIR);
717 flags = DARWIN_ACE_FLAGS_PERMIT;
718 if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
719 flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
720 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
721 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
722 darwin_aces->darwin_ace_flags = htonl(flags);
725 rights = posix_permset_to_darwin_rights(e, type & IS_DIR);
726 darwin_aces->darwin_ace_rights = htonl(rights);
732 /* Loop through the mapped ACE buffer once again, applying the mask */
733 for (int i = mapped_aces; i > 0; i--) {
734 saved_darwin_aces->darwin_ace_rights &= htonl(maskrights);
738 EC_STATUS(mapped_aces);
741 if (uid) acl_free(uid);
742 if (gid) acl_free(gid);
748 * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
749 * Reads from 'aces' buffer, writes to 'rbuf' buffer.
750 * Caller must provide buffer.
751 * Darwin ACEs are read and written in network byte order.
752 * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
753 * Ignores trivial ACEs.
754 * Return no of mapped ACEs or -1 on error.
756 static int map_acl(int type, void *acl, darwin_ace_t *buf, int ace_count)
760 LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
762 switch (type & MAP_MASK) {
764 #ifdef HAVE_SOLARIS_ACLS
765 case SOLARIS_2_DARWIN:
766 mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
769 case DARWIN_2_SOLARIS:
770 mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
772 #endif /* HAVE_SOLARIS_ACLS */
774 #ifdef HAVE_POSIX_ACLS
775 case POSIX_DEFAULT_2_DARWIN:
776 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
779 case POSIX_ACCESS_2_DARWIN:
780 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
783 case DARWIN_2_POSIX_DEFAULT:
786 case DARWIN_2_POSIX_ACCESS:
788 #endif /* HAVE_POSIX_ACLS */
795 LOG(log_debug9, logtype_afpd, "map_acl: END");
799 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
800 store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
801 Returns 0 on success, -1 on error. */
802 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
807 uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
808 #ifdef HAVE_SOLARIS_ACLS
812 #ifdef HAVE_POSIX_ACLS
815 LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
817 /* Skip length and flags */
822 #ifdef HAVE_SOLARIS_ACLS
823 EC_NEG1(ace_count = get_nfsv4_acl(name, &aces));
824 EC_NEG1(mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count));
825 #endif /* HAVE_SOLARIS_ACLS */
827 #ifdef HAVE_POSIX_ACLS
828 acl_t defacl = NULL , accacl = NULL;
830 /* stat to check if its a dir */
831 EC_ZERO_LOG(lstat(name, &st));
833 /* if its a dir, check for default acl too */
835 if (S_ISDIR(st.st_mode)) {
837 EC_NULL_LOG(defacl = acl_get_file(name, ACL_TYPE_DEFAULT));
838 EC_NEG1(mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
840 (darwin_ace_t *)rbuf,
844 EC_NULL_LOG(accacl = acl_get_file(name, ACL_TYPE_ACCESS));
847 EC_NEG1(tmp = map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
849 (darwin_ace_t *)(rbuf + mapped_aces * sizeof(darwin_ace_t)),
852 #endif /* HAVE_POSIX_ACLS */
854 LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
856 *darwin_ace_count = htonl(mapped_aces);
857 *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
862 #ifdef HAVE_SOLARIS_ACLS
863 if (aces) free(aces);
865 #ifdef HAVE_POSIX_ACLS
866 if (defacl) acl_free(defacl);
867 if (accacl) acl_free(accacl);
868 #endif /* HAVE_POSIX_ACLS */
870 LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
875 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
876 static int remove_acl(const struct vol *vol,const char *path, int dir)
880 #if (defined HAVE_SOLARIS_ACLS || defined HAVE_POSIX_ACLS)
881 /* Ressource etc. first */
882 if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
884 /* now the data fork or dir */
885 ret = remove_acl_vfs(path);
892 - the client sends a complete list of ACEs, not only new ones. So we dont need to do
893 any combination business (one exception being 'kFileSec_Inherit': see next)
894 - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
895 We will store inherited ACEs first, which is Darwins canonical order.
896 - returns AFPerror code
898 #ifdef HAVE_SOLARIS_ACLS
899 static int set_acl(const struct vol *vol,
906 int i, nfsv4_ace_count;
907 int tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
908 ace_t *old_aces, *new_aces = NULL;
911 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
914 /* inherited + trivial ACEs */
915 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
917 /* only trivial ACEs */
918 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
920 /* Get existing ACL and count ACEs which have to be copied */
921 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
923 for ( i=0; i < nfsv4_ace_count; i++) {
924 if (old_aces[i].a_flags & flags)
928 /* Now malloc buffer exactly sized to fit all new ACEs */
929 if ((new_aces = malloc((ace_count + tocopy_aces_count) * sizeof(ace_t))) == NULL) {
930 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
931 EC_STATUS(AFPERR_MISC);
935 /* Start building new ACL */
937 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
938 inherited ACEs first. */
940 for (i=0; i < nfsv4_ace_count; i++) {
941 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
942 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
947 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
949 /* Now the ACEs from the client */
950 if ((ret = (map_acl(DARWIN_2_SOLARIS,
951 &new_aces[new_aces_count],
953 ace_count))) == -1) {
954 EC_STATUS(AFPERR_PARAM);
957 new_aces_count += ret;
958 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ret);
960 /* Now copy the trivial ACEs */
961 for (i=0; i < nfsv4_ace_count; i++) {
962 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
963 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
968 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
970 /* Ressourcefork first.
971 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
972 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
973 if ((ret = (vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
974 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
975 if (errno == (EACCES | EPERM))
976 EC_STATUS(AFPERR_ACCESS);
977 else if (errno == ENOENT)
978 EC_STATUS(AFPERR_NOITEM);
980 EC_STATUS(AFPERR_MISC);
983 if ((ret = (acl(name, ACE_SETACL, new_aces_count, new_aces))) != 0) {
984 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
985 if (errno == (EACCES | EPERM))
986 EC_STATUS(AFPERR_ACCESS);
987 else if (errno == ENOENT)
988 EC_STATUS(AFPERR_NOITEM);
990 EC_STATUS(AFPERR_MISC);
997 if (old_aces) free(old_aces);
998 if (new_aces) free(new_aces);
1000 LOG(log_debug9, logtype_afpd, "set_acl: END");
1003 #endif /* HAVE_SOLARIS_ACLS */
1005 #ifdef HAVE_POSIX_ACLS
1006 static int set_acl(const struct vol *vol,
1009 darwin_ace_t *daces,
1013 acl_t def_acl = NULL;
1014 acl_t acc_acl = NULL;
1016 LOG(log_maxdebug, logtype_afpd, "set_acl: BEGIN");
1019 EC_ZERO_LOG_ERR(lstat(name, &st), AFPERR_NOOBJ);
1021 /* seed default ACL with access ACL */
1022 if (S_ISDIR(st.st_mode))
1023 EC_NULL_LOG_ERR(def_acl = acl_get_file(name, ACL_TYPE_ACCESS), AFPERR_MISC);
1025 /* for files def_acl will be NULL */
1027 /* create access acl from mode */
1028 EC_NULL_LOG_ERR(acc_acl = acl_from_mode(st.st_mode), AFPERR_MISC);
1030 /* adds the clients aces */
1031 EC_ZERO_ERR(map_aces_darwin_to_posix(daces, &def_acl, &acc_acl, ace_count), AFPERR_MISC);
1033 /* calcuate ACL mask */
1034 EC_ZERO_LOG_ERR(acl_calc_mask(&acc_acl), AFPERR_MISC);
1037 EC_ZERO_LOG_ERR(acl_valid(acc_acl), AFPERR_MISC);
1040 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_ACCESS, acc_acl), AFPERR_MISC);
1041 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_ACCESS, 0, acc_acl), AFPERR_MISC);
1044 EC_ZERO_LOG_ERR(acl_set_file(name, ACL_TYPE_DEFAULT, def_acl), AFPERR_MISC);
1045 EC_ZERO_LOG_ERR(vol->vfs->vfs_acl(vol, name, ACL_TYPE_DEFAULT, 0, def_acl), AFPERR_MISC);
1052 LOG(log_maxdebug, logtype_afpd, "set_acl: END");
1055 #endif /* HAVE_POSIX_ACLS */
1058 * Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
1060 * Note: this gets called frequently and is a good place for optimizations !
1062 * @param vol (r) volume
1063 * @param dir (r) directory
1064 * @param path (r) path to filesystem object
1065 * @param uuid (r) UUID of user
1066 * @param requested_rights (r) requested Darwin ACE
1068 * @returns AFP result code
1070 static int check_acl_access(const struct vol *vol,
1071 const struct dir *dir,
1074 uint32_t requested_rights)
1077 uint32_t allowed_rights = 0;
1078 char *username = NULL;
1079 uuidtype_t uuidtype;
1082 bstring parent = NULL;
1084 LOG(log_maxdebug, logtype_afpd, "check_access: Request: 0x%08x", requested_rights);
1086 /* Get uid or gid from UUID */
1087 EC_ZERO_LOG_ERR(getnamefromuuid(uuid, &username, &uuidtype), AFPERR_PARAM);
1088 EC_ZERO_LOG_ERR(lstat(path, &st), AFPERR_PARAM);
1092 LOG(log_warning, logtype_afpd, "check_access: afp_access not supported for groups");
1093 EC_STATUS(AFPERR_MISC);
1097 LOG(log_warning, logtype_afpd, "check_access: local UUID");
1098 EC_STATUS(AFPERR_MISC);
1102 EC_NULL_LOG_ERR(pwd = getpwnam(username), AFPERR_MISC);
1104 #ifdef HAVE_SOLARIS_ACLS
1105 EC_ZERO_LOG(solaris_acl_rights(path, &st, pwd, &allowed_rights));
1107 #ifdef HAVE_POSIX_ACLS
1108 EC_ZERO_LOG(posix_acl_rights(path, &st, pwd, &allowed_rights));
1111 LOG(log_debug, logtype_afpd, "allowed rights: 0x%08x", allowed_rights);
1114 * The DARWIN_ACE_DELETE right might implicitly result from write acces to the parent
1115 * directory. As it seems the 10.6 AFP client is puzzled when this right is not
1116 * allowed where a delete would succeed because the parent dir gives write perms.
1117 * So we check the parent dir for write access and set the right accordingly.
1118 * Currentyl acl2ownermode calls us with dir = NULL, because it doesn't make sense
1119 * there to do this extra check -- afaict.
1121 if (vol && dir && (requested_rights & DARWIN_ACE_DELETE)) {
1123 uint32_t parent_rights = 0;
1125 if (dir->d_did == DIRDID_ROOT_PARENT) {
1126 /* use volume path */
1127 EC_NULL_LOG_ERR(parent = bfromcstr(vol->v_path), AFPERR_MISC);
1129 /* build path for parent */
1130 EC_NULL_LOG_ERR(parent = bstrcpy(dir->d_fullpath), AFPERR_MISC);
1131 EC_ZERO_LOG_ERR(bconchar(parent, '/'), AFPERR_MISC);
1132 EC_ZERO_LOG_ERR(bcatcstr(parent, path), AFPERR_MISC);
1133 EC_NEG1_LOG_ERR(i = bstrrchr(parent, '/'), AFPERR_MISC);
1134 EC_ZERO_LOG_ERR(binsertch(parent, i, 1, 0), AFPERR_MISC);
1137 LOG(log_debug, logtype_afpd,"parent: %s", cfrombstr(parent));
1138 EC_ZERO_LOG_ERR(lstat(cfrombstr(parent), &st), AFPERR_MISC);
1140 #ifdef HAVE_SOLARIS_ACLS
1141 EC_ZERO_LOG(solaris_acl_rights(cfrombstr(parent), &st, pwd, &parent_rights));
1143 #ifdef HAVE_POSIX_ACLS
1144 EC_ZERO_LOG(posix_acl_rights(path, &st, pwd, &allowed_rights));
1146 if (parent_rights & (DARWIN_ACE_WRITE_DATA | DARWIN_ACE_DELETE_CHILD))
1147 allowed_rights |= DARWIN_ACE_DELETE; /* man, that was a lot of work! */
1150 if ((requested_rights & allowed_rights) != requested_rights) {
1151 LOG(log_debug, logtype_afpd, "some requested right wasn't allowed: 0x%08x / 0x%08x",
1152 requested_rights, allowed_rights);
1153 EC_STATUS(AFPERR_ACCESS);
1155 LOG(log_debug, logtype_afpd, "all requested rights are allowed: 0x%08x",
1161 if (username) free(username);
1162 if (parent) bdestroy(parent);
1167 /********************************************************
1169 ********************************************************/
1171 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1176 uint32_t did, darwin_ace_rights;
1178 struct path *s_path;
1184 memcpy(&vid, ibuf, sizeof( vid ));
1185 ibuf += sizeof(vid);
1186 if (NULL == ( vol = getvolbyvid( vid ))) {
1187 LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1188 return AFPERR_NOOBJ;
1191 memcpy(&did, ibuf, sizeof( did ));
1192 ibuf += sizeof( did );
1193 if (NULL == ( dir = dirlookup( vol, did ))) {
1194 LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1201 /* Store UUID address */
1202 uuid = (uuidp_t)ibuf;
1203 ibuf += UUID_BINSIZE;
1205 /* Store ACE rights */
1206 memcpy(&darwin_ace_rights, ibuf, 4);
1207 darwin_ace_rights = ntohl(darwin_ace_rights);
1210 /* get full path and handle file/dir subtleties in netatalk code*/
1211 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1212 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1213 return AFPERR_NOOBJ;
1215 if (!s_path->st_valid)
1216 of_statdir(vol, s_path);
1217 if ( s_path->st_errno != 0 ) {
1218 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1219 return AFPERR_NOOBJ;
1222 ret = check_acl_access(vol, dir, s_path->u_name, uuid, darwin_ace_rights);
1227 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1233 uint16_t vid, bitmap;
1234 struct path *s_path;
1238 LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1242 memcpy(&vid, ibuf, sizeof( vid ));
1243 ibuf += sizeof(vid);
1244 if (NULL == ( vol = getvolbyvid( vid ))) {
1245 LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1246 return AFPERR_NOOBJ;
1249 memcpy(&did, ibuf, sizeof( did ));
1250 ibuf += sizeof( did );
1251 if (NULL == ( dir = dirlookup( vol, did ))) {
1252 LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1256 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1257 memcpy(rbuf, ibuf, sizeof( bitmap ));
1258 bitmap = ntohs( bitmap );
1259 ibuf += sizeof( bitmap );
1260 rbuf += sizeof( bitmap );
1261 *rbuflen += sizeof( bitmap );
1263 /* skip maxreplysize */
1266 /* get full path and handle file/dir subtleties in netatalk code*/
1267 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1268 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1269 return AFPERR_NOOBJ;
1271 if (!s_path->st_valid)
1272 of_statdir(vol, s_path);
1273 if ( s_path->st_errno != 0 ) {
1274 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1275 return AFPERR_NOOBJ;
1278 /* Shall we return owner UUID ? */
1279 if (bitmap & kFileSec_UUID) {
1280 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1281 if (NULL == (pw = getpwuid(s_path->st.st_uid)))
1283 LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1284 if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1286 rbuf += UUID_BINSIZE;
1287 *rbuflen += UUID_BINSIZE;
1290 /* Shall we return group UUID ? */
1291 if (bitmap & kFileSec_GRPUUID) {
1292 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1293 if (NULL == (gr = getgrgid(s_path->st.st_gid)))
1295 LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1296 if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1298 rbuf += UUID_BINSIZE;
1299 *rbuflen += UUID_BINSIZE;
1302 /* Shall we return ACL ? */
1303 if (bitmap & kFileSec_ACL) {
1304 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1305 get_and_map_acl(s_path->u_name, rbuf, rbuflen);
1308 LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1312 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1318 uint16_t vid, bitmap;
1319 struct path *s_path;
1321 LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1325 memcpy(&vid, ibuf, sizeof( vid ));
1326 ibuf += sizeof(vid);
1327 if (NULL == ( vol = getvolbyvid( vid ))) {
1328 LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1329 return AFPERR_NOOBJ;
1332 memcpy(&did, ibuf, sizeof( did ));
1333 ibuf += sizeof( did );
1334 if (NULL == ( dir = dirlookup( vol, did ))) {
1335 LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1339 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1340 bitmap = ntohs( bitmap );
1341 ibuf += sizeof( bitmap );
1343 /* get full path and handle file/dir subtleties in netatalk code*/
1344 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1345 LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1346 return AFPERR_NOOBJ;
1348 if (!s_path->st_valid)
1349 of_statdir(vol, s_path);
1350 if ( s_path->st_errno != 0 ) {
1351 LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1352 return AFPERR_NOOBJ;
1354 LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1357 if ((unsigned long)ibuf & 1)
1360 /* Start processing request */
1362 /* Change owner: dont even try */
1363 if (bitmap & kFileSec_UUID) {
1364 LOG(log_note, logtype_afpd, "afp_setacl: change owner request, discarded");
1365 ret = AFPERR_ACCESS;
1366 ibuf += UUID_BINSIZE;
1369 /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1370 if (bitmap & kFileSec_UUID) {
1371 LOG(log_note, logtype_afpd, "afp_setacl: change group request, not supported");
1373 ibuf += UUID_BINSIZE;
1377 if (bitmap & kFileSec_REMOVEACL) {
1378 LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1379 if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1380 LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1384 if (bitmap & kFileSec_ACL) {
1385 LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1386 /* Get no of ACEs the client put on the wire */
1388 memcpy(&ace_count, ibuf, sizeof(uint32_t));
1389 ace_count = htonl(ace_count);
1390 ibuf += 8; /* skip ACL flags (see acls.h) */
1394 (bitmap & kFileSec_Inherit),
1395 (darwin_ace_t *)ibuf,
1400 LOG(log_warning, logtype_afpd, "afp_setacl(\"%s/%s\"): error",
1401 getcwdpath(), s_path->u_name);
1406 LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1411 unix.c/accessmode calls this: map ACL to OS 9 mode
1413 int acltoownermode(char *path, struct stat *st, uid_t uid, struct maccess *ma)
1417 uint32_t rights = 0;
1419 if ( ! (AFPobj->options.flags & OPTION_UUID)
1421 ! (AFPobj->options.flags & OPTION_ACL2MACCESS))
1424 LOG(log_maxdebug, logtype_afpd, "acltoownermode('%s')", path);
1426 EC_NULL_LOG(pw = getpwuid(uid));
1428 #ifdef HAVE_SOLARIS_ACLS
1429 EC_ZERO_LOG(solaris_acl_rights(path, st, pw, &rights));
1431 #ifdef HAVE_POSIX_ACLS
1434 LOG(log_debug, logtype_afpd, "rights: 0x%08x", rights);
1436 LOG(log_maxdebug, logtype_afpd, "acltoownermode: ma_user before: %04o",ma->ma_user);
1437 if (rights & DARWIN_ACE_READ_DATA)
1438 ma->ma_user |= AR_UREAD;
1439 if (rights & DARWIN_ACE_WRITE_DATA)
1440 ma->ma_user |= AR_UWRITE;
1441 if (rights & (DARWIN_ACE_EXECUTE | DARWIN_ACE_SEARCH))
1442 ma->ma_user |= AR_USEARCH;
1443 LOG(log_maxdebug, logtype_afpd, "acltoownermode: ma_user after: %04o", ma->ma_user);