1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5f48205beScasper * Common Development and Distribution License (the "License").
6f48205beScasper * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
2227dd1e87SMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*7206bf49SGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
24fa9e4066Sahrens */
25fa9e4066Sahrens
26fa9e4066Sahrens #include <sys/types.h>
27fa9e4066Sahrens #include <sys/stat.h>
28*7206bf49SGordon Ross #include <sys/errno.h>
29da6c28aaSamw #include <sys/avl.h>
30*7206bf49SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
31*7206bf49SGordon Ross #include <sys/debug.h>
32*7206bf49SGordon Ross #include <sys/kmem.h>
33fa9e4066Sahrens #include <sys/systm.h>
34da6c28aaSamw #include <sys/sysmacros.h>
35da6c28aaSamw #include <acl/acl_common.h>
36fa9e4066Sahrens #else
37fa9e4066Sahrens #include <errno.h>
38fa9e4066Sahrens #include <stdlib.h>
39da6c28aaSamw #include <stddef.h>
40fa9e4066Sahrens #include <strings.h>
41da6c28aaSamw #include <unistd.h>
42fa9e4066Sahrens #include <assert.h>
43da6c28aaSamw #include <grp.h>
44da6c28aaSamw #include <pwd.h>
45da6c28aaSamw #include <acl_common.h>
46fa9e4066Sahrens #define ASSERT assert
47fa9e4066Sahrens #endif
48fa9e4066Sahrens
49da6c28aaSamw #define ACE_POSIX_SUPPORTED_BITS (ACE_READ_DATA | \
50da6c28aaSamw ACE_WRITE_DATA | ACE_APPEND_DATA | ACE_EXECUTE | \
51da6c28aaSamw ACE_READ_ATTRIBUTES | ACE_READ_ACL | ACE_WRITE_ACL)
52da6c28aaSamw
53da6c28aaSamw
54da6c28aaSamw #define ACL_SYNCHRONIZE_SET_DENY 0x0000001
55da6c28aaSamw #define ACL_SYNCHRONIZE_SET_ALLOW 0x0000002
56da6c28aaSamw #define ACL_SYNCHRONIZE_ERR_DENY 0x0000004
57da6c28aaSamw #define ACL_SYNCHRONIZE_ERR_ALLOW 0x0000008
58da6c28aaSamw
59da6c28aaSamw #define ACL_WRITE_OWNER_SET_DENY 0x0000010
60da6c28aaSamw #define ACL_WRITE_OWNER_SET_ALLOW 0x0000020
61da6c28aaSamw #define ACL_WRITE_OWNER_ERR_DENY 0x0000040
62da6c28aaSamw #define ACL_WRITE_OWNER_ERR_ALLOW 0x0000080
63da6c28aaSamw
64da6c28aaSamw #define ACL_DELETE_SET_DENY 0x0000100
65da6c28aaSamw #define ACL_DELETE_SET_ALLOW 0x0000200
66da6c28aaSamw #define ACL_DELETE_ERR_DENY 0x0000400
67da6c28aaSamw #define ACL_DELETE_ERR_ALLOW 0x0000800
68da6c28aaSamw
69da6c28aaSamw #define ACL_WRITE_ATTRS_OWNER_SET_DENY 0x0001000
70da6c28aaSamw #define ACL_WRITE_ATTRS_OWNER_SET_ALLOW 0x0002000
71da6c28aaSamw #define ACL_WRITE_ATTRS_OWNER_ERR_DENY 0x0004000
72da6c28aaSamw #define ACL_WRITE_ATTRS_OWNER_ERR_ALLOW 0x0008000
73da6c28aaSamw
74da6c28aaSamw #define ACL_WRITE_ATTRS_WRITER_SET_DENY 0x0010000
75da6c28aaSamw #define ACL_WRITE_ATTRS_WRITER_SET_ALLOW 0x0020000
76da6c28aaSamw #define ACL_WRITE_ATTRS_WRITER_ERR_DENY 0x0040000
77da6c28aaSamw #define ACL_WRITE_ATTRS_WRITER_ERR_ALLOW 0x0080000
78da6c28aaSamw
79da6c28aaSamw #define ACL_WRITE_NAMED_WRITER_SET_DENY 0x0100000
80da6c28aaSamw #define ACL_WRITE_NAMED_WRITER_SET_ALLOW 0x0200000
81da6c28aaSamw #define ACL_WRITE_NAMED_WRITER_ERR_DENY 0x0400000
82da6c28aaSamw #define ACL_WRITE_NAMED_WRITER_ERR_ALLOW 0x0800000
83da6c28aaSamw
84da6c28aaSamw #define ACL_READ_NAMED_READER_SET_DENY 0x1000000
85da6c28aaSamw #define ACL_READ_NAMED_READER_SET_ALLOW 0x2000000
86da6c28aaSamw #define ACL_READ_NAMED_READER_ERR_DENY 0x4000000
87da6c28aaSamw #define ACL_READ_NAMED_READER_ERR_ALLOW 0x8000000
88da6c28aaSamw
89da6c28aaSamw
90da6c28aaSamw #define ACE_VALID_MASK_BITS (\
91da6c28aaSamw ACE_READ_DATA | \
92da6c28aaSamw ACE_LIST_DIRECTORY | \
93da6c28aaSamw ACE_WRITE_DATA | \
94da6c28aaSamw ACE_ADD_FILE | \
95da6c28aaSamw ACE_APPEND_DATA | \
96da6c28aaSamw ACE_ADD_SUBDIRECTORY | \
97da6c28aaSamw ACE_READ_NAMED_ATTRS | \
98da6c28aaSamw ACE_WRITE_NAMED_ATTRS | \
99da6c28aaSamw ACE_EXECUTE | \
100da6c28aaSamw ACE_DELETE_CHILD | \
101da6c28aaSamw ACE_READ_ATTRIBUTES | \
102da6c28aaSamw ACE_WRITE_ATTRIBUTES | \
103da6c28aaSamw ACE_DELETE | \
104da6c28aaSamw ACE_READ_ACL | \
105da6c28aaSamw ACE_WRITE_ACL | \
106da6c28aaSamw ACE_WRITE_OWNER | \
107da6c28aaSamw ACE_SYNCHRONIZE)
108da6c28aaSamw
109da6c28aaSamw #define ACE_MASK_UNDEFINED 0x80000000
110da6c28aaSamw
111da6c28aaSamw #define ACE_VALID_FLAG_BITS (ACE_FILE_INHERIT_ACE | \
112da6c28aaSamw ACE_DIRECTORY_INHERIT_ACE | \
113da6c28aaSamw ACE_NO_PROPAGATE_INHERIT_ACE | ACE_INHERIT_ONLY_ACE | \
114da6c28aaSamw ACE_SUCCESSFUL_ACCESS_ACE_FLAG | ACE_FAILED_ACCESS_ACE_FLAG | \
115da6c28aaSamw ACE_IDENTIFIER_GROUP | ACE_OWNER | ACE_GROUP | ACE_EVERYONE)
116da6c28aaSamw
117da6c28aaSamw /*
118da6c28aaSamw * ACL conversion helpers
119da6c28aaSamw */
120da6c28aaSamw
121da6c28aaSamw typedef enum {
122da6c28aaSamw ace_unused,
123da6c28aaSamw ace_user_obj,
124da6c28aaSamw ace_user,
125da6c28aaSamw ace_group, /* includes GROUP and GROUP_OBJ */
126da6c28aaSamw ace_other_obj
127da6c28aaSamw } ace_to_aent_state_t;
128da6c28aaSamw
129da6c28aaSamw typedef struct acevals {
130da6c28aaSamw uid_t key;
131da6c28aaSamw avl_node_t avl;
132da6c28aaSamw uint32_t mask;
133da6c28aaSamw uint32_t allowed;
134da6c28aaSamw uint32_t denied;
135da6c28aaSamw int aent_type;
136da6c28aaSamw } acevals_t;
137da6c28aaSamw
138da6c28aaSamw typedef struct ace_list {
139da6c28aaSamw acevals_t user_obj;
140da6c28aaSamw avl_tree_t user;
141da6c28aaSamw int numusers;
142da6c28aaSamw acevals_t group_obj;
143da6c28aaSamw avl_tree_t group;
144da6c28aaSamw int numgroups;
145da6c28aaSamw acevals_t other_obj;
146da6c28aaSamw uint32_t acl_mask;
147da6c28aaSamw int hasmask;
148da6c28aaSamw int dfacl_flag;
149da6c28aaSamw ace_to_aent_state_t state;
150da6c28aaSamw int seen; /* bitmask of all aclent_t a_type values seen */
151da6c28aaSamw } ace_list_t;
152fa9e4066Sahrens
153fa9e4066Sahrens /*
154fa9e4066Sahrens * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
155fa9e4066Sahrens * v = Ptr to array/vector of objs
156fa9e4066Sahrens * n = # objs in the array
157fa9e4066Sahrens * s = size of each obj (must be multiples of a word size)
158fa9e4066Sahrens * f = ptr to function to compare two objs
159fa9e4066Sahrens * returns (-1 = less than, 0 = equal, 1 = greater than
160fa9e4066Sahrens */
161fa9e4066Sahrens void
ksort(caddr_t v,int n,int s,int (* f)())162fa9e4066Sahrens ksort(caddr_t v, int n, int s, int (*f)())
163fa9e4066Sahrens {
164fa9e4066Sahrens int g, i, j, ii;
165fa9e4066Sahrens unsigned int *p1, *p2;
166fa9e4066Sahrens unsigned int tmp;
167fa9e4066Sahrens
168fa9e4066Sahrens /* No work to do */
169fa9e4066Sahrens if (v == NULL || n <= 1)
170fa9e4066Sahrens return;
171fa9e4066Sahrens
172fa9e4066Sahrens /* Sanity check on arguments */
173fa9e4066Sahrens ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
174fa9e4066Sahrens ASSERT(s > 0);
175fa9e4066Sahrens for (g = n / 2; g > 0; g /= 2) {
176fa9e4066Sahrens for (i = g; i < n; i++) {
177fa9e4066Sahrens for (j = i - g; j >= 0 &&
178fa9e4066Sahrens (*f)(v + j * s, v + (j + g) * s) == 1;
179fa9e4066Sahrens j -= g) {
180fa9e4066Sahrens p1 = (void *)(v + j * s);
181fa9e4066Sahrens p2 = (void *)(v + (j + g) * s);
182fa9e4066Sahrens for (ii = 0; ii < s / 4; ii++) {
183fa9e4066Sahrens tmp = *p1;
184fa9e4066Sahrens *p1++ = *p2;
185fa9e4066Sahrens *p2++ = tmp;
186fa9e4066Sahrens }
187fa9e4066Sahrens }
188fa9e4066Sahrens }
189fa9e4066Sahrens }
190fa9e4066Sahrens }
191fa9e4066Sahrens
192fa9e4066Sahrens /*
193fa9e4066Sahrens * Compare two acls, all fields. Returns:
194fa9e4066Sahrens * -1 (less than)
195fa9e4066Sahrens * 0 (equal)
196fa9e4066Sahrens * +1 (greater than)
197fa9e4066Sahrens */
198fa9e4066Sahrens int
cmp2acls(void * a,void * b)199fa9e4066Sahrens cmp2acls(void *a, void *b)
200fa9e4066Sahrens {
201fa9e4066Sahrens aclent_t *x = (aclent_t *)a;
202fa9e4066Sahrens aclent_t *y = (aclent_t *)b;
203fa9e4066Sahrens
204fa9e4066Sahrens /* Compare types */
205fa9e4066Sahrens if (x->a_type < y->a_type)
206fa9e4066Sahrens return (-1);
207fa9e4066Sahrens if (x->a_type > y->a_type)
208fa9e4066Sahrens return (1);
209fa9e4066Sahrens /* Equal types; compare id's */
210fa9e4066Sahrens if (x->a_id < y->a_id)
211fa9e4066Sahrens return (-1);
212fa9e4066Sahrens if (x->a_id > y->a_id)
213fa9e4066Sahrens return (1);
214fa9e4066Sahrens /* Equal ids; compare perms */
215fa9e4066Sahrens if (x->a_perm < y->a_perm)
216fa9e4066Sahrens return (-1);
217fa9e4066Sahrens if (x->a_perm > y->a_perm)
218fa9e4066Sahrens return (1);
219fa9e4066Sahrens /* Totally equal */
220fa9e4066Sahrens return (0);
221fa9e4066Sahrens }
222da6c28aaSamw
223da6c28aaSamw /*ARGSUSED*/
224da6c28aaSamw static void *
cacl_realloc(void * ptr,size_t size,size_t new_size)225da6c28aaSamw cacl_realloc(void *ptr, size_t size, size_t new_size)
226da6c28aaSamw {
227*7206bf49SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
228da6c28aaSamw void *tmp;
229da6c28aaSamw
230da6c28aaSamw tmp = kmem_alloc(new_size, KM_SLEEP);
231da6c28aaSamw (void) memcpy(tmp, ptr, (size < new_size) ? size : new_size);
232da6c28aaSamw kmem_free(ptr, size);
233da6c28aaSamw return (tmp);
234da6c28aaSamw #else
235da6c28aaSamw return (realloc(ptr, new_size));
236da6c28aaSamw #endif
237da6c28aaSamw }
238da6c28aaSamw
239da6c28aaSamw static int
cacl_malloc(void ** ptr,size_t size)240da6c28aaSamw cacl_malloc(void **ptr, size_t size)
241da6c28aaSamw {
242*7206bf49SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
243da6c28aaSamw *ptr = kmem_zalloc(size, KM_SLEEP);
244da6c28aaSamw return (0);
245da6c28aaSamw #else
246da6c28aaSamw *ptr = calloc(1, size);
247da6c28aaSamw if (*ptr == NULL)
248da6c28aaSamw return (errno);
249da6c28aaSamw
250da6c28aaSamw return (0);
251da6c28aaSamw #endif
252da6c28aaSamw }
253da6c28aaSamw
254da6c28aaSamw /*ARGSUSED*/
255da6c28aaSamw static void
cacl_free(void * ptr,size_t size)256da6c28aaSamw cacl_free(void *ptr, size_t size)
257da6c28aaSamw {
258*7206bf49SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
259da6c28aaSamw kmem_free(ptr, size);
260da6c28aaSamw #else
261da6c28aaSamw free(ptr);
262da6c28aaSamw #endif
263da6c28aaSamw }
264da6c28aaSamw
265da6c28aaSamw acl_t *
acl_alloc(enum acl_type type)266da6c28aaSamw acl_alloc(enum acl_type type)
267da6c28aaSamw {
268da6c28aaSamw acl_t *aclp;
269da6c28aaSamw
270da6c28aaSamw if (cacl_malloc((void **)&aclp, sizeof (acl_t)) != 0)
271da6c28aaSamw return (NULL);
272da6c28aaSamw
273da6c28aaSamw aclp->acl_aclp = NULL;
274da6c28aaSamw aclp->acl_cnt = 0;
275da6c28aaSamw
276da6c28aaSamw switch (type) {
277da6c28aaSamw case ACE_T:
278da6c28aaSamw aclp->acl_type = ACE_T;
279da6c28aaSamw aclp->acl_entry_size = sizeof (ace_t);
280da6c28aaSamw break;
281da6c28aaSamw case ACLENT_T:
282da6c28aaSamw aclp->acl_type = ACLENT_T;
283da6c28aaSamw aclp->acl_entry_size = sizeof (aclent_t);
284da6c28aaSamw break;
285da6c28aaSamw default:
286da6c28aaSamw acl_free(aclp);
287da6c28aaSamw aclp = NULL;
288da6c28aaSamw }
289da6c28aaSamw return (aclp);
290da6c28aaSamw }
291da6c28aaSamw
292da6c28aaSamw /*
293da6c28aaSamw * Free acl_t structure
294da6c28aaSamw */
295da6c28aaSamw void
acl_free(acl_t * aclp)296da6c28aaSamw acl_free(acl_t *aclp)
297da6c28aaSamw {
298da6c28aaSamw int acl_size;
299da6c28aaSamw
300da6c28aaSamw if (aclp == NULL)
301da6c28aaSamw return;
302da6c28aaSamw
303da6c28aaSamw if (aclp->acl_aclp) {
304da6c28aaSamw acl_size = aclp->acl_cnt * aclp->acl_entry_size;
305da6c28aaSamw cacl_free(aclp->acl_aclp, acl_size);
306da6c28aaSamw }
307da6c28aaSamw
308da6c28aaSamw cacl_free(aclp, sizeof (acl_t));
309da6c28aaSamw }
310da6c28aaSamw
311da6c28aaSamw static uint32_t
access_mask_set(int haswriteperm,int hasreadperm,int isowner,int isallow)312da6c28aaSamw access_mask_set(int haswriteperm, int hasreadperm, int isowner, int isallow)
313da6c28aaSamw {
314da6c28aaSamw uint32_t access_mask = 0;
315da6c28aaSamw int acl_produce;
316da6c28aaSamw int synchronize_set = 0, write_owner_set = 0;
317da6c28aaSamw int delete_set = 0, write_attrs_set = 0;
318da6c28aaSamw int read_named_set = 0, write_named_set = 0;
319da6c28aaSamw
320da6c28aaSamw acl_produce = (ACL_SYNCHRONIZE_SET_ALLOW |
321da6c28aaSamw ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
322da6c28aaSamw ACL_WRITE_ATTRS_WRITER_SET_DENY);
323da6c28aaSamw
324da6c28aaSamw if (isallow) {
325da6c28aaSamw synchronize_set = ACL_SYNCHRONIZE_SET_ALLOW;
326da6c28aaSamw write_owner_set = ACL_WRITE_OWNER_SET_ALLOW;
327da6c28aaSamw delete_set = ACL_DELETE_SET_ALLOW;
328da6c28aaSamw if (hasreadperm)
329da6c28aaSamw read_named_set = ACL_READ_NAMED_READER_SET_ALLOW;
330da6c28aaSamw if (haswriteperm)
331da6c28aaSamw write_named_set = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
332da6c28aaSamw if (isowner)
333da6c28aaSamw write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
334da6c28aaSamw else if (haswriteperm)
335da6c28aaSamw write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
336da6c28aaSamw } else {
337da6c28aaSamw
338da6c28aaSamw synchronize_set = ACL_SYNCHRONIZE_SET_DENY;
339da6c28aaSamw write_owner_set = ACL_WRITE_OWNER_SET_DENY;
340da6c28aaSamw delete_set = ACL_DELETE_SET_DENY;
341da6c28aaSamw if (hasreadperm)
342da6c28aaSamw read_named_set = ACL_READ_NAMED_READER_SET_DENY;
343da6c28aaSamw if (haswriteperm)
344da6c28aaSamw write_named_set = ACL_WRITE_NAMED_WRITER_SET_DENY;
345da6c28aaSamw if (isowner)
346da6c28aaSamw write_attrs_set = ACL_WRITE_ATTRS_OWNER_SET_DENY;
347da6c28aaSamw else if (haswriteperm)
348da6c28aaSamw write_attrs_set = ACL_WRITE_ATTRS_WRITER_SET_DENY;
349da6c28aaSamw else
350da6c28aaSamw /*
351da6c28aaSamw * If the entity is not the owner and does not
352da6c28aaSamw * have write permissions ACE_WRITE_ATTRIBUTES will
353da6c28aaSamw * always go in the DENY ACE.
354da6c28aaSamw */
355da6c28aaSamw access_mask |= ACE_WRITE_ATTRIBUTES;
356da6c28aaSamw }
357da6c28aaSamw
358da6c28aaSamw if (acl_produce & synchronize_set)
359da6c28aaSamw access_mask |= ACE_SYNCHRONIZE;
360da6c28aaSamw if (acl_produce & write_owner_set)
361da6c28aaSamw access_mask |= ACE_WRITE_OWNER;
362da6c28aaSamw if (acl_produce & delete_set)
363da6c28aaSamw access_mask |= ACE_DELETE;
364da6c28aaSamw if (acl_produce & write_attrs_set)
365da6c28aaSamw access_mask |= ACE_WRITE_ATTRIBUTES;
366da6c28aaSamw if (acl_produce & read_named_set)
367da6c28aaSamw access_mask |= ACE_READ_NAMED_ATTRS;
368da6c28aaSamw if (acl_produce & write_named_set)
369da6c28aaSamw access_mask |= ACE_WRITE_NAMED_ATTRS;
370da6c28aaSamw
371da6c28aaSamw return (access_mask);
372da6c28aaSamw }
373da6c28aaSamw
374da6c28aaSamw /*
375da6c28aaSamw * Given an mode_t, convert it into an access_mask as used
376da6c28aaSamw * by nfsace, assuming aclent_t -> nfsace semantics.
377da6c28aaSamw */
378da6c28aaSamw static uint32_t
mode_to_ace_access(mode_t mode,boolean_t isdir,int isowner,int isallow)379a3c49ce1SAlbert Lee mode_to_ace_access(mode_t mode, boolean_t isdir, int isowner, int isallow)
380da6c28aaSamw {
381da6c28aaSamw uint32_t access = 0;
382da6c28aaSamw int haswriteperm = 0;
383da6c28aaSamw int hasreadperm = 0;
384da6c28aaSamw
385da6c28aaSamw if (isallow) {
386da6c28aaSamw haswriteperm = (mode & S_IWOTH);
387da6c28aaSamw hasreadperm = (mode & S_IROTH);
388da6c28aaSamw } else {
389da6c28aaSamw haswriteperm = !(mode & S_IWOTH);
390da6c28aaSamw hasreadperm = !(mode & S_IROTH);
391da6c28aaSamw }
392da6c28aaSamw
393da6c28aaSamw /*
394da6c28aaSamw * The following call takes care of correctly setting the following
395da6c28aaSamw * mask bits in the access_mask:
396da6c28aaSamw * ACE_SYNCHRONIZE, ACE_WRITE_OWNER, ACE_DELETE,
397da6c28aaSamw * ACE_WRITE_ATTRIBUTES, ACE_WRITE_NAMED_ATTRS, ACE_READ_NAMED_ATTRS
398da6c28aaSamw */
399da6c28aaSamw access = access_mask_set(haswriteperm, hasreadperm, isowner, isallow);
400da6c28aaSamw
401da6c28aaSamw if (isallow) {
402da6c28aaSamw access |= ACE_READ_ACL | ACE_READ_ATTRIBUTES;
403da6c28aaSamw if (isowner)
404da6c28aaSamw access |= ACE_WRITE_ACL;
405da6c28aaSamw } else {
406da6c28aaSamw if (! isowner)
407da6c28aaSamw access |= ACE_WRITE_ACL;
408da6c28aaSamw }
409da6c28aaSamw
410da6c28aaSamw /* read */
411da6c28aaSamw if (mode & S_IROTH) {
412da6c28aaSamw access |= ACE_READ_DATA;
413da6c28aaSamw }
414da6c28aaSamw /* write */
415da6c28aaSamw if (mode & S_IWOTH) {
416da6c28aaSamw access |= ACE_WRITE_DATA |
417da6c28aaSamw ACE_APPEND_DATA;
418da6c28aaSamw if (isdir)
419da6c28aaSamw access |= ACE_DELETE_CHILD;
420da6c28aaSamw }
421da6c28aaSamw /* exec */
422a3c49ce1SAlbert Lee if (mode & S_IXOTH) {
423da6c28aaSamw access |= ACE_EXECUTE;
424da6c28aaSamw }
425da6c28aaSamw
426da6c28aaSamw return (access);
427da6c28aaSamw }
428da6c28aaSamw
429da6c28aaSamw /*
430da6c28aaSamw * Given an nfsace (presumably an ALLOW entry), make a
431da6c28aaSamw * corresponding DENY entry at the address given.
432da6c28aaSamw */
433da6c28aaSamw static void
ace_make_deny(ace_t * allow,ace_t * deny,int isdir,int isowner)434da6c28aaSamw ace_make_deny(ace_t *allow, ace_t *deny, int isdir, int isowner)
435da6c28aaSamw {
436da6c28aaSamw (void) memcpy(deny, allow, sizeof (ace_t));
437da6c28aaSamw
438da6c28aaSamw deny->a_who = allow->a_who;
439da6c28aaSamw
440da6c28aaSamw deny->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
441da6c28aaSamw deny->a_access_mask ^= ACE_POSIX_SUPPORTED_BITS;
442da6c28aaSamw if (isdir)
443da6c28aaSamw deny->a_access_mask ^= ACE_DELETE_CHILD;
444da6c28aaSamw
445da6c28aaSamw deny->a_access_mask &= ~(ACE_SYNCHRONIZE | ACE_WRITE_OWNER |
446da6c28aaSamw ACE_DELETE | ACE_WRITE_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
447da6c28aaSamw ACE_WRITE_NAMED_ATTRS);
448da6c28aaSamw deny->a_access_mask |= access_mask_set((allow->a_access_mask &
449da6c28aaSamw ACE_WRITE_DATA), (allow->a_access_mask & ACE_READ_DATA), isowner,
450da6c28aaSamw B_FALSE);
451da6c28aaSamw }
452da6c28aaSamw /*
453da6c28aaSamw * Make an initial pass over an array of aclent_t's. Gather
454da6c28aaSamw * information such as an ACL_MASK (if any), number of users,
455da6c28aaSamw * number of groups, and whether the array needs to be sorted.
456da6c28aaSamw */
457da6c28aaSamw static int
ln_aent_preprocess(aclent_t * aclent,int n,int * hasmask,mode_t * mask,int * numuser,int * numgroup,int * needsort)458da6c28aaSamw ln_aent_preprocess(aclent_t *aclent, int n,
459da6c28aaSamw int *hasmask, mode_t *mask,
460da6c28aaSamw int *numuser, int *numgroup, int *needsort)
461da6c28aaSamw {
462da6c28aaSamw int error = 0;
463da6c28aaSamw int i;
464da6c28aaSamw int curtype = 0;
465da6c28aaSamw
466da6c28aaSamw *hasmask = 0;
467da6c28aaSamw *mask = 07;
468da6c28aaSamw *needsort = 0;
469da6c28aaSamw *numuser = 0;
470da6c28aaSamw *numgroup = 0;
471da6c28aaSamw
472da6c28aaSamw for (i = 0; i < n; i++) {
473da6c28aaSamw if (aclent[i].a_type < curtype)
474da6c28aaSamw *needsort = 1;
475da6c28aaSamw else if (aclent[i].a_type > curtype)
476da6c28aaSamw curtype = aclent[i].a_type;
477da6c28aaSamw if (aclent[i].a_type & USER)
478da6c28aaSamw (*numuser)++;
479da6c28aaSamw if (aclent[i].a_type & (GROUP | GROUP_OBJ))
480da6c28aaSamw (*numgroup)++;
481da6c28aaSamw if (aclent[i].a_type & CLASS_OBJ) {
482da6c28aaSamw if (*hasmask) {
483da6c28aaSamw error = EINVAL;
484da6c28aaSamw goto out;
485da6c28aaSamw } else {
486da6c28aaSamw *hasmask = 1;
487da6c28aaSamw *mask = aclent[i].a_perm;
488da6c28aaSamw }
489da6c28aaSamw }
490da6c28aaSamw }
491da6c28aaSamw
492da6c28aaSamw if ((! *hasmask) && (*numuser + *numgroup > 1)) {
493da6c28aaSamw error = EINVAL;
494da6c28aaSamw goto out;
495da6c28aaSamw }
496da6c28aaSamw
497da6c28aaSamw out:
498da6c28aaSamw return (error);
499da6c28aaSamw }
500da6c28aaSamw
501da6c28aaSamw /*
502da6c28aaSamw * Convert an array of aclent_t into an array of nfsace entries,
503da6c28aaSamw * following POSIX draft -> nfsv4 conversion semantics as outlined in
504da6c28aaSamw * the IETF draft.
505da6c28aaSamw */
506da6c28aaSamw static int
ln_aent_to_ace(aclent_t * aclent,int n,ace_t ** acepp,int * rescount,int isdir)507da6c28aaSamw ln_aent_to_ace(aclent_t *aclent, int n, ace_t **acepp, int *rescount, int isdir)
508da6c28aaSamw {
509da6c28aaSamw int error = 0;
510da6c28aaSamw mode_t mask;
511da6c28aaSamw int numuser, numgroup, needsort;
512da6c28aaSamw int resultsize = 0;
513da6c28aaSamw int i, groupi = 0, skip;
514da6c28aaSamw ace_t *acep, *result = NULL;
515da6c28aaSamw int hasmask;
516da6c28aaSamw
517da6c28aaSamw error = ln_aent_preprocess(aclent, n, &hasmask, &mask,
518da6c28aaSamw &numuser, &numgroup, &needsort);
519da6c28aaSamw if (error != 0)
520da6c28aaSamw goto out;
521da6c28aaSamw
522da6c28aaSamw /* allow + deny for each aclent */
523da6c28aaSamw resultsize = n * 2;
524da6c28aaSamw if (hasmask) {
525da6c28aaSamw /*
526da6c28aaSamw * stick extra deny on the group_obj and on each
527da6c28aaSamw * user|group for the mask (the group_obj was added
528da6c28aaSamw * into the count for numgroup)
529da6c28aaSamw */
530da6c28aaSamw resultsize += numuser + numgroup;
531da6c28aaSamw /* ... and don't count the mask itself */
532da6c28aaSamw resultsize -= 2;
533da6c28aaSamw }
534da6c28aaSamw
535da6c28aaSamw /* sort the source if necessary */
536da6c28aaSamw if (needsort)
537da6c28aaSamw ksort((caddr_t)aclent, n, sizeof (aclent_t), cmp2acls);
538da6c28aaSamw
539da6c28aaSamw if (cacl_malloc((void **)&result, resultsize * sizeof (ace_t)) != 0)
540da6c28aaSamw goto out;
541da6c28aaSamw
542da6c28aaSamw acep = result;
543da6c28aaSamw
544da6c28aaSamw for (i = 0; i < n; i++) {
545da6c28aaSamw /*
546da6c28aaSamw * don't process CLASS_OBJ (mask); mask was grabbed in
547da6c28aaSamw * ln_aent_preprocess()
548da6c28aaSamw */
549da6c28aaSamw if (aclent[i].a_type & CLASS_OBJ)
550da6c28aaSamw continue;
551da6c28aaSamw
552da6c28aaSamw /* If we need an ACL_MASK emulator, prepend it now */
553da6c28aaSamw if ((hasmask) &&
554da6c28aaSamw (aclent[i].a_type & (USER | GROUP | GROUP_OBJ))) {
555da6c28aaSamw acep->a_type = ACE_ACCESS_DENIED_ACE_TYPE;
556da6c28aaSamw acep->a_flags = 0;
557da6c28aaSamw if (aclent[i].a_type & GROUP_OBJ) {
558da6c28aaSamw acep->a_who = (uid_t)-1;
559da6c28aaSamw acep->a_flags |=
560da6c28aaSamw (ACE_IDENTIFIER_GROUP|ACE_GROUP);
561da6c28aaSamw } else if (aclent[i].a_type & USER) {
562da6c28aaSamw acep->a_who = aclent[i].a_id;
563da6c28aaSamw } else {
564da6c28aaSamw acep->a_who = aclent[i].a_id;
565da6c28aaSamw acep->a_flags |= ACE_IDENTIFIER_GROUP;
566da6c28aaSamw }
567da6c28aaSamw if (aclent[i].a_type & ACL_DEFAULT) {
568da6c28aaSamw acep->a_flags |= ACE_INHERIT_ONLY_ACE |
569da6c28aaSamw ACE_FILE_INHERIT_ACE |
570da6c28aaSamw ACE_DIRECTORY_INHERIT_ACE;
571da6c28aaSamw }
572da6c28aaSamw /*
573da6c28aaSamw * Set the access mask for the prepended deny
574da6c28aaSamw * ace. To do this, we invert the mask (found
575da6c28aaSamw * in ln_aent_preprocess()) then convert it to an
576da6c28aaSamw * DENY ace access_mask.
577da6c28aaSamw */
578da6c28aaSamw acep->a_access_mask = mode_to_ace_access((mask ^ 07),
579da6c28aaSamw isdir, 0, 0);
580da6c28aaSamw acep += 1;
581da6c28aaSamw }
582da6c28aaSamw
583da6c28aaSamw /* handle a_perm -> access_mask */
584da6c28aaSamw acep->a_access_mask = mode_to_ace_access(aclent[i].a_perm,
585da6c28aaSamw isdir, aclent[i].a_type & USER_OBJ, 1);
586da6c28aaSamw
587da6c28aaSamw /* emulate a default aclent */
588da6c28aaSamw if (aclent[i].a_type & ACL_DEFAULT) {
589da6c28aaSamw acep->a_flags |= ACE_INHERIT_ONLY_ACE |
590da6c28aaSamw ACE_FILE_INHERIT_ACE |
591da6c28aaSamw ACE_DIRECTORY_INHERIT_ACE;
592da6c28aaSamw }
593da6c28aaSamw
594da6c28aaSamw /*
595da6c28aaSamw * handle a_perm and a_id
596da6c28aaSamw *
597da6c28aaSamw * this must be done last, since it involves the
598da6c28aaSamw * corresponding deny aces, which are handled
599da6c28aaSamw * differently for each different a_type.
600da6c28aaSamw */
601da6c28aaSamw if (aclent[i].a_type & USER_OBJ) {
602da6c28aaSamw acep->a_who = (uid_t)-1;
603da6c28aaSamw acep->a_flags |= ACE_OWNER;
604da6c28aaSamw ace_make_deny(acep, acep + 1, isdir, B_TRUE);
605da6c28aaSamw acep += 2;
606da6c28aaSamw } else if (aclent[i].a_type & USER) {
607da6c28aaSamw acep->a_who = aclent[i].a_id;
608da6c28aaSamw ace_make_deny(acep, acep + 1, isdir, B_FALSE);
609da6c28aaSamw acep += 2;
610da6c28aaSamw } else if (aclent[i].a_type & (GROUP_OBJ | GROUP)) {
611da6c28aaSamw if (aclent[i].a_type & GROUP_OBJ) {
612da6c28aaSamw acep->a_who = (uid_t)-1;
613da6c28aaSamw acep->a_flags |= ACE_GROUP;
614da6c28aaSamw } else {
615da6c28aaSamw acep->a_who = aclent[i].a_id;
616da6c28aaSamw }
617da6c28aaSamw acep->a_flags |= ACE_IDENTIFIER_GROUP;
618da6c28aaSamw /*
619da6c28aaSamw * Set the corresponding deny for the group ace.
620da6c28aaSamw *
621da6c28aaSamw * The deny aces go after all of the groups, unlike
622da6c28aaSamw * everything else, where they immediately follow
623da6c28aaSamw * the allow ace.
624da6c28aaSamw *
625da6c28aaSamw * We calculate "skip", the number of slots to
626da6c28aaSamw * skip ahead for the deny ace, here.
627da6c28aaSamw *
628da6c28aaSamw * The pattern is:
629da6c28aaSamw * MD1 A1 MD2 A2 MD3 A3 D1 D2 D3
630da6c28aaSamw * thus, skip is
631da6c28aaSamw * (2 * numgroup) - 1 - groupi
632da6c28aaSamw * (2 * numgroup) to account for MD + A
633da6c28aaSamw * - 1 to account for the fact that we're on the
634da6c28aaSamw * access (A), not the mask (MD)
635da6c28aaSamw * - groupi to account for the fact that we have
636da6c28aaSamw * passed up groupi number of MD's.
637da6c28aaSamw */
638da6c28aaSamw skip = (2 * numgroup) - 1 - groupi;
639da6c28aaSamw ace_make_deny(acep, acep + skip, isdir, B_FALSE);
640da6c28aaSamw /*
641da6c28aaSamw * If we just did the last group, skip acep past
642da6c28aaSamw * all of the denies; else, just move ahead one.
643da6c28aaSamw */
644da6c28aaSamw if (++groupi >= numgroup)
645da6c28aaSamw acep += numgroup + 1;
646da6c28aaSamw else
647da6c28aaSamw acep += 1;
648da6c28aaSamw } else if (aclent[i].a_type & OTHER_OBJ) {
649da6c28aaSamw acep->a_who = (uid_t)-1;
650da6c28aaSamw acep->a_flags |= ACE_EVERYONE;
651da6c28aaSamw ace_make_deny(acep, acep + 1, isdir, B_FALSE);
652da6c28aaSamw acep += 2;
653da6c28aaSamw } else {
654da6c28aaSamw error = EINVAL;
655da6c28aaSamw goto out;
656da6c28aaSamw }
657da6c28aaSamw }
658da6c28aaSamw
659da6c28aaSamw *acepp = result;
660da6c28aaSamw *rescount = resultsize;
661da6c28aaSamw
662da6c28aaSamw out:
663da6c28aaSamw if (error != 0) {
664da6c28aaSamw if ((result != NULL) && (resultsize > 0)) {
665da6c28aaSamw cacl_free(result, resultsize * sizeof (ace_t));
666da6c28aaSamw }
667da6c28aaSamw }
668da6c28aaSamw
669da6c28aaSamw return (error);
670da6c28aaSamw }
671da6c28aaSamw
672da6c28aaSamw static int
convert_aent_to_ace(aclent_t * aclentp,int aclcnt,boolean_t isdir,ace_t ** retacep,int * retacecnt)673a3c49ce1SAlbert Lee convert_aent_to_ace(aclent_t *aclentp, int aclcnt, boolean_t isdir,
674da6c28aaSamw ace_t **retacep, int *retacecnt)
675da6c28aaSamw {
676da6c28aaSamw ace_t *acep;
677da6c28aaSamw ace_t *dfacep;
678da6c28aaSamw int acecnt = 0;
679da6c28aaSamw int dfacecnt = 0;
680da6c28aaSamw int dfaclstart = 0;
681da6c28aaSamw int dfaclcnt = 0;
682da6c28aaSamw aclent_t *aclp;
683da6c28aaSamw int i;
684da6c28aaSamw int error;
685da6c28aaSamw int acesz, dfacesz;
686da6c28aaSamw
687da6c28aaSamw ksort((caddr_t)aclentp, aclcnt, sizeof (aclent_t), cmp2acls);
688da6c28aaSamw
689da6c28aaSamw for (i = 0, aclp = aclentp; i < aclcnt; aclp++, i++) {
690da6c28aaSamw if (aclp->a_type & ACL_DEFAULT)
691da6c28aaSamw break;
692da6c28aaSamw }
693da6c28aaSamw
694da6c28aaSamw if (i < aclcnt) {
695da6c28aaSamw dfaclstart = i;
696da6c28aaSamw dfaclcnt = aclcnt - i;
697da6c28aaSamw }
698da6c28aaSamw
699a3c49ce1SAlbert Lee if (dfaclcnt && !isdir) {
700da6c28aaSamw return (EINVAL);
701da6c28aaSamw }
702da6c28aaSamw
703da6c28aaSamw error = ln_aent_to_ace(aclentp, i, &acep, &acecnt, isdir);
704da6c28aaSamw if (error)
705da6c28aaSamw return (error);
706da6c28aaSamw
707da6c28aaSamw if (dfaclcnt) {
708da6c28aaSamw error = ln_aent_to_ace(&aclentp[dfaclstart], dfaclcnt,
709da6c28aaSamw &dfacep, &dfacecnt, isdir);
710da6c28aaSamw if (error) {
711da6c28aaSamw if (acep) {
712da6c28aaSamw cacl_free(acep, acecnt * sizeof (ace_t));
713da6c28aaSamw }
714da6c28aaSamw return (error);
715da6c28aaSamw }
716da6c28aaSamw }
717da6c28aaSamw
718da6c28aaSamw if (dfacecnt != 0) {
719da6c28aaSamw acesz = sizeof (ace_t) * acecnt;
720da6c28aaSamw dfacesz = sizeof (ace_t) * dfacecnt;
721da6c28aaSamw acep = cacl_realloc(acep, acesz, acesz + dfacesz);
722da6c28aaSamw if (acep == NULL)
723da6c28aaSamw return (ENOMEM);
724da6c28aaSamw if (dfaclcnt) {
725da6c28aaSamw (void) memcpy(acep + acecnt, dfacep, dfacesz);
726da6c28aaSamw }
727da6c28aaSamw }
728da6c28aaSamw if (dfaclcnt)
729da6c28aaSamw cacl_free(dfacep, dfacecnt * sizeof (ace_t));
730da6c28aaSamw
731da6c28aaSamw *retacecnt = acecnt + dfacecnt;
732da6c28aaSamw *retacep = acep;
733da6c28aaSamw return (0);
734da6c28aaSamw }
735da6c28aaSamw
736da6c28aaSamw static int
ace_mask_to_mode(uint32_t mask,o_mode_t * modep,boolean_t isdir)737a3c49ce1SAlbert Lee ace_mask_to_mode(uint32_t mask, o_mode_t *modep, boolean_t isdir)
738da6c28aaSamw {
739da6c28aaSamw int error = 0;
740da6c28aaSamw o_mode_t mode = 0;
741da6c28aaSamw uint32_t bits, wantbits;
742da6c28aaSamw
743da6c28aaSamw /* read */
744da6c28aaSamw if (mask & ACE_READ_DATA)
745da6c28aaSamw mode |= S_IROTH;
746da6c28aaSamw
747da6c28aaSamw /* write */
748da6c28aaSamw wantbits = (ACE_WRITE_DATA | ACE_APPEND_DATA);
749da6c28aaSamw if (isdir)
750da6c28aaSamw wantbits |= ACE_DELETE_CHILD;
751da6c28aaSamw bits = mask & wantbits;
752da6c28aaSamw if (bits != 0) {
753da6c28aaSamw if (bits != wantbits) {
754da6c28aaSamw error = ENOTSUP;
755da6c28aaSamw goto out;
756da6c28aaSamw }
757da6c28aaSamw mode |= S_IWOTH;
758da6c28aaSamw }
759da6c28aaSamw
760da6c28aaSamw /* exec */
761da6c28aaSamw if (mask & ACE_EXECUTE) {
762da6c28aaSamw mode |= S_IXOTH;
763da6c28aaSamw }
764da6c28aaSamw
765da6c28aaSamw *modep = mode;
766da6c28aaSamw
767da6c28aaSamw out:
768da6c28aaSamw return (error);
769da6c28aaSamw }
770da6c28aaSamw
771da6c28aaSamw static void
acevals_init(acevals_t * vals,uid_t key)772da6c28aaSamw acevals_init(acevals_t *vals, uid_t key)
773da6c28aaSamw {
774da6c28aaSamw bzero(vals, sizeof (*vals));
775da6c28aaSamw vals->allowed = ACE_MASK_UNDEFINED;
776da6c28aaSamw vals->denied = ACE_MASK_UNDEFINED;
777da6c28aaSamw vals->mask = ACE_MASK_UNDEFINED;
778da6c28aaSamw vals->key = key;
779da6c28aaSamw }
780da6c28aaSamw
781da6c28aaSamw static void
ace_list_init(ace_list_t * al,int dfacl_flag)782da6c28aaSamw ace_list_init(ace_list_t *al, int dfacl_flag)
783da6c28aaSamw {
784da6c28aaSamw acevals_init(&al->user_obj, NULL);
785da6c28aaSamw acevals_init(&al->group_obj, NULL);
786da6c28aaSamw acevals_init(&al->other_obj, NULL);
787da6c28aaSamw al->numusers = 0;
788da6c28aaSamw al->numgroups = 0;
789da6c28aaSamw al->acl_mask = 0;
790da6c28aaSamw al->hasmask = 0;
791da6c28aaSamw al->state = ace_unused;
792da6c28aaSamw al->seen = 0;
793da6c28aaSamw al->dfacl_flag = dfacl_flag;
794da6c28aaSamw }
795da6c28aaSamw
796da6c28aaSamw /*
797da6c28aaSamw * Find or create an acevals holder for a given id and avl tree.
798da6c28aaSamw *
799da6c28aaSamw * Note that only one thread will ever touch these avl trees, so
800da6c28aaSamw * there is no need for locking.
801da6c28aaSamw */
802da6c28aaSamw static acevals_t *
acevals_find(ace_t * ace,avl_tree_t * avl,int * num)803da6c28aaSamw acevals_find(ace_t *ace, avl_tree_t *avl, int *num)
804da6c28aaSamw {
805da6c28aaSamw acevals_t key, *rc;
806da6c28aaSamw avl_index_t where;
807da6c28aaSamw
808da6c28aaSamw key.key = ace->a_who;
809da6c28aaSamw rc = avl_find(avl, &key, &where);
810da6c28aaSamw if (rc != NULL)
811da6c28aaSamw return (rc);
812da6c28aaSamw
813da6c28aaSamw /* this memory is freed by ln_ace_to_aent()->ace_list_free() */
814da6c28aaSamw if (cacl_malloc((void **)&rc, sizeof (acevals_t)) != 0)
815da6c28aaSamw return (NULL);
816da6c28aaSamw
817da6c28aaSamw acevals_init(rc, ace->a_who);
818da6c28aaSamw avl_insert(avl, rc, where);
819da6c28aaSamw (*num)++;
820da6c28aaSamw
821da6c28aaSamw return (rc);
822da6c28aaSamw }
823da6c28aaSamw
824da6c28aaSamw static int
access_mask_check(ace_t * acep,int mask_bit,int isowner)825da6c28aaSamw access_mask_check(ace_t *acep, int mask_bit, int isowner)
826da6c28aaSamw {
827da6c28aaSamw int set_deny, err_deny;
828da6c28aaSamw int set_allow, err_allow;
829da6c28aaSamw int acl_consume;
830da6c28aaSamw int haswriteperm, hasreadperm;
831da6c28aaSamw
832da6c28aaSamw if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
833da6c28aaSamw haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 0 : 1;
834da6c28aaSamw hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 0 : 1;
835da6c28aaSamw } else {
836da6c28aaSamw haswriteperm = (acep->a_access_mask & ACE_WRITE_DATA) ? 1 : 0;
837da6c28aaSamw hasreadperm = (acep->a_access_mask & ACE_READ_DATA) ? 1 : 0;
838da6c28aaSamw }
839da6c28aaSamw
840da6c28aaSamw acl_consume = (ACL_SYNCHRONIZE_ERR_DENY |
841da6c28aaSamw ACL_DELETE_ERR_DENY |
842da6c28aaSamw ACL_WRITE_OWNER_ERR_DENY |
843da6c28aaSamw ACL_WRITE_OWNER_ERR_ALLOW |
844da6c28aaSamw ACL_WRITE_ATTRS_OWNER_SET_ALLOW |
845da6c28aaSamw ACL_WRITE_ATTRS_OWNER_ERR_DENY |
846da6c28aaSamw ACL_WRITE_ATTRS_WRITER_SET_DENY |
847da6c28aaSamw ACL_WRITE_ATTRS_WRITER_ERR_ALLOW |
848da6c28aaSamw ACL_WRITE_NAMED_WRITER_ERR_DENY |
849da6c28aaSamw ACL_READ_NAMED_READER_ERR_DENY);
850da6c28aaSamw
851da6c28aaSamw if (mask_bit == ACE_SYNCHRONIZE) {
852da6c28aaSamw set_deny = ACL_SYNCHRONIZE_SET_DENY;
853da6c28aaSamw err_deny = ACL_SYNCHRONIZE_ERR_DENY;
854da6c28aaSamw set_allow = ACL_SYNCHRONIZE_SET_ALLOW;
855da6c28aaSamw err_allow = ACL_SYNCHRONIZE_ERR_ALLOW;
856da6c28aaSamw } else if (mask_bit == ACE_WRITE_OWNER) {
857da6c28aaSamw set_deny = ACL_WRITE_OWNER_SET_DENY;
858da6c28aaSamw err_deny = ACL_WRITE_OWNER_ERR_DENY;
859da6c28aaSamw set_allow = ACL_WRITE_OWNER_SET_ALLOW;
860da6c28aaSamw err_allow = ACL_WRITE_OWNER_ERR_ALLOW;
861da6c28aaSamw } else if (mask_bit == ACE_DELETE) {
862da6c28aaSamw set_deny = ACL_DELETE_SET_DENY;
863da6c28aaSamw err_deny = ACL_DELETE_ERR_DENY;
864da6c28aaSamw set_allow = ACL_DELETE_SET_ALLOW;
865da6c28aaSamw err_allow = ACL_DELETE_ERR_ALLOW;
866da6c28aaSamw } else if (mask_bit == ACE_WRITE_ATTRIBUTES) {
867da6c28aaSamw if (isowner) {
868da6c28aaSamw set_deny = ACL_WRITE_ATTRS_OWNER_SET_DENY;
869da6c28aaSamw err_deny = ACL_WRITE_ATTRS_OWNER_ERR_DENY;
870da6c28aaSamw set_allow = ACL_WRITE_ATTRS_OWNER_SET_ALLOW;
871da6c28aaSamw err_allow = ACL_WRITE_ATTRS_OWNER_ERR_ALLOW;
872da6c28aaSamw } else if (haswriteperm) {
873da6c28aaSamw set_deny = ACL_WRITE_ATTRS_WRITER_SET_DENY;
874da6c28aaSamw err_deny = ACL_WRITE_ATTRS_WRITER_ERR_DENY;
875da6c28aaSamw set_allow = ACL_WRITE_ATTRS_WRITER_SET_ALLOW;
876da6c28aaSamw err_allow = ACL_WRITE_ATTRS_WRITER_ERR_ALLOW;
877da6c28aaSamw } else {
878da6c28aaSamw if ((acep->a_access_mask & mask_bit) &&
879da6c28aaSamw (acep->a_type & ACE_ACCESS_ALLOWED_ACE_TYPE)) {
880da6c28aaSamw return (ENOTSUP);
881da6c28aaSamw }
882da6c28aaSamw return (0);
883da6c28aaSamw }
884da6c28aaSamw } else if (mask_bit == ACE_READ_NAMED_ATTRS) {
885da6c28aaSamw if (!hasreadperm)
886da6c28aaSamw return (0);
887da6c28aaSamw
888da6c28aaSamw set_deny = ACL_READ_NAMED_READER_SET_DENY;
889da6c28aaSamw err_deny = ACL_READ_NAMED_READER_ERR_DENY;
890da6c28aaSamw set_allow = ACL_READ_NAMED_READER_SET_ALLOW;
891da6c28aaSamw err_allow = ACL_READ_NAMED_READER_ERR_ALLOW;
892da6c28aaSamw } else if (mask_bit == ACE_WRITE_NAMED_ATTRS) {
893da6c28aaSamw if (!haswriteperm)
894da6c28aaSamw return (0);
895da6c28aaSamw
896da6c28aaSamw set_deny = ACL_WRITE_NAMED_WRITER_SET_DENY;
897da6c28aaSamw err_deny = ACL_WRITE_NAMED_WRITER_ERR_DENY;
898da6c28aaSamw set_allow = ACL_WRITE_NAMED_WRITER_SET_ALLOW;
899da6c28aaSamw err_allow = ACL_WRITE_NAMED_WRITER_ERR_ALLOW;
900da6c28aaSamw } else {
901da6c28aaSamw return (EINVAL);
902da6c28aaSamw }
903da6c28aaSamw
904da6c28aaSamw if (acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) {
905da6c28aaSamw if (acl_consume & set_deny) {
906da6c28aaSamw if (!(acep->a_access_mask & mask_bit)) {
907da6c28aaSamw return (ENOTSUP);
908da6c28aaSamw }
909da6c28aaSamw } else if (acl_consume & err_deny) {
910da6c28aaSamw if (acep->a_access_mask & mask_bit) {
911da6c28aaSamw return (ENOTSUP);
912da6c28aaSamw }
913da6c28aaSamw }
914da6c28aaSamw } else {
915da6c28aaSamw /* ACE_ACCESS_ALLOWED_ACE_TYPE */
916da6c28aaSamw if (acl_consume & set_allow) {
917da6c28aaSamw if (!(acep->a_access_mask & mask_bit)) {
918da6c28aaSamw return (ENOTSUP);
919da6c28aaSamw }
920da6c28aaSamw } else if (acl_consume & err_allow) {
921da6c28aaSamw if (acep->a_access_mask & mask_bit) {
922da6c28aaSamw return (ENOTSUP);
923da6c28aaSamw }
924da6c28aaSamw }
925da6c28aaSamw }
926da6c28aaSamw return (0);
927da6c28aaSamw }
928da6c28aaSamw
929da6c28aaSamw static int
ace_to_aent_legal(ace_t * acep)930da6c28aaSamw ace_to_aent_legal(ace_t *acep)
931da6c28aaSamw {
932da6c28aaSamw int error = 0;
933da6c28aaSamw int isowner;
934da6c28aaSamw
935da6c28aaSamw /* only ALLOW or DENY */
936da6c28aaSamw if ((acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE) &&
937da6c28aaSamw (acep->a_type != ACE_ACCESS_DENIED_ACE_TYPE)) {
938da6c28aaSamw error = ENOTSUP;
939da6c28aaSamw goto out;
940da6c28aaSamw }
941da6c28aaSamw
942da6c28aaSamw /* check for invalid flags */
943da6c28aaSamw if (acep->a_flags & ~(ACE_VALID_FLAG_BITS)) {
944da6c28aaSamw error = EINVAL;
945da6c28aaSamw goto out;
946da6c28aaSamw }
947da6c28aaSamw
948da6c28aaSamw /* some flags are illegal */
949da6c28aaSamw if (acep->a_flags & (ACE_SUCCESSFUL_ACCESS_ACE_FLAG |
950da6c28aaSamw ACE_FAILED_ACCESS_ACE_FLAG |
951da6c28aaSamw ACE_NO_PROPAGATE_INHERIT_ACE)) {
952da6c28aaSamw error = ENOTSUP;
953da6c28aaSamw goto out;
954da6c28aaSamw }
955da6c28aaSamw
956da6c28aaSamw /* check for invalid masks */
957da6c28aaSamw if (acep->a_access_mask & ~(ACE_VALID_MASK_BITS)) {
958da6c28aaSamw error = EINVAL;
959da6c28aaSamw goto out;
960da6c28aaSamw }
961da6c28aaSamw
962da6c28aaSamw if ((acep->a_flags & ACE_OWNER)) {
963da6c28aaSamw isowner = 1;
964da6c28aaSamw } else {
965da6c28aaSamw isowner = 0;
966da6c28aaSamw }
967da6c28aaSamw
968da6c28aaSamw error = access_mask_check(acep, ACE_SYNCHRONIZE, isowner);
969da6c28aaSamw if (error)
970da6c28aaSamw goto out;
971da6c28aaSamw
972da6c28aaSamw error = access_mask_check(acep, ACE_WRITE_OWNER, isowner);
973da6c28aaSamw if (error)
974da6c28aaSamw goto out;
975da6c28aaSamw
976da6c28aaSamw error = access_mask_check(acep, ACE_DELETE, isowner);
977da6c28aaSamw if (error)
978da6c28aaSamw goto out;
979da6c28aaSamw
980da6c28aaSamw error = access_mask_check(acep, ACE_WRITE_ATTRIBUTES, isowner);
981da6c28aaSamw if (error)
982da6c28aaSamw goto out;
983da6c28aaSamw
984da6c28aaSamw error = access_mask_check(acep, ACE_READ_NAMED_ATTRS, isowner);
985da6c28aaSamw if (error)
986da6c28aaSamw goto out;
987da6c28aaSamw
988da6c28aaSamw error = access_mask_check(acep, ACE_WRITE_NAMED_ATTRS, isowner);
989da6c28aaSamw if (error)
990da6c28aaSamw goto out;
991da6c28aaSamw
992da6c28aaSamw /* more detailed checking of masks */
993da6c28aaSamw if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
994da6c28aaSamw if (! (acep->a_access_mask & ACE_READ_ATTRIBUTES)) {
995da6c28aaSamw error = ENOTSUP;
996da6c28aaSamw goto out;
997da6c28aaSamw }
998da6c28aaSamw if ((acep->a_access_mask & ACE_WRITE_DATA) &&
999da6c28aaSamw (! (acep->a_access_mask & ACE_APPEND_DATA))) {
1000da6c28aaSamw error = ENOTSUP;
1001da6c28aaSamw goto out;
1002da6c28aaSamw }
1003da6c28aaSamw if ((! (acep->a_access_mask & ACE_WRITE_DATA)) &&
1004da6c28aaSamw (acep->a_access_mask & ACE_APPEND_DATA)) {
1005da6c28aaSamw error = ENOTSUP;
1006da6c28aaSamw goto out;
1007da6c28aaSamw }
1008da6c28aaSamw }
1009da6c28aaSamw
1010da6c28aaSamw /* ACL enforcement */
1011da6c28aaSamw if ((acep->a_access_mask & ACE_READ_ACL) &&
1012da6c28aaSamw (acep->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE)) {
1013da6c28aaSamw error = ENOTSUP;
1014da6c28aaSamw goto out;
1015da6c28aaSamw }
1016da6c28aaSamw if (acep->a_access_mask & ACE_WRITE_ACL) {
1017da6c28aaSamw if ((acep->a_type == ACE_ACCESS_DENIED_ACE_TYPE) &&
1018da6c28aaSamw (isowner)) {
1019da6c28aaSamw error = ENOTSUP;
1020da6c28aaSamw goto out;
1021da6c28aaSamw }
1022da6c28aaSamw if ((acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) &&
1023da6c28aaSamw (! isowner)) {
1024da6c28aaSamw error = ENOTSUP;
1025da6c28aaSamw goto out;
1026da6c28aaSamw }
1027da6c28aaSamw }
1028da6c28aaSamw
1029da6c28aaSamw out:
1030da6c28aaSamw return (error);
1031da6c28aaSamw }
1032da6c28aaSamw
1033da6c28aaSamw static int
ace_allow_to_mode(uint32_t mask,o_mode_t * modep,boolean_t isdir)1034a3c49ce1SAlbert Lee ace_allow_to_mode(uint32_t mask, o_mode_t *modep, boolean_t isdir)
1035da6c28aaSamw {
1036da6c28aaSamw /* ACE_READ_ACL and ACE_READ_ATTRIBUTES must both be set */
1037da6c28aaSamw if ((mask & (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) !=
1038da6c28aaSamw (ACE_READ_ACL | ACE_READ_ATTRIBUTES)) {
1039da6c28aaSamw return (ENOTSUP);
1040da6c28aaSamw }
1041da6c28aaSamw
1042da6c28aaSamw return (ace_mask_to_mode(mask, modep, isdir));
1043da6c28aaSamw }
1044da6c28aaSamw
1045da6c28aaSamw static int
acevals_to_aent(acevals_t * vals,aclent_t * dest,ace_list_t * list,uid_t owner,gid_t group,boolean_t isdir)1046da6c28aaSamw acevals_to_aent(acevals_t *vals, aclent_t *dest, ace_list_t *list,
1047a3c49ce1SAlbert Lee uid_t owner, gid_t group, boolean_t isdir)
1048da6c28aaSamw {
1049da6c28aaSamw int error;
1050da6c28aaSamw uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1051da6c28aaSamw
1052da6c28aaSamw if (isdir)
1053da6c28aaSamw flips |= ACE_DELETE_CHILD;
1054da6c28aaSamw if (vals->allowed != (vals->denied ^ flips)) {
1055da6c28aaSamw error = ENOTSUP;
1056da6c28aaSamw goto out;
1057da6c28aaSamw }
1058da6c28aaSamw if ((list->hasmask) && (list->acl_mask != vals->mask) &&
1059da6c28aaSamw (vals->aent_type & (USER | GROUP | GROUP_OBJ))) {
1060da6c28aaSamw error = ENOTSUP;
1061da6c28aaSamw goto out;
1062da6c28aaSamw }
1063da6c28aaSamw error = ace_allow_to_mode(vals->allowed, &dest->a_perm, isdir);
1064da6c28aaSamw if (error != 0)
1065da6c28aaSamw goto out;
1066da6c28aaSamw dest->a_type = vals->aent_type;
1067da6c28aaSamw if (dest->a_type & (USER | GROUP)) {
1068da6c28aaSamw dest->a_id = vals->key;
1069da6c28aaSamw } else if (dest->a_type & USER_OBJ) {
1070da6c28aaSamw dest->a_id = owner;
1071da6c28aaSamw } else if (dest->a_type & GROUP_OBJ) {
1072da6c28aaSamw dest->a_id = group;
1073da6c28aaSamw } else if (dest->a_type & OTHER_OBJ) {
1074da6c28aaSamw dest->a_id = 0;
1075da6c28aaSamw } else {
1076da6c28aaSamw error = EINVAL;
1077da6c28aaSamw goto out;
1078da6c28aaSamw }
1079da6c28aaSamw
1080da6c28aaSamw out:
1081da6c28aaSamw return (error);
1082da6c28aaSamw }
1083da6c28aaSamw
1084da6c28aaSamw
1085da6c28aaSamw static int
ace_list_to_aent(ace_list_t * list,aclent_t ** aclentp,int * aclcnt,uid_t owner,gid_t group,boolean_t isdir)1086da6c28aaSamw ace_list_to_aent(ace_list_t *list, aclent_t **aclentp, int *aclcnt,
1087a3c49ce1SAlbert Lee uid_t owner, gid_t group, boolean_t isdir)
1088da6c28aaSamw {
1089da6c28aaSamw int error = 0;
1090da6c28aaSamw aclent_t *aent, *result = NULL;
1091da6c28aaSamw acevals_t *vals;
1092da6c28aaSamw int resultcount;
1093da6c28aaSamw
1094da6c28aaSamw if ((list->seen & (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) !=
1095da6c28aaSamw (USER_OBJ | GROUP_OBJ | OTHER_OBJ)) {
1096da6c28aaSamw error = ENOTSUP;
1097da6c28aaSamw goto out;
1098da6c28aaSamw }
1099da6c28aaSamw if ((! list->hasmask) && (list->numusers + list->numgroups > 0)) {
1100da6c28aaSamw error = ENOTSUP;
1101da6c28aaSamw goto out;
1102da6c28aaSamw }
1103da6c28aaSamw
1104da6c28aaSamw resultcount = 3 + list->numusers + list->numgroups;
1105da6c28aaSamw /*
1106da6c28aaSamw * This must be the same condition as below, when we add the CLASS_OBJ
1107da6c28aaSamw * (aka ACL mask)
1108da6c28aaSamw */
1109da6c28aaSamw if ((list->hasmask) || (! list->dfacl_flag))
1110da6c28aaSamw resultcount += 1;
1111da6c28aaSamw
1112da6c28aaSamw if (cacl_malloc((void **)&result,
1113da6c28aaSamw resultcount * sizeof (aclent_t)) != 0) {
1114da6c28aaSamw error = ENOMEM;
1115da6c28aaSamw goto out;
1116da6c28aaSamw }
1117da6c28aaSamw aent = result;
1118da6c28aaSamw
1119da6c28aaSamw /* USER_OBJ */
1120da6c28aaSamw if (!(list->user_obj.aent_type & USER_OBJ)) {
1121da6c28aaSamw error = EINVAL;
1122da6c28aaSamw goto out;
1123da6c28aaSamw }
1124da6c28aaSamw
1125da6c28aaSamw error = acevals_to_aent(&list->user_obj, aent, list, owner, group,
1126da6c28aaSamw isdir);
1127da6c28aaSamw
1128da6c28aaSamw if (error != 0)
1129da6c28aaSamw goto out;
1130da6c28aaSamw ++aent;
1131da6c28aaSamw /* USER */
1132da6c28aaSamw vals = NULL;
1133da6c28aaSamw for (vals = avl_first(&list->user); vals != NULL;
1134da6c28aaSamw vals = AVL_NEXT(&list->user, vals)) {
1135da6c28aaSamw if (!(vals->aent_type & USER)) {
1136da6c28aaSamw error = EINVAL;
1137da6c28aaSamw goto out;
1138da6c28aaSamw }
1139da6c28aaSamw error = acevals_to_aent(vals, aent, list, owner, group,
1140da6c28aaSamw isdir);
1141da6c28aaSamw if (error != 0)
1142da6c28aaSamw goto out;
1143da6c28aaSamw ++aent;
1144da6c28aaSamw }
1145da6c28aaSamw /* GROUP_OBJ */
1146da6c28aaSamw if (!(list->group_obj.aent_type & GROUP_OBJ)) {
1147da6c28aaSamw error = EINVAL;
1148da6c28aaSamw goto out;
1149da6c28aaSamw }
1150da6c28aaSamw error = acevals_to_aent(&list->group_obj, aent, list, owner, group,
1151da6c28aaSamw isdir);
1152da6c28aaSamw if (error != 0)
1153da6c28aaSamw goto out;
1154da6c28aaSamw ++aent;
1155da6c28aaSamw /* GROUP */
1156da6c28aaSamw vals = NULL;
1157da6c28aaSamw for (vals = avl_first(&list->group); vals != NULL;
1158da6c28aaSamw vals = AVL_NEXT(&list->group, vals)) {
1159da6c28aaSamw if (!(vals->aent_type & GROUP)) {
1160da6c28aaSamw error = EINVAL;
1161da6c28aaSamw goto out;
1162da6c28aaSamw }
1163da6c28aaSamw error = acevals_to_aent(vals, aent, list, owner, group,
1164da6c28aaSamw isdir);
1165da6c28aaSamw if (error != 0)
1166da6c28aaSamw goto out;
1167da6c28aaSamw ++aent;
1168da6c28aaSamw }
1169da6c28aaSamw /*
1170da6c28aaSamw * CLASS_OBJ (aka ACL_MASK)
1171da6c28aaSamw *
1172da6c28aaSamw * An ACL_MASK is not fabricated if the ACL is a default ACL.
1173da6c28aaSamw * This is to follow UFS's behavior.
1174da6c28aaSamw */
1175da6c28aaSamw if ((list->hasmask) || (! list->dfacl_flag)) {
1176da6c28aaSamw if (list->hasmask) {
1177da6c28aaSamw uint32_t flips = ACE_POSIX_SUPPORTED_BITS;
1178da6c28aaSamw if (isdir)
1179da6c28aaSamw flips |= ACE_DELETE_CHILD;
1180da6c28aaSamw error = ace_mask_to_mode(list->acl_mask ^ flips,
1181da6c28aaSamw &aent->a_perm, isdir);
1182da6c28aaSamw if (error != 0)
1183da6c28aaSamw goto out;
1184da6c28aaSamw } else {
1185da6c28aaSamw /* fabricate the ACL_MASK from the group permissions */
1186da6c28aaSamw error = ace_mask_to_mode(list->group_obj.allowed,
1187da6c28aaSamw &aent->a_perm, isdir);
1188da6c28aaSamw if (error != 0)
1189da6c28aaSamw goto out;
1190da6c28aaSamw }
1191da6c28aaSamw aent->a_id = 0;
1192da6c28aaSamw aent->a_type = CLASS_OBJ | list->dfacl_flag;
1193da6c28aaSamw ++aent;
1194da6c28aaSamw }
1195da6c28aaSamw /* OTHER_OBJ */
1196da6c28aaSamw if (!(list->other_obj.aent_type & OTHER_OBJ)) {
1197da6c28aaSamw error = EINVAL;
1198da6c28aaSamw goto out;
1199da6c28aaSamw }
1200da6c28aaSamw error = acevals_to_aent(&list->other_obj, aent, list, owner, group,
1201da6c28aaSamw isdir);
1202da6c28aaSamw if (error != 0)
1203da6c28aaSamw goto out;
1204da6c28aaSamw ++aent;
1205da6c28aaSamw
1206da6c28aaSamw *aclentp = result;
1207da6c28aaSamw *aclcnt = resultcount;
1208da6c28aaSamw
1209da6c28aaSamw out:
1210da6c28aaSamw if (error != 0) {
1211da6c28aaSamw if (result != NULL)
1212da6c28aaSamw cacl_free(result, resultcount * sizeof (aclent_t));
1213da6c28aaSamw }
1214da6c28aaSamw
1215da6c28aaSamw return (error);
1216da6c28aaSamw }
1217da6c28aaSamw
1218da6c28aaSamw
1219da6c28aaSamw /*
1220da6c28aaSamw * free all data associated with an ace_list
1221da6c28aaSamw */
1222da6c28aaSamw static void
ace_list_free(ace_list_t * al)1223da6c28aaSamw ace_list_free(ace_list_t *al)
1224da6c28aaSamw {
1225da6c28aaSamw acevals_t *node;
1226da6c28aaSamw void *cookie;
1227da6c28aaSamw
1228da6c28aaSamw if (al == NULL)
1229da6c28aaSamw return;
1230da6c28aaSamw
1231da6c28aaSamw cookie = NULL;
1232da6c28aaSamw while ((node = avl_destroy_nodes(&al->user, &cookie)) != NULL)
1233da6c28aaSamw cacl_free(node, sizeof (acevals_t));
1234da6c28aaSamw cookie = NULL;
1235da6c28aaSamw while ((node = avl_destroy_nodes(&al->group, &cookie)) != NULL)
1236da6c28aaSamw cacl_free(node, sizeof (acevals_t));
1237da6c28aaSamw
1238da6c28aaSamw avl_destroy(&al->user);
1239da6c28aaSamw avl_destroy(&al->group);
1240da6c28aaSamw
1241da6c28aaSamw /* free the container itself */
1242da6c28aaSamw cacl_free(al, sizeof (ace_list_t));
1243da6c28aaSamw }
1244da6c28aaSamw
1245da6c28aaSamw static int
acevals_compare(const void * va,const void * vb)1246da6c28aaSamw acevals_compare(const void *va, const void *vb)
1247da6c28aaSamw {
1248da6c28aaSamw const acevals_t *a = va, *b = vb;
1249da6c28aaSamw
1250da6c28aaSamw if (a->key == b->key)
1251da6c28aaSamw return (0);
1252da6c28aaSamw
1253da6c28aaSamw if (a->key > b->key)
1254da6c28aaSamw return (1);
1255da6c28aaSamw
1256da6c28aaSamw else
1257da6c28aaSamw return (-1);
1258da6c28aaSamw }
1259da6c28aaSamw
1260da6c28aaSamw /*
1261da6c28aaSamw * Convert a list of ace_t entries to equivalent regular and default
1262da6c28aaSamw * aclent_t lists. Return error (ENOTSUP) when conversion is not possible.
1263da6c28aaSamw */
1264da6c28aaSamw static int
ln_ace_to_aent(ace_t * ace,int n,uid_t owner,gid_t group,aclent_t ** aclentp,int * aclcnt,aclent_t ** dfaclentp,int * dfaclcnt,boolean_t isdir)1265da6c28aaSamw ln_ace_to_aent(ace_t *ace, int n, uid_t owner, gid_t group,
1266da6c28aaSamw aclent_t **aclentp, int *aclcnt, aclent_t **dfaclentp, int *dfaclcnt,
1267a3c49ce1SAlbert Lee boolean_t isdir)
1268da6c28aaSamw {
1269da6c28aaSamw int error = 0;
1270da6c28aaSamw ace_t *acep;
1271da6c28aaSamw uint32_t bits;
1272da6c28aaSamw int i;
1273da6c28aaSamw ace_list_t *normacl = NULL, *dfacl = NULL, *acl;
1274da6c28aaSamw acevals_t *vals;
1275da6c28aaSamw
1276da6c28aaSamw *aclentp = NULL;
1277da6c28aaSamw *aclcnt = 0;
1278da6c28aaSamw *dfaclentp = NULL;
1279da6c28aaSamw *dfaclcnt = 0;
1280da6c28aaSamw
1281da6c28aaSamw /* we need at least user_obj, group_obj, and other_obj */
1282da6c28aaSamw if (n < 6) {
1283da6c28aaSamw error = ENOTSUP;
1284da6c28aaSamw goto out;
1285da6c28aaSamw }
1286da6c28aaSamw if (ace == NULL) {
1287da6c28aaSamw error = EINVAL;
1288da6c28aaSamw goto out;
1289da6c28aaSamw }
1290da6c28aaSamw
1291da6c28aaSamw error = cacl_malloc((void **)&normacl, sizeof (ace_list_t));
1292da6c28aaSamw if (error != 0)
1293da6c28aaSamw goto out;
1294da6c28aaSamw
1295da6c28aaSamw avl_create(&normacl->user, acevals_compare, sizeof (acevals_t),
1296da6c28aaSamw offsetof(acevals_t, avl));
1297da6c28aaSamw avl_create(&normacl->group, acevals_compare, sizeof (acevals_t),
1298da6c28aaSamw offsetof(acevals_t, avl));
1299da6c28aaSamw
1300da6c28aaSamw ace_list_init(normacl, 0);
1301da6c28aaSamw
1302da6c28aaSamw error = cacl_malloc((void **)&dfacl, sizeof (ace_list_t));
1303da6c28aaSamw if (error != 0)
1304da6c28aaSamw goto out;
1305da6c28aaSamw
1306da6c28aaSamw avl_create(&dfacl->user, acevals_compare, sizeof (acevals_t),
1307da6c28aaSamw offsetof(acevals_t, avl));
1308da6c28aaSamw avl_create(&dfacl->group, acevals_compare, sizeof (acevals_t),
1309da6c28aaSamw offsetof(acevals_t, avl));
1310da6c28aaSamw ace_list_init(dfacl, ACL_DEFAULT);
1311da6c28aaSamw
1312da6c28aaSamw /* process every ace_t... */
1313da6c28aaSamw for (i = 0; i < n; i++) {
1314da6c28aaSamw acep = &ace[i];
1315da6c28aaSamw
1316da6c28aaSamw /* rule out certain cases quickly */
1317da6c28aaSamw error = ace_to_aent_legal(acep);
1318da6c28aaSamw if (error != 0)
1319da6c28aaSamw goto out;
1320da6c28aaSamw
1321da6c28aaSamw /*
1322da6c28aaSamw * Turn off these bits in order to not have to worry about
1323da6c28aaSamw * them when doing the checks for compliments.
1324da6c28aaSamw */
1325da6c28aaSamw acep->a_access_mask &= ~(ACE_WRITE_OWNER | ACE_DELETE |
1326da6c28aaSamw ACE_SYNCHRONIZE | ACE_WRITE_ATTRIBUTES |
1327da6c28aaSamw ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS);
1328da6c28aaSamw
1329da6c28aaSamw /* see if this should be a regular or default acl */
1330da6c28aaSamw bits = acep->a_flags &
1331da6c28aaSamw (ACE_INHERIT_ONLY_ACE |
1332da6c28aaSamw ACE_FILE_INHERIT_ACE |
1333da6c28aaSamw ACE_DIRECTORY_INHERIT_ACE);
1334da6c28aaSamw if (bits != 0) {
1335da6c28aaSamw /* all or nothing on these inherit bits */
1336da6c28aaSamw if (bits != (ACE_INHERIT_ONLY_ACE |
1337da6c28aaSamw ACE_FILE_INHERIT_ACE |
1338da6c28aaSamw ACE_DIRECTORY_INHERIT_ACE)) {
1339da6c28aaSamw error = ENOTSUP;
1340da6c28aaSamw goto out;
1341da6c28aaSamw }
1342da6c28aaSamw acl = dfacl;
1343da6c28aaSamw } else {
1344da6c28aaSamw acl = normacl;
1345da6c28aaSamw }
1346da6c28aaSamw
1347da6c28aaSamw if ((acep->a_flags & ACE_OWNER)) {
1348da6c28aaSamw if (acl->state > ace_user_obj) {
1349da6c28aaSamw error = ENOTSUP;
1350da6c28aaSamw goto out;
1351da6c28aaSamw }
1352da6c28aaSamw acl->state = ace_user_obj;
1353da6c28aaSamw acl->seen |= USER_OBJ;
1354da6c28aaSamw vals = &acl->user_obj;
1355da6c28aaSamw vals->aent_type = USER_OBJ | acl->dfacl_flag;
1356da6c28aaSamw } else if ((acep->a_flags & ACE_EVERYONE)) {
1357da6c28aaSamw acl->state = ace_other_obj;
1358da6c28aaSamw acl->seen |= OTHER_OBJ;
1359da6c28aaSamw vals = &acl->other_obj;
1360da6c28aaSamw vals->aent_type = OTHER_OBJ | acl->dfacl_flag;
1361da6c28aaSamw } else if (acep->a_flags & ACE_IDENTIFIER_GROUP) {
1362da6c28aaSamw if (acl->state > ace_group) {
1363da6c28aaSamw error = ENOTSUP;
1364da6c28aaSamw goto out;
1365da6c28aaSamw }
1366da6c28aaSamw if ((acep->a_flags & ACE_GROUP)) {
1367da6c28aaSamw acl->seen |= GROUP_OBJ;
1368da6c28aaSamw vals = &acl->group_obj;
1369da6c28aaSamw vals->aent_type = GROUP_OBJ | acl->dfacl_flag;
1370da6c28aaSamw } else {
1371da6c28aaSamw acl->seen |= GROUP;
1372da6c28aaSamw vals = acevals_find(acep, &acl->group,
1373da6c28aaSamw &acl->numgroups);
1374da6c28aaSamw if (vals == NULL) {
1375da6c28aaSamw error = ENOMEM;
1376da6c28aaSamw goto out;
1377da6c28aaSamw }
1378da6c28aaSamw vals->aent_type = GROUP | acl->dfacl_flag;
1379da6c28aaSamw }
1380da6c28aaSamw acl->state = ace_group;
1381da6c28aaSamw } else {
1382da6c28aaSamw if (acl->state > ace_user) {
1383da6c28aaSamw error = ENOTSUP;
1384da6c28aaSamw goto out;
1385da6c28aaSamw }
1386da6c28aaSamw acl->state = ace_user;
1387da6c28aaSamw acl->seen |= USER;
1388da6c28aaSamw vals = acevals_find(acep, &acl->user,
1389da6c28aaSamw &acl->numusers);
1390da6c28aaSamw if (vals == NULL) {
1391da6c28aaSamw error = ENOMEM;
1392da6c28aaSamw goto out;
1393da6c28aaSamw }
1394da6c28aaSamw vals->aent_type = USER | acl->dfacl_flag;
1395da6c28aaSamw }
1396da6c28aaSamw
1397da6c28aaSamw if (!(acl->state > ace_unused)) {
1398da6c28aaSamw error = EINVAL;
1399da6c28aaSamw goto out;
1400da6c28aaSamw }
1401da6c28aaSamw
1402da6c28aaSamw if (acep->a_type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
1403da6c28aaSamw /* no more than one allowed per aclent_t */
1404da6c28aaSamw if (vals->allowed != ACE_MASK_UNDEFINED) {
1405da6c28aaSamw error = ENOTSUP;
1406da6c28aaSamw goto out;
1407da6c28aaSamw }
1408da6c28aaSamw vals->allowed = acep->a_access_mask;
1409da6c28aaSamw } else {
1410da6c28aaSamw /*
1411da6c28aaSamw * it's a DENY; if there was a previous DENY, it
1412da6c28aaSamw * must have been an ACL_MASK.
1413da6c28aaSamw */
1414da6c28aaSamw if (vals->denied != ACE_MASK_UNDEFINED) {
1415da6c28aaSamw /* ACL_MASK is for USER and GROUP only */
1416da6c28aaSamw if ((acl->state != ace_user) &&
1417da6c28aaSamw (acl->state != ace_group)) {
1418da6c28aaSamw error = ENOTSUP;
1419da6c28aaSamw goto out;
1420da6c28aaSamw }
1421da6c28aaSamw
1422da6c28aaSamw if (! acl->hasmask) {
1423da6c28aaSamw acl->hasmask = 1;
1424da6c28aaSamw acl->acl_mask = vals->denied;
1425da6c28aaSamw /* check for mismatched ACL_MASK emulations */
1426da6c28aaSamw } else if (acl->acl_mask != vals->denied) {
1427da6c28aaSamw error = ENOTSUP;
1428da6c28aaSamw goto out;
1429da6c28aaSamw }
1430da6c28aaSamw vals->mask = vals->denied;
1431da6c28aaSamw }
1432da6c28aaSamw vals->denied = acep->a_access_mask;
1433da6c28aaSamw }
1434da6c28aaSamw }
1435da6c28aaSamw
1436da6c28aaSamw /* done collating; produce the aclent_t lists */
1437da6c28aaSamw if (normacl->state != ace_unused) {
1438da6c28aaSamw error = ace_list_to_aent(normacl, aclentp, aclcnt,
1439da6c28aaSamw owner, group, isdir);
1440da6c28aaSamw if (error != 0) {
1441da6c28aaSamw goto out;
1442da6c28aaSamw }
1443da6c28aaSamw }
1444da6c28aaSamw if (dfacl->state != ace_unused) {
1445da6c28aaSamw error = ace_list_to_aent(dfacl, dfaclentp, dfaclcnt,
1446da6c28aaSamw owner, group, isdir);
1447da6c28aaSamw if (error != 0) {
1448da6c28aaSamw goto out;
1449da6c28aaSamw }
1450da6c28aaSamw }
1451da6c28aaSamw
1452da6c28aaSamw out:
1453da6c28aaSamw if (normacl != NULL)
1454da6c28aaSamw ace_list_free(normacl);
1455da6c28aaSamw if (dfacl != NULL)
1456da6c28aaSamw ace_list_free(dfacl);
1457da6c28aaSamw
1458da6c28aaSamw return (error);
1459da6c28aaSamw }
1460da6c28aaSamw
1461da6c28aaSamw static int
convert_ace_to_aent(ace_t * acebufp,int acecnt,boolean_t isdir,uid_t owner,gid_t group,aclent_t ** retaclentp,int * retaclcnt)1462a3c49ce1SAlbert Lee convert_ace_to_aent(ace_t *acebufp, int acecnt, boolean_t isdir,
1463da6c28aaSamw uid_t owner, gid_t group, aclent_t **retaclentp, int *retaclcnt)
1464da6c28aaSamw {
1465da6c28aaSamw int error = 0;
1466da6c28aaSamw aclent_t *aclentp, *dfaclentp;
1467da6c28aaSamw int aclcnt, dfaclcnt;
1468*7206bf49SGordon Ross int aclsz, dfaclsz = 0;
1469da6c28aaSamw
1470da6c28aaSamw error = ln_ace_to_aent(acebufp, acecnt, owner, group,
1471da6c28aaSamw &aclentp, &aclcnt, &dfaclentp, &dfaclcnt, isdir);
1472da6c28aaSamw
1473da6c28aaSamw if (error)
1474da6c28aaSamw return (error);
1475da6c28aaSamw
1476da6c28aaSamw
1477da6c28aaSamw if (dfaclcnt != 0) {
1478da6c28aaSamw /*
1479da6c28aaSamw * Slap aclentp and dfaclentp into a single array.
1480da6c28aaSamw */
1481da6c28aaSamw aclsz = sizeof (aclent_t) * aclcnt;
1482da6c28aaSamw dfaclsz = sizeof (aclent_t) * dfaclcnt;
1483da6c28aaSamw aclentp = cacl_realloc(aclentp, aclsz, aclsz + dfaclsz);
1484da6c28aaSamw if (aclentp != NULL) {
1485da6c28aaSamw (void) memcpy(aclentp + aclcnt, dfaclentp, dfaclsz);
1486da6c28aaSamw } else {
1487da6c28aaSamw error = ENOMEM;
1488da6c28aaSamw }
1489da6c28aaSamw }
1490da6c28aaSamw
1491da6c28aaSamw if (aclentp) {
1492da6c28aaSamw *retaclentp = aclentp;
1493da6c28aaSamw *retaclcnt = aclcnt + dfaclcnt;
1494da6c28aaSamw }
1495da6c28aaSamw
1496da6c28aaSamw if (dfaclentp)
1497da6c28aaSamw cacl_free(dfaclentp, dfaclsz);
1498da6c28aaSamw
1499da6c28aaSamw return (error);
1500da6c28aaSamw }
1501da6c28aaSamw
1502da6c28aaSamw
1503da6c28aaSamw int
acl_translate(acl_t * aclp,int target_flavor,boolean_t isdir,uid_t owner,gid_t group)1504a3c49ce1SAlbert Lee acl_translate(acl_t *aclp, int target_flavor, boolean_t isdir, uid_t owner,
1505da6c28aaSamw gid_t group)
1506da6c28aaSamw {
1507da6c28aaSamw int aclcnt;
1508da6c28aaSamw void *acldata;
1509da6c28aaSamw int error;
1510da6c28aaSamw
1511da6c28aaSamw /*
1512da6c28aaSamw * See if we need to translate
1513da6c28aaSamw */
1514da6c28aaSamw if ((target_flavor == _ACL_ACE_ENABLED && aclp->acl_type == ACE_T) ||
1515da6c28aaSamw (target_flavor == _ACL_ACLENT_ENABLED &&
1516da6c28aaSamw aclp->acl_type == ACLENT_T))
1517da6c28aaSamw return (0);
1518da6c28aaSamw
1519da6c28aaSamw if (target_flavor == -1) {
1520da6c28aaSamw error = EINVAL;
1521da6c28aaSamw goto out;
1522da6c28aaSamw }
1523da6c28aaSamw
1524da6c28aaSamw if (target_flavor == _ACL_ACE_ENABLED &&
1525da6c28aaSamw aclp->acl_type == ACLENT_T) {
1526da6c28aaSamw error = convert_aent_to_ace(aclp->acl_aclp,
1527da6c28aaSamw aclp->acl_cnt, isdir, (ace_t **)&acldata, &aclcnt);
1528da6c28aaSamw if (error)
1529da6c28aaSamw goto out;
1530da6c28aaSamw
1531da6c28aaSamw } else if (target_flavor == _ACL_ACLENT_ENABLED &&
1532da6c28aaSamw aclp->acl_type == ACE_T) {
1533da6c28aaSamw error = convert_ace_to_aent(aclp->acl_aclp, aclp->acl_cnt,
1534da6c28aaSamw isdir, owner, group, (aclent_t **)&acldata, &aclcnt);
1535da6c28aaSamw if (error)
1536da6c28aaSamw goto out;
1537da6c28aaSamw } else {
1538da6c28aaSamw error = ENOTSUP;
1539da6c28aaSamw goto out;
1540da6c28aaSamw }
1541da6c28aaSamw
1542da6c28aaSamw /*
1543da6c28aaSamw * replace old acl with newly translated acl
1544da6c28aaSamw */
1545da6c28aaSamw cacl_free(aclp->acl_aclp, aclp->acl_cnt * aclp->acl_entry_size);
1546da6c28aaSamw aclp->acl_aclp = acldata;
1547da6c28aaSamw aclp->acl_cnt = aclcnt;
1548da6c28aaSamw if (target_flavor == _ACL_ACE_ENABLED) {
1549da6c28aaSamw aclp->acl_type = ACE_T;
1550da6c28aaSamw aclp->acl_entry_size = sizeof (ace_t);
1551da6c28aaSamw } else {
1552da6c28aaSamw aclp->acl_type = ACLENT_T;
1553da6c28aaSamw aclp->acl_entry_size = sizeof (aclent_t);
1554da6c28aaSamw }
1555da6c28aaSamw return (0);
1556da6c28aaSamw
1557da6c28aaSamw out:
1558da6c28aaSamw
1559*7206bf49SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
1560*7206bf49SGordon Ross return (error);
1561*7206bf49SGordon Ross #else
1562da6c28aaSamw errno = error;
1563da6c28aaSamw return (-1);
1564da6c28aaSamw #endif
1565da6c28aaSamw }
156627dd1e87SMark Shellenbaum
156727dd1e87SMark Shellenbaum #define SET_ACE(acl, index, who, mask, type, flags) { \
156827dd1e87SMark Shellenbaum acl[0][index].a_who = (uint32_t)who; \
156927dd1e87SMark Shellenbaum acl[0][index].a_type = type; \
157027dd1e87SMark Shellenbaum acl[0][index].a_flags = flags; \
157127dd1e87SMark Shellenbaum acl[0][index++].a_access_mask = mask; \
157227dd1e87SMark Shellenbaum }
157327dd1e87SMark Shellenbaum
157427dd1e87SMark Shellenbaum void
acl_trivial_access_masks(mode_t mode,boolean_t isdir,trivial_acl_t * masks)1575a3c49ce1SAlbert Lee acl_trivial_access_masks(mode_t mode, boolean_t isdir, trivial_acl_t *masks)
157627dd1e87SMark Shellenbaum {
1577a3c49ce1SAlbert Lee uint32_t read_mask = ACE_READ_DATA;
1578a3c49ce1SAlbert Lee uint32_t write_mask = ACE_WRITE_DATA|ACE_APPEND_DATA;
1579a3c49ce1SAlbert Lee uint32_t execute_mask = ACE_EXECUTE;
158027dd1e87SMark Shellenbaum
1581a3c49ce1SAlbert Lee (void) isdir; /* will need this later */
1582a3c49ce1SAlbert Lee
1583a3c49ce1SAlbert Lee masks->deny1 = 0;
158427dd1e87SMark Shellenbaum if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
1585a3c49ce1SAlbert Lee masks->deny1 |= read_mask;
158627dd1e87SMark Shellenbaum if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH)))
1587a3c49ce1SAlbert Lee masks->deny1 |= write_mask;
158827dd1e87SMark Shellenbaum if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
1589a3c49ce1SAlbert Lee masks->deny1 |= execute_mask;
159027dd1e87SMark Shellenbaum
1591a3c49ce1SAlbert Lee masks->deny2 = 0;
159227dd1e87SMark Shellenbaum if (!(mode & S_IRGRP) && (mode & S_IROTH))
1593a3c49ce1SAlbert Lee masks->deny2 |= read_mask;
159427dd1e87SMark Shellenbaum if (!(mode & S_IWGRP) && (mode & S_IWOTH))
1595a3c49ce1SAlbert Lee masks->deny2 |= write_mask;
159627dd1e87SMark Shellenbaum if (!(mode & S_IXGRP) && (mode & S_IXOTH))
1597a3c49ce1SAlbert Lee masks->deny2 |= execute_mask;
159827dd1e87SMark Shellenbaum
1599a3c49ce1SAlbert Lee masks->allow0 = 0;
160027dd1e87SMark Shellenbaum if ((mode & S_IRUSR) && (!(mode & S_IRGRP) && (mode & S_IROTH)))
1601a3c49ce1SAlbert Lee masks->allow0 |= read_mask;
160227dd1e87SMark Shellenbaum if ((mode & S_IWUSR) && (!(mode & S_IWGRP) && (mode & S_IWOTH)))
1603a3c49ce1SAlbert Lee masks->allow0 |= write_mask;
160427dd1e87SMark Shellenbaum if ((mode & S_IXUSR) && (!(mode & S_IXGRP) && (mode & S_IXOTH)))
1605a3c49ce1SAlbert Lee masks->allow0 |= execute_mask;
160627dd1e87SMark Shellenbaum
1607a3c49ce1SAlbert Lee masks->owner = ACE_WRITE_ATTRIBUTES|ACE_WRITE_OWNER|ACE_WRITE_ACL|
160827dd1e87SMark Shellenbaum ACE_WRITE_NAMED_ATTRS|ACE_READ_ACL|ACE_READ_ATTRIBUTES|
160927dd1e87SMark Shellenbaum ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE;
161027dd1e87SMark Shellenbaum if (mode & S_IRUSR)
1611a3c49ce1SAlbert Lee masks->owner |= read_mask;
161227dd1e87SMark Shellenbaum if (mode & S_IWUSR)
1613a3c49ce1SAlbert Lee masks->owner |= write_mask;
161427dd1e87SMark Shellenbaum if (mode & S_IXUSR)
1615a3c49ce1SAlbert Lee masks->owner |= execute_mask;
161627dd1e87SMark Shellenbaum
1617a3c49ce1SAlbert Lee masks->group = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
161827dd1e87SMark Shellenbaum ACE_SYNCHRONIZE;
161927dd1e87SMark Shellenbaum if (mode & S_IRGRP)
1620a3c49ce1SAlbert Lee masks->group |= read_mask;
162127dd1e87SMark Shellenbaum if (mode & S_IWGRP)
1622a3c49ce1SAlbert Lee masks->group |= write_mask;
162327dd1e87SMark Shellenbaum if (mode & S_IXGRP)
1624a3c49ce1SAlbert Lee masks->group |= execute_mask;
162527dd1e87SMark Shellenbaum
1626a3c49ce1SAlbert Lee masks->everyone = ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_NAMED_ATTRS|
162727dd1e87SMark Shellenbaum ACE_SYNCHRONIZE;
162827dd1e87SMark Shellenbaum if (mode & S_IROTH)
1629a3c49ce1SAlbert Lee masks->everyone |= read_mask;
163027dd1e87SMark Shellenbaum if (mode & S_IWOTH)
1631a3c49ce1SAlbert Lee masks->everyone |= write_mask;
163227dd1e87SMark Shellenbaum if (mode & S_IXOTH)
1633a3c49ce1SAlbert Lee masks->everyone |= execute_mask;
163427dd1e87SMark Shellenbaum }
163527dd1e87SMark Shellenbaum
163627dd1e87SMark Shellenbaum int
acl_trivial_create(mode_t mode,boolean_t isdir,ace_t ** acl,int * count)1637a3c49ce1SAlbert Lee acl_trivial_create(mode_t mode, boolean_t isdir, ace_t **acl, int *count)
163827dd1e87SMark Shellenbaum {
163927dd1e87SMark Shellenbaum int index = 0;
164027dd1e87SMark Shellenbaum int error;
1641a3c49ce1SAlbert Lee trivial_acl_t masks;
164227dd1e87SMark Shellenbaum
164327dd1e87SMark Shellenbaum *count = 3;
1644a3c49ce1SAlbert Lee acl_trivial_access_masks(mode, isdir, &masks);
164527dd1e87SMark Shellenbaum
1646a3c49ce1SAlbert Lee if (masks.allow0)
164727dd1e87SMark Shellenbaum (*count)++;
1648a3c49ce1SAlbert Lee if (masks.deny1)
164927dd1e87SMark Shellenbaum (*count)++;
1650a3c49ce1SAlbert Lee if (masks.deny2)
165127dd1e87SMark Shellenbaum (*count)++;
165227dd1e87SMark Shellenbaum
165327dd1e87SMark Shellenbaum if ((error = cacl_malloc((void **)acl, *count * sizeof (ace_t))) != 0)
165427dd1e87SMark Shellenbaum return (error);
165527dd1e87SMark Shellenbaum
1656a3c49ce1SAlbert Lee if (masks.allow0) {
1657a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.allow0,
1658a3c49ce1SAlbert Lee ACE_ACCESS_ALLOWED_ACE_TYPE, ACE_OWNER);
165927dd1e87SMark Shellenbaum }
1660a3c49ce1SAlbert Lee if (masks.deny1) {
1661a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.deny1,
1662a3c49ce1SAlbert Lee ACE_ACCESS_DENIED_ACE_TYPE, ACE_OWNER);
166327dd1e87SMark Shellenbaum }
1664a3c49ce1SAlbert Lee if (masks.deny2) {
1665a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.deny2,
1666a3c49ce1SAlbert Lee ACE_ACCESS_DENIED_ACE_TYPE, ACE_GROUP|ACE_IDENTIFIER_GROUP);
166727dd1e87SMark Shellenbaum }
166827dd1e87SMark Shellenbaum
1669a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.owner, ACE_ACCESS_ALLOWED_ACE_TYPE,
1670a3c49ce1SAlbert Lee ACE_OWNER);
1671a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.group, ACE_ACCESS_ALLOWED_ACE_TYPE,
167227dd1e87SMark Shellenbaum ACE_IDENTIFIER_GROUP|ACE_GROUP);
1673a3c49ce1SAlbert Lee SET_ACE(acl, index, -1, masks.everyone, ACE_ACCESS_ALLOWED_ACE_TYPE,
167427dd1e87SMark Shellenbaum ACE_EVERYONE);
167527dd1e87SMark Shellenbaum
167627dd1e87SMark Shellenbaum return (0);
167727dd1e87SMark Shellenbaum }
167827dd1e87SMark Shellenbaum
167927dd1e87SMark Shellenbaum /*
168027dd1e87SMark Shellenbaum * ace_trivial:
168127dd1e87SMark Shellenbaum * determine whether an ace_t acl is trivial
168227dd1e87SMark Shellenbaum *
168327dd1e87SMark Shellenbaum * Trivialness implies that the acl is composed of only
168427dd1e87SMark Shellenbaum * owner, group, everyone entries. ACL can't
168527dd1e87SMark Shellenbaum * have read_acl denied, and write_owner/write_acl/write_attributes
168627dd1e87SMark Shellenbaum * can only be owner@ entry.
168727dd1e87SMark Shellenbaum */
168827dd1e87SMark Shellenbaum int
ace_trivial_common(void * acep,int aclcnt,uint64_t (* walk)(void *,uint64_t,int aclcnt,uint16_t *,uint16_t *,uint32_t *))168927dd1e87SMark Shellenbaum ace_trivial_common(void *acep, int aclcnt,
169027dd1e87SMark Shellenbaum uint64_t (*walk)(void *, uint64_t, int aclcnt,
169127dd1e87SMark Shellenbaum uint16_t *, uint16_t *, uint32_t *))
169227dd1e87SMark Shellenbaum {
169327dd1e87SMark Shellenbaum uint16_t flags;
169427dd1e87SMark Shellenbaum uint32_t mask;
169527dd1e87SMark Shellenbaum uint16_t type;
169627dd1e87SMark Shellenbaum uint64_t cookie = 0;
169727dd1e87SMark Shellenbaum
169827dd1e87SMark Shellenbaum while (cookie = walk(acep, cookie, aclcnt, &flags, &type, &mask)) {
169927dd1e87SMark Shellenbaum switch (flags & ACE_TYPE_FLAGS) {
170027dd1e87SMark Shellenbaum case ACE_OWNER:
170127dd1e87SMark Shellenbaum case ACE_GROUP|ACE_IDENTIFIER_GROUP:
170227dd1e87SMark Shellenbaum case ACE_EVERYONE:
170327dd1e87SMark Shellenbaum break;
170427dd1e87SMark Shellenbaum default:
170527dd1e87SMark Shellenbaum return (1);
170627dd1e87SMark Shellenbaum
170727dd1e87SMark Shellenbaum }
170827dd1e87SMark Shellenbaum
170927dd1e87SMark Shellenbaum if (flags & (ACE_FILE_INHERIT_ACE|
171027dd1e87SMark Shellenbaum ACE_DIRECTORY_INHERIT_ACE|ACE_NO_PROPAGATE_INHERIT_ACE|
171127dd1e87SMark Shellenbaum ACE_INHERIT_ONLY_ACE))
171227dd1e87SMark Shellenbaum return (1);
171327dd1e87SMark Shellenbaum
171427dd1e87SMark Shellenbaum /*
171527dd1e87SMark Shellenbaum * Special check for some special bits
171627dd1e87SMark Shellenbaum *
171727dd1e87SMark Shellenbaum * Don't allow anybody to deny reading basic
171827dd1e87SMark Shellenbaum * attributes or a files ACL.
171927dd1e87SMark Shellenbaum */
172027dd1e87SMark Shellenbaum if ((mask & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
172127dd1e87SMark Shellenbaum (type == ACE_ACCESS_DENIED_ACE_TYPE))
172227dd1e87SMark Shellenbaum return (1);
172327dd1e87SMark Shellenbaum
172427dd1e87SMark Shellenbaum /*
172527dd1e87SMark Shellenbaum * Delete permissions are never set by default
172627dd1e87SMark Shellenbaum */
172727dd1e87SMark Shellenbaum if (mask & (ACE_DELETE|ACE_DELETE_CHILD))
172827dd1e87SMark Shellenbaum return (1);
172927dd1e87SMark Shellenbaum /*
173027dd1e87SMark Shellenbaum * only allow owner@ to have
173127dd1e87SMark Shellenbaum * write_acl/write_owner/write_attributes/write_xattr/
173227dd1e87SMark Shellenbaum */
173327dd1e87SMark Shellenbaum if (type == ACE_ACCESS_ALLOWED_ACE_TYPE &&
173427dd1e87SMark Shellenbaum (!(flags & ACE_OWNER) && (mask &
173527dd1e87SMark Shellenbaum (ACE_WRITE_OWNER|ACE_WRITE_ACL| ACE_WRITE_ATTRIBUTES|
173627dd1e87SMark Shellenbaum ACE_WRITE_NAMED_ATTRS))))
173727dd1e87SMark Shellenbaum return (1);
173827dd1e87SMark Shellenbaum
173927dd1e87SMark Shellenbaum }
174027dd1e87SMark Shellenbaum return (0);
174127dd1e87SMark Shellenbaum }
174227dd1e87SMark Shellenbaum
174327dd1e87SMark Shellenbaum uint64_t
ace_walk(void * datap,uint64_t cookie,int aclcnt,uint16_t * flags,uint16_t * type,uint32_t * mask)174427dd1e87SMark Shellenbaum ace_walk(void *datap, uint64_t cookie, int aclcnt, uint16_t *flags,
174527dd1e87SMark Shellenbaum uint16_t *type, uint32_t *mask)
174627dd1e87SMark Shellenbaum {
174727dd1e87SMark Shellenbaum ace_t *acep = datap;
174827dd1e87SMark Shellenbaum
174927dd1e87SMark Shellenbaum if (cookie >= aclcnt)
175027dd1e87SMark Shellenbaum return (0);
175127dd1e87SMark Shellenbaum
175227dd1e87SMark Shellenbaum *flags = acep[cookie].a_flags;
175327dd1e87SMark Shellenbaum *type = acep[cookie].a_type;
175427dd1e87SMark Shellenbaum *mask = acep[cookie++].a_access_mask;
175527dd1e87SMark Shellenbaum
175627dd1e87SMark Shellenbaum return (cookie);
175727dd1e87SMark Shellenbaum }
175827dd1e87SMark Shellenbaum
175927dd1e87SMark Shellenbaum int
ace_trivial(ace_t * acep,int aclcnt)176027dd1e87SMark Shellenbaum ace_trivial(ace_t *acep, int aclcnt)
176127dd1e87SMark Shellenbaum {
176227dd1e87SMark Shellenbaum return (ace_trivial_common(acep, aclcnt, ace_walk));
176327dd1e87SMark Shellenbaum }
1764