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
17 * afs_get_pag_from_groups
18 * afs_get_groups_from_pag
22 #include <afsconfig.h>
23 #include "../afs/param.h"
27 #include "../afs/sysincludes.h" /* Standard vendor system headers */
28 #include "../afs/afsincludes.h" /* Afs-based standard headers */
29 #include "../afs/afs_stats.h" /* statistics */
30 #include "../afs/afs_cbqueue.h"
31 #include "../afs/nfsclient.h"
32 #include "../afs/afs_osidnlc.h"
35 /* Imported variables */
36 extern int afs_shuttingdown;
38 /* Exported variables */
40 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
41 afs_uint32 pagCounter = 1;
43 afs_uint32 pagCounter = 0;
44 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
49 * Pags are implemented as follows: the set of groups whose long
50 * representation is '41XXXXXX' hex are used to represent the pags.
51 * Being a member of such a group means you are authenticated as pag
52 * XXXXXX (0x41 == 'A', for Andrew). You are never authenticated as
53 * multiple pags at once.
55 * The function afs_InitReq takes a credential field and formats the
56 * corresponding venus request structure. The uid field in the
57 * vrequest structure is set to the *pag* you are authenticated as, or
58 * the uid, if you aren't authenticated with a pag.
60 * The basic motivation behind pags is this: just because your unix
61 * uid is N doesn't mean that you should have the same privileges as
62 * anyone logged in on the machine as user N, since this would enable
63 * the superuser on the machine to sneak in and make use of anyone's
64 * authentication info, even that which is only accidentally left
65 * behind when someone leaves a public workstation.
67 * AFS doesn't use the unix uid for anything except
68 * a handle with which to find the actual authentication tokens
69 * anyway, so the pag is an alternative handle which is somewhat more
70 * secure (although of course not absolutely secure).
72 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
73 afs_uint32 genpag(void)
76 #ifdef AFS_LINUX20_ENV
77 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
78 return (('A' << 24) + ((pag_epoch + pagCounter++) & 0xffffff));
79 #else /* AFS_LINUX20_ENV */
80 return (('A' << 24) + (pagCounter++ & 0xffffff));
81 #endif /* AFS_LINUX20_ENV */
84 afs_uint32 getpag(void)
87 #ifdef AFS_LINUX20_ENV
88 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
89 return (('A' << 24) + ((pag_epoch + pagCounter) & 0xffffff));
91 return (('A' << 24) + (pagCounter & 0xffffff));
97 /* Web enhancement: we don't need to restrict pags to 41XXXXXX since
98 * we are not sharing the space with anyone. So we use the full 32 bits. */
100 afs_uint32 genpag(void)
103 #ifdef AFS_LINUX20_ENV
104 return (pag_epoch + pagCounter++);
106 return (pagCounter++);
107 #endif /* AFS_LINUX20_ENV */
110 afs_uint32 getpag(void)
113 #ifdef AFS_LINUX20_ENV
114 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
115 return (pag_epoch + pagCounter);
120 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
122 /* used to require 10 seconds between each setpag to guarantee that
123 * PAGs never wrap - which would be a security hole. If we presume
124 * that in ordinary operation, the average rate of PAG allocation
125 * will not exceed one per second, the 24 bits provided will be
126 * sufficient for ~200 days. Unfortunately, if we merely assume that,
127 * there will be an opportunity for attack. So we must enforce it.
128 * If we need to increase the average rate of PAG allocation, we
129 * should increase the number of bits in a PAG, and/or reduce our
130 * window in which we guarantee that the PAG counter won't wrap.
131 * By permitting an average of one new PAG per second, new PAGs can
132 * be allocated VERY frequently over a short period relative to total uptime.
133 * Of course, there's only an issue here if one user stays logged (and re-
134 * activates tokens repeatedly) for that entire period.
138 #if defined(AFS_SUN5_ENV)
139 afs_setpag (struct AFS_UCRED **credpp)
140 #elif defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
141 afs_setpag (struct proc *p, void *args, int *retval)
148 #if defined(AFS_SGI53_ENV) && defined(MP)
149 /* This is our first chance to get the global lock. */
151 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
153 AFS_STATCNT(afs_setpag);
155 if (!afs_suser(*credpp))
160 while (osi_Time() - pag_epoch < pagCounter ) {
161 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
165 #if defined(AFS_SUN5_ENV)
166 code = AddPag(genpag(), credpp);
167 #elif defined(AFS_OSF_ENV) || defined(AFS_FBSD_ENV)
168 code = AddPag(p, genpag(), &p->p_rcred);
169 #elif defined(AFS_AIX41_ENV)
172 struct ucred *credp0;
176 code = AddPag(genpag(), &credp);
177 /* If AddPag() didn't make a new cred, then free our cred ref */
178 if (credp == credp0) {
182 #elif defined(AFS_HPUX110_ENV)
184 struct ucred *credp = p_cred(u.u_procp);
185 code = AddPag(genpag(), &credp);
187 #elif defined(AFS_SGI_ENV)
190 credp = OSI_GET_CURRENT_CRED();
191 code = AddPag(genpag(), &credp);
193 #elif defined(AFS_LINUX20_ENV)
195 struct AFS_UCRED *credp = crref();
196 code = AddPag(genpag(), &credp);
199 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
201 struct ucred *credp=crdup(p->p_cred->pc_ucred);
202 code=AddPag(p, genpag(), &credp);
206 code = AddPag(genpag(), &u.u_cred);
209 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
210 #if defined(KERNEL_HAVE_UERROR)
214 #if defined(AFS_SGI53_ENV) && defined(MP)
216 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
220 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
223 * This function is like setpag but sets the current thread's pag id to a
224 * caller-provided value instead of calling genpag(). This implements a
225 * form of token caching since the caller can recall a particular pag value
226 * for the thread to restore tokens, rather than reauthenticating.
229 #if defined(AFS_SUN5_ENV)
230 afs_setpag_val (struct AFS_UCRED **credpp, int pagval)
231 #elif defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
232 afs_setpag_val (struct proc *p, void *args, int *retval, int pagval)
234 afs_setpag_val (int pagval)
239 #if defined(AFS_SGI53_ENV) && defined(MP)
240 /* This is our first chance to get the global lock. */
242 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
244 AFS_STATCNT(afs_setpag);
246 if (!afs_suser(*credpp))
251 while (osi_Time() - pag_epoch < pagCounter ) {
252 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
256 #if defined(AFS_SUN5_ENV)
257 code = AddPag(pagval, credpp);
258 #elif defined(AFS_OSF_ENV) || defined(AFS_FBSD_ENV)
259 code = AddPag(p, pagval, &p->p_rcred);
260 #elif defined(AFS_AIX41_ENV)
263 struct ucred *credp0;
267 code = AddPag(pagval, &credp);
268 /* If AddPag() didn't make a new cred, then free our cred ref */
269 if (credp == credp0) {
273 #elif defined(AFS_HPUX110_ENV)
275 struct ucred *credp = p_cred(u.u_procp);
276 code = AddPag(pagval, &credp);
278 #elif defined(AFS_SGI_ENV)
281 credp = OSI_GET_CURRENT_CRED();
282 code = AddPag(pagval, &credp);
284 #elif defined(AFS_LINUX20_ENV)
286 struct AFS_UCRED *credp = crref();
287 code = AddPag(pagval, &credp);
290 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
292 struct ucred *credp=crdup(p->p_cred->pc_ucred);
293 code=AddPag(p, pagval, &credp);
297 code = AddPag(pagval, &u.u_cred);
300 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
301 #if defined(KERNEL_HAVE_UERROR)
305 #if defined(AFS_SGI53_ENV) && defined(MP)
307 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
310 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
312 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
316 struct AFS_UCRED *credp = u.u_cred;
317 int gidset0, gidset1;
319 gidset0 = credp->cr_groups[0];
320 gidset1 = credp->cr_groups[1];
321 pagvalue=afs_get_pag_from_groups(gidset0, gidset1);
324 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
327 /* Note - needs to be available on AIX, others can be static - rework this */
328 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
329 int AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
330 #else /* AFS_OSF_ENV || AFS_FBSD_ENV */
331 int AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
334 afs_int32 newpag, code;
336 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
337 if ((code = setpag(p, credpp, aval, &newpag, 0)))
338 #else /* AFS_OSF_ENV */
339 if ((code = setpag(credpp, aval, &newpag, 0)))
341 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
344 return (setuerror(code), code);
350 int afs_InitReq(register struct vrequest *av, struct AFS_UCRED *acred)
352 AFS_STATCNT(afs_InitReq);
353 if (afs_shuttingdown) return EIO;
354 av->uid = PagInCred(acred);
355 if (av->uid == NOPAG) {
356 /* Afs doesn't use the unix uid for anuthing except a handle
357 * with which to find the actual authentication tokens so I
358 * think it's ok to use the real uid to make setuid
359 * programs (without setpag) to work properly.
361 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
362 av->uid = acred->cr_uid; /* default when no pag is set */
363 /* bsd creds don't have ruid */
365 av->uid = acred->cr_ruid; /* default when no pag is set */
374 afs_uint32 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
378 afs_uint32 h, l, ret;
380 AFS_STATCNT(afs_get_pag_from_groups);
383 if (g0 < 0xc000 && g1 < 0xc000) {
384 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
386 h = (g1 >> 14) + h + h + h;
387 ret = ((h << 28) | l);
388 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
391 /* Additional testing */
392 if (((ret >> 24) & 0xff) == 'A')
396 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
402 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
404 unsigned short g0, g1;
407 AFS_STATCNT(afs_get_groups_from_pag);
408 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
410 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
411 g0 = 0x3fff & (pag >> 14);
413 g0 |= ((pag >> 28) / 3) << 14;
414 g1 |= ((pag >> 28) % 3) << 14;
420 afs_int32 PagInCred(const struct AFS_UCRED *cred)
425 AFS_STATCNT(PagInCred);
429 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
430 if (cred == NOCRED || cred == FSCRED) {
433 if (cred->cr_ngroups < 3) return NOPAG;
434 /* gid is stored in cr_groups[0] */
435 g0 = cred->cr_groups[1];
436 g1 = cred->cr_groups[2];
440 if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
444 if (cred->cr_ngrps < 2) {
449 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX_ENV) || defined(AFS_FBSD_ENV)
450 if (cred->cr_ngroups < 2) return NOPAG;
454 g0 = cred->cr_groupset.gs_union.un_groups[0];
455 g1 = cred->cr_groupset.gs_union.un_groups[1];
457 g0 = cred->cr_groups[0];
458 g1 = cred->cr_groups[1];
461 pag = (afs_int32)afs_get_pag_from_groups(g0, g1);