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