1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <sys/param.h>
38*0Sstevel@tonic-gate #include <sys/isa_defs.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/sysmacros.h>
41*0Sstevel@tonic-gate #include <sys/cred.h>
42*0Sstevel@tonic-gate #include <sys/systm.h>
43*0Sstevel@tonic-gate #include <sys/errno.h>
44*0Sstevel@tonic-gate #include <sys/fcntl.h>
45*0Sstevel@tonic-gate #include <sys/pathname.h>
46*0Sstevel@tonic-gate #include <sys/vfs.h>
47*0Sstevel@tonic-gate #include <sys/vnode.h>
48*0Sstevel@tonic-gate #include <sys/file.h>
49*0Sstevel@tonic-gate #include <sys/mode.h>
50*0Sstevel@tonic-gate #include <sys/uio.h>
51*0Sstevel@tonic-gate #include <sys/kmem.h>
52*0Sstevel@tonic-gate #include <sys/filio.h>
53*0Sstevel@tonic-gate #include <sys/acl.h>
54*0Sstevel@tonic-gate #include <sys/cmn_err.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #include <sys/unistd.h>
57*0Sstevel@tonic-gate #include <sys/debug.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static int cacl(int cmd, int nentries, void *aclbufp,
60*0Sstevel@tonic-gate     vnode_t *vp, int *rv);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Get/Set ACL of a file.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate int
66*0Sstevel@tonic-gate acl(const char *fname, int cmd, int nentries, void *aclbufp)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	struct vnode *vp;
69*0Sstevel@tonic-gate 	int error;
70*0Sstevel@tonic-gate 	int rv = 0;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	/* Sanity check arguments */
73*0Sstevel@tonic-gate 	if (fname == NULL)
74*0Sstevel@tonic-gate 		return (set_errno(EINVAL));
75*0Sstevel@tonic-gate lookup:
76*0Sstevel@tonic-gate 	error = lookupname((char *)fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
77*0Sstevel@tonic-gate 	if (error) {
78*0Sstevel@tonic-gate 		if (error == ESTALE)
79*0Sstevel@tonic-gate 			goto lookup;
80*0Sstevel@tonic-gate 		return (set_errno(error));
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	error = cacl(cmd, nentries, aclbufp, vp, &rv);
84*0Sstevel@tonic-gate 	VN_RELE(vp);
85*0Sstevel@tonic-gate 	if (error) {
86*0Sstevel@tonic-gate 		if (error == ESTALE)
87*0Sstevel@tonic-gate 			goto lookup;
88*0Sstevel@tonic-gate 		return (set_errno(error));
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	return (rv);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate  * Get/Set ACL of a file with facl system call.
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate int
97*0Sstevel@tonic-gate facl(int fdes, int cmd, int nentries, void *aclbufp)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	file_t *fp;
100*0Sstevel@tonic-gate 	int error;
101*0Sstevel@tonic-gate 	int rv = 0;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
104*0Sstevel@tonic-gate 		return (set_errno(EBADF));
105*0Sstevel@tonic-gate #ifdef C2_AUDIT
106*0Sstevel@tonic-gate 	if (fp->f_flag & FREVOKED) {
107*0Sstevel@tonic-gate 		releasef(fdes);
108*0Sstevel@tonic-gate 		return (set_errno(EBADF));
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate #endif /* C2_AUDIT */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	error = cacl(cmd, nentries, aclbufp, fp->f_vnode, &rv);
113*0Sstevel@tonic-gate 	releasef(fdes);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (error)
116*0Sstevel@tonic-gate 		return (set_errno(error));
117*0Sstevel@tonic-gate 	return (rv);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate  * Common code for acl() and facl().
123*0Sstevel@tonic-gate  */
124*0Sstevel@tonic-gate static int
125*0Sstevel@tonic-gate cacl(int cmd, int nentries, void *aclbufp, vnode_t *vp, int *rv)
126*0Sstevel@tonic-gate {
127*0Sstevel@tonic-gate 	int		error;
128*0Sstevel@tonic-gate 	int		aclbsize;	/* size of acl list in bytes */
129*0Sstevel@tonic-gate 	int		dfaclbsize;	/* size of default acl list in bytes */
130*0Sstevel@tonic-gate 	int		numacls;
131*0Sstevel@tonic-gate 	caddr_t		uaddrp;
132*0Sstevel@tonic-gate 	aclent_t	*aclp, *aaclp;
133*0Sstevel@tonic-gate 	vsecattr_t	vsecattr;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	ASSERT(vp);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	bzero(&vsecattr, sizeof (vsecattr_t));
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	switch (cmd) {
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	case ACE_GETACLCNT:
142*0Sstevel@tonic-gate 	case GETACLCNT:
143*0Sstevel@tonic-gate 		if (cmd == GETACLCNT)
144*0Sstevel@tonic-gate 			vsecattr.vsa_mask = VSA_ACLCNT | VSA_DFACLCNT;
145*0Sstevel@tonic-gate 		else
146*0Sstevel@tonic-gate 			vsecattr.vsa_mask = VSA_ACECNT;
147*0Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
148*0Sstevel@tonic-gate 			return (error);
149*0Sstevel@tonic-gate 		*rv = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
150*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp) {
151*0Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
152*0Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (aclent_t));
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt && vsecattr.vsa_dfaclentp) {
155*0Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_dfaclentp,
156*0Sstevel@tonic-gate 			    vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 		break;
159*0Sstevel@tonic-gate 	case GETACL:
160*0Sstevel@tonic-gate 		/*
161*0Sstevel@tonic-gate 		 * Minimum ACL size is three entries so might as well
162*0Sstevel@tonic-gate 		 * bail out here.
163*0Sstevel@tonic-gate 		 */
164*0Sstevel@tonic-gate 		if (nentries < 3)
165*0Sstevel@tonic-gate 			return (EINVAL);
166*0Sstevel@tonic-gate 		/*
167*0Sstevel@tonic-gate 		 * NULL output buffer is also a pretty easy bail out.
168*0Sstevel@tonic-gate 		 */
169*0Sstevel@tonic-gate 		if (aclbufp == NULL)
170*0Sstevel@tonic-gate 			return (EFAULT);
171*0Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
172*0Sstevel@tonic-gate 		    VSA_DFACLCNT;
173*0Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
174*0Sstevel@tonic-gate 			return (error);
175*0Sstevel@tonic-gate 		/* Check user's buffer is big enough */
176*0Sstevel@tonic-gate 		numacls = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
177*0Sstevel@tonic-gate 		aclbsize = vsecattr.vsa_aclcnt * sizeof (aclent_t);
178*0Sstevel@tonic-gate 		dfaclbsize = vsecattr.vsa_dfaclcnt * sizeof (aclent_t);
179*0Sstevel@tonic-gate 		if (numacls > nentries) {
180*0Sstevel@tonic-gate 			error = ENOSPC;
181*0Sstevel@tonic-gate 			goto errout;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		/* Sort the acl & default acl lists */
184*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt > 1)
185*0Sstevel@tonic-gate 			ksort((caddr_t)vsecattr.vsa_aclentp,
186*0Sstevel@tonic-gate 			vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
187*0Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt > 1)
188*0Sstevel@tonic-gate 			ksort((caddr_t)vsecattr.vsa_dfaclentp,
189*0Sstevel@tonic-gate 			vsecattr.vsa_dfaclcnt, sizeof (aclent_t), cmp2acls);
190*0Sstevel@tonic-gate 		/* Copy out acl's */
191*0Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
192*0Sstevel@tonic-gate 		if (aclbsize > 0) {	/* bug #1262490 */
193*0Sstevel@tonic-gate 			if (copyout(vsecattr.vsa_aclentp, uaddrp, aclbsize)) {
194*0Sstevel@tonic-gate 				error = EFAULT;
195*0Sstevel@tonic-gate 				goto errout;
196*0Sstevel@tonic-gate 			}
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		/* Copy out default acl's */
199*0Sstevel@tonic-gate 		if (dfaclbsize > 0) {
200*0Sstevel@tonic-gate 			uaddrp += aclbsize;
201*0Sstevel@tonic-gate 			if (copyout(vsecattr.vsa_dfaclentp,
202*0Sstevel@tonic-gate 			    uaddrp, dfaclbsize)) {
203*0Sstevel@tonic-gate 				error = EFAULT;
204*0Sstevel@tonic-gate 				goto errout;
205*0Sstevel@tonic-gate 			}
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 		*rv = numacls;
208*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt) {
209*0Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
210*0Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (aclent_t));
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt) {
213*0Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_dfaclentp,
214*0Sstevel@tonic-gate 			    vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	case ACE_GETACL:
219*0Sstevel@tonic-gate 		if (nentries < 3)
220*0Sstevel@tonic-gate 			return (EINVAL);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		if (aclbufp == NULL)
223*0Sstevel@tonic-gate 			return (EFAULT);
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
226*0Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
227*0Sstevel@tonic-gate 			return (error);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 		aclbsize = vsecattr.vsa_aclcnt * sizeof (ace_t);
230*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt > nentries) {
231*0Sstevel@tonic-gate 			error = ENOSPC;
232*0Sstevel@tonic-gate 			goto errout;
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		if (aclbsize > 0) {
236*0Sstevel@tonic-gate 			if ((error = copyout(vsecattr.vsa_aclentp,
237*0Sstevel@tonic-gate 			    aclbufp, aclbsize)) != 0) {
238*0Sstevel@tonic-gate 				goto errout;
239*0Sstevel@tonic-gate 			}
240*0Sstevel@tonic-gate 		}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		*rv = vsecattr.vsa_aclcnt;
243*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt) {
244*0Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
245*0Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (ace_t));
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 		break;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	case SETACL:
250*0Sstevel@tonic-gate 		/*
251*0Sstevel@tonic-gate 		 * Minimum ACL size is three entries so might as well
252*0Sstevel@tonic-gate 		 * bail out here.  Also limit request size to prevent user
253*0Sstevel@tonic-gate 		 * from allocating too much kernel memory.  Maximum size
254*0Sstevel@tonic-gate 		 * is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
255*0Sstevel@tonic-gate 		 * for the default ACL part. (bug 4058667)
256*0Sstevel@tonic-gate 		 */
257*0Sstevel@tonic-gate 		if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
258*0Sstevel@tonic-gate 			return (EINVAL);
259*0Sstevel@tonic-gate 		/*
260*0Sstevel@tonic-gate 		 * NULL output buffer is also an easy bail out.
261*0Sstevel@tonic-gate 		 */
262*0Sstevel@tonic-gate 		if (aclbufp == NULL)
263*0Sstevel@tonic-gate 			return (EFAULT);
264*0Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACL;
265*0Sstevel@tonic-gate 		aclbsize = nentries * sizeof (aclent_t);
266*0Sstevel@tonic-gate 		vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
267*0Sstevel@tonic-gate 		aaclp = vsecattr.vsa_aclentp;
268*0Sstevel@tonic-gate 		vsecattr.vsa_aclcnt = nentries;
269*0Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
270*0Sstevel@tonic-gate 		if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
271*0Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
272*0Sstevel@tonic-gate 			return (EFAULT);
273*0Sstevel@tonic-gate 		}
274*0Sstevel@tonic-gate 		/* Sort the acl list */
275*0Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_aclentp,
276*0Sstevel@tonic-gate 		    vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 		/* Break into acl and default acl lists */
279*0Sstevel@tonic-gate 		for (numacls = 0, aclp = vsecattr.vsa_aclentp;
280*0Sstevel@tonic-gate 		    numacls < vsecattr.vsa_aclcnt;
281*0Sstevel@tonic-gate 		    aclp++, numacls++) {
282*0Sstevel@tonic-gate 			if (aclp->a_type & ACL_DEFAULT)
283*0Sstevel@tonic-gate 				break;
284*0Sstevel@tonic-gate 		}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 		/* Find where defaults start (if any) */
287*0Sstevel@tonic-gate 		if (numacls < vsecattr.vsa_aclcnt) {
288*0Sstevel@tonic-gate 			vsecattr.vsa_mask |= VSA_DFACL;
289*0Sstevel@tonic-gate 			vsecattr.vsa_dfaclcnt = nentries - numacls;
290*0Sstevel@tonic-gate 			vsecattr.vsa_dfaclentp = aclp;
291*0Sstevel@tonic-gate 			vsecattr.vsa_aclcnt = numacls;
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 		/* Adjust if they're all defaults */
294*0Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt == 0) {
295*0Sstevel@tonic-gate 			vsecattr.vsa_mask &= ~VSA_ACL;
296*0Sstevel@tonic-gate 			vsecattr.vsa_aclentp = NULL;
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 		/* Only directories can have defaults */
299*0Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt && vp->v_type != VDIR) {
300*0Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
301*0Sstevel@tonic-gate 			return (ENOTDIR);
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
304*0Sstevel@tonic-gate 		if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED())) {
305*0Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
306*0Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
307*0Sstevel@tonic-gate 			return (error);
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 		/*
311*0Sstevel@tonic-gate 		 * Should return 0 upon success according to the man page
312*0Sstevel@tonic-gate 		 * and SVR4 semantics. (Bug #1214399: SETACL returns wrong rc)
313*0Sstevel@tonic-gate 		 */
314*0Sstevel@tonic-gate 		*rv = 0;
315*0Sstevel@tonic-gate 		kmem_free(aaclp, aclbsize);
316*0Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
317*0Sstevel@tonic-gate 		break;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	case ACE_SETACL:
320*0Sstevel@tonic-gate 		if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
321*0Sstevel@tonic-gate 			return (EINVAL);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 		if (aclbufp == NULL)
324*0Sstevel@tonic-gate 			return (EFAULT);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACE;
327*0Sstevel@tonic-gate 		aclbsize = nentries * sizeof (ace_t);
328*0Sstevel@tonic-gate 		vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
329*0Sstevel@tonic-gate 		aaclp = vsecattr.vsa_aclentp;
330*0Sstevel@tonic-gate 		vsecattr.vsa_aclcnt = nentries;
331*0Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
332*0Sstevel@tonic-gate 		if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
333*0Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
334*0Sstevel@tonic-gate 			return (EFAULT);
335*0Sstevel@tonic-gate 		}
336*0Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
337*0Sstevel@tonic-gate 		if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED())) {
338*0Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
339*0Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
340*0Sstevel@tonic-gate 			return (error);
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 		*rv = 0;
343*0Sstevel@tonic-gate 		kmem_free(aaclp, aclbsize);
344*0Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
345*0Sstevel@tonic-gate 		break;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	default:
348*0Sstevel@tonic-gate 		return (EINVAL);
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	return (0);
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate errout:
354*0Sstevel@tonic-gate 	if (aclbsize && vsecattr.vsa_aclentp)
355*0Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
356*0Sstevel@tonic-gate 	if (dfaclbsize && vsecattr.vsa_dfaclentp)
357*0Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_dfaclentp, dfaclbsize);
358*0Sstevel@tonic-gate 	return (error);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate /*
363*0Sstevel@tonic-gate  * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
364*0Sstevel@tonic-gate  * v = Ptr to array/vector of objs
365*0Sstevel@tonic-gate  * n = # objs in the array
366*0Sstevel@tonic-gate  * s = size of each obj (must be multiples of a word size)
367*0Sstevel@tonic-gate  * f = ptr to function to compare two objs
368*0Sstevel@tonic-gate  *	returns (-1 = less than, 0 = equal, 1 = greater than
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate void
371*0Sstevel@tonic-gate ksort(caddr_t v, int n, int s, int (*f)())
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	int g, i, j, ii;
374*0Sstevel@tonic-gate 	unsigned int *p1, *p2;
375*0Sstevel@tonic-gate 	unsigned int tmp;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	/* No work to do */
378*0Sstevel@tonic-gate 	if (v == NULL || n <= 1)
379*0Sstevel@tonic-gate 		return;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	/* Sanity check on arguments */
382*0Sstevel@tonic-gate 	ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
383*0Sstevel@tonic-gate 	ASSERT(s > 0);
384*0Sstevel@tonic-gate 	for (g = n / 2; g > 0; g /= 2) {
385*0Sstevel@tonic-gate 		for (i = g; i < n; i++) {
386*0Sstevel@tonic-gate 			for (j = i - g; j >= 0 &&
387*0Sstevel@tonic-gate 				(*f)(v + j * s, v + (j + g) * s) == 1;
388*0Sstevel@tonic-gate 					j -= g) {
389*0Sstevel@tonic-gate 				p1 = (unsigned *)(v + j * s);
390*0Sstevel@tonic-gate 				p2 = (unsigned *)(v + (j + g) * s);
391*0Sstevel@tonic-gate 				for (ii = 0; ii < s / 4; ii++) {
392*0Sstevel@tonic-gate 					tmp = *p1;
393*0Sstevel@tonic-gate 					*p1++ = *p2;
394*0Sstevel@tonic-gate 					*p2++ = tmp;
395*0Sstevel@tonic-gate 				}
396*0Sstevel@tonic-gate 			}
397*0Sstevel@tonic-gate 		}
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate /*
402*0Sstevel@tonic-gate  * Compare two acls, all fields.  Returns:
403*0Sstevel@tonic-gate  * -1 (less than)
404*0Sstevel@tonic-gate  *  0 (equal)
405*0Sstevel@tonic-gate  * +1 (greater than)
406*0Sstevel@tonic-gate  */
407*0Sstevel@tonic-gate int
408*0Sstevel@tonic-gate cmp2acls(void *a, void *b)
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate 	aclent_t *x = (aclent_t *)a;
411*0Sstevel@tonic-gate 	aclent_t *y = (aclent_t *)b;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	/* Compare types */
414*0Sstevel@tonic-gate 	if (x->a_type < y->a_type)
415*0Sstevel@tonic-gate 		return (-1);
416*0Sstevel@tonic-gate 	if (x->a_type > y->a_type)
417*0Sstevel@tonic-gate 		return (1);
418*0Sstevel@tonic-gate 	/* Equal types; compare id's */
419*0Sstevel@tonic-gate 	if (x->a_id < y->a_id)
420*0Sstevel@tonic-gate 		return (-1);
421*0Sstevel@tonic-gate 	if (x->a_id > y->a_id)
422*0Sstevel@tonic-gate 		return (1);
423*0Sstevel@tonic-gate 	/* Equal ids; compare perms */
424*0Sstevel@tonic-gate 	if (x->a_perm < y->a_perm)
425*0Sstevel@tonic-gate 		return (-1);
426*0Sstevel@tonic-gate 	if (x->a_perm > y->a_perm)
427*0Sstevel@tonic-gate 		return (1);
428*0Sstevel@tonic-gate 	/* Totally equal */
429*0Sstevel@tonic-gate 	return (0);
430*0Sstevel@tonic-gate }
431