xref: /freebsd/lib/libc/posix1e/acl_perm.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001-2002 Chris D. Faulhaber
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include "namespace.h"
34 #include <sys/acl.h>
35 #include "un-namespace.h"
36 
37 #include <errno.h>
38 #include <string.h>
39 
40 static int
41 _perm_is_invalid(acl_perm_t perm)
42 {
43 
44 	/* Check if more than a single bit is set. */
45 	if ((perm & -perm) == perm &&
46 	    (perm & (ACL_POSIX1E_BITS | ACL_NFS4_PERM_BITS)) == perm)
47 		return (0);
48 
49 	errno = EINVAL;
50 
51 	return (1);
52 }
53 
54 /*
55  * acl_add_perm() (23.4.1): add the permission contained in perm to the
56  * permission set permset_d
57  */
58 int
59 acl_add_perm(acl_permset_t permset_d, acl_perm_t perm)
60 {
61 
62 	if (permset_d == NULL) {
63 		errno = EINVAL;
64 		return (-1);
65 	}
66 
67 	if (_perm_is_invalid(perm))
68 		return (-1);
69 
70 	*permset_d |= perm;
71 
72 	return (0);
73 }
74 
75 /*
76  * acl_clear_perms() (23.4.3): clear all permisions from the permission
77  * set permset_d
78  */
79 int
80 acl_clear_perms(acl_permset_t permset_d)
81 {
82 
83 	if (permset_d == NULL) {
84 		errno = EINVAL;
85 		return (-1);
86 	}
87 
88 	*permset_d = ACL_PERM_NONE;
89 
90 	return (0);
91 }
92 
93 /*
94  * acl_delete_perm() (23.4.10): remove the permission in perm from the
95  * permission set permset_d
96  */
97 int
98 acl_delete_perm(acl_permset_t permset_d, acl_perm_t perm)
99 {
100 
101 	if (permset_d == NULL) {
102 		errno = EINVAL;
103 		return (-1);
104 	}
105 
106 	if (_perm_is_invalid(perm))
107 		return (-1);
108 
109 	*permset_d &= ~perm;
110 
111 	return (0);
112 }
113 
114 int
115 acl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
116 {
117 
118 	if (permset_d == NULL) {
119 		errno = EINVAL;
120 		return (-1);
121 	}
122 
123 	if (_perm_is_invalid(perm))
124 		return (-1);
125 
126 	if (*permset_d & perm)
127 		return (1);
128 
129 	return (0);
130 }
131