xref: /freebsd/sys/kern/vfs_acl.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*-
2  * Copyright (c) 1999, 2000, 2001 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 /*
29  * Developed by the TrustedBSD Project.
30  * Support for POSIX.1e access control lists.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/lock.h>
40 #include <sys/namei.h>
41 #include <sys/file.h>
42 #include <sys/proc.h>
43 #include <sys/sysent.h>
44 #include <sys/errno.h>
45 #include <sys/stat.h>
46 #include <sys/acl.h>
47 
48 MALLOC_DEFINE(M_ACL, "acl", "access control list");
49 
50 static int	vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
51 	    struct acl *aclp);
52 static int	vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
53 	    struct acl *aclp);
54 static int	vacl_aclcheck(struct proc *p, struct vnode *vp,
55 	    acl_type_t type, struct acl *aclp);
56 
57 /*
58  * Implement a version of vaccess() that understands POSIX.1e ACL semantics.
59  * Return 0 on success, else an errno value.  Should be merged into
60  * vaccess() eventually.
61  */
62 int
63 vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
64     struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused)
65 {
66 	struct acl_entry *acl_other, *acl_mask;
67 	mode_t dac_granted;
68 	mode_t cap_granted;
69 	mode_t acl_mask_granted;
70 	int group_matched, i;
71 
72 	/*
73 	 * Look for a normal, non-privileged way to access the file/directory
74 	 * as requested.  If it exists, go with that.  Otherwise, attempt
75 	 * to use privileges granted via cap_granted.  In some cases,
76 	 * which privileges to use may be ambiguous due to "best match",
77 	 * in which case fall back on first match for the time being.
78 	 */
79 	if (privused != NULL)
80 		*privused = 0;
81 
82 	/*
83 	 * Determine privileges now, but don't apply until we've found
84 	 * a DAC match that has failed to allow access.
85 	 */
86 #ifndef CAPABILITIES
87 	if (suser_xxx(cred, NULL, PRISON_ROOT) == 0)
88 		cap_granted = (VEXEC | VREAD | VWRITE | VADMIN);
89 	else
90 		cap_granted = 0;
91 #else
92 	cap_granted = 0;
93 
94 	if (type == VDIR) {
95 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
96 		     CAP_DAC_READ_SEARCH, PRISON_ROOT))
97 			cap_granted |= VEXEC;
98 	} else {
99 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
100 		    CAP_DAC_EXECUTE, PRISON_ROOT))
101 			cap_granted |= VEXEC;
102 	}
103 
104 	if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH,
105 	    PRISON_ROOT))
106 		cap_granted |= VREAD;
107 
108 	if ((acc_mode & VWRITE) && !cap_check(cred, NULL, CAP_DAC_WRITE,
109 	    PRISON_ROOT))
110 		cap_granted |= VWRITE;
111 
112 	if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER,
113 	    PRISON_ROOT))
114 		cap_granted |= VADMIN;
115 #endif /* CAPABILITIES */
116 
117 	/*
118 	 * Check the owner.
119 	 * Also, record locations of ACL_MASK and ACL_OTHER for reference
120 	 * later if the owner doesn't match.
121 	 */
122 	acl_mask = acl_other = NULL;
123 	for (i = 0; i < acl->acl_cnt; i++) {
124 		switch (acl->acl_entry[i].ae_tag) {
125 		case ACL_USER_OBJ:
126 			if (file_uid != cred->cr_uid)
127 				break;
128 			dac_granted = 0;
129 			dac_granted |= VADMIN;
130 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
131 				dac_granted |= VEXEC;
132 			if (acl->acl_entry[i].ae_perm & ACL_READ)
133 				dac_granted |= VREAD;
134 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
135 				dac_granted |= VWRITE;
136 			if ((acc_mode & dac_granted) == acc_mode)
137 				return (0);
138 			if ((acc_mode & (dac_granted | cap_granted)) ==
139 			    acc_mode) {
140 				if (privused != NULL)
141 					*privused = 1;
142 				return (0);
143 			}
144 			goto error;
145 
146 		case ACL_MASK:
147 			acl_mask = &acl->acl_entry[i];
148 			break;
149 
150 		case ACL_OTHER:
151 			acl_other = &acl->acl_entry[i];
152 			break;
153 
154 		default:
155 		}
156 	}
157 
158 	/*
159 	 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields
160 	 * are masked by an ACL_MASK entry, if any.  As such, first identify
161 	 * the ACL_MASK field, then iterate through identifying potential
162 	 * user matches, then group matches.  If there is no ACL_MASK,
163 	 * assume that the mask allows all requests to succeed.
164 	 * Also keep track of the location of ACL_OTHER for later consumption.
165 	 */
166 	if (acl_other == NULL) {
167 		/*
168 		 * XXX: This should never happen.  Only properly formatted
169 		 * ACLs should be passed to vaccess_acl_posix1e.
170 		 * Should make this a panic post-debugging.
171 		 */
172 		printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
173 		return (EPERM);
174 	}
175 	if (acl_mask != NULL) {
176 		acl_mask_granted = 0;
177 		if (acl_mask->ae_perm & ACL_EXECUTE)
178 			acl_mask_granted |= VEXEC;
179 		if (acl_mask->ae_perm & ACL_READ)
180 			acl_mask_granted |= VREAD;
181 		if (acl_mask->ae_perm & ACL_WRITE)
182 			acl_mask_granted |= VWRITE;
183 	} else
184 		acl_mask_granted = VEXEC | VREAD | VWRITE;
185 
186 	/*
187 	 * We have to check each type even if we know ACL_MASK will reject,
188 	 * as we need to know what match there might have been, and
189 	 * therefore what further types we might be allowed to check.
190 	 * Do the checks twice -- once without privilege, and a second time
191 	 * with, if there was a match.
192 	 */
193 
194 	/*
195 	 * Check ACL_USER ACL entries.
196 	 */
197 	for (i = 0; i < acl->acl_cnt; i++) {
198 		switch (acl->acl_entry[i].ae_tag) {
199 		case ACL_USER:
200 			if (acl->acl_entry[i].ae_id != cred->cr_uid)
201 				break;
202 			dac_granted = 0;
203 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
204 				dac_granted |= VEXEC;
205 			if (acl->acl_entry[i].ae_perm & ACL_READ)
206 				dac_granted |= VREAD;
207 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
208 				dac_granted |= VWRITE;
209 			dac_granted &= acl_mask_granted;
210 			if ((acc_mode & dac_granted) == acc_mode)
211 				return (0);
212 			if ((acc_mode & (dac_granted | cap_granted)) !=
213 			    acc_mode)
214 				goto error;
215 
216 			if (privused != NULL)
217 				*privused = 1;
218 			return (0);
219 		}
220 	}
221 
222 	/*
223 	 * Group match is best-match, not first-match, so find a
224 	 * "best" match.  Iterate across, testing each potential group
225 	 * match.  Make sure we keep track of whether we found a match
226 	 * or not, so that we know if we can move on to ACL_OTHER.
227 	 */
228 	group_matched = 0;
229 	for (i = 0; i < acl->acl_cnt; i++) {
230 		switch (acl->acl_entry[i].ae_tag) {
231 		case ACL_GROUP_OBJ:
232 			if (!groupmember(file_gid, cred))
233 				break;
234 			dac_granted = 0;
235 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
236 				dac_granted |= VEXEC;
237 			if (acl->acl_entry[i].ae_perm & ACL_READ)
238 				dac_granted |= VREAD;
239 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
240 				dac_granted |= VWRITE;
241 			dac_granted  &= acl_mask_granted;
242 
243 			if ((acc_mode & dac_granted) == acc_mode)
244 				return (0);
245 
246 			group_matched = 1;
247 			break;
248 
249 		case ACL_GROUP:
250 			if (!groupmember(acl->acl_entry[i].ae_id, cred))
251 				break;
252 			dac_granted = 0;
253 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
254 				dac_granted |= VEXEC;
255 			if (acl->acl_entry[i].ae_perm & ACL_READ)
256 				dac_granted |= VREAD;
257 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
258 				dac_granted |= VWRITE;
259 			dac_granted  &= acl_mask_granted;
260 
261 			if ((acc_mode & dac_granted) == acc_mode)
262 				return (0);
263 
264 			group_matched = 1;
265 			break;
266 
267 		default:
268 		}
269 	}
270 
271 	if (group_matched == 1) {
272 		/*
273 		 * There was a match, but it did not grant rights via
274 		 * pure DAC.  Try again, this time with privilege.
275 		 */
276 		for (i = 0; i < acl->acl_cnt; i++) {
277 			switch (acl->acl_entry[i].ae_tag) {
278 			case ACL_GROUP_OBJ:
279 				if (!groupmember(file_gid, cred))
280 					break;
281 				dac_granted = 0;
282 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
283 					dac_granted |= VEXEC;
284 				if (acl->acl_entry[i].ae_perm & ACL_READ)
285 					dac_granted |= VREAD;
286 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
287 					dac_granted |= VWRITE;
288 				dac_granted &= acl_mask_granted;
289 
290 				if ((acc_mode & (dac_granted | cap_granted)) !=
291 				    acc_mode)
292 					break;
293 
294 				if (privused != NULL)
295 					*privused = 1;
296 				return (0);
297 
298 			case ACL_GROUP:
299 				if (!groupmember(acl->acl_entry[i].ae_id,
300 				    cred))
301 					break;
302 				dac_granted = 0;
303 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
304 				dac_granted |= VEXEC;
305 				if (acl->acl_entry[i].ae_perm & ACL_READ)
306 					dac_granted |= VREAD;
307 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
308 					dac_granted |= VWRITE;
309 				dac_granted &= acl_mask_granted;
310 
311 				if ((acc_mode & (dac_granted | cap_granted)) !=
312 				    acc_mode)
313 					break;
314 
315 				if (privused != NULL)
316 					*privused = 1;
317 				return (0);
318 
319 			default:
320 			}
321 		}
322 		/*
323 		 * Even with privilege, group membership was not sufficient.
324 		 * Return failure.
325 		 */
326 		goto error;
327 	}
328 
329 	/*
330 	 * Fall back on ACL_OTHER.  ACL_MASK is not applied to ACL_OTHER.
331 	 */
332 	dac_granted = 0;
333 	if (acl_other->ae_perm & ACL_EXECUTE)
334 		dac_granted |= VEXEC;
335 	if (acl_other->ae_perm & ACL_READ)
336 		dac_granted |= VREAD;
337 	if (acl_other->ae_perm & ACL_WRITE)
338 		dac_granted |= VWRITE;
339 
340 	if ((acc_mode & dac_granted) == acc_mode)
341 		return (0);
342 	if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) {
343 		if (privused != NULL)
344 			*privused = 1;
345 		return (0);
346 	}
347 
348 error:
349 	return ((acc_mode & VADMIN) ? EPERM : EACCES);
350 }
351 
352 /*
353  * For the purposes of file systems maintaining the _OBJ entries in an
354  * inode with a mode_t field, this routine converts a mode_t entry
355  * to an acl_perm_t.
356  */
357 acl_perm_t
358 acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
359 {
360 	acl_perm_t	perm = 0;
361 
362 	switch(tag) {
363 	case ACL_USER_OBJ:
364 		if (mode & S_IXUSR)
365 			perm |= ACL_EXECUTE;
366 		if (mode & S_IRUSR)
367 			perm |= ACL_READ;
368 		if (mode & S_IWUSR)
369 			perm |= ACL_WRITE;
370 		return (perm);
371 
372 	case ACL_GROUP_OBJ:
373 		if (mode & S_IXGRP)
374 			perm |= ACL_EXECUTE;
375 		if (mode & S_IRGRP)
376 			perm |= ACL_READ;
377 		if (mode & S_IWGRP)
378 			perm |= ACL_WRITE;
379 		return (perm);
380 
381 	case ACL_OTHER:
382 		if (mode & S_IXOTH)
383 			perm |= ACL_EXECUTE;
384 		if (mode & S_IROTH)
385 			perm |= ACL_READ;
386 		if (mode & S_IWOTH)
387 			perm |= ACL_WRITE;
388 		return (perm);
389 
390 	default:
391 		printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
392 		return (0);
393 	}
394 }
395 
396 /*
397  * Given inode information (uid, gid, mode), return an acl entry of the
398  * appropriate type.
399  */
400 struct acl_entry
401 acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
402 {
403 	struct acl_entry	acl_entry;
404 
405 	acl_entry.ae_tag = tag;
406 	acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
407 	switch(tag) {
408 	case ACL_USER_OBJ:
409 		acl_entry.ae_id = uid;
410 		break;
411 
412 	case ACL_GROUP_OBJ:
413 		acl_entry.ae_id = gid;
414 		break;
415 
416 	case ACL_OTHER:
417 		acl_entry.ae_id = 0;
418 		break;
419 
420 	default:
421 		acl_entry.ae_id = 0;
422 		printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
423 	}
424 
425 	return (acl_entry);
426 }
427 
428 /*
429  * Utility function to generate a file mode given appropriate ACL entries.
430  */
431 mode_t
432 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
433     struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
434 {
435 	mode_t	mode;
436 
437 	mode = 0;
438 	if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
439 		mode |= S_IXUSR;
440 	if (acl_user_obj_entry->ae_perm & ACL_READ)
441 		mode |= S_IRUSR;
442 	if (acl_user_obj_entry->ae_perm & ACL_WRITE)
443 		mode |= S_IWUSR;
444 	if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
445 		mode |= S_IXGRP;
446 	if (acl_group_obj_entry->ae_perm & ACL_READ)
447 		mode |= S_IRGRP;
448 	if (acl_group_obj_entry->ae_perm & ACL_WRITE)
449 		mode |= S_IWGRP;
450 	if (acl_other_entry->ae_perm & ACL_EXECUTE)
451 		mode |= S_IXOTH;
452 	if (acl_other_entry->ae_perm & ACL_READ)
453 		mode |= S_IROTH;
454 	if (acl_other_entry->ae_perm & ACL_WRITE)
455 		mode |= S_IWOTH;
456 
457 	return (mode);
458 }
459 
460 /*
461  * Perform a syntactic check of the ACL, sufficient to allow an
462  * implementing file system to determine if it should accept this and
463  * rely on the POSIX.1e ACL properties.
464  */
465 int
466 acl_posix1e_check(struct acl *acl)
467 {
468 	int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
469 	int num_acl_mask, num_acl_other, i;
470 
471 	/*
472 	 * Verify that the number of entries does not exceed the maximum
473 	 * defined for acl_t.
474 	 * Verify that the correct number of various sorts of ae_tags are
475 	 * present:
476 	 *   Exactly one ACL_USER_OBJ
477 	 *   Exactly one ACL_GROUP_OBJ
478 	 *   Exactly one ACL_OTHER
479 	 *   If any ACL_USER or ACL_GROUP entries appear, then exactly one
480 	 *   ACL_MASK entry must also appear.
481 	 * Verify that all ae_perm entries are in ACL_PERM_BITS.
482 	 * Verify all ae_tag entries are understood by this implementation.
483 	 * Note: Does not check for uniqueness of qualifier (ae_id) field.
484 	 */
485 	num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
486 	    num_acl_mask = num_acl_other = 0;
487 	if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0)
488 		return (EINVAL);
489 	for (i = 0; i < acl->acl_cnt; i++) {
490 		/*
491 		 * Check for a valid tag.
492 		 */
493 		switch(acl->acl_entry[i].ae_tag) {
494 		case ACL_USER_OBJ:
495 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
496 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
497 				return (EINVAL);
498 			num_acl_user_obj++;
499 			break;
500 		case ACL_GROUP_OBJ:
501 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
502 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
503 				return (EINVAL);
504 			num_acl_group_obj++;
505 			break;
506 		case ACL_USER:
507 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
508 				return (EINVAL);
509 			num_acl_user++;
510 			break;
511 		case ACL_GROUP:
512 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
513 				return (EINVAL);
514 			num_acl_group++;
515 			break;
516 		case ACL_OTHER:
517 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
518 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
519 				return (EINVAL);
520 			num_acl_other++;
521 			break;
522 		case ACL_MASK:
523 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
524 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
525 				return (EINVAL);
526 			num_acl_mask++;
527 			break;
528 		default:
529 			return (EINVAL);
530 		}
531 		/*
532 		 * Check for valid perm entries.
533 		 */
534 		if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
535 		    ACL_PERM_BITS)
536 			return (EINVAL);
537 	}
538 	if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
539 	    (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
540 		return (EINVAL);
541 	if (((num_acl_group != 0) || (num_acl_user != 0)) &&
542 	    (num_acl_mask != 1))
543 		return (EINVAL);
544 	return (0);
545 }
546 
547 /*
548  * These calls wrap the real vnode operations, and are called by the
549  * syscall code once the syscall has converted the path or file
550  * descriptor to a vnode (unlocked).  The aclp pointer is assumed
551  * still to point to userland, so this should not be consumed within
552  * the kernel except by syscall code.  Other code should directly
553  * invoke VOP_{SET,GET}ACL.
554  */
555 
556 /*
557  * Given a vnode, set its ACL.
558  */
559 static int
560 vacl_set_acl(struct proc *p, struct vnode *vp, acl_type_t type,
561     struct acl *aclp)
562 {
563 	struct acl inkernacl;
564 	int error;
565 
566 	error = copyin(aclp, &inkernacl, sizeof(struct acl));
567 	if (error)
568 		return(error);
569 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
570 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
571 	error = VOP_SETACL(vp, type, &inkernacl, p->p_ucred, p);
572 	VOP_UNLOCK(vp, 0, p);
573 	return(error);
574 }
575 
576 /*
577  * Given a vnode, get its ACL.
578  */
579 static int
580 vacl_get_acl(struct proc *p, struct vnode *vp, acl_type_t type,
581     struct acl *aclp)
582 {
583 	struct acl inkernelacl;
584 	int error;
585 
586 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
587 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
588 	error = VOP_GETACL(vp, type, &inkernelacl, p->p_ucred, p);
589 	VOP_UNLOCK(vp, 0, p);
590 	if (error == 0)
591 		error = copyout(&inkernelacl, aclp, sizeof(struct acl));
592 	return (error);
593 }
594 
595 /*
596  * Given a vnode, delete its ACL.
597  */
598 static int
599 vacl_delete(struct proc *p, struct vnode *vp, acl_type_t type)
600 {
601 	int error;
602 
603 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
604 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
605 	error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, p->p_ucred, p);
606 	VOP_UNLOCK(vp, 0, p);
607 	return (error);
608 }
609 
610 /*
611  * Given a vnode, check whether an ACL is appropriate for it
612  */
613 static int
614 vacl_aclcheck(struct proc *p, struct vnode *vp, acl_type_t type,
615     struct acl *aclp)
616 {
617 	struct acl inkernelacl;
618 	int error;
619 
620 	error = copyin(aclp, &inkernelacl, sizeof(struct acl));
621 	if (error)
622 		return(error);
623 	error = VOP_ACLCHECK(vp, type, &inkernelacl, p->p_ucred, p);
624 	return (error);
625 }
626 
627 /*
628  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
629  * Don't need to lock, as the vacl_ code will get/release any locks
630  * required.
631  */
632 
633 /*
634  * Given a file path, get an ACL for it
635  */
636 int
637 __acl_get_file(struct proc *p, struct __acl_get_file_args *uap)
638 {
639 	struct nameidata nd;
640 	int error;
641 
642 	/* what flags are required here -- possible not LOCKLEAF? */
643 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
644 	error = namei(&nd);
645 	if (error)
646 		return(error);
647 	error = vacl_get_acl(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
648 	NDFREE(&nd, 0);
649 	return (error);
650 }
651 
652 /*
653  * Given a file path, set an ACL for it
654  */
655 int
656 __acl_set_file(struct proc *p, struct __acl_set_file_args *uap)
657 {
658 	struct nameidata nd;
659 	int error;
660 
661 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
662 	error = namei(&nd);
663 	if (error)
664 		return(error);
665 	error = vacl_set_acl(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
666 	NDFREE(&nd, 0);
667 	return (error);
668 }
669 
670 /*
671  * Given a file descriptor, get an ACL for it
672  */
673 int
674 __acl_get_fd(struct proc *p, struct __acl_get_fd_args *uap)
675 {
676 	struct file *fp;
677 	int error;
678 
679 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
680 	if (error)
681 		return(error);
682 	return vacl_get_acl(p, (struct vnode *)fp->f_data, SCARG(uap, type),
683 	    SCARG(uap, aclp));
684 }
685 
686 /*
687  * Given a file descriptor, set an ACL for it
688  */
689 int
690 __acl_set_fd(struct proc *p, struct __acl_set_fd_args *uap)
691 {
692 	struct file *fp;
693 	int error;
694 
695 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
696 	if (error)
697 		return(error);
698 	return vacl_set_acl(p, (struct vnode *)fp->f_data, SCARG(uap, type),
699 	    SCARG(uap, aclp));
700 }
701 
702 /*
703  * Given a file path, delete an ACL from it.
704  */
705 int
706 __acl_delete_file(struct proc *p, struct __acl_delete_file_args *uap)
707 {
708 	struct nameidata nd;
709 	int error;
710 
711 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
712 	error = namei(&nd);
713 	if (error)
714 		return(error);
715 	error = vacl_delete(p, nd.ni_vp, SCARG(uap, type));
716 	NDFREE(&nd, 0);
717 	return (error);
718 }
719 
720 /*
721  * Given a file path, delete an ACL from it.
722  */
723 int
724 __acl_delete_fd(struct proc *p, struct __acl_delete_fd_args *uap)
725 {
726 	struct file *fp;
727 	int error;
728 
729 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
730 	if (error)
731 		return(error);
732 	error = vacl_delete(p, (struct vnode *)fp->f_data, SCARG(uap, type));
733 	return (error);
734 }
735 
736 /*
737  * Given a file path, check an ACL for it
738  */
739 int
740 __acl_aclcheck_file(struct proc *p, struct __acl_aclcheck_file_args *uap)
741 {
742 	struct nameidata	nd;
743 	int	error;
744 
745 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
746 	error = namei(&nd);
747 	if (error)
748 		return(error);
749 	error = vacl_aclcheck(p, nd.ni_vp, SCARG(uap, type), SCARG(uap, aclp));
750 	NDFREE(&nd, 0);
751 	return (error);
752 }
753 
754 /*
755  * Given a file descriptor, check an ACL for it
756  */
757 int
758 __acl_aclcheck_fd(struct proc *p, struct __acl_aclcheck_fd_args *uap)
759 {
760 	struct file *fp;
761 	int error;
762 
763 	error = getvnode(p->p_fd, SCARG(uap, filedes), &fp);
764 	if (error)
765 		return(error);
766 	return vacl_aclcheck(p, (struct vnode *)fp->f_data, SCARG(uap, type),
767 	    SCARG(uap, aclp));
768 }
769