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