Commit | Line | Data |
---|---|---|
f926daf9 | 1 | /* vfs_vnops.c 4.34 83/03/31 */ |
961945a8 SL |
2 | |
3 | #include "../machine/reg.h" | |
fc96c615 BJ |
4 | |
5 | #include "../h/param.h" | |
6 | #include "../h/systm.h" | |
7 | #include "../h/dir.h" | |
8 | #include "../h/user.h" | |
6459ebe0 | 9 | #include "../h/fs.h" |
fc96c615 BJ |
10 | #include "../h/file.h" |
11 | #include "../h/conf.h" | |
12 | #include "../h/inode.h" | |
fc96c615 | 13 | #include "../h/acct.h" |
f5b8937b | 14 | #include "../h/mount.h" |
ae921915 BJ |
15 | #include "../h/socket.h" |
16 | #include "../h/socketvar.h" | |
f3156a73 | 17 | #include "../h/proc.h" |
4f083fd7 | 18 | #include "../h/nami.h" |
fc96c615 | 19 | |
fc96c615 | 20 | /* |
fdd90ee3 | 21 | * Openi called to allow handler |
fc96c615 BJ |
22 | * of special files to initialize and |
23 | * validate before actual IO. | |
24 | */ | |
6b4c7376 | 25 | openi(ip, mode) |
fdd90ee3 | 26 | register struct inode *ip; |
fc96c615 | 27 | { |
f356c336 BJ |
28 | dev_t dev = (dev_t)ip->i_rdev; |
29 | register u_int maj = major(dev); | |
fc96c615 | 30 | |
fdd90ee3 | 31 | switch (ip->i_mode&IFMT) { |
fc96c615 BJ |
32 | |
33 | case IFCHR: | |
fdd90ee3 | 34 | if (maj >= nchrdev) |
f356c336 BJ |
35 | return (ENXIO); |
36 | return ((*cdevsw[maj].d_open)(dev, mode)); | |
fc96c615 BJ |
37 | |
38 | case IFBLK: | |
fdd90ee3 | 39 | if (maj >= nblkdev) |
f356c336 BJ |
40 | return (ENXIO); |
41 | return ((*bdevsw[maj].d_open)(dev, mode)); | |
fc96c615 | 42 | } |
f356c336 | 43 | return (0); |
fc96c615 BJ |
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) | |
fdd90ee3 BJ |
60 | register struct inode *ip; |
61 | int mode; | |
fc96c615 BJ |
62 | { |
63 | register m; | |
197da11b | 64 | register int *gp; |
fc96c615 BJ |
65 | |
66 | m = mode; | |
fdd90ee3 | 67 | if (m == IWRITE) { |
40123f2c SL |
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 | */ | |
6459ebe0 | 74 | if (ip->i_fs->fs_ronly != 0) { |
77cc8249 BJ |
75 | if ((ip->i_mode & IFMT) != IFCHR && |
76 | (ip->i_mode & IFMT) != IFBLK) { | |
77 | u.u_error = EROFS; | |
78 | return (1); | |
79 | } | |
fc96c615 | 80 | } |
40123f2c SL |
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) | |
fc96c615 | 87 | xrele(ip); |
fdd90ee3 | 88 | if (ip->i_flag & ITEXT) { |
fc96c615 | 89 | u.u_error = ETXTBSY; |
fdd90ee3 | 90 | return (1); |
fc96c615 BJ |
91 | } |
92 | } | |
40123f2c SL |
93 | /* |
94 | * If you're the super-user, | |
95 | * you always get access. | |
96 | */ | |
fdd90ee3 BJ |
97 | if (u.u_uid == 0) |
98 | return (0); | |
40123f2c SL |
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 | */ | |
fdd90ee3 | 106 | if (u.u_uid != ip->i_uid) { |
fc96c615 | 107 | m >>= 3; |
40123f2c SL |
108 | if (u.u_gid == ip->i_gid) |
109 | goto found; | |
8f3338c3 SL |
110 | gp = u.u_groups; |
111 | for (; gp < &u.u_groups[NGROUPS] && *gp != NOGROUP; gp++) | |
4060c051 | 112 | if (ip->i_gid == *gp) |
197da11b BJ |
113 | goto found; |
114 | m >>= 3; | |
115 | found: | |
116 | ; | |
fc96c615 | 117 | } |
fdd90ee3 BJ |
118 | if ((ip->i_mode&m) != 0) |
119 | return (0); | |
fc96c615 | 120 | u.u_error = EACCES; |
fdd90ee3 | 121 | return (1); |
fc96c615 BJ |
122 | } |
123 | ||
124 | /* | |
125 | * Look up a pathname and test if | |
126 | * the resultant inode is owned by the | |
127 | * current user. | |
128 | * If not, try for super-user. | |
129 | * If permission is granted, | |
130 | * return inode pointer. | |
131 | */ | |
132 | struct inode * | |
5485e062 BJ |
133 | owner(follow) |
134 | int follow; | |
fc96c615 BJ |
135 | { |
136 | register struct inode *ip; | |
137 | ||
4f083fd7 | 138 | ip = namei(uchar, LOOKUP, follow); |
fdd90ee3 BJ |
139 | if (ip == NULL) |
140 | return (NULL); | |
141 | if (u.u_uid == ip->i_uid) | |
142 | return (ip); | |
143 | if (suser()) | |
144 | return (ip); | |
fc96c615 | 145 | iput(ip); |
fdd90ee3 | 146 | return (NULL); |
fc96c615 BJ |
147 | } |
148 | ||
149 | /* | |
150 | * Test if the current user is the | |
151 | * super user. | |
152 | */ | |
153 | suser() | |
154 | { | |
155 | ||
fdd90ee3 | 156 | if (u.u_uid == 0) { |
fc96c615 | 157 | u.u_acflag |= ASU; |
fdd90ee3 | 158 | return (1); |
fc96c615 BJ |
159 | } |
160 | u.u_error = EPERM; | |
fdd90ee3 | 161 | return (0); |
fc96c615 | 162 | } |