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