Windows: split cm_buf_t.flags field to ensure proper locking
[openafs.git] / src / WINNT / afsd / cm_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 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <winsock2.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <nb30.h>
18 #include <osi.h>
19
20 #include "afsd.h"
21
22 int cm_deleteReadOnly = 0;
23 int cm_accessPerFileCheck = 0;
24
25 /* called with scp write-locked, check to see if we have the ACL info we need
26  * and can get it w/o blocking for any locks.
27  *
28  * Never drops the scp lock, but may fail if the access control info comes from
29  * the parent directory, and the parent's scache entry can't be found, or it
30  * can't be locked.  Thus, this must always be called in a while loop to stabilize
31  * things, since we can always lose the race condition getting to the parent vnode.
32  */
33 int cm_HaveAccessRights(struct cm_scache *scp, struct cm_user *userp, afs_uint32 rights,
34                         afs_uint32 *outRightsp)
35 {
36     cm_scache_t *aclScp;
37     long code;
38     cm_fid_t tfid;
39     int didLock;
40     long trights;
41     int release = 0;    /* Used to avoid a call to cm_HoldSCache in the directory case */
42
43     didLock = 0;
44     if (scp->fileType == CM_SCACHETYPE_DIRECTORY || cm_accessPerFileCheck) {
45         aclScp = scp;   /* not held, not released */
46     } else {
47         cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
48         aclScp = cm_FindSCache(&tfid);
49         if (!aclScp) 
50             return 0;
51         if (aclScp != scp) {
52             if (aclScp->fid.vnode < scp->fid.vnode)
53                 lock_ReleaseWrite(&scp->rw);
54             lock_ObtainRead(&aclScp->rw);
55             if (aclScp->fid.vnode < scp->fid.vnode)
56                 lock_ObtainWrite(&scp->rw);
57
58             /* check that we have a callback, too */
59             if (!cm_HaveCallback(aclScp)) {
60                 /* can't use it */
61                 lock_ReleaseRead(&aclScp->rw);
62                 cm_ReleaseSCache(aclScp);
63                 return 0;
64             }
65             didLock = 1;
66         }
67         release = 1;
68     }
69
70     lock_AssertAny(&aclScp->rw);
71         
72     /* now if rights is a subset of the public rights, we're done.
73      * Otherwise, if we an explicit acl entry, we're also in good shape,
74      * and can definitively answer.
75      */
76 #ifdef AFS_FREELANCE_CLIENT
77     if (cm_freelanceEnabled && aclScp == cm_data.rootSCachep)
78     {
79         *outRightsp = aclScp->anyAccess;
80     } else
81 #endif
82     if ((~aclScp->anyAccess & rights) == 0) {
83         *outRightsp = rights;
84     } else {
85         /* we have to check the specific rights info */
86         code = cm_FindACLCache(aclScp, userp, &trights);
87         if (code) {
88             code = 0;
89             goto done;
90         }
91         *outRightsp = trights;
92     }
93
94     if (scp->fileType > 0 && scp->fileType != CM_SCACHETYPE_DIRECTORY) {
95         /* check mode bits */
96         if ((scp->unixModeBits & 0400) == 0) {
97             osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing READ scp 0x%p unix 0x%x", 
98                       scp, scp->unixModeBits);
99             *outRightsp &= ~PRSFS_READ;
100         }
101         if ((scp->unixModeBits & 0200) == 0 && (rights != (PRSFS_WRITE | PRSFS_LOCK))) {
102             osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing WRITE scp 0x%p unix 0%o", 
103                       scp, scp->unixModeBits);
104             *outRightsp &= ~PRSFS_WRITE;
105         }
106         if ((scp->unixModeBits & 0200) == 0 && !cm_deleteReadOnly) {
107             osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing DELETE scp 0x%p unix 0%o", 
108                       scp, scp->unixModeBits);
109             *outRightsp &= ~PRSFS_DELETE;
110         }
111     }
112
113     /* if the user can insert, we must assume they can read/write as well
114      * because we do not have the ability to determine if the current user
115      * is the owner of the file. We will have to make the call to the
116      * file server and let the file server tell us if the request should
117      * be denied.
118      */
119     if ((*outRightsp & PRSFS_INSERT) && (scp->creator == userp))
120         *outRightsp |= PRSFS_READ | PRSFS_WRITE;
121
122     /* if the user can obtain a write-lock, read-locks are implied */
123     if (*outRightsp & PRSFS_WRITE)
124         *outRightsp |= PRSFS_LOCK;
125
126     code = 1;
127     /* fall through */
128
129   done:
130     if (didLock) 
131         lock_ReleaseRead(&aclScp->rw);
132     if (release)
133         cm_ReleaseSCache(aclScp);
134     return code;
135 }
136
137 /* called with locked scp; ensures that we have an ACL cache entry for the
138  * user specified by the parameter "userp."
139  * In pathological race conditions, this function may return success without
140  * having loaded the entry properly (due to a racing callback revoke), so this
141  * function must also be called in a while loop to make sure that we eventually
142  * succeed.
143  */
144 long cm_GetAccessRights(struct cm_scache *scp, struct cm_user *userp,
145                         struct cm_req *reqp)
146 {
147     long code;
148     cm_fid_t tfid;
149     cm_scache_t *aclScp = NULL;
150     int got_cb = 0;
151
152     /* pretty easy: just force a pass through the fetch status code */
153         
154     osi_Log2(afsd_logp, "GetAccessRights scp 0x%p user 0x%p", scp, userp);
155
156     /* first, start by finding out whether we have a directory or something
157      * else, so we can find what object's ACL we need.
158      */
159     if (scp->fileType == CM_SCACHETYPE_DIRECTORY || cm_accessPerFileCheck) {
160         code = cm_SyncOp(scp, NULL, userp, reqp, 0,
161                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_FORCECB);
162         if (!code) 
163             cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
164     else
165         osi_Log3(afsd_logp, "GetAccessRights syncop failure scp %x user %x code %x", scp, userp, code);
166     } else {
167         /* not a dir, use parent dir's acl */
168         cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
169         lock_ReleaseWrite(&scp->rw);
170         code = cm_GetSCache(&tfid, &aclScp, userp, reqp);
171         if (code) {
172             lock_ObtainWrite(&scp->rw);
173             goto _done;
174         }       
175                 
176         osi_Log2(afsd_logp, "GetAccessRights parent scp %x user %x", aclScp, userp);
177         lock_ObtainWrite(&aclScp->rw);
178         code = cm_SyncOp(aclScp, NULL, userp, reqp, 0,
179                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_FORCECB);
180         if (!code)
181             cm_SyncOpDone(aclScp, NULL, 
182                           CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
183         else 
184             osi_Log3(afsd_logp, "GetAccessRights parent syncop failure scp %x user %x code %x", aclScp, userp, code);
185         lock_ReleaseWrite(&aclScp->rw);
186         cm_ReleaseSCache(aclScp);
187         lock_ObtainWrite(&scp->rw);
188     }
189
190   _done:
191     return code;
192 }