xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 7867)
1 /*	vfs_vnops.c	4.27	82/08/24	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/fs.h"
8 #include "../h/file.h"
9 #include "../h/conf.h"
10 #include "../h/inode.h"
11 #include "../h/reg.h"
12 #include "../h/acct.h"
13 #include "../h/mount.h"
14 #include "../h/socket.h"
15 #include "../h/socketvar.h"
16 #include "../h/proc.h"
17 
18 /*
19  * Openi called to allow handler
20  * of special files to initialize and
21  * validate before actual IO.
22  */
23 openi(ip, mode)
24 	register struct inode *ip;
25 {
26 	dev_t dev;
27 	register unsigned int maj;
28 
29 	dev = (dev_t)ip->i_rdev;
30 	maj = major(dev);
31 	switch (ip->i_mode&IFMT) {
32 
33 	case IFCHR:
34 		if (maj >= nchrdev)
35 			goto bad;
36 		(*cdevsw[maj].d_open)(dev, mode);
37 		break;
38 
39 	case IFBLK:
40 		if (maj >= nblkdev)
41 			goto bad;
42 		(*bdevsw[maj].d_open)(dev, mode);
43 	}
44 	return;
45 
46 bad:
47 	u.u_error = ENXIO;
48 }
49 
50 /*
51  * Check mode permission on inode pointer.
52  * Mode is READ, WRITE or EXEC.
53  * In the case of WRITE, the
54  * read-only status of the file
55  * system is checked.
56  * Also in WRITE, prototype text
57  * segments cannot be written.
58  * The mode is shifted to select
59  * the owner/group/other fields.
60  * The super user is granted all
61  * permissions.
62  */
63 access(ip, mode)
64 	register struct inode *ip;
65 	int mode;
66 {
67 	register m;
68 	register int *gp;
69 
70 	m = mode;
71 	if (m == IWRITE) {
72 		if (ip->i_fs->fs_ronly != 0) {
73 			u.u_error = EROFS;
74 			return (1);
75 		}
76 		if (ip->i_flag&ITEXT)		/* try to free text */
77 			xrele(ip);
78 		if (ip->i_flag & ITEXT) {
79 			u.u_error = ETXTBSY;
80 			return (1);
81 		}
82 	}
83 	if (u.u_uid == 0)
84 		return (0);
85 	if (u.u_uid != ip->i_uid) {
86 		m >>= 3;
87 		for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
88 			if (ip->i_gid != *gp)
89 				goto found;
90 		m >>= 3;
91 found:
92 		;
93 	}
94 	if ((ip->i_mode&m) != 0)
95 		return (0);
96 	u.u_error = EACCES;
97 	return (1);
98 }
99 
100 /*
101  * Look up a pathname and test if
102  * the resultant inode is owned by the
103  * current user.
104  * If not, try for super-user.
105  * If permission is granted,
106  * return inode pointer.
107  */
108 struct inode *
109 owner(follow)
110 	int follow;
111 {
112 	register struct inode *ip;
113 
114 	ip = namei(uchar, 0, follow);
115 	if (ip == NULL)
116 		return (NULL);
117 	if (u.u_uid == ip->i_uid)
118 		return (ip);
119 	if (suser())
120 		return (ip);
121 	iput(ip);
122 	return (NULL);
123 }
124 
125 /*
126  * Test if the current user is the
127  * super user.
128  */
129 suser()
130 {
131 
132 	if (u.u_uid == 0) {
133 		u.u_acflag |= ASU;
134 		return (1);
135 	}
136 	u.u_error = EPERM;
137 	return (0);
138 }
139