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