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/adouble.h>
35 #include <atalk/vfs.h>
36 #include <atalk/afp.h>
37 #include <atalk/util.h>
38 #include <atalk/cnid.h>
39 #include <atalk/logger.h>
40 #include <atalk/uuid.h>
41 #include <atalk/acl.h>
43 #include "directory.h"
49 #include "acl_mappings.h"
52 #define SOLARIS_2_DARWIN 1
53 #define DARWIN_2_SOLARIS 2
54 #define POSIX_DEFAULT_2_DARWIN 3
55 #define POSIX_ACCESS_2_DARWIN 4
56 #define DARWIN_2_POSIX 5
61 /********************************************************
62 * Basic and helper funcs
63 ********************************************************/
66 Takes a users name, uid and primary gid and checks if user is member of any group
67 Returns -1 if no or error, 0 if yes
69 static int check_group(char *name, uid_t uid _U_, gid_t pgid, gid_t path_gid)
77 grp = getgrgid(path_gid);
82 while (grp->gr_mem[i] != NULL) {
83 if ( (strcmp(grp->gr_mem[i], name)) == 0 ) {
84 LOG(log_debug, logtype_afpd, "check_group: requested user:%s is member of: %s", name, grp->gr_name);
93 /********************************************************
95 ********************************************************/
97 #ifdef HAVE_SOLARIS_ACLS
99 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
101 static int strip_trivial_aces(ace_t **saces, int sacecount)
105 ace_t *aces = *saces;
108 /* Count non-trivial ACEs */
109 for (i=0; i < sacecount; ) {
110 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
114 /* malloc buffer for new ACL */
115 if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
116 LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
120 /* Copy non-trivial ACEs */
121 for (i=0, j=0; i < sacecount; ) {
122 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
123 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
132 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
138 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
140 static int strip_nontrivial_aces(ace_t **saces, int sacecount)
144 ace_t *aces = *saces;
147 /* Count trivial ACEs */
148 for (i=0; i < sacecount; ) {
149 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
153 /* malloc buffer for new ACL */
154 if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
155 LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
159 /* Copy trivial ACEs */
160 for (i=0, j=0; i < sacecount; ) {
161 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
162 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
171 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
179 static ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
184 /* malloc buffer for new ACL */
185 if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
186 LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
190 /* Copy ACEs from buf1 */
191 for (i=0; i < ace1count; ) {
192 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
198 /* Copy ACEs from buf2 */
199 for (i=0; i < ace2count; ) {
200 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
209 Maps ACE array from Solaris to Darwin. Darwin ACEs are stored in network byte order.
210 Return numer of mapped ACEs or -1 on error.
211 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
213 static int map_aces_solaris_to_darwin(ace_t *aces, darwin_ace_t *darwin_aces, int ace_count)
218 struct passwd *pwd = NULL;
219 struct group *grp = NULL;
221 LOG(log_debug7, logtype_afpd, "map_aces_solaris_to_darwin: parsing %d ACES", ace_count);
224 LOG(log_debug7, logtype_afpd, "map_aces_solaris_to_darwin: parsing ACE No. %d", ace_count + 1);
225 /* if its a ACE resulting from nfsv4 mode mapping, discard it */
226 if (aces->a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
227 LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: trivial ACE");
232 if ( ! (aces->a_flags & ACE_IDENTIFIER_GROUP) ) {
234 LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: found user ACE with uid: %d", aces->a_who);
235 pwd = getpwuid(aces->a_who);
237 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: getpwuid error: %s", strerror(errno));
240 LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: uid: %d -> name: %s", aces->a_who, pwd->pw_name);
241 if ( (getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid)) != 0) {
242 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: getuuidfromname error");
246 /* its a group ace */
247 LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: found group ACE with gid: %d", aces->a_who);
248 grp = getgrgid(aces->a_who);
250 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: getgrgid error: %s", strerror(errno));
253 LOG(log_debug, logtype_afpd, "map_aces_solaris_to_darwin: gid: %d -> name: %s", aces->a_who, grp->gr_name);
254 if ( (getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid)) != 0) {
255 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: getuuidfromname error");
261 if (aces->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE)
262 flags = DARWIN_ACE_FLAGS_PERMIT;
263 else if (aces->a_type == ACE_ACCESS_DENIED_ACE_TYPE)
264 flags = DARWIN_ACE_FLAGS_DENY;
265 else { /* unsupported type */
269 for(i=0; nfsv4_to_darwin_flags[i].from != 0; i++) {
270 if (aces->a_flags & nfsv4_to_darwin_flags[i].from)
271 flags |= nfsv4_to_darwin_flags[i].to;
273 darwin_aces->darwin_ace_flags = htonl(flags);
277 for (i=0; nfsv4_to_darwin_rights[i].from != 0; i++) {
278 if (aces->a_access_mask & nfsv4_to_darwin_rights[i].from)
279 rights |= nfsv4_to_darwin_rights[i].to;
281 darwin_aces->darwin_ace_rights = htonl(rights);
292 Maps ACE array from Darwin to Solaris. Darwin ACEs are expected in network byte order.
293 Return numer of mapped ACEs or -1 on error.
294 All errors while mapping (e.g. getting UUIDs from LDAP) are fatal.
296 int map_aces_darwin_to_solaris(darwin_ace_t *darwin_aces, ace_t *nfsv4_aces, int ace_count)
298 int i, mapped_aces = 0;
299 uint32_t darwin_ace_flags;
300 uint32_t darwin_ace_rights;
301 uint16_t nfsv4_ace_flags;
302 uint32_t nfsv4_ace_rights;
310 nfsv4_ace_rights = 0;
313 if ( (getnamefromuuid(darwin_aces->darwin_ace_uuid, &name, &uuidtype)) != 0)
315 if (uuidtype == UUID_USER) {
316 pwd = getpwnam(name);
318 LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: getpwnam: %s", strerror(errno));
321 nfsv4_aces->a_who = pwd->pw_uid;
322 } else { /* hopefully UUID_GROUP*/
323 grp = getgrnam(name);
325 LOG(log_error, logtype_afpd, "map_aces_darwin_to_solaris: getgrnam: %s", strerror(errno));
328 nfsv4_aces->a_who = (uid_t)(grp->gr_gid);
329 nfsv4_ace_flags |= ACE_IDENTIFIER_GROUP;
334 /* now type: allow/deny */
335 darwin_ace_flags = ntohl(darwin_aces->darwin_ace_flags);
336 if (darwin_ace_flags & DARWIN_ACE_FLAGS_PERMIT)
337 nfsv4_aces->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
338 else if (darwin_ace_flags & DARWIN_ACE_FLAGS_DENY)
339 nfsv4_aces->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
340 else { /* unsupported type */
345 for(i=0; darwin_to_nfsv4_flags[i].from != 0; i++) {
346 if (darwin_ace_flags & darwin_to_nfsv4_flags[i].from)
347 nfsv4_ace_flags |= darwin_to_nfsv4_flags[i].to;
351 darwin_ace_rights = ntohl(darwin_aces->darwin_ace_rights);
352 for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
353 if (darwin_ace_rights & darwin_to_nfsv4_rights[i].from)
354 nfsv4_ace_rights |= darwin_to_nfsv4_rights[i].to;
357 LOG(log_debug9, logtype_afpd, "map_aces_darwin_to_solaris: ACE flags: Darwin:%08x -> NFSv4:%04x", darwin_ace_flags, nfsv4_ace_flags);
358 LOG(log_debug9, logtype_afpd, "map_aces_darwin_to_solaris: ACE rights: Darwin:%08x -> NFSv4:%08x", darwin_ace_rights, nfsv4_ace_rights);
360 nfsv4_aces->a_flags = nfsv4_ace_flags;
361 nfsv4_aces->a_access_mask = nfsv4_ace_rights;
370 #endif /* HAVE_SOLARIS_ACLS */
373 * Map ACEs from POSIX to Darwin.
374 * type is either POSIX_DEFAULT_2_DARWIN or POSIX_ACCESS_2_DARWIN, cf. acl_get_file.
375 * Return number of mapped ACES, -1 on error.
377 #ifdef HAVE_POSIX_ACLS
378 static int map_acl_posix_to_darwin(int type, const acl_t acl, darwin_ace_t *darwin_aces)
381 int entry_id = ACL_FIRST_ENTRY;
384 acl_permset_t permset, mask;
387 struct passwd *pwd = NULL;
388 struct group *grp = NULL;
391 darwin_ace_t *saved_darwin_aces = darwin_aces;
393 LOG(log_maxdebug, logtype_afpd, "map_aces_posix_to_darwin(%s)",
394 type == POSIX_DEFAULT_2_DARWIN ?
395 "POSIX_DEFAULT_2_DARWIN" : "POSIX_ACCESS_2_DARWIN");
397 /* itereate through all ACEs */
398 while (acl_get_entry(acl, entry_id, &e) == 1) {
399 entry_id = ACL_NEXT_ENTRY;
402 if (acl_get_tag_type(e, &tag) != 0) {
403 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: acl_get_tag_type: %s", strerror(errno));
408 /* we return user and group ACE */
411 if ((uid = (uid_t *)acl_get_qualifier(e)) == NULL) {
412 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: acl_get_qualifier: %s",
417 pwd = getpwuid(*uid);
419 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: getpwuid error: %s",
424 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: uid: %d -> name: %s",
426 if (getuuidfromname(pwd->pw_name, UUID_USER, darwin_aces->darwin_ace_uuid) != 0) {
427 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: getuuidfromname error");
436 if ((gid = (gid_t *)acl_get_qualifier(e)) == NULL) {
437 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: acl_get_qualifier: %s",
442 grp = getgrgid(*gid);
444 LOG(log_error, logtype_afpd, "map_aces_posix_to_darwin: getgrgid error: %s",
449 LOG(log_debug, logtype_afpd, "map_aces_posix_to_darwin: gid: %d -> name: %s",
451 if (getuuidfromname(grp->gr_name, UUID_GROUP, darwin_aces->darwin_ace_uuid) != 0) {
452 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: getuuidfromname error");
460 /* store mask so we can apply it later in a second loop */
462 if (acl_get_permset(e, &mask) != 0) {
463 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: acl_get_permset: %s", strerror(errno));
473 flags = DARWIN_ACE_FLAGS_PERMIT;
474 if ((type & MAP_MASK) == POSIX_DEFAULT_2_DARWIN)
475 flags |= DARWIN_ACE_FLAGS_FILE_INHERIT
476 | DARWIN_ACE_FLAGS_DIRECTORY_INHERIT
477 | DARWIN_ACE_FLAGS_ONLY_INHERIT;
478 darwin_aces->darwin_ace_flags = htonl(flags);
481 if (acl_get_permset(e, &permset) != 0) {
482 LOG(log_error, logtype_afpd, "map_aces_solaris_to_darwin: acl_get_permset: %s", strerror(errno));
486 if (acl_get_perm(permset, ACL_READ))
487 rights = DARWIN_ACE_READ_DATA
488 | DARWIN_ACE_READ_EXTATTRIBUTES
489 | DARWIN_ACE_READ_ATTRIBUTES
490 | DARWIN_ACE_READ_SECURITY;
491 if (acl_get_perm(permset, ACL_WRITE)) {
492 rights |= DARWIN_ACE_WRITE_DATA
493 | DARWIN_ACE_APPEND_DATA
494 | DARWIN_ACE_WRITE_EXTATTRIBUTES
495 | DARWIN_ACE_WRITE_ATTRIBUTES;
496 if ((type & ~MAP_MASK) == IS_DIR)
497 rights |= DARWIN_ACE_DELETE;
499 if (acl_get_perm(permset, ACL_EXECUTE))
500 rights |= DARWIN_ACE_EXECUTE;
502 darwin_aces->darwin_ace_rights = htonl(rights);
508 /* Loop through the mapped ACE buffer once again, applying the mask */
509 /* Map the mask to Darwin ACE rights first */
510 if (acl_get_perm(mask, ACL_READ))
511 rights = DARWIN_ACE_READ_DATA
512 | DARWIN_ACE_READ_EXTATTRIBUTES
513 | DARWIN_ACE_READ_ATTRIBUTES
514 | DARWIN_ACE_READ_SECURITY;
515 if (acl_get_perm(mask, ACL_WRITE)) {
516 rights |= DARWIN_ACE_WRITE_DATA
517 | DARWIN_ACE_APPEND_DATA
518 | DARWIN_ACE_WRITE_EXTATTRIBUTES
519 | DARWIN_ACE_WRITE_ATTRIBUTES;
520 if ((type & ~MAP_MASK) == IS_DIR)
521 rights |= DARWIN_ACE_DELETE;
523 if (acl_get_perm(mask, ACL_EXECUTE))
524 rights |= DARWIN_ACE_EXECUTE;
527 saved_darwin_aces->darwin_ace_rights &= htonl(rights);
532 if (uid) acl_free(uid);
533 if (gid) acl_free(gid);
540 * Multiplex ACL mapping (SOLARIS_2_DARWIN, DARWIN_2_SOLARIS, POSIX_2_DARWIN, DARWIN_2_POSIX).
541 * Reads from 'aces' buffer, writes to 'rbuf' buffer.
542 * Caller must provide buffer.
543 * Darwin ACEs are read and written in network byte order.
544 * Needs to know how many ACEs are in the ACL (ace_count) for Solaris ACLs.
545 * Ignores trivial ACEs.
546 * Return no of mapped ACEs or -1 on error.
548 static int map_acl(int type, const void *acl, darwin_ace_t *buf, int ace_count)
552 LOG(log_debug9, logtype_afpd, "map_acl: BEGIN");
554 switch (type & MAP_MASK) {
556 #ifdef HAVE_SOLARIS_ACLS
557 case SOLARIS_2_DARWIN:
558 mapped_aces = map_aces_solaris_to_darwin( acl, buf, ace_count);
561 case DARWIN_2_SOLARIS:
562 mapped_aces = map_aces_darwin_to_solaris( buf, acl, ace_count);
564 #endif /* HAVE_SOLARIS_ACLS */
566 #ifdef HAVE_POSIX_ACLS
567 case POSIX_DEFAULT_2_DARWIN:
568 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
571 case POSIX_ACCESS_2_DARWIN:
572 mapped_aces = map_acl_posix_to_darwin(type, (const acl_t)acl, buf);
577 #endif /* HAVE_POSIX_ACLS */
584 LOG(log_debug9, logtype_afpd, "map_acl: END");
588 /* Get ACL from object omitting trivial ACEs. Map to Darwin ACL style and
589 store Darwin ACL at rbuf. Add length of ACL written to rbuf to *rbuflen.
590 Returns 0 on success, -1 on error. */
591 static int get_and_map_acl(char *name, char *rbuf, size_t *rbuflen)
596 uint32_t *darwin_ace_count = (u_int32_t *)rbuf;
597 #ifdef HAVE_SOLARIS_ACLS
600 #ifdef HAVE_POSIX_ACLS
603 LOG(log_debug9, logtype_afpd, "get_and_map_acl: BEGIN");
605 /* Skip length and flags */
610 #ifdef HAVE_SOLARIS_ACLS
611 if ( (ace_count = get_nfsv4_acl(name, &aces)) == -1) {
612 LOG(log_error, logtype_afpd, "get_and_map_acl: couldnt get ACL");
616 if ( (mapped_aces = map_acl(SOLARIS_2_DARWIN, aces, (darwin_ace_t *)rbuf, ace_count)) == -1) {
620 #endif /* HAVE_SOLARIS_ACLS */
622 #ifdef HAVE_POSIX_ACLS
623 acl_t defacl = NULL , accacl = NULL;
625 /* stat to check if its a dir */
626 if (stat(name, &st) != 0) {
627 LOG(log_error, logtype_afpd, "get_and_map_acl: stat: %s", strerror(errno));
632 /* if its a dir, check for default acl too */
634 if (S_ISDIR(st.st_mode)) {
636 if ((defacl = acl_get_file(name, ACL_TYPE_DEFAULT)) == NULL && errno != ENOTDIR) {
637 LOG(log_error, logtype_afpd, "get_and_map_acl: couldnt get default ACL");
641 if (defacl && (mapped_aces = map_acl(POSIX_DEFAULT_2_DARWIN | dirflag,
643 (darwin_ace_t *)rbuf,
650 if ((accacl = acl_get_file(name, ACL_TYPE_ACCESS)) == NULL) {
651 LOG(log_error, logtype_afpd, "get_and_map_acl: couldnt get access ACL");
656 if (accacl && (mapped_aces += map_acl(POSIX_ACCESS_2_DARWIN | dirflag,
658 (darwin_ace_t *)rbuf + mapped_aces * sizeof(darwin_ace_t),
663 #endif /* HAVE_POSIX_ACLS */
665 LOG(log_debug, logtype_afpd, "get_and_map_acl: mapped %d ACEs", mapped_aces);
668 *darwin_ace_count = htonl(mapped_aces);
669 *rbuflen += sizeof(darwin_acl_header_t) + (mapped_aces * sizeof(darwin_ace_t));
672 #ifdef HAVE_SOLARIS_ACLS
675 #ifdef HAVE_POSIX_ACLS
676 if (defacl) acl_free(defacl);
677 if (accacl) acl_free(accacl);
678 #endif /* HAVE_POSIX_ACLS */
680 LOG(log_debug9, logtype_afpd, "get_and_map_acl: END");
684 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
685 static int remove_acl(const struct vol *vol,const char *path, int dir)
689 /* Ressource etc. first */
690 if ((ret = vol->vfs->vfs_remove_acl(vol, path, dir)) != AFP_OK)
692 /* now the data fork or dir */
693 return (remove_acl_vfs(path));
698 - the client sends a complete list of ACEs, not only new ones. So we dont need to do
699 any combination business (one exception being 'kFileSec_Inherit': see next)
700 - client might request that we add inherited ACEs via 'kFileSec_Inherit'.
701 We will store inherited ACEs first, which is Darwins canonical order.
702 - returns AFPerror code
704 #ifdef HAVE_SOLARIS_ACLS
705 static int set_acl(const struct vol *vol, char *name, int inherit, char *ibuf)
707 int ret, i, nfsv4_ace_count, tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
708 ace_t *old_aces, *new_aces = NULL;
712 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
714 /* Get no of ACEs the client put on the wire */
715 ace_count = htonl(*((uint32_t *)ibuf));
716 ibuf += 8; /* skip ACL flags (see acls.h) */
719 /* inherited + trivial ACEs */
720 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
722 /* only trivial ACEs */
723 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
725 /* Get existing ACL and count ACEs which have to be copied */
726 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
728 for ( i=0; i < nfsv4_ace_count; i++) {
729 if (old_aces[i].a_flags & flags)
733 /* Now malloc buffer exactly sized to fit all new ACEs */
734 new_aces = malloc( (ace_count + tocopy_aces_count) * sizeof(ace_t) );
735 if (new_aces == NULL) {
736 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
741 /* Start building new ACL */
743 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
744 inherited ACEs first. */
746 for (i=0; i < nfsv4_ace_count; i++) {
747 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
748 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
753 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
755 /* Now the ACEs from the client */
756 ret = map_acl(DARWIN_2_SOLARIS, &new_aces[new_aces_count], (darwin_ace_t *)ibuf, ace_count);
761 new_aces_count += ace_count;
762 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
764 /* Now copy the trivial ACEs */
765 for (i=0; i < nfsv4_ace_count; i++) {
766 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
767 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
772 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
774 /* Ressourcefork first.
775 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
776 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
777 if ( (ret = vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
778 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
779 if (errno == (EACCES | EPERM))
781 else if (errno == ENOENT)
787 if ( (ret = acl(name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
788 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
789 if (errno == (EACCES | EPERM))
791 else if (errno == ENOENT)
804 LOG(log_debug9, logtype_afpd, "set_acl: END");
807 #endif /* HAVE_SOLARIS_ACLS */
809 #ifdef HAVE_POSIX_ACLS
810 static int set_acl(const struct vol *vol, char *name, int inherit, char *ibuf)
814 #endif /* HAVE_POSIX_ACLS */
817 Checks if a given UUID has requested_rights(type darwin_ace_rights) for path.
818 Note: this gets called frequently and is a good place for optimizations !
820 #ifdef HAVE_SOLARIS_ACLS
821 static int check_acl_access(const char *path, const uuidp_t uuid, uint32_t requested_darwin_rights)
823 int ret, i, ace_count, dir, checkgroup;
824 char *username; /* might be group too */
828 uint32_t requested_rights = 0, allowed_rights = 0, denied_rights = 0;
832 int check_user_trivace = 0, check_group_trivace = 0;
839 LOG(log_debug9, logtype_afpd, "check_access: BEGIN. Request: %08x", requested_darwin_rights);
841 /* Get uid or gid from UUID */
842 if ( (getnamefromuuid(uuid, &username, &uuidtype)) != 0) {
843 LOG(log_error, logtype_afpd, "check_access: error getting name from UUID");
848 if ((lstat(path, &st)) != 0) {
849 LOG(log_error, logtype_afpd, "check_access: stat: %s", strerror(errno));
853 dir = S_ISDIR(st.st_mode);
855 if (uuidtype == UUID_USER) {
856 pwd = getpwnam(username);
858 LOG(log_error, logtype_afpd, "check_access: getpwnam: %s", strerror(errno));
865 /* If user is file/dir owner we must check the user trivial ACE */
866 if (uid == st.st_uid) {
867 LOG(log_debug, logtype_afpd, "check_access: user: %s is files owner. Must check trivial user ACE", username);
868 check_user_trivace = 1;
871 /* Now check if requested user is files owning group. If yes we must check the group trivial ACE */
872 if ( (check_group(username, uid, pgid, st.st_gid)) == 0) {
873 LOG(log_debug, logtype_afpd, "check_access: user: %s is in group: %d. Must check trivial group ACE", username, st.st_gid);
874 check_group_trivace = 1;
876 } else { /* hopefully UUID_GROUP*/
877 LOG(log_error, logtype_afpd, "check_access: afp_access for UUID of groups not supported!");
879 grp = getgrnam(username);
881 LOG(log_error, logtype_afpd, "check_access: getgrnam: %s", strerror(errno));
884 if (st.st_gid == grp->gr_gid )
885 check_group_trivace = 1;
889 /* Map requested rights to Solaris style. */
890 for (i=0; darwin_to_nfsv4_rights[i].from != 0; i++) {
891 if (requested_darwin_rights & darwin_to_nfsv4_rights[i].from)
892 requested_rights |= darwin_to_nfsv4_rights[i].to;
895 /* Get ACL from file/dir */
896 if ( (ace_count = get_nfsv4_acl(path, &aces)) == -1) {
897 LOG(log_error, logtype_afpd, "check_access: error getting ACEs");
901 /* Now check requested rights */
904 do { /* Loop through ACEs */
906 flags = aces[i].a_flags;
907 type = aces[i].a_type;
908 rights = aces[i].a_access_mask;
910 if (flags & ACE_INHERIT_ONLY_ACE)
913 /* Check if its a group ACE and set checkgroup to 1 if yes */
915 if ( (flags & ACE_IDENTIFIER_GROUP) && !(flags & ACE_GROUP) ) {
916 if ( (check_group(username, uid, pgid, who)) == 0)
922 /* Now the tricky part: decide if ACE effects our user. I'll explain:
923 if its a dedicated (non trivial) ACE for the user
925 if its a ACE for a group we're member of
927 if its a trivial ACE_OWNER ACE and requested UUID is the owner
929 if its a trivial ACE_GROUP ACE and requested UUID is group
931 if its a trivial ACE_EVERYONE ACE
935 ( (who == uid) && !(flags & (ACE_TRIVIAL|ACE_IDENTIFIER_GROUP)) ) ||
937 ( (flags & ACE_OWNER) && check_user_trivace ) ||
938 ( (flags & ACE_GROUP) && check_group_trivace ) ||
939 ( flags & ACE_EVERYONE )
941 /* Found an applicable ACE */
942 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
943 allowed_rights |= rights;
944 else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
945 /* Only or to denied rights if not previously allowed !! */
946 denied_rights |= ((!allowed_rights) & rights);
948 } while (++i < ace_count);
951 /* Darwin likes to ask for "delete_child" on dir,
952 "write_data" is actually the same, so we add that for dirs */
953 if (dir && (allowed_rights & ACE_WRITE_DATA))
954 allowed_rights |= ACE_DELETE_CHILD;
956 if (requested_rights & denied_rights) {
957 LOG(log_debug, logtype_afpd, "check_access: some requested right was denied:");
959 } else if ((requested_rights & allowed_rights) != requested_rights) {
960 LOG(log_debug, logtype_afpd, "check_access: some requested right wasn't allowed:");
963 LOG(log_debug, logtype_afpd, "check_access: all requested rights are allowed:");
967 LOG(log_debug, logtype_afpd, "check_access: Requested rights: %08x, allowed_rights: %08x, denied_rights: %08x, Result: %d",
968 requested_rights, allowed_rights, denied_rights, ret);
974 LOG(log_debug9, logtype_afpd, "check_access: END");
978 #endif /* HAVE_SOLARIS_ACLS */
980 #ifdef HAVE_POSIX_ACLS
981 static int check_acl_access(const char *path, const uuidp_t uuid, uint32_t requested_darwin_rights)
985 #endif /* HAVE_POSIX_ACLS */
987 /********************************************************
989 ********************************************************/
991 int afp_access(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
996 uint32_t did, darwin_ace_rights;
1001 LOG(log_debug9, logtype_afpd, "afp_access: BEGIN");
1006 memcpy(&vid, ibuf, sizeof( vid ));
1007 ibuf += sizeof(vid);
1008 if (NULL == ( vol = getvolbyvid( vid ))) {
1009 LOG(log_error, logtype_afpd, "afp_access: error getting volid:%d", vid);
1010 return AFPERR_NOOBJ;
1013 memcpy(&did, ibuf, sizeof( did ));
1014 ibuf += sizeof( did );
1015 if (NULL == ( dir = dirlookup( vol, did ))) {
1016 LOG(log_error, logtype_afpd, "afp_access: error getting did:%d", did);
1023 /* Store UUID address */
1024 uuid = (uuidp_t)ibuf;
1025 ibuf += UUID_BINSIZE;
1027 /* Store ACE rights */
1028 memcpy(&darwin_ace_rights, ibuf, 4);
1029 darwin_ace_rights = ntohl(darwin_ace_rights);
1032 /* get full path and handle file/dir subtleties in netatalk code*/
1033 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1034 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1035 return AFPERR_NOOBJ;
1037 if (!s_path->st_valid)
1038 of_statdir(vol, s_path);
1039 if ( s_path->st_errno != 0 ) {
1040 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1041 return AFPERR_NOOBJ;
1044 ret = check_acl_access(s_path->u_name, uuid, darwin_ace_rights);
1046 LOG(log_debug9, logtype_afpd, "afp_access: END");
1050 int afp_getacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1056 uint16_t vid, bitmap;
1057 struct path *s_path;
1061 LOG(log_debug9, logtype_afpd, "afp_getacl: BEGIN");
1065 memcpy(&vid, ibuf, sizeof( vid ));
1066 ibuf += sizeof(vid);
1067 if (NULL == ( vol = getvolbyvid( vid ))) {
1068 LOG(log_error, logtype_afpd, "afp_getacl: error getting volid:%d", vid);
1069 return AFPERR_NOOBJ;
1072 memcpy(&did, ibuf, sizeof( did ));
1073 ibuf += sizeof( did );
1074 if (NULL == ( dir = dirlookup( vol, did ))) {
1075 LOG(log_error, logtype_afpd, "afp_getacl: error getting did:%d", did);
1079 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1080 memcpy(rbuf, ibuf, sizeof( bitmap ));
1081 bitmap = ntohs( bitmap );
1082 ibuf += sizeof( bitmap );
1083 rbuf += sizeof( bitmap );
1084 *rbuflen += sizeof( bitmap );
1086 /* skip maxreplysize */
1089 /* get full path and handle file/dir subtleties in netatalk code*/
1090 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1091 LOG(log_error, logtype_afpd, "afp_getacl: cname got an error!");
1092 return AFPERR_NOOBJ;
1094 if (!s_path->st_valid)
1095 of_statdir(vol, s_path);
1096 if ( s_path->st_errno != 0 ) {
1097 LOG(log_error, logtype_afpd, "afp_getacl: cant stat");
1098 return AFPERR_NOOBJ;
1101 /* Shall we return owner UUID ? */
1102 if (bitmap & kFileSec_UUID) {
1103 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner user UUID");
1104 if (NULL == (pw = getpwuid(s_path->st.st_uid)))
1106 LOG(log_debug, logtype_afpd, "afp_getacl: got uid: %d, name: %s", s_path->st.st_uid, pw->pw_name);
1107 if ((ret = getuuidfromname(pw->pw_name, UUID_USER, rbuf)) != 0)
1109 rbuf += UUID_BINSIZE;
1110 *rbuflen += UUID_BINSIZE;
1113 /* Shall we return group UUID ? */
1114 if (bitmap & kFileSec_GRPUUID) {
1115 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files owner group UUID");
1116 if (NULL == (gr = getgrgid(s_path->st.st_gid)))
1118 LOG(log_debug, logtype_afpd, "afp_getacl: got gid: %d, name: %s", s_path->st.st_gid, gr->gr_name);
1119 if ((ret = getuuidfromname(gr->gr_name, UUID_GROUP, rbuf)) != 0)
1121 rbuf += UUID_BINSIZE;
1122 *rbuflen += UUID_BINSIZE;
1125 /* Shall we return ACL ? */
1126 if (bitmap & kFileSec_ACL) {
1127 LOG(log_debug, logtype_afpd, "afp_getacl: client requested files ACL");
1128 get_and_map_acl(s_path->u_name, rbuf, rbuflen);
1131 LOG(log_debug9, logtype_afpd, "afp_getacl: END");
1135 int afp_setacl(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1141 uint16_t vid, bitmap;
1142 struct path *s_path;
1144 LOG(log_debug9, logtype_afpd, "afp_setacl: BEGIN");
1148 memcpy(&vid, ibuf, sizeof( vid ));
1149 ibuf += sizeof(vid);
1150 if (NULL == ( vol = getvolbyvid( vid ))) {
1151 LOG(log_error, logtype_afpd, "afp_setacl: error getting volid:%d", vid);
1152 return AFPERR_NOOBJ;
1155 memcpy(&did, ibuf, sizeof( did ));
1156 ibuf += sizeof( did );
1157 if (NULL == ( dir = dirlookup( vol, did ))) {
1158 LOG(log_error, logtype_afpd, "afp_setacl: error getting did:%d", did);
1162 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1163 bitmap = ntohs( bitmap );
1164 ibuf += sizeof( bitmap );
1166 /* get full path and handle file/dir subtleties in netatalk code*/
1167 if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
1168 LOG(log_error, logtype_afpd, "afp_setacl: cname got an error!");
1169 return AFPERR_NOOBJ;
1171 if (!s_path->st_valid)
1172 of_statdir(vol, s_path);
1173 if ( s_path->st_errno != 0 ) {
1174 LOG(log_error, logtype_afpd, "afp_setacl: cant stat");
1175 return AFPERR_NOOBJ;
1177 LOG(log_debug, logtype_afpd, "afp_setacl: unixname: %s", s_path->u_name);
1180 if ((unsigned long)ibuf & 1)
1183 /* Start processing request */
1185 /* Change owner: dont even try */
1186 if (bitmap & kFileSec_UUID) {
1187 LOG(log_debug, logtype_afpd, "afp_setacl: change owner request. discarded.");
1188 ret = AFPERR_ACCESS;
1189 ibuf += UUID_BINSIZE;
1192 /* Change group: certain changes might be allowed, so try it. FIXME: not implemented yet. */
1193 if (bitmap & kFileSec_UUID) {
1194 LOG(log_debug, logtype_afpd, "afp_setacl: change group request. not supported this time.");
1196 ibuf += UUID_BINSIZE;
1200 if (bitmap & kFileSec_REMOVEACL) {
1201 LOG(log_debug, logtype_afpd, "afp_setacl: Remove ACL request.");
1202 if ((ret = remove_acl(vol, s_path->u_name, S_ISDIR(s_path->st.st_mode))) != AFP_OK)
1203 LOG(log_error, logtype_afpd, "afp_setacl: error from remove_acl");
1207 if (bitmap & kFileSec_ACL) {
1208 LOG(log_debug, logtype_afpd, "afp_setacl: Change ACL request.");
1210 /* Check if its our job to preserve inherited ACEs */
1211 if (bitmap & kFileSec_Inherit)
1212 ret = set_acl(vol, s_path->u_name, 1, ibuf);
1214 ret = set_acl(vol, s_path->u_name, 0, ibuf);
1221 LOG(log_debug9, logtype_afpd, "afp_setacl: END");
1226 unix.c/accessmode calls this: map ACL to OS 9 mode
1228 void acltoownermode(char *path, struct stat *st, uid_t uid, struct maccess *ma)
1232 int r_ok, w_ok, x_ok;
1234 if ( ! (AFPobj->options.flags & OPTION_UUID) || ! (AFPobj->options.flags & OPTION_ACL2OS9MODE))
1237 LOG(log_maxdebug, logtype_afpd, "acltoownermode('%s')", path);
1239 if ((pw = getpwuid(uid)) == NULL) {
1240 LOG(log_error, logtype_afpd, "acltoownermode: %s", strerror(errno));
1244 /* We need the UUID for check_acl_access */
1245 if ((getuuidfromname(pw->pw_name, UUID_USER, uuid)) != 0)
1248 /* These work for files and dirs */
1249 r_ok = check_acl_access(path, uuid, DARWIN_ACE_READ_DATA);
1250 w_ok = check_acl_access(path, uuid, (DARWIN_ACE_WRITE_DATA|DARWIN_ACE_APPEND_DATA));
1251 x_ok = check_acl_access(path, uuid, DARWIN_ACE_EXECUTE);
1253 LOG(log_debug7, logtype_afpd, "acltoownermode: ma_user before: %04o",ma->ma_user);
1255 ma->ma_user |= AR_UREAD;
1257 ma->ma_user |= AR_UWRITE;
1259 ma->ma_user |= AR_USEARCH;
1260 LOG(log_debug7, logtype_afpd, "acltoownermode: ma_user after: %04o", ma->ma_user);
1266 We're being called at the end of afp_createdir. We're (hopefully) inside dir
1267 and ".AppleDouble" and ".AppleDouble/.Parent" should have already been created.
1268 We then inherit any explicit ACE from "." to ".AppleDouble" and ".AppleDouble/.Parent".
1269 FIXME: add to VFS layer ?
1271 #ifdef HAVE_SOLARIS_ACLS
1272 void addir_inherit_acl(const struct vol *vol)
1274 ace_t *diraces = NULL, *adaces = NULL, *combinedaces = NULL;
1275 int diracecount, adacecount;
1277 LOG(log_debug9, logtype_afpd, "addir_inherit_acl: BEGIN");
1279 /* Check if ACLs are enabled for the volume */
1280 if (vol->v_flags & AFPVOL_ACLS) {
1282 if ((diracecount = get_nfsv4_acl(".", &diraces)) <= 0)
1284 /* Remove any trivial ACE from "." */
1285 if ((diracecount = strip_trivial_aces(&diraces, diracecount)) <= 0)
1289 Inherit to ".AppleDouble"
1292 if ((adacecount = get_nfsv4_acl(".AppleDouble", &adaces)) <= 0)
1294 /* Remove any non-trivial ACE from ".AppleDouble" */
1295 if ((adacecount = strip_nontrivial_aces(&adaces, adacecount)) <= 0)
1299 if ((combinedaces = concat_aces(diraces, diracecount, adaces, adacecount)) == NULL)
1302 /* Now set new acl */
1303 if ((acl(".AppleDouble", ACE_SETACL, diracecount + adacecount, combinedaces)) != 0)
1304 LOG(log_error, logtype_afpd, "addir_inherit_acl: acl: %s", strerror(errno));
1309 combinedaces = NULL;
1312 Inherit to ".AppleDouble/.Parent"
1315 if ((adacecount = get_nfsv4_acl(".AppleDouble/.Parent", &adaces)) <= 0)
1317 if ((adacecount = strip_nontrivial_aces(&adaces, adacecount)) <= 0)
1321 if ((combinedaces = concat_aces(diraces, diracecount, adaces, adacecount)) == NULL)
1324 /* Now set new acl */
1325 if ((acl(".AppleDouble/.Parent", ACE_SETACL, diracecount + adacecount, combinedaces)) != 0)
1326 LOG(log_error, logtype_afpd, "addir_inherit_acl: acl: %s", strerror(errno));
1332 LOG(log_debug9, logtype_afpd, "addir_inherit_acl: END");
1338 #endif /* HAVE_SOLARIS_ACLS */
1340 #ifdef HAVE_POSIX_ACLS
1341 void addir_inherit_acl(const struct vol *vol)
1345 #endif /* HAVE_POSIX_ACLS */