174822Srwatson/*-
2165890Srwatson * Copyright (c) 1999-2003 Robert N. M. Watson
374822Srwatson * All rights reserved.
474822Srwatson *
585845Srwatson * This software was developed by Robert Watson for the TrustedBSD Project.
685845Srwatson *
774822Srwatson * Redistribution and use in source and binary forms, with or without
874822Srwatson * modification, are permitted provided that the following conditions
974822Srwatson * are met:
1074822Srwatson * 1. Redistributions of source code must retain the above copyright
1174822Srwatson *    notice, this list of conditions and the following disclaimer.
1274822Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1374822Srwatson *    notice, this list of conditions and the following disclaimer in the
1474822Srwatson *    documentation and/or other materials provided with the distribution.
1574822Srwatson *
1674822Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1774822Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874822Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974822Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2074822Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2174822Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2274822Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2374822Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2474822Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2574822Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2674822Srwatson * SUCH DAMAGE.
2774822Srwatson */
28116192Sobrien
2974822Srwatson/*
3074822Srwatson * Support for POSIX.1e access control lists: UFS-specific support functions.
3174822Srwatson */
3274822Srwatson
33116192Sobrien#include <sys/cdefs.h>
34116192Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/ufs/ufs/ufs_acl.c 241011 2012-09-27 23:30:49Z mdf $");
35116192Sobrien
3674822Srwatson#include "opt_ufs.h"
3774822Srwatson#include "opt_quota.h"
3874822Srwatson
3974822Srwatson#include <sys/param.h>
4074822Srwatson#include <sys/systm.h>
4174822Srwatson#include <sys/stat.h>
4274822Srwatson#include <sys/mount.h>
4374822Srwatson#include <sys/vnode.h>
4474822Srwatson#include <sys/types.h>
4574822Srwatson#include <sys/acl.h>
4674822Srwatson#include <sys/event.h>
4774822Srwatson#include <sys/extattr.h>
4874822Srwatson
4974822Srwatson#include <ufs/ufs/quota.h>
5074822Srwatson#include <ufs/ufs/inode.h>
5174822Srwatson#include <ufs/ufs/acl.h>
5274822Srwatson#include <ufs/ufs/extattr.h>
5374822Srwatson#include <ufs/ufs/dir.h>
5474822Srwatson#include <ufs/ufs/ufsmount.h>
5574822Srwatson#include <ufs/ufs/ufs_extern.h>
56184629Strasz#include <ufs/ffs/fs.h>
5774822Srwatson
5874822Srwatson#ifdef UFS_ACL
5974822Srwatson
60218485SnetchildFEATURE(ufs_acl, "ACL support for UFS");
61218485Snetchild
6274822Srwatson/*
6374822Srwatson * Synchronize an ACL and an inode by copying over appropriate inode fields
6474822Srwatson * to the passed ACL.  Assumes an ACL that would satisfy acl_posix1e_check(),
6574822Srwatson * and may panic if not.
6674822Srwatson */
6774822Srwatsonvoid
6874822Srwatsonufs_sync_acl_from_inode(struct inode *ip, struct acl *acl)
6974822Srwatson{
7074822Srwatson	struct acl_entry	*acl_mask, *acl_group_obj;
7174822Srwatson	int	i;
7274822Srwatson
7374822Srwatson	/*
7474822Srwatson	 * Update ACL_USER_OBJ, ACL_OTHER, but simply identify ACL_MASK
7574822Srwatson	 * and ACL_GROUP_OBJ for use after we know whether ACL_MASK is
7674822Srwatson	 * present.
7774822Srwatson	 */
7874822Srwatson	acl_mask = NULL;
7974822Srwatson	acl_group_obj = NULL;
8074822Srwatson	for (i = 0; i < acl->acl_cnt; i++) {
8174822Srwatson		switch (acl->acl_entry[i].ae_tag) {
8274822Srwatson		case ACL_USER_OBJ:
8374822Srwatson			acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
8474822Srwatson			    ACL_USER_OBJ, ip->i_mode);
8575571Srwatson			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
8674822Srwatson			break;
8774822Srwatson
8874822Srwatson		case ACL_GROUP_OBJ:
8974822Srwatson			acl_group_obj = &acl->acl_entry[i];
9075571Srwatson			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
9174822Srwatson			break;
9274822Srwatson
9374822Srwatson		case ACL_OTHER:
9474822Srwatson			acl->acl_entry[i].ae_perm = acl_posix1e_mode_to_perm(
9574822Srwatson			    ACL_OTHER, ip->i_mode);
9675571Srwatson			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
9774822Srwatson			break;
9874822Srwatson
9974822Srwatson		case ACL_MASK:
10074822Srwatson			acl_mask = &acl->acl_entry[i];
10175571Srwatson			acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
10274822Srwatson			break;
10374822Srwatson
10474822Srwatson		case ACL_USER:
10574822Srwatson		case ACL_GROUP:
10674822Srwatson			break;
10774822Srwatson
10874822Srwatson		default:
10974822Srwatson			panic("ufs_sync_acl_from_inode(): bad ae_tag");
11074822Srwatson		}
11174822Srwatson	}
11274822Srwatson
11374822Srwatson	if (acl_group_obj == NULL)
11474822Srwatson		panic("ufs_sync_acl_from_inode(): no ACL_GROUP_OBJ");
11574822Srwatson
11674822Srwatson	if (acl_mask == NULL) {
11774822Srwatson		/*
11874822Srwatson		 * There is no ACL_MASK, so update ACL_GROUP_OBJ.
11974822Srwatson		 */
12074822Srwatson		acl_group_obj->ae_perm = acl_posix1e_mode_to_perm(
12174822Srwatson		    ACL_GROUP_OBJ, ip->i_mode);
12274822Srwatson	} else {
12374822Srwatson		/*
12474822Srwatson		 * Update the ACL_MASK entry instead of ACL_GROUP_OBJ.
12574822Srwatson		 */
12674822Srwatson		acl_mask->ae_perm = acl_posix1e_mode_to_perm(ACL_GROUP_OBJ,
12774822Srwatson		    ip->i_mode);
12874822Srwatson	}
12974822Srwatson}
13074822Srwatson
13174822Srwatson/*
132118411Srwatson * Calculate what the inode mode should look like based on an authoritative
133118411Srwatson * ACL for the inode.  Replace only the fields in the inode that the ACL
134118411Srwatson * can represent.
13574822Srwatson */
13674822Srwatsonvoid
137118411Srwatsonufs_sync_inode_from_acl(struct acl *acl, struct inode *ip)
13874822Srwatson{
13974822Srwatson
140118411Srwatson	ip->i_mode &= ACL_PRESERVE_MASK;
141118411Srwatson	ip->i_mode |= acl_posix1e_acl_to_mode(acl);
142132775Skan	DIP_SET(ip, i_mode, ip->i_mode);
14374822Srwatson}
14474822Srwatson
14574822Srwatson/*
146200796Strasz * Retrieve NFSv4 ACL, skipping access checks.  Must be used in UFS code
147200796Strasz * instead of VOP_GETACL() when we don't want to be restricted by the user
148200796Strasz * not having ACL_READ_ACL permission, e.g. when calculating inherited ACL
149200796Strasz * or in ufs_vnops.c:ufs_accessx().
150200796Strasz */
151200796Straszint
152200796Straszufs_getacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
153200796Strasz{
154200796Strasz	int error, len;
155200796Strasz	struct inode *ip = VTOI(vp);
156200796Strasz
157200796Strasz	len = sizeof(*aclp);
158200796Strasz	bzero(aclp, len);
159200796Strasz
160200796Strasz	error = vn_extattr_get(vp, IO_NODELOCKED,
161200796Strasz	    NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
162200796Strasz	    &len, (char *) aclp, td);
163200796Strasz	aclp->acl_maxcnt = ACL_MAX_ENTRIES;
164200796Strasz	if (error == ENOATTR) {
165200796Strasz		/*
166200796Strasz		 * Legitimately no ACL set on object, purely
167200796Strasz		 * emulate it through the inode.
168200796Strasz		 */
169200796Strasz		acl_nfs4_sync_acl_from_mode(aclp, ip->i_mode, ip->i_uid);
170200796Strasz
171200796Strasz		return (0);
172200796Strasz	}
173200796Strasz
174200796Strasz	if (error)
175200796Strasz		return (error);
176200796Strasz
177200796Strasz	if (len != sizeof(*aclp)) {
178200796Strasz		/*
179200796Strasz		 * A short (or long) read, meaning that for
180200796Strasz		 * some reason the ACL is corrupted.  Return
181200796Strasz		 * EPERM since the object DAC protections
182200796Strasz		 * are unsafe.
183200796Strasz		 */
184200796Strasz		printf("ufs_getacl_nfs4(): Loaded invalid ACL ("
185241011Smdf		    "%d bytes), inumber %ju on %s\n", len,
186241011Smdf		    (uintmax_t)ip->i_number, ip->i_fs->fs_fsmnt);
187200796Strasz
188200796Strasz		return (EPERM);
189200796Strasz	}
190200796Strasz
191200796Strasz	error = acl_nfs4_check(aclp, vp->v_type == VDIR);
192200796Strasz	if (error) {
193200796Strasz		printf("ufs_getacl_nfs4(): Loaded invalid ACL "
194241011Smdf		    "(failed acl_nfs4_check), inumber %ju on %s\n",
195241011Smdf		    (uintmax_t)ip->i_number, ip->i_fs->fs_fsmnt);
196200796Strasz
197200796Strasz		return (EPERM);
198200796Strasz	}
199200796Strasz
200200796Strasz	return (0);
201200796Strasz}
202200796Strasz
203200796Straszstatic int
204200796Straszufs_getacl_nfs4(struct vop_getacl_args *ap)
205200796Strasz{
206200796Strasz	int error;
207200796Strasz
208200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
209200796Strasz		return (EINVAL);
210200796Strasz
211200796Strasz	error = VOP_ACCESSX(ap->a_vp, VREAD_ACL, ap->a_td->td_ucred, ap->a_td);
212200796Strasz	if (error)
213200796Strasz		return (error);
214200796Strasz
215200796Strasz	error = ufs_getacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
216200796Strasz
217200796Strasz	return (error);
218200796Strasz}
219200796Strasz
220200796Strasz/*
221192586Strasz * Read POSIX.1e ACL from an EA.  Return error if its not found
222192586Strasz * or if any other error has occured.
223192586Strasz */
224192586Straszstatic int
225192586Straszufs_get_oldacl(acl_type_t type, struct oldacl *old, struct vnode *vp,
226192586Strasz    struct thread *td)
227192586Strasz{
228192586Strasz	int error, len;
229192586Strasz	struct inode *ip = VTOI(vp);
230192586Strasz
231192586Strasz	len = sizeof(*old);
232192586Strasz
233192586Strasz	switch (type) {
234192586Strasz	case ACL_TYPE_ACCESS:
235192586Strasz		error = vn_extattr_get(vp, IO_NODELOCKED,
236192586Strasz		    POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
237192586Strasz		    POSIX1E_ACL_ACCESS_EXTATTR_NAME, &len, (char *) old,
238192586Strasz		    td);
239192586Strasz		break;
240192586Strasz	case ACL_TYPE_DEFAULT:
241192586Strasz		if (vp->v_type != VDIR)
242192586Strasz			return (EINVAL);
243192586Strasz		error = vn_extattr_get(vp, IO_NODELOCKED,
244192586Strasz		    POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
245192586Strasz		    POSIX1E_ACL_DEFAULT_EXTATTR_NAME, &len, (char *) old,
246192586Strasz		    td);
247192586Strasz		break;
248192586Strasz	default:
249192586Strasz		return (EINVAL);
250192586Strasz	}
251192586Strasz
252192586Strasz	if (error != 0)
253192586Strasz		return (error);
254192586Strasz
255192586Strasz	if (len != sizeof(*old)) {
256192586Strasz		/*
257192586Strasz		 * A short (or long) read, meaning that for some reason
258192586Strasz		 * the ACL is corrupted.  Return EPERM since the object
259192586Strasz		 * DAC protections are unsafe.
260192586Strasz		 */
261192586Strasz		printf("ufs_get_oldacl(): Loaded invalid ACL "
262241011Smdf		    "(len = %d), inumber %ju on %s\n", len,
263241011Smdf		    (uintmax_t)ip->i_number, ip->i_fs->fs_fsmnt);
264192586Strasz		return (EPERM);
265192586Strasz	}
266192586Strasz
267192586Strasz	return (0);
268192586Strasz}
269192586Strasz
270192586Strasz/*
27174822Srwatson * Retrieve the ACL on a file.
27274822Srwatson *
27374822Srwatson * As part of the ACL is stored in the inode, and the rest in an EA,
27474822Srwatson * assemble both into a final ACL product.  Right now this is not done
27574822Srwatson * very efficiently.
27674822Srwatson */
277192586Straszstatic int
278192586Straszufs_getacl_posix1e(struct vop_getacl_args *ap)
27974822Srwatson{
28074822Srwatson	struct inode *ip = VTOI(ap->a_vp);
281192586Strasz	int error;
282192586Strasz	struct oldacl *old;
28374822Srwatson
284105179Srwatson	/*
285105179Srwatson	 * XXX: If ufs_getacl() should work on file systems not supporting
286105179Srwatson	 * ACLs, remove this check.
287105179Srwatson	 */
288105179Srwatson	if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
289200796Strasz		return (EINVAL);
29074822Srwatson
291192586Strasz	old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
292192586Strasz
29374822Srwatson	/*
294192586Strasz	 * Attempt to retrieve the ACL from the extended attributes.
29574822Srwatson	 */
296192586Strasz	error = ufs_get_oldacl(ap->a_type, old, ap->a_vp, ap->a_td);
297192586Strasz	switch (error) {
298192586Strasz	/*
299192586Strasz	 * XXX: If ufs_getacl() should work on filesystems
300192586Strasz	 * without the EA configured, add case EOPNOTSUPP here.
301192586Strasz	 */
302192586Strasz	case ENOATTR:
303192586Strasz		switch (ap->a_type) {
304192586Strasz		case ACL_TYPE_ACCESS:
30574822Srwatson			/*
30674822Srwatson			 * Legitimately no ACL set on object, purely
30774822Srwatson			 * emulate it through the inode.  These fields will
30874822Srwatson			 * be updated when the ACL is synchronized with
30974822Srwatson			 * the inode later.
31074822Srwatson			 */
311192586Strasz			old->acl_cnt = 3;
312192586Strasz			old->acl_entry[0].ae_tag = ACL_USER_OBJ;
313192586Strasz			old->acl_entry[0].ae_id = ACL_UNDEFINED_ID;
314192586Strasz			old->acl_entry[0].ae_perm = ACL_PERM_NONE;
315192586Strasz			old->acl_entry[1].ae_tag = ACL_GROUP_OBJ;
316192586Strasz			old->acl_entry[1].ae_id = ACL_UNDEFINED_ID;
317192586Strasz			old->acl_entry[1].ae_perm = ACL_PERM_NONE;
318192586Strasz			old->acl_entry[2].ae_tag = ACL_OTHER;
319192586Strasz			old->acl_entry[2].ae_id = ACL_UNDEFINED_ID;
320192586Strasz			old->acl_entry[2].ae_perm = ACL_PERM_NONE;
32174822Srwatson			break;
32274822Srwatson
323192586Strasz		case ACL_TYPE_DEFAULT:
324192586Strasz			/*
325192586Strasz			 * Unlike ACL_TYPE_ACCESS, there is no relationship
326192586Strasz			 * between the inode contents and the ACL, and it is
327192586Strasz			 * therefore possible for the request for the ACL
328192586Strasz			 * to fail since the ACL is undefined.  In this
329192586Strasz			 * situation, return success and an empty ACL,
330192586Strasz			 * as required by POSIX.1e.
331192586Strasz			 */
332192586Strasz			old->acl_cnt = 0;
33374822Srwatson			break;
33474822Srwatson		}
335192586Strasz		/* FALLTHROUGH */
336192586Strasz	case 0:
337192586Strasz		error = acl_copy_oldacl_into_acl(old, ap->a_aclp);
338192586Strasz		if (error != 0)
33974822Srwatson			break;
34074822Srwatson
341192586Strasz		if (ap->a_type == ACL_TYPE_ACCESS)
342192586Strasz			ufs_sync_acl_from_inode(ip, ap->a_aclp);
343192586Strasz	default:
34474822Srwatson		break;
34574822Srwatson	}
34674822Srwatson
347192586Strasz	free(old, M_ACL);
34874822Srwatson	return (error);
34974822Srwatson}
35074822Srwatson
351192586Straszint
352192586Straszufs_getacl(ap)
353192586Strasz	struct vop_getacl_args /* {
354192586Strasz		struct vnode *vp;
355192586Strasz		acl_type_t type;
356192586Strasz		struct acl *aclp;
357192586Strasz		struct ucred *cred;
358192586Strasz		struct thread *td;
359192586Strasz	} */ *ap;
360192586Strasz{
361192586Strasz
362200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
363200796Strasz		return (EOPNOTSUPP);
364200796Strasz
365200796Strasz	if (ap->a_type == ACL_TYPE_NFS4)
366200796Strasz		return (ufs_getacl_nfs4(ap));
367200796Strasz
368192586Strasz	return (ufs_getacl_posix1e(ap));
369192586Strasz}
370192586Strasz
37174822Srwatson/*
372200796Strasz * Set NFSv4 ACL without doing any access checking.  This is required
373200796Strasz * e.g. by the UFS code that implements ACL inheritance, or from
374200796Strasz * ufs_vnops.c:ufs_chmod(), as some of the checks have to be skipped
375200796Strasz * in that case, and others are redundant.
376200796Strasz */
377200796Straszint
378200796Straszufs_setacl_nfs4_internal(struct vnode *vp, struct acl *aclp, struct thread *td)
379200796Strasz{
380200796Strasz	int error;
381200796Strasz	mode_t mode;
382200796Strasz	struct inode *ip = VTOI(vp);
383200796Strasz
384200796Strasz	KASSERT(acl_nfs4_check(aclp, vp->v_type == VDIR) == 0,
385200796Strasz	    ("invalid ACL passed to ufs_setacl_nfs4_internal"));
386200796Strasz
387200796Strasz	if (acl_nfs4_is_trivial(aclp, ip->i_uid)) {
388200796Strasz		error = vn_extattr_rm(vp, IO_NODELOCKED,
389200796Strasz		    NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME, td);
390200796Strasz
391200796Strasz		/*
392200796Strasz		 * An attempt to remove ACL from a file that didn't have
393200796Strasz		 * any extended entries is not an error.
394200796Strasz		 */
395200796Strasz		if (error == ENOATTR)
396200796Strasz			error = 0;
397200796Strasz
398200796Strasz	} else {
399200796Strasz		error = vn_extattr_set(vp, IO_NODELOCKED,
400200796Strasz		    NFS4_ACL_EXTATTR_NAMESPACE, NFS4_ACL_EXTATTR_NAME,
401200796Strasz		    sizeof(*aclp), (char *) aclp, td);
402200796Strasz	}
403200796Strasz
404200796Strasz	/*
405200796Strasz	 * Map lack of attribute definition in UFS_EXTATTR into lack of
406200796Strasz	 * support for ACLs on the filesystem.
407200796Strasz	 */
408200796Strasz	if (error == ENOATTR)
409200796Strasz		return (EOPNOTSUPP);
410200796Strasz
411200796Strasz	if (error)
412200796Strasz		return (error);
413200796Strasz
414200796Strasz	mode = ip->i_mode;
415200796Strasz
416200796Strasz	acl_nfs4_sync_mode_from_acl(&mode, aclp);
417200796Strasz
418200796Strasz	ip->i_mode &= ACL_PRESERVE_MASK;
419200796Strasz	ip->i_mode |= mode;
420200796Strasz	DIP_SET(ip, i_mode, ip->i_mode);
421200796Strasz	ip->i_flag |= IN_CHANGE;
422200796Strasz
423200796Strasz	VN_KNOTE_UNLOCKED(vp, NOTE_ATTRIB);
424200796Strasz
425231122Skib	error = UFS_UPDATE(vp, 0);
426231122Skib	return (error);
427200796Strasz}
428200796Strasz
429200796Straszstatic int
430200796Straszufs_setacl_nfs4(struct vop_setacl_args *ap)
431200796Strasz{
432200796Strasz	int error;
433200796Strasz	struct inode *ip = VTOI(ap->a_vp);
434200796Strasz
435200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
436200796Strasz		return (EINVAL);
437200796Strasz
438200796Strasz	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
439200796Strasz		return (EROFS);
440200796Strasz
441200796Strasz	if (ap->a_aclp == NULL)
442200796Strasz		return (EINVAL);
443200796Strasz
444200796Strasz	error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp, ap->a_cred,
445200796Strasz	    ap->a_td);
446200796Strasz	if (error)
447200796Strasz		return (error);
448200796Strasz
449200796Strasz	/*
450200796Strasz	 * Authorize the ACL operation.
451200796Strasz	 */
452200796Strasz	if (ip->i_flags & (IMMUTABLE | APPEND))
453200796Strasz		return (EPERM);
454200796Strasz
455200796Strasz	/*
456200796Strasz	 * Must hold VWRITE_ACL or have appropriate privilege.
457200796Strasz	 */
458200796Strasz	if ((error = VOP_ACCESSX(ap->a_vp, VWRITE_ACL, ap->a_cred, ap->a_td)))
459200796Strasz		return (error);
460200796Strasz
461200796Strasz	/*
462200796Strasz	 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
463200796Strasz	 * Make sure it has enough room for that - splitting every entry
464200796Strasz	 * into two and appending "canonical six" entries at the end.
465200796Strasz	 */
466200796Strasz	if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
467200796Strasz		return (ENOSPC);
468200796Strasz
469200796Strasz	error = ufs_setacl_nfs4_internal(ap->a_vp, ap->a_aclp, ap->a_td);
470200796Strasz
471202971Strasz	return (error);
472200796Strasz}
473200796Strasz
474200796Strasz/*
47574822Srwatson * Set the ACL on a file.
47674822Srwatson *
47774822Srwatson * As part of the ACL is stored in the inode, and the rest in an EA,
47874822Srwatson * this is necessarily non-atomic, and has complex authorization.
47974822Srwatson * As ufs_setacl() includes elements of ufs_chown() and ufs_chmod(),
48074822Srwatson * a fair number of different access checks may be required to go ahead
48174822Srwatson * with the operation at all.
48274822Srwatson */
483192586Straszstatic int
484192586Straszufs_setacl_posix1e(struct vop_setacl_args *ap)
48574822Srwatson{
48674822Srwatson	struct inode *ip = VTOI(ap->a_vp);
48775571Srwatson	int error;
488192586Strasz	struct oldacl *old;
48974822Srwatson
490105179Srwatson	if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
491200796Strasz		return (EINVAL);
492105179Srwatson
49374822Srwatson	/*
49474822Srwatson	 * If this is a set operation rather than a delete operation,
49574822Srwatson	 * invoke VOP_ACLCHECK() on the passed ACL to determine if it is
49674822Srwatson	 * valid for the target.  This will include a check on ap->a_type.
49774822Srwatson	 */
49874822Srwatson	if (ap->a_aclp != NULL) {
49974822Srwatson		/*
50074822Srwatson		 * Set operation.
50174822Srwatson		 */
50274822Srwatson		error = VOP_ACLCHECK(ap->a_vp, ap->a_type, ap->a_aclp,
50383366Sjulian		    ap->a_cred, ap->a_td);
50474822Srwatson		if (error != 0)
50574822Srwatson			return (error);
50674822Srwatson	} else {
50774822Srwatson		/*
50874822Srwatson		 * Delete operation.
50974822Srwatson		 * POSIX.1e allows only deletion of the default ACL on a
51074822Srwatson		 * directory (ACL_TYPE_DEFAULT).
51174822Srwatson		 */
51274822Srwatson		if (ap->a_type != ACL_TYPE_DEFAULT)
51374822Srwatson			return (EINVAL);
51474822Srwatson		if (ap->a_vp->v_type != VDIR)
51574822Srwatson			return (ENOTDIR);
51674822Srwatson	}
51774822Srwatson
51874822Srwatson	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
51974822Srwatson		return (EROFS);
52074822Srwatson
52174822Srwatson	/*
52274822Srwatson	 * Authorize the ACL operation.
52374822Srwatson	 */
52474822Srwatson	if (ip->i_flags & (IMMUTABLE | APPEND))
52574822Srwatson		return (EPERM);
52674822Srwatson
52774822Srwatson	/*
52874822Srwatson	 * Must hold VADMIN (be file owner) or have appropriate privilege.
52974822Srwatson	 */
53083366Sjulian	if ((error = VOP_ACCESS(ap->a_vp, VADMIN, ap->a_cred, ap->a_td)))
53174822Srwatson		return (error);
53274822Srwatson
53374822Srwatson	switch(ap->a_type) {
53474822Srwatson	case ACL_TYPE_ACCESS:
535192586Strasz		old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
536192586Strasz		error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
537192586Strasz		if (error == 0) {
538192586Strasz			error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
539192586Strasz			    POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE,
540192586Strasz			    POSIX1E_ACL_ACCESS_EXTATTR_NAME, sizeof(*old),
541192586Strasz			    (char *) old, ap->a_td);
542192586Strasz		}
543192586Strasz		free(old, M_ACL);
54474822Srwatson		break;
54574822Srwatson
54674822Srwatson	case ACL_TYPE_DEFAULT:
54774822Srwatson		if (ap->a_aclp == NULL) {
54874822Srwatson			error = vn_extattr_rm(ap->a_vp, IO_NODELOCKED,
54974822Srwatson			    POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
55083366Sjulian			    POSIX1E_ACL_DEFAULT_EXTATTR_NAME, ap->a_td);
55174822Srwatson			/*
55274822Srwatson			 * Attempting to delete a non-present default ACL
55374822Srwatson			 * will return success for portability purposes.
55474822Srwatson			 * (TRIX)
55585581Srwatson			 *
55685581Srwatson			 * XXX: Note that since we can't distinguish
55785581Srwatson			 * "that EA is not supported" from "that EA is not
55885581Srwatson			 * defined", the success case here overlaps the
55991814Sgreen			 * the ENOATTR->EOPNOTSUPP case below.
56074822Srwatson		 	 */
56191814Sgreen			if (error == ENOATTR)
56274822Srwatson				error = 0;
563192586Strasz		} else {
564192586Strasz			old = malloc(sizeof(*old), M_ACL, M_WAITOK | M_ZERO);
565192586Strasz			error = acl_copy_acl_into_oldacl(ap->a_aclp, old);
566192586Strasz			if (error == 0) {
567192586Strasz				error = vn_extattr_set(ap->a_vp, IO_NODELOCKED,
568192586Strasz				    POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE,
569192586Strasz				    POSIX1E_ACL_DEFAULT_EXTATTR_NAME,
570192586Strasz				    sizeof(*old), (char *) old, ap->a_td);
571192586Strasz			}
572192586Strasz			free(old, M_ACL);
573192586Strasz		}
57474822Srwatson		break;
57574822Srwatson
57674822Srwatson	default:
57774822Srwatson		error = EINVAL;
57874822Srwatson	}
57974822Srwatson	/*
58074822Srwatson	 * Map lack of attribute definition in UFS_EXTATTR into lack of
58196755Strhodes	 * support for ACLs on the filesystem.
58274822Srwatson	 */
58391814Sgreen	if (error == ENOATTR)
58474822Srwatson		return (EOPNOTSUPP);
58574822Srwatson	if (error != 0)
58674822Srwatson		return (error);
58774822Srwatson
58874822Srwatson	if (ap->a_type == ACL_TYPE_ACCESS) {
58974822Srwatson		/*
59074822Srwatson		 * Now that the EA is successfully updated, update the
59174822Srwatson		 * inode and mark it as changed.
59274822Srwatson		 */
593118411Srwatson		ufs_sync_inode_from_acl(ap->a_aclp, ip);
59474822Srwatson		ip->i_flag |= IN_CHANGE;
595231122Skib		error = UFS_UPDATE(ap->a_vp, 0);
59674822Srwatson	}
59774822Srwatson
598133741Sjmg	VN_KNOTE_UNLOCKED(ap->a_vp, NOTE_ATTRIB);
599231122Skib	return (error);
60074822Srwatson}
60174822Srwatson
60274822Srwatsonint
603192586Straszufs_setacl(ap)
604192586Strasz	struct vop_setacl_args /* {
60574822Srwatson		struct vnode *vp;
60674822Srwatson		acl_type_t type;
60774822Srwatson		struct acl *aclp;
60874822Srwatson		struct ucred *cred;
60983366Sjulian		struct thread *td;
61074822Srwatson	} */ *ap;
61174822Srwatson{
612200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
613200796Strasz		return (EOPNOTSUPP);
61474822Srwatson
615200796Strasz	if (ap->a_type == ACL_TYPE_NFS4)
616200796Strasz		return (ufs_setacl_nfs4(ap));
617200796Strasz
618192586Strasz	return (ufs_setacl_posix1e(ap));
619192586Strasz}
620192586Strasz
621192586Straszstatic int
622200796Straszufs_aclcheck_nfs4(struct vop_aclcheck_args *ap)
623200796Strasz{
624200796Strasz	int is_directory = 0;
625200796Strasz
626200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS) == 0)
627200796Strasz		return (EINVAL);
628200796Strasz
629200796Strasz	/*
630200796Strasz	 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
631200796Strasz	 * Make sure it has enough room for that - splitting every entry
632200796Strasz	 * into two and appending "canonical six" entries at the end.
633200796Strasz	 */
634200796Strasz	if (ap->a_aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2)
635200796Strasz		return (ENOSPC);
636200796Strasz
637200796Strasz	if (ap->a_vp->v_type == VDIR)
638200796Strasz		is_directory = 1;
639200796Strasz
640200796Strasz	return (acl_nfs4_check(ap->a_aclp, is_directory));
641200796Strasz}
642200796Strasz
643200796Straszstatic int
644192586Straszufs_aclcheck_posix1e(struct vop_aclcheck_args *ap)
645192586Strasz{
646192586Strasz
647105179Srwatson	if ((ap->a_vp->v_mount->mnt_flag & MNT_ACLS) == 0)
648200796Strasz		return (EINVAL);
649105179Srwatson
65074822Srwatson	/*
65174822Srwatson	 * Verify we understand this type of ACL, and that it applies
65274822Srwatson	 * to this kind of object.
65374822Srwatson	 * Rely on the acl_posix1e_check() routine to verify the contents.
65474822Srwatson	 */
65574822Srwatson	switch(ap->a_type) {
65674822Srwatson	case ACL_TYPE_ACCESS:
65774822Srwatson		break;
65874822Srwatson
65974822Srwatson	case ACL_TYPE_DEFAULT:
66074822Srwatson		if (ap->a_vp->v_type != VDIR)
66174822Srwatson			return (EINVAL);
66274822Srwatson		break;
66374822Srwatson
66474822Srwatson	default:
66574822Srwatson		return (EINVAL);
66674822Srwatson	}
667192586Strasz
668192586Strasz	if (ap->a_aclp->acl_cnt > OLDACL_MAX_ENTRIES)
669192586Strasz		return (EINVAL);
670192586Strasz
67174822Srwatson	return (acl_posix1e_check(ap->a_aclp));
67274822Srwatson}
67374822Srwatson
674192586Strasz/*
675192586Strasz * Check the validity of an ACL for a file.
676192586Strasz */
677192586Straszint
678192586Straszufs_aclcheck(ap)
679192586Strasz	struct vop_aclcheck_args /* {
680192586Strasz		struct vnode *vp;
681192586Strasz		acl_type_t type;
682192586Strasz		struct acl *aclp;
683192586Strasz		struct ucred *cred;
684192586Strasz		struct thread *td;
685192586Strasz	} */ *ap;
686192586Strasz{
687192586Strasz
688200796Strasz	if ((ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) == 0)
689200796Strasz		return (EOPNOTSUPP);
690200796Strasz
691200796Strasz	if (ap->a_type == ACL_TYPE_NFS4)
692200796Strasz		return (ufs_aclcheck_nfs4(ap));
693200796Strasz
694192586Strasz	return (ufs_aclcheck_posix1e(ap));
695192586Strasz}
696192586Strasz
69774822Srwatson#endif /* !UFS_ACL */
698