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