1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Chris D. Faulhaber
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 #include <errno.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <sys/acl.h>
34 #include <sys/stat.h>
35
36 #include "acl_support.h"
37
38 /*
39 * These routines from sys/kern/subr_acl_nfs4.c are used by both kernel
40 * and libc.
41 */
42 void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp);
43 void acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int file_owner_id,
44 int canonical_six);
45
46 static acl_t
_nfs4_acl_strip_np(const acl_t aclp,int canonical_six)47 _nfs4_acl_strip_np(const acl_t aclp, int canonical_six)
48 {
49 acl_t newacl;
50 mode_t mode = 0;
51
52 newacl = acl_init(ACL_MAX_ENTRIES);
53 if (newacl == NULL) {
54 errno = ENOMEM;
55 return (NULL);
56 }
57
58 _acl_brand_as(newacl, ACL_BRAND_NFS4);
59
60 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl));
61 acl_nfs4_trivial_from_mode_libc(&(newacl->ats_acl), mode, canonical_six);
62
63 return (newacl);
64 }
65
66 static acl_t
_posix1e_acl_strip_np(const acl_t aclp,int recalculate_mask)67 _posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask)
68 {
69 acl_t acl_new, acl_old;
70 acl_entry_t entry, entry_new;
71 acl_tag_t tag;
72 int entry_id, have_mask_entry;
73
74 assert(_acl_brand(aclp) == ACL_BRAND_POSIX);
75
76 acl_old = acl_dup(aclp);
77 if (acl_old == NULL)
78 return (NULL);
79
80 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX);
81
82 have_mask_entry = 0;
83 acl_new = acl_init(ACL_MAX_ENTRIES);
84 if (acl_new == NULL) {
85 acl_free(acl_old);
86 return (NULL);
87 }
88 tag = ACL_UNDEFINED_TAG;
89
90 /* only save the default user/group/other entries */
91 entry_id = ACL_FIRST_ENTRY;
92 while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
93 entry_id = ACL_NEXT_ENTRY;
94
95 assert(_entry_brand(entry) == ACL_BRAND_POSIX);
96
97 if (acl_get_tag_type(entry, &tag) == -1)
98 goto fail;
99
100 switch(tag) {
101 case ACL_USER_OBJ:
102 case ACL_GROUP_OBJ:
103 case ACL_OTHER:
104 if (acl_create_entry(&acl_new, &entry_new) == -1)
105 goto fail;
106 if (acl_copy_entry(entry_new, entry) == -1)
107 goto fail;
108 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX);
109 break;
110 case ACL_MASK:
111 have_mask_entry = 1;
112 break;
113 default:
114 break;
115 }
116 }
117
118 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX);
119
120 if (have_mask_entry && recalculate_mask) {
121 if (acl_calc_mask(&acl_new) == -1)
122 goto fail;
123 }
124
125 return (acl_new);
126
127 fail:
128 acl_free(acl_new);
129 acl_free(acl_old);
130
131 return (NULL);
132 }
133
134 acl_t
acl_strip_np(const acl_t aclp,int recalculate_mask)135 acl_strip_np(const acl_t aclp, int recalculate_mask)
136 {
137 switch (_acl_brand(aclp)) {
138 case ACL_BRAND_NFS4:
139 return (_nfs4_acl_strip_np(aclp, 0));
140
141 case ACL_BRAND_POSIX:
142 return (_posix1e_acl_strip_np(aclp, recalculate_mask));
143
144 default:
145 errno = EINVAL;
146 return (NULL);
147 }
148 }
149
150 /*
151 * Return 1, if ACL is trivial, 0 otherwise.
152 *
153 * ACL is trivial, iff its meaning could be fully expressed using just file
154 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right
155 * of the mode bits in "ls -l" output ;-)
156 */
157 int
acl_is_trivial_np(const acl_t aclp,int * trivialp)158 acl_is_trivial_np(const acl_t aclp, int *trivialp)
159 {
160 acl_t tmpacl;
161 int differs;
162
163 if (aclp == NULL || trivialp == NULL) {
164 errno = EINVAL;
165 return (-1);
166 }
167
168 switch (_acl_brand(aclp)) {
169 case ACL_BRAND_POSIX:
170 if (aclp->ats_acl.acl_cnt == 3)
171 *trivialp = 1;
172 else
173 *trivialp = 0;
174
175 return (0);
176
177 case ACL_BRAND_NFS4:
178 /*
179 * If the ACL has more than canonical six entries,
180 * it's non trivial by definition.
181 */
182 if (aclp->ats_acl.acl_cnt > 6) {
183 *trivialp = 0;
184 return (0);
185 }
186
187 /*
188 * Calculate trivial ACL - using acl_strip_np(3) - and compare
189 * with the original.
190 */
191 tmpacl = _nfs4_acl_strip_np(aclp, 0);
192 if (tmpacl == NULL)
193 return (-1);
194
195 differs = _acl_differs(aclp, tmpacl);
196 acl_free(tmpacl);
197
198 if (differs == 0) {
199 *trivialp = 1;
200 return (0);
201 }
202
203 /*
204 * Try again with an old-style, "canonical six" trivial ACL.
205 */
206 tmpacl = _nfs4_acl_strip_np(aclp, 1);
207 if (tmpacl == NULL)
208 return (-1);
209
210 differs = _acl_differs(aclp, tmpacl);
211 acl_free(tmpacl);
212
213 if (differs)
214 *trivialp = 0;
215 else
216 *trivialp = 1;
217
218 return (0);
219
220 default:
221 errno = EINVAL;
222 return (-1);
223 }
224 }
225