2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * afs_vnop_access.c - access vop ccess mode bit support for vnode operations.
22 #include <afsconfig.h>
23 #include "afs/param.h"
28 #include "afs/sysincludes.h" /* Standard vendor system headers */
29 #include "afsincludes.h" /* Afs-based standard headers */
30 #include "afs/afs_stats.h" /* statistics */
31 #include "afs/afs_cbqueue.h"
32 #include "afs/nfsclient.h"
33 #include "afs/afs_osidnlc.h"
36 #define ANONYMOUSID 32766 /* make sure this is same as in ptserver.h */
42 /* access bits to turn off for various owner Unix mode values */
43 static char fileModeMap[8] = {
44 PRSFS_READ | PRSFS_WRITE,
45 PRSFS_READ | PRSFS_WRITE,
54 /* avc must be held. Returns bit map of mode bits. Ignores file mode bits */
56 afs_GetAccessBits(register struct vcache *avc, register afs_int32 arights,
57 register struct vrequest *areq)
59 AFS_STATCNT(afs_GetAccessBits);
60 /* see if anyuser has the required access bits */
61 if ((arights & avc->anyAccess) == arights) {
65 /* look in per-pag cache */
66 if (avc->Access) { /* not beautiful, but Sun's cc will tolerate it */
69 ac = afs_FindAxs(avc->Access, areq->uid);
71 return (arights & ac->axess);
75 if (!(avc->states & CForeign)) {
76 /* If there aren't any bits cached for this user (but the vnode
77 * _is_ cached, obviously), make sure this user has valid tokens
78 * before bothering with the RPC. */
80 extern struct unixuser *afs_FindUser();
81 tu = afs_FindUser(areq->uid, avc->fid.Cell, READ_LOCK);
83 return (arights & avc->anyAccess);
85 if ((tu->vid == UNDEFVID) || !(tu->states & UHasTokens)
86 || (tu->states & UTokensBad)) {
87 afs_PutUser(tu, READ_LOCK);
88 return (arights & avc->anyAccess);
90 afs_PutUser(tu, READ_LOCK);
94 { /* Ok, user has valid tokens, go ask the server. */
95 struct AFSFetchStatus OutStatus;
98 code = afs_FetchStatus(avc, &avc->fid, areq, &OutStatus);
99 return (code ? 0 : OutStatus.CallerAccess & arights);
104 /* the new access ok function. AVC must be held but not locked. if avc is a
105 * file, its parent need not be held, and should not be locked. */
108 afs_AccessOK(struct vcache *avc, afs_int32 arights, struct vrequest *areq,
109 afs_int32 check_mode_bits)
111 register struct vcache *tvc;
112 struct VenusFid dirFid;
113 register afs_int32 mask;
115 register afs_int32 fileBits;
117 AFS_STATCNT(afs_AccessOK);
119 if ((vType(avc) == VDIR) || (avc->states & CForeign)) {
120 /* rights are just those from acl */
121 return (arights == afs_GetAccessBits(avc, arights, areq));
123 /* some rights come from dir and some from file. Specifically, you
124 * have "a" rights to a file if you are its owner, which comes
125 * back as "a" rights to the file. You have other rights just
126 * from dir, but all are restricted by the file mode bit. Now,
127 * if you have I and A rights to a file, we throw in R and W
128 * rights for free. These rights will then be restricted by
129 * the access mask. */
131 if (avc->parentVnode) {
132 dirFid.Cell = avc->fid.Cell;
133 dirFid.Fid.Volume = avc->fid.Fid.Volume;
134 dirFid.Fid.Vnode = avc->parentVnode;
135 dirFid.Fid.Unique = avc->parentUnique;
136 /* Avoid this GetVCache call */
137 tvc = afs_GetVCache(&dirFid, areq, NULL, NULL);
139 dirBits = afs_GetAccessBits(tvc, arights, areq);
143 dirBits = 0xffffffff; /* assume OK; this is a race condition */
144 if (arights & PRSFS_ADMINISTER)
145 fileBits = afs_GetAccessBits(avc, arights, areq);
147 fileBits = 0; /* don't make call if results don't matter */
149 /* compute basic rights in fileBits, taking A from file bits */
151 (fileBits & PRSFS_ADMINISTER) | (dirBits & ~PRSFS_ADMINISTER);
153 /* for files, throw in R and W if have I and A (owner). This makes
154 * insert-only dirs work properly */
155 if (vType(avc) != VDIR
156 && (fileBits & (PRSFS_ADMINISTER | PRSFS_INSERT)) ==
157 (PRSFS_ADMINISTER | PRSFS_INSERT))
158 fileBits |= (PRSFS_READ | PRSFS_WRITE);
160 if (check_mode_bits & CHECK_MODE_BITS) {
161 /* owner mode bits are further restrictions on the access mode
162 * The mode bits are mapped to protection bits through the
163 * fileModeMap. If CMB_ALLOW_EXEC_AS_READ is set, it's from the
164 * NFS translator and we don't know if it's a read or execute
165 * on the NFS client, but both need to read the data.
167 mask = (avc->m.Mode & 0700) >> 6; /* file restrictions to use */
168 fileBits &= ~fileModeMap[mask];
169 if (check_mode_bits & CMB_ALLOW_EXEC_AS_READ) {
170 if (avc->m.Mode & 0100)
171 fileBits |= PRSFS_READ;
174 return ((fileBits & arights) == arights); /* true if all rights bits are on */
179 #if defined(AFS_SUN5_ENV) || (defined(AFS_SGI_ENV) && !defined(AFS_SGI65_ENV))
181 afs_access(OSI_VC_DECL(avc), register afs_int32 amode, int flags,
182 struct AFS_UCRED *acred)
185 afs_access(OSI_VC_DECL(avc), register afs_int32 amode,
186 struct AFS_UCRED *acred)
189 register afs_int32 code;
190 struct vrequest treq;
191 struct afs_fakestat_state fakestate;
194 AFS_STATCNT(afs_access);
195 afs_Trace3(afs_iclSetp, CM_TRACE_ACCESS, ICL_TYPE_POINTER, avc,
196 ICL_TYPE_INT32, amode, ICL_TYPE_OFFSET,
197 ICL_HANDLE_OFFSET(avc->m.Length));
198 afs_InitFakeStat(&fakestate);
199 if ((code = afs_InitReq(&treq, acred)))
202 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
204 afs_PutFakeStat(&fakestate);
208 code = afs_VerifyVCache(avc, &treq);
210 afs_PutFakeStat(&fakestate);
211 code = afs_CheckCode(code, &treq, 16);
215 /* if we're looking for write access and we have a read-only file system, report it */
216 if ((amode & VWRITE) && (avc->states & CRO)) {
217 afs_PutFakeStat(&fakestate);
220 code = 1; /* Default from here on in is access ok. */
221 if (avc->states & CForeign) {
222 /* In the dfs xlator the EXEC bit is mapped to LOOKUP */
224 code = afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
225 if (code && (amode & VWRITE)) {
226 code = afs_AccessOK(avc, PRSFS_WRITE, &treq, CHECK_MODE_BITS);
227 if (code && (vType(avc) == VDIR)) {
230 afs_AccessOK(avc, PRSFS_INSERT, &treq,
234 afs_AccessOK(avc, PRSFS_DELETE, &treq,
238 if (code && (amode & VREAD))
239 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
241 if (vType(avc) == VDIR) {
244 afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
245 if (code && (amode & VWRITE)) {
247 afs_AccessOK(avc, PRSFS_INSERT, &treq, CHECK_MODE_BITS);
250 afs_AccessOK(avc, PRSFS_DELETE, &treq,
253 if (code && (amode & VREAD))
255 afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
258 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
262 * The nfs server in read operations for non-owner of a file
263 * will also check the access with the VEXEC (along with VREAD)
264 * because for them exec is the same as read over the net because of
265 * demand loading. But this means if the mode bit is '-rw' the call
266 * will fail below; so for this particular case where both modes are
267 * specified (only in rfs_read so far) and from the xlator requests
270 if (!((amode & VREAD) && AFS_NFSXLATORREQ(acred)))
272 if ((avc->m.Mode & 0100) == 0)
274 } else if (avc->m.Mode & 0100)
277 if (code && (amode & VWRITE)) {
278 code = afs_AccessOK(avc, PRSFS_WRITE, &treq, CHECK_MODE_BITS);
280 /* The above call fails when the NFS translator tries to copy
281 ** a file with r--r--r-- permissions into a directory which
282 ** has system:anyuser acl. This is because the destination file
283 ** file is first created with r--r--r-- permissions through an
284 ** unauthenticated connectin. hence, the above afs_AccessOK
285 ** call returns failure. hence, we retry without any file
286 ** mode bit checking */
287 if (!code && AFS_NFSXLATORREQ(acred)
288 && avc->m.Owner == ANONYMOUSID)
290 afs_AccessOK(avc, PRSFS_WRITE, &treq,
291 DONT_CHECK_MODE_BITS);
293 if (code && (amode & VREAD))
294 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
297 afs_PutFakeStat(&fakestate);
299 return 0; /* if access is ok */
301 code = afs_CheckCode(EACCES, &treq, 17); /* failure code */
306 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
309 * This function is just an interface to afs_GetAccessBits
312 afs_getRights(OSI_VC_DECL(avc), register afs_int32 arights,
313 struct AFS_UCRED *acred)
315 register afs_int32 code;
316 struct vrequest treq;
319 if (code = afs_InitReq(&treq, acred))
322 code = afs_VerifyVCache(avc, &treq);
324 code = afs_CheckCode(code, &treq, 16);
328 return afs_GetAccessBits(avc, arights, &treq);
330 #endif /* defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS) */