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>
45 /* Exported variables */
46 afs_rwlock_t afs_xuser;
47 struct unixuser *afs_users[NUSERS];
50 #ifndef AFS_PAG_MANAGER
51 /* Forward declarations */
52 void afs_ResetAccessCache(afs_int32 uid, int alock);
53 #endif /* !AFS_PAG_MANAGER */
56 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
57 * and their conns. The aforce parameter tells the function to flush all
58 * *unauthenticated* conns, no matter what their expiration time; it exists
59 * because after we choose our final rx epoch, we want to stop using calls with
60 * other epochs as soon as possible (old file servers act bizarrely when they
64 afs_GCUserData(int aforce)
66 struct unixuser *tu, **lu, *nu;
68 afs_int32 now, delFlag;
70 AFS_STATCNT(afs_GCUserData);
71 /* Obtain locks in valid order */
72 ObtainWriteLock(&afs_xuser, 95);
73 #ifndef AFS_PAG_MANAGER
74 ObtainReadLock(&afs_xserver);
75 ObtainWriteLock(&afs_xconn, 96);
78 for (i = 0; i < NUSERS; i++) {
79 for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
80 delFlag = 0; /* should we delete this dude? */
81 /* Don't garbage collect users in use now (refCount) */
82 if (tu->refCount == 0) {
84 /* Need to walk the token stack, and dispose of
85 * all expired tokens */
86 afs_DiscardExpiredTokens(&tu->tokens, now);
87 if (!afs_HasUsableTokens(tu->tokens, now))
90 if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
97 #ifndef AFS_PAG_MANAGER
98 afs_ReleaseConnsUser(tu);
100 afs_FreeTokens(&tu->tokens);
103 EXP_RELE(tu->exporter);
104 afs_osi_Free(tu, sizeof(struct unixuser));
110 #ifndef AFS_PAG_MANAGER
111 ReleaseWriteLock(&afs_xconn);
113 #ifndef AFS_PAG_MANAGER
114 ReleaseReadLock(&afs_xserver);
116 ReleaseWriteLock(&afs_xuser);
118 } /*afs_GCUserData */
121 #ifndef AFS_PAG_MANAGER
123 * Check for unixusers who encountered bad tokens, and reset the access
124 * cache for these guys. Can't do this when token expiration detected,
125 * since too many locks are set then.
128 afs_CheckTokenCache(void)
134 AFS_STATCNT(afs_CheckCacheResets);
135 ObtainReadLock(&afs_xvcache);
136 ObtainReadLock(&afs_xuser);
138 for (i = 0; i < NUSERS; i++) {
139 for (tu = afs_users[i]; tu; tu = tu->next) {
143 * If tokens are still good and user has Kerberos tickets,
146 if ((tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
147 if (!afs_HasUsableTokens(tu->tokens, now)) {
149 * This token has expired, warn users and reset access
152 tu->states |= (UTokensBad | UNeedsReset);
155 if (tu->states & UNeedsReset) {
156 tu->states &= ~UNeedsReset;
158 afs_ResetAccessCache(uid, 0);
162 ReleaseReadLock(&afs_xuser);
163 ReleaseReadLock(&afs_xvcache);
164 } /*afs_CheckTokenCache */
168 afs_ResetAccessCache(afs_int32 uid, int alock)
174 AFS_STATCNT(afs_ResetAccessCache);
176 ObtainReadLock(&afs_xvcache);
177 for (i = 0; i < VCSIZE; i++) {
178 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
179 /* really should do this under cache write lock, but that.
180 * is hard to under locking hierarchy */
181 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
182 afs_RemoveAxs(&tvc->Access, ac);
187 ReleaseReadLock(&afs_xvcache);
189 } /*afs_ResetAccessCache */
193 * Ensure all connections make use of new tokens. Discard incorrectly-cached
197 afs_ResetUserConns(struct unixuser *auser)
201 struct sa_conn_vector *tcv;
203 AFS_STATCNT(afs_ResetUserConns);
204 ObtainReadLock(&afs_xsrvAddr);
205 ObtainWriteLock(&afs_xconn, 98);
207 for (i = 0; i < NSERVERS; i++) {
208 for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
209 for (tcv = sa->conns; tcv; tcv = tcv->next) {
210 if (tcv->user == auser) {
211 for(j = 0; j < CVEC_LEN; ++j) {
212 (tcv->cvec[j]).forceConnectFS = 1;
219 ReleaseWriteLock(&afs_xconn);
220 ReleaseReadLock(&afs_xsrvAddr);
221 afs_ResetAccessCache(auser->uid, 1);
222 auser->states &= ~UNeedsReset;
223 } /*afs_ResetUserConns */
224 #endif /* !AFS_PAG_MANAGER */
228 afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
233 AFS_STATCNT(afs_FindUser);
235 ObtainWriteLock(&afs_xuser, 99);
236 for (tu = afs_users[i]; tu; tu = tu->next) {
237 if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
239 ReleaseWriteLock(&afs_xuser);
240 afs_LockUser(tu, locktype, 365);
244 ReleaseWriteLock(&afs_xuser);
250 /*------------------------------------------------------------------------
251 * EXPORTED afs_ComputePAGStats
254 * Compute a set of stats concerning PAGs used by this machine.
263 * The results are put in the structure responsible for keeping
264 * detailed CM stats. Note: entries corresponding to a single PAG
265 * will appear on the identical hash chain, so sweeping the chain
266 * will find all entries related to a single PAG.
270 *------------------------------------------------------------------------*/
273 afs_ComputePAGStats(void)
275 struct unixuser *currPAGP; /*Ptr to curr PAG */
276 struct unixuser *cmpPAGP; /*Ptr to PAG being compared */
277 struct afs_stats_AuthentInfo *authP; /*Ptr to stats area */
278 int curr_Record; /*Curr record */
279 int currChain; /*Curr hash chain */
280 int currChainLen; /*Length of curr hash chain */
281 int currPAGRecords; /*# records in curr PAG */
284 * Lock out everyone else from scribbling on the PAG entries.
286 ObtainReadLock(&afs_xuser);
289 * Initialize the tallies, then sweep through each hash chain. We
290 * can't bzero the structure, since some fields are cumulative over
294 authP = &(afs_stats_cmfullperf.authent);
295 authP->curr_PAGs = 0;
296 authP->curr_Records = 0;
297 authP->curr_AuthRecords = 0;
298 authP->curr_UnauthRecords = 0;
299 authP->curr_MaxRecordsInPAG = 0;
300 authP->curr_LongestChain = 0;
302 for (currChain = 0; currChain < NUSERS; currChain++) {
304 for (currPAGP = afs_users[currChain]; currPAGP;
305 currPAGP = currPAGP->next) {
307 * Bump the number of records on this current chain, along with
308 * the total number of records in existence.
313 * We've found a previously-uncounted PAG. If it's been deleted
314 * but just not garbage-collected yet, we step over it.
316 if (!(currPAGP->states & UHasTokens))
320 * If this PAG record has already been ``counted', namely
321 * associated with a PAG and tallied, clear its bit and move on.
323 (authP->curr_Records)++;
324 if (currPAGP->states & UPAGCounted) {
325 currPAGP->states &= ~UPAGCounted;
331 /*We've counted this one already */
333 * Jot initial info down, then sweep down the rest of the hash
334 * chain, looking for matching PAG entries. Note: to properly
335 * ``count'' the current record, we first compare it to itself
336 * in the following loop.
338 (authP->curr_PAGs)++;
341 for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
342 if (currPAGP->uid == cmpPAGP->uid) {
344 * The records belong to the same PAG. First, mark the
345 * new record as ``counted'' and bump the PAG size.
346 * Then, record the state of its ticket, if any.
348 cmpPAGP->states |= UPAGCounted;
350 if ((cmpPAGP->states & UHasTokens)
351 && !(cmpPAGP->states & UTokensBad))
352 (authP->curr_AuthRecords)++;
354 (authP->curr_UnauthRecords)++;
355 } /*Records belong to same PAG */
356 } /*Compare to rest of PAG records in chain */
359 * In the above comparisons, the current PAG record has been
360 * marked as counted. Erase this mark before moving on.
362 currPAGP->states &= ~UPAGCounted;
365 * We've compared our current PAG record with all remaining
366 * PAG records in the hash chain. Update our tallies, and
367 * perhaps even our lifetime high water marks. After that,
368 * remove our search mark and advance to the next comparison
371 if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
372 authP->curr_MaxRecordsInPAG = currPAGRecords;
373 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
374 authP->HWM_MaxRecordsInPAG = currPAGRecords;
376 } /*Sweep a hash chain */
379 * If the chain we just finished zipping through is the longest we've
380 * seen yet, remember this fact before advancing to the next chain.
382 if (currChainLen > authP->curr_LongestChain) {
383 authP->curr_LongestChain = currChainLen;
384 if (currChainLen > authP->HWM_LongestChain)
385 authP->HWM_LongestChain = currChainLen;
388 } /*For each hash chain in afs_user */
391 * Now that we've counted everything up, we can consider all-time
394 if (authP->curr_PAGs > authP->HWM_PAGs)
395 authP->HWM_PAGs = authP->curr_PAGs;
396 if (authP->curr_Records > authP->HWM_Records)
397 authP->HWM_Records = authP->curr_Records;
400 * People are free to manipulate the PAG structures now.
402 ReleaseReadLock(&afs_xuser);
404 } /*afs_ComputePAGStats */
408 afs_GetUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
410 struct unixuser *tu, *pu = 0;
412 afs_int32 RmtUser = 0;
414 AFS_STATCNT(afs_GetUser);
416 ObtainWriteLock(&afs_xuser, 104);
417 for (tu = afs_users[i]; tu; tu = tu->next) {
418 if (tu->uid == auid) {
425 if (tu->cell == -1 && acell != -1) {
426 /* Here we setup the real cell for the client */
430 } else if (tu->cell == acell || acell == -1) {
436 tu = afs_osi_Alloc(sizeof(struct unixuser));
437 osi_Assert(tu != NULL);
439 afs_stats_cmfullperf.authent.PAGCreations++;
440 #endif /* AFS_NOSTATS */
441 memset(tu, 0, sizeof(struct unixuser));
442 AFS_RWLOCK_INIT(&tu->lock, "unixuser lock");
443 tu->next = afs_users[i];
447 * This is for the case where an additional unixuser struct is
448 * created because the remote client is accessing a different cell;
449 * we simply rerecord relevant information from the original
452 if (pu && pu->exporter) {
453 tu->exporter = pu->exporter;
454 (void)EXP_HOLD(tu->exporter);
459 tu->viceId = UNDEFVID;
461 tu->tokenTime = osi_Time();
464 ReleaseWriteLock(&afs_xuser);
465 afs_LockUser(tu, locktype, 364);
471 afs_LockUser(struct unixuser *au, afs_int32 locktype,
472 unsigned int src_indicator)
476 ObtainReadLock(&au->lock);
479 ObtainWriteLock(&au->lock, src_indicator);
482 ObtainSharedLock(&au->lock, src_indicator);
491 afs_PutUser(struct unixuser *au, afs_int32 locktype)
493 AFS_STATCNT(afs_PutUser);
497 ReleaseReadLock(&au->lock);
500 ReleaseWriteLock(&au->lock);
503 ReleaseSharedLock(&au->lock);
515 * Set the primary flag on a unixuser structure, ensuring that exactly one
516 * dude has the flag set at any time for a particular unix uid.
519 afs_SetPrimary(struct unixuser *au, int aflag)
525 AFS_STATCNT(afs_SetPrimary);
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;
548 /* setting au to be primary */
550 pu->states &= ~UPrimary;
551 au->states |= UPrimary;
552 } else if (aflag == 0) {
553 /* we don't know if we're supposed to be primary or not */
554 if (!pu || au == pu) {
555 au->states |= UPrimary;
557 au->states &= ~UPrimary;
559 ReleaseWriteLock(&afs_xuser);
561 } /*afs_SetPrimary */
564 afs_NotifyUser(struct unixuser *auser, int event)
566 #ifdef AFS_DARWIN_ENV
567 darwin_notify_perms(auser, event);
572 * Mark all of the unixuser records held for a particular PAG as
576 * PAG to expire records for
579 afs_MarkUserExpired(afs_int32 pag)
585 ObtainWriteLock(&afs_xuser, 9);
586 for (tu = afs_users[i]; tu; tu = tu->next) {
587 if (tu->uid == pag) {
588 tu->states &= ~UHasTokens;
592 ReleaseWriteLock(&afs_xuser);
599 * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
600 * process in the system.
601 * If the specified process uses a PAG, clear that PAG's temporary
606 * This variable keeps track of the number of UID-base
607 * tokens in the afs_users table. When it's zero
608 * the per process loop in GCPAGs doesn't have to
609 * check processes without pags against the afs_users table.
611 static afs_int32 afs_GCPAGs_UIDBaseTokenCount = 0;
614 * These variables keep track of the number of times
615 * afs_GCPAGs_perproc_func() is called. If it is not called at all when
616 * walking the process table, there is something wrong and we should not
617 * prematurely expire any tokens.
619 static size_t afs_GCPAGs_perproc_count = 0;
620 static size_t afs_GCPAGs_cred_count = 0;
623 * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
625 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_STRUCT_HAS_CRED) || defined(HAVE_LINUX_RCU_READ_LOCK))
627 afs_GCPAGs_perproc_func(afs_proc_t * pproc)
629 afs_int32 pag, hash, uid;
630 const afs_ucred_t *pcred;
632 afs_GCPAGs_perproc_count++;
634 pcred = afs_osi_proc2cred(pproc);
638 afs_GCPAGs_cred_count++;
640 pag = PagInCred(pcred);
641 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_LINUX22_ENV)
642 uid = (pag != NOPAG ? pag : afs_cr_uid(pcred));
643 #elif defined(AFS_SUN510_ENV)
644 uid = (pag != NOPAG ? pag : crgetruid(pcred));
646 uid = (pag != NOPAG ? pag : afs_cr_ruid(pcred));
650 /* if this token is PAG based, or it's UID based and
651 * UID-based tokens exist */
652 if ((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
653 /* find the entries for this uid in all cells and clear the not
654 * referenced flag. Can't use afs_FindUser, because it just returns
655 * the specific cell asked for, or the first one found.
658 for (pu = afs_users[hash]; pu; pu = pu->next) {
659 if (pu->uid == uid) {
660 if (pu->states & TMP_UPAGNotReferenced) {
661 /* clear the 'deleteme' flag for this entry */
662 pu->states &= ~TMP_UPAGNotReferenced;
664 /* This is a uid based token that hadn't
665 * previously been cleared, so decrement the
666 * outstanding uid based token count */
667 afs_GCPAGs_UIDBaseTokenCount--;
677 * Go through the process table, find all unused PAGs
678 * and cause them to be deleted during the next GC.
680 * returns the number of PAGs marked for deletion
682 * On AIX we free PAGs when the last accessing process exits,
683 * so this routine is not needed.
685 * In AFS WebSecure, we explicitly call unlog when we remove
686 * an entry in the login cache, so this routine is not needed.
690 afs_GCPAGs(afs_int32 * ReleasedCount)
695 if (afs_gcpags != AFS_GCPAGS_OK) {
701 /* first, loop through afs_users, setting the temporary 'deleteme' flag */
702 ObtainWriteLock(&afs_xuser, 419);
703 afs_GCPAGs_UIDBaseTokenCount = 0;
704 for (i = 0; i < NUSERS; i++) {
705 for (pu = afs_users[i]; pu; pu = pu->next) {
706 pu->states |= TMP_UPAGNotReferenced;
707 if (((pu->uid >> 24) & 0xff) != 'A') {
708 /* this is a uid-based token, */
709 /* increment the count */
710 afs_GCPAGs_UIDBaseTokenCount++;
715 /* Now, iterate through the systems process table,
716 * for each process, mark it's PAGs (if any) in use.
717 * i.e. clear the temporary deleteme flag.
719 afs_GCPAGs_perproc_count = 0;
720 afs_GCPAGs_cred_count = 0;
722 afs_osi_TraverseProcTable();
724 /* If there is an internal problem and afs_GCPAGs_perproc_func()
725 * does not get called, disable gcpags so that we do not
726 * accidentally expire all the tokens in the system.
728 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
729 afs_gcpags = AFS_GCPAGS_EPROCWALK;
732 if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
733 afs_gcpags = AFS_GCPAGS_ECREDWALK;
736 /* Now, go through afs_users again, any that aren't in use
737 * (temp deleteme flag still set) will be marked for later deletion,
738 * by setting their expire times to 0.
740 for (i = 0; i < NUSERS; i++) {
741 for (pu = afs_users[i]; pu; pu = pu->next) {
742 if (pu->states & TMP_UPAGNotReferenced) {
744 /* clear the temp flag */
745 pu->states &= ~TMP_UPAGNotReferenced;
747 /* Is this entry on behalf of a 'remote' user ?
748 * i.e. nfs translator, etc.
750 if (!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
751 /* make afs_GCUserData remove this entry */
752 pu->states &= ~UHasTokens;
755 (*ReleasedCount)++; /* remember how many we marked (info only) */
761 ReleaseWriteLock(&afs_xuser);
766 #endif /* AFS_GCPAGS */