xref: /freebsd/lib/libc/posix1e/acl_delete_entry.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4e76872c1SChris D. Faulhaber  * Copyright (c) 2001-2002 Chris D. Faulhaber
514721edaSChris D. Faulhaber  * All rights reserved.
614721edaSChris D. Faulhaber  *
714721edaSChris D. Faulhaber  * Redistribution and use in source and binary forms, with or without
814721edaSChris D. Faulhaber  * modification, are permitted provided that the following conditions
914721edaSChris D. Faulhaber  * are met:
1014721edaSChris D. Faulhaber  * 1. Redistributions of source code must retain the above copyright
1114721edaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer.
1214721edaSChris D. Faulhaber  * 2. Redistributions in binary form must reproduce the above copyright
1314721edaSChris D. Faulhaber  *    notice, this list of conditions and the following disclaimer in the
1414721edaSChris D. Faulhaber  *    documentation and/or other materials provided with the distribution.
1514721edaSChris D. Faulhaber  *
1614721edaSChris D. Faulhaber  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1714721edaSChris D. Faulhaber  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1814721edaSChris D. Faulhaber  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1914721edaSChris D. Faulhaber  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2014721edaSChris D. Faulhaber  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2114721edaSChris D. Faulhaber  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2214721edaSChris D. Faulhaber  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2314721edaSChris D. Faulhaber  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2414721edaSChris D. Faulhaber  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2514721edaSChris D. Faulhaber  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2614721edaSChris D. Faulhaber  * SUCH DAMAGE.
2714721edaSChris D. Faulhaber  */
2814721edaSChris D. Faulhaber 
2914721edaSChris D. Faulhaber #include <sys/types.h>
307bd44e92SThomas Moestl #include "namespace.h"
3114721edaSChris D. Faulhaber #include <sys/acl.h>
327bd44e92SThomas Moestl #include "un-namespace.h"
3314721edaSChris D. Faulhaber #include <errno.h>
3414721edaSChris D. Faulhaber #include <string.h>
35aa015c8eSEdward Tomasz Napierala #include <stdio.h>
36aa015c8eSEdward Tomasz Napierala 
37aa015c8eSEdward Tomasz Napierala #include "acl_support.h"
38aa015c8eSEdward Tomasz Napierala 
39aa015c8eSEdward Tomasz Napierala static int
_entry_matches(const acl_entry_t a,const acl_entry_t b)40aa015c8eSEdward Tomasz Napierala _entry_matches(const acl_entry_t a, const acl_entry_t b)
41aa015c8eSEdward Tomasz Napierala {
42aa015c8eSEdward Tomasz Napierala 	/*
43aa015c8eSEdward Tomasz Napierala 	 * There is a semantical difference here between NFSv4 and POSIX
44aa015c8eSEdward Tomasz Napierala 	 * draft ACLs.  In POSIX, there may be only one entry for the particular
45aa015c8eSEdward Tomasz Napierala 	 * user or group.  In NFSv4 ACL, there may be any number of them.  We're
46aa015c8eSEdward Tomasz Napierala 	 * trying to be more specific here in that case.
47aa015c8eSEdward Tomasz Napierala 	 */
48aa015c8eSEdward Tomasz Napierala 	switch (_entry_brand(a)) {
49aa015c8eSEdward Tomasz Napierala 	case ACL_BRAND_NFS4:
50aa015c8eSEdward Tomasz Napierala 		if (a->ae_tag != b->ae_tag || a->ae_entry_type != b->ae_entry_type)
51aa015c8eSEdward Tomasz Napierala 			return (0);
52aa015c8eSEdward Tomasz Napierala 
53aa015c8eSEdward Tomasz Napierala 		/* If ae_ids matter, compare them as well. */
54aa015c8eSEdward Tomasz Napierala 		if (a->ae_tag == ACL_USER || a->ae_tag == ACL_GROUP) {
55aa015c8eSEdward Tomasz Napierala 			if (a->ae_id != b->ae_id)
56aa015c8eSEdward Tomasz Napierala 				return (0);
57aa015c8eSEdward Tomasz Napierala 		}
58aa015c8eSEdward Tomasz Napierala 
59aa015c8eSEdward Tomasz Napierala 		return (1);
60aa015c8eSEdward Tomasz Napierala 
61aa015c8eSEdward Tomasz Napierala 	default:
62aa015c8eSEdward Tomasz Napierala 		if ((a->ae_tag == b->ae_tag) && (a->ae_id == b->ae_id))
63aa015c8eSEdward Tomasz Napierala 			return (1);
64aa015c8eSEdward Tomasz Napierala 	}
65aa015c8eSEdward Tomasz Napierala 
66aa015c8eSEdward Tomasz Napierala 	return (0);
67aa015c8eSEdward Tomasz Napierala }
6814721edaSChris D. Faulhaber 
690f626307SChris D. Faulhaber /*
700f626307SChris D. Faulhaber  * acl_delete_entry() (23.4.9): remove the ACL entry indicated by entry_d
710f626307SChris D. Faulhaber  * from acl.
720f626307SChris D. Faulhaber  */
7314721edaSChris D. Faulhaber int
acl_delete_entry(acl_t acl,acl_entry_t entry_d)7414721edaSChris D. Faulhaber acl_delete_entry(acl_t acl, acl_entry_t entry_d)
7514721edaSChris D. Faulhaber {
7678da985fSMarkus Brueffer 	struct acl_entry entry_int;
77aa015c8eSEdward Tomasz Napierala 	int i, j, found = 0;
7814721edaSChris D. Faulhaber 
79e76872c1SChris D. Faulhaber 	if (acl == NULL || entry_d == NULL) {
8014721edaSChris D. Faulhaber 		errno = EINVAL;
81e76872c1SChris D. Faulhaber 		return (-1);
8214721edaSChris D. Faulhaber 	}
830f626307SChris D. Faulhaber 
84aa015c8eSEdward Tomasz Napierala 	if (_entry_brand(entry_d) != _acl_brand(acl)) {
85aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
86aa015c8eSEdward Tomasz Napierala 		return (-1);
87aa015c8eSEdward Tomasz Napierala 	}
88aa015c8eSEdward Tomasz Napierala 
890f626307SChris D. Faulhaber 	if ((acl->ats_acl.acl_cnt < 1) ||
900f626307SChris D. Faulhaber 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
910f626307SChris D. Faulhaber 		errno = EINVAL;
92e76872c1SChris D. Faulhaber 		return (-1);
930f626307SChris D. Faulhaber 	}
9478da985fSMarkus Brueffer 
9578da985fSMarkus Brueffer 	/* Use a local copy to prevent deletion of more than this entry */
9678da985fSMarkus Brueffer 	entry_int = *entry_d;
9778da985fSMarkus Brueffer 
98aa015c8eSEdward Tomasz Napierala 	for (i = 0; i < acl->ats_acl.acl_cnt;) {
9978da985fSMarkus Brueffer 		if (_entry_matches(&(acl->ats_acl.acl_entry[i]), &entry_int)) {
10014721edaSChris D. Faulhaber 			/* ...shift the remaining entries... */
101aa015c8eSEdward Tomasz Napierala 			for (j = i; j < acl->ats_acl.acl_cnt - 1; ++j)
102aa015c8eSEdward Tomasz Napierala 				acl->ats_acl.acl_entry[j] =
103aa015c8eSEdward Tomasz Napierala 				    acl->ats_acl.acl_entry[j+1];
104aa015c8eSEdward Tomasz Napierala 			/* ...drop the count and zero the unused entry... */
105aa015c8eSEdward Tomasz Napierala 			acl->ats_acl.acl_cnt--;
106aa015c8eSEdward Tomasz Napierala 			bzero(&acl->ats_acl.acl_entry[j],
107aa015c8eSEdward Tomasz Napierala 			    sizeof(struct acl_entry));
108aa015c8eSEdward Tomasz Napierala 			acl->ats_cur_entry = 0;
109aa015c8eSEdward Tomasz Napierala 
11032223c1bSPedro F. Giffuni 			/* Continue with the loop to remove all matching entries. */
111aa015c8eSEdward Tomasz Napierala 			found = 1;
112aa015c8eSEdward Tomasz Napierala 		} else
113aa015c8eSEdward Tomasz Napierala 			i++;
114aa015c8eSEdward Tomasz Napierala 	}
115aa015c8eSEdward Tomasz Napierala 
116aa015c8eSEdward Tomasz Napierala 	if (found)
117aa015c8eSEdward Tomasz Napierala 		return (0);
118aa015c8eSEdward Tomasz Napierala 
119aa015c8eSEdward Tomasz Napierala 	errno = EINVAL;
120aa015c8eSEdward Tomasz Napierala 	return (-1);
121aa015c8eSEdward Tomasz Napierala }
122aa015c8eSEdward Tomasz Napierala 
123aa015c8eSEdward Tomasz Napierala int
acl_delete_entry_np(acl_t acl,int offset)124aa015c8eSEdward Tomasz Napierala acl_delete_entry_np(acl_t acl, int offset)
125aa015c8eSEdward Tomasz Napierala {
126aa015c8eSEdward Tomasz Napierala 	struct acl *acl_int;
127aa015c8eSEdward Tomasz Napierala 	int i;
128aa015c8eSEdward Tomasz Napierala 
129aa015c8eSEdward Tomasz Napierala 	if (acl == NULL) {
130aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
131aa015c8eSEdward Tomasz Napierala 		return (-1);
132aa015c8eSEdward Tomasz Napierala 	}
133aa015c8eSEdward Tomasz Napierala 
134aa015c8eSEdward Tomasz Napierala 	acl_int = &acl->ats_acl;
135aa015c8eSEdward Tomasz Napierala 
136aa015c8eSEdward Tomasz Napierala 	if (offset < 0 || offset >= acl_int->acl_cnt) {
137aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
138aa015c8eSEdward Tomasz Napierala 		return (-1);
139aa015c8eSEdward Tomasz Napierala 	}
140aa015c8eSEdward Tomasz Napierala 
141aa015c8eSEdward Tomasz Napierala 	if ((acl->ats_acl.acl_cnt < 1) ||
142aa015c8eSEdward Tomasz Napierala 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
143aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
144aa015c8eSEdward Tomasz Napierala 		return (-1);
145aa015c8eSEdward Tomasz Napierala 	}
146aa015c8eSEdward Tomasz Napierala 
147aa015c8eSEdward Tomasz Napierala 	/* ...shift the remaining entries... */
148aa015c8eSEdward Tomasz Napierala 	for (i = offset; i < acl->ats_acl.acl_cnt - 1; ++i)
1490f626307SChris D. Faulhaber 		acl->ats_acl.acl_entry[i] =
150ff5fe653SJacques Vidrine 		    acl->ats_acl.acl_entry[i+1];
15114721edaSChris D. Faulhaber 	/* ...drop the count and zero the unused entry... */
1520f626307SChris D. Faulhaber 	acl->ats_acl.acl_cnt--;
1530f626307SChris D. Faulhaber 	bzero(&acl->ats_acl.acl_entry[i],
1540f626307SChris D. Faulhaber 	    sizeof(struct acl_entry));
1550f626307SChris D. Faulhaber 	acl->ats_cur_entry = 0;
156aa015c8eSEdward Tomasz Napierala 
157e76872c1SChris D. Faulhaber 	return (0);
15814721edaSChris D. Faulhaber }
159