xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 7654)
1*7654Ssam /*	vfs_vnops.c	4.26	82/08/03	*/
218Sbill 
318Sbill #include "../h/param.h"
418Sbill #include "../h/systm.h"
518Sbill #include "../h/dir.h"
618Sbill #include "../h/user.h"
76569Smckusic #include "../h/fs.h"
818Sbill #include "../h/file.h"
918Sbill #include "../h/conf.h"
1018Sbill #include "../h/inode.h"
1118Sbill #include "../h/reg.h"
1218Sbill #include "../h/acct.h"
132302Skre #include "../h/mount.h"
144914Swnj #include "../h/socket.h"
154914Swnj #include "../h/socketvar.h"
165581Swnj #include "../h/proc.h"
1718Sbill 
1818Sbill /*
194817Swnj  * Openi called to allow handler
2018Sbill  * of special files to initialize and
2118Sbill  * validate before actual IO.
2218Sbill  */
23*7654Ssam openi(ip, mode)
244817Swnj 	register struct inode *ip;
2518Sbill {
2618Sbill 	dev_t dev;
2718Sbill 	register unsigned int maj;
2818Sbill 
296569Smckusic 	dev = (dev_t)ip->i_rdev;
3018Sbill 	maj = major(dev);
314817Swnj 	switch (ip->i_mode&IFMT) {
3218Sbill 
3318Sbill 	case IFCHR:
344817Swnj 		if (maj >= nchrdev)
3518Sbill 			goto bad;
36*7654Ssam 		(*cdevsw[maj].d_open)(dev, mode);
3718Sbill 		break;
3818Sbill 
3918Sbill 	case IFBLK:
404817Swnj 		if (maj >= nblkdev)
4118Sbill 			goto bad;
42*7654Ssam 		(*bdevsw[maj].d_open)(dev, mode);
4318Sbill 	}
4418Sbill 	return;
4518Sbill 
4618Sbill bad:
4718Sbill 	u.u_error = ENXIO;
4818Sbill }
4918Sbill 
5018Sbill /*
5118Sbill  * Check mode permission on inode pointer.
5218Sbill  * Mode is READ, WRITE or EXEC.
5318Sbill  * In the case of WRITE, the
5418Sbill  * read-only status of the file
5518Sbill  * system is checked.
5618Sbill  * Also in WRITE, prototype text
5718Sbill  * segments cannot be written.
5818Sbill  * The mode is shifted to select
5918Sbill  * the owner/group/other fields.
6018Sbill  * The super user is granted all
6118Sbill  * permissions.
6218Sbill  */
6318Sbill access(ip, mode)
644817Swnj 	register struct inode *ip;
654817Swnj 	int mode;
6618Sbill {
6718Sbill 	register m;
6818Sbill 
6918Sbill 	m = mode;
704817Swnj 	if (m == IWRITE) {
716569Smckusic 		if (ip->i_fs->fs_ronly != 0) {
7218Sbill 			u.u_error = EROFS;
734817Swnj 			return (1);
7418Sbill 		}
7518Sbill 		if (ip->i_flag&ITEXT)		/* try to free text */
7618Sbill 			xrele(ip);
774817Swnj 		if (ip->i_flag & ITEXT) {
7818Sbill 			u.u_error = ETXTBSY;
794817Swnj 			return (1);
8018Sbill 		}
8118Sbill 	}
824817Swnj 	if (u.u_uid == 0)
834817Swnj 		return (0);
844817Swnj 	if (u.u_uid != ip->i_uid) {
8518Sbill 		m >>= 3;
865855Swnj 		if (ip->i_gid >= NGRPS ||
875855Swnj 		    (u.u_grps[ip->i_gid/(sizeof(int)*8)] &
885855Swnj 		     (1 << ip->i_gid%(sizeof(int)*8)) == 0))
8918Sbill 			m >>= 3;
9018Sbill 	}
914817Swnj 	if ((ip->i_mode&m) != 0)
924817Swnj 		return (0);
9318Sbill 	u.u_error = EACCES;
944817Swnj 	return (1);
9518Sbill }
9618Sbill 
9718Sbill /*
9818Sbill  * Look up a pathname and test if
9918Sbill  * the resultant inode is owned by the
10018Sbill  * current user.
10118Sbill  * If not, try for super-user.
10218Sbill  * If permission is granted,
10318Sbill  * return inode pointer.
10418Sbill  */
10518Sbill struct inode *
1065990Swnj owner(follow)
1075990Swnj 	int follow;
10818Sbill {
10918Sbill 	register struct inode *ip;
11018Sbill 
1115990Swnj 	ip = namei(uchar, 0, follow);
1124817Swnj 	if (ip == NULL)
1134817Swnj 		return (NULL);
1144817Swnj 	if (u.u_uid == ip->i_uid)
1154817Swnj 		return (ip);
1164817Swnj 	if (suser())
1174817Swnj 		return (ip);
11818Sbill 	iput(ip);
1194817Swnj 	return (NULL);
12018Sbill }
12118Sbill 
12218Sbill /*
12318Sbill  * Test if the current user is the
12418Sbill  * super user.
12518Sbill  */
12618Sbill suser()
12718Sbill {
12818Sbill 
1294817Swnj 	if (u.u_uid == 0) {
13018Sbill 		u.u_acflag |= ASU;
1314817Swnj 		return (1);
13218Sbill 	}
13318Sbill 	u.u_error = EPERM;
1344817Swnj 	return (0);
13518Sbill }
136