xref: /freebsd/lib/libc/posix1e/acl_branding.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <assert.h>
31 #include <errno.h>
32 #include <sys/acl.h>
33 
34 #include "acl_support.h"
35 
36 /*
37  * An ugly detail of the implementation - fortunately not visible
38  * to the API users - is the "branding": libc needs to keep track
39  * of what "brand" ACL is: NFSv4, POSIX.1e or unknown.  It happens
40  * automatically - for example, during acl_get_file(3) ACL gets
41  * branded according to the "type" argument; during acl_set_permset
42  * ACL, if its brand is unknown it gets branded as NFSv4 if any of the
43  * NFSv4 permissions that are not valid for POSIX.1e ACL are set etc.
44  * Branding information is used for printing out the ACL (acl_to_text(3)),
45  * veryfying acl_set_whatever arguments (checking against setting
46  * bits that are valid only for NFSv4 in ACL branded as POSIX.1e) etc.
47  */
48 
49 static acl_t
50 entry2acl(acl_entry_t entry)
51 {
52 	acl_t aclp;
53 
54 	aclp = (acl_t)(((long)entry >> _ACL_T_ALIGNMENT_BITS) << _ACL_T_ALIGNMENT_BITS);
55 
56 	return (aclp);
57 }
58 
59 /*
60  * Return brand of an ACL.
61  */
62 int
63 _acl_brand(const acl_t acl)
64 {
65 
66 	return (acl->ats_brand);
67 }
68 
69 int
70 _entry_brand(const acl_entry_t entry)
71 {
72 
73 	return (_acl_brand(entry2acl(entry)));
74 }
75 
76 /*
77  * Return 1, iff branding ACL as "brand" is ok.
78  */
79 int
80 _acl_brand_may_be(const acl_t acl, int brand)
81 {
82 
83 	if (_acl_brand(acl) == ACL_BRAND_UNKNOWN)
84 		return (1);
85 
86 	if (_acl_brand(acl) == brand)
87 		return (1);
88 
89 	return (0);
90 }
91 
92 int
93 _entry_brand_may_be(const acl_entry_t entry, int brand)
94 {
95 
96 	return (_acl_brand_may_be(entry2acl(entry), brand));
97 }
98 
99 /*
100  * Brand ACL as "brand".
101  */
102 void
103 _acl_brand_as(acl_t acl, int brand)
104 {
105 
106 	assert(_acl_brand_may_be(acl, brand));
107 
108 	acl->ats_brand = brand;
109 }
110 
111 void
112 _entry_brand_as(const acl_entry_t entry, int brand)
113 {
114 
115 	_acl_brand_as(entry2acl(entry), brand);
116 }
117 
118 int
119 _acl_type_not_valid_for_acl(const acl_t acl, acl_type_t type)
120 {
121 
122 	switch (_acl_brand(acl)) {
123 	case ACL_BRAND_NFS4:
124 		if (type == ACL_TYPE_NFS4)
125 			return (0);
126 		break;
127 
128 	case ACL_BRAND_POSIX:
129 		if (type == ACL_TYPE_ACCESS || type == ACL_TYPE_DEFAULT)
130 			return (0);
131 		break;
132 
133 	case ACL_BRAND_UNKNOWN:
134 		return (0);
135 	}
136 
137 	return (-1);
138 }
139 
140 void
141 _acl_brand_from_type(acl_t acl, acl_type_t type)
142 {
143 
144 	switch (type) {
145 	case ACL_TYPE_NFS4:
146 		_acl_brand_as(acl, ACL_BRAND_NFS4);
147 		break;
148 	case ACL_TYPE_ACCESS:
149 	case ACL_TYPE_DEFAULT:
150 		_acl_brand_as(acl, ACL_BRAND_POSIX);
151 		break;
152 	default:
153 		/* XXX: What to do here? */
154 		break;
155 	}
156 }
157 
158 int
159 acl_get_brand_np(acl_t acl, int *brand_p)
160 {
161 
162 	if (acl == NULL || brand_p == NULL) {
163 		errno = EINVAL;
164 		return (-1);
165 	}
166 	*brand_p = _acl_brand(acl);
167 
168 	return (0);
169 }
170