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