xref: /netbsd-src/bin/setfacl/merge.c (revision 9aa2a9c323eb12a08584c70d6ea91d316703d3fe)
1*9aa2a9c3Schristos /*	$NetBSD: merge.c,v 1.1 2020/05/16 18:31:45 christos Exp $	*/
2*9aa2a9c3Schristos 
3*9aa2a9c3Schristos /*-
4*9aa2a9c3Schristos  * Copyright (c) 2001 Chris D. Faulhaber
5*9aa2a9c3Schristos  * All rights reserved.
6*9aa2a9c3Schristos  *
7*9aa2a9c3Schristos  * Redistribution and use in source and binary forms, with or without
8*9aa2a9c3Schristos  * modification, are permitted provided that the following conditions
9*9aa2a9c3Schristos  * are met:
10*9aa2a9c3Schristos  * 1. Redistributions of source code must retain the above copyright
11*9aa2a9c3Schristos  *    notice, this list of conditions and the following disclaimer.
12*9aa2a9c3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*9aa2a9c3Schristos  *    notice, this list of conditions and the following disclaimer in the
14*9aa2a9c3Schristos  *    documentation and/or other materials provided with the distribution.
15*9aa2a9c3Schristos  *
16*9aa2a9c3Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*9aa2a9c3Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*9aa2a9c3Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*9aa2a9c3Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*9aa2a9c3Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*9aa2a9c3Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*9aa2a9c3Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*9aa2a9c3Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*9aa2a9c3Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*9aa2a9c3Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*9aa2a9c3Schristos  * SUCH DAMAGE.
27*9aa2a9c3Schristos  */
28*9aa2a9c3Schristos 
29*9aa2a9c3Schristos #include <sys/cdefs.h>
30*9aa2a9c3Schristos #if 0
31*9aa2a9c3Schristos __FBSDID("$FreeBSD: head/bin/setfacl/merge.c 333065 2018-04-27 15:25:24Z emaste $");
32*9aa2a9c3Schristos #else
33*9aa2a9c3Schristos __RCSID("$NetBSD: merge.c,v 1.1 2020/05/16 18:31:45 christos Exp $");
34*9aa2a9c3Schristos #endif
35*9aa2a9c3Schristos 
36*9aa2a9c3Schristos #include <sys/types.h>
37*9aa2a9c3Schristos #include <sys/acl.h>
38*9aa2a9c3Schristos #include <sys/stat.h>
39*9aa2a9c3Schristos 
40*9aa2a9c3Schristos #include <err.h>
41*9aa2a9c3Schristos #include <stdio.h>
42*9aa2a9c3Schristos 
43*9aa2a9c3Schristos #include "setfacl.h"
44*9aa2a9c3Schristos 
45*9aa2a9c3Schristos static int merge_user_group(acl_entry_t *entry, acl_entry_t *entry_new,
46*9aa2a9c3Schristos     int acl_brand);
47*9aa2a9c3Schristos 
48*9aa2a9c3Schristos static int
merge_user_group(acl_entry_t * entry,acl_entry_t * entry_new,int acl_brand)49*9aa2a9c3Schristos merge_user_group(acl_entry_t *entry, acl_entry_t *entry_new, int acl_brand)
50*9aa2a9c3Schristos {
51*9aa2a9c3Schristos 	acl_permset_t permset;
52*9aa2a9c3Schristos 	acl_entry_type_t entry_type;
53*9aa2a9c3Schristos 	acl_flagset_t flagset;
54*9aa2a9c3Schristos 	int have_entry;
55*9aa2a9c3Schristos 	uid_t *id, *id_new;
56*9aa2a9c3Schristos 
57*9aa2a9c3Schristos 	have_entry = 0;
58*9aa2a9c3Schristos 
59*9aa2a9c3Schristos 	id = acl_get_qualifier(*entry);
60*9aa2a9c3Schristos 	if (id == NULL)
61*9aa2a9c3Schristos 		err(1, "acl_get_qualifier() failed");
62*9aa2a9c3Schristos 	id_new = acl_get_qualifier(*entry_new);
63*9aa2a9c3Schristos 	if (id_new == NULL)
64*9aa2a9c3Schristos 		err(1, "acl_get_qualifier() failed");
65*9aa2a9c3Schristos 	if (*id == *id_new) {
66*9aa2a9c3Schristos 		/* any other matches */
67*9aa2a9c3Schristos 		if (acl_get_permset(*entry, &permset) == -1)
68*9aa2a9c3Schristos 			err(1, "acl_get_permset() failed");
69*9aa2a9c3Schristos 		if (acl_set_permset(*entry_new, permset) == -1)
70*9aa2a9c3Schristos 			err(1, "acl_set_permset() failed");
71*9aa2a9c3Schristos 
72*9aa2a9c3Schristos 		if (acl_brand == ACL_BRAND_NFS4) {
73*9aa2a9c3Schristos 			if (acl_get_entry_type_np(*entry, &entry_type))
74*9aa2a9c3Schristos 				err(1, "acl_get_entry_type_np() failed");
75*9aa2a9c3Schristos 			if (acl_set_entry_type_np(*entry_new, entry_type))
76*9aa2a9c3Schristos 				err(1, "acl_set_entry_type_np() failed");
77*9aa2a9c3Schristos 			if (acl_get_flagset_np(*entry, &flagset))
78*9aa2a9c3Schristos 				err(1, "acl_get_flagset_np() failed");
79*9aa2a9c3Schristos 			if (acl_set_flagset_np(*entry_new, flagset))
80*9aa2a9c3Schristos 				err(1, "acl_set_flagset_np() failed");
81*9aa2a9c3Schristos 		}
82*9aa2a9c3Schristos 
83*9aa2a9c3Schristos 		have_entry = 1;
84*9aa2a9c3Schristos 	}
85*9aa2a9c3Schristos 	acl_free(id);
86*9aa2a9c3Schristos 	acl_free(id_new);
87*9aa2a9c3Schristos 
88*9aa2a9c3Schristos 	return (have_entry);
89*9aa2a9c3Schristos }
90*9aa2a9c3Schristos 
91*9aa2a9c3Schristos /*
92*9aa2a9c3Schristos  * merge an ACL into existing file's ACL
93*9aa2a9c3Schristos  */
94*9aa2a9c3Schristos int
merge_acl(acl_t acl,acl_t * prev_acl,const char * filename)95*9aa2a9c3Schristos merge_acl(acl_t acl, acl_t *prev_acl, const char *filename)
96*9aa2a9c3Schristos {
97*9aa2a9c3Schristos 	acl_entry_t entry, entry_new;
98*9aa2a9c3Schristos 	acl_permset_t permset;
99*9aa2a9c3Schristos 	acl_t acl_new;
100*9aa2a9c3Schristos 	acl_tag_t tag, tag_new;
101*9aa2a9c3Schristos 	acl_entry_type_t entry_type, entry_type_new;
102*9aa2a9c3Schristos 	acl_flagset_t flagset;
103*9aa2a9c3Schristos 	int entry_id, entry_id_new, have_entry, had_entry, entry_number = 0;
104*9aa2a9c3Schristos 	int acl_brand, prev_acl_brand;
105*9aa2a9c3Schristos 
106*9aa2a9c3Schristos 	acl_get_brand_np(acl, &acl_brand);
107*9aa2a9c3Schristos 	acl_get_brand_np(*prev_acl, &prev_acl_brand);
108*9aa2a9c3Schristos 
109*9aa2a9c3Schristos 	if (branding_mismatch(acl_brand, prev_acl_brand)) {
110*9aa2a9c3Schristos 		warnx("%s: branding mismatch; existing ACL is %s, "
111*9aa2a9c3Schristos 		    "entry to be merged is %s", filename,
112*9aa2a9c3Schristos 		    brand_name(prev_acl_brand), brand_name(acl_brand));
113*9aa2a9c3Schristos 		return (-1);
114*9aa2a9c3Schristos 	}
115*9aa2a9c3Schristos 
116*9aa2a9c3Schristos 	acl_new = acl_dup(*prev_acl);
117*9aa2a9c3Schristos 	if (acl_new == NULL)
118*9aa2a9c3Schristos 		err(1, "%s: acl_dup() failed", filename);
119*9aa2a9c3Schristos 
120*9aa2a9c3Schristos 	entry_id = ACL_FIRST_ENTRY;
121*9aa2a9c3Schristos 
122*9aa2a9c3Schristos 	while (acl_get_entry(acl, entry_id, &entry) == 1) {
123*9aa2a9c3Schristos 		entry_id = ACL_NEXT_ENTRY;
124*9aa2a9c3Schristos 		have_entry = 0;
125*9aa2a9c3Schristos 		had_entry = 0;
126*9aa2a9c3Schristos 
127*9aa2a9c3Schristos 		/* keep track of existing ACL_MASK entries */
128*9aa2a9c3Schristos 		if (acl_get_tag_type(entry, &tag) == -1)
129*9aa2a9c3Schristos 			err(1, "%s: acl_get_tag_type() failed - "
130*9aa2a9c3Schristos 			    "invalid ACL entry", filename);
131*9aa2a9c3Schristos 		if (tag == ACL_MASK)
132*9aa2a9c3Schristos 			have_mask = true;
133*9aa2a9c3Schristos 
134*9aa2a9c3Schristos 		/* check against the existing ACL entries */
135*9aa2a9c3Schristos 		entry_id_new = ACL_FIRST_ENTRY;
136*9aa2a9c3Schristos 		while (acl_get_entry(acl_new, entry_id_new, &entry_new) == 1) {
137*9aa2a9c3Schristos 			entry_id_new = ACL_NEXT_ENTRY;
138*9aa2a9c3Schristos 
139*9aa2a9c3Schristos 			if (acl_get_tag_type(entry, &tag) == -1)
140*9aa2a9c3Schristos 				err(1, "%s: acl_get_tag_type() failed",
141*9aa2a9c3Schristos 				    filename);
142*9aa2a9c3Schristos 			if (acl_get_tag_type(entry_new, &tag_new) == -1)
143*9aa2a9c3Schristos 				err(1, "%s: acl_get_tag_type() failed",
144*9aa2a9c3Schristos 				    filename);
145*9aa2a9c3Schristos 			if (tag != tag_new)
146*9aa2a9c3Schristos 				continue;
147*9aa2a9c3Schristos 
148*9aa2a9c3Schristos 			/*
149*9aa2a9c3Schristos 			 * For NFSv4, in addition to "tag" and "id" we also
150*9aa2a9c3Schristos 			 * compare "entry_type".
151*9aa2a9c3Schristos 			 */
152*9aa2a9c3Schristos 			if (acl_brand == ACL_BRAND_NFS4) {
153*9aa2a9c3Schristos 				if (acl_get_entry_type_np(entry, &entry_type))
154*9aa2a9c3Schristos 					err(1, "%s: acl_get_entry_type_np() "
155*9aa2a9c3Schristos 					    "failed", filename);
156*9aa2a9c3Schristos 				if (acl_get_entry_type_np(entry_new, &entry_type_new))
157*9aa2a9c3Schristos 					err(1, "%s: acl_get_entry_type_np() "
158*9aa2a9c3Schristos 					    "failed", filename);
159*9aa2a9c3Schristos 				if (entry_type != entry_type_new)
160*9aa2a9c3Schristos 					continue;
161*9aa2a9c3Schristos 			}
162*9aa2a9c3Schristos 
163*9aa2a9c3Schristos 			switch(tag) {
164*9aa2a9c3Schristos 			case ACL_USER:
165*9aa2a9c3Schristos 			case ACL_GROUP:
166*9aa2a9c3Schristos 				have_entry = merge_user_group(&entry,
167*9aa2a9c3Schristos 				    &entry_new, acl_brand);
168*9aa2a9c3Schristos 				if (have_entry == 0)
169*9aa2a9c3Schristos 					break;
170*9aa2a9c3Schristos 				/* FALLTHROUGH */
171*9aa2a9c3Schristos 			case ACL_USER_OBJ:
172*9aa2a9c3Schristos 			case ACL_GROUP_OBJ:
173*9aa2a9c3Schristos 			case ACL_OTHER:
174*9aa2a9c3Schristos 			case ACL_MASK:
175*9aa2a9c3Schristos 			case ACL_EVERYONE:
176*9aa2a9c3Schristos 				if (acl_get_permset(entry, &permset) == -1)
177*9aa2a9c3Schristos 					err(1, "%s: acl_get_permset() failed",
178*9aa2a9c3Schristos 					    filename);
179*9aa2a9c3Schristos 				if (acl_set_permset(entry_new, permset) == -1)
180*9aa2a9c3Schristos 					err(1, "%s: acl_set_permset() failed",
181*9aa2a9c3Schristos 					    filename);
182*9aa2a9c3Schristos 
183*9aa2a9c3Schristos 				if (acl_brand == ACL_BRAND_NFS4) {
184*9aa2a9c3Schristos 					if (acl_get_entry_type_np(entry, &entry_type))
185*9aa2a9c3Schristos 						err(1, "%s: acl_get_entry_type_np() failed",
186*9aa2a9c3Schristos 						    filename);
187*9aa2a9c3Schristos 					if (acl_set_entry_type_np(entry_new, entry_type))
188*9aa2a9c3Schristos 						err(1, "%s: acl_set_entry_type_np() failed",
189*9aa2a9c3Schristos 						    filename);
190*9aa2a9c3Schristos 					if (acl_get_flagset_np(entry, &flagset))
191*9aa2a9c3Schristos 						err(1, "%s: acl_get_flagset_np() failed",
192*9aa2a9c3Schristos 						    filename);
193*9aa2a9c3Schristos 					if (acl_set_flagset_np(entry_new, flagset))
194*9aa2a9c3Schristos 						err(1, "%s: acl_set_flagset_np() failed",
195*9aa2a9c3Schristos 						    filename);
196*9aa2a9c3Schristos 				}
197*9aa2a9c3Schristos 				had_entry = have_entry = 1;
198*9aa2a9c3Schristos 				break;
199*9aa2a9c3Schristos 			default:
200*9aa2a9c3Schristos 				/* should never be here */
201*9aa2a9c3Schristos 				errx(1, "%s: invalid tag type: %i", filename, tag);
202*9aa2a9c3Schristos 				break;
203*9aa2a9c3Schristos 			}
204*9aa2a9c3Schristos 		}
205*9aa2a9c3Schristos 
206*9aa2a9c3Schristos 		/* if this entry has not been found, it must be new */
207*9aa2a9c3Schristos 		if (had_entry == 0) {
208*9aa2a9c3Schristos 
209*9aa2a9c3Schristos 			/*
210*9aa2a9c3Schristos 			 * NFSv4 ACL entries must be prepended to the ACL.
211*9aa2a9c3Schristos 			 * Appending them at the end makes no sense, since
212*9aa2a9c3Schristos 			 * in most cases they wouldn't even get evaluated.
213*9aa2a9c3Schristos 			 */
214*9aa2a9c3Schristos 			if (acl_brand == ACL_BRAND_NFS4) {
215*9aa2a9c3Schristos 				if (acl_create_entry_np(&acl_new, &entry_new, entry_number) == -1) {
216*9aa2a9c3Schristos 					warn("%s: acl_create_entry_np() failed", filename);
217*9aa2a9c3Schristos 					acl_free(acl_new);
218*9aa2a9c3Schristos 					return (-1);
219*9aa2a9c3Schristos 				}
220*9aa2a9c3Schristos 				/*
221*9aa2a9c3Schristos 				 * Without this increment, adding several
222*9aa2a9c3Schristos 				 * entries at once, for example
223*9aa2a9c3Schristos 				 * "setfacl -m user:1:r:allow,user:2:r:allow",
224*9aa2a9c3Schristos 				 * would make them appear in reverse order.
225*9aa2a9c3Schristos 				 */
226*9aa2a9c3Schristos 				entry_number++;
227*9aa2a9c3Schristos 			} else {
228*9aa2a9c3Schristos 				if (acl_create_entry(&acl_new, &entry_new) == -1) {
229*9aa2a9c3Schristos 					warn("%s: acl_create_entry() failed", filename);
230*9aa2a9c3Schristos 					acl_free(acl_new);
231*9aa2a9c3Schristos 					return (-1);
232*9aa2a9c3Schristos 				}
233*9aa2a9c3Schristos 			}
234*9aa2a9c3Schristos 			if (acl_copy_entry(entry_new, entry) == -1)
235*9aa2a9c3Schristos 				err(1, "%s: acl_copy_entry() failed", filename);
236*9aa2a9c3Schristos 		}
237*9aa2a9c3Schristos 	}
238*9aa2a9c3Schristos 
239*9aa2a9c3Schristos 	acl_free(*prev_acl);
240*9aa2a9c3Schristos 	*prev_acl = acl_new;
241*9aa2a9c3Schristos 
242*9aa2a9c3Schristos 	return (0);
243*9aa2a9c3Schristos }
244*9aa2a9c3Schristos 
245*9aa2a9c3Schristos int
add_acl(acl_t acl,uint entry_number,acl_t * prev_acl,const char * filename)246*9aa2a9c3Schristos add_acl(acl_t acl, uint entry_number, acl_t *prev_acl, const char *filename)
247*9aa2a9c3Schristos {
248*9aa2a9c3Schristos 	acl_entry_t entry, entry_new;
249*9aa2a9c3Schristos 	acl_t acl_new;
250*9aa2a9c3Schristos 	int entry_id, acl_brand, prev_acl_brand;
251*9aa2a9c3Schristos 
252*9aa2a9c3Schristos 	acl_get_brand_np(acl, &acl_brand);
253*9aa2a9c3Schristos 	acl_get_brand_np(*prev_acl, &prev_acl_brand);
254*9aa2a9c3Schristos 
255*9aa2a9c3Schristos 	if (prev_acl_brand != ACL_BRAND_NFS4) {
256*9aa2a9c3Schristos 		warnx("%s: the '-a' option is only applicable to NFSv4 ACLs",
257*9aa2a9c3Schristos 		    filename);
258*9aa2a9c3Schristos 		return (-1);
259*9aa2a9c3Schristos 	}
260*9aa2a9c3Schristos 
261*9aa2a9c3Schristos 	if (branding_mismatch(acl_brand, ACL_BRAND_NFS4)) {
262*9aa2a9c3Schristos 		warnx("%s: branding mismatch; existing ACL is NFSv4, "
263*9aa2a9c3Schristos 		    "entry to be added is %s", filename,
264*9aa2a9c3Schristos 		    brand_name(acl_brand));
265*9aa2a9c3Schristos 		return (-1);
266*9aa2a9c3Schristos 	}
267*9aa2a9c3Schristos 
268*9aa2a9c3Schristos 	acl_new = acl_dup(*prev_acl);
269*9aa2a9c3Schristos 	if (acl_new == NULL)
270*9aa2a9c3Schristos 		err(1, "%s: acl_dup() failed", filename);
271*9aa2a9c3Schristos 
272*9aa2a9c3Schristos 	entry_id = ACL_FIRST_ENTRY;
273*9aa2a9c3Schristos 
274*9aa2a9c3Schristos 	while (acl_get_entry(acl, entry_id, &entry) == 1) {
275*9aa2a9c3Schristos 		entry_id = ACL_NEXT_ENTRY;
276*9aa2a9c3Schristos 
277*9aa2a9c3Schristos 		if (acl_create_entry_np(&acl_new, &entry_new, entry_number) == -1) {
278*9aa2a9c3Schristos 			warn("%s: acl_create_entry_np() failed", filename);
279*9aa2a9c3Schristos 			acl_free(acl_new);
280*9aa2a9c3Schristos 			return (-1);
281*9aa2a9c3Schristos 		}
282*9aa2a9c3Schristos 
283*9aa2a9c3Schristos 		/*
284*9aa2a9c3Schristos 		 * Without this increment, adding several
285*9aa2a9c3Schristos 		 * entries at once, for example
286*9aa2a9c3Schristos 		 * "setfacl -m user:1:r:allow,user:2:r:allow",
287*9aa2a9c3Schristos 		 * would make them appear in reverse order.
288*9aa2a9c3Schristos 		 */
289*9aa2a9c3Schristos 		entry_number++;
290*9aa2a9c3Schristos 
291*9aa2a9c3Schristos 		if (acl_copy_entry(entry_new, entry) == -1)
292*9aa2a9c3Schristos 			err(1, "%s: acl_copy_entry() failed", filename);
293*9aa2a9c3Schristos 	}
294*9aa2a9c3Schristos 
295*9aa2a9c3Schristos 	acl_free(*prev_acl);
296*9aa2a9c3Schristos 	*prev_acl = acl_new;
297*9aa2a9c3Schristos 
298*9aa2a9c3Schristos 	return (0);
299*9aa2a9c3Schristos }
300