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