1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001 Chris D. Faulhaber
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if 0
31 __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_strip.c 344294 2019-02-19 19:15:15Z sef $");
32 #else
33 __RCSID("$NetBSD: acl_strip.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
34 #endif
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <sys/acl.h>
40 #include <sys/stat.h>
41
42 #include "acl_support.h"
43
44 static acl_t
_nfs4_acl_strip_np(const acl_t aclp,int canonical_six)45 _nfs4_acl_strip_np(const acl_t aclp, int canonical_six)
46 {
47 acl_t newacl;
48 mode_t mode = 0;
49
50 newacl = acl_init(ACL_MAX_ENTRIES);
51 if (newacl == NULL) {
52 errno = ENOMEM;
53 return (NULL);
54 }
55
56 _acl_brand_as(newacl, ACL_BRAND_NFS4);
57
58 __acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl));
59 __acl_nfs4_trivial_from_mode_libc(&(newacl->ats_acl), mode, canonical_six);
60
61 return (newacl);
62 }
63
64 static acl_t
_posix1e_acl_strip_np(const acl_t aclp,int recalculate_mask)65 _posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask)
66 {
67 acl_t acl_new, acl_old;
68 acl_entry_t entry, entry_new;
69 acl_tag_t tag;
70 int entry_id, have_mask_entry;
71
72 assert(_acl_brand(aclp) == ACL_BRAND_POSIX);
73
74 acl_old = acl_dup(aclp);
75 if (acl_old == NULL)
76 return (NULL);
77
78 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX);
79
80 have_mask_entry = 0;
81 acl_new = acl_init(ACL_MAX_ENTRIES);
82 if (acl_new == NULL) {
83 acl_free(acl_old);
84 return (NULL);
85 }
86 tag = ACL_UNDEFINED_TAG;
87
88 /* only save the default user/group/other entries */
89 entry_id = ACL_FIRST_ENTRY;
90 while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
91 entry_id = ACL_NEXT_ENTRY;
92
93 assert(_entry_brand(entry) == ACL_BRAND_POSIX);
94
95 if (acl_get_tag_type(entry, &tag) == -1)
96 goto fail;
97
98 switch(tag) {
99 case ACL_USER_OBJ:
100 case ACL_GROUP_OBJ:
101 case ACL_OTHER:
102 if (acl_create_entry(&acl_new, &entry_new) == -1)
103 goto fail;
104 if (acl_copy_entry(entry_new, entry) == -1)
105 goto fail;
106 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX);
107 break;
108 case ACL_MASK:
109 have_mask_entry = 1;
110 break;
111 default:
112 break;
113 }
114 }
115
116 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX);
117
118 if (have_mask_entry && recalculate_mask) {
119 if (acl_calc_mask(&acl_new) == -1)
120 goto fail;
121 }
122
123 return (acl_new);
124
125 fail:
126 acl_free(acl_new);
127 acl_free(acl_old);
128
129 return (NULL);
130 }
131
132 acl_t
acl_strip_np(const acl_t aclp,int recalculate_mask)133 acl_strip_np(const acl_t aclp, int recalculate_mask)
134 {
135 switch (_acl_brand(aclp)) {
136 case ACL_BRAND_NFS4:
137 return (_nfs4_acl_strip_np(aclp, 0));
138
139 case ACL_BRAND_POSIX:
140 return (_posix1e_acl_strip_np(aclp, recalculate_mask));
141
142 default:
143 errno = EINVAL;
144 return (NULL);
145 }
146 }
147
148 /*
149 * Return 1, if ACL is trivial, 0 otherwise.
150 *
151 * ACL is trivial, iff its meaning could be fully expressed using just file
152 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right
153 * of the mode bits in "ls -l" output ;-)
154 */
155 int
acl_is_trivial_np(const acl_t aclp,int * trivialp)156 acl_is_trivial_np(const acl_t aclp, int *trivialp)
157 {
158 acl_t tmpacl;
159 int differs;
160
161 if (aclp == NULL || trivialp == NULL) {
162 errno = EINVAL;
163 return (-1);
164 }
165
166 switch (_acl_brand(aclp)) {
167 case ACL_BRAND_POSIX:
168 if (aclp->ats_acl.acl_cnt == 3)
169 *trivialp = 1;
170 else
171 *trivialp = 0;
172
173 return (0);
174
175 case ACL_BRAND_NFS4:
176 /*
177 * If the ACL has more than canonical six entries,
178 * it's non trivial by definition.
179 */
180 if (aclp->ats_acl.acl_cnt > 6) {
181 *trivialp = 0;
182 return (0);
183 }
184
185 /*
186 * Calculate trivial ACL - using acl_strip_np(3) - and compare
187 * with the original.
188 */
189 tmpacl = _nfs4_acl_strip_np(aclp, 0);
190 if (tmpacl == NULL)
191 return (-1);
192
193 differs = _acl_differs(aclp, tmpacl);
194 acl_free(tmpacl);
195
196 if (differs == 0) {
197 *trivialp = 1;
198 return (0);
199 }
200
201 /*
202 * Try again with an old-style, "canonical six" trivial ACL.
203 */
204 tmpacl = _nfs4_acl_strip_np(aclp, 1);
205 if (tmpacl == NULL)
206 return (-1);
207
208 differs = _acl_differs(aclp, tmpacl);
209 acl_free(tmpacl);
210
211 if (differs)
212 *trivialp = 0;
213 else
214 *trivialp = 1;
215
216 return (0);
217
218 default:
219 errno = EINVAL;
220 return (-1);
221 }
222 }
223