4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <acl/acl_common.h>
37 #include <sys/debug.h>
47 #include <acl_common.h>
51 #define ACE_POSIX_SUPPORTED_BITS (ACE_READ_DATA | \
52 ACE_WRITE_DATA | ACE_APPEND_DATA | ACE_EXECUTE | \
53 ACE_READ_ATTRIBUTES | ACE_READ_ACL | ACE_WRITE_ACL)
56 #define ACL_SYNCHRONIZE_SET_DENY 0x0000001
57 #define ACL_SYNCHRONIZE_SET_ALLOW 0x0000002
58 #define ACL_SYNCHRONIZE_ERR_DENY 0x0000004
59 #define ACL_SYNCHRONIZE_ERR_ALLOW 0x0000008
61 #define ACL_WRITE_OWNER_SET_DENY 0x0000010
62 #define ACL_WRITE_OWNER_SET_ALLOW 0x0000020
63 #define ACL_WRITE_OWNER_ERR_DENY 0x0000040
64 #define ACL_WRITE_OWNER_ERR_ALLOW 0x0000080
66 #define ACL_DELETE_SET_DENY 0x0000100
67 #define ACL_DELETE_SET_ALLOW 0x0000200
68 #define ACL_DELETE_ERR_DENY 0x0000400
69 #define ACL_DELETE_ERR_ALLOW 0x0000800
71 #define ACL_WRITE_ATTRS_OWNER_SET_DENY 0x0001000
72 #define ACL_WRITE_ATTRS_OWNER_SET_ALLOW 0x0002000
73 #define ACL_WRITE_ATTRS_OWNER_ERR_DENY 0x0004000
74 #define ACL_WRITE_ATTRS_OWNER_ERR_ALLOW 0x0008000
76 #define ACL_WRITE_ATTRS_WRITER_SET_DENY 0x0010000
77 #define ACL_WRITE_ATTRS_WRITER_SET_ALLOW 0x0020000
78 #define ACL_WRITE_ATTRS_WRITER_ERR_DENY 0x0040000
79 #define ACL_WRITE_ATTRS_WRITER_ERR_ALLOW 0x0080000
81 #define ACL_WRITE_NAMED_WRITER_SET_DENY 0x0100000
82 #define ACL_WRITE_NAMED_WRITER_SET_ALLOW 0x0200000
83 #define ACL_WRITE_NAMED_WRITER_ERR_DENY 0x0400000
84 #define ACL_WRITE_NAMED_WRITER_ERR_ALLOW 0x0800000
86 #define ACL_READ_NAMED_READER_SET_DENY 0x1000000
87 #define ACL_READ_NAMED_READER_SET_ALLOW 0x2000000
88 #define ACL_READ_NAMED_READER_ERR_DENY 0x4000000
89 #define ACL_READ_NAMED_READER_ERR_ALLOW 0x8000000
92 #define ACE_VALID_MASK_BITS (\
94 ACE_LIST_DIRECTORY | \
98 ACE_ADD_SUBDIRECTORY | \
99 ACE_READ_NAMED_ATTRS | \
100 ACE_WRITE_NAMED_ATTRS | \
103 ACE_READ_ATTRIBUTES | \
104 ACE_WRITE_ATTRIBUTES | \
111 #define ACE_MASK_UNDEFINED 0x80000000
113 #define ACE_VALID_FLAG_BITS (ACE_FILE_INHERIT_ACE | \
114 ACE_DIRECTORY_INHERIT_ACE | \
115 ACE_NO_PROPAGATE_INHERIT_ACE | ACE_INHERIT_ONLY_ACE | \
116 ACE_SUCCESSFUL_ACCESS_ACE_FLAG | ACE_FAILED_ACCESS_ACE_FLAG | \
117 ACE_IDENTIFIER_GROUP | ACE_OWNER | ACE_GROUP | ACE_EVERYONE)
120 * ACL conversion helpers
127 ace_group, /* includes GROUP and GROUP_OBJ */
129 } ace_to_aent_state_t;
131 typedef struct acevals {
140 typedef struct ace_list {
151 ace_to_aent_state_t state;
152 int seen; /* bitmask of all aclent_t a_type values seen */
155 ace_t trivial_acl[] = {
156 {(uid_t)-1, 0, ACE_OWNER, ACE_ACCESS_DENIED_ACE_TYPE},
157 {(uid_t)-1, ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES|
158 ACE_WRITE_NAMED_ATTRS, ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE},
159 {(uid_t)-1, 0, ACE_GROUP|ACE_IDENTIFIER_GROUP,
160 ACE_ACCESS_DENIED_ACE_TYPE},
161 {(uid_t)-1, 0, ACE_GROUP|ACE_IDENTIFIER_GROUP,
162 ACE_ACCESS_ALLOWED_ACE_TYPE},
163 {(uid_t)-1, ACE_WRITE_ACL|ACE_WRITE_OWNER| ACE_WRITE_ATTRIBUTES|
164 ACE_WRITE_NAMED_ATTRS, ACE_EVERYONE, ACE_ACCESS_DENIED_ACE_TYPE},
165 {(uid_t)-1, ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
166 ACE_SYNCHRONIZE, ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE}
171 adjust_ace_pair_common(void *pair, size_t access_off,
172 size_t pairsize, mode_t mode)
174 char *datap = (char *)pair;
175 uint32_t *amask0 = (uint32_t *)(uintptr_t)(datap + access_off);
176 uint32_t *amask1 = (uint32_t *)(uintptr_t)(datap + pairsize +
179 *amask1 |= ACE_READ_DATA;
181 *amask0 |= ACE_READ_DATA;
183 *amask1 |= ACE_WRITE_DATA|ACE_APPEND_DATA;
185 *amask0 |= ACE_WRITE_DATA|ACE_APPEND_DATA;
187 *amask1 |= ACE_EXECUTE;
189 *amask0 |= ACE_EXECUTE;
193 adjust_ace_pair(ace_t *pair, mode_t mode)
195 adjust_ace_pair_common(pair, offsetof(ace_t, a_access_mask),
196 sizeof (ace_t), mode);
200 ace_allow_deny_helper(uint16_t type, boolean_t *allow, boolean_t *deny)
202 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
204 else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
210 * determine whether an ace_t acl is trivial
212 * Trivialness implies that the acl is composed of only
213 * owner, group, everyone entries. ACL can't
214 * have read_acl denied, and write_owner/write_acl/write_attributes
215 * can only be owner@ entry.
218 ace_trivial_common(void *acep, int aclcnt,
219 uint64_t (*walk)(void *, uint64_t, int aclcnt,
220 uint16_t *, uint16_t *, uint32_t *))
222 boolean_t owner_allow = B_FALSE;
223 boolean_t group_allow = B_FALSE;
224 boolean_t everyone_allow = B_FALSE;
225 boolean_t owner_deny = B_FALSE;
226 boolean_t group_deny = B_FALSE;
227 boolean_t everyone_deny = B_FALSE;
233 while (cookie = walk(acep, cookie, aclcnt, &flags, &type, &mask)) {
234 switch (flags & ACE_TYPE_FLAGS) {
236 if (group_allow || group_deny || everyone_allow ||
239 ace_allow_deny_helper(type, &owner_allow, &owner_deny);
241 case ACE_GROUP|ACE_IDENTIFIER_GROUP:
242 if (everyone_allow || everyone_deny &&
243 (!owner_allow && !owner_deny))
245 ace_allow_deny_helper(type, &group_allow, &group_deny);
249 if (!owner_allow && !owner_deny &&
250 !group_allow && !group_deny)
252 ace_allow_deny_helper(type,
253 &everyone_allow, &everyone_deny);
260 if (flags & (ACE_FILE_INHERIT_ACE|
261 ACE_DIRECTORY_INHERIT_ACE|ACE_NO_PROPAGATE_INHERIT_ACE|
262 ACE_INHERIT_ONLY_ACE))
266 * Special check for some special bits
268 * Don't allow anybody to deny reading basic
269 * attributes or a files ACL.
271 if ((mask & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
272 (type == ACE_ACCESS_DENIED_ACE_TYPE))
276 * Allow on owner@ to allow
277 * write_acl/write_owner/write_attributes
279 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE &&
280 (!(flags & ACE_OWNER) && (mask &
281 (ACE_WRITE_OWNER|ACE_WRITE_ACL|ACE_WRITE_ATTRIBUTES))))
286 if (!owner_allow || !owner_deny || !group_allow || !group_deny ||
287 !everyone_allow || !everyone_deny)
294 ace_walk(void *datap, uint64_t cookie, int aclcnt, uint16_t *flags,
295 uint16_t *type, uint32_t *mask)
299 if (cookie >= aclcnt)
302 *flags = acep[cookie].a_flags;
303 *type = acep[cookie].a_type;
304 *mask = acep[cookie++].a_access_mask;
310 ace_trivial(ace_t *acep, int aclcnt)
312 return (ace_trivial_common(acep, aclcnt, ace_walk));
316 * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
317 * v = Ptr to array/vector of objs
318 * n = # objs in the array
319 * s = size of each obj (must be multiples of a word size)
320 * f = ptr to function to compare two objs
321 * returns (-1 = less than, 0 = equal, 1 = greater than
324 ksort(caddr_t v, int n, int s, int (*f)())
327 unsigned int *p1, *p2;
331 if (v == NULL || n <= 1)
334 /* Sanity check on arguments */
335 ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
337 for (g = n / 2; g > 0; g /= 2) {
338 for (i = g; i < n; i++) {
339 for (j = i - g; j >= 0 &&
340 (*f)(v + j * s, v + (j + g) * s) == 1;
342 p1 = (void *)(v + j * s);
343 p2 = (void *)(v + (j + g) * s);
344 for (ii = 0; ii < s / 4; ii++) {
355 * Compare two acls, all fields. Returns:
361 cmp2acls(void *a, void *b)
363 aclent_t *x = (aclent_t *)a;
364 aclent_t *y = (aclent_t *)b;
367 if (x->a_type < y->a_type)
369 if (x->a_type > y->a_type)
371 /* Equal types; compare id's */
372 if (x->a_id < y->a_id)
374 if (x->a_id > y->a_id)
376 /* Equal ids; compare perms */
377 if (x->a_perm < y->a_perm)
379 if (x->a_perm > y->a_perm)
387 cacl_realloc(void *ptr, size_t size, size_t new_size)
392 tmp = kmem_alloc(new_size, KM_SLEEP);
393 (void) memcpy(tmp, ptr, (size < new_size) ? size : new_size);
394 kmem_free(ptr, size);
397 return (realloc(ptr, new_size));
402 cacl_malloc(void **ptr, size_t size)
405 *ptr = kmem_zalloc(size, KM_SLEEP);
408 *ptr = calloc(1, size);
418 cacl_free(void *ptr, size_t size)
421 kmem_free(ptr, size);
427 #if !defined(_KERNEL)
429 acl_alloc(enum acl_type type)
433 if (cacl_malloc((void **)&aclp, sizeof (acl_t)) != 0)
436 aclp->acl_aclp = NULL;
441 aclp->acl_type = ACE_T;
442 aclp->acl_entry_size = sizeof (ace_t);
445 aclp->acl_type = ACLENT_T;
446 aclp->acl_entry_size = sizeof (aclent_t);
456 * Free acl_t structure
459 acl_free(acl_t *aclp)
466 if (aclp->acl_aclp) {
467 acl_size = aclp->acl_cnt * aclp->acl_entry_size;
468 cacl_free(aclp->acl_aclp, acl_size);
471 cacl_free(aclp, sizeof (acl_t));
475 access_mask_set(int haswriteperm, int hasreadperm, int isowner, int isallow)
477 uint32_t access_mask = 0;
479 int synchronize_set = 0, write_owner_set = 0;
480 int delete_set = 0, write_attrs_set = 0;
481 int read_named_set = 0, write_named_set = 0;
483 acl_produce = (ACL_SYNCHRONIZE_SET_ALLOW |
484 ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
485 ACL_WRITE_ATTRS_WRITER_SET_DENY);
488 synchronize_set = ACL_SYNCHRONIZE_SET_ALLOW;
489 write_owner_set = ACL_WRITE_OWNER_SET_ALLOW;
490 delete_set = ACL_DELETE_SET_ALLOW;
492 read_named_set = ACL_READ_NAMED_READER_SET_ALLOW;
494 write_named_set = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
496 write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
497 else if (haswriteperm)
498 write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
501 synchronize_set = ACL_SYNCHRONIZE_SET_DENY;
502 write_owner_set = ACL_WRITE_OWNER_SET_DENY;
503 delete_set = ACL_DELETE_SET_DENY;
505 read_named_set = ACL_READ_NAMED_READER_SET_DENY;
507 write_named_set = ACL_WRITE_NAMED_WRITER_SET_DENY;
509 write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_DENY;
510 else if (haswriteperm)
511 write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_DENY;
514 * If the entity is not the owner and does not
515 * have write permissions ACE_WRITE_ATTRIBUTES will
516 * always go in the DENY ACE.
518 access_mask |= ACE_WRITE_ATTRIBUTES;
521 if (acl_produce & synchronize_set)
522 access_mask |= ACE_SYNCHRONIZE;
523 if (acl_produce & write_owner_set)
524 access_mask |= ACE_WRITE_OWNER;
525 if (acl_produce & delete_set)
526 access_mask |= ACE_DELETE;
527 if (acl_produce & write_attrs_set)
528 access_mask |= ACE_WRITE_ATTRIBUTES;
529 if (acl_produce & read_named_set)
530 access_mask |= ACE_READ_NAMED_ATTRS;
531 if (acl_produce & write_named_set)
532 access_mask |= ACE_WRITE_NAMED_ATTRS;
534 return (access_mask);
538 * Given an mode_t, convert it into an access_mask as used
539 * by nfsace, assuming aclent_t -> nfsace semantics.
542 mode_to_ace_access(mode_t mode, int isdir, int isowner, int isallow)
545 int haswriteperm = 0;
549 haswriteperm = (mode & S_IWOTH);
550 hasreadperm = (mode & S_IROTH);
552 haswriteperm = !(mode & S_IWOTH);
553 hasreadperm = !(mode & S_IROTH);
557 * The following call takes care of correctly setting the following
558 * mask bits in the access_mask:
559 * ACE_SYNCHRONIZE, ACE_WRITE_OWNER, ACE_DELETE,
560 * ACE_WRITE_ATTRIBUTES, ACE_WRITE_NAMED_ATTRS, ACE_READ_NAMED_ATTRS
562 access = access_mask_set(haswriteperm, hasreadperm, isowner, isallow);
565 access |= ACE_READ_ACL | ACE_READ_ATTRIBUTES;
567 access |= ACE_WRITE_ACL;
570 access |= ACE_WRITE_ACL;
574 if (mode & S_IROTH) {
575 access |= ACE_READ_DATA;
578 if (mode & S_IWOTH) {
579 access |= ACE_WRITE_DATA |
582 access |= ACE_DELETE_CHILD;
586 access |= ACE_EXECUTE;
593 * Given an nfsace (presumably an ALLOW entry), make a
594 * corresponding DENY entry at the address given.
597 ace_make_deny(ace_t *allow, ace_t *deny, int isdir, int isowner)
599 (void) memcpy(deny, allow, sizeof (ace_t));
601 deny->a_who = allow->a_who;
603 deny->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
604 deny->a_access_mask ^= ACE_POSIX_SUPPORTED_BITS;
606 deny->a_access_mask ^= ACE_DELETE_CHILD;
608 deny->a_access_mask &= ~(ACE_SYNCHRONIZE | ACE_WRITE_OWNER |
609 ACE_DELETE | ACE_WRITE_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
610 ACE_WRITE_NAMED_ATTRS);
611 deny->a_access_mask |= access_mask_set((allow->a_access_mask &
612 ACE_WRITE_DATA), (allow->a_access_mask & ACE_READ_DATA), isowner,
616 * Make an initial pass over an array of aclent_t's. Gather
617 * information such as an ACL_MASK (if any), number of users,
618 * number of groups, and whether the array needs to be sorted.
621 ln_aent_preprocess(aclent_t *aclent, int n,
622 int *hasmask, mode_t *mask,
623 int *numuser, int *numgroup, int *needsort)
635 for (i = 0; i < n; i++) {
636 if (aclent[i].a_type < curtype)
638 else if (aclent[i].a_type > curtype)
639 curtype = aclent[i].a_type;
640 if (aclent[i].a_type & USER)
642 if (aclent[i].a_type & (GROUP | GROUP_OBJ))
644 if (aclent[i].a_type & CLASS_OBJ) {
650 *mask = aclent[i].a_perm;
655 if ((! *hasmask) && (*numuser + *numgroup > 1)) {
665 * Convert an array of aclent_t into an array of nfsace entries,
666 * following POSIX draft -> nfsv4 conversion semantics as outlined in
670 ln_aent_to_ace(aclent_t *aclent, int n, ace_t **acepp, int *rescount, int isdir)
674 int numuser, numgroup, needsort;
676 int i, groupi = 0, skip;
677 ace_t *acep, *result = NULL;
680 error = ln_aent_preprocess(aclent, n, &hasmask, &mask,
681 &numuser, &numgroup, &needsort);
685 /* allow + deny for each aclent */
689 * stick extra deny on the group_obj and on each
690 * user|group for the mask (the group_obj was added
691 * into the count for numgroup)
693 resultsize += numuser + numgroup;
694 /* ... and don't count the mask itself */
698 /* sort the source if necessary */
700 ksort((caddr_t)aclent, n, sizeof (aclent_t), cmp2acls);
702 if (cacl_malloc((void **)&result, resultsize * sizeof (ace_t)) != 0)
707 for (i = 0; i < n; i++) {
709 * don't process CLASS_OBJ (mask); mask was grabbed in
710 * ln_aent_preprocess()
712 if (aclent[i].a_type & CLASS_OBJ)
715 /* If we need an ACL_MASK emulator, prepend it now */
717 (aclent[i].a_type & (USER | GROUP | GROUP_OBJ))) {
718 acep->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
720 if (aclent[i].a_type & GROUP_OBJ) {
721 acep->a_who = (uid_t)-1;
723 (ACE_IDENTIFIER_GROUP|ACE_GROUP);
724 } else if (aclent[i].a_type & USER) {
725 acep->a_who = aclent[i].a_id;
727 acep->a_who = aclent[i].a_id;
728 acep->a_flags |= ACE_IDENTIFIER_GROUP;
730 if (aclent[i].a_type & ACL_DEFAULT) {
731 acep->a_flags |= ACE_INHERIT_ONLY_ACE |
732 ACE_FILE_INHERIT_ACE |
733 ACE_DIRECTORY_INHERIT_ACE;
736 * Set the access mask for the prepended deny
737 * ace. To do this, we invert the mask (found
738 * in ln_aent_preprocess()) then convert it to an
739 * DENY ace access_mask.
741 acep->a_access_mask = mode_to_ace_access((mask ^ 07),
746 /* handle a_perm -> access_mask */
747 acep->a_access_mask = mode_to_ace_access(aclent[i].a_perm,
748 isdir, aclent[i].a_type & USER_OBJ, 1);
750 /* emulate a default aclent */
751 if (aclent[i].a_type & ACL_DEFAULT) {
752 acep->a_flags |= ACE_INHERIT_ONLY_ACE |
753 ACE_FILE_INHERIT_ACE |
754 ACE_DIRECTORY_INHERIT_ACE;
758 * handle a_perm and a_id
760 * this must be done last, since it involves the
761 * corresponding deny aces, which are handled
762 * differently for each different a_type.
764 if (aclent[i].a_type & USER_OBJ) {
765 acep->a_who = (uid_t)-1;
766 acep->a_flags |= ACE_OWNER;
767 ace_make_deny(acep, acep + 1, isdir, B_TRUE);
769 } else if (aclent[i].a_type & USER) {
770 acep->a_who = aclent[i].a_id;
771 ace_make_deny(acep, acep + 1, isdir, B_FALSE);
773 } else if (aclent[i].a_type & (GROUP_OBJ | GROUP)) {
774 if (aclent[i].a_type & GROUP_OBJ) {
775 acep->a_who = (uid_t)-1;
776 acep->a_flags |= ACE_GROUP;
778 acep->a_who = aclent[i].a_id;
780 acep->a_flags |= ACE_IDENTIFIER_GROUP;
782 * Set the corresponding deny for the group ace.
784 * The deny aces go after all of the groups, unlike
785 * everything else, where they immediately follow
788 * We calculate "skip", the number of slots to
789 * skip ahead for the deny ace, here.
792 * MD1 A1 MD2 A2 MD3 A3 D1 D2 D3
794 * (2 * numgroup) - 1 - groupi
795 * (2 * numgroup) to account for MD + A
796 * - 1 to account for the fact that we're on the
797 * access (A), not the mask (MD)
798 * - groupi to account for the fact that we have
799 * passed up groupi number of MD's.
801 skip = (2 * numgroup) - 1 - groupi;
802 ace_make_deny(acep, acep + skip, isdir, B_FALSE);
804 * If we just did the last group, skip acep past
805 * all of the denies; else, just move ahead one.
807 if (++groupi >= numgroup)
808 acep += numgroup + 1;
811 } else if (aclent[i].a_type & OTHER_OBJ) {
812 acep->a_who = (uid_t)-1;
813 acep->a_flags |= ACE_EVERYONE;
814 ace_make_deny(acep, acep + 1, isdir, B_FALSE);
823 *rescount = resultsize;
827 if ((result != NULL) && (resultsize > 0)) {
828 cacl_free(result, resultsize * sizeof (ace_t));
836 convert_aent_to_ace(aclent_t *aclentp, int aclcnt, int isdir,
837 ace_t **retacep, int *retacecnt)
850 ksort((caddr_t)aclentp, aclcnt, sizeof (aclent_t), cmp2acls);
852 for (i = 0, aclp = aclentp; i < aclcnt; aclp++, i++) {
853 if (aclp->a_type & ACL_DEFAULT)
859 dfaclcnt = aclcnt - i;
862 if (dfaclcnt && isdir == 0) {
866 error = ln_aent_to_ace(aclentp, i, &acep, &acecnt, isdir);
871 error = ln_aent_to_ace(&aclentp[dfaclstart], dfaclcnt,
872 &dfacep, &dfacecnt, isdir);
875 cacl_free(acep, acecnt * sizeof (ace_t));
882 acesz = sizeof (ace_t) * acecnt;
883 dfacesz = sizeof (ace_t) * dfacecnt;
884 acep = cacl_realloc(acep, acesz, acesz + dfacesz);
888 (void) memcpy(acep + acecnt, dfacep, dfacesz);
892 cacl_free(dfacep, dfacecnt * sizeof (ace_t));
894 *retacecnt = acecnt + dfacecnt;
900 ace_mask_to_mode(uint32_t mask, o_mode_t *modep, int isdir)
904 uint32_t bits, wantbits;
907 if (mask & ACE_READ_DATA)
911 wantbits = (ACE_WRITE_DATA | ACE_APPEND_DATA);
913 wantbits |= ACE_DELETE_CHILD;
914 bits = mask & wantbits;
916 if (bits != wantbits) {
924 if (mask & ACE_EXECUTE) {
935 acevals_init(acevals_t *vals, uid_t key)
937 bzero(vals, sizeof (*vals));
938 vals->allowed = ACE_MASK_UNDEFINED;
939 vals->denied = ACE_MASK_UNDEFINED;
940 vals->mask = ACE_MASK_UNDEFINED;
945 ace_list_init(ace_list_t *al, int dfacl_flag)
947 acevals_init(&al->user_obj, 0);
948 acevals_init(&al->group_obj, 0);
949 acevals_init(&al->other_obj, 0);
954 al->state = ace_unused;
956 al->dfacl_flag = dfacl_flag;
960 * Find or create an acevals holder for a given id and avl tree.
962 * Note that only one thread will ever touch these avl trees, so
963 * there is no need for locking.
966 acevals_find(ace_t *ace, avl_tree_t *avl, int *num)
971 key.key = ace->a_who;
972 rc = avl_find(avl, &key, &where);
976 /* this memory is freed by ln_ace_to_aent()->ace_list_free() */
977 if (cacl_malloc((void **)&rc, sizeof (acevals_t)) != 0)
980 acevals_init(rc, ace->a_who);
981 avl_insert(avl, rc, where);
988 access_mask_check(ace_t *acep, int mask_bit, int isowner)
990 int set_deny, err_deny;
991 int set_allow, err_allow;
993 int haswriteperm, hasreadperm;
995 if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
996 haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 0 : 1;
997 hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 0 : 1;
999 haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 1 : 0;
1000 hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 1 : 0;
1003 acl_consume = (ACL_SYNCHRONIZE_ERR_DENY |
1004 ACL_DELETE_ERR_DENY |
1005 ACL_WRITE_OWNER_ERR_DENY |
1006 ACL_WRITE_OWNER_ERR_ALLOW |
1007 ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
1008 ACL_WRITE_ATTRS_OWNER_ERR_DENY |
1009 ACL_WRITE_ATTRS_WRITER_SET_DENY |
1010 ACL_WRITE_ATTRS_WRITER_ERR_ALLOW |
1011 ACL_WRITE_NAMED_WRITER_ERR_DENY |
1012 ACL_READ_NAMED_READER_ERR_DENY);
1014 if (mask_bit == ACE_SYNCHRONIZE) {
1015 set_deny = ACL_SYNCHRONIZE_SET_DENY;
1016 err_deny = ACL_SYNCHRONIZE_ERR_DENY;
1017 set_allow = ACL_SYNCHRONIZE_SET_ALLOW;
1018 err_allow = ACL_SYNCHRONIZE_ERR_ALLOW;
1019 } else if (mask_bit == ACE_WRITE_OWNER) {
1020 set_deny = ACL_WRITE_OWNER_SET_DENY;
1021 err_deny = ACL_WRITE_OWNER_ERR_DENY;
1022 set_allow = ACL_WRITE_OWNER_SET_ALLOW;
1023 err_allow = ACL_WRITE_OWNER_ERR_ALLOW;
1024 } else if (mask_bit == ACE_DELETE) {
1025 set_deny = ACL_DELETE_SET_DENY;
1026 err_deny = ACL_DELETE_ERR_DENY;
1027 set_allow = ACL_DELETE_SET_ALLOW;
1028 err_allow = ACL_DELETE_ERR_ALLOW;
1029 } else if (mask_bit == ACE_WRITE_ATTRIBUTES) {
1031 set_deny = ACL_WRITE_ATTRS_OWNER_SET_DENY;
1032 err_deny = ACL_WRITE_ATTRS_OWNER_ERR_DENY;
1033 set_allow = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
1034 err_allow = ACL_WRITE_ATTRS_OWNER_ERR_ALLOW;
1035 } else if (haswriteperm) {
1036 set_deny = ACL_WRITE_ATTRS_WRITER_SET_DENY;
1037 err_deny = ACL_WRITE_ATTRS_WRITER_ERR_DENY;
1038 set_allow = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
1039 err_allow = ACL_WRITE_ATTRS_WRITER_ERR_ALLOW;
1041 if ((acep->a_access_mask & mask_bit) &&
1042 (acep->a_type & ACE_ACCESS_ALLOWED_ACE_TYPE)) {
1047 } else if (mask_bit == ACE_READ_NAMED_ATTRS) {
1051 set_deny = ACL_READ_NAMED_READER_SET_DENY;
1052 err_deny = ACL_READ_NAMED_READER_ERR_DENY;
1053 set_allow = ACL_READ_NAMED_READER_SET_ALLOW;
1054 err_allow = ACL_READ_NAMED_READER_ERR_ALLOW;
1055 } else if (mask_bit == ACE_WRITE_NAMED_ATTRS) {
1059 set_deny = ACL_WRITE_NAMED_WRITER_SET_DENY;
1060 err_deny = ACL_WRITE_NAMED_WRITER_ERR_DENY;
1061 set_allow = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
1062 err_allow = ACL_WRITE_NAMED_WRITER_ERR_ALLOW;
1067 if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
1068 if (acl_consume & set_deny) {
1069 if (!(acep->a_access_mask & mask_bit)) {
1072 } else if (acl_consume & err_deny) {
1073 if (acep->a_access_mask & mask_bit) {
1078 /* ACE_ACCESS_ALLOWED_ACE_TYPE */
1079 if (acl_consume & set_allow) {
1080 if (!(acep->a_access_mask & mask_bit)) {
1083 } else if (acl_consume & err_allow) {
1084 if (acep->a_access_mask & mask_bit) {
1093 ace_to_aent_legal(ace_t *acep)
1098 /* only ALLOW or DENY */
1099 if ((acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE) &&
1100 (acep->a_type != ACE_ACCESS_DENIED_ACE_TYPE)) {
1105 /* check for invalid flags */
1106 if (acep->a_flags & ~(ACE_VALID_FLAG_BITS)) {
1111 /* some flags are illegal */
1112 if (acep->a_flags & (ACE_SUCCESSFUL_ACCESS_ACE_FLAG |
1113 ACE_FAILED_ACCESS_ACE_FLAG |
1114 ACE_NO_PROPAGATE_INHERIT_ACE)) {
1119 /* check for invalid masks */
1120 if (acep->a_access_mask & ~(ACE_VALID_MASK_BITS)) {
1125 if ((acep->a_flags & ACE_OWNER)) {
1131 error = access_mask_check(acep, ACE_SYNCHRONIZE, isowner);
1135 error = access_mask_check(acep, ACE_WRITE_OWNER, isowner);
1139 error = access_mask_check(acep, ACE_DELETE, isowner);
1143 error = access_mask_check(acep, ACE_WRITE_ATTRIBUTES, isowner);
1147 error = access_mask_check(acep, ACE_READ_NAMED_ATTRS, isowner);
1151 error = access_mask_check(acep, ACE_WRITE_NAMED_ATTRS, isowner);
1155 /* more detailed checking of masks */
1156 if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
1157 if (! (acep->a_access_mask & ACE_READ_ATTRIBUTES)) {
1161 if ((acep->a_access_mask & ACE_WRITE_DATA) &&
1162 (! (acep->a_access_mask & ACE_APPEND_DATA))) {
1166 if ((! (acep->a_access_mask & ACE_WRITE_DATA)) &&
1167 (acep->a_access_mask & ACE_APPEND_DATA)) {
1173 /* ACL enforcement */
1174 if ((acep->a_access_mask & ACE_READ_ACL) &&
1175 (acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE)) {
1179 if (acep->a_access_mask & ACE_WRITE_ACL) {
1180 if ((acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) &&
1185 if ((acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) &&
1197 ace_allow_to_mode(uint32_t mask, o_mode_t *modep, int isdir)
1199 /* ACE_READ_ACL and ACE_READ_ATTRIBUTES must both be set */
1200 if ((mask & (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) !=
1201 (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) {
1205 return (ace_mask_to_mode(mask, modep, isdir));
1209 acevals_to_aent(acevals_t *vals, aclent_t *dest, ace_list_t *list,
1210 uid_t owner, gid_t group, int isdir)
1213 uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1216 flips |= ACE_DELETE_CHILD;
1217 if (vals->allowed != (vals->denied ^ flips)) {
1221 if ((list->hasmask) && (list->acl_mask != vals->mask) &&
1222 (vals->aent_type & (USER | GROUP | GROUP_OBJ))) {
1226 error = ace_allow_to_mode(vals->allowed, &dest->a_perm, isdir);
1229 dest->a_type = vals->aent_type;
1230 if (dest->a_type & (USER | GROUP)) {
1231 dest->a_id = vals->key;
1232 } else if (dest->a_type & USER_OBJ) {
1234 } else if (dest->a_type & GROUP_OBJ) {
1236 } else if (dest->a_type & OTHER_OBJ) {
1249 ace_list_to_aent(ace_list_t *list, aclent_t **aclentp, int *aclcnt,
1250 uid_t owner, gid_t group, int isdir)
1253 aclent_t *aent, *result = NULL;
1257 if ((list->seen & (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) !=
1258 (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) {
1262 if ((! list->hasmask) && (list->numusers + list->numgroups > 0)) {
1267 resultcount = 3 + list->numusers + list->numgroups;
1269 * This must be the same condition as below, when we add the CLASS_OBJ
1272 if ((list->hasmask) || (! list->dfacl_flag))
1275 if (cacl_malloc((void **)&result,
1276 resultcount * sizeof (aclent_t)) != 0) {
1283 if (!(list->user_obj.aent_type & USER_OBJ)) {
1288 error = acevals_to_aent(&list->user_obj, aent, list, owner, group,
1296 for (vals = avl_first(&list->user); vals != NULL;
1297 vals = AVL_NEXT(&list->user, vals)) {
1298 if (!(vals->aent_type & USER)) {
1302 error = acevals_to_aent(vals, aent, list, owner, group,
1309 if (!(list->group_obj.aent_type & GROUP_OBJ)) {
1313 error = acevals_to_aent(&list->group_obj, aent, list, owner, group,
1320 for (vals = avl_first(&list->group); vals != NULL;
1321 vals = AVL_NEXT(&list->group, vals)) {
1322 if (!(vals->aent_type & GROUP)) {
1326 error = acevals_to_aent(vals, aent, list, owner, group,
1333 * CLASS_OBJ (aka ACL_MASK)
1335 * An ACL_MASK is not fabricated if the ACL is a default ACL.
1336 * This is to follow UFS's behavior.
1338 if ((list->hasmask) || (! list->dfacl_flag)) {
1339 if (list->hasmask) {
1340 uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1342 flips |= ACE_DELETE_CHILD;
1343 error = ace_mask_to_mode(list->acl_mask ^ flips,
1344 &aent->a_perm, isdir);
1348 /* fabricate the ACL_MASK from the group permissions */
1349 error = ace_mask_to_mode(list->group_obj.allowed,
1350 &aent->a_perm, isdir);
1355 aent->a_type = CLASS_OBJ | list->dfacl_flag;
1359 if (!(list->other_obj.aent_type & OTHER_OBJ)) {
1363 error = acevals_to_aent(&list->other_obj, aent, list, owner, group,
1370 *aclcnt = resultcount;
1375 cacl_free(result, resultcount * sizeof (aclent_t));
1383 * free all data associated with an ace_list
1386 ace_list_free(ace_list_t *al)
1395 while ((node = avl_destroy_nodes(&al->user, &cookie)) != NULL)
1396 cacl_free(node, sizeof (acevals_t));
1398 while ((node = avl_destroy_nodes(&al->group, &cookie)) != NULL)
1399 cacl_free(node, sizeof (acevals_t));
1401 avl_destroy(&al->user);
1402 avl_destroy(&al->group);
1404 /* free the container itself */
1405 cacl_free(al, sizeof (ace_list_t));
1409 acevals_compare(const void *va, const void *vb)
1411 const acevals_t *a = va, *b = vb;
1413 if (a->key == b->key)
1416 if (a->key > b->key)
1424 * Convert a list of ace_t entries to equivalent regular and default
1425 * aclent_t lists. Return error (ENOTSUP) when conversion is not possible.
1428 ln_ace_to_aent(ace_t *ace, int n, uid_t owner, gid_t group,
1429 aclent_t **aclentp, int *aclcnt, aclent_t **dfaclentp, int *dfaclcnt,
1436 ace_list_t *normacl = NULL, *dfacl = NULL, *acl;
1444 /* we need at least user_obj, group_obj, and other_obj */
1454 error = cacl_malloc((void **)&normacl, sizeof (ace_list_t));
1458 avl_create(&normacl->user, acevals_compare, sizeof (acevals_t),
1459 offsetof(acevals_t, avl));
1460 avl_create(&normacl->group, acevals_compare, sizeof (acevals_t),
1461 offsetof(acevals_t, avl));
1463 ace_list_init(normacl, 0);
1465 error = cacl_malloc((void **)&dfacl, sizeof (ace_list_t));
1469 avl_create(&dfacl->user, acevals_compare, sizeof (acevals_t),
1470 offsetof(acevals_t, avl));
1471 avl_create(&dfacl->group, acevals_compare, sizeof (acevals_t),
1472 offsetof(acevals_t, avl));
1473 ace_list_init(dfacl, ACL_DEFAULT);
1475 /* process every ace_t... */
1476 for (i = 0; i < n; i++) {
1479 /* rule out certain cases quickly */
1480 error = ace_to_aent_legal(acep);
1485 * Turn off these bits in order to not have to worry about
1486 * them when doing the checks for compliments.
1488 acep->a_access_mask &= ~(ACE_WRITE_OWNER | ACE_DELETE |
1489 ACE_SYNCHRONIZE | ACE_WRITE_ATTRIBUTES |
1490 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS);
1492 /* see if this should be a regular or default acl */
1493 bits = acep->a_flags &
1494 (ACE_INHERIT_ONLY_ACE |
1495 ACE_FILE_INHERIT_ACE |
1496 ACE_DIRECTORY_INHERIT_ACE);
1498 /* all or nothing on these inherit bits */
1499 if (bits != (ACE_INHERIT_ONLY_ACE |
1500 ACE_FILE_INHERIT_ACE |
1501 ACE_DIRECTORY_INHERIT_ACE)) {
1510 if ((acep->a_flags & ACE_OWNER)) {
1511 if (acl->state > ace_user_obj) {
1515 acl->state = ace_user_obj;
1516 acl->seen |= USER_OBJ;
1517 vals = &acl->user_obj;
1518 vals->aent_type = USER_OBJ | acl->dfacl_flag;
1519 } else if ((acep->a_flags & ACE_EVERYONE)) {
1520 acl->state = ace_other_obj;
1521 acl->seen |= OTHER_OBJ;
1522 vals = &acl->other_obj;
1523 vals->aent_type = OTHER_OBJ | acl->dfacl_flag;
1524 } else if (acep->a_flags & ACE_IDENTIFIER_GROUP) {
1525 if (acl->state > ace_group) {
1529 if ((acep->a_flags & ACE_GROUP)) {
1530 acl->seen |= GROUP_OBJ;
1531 vals = &acl->group_obj;
1532 vals->aent_type = GROUP_OBJ | acl->dfacl_flag;
1535 vals = acevals_find(acep, &acl->group,
1541 vals->aent_type = GROUP | acl->dfacl_flag;
1543 acl->state = ace_group;
1545 if (acl->state > ace_user) {
1549 acl->state = ace_user;
1551 vals = acevals_find(acep, &acl->user,
1557 vals->aent_type = USER | acl->dfacl_flag;
1560 if (!(acl->state > ace_unused)) {
1565 if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
1566 /* no more than one allowed per aclent_t */
1567 if (vals->allowed != ACE_MASK_UNDEFINED) {
1571 vals->allowed = acep->a_access_mask;
1574 * it's a DENY; if there was a previous DENY, it
1575 * must have been an ACL_MASK.
1577 if (vals->denied != ACE_MASK_UNDEFINED) {
1578 /* ACL_MASK is for USER and GROUP only */
1579 if ((acl->state != ace_user) &&
1580 (acl->state != ace_group)) {
1585 if (! acl->hasmask) {
1587 acl->acl_mask = vals->denied;
1588 /* check for mismatched ACL_MASK emulations */
1589 } else if (acl->acl_mask != vals->denied) {
1593 vals->mask = vals->denied;
1595 vals->denied = acep->a_access_mask;
1599 /* done collating; produce the aclent_t lists */
1600 if (normacl->state != ace_unused) {
1601 error = ace_list_to_aent(normacl, aclentp, aclcnt,
1602 owner, group, isdir);
1607 if (dfacl->state != ace_unused) {
1608 error = ace_list_to_aent(dfacl, dfaclentp, dfaclcnt,
1609 owner, group, isdir);
1616 if (normacl != NULL)
1617 ace_list_free(normacl);
1619 ace_list_free(dfacl);
1625 convert_ace_to_aent(ace_t *acebufp, int acecnt, int isdir,
1626 uid_t owner, gid_t group, aclent_t **retaclentp, int *retaclcnt)
1629 aclent_t *aclentp, *dfaclentp;
1630 int aclcnt, dfaclcnt;
1633 error = ln_ace_to_aent(acebufp, acecnt, owner, group,
1634 &aclentp, &aclcnt, &dfaclentp, &dfaclcnt, isdir);
1640 if (dfaclcnt != 0) {
1642 * Slap aclentp and dfaclentp into a single array.
1644 aclsz = sizeof (aclent_t) * aclcnt;
1645 dfaclsz = sizeof (aclent_t) * dfaclcnt;
1646 aclentp = cacl_realloc(aclentp, aclsz, aclsz + dfaclsz);
1647 if (aclentp != NULL) {
1648 (void) memcpy(aclentp + aclcnt, dfaclentp, dfaclsz);
1655 *retaclentp = aclentp;
1656 *retaclcnt = aclcnt + dfaclcnt;
1660 cacl_free(dfaclentp, dfaclsz);
1667 acl_translate(acl_t *aclp, int target_flavor, int isdir, uid_t owner,
1675 * See if we need to translate
1677 if ((target_flavor == _ACL_ACE_ENABLED && aclp->acl_type == ACE_T) ||
1678 (target_flavor == _ACL_ACLENT_ENABLED &&
1679 aclp->acl_type == ACLENT_T))
1682 if (target_flavor == -1) {
1687 if (target_flavor == _ACL_ACE_ENABLED &&
1688 aclp->acl_type == ACLENT_T) {
1689 error = convert_aent_to_ace(aclp->acl_aclp,
1690 aclp->acl_cnt, isdir, (ace_t **)&acldata, &aclcnt);
1694 } else if (target_flavor == _ACL_ACLENT_ENABLED &&
1695 aclp->acl_type == ACE_T) {
1696 error = convert_ace_to_aent(aclp->acl_aclp, aclp->acl_cnt,
1697 isdir, owner, group, (aclent_t **)&acldata, &aclcnt);
1706 * replace old acl with newly translated acl
1708 cacl_free(aclp->acl_aclp, aclp->acl_cnt * aclp->acl_entry_size);
1709 aclp->acl_aclp = acldata;
1710 aclp->acl_cnt = aclcnt;
1711 if (target_flavor == _ACL_ACE_ENABLED) {
1712 aclp->acl_type = ACE_T;
1713 aclp->acl_entry_size = sizeof (ace_t);
1715 aclp->acl_type = ACLENT_T;
1716 aclp->acl_entry_size = sizeof (aclent_t);
1722 #if !defined(_KERNEL)
1729 #endif /* _KERNEL */