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