xref: /freebsd/sys/kern/subr_acl_nfs4.c (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008-2010 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 /*
30  * ACL support routines specific to NFSv4 access control lists.  These are
31  * utility routines for code common across file systems implementing NFSv4
32  * ACLs.
33  */
34 
35 #ifdef _KERNEL
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/mount.h>
44 #include <sys/priv.h>
45 #include <sys/vnode.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/acl.h>
50 #else
51 #include <errno.h>
52 #include <assert.h>
53 #include <sys/acl.h>
54 #include <sys/stat.h>
55 #define KASSERT(a, b) assert(a)
56 #define CTASSERT(a)
57 
58 #endif /* !_KERNEL */
59 
60 #ifdef _KERNEL
61 
62 static void	acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode);
63 
64 static int	acl_nfs4_old_semantics = 0;
65 
66 SYSCTL_INT(_vfs, OID_AUTO, acl_nfs4_old_semantics, CTLFLAG_RW,
67     &acl_nfs4_old_semantics, 0, "Use pre-PSARC/2010/029 NFSv4 ACL semantics");
68 
69 static struct {
70 	accmode_t accmode;
71 	int mask;
72 } accmode2mask[] = {{VREAD, ACL_READ_DATA},
73 		    {VWRITE, ACL_WRITE_DATA},
74 		    {VAPPEND, ACL_APPEND_DATA},
75 		    {VEXEC, ACL_EXECUTE},
76 		    {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
77 		    {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
78 		    {VDELETE_CHILD, ACL_DELETE_CHILD},
79 		    {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES},
80 		    {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
81 		    {VDELETE, ACL_DELETE},
82 		    {VREAD_ACL, ACL_READ_ACL},
83 		    {VWRITE_ACL, ACL_WRITE_ACL},
84 		    {VWRITE_OWNER, ACL_WRITE_OWNER},
85 		    {VSYNCHRONIZE, ACL_SYNCHRONIZE},
86 		    {0, 0}};
87 
88 static int
89 _access_mask_from_accmode(accmode_t accmode)
90 {
91 	int access_mask = 0, i;
92 
93 	for (i = 0; accmode2mask[i].accmode != 0; i++) {
94 		if (accmode & accmode2mask[i].accmode)
95 			access_mask |= accmode2mask[i].mask;
96 	}
97 
98 	/*
99 	 * VAPPEND is just a modifier for VWRITE; if the caller asked
100 	 * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
101 	 */
102 	if (access_mask & ACL_APPEND_DATA)
103 		access_mask &= ~ACL_WRITE_DATA;
104 
105 	return (access_mask);
106 }
107 
108 /*
109  * Return 0, iff access is allowed, 1 otherwise.
110  */
111 static int
112 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred,
113     int file_uid, int file_gid, int *denied_explicitly)
114 {
115 	int i;
116 	const struct acl_entry *entry;
117 
118 	if (denied_explicitly != NULL)
119 		*denied_explicitly = 0;
120 
121 	KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
122 	    ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
123 
124 	for (i = 0; i < aclp->acl_cnt; i++) {
125 		entry = &(aclp->acl_entry[i]);
126 
127 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
128 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
129 			continue;
130 		if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
131 			continue;
132 		switch (entry->ae_tag) {
133 		case ACL_USER_OBJ:
134 			if (file_uid != cred->cr_uid)
135 				continue;
136 			break;
137 		case ACL_USER:
138 			if (entry->ae_id != cred->cr_uid)
139 				continue;
140 			break;
141 		case ACL_GROUP_OBJ:
142 			if (!groupmember(file_gid, cred))
143 				continue;
144 			break;
145 		case ACL_GROUP:
146 			if (!groupmember(entry->ae_id, cred))
147 				continue;
148 			break;
149 		default:
150 			KASSERT(entry->ae_tag == ACL_EVERYONE,
151 			    ("entry->ae_tag == ACL_EVERYONE"));
152 		}
153 
154 		if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
155 			if (entry->ae_perm & access_mask) {
156 				if (denied_explicitly != NULL)
157 					*denied_explicitly = 1;
158 				return (1);
159 			}
160 		}
161 
162 		access_mask &= ~(entry->ae_perm);
163 		if (access_mask == 0)
164 			return (0);
165 	}
166 
167 	if (access_mask == 0)
168 		return (0);
169 
170 	return (1);
171 }
172 
173 int
174 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid,
175     struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused)
176 {
177 	accmode_t priv_granted = 0;
178 	int denied, explicitly_denied, access_mask, is_directory,
179 	    must_be_owner = 0;
180 	mode_t file_mode = 0;
181 
182 	KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND |
183 	    VEXPLICIT_DENY | VREAD_NAMED_ATTRS | VWRITE_NAMED_ATTRS |
184 	    VDELETE_CHILD | VREAD_ATTRIBUTES | VWRITE_ATTRIBUTES | VDELETE |
185 	    VREAD_ACL | VWRITE_ACL | VWRITE_OWNER | VSYNCHRONIZE)) == 0,
186 	    ("invalid bit in accmode"));
187 	KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
188 	    	("VAPPEND without VWRITE"));
189 
190 	if (privused != NULL)
191 		*privused = 0;
192 
193 	if (accmode & VADMIN)
194 		must_be_owner = 1;
195 
196 	/*
197 	 * Ignore VSYNCHRONIZE permission.
198 	 */
199 	accmode &= ~VSYNCHRONIZE;
200 
201 	access_mask = _access_mask_from_accmode(accmode);
202 
203 	if (type == VDIR)
204 		is_directory = 1;
205 	else
206 		is_directory = 0;
207 
208 	/*
209 	 * File owner is always allowed to read and write the ACL
210 	 * and basic attributes.  This is to prevent a situation
211 	 * where user would change ACL in a way that prevents him
212 	 * from undoing the change.
213 	 */
214 	if (file_uid == cred->cr_uid)
215 		access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
216 		    ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
217 
218 	/*
219 	 * Ignore append permission for regular files; use write
220 	 * permission instead.
221 	 */
222 	if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
223 		access_mask &= ~ACL_APPEND_DATA;
224 		access_mask |= ACL_WRITE_DATA;
225 	}
226 
227 	denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
228 	    &explicitly_denied);
229 
230 	if (must_be_owner) {
231 		if (file_uid != cred->cr_uid)
232 			denied = EPERM;
233 	}
234 
235 	/*
236 	 * For VEXEC, ensure that at least one execute bit is set for
237 	 * non-directories. We have to check the mode here to stay
238 	 * consistent with execve(2). See the test in
239 	 * exec_check_permissions().
240 	 */
241 	acl_nfs4_sync_mode_from_acl(&file_mode, aclp);
242 	if (!denied && !is_directory && (accmode & VEXEC) &&
243 	    (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
244 		denied = EACCES;
245 
246 	if (!denied)
247 		return (0);
248 
249 	/*
250 	 * Access failed.  Iff it was not denied explicitly and
251 	 * VEXPLICIT_DENY flag was specified, allow access.
252 	 */
253 	if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
254 		return (0);
255 
256 	accmode &= ~VEXPLICIT_DENY;
257 
258 	/*
259 	 * No match.  Try to use privileges, if there are any.
260 	 */
261 	if (is_directory) {
262 		if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
263 			priv_granted |= VEXEC;
264 	} else {
265 		/*
266 		 * Ensure that at least one execute bit is on. Otherwise,
267 		 * a privileged user will always succeed, and we don't want
268 		 * this to happen unless the file really is executable.
269 		 */
270 		if ((accmode & VEXEC) && (file_mode &
271 		    (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
272 		    !priv_check_cred(cred, PRIV_VFS_EXEC))
273 			priv_granted |= VEXEC;
274 	}
275 
276 	if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
277 		priv_granted |= VREAD;
278 
279 	if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
280 	    !priv_check_cred(cred, PRIV_VFS_WRITE))
281 		priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
282 
283 	if ((accmode & VADMIN_PERMS) &&
284 	    !priv_check_cred(cred, PRIV_VFS_ADMIN))
285 		priv_granted |= VADMIN_PERMS;
286 
287 	if ((accmode & VSTAT_PERMS) &&
288 	    !priv_check_cred(cred, PRIV_VFS_STAT))
289 		priv_granted |= VSTAT_PERMS;
290 
291 	if ((accmode & priv_granted) == accmode) {
292 		if (privused != NULL)
293 			*privused = 1;
294 
295 		return (0);
296 	}
297 
298 	if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
299 		denied = EPERM;
300 	else
301 		denied = EACCES;
302 
303 	return (denied);
304 }
305 #endif /* _KERNEL */
306 
307 static int
308 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
309     acl_entry_type_t entry_type)
310 {
311 	if (entry->ae_tag != tag)
312 		return (0);
313 
314 	if (entry->ae_id != ACL_UNDEFINED_ID)
315 		return (0);
316 
317 	if (entry->ae_perm != perm)
318 		return (0);
319 
320 	if (entry->ae_entry_type != entry_type)
321 		return (0);
322 
323 	if (entry->ae_flags != 0)
324 		return (0);
325 
326 	return (1);
327 }
328 
329 static struct acl_entry *
330 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
331     acl_entry_type_t entry_type)
332 {
333 	struct acl_entry *entry;
334 
335 	KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
336 	    ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
337 
338 	entry = &(aclp->acl_entry[aclp->acl_cnt]);
339 	aclp->acl_cnt++;
340 
341 	entry->ae_tag = tag;
342 	entry->ae_id = ACL_UNDEFINED_ID;
343 	entry->ae_perm = perm;
344 	entry->ae_entry_type = entry_type;
345 	entry->ae_flags = 0;
346 
347 	return (entry);
348 }
349 
350 static struct acl_entry *
351 _acl_duplicate_entry(struct acl *aclp, int entry_index)
352 {
353 	int i;
354 
355 	KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
356 	    ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
357 
358 	for (i = aclp->acl_cnt; i > entry_index; i--)
359 		aclp->acl_entry[i] = aclp->acl_entry[i - 1];
360 
361 	aclp->acl_cnt++;
362 
363 	return (&(aclp->acl_entry[entry_index + 1]));
364 }
365 
366 static void
367 acl_nfs4_sync_acl_from_mode_draft(struct acl *aclp, mode_t mode,
368     int file_owner_id)
369 {
370 	int i, meets, must_append;
371 	struct acl_entry *entry, *copy, *previous,
372 	    *a1, *a2, *a3, *a4, *a5, *a6;
373 	mode_t amode;
374 	const int READ = 04;
375 	const int WRITE = 02;
376 	const int EXEC = 01;
377 
378 	KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
379 	    ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
380 
381 	/*
382 	 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
383 	 *
384 	 * 3.16.6.3. Applying a Mode to an Existing ACL
385 	 */
386 
387 	/*
388 	 * 1. For each ACE:
389 	 */
390 	for (i = 0; i < aclp->acl_cnt; i++) {
391 		entry = &(aclp->acl_entry[i]);
392 
393 		/*
394 		 * 1.1. If the type is neither ALLOW or DENY - skip.
395 		 */
396 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
397 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
398 			continue;
399 
400 		/*
401 		 * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
402 		 */
403 		if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
404 			continue;
405 
406 		/*
407 		 * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
408 		 *      are set:
409 		 */
410 		if (entry->ae_flags &
411 		    (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
412 			/*
413 			 * 1.3.1. A copy of the current ACE is made, and placed
414 			 *        in the ACL immediately following the current
415 			 *        ACE.
416 			 */
417 			copy = _acl_duplicate_entry(aclp, i);
418 
419 			/*
420 			 * 1.3.2. In the first ACE, the flag
421 			 *        ACL_ENTRY_INHERIT_ONLY is set.
422 			 */
423 			entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
424 
425 			/*
426 			 * 1.3.3. In the second ACE, the following flags
427 			 *        are cleared:
428 			 *        ACL_ENTRY_FILE_INHERIT,
429 			 *        ACL_ENTRY_DIRECTORY_INHERIT,
430 			 *        ACL_ENTRY_NO_PROPAGATE_INHERIT.
431 			 */
432 			copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
433 			    ACL_ENTRY_DIRECTORY_INHERIT |
434 			    ACL_ENTRY_NO_PROPAGATE_INHERIT);
435 
436 			/*
437 			 * The algorithm continues on with the second ACE.
438 			 */
439 			i++;
440 			entry = copy;
441 		}
442 
443 		/*
444 		 * 1.4. If it's owner@, group@ or everyone@ entry, clear
445 		 *      ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
446 		 *      and ACL_EXECUTE.  Continue to the next entry.
447 		 */
448 		if (entry->ae_tag == ACL_USER_OBJ ||
449 		    entry->ae_tag == ACL_GROUP_OBJ ||
450 		    entry->ae_tag == ACL_EVERYONE) {
451 			entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
452 			    ACL_APPEND_DATA | ACL_EXECUTE);
453 			continue;
454 		}
455 
456 		/*
457 		 * 1.5. Otherwise, if the "who" field did not match one
458 		 *      of OWNER@, GROUP@, EVERYONE@:
459 		 *
460 		 * 1.5.1. If the type is ALLOW, check the preceding ACE.
461 		 *        If it does not meet all of the following criteria:
462 		 */
463 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
464 			continue;
465 
466 		meets = 0;
467 		if (i > 0) {
468 			meets = 1;
469 			previous = &(aclp->acl_entry[i - 1]);
470 
471 			/*
472 			 * 1.5.1.1. The type field is DENY,
473 			 */
474 			if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
475 				meets = 0;
476 
477 			/*
478 			 * 1.5.1.2. The "who" field is the same as the current
479 			 *          ACE,
480 			 *
481 			 * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
482 			 *          is the same as it is in the current ACE,
483 			 *          and no other flag bits are set,
484 			 */
485 			if (previous->ae_id != entry->ae_id ||
486 			    previous->ae_tag != entry->ae_tag)
487 				meets = 0;
488 
489 			if (previous->ae_flags)
490 				meets = 0;
491 
492 			/*
493 			 * 1.5.1.4. The mask bits are a subset of the mask bits
494 			 *          of the current ACE, and are also subset of
495 			 *          the following: ACL_READ_DATA,
496 			 *          ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
497 			 */
498 			if (previous->ae_perm & ~(entry->ae_perm))
499 				meets = 0;
500 
501 			if (previous->ae_perm & ~(ACL_READ_DATA |
502 			    ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
503 				meets = 0;
504 		}
505 
506 		if (!meets) {
507 			/*
508 		 	 * Then the ACE of type DENY, with a who equal
509 			 * to the current ACE, flag bits equal to
510 			 * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
511 			 * and no mask bits, is prepended.
512 			 */
513 			previous = entry;
514 			entry = _acl_duplicate_entry(aclp, i);
515 
516 			/* Adjust counter, as we've just added an entry. */
517 			i++;
518 
519 			previous->ae_tag = entry->ae_tag;
520 			previous->ae_id = entry->ae_id;
521 			previous->ae_flags = entry->ae_flags;
522 			previous->ae_perm = 0;
523 			previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
524 		}
525 
526 		/*
527 		 * 1.5.2. The following modifications are made to the prepended
528 		 *        ACE.  The intent is to mask the following ACE
529 		 *        to disallow ACL_READ_DATA, ACL_WRITE_DATA,
530 		 *        ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
531 		 *        permissions of the new mode.  As a special case,
532 		 *        if the ACE matches the current owner of the file,
533 		 *        the owner bits are used, rather than the group bits.
534 		 *        This is reflected in the algorithm below.
535 		 */
536 		amode = mode >> 3;
537 
538 		/*
539 		 * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
540 		 * in ACE matches the owner of the file, we shift amode three
541 		 * more bits, in order to have the owner permission bits
542 		 * placed in the three low order bits of amode.
543 		 */
544 		if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
545 			amode = amode >> 3;
546 
547 		if (entry->ae_perm & ACL_READ_DATA) {
548 			if (amode & READ)
549 				previous->ae_perm &= ~ACL_READ_DATA;
550 			else
551 				previous->ae_perm |= ACL_READ_DATA;
552 		}
553 
554 		if (entry->ae_perm & ACL_WRITE_DATA) {
555 			if (amode & WRITE)
556 				previous->ae_perm &= ~ACL_WRITE_DATA;
557 			else
558 				previous->ae_perm |= ACL_WRITE_DATA;
559 		}
560 
561 		if (entry->ae_perm & ACL_APPEND_DATA) {
562 			if (amode & WRITE)
563 				previous->ae_perm &= ~ACL_APPEND_DATA;
564 			else
565 				previous->ae_perm |= ACL_APPEND_DATA;
566 		}
567 
568 		if (entry->ae_perm & ACL_EXECUTE) {
569 			if (amode & EXEC)
570 				previous->ae_perm &= ~ACL_EXECUTE;
571 			else
572 				previous->ae_perm |= ACL_EXECUTE;
573 		}
574 
575 		/*
576 		 * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
577 		 *        of the ALLOW ace:
578 		 *
579 		 * XXX: This point is not there in the Falkner's draft.
580 		 */
581 		if (entry->ae_tag == ACL_GROUP &&
582 		    entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
583 			mode_t extramode, ownermode;
584 			extramode = (mode >> 3) & 07;
585 			ownermode = mode >> 6;
586 			extramode &= ~ownermode;
587 
588 			if (extramode) {
589 				if (extramode & READ) {
590 					entry->ae_perm &= ~ACL_READ_DATA;
591 					previous->ae_perm &= ~ACL_READ_DATA;
592 				}
593 
594 				if (extramode & WRITE) {
595 					entry->ae_perm &=
596 					    ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
597 					previous->ae_perm &=
598 					    ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
599 				}
600 
601 				if (extramode & EXEC) {
602 					entry->ae_perm &= ~ACL_EXECUTE;
603 					previous->ae_perm &= ~ACL_EXECUTE;
604 				}
605 			}
606 		}
607 	}
608 
609 	/*
610 	 * 2. If there at least six ACEs, the final six ACEs are examined.
611 	 *    If they are not equal to what we want, append six ACEs.
612 	 */
613 	must_append = 0;
614 	if (aclp->acl_cnt < 6) {
615 		must_append = 1;
616 	} else {
617 		a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
618 		a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
619 		a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
620 		a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
621 		a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
622 		a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
623 
624 		if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
625 		    ACL_ENTRY_TYPE_DENY))
626 			must_append = 1;
627 		if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
628 		    ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
629 		    ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
630 			must_append = 1;
631 		if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
632 		    ACL_ENTRY_TYPE_DENY))
633 			must_append = 1;
634 		if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
635 		    ACL_ENTRY_TYPE_ALLOW))
636 			must_append = 1;
637 		if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
638 		    ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
639 		    ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
640 			must_append = 1;
641 		if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
642 		    ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
643 		    ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
644 			must_append = 1;
645 	}
646 
647 	if (must_append) {
648 		KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
649 		    ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
650 
651 		a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
652 		a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
653 		    ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
654 		    ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
655 		a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
656 		a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
657 		a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
658 		    ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
659 		    ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
660 		a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
661 		    ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
662 		    ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
663 
664 		KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
665 		    a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
666 	}
667 
668 	/*
669 	 * 3. The final six ACEs are adjusted according to the incoming mode.
670 	 */
671 	if (mode & S_IRUSR)
672 		a2->ae_perm |= ACL_READ_DATA;
673 	else
674 		a1->ae_perm |= ACL_READ_DATA;
675 	if (mode & S_IWUSR)
676 		a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
677 	else
678 		a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
679 	if (mode & S_IXUSR)
680 		a2->ae_perm |= ACL_EXECUTE;
681 	else
682 		a1->ae_perm |= ACL_EXECUTE;
683 
684 	if (mode & S_IRGRP)
685 		a4->ae_perm |= ACL_READ_DATA;
686 	else
687 		a3->ae_perm |= ACL_READ_DATA;
688 	if (mode & S_IWGRP)
689 		a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
690 	else
691 		a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
692 	if (mode & S_IXGRP)
693 		a4->ae_perm |= ACL_EXECUTE;
694 	else
695 		a3->ae_perm |= ACL_EXECUTE;
696 
697 	if (mode & S_IROTH)
698 		a6->ae_perm |= ACL_READ_DATA;
699 	else
700 		a5->ae_perm |= ACL_READ_DATA;
701 	if (mode & S_IWOTH)
702 		a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
703 	else
704 		a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
705 	if (mode & S_IXOTH)
706 		a6->ae_perm |= ACL_EXECUTE;
707 	else
708 		a5->ae_perm |= ACL_EXECUTE;
709 }
710 
711 #ifdef _KERNEL
712 void
713 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode,
714     int file_owner_id)
715 {
716 
717 	if (acl_nfs4_old_semantics)
718 		acl_nfs4_sync_acl_from_mode_draft(aclp, mode, file_owner_id);
719 	else
720 		acl_nfs4_trivial_from_mode(aclp, mode);
721 }
722 #endif /* _KERNEL */
723 
724 void
725 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
726 {
727 	int i;
728 	mode_t old_mode = *_mode, mode = 0, seen = 0;
729 	const struct acl_entry *entry;
730 
731 	KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
732 	    ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
733 
734 	/*
735 	 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
736 	 *
737 	 * 3.16.6.1. Recomputing mode upon SETATTR of ACL
738 	 */
739 
740 	for (i = 0; i < aclp->acl_cnt; i++) {
741 		entry = &(aclp->acl_entry[i]);
742 
743 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
744 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
745 			continue;
746 
747 		if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
748 			continue;
749 
750 		if (entry->ae_tag == ACL_USER_OBJ) {
751 			if ((entry->ae_perm & ACL_READ_DATA) &&
752 			    ((seen & S_IRUSR) == 0)) {
753 				seen |= S_IRUSR;
754 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
755 					mode |= S_IRUSR;
756 			}
757 			if ((entry->ae_perm & ACL_WRITE_DATA) &&
758 			     ((seen & S_IWUSR) == 0)) {
759 				seen |= S_IWUSR;
760 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
761 					mode |= S_IWUSR;
762 			}
763 			if ((entry->ae_perm & ACL_EXECUTE) &&
764 			    ((seen & S_IXUSR) == 0)) {
765 				seen |= S_IXUSR;
766 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
767 					mode |= S_IXUSR;
768 			}
769 		} else if (entry->ae_tag == ACL_GROUP_OBJ) {
770 			if ((entry->ae_perm & ACL_READ_DATA) &&
771 			    ((seen & S_IRGRP) == 0)) {
772 				seen |= S_IRGRP;
773 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
774 					mode |= S_IRGRP;
775 			}
776 			if ((entry->ae_perm & ACL_WRITE_DATA) &&
777 			    ((seen & S_IWGRP) == 0)) {
778 				seen |= S_IWGRP;
779 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
780 					mode |= S_IWGRP;
781 			}
782 			if ((entry->ae_perm & ACL_EXECUTE) &&
783 			    ((seen & S_IXGRP) == 0)) {
784 				seen |= S_IXGRP;
785 				if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
786 					mode |= S_IXGRP;
787 			}
788 		} else if (entry->ae_tag == ACL_EVERYONE) {
789 			if (entry->ae_perm & ACL_READ_DATA) {
790 				if ((seen & S_IRUSR) == 0) {
791 					seen |= S_IRUSR;
792 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
793 						mode |= S_IRUSR;
794 				}
795 				if ((seen & S_IRGRP) == 0) {
796 					seen |= S_IRGRP;
797 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
798 						mode |= S_IRGRP;
799 				}
800 				if ((seen & S_IROTH) == 0) {
801 					seen |= S_IROTH;
802 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
803 						mode |= S_IROTH;
804 				}
805 			}
806 			if (entry->ae_perm & ACL_WRITE_DATA) {
807 				if ((seen & S_IWUSR) == 0) {
808 					seen |= S_IWUSR;
809 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
810 						mode |= S_IWUSR;
811 				}
812 				if ((seen & S_IWGRP) == 0) {
813 					seen |= S_IWGRP;
814 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
815 						mode |= S_IWGRP;
816 				}
817 				if ((seen & S_IWOTH) == 0) {
818 					seen |= S_IWOTH;
819 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
820 						mode |= S_IWOTH;
821 				}
822 			}
823 			if (entry->ae_perm & ACL_EXECUTE) {
824 				if ((seen & S_IXUSR) == 0) {
825 					seen |= S_IXUSR;
826 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
827 						mode |= S_IXUSR;
828 				}
829 				if ((seen & S_IXGRP) == 0) {
830 					seen |= S_IXGRP;
831 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
832 						mode |= S_IXGRP;
833 				}
834 				if ((seen & S_IXOTH) == 0) {
835 					seen |= S_IXOTH;
836 					if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
837 						mode |= S_IXOTH;
838 				}
839 			}
840 		}
841 	}
842 
843 	*_mode = mode | (old_mode & ACL_PRESERVE_MASK);
844 }
845 
846 #ifdef _KERNEL
847 /*
848  * Calculate inherited ACL in a manner compatible with NFSv4 Minor Version 1,
849  * draft-ietf-nfsv4-minorversion1-03.txt.
850  */
851 static void
852 acl_nfs4_compute_inherited_acl_draft(const struct acl *parent_aclp,
853     struct acl *child_aclp, mode_t mode, int file_owner_id,
854     int is_directory)
855 {
856 	int i, flags;
857 	const struct acl_entry *parent_entry;
858 	struct acl_entry *entry, *copy;
859 
860 	KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
861 	KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
862 	    ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
863 
864 	/*
865 	 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
866 	 *
867 	 * 3.16.6.2. Applying the mode given to CREATE or OPEN
868 	 *           to an inherited ACL
869 	 */
870 
871 	/*
872 	 * 1. Form an ACL that is the concatenation of all inheritable ACEs.
873 	 */
874 	for (i = 0; i < parent_aclp->acl_cnt; i++) {
875 		parent_entry = &(parent_aclp->acl_entry[i]);
876 		flags = parent_entry->ae_flags;
877 
878 		/*
879 		 * Entry is not inheritable at all.
880 		 */
881 		if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
882 		    ACL_ENTRY_FILE_INHERIT)) == 0)
883 			continue;
884 
885 		/*
886 		 * We're creating a file, but entry is not inheritable
887 		 * by files.
888 		 */
889 		if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
890 			continue;
891 
892 		/*
893 		 * Entry is inheritable only by files, but has NO_PROPAGATE
894 		 * flag set, and we're creating a directory, so it wouldn't
895 		 * propagate to any file in that directory anyway.
896 		 */
897 		if (is_directory &&
898 		    (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
899 		    (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
900 			continue;
901 
902 		KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
903 		    ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
904 		child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
905 		child_aclp->acl_cnt++;
906 	}
907 
908 	/*
909 	 * 2. For each entry in the new ACL, adjust its flags, possibly
910 	 *    creating two entries in place of one.
911 	 */
912 	for (i = 0; i < child_aclp->acl_cnt; i++) {
913 		entry = &(child_aclp->acl_entry[i]);
914 
915 		/*
916 		 * This is not in the specification, but SunOS
917 		 * apparently does that.
918 		 */
919 		if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
920 		    !is_directory) &&
921 		    entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
922 			entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
923 
924 		/*
925 		 * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
926 		 *      being created is not a directory, then clear the
927 		 *      following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
928 		 *      ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
929 		 *      ACL_ENTRY_INHERIT_ONLY.
930 		 */
931 		if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
932 		    !is_directory) {
933 			entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
934 			ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
935 			ACL_ENTRY_INHERIT_ONLY);
936 
937 			/*
938 			 * Continue on to the next ACE.
939 			 */
940 			continue;
941 		}
942 
943 		/*
944 		 * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
945 		 *      is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
946 		 *      that ACL_ENTRY_INHERIT_ONLY is set.  Continue to the
947 		 *      next ACE.  Otherwise...
948 		 */
949 		/*
950 		 * XXX: Read it again and make sure what does the "otherwise"
951 		 *      apply to.
952 		 */
953 		if (is_directory &&
954 		    (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
955 		    ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
956 			entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
957 			continue;
958 		}
959 
960 		/*
961 		 * 2.C. If the type of the ACE is neither ALLOW nor deny,
962 		 *      then continue.
963 		 */
964 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
965 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
966 			continue;
967 
968 		/*
969 		 * 2.D. Copy the original ACE into a second, adjacent ACE.
970 		 */
971 		copy = _acl_duplicate_entry(child_aclp, i);
972 
973 		/*
974 		 * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
975 		 *      is set.
976 		 */
977 		entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
978 
979 		/*
980 		 * 2.F. On the second ACE, clear the following flags:
981 		 *      ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
982 		 *      ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
983 		 */
984 		copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
985 		    ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
986 		    ACL_ENTRY_INHERIT_ONLY);
987 
988 		/*
989 		 * 2.G. On the second ACE, if the type is ALLOW,
990 		 *      an implementation MAY clear the following
991 		 *      mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
992 		 */
993 		if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
994 			copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
995 
996 		/*
997 		 * Increment the counter to skip the copied entry.
998 		 */
999 		i++;
1000 	}
1001 
1002 	/*
1003 	 * 3. To ensure that the mode is honored, apply the algorithm describe
1004 	 *    in Section 2.16.6.3, using the mode that is to be used for file
1005 	 *    creation.
1006 	 */
1007 	acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
1008 }
1009 #endif /* _KERNEL */
1010 
1011 /*
1012  * Populate the ACL with entries inherited from parent_aclp.
1013  */
1014 static void
1015 acl_nfs4_inherit_entries(const struct acl *parent_aclp,
1016     struct acl *child_aclp, mode_t mode, int file_owner_id,
1017     int is_directory)
1018 {
1019 	int i, flags, tag;
1020 	const struct acl_entry *parent_entry;
1021 	struct acl_entry *entry;
1022 
1023 	KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
1024 	    ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
1025 
1026 	for (i = 0; i < parent_aclp->acl_cnt; i++) {
1027 		parent_entry = &(parent_aclp->acl_entry[i]);
1028 		flags = parent_entry->ae_flags;
1029 		tag = parent_entry->ae_tag;
1030 
1031 		/*
1032 		 * Don't inherit owner@, group@, or everyone@ entries.
1033 		 */
1034 		if (tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ ||
1035 		    tag == ACL_EVERYONE)
1036 			continue;
1037 
1038 		/*
1039 		 * Entry is not inheritable at all.
1040 		 */
1041 		if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
1042 		    ACL_ENTRY_FILE_INHERIT)) == 0)
1043 			continue;
1044 
1045 		/*
1046 		 * We're creating a file, but entry is not inheritable
1047 		 * by files.
1048 		 */
1049 		if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
1050 			continue;
1051 
1052 		/*
1053 		 * Entry is inheritable only by files, but has NO_PROPAGATE
1054 		 * flag set, and we're creating a directory, so it wouldn't
1055 		 * propagate to any file in that directory anyway.
1056 		 */
1057 		if (is_directory &&
1058 		    (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
1059 		    (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
1060 			continue;
1061 
1062 		/*
1063 		 * Entry qualifies for being inherited.
1064 		 */
1065 		KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
1066 		    ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
1067 		entry = &(child_aclp->acl_entry[child_aclp->acl_cnt]);
1068 		*entry = *parent_entry;
1069 		child_aclp->acl_cnt++;
1070 
1071 		entry->ae_flags &= ~ACL_ENTRY_INHERIT_ONLY;
1072 		entry->ae_flags |= ACL_ENTRY_INHERITED;
1073 
1074 		/*
1075 		 * If the type of the ACE is neither ALLOW nor DENY,
1076 		 * then leave it as it is and proceed to the next one.
1077 		 */
1078 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1079 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1080 			continue;
1081 
1082 		/*
1083 		 * If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if
1084 		 * the object being created is not a directory, then clear
1085 		 * the following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
1086 		 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
1087 		 * ACL_ENTRY_INHERIT_ONLY.
1088 		 */
1089 		if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
1090 		    !is_directory) {
1091 			entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
1092 			ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
1093 			ACL_ENTRY_INHERIT_ONLY);
1094 		}
1095 
1096 		/*
1097 		 * If the object is a directory and ACL_ENTRY_FILE_INHERIT
1098 		 * is set, but ACL_ENTRY_DIRECTORY_INHERIT is not set, ensure
1099 		 * that ACL_ENTRY_INHERIT_ONLY is set.
1100 		 */
1101 		if (is_directory &&
1102 		    (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
1103 		    ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
1104 			entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
1105 		}
1106 
1107 		if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW &&
1108 		    (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) == 0) {
1109 			/*
1110 			 * Some permissions must never be inherited.
1111 			 */
1112 			entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
1113 			    ACL_WRITE_NAMED_ATTRS | ACL_WRITE_ATTRIBUTES);
1114 
1115 			/*
1116 			 * Others must be masked according to the file mode.
1117 			 */
1118 			if ((mode & S_IRGRP) == 0)
1119 				entry->ae_perm &= ~ACL_READ_DATA;
1120 			if ((mode & S_IWGRP) == 0)
1121 				entry->ae_perm &=
1122 				    ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
1123 			if ((mode & S_IXGRP) == 0)
1124 				entry->ae_perm &= ~ACL_EXECUTE;
1125 		}
1126 	}
1127 }
1128 
1129 /*
1130  * Calculate inherited ACL in a manner compatible with PSARC/2010/029.
1131  * It's also being used to calculate a trivial ACL, by inheriting from
1132  * a NULL ACL.
1133  */
1134 static void
1135 acl_nfs4_compute_inherited_acl_psarc(const struct acl *parent_aclp,
1136     struct acl *aclp, mode_t mode, int file_owner_id, int is_directory)
1137 {
1138 	acl_perm_t user_allow_first = 0, user_deny = 0, group_deny = 0;
1139 	acl_perm_t user_allow, group_allow, everyone_allow;
1140 
1141 	KASSERT(aclp->acl_cnt == 0, ("aclp->acl_cnt == 0"));
1142 
1143 	user_allow = group_allow = everyone_allow = ACL_READ_ACL |
1144 	    ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | ACL_SYNCHRONIZE;
1145 	user_allow |= ACL_WRITE_ACL | ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
1146 	    ACL_WRITE_NAMED_ATTRS;
1147 
1148 	if (mode & S_IRUSR)
1149 		user_allow |= ACL_READ_DATA;
1150 	if (mode & S_IWUSR)
1151 		user_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1152 	if (mode & S_IXUSR)
1153 		user_allow |= ACL_EXECUTE;
1154 
1155 	if (mode & S_IRGRP)
1156 		group_allow |= ACL_READ_DATA;
1157 	if (mode & S_IWGRP)
1158 		group_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1159 	if (mode & S_IXGRP)
1160 		group_allow |= ACL_EXECUTE;
1161 
1162 	if (mode & S_IROTH)
1163 		everyone_allow |= ACL_READ_DATA;
1164 	if (mode & S_IWOTH)
1165 		everyone_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
1166 	if (mode & S_IXOTH)
1167 		everyone_allow |= ACL_EXECUTE;
1168 
1169 	user_deny = ((group_allow | everyone_allow) & ~user_allow);
1170 	group_deny = everyone_allow & ~group_allow;
1171 	user_allow_first = group_deny & ~user_deny;
1172 
1173 	if (user_allow_first != 0)
1174 		_acl_append(aclp, ACL_USER_OBJ, user_allow_first,
1175 		    ACL_ENTRY_TYPE_ALLOW);
1176 	if (user_deny != 0)
1177 		_acl_append(aclp, ACL_USER_OBJ, user_deny,
1178 		    ACL_ENTRY_TYPE_DENY);
1179 	if (group_deny != 0)
1180 		_acl_append(aclp, ACL_GROUP_OBJ, group_deny,
1181 		    ACL_ENTRY_TYPE_DENY);
1182 
1183 	if (parent_aclp != NULL)
1184 		acl_nfs4_inherit_entries(parent_aclp, aclp, mode,
1185 		    file_owner_id, is_directory);
1186 
1187 	_acl_append(aclp, ACL_USER_OBJ, user_allow, ACL_ENTRY_TYPE_ALLOW);
1188 	_acl_append(aclp, ACL_GROUP_OBJ, group_allow, ACL_ENTRY_TYPE_ALLOW);
1189 	_acl_append(aclp, ACL_EVERYONE, everyone_allow, ACL_ENTRY_TYPE_ALLOW);
1190 }
1191 
1192 #ifdef _KERNEL
1193 void
1194 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
1195     struct acl *child_aclp, mode_t mode, int file_owner_id,
1196     int is_directory)
1197 {
1198 
1199 	if (acl_nfs4_old_semantics)
1200 		acl_nfs4_compute_inherited_acl_draft(parent_aclp, child_aclp,
1201 		    mode, file_owner_id, is_directory);
1202 	else
1203 		acl_nfs4_compute_inherited_acl_psarc(parent_aclp, child_aclp,
1204 		    mode, file_owner_id, is_directory);
1205 }
1206 #endif /* _KERNEL */
1207 
1208 /*
1209  * Calculate trivial ACL in a manner compatible with PSARC/2010/029.
1210  * Note that this results in an ACL different from (but semantically
1211  * equal to) the "canonical six" trivial ACL computed using algorithm
1212  * described in draft-ietf-nfsv4-minorversion1-03.txt, 3.16.6.2.
1213  */
1214 static void
1215 acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode)
1216 {
1217 
1218 	aclp->acl_cnt = 0;
1219 	acl_nfs4_compute_inherited_acl_psarc(NULL, aclp, mode, -1, -1);
1220 }
1221 
1222 #ifndef _KERNEL
1223 /*
1224  * This routine is used by libc to implement acl_strip_np(3)
1225  * and acl_is_trivial_np(3).
1226  */
1227 void
1228 acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int mode, int canonical_six)
1229 {
1230 
1231 	aclp->acl_cnt = 0;
1232 	if (canonical_six)
1233 		acl_nfs4_sync_acl_from_mode_draft(aclp, mode, -1);
1234 	else
1235 		acl_nfs4_trivial_from_mode(aclp, mode);
1236 }
1237 #endif /* !_KERNEL */
1238 
1239 #ifdef _KERNEL
1240 static int
1241 _acls_are_equal(const struct acl *a, const struct acl *b)
1242 {
1243 	int i;
1244 	const struct acl_entry *entrya, *entryb;
1245 
1246 	if (a->acl_cnt != b->acl_cnt)
1247 		return (0);
1248 
1249 	for (i = 0; i < b->acl_cnt; i++) {
1250 		entrya = &(a->acl_entry[i]);
1251 		entryb = &(b->acl_entry[i]);
1252 
1253 		if (entrya->ae_tag != entryb->ae_tag ||
1254 		    entrya->ae_id != entryb->ae_id ||
1255 		    entrya->ae_perm != entryb->ae_perm ||
1256 		    entrya->ae_entry_type != entryb->ae_entry_type ||
1257 		    entrya->ae_flags != entryb->ae_flags)
1258 			return (0);
1259 	}
1260 
1261 	return (1);
1262 }
1263 
1264 /*
1265  * This routine is used to determine whether to remove extended attribute
1266  * that stores ACL contents.
1267  */
1268 int
1269 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
1270 {
1271 	int trivial;
1272 	mode_t tmpmode = 0;
1273 	struct acl *tmpaclp;
1274 
1275 	if (aclp->acl_cnt > 6)
1276 		return (0);
1277 
1278 	/*
1279 	 * Compute the mode from the ACL, then compute new ACL from that mode.
1280 	 * If the ACLs are identical, then the ACL is trivial.
1281 	 *
1282 	 * XXX: I guess there is a faster way to do this.  However, even
1283 	 *      this slow implementation significantly speeds things up
1284 	 *      for files that don't have non-trivial ACLs - it's critical
1285 	 *      for performance to not use EA when they are not needed.
1286 	 *
1287 	 * First try the PSARC/2010/029 semantics.
1288 	 */
1289 	tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
1290 	acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
1291 	acl_nfs4_trivial_from_mode(tmpaclp, tmpmode);
1292 	trivial = _acls_are_equal(aclp, tmpaclp);
1293 	if (trivial) {
1294 		acl_free(tmpaclp);
1295 		return (trivial);
1296 	}
1297 
1298 	/*
1299 	 * Check if it's a draft-ietf-nfsv4-minorversion1-03.txt trivial ACL.
1300 	 */
1301 	tmpaclp->acl_cnt = 0;
1302 	acl_nfs4_sync_acl_from_mode_draft(tmpaclp, tmpmode, file_owner_id);
1303 	trivial = _acls_are_equal(aclp, tmpaclp);
1304 	acl_free(tmpaclp);
1305 
1306 	return (trivial);
1307 }
1308 #endif /* _KERNEL */
1309 
1310 int
1311 acl_nfs4_check(const struct acl *aclp, int is_directory)
1312 {
1313 	int i;
1314 	const struct acl_entry *entry;
1315 
1316 	/*
1317 	 * The spec doesn't seem to say anything about ACL validity.
1318 	 * It seems there is not much to do here.  There is even no need
1319 	 * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
1320 	 * entries, as there can be several of them and that's perfectly
1321 	 * valid.  There can be none of them too.  Really.
1322 	 */
1323 
1324 	if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
1325 		return (EINVAL);
1326 
1327 	for (i = 0; i < aclp->acl_cnt; i++) {
1328 		entry = &(aclp->acl_entry[i]);
1329 
1330 		switch (entry->ae_tag) {
1331 		case ACL_USER_OBJ:
1332 		case ACL_GROUP_OBJ:
1333 		case ACL_EVERYONE:
1334 			if (entry->ae_id != ACL_UNDEFINED_ID)
1335 				return (EINVAL);
1336 			break;
1337 
1338 		case ACL_USER:
1339 		case ACL_GROUP:
1340 			if (entry->ae_id == ACL_UNDEFINED_ID)
1341 				return (EINVAL);
1342 			break;
1343 
1344 		default:
1345 			return (EINVAL);
1346 		}
1347 
1348 		if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
1349 			return (EINVAL);
1350 
1351 		/*
1352 		 * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
1353 		 */
1354 		if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1355 		    entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1356 			return (EINVAL);
1357 
1358 		if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
1359 			return (EINVAL);
1360 
1361 		/* Disallow unimplemented flags. */
1362 		if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
1363 		    ACL_ENTRY_FAILED_ACCESS))
1364 			return (EINVAL);
1365 
1366 		/* Disallow flags not allowed for ordinary files. */
1367 		if (!is_directory) {
1368 			if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
1369 			    ACL_ENTRY_DIRECTORY_INHERIT |
1370 			    ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
1371 				return (EINVAL);
1372 		}
1373 	}
1374 
1375 	return (0);
1376 }
1377 
1378 #ifdef	_KERNEL
1379 static int
1380 acl_nfs4_modload(module_t module, int what, void *arg)
1381 {
1382 	int ret;
1383 
1384 	ret = 0;
1385 
1386 	switch (what) {
1387 	case MOD_LOAD:
1388 	case MOD_SHUTDOWN:
1389 		break;
1390 
1391 	case MOD_QUIESCE:
1392 		/* XXX TODO */
1393 		ret = 0;
1394 		break;
1395 
1396 	case MOD_UNLOAD:
1397 		/* XXX TODO */
1398 		ret = 0;
1399 		break;
1400 	default:
1401 		ret = EINVAL;
1402 		break;
1403 	}
1404 
1405 	return (ret);
1406 }
1407 
1408 static moduledata_t acl_nfs4_mod = {
1409 	"acl_nfs4",
1410 	acl_nfs4_modload,
1411 	NULL
1412 };
1413 
1414 /*
1415  * XXX TODO: which subsystem, order?
1416  */
1417 DECLARE_MODULE(acl_nfs4, acl_nfs4_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1418 MODULE_VERSION(acl_nfs4, 1);
1419 #endif	/* _KERNEL */
1420