xref: /freebsd/lib/libc/posix1e/acl_delete_entry.c (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001-2002 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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include "namespace.h"
34 #include <sys/acl.h>
35 #include "un-namespace.h"
36 #include <errno.h>
37 #include <string.h>
38 #include <stdio.h>
39 
40 #include "acl_support.h"
41 
42 static int
43 _entry_matches(const acl_entry_t a, const acl_entry_t b)
44 {
45 	/*
46 	 * There is a semantical difference here between NFSv4 and POSIX
47 	 * draft ACLs.  In POSIX, there may be only one entry for the particular
48 	 * user or group.  In NFSv4 ACL, there may be any number of them.  We're
49 	 * trying to be more specific here in that case.
50 	 */
51 	switch (_entry_brand(a)) {
52 	case ACL_BRAND_NFS4:
53 		if (a->ae_tag != b->ae_tag || a->ae_entry_type != b->ae_entry_type)
54 			return (0);
55 
56 		/* If ae_ids matter, compare them as well. */
57 		if (a->ae_tag == ACL_USER || a->ae_tag == ACL_GROUP) {
58 			if (a->ae_id != b->ae_id)
59 				return (0);
60 		}
61 
62 		return (1);
63 
64 	default:
65 		if ((a->ae_tag == b->ae_tag) && (a->ae_id == b->ae_id))
66 			return (1);
67 	}
68 
69 	return (0);
70 }
71 
72 /*
73  * acl_delete_entry() (23.4.9): remove the ACL entry indicated by entry_d
74  * from acl.
75  */
76 int
77 acl_delete_entry(acl_t acl, acl_entry_t entry_d)
78 {
79 	struct acl_entry entry_int;
80 	int i, j, found = 0;
81 
82 	if (acl == NULL || entry_d == NULL) {
83 		errno = EINVAL;
84 		return (-1);
85 	}
86 
87 	if (_entry_brand(entry_d) != _acl_brand(acl)) {
88 		errno = EINVAL;
89 		return (-1);
90 	}
91 
92 	if ((acl->ats_acl.acl_cnt < 1) ||
93 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
94 		errno = EINVAL;
95 		return (-1);
96 	}
97 
98 	/* Use a local copy to prevent deletion of more than this entry */
99 	entry_int = *entry_d;
100 
101 	for (i = 0; i < acl->ats_acl.acl_cnt;) {
102 		if (_entry_matches(&(acl->ats_acl.acl_entry[i]), &entry_int)) {
103 			/* ...shift the remaining entries... */
104 			for (j = i; j < acl->ats_acl.acl_cnt - 1; ++j)
105 				acl->ats_acl.acl_entry[j] =
106 				    acl->ats_acl.acl_entry[j+1];
107 			/* ...drop the count and zero the unused entry... */
108 			acl->ats_acl.acl_cnt--;
109 			bzero(&acl->ats_acl.acl_entry[j],
110 			    sizeof(struct acl_entry));
111 			acl->ats_cur_entry = 0;
112 
113 			/* Continue with the loop to remove all matching entries. */
114 			found = 1;
115 		} else
116 			i++;
117 	}
118 
119 	if (found)
120 		return (0);
121 
122 	errno = EINVAL;
123 	return (-1);
124 }
125 
126 int
127 acl_delete_entry_np(acl_t acl, int offset)
128 {
129 	struct acl *acl_int;
130 	int i;
131 
132 	if (acl == NULL) {
133 		errno = EINVAL;
134 		return (-1);
135 	}
136 
137 	acl_int = &acl->ats_acl;
138 
139 	if (offset < 0 || offset >= acl_int->acl_cnt) {
140 		errno = EINVAL;
141 		return (-1);
142 	}
143 
144 	if ((acl->ats_acl.acl_cnt < 1) ||
145 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
146 		errno = EINVAL;
147 		return (-1);
148 	}
149 
150 	/* ...shift the remaining entries... */
151 	for (i = offset; i < acl->ats_acl.acl_cnt - 1; ++i)
152 		acl->ats_acl.acl_entry[i] =
153 		    acl->ats_acl.acl_entry[i+1];
154 	/* ...drop the count and zero the unused entry... */
155 	acl->ats_acl.acl_cnt--;
156 	bzero(&acl->ats_acl.acl_entry[i],
157 	    sizeof(struct acl_entry));
158 	acl->ats_cur_entry = 0;
159 
160 	return (0);
161 }
162