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