xref: /freebsd/lib/libc/posix1e/acl_flag.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <stdio.h>
32 #include <errno.h>
33 #include <sys/acl.h>
34 
35 #include "acl_support.h"
36 
37 static int
38 _flag_is_invalid(acl_flag_t flag)
39 {
40 
41 	if ((flag & ACL_FLAGS_BITS) == flag)
42 		return (0);
43 
44 	errno = EINVAL;
45 
46 	return (1);
47 }
48 
49 int
50 acl_add_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
51 {
52 
53 	if (flagset_d == NULL) {
54 		errno = EINVAL;
55 		return (-1);
56 	}
57 
58 	if (_flag_is_invalid(flag))
59 		return (-1);
60 
61 	*flagset_d |= flag;
62 
63 	return (0);
64 }
65 
66 int
67 acl_clear_flags_np(acl_flagset_t flagset_d)
68 {
69 
70 	if (flagset_d == NULL) {
71 		errno = EINVAL;
72 		return (-1);
73 	}
74 
75 	*flagset_d = 0;
76 
77 	return (0);
78 }
79 
80 int
81 acl_delete_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
82 {
83 
84 	if (flagset_d == NULL) {
85 		errno = EINVAL;
86 		return (-1);
87 	}
88 
89 	if (_flag_is_invalid(flag))
90 		return (-1);
91 
92 	*flagset_d &= ~flag;
93 
94 	return (0);
95 }
96 
97 int
98 acl_get_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
99 {
100 
101 	if (flagset_d == NULL) {
102 		errno = EINVAL;
103 		return (-1);
104 	}
105 
106 	if (_flag_is_invalid(flag))
107 		return (-1);
108 
109 	if (*flagset_d & flag)
110 		return (1);
111 
112 	return (0);
113 }
114 
115 int
116 acl_get_flagset_np(acl_entry_t entry_d, acl_flagset_t *flagset_p)
117 {
118 
119 	if (entry_d == NULL || flagset_p == NULL) {
120 		errno = EINVAL;
121 		return (-1);
122 	}
123 
124 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
125 		errno = EINVAL;
126 		return (-1);
127 	}
128 
129 	*flagset_p = &entry_d->ae_flags;
130 
131 	return (0);
132 }
133 
134 int
135 acl_set_flagset_np(acl_entry_t entry_d, acl_flagset_t flagset_d)
136 {
137 
138 	if (entry_d == NULL) {
139 		errno = EINVAL;
140 		return (-1);
141 	}
142 
143 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
144 		errno = EINVAL;
145 		return (-1);
146 	}
147 
148 	_entry_brand_as(entry_d, ACL_BRAND_NFS4);
149 
150 	if (_flag_is_invalid(*flagset_d))
151 		return (-1);
152 
153 	entry_d->ae_flags = *flagset_d;
154 
155 	return (0);
156 }
157