xref: /freebsd/lib/libc/posix1e/acl_set.c (revision 559a218c)
1515d7c92SRobert Watson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
46394f703SRobert Watson  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
5515d7c92SRobert Watson  * All rights reserved.
6515d7c92SRobert Watson  *
76394f703SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
86394f703SRobert Watson  *
9515d7c92SRobert Watson  * Redistribution and use in source and binary forms, with or without
10515d7c92SRobert Watson  * modification, are permitted provided that the following conditions
11515d7c92SRobert Watson  * are met:
12515d7c92SRobert Watson  * 1. Redistributions of source code must retain the above copyright
13515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer.
14515d7c92SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
15515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
16515d7c92SRobert Watson  *    documentation and/or other materials provided with the distribution.
17515d7c92SRobert Watson  *
18515d7c92SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19515d7c92SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20515d7c92SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21515d7c92SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22515d7c92SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23515d7c92SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24515d7c92SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25515d7c92SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26515d7c92SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27515d7c92SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28515d7c92SRobert Watson  * SUCH DAMAGE.
29515d7c92SRobert Watson  */
30515d7c92SRobert Watson /*
31515d7c92SRobert Watson  * acl_set_file -- set a file/directory ACL by name
32515d7c92SRobert Watson  */
33515d7c92SRobert Watson 
34515d7c92SRobert Watson #include <sys/types.h>
357bd44e92SThomas Moestl #include "namespace.h"
36515d7c92SRobert Watson #include <sys/acl.h>
377bd44e92SThomas Moestl #include "un-namespace.h"
384bf60dfaSChris D. Faulhaber 
39515d7c92SRobert Watson #include <errno.h>
404bf60dfaSChris D. Faulhaber #include <stdlib.h>
414bf60dfaSChris D. Faulhaber #include <string.h>
42aa015c8eSEdward Tomasz Napierala #include <unistd.h>
43515d7c92SRobert Watson 
44515d7c92SRobert Watson #include "acl_support.h"
45515d7c92SRobert Watson 
46515d7c92SRobert Watson /*
47515d7c92SRobert Watson  * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to
48515d7c92SRobert Watson  * (the POSIX.1e semantic code will reject unsorted ACL submission).  If it's
49515d7c92SRobert Watson  * not a semantic that the library knows about, just submit it flat and
50515d7c92SRobert Watson  * assume the caller knows what they're up to.
51515d7c92SRobert Watson  */
52515d7c92SRobert Watson int
acl_set_file(const char * path_p,acl_type_t type,acl_t acl)53515d7c92SRobert Watson acl_set_file(const char *path_p, acl_type_t type, acl_t acl)
54515d7c92SRobert Watson {
55515d7c92SRobert Watson 
56e146d0bcSChris D. Faulhaber 	if (acl == NULL || path_p == NULL) {
57e146d0bcSChris D. Faulhaber 		errno = EINVAL;
58e146d0bcSChris D. Faulhaber 		return (-1);
59e146d0bcSChris D. Faulhaber 	}
60ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
61aa015c8eSEdward Tomasz Napierala 	if (_acl_type_not_valid_for_acl(acl, type)) {
62aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
63aa015c8eSEdward Tomasz Napierala 		return (-1);
64aa015c8eSEdward Tomasz Napierala 	}
65d72fb30aSEdward Tomasz Napierala 	if (_posix1e_acl(acl, type))
66d72fb30aSEdward Tomasz Napierala 		_posix1e_acl_sort(acl);
67515d7c92SRobert Watson 
680f626307SChris D. Faulhaber 	acl->ats_cur_entry = 0;
690f626307SChris D. Faulhaber 
700f626307SChris D. Faulhaber 	return (__acl_set_file(path_p, type, &acl->ats_acl));
71515d7c92SRobert Watson }
72515d7c92SRobert Watson 
73515d7c92SRobert Watson int
acl_set_link_np(const char * path_p,acl_type_t type,acl_t acl)746394f703SRobert Watson acl_set_link_np(const char *path_p, acl_type_t type, acl_t acl)
756394f703SRobert Watson {
766394f703SRobert Watson 
776394f703SRobert Watson 	if (acl == NULL || path_p == NULL) {
786394f703SRobert Watson 		errno = EINVAL;
796394f703SRobert Watson 		return (-1);
806394f703SRobert Watson 	}
81ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
82aa015c8eSEdward Tomasz Napierala 	if (_acl_type_not_valid_for_acl(acl, type)) {
83aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
84aa015c8eSEdward Tomasz Napierala 		return (-1);
85aa015c8eSEdward Tomasz Napierala 	}
86d72fb30aSEdward Tomasz Napierala 	if (_posix1e_acl(acl, type))
87d72fb30aSEdward Tomasz Napierala 		_posix1e_acl_sort(acl);
886394f703SRobert Watson 
896394f703SRobert Watson 	acl->ats_cur_entry = 0;
906394f703SRobert Watson 
916394f703SRobert Watson 	return (__acl_set_link(path_p, type, &acl->ats_acl));
926394f703SRobert Watson }
936394f703SRobert Watson 
946394f703SRobert Watson int
acl_set_fd(int fd,acl_t acl)958f45e8c0SRobert Watson acl_set_fd(int fd, acl_t acl)
96515d7c92SRobert Watson {
97515d7c92SRobert Watson 
98c3380d40SEdward Tomasz Napierala 	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
99aa015c8eSEdward Tomasz Napierala 		return (acl_set_fd_np(fd, acl, ACL_TYPE_NFS4));
1008f45e8c0SRobert Watson 
101aa015c8eSEdward Tomasz Napierala 	return (acl_set_fd_np(fd, acl, ACL_TYPE_ACCESS));
1028f45e8c0SRobert Watson }
1038f45e8c0SRobert Watson 
1048f45e8c0SRobert Watson int
acl_set_fd_np(int fd,acl_t acl,acl_type_t type)1058f45e8c0SRobert Watson acl_set_fd_np(int fd, acl_t acl, acl_type_t type)
1068f45e8c0SRobert Watson {
1078f45e8c0SRobert Watson 
108aa015c8eSEdward Tomasz Napierala 	if (acl == NULL) {
109aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
110aa015c8eSEdward Tomasz Napierala 		return (-1);
111aa015c8eSEdward Tomasz Napierala 	}
112ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
113aa015c8eSEdward Tomasz Napierala 	if (_acl_type_not_valid_for_acl(acl, type)) {
114aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
115aa015c8eSEdward Tomasz Napierala 		return (-1);
116aa015c8eSEdward Tomasz Napierala 	}
117d72fb30aSEdward Tomasz Napierala 	if (_posix1e_acl(acl, type))
118d72fb30aSEdward Tomasz Napierala 		_posix1e_acl_sort(acl);
119515d7c92SRobert Watson 
1200f626307SChris D. Faulhaber 	acl->ats_cur_entry = 0;
1210f626307SChris D. Faulhaber 
1220f626307SChris D. Faulhaber 	return (___acl_set_fd(fd, type, &acl->ats_acl));
123515d7c92SRobert Watson }
1244bf60dfaSChris D. Faulhaber 
1254bf60dfaSChris D. Faulhaber /*
1260f626307SChris D. Faulhaber  * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d
1274bf60dfaSChris D. Faulhaber  * with the permissions in permset_d
1284bf60dfaSChris D. Faulhaber  */
1294bf60dfaSChris D. Faulhaber int
acl_set_permset(acl_entry_t entry_d,acl_permset_t permset_d)1304bf60dfaSChris D. Faulhaber acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d)
1314bf60dfaSChris D. Faulhaber {
1324bf60dfaSChris D. Faulhaber 
1334bf60dfaSChris D. Faulhaber 	if (!entry_d) {
1344bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1359fd46b02SChris D. Faulhaber 		return (-1);
1364bf60dfaSChris D. Faulhaber 	}
1374bf60dfaSChris D. Faulhaber 
138aa015c8eSEdward Tomasz Napierala 	if ((*permset_d & ACL_POSIX1E_BITS) != *permset_d) {
139aa015c8eSEdward Tomasz Napierala 		if ((*permset_d & ACL_NFS4_PERM_BITS) != *permset_d) {
140aa015c8eSEdward Tomasz Napierala 			errno = EINVAL;
141aa015c8eSEdward Tomasz Napierala 			return (-1);
142aa015c8eSEdward Tomasz Napierala 		}
143aa015c8eSEdward Tomasz Napierala 		if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
144aa015c8eSEdward Tomasz Napierala 			errno = EINVAL;
145aa015c8eSEdward Tomasz Napierala 			return (-1);
146aa015c8eSEdward Tomasz Napierala 		}
147aa015c8eSEdward Tomasz Napierala 		_entry_brand_as(entry_d, ACL_BRAND_NFS4);
148aa015c8eSEdward Tomasz Napierala 	}
149aa015c8eSEdward Tomasz Napierala 
1504bf60dfaSChris D. Faulhaber 	entry_d->ae_perm = *permset_d;
1514bf60dfaSChris D. Faulhaber 
1529fd46b02SChris D. Faulhaber 	return (0);
1534bf60dfaSChris D. Faulhaber }
1544bf60dfaSChris D. Faulhaber 
1554bf60dfaSChris D. Faulhaber /*
1564bf60dfaSChris D. Faulhaber  * acl_set_qualifier() sets the qualifier (ae_id) of the tag for
1574bf60dfaSChris D. Faulhaber  * ACL entry entry_d to the value referred to by tag_qualifier_p
1584bf60dfaSChris D. Faulhaber  */
1594bf60dfaSChris D. Faulhaber int
acl_set_qualifier(acl_entry_t entry_d,const void * tag_qualifier_p)1604bf60dfaSChris D. Faulhaber acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p)
1614bf60dfaSChris D. Faulhaber {
162aa015c8eSEdward Tomasz Napierala 
1634bf60dfaSChris D. Faulhaber 	if (!entry_d || !tag_qualifier_p) {
1644bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1659fd46b02SChris D. Faulhaber 		return (-1);
1664bf60dfaSChris D. Faulhaber 	}
1674bf60dfaSChris D. Faulhaber 	switch(entry_d->ae_tag) {
1684bf60dfaSChris D. Faulhaber 	case ACL_USER:
1694bf60dfaSChris D. Faulhaber 	case ACL_GROUP:
170d5675fffSChris D. Faulhaber 		entry_d->ae_id = *(uid_t *)tag_qualifier_p;
1714bf60dfaSChris D. Faulhaber 		break;
1724bf60dfaSChris D. Faulhaber 	default:
1734bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1749fd46b02SChris D. Faulhaber 		return (-1);
1754bf60dfaSChris D. Faulhaber 	}
1764bf60dfaSChris D. Faulhaber 
1779fd46b02SChris D. Faulhaber 	return (0);
1784bf60dfaSChris D. Faulhaber }
1794bf60dfaSChris D. Faulhaber 
1804bf60dfaSChris D. Faulhaber /*
1814bf60dfaSChris D. Faulhaber  * acl_set_tag_type() sets the tag type for ACL entry entry_d to the
1824bf60dfaSChris D. Faulhaber  * value of tag_type
1834bf60dfaSChris D. Faulhaber  */
1844bf60dfaSChris D. Faulhaber int
acl_set_tag_type(acl_entry_t entry_d,acl_tag_t tag_type)1854bf60dfaSChris D. Faulhaber acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type)
1864bf60dfaSChris D. Faulhaber {
1874bf60dfaSChris D. Faulhaber 
1889fd46b02SChris D. Faulhaber 	if (entry_d == NULL) {
1894bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1909fd46b02SChris D. Faulhaber 		return (-1);
1914bf60dfaSChris D. Faulhaber 	}
1924bf60dfaSChris D. Faulhaber 
1934bf60dfaSChris D. Faulhaber 	switch(tag_type) {
194aa015c8eSEdward Tomasz Napierala 	case ACL_OTHER:
195aa015c8eSEdward Tomasz Napierala 	case ACL_MASK:
196aa015c8eSEdward Tomasz Napierala 		if (!_entry_brand_may_be(entry_d, ACL_BRAND_POSIX)) {
197aa015c8eSEdward Tomasz Napierala 			errno = EINVAL;
198aa015c8eSEdward Tomasz Napierala 			return (-1);
199aa015c8eSEdward Tomasz Napierala 		}
200aa015c8eSEdward Tomasz Napierala 		_entry_brand_as(entry_d, ACL_BRAND_POSIX);
201aa015c8eSEdward Tomasz Napierala 		break;
202aa015c8eSEdward Tomasz Napierala 	case ACL_EVERYONE:
203aa015c8eSEdward Tomasz Napierala 		if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
204aa015c8eSEdward Tomasz Napierala 			errno = EINVAL;
205aa015c8eSEdward Tomasz Napierala 			return (-1);
206aa015c8eSEdward Tomasz Napierala 		}
207aa015c8eSEdward Tomasz Napierala 		_entry_brand_as(entry_d, ACL_BRAND_NFS4);
208aa015c8eSEdward Tomasz Napierala 		break;
209aa015c8eSEdward Tomasz Napierala 	}
210aa015c8eSEdward Tomasz Napierala 
211aa015c8eSEdward Tomasz Napierala 	switch(tag_type) {
2124bf60dfaSChris D. Faulhaber 	case ACL_USER_OBJ:
2134bf60dfaSChris D. Faulhaber 	case ACL_USER:
2144bf60dfaSChris D. Faulhaber 	case ACL_GROUP_OBJ:
2154bf60dfaSChris D. Faulhaber 	case ACL_GROUP:
2164bf60dfaSChris D. Faulhaber 	case ACL_MASK:
2174bf60dfaSChris D. Faulhaber 	case ACL_OTHER:
218aa015c8eSEdward Tomasz Napierala 	case ACL_EVERYONE:
2194bf60dfaSChris D. Faulhaber 		entry_d->ae_tag = tag_type;
2209fd46b02SChris D. Faulhaber 		return (0);
2214bf60dfaSChris D. Faulhaber 	}
2224bf60dfaSChris D. Faulhaber 
2234bf60dfaSChris D. Faulhaber 	errno = EINVAL;
2249fd46b02SChris D. Faulhaber 	return (-1);
2254bf60dfaSChris D. Faulhaber }
226aa015c8eSEdward Tomasz Napierala 
227aa015c8eSEdward Tomasz Napierala int
acl_set_entry_type_np(acl_entry_t entry_d,acl_entry_type_t entry_type)228aa015c8eSEdward Tomasz Napierala acl_set_entry_type_np(acl_entry_t entry_d, acl_entry_type_t entry_type)
229aa015c8eSEdward Tomasz Napierala {
230aa015c8eSEdward Tomasz Napierala 
231aa015c8eSEdward Tomasz Napierala 	if (entry_d == NULL) {
232aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
233aa015c8eSEdward Tomasz Napierala 		return (-1);
234aa015c8eSEdward Tomasz Napierala 	}
235aa015c8eSEdward Tomasz Napierala 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
236aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
237aa015c8eSEdward Tomasz Napierala 		return (-1);
238aa015c8eSEdward Tomasz Napierala 	}
239aa015c8eSEdward Tomasz Napierala 	_entry_brand_as(entry_d, ACL_BRAND_NFS4);
240aa015c8eSEdward Tomasz Napierala 
241aa015c8eSEdward Tomasz Napierala 	switch (entry_type) {
242aa015c8eSEdward Tomasz Napierala 	case ACL_ENTRY_TYPE_ALLOW:
243aa015c8eSEdward Tomasz Napierala 	case ACL_ENTRY_TYPE_DENY:
244aa015c8eSEdward Tomasz Napierala 	case ACL_ENTRY_TYPE_AUDIT:
245aa015c8eSEdward Tomasz Napierala 	case ACL_ENTRY_TYPE_ALARM:
246aa015c8eSEdward Tomasz Napierala 		entry_d->ae_entry_type = entry_type;
247aa015c8eSEdward Tomasz Napierala 		return (0);
248aa015c8eSEdward Tomasz Napierala 	}
249aa015c8eSEdward Tomasz Napierala 
250aa015c8eSEdward Tomasz Napierala 	errno = EINVAL;
251aa015c8eSEdward Tomasz Napierala 	return (-1);
252aa015c8eSEdward Tomasz Napierala }
253