afd6c9c64439e0dd9db123d52cdac8438a4d4699
[openafs.git] / src / afs / VNOPS / afs_vnop_access.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * afs_vnop_access.c - access vop ccess mode bit support for vnode operations.
12  *
13  * Implements:
14  * afs_GetAccessBits
15  * afs_AccessOK
16  * afs_access
17  *
18  * Local:
19  * fileModeMap (table)
20  */
21
22 #include <afsconfig.h>
23 #include "afs/param.h"
24
25 RCSID
26     ("$Header$");
27
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"
34
35 #ifndef ANONYMOUSID
36 #define ANONYMOUSID     32766   /* make sure this is same as in ptserver.h */
37 #endif
38
39
40
41
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,
46     PRSFS_READ,
47     PRSFS_READ,
48     PRSFS_WRITE,
49     PRSFS_WRITE,
50     0,
51     0
52 };
53
54 /* avc must be held.  Returns bit map of mode bits.  Ignores file mode bits */
55 afs_int32
56 afs_GetAccessBits(register struct vcache *avc, register afs_int32 arights,
57                   register struct vrequest *areq)
58 {
59     AFS_STATCNT(afs_GetAccessBits);
60     /* see if anyuser has the required access bits */
61     if ((arights & avc->anyAccess) == arights) {
62         return arights;
63     }
64
65     /* look in per-pag cache */
66     if (avc->Access) {          /* not beautiful, but Sun's cc will tolerate it */
67         struct axscache *ac;
68
69         ac = afs_FindAxs(avc->Access, areq->uid);
70         if (ac) {
71             return (arights & ac->axess);
72         }
73     }
74
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.  */
79         struct unixuser *tu;
80         extern struct unixuser *afs_FindUser();
81         tu = afs_FindUser(areq->uid, avc->fid.Cell, READ_LOCK);
82         if (!tu) {
83             return (arights & avc->anyAccess);
84         }
85         if ((tu->vid == UNDEFVID) || !(tu->states & UHasTokens)
86             || (tu->states & UTokensBad)) {
87             afs_PutUser(tu, READ_LOCK);
88             return (arights & avc->anyAccess);
89         } else {
90             afs_PutUser(tu, READ_LOCK);
91         }
92     }
93
94     {                           /* Ok, user has valid tokens, go ask the server. */
95         struct AFSFetchStatus OutStatus;
96         afs_int32 code;
97
98         code = afs_FetchStatus(avc, &avc->fid, areq, &OutStatus);
99         return (code ? 0 : OutStatus.CallerAccess & arights);
100     }
101 }
102
103
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. */
106
107 int
108 afs_AccessOK(struct vcache *avc, afs_int32 arights, struct vrequest *areq,
109              afs_int32 check_mode_bits)
110 {
111     register struct vcache *tvc;
112     struct VenusFid dirFid;
113     register afs_int32 mask;
114     afs_int32 dirBits;
115     register afs_int32 fileBits;
116
117     AFS_STATCNT(afs_AccessOK);
118
119     if ((vType(avc) == VDIR) || (avc->states & CForeign)) {
120         /* rights are just those from acl */
121         return (arights == afs_GetAccessBits(avc, arights, areq));
122     } else {
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. */
130         dirBits = 0;
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);
138             if (tvc) {
139                 dirBits = afs_GetAccessBits(tvc, arights, areq);
140                 afs_PutVCache(tvc);
141             }
142         } else
143             dirBits = 0xffffffff;       /* assume OK; this is a race condition */
144         if (arights & PRSFS_ADMINISTER)
145             fileBits = afs_GetAccessBits(avc, arights, areq);
146         else
147             fileBits = 0;       /* don't make call if results don't matter */
148
149         /* compute basic rights in fileBits, taking A from file bits */
150         fileBits =
151             (fileBits & PRSFS_ADMINISTER) | (dirBits & ~PRSFS_ADMINISTER);
152
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);
159
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.
166              */
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;
172             }
173         }
174         return ((fileBits & arights) == arights);       /* true if all rights bits are on */
175     }
176 }
177
178
179 #if defined(AFS_SUN5_ENV) || (defined(AFS_SGI_ENV) && !defined(AFS_SGI65_ENV))
180 int
181 afs_access(OSI_VC_DECL(avc), register afs_int32 amode, int flags,
182            struct AFS_UCRED *acred)
183 #else
184 int
185 afs_access(OSI_VC_DECL(avc), register afs_int32 amode,
186            struct AFS_UCRED *acred)
187 #endif
188 {
189     register afs_int32 code;
190     struct vrequest treq;
191     struct afs_fakestat_state fakestate;
192     OSI_VC_CONVERT(avc)
193
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)))
200         return code;
201
202     code = afs_EvalFakeStat(&avc, &fakestate, &treq);
203     if (code) {
204         afs_PutFakeStat(&fakestate);
205         return code;
206     }
207
208     code = afs_VerifyVCache(avc, &treq);
209     if (code) {
210         afs_PutFakeStat(&fakestate);
211         code = afs_CheckCode(code, &treq, 16);
212         return code;
213     }
214
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);
218         return EROFS;
219     }
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 */
223         if (amode & VEXEC)
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)) {
228                 if (code)
229                     code =
230                         afs_AccessOK(avc, PRSFS_INSERT, &treq,
231                                      CHECK_MODE_BITS);
232                 if (!code)
233                     code =
234                         afs_AccessOK(avc, PRSFS_DELETE, &treq,
235                                      CHECK_MODE_BITS);
236             }
237         }
238         if (code && (amode & VREAD))
239             code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
240     } else {
241         if (vType(avc) == VDIR) {
242             if (amode & VEXEC)
243                 code =
244                     afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
245             if (code && (amode & VWRITE)) {
246                 code =
247                     afs_AccessOK(avc, PRSFS_INSERT, &treq, CHECK_MODE_BITS);
248                 if (!code)
249                     code =
250                         afs_AccessOK(avc, PRSFS_DELETE, &treq,
251                                      CHECK_MODE_BITS);
252             }
253             if (code && (amode & VREAD))
254                 code =
255                     afs_AccessOK(avc, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS);
256         } else {
257             if (amode & VEXEC) {
258                 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
259                 if (code) {
260 #ifdef  AFS_OSF_ENV
261                     /*
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
268                      * we return succes.
269                      */
270                     if (!((amode & VREAD) && AFS_NFSXLATORREQ(acred)))
271 #endif
272                         if ((avc->m.Mode & 0100) == 0)
273                             code = 0;
274                 } else if (avc->m.Mode & 0100)
275                     code = 1;
276             }
277             if (code && (amode & VWRITE)) {
278                 code = afs_AccessOK(avc, PRSFS_WRITE, &treq, CHECK_MODE_BITS);
279
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)
289                     code =
290                         afs_AccessOK(avc, PRSFS_WRITE, &treq,
291                                      DONT_CHECK_MODE_BITS);
292             }
293             if (code && (amode & VREAD))
294                 code = afs_AccessOK(avc, PRSFS_READ, &treq, CHECK_MODE_BITS);
295         }
296     }
297     afs_PutFakeStat(&fakestate);
298     if (code) {
299         return 0;               /* if access is ok */
300     } else {
301         code = afs_CheckCode(EACCES, &treq, 17);        /* failure code */
302         return code;
303     }
304 }
305
306 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
307 /*
308  * afs_getRights
309  * This function is just an interface to afs_GetAccessBits
310  */
311 int
312 afs_getRights(OSI_VC_DECL(avc), register afs_int32 arights,
313               struct AFS_UCRED *acred)
314 {
315     register afs_int32 code;
316     struct vrequest treq;
317     OSI_VC_CONVERT(avc)
318
319         if (code = afs_InitReq(&treq, acred))
320         return code;
321
322     code = afs_VerifyVCache(avc, &treq);
323     if (code) {
324         code = afs_CheckCode(code, &treq, 16);
325         return code;
326     }
327
328     return afs_GetAccessBits(avc, arights, &treq);
329 }
330 #endif /* defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS) */