4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
34 #pragma ident "%Z%%M% %I% %E% SMI"
36 #include <sys/param.h>
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/fcntl.h>
44 #include <sys/pathname.h>
46 #include <sys/vnode.h>
51 #include <sys/filio.h>
53 #include <sys/cmn_err.h>
54 #include <acl/acl_common.h>
56 #include <sys/unistd.h>
57 #include <sys/debug.h>
58 #include <sys/fs_subr.h>
60 static int cacl(int cmd
, int nentries
, void *aclbufp
,
61 vnode_t
*vp
, int *rv
);
64 * Get/Set ACL of a file.
67 acl(const char *fname
, int cmd
, int nentries
, void *aclbufp
)
74 /* Sanity check arguments */
76 return (set_errno(EINVAL
));
78 error
= lookupname((char *)fname
, UIO_USERSPACE
, FOLLOW
, NULLVPP
, &vp
);
80 if ((error
== ESTALE
) && fs_need_estale_retry(estale_retry
++))
82 return (set_errno(error
));
85 error
= cacl(cmd
, nentries
, aclbufp
, vp
, &rv
);
88 if ((error
== ESTALE
) && fs_need_estale_retry(estale_retry
++))
90 return (set_errno(error
));
96 * Get/Set ACL of a file with facl system call.
99 facl(int fdes
, int cmd
, int nentries
, void *aclbufp
)
105 if ((fp
= getf(fdes
)) == NULL
)
106 return (set_errno(EBADF
));
107 if (fp
->f_flag
& FREVOKED
) {
109 return (set_errno(EBADF
));
112 error
= cacl(cmd
, nentries
, aclbufp
, fp
->f_vnode
, &rv
);
116 return (set_errno(error
));
122 * Common code for acl() and facl().
125 cacl(int cmd
, int nentries
, void *aclbufp
, vnode_t
*vp
, int *rv
)
128 int aclbsize
; /* size of acl list in bytes */
129 int dfaclbsize
; /* size of default acl list in bytes */
132 aclent_t
*aclp
, *aaclp
;
138 bzero(&vsecattr
, sizeof (vsecattr_t
));
144 if (cmd
== GETACLCNT
) {
145 entry_size
= sizeof (aclent_t
);
146 vsecattr
.vsa_mask
= VSA_ACLCNT
| VSA_DFACLCNT
;
148 entry_size
= sizeof (ace_t
);
149 vsecattr
.vsa_mask
= VSA_ACECNT
;
151 if (error
= fop_getsecattr(vp
, &vsecattr
, 0, CRED(), NULL
))
153 *rv
= vsecattr
.vsa_aclcnt
+ vsecattr
.vsa_dfaclcnt
;
154 if (vsecattr
.vsa_aclcnt
&& vsecattr
.vsa_aclentp
) {
155 kmem_free(vsecattr
.vsa_aclentp
,
156 vsecattr
.vsa_aclcnt
* entry_size
);
158 if (vsecattr
.vsa_dfaclcnt
&& vsecattr
.vsa_dfaclentp
) {
159 kmem_free(vsecattr
.vsa_dfaclentp
,
160 vsecattr
.vsa_dfaclcnt
* entry_size
);
165 * Minimum ACL size is three entries so might as well
171 * NULL output buffer is also a pretty easy bail out.
175 vsecattr
.vsa_mask
= VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
|
177 if (error
= fop_getsecattr(vp
, &vsecattr
, 0, CRED(), NULL
))
179 /* Check user's buffer is big enough */
180 numacls
= vsecattr
.vsa_aclcnt
+ vsecattr
.vsa_dfaclcnt
;
181 aclbsize
= vsecattr
.vsa_aclcnt
* sizeof (aclent_t
);
182 dfaclbsize
= vsecattr
.vsa_dfaclcnt
* sizeof (aclent_t
);
183 if (numacls
> nentries
) {
187 /* Sort the acl & default acl lists */
188 if (vsecattr
.vsa_aclcnt
> 1)
189 ksort((caddr_t
)vsecattr
.vsa_aclentp
,
190 vsecattr
.vsa_aclcnt
, sizeof (aclent_t
), cmp2acls
);
191 if (vsecattr
.vsa_dfaclcnt
> 1)
192 ksort((caddr_t
)vsecattr
.vsa_dfaclentp
,
193 vsecattr
.vsa_dfaclcnt
, sizeof (aclent_t
), cmp2acls
);
195 uaddrp
= (caddr_t
)aclbufp
;
196 if (aclbsize
> 0) { /* bug #1262490 */
197 if (copyout(vsecattr
.vsa_aclentp
, uaddrp
, aclbsize
)) {
202 /* Copy out default acl's */
203 if (dfaclbsize
> 0) {
205 if (copyout(vsecattr
.vsa_dfaclentp
,
206 uaddrp
, dfaclbsize
)) {
212 if (vsecattr
.vsa_aclcnt
) {
213 kmem_free(vsecattr
.vsa_aclentp
,
214 vsecattr
.vsa_aclcnt
* sizeof (aclent_t
));
216 if (vsecattr
.vsa_dfaclcnt
) {
217 kmem_free(vsecattr
.vsa_dfaclentp
,
218 vsecattr
.vsa_dfaclcnt
* sizeof (aclent_t
));
226 vsecattr
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
227 if (error
= fop_getsecattr(vp
, &vsecattr
, 0, CRED(), NULL
))
230 aclbsize
= vsecattr
.vsa_aclcnt
* sizeof (ace_t
);
231 if (vsecattr
.vsa_aclcnt
> nentries
) {
237 if ((error
= copyout(vsecattr
.vsa_aclentp
,
238 aclbufp
, aclbsize
)) != 0) {
243 *rv
= vsecattr
.vsa_aclcnt
;
244 if (vsecattr
.vsa_aclcnt
) {
245 kmem_free(vsecattr
.vsa_aclentp
, vsecattr
.vsa_aclentsz
);
251 * Minimum ACL size is three entries so might as well
252 * bail out here. Also limit request size to prevent user
253 * from allocating too much kernel memory. Maximum size
254 * is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
255 * for the default ACL part. (bug 4058667)
257 if (nentries
< 3 || nentries
> (MAX_ACL_ENTRIES
* 2))
260 * NULL output buffer is also an easy bail out.
264 vsecattr
.vsa_mask
= VSA_ACL
;
265 aclbsize
= nentries
* sizeof (aclent_t
);
266 vsecattr
.vsa_aclentp
= kmem_alloc(aclbsize
, KM_SLEEP
);
267 aaclp
= vsecattr
.vsa_aclentp
;
268 vsecattr
.vsa_aclcnt
= nentries
;
269 uaddrp
= (caddr_t
)aclbufp
;
270 if (copyin(uaddrp
, vsecattr
.vsa_aclentp
, aclbsize
)) {
271 kmem_free(aaclp
, aclbsize
);
274 /* Sort the acl list */
275 ksort((caddr_t
)vsecattr
.vsa_aclentp
,
276 vsecattr
.vsa_aclcnt
, sizeof (aclent_t
), cmp2acls
);
278 /* Break into acl and default acl lists */
279 for (numacls
= 0, aclp
= vsecattr
.vsa_aclentp
;
280 numacls
< vsecattr
.vsa_aclcnt
;
282 if (aclp
->a_type
& ACL_DEFAULT
)
286 /* Find where defaults start (if any) */
287 if (numacls
< vsecattr
.vsa_aclcnt
) {
288 vsecattr
.vsa_mask
|= VSA_DFACL
;
289 vsecattr
.vsa_dfaclcnt
= nentries
- numacls
;
290 vsecattr
.vsa_dfaclentp
= aclp
;
291 vsecattr
.vsa_aclcnt
= numacls
;
293 /* Adjust if they're all defaults */
294 if (vsecattr
.vsa_aclcnt
== 0) {
295 vsecattr
.vsa_mask
&= ~VSA_ACL
;
296 vsecattr
.vsa_aclentp
= NULL
;
298 /* Only directories can have defaults */
299 if (vsecattr
.vsa_dfaclcnt
&& vp
->v_type
!= VDIR
) {
300 kmem_free(aaclp
, aclbsize
);
303 (void) fop_rwlock(vp
, V_WRITELOCK_TRUE
, NULL
);
304 if (error
= fop_setsecattr(vp
, &vsecattr
, 0, CRED(), NULL
)) {
305 kmem_free(aaclp
, aclbsize
);
306 fop_rwunlock(vp
, V_WRITELOCK_TRUE
, NULL
);
311 * Should return 0 upon success according to the man page
312 * and SVR4 semantics. (Bug #1214399: SETACL returns wrong rc)
315 kmem_free(aaclp
, aclbsize
);
316 fop_rwunlock(vp
, V_WRITELOCK_TRUE
, NULL
);
320 if (nentries
< 1 || nentries
> MAX_ACL_ENTRIES
)
326 vsecattr
.vsa_mask
= VSA_ACE
;
327 aclbsize
= nentries
* sizeof (ace_t
);
328 vsecattr
.vsa_aclentp
= kmem_alloc(aclbsize
, KM_SLEEP
);
329 aaclp
= vsecattr
.vsa_aclentp
;
330 vsecattr
.vsa_aclcnt
= nentries
;
331 vsecattr
.vsa_aclentsz
= aclbsize
;
332 uaddrp
= (caddr_t
)aclbufp
;
333 if (copyin(uaddrp
, vsecattr
.vsa_aclentp
, aclbsize
)) {
334 kmem_free(aaclp
, aclbsize
);
337 (void) fop_rwlock(vp
, V_WRITELOCK_TRUE
, NULL
);
338 if (error
= fop_setsecattr(vp
, &vsecattr
, 0, CRED(), NULL
)) {
339 kmem_free(aaclp
, aclbsize
);
340 fop_rwunlock(vp
, V_WRITELOCK_TRUE
, NULL
);
344 kmem_free(aaclp
, aclbsize
);
345 fop_rwunlock(vp
, V_WRITELOCK_TRUE
, NULL
);
355 if (aclbsize
&& vsecattr
.vsa_aclentp
)
356 kmem_free(vsecattr
.vsa_aclentp
, aclbsize
);
357 if (dfaclbsize
&& vsecattr
.vsa_dfaclentp
)
358 kmem_free(vsecattr
.vsa_dfaclentp
, dfaclbsize
);