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