xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_acl.c (revision 6139:5c743b207bf9)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
22*6139Sjb150015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
275331Samw 
285331Samw #include <sys/acl.h>
295521Sas200622 #include <acl/acl_common.h>
305331Samw #include <smbsrv/ntsid.h>
315331Samw #include <smbsrv/smb_fsops.h>
325331Samw #include <smbsrv/smb_idmap.h>
335521Sas200622 #include <smbsrv/smb_kproto.h>
345521Sas200622 #include <smbsrv/ntstatus.h>
355521Sas200622 #include <smbsrv/ntaccess.h>
365331Samw 
375331Samw #define	ACE_FD_INHERIT_ACE (ACE_FILE_INHERIT_ACE | ACE_DIRECTORY_INHERIT_ACE)
385331Samw 
395331Samw #define	ZACE_IS_OWNER(zace) ((zace->a_flags & ACE_TYPE_FLAGS) == ACE_OWNER)
405331Samw #define	ZACE_IS_OWNGRP(zace) \
415331Samw 	((zace->a_flags & ACE_TYPE_FLAGS) == (ACE_IDENTIFIER_GROUP|ACE_GROUP))
425331Samw 
435331Samw #define	ZACE_IS_USER(zace) \
445331Samw 	(((zace->a_flags & ACE_TYPE_FLAGS) == 0) || (ZACE_IS_OWNER(zace)))
455331Samw #define	ZACE_IS_GROUP(zace) (zace->a_flags & ACE_IDENTIFIER_GROUP)
465331Samw #define	ZACE_IS_EVERYONE(zace) (zace->a_flags & ACE_EVERYONE)
475331Samw 
485331Samw #define	ZACE_IS_PROPAGATE(zace) \
495331Samw 	((zace->a_flags & ACE_NO_PROPAGATE_INHERIT_ACE) == 0)
505331Samw 
515331Samw #define	ZACE_IS_CREATOR_OWNER(zace) \
525331Samw 	(ZACE_IS_USER(zace) && (zace->a_who == IDMAP_WK_CREATOR_OWNER_UID))
535331Samw 
545331Samw #define	ZACE_IS_CREATOR_GROUP(zace) \
555331Samw 	(ZACE_IS_GROUP(zace) && (zace->a_who == IDMAP_WK_CREATOR_GROUP_GID))
565331Samw 
575331Samw #define	ZACE_IS_CREATOR(zace) \
585331Samw 	(ZACE_IS_CREATOR_OWNER(zace) || ZACE_IS_CREATOR_GROUP(zace))
595331Samw 
605331Samw /*
615331Samw  * ACE groups within a DACL
625331Samw  *
635331Samw  * This is from lower to higher ACE order priority
645331Samw  */
655331Samw #define	SMB_AG_START		0
665331Samw #define	SMB_AG_ALW_INHRT	0
675331Samw #define	SMB_AG_DNY_INHRT	1
685331Samw #define	SMB_AG_ALW_DRCT		2
695331Samw #define	SMB_AG_DNY_DRCT		3
705331Samw #define	SMB_AG_NUM		4
715331Samw 
725331Samw /*
735521Sas200622  * SID for Everyone group: S-1-1-0.
745521Sas200622  */
755521Sas200622 nt_sid_t everyone_sid = {
765521Sas200622 	NT_SID_REVISION,
775521Sas200622 	1,
785521Sas200622 	NT_SECURITY_WORLD_AUTH,
795521Sas200622 	{ 0 }
805521Sas200622 };
815521Sas200622 
825521Sas200622 #define	DEFAULT_DACL_ACENUM	2
835521Sas200622 /*
845521Sas200622  * Default ACL:
855521Sas200622  *    owner: full access
865521Sas200622  *    SYSTEM: full access
875521Sas200622  */
885521Sas200622 static ace_t default_dacl[DEFAULT_DACL_ACENUM] = {
895521Sas200622 	{ (uid_t)-1, ACE_ALL_PERMS, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
905521Sas200622 	{ IDMAP_WK_LOCAL_SYSTEM_GID, ACE_ALL_PERMS, ACE_IDENTIFIER_GROUP,
915521Sas200622 	    ACE_ACCESS_ALLOWED_ACE_TYPE }
925521Sas200622 };
935521Sas200622 
945521Sas200622 /*
955521Sas200622  * Note:
965331Samw  *
975521Sas200622  * smb_acl_xxx functions work with smb_acl_t which represents the CIFS format
985521Sas200622  * smb_fsacl_xxx functions work with acl_t which represents the Solaris native
995521Sas200622  * format
1005521Sas200622  */
1015521Sas200622 
1025521Sas200622 static idmap_stat smb_acl_getsids(smb_idmap_batch_t *, acl_t *, uid_t, gid_t);
1035521Sas200622 static acl_t *smb_acl_null_empty(boolean_t null);
1045521Sas200622 
1055521Sas200622 static int smb_fsacl_inheritable(acl_t *, int);
1065521Sas200622 
1075521Sas200622 
1085521Sas200622 static void smb_ace_inherit(ace_t *, ace_t *, int);
1095521Sas200622 static boolean_t smb_ace_isvalid(smb_ace_t *, int);
1105521Sas200622 static uint16_t smb_ace_len(smb_ace_t *);
1115521Sas200622 static uint32_t smb_ace_mask_g2s(uint32_t);
1125521Sas200622 static uint16_t smb_ace_flags_tozfs(uint8_t, int);
1135521Sas200622 static uint8_t smb_ace_flags_fromzfs(uint16_t);
1145521Sas200622 
1155521Sas200622 smb_acl_t *
1165521Sas200622 smb_acl_alloc(uint8_t revision, uint16_t bsize, uint16_t acecnt)
1175521Sas200622 {
1185521Sas200622 	smb_acl_t *acl;
1195521Sas200622 	int size;
1205521Sas200622 
1215521Sas200622 	size = sizeof (smb_acl_t) + (acecnt * sizeof (smb_ace_t));
1225521Sas200622 	acl = kmem_zalloc(size, KM_SLEEP);
1235521Sas200622 	acl->sl_revision = revision;
1245521Sas200622 	acl->sl_bsize = bsize;
1255521Sas200622 	acl->sl_acecnt = acecnt;
1265521Sas200622 	acl->sl_aces = (smb_ace_t *)(acl + 1);
1275521Sas200622 
1285521Sas200622 	list_create(&acl->sl_sorted, sizeof (smb_ace_t),
1295521Sas200622 	    offsetof(smb_ace_t, se_sln));
1305521Sas200622 	return (acl);
1315521Sas200622 }
1325521Sas200622 
1335521Sas200622 void
1345521Sas200622 smb_acl_free(smb_acl_t *acl)
1355521Sas200622 {
1365521Sas200622 	int i, size;
1375521Sas200622 	void *ace;
1385521Sas200622 
1395521Sas200622 	if (acl == NULL)
1405521Sas200622 		return;
1415521Sas200622 
1425521Sas200622 	for (i = 0; i < acl->sl_acecnt; i++) {
1435521Sas200622 		MEM_FREE("smbsrv", acl->sl_aces[i].se_sid);
1445521Sas200622 	}
1455521Sas200622 
1465521Sas200622 	while ((ace = list_head(&acl->sl_sorted)) != NULL)
1475521Sas200622 		list_remove(&acl->sl_sorted, ace);
1485521Sas200622 	list_destroy(&acl->sl_sorted);
1495521Sas200622 
1505521Sas200622 	size = sizeof (smb_acl_t) + (acl->sl_acecnt * sizeof (smb_ace_t));
1515521Sas200622 	kmem_free(acl, size);
1525521Sas200622 }
1535521Sas200622 
1545521Sas200622 /*
1555521Sas200622  * smb_acl_len
1565521Sas200622  *
1575521Sas200622  * Returns the size of given ACL in bytes. Note that this
1585521Sas200622  * is not an in-memory size, it's the ACL's size as it would
1595521Sas200622  * appear on the wire
1605521Sas200622  */
1615521Sas200622 uint16_t
1625521Sas200622 smb_acl_len(smb_acl_t *acl)
1635521Sas200622 {
1645521Sas200622 	if (acl == NULL)
1655521Sas200622 		return (0);
1665521Sas200622 
1675521Sas200622 	return (acl->sl_bsize);
1685521Sas200622 }
1695521Sas200622 
1705521Sas200622 boolean_t
1715521Sas200622 smb_acl_isvalid(smb_acl_t *acl, int which_acl)
1725521Sas200622 {
1735521Sas200622 	int i;
1745521Sas200622 
1755521Sas200622 	if (acl->sl_bsize < SMB_ACL_HDRSIZE)
1765521Sas200622 		return (B_FALSE);
1775521Sas200622 
1785521Sas200622 	if (acl->sl_revision != ACL_REVISION) {
1795521Sas200622 		/*
1805521Sas200622 		 * we are rejecting ACLs with object-specific ACEs for now
1815521Sas200622 		 */
1825521Sas200622 		return (B_FALSE);
1835521Sas200622 	}
1845521Sas200622 
1855521Sas200622 	for (i = 0; i < acl->sl_acecnt; i++) {
1865521Sas200622 		if (!smb_ace_isvalid(&acl->sl_aces[i], which_acl))
1875521Sas200622 			return (B_FALSE);
1885521Sas200622 	}
1895521Sas200622 
1905521Sas200622 	return (B_TRUE);
1915521Sas200622 }
1925521Sas200622 
1935521Sas200622 /*
1945521Sas200622  * smb_acl_sort
1955521Sas200622  *
1965521Sas200622  * Sorts the given ACL in place if it needs to be sorted.
1975331Samw  *
1985331Samw  * The following is an excerpt from MSDN website.
1995331Samw  *
2005331Samw  * Order of ACEs in a DACL
2015331Samw  *
2025331Samw  * For Windows NT versions 4.0 and earlier, the preferred order of ACEs
2035331Samw  * is simple: In a DACL, all access-denied ACEs should precede any
2045331Samw  * access-allowed ACEs.
2055331Samw  *
2065331Samw  * For Windows 2000 or later, the proper order of ACEs is more complicated
2075331Samw  * because of the introduction of object-specific ACEs and automatic
2085331Samw  * inheritance.
2095331Samw  *
2105331Samw  * The following describes the preferred order:
2115331Samw  *
2125331Samw  * To ensure that noninherited ACEs have precedence over inherited ACEs,
2135331Samw  * place all noninherited ACEs in a group before any inherited ACEs. This
2145331Samw  * ordering ensures, for example, that a noninherited access-denied ACE
2155331Samw  * is enforced regardless of any inherited ACE that allows access.
2165331Samw  * Within the groups of noninherited ACEs and inherited ACEs, order ACEs
2175331Samw  * according to ACE type, as the following shows:
2185331Samw  * 	. Access-denied ACEs that apply to the object itself
2195331Samw  * 	. Access-denied ACEs that apply to a subobject of the
2205331Samw  *	  object, such as a property set or property
2215331Samw  * 	. Access-allowed ACEs that apply to the object itself
2225331Samw  * 	. Access-allowed ACEs that apply to a subobject of the object
2235331Samw  *
2245521Sas200622  * So, here is the desired ACE order
2255331Samw  *
2265331Samw  * deny-direct, allow-direct, deny-inherited, allow-inherited
2275331Samw  *
2285521Sas200622  * Of course, not all ACE types are required in an ACL.
2295331Samw  */
2305521Sas200622 void
2315331Samw smb_acl_sort(smb_acl_t *acl)
2325331Samw {
2335521Sas200622 	list_t ace_grps[SMB_AG_NUM];
2345521Sas200622 	list_t *alist;
2355331Samw 	smb_ace_t *ace;
2365331Samw 	uint8_t ace_flags;
2375521Sas200622 	int ag, i;
2385331Samw 
2395331Samw 	ASSERT(acl);
2405331Samw 
2415331Samw 	if (acl->sl_acecnt == 0) {
2425331Samw 		/*
2435331Samw 		 * ACL with no entry is a valid ACL and it means
2445331Samw 		 * no access for anybody.
2455331Samw 		 */
2465521Sas200622 		return;
2475331Samw 	}
2485331Samw 
2495331Samw 	for (i = SMB_AG_START; i < SMB_AG_NUM; i++) {
2505521Sas200622 		list_create(&ace_grps[i], sizeof (smb_ace_t),
2515521Sas200622 		    offsetof(smb_ace_t, se_sln));
2525331Samw 	}
2535331Samw 
2545521Sas200622 	for (i = 0, ace = acl->sl_aces; i < acl->sl_acecnt; ++i, ace++) {
2555521Sas200622 		ace_flags = ace->se_hdr.se_flags;
2565331Samw 
2575521Sas200622 		switch (ace->se_hdr.se_type) {
2585331Samw 		case ACCESS_DENIED_ACE_TYPE:
2595521Sas200622 			ag = (ace_flags & INHERITED_ACE) ?
2605521Sas200622 			    SMB_AG_DNY_INHRT : SMB_AG_DNY_DRCT;
2615331Samw 			break;
2625331Samw 
2635331Samw 		case ACCESS_ALLOWED_ACE_TYPE:
2645521Sas200622 			ag = (ace_flags & INHERITED_ACE) ?
2655521Sas200622 			    SMB_AG_ALW_INHRT : SMB_AG_ALW_DRCT;
2665331Samw 			break;
2675331Samw 
2685331Samw 		default:
2695331Samw 			/*
2705331Samw 			 * This is the lowest priority group so we put
2715331Samw 			 * evertything unknown here.
2725331Samw 			 */
2735331Samw 			ag = SMB_AG_ALW_INHRT;
2745331Samw 			break;
2755331Samw 		}
2765331Samw 
2775521Sas200622 		/* Add the ACE to the selected group */
2785521Sas200622 		list_insert_tail(&ace_grps[ag], ace);
2795331Samw 	}
2805331Samw 
2815521Sas200622 	/*
2825521Sas200622 	 * start with highest priority ACE group and append
2835521Sas200622 	 * the ACEs to the ACL.
2845521Sas200622 	 */
2855521Sas200622 	for (i = SMB_AG_NUM - 1; i >= SMB_AG_START; i--) {
2865521Sas200622 		alist = &ace_grps[i];
2875521Sas200622 		while ((ace = list_head(alist)) != NULL) {
2885521Sas200622 			list_remove(alist, ace);
2895521Sas200622 			list_insert_tail(&acl->sl_sorted, ace);
2905521Sas200622 		}
2915521Sas200622 		list_destroy(alist);
2925331Samw 	}
2935331Samw }
2945331Samw 
2955521Sas200622 /*
2965521Sas200622  * smb_acl_from_zfs
2975521Sas200622  *
2985521Sas200622  * Converts given ZFS ACL to a Windows ACL.
2995521Sas200622  *
3005521Sas200622  * A pointer to allocated memory for the Win ACL will be
3015521Sas200622  * returned upon successful conversion.
3025521Sas200622  */
3035521Sas200622 smb_acl_t *
3045521Sas200622 smb_acl_from_zfs(acl_t *zacl, uid_t uid, gid_t gid)
3055331Samw {
3065521Sas200622 	ace_t *zace;
3075521Sas200622 	int numaces;
3085521Sas200622 	smb_acl_t *acl;
3095331Samw 	smb_ace_t *ace;
3105521Sas200622 	smb_idmap_batch_t sib;
3115521Sas200622 	smb_idmap_t *sim;
3125521Sas200622 	idmap_stat idm_stat;
3135331Samw 
3145521Sas200622 	idm_stat = smb_idmap_batch_create(&sib, zacl->acl_cnt,
3155521Sas200622 	    SMB_IDMAP_ID2SID);
3165521Sas200622 	if (idm_stat != IDMAP_SUCCESS)
3175331Samw 		return (NULL);
3185331Samw 
3195521Sas200622 	if (smb_acl_getsids(&sib, zacl, uid, gid) != IDMAP_SUCCESS) {
3205521Sas200622 		smb_idmap_batch_destroy(&sib);
3215521Sas200622 		return (NULL);
3225331Samw 	}
3235331Samw 
3245521Sas200622 	acl = smb_acl_alloc(ACL_REVISION, SMB_ACL_HDRSIZE, zacl->acl_cnt);
3255331Samw 
3265521Sas200622 	sim = sib.sib_maps;
3275521Sas200622 	for (numaces = 0, zace = zacl->acl_aclp;
3285521Sas200622 	    numaces < zacl->acl_cnt;
3295521Sas200622 	    zace++, numaces++, sim++) {
3305521Sas200622 		ASSERT(sim->sim_sid);
3315521Sas200622 		if (sim->sim_sid == NULL) {
3325521Sas200622 			smb_acl_free(acl);
3335521Sas200622 			acl = NULL;
3345521Sas200622 			break;
3355521Sas200622 		}
3365331Samw 
3375521Sas200622 		ace = &acl->sl_aces[numaces];
3385521Sas200622 		ace->se_hdr.se_type = zace->a_type;
3395521Sas200622 		ace->se_hdr.se_flags = smb_ace_flags_fromzfs(zace->a_flags);
3405521Sas200622 		ace->se_mask = zace->a_access_mask;
3415521Sas200622 		ace->se_sid = nt_sid_dup(sim->sim_sid);
3425521Sas200622 		ace->se_hdr.se_bsize = smb_ace_len(ace);
3435521Sas200622 
3445521Sas200622 		acl->sl_bsize += ace->se_hdr.se_bsize;
3455331Samw 	}
3465331Samw 
3475521Sas200622 	smb_idmap_batch_destroy(&sib);
3485521Sas200622 	return (acl);
3495331Samw }
3505331Samw 
3515331Samw /*
3525521Sas200622  * smb_acl_to_zfs
3535331Samw  *
3545521Sas200622  * Converts given Windows ACL to a ZFS ACL.
3555331Samw  *
3565521Sas200622  * fs_acl will contain a pointer to the created ZFS ACL.
3575521Sas200622  * The allocated memory should be freed by calling
3585521Sas200622  * smb_fsacl_free().
3595331Samw  *
3605521Sas200622  * Since the output parameter, fs_acl, is allocated in this
3615521Sas200622  * function, the caller has to make sure *fs_acl is NULL which
3625521Sas200622  * means it's not pointing to any memory.
3635331Samw  */
3645331Samw uint32_t
3655521Sas200622 smb_acl_to_zfs(smb_acl_t *acl, uint32_t flags, int which_acl, acl_t **fs_acl)
3665331Samw {
3675521Sas200622 	smb_ace_t *ace;
3685521Sas200622 	acl_t *zacl;
3695521Sas200622 	ace_t *zace;
3705521Sas200622 	smb_idmap_batch_t sib;
3715521Sas200622 	smb_idmap_t *sim;
3725521Sas200622 	idmap_stat idm_stat;
3735521Sas200622 	int i, isdir;
3745331Samw 
3755521Sas200622 	ASSERT(fs_acl);
3765521Sas200622 	ASSERT(*fs_acl == NULL);
3775521Sas200622 
3785521Sas200622 	if (acl && !smb_acl_isvalid(acl, which_acl))
3795521Sas200622 		return (NT_STATUS_INVALID_ACL);
3805521Sas200622 
3815521Sas200622 	if ((acl == NULL) || (acl->sl_acecnt == 0)) {
3825521Sas200622 		if (which_acl == SMB_DACL_SECINFO) {
3835521Sas200622 			*fs_acl = smb_acl_null_empty(acl == NULL);
3845521Sas200622 		}
3855521Sas200622 
3865521Sas200622 		return (NT_STATUS_SUCCESS);
3875331Samw 	}
3885331Samw 
3895521Sas200622 	idm_stat = smb_idmap_batch_create(&sib, acl->sl_acecnt,
3905521Sas200622 	    SMB_IDMAP_SID2ID);
3915521Sas200622 	if (idm_stat != IDMAP_SUCCESS)
3925521Sas200622 		return (NT_STATUS_INTERNAL_ERROR);
3935521Sas200622 
3945521Sas200622 	isdir = ((flags & ACL_IS_DIR) == ACL_IS_DIR);
3955521Sas200622 
3965521Sas200622 	zacl = smb_fsacl_alloc(acl->sl_acecnt, flags);
3975521Sas200622 
3985521Sas200622 	zace = zacl->acl_aclp;
3995521Sas200622 	ace = acl->sl_aces;
4005521Sas200622 	sim = sib.sib_maps;
4015521Sas200622 
4025521Sas200622 	for (i = 0; i < acl->sl_acecnt; i++, zace++, ace++, sim++) {
4035521Sas200622 		zace->a_type = ace->se_hdr.se_type & ACE_ALL_TYPES;
4045521Sas200622 		zace->a_access_mask = smb_ace_mask_g2s(ace->se_mask);
4055521Sas200622 		zace->a_flags = smb_ace_flags_tozfs(ace->se_hdr.se_flags,
4065521Sas200622 		    isdir);
4075521Sas200622 
4085521Sas200622 		if (nt_sid_is_equal(ace->se_sid, &everyone_sid))
4095521Sas200622 			zace->a_flags |= ACE_EVERYONE;
4105521Sas200622 		else {
4115521Sas200622 			sim->sim_id = &zace->a_who;
4125521Sas200622 			idm_stat = smb_idmap_batch_getid(sib.sib_idmaph, sim,
4135521Sas200622 			    ace->se_sid, -1);
4145521Sas200622 
4155521Sas200622 			if (idm_stat != IDMAP_SUCCESS) {
4165521Sas200622 				smb_fsacl_free(zacl);
4175521Sas200622 				smb_idmap_batch_destroy(&sib);
4185521Sas200622 				return (NT_STATUS_INTERNAL_ERROR);
4195521Sas200622 			}
4205521Sas200622 		}
4215331Samw 	}
4225331Samw 
4235521Sas200622 	idm_stat = smb_idmap_batch_getmappings(&sib);
4245521Sas200622 	if (idm_stat != IDMAP_SUCCESS) {
4255521Sas200622 		smb_fsacl_free(zacl);
4265521Sas200622 		smb_idmap_batch_destroy(&sib);
4275521Sas200622 		return (NT_STATUS_NONE_MAPPED);
4285331Samw 	}
4295331Samw 
4305521Sas200622 	/*
4315521Sas200622 	 * Set the ACEs group flag based on the type of ID returned.
4325521Sas200622 	 */
4335521Sas200622 	zace = zacl->acl_aclp;
4345521Sas200622 	ace = acl->sl_aces;
4355521Sas200622 	sim = sib.sib_maps;
4365521Sas200622 	for (i = 0; i < acl->sl_acecnt; i++, zace++, ace++, sim++) {
4375521Sas200622 		if (zace->a_flags & ACE_EVERYONE)
4385521Sas200622 			continue;
4395521Sas200622 
4405521Sas200622 		if (sim->sim_idtype == SMB_IDMAP_GROUP)
4415521Sas200622 			zace->a_flags |= ACE_IDENTIFIER_GROUP;
4425331Samw 	}
4435331Samw 
4445521Sas200622 	smb_idmap_batch_destroy(&sib);
4455521Sas200622 
4465521Sas200622 	*fs_acl = zacl;
4475521Sas200622 	return (NT_STATUS_SUCCESS);
4485331Samw }
4495331Samw 
4505331Samw /*
4515331Samw  * smb_acl_getsids
4525331Samw  *
4535331Samw  * Batch all the uid/gid in given ZFS ACL to get their corresponding SIDs.
4545331Samw  */
4555331Samw static idmap_stat
4565331Samw smb_acl_getsids(smb_idmap_batch_t *sib, acl_t *zacl, uid_t uid, gid_t gid)
4575331Samw {
4585331Samw 	ace_t *zace;
4595331Samw 	idmap_stat idm_stat;
4605331Samw 	smb_idmap_t *sim;
4615331Samw 	uid_t id;
4625331Samw 	int i, idtype;
4635331Samw 
4645331Samw 	sim = sib->sib_maps;
4655331Samw 
4665331Samw 	for (i = 0, zace = zacl->acl_aclp; i < zacl->acl_cnt;
4675331Samw 	    zace++, i++, sim++) {
4685331Samw 		switch (zace->a_flags & ACE_TYPE_FLAGS) {
4695331Samw 		case ACE_OWNER:
4705331Samw 			id = uid;
4715331Samw 			idtype = SMB_IDMAP_USER;
4725331Samw 			break;
4735331Samw 
4745331Samw 		case (ACE_GROUP | ACE_IDENTIFIER_GROUP):
4755331Samw 			/* owning group */
4765331Samw 			id = gid;
4775331Samw 			idtype = SMB_IDMAP_GROUP;
4785331Samw 			break;
4795331Samw 
4805331Samw 		case ACE_IDENTIFIER_GROUP:
4815331Samw 			/* regular group */
4825331Samw 			id = zace->a_who;
4835331Samw 			idtype = SMB_IDMAP_GROUP;
4845331Samw 			break;
4855331Samw 
4865331Samw 		case ACE_EVERYONE:
4875331Samw 			idtype = SMB_IDMAP_EVERYONE;
4885331Samw 			break;
4895331Samw 
4905331Samw 		default:
4915331Samw 			/* user entry */
4925331Samw 			id = zace->a_who;
4935331Samw 			idtype = SMB_IDMAP_USER;
4945331Samw 		}
4955331Samw 
4965331Samw 		idm_stat = smb_idmap_batch_getsid(sib->sib_idmaph, sim,
4975331Samw 		    id, idtype);
4985331Samw 
4995331Samw 		if (idm_stat != IDMAP_SUCCESS) {
5005331Samw 			return (idm_stat);
5015331Samw 		}
5025331Samw 	}
5035331Samw 
5045331Samw 	idm_stat = smb_idmap_batch_getmappings(sib);
5055331Samw 	return (idm_stat);
5065331Samw }
5075331Samw 
5085331Samw /*
5095331Samw  * smb_acl_null_empty
5105331Samw  *
5115331Samw  * NULL DACL means everyone full-access
5125331Samw  * Empty DACL means everyone full-deny
5135331Samw  *
5145331Samw  * ZFS ACL must have at least one entry so smb server has
5155331Samw  * to simulate the aforementioned expected behavior by adding
5165331Samw  * an entry in case the requested DACL is null or empty. Adding
5175331Samw  * a everyone full-deny entry has proved to be problematic in
5185331Samw  * tests since a deny entry takes precedence over allow entries.
5195331Samw  * So, instead of adding a everyone full-deny, an owner ACE with
5205331Samw  * owner implicit permissions will be set.
5215331Samw  */
5225521Sas200622 static acl_t *
5235521Sas200622 smb_acl_null_empty(boolean_t null)
5245331Samw {
5255331Samw 	acl_t *zacl;
5265331Samw 	ace_t *zace;
5275331Samw 
5285521Sas200622 	zacl = smb_fsacl_alloc(1, ACL_AUTO_INHERIT);
5295331Samw 	zace = zacl->acl_aclp;
5305331Samw 
5315331Samw 	zace->a_type = ACE_ACCESS_ALLOWED_ACE_TYPE;
5325331Samw 	if (null) {
5335331Samw 		zace->a_access_mask = ACE_ALL_PERMS;
5345331Samw 		zace->a_flags = ACE_EVERYONE;
5355331Samw 	} else {
5365331Samw 		zace->a_access_mask = ACE_READ_ACL | ACE_WRITE_ACL |
5375331Samw 		    ACE_READ_ATTRIBUTES;
5385331Samw 		zace->a_flags = ACE_OWNER;
5395331Samw 	}
5405331Samw 
5415331Samw 	return (zacl);
5425331Samw }
5435331Samw 
5445331Samw /*
5455521Sas200622  * FS ACL (acl_t) Functions
5465331Samw  */
5475521Sas200622 acl_t *
5485521Sas200622 smb_fsacl_alloc(int acenum, int flags)
5495331Samw {
5505521Sas200622 	acl_t *acl;
5515331Samw 
5525521Sas200622 	acl = acl_alloc(ACE_T);
5535521Sas200622 	acl->acl_cnt = acenum;
5545521Sas200622 	acl->acl_aclp = kmem_zalloc(acl->acl_entry_size * acenum, KM_SLEEP);
5555521Sas200622 	acl->acl_flags = flags;
5565521Sas200622 	return (acl);
5575521Sas200622 }
5585331Samw 
5595521Sas200622 void
5605521Sas200622 smb_fsacl_free(acl_t *acl)
5615521Sas200622 {
5625521Sas200622 	if (acl)
5635521Sas200622 		acl_free(acl);
5645521Sas200622 }
5655331Samw 
5665521Sas200622 /*
5675521Sas200622  * smb_fsop_aclmerge
5685521Sas200622  *
5695521Sas200622  * smb_fsop_aclread/write routines which interact with filesystem
5705521Sas200622  * work with single ACL. This routine merges given DACL and SACL
5715521Sas200622  * which might have been created during CIFS to FS conversion into
5725521Sas200622  * one single ACL.
5735521Sas200622  */
5745521Sas200622 acl_t *
5755521Sas200622 smb_fsacl_merge(acl_t *dacl, acl_t *sacl)
5765521Sas200622 {
5775521Sas200622 	acl_t *acl;
5785521Sas200622 	int dacl_size;
5795331Samw 
5805521Sas200622 	ASSERT(dacl);
5815521Sas200622 	ASSERT(sacl);
5825331Samw 
5835521Sas200622 	acl = smb_fsacl_alloc(dacl->acl_cnt + sacl->acl_cnt, dacl->acl_flags);
5845521Sas200622 	dacl_size = dacl->acl_cnt * dacl->acl_entry_size;
5855521Sas200622 	bcopy(dacl->acl_aclp, acl->acl_aclp, dacl_size);
5865521Sas200622 	bcopy(sacl->acl_aclp, (char *)acl->acl_aclp + dacl_size,
5875521Sas200622 	    sacl->acl_cnt * sacl->acl_entry_size);
5885331Samw 
5895521Sas200622 	return (acl);
5905331Samw }
5915331Samw 
5925331Samw /*
5935521Sas200622  * smb_fsacl_split
5945331Samw  *
5955521Sas200622  * splits the given ACE_T ACL (zacl) to one or two ACLs (DACL/SACL) based on
5965521Sas200622  * the 'which_acl' parameter. Note that output dacl/sacl parameters could be
5975521Sas200622  * NULL even if they're specified in 'which_acl', which means the target
5985521Sas200622  * doesn't have any access and/or audit ACEs.
5995331Samw  */
6005521Sas200622 void
6015521Sas200622 smb_fsacl_split(acl_t *zacl, acl_t **dacl, acl_t **sacl, int which_acl)
6025331Samw {
6035331Samw 	ace_t *zace;
6045521Sas200622 	ace_t *access_ace;
6055521Sas200622 	ace_t *audit_ace;
6065521Sas200622 	int naccess, naudit;
6075521Sas200622 	int get_dacl, get_sacl;
6085521Sas200622 	int i;
6095521Sas200622 
6105521Sas200622 	*dacl = *sacl = NULL;
6115521Sas200622 	naccess = naudit = 0;
6125521Sas200622 	get_dacl = (which_acl & SMB_DACL_SECINFO);
6135521Sas200622 	get_sacl = (which_acl & SMB_SACL_SECINFO);
6145331Samw 
6155521Sas200622 	for (i = 0, zace = zacl->acl_aclp; i < zacl->acl_cnt; zace++, i++) {
6165521Sas200622 		if (get_dacl && smb_ace_is_access(zace->a_type))
6175521Sas200622 			naccess++;
6185521Sas200622 		else if (get_sacl && smb_ace_is_audit(zace->a_type))
6195521Sas200622 			naudit++;
6205521Sas200622 	}
6215331Samw 
6225521Sas200622 	if (naccess) {
6235521Sas200622 		*dacl = smb_fsacl_alloc(naccess, zacl->acl_flags);
6245521Sas200622 		access_ace = (*dacl)->acl_aclp;
6255521Sas200622 	}
6265331Samw 
6275521Sas200622 	if (naudit) {
6285521Sas200622 		*sacl = smb_fsacl_alloc(naudit, zacl->acl_flags);
6295521Sas200622 		audit_ace = (*sacl)->acl_aclp;
6305521Sas200622 	}
6315331Samw 
6325521Sas200622 	for (i = 0, zace = zacl->acl_aclp; i < zacl->acl_cnt; zace++, i++) {
6335521Sas200622 		if (get_dacl && smb_ace_is_access(zace->a_type)) {
6345521Sas200622 			*access_ace = *zace;
6355521Sas200622 			access_ace++;
6365521Sas200622 		} else if (get_sacl && smb_ace_is_audit(zace->a_type)) {
6375521Sas200622 			*audit_ace = *zace;
6385521Sas200622 			audit_ace++;
6395331Samw 		}
6405331Samw 	}
6415331Samw }
6425331Samw 
6435331Samw /*
6445521Sas200622  * ACE Inheritance Rules
6455521Sas200622  *
6465521Sas200622  * The system propagates inheritable ACEs to child objects according to a
6475521Sas200622  * set of inheritance rules. The system places inherited ACEs in the child's
6485521Sas200622  * DACL according to the preferred order of ACEs in a DACL. For Windows
6495521Sas200622  * 2000 or later, the system sets the INHERITED_ACE flag in all inherited ACEs.
6505521Sas200622  *
6515521Sas200622  * The following table shows the ACEs inherited by container and noncontainer
6525521Sas200622  * child objects for different combinations of inheritance flags. These
6535521Sas200622  * inheritance rules work the same for both DACLs and SACLs.
6545521Sas200622  *
6555521Sas200622  * Parent ACE type 			Effect on Child ACL
6565521Sas200622  * -----------------------		-------------------
6575521Sas200622  * OBJECT_INHERIT_ACE only 		Noncontainer child objects:
6585521Sas200622  *					Inherited as an effective ACE.
6595521Sas200622  *					Container child objects:
6605521Sas200622  *					Containers inherit an inherit-only ACE
6615521Sas200622  *					unless the NO_PROPAGATE_INHERIT_ACE bit
6625521Sas200622  *					flag is also set.
6635521Sas200622  *
6645521Sas200622  * CONTAINER_INHERIT_ACE only 		Noncontainer child objects:
6655521Sas200622  *					No effect on the child object.
6665521Sas200622  *					Container child objects:
6675521Sas200622  *				The child object inherits an effective ACE.
6685521Sas200622  *				The inherited ACE is inheritable unless the
6695521Sas200622  *				NO_PROPAGATE_INHERIT_ACE bit flag is also set.
6705521Sas200622  *
6715521Sas200622  * CONTAINER_INHERIT_ACE and
6725521Sas200622  * OBJECT_INHERIT_ACE 			Noncontainer child objects:
6735521Sas200622  *					Inherited as an effective ACE.
6745521Sas200622  *					Container child objects:
6755521Sas200622  *				The child object inherits an effective ACE.
6765521Sas200622  *				The inherited ACE is inheritable unless the
6775521Sas200622  *				NO_PROPAGATE_INHERIT_ACE bit flag is also set
6785521Sas200622  *
6795521Sas200622  * No inheritance flags set 	No effect on child container or noncontainer
6805521Sas200622  *				objects.
6815521Sas200622  *
6825521Sas200622  * If an inherited ACE is an effective ACE for the child object, the system
6835521Sas200622  * maps any generic rights to the specific rights for the child object.
6845521Sas200622  * Similarly, the system maps generic SIDs, such as CREATOR_OWNER, to the
6855521Sas200622  * appropriate SID. If an inherited ACE is an inherit-only ACE, any generic
6865521Sas200622  * rights or generic SIDs are left unchanged so that they can be mapped
6875521Sas200622  * appropriately when the ACE is inherited by the next generation of child
6885521Sas200622  * objects.
6895521Sas200622  *
6905521Sas200622  * For a case in which a container object inherits an ACE that is both
6915521Sas200622  * effective on the container and inheritable by its descendants, the
6925521Sas200622  * container may inherit two ACEs. This occurs if the inheritable ACE
6935521Sas200622  * contains generic information. The container inherits an inherit-only
6945521Sas200622  * ACE containing the generic information and an effective-only ACE in
6955521Sas200622  * which the generic information has been mapped.
6965331Samw  */
6975331Samw 
6985331Samw /*
6995521Sas200622  * smb_fsacl_inherit
7005331Samw  *
7015331Samw  * Manufacture the inherited ACL from the given ACL considering
7025331Samw  * the new object type (file/dir) specified by 'is_dir'. The
7035331Samw  * returned ACL is used in smb_fsop_create/smb_fsop_mkdir functions.
7045521Sas200622  * This function implements Windows inheritance rules explained above.
7055331Samw  *
7065521Sas200622  * Note that the in/out ACLs are ZFS ACLs not Windows ACLs
7075331Samw  */
7085331Samw acl_t *
7095521Sas200622 smb_fsacl_inherit(acl_t *dir_zacl, int is_dir, int which_acl, uid_t owner_uid)
7105331Samw {
7115331Samw 	boolean_t use_default = B_FALSE;
7125331Samw 	int num_inheritable = 0;
7135331Samw 	int numaces;
7145331Samw 	ace_t *dir_zace;
7155331Samw 	acl_t *new_zacl;
7165331Samw 	ace_t *new_zace;
7175331Samw 
7185521Sas200622 	num_inheritable = smb_fsacl_inheritable(dir_zacl, is_dir);
7195331Samw 
7205331Samw 	if (num_inheritable == 0) {
7215331Samw 		if (which_acl == SMB_DACL_SECINFO) {
7225331Samw 			/* No inheritable access ACEs -> default DACL */
7235331Samw 			num_inheritable = DEFAULT_DACL_ACENUM;
7245331Samw 			use_default = B_TRUE;
7255331Samw 		} else {
7265331Samw 			return (NULL);
7275331Samw 		}
7285331Samw 	}
7295331Samw 
7305521Sas200622 	new_zacl = smb_fsacl_alloc(num_inheritable, ACL_AUTO_INHERIT);
7315331Samw 	new_zace = new_zacl->acl_aclp;
7325331Samw 
7335331Samw 	if (use_default) {
7345331Samw 		bcopy(default_dacl, new_zacl->acl_aclp, sizeof (default_dacl));
7355331Samw 		new_zace->a_who = owner_uid;
7365331Samw 		return (new_zacl);
7375331Samw 	}
7385331Samw 
7395331Samw 	for (numaces = 0, dir_zace = dir_zacl->acl_aclp;
7405331Samw 	    numaces < dir_zacl->acl_cnt;
7415331Samw 	    dir_zace++, numaces++) {
7425331Samw 		switch (dir_zace->a_flags & ACE_FD_INHERIT_ACE) {
7435331Samw 		case (ACE_FILE_INHERIT_ACE | ACE_DIRECTORY_INHERIT_ACE):
7445331Samw 			/*
7455331Samw 			 * Files inherit an effective ACE.
7465331Samw 			 *
7475331Samw 			 * Dirs inherit an effective ACE.
7485331Samw 			 * The inherited ACE is inheritable unless the
7495331Samw 			 * ACE_NO_PROPAGATE_INHERIT_ACE bit flag is also set
7505331Samw 			 */
7515331Samw 			smb_ace_inherit(dir_zace, new_zace, is_dir);
7525331Samw 			new_zace++;
7535331Samw 
7545331Samw 			if (is_dir && ZACE_IS_CREATOR(dir_zace) &&
7555331Samw 			    (ZACE_IS_PROPAGATE(dir_zace))) {
7565331Samw 				*new_zace = *dir_zace;
7575331Samw 				new_zace->a_flags |= (ACE_INHERIT_ONLY_ACE |
7585331Samw 				    ACE_INHERITED_ACE);
7595331Samw 				new_zace++;
7605331Samw 			}
7615331Samw 			break;
7625331Samw 
7635331Samw 		case ACE_FILE_INHERIT_ACE:
7645331Samw 			/*
7655331Samw 			 * Files inherit as an effective ACE.
7665331Samw 			 *
7675331Samw 			 * Dirs inherit an inherit-only ACE
7685331Samw 			 * unless the ACE_NO_PROPAGATE_INHERIT_ACE bit
7695331Samw 			 * flag is also set.
7705331Samw 			 */
7715331Samw 			if (is_dir == 0) {
7725331Samw 				smb_ace_inherit(dir_zace, new_zace, is_dir);
7735331Samw 				new_zace++;
7745331Samw 			} else if (ZACE_IS_PROPAGATE(dir_zace)) {
7755331Samw 				*new_zace = *dir_zace;
7765331Samw 				new_zace->a_flags |= (ACE_INHERIT_ONLY_ACE |
7775331Samw 				    ACE_INHERITED_ACE);
7785331Samw 				new_zace++;
7795331Samw 			}
7805331Samw 			break;
7815331Samw 
7825331Samw 		case ACE_DIRECTORY_INHERIT_ACE:
7835331Samw 			/*
7845331Samw 			 * No effect on files
7855331Samw 			 *
7865331Samw 			 * Dirs inherit an effective ACE.
7875331Samw 			 * The inherited ACE is inheritable unless the
7885331Samw 			 * ACE_NO_PROPAGATE_INHERIT_ACE bit flag is also set.
7895331Samw 			 */
7905331Samw 			if (is_dir == 0)
7915331Samw 				break;
7925331Samw 
7935331Samw 			smb_ace_inherit(dir_zace, new_zace, is_dir);
7945331Samw 			new_zace++;
7955331Samw 
7965331Samw 			if (ZACE_IS_CREATOR(dir_zace) &&
7975331Samw 			    (ZACE_IS_PROPAGATE(dir_zace))) {
7985331Samw 				*new_zace = *dir_zace;
7995331Samw 				new_zace->a_flags |= (ACE_INHERIT_ONLY_ACE |
8005331Samw 				    ACE_INHERITED_ACE);
8015331Samw 				new_zace++;
8025331Samw 			}
8035331Samw 
8045331Samw 			break;
8055331Samw 
8065331Samw 		default:
8075331Samw 			break;
8085331Samw 		}
8095331Samw 	}
8105331Samw 
8115331Samw 	return (new_zacl);
8125331Samw }
8135331Samw 
8145521Sas200622 /*
8155521Sas200622  * smb_fsacl_from_vsa
8165521Sas200622  *
8175521Sas200622  * Converts given vsecattr_t structure to a acl_t structure.
8185521Sas200622  *
8195521Sas200622  * The allocated memory for retuned acl_t should be freed by
8205521Sas200622  * calling acl_free().
8215521Sas200622  */
8225521Sas200622 acl_t *
8235521Sas200622 smb_fsacl_from_vsa(vsecattr_t *vsecattr, acl_type_t acl_type)
8245521Sas200622 {
8255521Sas200622 	int		aclbsize = 0;	/* size of acl list in bytes */
8265521Sas200622 	int		dfaclbsize = 0;	/* size of default acl list in bytes */
8275521Sas200622 	int		numacls;
8285521Sas200622 	acl_t		*acl_info;
8295521Sas200622 
8305521Sas200622 	ASSERT(vsecattr);
8315521Sas200622 
8325521Sas200622 	acl_info = acl_alloc(acl_type);
8335521Sas200622 	if (acl_info == NULL)
8345521Sas200622 		return (NULL);
8355521Sas200622 
8365521Sas200622 	acl_info->acl_flags = 0;
8375521Sas200622 
8385521Sas200622 	switch (acl_type) {
8395521Sas200622 
8405521Sas200622 	case ACLENT_T:
8415521Sas200622 		numacls = vsecattr->vsa_aclcnt + vsecattr->vsa_dfaclcnt;
8425521Sas200622 		aclbsize = vsecattr->vsa_aclcnt * sizeof (aclent_t);
8435521Sas200622 		dfaclbsize = vsecattr->vsa_dfaclcnt * sizeof (aclent_t);
8445521Sas200622 
8455521Sas200622 		acl_info->acl_cnt = numacls;
8465521Sas200622 		acl_info->acl_aclp = kmem_alloc(aclbsize + dfaclbsize,
8475521Sas200622 		    KM_SLEEP);
8485521Sas200622 		(void) memcpy(acl_info->acl_aclp, vsecattr->vsa_aclentp,
8495521Sas200622 		    aclbsize);
8505521Sas200622 		(void) memcpy((char *)acl_info->acl_aclp + aclbsize,
8515521Sas200622 		    vsecattr->vsa_dfaclentp, dfaclbsize);
8525521Sas200622 
8535521Sas200622 		if (acl_info->acl_cnt <= MIN_ACL_ENTRIES)
8545521Sas200622 			acl_info->acl_flags |= ACL_IS_TRIVIAL;
8555521Sas200622 
8565521Sas200622 		break;
8575521Sas200622 
8585521Sas200622 	case ACE_T:
8595521Sas200622 		aclbsize = vsecattr->vsa_aclcnt * sizeof (ace_t);
8605521Sas200622 		acl_info->acl_cnt = vsecattr->vsa_aclcnt;
8615521Sas200622 		acl_info->acl_flags = vsecattr->vsa_aclflags;
8625521Sas200622 		acl_info->acl_aclp = kmem_alloc(aclbsize, KM_SLEEP);
8635521Sas200622 		(void) memcpy(acl_info->acl_aclp, vsecattr->vsa_aclentp,
8645521Sas200622 		    aclbsize);
8655521Sas200622 		if (ace_trivial(acl_info->acl_aclp, acl_info->acl_cnt) == 0)
8665521Sas200622 			acl_info->acl_flags |= ACL_IS_TRIVIAL;
8675521Sas200622 
8685521Sas200622 		break;
8695521Sas200622 
8705521Sas200622 	default:
8715521Sas200622 		acl_free(acl_info);
8725521Sas200622 		return (NULL);
8735521Sas200622 	}
8745521Sas200622 
8755521Sas200622 	if (aclbsize && vsecattr->vsa_aclentp)
8765521Sas200622 		kmem_free(vsecattr->vsa_aclentp, aclbsize);
8775521Sas200622 	if (dfaclbsize && vsecattr->vsa_dfaclentp)
8785521Sas200622 		kmem_free(vsecattr->vsa_dfaclentp, dfaclbsize);
8795521Sas200622 
8805521Sas200622 	return (acl_info);
8815521Sas200622 }
8825521Sas200622 
8835521Sas200622 /*
8845521Sas200622  * smb_fsacl_to_vsa
8855521Sas200622  *
8865521Sas200622  * Converts given acl_t structure to a vsecattr_t structure.
8875521Sas200622  *
8885521Sas200622  * IMPORTANT:
8895521Sas200622  * Upon successful return the memory allocated for vsa_aclentp
8905521Sas200622  * should be freed by calling kmem_free(). The size is returned
8915521Sas200622  * in aclbsize.
8925521Sas200622  */
8935521Sas200622 int
8945521Sas200622 smb_fsacl_to_vsa(acl_t *acl_info, vsecattr_t *vsecattr, int *aclbsize)
8955521Sas200622 {
8965521Sas200622 	int		error = 0;
8975521Sas200622 	int		numacls;
8985521Sas200622 	aclent_t	*aclp;
8995521Sas200622 
9005521Sas200622 	ASSERT(acl_info);
9015521Sas200622 	ASSERT(vsecattr);
9025521Sas200622 	ASSERT(aclbsize);
9035521Sas200622 
9045521Sas200622 	bzero(vsecattr, sizeof (vsecattr_t));
9055521Sas200622 	*aclbsize = 0;
9065521Sas200622 
9075521Sas200622 	switch (acl_info->acl_type) {
9085521Sas200622 	case ACLENT_T:
9095521Sas200622 		numacls = acl_info->acl_cnt;
9105521Sas200622 		/*
9115521Sas200622 		 * Minimum ACL size is three entries so might as well
9125521Sas200622 		 * bail out here.  Also limit request size to prevent user
9135521Sas200622 		 * from allocating too much kernel memory.  Maximum size
9145521Sas200622 		 * is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
9155521Sas200622 		 * for the default ACL part.
9165521Sas200622 		 */
9175521Sas200622 		if (numacls < 3 || numacls > (MAX_ACL_ENTRIES * 2)) {
9185521Sas200622 			error = EINVAL;
9195521Sas200622 			break;
9205521Sas200622 		}
9215521Sas200622 
9225521Sas200622 		vsecattr->vsa_mask = VSA_ACL;
9235521Sas200622 
9245521Sas200622 		vsecattr->vsa_aclcnt = numacls;
9255521Sas200622 		*aclbsize = numacls * sizeof (aclent_t);
9265521Sas200622 		vsecattr->vsa_aclentp = kmem_alloc(*aclbsize, KM_SLEEP);
9275521Sas200622 		(void) memcpy(vsecattr->vsa_aclentp, acl_info->acl_aclp,
9285521Sas200622 		    *aclbsize);
9295521Sas200622 
9305521Sas200622 		/* Sort the acl list */
9315521Sas200622 		ksort((caddr_t)vsecattr->vsa_aclentp,
9325521Sas200622 		    vsecattr->vsa_aclcnt, sizeof (aclent_t), cmp2acls);
9335521Sas200622 
9345521Sas200622 		/* Break into acl and default acl lists */
9355521Sas200622 		for (numacls = 0, aclp = vsecattr->vsa_aclentp;
9365521Sas200622 		    numacls < vsecattr->vsa_aclcnt;
9375521Sas200622 		    aclp++, numacls++) {
9385521Sas200622 			if (aclp->a_type & ACL_DEFAULT)
9395521Sas200622 				break;
9405521Sas200622 		}
9415521Sas200622 
9425521Sas200622 		/* Find where defaults start (if any) */
9435521Sas200622 		if (numacls < vsecattr->vsa_aclcnt) {
9445521Sas200622 			vsecattr->vsa_mask |= VSA_DFACL;
9455521Sas200622 			vsecattr->vsa_dfaclcnt = vsecattr->vsa_aclcnt - numacls;
9465521Sas200622 			vsecattr->vsa_dfaclentp = aclp;
9475521Sas200622 			vsecattr->vsa_aclcnt = numacls;
9485521Sas200622 		}
9495521Sas200622 
9505521Sas200622 		/* Adjust if they're all defaults */
9515521Sas200622 		if (vsecattr->vsa_aclcnt == 0) {
9525521Sas200622 			vsecattr->vsa_mask &= ~VSA_ACL;
9535521Sas200622 			vsecattr->vsa_aclentp = NULL;
9545521Sas200622 		}
9555521Sas200622 
9565521Sas200622 		/* Only directories can have defaults */
9575521Sas200622 		if (vsecattr->vsa_dfaclcnt &&
9585521Sas200622 		    (acl_info->acl_flags & ACL_IS_DIR)) {
9595521Sas200622 			error = ENOTDIR;
9605521Sas200622 		}
9615521Sas200622 
9625521Sas200622 		break;
9635521Sas200622 
9645521Sas200622 	case ACE_T:
9655521Sas200622 		if (acl_info->acl_cnt < 1 ||
9665521Sas200622 		    acl_info->acl_cnt > MAX_ACL_ENTRIES) {
9675521Sas200622 			error = EINVAL;
9685521Sas200622 			break;
9695521Sas200622 		}
9705521Sas200622 
9715521Sas200622 		vsecattr->vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
9725521Sas200622 		vsecattr->vsa_aclcnt = acl_info->acl_cnt;
9735521Sas200622 		vsecattr->vsa_aclflags = acl_info->acl_flags & ACL_FLAGS_ALL;
9745521Sas200622 		*aclbsize = vsecattr->vsa_aclcnt * sizeof (ace_t);
9755521Sas200622 		vsecattr->vsa_aclentsz = *aclbsize;
9765521Sas200622 		vsecattr->vsa_aclentp = kmem_alloc(*aclbsize, KM_SLEEP);
9775521Sas200622 		(void) memcpy(vsecattr->vsa_aclentp, acl_info->acl_aclp,
9785521Sas200622 		    *aclbsize);
9795521Sas200622 
9805521Sas200622 		break;
9815521Sas200622 
9825521Sas200622 	default:
9835521Sas200622 		error = EINVAL;
9845521Sas200622 	}
9855521Sas200622 
9865521Sas200622 	return (error);
9875521Sas200622 }
9885521Sas200622 
9895521Sas200622 /*
9905521Sas200622  * smb_fsacl_inheritable
9915521Sas200622  *
9925521Sas200622  * Checks to see if there are any inheritable ACEs in the
9935521Sas200622  * given ZFS ACL. Returns the number of inheritable ACEs.
9945521Sas200622  *
9955521Sas200622  * The inherited ACL could be different based on the type of
9965521Sas200622  * new object (file/dir) specified by 'is_dir'.
9975521Sas200622  *
9985521Sas200622  * Note that the input ACL is a ZFS ACL not Windows ACL.
9995521Sas200622  *
10005521Sas200622  * Any ACE except creator owner/group:
10015521Sas200622  *
10025521Sas200622  *  FI   DI   NP   #F  #D
10035521Sas200622  * ---- ---- ---- ---- ----
10045521Sas200622  *  -    -    ?    0    0
10055521Sas200622  *  X    -    -    1    1
10065521Sas200622  *  X    -    X    1    0
10075521Sas200622  *  -    X    -    0    1
10085521Sas200622  *  -    X    X    0    1
10095521Sas200622  *  X    X    -    1    1
10105521Sas200622  *  X    X    X    1    1
10115521Sas200622  *
10125521Sas200622  * Creator owner/group ACE:
10135521Sas200622  *
10145521Sas200622  *  FI   DI   NP   #F  #D
10155521Sas200622  * ---- ---- ---- ---- ----
10165521Sas200622  *  -    -    ?    0    0
10175521Sas200622  *  X    -    -    1r   1c
10185521Sas200622  *  X    -    X    1r   0
10195521Sas200622  *  -    X    -    0    2
10205521Sas200622  *  -    X    X    0    1r
10215521Sas200622  *  X    X    -    1r   2
10225521Sas200622  *  X    X    X    1r   1r
10235521Sas200622  *
10245521Sas200622  * Legend:
10255521Sas200622  *
10265521Sas200622  *  FI: File Inherit
10275521Sas200622  *  DI: Dir Inherit
10285521Sas200622  *  NP: No Propagate
10295521Sas200622  *  #F: #ACE for a new file
10305521Sas200622  *  #D: #ACE for a new dir
10315521Sas200622  *
10325521Sas200622  *   X: bit is set
10335521Sas200622  *   -: bit is not set
10345521Sas200622  *   ?: don't care
10355521Sas200622  *
10365521Sas200622  *  1r: one owner/group ACE
10375521Sas200622  *  1c: one creator owner/group ACE
10385521Sas200622  */
10395521Sas200622 static int
10405521Sas200622 smb_fsacl_inheritable(acl_t *zacl, int is_dir)
10415521Sas200622 {
10425521Sas200622 	int numaces;
10435521Sas200622 	int num_inheritable = 0;
10445521Sas200622 	ace_t *zace;
10455521Sas200622 
10465521Sas200622 	if (zacl == NULL)
10475521Sas200622 		return (0);
10485521Sas200622 
10495521Sas200622 	for (numaces = 0, zace = zacl->acl_aclp;
10505521Sas200622 	    numaces < zacl->acl_cnt;
10515521Sas200622 	    zace++, numaces++) {
10525521Sas200622 		switch (zace->a_flags & ACE_FD_INHERIT_ACE) {
10535521Sas200622 		case (ACE_FILE_INHERIT_ACE | ACE_DIRECTORY_INHERIT_ACE):
10545521Sas200622 			/*
10555521Sas200622 			 * Files inherit an effective ACE.
10565521Sas200622 			 *
10575521Sas200622 			 * Dirs inherit an effective ACE.
10585521Sas200622 			 * The inherited ACE is inheritable unless the
10595521Sas200622 			 * ACE_NO_PROPAGATE_INHERIT_ACE bit flag is also set
10605521Sas200622 			 */
10615521Sas200622 			num_inheritable++;
10625521Sas200622 
10635521Sas200622 			if (is_dir && ZACE_IS_CREATOR(zace) &&
10645521Sas200622 			    (ZACE_IS_PROPAGATE(zace))) {
10655521Sas200622 				num_inheritable++;
10665521Sas200622 			}
10675521Sas200622 			break;
10685521Sas200622 
10695521Sas200622 		case ACE_FILE_INHERIT_ACE:
10705521Sas200622 			/*
10715521Sas200622 			 * Files inherit as an effective ACE.
10725521Sas200622 			 *
10735521Sas200622 			 * Dirs inherit an inherit-only ACE
10745521Sas200622 			 * unless the ACE_NO_PROPAGATE_INHERIT_ACE bit
10755521Sas200622 			 * flag is also set.
10765521Sas200622 			 */
10775521Sas200622 			if (is_dir == 0)
10785521Sas200622 				num_inheritable++;
10795521Sas200622 			else if (ZACE_IS_PROPAGATE(zace))
10805521Sas200622 				num_inheritable++;
10815521Sas200622 			break;
10825521Sas200622 
10835521Sas200622 		case ACE_DIRECTORY_INHERIT_ACE:
10845521Sas200622 			/*
10855521Sas200622 			 * No effect on files
10865521Sas200622 			 *
10875521Sas200622 			 * Dirs inherit an effective ACE.
10885521Sas200622 			 * The inherited ACE is inheritable unless the
10895521Sas200622 			 * ACE_NO_PROPAGATE_INHERIT_ACE bit flag is also set.
10905521Sas200622 			 */
10915521Sas200622 			if (is_dir == 0)
10925521Sas200622 				break;
10935521Sas200622 
10945521Sas200622 			num_inheritable++;
10955521Sas200622 
10965521Sas200622 			if (ZACE_IS_CREATOR(zace) &&
10975521Sas200622 			    (ZACE_IS_PROPAGATE(zace)))
10985521Sas200622 				num_inheritable++;
10995521Sas200622 			break;
11005521Sas200622 
11015521Sas200622 		default:
11025521Sas200622 			break;
11035521Sas200622 		}
11045521Sas200622 	}
11055521Sas200622 
11065521Sas200622 	return (num_inheritable);
11075521Sas200622 }
11085521Sas200622 
11095521Sas200622 
11105521Sas200622 /*
11115521Sas200622  * ACE Functions
11125521Sas200622  */
11135521Sas200622 
11145521Sas200622 /*
11155521Sas200622  * This is generic (ACL version 2) vs. object-specific
11165521Sas200622  * (ACL version 4) ACE types.
11175521Sas200622  */
11185521Sas200622 boolean_t
11195521Sas200622 smb_ace_is_generic(int type)
11205521Sas200622 {
11215521Sas200622 	switch (type) {
11225521Sas200622 	case ACE_ACCESS_ALLOWED_ACE_TYPE:
11235521Sas200622 	case ACE_ACCESS_DENIED_ACE_TYPE:
11245521Sas200622 	case ACE_SYSTEM_AUDIT_ACE_TYPE:
11255521Sas200622 	case ACE_SYSTEM_ALARM_ACE_TYPE:
11265521Sas200622 	case ACE_ACCESS_ALLOWED_CALLBACK_ACE_TYPE:
11275521Sas200622 	case ACE_ACCESS_DENIED_CALLBACK_ACE_TYPE:
11285521Sas200622 	case ACE_SYSTEM_AUDIT_CALLBACK_ACE_TYPE:
11295521Sas200622 	case ACE_SYSTEM_ALARM_CALLBACK_ACE_TYPE:
11305521Sas200622 		return (B_TRUE);
11315521Sas200622 
11325521Sas200622 	default:
11335521Sas200622 		break;
11345521Sas200622 	}
11355521Sas200622 
11365521Sas200622 	return (B_FALSE);
11375521Sas200622 }
11385521Sas200622 
11395521Sas200622 boolean_t
11405521Sas200622 smb_ace_is_access(int type)
11415521Sas200622 {
11425521Sas200622 	switch (type) {
11435521Sas200622 	case ACE_ACCESS_ALLOWED_ACE_TYPE:
11445521Sas200622 	case ACE_ACCESS_DENIED_ACE_TYPE:
11455521Sas200622 	case ACE_ACCESS_ALLOWED_COMPOUND_ACE_TYPE:
11465521Sas200622 	case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
11475521Sas200622 	case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
11485521Sas200622 	case ACE_ACCESS_ALLOWED_CALLBACK_ACE_TYPE:
11495521Sas200622 	case ACE_ACCESS_DENIED_CALLBACK_ACE_TYPE:
11505521Sas200622 	case ACE_ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE:
11515521Sas200622 	case ACE_ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE:
11525521Sas200622 		return (B_TRUE);
11535521Sas200622 
11545521Sas200622 	default:
11555521Sas200622 		break;
11565521Sas200622 	}
11575521Sas200622 
11585521Sas200622 	return (B_FALSE);
11595521Sas200622 }
11605521Sas200622 
11615521Sas200622 boolean_t
11625521Sas200622 smb_ace_is_audit(int type)
11635521Sas200622 {
11645521Sas200622 	switch (type) {
11655521Sas200622 	case ACE_SYSTEM_AUDIT_ACE_TYPE:
11665521Sas200622 	case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
11675521Sas200622 	case ACE_SYSTEM_AUDIT_CALLBACK_ACE_TYPE:
11685521Sas200622 	case ACE_SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE:
11695521Sas200622 		return (B_TRUE);
11705521Sas200622 
11715521Sas200622 	default:
11725521Sas200622 		break;
11735521Sas200622 	}
11745521Sas200622 
11755521Sas200622 	return (B_FALSE);
11765521Sas200622 }
11775521Sas200622 
11785521Sas200622 /*
11795521Sas200622  * smb_ace_len
11805521Sas200622  *
11815521Sas200622  * Returns the length of the given ACE as it appears in an
11825521Sas200622  * ACL on the wire (i.e. a flat buffer which contains the SID)
11835521Sas200622  */
11845521Sas200622 static uint16_t
11855521Sas200622 smb_ace_len(smb_ace_t *ace)
11865521Sas200622 {
11875521Sas200622 	ASSERT(ace);
11885521Sas200622 	ASSERT(ace->se_sid);
11895521Sas200622 
11905521Sas200622 	if (ace == NULL)
11915521Sas200622 		return (0);
11925521Sas200622 
11935521Sas200622 	return (SMB_ACE_HDRSIZE + sizeof (ace->se_mask) +
11945521Sas200622 	    nt_sid_length(ace->se_sid));
11955521Sas200622 }
11965521Sas200622 
11975331Samw static void
11985331Samw smb_ace_inherit(ace_t *dir_zace, ace_t *zace, int is_dir)
11995331Samw {
12005331Samw 	*zace = *dir_zace;
12015331Samw 	if (!(is_dir && ZACE_IS_PROPAGATE(dir_zace)))
12025331Samw 		zace->a_flags &= ~ACE_INHERIT_FLAGS;
12035331Samw 	zace->a_flags |= ACE_INHERITED_ACE;
12045331Samw 
12055331Samw 	/*
12065331Samw 	 * Replace creator owner/group ACEs with
12075331Samw 	 * actual owner/group ACEs.
12085331Samw 	 */
12095331Samw 	if (ZACE_IS_CREATOR_OWNER(dir_zace)) {
12105331Samw 		zace->a_who = (uid_t)-1;
12115331Samw 		zace->a_flags |= ACE_OWNER;
12125331Samw 	} else if (ZACE_IS_CREATOR_GROUP(dir_zace)) {
12135331Samw 		zace->a_who = (uid_t)-1;
12145331Samw 		zace->a_flags |= ACE_GROUP;
12155331Samw 	}
12165331Samw }
12175331Samw 
12185521Sas200622 /*
12195521Sas200622  * smb_ace_mask_g2s
12205521Sas200622  *
12215521Sas200622  * Converts generic access bits in the given mask (if any)
12225521Sas200622  * to file specific bits. Generic access masks shouldn't be
12235521Sas200622  * stored in filesystem ACEs.
12245521Sas200622  */
12255521Sas200622 static uint32_t
12265521Sas200622 smb_ace_mask_g2s(uint32_t mask)
12275521Sas200622 {
12285521Sas200622 	if (mask & GENERIC_ALL) {
12295521Sas200622 		mask &= ~(GENERIC_ALL | GENERIC_READ | GENERIC_WRITE
12305521Sas200622 		    | GENERIC_EXECUTE);
12315521Sas200622 
12325521Sas200622 		mask |= FILE_ALL_ACCESS;
12335521Sas200622 		return (mask);
12345521Sas200622 	}
12355521Sas200622 
12365521Sas200622 	if (mask & GENERIC_READ) {
12375521Sas200622 		mask &= ~GENERIC_READ;
12385521Sas200622 		mask |= FILE_GENERIC_READ;
12395521Sas200622 	}
12405521Sas200622 
12415521Sas200622 	if (mask & GENERIC_WRITE) {
12425521Sas200622 		mask &= ~GENERIC_WRITE;
12435521Sas200622 		mask |= FILE_GENERIC_WRITE;
12445521Sas200622 	}
12455521Sas200622 
12465521Sas200622 	if (mask & GENERIC_EXECUTE) {
12475521Sas200622 		mask &= ~GENERIC_EXECUTE;
12485521Sas200622 		mask |= FILE_GENERIC_EXECUTE;
12495521Sas200622 	}
12505521Sas200622 
12515521Sas200622 	return (mask);
12525521Sas200622 }
12535521Sas200622 
12545331Samw static uint16_t
12555331Samw smb_ace_flags_tozfs(uint8_t c_flags, int isdir)
12565331Samw {
12575331Samw 	uint16_t z_flags = 0;
12585331Samw 
12595331Samw 	if (c_flags & SUCCESSFUL_ACCESS_ACE_FLAG)
12605331Samw 		z_flags |= ACE_SUCCESSFUL_ACCESS_ACE_FLAG;
12615331Samw 
12625331Samw 	if (c_flags & FAILED_ACCESS_ACE_FLAG)
12635331Samw 		z_flags |= ACE_FAILED_ACCESS_ACE_FLAG;
12645331Samw 
12655331Samw 	if (c_flags & INHERITED_ACE)
12665331Samw 		z_flags |= ACE_INHERITED_ACE;
12675331Samw 
12685331Samw 	/*
12695331Samw 	 * ZFS doesn't like any inheritance flags to be set on a
12705331Samw 	 * file's ACE, only directories. Windows doesn't care.
12715331Samw 	 */
12725331Samw 	if (isdir)
12735331Samw 		z_flags |= (c_flags & ACE_INHERIT_FLAGS);
12745331Samw 
12755331Samw 	return (z_flags);
12765331Samw }
12775331Samw 
12785331Samw static uint8_t
12795331Samw smb_ace_flags_fromzfs(uint16_t z_flags)
12805331Samw {
12815331Samw 	uint8_t c_flags;
12825331Samw 
12835331Samw 	c_flags = z_flags & ACE_INHERIT_FLAGS;
12845331Samw 
12855331Samw 	if (z_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
12865331Samw 		c_flags |= SUCCESSFUL_ACCESS_ACE_FLAG;
12875331Samw 
12885331Samw 	if (z_flags & ACE_FAILED_ACCESS_ACE_FLAG)
12895331Samw 		c_flags |= FAILED_ACCESS_ACE_FLAG;
12905331Samw 
12915331Samw 	if (z_flags & ACE_INHERITED_ACE)
12925331Samw 		c_flags |= INHERITED_ACE;
12935331Samw 
12945331Samw 	return (c_flags);
12955331Samw }
12965331Samw 
12975521Sas200622 static boolean_t
12985521Sas200622 smb_ace_isvalid(smb_ace_t *ace, int which_acl)
12995331Samw {
13005521Sas200622 	uint16_t min_len;
13015521Sas200622 
13025521Sas200622 	min_len = sizeof (smb_acehdr_t);
13035521Sas200622 
13045521Sas200622 	if (ace->se_hdr.se_bsize < min_len)
13055521Sas200622 		return (B_FALSE);
13065521Sas200622 
13075521Sas200622 	if (smb_ace_is_access(ace->se_hdr.se_type) &&
13085521Sas200622 	    (which_acl != SMB_DACL_SECINFO))
13095521Sas200622 		return (B_FALSE);
13105331Samw 
13115521Sas200622 	if (smb_ace_is_audit(ace->se_hdr.se_type) &&
13125521Sas200622 	    (which_acl != SMB_SACL_SECINFO))
13135521Sas200622 		return (B_FALSE);
13145521Sas200622 
13155521Sas200622 	if (smb_ace_is_generic(ace->se_hdr.se_type)) {
13165521Sas200622 		if (nt_sid_is_valid(ace->se_sid) == 0)
13175521Sas200622 			return (B_FALSE);
13185521Sas200622 
13195521Sas200622 		min_len += sizeof (ace->se_mask);
13205521Sas200622 		min_len += nt_sid_length(ace->se_sid);
13215521Sas200622 
13225521Sas200622 		if (ace->se_hdr.se_bsize < min_len)
13235521Sas200622 			return (B_FALSE);
13245331Samw 	}
13255331Samw 
13265521Sas200622 	/*
13275521Sas200622 	 * object-specific ACE validation will be added later.
13285521Sas200622 	 */
13295521Sas200622 	return (B_TRUE);
13305331Samw }
1331