irix-cleanup-20041019
[openafs.git] / src / afs / afs_osi_pag.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  * genpag
13  * getpag
14  * afs_setpag
15  * AddPag
16  * afs_InitReq
17  * afs_get_pag_from_groups
18  * afs_get_groups_from_pag
19  * PagInCred
20  */
21
22 #include <afsconfig.h>
23 #include "afs/param.h"
24
25 RCSID
26     ("$Header$");
27
28 #include "afs/sysincludes.h"    /* Standard vendor system headers */
29 #include "afsincludes.h"        /* Afs-based standard headers */
30 #include "afs/afs_stats.h"      /* statistics */
31 #include "afs/afs_cbqueue.h"
32 #include "afs/nfsclient.h"
33 #include "afs/afs_osidnlc.h"
34
35
36 /* Imported variables */
37 extern int afs_shuttingdown;
38
39 /* Exported variables */
40 afs_uint32 pag_epoch;
41 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
42 afs_uint32 pagCounter = 1;
43 #else
44 afs_uint32 pagCounter = 0;
45 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
46
47 /* Local variables */
48
49 /*
50  * Pags are implemented as follows: the set of groups whose long
51  * representation is '41XXXXXX' hex are used to represent the pags.
52  * Being a member of such a group means you are authenticated as pag
53  * XXXXXX (0x41 == 'A', for Andrew).  You are never authenticated as
54  * multiple pags at once.
55  *
56  * The function afs_InitReq takes a credential field and formats the
57  * corresponding venus request structure.  The uid field in the
58  * vrequest structure is set to the *pag* you are authenticated as, or
59  * the uid, if you aren't authenticated with a pag.
60  *
61  * The basic motivation behind pags is this: just because your unix
62  * uid is N doesn't mean that you should have the same privileges as
63  * anyone logged in on the machine as user N, since this would enable
64  * the superuser on the machine to sneak in and make use of anyone's
65  * authentication info, even that which is only accidentally left
66  * behind when someone leaves a public workstation.
67  *
68  * AFS doesn't use the unix uid for anything except
69  * a handle with which to find the actual authentication tokens
70  * anyway, so the pag is an alternative handle which is somewhat more
71  * secure (although of course not absolutely secure).
72 */
73 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
74 afs_uint32
75 genpag(void)
76 {
77     AFS_STATCNT(genpag);
78 #ifdef AFS_LINUX20_ENV
79     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
80     return (('A' << 24) + ((pag_epoch + pagCounter++) & 0xffffff));
81 #else /* AFS_LINUX20_ENV */
82     return (('A' << 24) + (pagCounter++ & 0xffffff));
83 #endif /* AFS_LINUX20_ENV */
84 }
85
86 afs_uint32
87 getpag(void)
88 {
89     AFS_STATCNT(getpag);
90 #ifdef AFS_LINUX20_ENV
91     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
92     return (('A' << 24) + ((pag_epoch + pagCounter) & 0xffffff));
93 #else
94     return (('A' << 24) + (pagCounter & 0xffffff));
95 #endif
96 }
97
98 #else
99
100 /* Web enhancement: we don't need to restrict pags to 41XXXXXX since
101  * we are not sharing the space with anyone.  So we use the full 32 bits. */
102
103 afs_uint32
104 genpag(void)
105 {
106     AFS_STATCNT(genpag);
107 #ifdef AFS_LINUX20_ENV
108     return (pag_epoch + pagCounter++);
109 #else
110     return (pagCounter++);
111 #endif /* AFS_LINUX20_ENV */
112 }
113
114 afs_uint32
115 getpag(void)
116 {
117     AFS_STATCNT(getpag);
118 #ifdef AFS_LINUX20_ENV
119     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
120     return (pag_epoch + pagCounter);
121 #else
122     return (pagCounter);
123 #endif
124 }
125 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
126
127 /* used to require 10 seconds between each setpag to guarantee that
128  * PAGs never wrap - which would be a security hole.  If we presume
129  * that in ordinary operation, the average rate of PAG allocation
130  * will not exceed one per second, the 24 bits provided will be
131  * sufficient for ~200 days.  Unfortunately, if we merely assume that,
132  * there will be an opportunity for attack.  So we must enforce it.
133  * If we need to increase the average rate of PAG allocation, we
134  * should increase the number of bits in a PAG, and/or reduce our
135  * window in which we guarantee that the PAG counter won't wrap.
136  * By permitting an average of one new PAG per second, new PAGs can
137  * be allocated VERY frequently over a short period relative to total uptime.
138  * Of course, there's only an issue here if one user stays logged (and re-
139  * activates tokens repeatedly) for that entire period.
140  */
141
142 static int afs_pag_sleepcnt = 0;
143
144 static int 
145 afs_pag_sleep(struct AFS_UCRED **acred) 
146 {
147   int rv = 0;
148   if(!afs_suser(acred)) {
149     if(osi_Time() - pag_epoch < pagCounter) {
150       rv = 1;
151     }
152   }
153
154   return rv;
155 }
156
157 static int 
158 afs_pag_wait(struct AFS_UCRED **acred)
159 {
160   if(afs_pag_sleep(acred)) {
161     if(!afs_pag_sleepcnt) {
162       printf("%s() PAG throttling triggered, pid %d... sleeping.  sleepcnt %d\n",
163              "afs_pag_wait", getpid(), afs_pag_sleepcnt);
164     }
165     
166     afs_pag_sleepcnt++;
167     
168     do {
169       /* XXX spins on EINTR */
170       afs_osi_Wait(1000, (struct afs_osi_WaitHandle *)0, 0);
171     } while(afs_pag_sleep(acred));
172     
173     afs_pag_sleepcnt--;
174   }
175
176   return 0;
177 }
178
179 int
180 #if     defined(AFS_SUN5_ENV)
181 afs_setpag(struct AFS_UCRED **credpp)
182 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
183 afs_setpag(struct proc *p, void *args, int *retval)
184 #else
185 afs_setpag(void)
186 #endif
187 {
188
189 #if     defined(AFS_SUN5_ENV)
190     struct AFS_UCRED **acred = *credpp;
191 #elif  defined(AFS_OBSD_ENV)
192     struct AFS_UCRED **acred = p->p_ucred;
193 #else
194     struct AFS_UCRED **acred = NULL;
195 #endif
196
197     int code = 0;
198
199 #if defined(AFS_SGI53_ENV) && defined(MP)
200     /* This is our first chance to get the global lock. */
201     AFS_GLOCK();
202 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
203
204     AFS_STATCNT(afs_setpag);
205
206     afs_pag_wait(acred);
207
208
209 #if     defined(AFS_SUN5_ENV)
210     code = AddPag(genpag(), credpp);
211 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
212     code = AddPag(p, genpag(), &p->p_rcred);
213 #elif   defined(AFS_AIX41_ENV)
214     {
215         struct ucred *credp;
216         struct ucred *credp0;
217
218         credp = crref();
219         credp0 = credp;
220         code = AddPag(genpag(), &credp);
221         /* If AddPag() didn't make a new cred, then free our cred ref */
222         if (credp == credp0) {
223             crfree(credp);
224         }
225     }
226 #elif   defined(AFS_HPUX110_ENV)
227     {
228         struct ucred *credp = p_cred(u.u_procp);
229         code = AddPag(genpag(), &credp);
230     }
231 #elif   defined(AFS_SGI_ENV)
232     {
233         cred_t *credp;
234         credp = OSI_GET_CURRENT_CRED();
235         code = AddPag(genpag(), &credp);
236     }
237 #elif   defined(AFS_LINUX20_ENV)
238     {
239         struct AFS_UCRED *credp = crref();
240         code = AddPag(genpag(), &credp);
241         crfree(credp);
242     }
243 #elif defined(AFS_DARWIN_ENV)
244     {
245         struct ucred *credp = crdup(p->p_cred->pc_ucred);
246         code = AddPag(p, genpag(), &credp);
247         crfree(credp);
248     }
249 #else
250     code = AddPag(genpag(), &u.u_cred);
251 #endif
252
253     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
254
255 #if defined(KERNEL_HAVE_UERROR)
256     if (!getuerror())
257         setuerror(code);
258 #endif
259
260 #if defined(AFS_SGI53_ENV) && defined(MP)
261     AFS_GUNLOCK();
262 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
263
264     return (code);
265 }
266
267 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
268 /*
269  * afs_setpag_val
270  * This function is like setpag but sets the current thread's pag id to a
271  * caller-provided value instead of calling genpag().  This implements a
272  * form of token caching since the caller can recall a particular pag value
273  * for the thread to restore tokens, rather than reauthenticating.
274  */
275 int
276 #if     defined(AFS_SUN5_ENV)
277 afs_setpag_val(struct AFS_UCRED **credpp, int pagval)
278 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
279 afs_setpag_val(struct proc *p, void *args, int *retval, int pagval)
280 #else
281 afs_setpag_val(int pagval)
282 #endif
283 {
284
285 #if     defined(AFS_SUN5_ENV)
286     struct AFS_UCRED **acred = *credp;
287 #elif  defined(AFS_OBSD_ENV)
288     struct AFS_UCRED **acred = p->p_ucred;
289 #else
290     struct AFS_UCRED **acred = NULL;
291 #endif
292
293     int code = 0;
294
295 #if defined(AFS_SGI53_ENV) && defined(MP)
296     /* This is our first chance to get the global lock. */
297     AFS_GLOCK();
298 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
299
300     AFS_STATCNT(afs_setpag);
301
302     afs_pag_wait(acred);
303
304 #if     defined(AFS_SUN5_ENV)
305     code = AddPag(pagval, credpp);
306 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
307     code = AddPag(p, pagval, &p->p_rcred);
308 #elif   defined(AFS_AIX41_ENV)
309     {
310         struct ucred *credp;
311         struct ucred *credp0;
312
313         credp = crref();
314         credp0 = credp;
315         code = AddPag(pagval, &credp);
316         /* If AddPag() didn't make a new cred, then free our cred ref */
317         if (credp == credp0) {
318             crfree(credp);
319         }
320     }
321 #elif   defined(AFS_HPUX110_ENV)
322     {
323         struct ucred *credp = p_cred(u.u_procp);
324         code = AddPag(pagval, &credp);
325     }
326 #elif   defined(AFS_SGI_ENV)
327     {
328         cred_t *credp;
329         credp = OSI_GET_CURRENT_CRED();
330         code = AddPag(pagval, &credp);
331     }
332 #elif   defined(AFS_LINUX20_ENV)
333     {
334         struct AFS_UCRED *credp = crref();
335         code = AddPag(pagval, &credp);
336         crfree(credp);
337     }
338 #elif defined(AFS_DARWIN_ENV)
339     {
340         struct ucred *credp = crdup(p->p_cred->pc_ucred);
341         code = AddPag(p, pagval, &credp);
342         crfree(credp);
343     }
344 #else
345     code = AddPag(pagval, &u.u_cred);
346 #endif
347
348     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
349 #if defined(KERNEL_HAVE_UERROR)
350     if (!getuerror())
351         setuerror(code);
352 #endif
353 #if defined(AFS_SGI53_ENV) && defined(MP)
354     AFS_GUNLOCK();
355 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
356     return (code);
357 }
358
359 int
360 afs_getpag_val()
361 {
362     int pagvalue;
363     struct AFS_UCRED *credp = u.u_cred;
364     int gidset0, gidset1;
365
366     gidset0 = credp->cr_groups[0];
367     gidset1 = credp->cr_groups[1];
368     pagvalue = afs_get_pag_from_groups(gidset0, gidset1);
369     return pagvalue;
370 }
371 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
372
373
374 /* Note - needs to be available on AIX, others can be static - rework this */
375 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
376 int
377 AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
378 #else
379 int
380 AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
381 #endif
382 {
383     afs_int32 newpag, code;
384
385     AFS_STATCNT(AddPag);
386 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
387     if ((code = setpag(p, credpp, aval, &newpag, 0)))
388 #else
389     if ((code = setpag(credpp, aval, &newpag, 0)))
390 #endif
391 #if defined(KERNEL_HAVE_UERROR)
392         return (setuerror(code), code);
393 #else
394         return (code);
395 #endif
396     return 0;
397 }
398
399
400 int
401 afs_InitReq(register struct vrequest *av, struct AFS_UCRED *acred)
402 {
403     AFS_STATCNT(afs_InitReq);
404     if (afs_shuttingdown)
405         return EIO;
406     av->uid = PagInCred(acred);
407     if (av->uid == NOPAG) {
408         /* Afs doesn't use the unix uid for anuthing except a handle
409          * with which to find the actual authentication tokens so I
410          * think it's ok to use the real uid to make setuid
411          * programs (without setpag) to work properly.
412          */
413 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
414         if (acred == NOCRED)
415             av->uid = -2;       /* XXX nobody... ? */
416         else
417             av->uid = acred->cr_uid;    /* bsd creds don't have ruid */
418 #else
419         av->uid = acred->cr_ruid;       /* default when no pag is set */
420 #endif
421     }
422     av->initd = 0;
423     return 0;
424 }
425
426
427
428 afs_uint32
429 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
430 {
431     afs_uint32 g0 = g0a;
432     afs_uint32 g1 = g1a;
433     afs_uint32 h, l, ret;
434
435     AFS_STATCNT(afs_get_pag_from_groups);
436     g0 -= 0x3f00;
437     g1 -= 0x3f00;
438     if (g0 < 0xc000 && g1 < 0xc000) {
439         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
440         h = (g0 >> 14);
441         h = (g1 >> 14) + h + h + h;
442         ret = ((h << 28) | l);
443 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
444         return ret;
445 #else
446         /* Additional testing */
447         if (((ret >> 24) & 0xff) == 'A')
448             return ret;
449         else
450             return NOPAG;
451 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
452     }
453     return NOPAG;
454 }
455
456
457 void
458 afs_get_groups_from_pag(afs_uint32 pag, gid_t * g0p, gid_t * g1p)
459 {
460     unsigned short g0, g1;
461
462
463     AFS_STATCNT(afs_get_groups_from_pag);
464 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
465     pag &= 0x7fffffff;
466 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
467     g0 = 0x3fff & (pag >> 14);
468     g1 = 0x3fff & pag;
469     g0 |= ((pag >> 28) / 3) << 14;
470     g1 |= ((pag >> 28) % 3) << 14;
471     *g0p = g0 + 0x3f00;
472     *g1p = g1 + 0x3f00;
473 }
474
475
476 afs_int32
477 PagInCred(const struct AFS_UCRED *cred)
478 {
479     afs_int32 pag;
480     gid_t g0, g1;
481
482     AFS_STATCNT(PagInCred);
483     if (cred == NULL) {
484         return NOPAG;
485     }
486 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
487     if (cred == NOCRED || cred == FSCRED) {
488         return NOPAG;
489     }
490     if (cred->cr_ngroups < 3)
491         return NOPAG;
492     /* gid is stored in cr_groups[0] */
493     g0 = cred->cr_groups[1];
494     g1 = cred->cr_groups[2];
495 #else
496 #if defined(AFS_AIX51_ENV)
497     if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
498         pag = NOPAG;
499     return pag;
500 #elif defined(AFS_AIX_ENV)
501     if (cred->cr_ngrps < 2) {
502         return NOPAG;
503     }
504 #elif defined(AFS_LINUX26_ENV)
505     if (cred->cr_group_info->ngroups < 2)
506         return NOPAG;
507 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
508     if (cred->cr_ngroups < 2)
509         return NOPAG;
510 #endif
511 #if defined(AFS_AIX51_ENV)
512     g0 = cred->cr_groupset.gs_union.un_groups[0];
513     g1 = cred->cr_groupset.gs_union.un_groups[1];
514 #elif defined(AFS_LINUX26_ENV)
515     g0 = GROUP_AT(cred->cr_group_info, 0);
516     g1 = GROUP_AT(cred->cr_group_info, 1);
517 #else
518     g0 = cred->cr_groups[0];
519     g1 = cred->cr_groups[1];
520 #endif
521 #endif
522     pag = (afs_int32) afs_get_pag_from_groups(g0, g1);
523     return pag;
524 }