xref: /freebsd/lib/libc/posix1e/acl_valid.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1515d7c92SRobert Watson /*-
2*4d846d26SWarner 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_valid -- POSIX.1e ACL check routine
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"
38515d7c92SRobert Watson #include <sys/errno.h>
39e146d0bcSChris D. Faulhaber #include <stdlib.h>
40515d7c92SRobert Watson 
41515d7c92SRobert Watson #include "acl_support.h"
42515d7c92SRobert Watson 
43515d7c92SRobert Watson /*
44515d7c92SRobert Watson  * acl_valid: accepts an ACL, returns 0 on valid ACL, -1 for invalid,
45515d7c92SRobert Watson  * and errno set to EINVAL.
46515d7c92SRobert Watson  *
47515d7c92SRobert Watson  * Implemented by calling the acl_check routine in acl_support, which
482de14c39SRobert Watson  * requires ordering.  We call acl_support's _posix1e_acl_sort to make this
49a889d1fbSRobert Watson  * true.  POSIX.1e allows acl_valid() to reorder the ACL as it sees fit.
50515d7c92SRobert Watson  *
51515d7c92SRobert Watson  * This call is deprecated, as it doesn't ask whether the ACL is valid
52a889d1fbSRobert Watson  * for a particular target.  However, this call is standardized, unlike
53a889d1fbSRobert Watson  * the other two forms.
54515d7c92SRobert Watson  */
55515d7c92SRobert Watson int
acl_valid(acl_t acl)56515d7c92SRobert Watson acl_valid(acl_t acl)
57515d7c92SRobert Watson {
58515d7c92SRobert Watson 	int	error;
59515d7c92SRobert Watson 
60e146d0bcSChris D. Faulhaber 	if (acl == NULL) {
61e146d0bcSChris D. Faulhaber 		errno = EINVAL;
62e146d0bcSChris D. Faulhaber 		return (-1);
63e146d0bcSChris D. Faulhaber 	}
64aa015c8eSEdward Tomasz Napierala 	if (!_acl_brand_may_be(acl, ACL_BRAND_POSIX)) {
65aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
66aa015c8eSEdward Tomasz Napierala 		return (-1);
67aa015c8eSEdward Tomasz Napierala 	}
682de14c39SRobert Watson 	_posix1e_acl_sort(acl);
692de14c39SRobert Watson 	error = _posix1e_acl_check(acl);
70515d7c92SRobert Watson 	if (error) {
71515d7c92SRobert Watson 		errno = error;
72515d7c92SRobert Watson 		return (-1);
73515d7c92SRobert Watson 	} else {
74515d7c92SRobert Watson 		return (0);
75515d7c92SRobert Watson 	}
76515d7c92SRobert Watson }
77515d7c92SRobert Watson 
78515d7c92SRobert Watson int
acl_valid_file_np(const char * pathp,acl_type_t type,acl_t acl)79a889d1fbSRobert Watson acl_valid_file_np(const char *pathp, acl_type_t type, acl_t acl)
80515d7c92SRobert Watson {
81515d7c92SRobert Watson 
82e146d0bcSChris D. Faulhaber 	if (pathp == NULL || acl == NULL) {
83e146d0bcSChris D. Faulhaber 		errno = EINVAL;
84e146d0bcSChris D. Faulhaber 		return (-1);
85e146d0bcSChris D. Faulhaber 	}
86ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
87d72fb30aSEdward Tomasz Napierala 	if (_posix1e_acl(acl, type))
88d72fb30aSEdward Tomasz Napierala 		_posix1e_acl_sort(acl);
89515d7c92SRobert Watson 
900f626307SChris D. Faulhaber 	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
91515d7c92SRobert Watson }
92515d7c92SRobert Watson 
936394f703SRobert Watson int
acl_valid_link_np(const char * pathp,acl_type_t type,acl_t acl)946394f703SRobert Watson acl_valid_link_np(const char *pathp, acl_type_t type, acl_t acl)
956394f703SRobert Watson {
966394f703SRobert Watson 
976394f703SRobert Watson 	if (pathp == NULL || acl == NULL) {
986394f703SRobert Watson 		errno = EINVAL;
996394f703SRobert Watson 		return (-1);
1006394f703SRobert Watson 	}
101ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
102d72fb30aSEdward Tomasz Napierala 	if (_posix1e_acl(acl, type))
103d72fb30aSEdward Tomasz Napierala 		_posix1e_acl_sort(acl);
1046394f703SRobert Watson 
1056394f703SRobert Watson 	return (__acl_aclcheck_link(pathp, type, &acl->ats_acl));
1066394f703SRobert Watson }
107515d7c92SRobert Watson 
108515d7c92SRobert Watson int
acl_valid_fd_np(int fd,acl_type_t type,acl_t acl)109a889d1fbSRobert Watson acl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
110515d7c92SRobert Watson {
111515d7c92SRobert Watson 
112e146d0bcSChris D. Faulhaber 	if (acl == NULL) {
113e146d0bcSChris D. Faulhaber 		errno = EINVAL;
114e146d0bcSChris D. Faulhaber 		return (-1);
115e146d0bcSChris D. Faulhaber 	}
116ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
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_aclcheck_fd(fd, type, &acl->ats_acl));
123515d7c92SRobert Watson }
124