xref: /netbsd/lib/libc/posix1e/acl_flag.c (revision b9c1fd7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@FreeBSD.org>
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 THE 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_flag.c 326193 2017-11-25 17:12:48Z pfg $");
32 #else
33 __RCSID("$NetBSD: acl_flag.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
34 #endif
35 
36 #include <stdio.h>
37 #include <errno.h>
38 #include <sys/acl.h>
39 
40 #include "acl_support.h"
41 
42 static int
_flag_is_invalid(acl_flag_t flag)43 _flag_is_invalid(acl_flag_t flag)
44 {
45 
46 	if ((flag & ACL_FLAGS_BITS) == flag)
47 		return (0);
48 
49 	errno = EINVAL;
50 
51 	return (1);
52 }
53 
54 int
acl_add_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)55 acl_add_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
56 {
57 
58 	if (flagset_d == NULL) {
59 		errno = EINVAL;
60 		return (-1);
61 	}
62 
63 	if (_flag_is_invalid(flag))
64 		return (-1);
65 
66 	*flagset_d |= flag;
67 
68 	return (0);
69 }
70 
71 int
acl_clear_flags_np(acl_flagset_t flagset_d)72 acl_clear_flags_np(acl_flagset_t flagset_d)
73 {
74 
75 	if (flagset_d == NULL) {
76 		errno = EINVAL;
77 		return (-1);
78 	}
79 
80 	*flagset_d = 0;
81 
82 	return (0);
83 }
84 
85 int
acl_delete_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)86 acl_delete_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
87 {
88 
89 	if (flagset_d == NULL) {
90 		errno = EINVAL;
91 		return (-1);
92 	}
93 
94 	if (_flag_is_invalid(flag))
95 		return (-1);
96 
97 	*flagset_d &= ~flag;
98 
99 	return (0);
100 }
101 
102 int
acl_get_flag_np(acl_flagset_t flagset_d,acl_flag_t flag)103 acl_get_flag_np(acl_flagset_t flagset_d, acl_flag_t flag)
104 {
105 
106 	if (flagset_d == NULL) {
107 		errno = EINVAL;
108 		return (-1);
109 	}
110 
111 	if (_flag_is_invalid(flag))
112 		return (-1);
113 
114 	if (*flagset_d & flag)
115 		return (1);
116 
117 	return (0);
118 }
119 
120 int
acl_get_flagset_np(acl_entry_t entry_d,acl_flagset_t * flagset_p)121 acl_get_flagset_np(acl_entry_t entry_d, acl_flagset_t *flagset_p)
122 {
123 
124 	if (entry_d == NULL || flagset_p == NULL) {
125 		errno = EINVAL;
126 		return (-1);
127 	}
128 
129 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
130 		errno = EINVAL;
131 		return (-1);
132 	}
133 
134 	*flagset_p = &entry_d->ae_flags;
135 
136 	return (0);
137 }
138 
139 int
acl_set_flagset_np(acl_entry_t entry_d,acl_flagset_t flagset_d)140 acl_set_flagset_np(acl_entry_t entry_d, acl_flagset_t flagset_d)
141 {
142 
143 	if (entry_d == NULL) {
144 		errno = EINVAL;
145 		return (-1);
146 	}
147 
148 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
149 		errno = EINVAL;
150 		return (-1);
151 	}
152 
153 	_entry_brand_as(entry_d, ACL_BRAND_NFS4);
154 
155 	if (_flag_is_invalid(*flagset_d))
156 		return (-1);
157 
158 	entry_d->ae_flags = *flagset_d;
159 
160 	return (0);
161 }
162