29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * CDDL HEADER START
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * The contents of this file are subject to the terms of the
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Common Development and Distribution License (the "License").
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * You may not use this file except in compliance with the License.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * See the License for the specific language governing permissions
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * and limitations under the License.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * When distributing Covered Code, include this CDDL HEADER in each
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * If applicable, add the following below this CDDL HEADER, with the
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * fields enclosed by brackets "[]" replaced with your own identifying
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * information: Portions Copyright [yyyy] [name of copyright owner]
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * CDDL HEADER END
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wright * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Use is subject to license terms.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ACE groups within a DACL
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * This is from lower to higher ACE order priority
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wrightstatic idmap_stat smb_fsacl_getsids(smb_idmap_batch_t *, acl_t *);
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wrightstatic boolean_t smb_ace_wellknown_update(const char *, ace_t *);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmb_acl_alloc(uint8_t revision, uint16_t bsize, uint16_t acecnt)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright size = sizeof (smb_acl_t) + (acecnt * sizeof (smb_ace_t));
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright list_create(&acl->sl_sorted, sizeof (smb_ace_t),
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright while ((ace = list_head(&acl->sl_sorted)) != NULL)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_acl_len
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Returns the size of given ACL in bytes. Note that this
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * is not an in-memory size, it's the ACL's size as it would
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * appear on the wire
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * we are rejecting ACLs with object-specific ACEs for now
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_acl_sort
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Sorts the given ACL in place if it needs to be sorted.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * The following is an excerpt from MSDN website.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Order of ACEs in a DACL
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * For Windows NT versions 4.0 and earlier, the preferred order of ACEs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * is simple: In a DACL, all access-denied ACEs should precede any
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * access-allowed ACEs.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * For Windows 2000 or later, the proper order of ACEs is more complicated
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * because of the introduction of object-specific ACEs and automatic
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * inheritance.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * The following describes the preferred order:
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * To ensure that noninherited ACEs have precedence over inherited ACEs,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * place all noninherited ACEs in a group before any inherited ACEs. This
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ordering ensures, for example, that a noninherited access-denied ACE
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * is enforced regardless of any inherited ACE that allows access.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Within the groups of noninherited ACEs and inherited ACEs, order ACEs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * according to ACE type, as the following shows:
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * . Access-denied ACEs that apply to the object itself
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * . Access-denied ACEs that apply to a subobject of the
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * object, such as a property set or property
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * . Access-allowed ACEs that apply to the object itself
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * . Access-allowed ACEs that apply to a subobject of the object
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * So, here is the desired ACE order
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * deny-direct, allow-direct, deny-inherited, allow-inherited
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Of course, not all ACE types are required in an ACL.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ACL with no entry is a valid ACL and it means
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * no access for anybody.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright for (i = 0, ace = acl->sl_aces; i < acl->sl_acecnt; ++i, ace++) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * This is the lowest priority group so we put
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * evertything unknown here.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* Add the ACE to the selected group */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * start with highest priority ACE group and append
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * the ACEs to the ACL.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright for (i = SMB_AG_NUM - 1; i >= SMB_AG_START; i--) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_acl_from_zfs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Converts given ZFS ACL to a Windows ACL.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * A pointer to allocated memory for the Windows ACL will be
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * returned upon successful conversion.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright idm_stat = smb_idmap_batch_create(&sib, zacl->acl_cnt,
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wright if (smb_fsacl_getsids(&sib, zacl) != IDMAP_SUCCESS) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright acl = smb_acl_alloc(ACL_REVISION, SMB_ACL_HDRSIZE, zacl->acl_cnt);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright ace->se_hdr.se_flags = smb_ace_flags_fromzfs(zace->a_flags);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_acl_to_zfs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Converts given Windows ACL to a ZFS ACL.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * fs_acl will contain a pointer to the created ZFS ACL.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * The allocated memory should be freed by calling
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_fsacl_free().
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Since the output parameter, fs_acl, is allocated in this
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * function, the caller has to make sure *fs_acl is NULL which
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * means it's not pointing to any memory.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wrightsmb_acl_to_zfs(smb_acl_t *acl, uint32_t flags, int which_acl, acl_t **fs_acl)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright idm_stat = smb_idmap_batch_create(&sib, acl->sl_acecnt,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright for (i = 0; i < acl->sl_acecnt; i++, zace++, ace++, sim++) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright zace->a_type = ace->se_hdr.se_type & ACE_ALL_TYPES;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright zace->a_access_mask = smb_ace_mask_g2s(ace->se_mask);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright zace->a_flags = smb_ace_flags_tozfs(ace->se_hdr.se_flags);
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright idm_stat = smb_idmap_batch_getid(sib.sib_idmaph, sim,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Set the ACEs group flag based on the type of ID returned.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright for (i = 0; i < acl->sl_acecnt; i++, zace++, ace++, sim++) {
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wrightsmb_ace_wellknown_update(const char *sid, ace_t *zace)
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wright for (i = 0; i < (sizeof (map) / sizeof (map[0])); ++i) {
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_fsacl_getsids
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Batch all the uid/gid in given ZFS ACL to get their corresponding SIDs.
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wrightsmb_fsacl_getsids(smb_idmap_batch_t *sib, acl_t *zacl)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright for (i = 0, zace = zacl->acl_aclp; i < zacl->acl_cnt;
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* owning group */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* regular group */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright /* user entry */
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright idm_stat = smb_idmap_batch_getsid(sib->sib_idmaph, sim,
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_fsacl_null_empty
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * NULL DACL means everyone full-access
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Empty DACL means everyone full-deny
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ZFS ACL must have at least one entry so smb server has
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * to simulate the aforementioned expected behavior by adding
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * an entry in case the requested DACL is null or empty. Adding
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * a everyone full-deny entry has proved to be problematic in
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * tests since a deny entry takes precedence over allow entries.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * So, instead of adding a everyone full-deny, an owner ACE with
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * owner implicit permissions will be set.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright zace->a_access_mask = ACE_READ_ACL | ACE_WRITE_ACL |
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * FS ACL (acl_t) Functions
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright if ((acl->acl_aclp = malloc(acl->acl_entry_size * acenum)) == NULL)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ACE Functions
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_ace_len
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Returns the length of the given ACE as it appears in an
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * ACL on the wire (i.e. a flat buffer which contains the SID)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright return (SMB_ACE_HDRSIZE + sizeof (ace->se_mask) +
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_ace_mask_g2s
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * Converts generic access bits in the given mask (if any)
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * to file specific bits. Generic access masks shouldn't be
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * stored in filesystem ACEs.
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright mask &= ~(GENERIC_ALL | GENERIC_READ | GENERIC_WRITE
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * smb_ace_flags_tozfs
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * This function maps the flags which have different values
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * in Windows and Solaris. The ones with the same value are
29bd28862cfb8abbd3a0f0a4b17e08bbc3652836Alan Wright * transferred untouched.