Commit | Line | Data |
---|---|---|
6b4c7376 | 1 | /* vfs_vnops.c 4.26 82/08/03 */ |
fc96c615 BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
5 | #include "../h/dir.h" | |
6 | #include "../h/user.h" | |
6459ebe0 | 7 | #include "../h/fs.h" |
fc96c615 BJ |
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" | |
f5b8937b | 13 | #include "../h/mount.h" |
ae921915 BJ |
14 | #include "../h/socket.h" |
15 | #include "../h/socketvar.h" | |
f3156a73 | 16 | #include "../h/proc.h" |
fc96c615 | 17 | |
fc96c615 | 18 | /* |
fdd90ee3 | 19 | * Openi called to allow handler |
fc96c615 BJ |
20 | * of special files to initialize and |
21 | * validate before actual IO. | |
22 | */ | |
6b4c7376 | 23 | openi(ip, mode) |
fdd90ee3 | 24 | register struct inode *ip; |
fc96c615 BJ |
25 | { |
26 | dev_t dev; | |
27 | register unsigned int maj; | |
28 | ||
6459ebe0 | 29 | dev = (dev_t)ip->i_rdev; |
fc96c615 | 30 | maj = major(dev); |
fdd90ee3 | 31 | switch (ip->i_mode&IFMT) { |
fc96c615 BJ |
32 | |
33 | case IFCHR: | |
fdd90ee3 | 34 | if (maj >= nchrdev) |
fc96c615 | 35 | goto bad; |
6b4c7376 | 36 | (*cdevsw[maj].d_open)(dev, mode); |
fc96c615 BJ |
37 | break; |
38 | ||
39 | case IFBLK: | |
fdd90ee3 | 40 | if (maj >= nblkdev) |
fc96c615 | 41 | goto bad; |
6b4c7376 | 42 | (*bdevsw[maj].d_open)(dev, mode); |
fc96c615 BJ |
43 | } |
44 | return; | |
45 | ||
46 | bad: | |
47 | u.u_error = ENXIO; | |
48 | } | |
49 | ||
50 | /* | |
51 | * Check mode permission on inode pointer. | |
52 | * Mode is READ, WRITE or EXEC. | |
53 | * In the case of WRITE, the | |
54 | * read-only status of the file | |
55 | * system is checked. | |
56 | * Also in WRITE, prototype text | |
57 | * segments cannot be written. | |
58 | * The mode is shifted to select | |
59 | * the owner/group/other fields. | |
60 | * The super user is granted all | |
61 | * permissions. | |
62 | */ | |
63 | access(ip, mode) | |
fdd90ee3 BJ |
64 | register struct inode *ip; |
65 | int mode; | |
fc96c615 BJ |
66 | { |
67 | register m; | |
68 | ||
69 | m = mode; | |
fdd90ee3 | 70 | if (m == IWRITE) { |
6459ebe0 | 71 | if (ip->i_fs->fs_ronly != 0) { |
fc96c615 | 72 | u.u_error = EROFS; |
fdd90ee3 | 73 | return (1); |
fc96c615 BJ |
74 | } |
75 | if (ip->i_flag&ITEXT) /* try to free text */ | |
76 | xrele(ip); | |
fdd90ee3 | 77 | if (ip->i_flag & ITEXT) { |
fc96c615 | 78 | u.u_error = ETXTBSY; |
fdd90ee3 | 79 | return (1); |
fc96c615 BJ |
80 | } |
81 | } | |
fdd90ee3 BJ |
82 | if (u.u_uid == 0) |
83 | return (0); | |
84 | if (u.u_uid != ip->i_uid) { | |
fc96c615 | 85 | m >>= 3; |
4f4caf05 BJ |
86 | if (ip->i_gid >= NGRPS || |
87 | (u.u_grps[ip->i_gid/(sizeof(int)*8)] & | |
88 | (1 << ip->i_gid%(sizeof(int)*8)) == 0)) | |
fc96c615 BJ |
89 | m >>= 3; |
90 | } | |
fdd90ee3 BJ |
91 | if ((ip->i_mode&m) != 0) |
92 | return (0); | |
fc96c615 | 93 | u.u_error = EACCES; |
fdd90ee3 | 94 | return (1); |
fc96c615 BJ |
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 * | |
5485e062 BJ |
106 | owner(follow) |
107 | int follow; | |
fc96c615 BJ |
108 | { |
109 | register struct inode *ip; | |
110 | ||
5485e062 | 111 | ip = namei(uchar, 0, follow); |
fdd90ee3 BJ |
112 | if (ip == NULL) |
113 | return (NULL); | |
114 | if (u.u_uid == ip->i_uid) | |
115 | return (ip); | |
116 | if (suser()) | |
117 | return (ip); | |
fc96c615 | 118 | iput(ip); |
fdd90ee3 | 119 | return (NULL); |
fc96c615 BJ |
120 | } |
121 | ||
122 | /* | |
123 | * Test if the current user is the | |
124 | * super user. | |
125 | */ | |
126 | suser() | |
127 | { | |
128 | ||
fdd90ee3 | 129 | if (u.u_uid == 0) { |
fc96c615 | 130 | u.u_acflag |= ASU; |
fdd90ee3 | 131 | return (1); |
fc96c615 BJ |
132 | } |
133 | u.u_error = EPERM; | |
fdd90ee3 | 134 | return (0); |
fc96c615 | 135 | } |