xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 8956)
1 /*	vfs_vnops.c	4.29	82/10/31	*/
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 			if ((ip->i_mode & IFMT) != IFCHR &&
68 			    (ip->i_mode & IFMT) != IFBLK) {
69 				u.u_error = EROFS;
70 				return (1);
71 			}
72 		}
73 		if (ip->i_flag&ITEXT)		/* try to free text */
74 			xrele(ip);
75 		if (ip->i_flag & ITEXT) {
76 			u.u_error = ETXTBSY;
77 			return (1);
78 		}
79 	}
80 	if (u.u_uid == 0)
81 		return (0);
82 	if (u.u_uid != ip->i_uid) {
83 		m >>= 3;
84 		for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
85 			if (ip->i_gid != *gp)
86 				goto found;
87 		m >>= 3;
88 found:
89 		;
90 	}
91 	if ((ip->i_mode&m) != 0)
92 		return (0);
93 	u.u_error = EACCES;
94 	return (1);
95 }
96 
97 /*
98  * Look up a pathname and test if
99  * the resultant inode is owned by the
100  * current user.
101  * If not, try for super-user.
102  * If permission is granted,
103  * return inode pointer.
104  */
105 struct inode *
106 owner(follow)
107 	int follow;
108 {
109 	register struct inode *ip;
110 
111 	ip = namei(uchar, 0, follow);
112 	if (ip == NULL)
113 		return (NULL);
114 	if (u.u_uid == ip->i_uid)
115 		return (ip);
116 	if (suser())
117 		return (ip);
118 	iput(ip);
119 	return (NULL);
120 }
121 
122 /*
123  * Test if the current user is the
124  * super user.
125  */
126 suser()
127 {
128 
129 	if (u.u_uid == 0) {
130 		u.u_acflag |= ASU;
131 		return (1);
132 	}
133 	u.u_error = EPERM;
134 	return (0);
135 }
136