DEVEL15-linux-afs-translator-xen-20060731
[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
149     if (!afs_suser(acred)) {
150         if(osi_Time() - pag_epoch < pagCounter) {
151             rv = 1;
152         }
153     }
154
155     return rv;
156 }
157
158 static int
159 afs_pag_wait(struct AFS_UCRED **acred)
160 {
161     if (afs_pag_sleep(acred)) {
162         if (!afs_pag_sleepcnt) {
163             printf("%s() PAG throttling triggered, pid %d... sleeping.  sleepcnt %d\n",
164                    "afs_pag_wait", osi_getpid(), afs_pag_sleepcnt);
165         }
166
167         afs_pag_sleepcnt++;
168
169         do {
170             /* XXX spins on EINTR */
171             afs_osi_Wait(1000, (struct afs_osi_WaitHandle *)0, 0);
172         } while (afs_pag_sleep(acred));
173
174         afs_pag_sleepcnt--;
175     }
176
177     return 0;
178 }
179
180 int
181 #if     defined(AFS_SUN5_ENV)
182 afs_setpag(struct AFS_UCRED **credpp)
183 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
184 afs_setpag(struct proc *p, void *args, int *retval)
185 #else
186 afs_setpag(void)
187 #endif
188 {
189
190 #if     defined(AFS_SUN5_ENV)
191     struct AFS_UCRED **acred = *credpp;
192 #elif  defined(AFS_OBSD_ENV)
193     struct AFS_UCRED **acred = &p->p_ucred;
194 #else
195     struct AFS_UCRED **acred = NULL;
196 #endif
197
198     int code = 0;
199
200 #if defined(AFS_SGI53_ENV) && defined(MP)
201     /* This is our first chance to get the global lock. */
202     AFS_GLOCK();
203 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
204
205     AFS_STATCNT(afs_setpag);
206
207     afs_pag_wait(acred);
208
209
210 #if     defined(AFS_SUN5_ENV)
211     code = AddPag(genpag(), credpp);
212 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
213     code = AddPag(p, genpag(), &p->p_rcred);
214 #elif   defined(AFS_AIX41_ENV)
215     {
216         struct ucred *credp;
217         struct ucred *credp0;
218
219         credp = crref();
220         credp0 = credp;
221         code = AddPag(genpag(), &credp);
222         /* If AddPag() didn't make a new cred, then free our cred ref */
223         if (credp == credp0) {
224             crfree(credp);
225         }
226     }
227 #elif   defined(AFS_HPUX110_ENV)
228     {
229         struct ucred *credp = p_cred(u.u_procp);
230         code = AddPag(genpag(), &credp);
231     }
232 #elif   defined(AFS_SGI_ENV)
233     {
234         cred_t *credp;
235         credp = OSI_GET_CURRENT_CRED();
236         code = AddPag(genpag(), &credp);
237     }
238 #elif   defined(AFS_LINUX20_ENV)
239     {
240         struct AFS_UCRED *credp = crref();
241         code = AddPag(genpag(), &credp);
242         crfree(credp);
243     }
244 #elif defined(AFS_DARWIN80_ENV)
245     {
246         struct ucred *credp = kauth_cred_proc_ref(p);
247         code = AddPag(p, genpag(), &credp);
248         kauth_cred_rele(credp);
249     }
250 #elif defined(AFS_DARWIN_ENV)
251     {
252         struct ucred *credp = crdup(p->p_cred->pc_ucred);
253         code = AddPag(p, genpag(), &credp);
254         crfree(credp);
255     }
256 #else
257     code = AddPag(genpag(), &u.u_cred);
258 #endif
259
260     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
261
262 #if defined(KERNEL_HAVE_UERROR)
263     if (!getuerror())
264         setuerror(code);
265 #endif
266
267 #if defined(AFS_SGI53_ENV) && defined(MP)
268     AFS_GUNLOCK();
269 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
270
271     return (code);
272 }
273
274 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
275 /*
276  * afs_setpag_val
277  * This function is like setpag but sets the current thread's pag id to a
278  * caller-provided value instead of calling genpag().  This implements a
279  * form of token caching since the caller can recall a particular pag value
280  * for the thread to restore tokens, rather than reauthenticating.
281  */
282 int
283 #if     defined(AFS_SUN5_ENV)
284 afs_setpag_val(struct AFS_UCRED **credpp, int pagval)
285 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
286 afs_setpag_val(struct proc *p, void *args, int *retval, int pagval)
287 #else
288 afs_setpag_val(int pagval)
289 #endif
290 {
291
292 #if     defined(AFS_SUN5_ENV)
293     struct AFS_UCRED **acred = *credp;
294 #elif  defined(AFS_OBSD_ENV)
295     struct AFS_UCRED **acred = &p->p_ucred;
296 #else
297     struct AFS_UCRED **acred = NULL;
298 #endif
299
300     int code = 0;
301
302 #if defined(AFS_SGI53_ENV) && defined(MP)
303     /* This is our first chance to get the global lock. */
304     AFS_GLOCK();
305 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
306
307     AFS_STATCNT(afs_setpag);
308
309     afs_pag_wait(acred);
310
311 #if     defined(AFS_SUN5_ENV)
312     code = AddPag(pagval, credpp);
313 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
314     code = AddPag(p, pagval, &p->p_rcred);
315 #elif   defined(AFS_AIX41_ENV)
316     {
317         struct ucred *credp;
318         struct ucred *credp0;
319
320         credp = crref();
321         credp0 = credp;
322         code = AddPag(pagval, &credp);
323         /* If AddPag() didn't make a new cred, then free our cred ref */
324         if (credp == credp0) {
325             crfree(credp);
326         }
327     }
328 #elif   defined(AFS_HPUX110_ENV)
329     {
330         struct ucred *credp = p_cred(u.u_procp);
331         code = AddPag(pagval, &credp);
332     }
333 #elif   defined(AFS_SGI_ENV)
334     {
335         cred_t *credp;
336         credp = OSI_GET_CURRENT_CRED();
337         code = AddPag(pagval, &credp);
338     }
339 #elif   defined(AFS_LINUX20_ENV)
340     {
341         struct AFS_UCRED *credp = crref();
342         code = AddPag(pagval, &credp);
343         crfree(credp);
344     }
345 #elif defined(AFS_DARWIN_ENV)
346     {
347         struct ucred *credp = crdup(p->p_cred->pc_ucred);
348         code = AddPag(p, pagval, &credp);
349         crfree(credp);
350     }
351 #else
352     code = AddPag(pagval, &u.u_cred);
353 #endif
354
355     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
356 #if defined(KERNEL_HAVE_UERROR)
357     if (!getuerror())
358         setuerror(code);
359 #endif
360 #if defined(AFS_SGI53_ENV) && defined(MP)
361     AFS_GUNLOCK();
362 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
363     return (code);
364 }
365
366 int
367 afs_getpag_val()
368 {
369     int pagvalue;
370     struct AFS_UCRED *credp = u.u_cred;
371     int gidset0, gidset1;
372
373     gidset0 = credp->cr_groups[0];
374     gidset1 = credp->cr_groups[1];
375     pagvalue = afs_get_pag_from_groups(gidset0, gidset1);
376     return pagvalue;
377 }
378 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
379
380
381 /* Note - needs to be available on AIX, others can be static - rework this */
382 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
383 int
384 AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
385 #else
386 int
387 AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
388 #endif
389 {
390     afs_int32 newpag, code;
391
392     AFS_STATCNT(AddPag);
393 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
394     if ((code = setpag(p, credpp, aval, &newpag, 0)))
395 #else
396     if ((code = setpag(credpp, aval, &newpag, 0)))
397 #endif
398 #if defined(KERNEL_HAVE_UERROR)
399         return (setuerror(code), code);
400 #else
401         return (code);
402 #endif
403     return 0;
404 }
405
406
407 int
408 afs_InitReq(register struct vrequest *av, struct AFS_UCRED *acred)
409 {
410     int code;
411
412     AFS_STATCNT(afs_InitReq);
413     memset(av, 0, sizeof(*av));
414     if (afs_shuttingdown)
415         return EIO;
416
417 #ifdef AFS_LINUX26_ENV
418     if (osi_linux_nfs_initreq(av, acred, &code))
419         return code;
420 #endif
421
422     av->uid = PagInCred(acred);
423     if (av->uid == NOPAG) {
424         /* Afs doesn't use the unix uid for anuthing except a handle
425          * with which to find the actual authentication tokens so I
426          * think it's ok to use the real uid to make setuid
427          * programs (without setpag) to work properly.
428          */
429 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
430         if (acred == NOCRED)
431             av->uid = -2;       /* XXX nobody... ? */
432         else
433             av->uid = acred->cr_uid;    /* bsd creds don't have ruid */
434 #else
435         av->uid = acred->cr_ruid;       /* default when no pag is set */
436 #endif
437     }
438     av->initd = 0;
439     return 0;
440 }
441
442
443
444 afs_uint32
445 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
446 {
447     afs_uint32 g0 = g0a;
448     afs_uint32 g1 = g1a;
449     afs_uint32 h, l, ret;
450
451     AFS_STATCNT(afs_get_pag_from_groups);
452     g0 -= 0x3f00;
453     g1 -= 0x3f00;
454     if (g0 < 0xc000 && g1 < 0xc000) {
455         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
456         h = (g0 >> 14);
457         h = (g1 >> 14) + h + h + h;
458         ret = ((h << 28) | l);
459 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
460         return ret;
461 #else
462         /* Additional testing */
463         if (((ret >> 24) & 0xff) == 'A')
464             return ret;
465         else
466             return NOPAG;
467 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
468     }
469     return NOPAG;
470 }
471
472
473 void
474 afs_get_groups_from_pag(afs_uint32 pag, gid_t * g0p, gid_t * g1p)
475 {
476     unsigned short g0, g1;
477
478
479     AFS_STATCNT(afs_get_groups_from_pag);
480 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
481     pag &= 0x7fffffff;
482 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
483     g0 = 0x3fff & (pag >> 14);
484     g1 = 0x3fff & pag;
485     g0 |= ((pag >> 28) / 3) << 14;
486     g1 |= ((pag >> 28) % 3) << 14;
487     *g0p = g0 + 0x3f00;
488     *g1p = g1 + 0x3f00;
489 }
490
491
492 afs_int32
493 PagInCred(const struct AFS_UCRED *cred)
494 {
495     afs_int32 pag;
496     gid_t g0, g1;
497
498     AFS_STATCNT(PagInCred);
499     if (cred == NULL) {
500         return NOPAG;
501     }
502 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
503     if (cred == NOCRED || cred == FSCRED) {
504         return NOPAG;
505     }
506     if (cred->cr_ngroups < 3)
507         return NOPAG;
508     /* gid is stored in cr_groups[0] */
509     g0 = cred->cr_groups[1];
510     g1 = cred->cr_groups[2];
511 #else
512 #if defined(AFS_AIX51_ENV)
513     if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
514         pag = NOPAG;
515     return pag;
516 #elif defined(AFS_AIX_ENV)
517     if (cred->cr_ngrps < 2) {
518         return NOPAG;
519     }
520 #elif defined(AFS_LINUX26_ENV)
521     if (cred->cr_group_info->ngroups < 2)
522         return NOPAG;
523 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
524     if (cred->cr_ngroups < 2)
525         return NOPAG;
526 #endif
527 #if defined(AFS_AIX51_ENV)
528     g0 = cred->cr_groupset.gs_union.un_groups[0];
529     g1 = cred->cr_groupset.gs_union.un_groups[1];
530 #elif defined(AFS_LINUX26_ENV)
531     g0 = GROUP_AT(cred->cr_group_info, 0);
532     g1 = GROUP_AT(cred->cr_group_info, 1);
533 #else
534     g0 = cred->cr_groups[0];
535     g1 = cred->cr_groups[1];
536 #endif
537 #endif
538     pag = (afs_int32) afs_get_pag_from_groups(g0, g1);
539     return pag;
540 }