2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
23 int cm_deleteReadOnly = 0;
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.
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.
33 int cm_HaveAccessRights(struct cm_scache *scp, struct cm_user *userp, afs_uint32 rights,
34 afs_uint32 *outRightsp)
43 if (scp->flags & CM_SCACHEFLAG_EACCESS) {
49 if (scp->fileType == CM_SCACHETYPE_DIRECTORY) {
53 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
54 aclScp = cm_FindSCache(&tfid);
58 code = lock_TryMutex(&aclScp->mx);
60 /* can't get lock safely and easily */
61 cm_ReleaseSCache(aclScp);
65 /* check that we have a callback, too */
66 if (!cm_HaveCallback(aclScp)) {
68 lock_ReleaseMutex(&aclScp->mx);
69 cm_ReleaseSCache(aclScp);
76 lock_AssertMutex(&aclScp->mx);
78 /* now if rights is a subset of the public rights, we're done.
79 * Otherwise, if we an explicit acl entry, we're also in good shape,
80 * and can definitively answer.
82 #ifdef AFS_FREELANCE_CLIENT
83 if (cm_freelanceEnabled && aclScp == cm_data.rootSCachep)
85 *outRightsp = aclScp->anyAccess;
88 if ((~aclScp->anyAccess & rights) == 0) {
91 /* we have to check the specific rights info */
92 code = cm_FindACLCache(aclScp, userp, &trights);
97 *outRightsp = trights;
100 if (scp->fileType > 0 && scp->fileType != CM_SCACHETYPE_DIRECTORY) {
101 /* check mode bits */
102 if ((scp->unixModeBits & 0400) == 0) {
103 osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing READ scp 0x%p unix 0x%x",
104 scp, scp->unixModeBits);
105 *outRightsp &= ~PRSFS_READ;
107 if ((scp->unixModeBits & 0200) == 0 && (rights != (PRSFS_WRITE | PRSFS_LOCK))) {
108 osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing WRITE scp 0x%p unix 0%o",
109 scp, scp->unixModeBits);
110 *outRightsp &= ~PRSFS_WRITE;
112 if ((scp->unixModeBits & 0200) == 0 && !cm_deleteReadOnly) {
113 osi_Log2(afsd_logp,"cm_HaveAccessRights UnixMode removing DELETE scp 0x%p unix 0%o",
114 scp, scp->unixModeBits);
115 *outRightsp &= ~PRSFS_DELETE;
119 /* if the user can insert, we must assume they can read/write as well
120 * because we do not have the ability to determine if the current user
121 * is the owner of the file. We will have to make the call to the
122 * file server and let the file server tell us if the request should
125 if ((*outRightsp & PRSFS_INSERT) && (scp->creator == userp))
126 *outRightsp |= PRSFS_READ | PRSFS_WRITE;
128 /* if the user can obtain a write-lock, read-locks are implied */
129 if (*outRightsp & PRSFS_WRITE)
130 *outRightsp |= PRSFS_LOCK;
137 lock_ReleaseMutex(&aclScp->mx);
138 cm_ReleaseSCache(aclScp);
142 /* called with locked scp; ensures that we have an ACL cache entry for the
143 * user specified by the parameter "userp."
144 * In pathological race conditions, this function may return success without
145 * having loaded the entry properly (due to a racing callback revoke), so this
146 * function must also be called in a while loop to make sure that we eventually
149 long cm_GetAccessRights(struct cm_scache *scp, struct cm_user *userp,
154 cm_scache_t *aclScp = NULL;
157 /* pretty easy: just force a pass through the fetch status code */
159 osi_Log2(afsd_logp, "GetAccess scp 0x%p user 0x%p", scp, userp);
161 /* first, start by finding out whether we have a directory or something
162 * else, so we can find what object's ACL we need.
164 if (scp->fileType == CM_SCACHETYPE_DIRECTORY ) {
165 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
166 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_FORCECB);
168 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
170 /* not a dir, use parent dir's acl */
171 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
172 lock_ReleaseMutex(&scp->mx);
173 code = cm_GetSCache(&tfid, &aclScp, userp, reqp);
175 lock_ObtainMutex(&scp->mx);
179 osi_Log2(afsd_logp, "GetAccess parent scp %x user %x", aclScp, userp);
180 lock_ObtainMutex(&aclScp->mx);
181 code = cm_SyncOp(aclScp, NULL, userp, reqp, 0,
182 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_FORCECB);
184 cm_SyncOpDone(aclScp, NULL,
185 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
186 lock_ReleaseMutex(&aclScp->mx);
187 cm_ReleaseSCache(aclScp);
188 lock_ObtainMutex(&scp->mx);