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 "../afs/param.h" /* Should be always first */
14 #include "../afs/stds.h"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include <netinet/in.h>
22 #include "../h/hashing.h"
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
25 #include <netinet/in_var.h>
26 #endif /* ! AFS_HPUX110_ENV */
27 #endif /* !defined(UKERNEL) */
29 #include "../afs/afsincludes.h" /* Afs-based standard headers */
30 #include "../afs/afs_stats.h" /* afs statistics */
32 #if defined(AFS_SUN56_ENV)
34 #include <inet/common.h>
35 #if defined(AFS_SUN58_ENV)
36 #include <netinet/ip6.h>
42 /* Imported variables */
43 extern afs_rwlock_t afs_xserver;
44 extern afs_rwlock_t afs_xsrvAddr;
45 extern afs_rwlock_t afs_xconn;
46 extern afs_rwlock_t afs_xvcache;
47 extern struct srvAddr *afs_srvAddrs[NSERVERS]; /* Hashed by server's ip */
48 extern struct server *afs_servers[NSERVERS];
50 /* Exported variables */
51 afs_rwlock_t afs_xuser;
52 struct unixuser *afs_users[NUSERS];
55 /* Forward declarations */
56 void afs_ResetAccessCache(afs_int32 uid, int alock);
59 * Called with afs_xuser, afs_xserver and afs_xconn locks held, to delete
60 * appropriate conn structures for au
62 static void RemoveUserConns(au)
63 register struct unixuser *au;
66 register struct server *ts;
67 register struct srvAddr *sa;
68 register struct conn *tc, **lc;
70 AFS_STATCNT(RemoveUserConns);
71 for (i=0;i<NSERVERS;i++) {
72 for (ts = afs_servers[i]; ts; ts=ts->next) {
73 for (sa = ts->addr; sa; sa = sa->next_sa) {
75 for (tc = *lc; tc; lc = &tc->next, tc = *lc) {
76 if (tc->user == au && tc->refCount == 0) {
79 rx_DestroyConnection(tc->id);
81 afs_osi_Free(tc, sizeof(struct conn));
82 break; /* at most one instance per server */
83 } /*Found unreferenced connection for user*/
84 } /*For each connection on the server*/
86 } /*For each server on chain*/
92 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
93 * and their conns. The aforce parameter tells the function to flush all
94 * *unauthenticated* conns, no matter what their expiration time; it exists
95 * because after we choose our final rx epoch, we want to stop using calls with
96 * other epochs as soon as possible (old file servers act bizarrely when they
99 void afs_GCUserData(aforce) {
100 register struct unixuser *tu, **lu, *nu;
102 afs_int32 now, delFlag;
104 AFS_STATCNT(afs_GCUserData);
105 /* Obtain locks in valid order */
106 ObtainWriteLock(&afs_xuser,95);
107 ObtainReadLock(&afs_xserver);
108 ObtainWriteLock(&afs_xconn,96);
110 for (i=0;i<NUSERS;i++) {
111 for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
112 delFlag = 0; /* should we delete this dude? */
113 /* Don't garbage collect users in use now (refCount) */
114 if (tu->refCount == 0) {
115 if (tu->states & UHasTokens) {
117 * Give ourselves a little extra slack, in case we
120 if (tu->ct.EndTimestamp < now - NOTOKTIMEOUT)
124 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
133 afs_osi_Free(tu->stp, tu->stLen);
135 EXP_RELE(tu->exporter);
136 afs_osi_Free(tu, sizeof(struct unixuser));
143 ReleaseWriteLock(&afs_xconn);
144 ReleaseWriteLock(&afs_xuser);
145 ReleaseReadLock(&afs_xserver);
151 * Check for unixusers who encountered bad tokens, and reset the access
152 * cache for these guys. Can't do this when token expiration detected,
153 * since too many locks are set then.
155 void afs_CheckTokenCache()
159 register struct unixuser *tu;
161 register struct cell *tcell;
163 AFS_STATCNT(afs_CheckCacheResets);
164 ObtainReadLock(&afs_xvcache);
165 ObtainReadLock(&afs_xuser);
167 for (i=0;i<NUSERS;i++) {
168 for (tu=afs_users[i]; tu; tu=tu->next) {
169 register afs_int32 uid;
172 * If tokens are still good and user has Kerberos tickets,
175 if (!(tu->states & UTokensBad) && tu->vid != UNDEFVID) {
176 if (tu->ct.EndTimestamp < now) {
178 * This token has expired, warn users and reset access
182 tcell = afs_GetCell(tu->cell);
183 /* I really hate this message - MLK */
184 afs_warn("afs: Tokens for user of AFS id %d for cell %s expired now\n",
185 tu->vid, tcell->cellName);
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);
200 } /*afs_CheckTokenCache*/
203 void afs_ResetAccessCache(afs_int32 uid, int alock)
206 register struct vcache *tvc;
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
231 void afs_ResetUserConns (auser)
232 register struct unixuser *auser;
240 AFS_STATCNT(afs_ResetUserConns);
241 ObtainReadLock(&afs_xsrvAddr);
242 ObtainWriteLock(&afs_xconn,98);
244 for (i=0;i<NSERVERS;i++) {
245 for (sa = afs_srvAddrs[i]; sa; sa=sa->next_bkt) {
246 for (tc = sa->conns; tc; tc=tc->next) {
247 if (tc->user == auser) {
248 tc->forceConnectFS = 1;
254 ReleaseWriteLock(&afs_xconn);
255 ReleaseReadLock(&afs_xsrvAddr);
256 afs_ResetAccessCache(auser->uid, 1);
257 auser->states &= ~UNeedsReset;
258 } /*afs_ResetUserConns*/
261 struct unixuser *afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
263 register struct unixuser *tu;
264 register afs_int32 i;
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);
277 return (struct unixuser *) 0;
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 *------------------------------------------------------------------------*/
304 void afs_ComputePAGStats()
306 { /*afs_ComputePAGStats*/
308 register struct unixuser *currPAGP; /*Ptr to curr PAG*/
309 register struct unixuser *cmpPAGP; /*Ptr to PAG being compared*/
310 register struct afs_stats_AuthentInfo *authP; /*Ptr to stats area*/
311 int curr_Record; /*Curr record */
312 int currChain; /*Curr hash chain*/
313 int currChainLen; /*Length of curr hash chain*/
314 int currPAGRecords; /*# records in curr PAG*/
317 * Lock out everyone else from scribbling on the PAG entries.
319 ObtainReadLock(&afs_xuser);
322 * Initialize the tallies, then sweep through each hash chain. We
323 * can't bzero the structure, since some fields are cumulative over
327 authP = &(afs_stats_cmfullperf.authent);
328 authP->curr_PAGs = 0;
329 authP->curr_Records = 0;
330 authP->curr_AuthRecords = 0;
331 authP->curr_UnauthRecords = 0;
332 authP->curr_MaxRecordsInPAG = 0;
333 authP->curr_LongestChain = 0;
335 for (currChain = 0; currChain < NUSERS; currChain++) {
337 for (currPAGP = afs_users[currChain]; currPAGP;
338 currPAGP = currPAGP->next)
341 * Bump the number of records on this current chain, along with
342 * the total number of records in existence.
347 * We've found a previously-uncounted PAG. If it's been deleted
348 * but just not garbage-collected yet, we step over it.
350 if (currPAGP->vid == UNDEFVID)
354 * If this PAG record has already been ``counted', namely
355 * associated with a PAG and tallied, clear its bit and move on.
357 (authP->curr_Records)++;
358 if (currPAGP->states & UPAGCounted) {
359 currPAGP->states &= ~UPAGCounted;
361 } /*We've counted this one already*/
366 * Jot initial info down, then sweep down the rest of the hash
367 * chain, looking for matching PAG entries. Note: to properly
368 * ``count'' the current record, we first compare it to itself
369 * in the following loop.
371 (authP->curr_PAGs)++;
374 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
375 if (currPAGP->uid == cmpPAGP->uid) {
377 * The records belong to the same PAG. First, mark the
378 * new record as ``counted'' and bump the PAG size.
379 * Then, record the state of its ticket, if any.
381 cmpPAGP->states |= UPAGCounted;
383 if ((cmpPAGP->states & UHasTokens) &&
384 !(cmpPAGP->states & UTokensBad))
385 (authP->curr_AuthRecords)++;
387 (authP->curr_UnauthRecords)++;
388 } /*Records belong to same PAG*/
389 } /*Compare to rest of PAG records in chain*/
392 * In the above comparisons, the current PAG record has been
393 * marked as counted. Erase this mark before moving on.
395 currPAGP->states &= ~UPAGCounted;
398 * We've compared our current PAG record with all remaining
399 * PAG records in the hash chain. Update our tallies, and
400 * perhaps even our lifetime high water marks. After that,
401 * remove our search mark and advance to the next comparison
404 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
405 authP->curr_MaxRecordsInPAG = currPAGRecords;
406 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
407 authP->HWM_MaxRecordsInPAG = currPAGRecords;
409 } /*Sweep a hash chain*/
412 * If the chain we just finished zipping through is the longest we've
413 * seen yet, remember this fact before advancing to the next chain.
415 if (currChainLen > authP->curr_LongestChain) {
416 authP->curr_LongestChain = currChainLen;
417 if (currChainLen > authP->HWM_LongestChain)
418 authP->HWM_LongestChain = currChainLen;
421 } /*For each hash chain in afs_user*/
424 * Now that we've counted everything up, we can consider all-time
427 if (authP->curr_PAGs > authP->HWM_PAGs)
428 authP->HWM_PAGs = authP->curr_PAGs;
429 if (authP->curr_Records > authP->HWM_Records)
430 authP->HWM_Records = authP->curr_Records;
433 * People are free to manipulate the PAG structures now.
435 ReleaseReadLock(&afs_xuser);
437 } /*afs_ComputePAGStats*/
440 struct unixuser *afs_GetUser(auid, acell, locktype)
442 register afs_int32 auid;
445 register struct unixuser *tu, *pu=0;
446 register afs_int32 i;
447 register afs_int32 RmtUser = 0;
449 AFS_STATCNT(afs_GetUser);
451 ObtainWriteLock(&afs_xuser,104);
452 for (tu = afs_users[i]; tu; tu = tu->next) {
453 if (tu->uid == auid) {
455 pu = (struct unixuser *)0;
460 if (tu->cell == -1 && acell != -1) {
461 /* Here we setup the real cell for the client */
464 ReleaseWriteLock(&afs_xuser);
467 if (tu->cell == acell || acell == -1) {
469 ReleaseWriteLock(&afs_xuser);
474 tu = (struct unixuser *) afs_osi_Alloc(sizeof(struct unixuser));
476 afs_stats_cmfullperf.authent.PAGCreations++;
477 #endif /* AFS_NOSTATS */
478 bzero((char *)tu, sizeof(struct unixuser));
479 tu->next = afs_users[i];
483 * This is for the case where an additional unixuser struct is
484 * created because the remote client is accessing a different cell;
485 * we simply rerecord relevant information from the original
488 if (pu && pu->exporter) {
489 (void) EXP_HOLD(tu->exporter = pu->exporter);
496 tu->tokenTime = osi_Time();
497 ReleaseWriteLock(&afs_xuser);
503 void afs_PutUser(au, locktype)
504 register struct unixuser *au;
507 AFS_STATCNT(afs_PutUser);
513 * Set the primary flag on a unixuser structure, ensuring that exactly one
514 * dude has the flag set at any time for a particular unix uid.
516 void afs_SetPrimary(au, aflag)
517 register struct unixuser *au;
521 register struct unixuser *tu;
525 AFS_STATCNT(afs_SetPrimary);
527 pu = (struct unixuser *) 0;
528 ObtainWriteLock(&afs_xuser,105);
530 * See if anyone is this uid's primary cell yet; recording in pu the
533 for (tu=afs_users[i]; tu; tu=tu->next) {
534 if (tu->uid == au->uid && (tu->states & UPrimary)) {
538 if (pu && !(pu->states & UHasTokens)) {
540 * Primary user has unlogged, don't treat him as primary any longer;
541 * note that we want to treat him as primary until now, so that
542 * people see a primary identity until now.
544 pu->states &= ~UPrimary;
545 pu = (struct unixuser *) 0;
548 /* setting au to be primary */
549 if (pu) pu->states &= ~UPrimary;
550 au->states |= UPrimary;
554 /* we don't know if we're supposed to be primary or not */
555 if (!pu || au == pu) {
556 au->states |= UPrimary;
559 au->states &= ~UPrimary;
561 ReleaseWriteLock(&afs_xuser);
569 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
570 * process in the system.
571 * If the specified process uses a PAG, clear that PAG's temporary
576 * This variable keeps track of the number of UID-base
577 * tokens in the afs_users table. When it's zero
578 * the per process loop in GCPAGs doesn't have to
579 * check processes without pags against the afs_users table.
581 static afs_int32 afs_GCPAGs_UIDBaseTokenCount=0;
584 * These variables keep track of the number of times
585 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
586 * walking the process table, there is something wrong and we should not
587 * prematurely expire any tokens.
589 static size_t afs_GCPAGs_perproc_count=0;
590 static size_t afs_GCPAGs_cred_count=0;
593 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
595 void afs_GCPAGs_perproc_func(AFS_PROC *pproc)
597 afs_int32 pag, hash, uid;
598 const struct AFS_UCRED *pcred;
600 afs_GCPAGs_perproc_count++;
602 pcred = afs_osi_proc2cred(pproc);
606 afs_GCPAGs_cred_count++;
608 pag = PagInCred(pcred);
609 uid = (pag != NOPAG ? pag : pcred->cr_ruid);
612 /* if this token is PAG based, or it's UID based and
613 UID-based tokens exist */
614 if((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
615 /* find the entries for this uid in all cells and clear the not
616 * referenced flag. Can't use afs_FindUser, because it just returns
617 * the specific cell asked for, or the first one found.
620 for(pu = afs_users[hash]; pu; pu = pu->next) {
621 if (pu->uid == uid) {
622 if(pu->states & TMP_UPAGNotReferenced) {
623 /* clear the 'deleteme' flag for this entry */
624 pu->states &= ~TMP_UPAGNotReferenced;
626 /* This is a uid based token that hadn't
627 previously been cleared, so decrement the
628 outstanding uid based token count */
629 afs_GCPAGs_UIDBaseTokenCount--;
638 * Go through the process table, find all unused PAGs
639 * and cause them to be deleted during the next GC.
641 * returns the number of PAGs marked for deletion
643 * On AIX we free PAGs when the last accessing process exits,
644 * so this routine is not needed.
646 * In AFS WebSecure, we explicitly call unlog when we remove
647 * an entry in the login cache, so this routine is not needed.
650 afs_int32 afs_GCPAGs(afs_int32 *ReleasedCount)
655 if (afs_gcpags != AFS_GCPAGS_OK) {
661 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
662 ObtainWriteLock(&afs_xuser,419);
663 afs_GCPAGs_UIDBaseTokenCount=0;
664 for(i=0; i < NUSERS; i++) {
665 for(pu = afs_users[i]; pu; pu = pu->next) {
666 pu->states |= TMP_UPAGNotReferenced;
667 if (((pu->uid >> 24) & 0xff) != 'A') {
668 /* this is a uid-based token, */
669 /* increment the count */
670 afs_GCPAGs_UIDBaseTokenCount++;
675 /* Now, iterate through the systems process table,
676 * for each process, mark it's PAGs (if any) in use.
677 * i.e. clear the temporary deleteme flag.
679 afs_GCPAGs_perproc_count=0;
680 afs_GCPAGs_cred_count=0;
682 afs_osi_TraverseProcTable();
684 /* If there is an internal problem and afs_GCPAGs_perproc_func()
685 * does not get called, disable gcpags so that we do not
686 * accidentally expire all the tokens in the system.
688 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
689 afs_gcpags = AFS_GCPAGS_EPROCWALK;
692 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
693 afs_gcpags = AFS_GCPAGS_ECREDWALK;
696 /* Now, go through afs_users again, any that aren't in use
697 * (temp deleteme flag still set) will be marked for later deletion,
698 * by setting their expire times to 0.
700 for(i=0; i < NUSERS; i++) {
701 for(pu = afs_users[i]; pu; pu = pu->next) {
702 if(pu->states & TMP_UPAGNotReferenced) {
704 /* clear the temp flag */
705 pu->states &= ~TMP_UPAGNotReferenced;
707 /* Is this entry on behalf of a 'remote' user ?
708 * i.e. nfs translator, etc.
710 if(!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
711 /* set the expire times to 0, causes
712 * afs_GCUserData to remove this entry
714 pu->ct.EndTimestamp = 0;
717 (*ReleasedCount)++; /* remember how many we marked (info only) */
723 ReleaseWriteLock(&afs_xuser);
728 #endif /* AFS_GCPAGS */