2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Chris D. Faulhaber
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
38 #include "acl_support.h"
41 * These routines from sys/kern/subr_acl_nfs4.c are used by both kernel
44 void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp);
45 void acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int file_owner_id,
49 _nfs4_acl_strip_np(const acl_t aclp, int canonical_six)
54 newacl = acl_init(ACL_MAX_ENTRIES);
60 _acl_brand_as(newacl, ACL_BRAND_NFS4);
62 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl));
63 acl_nfs4_trivial_from_mode_libc(&(newacl->ats_acl), mode, canonical_six);
69 _posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask)
71 acl_t acl_new, acl_old;
72 acl_entry_t entry, entry_new;
74 int entry_id, have_mask_entry;
76 assert(_acl_brand(aclp) == ACL_BRAND_POSIX);
78 acl_old = acl_dup(aclp);
82 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX);
85 acl_new = acl_init(ACL_MAX_ENTRIES);
86 if (acl_new == NULL) {
90 tag = ACL_UNDEFINED_TAG;
92 /* only save the default user/group/other entries */
93 entry_id = ACL_FIRST_ENTRY;
94 while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
95 entry_id = ACL_NEXT_ENTRY;
97 assert(_entry_brand(entry) == ACL_BRAND_POSIX);
99 if (acl_get_tag_type(entry, &tag) == -1)
106 if (acl_create_entry(&acl_new, &entry_new) == -1)
108 if (acl_copy_entry(entry_new, entry) == -1)
110 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX);
120 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX);
122 if (have_mask_entry && recalculate_mask) {
123 if (acl_calc_mask(&acl_new) == -1)
137 acl_strip_np(const acl_t aclp, int recalculate_mask)
139 switch (_acl_brand(aclp)) {
141 return (_nfs4_acl_strip_np(aclp, 0));
143 case ACL_BRAND_POSIX:
144 return (_posix1e_acl_strip_np(aclp, recalculate_mask));
153 * Return 1, if ACL is trivial, 0 otherwise.
155 * ACL is trivial, iff its meaning could be fully expressed using just file
156 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right
157 * of the mode bits in "ls -l" output ;-)
160 acl_is_trivial_np(const acl_t aclp, int *trivialp)
165 if (aclp == NULL || trivialp == NULL) {
170 switch (_acl_brand(aclp)) {
171 case ACL_BRAND_POSIX:
172 if (aclp->ats_acl.acl_cnt == 3)
181 * If the ACL has more than canonical six entries,
182 * it's non trivial by definition.
184 if (aclp->ats_acl.acl_cnt > 6) {
190 * Calculate trivial ACL - using acl_strip_np(3) - and compare
193 tmpacl = _nfs4_acl_strip_np(aclp, 0);
197 differs = _acl_differs(aclp, tmpacl);
206 * Try again with an old-style, "canonical six" trivial ACL.
208 tmpacl = _nfs4_acl_strip_np(aclp, 1);
212 differs = _acl_differs(aclp, tmpacl);