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
13 #include <afsconfig.h>
14 #include "afs/param.h"
18 #include "afs/sysincludes.h" /* Standard vendor system headers */
21 #if !defined(AFS_LINUX20_ENV)
24 #include <netinet/in.h>
27 #include "h/hashing.h"
29 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV)
30 #include <netinet/in_var.h>
31 #endif /* ! AFS_HPUX110_ENV */
32 #endif /* !defined(UKERNEL) */
34 #include "afsincludes.h" /* Afs-based standard headers */
35 #include "afs/afs_stats.h" /* afs statistics */
37 #if defined(AFS_SUN5_ENV)
39 #include <inet/common.h>
40 #include <netinet/ip6.h>
44 #include "afs/afs_axscache.h"
46 /* Exported variables */
47 afs_rwlock_t afs_xuser;
48 struct unixuser *afs_users[NUSERS];
51 #ifndef AFS_PAG_MANAGER
52 /* Forward declarations */
53 void afs_ResetAccessCache(afs_int32 uid, int alock);
54 #endif /* !AFS_PAG_MANAGER */
57 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
58 * and their conns. The aforce parameter tells the function to flush all
59 * *unauthenticated* conns, no matter what their expiration time; it exists
60 * because after we choose our final rx epoch, we want to stop using calls with
61 * other epochs as soon as possible (old file servers act bizarrely when they
65 afs_GCUserData(int aforce)
67 struct unixuser *tu, **lu, *nu;
69 afs_int32 now, delFlag;
71 AFS_STATCNT(afs_GCUserData);
72 /* Obtain locks in valid order */
73 ObtainWriteLock(&afs_xuser, 95);
74 #ifndef AFS_PAG_MANAGER
75 ObtainReadLock(&afs_xserver);
76 ObtainWriteLock(&afs_xconn, 96);
79 for (i = 0; i < NUSERS; i++) {
80 for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
81 delFlag = 0; /* should we delete this dude? */
82 /* Don't garbage collect users in use now (refCount) */
83 if (tu->refCount == 0) {
85 /* Need to walk the token stack, and dispose of
86 * all expired tokens */
87 afs_DiscardExpiredTokens(&tu->tokens, now);
88 if (!afs_HasUsableTokens(tu->tokens, now))
91 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
98 #ifndef AFS_PAG_MANAGER
99 afs_ReleaseConnsUser(tu);
101 afs_FreeTokens(&tu->tokens);
104 EXP_RELE(tu->exporter);
105 afs_osi_Free(tu, sizeof(struct unixuser));
111 #ifndef AFS_PAG_MANAGER
112 ReleaseWriteLock(&afs_xconn);
114 #ifndef AFS_PAG_MANAGER
115 ReleaseReadLock(&afs_xserver);
117 ReleaseWriteLock(&afs_xuser);
119 } /*afs_GCUserData */
121 static struct unixuser *
122 afs_FindUserNoLock(afs_int32 auid, afs_int32 acell)
127 AFS_STATCNT(afs_FindUser);
129 for (tu = afs_users[i]; tu; tu = tu->next) {
130 if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
139 #ifndef AFS_PAG_MANAGER
141 * Check for unixusers who encountered bad tokens, and reset the access
142 * cache for these guys. Can't do this when token expiration detected,
143 * since too many locks are set then.
146 afs_CheckTokenCache(void)
152 struct axscache *tofreelist;
155 AFS_STATCNT(afs_CheckCacheResets);
156 ObtainReadLock(&afs_xvcache);
157 ObtainReadLock(&afs_xuser);
159 for (i = 0; i < NUSERS; i++) {
160 for (tu = afs_users[i]; tu; tu = tu->next) {
162 * If tokens are still good and user has Kerberos tickets,
165 if ((tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
166 if (!afs_HasUsableTokens(tu->tokens, now)) {
168 * This token has expired, warn users and reset access
171 tu->states |= (UTokensBad | UNeedsReset);
174 if (tu->states & UNeedsReset)
178 /* Skip the potentially expensive scan if nothing to do */
183 for (i = 0; i < VCSIZE; i++) {
184 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
185 /* really should do this under cache write lock, but that.
186 * is hard to under locking hierarchy */
188 struct axscache **ac, **nac;
190 for ( ac = &tvc->Access; *ac;) {
192 tu = afs_FindUserNoLock((*ac)->uid, tvc->f.fid.Cell);
193 if (tu == NULL || (tu->states & UNeedsReset)) {
194 struct axscache *tmp;
197 tmp->next = tofreelist;
207 afs_FreeAllAxs(&tofreelist);
208 for (i = 0; i < NUSERS; i++) {
209 for (tu = afs_users[i]; tu; tu = tu->next) {
210 if (tu->states & UNeedsReset)
211 tu->states &= ~UNeedsReset;
216 ReleaseReadLock(&afs_xuser);
217 ReleaseReadLock(&afs_xvcache);
218 } /*afs_CheckTokenCache */
222 afs_ResetAccessCache(afs_int32 uid, int alock)
228 AFS_STATCNT(afs_ResetAccessCache);
230 ObtainReadLock(&afs_xvcache);
231 for (i = 0; i < VCSIZE; i++) {
232 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
233 /* really should do this under cache write lock, but that.
234 * is hard to under locking hierarchy */
235 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
236 afs_RemoveAxs(&tvc->Access, ac);
241 ReleaseReadLock(&afs_xvcache);
243 } /*afs_ResetAccessCache */
247 * Ensure all connections make use of new tokens. Discard incorrectly-cached
251 afs_ResetUserConns(struct unixuser *auser)
255 struct sa_conn_vector *tcv;
257 AFS_STATCNT(afs_ResetUserConns);
258 ObtainReadLock(&afs_xsrvAddr);
259 ObtainWriteLock(&afs_xconn, 98);
261 for (i = 0; i < NSERVERS; i++) {
262 for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
263 for (tcv = sa->conns; tcv; tcv = tcv->next) {
264 if (tcv->user == auser) {
265 for(j = 0; j < CVEC_LEN; ++j) {
266 (tcv->cvec[j]).forceConnectFS = 1;
273 ReleaseWriteLock(&afs_xconn);
274 ReleaseReadLock(&afs_xsrvAddr);
275 afs_ResetAccessCache(auser->uid, 1);
276 auser->states &= ~UNeedsReset;
277 } /*afs_ResetUserConns */
278 #endif /* !AFS_PAG_MANAGER */
282 afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
286 ObtainWriteLock(&afs_xuser, 99);
287 tu = afs_FindUserNoLock(auid, acell);
288 ReleaseWriteLock(&afs_xuser);
290 afs_LockUser(tu, locktype, 365);
295 /*------------------------------------------------------------------------
296 * EXPORTED afs_ComputePAGStats
299 * Compute a set of stats concerning PAGs used by this machine.
308 * The results are put in the structure responsible for keeping
309 * detailed CM stats. Note: entries corresponding to a single PAG
310 * will appear on the identical hash chain, so sweeping the chain
311 * will find all entries related to a single PAG.
315 *------------------------------------------------------------------------*/
318 afs_ComputePAGStats(void)
320 struct unixuser *currPAGP; /*Ptr to curr PAG */
321 struct unixuser *cmpPAGP; /*Ptr to PAG being compared */
322 struct afs_stats_AuthentInfo *authP; /*Ptr to stats area */
323 int curr_Record; /*Curr record */
324 int currChain; /*Curr hash chain */
325 int currChainLen; /*Length of curr hash chain */
326 int currPAGRecords; /*# records in curr PAG */
329 * Lock out everyone else from scribbling on the PAG entries.
331 ObtainReadLock(&afs_xuser);
334 * Initialize the tallies, then sweep through each hash chain. We
335 * can't bzero the structure, since some fields are cumulative over
339 authP = &(afs_stats_cmfullperf.authent);
340 authP->curr_PAGs = 0;
341 authP->curr_Records = 0;
342 authP->curr_AuthRecords = 0;
343 authP->curr_UnauthRecords = 0;
344 authP->curr_MaxRecordsInPAG = 0;
345 authP->curr_LongestChain = 0;
347 for (currChain = 0; currChain < NUSERS; currChain++) {
349 for (currPAGP = afs_users[currChain]; currPAGP;
350 currPAGP = currPAGP->next) {
352 * Bump the number of records on this current chain, along with
353 * the total number of records in existence.
358 * We've found a previously-uncounted PAG. If it's been deleted
359 * but just not garbage-collected yet, we step over it.
361 if (!(currPAGP->states & UHasTokens))
365 * If this PAG record has already been ``counted', namely
366 * associated with a PAG and tallied, clear its bit and move on.
368 (authP->curr_Records)++;
369 if (currPAGP->states & UPAGCounted) {
370 currPAGP->states &= ~UPAGCounted;
376 /*We've counted this one already */
378 * Jot initial info down, then sweep down the rest of the hash
379 * chain, looking for matching PAG entries. Note: to properly
380 * ``count'' the current record, we first compare it to itself
381 * in the following loop.
383 (authP->curr_PAGs)++;
386 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
387 if (currPAGP->uid == cmpPAGP->uid) {
389 * The records belong to the same PAG. First, mark the
390 * new record as ``counted'' and bump the PAG size.
391 * Then, record the state of its ticket, if any.
393 cmpPAGP->states |= UPAGCounted;
395 if ((cmpPAGP->states & UHasTokens)
396 && !(cmpPAGP->states & UTokensBad))
397 (authP->curr_AuthRecords)++;
399 (authP->curr_UnauthRecords)++;
400 } /*Records belong to same PAG */
401 } /*Compare to rest of PAG records in chain */
404 * In the above comparisons, the current PAG record has been
405 * marked as counted. Erase this mark before moving on.
407 currPAGP->states &= ~UPAGCounted;
410 * We've compared our current PAG record with all remaining
411 * PAG records in the hash chain. Update our tallies, and
412 * perhaps even our lifetime high water marks. After that,
413 * remove our search mark and advance to the next comparison
416 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
417 authP->curr_MaxRecordsInPAG = currPAGRecords;
418 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
419 authP->HWM_MaxRecordsInPAG = currPAGRecords;
421 } /*Sweep a hash chain */
424 * If the chain we just finished zipping through is the longest we've
425 * seen yet, remember this fact before advancing to the next chain.
427 if (currChainLen > authP->curr_LongestChain) {
428 authP->curr_LongestChain = currChainLen;
429 if (currChainLen > authP->HWM_LongestChain)
430 authP->HWM_LongestChain = currChainLen;
433 } /*For each hash chain in afs_user */
436 * Now that we've counted everything up, we can consider all-time
439 if (authP->curr_PAGs > authP->HWM_PAGs)
440 authP->HWM_PAGs = authP->curr_PAGs;
441 if (authP->curr_Records > authP->HWM_Records)
442 authP->HWM_Records = authP->curr_Records;
445 * People are free to manipulate the PAG structures now.
447 ReleaseReadLock(&afs_xuser);
449 } /*afs_ComputePAGStats */
453 afs_GetUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
455 struct unixuser *tu, *pu = 0;
457 afs_int32 RmtUser = 0;
459 AFS_STATCNT(afs_GetUser);
461 ObtainWriteLock(&afs_xuser, 104);
462 for (tu = afs_users[i]; tu; tu = tu->next) {
463 if (tu->uid == auid) {
470 if (tu->cell == -1 && acell != -1) {
471 /* Here we setup the real cell for the client */
475 } else if (tu->cell == acell || acell == -1) {
481 tu = afs_osi_Alloc(sizeof(struct unixuser));
482 osi_Assert(tu != NULL);
484 afs_stats_cmfullperf.authent.PAGCreations++;
485 #endif /* AFS_NOSTATS */
486 memset(tu, 0, sizeof(struct unixuser));
487 AFS_RWLOCK_INIT(&tu->lock, "unixuser lock");
488 tu->next = afs_users[i];
492 * This is for the case where an additional unixuser struct is
493 * created because the remote client is accessing a different cell;
494 * we simply rerecord relevant information from the original
497 if (pu && pu->exporter) {
498 tu->exporter = pu->exporter;
499 (void)EXP_HOLD(tu->exporter);
504 tu->viceId = UNDEFVID;
506 tu->tokenTime = osi_Time();
509 ReleaseWriteLock(&afs_xuser);
510 afs_LockUser(tu, locktype, 364);
516 afs_LockUser(struct unixuser *au, afs_int32 locktype,
517 unsigned int src_indicator)
521 ObtainReadLock(&au->lock);
524 ObtainWriteLock(&au->lock, src_indicator);
527 ObtainSharedLock(&au->lock, src_indicator);
536 afs_PutUser(struct unixuser *au, afs_int32 locktype)
538 AFS_STATCNT(afs_PutUser);
542 ReleaseReadLock(&au->lock);
545 ReleaseWriteLock(&au->lock);
548 ReleaseSharedLock(&au->lock);
560 * Set the primary flag on a unixuser structure, ensuring that exactly one
561 * dude has the flag set at any time for a particular unix uid.
564 afs_SetPrimary(struct unixuser *au, int aflag)
570 AFS_STATCNT(afs_SetPrimary);
573 ObtainWriteLock(&afs_xuser, 105);
575 * See if anyone is this uid's primary cell yet; recording in pu the
578 for (tu = afs_users[i]; tu; tu = tu->next) {
579 if (tu->uid == au->uid && (tu->states & UPrimary)) {
583 if (pu && !(pu->states & UHasTokens)) {
585 * Primary user has unlogged, don't treat him as primary any longer;
586 * note that we want to treat him as primary until now, so that
587 * people see a primary identity until now.
589 pu->states &= ~UPrimary;
593 /* setting au to be primary */
595 pu->states &= ~UPrimary;
596 au->states |= UPrimary;
597 } else if (aflag == 0) {
598 /* we don't know if we're supposed to be primary or not */
599 if (!pu || au == pu) {
600 au->states |= UPrimary;
602 au->states &= ~UPrimary;
604 ReleaseWriteLock(&afs_xuser);
606 } /*afs_SetPrimary */
609 afs_NotifyUser(struct unixuser *auser, int event)
611 #ifdef AFS_DARWIN_ENV
612 darwin_notify_perms(auser, event);
617 * Mark all of the unixuser records held for a particular PAG as
621 * PAG to expire records for
624 afs_MarkUserExpired(afs_int32 pag)
630 ObtainWriteLock(&afs_xuser, 9);
631 for (tu = afs_users[i]; tu; tu = tu->next) {
632 if (tu->uid == pag) {
633 tu->states &= ~UHasTokens;
637 ReleaseWriteLock(&afs_xuser);
644 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
645 * process in the system.
646 * If the specified process uses a PAG, clear that PAG's temporary
651 * This variable keeps track of the number of UID-base
652 * tokens in the afs_users table. When it's zero
653 * the per process loop in GCPAGs doesn't have to
654 * check processes without pags against the afs_users table.
656 static afs_int32 afs_GCPAGs_UIDBaseTokenCount = 0;
659 * These variables keep track of the number of times
660 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
661 * walking the process table, there is something wrong and we should not
662 * prematurely expire any tokens.
664 static size_t afs_GCPAGs_perproc_count = 0;
665 static size_t afs_GCPAGs_cred_count = 0;
668 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
670 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_STRUCT_HAS_CRED) || defined(HAVE_LINUX_RCU_READ_LOCK))
672 afs_GCPAGs_perproc_func(afs_proc_t * pproc)
674 afs_int32 pag, hash, uid;
675 const afs_ucred_t *pcred;
677 afs_GCPAGs_perproc_count++;
679 pcred = afs_osi_proc2cred(pproc);
683 afs_GCPAGs_cred_count++;
685 pag = PagInCred(pcred);
686 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_LINUX22_ENV)
687 uid = (pag != NOPAG ? pag : afs_cr_uid(pcred));
688 #elif defined(AFS_SUN510_ENV)
689 uid = (pag != NOPAG ? pag : crgetruid(pcred));
691 uid = (pag != NOPAG ? pag : afs_cr_ruid(pcred));
695 /* if this token is PAG based, or it's UID based and
696 * UID-based tokens exist */
697 if ((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
698 /* find the entries for this uid in all cells and clear the not
699 * referenced flag. Can't use afs_FindUser, because it just returns
700 * the specific cell asked for, or the first one found.
703 for (pu = afs_users[hash]; pu; pu = pu->next) {
704 if (pu->uid == uid) {
705 if (pu->states & TMP_UPAGNotReferenced) {
706 /* clear the 'deleteme' flag for this entry */
707 pu->states &= ~TMP_UPAGNotReferenced;
709 /* This is a uid based token that hadn't
710 * previously been cleared, so decrement the
711 * outstanding uid based token count */
712 afs_GCPAGs_UIDBaseTokenCount--;
722 * Go through the process table, find all unused PAGs
723 * and cause them to be deleted during the next GC.
725 * returns the number of PAGs marked for deletion
727 * On AIX we free PAGs when the last accessing process exits,
728 * so this routine is not needed.
730 * In AFS WebSecure, we explicitly call unlog when we remove
731 * an entry in the login cache, so this routine is not needed.
735 afs_GCPAGs(afs_int32 * ReleasedCount)
740 if (afs_gcpags != AFS_GCPAGS_OK) {
746 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
747 ObtainWriteLock(&afs_xuser, 419);
748 afs_GCPAGs_UIDBaseTokenCount = 0;
749 for (i = 0; i < NUSERS; i++) {
750 for (pu = afs_users[i]; pu; pu = pu->next) {
751 pu->states |= TMP_UPAGNotReferenced;
752 if (((pu->uid >> 24) & 0xff) != 'A') {
753 /* this is a uid-based token, */
754 /* increment the count */
755 afs_GCPAGs_UIDBaseTokenCount++;
760 /* Now, iterate through the systems process table,
761 * for each process, mark it's PAGs (if any) in use.
762 * i.e. clear the temporary deleteme flag.
764 afs_GCPAGs_perproc_count = 0;
765 afs_GCPAGs_cred_count = 0;
767 afs_osi_TraverseProcTable();
769 /* If there is an internal problem and afs_GCPAGs_perproc_func()
770 * does not get called, disable gcpags so that we do not
771 * accidentally expire all the tokens in the system.
773 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
774 afs_gcpags = AFS_GCPAGS_EPROCWALK;
777 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
778 afs_gcpags = AFS_GCPAGS_ECREDWALK;
781 /* Now, go through afs_users again, any that aren't in use
782 * (temp deleteme flag still set) will be marked for later deletion,
783 * by setting their expire times to 0.
785 for (i = 0; i < NUSERS; i++) {
786 for (pu = afs_users[i]; pu; pu = pu->next) {
787 if (pu->states & TMP_UPAGNotReferenced) {
789 /* clear the temp flag */
790 pu->states &= ~TMP_UPAGNotReferenced;
792 /* Is this entry on behalf of a 'remote' user ?
793 * i.e. nfs translator, etc.
795 if (!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
796 /* make afs_GCUserData remove this entry */
797 pu->states &= ~UHasTokens;
800 (*ReleasedCount)++; /* remember how many we marked (info only) */
806 ReleaseWriteLock(&afs_xuser);
811 #endif /* AFS_GCPAGS */