acl_set.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 1.1 christos *
4 1.1 christos * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This software was developed by Robert Watson for the TrustedBSD Project.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 christos * SUCH DAMAGE.
29 1.1 christos */
30 1.1 christos /*
31 1.1 christos * acl_set_file -- set a file/directory ACL by name
32 1.1 christos */
33 1.1 christos
34 1.1 christos #include <sys/cdefs.h>
35 1.1 christos #if 0
36 1.1 christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_set.c 326193 2017-11-25 17:12:48Z pfg $");
37 1.1 christos #else
38 1.1 christos __RCSID("$NetBSD: acl_set.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
39 1.1 christos #endif
40 1.1 christos
41 1.1 christos #include "namespace.h"
42 1.1 christos #include <sys/types.h>
43 1.1 christos #include <sys/acl.h>
44 1.1 christos
45 1.1 christos #include <errno.h>
46 1.1 christos #include <stdlib.h>
47 1.1 christos #include <string.h>
48 1.1 christos #include <unistd.h>
49 1.1 christos
50 1.1 christos #include "acl_support.h"
51 1.1 christos
52 1.1 christos /*
53 1.1 christos * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to
54 1.1 christos * (the POSIX.1e semantic code will reject unsorted ACL submission). If it's
55 1.1 christos * not a semantic that the library knows about, just submit it flat and
56 1.1 christos * assume the caller knows what they're up to.
57 1.1 christos */
58 1.1 christos int
59 1.1 christos acl_set_file(const char *path_p, acl_type_t type, acl_t acl)
60 1.1 christos {
61 1.1 christos
62 1.1 christos if (acl == NULL || path_p == NULL) {
63 1.1 christos errno = EINVAL;
64 1.1 christos return (-1);
65 1.1 christos }
66 1.1 christos type = _acl_type_unold(type);
67 1.1 christos if (_acl_type_not_valid_for_acl(acl, type)) {
68 1.1 christos errno = EINVAL;
69 1.1 christos return (-1);
70 1.1 christos }
71 1.1 christos if (_posix1e_acl(acl, type))
72 1.1 christos _posix1e_acl_sort(acl);
73 1.1 christos
74 1.1 christos acl->ats_cur_entry = 0;
75 1.1 christos
76 1.1 christos return (__acl_set_file(path_p, type, &acl->ats_acl));
77 1.1 christos }
78 1.1 christos
79 1.1 christos int
80 1.1 christos acl_set_link_np(const char *path_p, acl_type_t type, acl_t acl)
81 1.1 christos {
82 1.1 christos
83 1.1 christos if (acl == NULL || path_p == NULL) {
84 1.1 christos errno = EINVAL;
85 1.1 christos return (-1);
86 1.1 christos }
87 1.1 christos type = _acl_type_unold(type);
88 1.1 christos if (_acl_type_not_valid_for_acl(acl, type)) {
89 1.1 christos errno = EINVAL;
90 1.1 christos return (-1);
91 1.1 christos }
92 1.1 christos if (_posix1e_acl(acl, type))
93 1.1 christos _posix1e_acl_sort(acl);
94 1.1 christos
95 1.1 christos acl->ats_cur_entry = 0;
96 1.1 christos
97 1.1 christos return (__acl_set_link(path_p, type, &acl->ats_acl));
98 1.1 christos }
99 1.1 christos
100 1.1 christos int
101 1.1 christos acl_set_fd(int fd, acl_t acl)
102 1.1 christos {
103 1.1 christos
104 1.1 christos if (fpathconf(fd, _PC_ACL_NFS4) == 1)
105 1.1 christos return (acl_set_fd_np(fd, acl, ACL_TYPE_NFS4));
106 1.1 christos
107 1.1 christos return (acl_set_fd_np(fd, acl, ACL_TYPE_ACCESS));
108 1.1 christos }
109 1.1 christos
110 1.1 christos int
111 1.1 christos acl_set_fd_np(int fd, acl_t acl, acl_type_t type)
112 1.1 christos {
113 1.1 christos
114 1.1 christos if (acl == NULL) {
115 1.1 christos errno = EINVAL;
116 1.1 christos return (-1);
117 1.1 christos }
118 1.1 christos type = _acl_type_unold(type);
119 1.1 christos if (_acl_type_not_valid_for_acl(acl, type)) {
120 1.1 christos errno = EINVAL;
121 1.1 christos return (-1);
122 1.1 christos }
123 1.1 christos if (_posix1e_acl(acl, type))
124 1.1 christos _posix1e_acl_sort(acl);
125 1.1 christos
126 1.1 christos acl->ats_cur_entry = 0;
127 1.1 christos
128 1.1 christos return (__acl_set_fd(fd, type, &acl->ats_acl));
129 1.1 christos }
130 1.1 christos
131 1.1 christos /*
132 1.1 christos * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d
133 1.1 christos * with the permissions in permset_d
134 1.1 christos */
135 1.1 christos int
136 1.1 christos acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d)
137 1.1 christos {
138 1.1 christos
139 1.1 christos if (!entry_d) {
140 1.1 christos errno = EINVAL;
141 1.1 christos return (-1);
142 1.1 christos }
143 1.1 christos
144 1.1 christos if ((*permset_d & ACL_POSIX1E_BITS) != *permset_d) {
145 1.1 christos if ((*permset_d & ACL_NFS4_PERM_BITS) != *permset_d) {
146 1.1 christos errno = EINVAL;
147 1.1 christos return (-1);
148 1.1 christos }
149 1.1 christos if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
150 1.1 christos errno = EINVAL;
151 1.1 christos return (-1);
152 1.1 christos }
153 1.1 christos _entry_brand_as(entry_d, ACL_BRAND_NFS4);
154 1.1 christos }
155 1.1 christos
156 1.1 christos entry_d->ae_perm = *permset_d;
157 1.1 christos
158 1.1 christos return (0);
159 1.1 christos }
160 1.1 christos
161 1.1 christos /*
162 1.1 christos * acl_set_qualifier() sets the qualifier (ae_id) of the tag for
163 1.1 christos * ACL entry entry_d to the value referred to by tag_qualifier_p
164 1.1 christos */
165 1.1 christos int
166 1.1 christos acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p)
167 1.1 christos {
168 1.1 christos
169 1.1 christos if (!entry_d || !tag_qualifier_p) {
170 1.1 christos errno = EINVAL;
171 1.1 christos return (-1);
172 1.1 christos }
173 1.1 christos switch(entry_d->ae_tag) {
174 1.1 christos case ACL_USER:
175 1.1 christos case ACL_GROUP:
176 1.1 christos entry_d->ae_id = *(const uid_t *)tag_qualifier_p;
177 1.1 christos break;
178 1.1 christos default:
179 1.1 christos errno = EINVAL;
180 1.1 christos return (-1);
181 1.1 christos }
182 1.1 christos
183 1.1 christos return (0);
184 1.1 christos }
185 1.1 christos
186 1.1 christos /*
187 1.1 christos * acl_set_tag_type() sets the tag type for ACL entry entry_d to the
188 1.1 christos * value of tag_type
189 1.1 christos */
190 1.1 christos int
191 1.1 christos acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type)
192 1.1 christos {
193 1.1 christos
194 1.1 christos if (entry_d == NULL) {
195 1.1 christos errno = EINVAL;
196 1.1 christos return (-1);
197 1.1 christos }
198 1.1 christos
199 1.1 christos switch(tag_type) {
200 1.1 christos case ACL_OTHER:
201 1.1 christos case ACL_MASK:
202 1.1 christos if (!_entry_brand_may_be(entry_d, ACL_BRAND_POSIX)) {
203 1.1 christos errno = EINVAL;
204 1.1 christos return (-1);
205 1.1 christos }
206 1.1 christos _entry_brand_as(entry_d, ACL_BRAND_POSIX);
207 1.1 christos break;
208 1.1 christos case ACL_EVERYONE:
209 1.1 christos if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
210 1.1 christos errno = EINVAL;
211 1.1 christos return (-1);
212 1.1 christos }
213 1.1 christos _entry_brand_as(entry_d, ACL_BRAND_NFS4);
214 1.1 christos break;
215 1.1 christos }
216 1.1 christos
217 1.1 christos switch(tag_type) {
218 1.1 christos case ACL_USER_OBJ:
219 1.1 christos case ACL_USER:
220 1.1 christos case ACL_GROUP_OBJ:
221 1.1 christos case ACL_GROUP:
222 1.1 christos case ACL_MASK:
223 1.1 christos case ACL_OTHER:
224 1.1 christos case ACL_EVERYONE:
225 1.1 christos entry_d->ae_tag = tag_type;
226 1.1 christos return (0);
227 1.1 christos }
228 1.1 christos
229 1.1 christos errno = EINVAL;
230 1.1 christos return (-1);
231 1.1 christos }
232 1.1 christos
233 1.1 christos int
234 1.1 christos acl_set_entry_type_np(acl_entry_t entry_d, acl_entry_type_t entry_type)
235 1.1 christos {
236 1.1 christos
237 1.1 christos if (entry_d == NULL) {
238 1.1 christos errno = EINVAL;
239 1.1 christos return (-1);
240 1.1 christos }
241 1.1 christos if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
242 1.1 christos errno = EINVAL;
243 1.1 christos return (-1);
244 1.1 christos }
245 1.1 christos _entry_brand_as(entry_d, ACL_BRAND_NFS4);
246 1.1 christos
247 1.1 christos switch (entry_type) {
248 1.1 christos case ACL_ENTRY_TYPE_ALLOW:
249 1.1 christos case ACL_ENTRY_TYPE_DENY:
250 1.1 christos case ACL_ENTRY_TYPE_AUDIT:
251 1.1 christos case ACL_ENTRY_TYPE_ALARM:
252 1.1 christos entry_d->ae_entry_type = entry_type;
253 1.1 christos return (0);
254 1.1 christos }
255 1.1 christos
256 1.1 christos errno = EINVAL;
257 1.1 christos return (-1);
258 1.1 christos }
259