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"
26 #include "afs/sysincludes.h" /* Standard vendor system headers */
27 #include "afsincludes.h" /* Afs-based standard headers */
28 #include "afs/afs_stats.h" /* statistics */
29 #include "afs/afs_cbqueue.h"
30 #include "afs/nfsclient.h"
31 #include "afs/afs_osidnlc.h"
34 #define ANONYMOUSID 32766 /* make sure this is same as in ptserver.h */
40 /* access bits to turn off for various owner Unix mode values */
41 static char fileModeMap[8] = {
42 PRSFS_READ | PRSFS_WRITE,
43 PRSFS_READ | PRSFS_WRITE,
52 /* avc must be held. Returns bit map of mode bits. Ignores file mode bits */
54 afs_GetAccessBits(register struct vcache *avc, register afs_int32 arights,
55 register struct vrequest *areq)
57 AFS_STATCNT(afs_GetAccessBits);
58 /* see if anyuser has the required access bits */
59 if ((arights & avc->f.anyAccess) == arights) {
63 /* look in per-pag cache */
64 if (avc->Access) { /* not beautiful, but Sun's cc will tolerate it */
67 ac = afs_FindAxs(avc->Access, areq->uid);
69 return (arights & ac->axess);
73 if (!(avc->f.states & CForeign)) {
74 /* If there aren't any bits cached for this user (but the vnode
75 * _is_ cached, obviously), make sure this user has valid tokens
76 * before bothering with the RPC. */
78 tu = afs_FindUser(areq->uid, avc->f.fid.Cell, READ_LOCK);
80 return (arights & avc->f.anyAccess);
82 if ((tu->vid == UNDEFVID) || !(tu->states & UHasTokens)
83 || (tu->states & UTokensBad)) {
84 afs_PutUser(tu, READ_LOCK);
85 return (arights & avc->f.anyAccess);
87 afs_PutUser(tu, READ_LOCK);
91 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
92 /* If we get this far, we have to ask the network. But we can't, so
93 * they're out of luck... */
96 { /* Ok, user has valid tokens, go ask the server. */
97 struct AFSFetchStatus OutStatus;
100 code = afs_FetchStatus(avc, &avc->f.fid, areq, &OutStatus);
101 return (code ? 0 : OutStatus.CallerAccess & arights);
106 /* the new access ok function. AVC must be held but not locked. if avc is a
107 * file, its parent need not be held, and should not be locked. */
110 afs_AccessOK(struct vcache *avc, afs_int32 arights, struct vrequest *areq,
111 afs_int32 check_mode_bits)
113 register struct vcache *tvc;
114 struct VenusFid dirFid;
115 register afs_int32 mask;
117 register afs_int32 fileBits;
119 AFS_STATCNT(afs_AccessOK);
121 if ((vType(avc) == VDIR) || (avc->f.states & CForeign)) {
122 /* rights are just those from acl */
123 if (afs_InReadDir(avc)) {
124 /* if we are already in readdir, then they may have read and
125 * lookup, and nothing else, and nevermind the real ACL.
126 * Otherwise we might end up with problems trying to call
127 * FetchStatus on the vnode readdir is working on, and that
128 * would be a real mess.
130 dirBits = PRSFS_LOOKUP | PRSFS_READ;
131 return (arights == (dirBits & arights));
133 return (arights == afs_GetAccessBits(avc, arights, areq));
135 /* some rights come from dir and some from file. Specifically, you
136 * have "a" rights to a file if you are its owner, which comes
137 * back as "a" rights to the file. You have other rights just
138 * from dir, but all are restricted by the file mode bit. Now,
139 * if you have I and A rights to a file, we throw in R and W
140 * rights for free. These rights will then be restricted by
141 * the access mask. */
143 if (avc->f.parent.vnode) {
144 dirFid.Cell = avc->f.fid.Cell;
145 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
146 dirFid.Fid.Vnode = avc->f.parent.vnode;
147 dirFid.Fid.Unique = avc->f.parent.unique;
148 /* Avoid this GetVCache call */
149 tvc = afs_GetVCache(&dirFid, areq, NULL, NULL);
151 dirBits = afs_GetAccessBits(tvc, arights, areq);
155 dirBits = 0xffffffff; /* assume OK; this is a race condition */
156 if (arights & PRSFS_ADMINISTER)
157 fileBits = afs_GetAccessBits(avc, arights, areq);
159 fileBits = 0; /* don't make call if results don't matter */
161 /* compute basic rights in fileBits, taking A from file bits */
163 (fileBits & PRSFS_ADMINISTER) | (dirBits & ~PRSFS_ADMINISTER);
165 /* for files, throw in R and W if have I and A (owner). This makes
166 * insert-only dirs work properly */
167 if (vType(avc) != VDIR
168 && (fileBits & (PRSFS_ADMINISTER | PRSFS_INSERT)) ==
169 (PRSFS_ADMINISTER | PRSFS_INSERT))
170 fileBits |= (PRSFS_READ | PRSFS_WRITE);
172 if (check_mode_bits & CHECK_MODE_BITS) {
173 /* owner mode bits are further restrictions on the access mode
174 * The mode bits are mapped to protection bits through the
175 * fileModeMap. If CMB_ALLOW_EXEC_AS_READ is set, it's from the
176 * NFS translator and we don't know if it's a read or execute
177 * on the NFS client, but both need to read the data.
179 mask = (avc->f.m.Mode & 0700) >> 6; /* file restrictions to use */
180 fileBits &= ~fileModeMap[mask];
181 if (check_mode_bits & CMB_ALLOW_EXEC_AS_READ) {
182 if (avc->f.m.Mode & 0100)
183 fileBits |= PRSFS_READ;
186 return ((fileBits & arights) == arights); /* true if all rights bits are on */
191 #if defined(AFS_SUN5_ENV) || (defined(AFS_SGI_ENV) && !defined(AFS_SGI65_ENV))
193 afs_access(OSI_VC_DECL(avc), register afs_int32 amode, int flags,
197 afs_access(OSI_VC_DECL(avc), register afs_int32 amode,
201 register afs_int32 code;
202 struct vrequest treq;
203 struct afs_fakestat_state fakestate;
206 AFS_STATCNT(afs_access);
207 afs_Trace3(afs_iclSetp, CM_TRACE_ACCESS, ICL_TYPE_POINTER, avc,
208 ICL_TYPE_INT32, amode, ICL_TYPE_OFFSET,
209 ICL_HANDLE_OFFSET(avc->f.m.Length));
210 afs_InitFakeStat(&fakestate);
211 if ((code = afs_InitReq(&treq, acred)))
216 if (afs_fakestat_enable && avc->mvstat == 1) {
217 code = afs_TryEvalFakeStat(&avc, &fakestate, &treq);
218 if (code == 0 && avc->mvstat == 1) {
219 afs_PutFakeStat(&fakestate);
224 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
228 afs_PutFakeStat(&fakestate);
233 if (vType(avc) != VDIR || !afs_InReadDir(avc)) {
234 code = afs_VerifyVCache(avc, &treq);
236 afs_PutFakeStat(&fakestate);
238 code = afs_CheckCode(code, &treq, 16);
243 /* if we're looking for write access and we have a read-only file system, report it */
244 if ((amode & VWRITE) && (avc->f.states & CRO)) {
245 afs_PutFakeStat(&fakestate);
250 /* If we're looking for write access, and we're disconnected without logging, forget it */
251 if ((amode & VWRITE) && (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)) {
252 afs_PutFakeStat(&fakestate);
254 printf("Network is down in afs_vnop_access\n");
258 code = 1; /* Default from here on in is access ok. */
259 if (avc->f.states & CForeign) {
260 /* In the dfs xlator the EXEC bit is mapped to LOOKUP */
262 code = afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
263 if (code && (amode & VWRITE)) {
264 code = afs_AccessOK(avc, PRSFS_WRITE, &treq, CHECK_MODE_BITS);
265 if (code && (vType(avc) == VDIR)) {
268 afs_AccessOK(avc, PRSFS_INSERT, &treq,
272 afs_AccessOK(avc, PRSFS_DELETE, &treq,
276 if (code && (amode & VREAD))
277 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
279 if (vType(avc) == VDIR) {
282 afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
283 if (code && (amode & VWRITE)) {
285 afs_AccessOK(avc, PRSFS_INSERT, &treq, CHECK_MODE_BITS);
288 afs_AccessOK(avc, PRSFS_DELETE, &treq,
291 if (code && (amode & VREAD))
293 afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
296 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
300 * The nfs server in read operations for non-owner of a file
301 * will also check the access with the VEXEC (along with VREAD)
302 * because for them exec is the same as read over the net because of
303 * demand loading. But this means if the mode bit is '-rw' the call
304 * will fail below; so for this particular case where both modes are
305 * specified (only in rfs_read so far) and from the xlator requests
308 if (!((amode & VREAD) && AFS_NFSXLATORREQ(acred)))
310 if ((avc->f.m.Mode & 0100) == 0)
312 } else if (avc->f.m.Mode & 0100)
315 if (code && (amode & VWRITE)) {
316 code = afs_AccessOK(avc, PRSFS_WRITE, &treq, CHECK_MODE_BITS);
318 /* The above call fails when the NFS translator tries to copy
319 ** a file with r--r--r-- permissions into a directory which
320 ** has system:anyuser acl. This is because the destination file
321 ** file is first created with r--r--r-- permissions through an
322 ** unauthenticated connectin. hence, the above afs_AccessOK
323 ** call returns failure. hence, we retry without any file
324 ** mode bit checking */
325 if (!code && AFS_NFSXLATORREQ(acred)
326 && avc->f.m.Owner == ANONYMOUSID)
328 afs_AccessOK(avc, PRSFS_WRITE, &treq,
329 DONT_CHECK_MODE_BITS);
331 if (code && (amode & VREAD))
332 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
335 afs_PutFakeStat(&fakestate);
340 return 0; /* if access is ok */
342 code = afs_CheckCode(EACCES, &treq, 17); /* failure code */
347 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
350 * This function is just an interface to afs_GetAccessBits
353 afs_getRights(OSI_VC_DECL(avc), register afs_int32 arights,
356 register afs_int32 code;
357 struct vrequest treq;
360 if ((code = afs_InitReq(&treq, acred)))
363 code = afs_VerifyVCache(avc, &treq);
365 code = afs_CheckCode(code, &treq, 16);
369 return afs_GetAccessBits(avc, arights, &treq);
371 #endif /* defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS) */