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/stds.h"
19 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include <netinet/in.h>
26 #include "../h/hashing.h"
28 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
29 #include <netinet/in_var.h>
30 #endif /* ! AFS_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
33 #include "../afs/afsincludes.h" /* Afs-based standard headers */
34 #include "../afs/afs_stats.h" /* afs statistics */
36 #if defined(AFS_SUN56_ENV)
38 #include <inet/common.h>
39 #if defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
46 /* Exported variables */
47 afs_rwlock_t afs_xuser;
48 struct unixuser *afs_users[NUSERS];
51 /* Forward declarations */
52 void afs_ResetAccessCache(afs_int32 uid, int alock);
55 * Called with afs_xuser, afs_xserver and afs_xconn locks held, to delete
56 * appropriate conn structures for au
58 static void RemoveUserConns(register struct unixuser *au)
61 register struct server *ts;
62 register struct srvAddr *sa;
63 register struct conn *tc, **lc;
65 AFS_STATCNT(RemoveUserConns);
66 for (i=0;i<NSERVERS;i++) {
67 for (ts = afs_servers[i]; ts; ts=ts->next) {
68 for (sa = ts->addr; sa; sa = sa->next_sa) {
70 for (tc = *lc; tc; lc = &tc->next, tc = *lc) {
71 if (tc->user == au && tc->refCount == 0) {
74 rx_DestroyConnection(tc->id);
76 afs_osi_Free(tc, sizeof(struct conn));
77 break; /* at most one instance per server */
78 } /*Found unreferenced connection for user*/
79 } /*For each connection on the server*/
81 } /*For each server on chain*/
87 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
88 * and their conns. The aforce parameter tells the function to flush all
89 * *unauthenticated* conns, no matter what their expiration time; it exists
90 * because after we choose our final rx epoch, we want to stop using calls with
91 * other epochs as soon as possible (old file servers act bizarrely when they
94 void afs_GCUserData(int aforce)
96 register struct unixuser *tu, **lu, *nu;
98 afs_int32 now, delFlag;
100 AFS_STATCNT(afs_GCUserData);
101 /* Obtain locks in valid order */
102 ObtainWriteLock(&afs_xuser,95);
103 ObtainReadLock(&afs_xserver);
104 ObtainWriteLock(&afs_xconn,96);
106 for (i=0;i<NUSERS;i++) {
107 for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
108 delFlag = 0; /* should we delete this dude? */
109 /* Don't garbage collect users in use now (refCount) */
110 if (tu->refCount == 0) {
111 if (tu->states & UHasTokens) {
113 * Give ourselves a little extra slack, in case we
116 if (tu->ct.EndTimestamp < now - NOTOKTIMEOUT)
120 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
129 afs_osi_Free(tu->stp, tu->stLen);
131 EXP_RELE(tu->exporter);
132 afs_osi_Free(tu, sizeof(struct unixuser));
139 ReleaseWriteLock(&afs_xconn);
140 ReleaseWriteLock(&afs_xuser);
141 ReleaseReadLock(&afs_xserver);
147 * Check for unixusers who encountered bad tokens, and reset the access
148 * cache for these guys. Can't do this when token expiration detected,
149 * since too many locks are set then.
151 void afs_CheckTokenCache(void)
154 register struct unixuser *tu;
157 AFS_STATCNT(afs_CheckCacheResets);
158 ObtainReadLock(&afs_xvcache);
159 ObtainReadLock(&afs_xuser);
161 for (i=0;i<NUSERS;i++) {
162 for (tu=afs_users[i]; tu; tu=tu->next) {
163 register afs_int32 uid;
166 * If tokens are still good and user has Kerberos tickets,
169 if (!(tu->states & UTokensBad) && tu->vid != UNDEFVID) {
170 if (tu->ct.EndTimestamp < now) {
172 * This token has expired, warn users and reset access
176 /* I really hate this message - MLK */
177 afs_warn("afs: Tokens for user of AFS id %d for cell %s expired now\n",
178 tu->vid, afs_GetCell(tu->cell)->cellName);
180 tu->states |= (UTokensBad | UNeedsReset);
183 if (tu->states & UNeedsReset) {
184 tu->states &= ~UNeedsReset;
186 afs_ResetAccessCache(uid, 0);
190 ReleaseReadLock(&afs_xuser);
191 ReleaseReadLock(&afs_xvcache);
193 } /*afs_CheckTokenCache*/
196 void afs_ResetAccessCache(afs_int32 uid, int alock)
199 register struct vcache *tvc;
202 AFS_STATCNT(afs_ResetAccessCache);
204 ObtainReadLock(&afs_xvcache);
205 for(i=0;i<VCSIZE;i++) {
206 for(tvc=afs_vhashT[i]; tvc; tvc=tvc->hnext) {
207 /* really should do this under cache write lock, but that.
208 is hard to under locking hierarchy */
209 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
210 afs_RemoveAxs (&tvc->Access, ac);
215 ReleaseReadLock(&afs_xvcache);
217 } /*afs_ResetAccessCache*/
221 * Ensure all connections make use of new tokens. Discard incorrectly-cached
224 void afs_ResetUserConns (register struct unixuser *auser)
230 AFS_STATCNT(afs_ResetUserConns);
231 ObtainReadLock(&afs_xsrvAddr);
232 ObtainWriteLock(&afs_xconn,98);
234 for (i=0;i<NSERVERS;i++) {
235 for (sa = afs_srvAddrs[i]; sa; sa=sa->next_bkt) {
236 for (tc = sa->conns; tc; tc=tc->next) {
237 if (tc->user == auser) {
238 tc->forceConnectFS = 1;
244 ReleaseWriteLock(&afs_xconn);
245 ReleaseReadLock(&afs_xsrvAddr);
246 afs_ResetAccessCache(auser->uid, 1);
247 auser->states &= ~UNeedsReset;
248 } /*afs_ResetUserConns*/
251 struct unixuser *afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
253 register struct unixuser *tu;
254 register afs_int32 i;
256 AFS_STATCNT(afs_FindUser);
258 ObtainWriteLock(&afs_xuser,99);
259 for(tu = afs_users[i]; tu; tu = tu->next) {
260 if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
262 ReleaseWriteLock(&afs_xuser);
266 ReleaseWriteLock(&afs_xuser);
272 /*------------------------------------------------------------------------
273 * EXPORTED afs_ComputePAGStats
276 * Compute a set of stats concerning PAGs used by this machine.
285 * The results are put in the structure responsible for keeping
286 * detailed CM stats. Note: entries corresponding to a single PAG
287 * will appear on the identical hash chain, so sweeping the chain
288 * will find all entries related to a single PAG.
292 *------------------------------------------------------------------------*/
294 void afs_ComputePAGStats(void)
296 register struct unixuser *currPAGP; /*Ptr to curr PAG*/
297 register struct unixuser *cmpPAGP; /*Ptr to PAG being compared*/
298 register struct afs_stats_AuthentInfo *authP; /*Ptr to stats area*/
299 int curr_Record; /*Curr record */
300 int currChain; /*Curr hash chain*/
301 int currChainLen; /*Length of curr hash chain*/
302 int currPAGRecords; /*# records in curr PAG*/
305 * Lock out everyone else from scribbling on the PAG entries.
307 ObtainReadLock(&afs_xuser);
310 * Initialize the tallies, then sweep through each hash chain. We
311 * can't bzero the structure, since some fields are cumulative over
315 authP = &(afs_stats_cmfullperf.authent);
316 authP->curr_PAGs = 0;
317 authP->curr_Records = 0;
318 authP->curr_AuthRecords = 0;
319 authP->curr_UnauthRecords = 0;
320 authP->curr_MaxRecordsInPAG = 0;
321 authP->curr_LongestChain = 0;
323 for (currChain = 0; currChain < NUSERS; currChain++) {
325 for (currPAGP = afs_users[currChain]; currPAGP;
326 currPAGP = currPAGP->next)
329 * Bump the number of records on this current chain, along with
330 * the total number of records in existence.
335 * We've found a previously-uncounted PAG. If it's been deleted
336 * but just not garbage-collected yet, we step over it.
338 if (currPAGP->vid == UNDEFVID)
342 * If this PAG record has already been ``counted', namely
343 * associated with a PAG and tallied, clear its bit and move on.
345 (authP->curr_Records)++;
346 if (currPAGP->states & UPAGCounted) {
347 currPAGP->states &= ~UPAGCounted;
349 } /*We've counted this one already*/
354 * Jot initial info down, then sweep down the rest of the hash
355 * chain, looking for matching PAG entries. Note: to properly
356 * ``count'' the current record, we first compare it to itself
357 * in the following loop.
359 (authP->curr_PAGs)++;
362 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
363 if (currPAGP->uid == cmpPAGP->uid) {
365 * The records belong to the same PAG. First, mark the
366 * new record as ``counted'' and bump the PAG size.
367 * Then, record the state of its ticket, if any.
369 cmpPAGP->states |= UPAGCounted;
371 if ((cmpPAGP->states & UHasTokens) &&
372 !(cmpPAGP->states & UTokensBad))
373 (authP->curr_AuthRecords)++;
375 (authP->curr_UnauthRecords)++;
376 } /*Records belong to same PAG*/
377 } /*Compare to rest of PAG records in chain*/
380 * In the above comparisons, the current PAG record has been
381 * marked as counted. Erase this mark before moving on.
383 currPAGP->states &= ~UPAGCounted;
386 * We've compared our current PAG record with all remaining
387 * PAG records in the hash chain. Update our tallies, and
388 * perhaps even our lifetime high water marks. After that,
389 * remove our search mark and advance to the next comparison
392 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
393 authP->curr_MaxRecordsInPAG = currPAGRecords;
394 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
395 authP->HWM_MaxRecordsInPAG = currPAGRecords;
397 } /*Sweep a hash chain*/
400 * If the chain we just finished zipping through is the longest we've
401 * seen yet, remember this fact before advancing to the next chain.
403 if (currChainLen > authP->curr_LongestChain) {
404 authP->curr_LongestChain = currChainLen;
405 if (currChainLen > authP->HWM_LongestChain)
406 authP->HWM_LongestChain = currChainLen;
409 } /*For each hash chain in afs_user*/
412 * Now that we've counted everything up, we can consider all-time
415 if (authP->curr_PAGs > authP->HWM_PAGs)
416 authP->HWM_PAGs = authP->curr_PAGs;
417 if (authP->curr_Records > authP->HWM_Records)
418 authP->HWM_Records = authP->curr_Records;
421 * People are free to manipulate the PAG structures now.
423 ReleaseReadLock(&afs_xuser);
425 } /*afs_ComputePAGStats*/
428 struct unixuser *afs_GetUser(register afs_int32 auid,
429 afs_int32 acell, afs_int32 locktype)
431 register struct unixuser *tu, *pu=0;
432 register afs_int32 i;
433 register afs_int32 RmtUser = 0;
435 AFS_STATCNT(afs_GetUser);
437 ObtainWriteLock(&afs_xuser,104);
438 for (tu = afs_users[i]; tu; tu = tu->next) {
439 if (tu->uid == auid) {
446 if (tu->cell == -1 && acell != -1) {
447 /* Here we setup the real cell for the client */
450 ReleaseWriteLock(&afs_xuser);
453 if (tu->cell == acell || acell == -1) {
455 ReleaseWriteLock(&afs_xuser);
460 tu = (struct unixuser *) afs_osi_Alloc(sizeof(struct unixuser));
462 afs_stats_cmfullperf.authent.PAGCreations++;
463 #endif /* AFS_NOSTATS */
464 memset((char *)tu, 0, sizeof(struct unixuser));
465 tu->next = afs_users[i];
469 * This is for the case where an additional unixuser struct is
470 * created because the remote client is accessing a different cell;
471 * we simply rerecord relevant information from the original
474 if (pu && pu->exporter) {
475 (void) EXP_HOLD(tu->exporter = pu->exporter);
482 tu->tokenTime = osi_Time();
483 ReleaseWriteLock(&afs_xuser);
489 void afs_PutUser(register struct unixuser *au, afs_int32 locktype)
491 AFS_STATCNT(afs_PutUser);
497 * Set the primary flag on a unixuser structure, ensuring that exactly one
498 * dude has the flag set at any time for a particular unix uid.
500 void afs_SetPrimary(register struct unixuser *au, register int aflag)
502 register struct unixuser *tu;
506 AFS_STATCNT(afs_SetPrimary);
509 ObtainWriteLock(&afs_xuser,105);
511 * See if anyone is this uid's primary cell yet; recording in pu the
514 for (tu=afs_users[i]; tu; tu=tu->next) {
515 if (tu->uid == au->uid && (tu->states & UPrimary)) {
519 if (pu && !(pu->states & UHasTokens)) {
521 * Primary user has unlogged, don't treat him as primary any longer;
522 * note that we want to treat him as primary until now, so that
523 * people see a primary identity until now.
525 pu->states &= ~UPrimary;
529 /* setting au to be primary */
530 if (pu) pu->states &= ~UPrimary;
531 au->states |= UPrimary;
535 /* we don't know if we're supposed to be primary or not */
536 if (!pu || au == pu) {
537 au->states |= UPrimary;
540 au->states &= ~UPrimary;
542 ReleaseWriteLock(&afs_xuser);
550 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
551 * process in the system.
552 * If the specified process uses a PAG, clear that PAG's temporary
557 * This variable keeps track of the number of UID-base
558 * tokens in the afs_users table. When it's zero
559 * the per process loop in GCPAGs doesn't have to
560 * check processes without pags against the afs_users table.
562 static afs_int32 afs_GCPAGs_UIDBaseTokenCount=0;
565 * These variables keep track of the number of times
566 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
567 * walking the process table, there is something wrong and we should not
568 * prematurely expire any tokens.
570 static size_t afs_GCPAGs_perproc_count=0;
571 static size_t afs_GCPAGs_cred_count=0;
574 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
576 void afs_GCPAGs_perproc_func(AFS_PROC *pproc)
578 afs_int32 pag, hash, uid;
579 const struct AFS_UCRED *pcred;
581 afs_GCPAGs_perproc_count++;
583 pcred = afs_osi_proc2cred(pproc);
587 afs_GCPAGs_cred_count++;
589 pag = PagInCred(pcred);
590 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD40_ENV)
591 uid = (pag != NOPAG ? pag : pcred->cr_uid);
593 uid = (pag != NOPAG ? pag : pcred->cr_ruid);
597 /* if this token is PAG based, or it's UID based and
598 UID-based tokens exist */
599 if((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
600 /* find the entries for this uid in all cells and clear the not
601 * referenced flag. Can't use afs_FindUser, because it just returns
602 * the specific cell asked for, or the first one found.
605 for(pu = afs_users[hash]; pu; pu = pu->next) {
606 if (pu->uid == uid) {
607 if(pu->states & TMP_UPAGNotReferenced) {
608 /* clear the 'deleteme' flag for this entry */
609 pu->states &= ~TMP_UPAGNotReferenced;
611 /* This is a uid based token that hadn't
612 previously been cleared, so decrement the
613 outstanding uid based token count */
614 afs_GCPAGs_UIDBaseTokenCount--;
623 * Go through the process table, find all unused PAGs
624 * and cause them to be deleted during the next GC.
626 * returns the number of PAGs marked for deletion
628 * On AIX we free PAGs when the last accessing process exits,
629 * so this routine is not needed.
631 * In AFS WebSecure, we explicitly call unlog when we remove
632 * an entry in the login cache, so this routine is not needed.
635 afs_int32 afs_GCPAGs(afs_int32 *ReleasedCount)
640 if (afs_gcpags != AFS_GCPAGS_OK) {
646 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
647 ObtainWriteLock(&afs_xuser,419);
648 afs_GCPAGs_UIDBaseTokenCount=0;
649 for(i=0; i < NUSERS; i++) {
650 for(pu = afs_users[i]; pu; pu = pu->next) {
651 pu->states |= TMP_UPAGNotReferenced;
652 if (((pu->uid >> 24) & 0xff) != 'A') {
653 /* this is a uid-based token, */
654 /* increment the count */
655 afs_GCPAGs_UIDBaseTokenCount++;
660 /* Now, iterate through the systems process table,
661 * for each process, mark it's PAGs (if any) in use.
662 * i.e. clear the temporary deleteme flag.
664 afs_GCPAGs_perproc_count=0;
665 afs_GCPAGs_cred_count=0;
667 afs_osi_TraverseProcTable();
669 /* If there is an internal problem and afs_GCPAGs_perproc_func()
670 * does not get called, disable gcpags so that we do not
671 * accidentally expire all the tokens in the system.
673 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
674 afs_gcpags = AFS_GCPAGS_EPROCWALK;
677 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
678 afs_gcpags = AFS_GCPAGS_ECREDWALK;
681 /* Now, go through afs_users again, any that aren't in use
682 * (temp deleteme flag still set) will be marked for later deletion,
683 * by setting their expire times to 0.
685 for(i=0; i < NUSERS; i++) {
686 for(pu = afs_users[i]; pu; pu = pu->next) {
687 if(pu->states & TMP_UPAGNotReferenced) {
689 /* clear the temp flag */
690 pu->states &= ~TMP_UPAGNotReferenced;
692 /* Is this entry on behalf of a 'remote' user ?
693 * i.e. nfs translator, etc.
695 if(!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
696 /* set the expire times to 0, causes
697 * afs_GCUserData to remove this entry
699 pu->ct.EndTimestamp = 0;
702 (*ReleasedCount)++; /* remember how many we marked (info only) */
708 ReleaseWriteLock(&afs_xuser);
713 #endif /* AFS_GCPAGS */