xref: /netbsd-src/lib/libc/posix1e/acl_valid.c (revision 9aa2a9c323eb12a08584c70d6ea91d316703d3fe)
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_valid -- POSIX.1e ACL check routine
32*9aa2a9c3Schristos  */
33*9aa2a9c3Schristos 
34*9aa2a9c3Schristos #include <sys/cdefs.h>
35*9aa2a9c3Schristos #if 0
36*9aa2a9c3Schristos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_valid.c 326193 2017-11-25 17:12:48Z pfg $");
37*9aa2a9c3Schristos #else
38*9aa2a9c3Schristos __RCSID("$NetBSD: acl_valid.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 #include <errno.h>
45*9aa2a9c3Schristos #include <stdlib.h>
46*9aa2a9c3Schristos 
47*9aa2a9c3Schristos #include "acl_support.h"
48*9aa2a9c3Schristos 
49*9aa2a9c3Schristos /*
50*9aa2a9c3Schristos  * acl_valid: accepts an ACL, returns 0 on valid ACL, -1 for invalid,
51*9aa2a9c3Schristos  * and errno set to EINVAL.
52*9aa2a9c3Schristos  *
53*9aa2a9c3Schristos  * Implemented by calling the acl_check routine in acl_support, which
54*9aa2a9c3Schristos  * requires ordering.  We call acl_support's _posix1e_acl_sort to make this
55*9aa2a9c3Schristos  * true.  POSIX.1e allows acl_valid() to reorder the ACL as it sees fit.
56*9aa2a9c3Schristos  *
57*9aa2a9c3Schristos  * This call is deprecated, as it doesn't ask whether the ACL is valid
58*9aa2a9c3Schristos  * for a particular target.  However, this call is standardized, unlike
59*9aa2a9c3Schristos  * the other two forms.
60*9aa2a9c3Schristos  */
61*9aa2a9c3Schristos int
acl_valid(acl_t acl)62*9aa2a9c3Schristos acl_valid(acl_t acl)
63*9aa2a9c3Schristos {
64*9aa2a9c3Schristos 	int	error;
65*9aa2a9c3Schristos 
66*9aa2a9c3Schristos 	if (acl == NULL) {
67*9aa2a9c3Schristos 		errno = EINVAL;
68*9aa2a9c3Schristos 		return (-1);
69*9aa2a9c3Schristos 	}
70*9aa2a9c3Schristos 	if (!_acl_brand_may_be(acl, ACL_BRAND_POSIX)) {
71*9aa2a9c3Schristos 		errno = EINVAL;
72*9aa2a9c3Schristos 		return (-1);
73*9aa2a9c3Schristos 	}
74*9aa2a9c3Schristos 	_posix1e_acl_sort(acl);
75*9aa2a9c3Schristos 	error = _posix1e_acl_check(acl);
76*9aa2a9c3Schristos 	if (error) {
77*9aa2a9c3Schristos 		errno = error;
78*9aa2a9c3Schristos 		return (-1);
79*9aa2a9c3Schristos 	} else {
80*9aa2a9c3Schristos 		return (0);
81*9aa2a9c3Schristos 	}
82*9aa2a9c3Schristos }
83*9aa2a9c3Schristos 
84*9aa2a9c3Schristos int
acl_valid_file_np(const char * pathp,acl_type_t type,acl_t acl)85*9aa2a9c3Schristos acl_valid_file_np(const char *pathp, acl_type_t type, acl_t acl)
86*9aa2a9c3Schristos {
87*9aa2a9c3Schristos 
88*9aa2a9c3Schristos 	if (pathp == NULL || acl == NULL) {
89*9aa2a9c3Schristos 		errno = EINVAL;
90*9aa2a9c3Schristos 		return (-1);
91*9aa2a9c3Schristos 	}
92*9aa2a9c3Schristos 	type = _acl_type_unold(type);
93*9aa2a9c3Schristos 	if (_posix1e_acl(acl, type))
94*9aa2a9c3Schristos 		_posix1e_acl_sort(acl);
95*9aa2a9c3Schristos 
96*9aa2a9c3Schristos 	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
97*9aa2a9c3Schristos }
98*9aa2a9c3Schristos 
99*9aa2a9c3Schristos int
acl_valid_link_np(const char * pathp,acl_type_t type,acl_t acl)100*9aa2a9c3Schristos acl_valid_link_np(const char *pathp, acl_type_t type, acl_t acl)
101*9aa2a9c3Schristos {
102*9aa2a9c3Schristos 
103*9aa2a9c3Schristos 	if (pathp == NULL || acl == NULL) {
104*9aa2a9c3Schristos 		errno = EINVAL;
105*9aa2a9c3Schristos 		return (-1);
106*9aa2a9c3Schristos 	}
107*9aa2a9c3Schristos 	type = _acl_type_unold(type);
108*9aa2a9c3Schristos 	if (_posix1e_acl(acl, type))
109*9aa2a9c3Schristos 		_posix1e_acl_sort(acl);
110*9aa2a9c3Schristos 
111*9aa2a9c3Schristos 	return (__acl_aclcheck_link(pathp, type, &acl->ats_acl));
112*9aa2a9c3Schristos }
113*9aa2a9c3Schristos 
114*9aa2a9c3Schristos int
acl_valid_fd_np(int fd,acl_type_t type,acl_t acl)115*9aa2a9c3Schristos acl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
116*9aa2a9c3Schristos {
117*9aa2a9c3Schristos 
118*9aa2a9c3Schristos 	if (acl == NULL) {
119*9aa2a9c3Schristos 		errno = EINVAL;
120*9aa2a9c3Schristos 		return (-1);
121*9aa2a9c3Schristos 	}
122*9aa2a9c3Schristos 	type = _acl_type_unold(type);
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_aclcheck_fd(fd, type, &acl->ats_acl));
129*9aa2a9c3Schristos }
130