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