merge.c revision 1.1 1 1.1 christos /* $NetBSD: merge.c,v 1.1 2020/05/16 18:31:45 christos Exp $ */
2 1.1 christos
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/bin/setfacl/merge.c 333065 2018-04-27 15:25:24Z emaste $");
32 1.1 christos #else
33 1.1 christos __RCSID("$NetBSD: merge.c,v 1.1 2020/05/16 18:31:45 christos Exp $");
34 1.1 christos #endif
35 1.1 christos
36 1.1 christos #include <sys/types.h>
37 1.1 christos #include <sys/acl.h>
38 1.1 christos #include <sys/stat.h>
39 1.1 christos
40 1.1 christos #include <err.h>
41 1.1 christos #include <stdio.h>
42 1.1 christos
43 1.1 christos #include "setfacl.h"
44 1.1 christos
45 1.1 christos static int merge_user_group(acl_entry_t *entry, acl_entry_t *entry_new,
46 1.1 christos int acl_brand);
47 1.1 christos
48 1.1 christos static int
49 1.1 christos merge_user_group(acl_entry_t *entry, acl_entry_t *entry_new, int acl_brand)
50 1.1 christos {
51 1.1 christos acl_permset_t permset;
52 1.1 christos acl_entry_type_t entry_type;
53 1.1 christos acl_flagset_t flagset;
54 1.1 christos int have_entry;
55 1.1 christos uid_t *id, *id_new;
56 1.1 christos
57 1.1 christos have_entry = 0;
58 1.1 christos
59 1.1 christos id = acl_get_qualifier(*entry);
60 1.1 christos if (id == NULL)
61 1.1 christos err(1, "acl_get_qualifier() failed");
62 1.1 christos id_new = acl_get_qualifier(*entry_new);
63 1.1 christos if (id_new == NULL)
64 1.1 christos err(1, "acl_get_qualifier() failed");
65 1.1 christos if (*id == *id_new) {
66 1.1 christos /* any other matches */
67 1.1 christos if (acl_get_permset(*entry, &permset) == -1)
68 1.1 christos err(1, "acl_get_permset() failed");
69 1.1 christos if (acl_set_permset(*entry_new, permset) == -1)
70 1.1 christos err(1, "acl_set_permset() failed");
71 1.1 christos
72 1.1 christos if (acl_brand == ACL_BRAND_NFS4) {
73 1.1 christos if (acl_get_entry_type_np(*entry, &entry_type))
74 1.1 christos err(1, "acl_get_entry_type_np() failed");
75 1.1 christos if (acl_set_entry_type_np(*entry_new, entry_type))
76 1.1 christos err(1, "acl_set_entry_type_np() failed");
77 1.1 christos if (acl_get_flagset_np(*entry, &flagset))
78 1.1 christos err(1, "acl_get_flagset_np() failed");
79 1.1 christos if (acl_set_flagset_np(*entry_new, flagset))
80 1.1 christos err(1, "acl_set_flagset_np() failed");
81 1.1 christos }
82 1.1 christos
83 1.1 christos have_entry = 1;
84 1.1 christos }
85 1.1 christos acl_free(id);
86 1.1 christos acl_free(id_new);
87 1.1 christos
88 1.1 christos return (have_entry);
89 1.1 christos }
90 1.1 christos
91 1.1 christos /*
92 1.1 christos * merge an ACL into existing file's ACL
93 1.1 christos */
94 1.1 christos int
95 1.1 christos merge_acl(acl_t acl, acl_t *prev_acl, const char *filename)
96 1.1 christos {
97 1.1 christos acl_entry_t entry, entry_new;
98 1.1 christos acl_permset_t permset;
99 1.1 christos acl_t acl_new;
100 1.1 christos acl_tag_t tag, tag_new;
101 1.1 christos acl_entry_type_t entry_type, entry_type_new;
102 1.1 christos acl_flagset_t flagset;
103 1.1 christos int entry_id, entry_id_new, have_entry, had_entry, entry_number = 0;
104 1.1 christos int acl_brand, prev_acl_brand;
105 1.1 christos
106 1.1 christos acl_get_brand_np(acl, &acl_brand);
107 1.1 christos acl_get_brand_np(*prev_acl, &prev_acl_brand);
108 1.1 christos
109 1.1 christos if (branding_mismatch(acl_brand, prev_acl_brand)) {
110 1.1 christos warnx("%s: branding mismatch; existing ACL is %s, "
111 1.1 christos "entry to be merged is %s", filename,
112 1.1 christos brand_name(prev_acl_brand), brand_name(acl_brand));
113 1.1 christos return (-1);
114 1.1 christos }
115 1.1 christos
116 1.1 christos acl_new = acl_dup(*prev_acl);
117 1.1 christos if (acl_new == NULL)
118 1.1 christos err(1, "%s: acl_dup() failed", filename);
119 1.1 christos
120 1.1 christos entry_id = ACL_FIRST_ENTRY;
121 1.1 christos
122 1.1 christos while (acl_get_entry(acl, entry_id, &entry) == 1) {
123 1.1 christos entry_id = ACL_NEXT_ENTRY;
124 1.1 christos have_entry = 0;
125 1.1 christos had_entry = 0;
126 1.1 christos
127 1.1 christos /* keep track of existing ACL_MASK entries */
128 1.1 christos if (acl_get_tag_type(entry, &tag) == -1)
129 1.1 christos err(1, "%s: acl_get_tag_type() failed - "
130 1.1 christos "invalid ACL entry", filename);
131 1.1 christos if (tag == ACL_MASK)
132 1.1 christos have_mask = true;
133 1.1 christos
134 1.1 christos /* check against the existing ACL entries */
135 1.1 christos entry_id_new = ACL_FIRST_ENTRY;
136 1.1 christos while (acl_get_entry(acl_new, entry_id_new, &entry_new) == 1) {
137 1.1 christos entry_id_new = ACL_NEXT_ENTRY;
138 1.1 christos
139 1.1 christos if (acl_get_tag_type(entry, &tag) == -1)
140 1.1 christos err(1, "%s: acl_get_tag_type() failed",
141 1.1 christos filename);
142 1.1 christos if (acl_get_tag_type(entry_new, &tag_new) == -1)
143 1.1 christos err(1, "%s: acl_get_tag_type() failed",
144 1.1 christos filename);
145 1.1 christos if (tag != tag_new)
146 1.1 christos continue;
147 1.1 christos
148 1.1 christos /*
149 1.1 christos * For NFSv4, in addition to "tag" and "id" we also
150 1.1 christos * compare "entry_type".
151 1.1 christos */
152 1.1 christos if (acl_brand == ACL_BRAND_NFS4) {
153 1.1 christos if (acl_get_entry_type_np(entry, &entry_type))
154 1.1 christos err(1, "%s: acl_get_entry_type_np() "
155 1.1 christos "failed", filename);
156 1.1 christos if (acl_get_entry_type_np(entry_new, &entry_type_new))
157 1.1 christos err(1, "%s: acl_get_entry_type_np() "
158 1.1 christos "failed", filename);
159 1.1 christos if (entry_type != entry_type_new)
160 1.1 christos continue;
161 1.1 christos }
162 1.1 christos
163 1.1 christos switch(tag) {
164 1.1 christos case ACL_USER:
165 1.1 christos case ACL_GROUP:
166 1.1 christos have_entry = merge_user_group(&entry,
167 1.1 christos &entry_new, acl_brand);
168 1.1 christos if (have_entry == 0)
169 1.1 christos break;
170 1.1 christos /* FALLTHROUGH */
171 1.1 christos case ACL_USER_OBJ:
172 1.1 christos case ACL_GROUP_OBJ:
173 1.1 christos case ACL_OTHER:
174 1.1 christos case ACL_MASK:
175 1.1 christos case ACL_EVERYONE:
176 1.1 christos if (acl_get_permset(entry, &permset) == -1)
177 1.1 christos err(1, "%s: acl_get_permset() failed",
178 1.1 christos filename);
179 1.1 christos if (acl_set_permset(entry_new, permset) == -1)
180 1.1 christos err(1, "%s: acl_set_permset() failed",
181 1.1 christos filename);
182 1.1 christos
183 1.1 christos if (acl_brand == ACL_BRAND_NFS4) {
184 1.1 christos if (acl_get_entry_type_np(entry, &entry_type))
185 1.1 christos err(1, "%s: acl_get_entry_type_np() failed",
186 1.1 christos filename);
187 1.1 christos if (acl_set_entry_type_np(entry_new, entry_type))
188 1.1 christos err(1, "%s: acl_set_entry_type_np() failed",
189 1.1 christos filename);
190 1.1 christos if (acl_get_flagset_np(entry, &flagset))
191 1.1 christos err(1, "%s: acl_get_flagset_np() failed",
192 1.1 christos filename);
193 1.1 christos if (acl_set_flagset_np(entry_new, flagset))
194 1.1 christos err(1, "%s: acl_set_flagset_np() failed",
195 1.1 christos filename);
196 1.1 christos }
197 1.1 christos had_entry = have_entry = 1;
198 1.1 christos break;
199 1.1 christos default:
200 1.1 christos /* should never be here */
201 1.1 christos errx(1, "%s: invalid tag type: %i", filename, tag);
202 1.1 christos break;
203 1.1 christos }
204 1.1 christos }
205 1.1 christos
206 1.1 christos /* if this entry has not been found, it must be new */
207 1.1 christos if (had_entry == 0) {
208 1.1 christos
209 1.1 christos /*
210 1.1 christos * NFSv4 ACL entries must be prepended to the ACL.
211 1.1 christos * Appending them at the end makes no sense, since
212 1.1 christos * in most cases they wouldn't even get evaluated.
213 1.1 christos */
214 1.1 christos if (acl_brand == ACL_BRAND_NFS4) {
215 1.1 christos if (acl_create_entry_np(&acl_new, &entry_new, entry_number) == -1) {
216 1.1 christos warn("%s: acl_create_entry_np() failed", filename);
217 1.1 christos acl_free(acl_new);
218 1.1 christos return (-1);
219 1.1 christos }
220 1.1 christos /*
221 1.1 christos * Without this increment, adding several
222 1.1 christos * entries at once, for example
223 1.1 christos * "setfacl -m user:1:r:allow,user:2:r:allow",
224 1.1 christos * would make them appear in reverse order.
225 1.1 christos */
226 1.1 christos entry_number++;
227 1.1 christos } else {
228 1.1 christos if (acl_create_entry(&acl_new, &entry_new) == -1) {
229 1.1 christos warn("%s: acl_create_entry() failed", filename);
230 1.1 christos acl_free(acl_new);
231 1.1 christos return (-1);
232 1.1 christos }
233 1.1 christos }
234 1.1 christos if (acl_copy_entry(entry_new, entry) == -1)
235 1.1 christos err(1, "%s: acl_copy_entry() failed", filename);
236 1.1 christos }
237 1.1 christos }
238 1.1 christos
239 1.1 christos acl_free(*prev_acl);
240 1.1 christos *prev_acl = acl_new;
241 1.1 christos
242 1.1 christos return (0);
243 1.1 christos }
244 1.1 christos
245 1.1 christos int
246 1.1 christos add_acl(acl_t acl, uint entry_number, acl_t *prev_acl, const char *filename)
247 1.1 christos {
248 1.1 christos acl_entry_t entry, entry_new;
249 1.1 christos acl_t acl_new;
250 1.1 christos int entry_id, acl_brand, prev_acl_brand;
251 1.1 christos
252 1.1 christos acl_get_brand_np(acl, &acl_brand);
253 1.1 christos acl_get_brand_np(*prev_acl, &prev_acl_brand);
254 1.1 christos
255 1.1 christos if (prev_acl_brand != ACL_BRAND_NFS4) {
256 1.1 christos warnx("%s: the '-a' option is only applicable to NFSv4 ACLs",
257 1.1 christos filename);
258 1.1 christos return (-1);
259 1.1 christos }
260 1.1 christos
261 1.1 christos if (branding_mismatch(acl_brand, ACL_BRAND_NFS4)) {
262 1.1 christos warnx("%s: branding mismatch; existing ACL is NFSv4, "
263 1.1 christos "entry to be added is %s", filename,
264 1.1 christos brand_name(acl_brand));
265 1.1 christos return (-1);
266 1.1 christos }
267 1.1 christos
268 1.1 christos acl_new = acl_dup(*prev_acl);
269 1.1 christos if (acl_new == NULL)
270 1.1 christos err(1, "%s: acl_dup() failed", filename);
271 1.1 christos
272 1.1 christos entry_id = ACL_FIRST_ENTRY;
273 1.1 christos
274 1.1 christos while (acl_get_entry(acl, entry_id, &entry) == 1) {
275 1.1 christos entry_id = ACL_NEXT_ENTRY;
276 1.1 christos
277 1.1 christos if (acl_create_entry_np(&acl_new, &entry_new, entry_number) == -1) {
278 1.1 christos warn("%s: acl_create_entry_np() failed", filename);
279 1.1 christos acl_free(acl_new);
280 1.1 christos return (-1);
281 1.1 christos }
282 1.1 christos
283 1.1 christos /*
284 1.1 christos * Without this increment, adding several
285 1.1 christos * entries at once, for example
286 1.1 christos * "setfacl -m user:1:r:allow,user:2:r:allow",
287 1.1 christos * would make them appear in reverse order.
288 1.1 christos */
289 1.1 christos entry_number++;
290 1.1 christos
291 1.1 christos if (acl_copy_entry(entry_new, entry) == -1)
292 1.1 christos err(1, "%s: acl_copy_entry() failed", filename);
293 1.1 christos }
294 1.1 christos
295 1.1 christos acl_free(*prev_acl);
296 1.1 christos *prev_acl = acl_new;
297 1.1 christos
298 1.1 christos return (0);
299 1.1 christos }
300