0b63550c0df59456298663764aca14065326b300
[openafs.git] / src / afs / afs_user.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 /*
11  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16
17 #include "afs/stds.h"
18 #include "afs/sysincludes.h"    /* Standard vendor system headers */
19
20 #if !defined(UKERNEL)
21 #if !defined(AFS_LINUX20_ENV)
22 #include <net/if.h>
23 #endif
24 #include <netinet/in.h>
25
26 #ifdef AFS_SGI62_ENV
27 #include "h/hashing.h"
28 #endif
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) */
33
34 #include "afsincludes.h"        /* Afs-based standard headers */
35 #include "afs/afs_stats.h"      /* afs statistics */
36
37 #if     defined(AFS_SUN5_ENV)
38 #include <inet/led.h>
39 #include <inet/common.h>
40 #include <netinet/ip6.h>
41 #include <inet/ip.h>
42 #endif
43
44 #include "afs/afs_axscache.h"
45
46 /* Exported variables */
47 afs_rwlock_t afs_xuser;
48 struct unixuser *afs_users[NUSERS];
49
50
51 #ifndef AFS_PAG_MANAGER
52 /* Forward declarations */
53 void afs_ResetAccessCache(afs_int32 uid, int alock);
54 #endif /* !AFS_PAG_MANAGER */
55
56
57 /* Called from afs_Daemon to garbage collect unixusers no longer using system,
58  * and their conns.  The aforce parameter tells the function to flush all
59  * *unauthenticated* conns, no matter what their expiration time; it exists
60  * because after we choose our final rx epoch, we want to stop using calls with
61  * other epochs as soon as possible (old file servers act bizarrely when they
62  * see epoch changes).
63  */
64 void
65 afs_GCUserData(int aforce)
66 {
67     struct unixuser *tu, **lu, *nu;
68     int i;
69     afs_int32 now, delFlag;
70
71     AFS_STATCNT(afs_GCUserData);
72     /* Obtain locks in valid order */
73     ObtainWriteLock(&afs_xuser, 95);
74 #ifndef AFS_PAG_MANAGER
75     ObtainReadLock(&afs_xserver);
76     ObtainWriteLock(&afs_xconn, 96);
77 #endif
78     now = osi_Time();
79     for (i = 0; i < NUSERS; i++) {
80         for (lu = &afs_users[i], tu = *lu; tu; tu = nu) {
81             delFlag = 0;        /* should we delete this dude? */
82             /* Don't garbage collect users in use now (refCount) */
83             if (tu->refCount == 0) {
84                 if (tu->tokens) {
85                     /* Need to walk the token stack, and dispose of
86                      * all expired tokens */
87                     afs_DiscardExpiredTokens(&tu->tokens, now);
88                     if (!afs_HasUsableTokens(tu->tokens, now))
89                         delFlag = 1;
90                 } else {
91                     if (aforce || (tu->tokenTime < now - NOTOKTIMEOUT))
92                         delFlag = 1;
93                 }
94             }
95             nu = tu->next;
96             if (delFlag) {
97                 *lu = tu->next;
98 #ifndef AFS_PAG_MANAGER
99                 afs_ReleaseConnsUser(tu);
100 #endif
101                 afs_FreeTokens(&tu->tokens);
102
103                 if (tu->exporter)
104                     EXP_RELE(tu->exporter);
105                 afs_osi_Free(tu, sizeof(struct unixuser));
106             } else {
107                 lu = &tu->next;
108             }
109         }
110     }
111 #ifndef AFS_PAG_MANAGER
112     ReleaseWriteLock(&afs_xconn);
113 #endif
114 #ifndef AFS_PAG_MANAGER
115     ReleaseReadLock(&afs_xserver);
116 #endif
117     ReleaseWriteLock(&afs_xuser);
118
119 }                               /*afs_GCUserData */
120
121 static struct unixuser *
122 afs_FindUserNoLock(afs_int32 auid, afs_int32 acell)
123 {
124     struct unixuser *tu;
125     afs_int32 i;
126
127     AFS_STATCNT(afs_FindUser);
128     i = UHash(auid);
129     for (tu = afs_users[i]; tu; tu = tu->next) {
130         if (tu->uid == auid && ((tu->cell == acell) || (acell == -1))) {
131             tu->refCount++;
132             return tu;
133         }
134     }
135     return NULL;
136
137 }
138
139 #ifndef AFS_PAG_MANAGER
140 /*
141  * Check for unixusers who encountered bad tokens, and reset the access
142  * cache for these guys.  Can't do this when token expiration detected,
143  * since too many locks are set then.
144  */
145 void
146 afs_CheckTokenCache(void)
147 {
148     int i;
149     struct unixuser *tu;
150     afs_int32 now;
151     struct vcache *tvc;
152     struct axscache *tofreelist;
153     int do_scan = 0;
154
155     AFS_STATCNT(afs_CheckCacheResets);
156     ObtainReadLock(&afs_xvcache);
157     ObtainReadLock(&afs_xuser);
158     now = osi_Time();
159     for (i = 0; i < NUSERS; i++) {
160         for (tu = afs_users[i]; tu; tu = tu->next) {
161             /*
162              * If tokens are still good and user has Kerberos tickets,
163              * check expiration
164              */
165             if ((tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
166                 if (!afs_HasUsableTokens(tu->tokens, now)) {
167                     /*
168                      * This token has expired, warn users and reset access
169                      * cache.
170                      */
171                     tu->states |= (UTokensBad | UNeedsReset);
172                 }
173             }
174             if (tu->states & UNeedsReset)
175                 do_scan = 1;
176         }
177     }
178     /* Skip the potentially expensive scan if nothing to do */
179     if (!do_scan)
180         goto done;
181
182     tofreelist = NULL;
183     for (i = 0; i < VCSIZE; i++) {
184         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
185             /* really should do this under cache write lock, but that.
186              * is hard to under locking hierarchy */
187             if (tvc->Access) {
188                 struct axscache **ac, **nac;
189
190                 for ( ac = &tvc->Access; *ac;)  {
191                     nac = &(*ac)->next;
192                     tu = afs_FindUserNoLock((*ac)->uid, tvc->f.fid.Cell);
193                     if (tu == NULL || (tu->states & UNeedsReset)) {
194                         struct axscache *tmp;
195                         tmp = *ac;
196                         *ac = *nac;
197                         tmp->next = tofreelist;
198                         tofreelist = tmp;
199                     } else
200                         ac = nac;
201                     if (tu != NULL)
202                         tu->refCount--;
203                 }
204             }
205         }
206     }
207     afs_FreeAllAxs(&tofreelist);
208     for (i = 0; i < NUSERS; i++) {
209         for (tu = afs_users[i]; tu; tu = tu->next) {
210             if (tu->states & UNeedsReset)
211                 tu->states &= ~UNeedsReset;
212         }
213     }
214
215 done:
216     ReleaseReadLock(&afs_xuser);
217     ReleaseReadLock(&afs_xvcache);
218 }                               /*afs_CheckTokenCache */
219
220
221 void
222 afs_ResetAccessCache(afs_int32 uid, int alock)
223 {
224     int i;
225     struct vcache *tvc;
226     struct axscache *ac;
227
228     AFS_STATCNT(afs_ResetAccessCache);
229     if (alock)
230         ObtainReadLock(&afs_xvcache);
231     for (i = 0; i < VCSIZE; i++) {
232         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
233             /* really should do this under cache write lock, but that.
234              * is hard to under locking hierarchy */
235             if (tvc->Access && (ac = afs_FindAxs(tvc->Access, uid))) {
236                 afs_RemoveAxs(&tvc->Access, ac);
237             }
238         }
239     }
240     if (alock)
241         ReleaseReadLock(&afs_xvcache);
242
243 }                               /*afs_ResetAccessCache */
244
245
246 /*
247  * Ensure all connections make use of new tokens.  Discard incorrectly-cached
248  * access info.
249  */
250 void
251 afs_ResetUserConns(struct unixuser *auser)
252 {
253     int i, j;
254     struct srvAddr *sa;
255     struct sa_conn_vector *tcv;
256
257     AFS_STATCNT(afs_ResetUserConns);
258     ObtainReadLock(&afs_xsrvAddr);
259     ObtainWriteLock(&afs_xconn, 98);
260
261     for (i = 0; i < NSERVERS; i++) {
262         for (sa = afs_srvAddrs[i]; sa; sa = sa->next_bkt) {
263             for (tcv = sa->conns; tcv; tcv = tcv->next) {
264                 if (tcv->user == auser) {
265                     for(j = 0; j < CVEC_LEN; ++j) {
266                         (tcv->cvec[j]).forceConnectFS = 1;
267                     }
268                 }
269             }
270         }
271     }
272
273     ReleaseWriteLock(&afs_xconn);
274     ReleaseReadLock(&afs_xsrvAddr);
275     afs_ResetAccessCache(auser->uid, 1);
276     auser->states &= ~UNeedsReset;
277 }                               /*afs_ResetUserConns */
278 #endif /* !AFS_PAG_MANAGER */
279
280
281 struct unixuser *
282 afs_FindUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
283 {
284     struct unixuser *tu;
285
286     ObtainWriteLock(&afs_xuser, 99);
287     tu = afs_FindUserNoLock(auid, acell);
288     ReleaseWriteLock(&afs_xuser);
289     if (tu)
290         afs_LockUser(tu, locktype, 365);
291     return tu;
292 }                               /*afs_FindUser */
293
294
295 /*------------------------------------------------------------------------
296  * EXPORTED afs_ComputePAGStats
297  *
298  * Description:
299  *      Compute a set of stats concerning PAGs used by this machine.
300  *
301  * Arguments:
302  *      None.
303  *
304  * Returns:
305  *      Nothing.
306  *
307  * Environment:
308  *      The results are put in the structure responsible for keeping
309  *      detailed CM stats.  Note: entries corresponding to a single PAG
310  *      will appear on the identical hash chain, so sweeping the chain
311  *      will find all entries related to a single PAG.
312  *
313  * Side Effects:
314  *      As advertised.
315  *------------------------------------------------------------------------*/
316
317 void
318 afs_ComputePAGStats(void)
319 {
320     struct unixuser *currPAGP;  /*Ptr to curr PAG */
321     struct unixuser *cmpPAGP;   /*Ptr to PAG being compared */
322     struct afs_stats_AuthentInfo *authP;        /*Ptr to stats area */
323     int curr_Record;            /*Curr record */
324     int currChain;              /*Curr hash chain */
325     int currChainLen;           /*Length of curr hash chain */
326     int currPAGRecords;         /*# records in curr PAG */
327
328     /*
329      * Lock out everyone else from scribbling on the PAG entries.
330      */
331     ObtainReadLock(&afs_xuser);
332
333     /*
334      * Initialize the tallies, then sweep through each hash chain.  We
335      * can't bzero the structure, since some fields are cumulative over
336      * the CM's lifetime.
337      */
338     curr_Record = 0;
339     authP = &(afs_stats_cmfullperf.authent);
340     authP->curr_PAGs = 0;
341     authP->curr_Records = 0;
342     authP->curr_AuthRecords = 0;
343     authP->curr_UnauthRecords = 0;
344     authP->curr_MaxRecordsInPAG = 0;
345     authP->curr_LongestChain = 0;
346
347     for (currChain = 0; currChain < NUSERS; currChain++) {
348         currChainLen = 0;
349         for (currPAGP = afs_users[currChain]; currPAGP;
350              currPAGP = currPAGP->next) {
351             /*
352              * Bump the number of records on this current chain, along with
353              * the total number of records in existence.
354              */
355             currChainLen++;
356             curr_Record++;
357             /*
358              * We've found a previously-uncounted PAG.  If it's been deleted
359              * but just not garbage-collected yet, we step over it.
360              */
361             if (!(currPAGP->states & UHasTokens))
362                 continue;
363
364             /*
365              * If this PAG record has already been ``counted', namely
366              * associated with a PAG and tallied, clear its bit and move on.
367              */
368             (authP->curr_Records)++;
369             if (currPAGP->states & UPAGCounted) {
370                 currPAGP->states &= ~UPAGCounted;
371                 continue;
372             }
373
374
375
376             /*We've counted this one already */
377             /*
378              * Jot initial info down, then sweep down the rest of the hash
379              * chain, looking for matching PAG entries.  Note: to properly
380              * ``count'' the current record, we first compare it to itself
381              * in the following loop.
382              */
383             (authP->curr_PAGs)++;
384             currPAGRecords = 0;
385
386             for (cmpPAGP = currPAGP; cmpPAGP; cmpPAGP = cmpPAGP->next) {
387                 if (currPAGP->uid == cmpPAGP->uid) {
388                     /*
389                      * The records belong to the same PAG.  First, mark the
390                      * new record as ``counted'' and bump the PAG size.
391                      * Then, record the state of its ticket, if any.
392                      */
393                     cmpPAGP->states |= UPAGCounted;
394                     currPAGRecords++;
395                     if ((cmpPAGP->states & UHasTokens)
396                         && !(cmpPAGP->states & UTokensBad))
397                         (authP->curr_AuthRecords)++;
398                     else
399                         (authP->curr_UnauthRecords)++;
400                 }               /*Records belong to same PAG */
401             }                   /*Compare to rest of PAG records in chain */
402
403             /*
404              * In the above comparisons, the current PAG record has been
405              * marked as counted.  Erase this mark before moving on.
406              */
407             currPAGP->states &= ~UPAGCounted;
408
409             /*
410              * We've compared our current PAG record with all remaining
411              * PAG records in the hash chain.  Update our tallies, and
412              * perhaps even our lifetime high water marks.  After that,
413              * remove our search mark and advance to the next comparison
414              * pair.
415              */
416             if (currPAGRecords > authP->curr_MaxRecordsInPAG) {
417                 authP->curr_MaxRecordsInPAG = currPAGRecords;
418                 if (currPAGRecords > authP->HWM_MaxRecordsInPAG)
419                     authP->HWM_MaxRecordsInPAG = currPAGRecords;
420             }
421         }                       /*Sweep a hash chain */
422
423         /*
424          * If the chain we just finished zipping through is the longest we've
425          * seen yet, remember this fact before advancing to the next chain.
426          */
427         if (currChainLen > authP->curr_LongestChain) {
428             authP->curr_LongestChain = currChainLen;
429             if (currChainLen > authP->HWM_LongestChain)
430                 authP->HWM_LongestChain = currChainLen;
431         }
432
433     }                           /*For each hash chain in afs_user */
434
435     /*
436      * Now that we've counted everything up, we can consider all-time
437      * numbers.
438      */
439     if (authP->curr_PAGs > authP->HWM_PAGs)
440         authP->HWM_PAGs = authP->curr_PAGs;
441     if (authP->curr_Records > authP->HWM_Records)
442         authP->HWM_Records = authP->curr_Records;
443
444     /*
445      * People are free to manipulate the PAG structures now.
446      */
447     ReleaseReadLock(&afs_xuser);
448
449 }                               /*afs_ComputePAGStats */
450
451
452 struct unixuser *
453 afs_GetUser(afs_int32 auid, afs_int32 acell, afs_int32 locktype)
454 {
455     struct unixuser *tu, *pu = 0;
456     afs_int32 i;
457     afs_int32 RmtUser = 0;
458
459     AFS_STATCNT(afs_GetUser);
460     i = UHash(auid);
461     ObtainWriteLock(&afs_xuser, 104);
462     for (tu = afs_users[i]; tu; tu = tu->next) {
463         if (tu->uid == auid) {
464             RmtUser = 0;
465             pu = NULL;
466             if (tu->exporter) {
467                 RmtUser = 1;
468                 pu = tu;
469             }
470             if (tu->cell == -1 && acell != -1) {
471                 /* Here we setup the real cell for the client */
472                 tu->cell = acell;
473                 tu->refCount++;
474                 goto done;
475             } else if (tu->cell == acell || acell == -1) {
476                 tu->refCount++;
477                 goto done;
478             }
479         }
480     }
481     tu = afs_osi_Alloc(sizeof(struct unixuser));
482     osi_Assert(tu != NULL);
483 #ifndef AFS_NOSTATS
484     afs_stats_cmfullperf.authent.PAGCreations++;
485 #endif /* AFS_NOSTATS */
486     memset(tu, 0, sizeof(struct unixuser));
487     AFS_RWLOCK_INIT(&tu->lock, "unixuser lock");
488     tu->next = afs_users[i];
489     afs_users[i] = tu;
490     if (RmtUser) {
491         /*
492          * This is for the case where an additional unixuser struct is
493          * created because the remote client is accessing a different cell;
494          * we simply rerecord relevant information from the original
495          * structure
496          */
497         if (pu && pu->exporter) {
498             tu->exporter = pu->exporter;
499             (void)EXP_HOLD(tu->exporter);
500         }
501     }
502     tu->uid = auid;
503     tu->cell = acell;
504     tu->viceId = UNDEFVID;
505     tu->refCount = 1;
506     tu->tokenTime = osi_Time();
507
508  done:
509     ReleaseWriteLock(&afs_xuser);
510     afs_LockUser(tu, locktype, 364);
511     return tu;
512
513 }                               /*afs_GetUser */
514
515 void
516 afs_LockUser(struct unixuser *au, afs_int32 locktype,
517              unsigned int src_indicator)
518 {
519     switch (locktype) {
520     case READ_LOCK:
521         ObtainReadLock(&au->lock);
522         break;
523     case WRITE_LOCK:
524         ObtainWriteLock(&au->lock, src_indicator);
525         break;
526     case SHARED_LOCK:
527         ObtainSharedLock(&au->lock, src_indicator);
528         break;
529     default:
530         /* noop */
531         break;
532     }
533 }
534
535 void
536 afs_PutUser(struct unixuser *au, afs_int32 locktype)
537 {
538     AFS_STATCNT(afs_PutUser);
539
540     switch (locktype) {
541     case READ_LOCK:
542         ReleaseReadLock(&au->lock);
543         break;
544     case WRITE_LOCK:
545         ReleaseWriteLock(&au->lock);
546         break;
547     case SHARED_LOCK:
548         ReleaseSharedLock(&au->lock);
549         break;
550     default:
551         /* noop */
552         break;
553     }
554
555     --au->refCount;
556 }                               /*afs_PutUser */
557
558
559 /*
560  * Set the primary flag on a unixuser structure, ensuring that exactly one
561  * dude has the flag set at any time for a particular unix uid.
562  */
563 void
564 afs_SetPrimary(struct unixuser *au, int aflag)
565 {
566     struct unixuser *tu;
567     int i;
568     struct unixuser *pu;
569
570     AFS_STATCNT(afs_SetPrimary);
571     i = UHash(au->uid);
572     pu = NULL;
573     ObtainWriteLock(&afs_xuser, 105);
574     /*
575      * See if anyone is this uid's primary cell yet; recording in pu the
576      * corresponding user
577      */
578     for (tu = afs_users[i]; tu; tu = tu->next) {
579         if (tu->uid == au->uid && (tu->states & UPrimary)) {
580             pu = tu;
581         }
582     }
583     if (pu && !(pu->states & UHasTokens)) {
584         /*
585          * Primary user has unlogged, don't treat him as primary any longer;
586          * note that we want to treat him as primary until now, so that
587          * people see a primary identity until now.
588          */
589         pu->states &= ~UPrimary;
590         pu = NULL;
591     }
592     if (aflag == 1) {
593         /* setting au to be primary */
594         if (pu)
595             pu->states &= ~UPrimary;
596         au->states |= UPrimary;
597     } else if (aflag == 0) {
598         /* we don't know if we're supposed to be primary or not */
599         if (!pu || au == pu) {
600             au->states |= UPrimary;
601         } else
602             au->states &= ~UPrimary;
603     }
604     ReleaseWriteLock(&afs_xuser);
605
606 }                               /*afs_SetPrimary */
607
608 void
609 afs_NotifyUser(struct unixuser *auser, int event)
610 {
611 #ifdef AFS_DARWIN_ENV
612     darwin_notify_perms(auser, event);
613 #endif
614 }
615
616 /**
617  * Mark all of the unixuser records held for a particular PAG as
618  * expired
619  *
620  * @param[in] pag
621  *      PAG to expire records for
622  */
623 void
624 afs_MarkUserExpired(afs_int32 pag)
625 {
626     afs_int32 i;
627     struct unixuser *tu;
628
629     i = UHash(pag);
630     ObtainWriteLock(&afs_xuser, 9);
631     for (tu = afs_users[i]; tu; tu = tu->next) {
632         if (tu->uid == pag) {
633             tu->states &= ~UHasTokens;
634             tu->tokenTime = 0;
635         }
636     }
637     ReleaseWriteLock(&afs_xuser);
638 }
639
640
641 #if AFS_GCPAGS
642
643 /*
644  * Called by osi_TraverseProcTable (from afs_GCPAGs) for each
645  * process in the system.
646  * If the specified process uses a PAG, clear that PAG's temporary
647  * 'deleteme' flag.
648  */
649
650 /*
651  * This variable keeps track of the number of UID-base
652  * tokens in the afs_users table. When it's zero
653  * the per process loop in GCPAGs doesn't have to
654  * check processes without pags against the afs_users table.
655  */
656 static afs_int32 afs_GCPAGs_UIDBaseTokenCount = 0;
657
658 /*
659  * These variables keep track of the number of times
660  * afs_GCPAGs_perproc_func() is called.  If it is not called at all when
661  * walking the process table, there is something wrong and we should not
662  * prematurely expire any tokens.
663  */
664 static size_t afs_GCPAGs_perproc_count = 0;
665 static size_t afs_GCPAGs_cred_count = 0;
666
667 /*
668  * LOCKS: afs_GCPAGs_perproc_func requires write lock on afs_xuser
669  */
670 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_STRUCT_HAS_CRED) || defined(HAVE_LINUX_RCU_READ_LOCK))
671 void
672 afs_GCPAGs_perproc_func(afs_proc_t * pproc)
673 {
674     afs_int32 pag, hash, uid;
675     const afs_ucred_t *pcred;
676
677     afs_GCPAGs_perproc_count++;
678
679     pcred = afs_osi_proc2cred(pproc);
680     if (!pcred)
681         return;
682
683     afs_GCPAGs_cred_count++;
684
685     pag = PagInCred(pcred);
686 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_LINUX22_ENV)
687     uid = (pag != NOPAG ? pag : afs_cr_uid(pcred));
688 #elif defined(AFS_SUN510_ENV)
689     uid = (pag != NOPAG ? pag : crgetruid(pcred));
690 #else
691     uid = (pag != NOPAG ? pag : afs_cr_ruid(pcred));
692 #endif
693     hash = UHash(uid);
694
695     /* if this token is PAG based, or it's UID based and
696      * UID-based tokens exist */
697     if ((pag != NOPAG) || (afs_GCPAGs_UIDBaseTokenCount)) {
698         /* find the entries for this uid in all cells and clear the not
699          * referenced flag.  Can't use afs_FindUser, because it just returns
700          * the specific cell asked for, or the first one found.
701          */
702         struct unixuser *pu;
703         for (pu = afs_users[hash]; pu; pu = pu->next) {
704             if (pu->uid == uid) {
705                 if (pu->states & TMP_UPAGNotReferenced) {
706                     /* clear the 'deleteme' flag for this entry */
707                     pu->states &= ~TMP_UPAGNotReferenced;
708                     if (pag == NOPAG) {
709                         /* This is a uid based token that hadn't
710                          * previously been cleared, so decrement the
711                          * outstanding uid based token count */
712                         afs_GCPAGs_UIDBaseTokenCount--;
713                     }
714                 }
715             }
716         }
717     }
718 }
719 #endif
720
721 /*
722  * Go through the process table, find all unused PAGs
723  * and cause them to be deleted during the next GC.
724  *
725  * returns the number of PAGs marked for deletion
726  *
727  * On AIX we free PAGs when the last accessing process exits,
728  * so this routine is not needed.
729  *
730  * In AFS WebSecure, we explicitly call unlog when we remove
731  * an entry in the login cache, so this routine is not needed.
732  */
733
734 afs_int32
735 afs_GCPAGs(afs_int32 * ReleasedCount)
736 {
737     struct unixuser *pu;
738     int i;
739
740     if (afs_gcpags != AFS_GCPAGS_OK) {
741         return 0;
742     }
743
744     *ReleasedCount = 0;
745
746     /* first, loop through afs_users, setting the temporary 'deleteme' flag */
747     ObtainWriteLock(&afs_xuser, 419);
748     afs_GCPAGs_UIDBaseTokenCount = 0;
749     for (i = 0; i < NUSERS; i++) {
750         for (pu = afs_users[i]; pu; pu = pu->next) {
751             pu->states |= TMP_UPAGNotReferenced;
752             if (((pu->uid >> 24) & 0xff) != 'A') {
753                 /* this is a uid-based token, */
754                 /* increment the count */
755                 afs_GCPAGs_UIDBaseTokenCount++;
756             }
757         }
758     }
759
760     /* Now, iterate through the systems process table,
761      * for each process, mark it's PAGs (if any) in use.
762      * i.e. clear the temporary deleteme flag.
763      */
764     afs_GCPAGs_perproc_count = 0;
765     afs_GCPAGs_cred_count = 0;
766
767     afs_osi_TraverseProcTable();
768
769     /* If there is an internal problem and afs_GCPAGs_perproc_func()
770      * does not get called, disable gcpags so that we do not
771      * accidentally expire all the tokens in the system.
772      */
773     if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_perproc_count) {
774         afs_gcpags = AFS_GCPAGS_EPROCWALK;
775     }
776
777     if (afs_gcpags == AFS_GCPAGS_OK && !afs_GCPAGs_cred_count) {
778         afs_gcpags = AFS_GCPAGS_ECREDWALK;
779     }
780
781     /* Now, go through afs_users again, any that aren't in use
782      * (temp deleteme flag still set) will be marked for later deletion,
783      * by setting their expire times to 0.
784      */
785     for (i = 0; i < NUSERS; i++) {
786         for (pu = afs_users[i]; pu; pu = pu->next) {
787             if (pu->states & TMP_UPAGNotReferenced) {
788
789                 /* clear the temp flag */
790                 pu->states &= ~TMP_UPAGNotReferenced;
791
792                 /* Is this entry on behalf of a 'remote' user ?
793                  * i.e. nfs translator, etc.
794                  */
795                 if (!pu->exporter && afs_gcpags == AFS_GCPAGS_OK) {
796                     /* make afs_GCUserData remove this entry  */
797                     pu->states &= ~UHasTokens;
798                     pu->tokenTime = 0;
799
800                     (*ReleasedCount)++; /* remember how many we marked (info only) */
801                 }
802             }
803         }
804     }
805
806     ReleaseWriteLock(&afs_xuser);
807
808     return 0;
809 }
810
811 #endif /* AFS_GCPAGS */