xref: /freebsd/lib/libc/posix1e/acl_perm.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/types.h>
30 #include "namespace.h"
31 #include <sys/acl.h>
32 #include "un-namespace.h"
33 
34 #include <errno.h>
35 #include <string.h>
36 
37 static int
38 _perm_is_invalid(acl_perm_t perm)
39 {
40 
41 	/* Check if more than a single bit is set. */
42 	if ((perm & -perm) == perm &&
43 	    (perm & (ACL_POSIX1E_BITS | ACL_NFS4_PERM_BITS)) == perm)
44 		return (0);
45 
46 	errno = EINVAL;
47 
48 	return (1);
49 }
50 
51 /*
52  * acl_add_perm() (23.4.1): add the permission contained in perm to the
53  * permission set permset_d
54  */
55 int
56 acl_add_perm(acl_permset_t permset_d, acl_perm_t perm)
57 {
58 
59 	if (permset_d == NULL) {
60 		errno = EINVAL;
61 		return (-1);
62 	}
63 
64 	if (_perm_is_invalid(perm))
65 		return (-1);
66 
67 	*permset_d |= perm;
68 
69 	return (0);
70 }
71 
72 /*
73  * acl_clear_perms() (23.4.3): clear all permisions from the permission
74  * set permset_d
75  */
76 int
77 acl_clear_perms(acl_permset_t permset_d)
78 {
79 
80 	if (permset_d == NULL) {
81 		errno = EINVAL;
82 		return (-1);
83 	}
84 
85 	*permset_d = ACL_PERM_NONE;
86 
87 	return (0);
88 }
89 
90 /*
91  * acl_delete_perm() (23.4.10): remove the permission in perm from the
92  * permission set permset_d
93  */
94 int
95 acl_delete_perm(acl_permset_t permset_d, acl_perm_t perm)
96 {
97 
98 	if (permset_d == NULL) {
99 		errno = EINVAL;
100 		return (-1);
101 	}
102 
103 	if (_perm_is_invalid(perm))
104 		return (-1);
105 
106 	*permset_d &= ~perm;
107 
108 	return (0);
109 }
110 
111 int
112 acl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
113 {
114 
115 	if (permset_d == NULL) {
116 		errno = EINVAL;
117 		return (-1);
118 	}
119 
120 	if (_perm_is_invalid(perm))
121 		return (-1);
122 
123 	if (*permset_d & perm)
124 		return (1);
125 
126 	return (0);
127 }
128