xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 9764)
1*9764Ssam /*	vfs_vnops.c	4.31	82/12/17	*/
218Sbill 
3*9764Ssam #include "../machine/reg.h"
4*9764Ssam 
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) {
686569Smckusic 		if (ip->i_fs->fs_ronly != 0) {
698956Sroot 			if ((ip->i_mode & IFMT) != IFCHR &&
708956Sroot 			    (ip->i_mode & IFMT) != IFBLK) {
718956Sroot 				u.u_error = EROFS;
728956Sroot 				return (1);
738956Sroot 			}
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;
867867Sroot 		for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
877867Sroot 			if (ip->i_gid != *gp)
887867Sroot 				goto found;
897867Sroot 		m >>= 3;
907867Sroot found:
917867Sroot 		;
9218Sbill 	}
934817Swnj 	if ((ip->i_mode&m) != 0)
944817Swnj 		return (0);
9518Sbill 	u.u_error = EACCES;
964817Swnj 	return (1);
9718Sbill }
9818Sbill 
9918Sbill /*
10018Sbill  * Look up a pathname and test if
10118Sbill  * the resultant inode is owned by the
10218Sbill  * current user.
10318Sbill  * If not, try for super-user.
10418Sbill  * If permission is granted,
10518Sbill  * return inode pointer.
10618Sbill  */
10718Sbill struct inode *
1085990Swnj owner(follow)
1095990Swnj 	int follow;
11018Sbill {
11118Sbill 	register struct inode *ip;
11218Sbill 
1139165Ssam 	ip = namei(uchar, LOOKUP, follow);
1144817Swnj 	if (ip == NULL)
1154817Swnj 		return (NULL);
1164817Swnj 	if (u.u_uid == ip->i_uid)
1174817Swnj 		return (ip);
1184817Swnj 	if (suser())
1194817Swnj 		return (ip);
12018Sbill 	iput(ip);
1214817Swnj 	return (NULL);
12218Sbill }
12318Sbill 
12418Sbill /*
12518Sbill  * Test if the current user is the
12618Sbill  * super user.
12718Sbill  */
12818Sbill suser()
12918Sbill {
13018Sbill 
1314817Swnj 	if (u.u_uid == 0) {
13218Sbill 		u.u_acflag |= ASU;
1334817Swnj 		return (1);
13418Sbill 	}
13518Sbill 	u.u_error = EPERM;
1364817Swnj 	return (0);
13718Sbill }
138