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