xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 8956)
1*8956Sroot /*	vfs_vnops.c	4.29	82/10/31	*/
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  */
237654Ssam openi(ip, mode)
244817Swnj 	register struct inode *ip;
2518Sbill {
268565Sroot 	dev_t dev = (dev_t)ip->i_rdev;
278565Sroot 	register u_int maj = major(dev);
2818Sbill 
294817Swnj 	switch (ip->i_mode&IFMT) {
3018Sbill 
3118Sbill 	case IFCHR:
324817Swnj 		if (maj >= nchrdev)
338565Sroot 			return (ENXIO);
348565Sroot 		return ((*cdevsw[maj].d_open)(dev, mode));
3518Sbill 
3618Sbill 	case IFBLK:
374817Swnj 		if (maj >= nblkdev)
388565Sroot 			return (ENXIO);
398565Sroot 		return ((*bdevsw[maj].d_open)(dev, mode));
4018Sbill 	}
418565Sroot 	return (0);
4218Sbill }
4318Sbill 
4418Sbill /*
4518Sbill  * Check mode permission on inode pointer.
4618Sbill  * Mode is READ, WRITE or EXEC.
4718Sbill  * In the case of WRITE, the
4818Sbill  * read-only status of the file
4918Sbill  * system is checked.
5018Sbill  * Also in WRITE, prototype text
5118Sbill  * segments cannot be written.
5218Sbill  * The mode is shifted to select
5318Sbill  * the owner/group/other fields.
5418Sbill  * The super user is granted all
5518Sbill  * permissions.
5618Sbill  */
5718Sbill access(ip, mode)
584817Swnj 	register struct inode *ip;
594817Swnj 	int mode;
6018Sbill {
6118Sbill 	register m;
627867Sroot 	register int *gp;
6318Sbill 
6418Sbill 	m = mode;
654817Swnj 	if (m == IWRITE) {
666569Smckusic 		if (ip->i_fs->fs_ronly != 0) {
67*8956Sroot 			if ((ip->i_mode & IFMT) != IFCHR &&
68*8956Sroot 			    (ip->i_mode & IFMT) != IFBLK) {
69*8956Sroot 				u.u_error = EROFS;
70*8956Sroot 				return (1);
71*8956Sroot 			}
7218Sbill 		}
7318Sbill 		if (ip->i_flag&ITEXT)		/* try to free text */
7418Sbill 			xrele(ip);
754817Swnj 		if (ip->i_flag & ITEXT) {
7618Sbill 			u.u_error = ETXTBSY;
774817Swnj 			return (1);
7818Sbill 		}
7918Sbill 	}
804817Swnj 	if (u.u_uid == 0)
814817Swnj 		return (0);
824817Swnj 	if (u.u_uid != ip->i_uid) {
8318Sbill 		m >>= 3;
847867Sroot 		for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
857867Sroot 			if (ip->i_gid != *gp)
867867Sroot 				goto found;
877867Sroot 		m >>= 3;
887867Sroot found:
897867Sroot 		;
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