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