xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 11812)
1*11812Ssam /*	vfs_vnops.c	4.34	83/03/31	*/
218Sbill 
39764Ssam #include "../machine/reg.h"
49764Ssam 
518Sbill #include "../h/param.h"
618Sbill #include "../h/systm.h"
718Sbill #include "../h/dir.h"
818Sbill #include "../h/user.h"
96569Smckusic #include "../h/fs.h"
1018Sbill #include "../h/file.h"
1118Sbill #include "../h/conf.h"
1218Sbill #include "../h/inode.h"
1318Sbill #include "../h/acct.h"
142302Skre #include "../h/mount.h"
154914Swnj #include "../h/socket.h"
164914Swnj #include "../h/socketvar.h"
175581Swnj #include "../h/proc.h"
189165Ssam #include "../h/nami.h"
1918Sbill 
2018Sbill /*
214817Swnj  * Openi called to allow handler
2218Sbill  * of special files to initialize and
2318Sbill  * validate before actual IO.
2418Sbill  */
257654Ssam openi(ip, mode)
264817Swnj 	register struct inode *ip;
2718Sbill {
288565Sroot 	dev_t dev = (dev_t)ip->i_rdev;
298565Sroot 	register u_int maj = major(dev);
3018Sbill 
314817Swnj 	switch (ip->i_mode&IFMT) {
3218Sbill 
3318Sbill 	case IFCHR:
344817Swnj 		if (maj >= nchrdev)
358565Sroot 			return (ENXIO);
368565Sroot 		return ((*cdevsw[maj].d_open)(dev, mode));
3718Sbill 
3818Sbill 	case IFBLK:
394817Swnj 		if (maj >= nblkdev)
408565Sroot 			return (ENXIO);
418565Sroot 		return ((*bdevsw[maj].d_open)(dev, mode));
4218Sbill 	}
438565Sroot 	return (0);
4418Sbill }
4518Sbill 
4618Sbill /*
4718Sbill  * Check mode permission on inode pointer.
4818Sbill  * Mode is READ, WRITE or EXEC.
4918Sbill  * In the case of WRITE, the
5018Sbill  * read-only status of the file
5118Sbill  * system is checked.
5218Sbill  * Also in WRITE, prototype text
5318Sbill  * segments cannot be written.
5418Sbill  * The mode is shifted to select
5518Sbill  * the owner/group/other fields.
5618Sbill  * The super user is granted all
5718Sbill  * permissions.
5818Sbill  */
5918Sbill access(ip, mode)
604817Swnj 	register struct inode *ip;
614817Swnj 	int mode;
6218Sbill {
6318Sbill 	register m;
647867Sroot 	register int *gp;
6518Sbill 
6618Sbill 	m = mode;
674817Swnj 	if (m == IWRITE) {
6811162Ssam 		/*
6911162Ssam 		 * Disallow write attempts on read-only
7011162Ssam 		 * file systems; unless the file is a block
7111162Ssam 		 * or character device resident on the
7211162Ssam 		 * file system.
7311162Ssam 		 */
746569Smckusic 		if (ip->i_fs->fs_ronly != 0) {
758956Sroot 			if ((ip->i_mode & IFMT) != IFCHR &&
768956Sroot 			    (ip->i_mode & IFMT) != IFBLK) {
778956Sroot 				u.u_error = EROFS;
788956Sroot 				return (1);
798956Sroot 			}
8018Sbill 		}
8111162Ssam 		/*
8211162Ssam 		 * If there's shared text associated with
8311162Ssam 		 * the inode, try to free it up once.  If
8411162Ssam 		 * we fail, we can't allow writing.
8511162Ssam 		 */
8611162Ssam 		if (ip->i_flag&ITEXT)
8718Sbill 			xrele(ip);
884817Swnj 		if (ip->i_flag & ITEXT) {
8918Sbill 			u.u_error = ETXTBSY;
904817Swnj 			return (1);
9118Sbill 		}
9218Sbill 	}
9311162Ssam 	/*
9411162Ssam 	 * If you're the super-user,
9511162Ssam 	 * you always get access.
9611162Ssam 	 */
974817Swnj 	if (u.u_uid == 0)
984817Swnj 		return (0);
9911162Ssam 	/*
10011162Ssam 	 * Access check is based on only
10111162Ssam 	 * one of owner, group, public.
10211162Ssam 	 * If not owner, then check group.
10311162Ssam 	 * If not a member of the group, then
10411162Ssam 	 * check public access.
10511162Ssam 	 */
1064817Swnj 	if (u.u_uid != ip->i_uid) {
10718Sbill 		m >>= 3;
10811162Ssam 		if (u.u_gid == ip->i_gid)
10911162Ssam 			goto found;
110*11812Ssam 		gp = u.u_groups;
111*11812Ssam 		for (; gp < &u.u_groups[NGROUPS] && *gp != NOGROUP; gp++)
11210044Ssam 			if (ip->i_gid == *gp)
1137867Sroot 				goto found;
1147867Sroot 		m >>= 3;
1157867Sroot found:
1167867Sroot 		;
11718Sbill 	}
1184817Swnj 	if ((ip->i_mode&m) != 0)
1194817Swnj 		return (0);
12018Sbill 	u.u_error = EACCES;
1214817Swnj 	return (1);
12218Sbill }
12318Sbill 
12418Sbill /*
12518Sbill  * Look up a pathname and test if
12618Sbill  * the resultant inode is owned by the
12718Sbill  * current user.
12818Sbill  * If not, try for super-user.
12918Sbill  * If permission is granted,
13018Sbill  * return inode pointer.
13118Sbill  */
13218Sbill struct inode *
1335990Swnj owner(follow)
1345990Swnj 	int follow;
13518Sbill {
13618Sbill 	register struct inode *ip;
13718Sbill 
1389165Ssam 	ip = namei(uchar, LOOKUP, follow);
1394817Swnj 	if (ip == NULL)
1404817Swnj 		return (NULL);
1414817Swnj 	if (u.u_uid == ip->i_uid)
1424817Swnj 		return (ip);
1434817Swnj 	if (suser())
1444817Swnj 		return (ip);
14518Sbill 	iput(ip);
1464817Swnj 	return (NULL);
14718Sbill }
14818Sbill 
14918Sbill /*
15018Sbill  * Test if the current user is the
15118Sbill  * super user.
15218Sbill  */
15318Sbill suser()
15418Sbill {
15518Sbill 
1564817Swnj 	if (u.u_uid == 0) {
15718Sbill 		u.u_acflag |= ASU;
1584817Swnj 		return (1);
15918Sbill 	}
16018Sbill 	u.u_error = EPERM;
1614817Swnj 	return (0);
16218Sbill }
163