xref: /titanic_41/usr/src/uts/common/syscall/access.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/cred_impl.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/pathname.h>
45 #include <sys/vnode.h>
46 #include <sys/uio.h>
47 #include <sys/cmn_err.h>
48 #include <sys/debug.h>
49 
50 /*
51  * Determine accessibility of file.
52  */
53 
54 #define	E_OK	010	/* use effective ids */
55 #define	R_OK	004
56 #define	W_OK	002
57 #define	X_OK	001
58 
59 int
60 access(char *fname, int fmode)
61 {
62 	vnode_t *vp;
63 	cred_t *tmpcr;
64 	int error;
65 	int mode;
66 	int eok;
67 	cred_t *cr;
68 
69 	if (fmode & ~(E_OK|R_OK|W_OK|X_OK))
70 		return (set_errno(EINVAL));
71 
72 	mode = ((fmode & (R_OK|W_OK|X_OK)) << 6);
73 
74 	cr = CRED();
75 
76 	/* OK to use effective uid/gid, i.e., no need to crdup(CRED())? */
77 	eok = (fmode & E_OK) ||
78 		(cr->cr_uid == cr->cr_ruid && cr->cr_gid == cr->cr_rgid);
79 
80 	if (eok)
81 		tmpcr = cr;
82 	else {
83 		tmpcr = crdup(cr);
84 		tmpcr->cr_uid = cr->cr_ruid;
85 		tmpcr->cr_gid = cr->cr_rgid;
86 		tmpcr->cr_ruid = cr->cr_uid;
87 		tmpcr->cr_rgid = cr->cr_gid;
88 	}
89 
90 lookup:
91 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
92 		if (error == ESTALE)
93 			goto lookup;
94 		if (!eok)
95 			crfree(tmpcr);
96 		return (set_errno(error));
97 	}
98 
99 	if (mode) {
100 		error = VOP_ACCESS(vp, mode, 0, tmpcr);
101 		if (error) {
102 			if (error == ESTALE) {
103 				VN_RELE(vp);
104 				goto lookup;
105 			}
106 			(void) set_errno(error);
107 		}
108 	}
109 
110 	if (!eok)
111 		crfree(tmpcr);
112 	VN_RELE(vp);
113 	return (error);
114 }
115