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