xref: /freebsd/lib/libc/posix1e/acl_branding.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 <assert.h>
30 #include <errno.h>
31 #include <sys/acl.h>
32 
33 #include "acl_support.h"
34 
35 /*
36  * An ugly detail of the implementation - fortunately not visible
37  * to the API users - is the "branding": libc needs to keep track
38  * of what "brand" ACL is: NFSv4, POSIX.1e or unknown.  It happens
39  * automatically - for example, during acl_get_file(3) ACL gets
40  * branded according to the "type" argument; during acl_set_permset
41  * ACL, if its brand is unknown it gets branded as NFSv4 if any of the
42  * NFSv4 permissions that are not valid for POSIX.1e ACL are set etc.
43  * Branding information is used for printing out the ACL (acl_to_text(3)),
44  * veryfying acl_set_whatever arguments (checking against setting
45  * bits that are valid only for NFSv4 in ACL branded as POSIX.1e) etc.
46  */
47 
48 static acl_t
49 entry2acl(acl_entry_t entry)
50 {
51 	acl_t aclp;
52 
53 	aclp = (acl_t)(((long)entry >> _ACL_T_ALIGNMENT_BITS) << _ACL_T_ALIGNMENT_BITS);
54 
55 	return (aclp);
56 }
57 
58 /*
59  * Return brand of an ACL.
60  */
61 int
62 _acl_brand(const acl_t acl)
63 {
64 
65 	return (acl->ats_brand);
66 }
67 
68 int
69 _entry_brand(const acl_entry_t entry)
70 {
71 
72 	return (_acl_brand(entry2acl(entry)));
73 }
74 
75 /*
76  * Return 1, iff branding ACL as "brand" is ok.
77  */
78 int
79 _acl_brand_may_be(const acl_t acl, int brand)
80 {
81 
82 	if (_acl_brand(acl) == ACL_BRAND_UNKNOWN)
83 		return (1);
84 
85 	if (_acl_brand(acl) == brand)
86 		return (1);
87 
88 	return (0);
89 }
90 
91 int
92 _entry_brand_may_be(const acl_entry_t entry, int brand)
93 {
94 
95 	return (_acl_brand_may_be(entry2acl(entry), brand));
96 }
97 
98 /*
99  * Brand ACL as "brand".
100  */
101 void
102 _acl_brand_as(acl_t acl, int brand)
103 {
104 
105 	assert(_acl_brand_may_be(acl, brand));
106 
107 	acl->ats_brand = brand;
108 }
109 
110 void
111 _entry_brand_as(const acl_entry_t entry, int brand)
112 {
113 
114 	_acl_brand_as(entry2acl(entry), brand);
115 }
116 
117 int
118 _acl_type_not_valid_for_acl(const acl_t acl, acl_type_t type)
119 {
120 
121 	switch (_acl_brand(acl)) {
122 	case ACL_BRAND_NFS4:
123 		if (type == ACL_TYPE_NFS4)
124 			return (0);
125 		break;
126 
127 	case ACL_BRAND_POSIX:
128 		if (type == ACL_TYPE_ACCESS || type == ACL_TYPE_DEFAULT)
129 			return (0);
130 		break;
131 
132 	case ACL_BRAND_UNKNOWN:
133 		return (0);
134 	}
135 
136 	return (-1);
137 }
138 
139 void
140 _acl_brand_from_type(acl_t acl, acl_type_t type)
141 {
142 
143 	switch (type) {
144 	case ACL_TYPE_NFS4:
145 		_acl_brand_as(acl, ACL_BRAND_NFS4);
146 		break;
147 	case ACL_TYPE_ACCESS:
148 	case ACL_TYPE_DEFAULT:
149 		_acl_brand_as(acl, ACL_BRAND_POSIX);
150 		break;
151 	default:
152 		/* XXX: What to do here? */
153 		break;
154 	}
155 }
156 
157 int
158 acl_get_brand_np(acl_t acl, int *brand_p)
159 {
160 
161 	if (acl == NULL || brand_p == NULL) {
162 		errno = EINVAL;
163 		return (-1);
164 	}
165 	*brand_p = _acl_brand(acl);
166 
167 	return (0);
168 }
169