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