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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <acl/acl_common.h>
35 #include <sys/debug.h>
45 #include <acl_common.h>
49 #define ACE_POSIX_SUPPORTED_BITS (ACE_READ_DATA | \
50 ACE_WRITE_DATA | ACE_APPEND_DATA | ACE_EXECUTE | \
51 ACE_READ_ATTRIBUTES | ACE_READ_ACL | ACE_WRITE_ACL)
54 #define ACL_SYNCHRONIZE_SET_DENY 0x0000001
55 #define ACL_SYNCHRONIZE_SET_ALLOW 0x0000002
56 #define ACL_SYNCHRONIZE_ERR_DENY 0x0000004
57 #define ACL_SYNCHRONIZE_ERR_ALLOW 0x0000008
59 #define ACL_WRITE_OWNER_SET_DENY 0x0000010
60 #define ACL_WRITE_OWNER_SET_ALLOW 0x0000020
61 #define ACL_WRITE_OWNER_ERR_DENY 0x0000040
62 #define ACL_WRITE_OWNER_ERR_ALLOW 0x0000080
64 #define ACL_DELETE_SET_DENY 0x0000100
65 #define ACL_DELETE_SET_ALLOW 0x0000200
66 #define ACL_DELETE_ERR_DENY 0x0000400
67 #define ACL_DELETE_ERR_ALLOW 0x0000800
69 #define ACL_WRITE_ATTRS_OWNER_SET_DENY 0x0001000
70 #define ACL_WRITE_ATTRS_OWNER_SET_ALLOW 0x0002000
71 #define ACL_WRITE_ATTRS_OWNER_ERR_DENY 0x0004000
72 #define ACL_WRITE_ATTRS_OWNER_ERR_ALLOW 0x0008000
74 #define ACL_WRITE_ATTRS_WRITER_SET_DENY 0x0010000
75 #define ACL_WRITE_ATTRS_WRITER_SET_ALLOW 0x0020000
76 #define ACL_WRITE_ATTRS_WRITER_ERR_DENY 0x0040000
77 #define ACL_WRITE_ATTRS_WRITER_ERR_ALLOW 0x0080000
79 #define ACL_WRITE_NAMED_WRITER_SET_DENY 0x0100000
80 #define ACL_WRITE_NAMED_WRITER_SET_ALLOW 0x0200000
81 #define ACL_WRITE_NAMED_WRITER_ERR_DENY 0x0400000
82 #define ACL_WRITE_NAMED_WRITER_ERR_ALLOW 0x0800000
84 #define ACL_READ_NAMED_READER_SET_DENY 0x1000000
85 #define ACL_READ_NAMED_READER_SET_ALLOW 0x2000000
86 #define ACL_READ_NAMED_READER_ERR_DENY 0x4000000
87 #define ACL_READ_NAMED_READER_ERR_ALLOW 0x8000000
90 #define ACE_VALID_MASK_BITS (\
92 ACE_LIST_DIRECTORY | \
96 ACE_ADD_SUBDIRECTORY | \
97 ACE_READ_NAMED_ATTRS | \
98 ACE_WRITE_NAMED_ATTRS | \
101 ACE_READ_ATTRIBUTES | \
102 ACE_WRITE_ATTRIBUTES | \
109 #define ACE_MASK_UNDEFINED 0x80000000
111 #define ACE_VALID_FLAG_BITS (ACE_FILE_INHERIT_ACE | \
112 ACE_DIRECTORY_INHERIT_ACE | \
113 ACE_NO_PROPAGATE_INHERIT_ACE | ACE_INHERIT_ONLY_ACE | \
114 ACE_SUCCESSFUL_ACCESS_ACE_FLAG | ACE_FAILED_ACCESS_ACE_FLAG | \
115 ACE_IDENTIFIER_GROUP | ACE_OWNER | ACE_GROUP | ACE_EVERYONE)
118 * ACL conversion helpers
125 ace_group, /* includes GROUP and GROUP_OBJ */
127 } ace_to_aent_state_t;
129 typedef struct acevals {
138 typedef struct ace_list {
149 ace_to_aent_state_t state;
150 int seen; /* bitmask of all aclent_t a_type values seen */
154 * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
155 * v = Ptr to array/vector of objs
156 * n = # objs in the array
157 * s = size of each obj (must be multiples of a word size)
158 * f = ptr to function to compare two objs
159 * returns (-1 = less than, 0 = equal, 1 = greater than
162 ksort(caddr_t v, int n, int s, int (*f)())
165 unsigned int *p1, *p2;
169 if (v == NULL || n <= 1)
172 /* Sanity check on arguments */
173 ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
175 for (g = n / 2; g > 0; g /= 2) {
176 for (i = g; i < n; i++) {
177 for (j = i - g; j >= 0 &&
178 (*f)(v + j * s, v + (j + g) * s) == 1;
180 p1 = (void *)(v + j * s);
181 p2 = (void *)(v + (j + g) * s);
182 for (ii = 0; ii < s / 4; ii++) {
193 * Compare two acls, all fields. Returns:
199 cmp2acls(void *a, void *b)
201 aclent_t *x = (aclent_t *)a;
202 aclent_t *y = (aclent_t *)b;
205 if (x->a_type < y->a_type)
207 if (x->a_type > y->a_type)
209 /* Equal types; compare id's */
210 if (x->a_id < y->a_id)
212 if (x->a_id > y->a_id)
214 /* Equal ids; compare perms */
215 if (x->a_perm < y->a_perm)
217 if (x->a_perm > y->a_perm)
225 cacl_realloc(void *ptr, size_t size, size_t new_size)
230 tmp = kmem_alloc(new_size, KM_SLEEP);
231 (void) memcpy(tmp, ptr, (size < new_size) ? size : new_size);
232 kmem_free(ptr, size);
235 return (realloc(ptr, new_size));
240 cacl_malloc(void **ptr, size_t size)
243 *ptr = kmem_zalloc(size, KM_SLEEP);
246 *ptr = calloc(1, size);
256 cacl_free(void *ptr, size_t size)
259 kmem_free(ptr, size);
265 #if !defined(_KERNEL)
267 acl_alloc(enum acl_type type)
271 if (cacl_malloc((void **)&aclp, sizeof (acl_t)) != 0)
274 aclp->acl_aclp = NULL;
279 aclp->acl_type = ACE_T;
280 aclp->acl_entry_size = sizeof (ace_t);
283 aclp->acl_type = ACLENT_T;
284 aclp->acl_entry_size = sizeof (aclent_t);
294 * Free acl_t structure
297 acl_free(acl_t *aclp)
304 if (aclp->acl_aclp) {
305 acl_size = aclp->acl_cnt * aclp->acl_entry_size;
306 cacl_free(aclp->acl_aclp, acl_size);
309 cacl_free(aclp, sizeof (acl_t));
313 access_mask_set(int haswriteperm, int hasreadperm, int isowner, int isallow)
315 uint32_t access_mask = 0;
317 int synchronize_set = 0, write_owner_set = 0;
318 int delete_set = 0, write_attrs_set = 0;
319 int read_named_set = 0, write_named_set = 0;
321 acl_produce = (ACL_SYNCHRONIZE_SET_ALLOW |
322 ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
323 ACL_WRITE_ATTRS_WRITER_SET_DENY);
326 synchronize_set = ACL_SYNCHRONIZE_SET_ALLOW;
327 write_owner_set = ACL_WRITE_OWNER_SET_ALLOW;
328 delete_set = ACL_DELETE_SET_ALLOW;
330 read_named_set = ACL_READ_NAMED_READER_SET_ALLOW;
332 write_named_set = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
334 write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
335 else if (haswriteperm)
336 write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
339 synchronize_set = ACL_SYNCHRONIZE_SET_DENY;
340 write_owner_set = ACL_WRITE_OWNER_SET_DENY;
341 delete_set = ACL_DELETE_SET_DENY;
343 read_named_set = ACL_READ_NAMED_READER_SET_DENY;
345 write_named_set = ACL_WRITE_NAMED_WRITER_SET_DENY;
347 write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_DENY;
348 else if (haswriteperm)
349 write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_DENY;
352 * If the entity is not the owner and does not
353 * have write permissions ACE_WRITE_ATTRIBUTES will
354 * always go in the DENY ACE.
356 access_mask |= ACE_WRITE_ATTRIBUTES;
359 if (acl_produce & synchronize_set)
360 access_mask |= ACE_SYNCHRONIZE;
361 if (acl_produce & write_owner_set)
362 access_mask |= ACE_WRITE_OWNER;
363 if (acl_produce & delete_set)
364 access_mask |= ACE_DELETE;
365 if (acl_produce & write_attrs_set)
366 access_mask |= ACE_WRITE_ATTRIBUTES;
367 if (acl_produce & read_named_set)
368 access_mask |= ACE_READ_NAMED_ATTRS;
369 if (acl_produce & write_named_set)
370 access_mask |= ACE_WRITE_NAMED_ATTRS;
372 return (access_mask);
376 * Given an mode_t, convert it into an access_mask as used
377 * by nfsace, assuming aclent_t -> nfsace semantics.
380 mode_to_ace_access(mode_t mode, boolean_t isdir, int isowner, int isallow)
383 int haswriteperm = 0;
387 haswriteperm = (mode & S_IWOTH);
388 hasreadperm = (mode & S_IROTH);
390 haswriteperm = !(mode & S_IWOTH);
391 hasreadperm = !(mode & S_IROTH);
395 * The following call takes care of correctly setting the following
396 * mask bits in the access_mask:
397 * ACE_SYNCHRONIZE, ACE_WRITE_OWNER, ACE_DELETE,
398 * ACE_WRITE_ATTRIBUTES, ACE_WRITE_NAMED_ATTRS, ACE_READ_NAMED_ATTRS
400 access = access_mask_set(haswriteperm, hasreadperm, isowner, isallow);
403 access |= ACE_READ_ACL | ACE_READ_ATTRIBUTES;
405 access |= ACE_WRITE_ACL;
408 access |= ACE_WRITE_ACL;
412 if (mode & S_IROTH) {
413 access |= ACE_READ_DATA;
416 if (mode & S_IWOTH) {
417 access |= ACE_WRITE_DATA |
420 access |= ACE_DELETE_CHILD;
423 if (mode & S_IXOTH) {
424 access |= ACE_EXECUTE;
431 * Given an nfsace (presumably an ALLOW entry), make a
432 * corresponding DENY entry at the address given.
435 ace_make_deny(ace_t *allow, ace_t *deny, int isdir, int isowner)
437 (void) memcpy(deny, allow, sizeof (ace_t));
439 deny->a_who = allow->a_who;
441 deny->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
442 deny->a_access_mask ^= ACE_POSIX_SUPPORTED_BITS;
444 deny->a_access_mask ^= ACE_DELETE_CHILD;
446 deny->a_access_mask &= ~(ACE_SYNCHRONIZE | ACE_WRITE_OWNER |
447 ACE_DELETE | ACE_WRITE_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
448 ACE_WRITE_NAMED_ATTRS);
449 deny->a_access_mask |= access_mask_set((allow->a_access_mask &
450 ACE_WRITE_DATA), (allow->a_access_mask & ACE_READ_DATA), isowner,
454 * Make an initial pass over an array of aclent_t's. Gather
455 * information such as an ACL_MASK (if any), number of users,
456 * number of groups, and whether the array needs to be sorted.
459 ln_aent_preprocess(aclent_t *aclent, int n,
460 int *hasmask, mode_t *mask,
461 int *numuser, int *numgroup, int *needsort)
473 for (i = 0; i < n; i++) {
474 if (aclent[i].a_type < curtype)
476 else if (aclent[i].a_type > curtype)
477 curtype = aclent[i].a_type;
478 if (aclent[i].a_type & USER)
480 if (aclent[i].a_type & (GROUP | GROUP_OBJ))
482 if (aclent[i].a_type & CLASS_OBJ) {
488 *mask = aclent[i].a_perm;
493 if ((! *hasmask) && (*numuser + *numgroup > 1)) {
503 * Convert an array of aclent_t into an array of nfsace entries,
504 * following POSIX draft -> nfsv4 conversion semantics as outlined in
508 ln_aent_to_ace(aclent_t *aclent, int n, ace_t **acepp, int *rescount, int isdir)
512 int numuser, numgroup, needsort;
514 int i, groupi = 0, skip;
515 ace_t *acep, *result = NULL;
518 error = ln_aent_preprocess(aclent, n, &hasmask, &mask,
519 &numuser, &numgroup, &needsort);
523 /* allow + deny for each aclent */
527 * stick extra deny on the group_obj and on each
528 * user|group for the mask (the group_obj was added
529 * into the count for numgroup)
531 resultsize += numuser + numgroup;
532 /* ... and don't count the mask itself */
536 /* sort the source if necessary */
538 ksort((caddr_t)aclent, n, sizeof (aclent_t), cmp2acls);
540 if (cacl_malloc((void **)&result, resultsize * sizeof (ace_t)) != 0)
545 for (i = 0; i < n; i++) {
547 * don't process CLASS_OBJ (mask); mask was grabbed in
548 * ln_aent_preprocess()
550 if (aclent[i].a_type & CLASS_OBJ)
553 /* If we need an ACL_MASK emulator, prepend it now */
555 (aclent[i].a_type & (USER | GROUP | GROUP_OBJ))) {
556 acep->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
558 if (aclent[i].a_type & GROUP_OBJ) {
559 acep->a_who = (uid_t)-1;
561 (ACE_IDENTIFIER_GROUP|ACE_GROUP);
562 } else if (aclent[i].a_type & USER) {
563 acep->a_who = aclent[i].a_id;
565 acep->a_who = aclent[i].a_id;
566 acep->a_flags |= ACE_IDENTIFIER_GROUP;
568 if (aclent[i].a_type & ACL_DEFAULT) {
569 acep->a_flags |= ACE_INHERIT_ONLY_ACE |
570 ACE_FILE_INHERIT_ACE |
571 ACE_DIRECTORY_INHERIT_ACE;
574 * Set the access mask for the prepended deny
575 * ace. To do this, we invert the mask (found
576 * in ln_aent_preprocess()) then convert it to an
577 * DENY ace access_mask.
579 acep->a_access_mask = mode_to_ace_access((mask ^ 07),
584 /* handle a_perm -> access_mask */
585 acep->a_access_mask = mode_to_ace_access(aclent[i].a_perm,
586 isdir, aclent[i].a_type & USER_OBJ, 1);
588 /* emulate a default aclent */
589 if (aclent[i].a_type & ACL_DEFAULT) {
590 acep->a_flags |= ACE_INHERIT_ONLY_ACE |
591 ACE_FILE_INHERIT_ACE |
592 ACE_DIRECTORY_INHERIT_ACE;
596 * handle a_perm and a_id
598 * this must be done last, since it involves the
599 * corresponding deny aces, which are handled
600 * differently for each different a_type.
602 if (aclent[i].a_type & USER_OBJ) {
603 acep->a_who = (uid_t)-1;
604 acep->a_flags |= ACE_OWNER;
605 ace_make_deny(acep, acep + 1, isdir, B_TRUE);
607 } else if (aclent[i].a_type & USER) {
608 acep->a_who = aclent[i].a_id;
609 ace_make_deny(acep, acep + 1, isdir, B_FALSE);
611 } else if (aclent[i].a_type & (GROUP_OBJ | GROUP)) {
612 if (aclent[i].a_type & GROUP_OBJ) {
613 acep->a_who = (uid_t)-1;
614 acep->a_flags |= ACE_GROUP;
616 acep->a_who = aclent[i].a_id;
618 acep->a_flags |= ACE_IDENTIFIER_GROUP;
620 * Set the corresponding deny for the group ace.
622 * The deny aces go after all of the groups, unlike
623 * everything else, where they immediately follow
626 * We calculate "skip", the number of slots to
627 * skip ahead for the deny ace, here.
630 * MD1 A1 MD2 A2 MD3 A3 D1 D2 D3
632 * (2 * numgroup) - 1 - groupi
633 * (2 * numgroup) to account for MD + A
634 * - 1 to account for the fact that we're on the
635 * access (A), not the mask (MD)
636 * - groupi to account for the fact that we have
637 * passed up groupi number of MD's.
639 skip = (2 * numgroup) - 1 - groupi;
640 ace_make_deny(acep, acep + skip, isdir, B_FALSE);
642 * If we just did the last group, skip acep past
643 * all of the denies; else, just move ahead one.
645 if (++groupi >= numgroup)
646 acep += numgroup + 1;
649 } else if (aclent[i].a_type & OTHER_OBJ) {
650 acep->a_who = (uid_t)-1;
651 acep->a_flags |= ACE_EVERYONE;
652 ace_make_deny(acep, acep + 1, isdir, B_FALSE);
661 *rescount = resultsize;
665 if ((result != NULL) && (resultsize > 0)) {
666 cacl_free(result, resultsize * sizeof (ace_t));
674 convert_aent_to_ace(aclent_t *aclentp, int aclcnt, boolean_t isdir,
675 ace_t **retacep, int *retacecnt)
688 ksort((caddr_t)aclentp, aclcnt, sizeof (aclent_t), cmp2acls);
690 for (i = 0, aclp = aclentp; i < aclcnt; aclp++, i++) {
691 if (aclp->a_type & ACL_DEFAULT)
697 dfaclcnt = aclcnt - i;
700 if (dfaclcnt && !isdir) {
704 error = ln_aent_to_ace(aclentp, i, &acep, &acecnt, isdir);
709 error = ln_aent_to_ace(&aclentp[dfaclstart], dfaclcnt,
710 &dfacep, &dfacecnt, isdir);
713 cacl_free(acep, acecnt * sizeof (ace_t));
720 acesz = sizeof (ace_t) * acecnt;
721 dfacesz = sizeof (ace_t) * dfacecnt;
722 acep = cacl_realloc(acep, acesz, acesz + dfacesz);
726 (void) memcpy(acep + acecnt, dfacep, dfacesz);
730 cacl_free(dfacep, dfacecnt * sizeof (ace_t));
732 *retacecnt = acecnt + dfacecnt;
738 ace_mask_to_mode(uint32_t mask, o_mode_t *modep, boolean_t isdir)
742 uint32_t bits, wantbits;
745 if (mask & ACE_READ_DATA)
749 wantbits = (ACE_WRITE_DATA | ACE_APPEND_DATA);
751 wantbits |= ACE_DELETE_CHILD;
752 bits = mask & wantbits;
754 if (bits != wantbits) {
762 if (mask & ACE_EXECUTE) {
773 acevals_init(acevals_t *vals, uid_t key)
775 bzero(vals, sizeof (*vals));
776 vals->allowed = ACE_MASK_UNDEFINED;
777 vals->denied = ACE_MASK_UNDEFINED;
778 vals->mask = ACE_MASK_UNDEFINED;
783 ace_list_init(ace_list_t *al, int dfacl_flag)
785 acevals_init(&al->user_obj, 0);
786 acevals_init(&al->group_obj, 0);
787 acevals_init(&al->other_obj, 0);
792 al->state = ace_unused;
794 al->dfacl_flag = dfacl_flag;
798 * Find or create an acevals holder for a given id and avl tree.
800 * Note that only one thread will ever touch these avl trees, so
801 * there is no need for locking.
804 acevals_find(ace_t *ace, avl_tree_t *avl, int *num)
809 key.key = ace->a_who;
810 rc = avl_find(avl, &key, &where);
814 /* this memory is freed by ln_ace_to_aent()->ace_list_free() */
815 if (cacl_malloc((void **)&rc, sizeof (acevals_t)) != 0)
818 acevals_init(rc, ace->a_who);
819 avl_insert(avl, rc, where);
826 access_mask_check(ace_t *acep, int mask_bit, int isowner)
828 int set_deny, err_deny;
829 int set_allow, err_allow;
831 int haswriteperm, hasreadperm;
833 if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
834 haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 0 : 1;
835 hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 0 : 1;
837 haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 1 : 0;
838 hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 1 : 0;
841 acl_consume = (ACL_SYNCHRONIZE_ERR_DENY |
842 ACL_DELETE_ERR_DENY |
843 ACL_WRITE_OWNER_ERR_DENY |
844 ACL_WRITE_OWNER_ERR_ALLOW |
845 ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
846 ACL_WRITE_ATTRS_OWNER_ERR_DENY |
847 ACL_WRITE_ATTRS_WRITER_SET_DENY |
848 ACL_WRITE_ATTRS_WRITER_ERR_ALLOW |
849 ACL_WRITE_NAMED_WRITER_ERR_DENY |
850 ACL_READ_NAMED_READER_ERR_DENY);
852 if (mask_bit == ACE_SYNCHRONIZE) {
853 set_deny = ACL_SYNCHRONIZE_SET_DENY;
854 err_deny = ACL_SYNCHRONIZE_ERR_DENY;
855 set_allow = ACL_SYNCHRONIZE_SET_ALLOW;
856 err_allow = ACL_SYNCHRONIZE_ERR_ALLOW;
857 } else if (mask_bit == ACE_WRITE_OWNER) {
858 set_deny = ACL_WRITE_OWNER_SET_DENY;
859 err_deny = ACL_WRITE_OWNER_ERR_DENY;
860 set_allow = ACL_WRITE_OWNER_SET_ALLOW;
861 err_allow = ACL_WRITE_OWNER_ERR_ALLOW;
862 } else if (mask_bit == ACE_DELETE) {
863 set_deny = ACL_DELETE_SET_DENY;
864 err_deny = ACL_DELETE_ERR_DENY;
865 set_allow = ACL_DELETE_SET_ALLOW;
866 err_allow = ACL_DELETE_ERR_ALLOW;
867 } else if (mask_bit == ACE_WRITE_ATTRIBUTES) {
869 set_deny = ACL_WRITE_ATTRS_OWNER_SET_DENY;
870 err_deny = ACL_WRITE_ATTRS_OWNER_ERR_DENY;
871 set_allow = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
872 err_allow = ACL_WRITE_ATTRS_OWNER_ERR_ALLOW;
873 } else if (haswriteperm) {
874 set_deny = ACL_WRITE_ATTRS_WRITER_SET_DENY;
875 err_deny = ACL_WRITE_ATTRS_WRITER_ERR_DENY;
876 set_allow = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
877 err_allow = ACL_WRITE_ATTRS_WRITER_ERR_ALLOW;
879 if ((acep->a_access_mask & mask_bit) &&
880 (acep->a_type & ACE_ACCESS_ALLOWED_ACE_TYPE)) {
885 } else if (mask_bit == ACE_READ_NAMED_ATTRS) {
889 set_deny = ACL_READ_NAMED_READER_SET_DENY;
890 err_deny = ACL_READ_NAMED_READER_ERR_DENY;
891 set_allow = ACL_READ_NAMED_READER_SET_ALLOW;
892 err_allow = ACL_READ_NAMED_READER_ERR_ALLOW;
893 } else if (mask_bit == ACE_WRITE_NAMED_ATTRS) {
897 set_deny = ACL_WRITE_NAMED_WRITER_SET_DENY;
898 err_deny = ACL_WRITE_NAMED_WRITER_ERR_DENY;
899 set_allow = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
900 err_allow = ACL_WRITE_NAMED_WRITER_ERR_ALLOW;
905 if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
906 if (acl_consume & set_deny) {
907 if (!(acep->a_access_mask & mask_bit)) {
910 } else if (acl_consume & err_deny) {
911 if (acep->a_access_mask & mask_bit) {
916 /* ACE_ACCESS_ALLOWED_ACE_TYPE */
917 if (acl_consume & set_allow) {
918 if (!(acep->a_access_mask & mask_bit)) {
921 } else if (acl_consume & err_allow) {
922 if (acep->a_access_mask & mask_bit) {
931 ace_to_aent_legal(ace_t *acep)
936 /* only ALLOW or DENY */
937 if ((acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE) &&
938 (acep->a_type != ACE_ACCESS_DENIED_ACE_TYPE)) {
943 /* check for invalid flags */
944 if (acep->a_flags & ~(ACE_VALID_FLAG_BITS)) {
949 /* some flags are illegal */
950 if (acep->a_flags & (ACE_SUCCESSFUL_ACCESS_ACE_FLAG |
951 ACE_FAILED_ACCESS_ACE_FLAG |
952 ACE_NO_PROPAGATE_INHERIT_ACE)) {
957 /* check for invalid masks */
958 if (acep->a_access_mask & ~(ACE_VALID_MASK_BITS)) {
963 if ((acep->a_flags & ACE_OWNER)) {
969 error = access_mask_check(acep, ACE_SYNCHRONIZE, isowner);
973 error = access_mask_check(acep, ACE_WRITE_OWNER, isowner);
977 error = access_mask_check(acep, ACE_DELETE, isowner);
981 error = access_mask_check(acep, ACE_WRITE_ATTRIBUTES, isowner);
985 error = access_mask_check(acep, ACE_READ_NAMED_ATTRS, isowner);
989 error = access_mask_check(acep, ACE_WRITE_NAMED_ATTRS, isowner);
993 /* more detailed checking of masks */
994 if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
995 if (! (acep->a_access_mask & ACE_READ_ATTRIBUTES)) {
999 if ((acep->a_access_mask & ACE_WRITE_DATA) &&
1000 (! (acep->a_access_mask & ACE_APPEND_DATA))) {
1004 if ((! (acep->a_access_mask & ACE_WRITE_DATA)) &&
1005 (acep->a_access_mask & ACE_APPEND_DATA)) {
1011 /* ACL enforcement */
1012 if ((acep->a_access_mask & ACE_READ_ACL) &&
1013 (acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE)) {
1017 if (acep->a_access_mask & ACE_WRITE_ACL) {
1018 if ((acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) &&
1023 if ((acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) &&
1035 ace_allow_to_mode(uint32_t mask, o_mode_t *modep, boolean_t isdir)
1037 /* ACE_READ_ACL and ACE_READ_ATTRIBUTES must both be set */
1038 if ((mask & (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) !=
1039 (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) {
1043 return (ace_mask_to_mode(mask, modep, isdir));
1047 acevals_to_aent(acevals_t *vals, aclent_t *dest, ace_list_t *list,
1048 uid_t owner, gid_t group, boolean_t isdir)
1051 uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1054 flips |= ACE_DELETE_CHILD;
1055 if (vals->allowed != (vals->denied ^ flips)) {
1059 if ((list->hasmask) && (list->acl_mask != vals->mask) &&
1060 (vals->aent_type & (USER | GROUP | GROUP_OBJ))) {
1064 error = ace_allow_to_mode(vals->allowed, &dest->a_perm, isdir);
1067 dest->a_type = vals->aent_type;
1068 if (dest->a_type & (USER | GROUP)) {
1069 dest->a_id = vals->key;
1070 } else if (dest->a_type & USER_OBJ) {
1072 } else if (dest->a_type & GROUP_OBJ) {
1074 } else if (dest->a_type & OTHER_OBJ) {
1087 ace_list_to_aent(ace_list_t *list, aclent_t **aclentp, int *aclcnt,
1088 uid_t owner, gid_t group, boolean_t isdir)
1091 aclent_t *aent, *result = NULL;
1095 if ((list->seen & (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) !=
1096 (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) {
1100 if ((! list->hasmask) && (list->numusers + list->numgroups > 0)) {
1105 resultcount = 3 + list->numusers + list->numgroups;
1107 * This must be the same condition as below, when we add the CLASS_OBJ
1110 if ((list->hasmask) || (! list->dfacl_flag))
1113 if (cacl_malloc((void **)&result,
1114 resultcount * sizeof (aclent_t)) != 0) {
1121 if (!(list->user_obj.aent_type & USER_OBJ)) {
1126 error = acevals_to_aent(&list->user_obj, aent, list, owner, group,
1134 for (vals = avl_first(&list->user); vals != NULL;
1135 vals = AVL_NEXT(&list->user, vals)) {
1136 if (!(vals->aent_type & USER)) {
1140 error = acevals_to_aent(vals, aent, list, owner, group,
1147 if (!(list->group_obj.aent_type & GROUP_OBJ)) {
1151 error = acevals_to_aent(&list->group_obj, aent, list, owner, group,
1158 for (vals = avl_first(&list->group); vals != NULL;
1159 vals = AVL_NEXT(&list->group, vals)) {
1160 if (!(vals->aent_type & GROUP)) {
1164 error = acevals_to_aent(vals, aent, list, owner, group,
1171 * CLASS_OBJ (aka ACL_MASK)
1173 * An ACL_MASK is not fabricated if the ACL is a default ACL.
1174 * This is to follow UFS's behavior.
1176 if ((list->hasmask) || (! list->dfacl_flag)) {
1177 if (list->hasmask) {
1178 uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1180 flips |= ACE_DELETE_CHILD;
1181 error = ace_mask_to_mode(list->acl_mask ^ flips,
1182 &aent->a_perm, isdir);
1186 /* fabricate the ACL_MASK from the group permissions */
1187 error = ace_mask_to_mode(list->group_obj.allowed,
1188 &aent->a_perm, isdir);
1193 aent->a_type = CLASS_OBJ | list->dfacl_flag;
1197 if (!(list->other_obj.aent_type & OTHER_OBJ)) {
1201 error = acevals_to_aent(&list->other_obj, aent, list, owner, group,
1208 *aclcnt = resultcount;
1213 cacl_free(result, resultcount * sizeof (aclent_t));
1221 * free all data associated with an ace_list
1224 ace_list_free(ace_list_t *al)
1233 while ((node = avl_destroy_nodes(&al->user, &cookie)) != NULL)
1234 cacl_free(node, sizeof (acevals_t));
1236 while ((node = avl_destroy_nodes(&al->group, &cookie)) != NULL)
1237 cacl_free(node, sizeof (acevals_t));
1239 avl_destroy(&al->user);
1240 avl_destroy(&al->group);
1242 /* free the container itself */
1243 cacl_free(al, sizeof (ace_list_t));
1247 acevals_compare(const void *va, const void *vb)
1249 const acevals_t *a = va, *b = vb;
1251 if (a->key == b->key)
1254 if (a->key > b->key)
1262 * Convert a list of ace_t entries to equivalent regular and default
1263 * aclent_t lists. Return error (ENOTSUP) when conversion is not possible.
1266 ln_ace_to_aent(ace_t *ace, int n, uid_t owner, gid_t group,
1267 aclent_t **aclentp, int *aclcnt, aclent_t **dfaclentp, int *dfaclcnt,
1274 ace_list_t *normacl = NULL, *dfacl = NULL, *acl;
1282 /* we need at least user_obj, group_obj, and other_obj */
1292 error = cacl_malloc((void **)&normacl, sizeof (ace_list_t));
1296 avl_create(&normacl->user, acevals_compare, sizeof (acevals_t),
1297 offsetof(acevals_t, avl));
1298 avl_create(&normacl->group, acevals_compare, sizeof (acevals_t),
1299 offsetof(acevals_t, avl));
1301 ace_list_init(normacl, 0);
1303 error = cacl_malloc((void **)&dfacl, sizeof (ace_list_t));
1307 avl_create(&dfacl->user, acevals_compare, sizeof (acevals_t),
1308 offsetof(acevals_t, avl));
1309 avl_create(&dfacl->group, acevals_compare, sizeof (acevals_t),
1310 offsetof(acevals_t, avl));
1311 ace_list_init(dfacl, ACL_DEFAULT);
1313 /* process every ace_t... */
1314 for (i = 0; i < n; i++) {
1317 /* rule out certain cases quickly */
1318 error = ace_to_aent_legal(acep);
1323 * Turn off these bits in order to not have to worry about
1324 * them when doing the checks for compliments.
1326 acep->a_access_mask &= ~(ACE_WRITE_OWNER | ACE_DELETE |
1327 ACE_SYNCHRONIZE | ACE_WRITE_ATTRIBUTES |
1328 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS);
1330 /* see if this should be a regular or default acl */
1331 bits = acep->a_flags &
1332 (ACE_INHERIT_ONLY_ACE |
1333 ACE_FILE_INHERIT_ACE |
1334 ACE_DIRECTORY_INHERIT_ACE);
1336 /* all or nothing on these inherit bits */
1337 if (bits != (ACE_INHERIT_ONLY_ACE |
1338 ACE_FILE_INHERIT_ACE |
1339 ACE_DIRECTORY_INHERIT_ACE)) {
1348 if ((acep->a_flags & ACE_OWNER)) {
1349 if (acl->state > ace_user_obj) {
1353 acl->state = ace_user_obj;
1354 acl->seen |= USER_OBJ;
1355 vals = &acl->user_obj;
1356 vals->aent_type = USER_OBJ | acl->dfacl_flag;
1357 } else if ((acep->a_flags & ACE_EVERYONE)) {
1358 acl->state = ace_other_obj;
1359 acl->seen |= OTHER_OBJ;
1360 vals = &acl->other_obj;
1361 vals->aent_type = OTHER_OBJ | acl->dfacl_flag;
1362 } else if (acep->a_flags & ACE_IDENTIFIER_GROUP) {
1363 if (acl->state > ace_group) {
1367 if ((acep->a_flags & ACE_GROUP)) {
1368 acl->seen |= GROUP_OBJ;
1369 vals = &acl->group_obj;
1370 vals->aent_type = GROUP_OBJ | acl->dfacl_flag;
1373 vals = acevals_find(acep, &acl->group,
1379 vals->aent_type = GROUP | acl->dfacl_flag;
1381 acl->state = ace_group;
1383 if (acl->state > ace_user) {
1387 acl->state = ace_user;
1389 vals = acevals_find(acep, &acl->user,
1395 vals->aent_type = USER | acl->dfacl_flag;
1398 if (!(acl->state > ace_unused)) {
1403 if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
1404 /* no more than one allowed per aclent_t */
1405 if (vals->allowed != ACE_MASK_UNDEFINED) {
1409 vals->allowed = acep->a_access_mask;
1412 * it's a DENY; if there was a previous DENY, it
1413 * must have been an ACL_MASK.
1415 if (vals->denied != ACE_MASK_UNDEFINED) {
1416 /* ACL_MASK is for USER and GROUP only */
1417 if ((acl->state != ace_user) &&
1418 (acl->state != ace_group)) {
1423 if (! acl->hasmask) {
1425 acl->acl_mask = vals->denied;
1426 /* check for mismatched ACL_MASK emulations */
1427 } else if (acl->acl_mask != vals->denied) {
1431 vals->mask = vals->denied;
1433 vals->denied = acep->a_access_mask;
1437 /* done collating; produce the aclent_t lists */
1438 if (normacl->state != ace_unused) {
1439 error = ace_list_to_aent(normacl, aclentp, aclcnt,
1440 owner, group, isdir);
1445 if (dfacl->state != ace_unused) {
1446 error = ace_list_to_aent(dfacl, dfaclentp, dfaclcnt,
1447 owner, group, isdir);
1454 if (normacl != NULL)
1455 ace_list_free(normacl);
1457 ace_list_free(dfacl);
1463 convert_ace_to_aent(ace_t *acebufp, int acecnt, boolean_t isdir,
1464 uid_t owner, gid_t group, aclent_t **retaclentp, int *retaclcnt)
1467 aclent_t *aclentp, *dfaclentp;
1468 int aclcnt, dfaclcnt;
1471 error = ln_ace_to_aent(acebufp, acecnt, owner, group,
1472 &aclentp, &aclcnt, &dfaclentp, &dfaclcnt, isdir);
1478 if (dfaclcnt != 0) {
1480 * Slap aclentp and dfaclentp into a single array.
1482 aclsz = sizeof (aclent_t) * aclcnt;
1483 dfaclsz = sizeof (aclent_t) * dfaclcnt;
1484 aclentp = cacl_realloc(aclentp, aclsz, aclsz + dfaclsz);
1485 if (aclentp != NULL) {
1486 (void) memcpy(aclentp + aclcnt, dfaclentp, dfaclsz);
1493 *retaclentp = aclentp;
1494 *retaclcnt = aclcnt + dfaclcnt;
1498 cacl_free(dfaclentp, dfaclsz);
1505 acl_translate(acl_t *aclp, int target_flavor, boolean_t isdir, uid_t owner,
1513 * See if we need to translate
1515 if ((target_flavor == _ACL_ACE_ENABLED && aclp->acl_type == ACE_T) ||
1516 (target_flavor == _ACL_ACLENT_ENABLED &&
1517 aclp->acl_type == ACLENT_T))
1520 if (target_flavor == -1) {
1525 if (target_flavor == _ACL_ACE_ENABLED &&
1526 aclp->acl_type == ACLENT_T) {
1527 error = convert_aent_to_ace(aclp->acl_aclp,
1528 aclp->acl_cnt, isdir, (ace_t **)&acldata, &aclcnt);
1532 } else if (target_flavor == _ACL_ACLENT_ENABLED &&
1533 aclp->acl_type == ACE_T) {
1534 error = convert_ace_to_aent(aclp->acl_aclp, aclp->acl_cnt,
1535 isdir, owner, group, (aclent_t **)&acldata, &aclcnt);
1544 * replace old acl with newly translated acl
1546 cacl_free(aclp->acl_aclp, aclp->acl_cnt * aclp->acl_entry_size);
1547 aclp->acl_aclp = acldata;
1548 aclp->acl_cnt = aclcnt;
1549 if (target_flavor == _ACL_ACE_ENABLED) {
1550 aclp->acl_type = ACE_T;
1551 aclp->acl_entry_size = sizeof (ace_t);
1553 aclp->acl_type = ACLENT_T;
1554 aclp->acl_entry_size = sizeof (aclent_t);
1560 #if !defined(_KERNEL)
1567 #endif /* !_KERNEL */
1569 #define SET_ACE(acl, index, who, mask, type, flags) { \
1570 acl[0][index].a_who = (uint32_t)who; \
1571 acl[0][index].a_type = type; \
1572 acl[0][index].a_flags = flags; \
1573 acl[0][index++].a_access_mask = mask; \
1577 acl_trivial_access_masks(mode_t mode, boolean_t isdir, trivial_acl_t *masks)
1579 uint32_t read_mask = ACE_READ_DATA;
1580 uint32_t write_mask = ACE_WRITE_DATA|ACE_APPEND_DATA;
1581 uint32_t execute_mask = ACE_EXECUTE;
1583 (void) isdir; /* will need this later */
1586 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
1587 masks->deny1 |= read_mask;
1588 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH)))
1589 masks->deny1 |= write_mask;
1590 if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
1591 masks->deny1 |= execute_mask;
1594 if (!(mode & S_IRGRP) && (mode & S_IROTH))
1595 masks->deny2 |= read_mask;
1596 if (!(mode & S_IWGRP) && (mode & S_IWOTH))
1597 masks->deny2 |= write_mask;
1598 if (!(mode & S_IXGRP) && (mode & S_IXOTH))
1599 masks->deny2 |= execute_mask;
1602 if ((mode & S_IRUSR) && (!(mode & S_IRGRP) && (mode & S_IROTH)))
1603 masks->allow0 |= read_mask;
1604 if ((mode & S_IWUSR) && (!(mode & S_IWGRP) && (mode & S_IWOTH)))
1605 masks->allow0 |= write_mask;
1606 if ((mode & S_IXUSR) && (!(mode & S_IXGRP) && (mode & S_IXOTH)))
1607 masks->allow0 |= execute_mask;
1609 masks->owner = ACE_WRITE_ATTRIBUTES|ACE_WRITE_OWNER|ACE_WRITE_ACL|
1610 ACE_WRITE_NAMED_ATTRS|ACE_READ_ACL|ACE_READ_ATTRIBUTES|
1611 ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE;
1613 masks->owner |= read_mask;
1615 masks->owner |= write_mask;
1617 masks->owner |= execute_mask;
1619 masks->group = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
1622 masks->group |= read_mask;
1624 masks->group |= write_mask;
1626 masks->group |= execute_mask;
1628 masks->everyone = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
1631 masks->everyone |= read_mask;
1633 masks->everyone |= write_mask;
1635 masks->everyone |= execute_mask;
1639 acl_trivial_create(mode_t mode, boolean_t isdir, ace_t **acl, int *count)
1643 trivial_acl_t masks;
1646 acl_trivial_access_masks(mode, isdir, &masks);
1655 if ((error = cacl_malloc((void **)acl, *count * sizeof (ace_t))) != 0)
1659 SET_ACE(acl, index, -1, masks.allow0,
1660 ACE_ACCESS_ALLOWED_ACE_TYPE, ACE_OWNER);
1663 SET_ACE(acl, index, -1, masks.deny1,
1664 ACE_ACCESS_DENIED_ACE_TYPE, ACE_OWNER);
1667 SET_ACE(acl, index, -1, masks.deny2,
1668 ACE_ACCESS_DENIED_ACE_TYPE, ACE_GROUP|ACE_IDENTIFIER_GROUP);
1671 SET_ACE(acl, index, -1, masks.owner, ACE_ACCESS_ALLOWED_ACE_TYPE,
1673 SET_ACE(acl, index, -1, masks.group, ACE_ACCESS_ALLOWED_ACE_TYPE,
1674 ACE_IDENTIFIER_GROUP|ACE_GROUP);
1675 SET_ACE(acl, index, -1, masks.everyone, ACE_ACCESS_ALLOWED_ACE_TYPE,
1683 * determine whether an ace_t acl is trivial
1685 * Trivialness implies that the acl is composed of only
1686 * owner, group, everyone entries. ACL can't
1687 * have read_acl denied, and write_owner/write_acl/write_attributes
1688 * can only be owner@ entry.
1691 ace_trivial_common(void *acep, int aclcnt,
1692 uint64_t (*walk)(void *, uint64_t, int aclcnt,
1693 uint16_t *, uint16_t *, uint32_t *))
1698 uint64_t cookie = 0;
1700 while (cookie = walk(acep, cookie, aclcnt, &flags, &type, &mask)) {
1701 switch (flags & ACE_TYPE_FLAGS) {
1703 case ACE_GROUP|ACE_IDENTIFIER_GROUP:
1711 if (flags & (ACE_FILE_INHERIT_ACE|
1712 ACE_DIRECTORY_INHERIT_ACE|ACE_NO_PROPAGATE_INHERIT_ACE|
1713 ACE_INHERIT_ONLY_ACE))
1717 * Special check for some special bits
1719 * Don't allow anybody to deny reading basic
1720 * attributes or a files ACL.
1722 if ((mask & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
1723 (type == ACE_ACCESS_DENIED_ACE_TYPE))
1727 * Delete permissions are never set by default
1729 if (mask & (ACE_DELETE|ACE_DELETE_CHILD))
1732 * only allow owner@ to have
1733 * write_acl/write_owner/write_attributes/write_xattr/
1735 if (type == ACE_ACCESS_ALLOWED_ACE_TYPE &&
1736 (!(flags & ACE_OWNER) && (mask &
1737 (ACE_WRITE_OWNER|ACE_WRITE_ACL| ACE_WRITE_ATTRIBUTES|
1738 ACE_WRITE_NAMED_ATTRS))))
1746 ace_walk(void *datap, uint64_t cookie, int aclcnt, uint16_t *flags,
1747 uint16_t *type, uint32_t *mask)
1749 ace_t *acep = datap;
1751 if (cookie >= aclcnt)
1754 *flags = acep[cookie].a_flags;
1755 *type = acep[cookie].a_type;
1756 *mask = acep[cookie++].a_access_mask;
1762 ace_trivial(ace_t *acep, int aclcnt)
1764 return (ace_trivial_common(acep, aclcnt, ace_walk));