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