xref: /freebsd/sys/kern/subr_acl_posix1e.c (revision 6d8785434f17c00afaa247d8aee005ed9aafac32)
191f37dcbSRobert Watson /*-
248be932aSRobert Watson  * Copyright (c) 1999-2001 Robert N. M. Watson
391f37dcbSRobert Watson  * All rights reserved.
491f37dcbSRobert Watson  *
56d878543SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
66d878543SRobert Watson  *
791f37dcbSRobert Watson  * Redistribution and use in source and binary forms, with or without
891f37dcbSRobert Watson  * modification, are permitted provided that the following conditions
991f37dcbSRobert Watson  * are met:
1091f37dcbSRobert Watson  * 1. Redistributions of source code must retain the above copyright
1191f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer.
1291f37dcbSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1391f37dcbSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1491f37dcbSRobert Watson  *    documentation and/or other materials provided with the distribution.
1591f37dcbSRobert Watson  *
1691f37dcbSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1791f37dcbSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1891f37dcbSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1991f37dcbSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2091f37dcbSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2191f37dcbSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2291f37dcbSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2391f37dcbSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2491f37dcbSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2591f37dcbSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2691f37dcbSRobert Watson  * SUCH DAMAGE.
2791f37dcbSRobert Watson  *
2891f37dcbSRobert Watson  * $FreeBSD$
2991f37dcbSRobert Watson  */
3091f37dcbSRobert Watson /*
315293465fSRobert Watson  * Developed by the TrustedBSD Project.
325293465fSRobert Watson  * Support for POSIX.1e access control lists.
3391f37dcbSRobert Watson  */
3491f37dcbSRobert Watson 
3591f37dcbSRobert Watson #include <sys/param.h>
3691f37dcbSRobert Watson #include <sys/systm.h>
3791f37dcbSRobert Watson #include <sys/sysproto.h>
3891f37dcbSRobert Watson #include <sys/kernel.h>
3991f37dcbSRobert Watson #include <sys/malloc.h>
4091f37dcbSRobert Watson #include <sys/vnode.h>
4191f37dcbSRobert Watson #include <sys/lock.h>
42f708f4d1SMatthew Dillon #include <sys/mutex.h>
4391f37dcbSRobert Watson #include <sys/namei.h>
4491f37dcbSRobert Watson #include <sys/file.h>
4591f37dcbSRobert Watson #include <sys/proc.h>
4691f37dcbSRobert Watson #include <sys/sysent.h>
4791f37dcbSRobert Watson #include <sys/errno.h>
4891f37dcbSRobert Watson #include <sys/stat.h>
4991f37dcbSRobert Watson #include <sys/acl.h>
5091f37dcbSRobert Watson 
515293465fSRobert Watson MALLOC_DEFINE(M_ACL, "acl", "access control list");
5291f37dcbSRobert Watson 
53b40ce416SJulian Elischer static int	vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
5491f37dcbSRobert Watson 	    struct acl *aclp);
55b40ce416SJulian Elischer static int	vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
5691f37dcbSRobert Watson 	    struct acl *aclp);
57b40ce416SJulian Elischer static int	vacl_aclcheck(struct thread *td, struct vnode *vp,
58b114e127SRobert Watson 	    acl_type_t type, struct acl *aclp);
5991f37dcbSRobert Watson 
6091f37dcbSRobert Watson /*
615293465fSRobert Watson  * Implement a version of vaccess() that understands POSIX.1e ACL semantics.
625293465fSRobert Watson  * Return 0 on success, else an errno value.  Should be merged into
635293465fSRobert Watson  * vaccess() eventually.
645293465fSRobert Watson  */
655293465fSRobert Watson int
66b114e127SRobert Watson vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid,
67b114e127SRobert Watson     struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused)
685293465fSRobert Watson {
695293465fSRobert Watson 	struct acl_entry *acl_other, *acl_mask;
705293465fSRobert Watson 	mode_t dac_granted;
715293465fSRobert Watson 	mode_t cap_granted;
725293465fSRobert Watson 	mode_t acl_mask_granted;
735293465fSRobert Watson 	int group_matched, i;
745293465fSRobert Watson 
755293465fSRobert Watson 	/*
765293465fSRobert Watson 	 * Look for a normal, non-privileged way to access the file/directory
775293465fSRobert Watson 	 * as requested.  If it exists, go with that.  Otherwise, attempt
785293465fSRobert Watson 	 * to use privileges granted via cap_granted.  In some cases,
795293465fSRobert Watson 	 * which privileges to use may be ambiguous due to "best match",
805293465fSRobert Watson 	 * in which case fall back on first match for the time being.
815293465fSRobert Watson 	 */
825293465fSRobert Watson 	if (privused != NULL)
835293465fSRobert Watson 		*privused = 0;
845293465fSRobert Watson 
855293465fSRobert Watson 	/*
865293465fSRobert Watson 	 * Determine privileges now, but don't apply until we've found
87670f6b2fSRobert Watson 	 * a DAC entry that matches but has failed to allow access.
885293465fSRobert Watson 	 */
895293465fSRobert Watson #ifndef CAPABILITIES
905293465fSRobert Watson 	if (suser_xxx(cred, NULL, PRISON_ROOT) == 0)
915293465fSRobert Watson 		cap_granted = (VEXEC | VREAD | VWRITE | VADMIN);
925293465fSRobert Watson 	else
935293465fSRobert Watson 		cap_granted = 0;
945293465fSRobert Watson #else
955293465fSRobert Watson 	cap_granted = 0;
965293465fSRobert Watson 
975293465fSRobert Watson 	if (type == VDIR) {
985293465fSRobert Watson 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
995293465fSRobert Watson 		     CAP_DAC_READ_SEARCH, PRISON_ROOT))
1005293465fSRobert Watson 			cap_granted |= VEXEC;
1015293465fSRobert Watson 	} else {
1025293465fSRobert Watson 		if ((acc_mode & VEXEC) && !cap_check(cred, NULL,
1035293465fSRobert Watson 		    CAP_DAC_EXECUTE, PRISON_ROOT))
1045293465fSRobert Watson 			cap_granted |= VEXEC;
1055293465fSRobert Watson 	}
1065293465fSRobert Watson 
1075293465fSRobert Watson 	if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH,
1085293465fSRobert Watson 	    PRISON_ROOT))
1095293465fSRobert Watson 		cap_granted |= VREAD;
1105293465fSRobert Watson 
1115293465fSRobert Watson 	if ((acc_mode & VWRITE) && !cap_check(cred, NULL, CAP_DAC_WRITE,
1125293465fSRobert Watson 	    PRISON_ROOT))
1135293465fSRobert Watson 		cap_granted |= VWRITE;
1145293465fSRobert Watson 
1155293465fSRobert Watson 	if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER,
1165293465fSRobert Watson 	    PRISON_ROOT))
1175293465fSRobert Watson 		cap_granted |= VADMIN;
1185293465fSRobert Watson #endif /* CAPABILITIES */
1195293465fSRobert Watson 
1205293465fSRobert Watson 	/*
121670f6b2fSRobert Watson 	 * The owner matches if the effective uid associated with the
122670f6b2fSRobert Watson 	 * credential matches that of the ACL_USER_OBJ entry.  While we're
123670f6b2fSRobert Watson 	 * doing the first scan, also cache the location of the ACL_MASK
124670f6b2fSRobert Watson 	 * and ACL_OTHER entries, preventing some future iterations.
1255293465fSRobert Watson 	 */
1265293465fSRobert Watson 	acl_mask = acl_other = NULL;
1275293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
1285293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
1295293465fSRobert Watson 		case ACL_USER_OBJ:
130b114e127SRobert Watson 			if (file_uid != cred->cr_uid)
1315293465fSRobert Watson 				break;
1325293465fSRobert Watson 			dac_granted = 0;
1335293465fSRobert Watson 			dac_granted |= VADMIN;
134fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
1355293465fSRobert Watson 				dac_granted |= VEXEC;
136fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
1375293465fSRobert Watson 				dac_granted |= VREAD;
138fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
1395293465fSRobert Watson 				dac_granted |= VWRITE;
1405293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
1415293465fSRobert Watson 				return (0);
1425293465fSRobert Watson 			if ((acc_mode & (dac_granted | cap_granted)) ==
1435293465fSRobert Watson 			    acc_mode) {
1445293465fSRobert Watson 				if (privused != NULL)
1455293465fSRobert Watson 					*privused = 1;
1465293465fSRobert Watson 				return (0);
1475293465fSRobert Watson 			}
1485293465fSRobert Watson 			goto error;
1495293465fSRobert Watson 
1505293465fSRobert Watson 		case ACL_MASK:
1515293465fSRobert Watson 			acl_mask = &acl->acl_entry[i];
1525293465fSRobert Watson 			break;
1535293465fSRobert Watson 
1545293465fSRobert Watson 		case ACL_OTHER:
1555293465fSRobert Watson 			acl_other = &acl->acl_entry[i];
1565293465fSRobert Watson 			break;
1575293465fSRobert Watson 
1585293465fSRobert Watson 		default:
1595293465fSRobert Watson 		}
1605293465fSRobert Watson 	}
1615293465fSRobert Watson 
1625293465fSRobert Watson 	/*
163670f6b2fSRobert Watson 	 * An ACL_OTHER entry should always exist in a valid access
164670f6b2fSRobert Watson 	 * ACL.  If it doesn't, then generate a serious failure.  For now,
165670f6b2fSRobert Watson 	 * this means a debugging message and EPERM, but in the future
166670f6b2fSRobert Watson 	 * should probably be a panic.
167670f6b2fSRobert Watson 	 */
168670f6b2fSRobert Watson 	if (acl_other == NULL) {
169670f6b2fSRobert Watson 		/*
170670f6b2fSRobert Watson 		 * XXX This should never happen
171670f6b2fSRobert Watson 		 */
172670f6b2fSRobert Watson 		printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
173670f6b2fSRobert Watson 		return (EPERM);
174670f6b2fSRobert Watson 	}
175670f6b2fSRobert Watson 
176670f6b2fSRobert Watson 	/*
1775293465fSRobert Watson 	 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields
1785293465fSRobert Watson 	 * are masked by an ACL_MASK entry, if any.  As such, first identify
1795293465fSRobert Watson 	 * the ACL_MASK field, then iterate through identifying potential
1805293465fSRobert Watson 	 * user matches, then group matches.  If there is no ACL_MASK,
1815293465fSRobert Watson 	 * assume that the mask allows all requests to succeed.
1825293465fSRobert Watson 	 */
1835293465fSRobert Watson 	if (acl_mask != NULL) {
1845293465fSRobert Watson 		acl_mask_granted = 0;
185fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_EXECUTE)
1865293465fSRobert Watson 			acl_mask_granted |= VEXEC;
187fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_READ)
1885293465fSRobert Watson 			acl_mask_granted |= VREAD;
189fb1af1f2SChris D. Faulhaber 		if (acl_mask->ae_perm & ACL_WRITE)
1905293465fSRobert Watson 			acl_mask_granted |= VWRITE;
1915293465fSRobert Watson 	} else
1925293465fSRobert Watson 		acl_mask_granted = VEXEC | VREAD | VWRITE;
1935293465fSRobert Watson 
1945293465fSRobert Watson 	/*
195670f6b2fSRobert Watson 	 * Iterate through user ACL entries.  Do checks twice, first
196670f6b2fSRobert Watson 	 * without privilege, and then if a match is found but failed,
197670f6b2fSRobert Watson 	 * a second time with privilege.
1985293465fSRobert Watson 	 */
1995293465fSRobert Watson 
2005293465fSRobert Watson 	/*
2015293465fSRobert Watson 	 * Check ACL_USER ACL entries.
2025293465fSRobert Watson 	 */
2035293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
2045293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
2055293465fSRobert Watson 		case ACL_USER:
2065293465fSRobert Watson 			if (acl->acl_entry[i].ae_id != cred->cr_uid)
2075293465fSRobert Watson 				break;
2085293465fSRobert Watson 			dac_granted = 0;
209fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2105293465fSRobert Watson 				dac_granted |= VEXEC;
211fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2125293465fSRobert Watson 				dac_granted |= VREAD;
213fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
2145293465fSRobert Watson 				dac_granted |= VWRITE;
2155293465fSRobert Watson 			dac_granted &= acl_mask_granted;
2165293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
2175293465fSRobert Watson 				return (0);
218b114e127SRobert Watson 			if ((acc_mode & (dac_granted | cap_granted)) !=
219b114e127SRobert Watson 			    acc_mode)
220b114e127SRobert Watson 				goto error;
221b114e127SRobert Watson 
2225293465fSRobert Watson 			if (privused != NULL)
2235293465fSRobert Watson 				*privused = 1;
2245293465fSRobert Watson 			return (0);
2255293465fSRobert Watson 		}
2265293465fSRobert Watson 	}
2275293465fSRobert Watson 
2285293465fSRobert Watson 	/*
2295293465fSRobert Watson 	 * Group match is best-match, not first-match, so find a
2305293465fSRobert Watson 	 * "best" match.  Iterate across, testing each potential group
2315293465fSRobert Watson 	 * match.  Make sure we keep track of whether we found a match
232670f6b2fSRobert Watson 	 * or not, so that we know if we should try again with any
233670f6b2fSRobert Watson 	 * available privilege, or if we should move on to ACL_OTHER.
2345293465fSRobert Watson 	 */
2355293465fSRobert Watson 	group_matched = 0;
2365293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
2375293465fSRobert Watson 		switch (acl->acl_entry[i].ae_tag) {
2385293465fSRobert Watson 		case ACL_GROUP_OBJ:
239e15480f8SThomas Moestl 			if (!groupmember(file_gid, cred))
240b114e127SRobert Watson 				break;
2415293465fSRobert Watson 			dac_granted = 0;
242fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2435293465fSRobert Watson 				dac_granted |= VEXEC;
244fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_READ)
2455293465fSRobert Watson 				dac_granted |= VREAD;
246fb1af1f2SChris D. Faulhaber 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
2475293465fSRobert Watson 				dac_granted |= VWRITE;
2485293465fSRobert Watson 			dac_granted  &= acl_mask_granted;
2495293465fSRobert Watson 
2505293465fSRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
2515293465fSRobert Watson 				return (0);
2525293465fSRobert Watson 
2535293465fSRobert Watson 			group_matched = 1;
254b114e127SRobert Watson 			break;
255b114e127SRobert Watson 
256b114e127SRobert Watson 		case ACL_GROUP:
257b114e127SRobert Watson 			if (!groupmember(acl->acl_entry[i].ae_id, cred))
258b114e127SRobert Watson 				break;
259b114e127SRobert Watson 			dac_granted = 0;
260b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
261b114e127SRobert Watson 				dac_granted |= VEXEC;
262b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_READ)
263b114e127SRobert Watson 				dac_granted |= VREAD;
264b114e127SRobert Watson 			if (acl->acl_entry[i].ae_perm & ACL_WRITE)
265b114e127SRobert Watson 				dac_granted |= VWRITE;
266b114e127SRobert Watson 			dac_granted  &= acl_mask_granted;
267b114e127SRobert Watson 
268b114e127SRobert Watson 			if ((acc_mode & dac_granted) == acc_mode)
269b114e127SRobert Watson 				return (0);
270b114e127SRobert Watson 
271b114e127SRobert Watson 			group_matched = 1;
272b114e127SRobert Watson 			break;
273b114e127SRobert Watson 
2745293465fSRobert Watson 		default:
2755293465fSRobert Watson 		}
2765293465fSRobert Watson 	}
2775293465fSRobert Watson 
2785293465fSRobert Watson 	if (group_matched == 1) {
2795293465fSRobert Watson 		/*
2805293465fSRobert Watson 		 * There was a match, but it did not grant rights via
2815293465fSRobert Watson 		 * pure DAC.  Try again, this time with privilege.
2825293465fSRobert Watson 		 */
2835293465fSRobert Watson 		for (i = 0; i < acl->acl_cnt; i++) {
2845293465fSRobert Watson 			switch (acl->acl_entry[i].ae_tag) {
2855293465fSRobert Watson 			case ACL_GROUP_OBJ:
28646157a65SRobert Watson 				if (!groupmember(file_gid, cred))
287b114e127SRobert Watson 					break;
2885293465fSRobert Watson 				dac_granted = 0;
289b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
2905293465fSRobert Watson 					dac_granted |= VEXEC;
291b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
2925293465fSRobert Watson 					dac_granted |= VREAD;
293b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
2945293465fSRobert Watson 					dac_granted |= VWRITE;
2955293465fSRobert Watson 				dac_granted &= acl_mask_granted;
296b114e127SRobert Watson 
297b114e127SRobert Watson 				if ((acc_mode & (dac_granted | cap_granted)) !=
298b114e127SRobert Watson 				    acc_mode)
299b114e127SRobert Watson 					break;
300b114e127SRobert Watson 
3015293465fSRobert Watson 				if (privused != NULL)
3025293465fSRobert Watson 					*privused = 1;
3035293465fSRobert Watson 				return (0);
304b114e127SRobert Watson 
305b114e127SRobert Watson 			case ACL_GROUP:
306b114e127SRobert Watson 				if (!groupmember(acl->acl_entry[i].ae_id,
307b114e127SRobert Watson 				    cred))
308b114e127SRobert Watson 					break;
309b114e127SRobert Watson 				dac_granted = 0;
310b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
311b114e127SRobert Watson 				dac_granted |= VEXEC;
312b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_READ)
313b114e127SRobert Watson 					dac_granted |= VREAD;
314b114e127SRobert Watson 				if (acl->acl_entry[i].ae_perm & ACL_WRITE)
315b114e127SRobert Watson 					dac_granted |= VWRITE;
316b114e127SRobert Watson 				dac_granted &= acl_mask_granted;
317b114e127SRobert Watson 
318b114e127SRobert Watson 				if ((acc_mode & (dac_granted | cap_granted)) !=
319b114e127SRobert Watson 				    acc_mode)
320b114e127SRobert Watson 					break;
321b114e127SRobert Watson 
322b114e127SRobert Watson 				if (privused != NULL)
323b114e127SRobert Watson 					*privused = 1;
324b114e127SRobert Watson 				return (0);
325b114e127SRobert Watson 
3265293465fSRobert Watson 			default:
3275293465fSRobert Watson 			}
3285293465fSRobert Watson 		}
3295293465fSRobert Watson 		/*
3305293465fSRobert Watson 		 * Even with privilege, group membership was not sufficient.
3315293465fSRobert Watson 		 * Return failure.
3325293465fSRobert Watson 		 */
3335293465fSRobert Watson 		goto error;
3345293465fSRobert Watson 	}
3355293465fSRobert Watson 
3365293465fSRobert Watson 	/*
3375293465fSRobert Watson 	 * Fall back on ACL_OTHER.  ACL_MASK is not applied to ACL_OTHER.
3385293465fSRobert Watson 	 */
3395293465fSRobert Watson 	dac_granted = 0;
340fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_EXECUTE)
3415293465fSRobert Watson 		dac_granted |= VEXEC;
342fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_READ)
3435293465fSRobert Watson 		dac_granted |= VREAD;
344fb1af1f2SChris D. Faulhaber 	if (acl_other->ae_perm & ACL_WRITE)
3455293465fSRobert Watson 		dac_granted |= VWRITE;
3465293465fSRobert Watson 
3475293465fSRobert Watson 	if ((acc_mode & dac_granted) == acc_mode)
3485293465fSRobert Watson 		return (0);
3495293465fSRobert Watson 	if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) {
3505293465fSRobert Watson 		if (privused != NULL)
3515293465fSRobert Watson 			*privused = 1;
3525293465fSRobert Watson 		return (0);
3535293465fSRobert Watson 	}
3545293465fSRobert Watson 
3555293465fSRobert Watson error:
3565293465fSRobert Watson 	return ((acc_mode & VADMIN) ? EPERM : EACCES);
3575293465fSRobert Watson }
3585293465fSRobert Watson 
3595293465fSRobert Watson /*
3605293465fSRobert Watson  * For the purposes of file systems maintaining the _OBJ entries in an
3615293465fSRobert Watson  * inode with a mode_t field, this routine converts a mode_t entry
3625293465fSRobert Watson  * to an acl_perm_t.
3635293465fSRobert Watson  */
3645293465fSRobert Watson acl_perm_t
3655293465fSRobert Watson acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
3665293465fSRobert Watson {
3675293465fSRobert Watson 	acl_perm_t	perm = 0;
3685293465fSRobert Watson 
3695293465fSRobert Watson 	switch(tag) {
3705293465fSRobert Watson 	case ACL_USER_OBJ:
3715293465fSRobert Watson 		if (mode & S_IXUSR)
372fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3735293465fSRobert Watson 		if (mode & S_IRUSR)
374fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3755293465fSRobert Watson 		if (mode & S_IWUSR)
376fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3775293465fSRobert Watson 		return (perm);
3785293465fSRobert Watson 
3795293465fSRobert Watson 	case ACL_GROUP_OBJ:
3805293465fSRobert Watson 		if (mode & S_IXGRP)
381fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3825293465fSRobert Watson 		if (mode & S_IRGRP)
383fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3845293465fSRobert Watson 		if (mode & S_IWGRP)
385fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3865293465fSRobert Watson 		return (perm);
3875293465fSRobert Watson 
3885293465fSRobert Watson 	case ACL_OTHER:
3895293465fSRobert Watson 		if (mode & S_IXOTH)
390fb1af1f2SChris D. Faulhaber 			perm |= ACL_EXECUTE;
3915293465fSRobert Watson 		if (mode & S_IROTH)
392fb1af1f2SChris D. Faulhaber 			perm |= ACL_READ;
3935293465fSRobert Watson 		if (mode & S_IWOTH)
394fb1af1f2SChris D. Faulhaber 			perm |= ACL_WRITE;
3955293465fSRobert Watson 		return (perm);
3965293465fSRobert Watson 
3975293465fSRobert Watson 	default:
3985293465fSRobert Watson 		printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
3995293465fSRobert Watson 		return (0);
4005293465fSRobert Watson 	}
4015293465fSRobert Watson }
4025293465fSRobert Watson 
4035293465fSRobert Watson /*
4045293465fSRobert Watson  * Given inode information (uid, gid, mode), return an acl entry of the
4055293465fSRobert Watson  * appropriate type.
4065293465fSRobert Watson  */
4075293465fSRobert Watson struct acl_entry
4085293465fSRobert Watson acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
4095293465fSRobert Watson {
4105293465fSRobert Watson 	struct acl_entry	acl_entry;
4115293465fSRobert Watson 
4125293465fSRobert Watson 	acl_entry.ae_tag = tag;
4135293465fSRobert Watson 	acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
4145293465fSRobert Watson 	switch(tag) {
4155293465fSRobert Watson 	case ACL_USER_OBJ:
4165293465fSRobert Watson 		acl_entry.ae_id = uid;
4175293465fSRobert Watson 		break;
4185293465fSRobert Watson 
4195293465fSRobert Watson 	case ACL_GROUP_OBJ:
4205293465fSRobert Watson 		acl_entry.ae_id = gid;
4215293465fSRobert Watson 		break;
4225293465fSRobert Watson 
4235293465fSRobert Watson 	case ACL_OTHER:
424dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4255293465fSRobert Watson 		break;
4265293465fSRobert Watson 
4275293465fSRobert Watson 	default:
428dbb14f98SChris D. Faulhaber 		acl_entry.ae_id = ACL_UNDEFINED_ID;
4295293465fSRobert Watson 		printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
4305293465fSRobert Watson 	}
4315293465fSRobert Watson 
4325293465fSRobert Watson 	return (acl_entry);
4335293465fSRobert Watson }
4345293465fSRobert Watson 
4355293465fSRobert Watson /*
4365293465fSRobert Watson  * Utility function to generate a file mode given appropriate ACL entries.
4375293465fSRobert Watson  */
4385293465fSRobert Watson mode_t
4395293465fSRobert Watson acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
4405293465fSRobert Watson     struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
4415293465fSRobert Watson {
4425293465fSRobert Watson 	mode_t	mode;
4435293465fSRobert Watson 
4445293465fSRobert Watson 	mode = 0;
445fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
4465293465fSRobert Watson 		mode |= S_IXUSR;
447fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_READ)
4485293465fSRobert Watson 		mode |= S_IRUSR;
449fb1af1f2SChris D. Faulhaber 	if (acl_user_obj_entry->ae_perm & ACL_WRITE)
4505293465fSRobert Watson 		mode |= S_IWUSR;
451fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
4525293465fSRobert Watson 		mode |= S_IXGRP;
453fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_READ)
4545293465fSRobert Watson 		mode |= S_IRGRP;
455fb1af1f2SChris D. Faulhaber 	if (acl_group_obj_entry->ae_perm & ACL_WRITE)
4565293465fSRobert Watson 		mode |= S_IWGRP;
457fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_EXECUTE)
4585293465fSRobert Watson 		mode |= S_IXOTH;
459fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_READ)
4605293465fSRobert Watson 		mode |= S_IROTH;
461fb1af1f2SChris D. Faulhaber 	if (acl_other_entry->ae_perm & ACL_WRITE)
4625293465fSRobert Watson 		mode |= S_IWOTH;
4635293465fSRobert Watson 
4645293465fSRobert Watson 	return (mode);
4655293465fSRobert Watson }
4665293465fSRobert Watson 
4675293465fSRobert Watson /*
4685293465fSRobert Watson  * Perform a syntactic check of the ACL, sufficient to allow an
4695293465fSRobert Watson  * implementing file system to determine if it should accept this and
4705293465fSRobert Watson  * rely on the POSIX.1e ACL properties.
4715293465fSRobert Watson  */
4725293465fSRobert Watson int
4735293465fSRobert Watson acl_posix1e_check(struct acl *acl)
4745293465fSRobert Watson {
4755293465fSRobert Watson 	int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
4765293465fSRobert Watson 	int num_acl_mask, num_acl_other, i;
4775293465fSRobert Watson 
4785293465fSRobert Watson 	/*
4795293465fSRobert Watson 	 * Verify that the number of entries does not exceed the maximum
4805293465fSRobert Watson 	 * defined for acl_t.
4815293465fSRobert Watson 	 * Verify that the correct number of various sorts of ae_tags are
4825293465fSRobert Watson 	 * present:
4835293465fSRobert Watson 	 *   Exactly one ACL_USER_OBJ
4845293465fSRobert Watson 	 *   Exactly one ACL_GROUP_OBJ
4855293465fSRobert Watson 	 *   Exactly one ACL_OTHER
4865293465fSRobert Watson 	 *   If any ACL_USER or ACL_GROUP entries appear, then exactly one
4875293465fSRobert Watson 	 *   ACL_MASK entry must also appear.
4885293465fSRobert Watson 	 * Verify that all ae_perm entries are in ACL_PERM_BITS.
4895293465fSRobert Watson 	 * Verify all ae_tag entries are understood by this implementation.
4905293465fSRobert Watson 	 * Note: Does not check for uniqueness of qualifier (ae_id) field.
4915293465fSRobert Watson 	 */
4925293465fSRobert Watson 	num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
4935293465fSRobert Watson 	    num_acl_mask = num_acl_other = 0;
4945293465fSRobert Watson 	if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0)
4955293465fSRobert Watson 		return (EINVAL);
4965293465fSRobert Watson 	for (i = 0; i < acl->acl_cnt; i++) {
4975293465fSRobert Watson 		/*
4985293465fSRobert Watson 		 * Check for a valid tag.
4995293465fSRobert Watson 		 */
5005293465fSRobert Watson 		switch(acl->acl_entry[i].ae_tag) {
5015293465fSRobert Watson 		case ACL_USER_OBJ:
502b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
503b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
504b114e127SRobert Watson 				return (EINVAL);
5055293465fSRobert Watson 			num_acl_user_obj++;
5065293465fSRobert Watson 			break;
5075293465fSRobert Watson 		case ACL_GROUP_OBJ:
508b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
509b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
510b114e127SRobert Watson 				return (EINVAL);
5115293465fSRobert Watson 			num_acl_group_obj++;
5125293465fSRobert Watson 			break;
5135293465fSRobert Watson 		case ACL_USER:
514b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
515b114e127SRobert Watson 				return (EINVAL);
5165293465fSRobert Watson 			num_acl_user++;
5175293465fSRobert Watson 			break;
5185293465fSRobert Watson 		case ACL_GROUP:
519b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
520b114e127SRobert Watson 				return (EINVAL);
5215293465fSRobert Watson 			num_acl_group++;
5225293465fSRobert Watson 			break;
5235293465fSRobert Watson 		case ACL_OTHER:
524b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
525b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
526b114e127SRobert Watson 				return (EINVAL);
5275293465fSRobert Watson 			num_acl_other++;
5285293465fSRobert Watson 			break;
5295293465fSRobert Watson 		case ACL_MASK:
530b114e127SRobert Watson 			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */
531b114e127SRobert Watson 			if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
532b114e127SRobert Watson 				return (EINVAL);
5335293465fSRobert Watson 			num_acl_mask++;
5345293465fSRobert Watson 			break;
5355293465fSRobert Watson 		default:
5365293465fSRobert Watson 			return (EINVAL);
5375293465fSRobert Watson 		}
5385293465fSRobert Watson 		/*
5395293465fSRobert Watson 		 * Check for valid perm entries.
5405293465fSRobert Watson 		 */
5415293465fSRobert Watson 		if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
5425293465fSRobert Watson 		    ACL_PERM_BITS)
5435293465fSRobert Watson 			return (EINVAL);
5445293465fSRobert Watson 	}
5455293465fSRobert Watson 	if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
5465293465fSRobert Watson 	    (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
5475293465fSRobert Watson 		return (EINVAL);
5485293465fSRobert Watson 	if (((num_acl_group != 0) || (num_acl_user != 0)) &&
5495293465fSRobert Watson 	    (num_acl_mask != 1))
5505293465fSRobert Watson 		return (EINVAL);
5515293465fSRobert Watson 	return (0);
5525293465fSRobert Watson }
5535293465fSRobert Watson 
5545293465fSRobert Watson /*
55591f37dcbSRobert Watson  * These calls wrap the real vnode operations, and are called by the
55691f37dcbSRobert Watson  * syscall code once the syscall has converted the path or file
55791f37dcbSRobert Watson  * descriptor to a vnode (unlocked).  The aclp pointer is assumed
55891f37dcbSRobert Watson  * still to point to userland, so this should not be consumed within
55991f37dcbSRobert Watson  * the kernel except by syscall code.  Other code should directly
56091f37dcbSRobert Watson  * invoke VOP_{SET,GET}ACL.
56191f37dcbSRobert Watson  */
56291f37dcbSRobert Watson 
56391f37dcbSRobert Watson /*
56491f37dcbSRobert Watson  * Given a vnode, set its ACL.
56591f37dcbSRobert Watson  */
56691f37dcbSRobert Watson static int
567b40ce416SJulian Elischer vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
56891f37dcbSRobert Watson     struct acl *aclp)
56991f37dcbSRobert Watson {
57091f37dcbSRobert Watson 	struct acl inkernacl;
57191f37dcbSRobert Watson 	int error;
57291f37dcbSRobert Watson 
57391f37dcbSRobert Watson 	error = copyin(aclp, &inkernacl, sizeof(struct acl));
57491f37dcbSRobert Watson 	if (error)
57591f37dcbSRobert Watson 		return(error);
576b40ce416SJulian Elischer 	VOP_LEASE(vp, td, td->td_proc->p_ucred, LEASE_WRITE);
577b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
578b40ce416SJulian Elischer 	error = VOP_SETACL(vp, type, &inkernacl, td->td_proc->p_ucred, td);
579b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
58091f37dcbSRobert Watson 	return(error);
58191f37dcbSRobert Watson }
58291f37dcbSRobert Watson 
58391f37dcbSRobert Watson /*
58491f37dcbSRobert Watson  * Given a vnode, get its ACL.
58591f37dcbSRobert Watson  */
58691f37dcbSRobert Watson static int
587b40ce416SJulian Elischer vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
58891f37dcbSRobert Watson     struct acl *aclp)
58991f37dcbSRobert Watson {
59091f37dcbSRobert Watson 	struct acl inkernelacl;
59191f37dcbSRobert Watson 	int error;
59291f37dcbSRobert Watson 
593b40ce416SJulian Elischer 	VOP_LEASE(vp, td, td->td_proc->p_ucred, LEASE_WRITE);
594b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
595b40ce416SJulian Elischer 	error = VOP_GETACL(vp, type, &inkernelacl, td->td_proc->p_ucred, td);
596b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
59791f37dcbSRobert Watson 	if (error == 0)
59891f37dcbSRobert Watson 		error = copyout(&inkernelacl, aclp, sizeof(struct acl));
59991f37dcbSRobert Watson 	return (error);
60091f37dcbSRobert Watson }
60191f37dcbSRobert Watson 
60291f37dcbSRobert Watson /*
60391f37dcbSRobert Watson  * Given a vnode, delete its ACL.
60491f37dcbSRobert Watson  */
60591f37dcbSRobert Watson static int
606b40ce416SJulian Elischer vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
60791f37dcbSRobert Watson {
60891f37dcbSRobert Watson 	int error;
60991f37dcbSRobert Watson 
610b40ce416SJulian Elischer 	VOP_LEASE(vp, td, td->td_proc->p_ucred, LEASE_WRITE);
611b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
612fdba6d3aSRobert Watson 	error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, td->td_proc->p_ucred,
613fdba6d3aSRobert Watson 	    td);
614b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
61591f37dcbSRobert Watson 	return (error);
61691f37dcbSRobert Watson }
61791f37dcbSRobert Watson 
61891f37dcbSRobert Watson /*
61991f37dcbSRobert Watson  * Given a vnode, check whether an ACL is appropriate for it
62091f37dcbSRobert Watson  */
62191f37dcbSRobert Watson static int
622b40ce416SJulian Elischer vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
62391f37dcbSRobert Watson     struct acl *aclp)
62491f37dcbSRobert Watson {
62591f37dcbSRobert Watson 	struct acl inkernelacl;
62691f37dcbSRobert Watson 	int error;
62791f37dcbSRobert Watson 
62891f37dcbSRobert Watson 	error = copyin(aclp, &inkernelacl, sizeof(struct acl));
62991f37dcbSRobert Watson 	if (error)
63091f37dcbSRobert Watson 		return(error);
631fdba6d3aSRobert Watson 	error = VOP_ACLCHECK(vp, type, &inkernelacl, td->td_proc->p_ucred,
632fdba6d3aSRobert Watson 	    td);
63391f37dcbSRobert Watson 	return (error);
63491f37dcbSRobert Watson }
63591f37dcbSRobert Watson 
63691f37dcbSRobert Watson /*
63791f37dcbSRobert Watson  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
63891f37dcbSRobert Watson  * Don't need to lock, as the vacl_ code will get/release any locks
63991f37dcbSRobert Watson  * required.
64091f37dcbSRobert Watson  */
64191f37dcbSRobert Watson 
64291f37dcbSRobert Watson /*
64391f37dcbSRobert Watson  * Given a file path, get an ACL for it
644f708f4d1SMatthew Dillon  *
645f708f4d1SMatthew Dillon  * MPSAFE
64691f37dcbSRobert Watson  */
64791f37dcbSRobert Watson int
648b40ce416SJulian Elischer __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
64991f37dcbSRobert Watson {
65091f37dcbSRobert Watson 	struct nameidata nd;
65191f37dcbSRobert Watson 	int error;
65291f37dcbSRobert Watson 
653f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
654b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
65591f37dcbSRobert Watson 	error = namei(&nd);
656f708f4d1SMatthew Dillon 	if (error == 0) {
657b40ce416SJulian Elischer 		error = vacl_get_acl(td, nd.ni_vp, SCARG(uap, type),
658f708f4d1SMatthew Dillon 			    SCARG(uap, aclp));
65991f37dcbSRobert Watson 		NDFREE(&nd, 0);
660f708f4d1SMatthew Dillon 	}
661f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
66291f37dcbSRobert Watson 	return (error);
66391f37dcbSRobert Watson }
66491f37dcbSRobert Watson 
66591f37dcbSRobert Watson /*
66691f37dcbSRobert Watson  * Given a file path, set an ACL for it
667f708f4d1SMatthew Dillon  *
668f708f4d1SMatthew Dillon  * MPSAFE
66991f37dcbSRobert Watson  */
67091f37dcbSRobert Watson int
671b40ce416SJulian Elischer __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
67291f37dcbSRobert Watson {
67391f37dcbSRobert Watson 	struct nameidata nd;
67491f37dcbSRobert Watson 	int error;
67591f37dcbSRobert Watson 
676f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
677b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
67891f37dcbSRobert Watson 	error = namei(&nd);
679f708f4d1SMatthew Dillon 	if (error == 0) {
680b40ce416SJulian Elischer 		error = vacl_set_acl(td, nd.ni_vp, SCARG(uap, type),
681f708f4d1SMatthew Dillon 			    SCARG(uap, aclp));
68291f37dcbSRobert Watson 		NDFREE(&nd, 0);
683f708f4d1SMatthew Dillon 	}
684f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
68591f37dcbSRobert Watson 	return (error);
68691f37dcbSRobert Watson }
68791f37dcbSRobert Watson 
68891f37dcbSRobert Watson /*
68991f37dcbSRobert Watson  * Given a file descriptor, get an ACL for it
690f708f4d1SMatthew Dillon  *
691f708f4d1SMatthew Dillon  * MPSAFE
69291f37dcbSRobert Watson  */
69391f37dcbSRobert Watson int
694b40ce416SJulian Elischer __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
69591f37dcbSRobert Watson {
69691f37dcbSRobert Watson 	struct file *fp;
69791f37dcbSRobert Watson 	int error;
69891f37dcbSRobert Watson 
699f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
700b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
701f708f4d1SMatthew Dillon 	if (error == 0) {
702b40ce416SJulian Elischer 		error = vacl_get_acl(td, (struct vnode *)fp->f_data,
703f708f4d1SMatthew Dillon 			    SCARG(uap, type), SCARG(uap, aclp));
704f708f4d1SMatthew Dillon 	}
705f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
70691f37dcbSRobert Watson 	return (error);
70791f37dcbSRobert Watson }
70891f37dcbSRobert Watson 
70991f37dcbSRobert Watson /*
71091f37dcbSRobert Watson  * Given a file descriptor, set an ACL for it
711f708f4d1SMatthew Dillon  *
712f708f4d1SMatthew Dillon  * MPSAFE
71391f37dcbSRobert Watson  */
71491f37dcbSRobert Watson int
715b40ce416SJulian Elischer __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
71691f37dcbSRobert Watson {
71791f37dcbSRobert Watson 	struct file *fp;
71891f37dcbSRobert Watson 	int error;
71991f37dcbSRobert Watson 
720f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
721b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
722f708f4d1SMatthew Dillon 	if (error == 0) {
723b40ce416SJulian Elischer 		error = vacl_set_acl(td, (struct vnode *)fp->f_data,
724f708f4d1SMatthew Dillon 			    SCARG(uap, type), SCARG(uap, aclp));
725f708f4d1SMatthew Dillon 	}
726f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
72791f37dcbSRobert Watson 	return (error);
72891f37dcbSRobert Watson }
72991f37dcbSRobert Watson 
73091f37dcbSRobert Watson /*
73191f37dcbSRobert Watson  * Given a file path, delete an ACL from it.
732f708f4d1SMatthew Dillon  *
733f708f4d1SMatthew Dillon  * MPSAFE
73491f37dcbSRobert Watson  */
73591f37dcbSRobert Watson int
736b40ce416SJulian Elischer __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
73791f37dcbSRobert Watson {
73891f37dcbSRobert Watson 	struct nameidata nd;
73991f37dcbSRobert Watson 	int error;
74091f37dcbSRobert Watson 
741f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
742b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
74391f37dcbSRobert Watson 	error = namei(&nd);
744f708f4d1SMatthew Dillon 	if (error == 0) {
745b40ce416SJulian Elischer 		error = vacl_delete(td, nd.ni_vp, SCARG(uap, type));
74691f37dcbSRobert Watson 		NDFREE(&nd, 0);
747f708f4d1SMatthew Dillon 	}
748f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
74991f37dcbSRobert Watson 	return (error);
75091f37dcbSRobert Watson }
75191f37dcbSRobert Watson 
75291f37dcbSRobert Watson /*
75391f37dcbSRobert Watson  * Given a file path, delete an ACL from it.
754f708f4d1SMatthew Dillon  *
755f708f4d1SMatthew Dillon  * MPSAFE
75691f37dcbSRobert Watson  */
75791f37dcbSRobert Watson int
758b40ce416SJulian Elischer __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
75991f37dcbSRobert Watson {
76091f37dcbSRobert Watson 	struct file *fp;
76191f37dcbSRobert Watson 	int error;
76291f37dcbSRobert Watson 
763f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
764b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
765f708f4d1SMatthew Dillon 	if (error == 0) {
766b40ce416SJulian Elischer 		error = vacl_delete(td, (struct vnode *)fp->f_data,
767f708f4d1SMatthew Dillon 			    SCARG(uap, type));
768f708f4d1SMatthew Dillon 	}
769f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
77091f37dcbSRobert Watson 	return (error);
77191f37dcbSRobert Watson }
77291f37dcbSRobert Watson 
77391f37dcbSRobert Watson /*
77491f37dcbSRobert Watson  * Given a file path, check an ACL for it
775f708f4d1SMatthew Dillon  *
776f708f4d1SMatthew Dillon  * MPSAFE
77791f37dcbSRobert Watson  */
77891f37dcbSRobert Watson int
779b40ce416SJulian Elischer __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
78091f37dcbSRobert Watson {
78191f37dcbSRobert Watson 	struct nameidata	nd;
78291f37dcbSRobert Watson 	int	error;
78391f37dcbSRobert Watson 
784f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
785b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
78691f37dcbSRobert Watson 	error = namei(&nd);
787f708f4d1SMatthew Dillon 	if (error == 0) {
788b40ce416SJulian Elischer 		error = vacl_aclcheck(td, nd.ni_vp, SCARG(uap, type),
789f708f4d1SMatthew Dillon 			    SCARG(uap, aclp));
79091f37dcbSRobert Watson 		NDFREE(&nd, 0);
791f708f4d1SMatthew Dillon 	}
792f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
79391f37dcbSRobert Watson 	return (error);
79491f37dcbSRobert Watson }
79591f37dcbSRobert Watson 
79691f37dcbSRobert Watson /*
79791f37dcbSRobert Watson  * Given a file descriptor, check an ACL for it
798f708f4d1SMatthew Dillon  *
799f708f4d1SMatthew Dillon  * MPSAFE
80091f37dcbSRobert Watson  */
80191f37dcbSRobert Watson int
802b40ce416SJulian Elischer __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
80391f37dcbSRobert Watson {
80491f37dcbSRobert Watson 	struct file *fp;
80591f37dcbSRobert Watson 	int error;
80691f37dcbSRobert Watson 
807f708f4d1SMatthew Dillon 	mtx_lock(&Giant);
808b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, SCARG(uap, filedes), &fp);
809f708f4d1SMatthew Dillon 	if (error == 0) {
810b40ce416SJulian Elischer 		error = vacl_aclcheck(td, (struct vnode *)fp->f_data,
811f708f4d1SMatthew Dillon 			    SCARG(uap, type), SCARG(uap, aclp));
812f708f4d1SMatthew Dillon 	}
813f708f4d1SMatthew Dillon 	mtx_unlock(&Giant);
81491f37dcbSRobert Watson 	return (error);
81591f37dcbSRobert Watson }
816