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_SUN56_ENV)
39 #include <inet/common.h>
40 #if defined(AFS_SUN58_ENV)
41 #include <netinet/ip6.h>
47 /* Exported variables */
48 afs_rwlock_t afs_xuser;
49 struct unixuser *afs_users[NUSERS];
52 #ifndef AFS_PAG_MANAGER
53 /* Forward declarations */
54 void afs_ResetAccessCache(afs_int32 uid, int alock);
57 * Called with afs_xuser, afs_xserver and afs_xconn locks held, to delete
58 * appropriate conn structures for au
61 RemoveUserConns(struct unixuser *au)
66 struct afs_conn *tc, **lc;
68 AFS_STATCNT(RemoveUserConns);
69 for (i = 0; i < NSERVERS; i++) {
70 for (ts = afs_servers[i]; ts; ts = ts->next) {
71 for (sa = ts->addr; sa; sa = sa->next_sa) {
73 for (tc = *lc; tc; lc = &tc->next, tc = *lc) {
74 if (tc->user == au && tc->refCount == 0) {
77 rx_DestroyConnection(tc->id);
79 afs_osi_Free(tc, sizeof(struct afs_conn));
80 break; /* at most one instance per server */
81 } /*Found unreferenced connection for user */
82 } /*For each connection on the server */
84 } /*For each server on chain */
87 } /*RemoveUserConns */
88 #endif /* !AFS_PAG_MANAGER */
91 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
92 * and their conns. The aforce parameter tells the function to flush all
93 * *unauthenticated* conns, no matter what their expiration time; it exists
94 * because after we choose our final rx epoch, we want to stop using calls with
95 * other epochs as soon as possible (old file servers act bizarrely when they
99 afs_GCUserData(int aforce)
101 struct unixuser *tu, **lu, *nu;
103 afs_int32 now, delFlag;
105 AFS_STATCNT(afs_GCUserData);
106 /* Obtain locks in valid order */
107 ObtainWriteLock(&afs_xuser, 95);
108 #ifndef AFS_PAG_MANAGER
109 ObtainReadLock(&afs_xserver);
110 ObtainWriteLock(&afs_xconn, 96);
113 for (i = 0; i < NUSERS; i++) {
114 for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
115 delFlag = 0; /* should we delete this dude? */
116 /* Don't garbage collect users in use now (refCount) */
117 if (tu->refCount == 0) {
119 /* Need to walk the token stack, and dispose of
120 * all expired tokens */
121 afs_DiscardExpiredTokens(&tu->tokens, now);
122 if (!afs_HasUsableTokens(tu->tokens, now))
125 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
132 #ifndef AFS_PAG_MANAGER
135 afs_FreeTokens(&tu->tokens);
138 EXP_RELE(tu->exporter);
139 afs_osi_Free(tu, sizeof(struct unixuser));
145 #ifndef AFS_PAG_MANAGER
146 ReleaseWriteLock(&afs_xconn);
148 #ifndef AFS_PAG_MANAGER
149 ReleaseReadLock(&afs_xserver);
151 ReleaseWriteLock(&afs_xuser);
153 } /*afs_GCUserData */
156 #ifndef AFS_PAG_MANAGER
158 * Check for unixusers who encountered bad tokens, and reset the access
159 * cache for these guys. Can't do this when token expiration detected,
160 * since too many locks are set then.
163 afs_CheckTokenCache(void)
169 AFS_STATCNT(afs_CheckCacheResets);
170 ObtainReadLock(&afs_xvcache);
171 ObtainReadLock(&afs_xuser);
173 for (i = 0; i < NUSERS; i++) {
174 for (tu = afs_users[i]; tu; tu = tu->next) {
178 * If tokens are still good and user has Kerberos tickets,
181 if ((tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
182 if (!afs_HasUsableTokens(tu->tokens, now)) {
184 * This token has expired, warn users and reset access
187 tu->states |= (UTokensBad | UNeedsReset);
190 if (tu->states & UNeedsReset) {
191 tu->states &= ~UNeedsReset;
193 afs_ResetAccessCache(uid, 0);
197 ReleaseReadLock(&afs_xuser);
198 ReleaseReadLock(&afs_xvcache);
199 } /*afs_CheckTokenCache */
203 afs_ResetAccessCache(afs_int32 uid, int alock)
209 AFS_STATCNT(afs_ResetAccessCache);
211 ObtainReadLock(&afs_xvcache);
212 for (i = 0; i < VCSIZE; i++) {
213 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
214 /* really should do this under cache write lock, but that.
215 * is hard to under locking hierarchy */
216 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
217 afs_RemoveAxs(&tvc->Access, ac);
222 ReleaseReadLock(&afs_xvcache);
224 } /*afs_ResetAccessCache */
228 * Ensure all connections make use of new tokens. Discard incorrectly-cached
232 afs_ResetUserConns(struct unixuser *auser)
238 AFS_STATCNT(afs_ResetUserConns);
239 ObtainReadLock(&afs_xsrvAddr);
240 ObtainWriteLock(&afs_xconn, 98);
242 for (i = 0; i < NSERVERS; i++) {
243 for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
244 for (tc = sa->conns; tc; tc = tc->next) {
245 if (tc->user == auser) {
246 tc->forceConnectFS = 1;
252 ReleaseWriteLock(&afs_xconn);
253 ReleaseReadLock(&afs_xsrvAddr);
254 afs_ResetAccessCache(auser->uid, 1);
255 auser->states &= ~UNeedsReset;
256 } /*afs_ResetUserConns */
257 #endif /* !AFS_PAG_MANAGER */
261 afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
266 AFS_STATCNT(afs_FindUser);
268 ObtainWriteLock(&afs_xuser, 99);
269 for (tu = afs_users[i]; tu; tu = tu->next) {
270 if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
272 ReleaseWriteLock(&afs_xuser);
276 ReleaseWriteLock(&afs_xuser);
282 /*------------------------------------------------------------------------
283 * EXPORTED afs_ComputePAGStats
286 * Compute a set of stats concerning PAGs used by this machine.
295 * The results are put in the structure responsible for keeping
296 * detailed CM stats. Note: entries corresponding to a single PAG
297 * will appear on the identical hash chain, so sweeping the chain
298 * will find all entries related to a single PAG.
302 *------------------------------------------------------------------------*/
305 afs_ComputePAGStats(void)
307 struct unixuser *currPAGP; /*Ptr to curr PAG */
308 struct unixuser *cmpPAGP; /*Ptr to PAG being compared */
309 struct afs_stats_AuthentInfo *authP; /*Ptr to stats area */
310 int curr_Record; /*Curr record */
311 int currChain; /*Curr hash chain */
312 int currChainLen; /*Length of curr hash chain */
313 int currPAGRecords; /*# records in curr PAG */
316 * Lock out everyone else from scribbling on the PAG entries.
318 ObtainReadLock(&afs_xuser);
321 * Initialize the tallies, then sweep through each hash chain. We
322 * can't bzero the structure, since some fields are cumulative over
326 authP = &(afs_stats_cmfullperf.authent);
327 authP->curr_PAGs = 0;
328 authP->curr_Records = 0;
329 authP->curr_AuthRecords = 0;
330 authP->curr_UnauthRecords = 0;
331 authP->curr_MaxRecordsInPAG = 0;
332 authP->curr_LongestChain = 0;
334 for (currChain = 0; currChain < NUSERS; currChain++) {
336 for (currPAGP = afs_users[currChain]; currPAGP;
337 currPAGP = currPAGP->next) {
339 * Bump the number of records on this current chain, along with
340 * the total number of records in existence.
345 * We've found a previously-uncounted PAG. If it's been deleted
346 * but just not garbage-collected yet, we step over it.
348 if (!(currPAGP->states & UHasTokens))
352 * If this PAG record has already been ``counted', namely
353 * associated with a PAG and tallied, clear its bit and move on.
355 (authP->curr_Records)++;
356 if (currPAGP->states & UPAGCounted) {
357 currPAGP->states &= ~UPAGCounted;
363 /*We've counted this one already */
365 * Jot initial info down, then sweep down the rest of the hash
366 * chain, looking for matching PAG entries. Note: to properly
367 * ``count'' the current record, we first compare it to itself
368 * in the following loop.
370 (authP->curr_PAGs)++;
373 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
374 if (currPAGP->uid == cmpPAGP->uid) {
376 * The records belong to the same PAG. First, mark the
377 * new record as ``counted'' and bump the PAG size.
378 * Then, record the state of its ticket, if any.
380 cmpPAGP->states |= UPAGCounted;
382 if ((cmpPAGP->states & UHasTokens)
383 && !(cmpPAGP->states & UTokensBad))
384 (authP->curr_AuthRecords)++;
386 (authP->curr_UnauthRecords)++;
387 } /*Records belong to same PAG */
388 } /*Compare to rest of PAG records in chain */
391 * In the above comparisons, the current PAG record has been
392 * marked as counted. Erase this mark before moving on.
394 currPAGP->states &= ~UPAGCounted;
397 * We've compared our current PAG record with all remaining
398 * PAG records in the hash chain. Update our tallies, and
399 * perhaps even our lifetime high water marks. After that,
400 * remove our search mark and advance to the next comparison
403 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
404 authP->curr_MaxRecordsInPAG = currPAGRecords;
405 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
406 authP->HWM_MaxRecordsInPAG = currPAGRecords;
408 } /*Sweep a hash chain */
411 * If the chain we just finished zipping through is the longest we've
412 * seen yet, remember this fact before advancing to the next chain.
414 if (currChainLen > authP->curr_LongestChain) {
415 authP->curr_LongestChain = currChainLen;
416 if (currChainLen > authP->HWM_LongestChain)
417 authP->HWM_LongestChain = currChainLen;
420 } /*For each hash chain in afs_user */
423 * Now that we've counted everything up, we can consider all-time
426 if (authP->curr_PAGs > authP->HWM_PAGs)
427 authP->HWM_PAGs = authP->curr_PAGs;
428 if (authP->curr_Records > authP->HWM_Records)
429 authP->HWM_Records = authP->curr_Records;
432 * People are free to manipulate the PAG structures now.
434 ReleaseReadLock(&afs_xuser);
436 } /*afs_ComputePAGStats */
440 afs_GetUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
442 struct unixuser *tu, *pu = 0;
444 afs_int32 RmtUser = 0;
446 AFS_STATCNT(afs_GetUser);
448 ObtainWriteLock(&afs_xuser, 104);
449 for (tu = afs_users[i]; tu; tu = tu->next) {
450 if (tu->uid == auid) {
457 if (tu->cell == -1 && acell != -1) {
458 /* Here we setup the real cell for the client */
461 ReleaseWriteLock(&afs_xuser);
463 } else if (tu->cell == acell || acell == -1) {
465 ReleaseWriteLock(&afs_xuser);
470 tu = afs_osi_Alloc(sizeof(struct unixuser));
471 osi_Assert(tu != NULL);
473 afs_stats_cmfullperf.authent.PAGCreations++;
474 #endif /* AFS_NOSTATS */
475 memset(tu, 0, sizeof(struct unixuser));
476 tu->next = afs_users[i];
480 * This is for the case where an additional unixuser struct is
481 * created because the remote client is accessing a different cell;
482 * we simply rerecord relevant information from the original
485 if (pu && pu->exporter) {
486 tu->exporter = pu->exporter;
487 (void)EXP_HOLD(tu->exporter);
492 tu->viceId = UNDEFVID;
494 tu->tokenTime = osi_Time();
495 ReleaseWriteLock(&afs_xuser);
502 afs_PutUser(struct unixuser *au, afs_int32 locktype)
504 AFS_STATCNT(afs_PutUser);
510 * Set the primary flag on a unixuser structure, ensuring that exactly one
511 * dude has the flag set at any time for a particular unix uid.
514 afs_SetPrimary(struct unixuser *au, int aflag)
520 AFS_STATCNT(afs_SetPrimary);
523 ObtainWriteLock(&afs_xuser, 105);
525 * See if anyone is this uid's primary cell yet; recording in pu the
528 for (tu = afs_users[i]; tu; tu = tu->next) {
529 if (tu->uid == au->uid && (tu->states & UPrimary)) {
533 if (pu && !(pu->states & UHasTokens)) {
535 * Primary user has unlogged, don't treat him as primary any longer;
536 * note that we want to treat him as primary until now, so that
537 * people see a primary identity until now.
539 pu->states &= ~UPrimary;
543 /* setting au to be primary */
545 pu->states &= ~UPrimary;
546 au->states |= UPrimary;
547 } else if (aflag == 0) {
548 /* we don't know if we're supposed to be primary or not */
549 if (!pu || au == pu) {
550 au->states |= UPrimary;
552 au->states &= ~UPrimary;
554 ReleaseWriteLock(&afs_xuser);
556 } /*afs_SetPrimary */
559 afs_NotifyUser(struct unixuser *auser, int event)
561 #ifdef AFS_DARWIN_ENV
562 darwin_notify_perms(auser, event);
567 * Mark all of the unixuser records held for a particular PAG as
571 * PAG to expire records for
574 afs_MarkUserExpired(afs_int32 pag)
580 ObtainWriteLock(&afs_xuser, 9);
581 for (tu = afs_users[i]; tu; tu = tu->next) {
582 if (tu->uid == pag) {
583 tu->states &= ~UHasTokens;
587 ReleaseWriteLock(&afs_xuser);
594 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
595 * process in the system.
596 * If the specified process uses a PAG, clear that PAG's temporary
601 * This variable keeps track of the number of UID-base
602 * tokens in the afs_users table. When it's zero
603 * the per process loop in GCPAGs doesn't have to
604 * check processes without pags against the afs_users table.
606 static afs_int32 afs_GCPAGs_UIDBaseTokenCount = 0;
609 * These variables keep track of the number of times
610 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
611 * walking the process table, there is something wrong and we should not
612 * prematurely expire any tokens.
614 static size_t afs_GCPAGs_perproc_count = 0;
615 static size_t afs_GCPAGs_cred_count = 0;
618 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
620 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_STRUCT_HAS_CRED) || defined(HAVE_LINUX_RCU_READ_LOCK))
622 afs_GCPAGs_perproc_func(afs_proc_t * pproc)
624 afs_int32 pag, hash, uid;
625 const afs_ucred_t *pcred;
627 afs_GCPAGs_perproc_count++;
629 pcred = afs_osi_proc2cred(pproc);
633 afs_GCPAGs_cred_count++;
635 pag = PagInCred(pcred);
636 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_LINUX22_ENV)
637 uid = (pag != NOPAG ? pag : afs_cr_uid(pcred));
638 #elif defined(AFS_SUN510_ENV)
639 uid = (pag != NOPAG ? pag : crgetruid(pcred));
641 uid = (pag != NOPAG ? pag : afs_cr_ruid(pcred));
645 /* if this token is PAG based, or it's UID based and
646 * UID-based tokens exist */
647 if ((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
648 /* find the entries for this uid in all cells and clear the not
649 * referenced flag. Can't use afs_FindUser, because it just returns
650 * the specific cell asked for, or the first one found.
653 for (pu = afs_users[hash]; pu; pu = pu->next) {
654 if (pu->uid == uid) {
655 if (pu->states & TMP_UPAGNotReferenced) {
656 /* clear the 'deleteme' flag for this entry */
657 pu->states &= ~TMP_UPAGNotReferenced;
659 /* This is a uid based token that hadn't
660 * previously been cleared, so decrement the
661 * outstanding uid based token count */
662 afs_GCPAGs_UIDBaseTokenCount--;
672 * Go through the process table, find all unused PAGs
673 * and cause them to be deleted during the next GC.
675 * returns the number of PAGs marked for deletion
677 * On AIX we free PAGs when the last accessing process exits,
678 * so this routine is not needed.
680 * In AFS WebSecure, we explicitly call unlog when we remove
681 * an entry in the login cache, so this routine is not needed.
685 afs_GCPAGs(afs_int32 * ReleasedCount)
690 if (afs_gcpags != AFS_GCPAGS_OK) {
696 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
697 ObtainWriteLock(&afs_xuser, 419);
698 afs_GCPAGs_UIDBaseTokenCount = 0;
699 for (i = 0; i < NUSERS; i++) {
700 for (pu = afs_users[i]; pu; pu = pu->next) {
701 pu->states |= TMP_UPAGNotReferenced;
702 if (((pu->uid >> 24) & 0xff) != 'A') {
703 /* this is a uid-based token, */
704 /* increment the count */
705 afs_GCPAGs_UIDBaseTokenCount++;
710 /* Now, iterate through the systems process table,
711 * for each process, mark it's PAGs (if any) in use.
712 * i.e. clear the temporary deleteme flag.
714 afs_GCPAGs_perproc_count = 0;
715 afs_GCPAGs_cred_count = 0;
717 afs_osi_TraverseProcTable();
719 /* If there is an internal problem and afs_GCPAGs_perproc_func()
720 * does not get called, disable gcpags so that we do not
721 * accidentally expire all the tokens in the system.
723 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
724 afs_gcpags = AFS_GCPAGS_EPROCWALK;
727 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
728 afs_gcpags = AFS_GCPAGS_ECREDWALK;
731 /* Now, go through afs_users again, any that aren't in use
732 * (temp deleteme flag still set) will be marked for later deletion,
733 * by setting their expire times to 0.
735 for (i = 0; i < NUSERS; i++) {
736 for (pu = afs_users[i]; pu; pu = pu->next) {
737 if (pu->states & TMP_UPAGNotReferenced) {
739 /* clear the temp flag */
740 pu->states &= ~TMP_UPAGNotReferenced;
742 /* Is this entry on behalf of a 'remote' user ?
743 * i.e. nfs translator, etc.
745 if (!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
746 /* make afs_GCUserData remove this entry */
747 pu->states &= ~UHasTokens;
750 (*ReleasedCount)++; /* remember how many we marked (info only) */
756 ReleaseWriteLock(&afs_xuser);
761 #endif /* AFS_GCPAGS */