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(register struct unixuser *au)
64 register struct server *ts;
65 register struct srvAddr *sa;
66 register 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 register 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) {
118 if (tu->states & UHasTokens) {
120 * Give ourselves a little extra slack, in case we
123 if (tu->ct.EndTimestamp < now - NOTOKTIMEOUT)
126 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
133 #ifndef AFS_PAG_MANAGER
137 afs_osi_Free(tu->stp, tu->stLen);
139 EXP_RELE(tu->exporter);
140 afs_osi_Free(tu, sizeof(struct unixuser));
146 #ifndef AFS_PAG_MANAGER
147 ReleaseWriteLock(&afs_xconn);
149 #ifndef AFS_PAG_MANAGER
150 ReleaseReadLock(&afs_xserver);
152 ReleaseWriteLock(&afs_xuser);
154 } /*afs_GCUserData */
157 #ifndef AFS_PAG_MANAGER
159 * Check for unixusers who encountered bad tokens, and reset the access
160 * cache for these guys. Can't do this when token expiration detected,
161 * since too many locks are set then.
164 afs_CheckTokenCache(void)
167 register struct unixuser *tu;
170 AFS_STATCNT(afs_CheckCacheResets);
171 ObtainReadLock(&afs_xvcache);
172 ObtainReadLock(&afs_xuser);
174 for (i = 0; i < NUSERS; i++) {
175 for (tu = afs_users[i]; tu; tu = tu->next) {
176 register afs_int32 uid;
179 * If tokens are still good and user has Kerberos tickets,
182 if (!(tu->states & UTokensBad) && tu->vid != UNDEFVID) {
183 if (tu->ct.EndTimestamp < now) {
185 * This token has expired, warn users and reset access
189 /* I really hate this message - MLK */
191 ("afs: Tokens for user of AFS id %d for cell %s expired now\n",
192 tu->vid, afs_GetCell(tu->cell)->cellName);
194 tu->states |= (UTokensBad | UNeedsReset);
197 if (tu->states & UNeedsReset) {
198 tu->states &= ~UNeedsReset;
200 afs_ResetAccessCache(uid, 0);
204 ReleaseReadLock(&afs_xuser);
205 ReleaseReadLock(&afs_xvcache);
207 } /*afs_CheckTokenCache */
211 afs_ResetAccessCache(afs_int32 uid, int alock)
214 register struct vcache *tvc;
217 AFS_STATCNT(afs_ResetAccessCache);
219 ObtainReadLock(&afs_xvcache);
220 for (i = 0; i < VCSIZE; i++) {
221 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
222 /* really should do this under cache write lock, but that.
223 * is hard to under locking hierarchy */
224 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
225 afs_RemoveAxs(&tvc->Access, ac);
230 ReleaseReadLock(&afs_xvcache);
232 } /*afs_ResetAccessCache */
236 * Ensure all connections make use of new tokens. Discard incorrectly-cached
240 afs_ResetUserConns(register struct unixuser *auser)
246 AFS_STATCNT(afs_ResetUserConns);
247 ObtainReadLock(&afs_xsrvAddr);
248 ObtainWriteLock(&afs_xconn, 98);
250 for (i = 0; i < NSERVERS; i++) {
251 for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
252 for (tc = sa->conns; tc; tc = tc->next) {
253 if (tc->user == auser) {
254 tc->forceConnectFS = 1;
260 ReleaseWriteLock(&afs_xconn);
261 ReleaseReadLock(&afs_xsrvAddr);
262 afs_ResetAccessCache(auser->uid, 1);
263 auser->states &= ~UNeedsReset;
264 } /*afs_ResetUserConns */
265 #endif /* !AFS_PAG_MANAGER */
269 afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
271 register struct unixuser *tu;
272 register afs_int32 i;
274 AFS_STATCNT(afs_FindUser);
276 ObtainWriteLock(&afs_xuser, 99);
277 for (tu = afs_users[i]; tu; tu = tu->next) {
278 if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
280 ReleaseWriteLock(&afs_xuser);
284 ReleaseWriteLock(&afs_xuser);
290 /*------------------------------------------------------------------------
291 * EXPORTED afs_ComputePAGStats
294 * Compute a set of stats concerning PAGs used by this machine.
303 * The results are put in the structure responsible for keeping
304 * detailed CM stats. Note: entries corresponding to a single PAG
305 * will appear on the identical hash chain, so sweeping the chain
306 * will find all entries related to a single PAG.
310 *------------------------------------------------------------------------*/
313 afs_ComputePAGStats(void)
315 register struct unixuser *currPAGP; /*Ptr to curr PAG */
316 register struct unixuser *cmpPAGP; /*Ptr to PAG being compared */
317 register struct afs_stats_AuthentInfo *authP; /*Ptr to stats area */
318 int curr_Record; /*Curr record */
319 int currChain; /*Curr hash chain */
320 int currChainLen; /*Length of curr hash chain */
321 int currPAGRecords; /*# records in curr PAG */
324 * Lock out everyone else from scribbling on the PAG entries.
326 ObtainReadLock(&afs_xuser);
329 * Initialize the tallies, then sweep through each hash chain. We
330 * can't bzero the structure, since some fields are cumulative over
334 authP = &(afs_stats_cmfullperf.authent);
335 authP->curr_PAGs = 0;
336 authP->curr_Records = 0;
337 authP->curr_AuthRecords = 0;
338 authP->curr_UnauthRecords = 0;
339 authP->curr_MaxRecordsInPAG = 0;
340 authP->curr_LongestChain = 0;
342 for (currChain = 0; currChain < NUSERS; currChain++) {
344 for (currPAGP = afs_users[currChain]; currPAGP;
345 currPAGP = currPAGP->next) {
347 * Bump the number of records on this current chain, along with
348 * the total number of records in existence.
353 * We've found a previously-uncounted PAG. If it's been deleted
354 * but just not garbage-collected yet, we step over it.
356 if (currPAGP->vid == UNDEFVID)
360 * If this PAG record has already been ``counted', namely
361 * associated with a PAG and tallied, clear its bit and move on.
363 (authP->curr_Records)++;
364 if (currPAGP->states & UPAGCounted) {
365 currPAGP->states &= ~UPAGCounted;
371 /*We've counted this one already */
373 * Jot initial info down, then sweep down the rest of the hash
374 * chain, looking for matching PAG entries. Note: to properly
375 * ``count'' the current record, we first compare it to itself
376 * in the following loop.
378 (authP->curr_PAGs)++;
381 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
382 if (currPAGP->uid == cmpPAGP->uid) {
384 * The records belong to the same PAG. First, mark the
385 * new record as ``counted'' and bump the PAG size.
386 * Then, record the state of its ticket, if any.
388 cmpPAGP->states |= UPAGCounted;
390 if ((cmpPAGP->states & UHasTokens)
391 && !(cmpPAGP->states & UTokensBad))
392 (authP->curr_AuthRecords)++;
394 (authP->curr_UnauthRecords)++;
395 } /*Records belong to same PAG */
396 } /*Compare to rest of PAG records in chain */
399 * In the above comparisons, the current PAG record has been
400 * marked as counted. Erase this mark before moving on.
402 currPAGP->states &= ~UPAGCounted;
405 * We've compared our current PAG record with all remaining
406 * PAG records in the hash chain. Update our tallies, and
407 * perhaps even our lifetime high water marks. After that,
408 * remove our search mark and advance to the next comparison
411 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
412 authP->curr_MaxRecordsInPAG = currPAGRecords;
413 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
414 authP->HWM_MaxRecordsInPAG = currPAGRecords;
416 } /*Sweep a hash chain */
419 * If the chain we just finished zipping through is the longest we've
420 * seen yet, remember this fact before advancing to the next chain.
422 if (currChainLen > authP->curr_LongestChain) {
423 authP->curr_LongestChain = currChainLen;
424 if (currChainLen > authP->HWM_LongestChain)
425 authP->HWM_LongestChain = currChainLen;
428 } /*For each hash chain in afs_user */
431 * Now that we've counted everything up, we can consider all-time
434 if (authP->curr_PAGs > authP->HWM_PAGs)
435 authP->HWM_PAGs = authP->curr_PAGs;
436 if (authP->curr_Records > authP->HWM_Records)
437 authP->HWM_Records = authP->curr_Records;
440 * People are free to manipulate the PAG structures now.
442 ReleaseReadLock(&afs_xuser);
444 } /*afs_ComputePAGStats */
448 afs_GetUser(register afs_int32 auid, afs_int32 acell, afs_int32 locktype)
450 register struct unixuser *tu, *pu = 0;
451 register afs_int32 i;
452 register afs_int32 RmtUser = 0;
454 AFS_STATCNT(afs_GetUser);
456 ObtainWriteLock(&afs_xuser, 104);
457 for (tu = afs_users[i]; tu; tu = tu->next) {
458 if (tu->uid == auid) {
465 if (tu->cell == -1 && acell != -1) {
466 /* Here we setup the real cell for the client */
469 ReleaseWriteLock(&afs_xuser);
471 } else if (tu->cell == acell || acell == -1) {
473 ReleaseWriteLock(&afs_xuser);
478 tu = (struct unixuser *)afs_osi_Alloc(sizeof(struct unixuser));
480 afs_stats_cmfullperf.authent.PAGCreations++;
481 #endif /* AFS_NOSTATS */
482 memset(tu, 0, sizeof(struct unixuser));
483 tu->next = afs_users[i];
487 * This is for the case where an additional unixuser struct is
488 * created because the remote client is accessing a different cell;
489 * we simply rerecord relevant information from the original
492 if (pu && pu->exporter) {
493 (void)EXP_HOLD(tu->exporter = pu->exporter);
500 tu->tokenTime = osi_Time();
501 ReleaseWriteLock(&afs_xuser);
508 afs_PutUser(register struct unixuser *au, afs_int32 locktype)
510 AFS_STATCNT(afs_PutUser);
516 * Set the primary flag on a unixuser structure, ensuring that exactly one
517 * dude has the flag set at any time for a particular unix uid.
520 afs_SetPrimary(register struct unixuser *au, register int aflag)
522 register struct unixuser *tu;
526 AFS_STATCNT(afs_SetPrimary);
529 ObtainWriteLock(&afs_xuser, 105);
531 * See if anyone is this uid's primary cell yet; recording in pu the
534 for (tu = afs_users[i]; tu; tu = tu->next) {
535 if (tu->uid == au->uid && (tu->states & UPrimary)) {
539 if (pu && !(pu->states & UHasTokens)) {
541 * Primary user has unlogged, don't treat him as primary any longer;
542 * note that we want to treat him as primary until now, so that
543 * people see a primary identity until now.
545 pu->states &= ~UPrimary;
549 /* setting au to be primary */
551 pu->states &= ~UPrimary;
552 au->states |= UPrimary;
553 } else if (aflag == 0) {
554 /* we don't know if we're supposed to be primary or not */
555 if (!pu || au == pu) {
556 au->states |= UPrimary;
558 au->states &= ~UPrimary;
560 ReleaseWriteLock(&afs_xuser);
562 } /*afs_SetPrimary */
565 afs_NotifyUser(struct unixuser *auser, int event)
567 #ifdef AFS_DARWIN_ENV
568 darwin_notify_perms(auser, event);
573 * Mark all of the unixuser records held for a particular PAG as
577 * PAG to expire records for
580 afs_MarkUserExpired(afs_int32 pag)
586 ObtainWriteLock(&afs_xuser, 9);
587 for (tu = afs_users[i]; tu; tu = tu->next) {
588 if (tu->uid == pag) {
589 tu->ct.EndTimestamp = 0;
593 ReleaseWriteLock(&afs_xuser);
600 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
601 * process in the system.
602 * If the specified process uses a PAG, clear that PAG's temporary
607 * This variable keeps track of the number of UID-base
608 * tokens in the afs_users table. When it's zero
609 * the per process loop in GCPAGs doesn't have to
610 * check processes without pags against the afs_users table.
612 static afs_int32 afs_GCPAGs_UIDBaseTokenCount = 0;
615 * These variables keep track of the number of times
616 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
617 * walking the process table, there is something wrong and we should not
618 * prematurely expire any tokens.
620 static size_t afs_GCPAGs_perproc_count = 0;
621 static size_t afs_GCPAGs_cred_count = 0;
624 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
626 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_STRUCT_HAS_CRED) || defined(HAVE_LINUX_RCU_READ_LOCK))
628 afs_GCPAGs_perproc_func(afs_proc_t * pproc)
630 afs_int32 pag, hash, uid;
631 const afs_ucred_t *pcred;
633 afs_GCPAGs_perproc_count++;
635 pcred = afs_osi_proc2cred(pproc);
639 afs_GCPAGs_cred_count++;
641 pag = PagInCred(pcred);
642 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_LINUX22_ENV)
643 uid = (pag != NOPAG ? pag : afs_cr_uid(pcred));
644 #elif defined(AFS_SUN510_ENV)
645 uid = (pag != NOPAG ? pag : crgetruid(pcred));
647 uid = (pag != NOPAG ? pag : afs_cr_ruid(pcred));
651 /* if this token is PAG based, or it's UID based and
652 * UID-based tokens exist */
653 if ((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
654 /* find the entries for this uid in all cells and clear the not
655 * referenced flag. Can't use afs_FindUser, because it just returns
656 * the specific cell asked for, or the first one found.
659 for (pu = afs_users[hash]; pu; pu = pu->next) {
660 if (pu->uid == uid) {
661 if (pu->states & TMP_UPAGNotReferenced) {
662 /* clear the 'deleteme' flag for this entry */
663 pu->states &= ~TMP_UPAGNotReferenced;
665 /* This is a uid based token that hadn't
666 * previously been cleared, so decrement the
667 * outstanding uid based token count */
668 afs_GCPAGs_UIDBaseTokenCount--;
678 * Go through the process table, find all unused PAGs
679 * and cause them to be deleted during the next GC.
681 * returns the number of PAGs marked for deletion
683 * On AIX we free PAGs when the last accessing process exits,
684 * so this routine is not needed.
686 * In AFS WebSecure, we explicitly call unlog when we remove
687 * an entry in the login cache, so this routine is not needed.
691 afs_GCPAGs(afs_int32 * ReleasedCount)
696 if (afs_gcpags != AFS_GCPAGS_OK) {
702 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
703 ObtainWriteLock(&afs_xuser, 419);
704 afs_GCPAGs_UIDBaseTokenCount = 0;
705 for (i = 0; i < NUSERS; i++) {
706 for (pu = afs_users[i]; pu; pu = pu->next) {
707 pu->states |= TMP_UPAGNotReferenced;
708 if (((pu->uid >> 24) & 0xff) != 'A') {
709 /* this is a uid-based token, */
710 /* increment the count */
711 afs_GCPAGs_UIDBaseTokenCount++;
716 /* Now, iterate through the systems process table,
717 * for each process, mark it's PAGs (if any) in use.
718 * i.e. clear the temporary deleteme flag.
720 afs_GCPAGs_perproc_count = 0;
721 afs_GCPAGs_cred_count = 0;
723 afs_osi_TraverseProcTable();
725 /* If there is an internal problem and afs_GCPAGs_perproc_func()
726 * does not get called, disable gcpags so that we do not
727 * accidentally expire all the tokens in the system.
729 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
730 afs_gcpags = AFS_GCPAGS_EPROCWALK;
733 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
734 afs_gcpags = AFS_GCPAGS_ECREDWALK;
737 /* Now, go through afs_users again, any that aren't in use
738 * (temp deleteme flag still set) will be marked for later deletion,
739 * by setting their expire times to 0.
741 for (i = 0; i < NUSERS; i++) {
742 for (pu = afs_users[i]; pu; pu = pu->next) {
743 if (pu->states & TMP_UPAGNotReferenced) {
745 /* clear the temp flag */
746 pu->states &= ~TMP_UPAGNotReferenced;
748 /* Is this entry on behalf of a 'remote' user ?
749 * i.e. nfs translator, etc.
751 if (!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
752 /* set the expire times to 0, causes
753 * afs_GCUserData to remove this entry
755 pu->ct.EndTimestamp = 0;
758 (*ReleasedCount)++; /* remember how many we marked (info only) */
764 ReleaseWriteLock(&afs_xuser);
769 #endif /* AFS_GCPAGS */