Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / afsd / cm_access.c
1 /* 
2  * Copyright (C) 1998, 1989 Transarc Corporation - All rights reserved
3  *
4  * (C) COPYRIGHT IBM CORPORATION 1987, 1988
5  * LICENSED MATERIALS - PROPERTY OF IBM
6  *
7  *
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 /* called with scp write-locked, check to see if we have the ACL info we need
24  * and can get it w/o blocking for any locks.
25  *
26  * Never drops the scp lock, but may fail if the access control info comes from
27  * the parent directory, and the parent's scache entry can't be found, or it
28  * can't be locked.  Thus, this must always be called in a while loop to stabilize
29  * things, since we can always lose the race condition getting to the parent vnode.
30  */
31 int cm_HaveAccessRights(struct cm_scache *scp, struct cm_user *up, long rights,
32         long *outRightsp)
33 {
34         cm_scache_t *aclScp;
35         long code;
36         cm_fid_t tfid;
37         int didLock;
38         long trights;
39         
40         didLock = 0;
41         if (scp->fileType == CM_SCACHETYPE_DIRECTORY) {
42                 aclScp = scp;
43                 cm_HoldSCache(scp);
44         }
45         else {
46                 tfid.cell = scp->fid.cell;
47                 tfid.volume = scp->fid.volume;
48                 tfid.vnode = scp->parentVnode;
49                 tfid.unique = scp->parentUnique;
50                 aclScp = cm_FindSCache(&tfid);
51                 if (!aclScp) return 0;
52                 if (aclScp != scp) {
53                         code = lock_TryMutex(&aclScp->mx);
54                         if (code == 0) {
55                                 /* can't get lock safely and easily */
56                                 cm_ReleaseSCache(aclScp);
57                                 return 0;
58                         }
59
60                         /* check that we have a callback, too */
61                         if (!cm_HaveCallback(aclScp)) {
62                                 /* can't use it */
63                                 lock_ReleaseMutex(&aclScp->mx);
64                                 cm_ReleaseSCache(aclScp);
65                                 return 0;
66                         }
67                         didLock = 1;
68                 }
69         }
70
71         lock_AssertMutex(&aclScp->mx);
72         
73         /* now if rights is a subset of the public rights, we're done.
74          * Otherwise, if we an explicit acl entry, we're also in good shape,
75          * and can definitively answer.
76          */
77         if ((~aclScp->anyAccess & rights) == 0) {
78                 *outRightsp = rights;
79         }
80         else {
81                 /* we have to check the specific rights info */
82                 code = cm_FindACLCache(aclScp, up, &trights);
83                 if (code) {
84                         code = 0;
85                         goto done;
86                 }
87                 *outRightsp = trights;
88         }
89
90         /* check mode bits */
91         if (!(scp->unixModeBits & 0400))
92                 *outRightsp &= ~PRSFS_READ;
93         if (!(scp->unixModeBits & 0200))
94                 *outRightsp &= ~PRSFS_WRITE;
95
96         code = 1;
97         /* fall through */
98         
99 done:
100         if (didLock) lock_ReleaseMutex(&aclScp->mx);
101         cm_ReleaseSCache(aclScp);
102         return code;
103 }
104
105 /* called with locked scp; ensures that we have an ACL cache entry for the
106  * user specified by the parameter "up."
107  * In pathological race conditions, this function may return success without
108  * having loaded the entry properly (due to a racing callback revoke), so this
109  * function must also be called in a while loop to make sure that we eventually
110  * succeed.
111  */
112 long cm_GetAccessRights(struct cm_scache *scp, struct cm_user *up,
113         struct cm_req *reqp)
114 {
115         long code;
116         cm_fid_t tfid;
117         cm_scache_t *aclScp;
118
119         /* pretty easy: just force a pass through the fetch status code */
120         
121         osi_Log2(afsd_logp, "GetAccess scp %x user %x", scp, up);
122
123         /* first, start by finding out whether we have a directory or something
124          * else, so we can find what object's ACL we need.
125          */
126         code = cm_SyncOp(scp, NULL, up, reqp, 0, CM_SCACHESYNC_GETSTATUS
127                         | CM_SCACHESYNC_NEEDCALLBACK);
128                         
129         if (code) return code;
130         
131         if (scp->fileType != CM_SCACHETYPE_DIRECTORY) {
132                 /* not a dir, use parent dir's acl */
133                 tfid.cell = scp->fid.cell;
134                 tfid.volume = scp->fid.volume;
135                 tfid.vnode = scp->parentVnode;
136                 tfid.unique = scp->parentUnique;
137                 lock_ReleaseMutex(&scp->mx);
138                 code = cm_GetSCache(&tfid, &aclScp, up, reqp);
139                 if (code) {
140                         lock_ObtainMutex(&scp->mx);
141                         return code;
142                 }
143                 
144                 osi_Log1(afsd_logp, "GetAccess parent %x", aclScp);
145                 lock_ObtainMutex(&aclScp->mx);
146                 code = cm_GetCallback(aclScp, up, reqp, 1);
147                 lock_ReleaseMutex(&aclScp->mx);
148                 cm_ReleaseSCache(aclScp);
149                 lock_ObtainMutex(&scp->mx);
150         }
151         else {
152                 code = cm_GetCallback(scp, up, reqp, 1);
153         }
154
155         return code;
156 }