xref: /freebsd/lib/libc/posix1e/acl_flag.c (revision 559a218c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 <stdio.h>
29 #include <errno.h>
30 #include <sys/acl.h>
31 
32 #include "acl_support.h"
33 
34 static int
_flag_is_invalid(acl_flag_t flag)35 _flag_is_invalid(acl_flag_t flag)
36 {
37 
38 	if ((flag & ACL_FLAGS_BITS) == flag)
39 		return (0);
40 
41 	errno = EINVAL;
42 
43 	return (1);
44 }
45 
46 int
acl_add_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)47 acl_add_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
48 {
49 
50 	if (flagset_d == NULL) {
51 		errno = EINVAL;
52 		return (-1);
53 	}
54 
55 	if (_flag_is_invalid(flag))
56 		return (-1);
57 
58 	*flagset_d |= flag;
59 
60 	return (0);
61 }
62 
63 int
acl_clear_flags_np(acl_flagset_t flagset_d)64 acl_clear_flags_np(acl_flagset_t flagset_d)
65 {
66 
67 	if (flagset_d == NULL) {
68 		errno = EINVAL;
69 		return (-1);
70 	}
71 
72 	*flagset_d = 0;
73 
74 	return (0);
75 }
76 
77 int
acl_delete_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)78 acl_delete_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
79 {
80 
81 	if (flagset_d == NULL) {
82 		errno = EINVAL;
83 		return (-1);
84 	}
85 
86 	if (_flag_is_invalid(flag))
87 		return (-1);
88 
89 	*flagset_d &= ~flag;
90 
91 	return (0);
92 }
93 
94 int
acl_get_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)95 acl_get_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
96 {
97 
98 	if (flagset_d == NULL) {
99 		errno = EINVAL;
100 		return (-1);
101 	}
102 
103 	if (_flag_is_invalid(flag))
104 		return (-1);
105 
106 	if (*flagset_d & flag)
107 		return (1);
108 
109 	return (0);
110 }
111 
112 int
acl_get_flagset_np(acl_entry_t entry_d,acl_flagset_t * flagset_p)113 acl_get_flagset_np(acl_entry_t entry_d, acl_flagset_t *flagset_p)
114 {
115 
116 	if (entry_d == NULL || flagset_p == NULL) {
117 		errno = EINVAL;
118 		return (-1);
119 	}
120 
121 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
122 		errno = EINVAL;
123 		return (-1);
124 	}
125 
126 	*flagset_p = &entry_d->ae_flags;
127 
128 	return (0);
129 }
130 
131 int
acl_set_flagset_np(acl_entry_t entry_d,acl_flagset_t flagset_d)132 acl_set_flagset_np(acl_entry_t entry_d, acl_flagset_t flagset_d)
133 {
134 
135 	if (entry_d == NULL) {
136 		errno = EINVAL;
137 		return (-1);
138 	}
139 
140 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
141 		errno = EINVAL;
142 		return (-1);
143 	}
144 
145 	_entry_brand_as(entry_d, ACL_BRAND_NFS4);
146 
147 	if (_flag_is_invalid(*flagset_d))
148 		return (-1);
149 
150 	entry_d->ae_flags = *flagset_d;
151 
152 	return (0);
153 }
154