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