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 "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_XBSD_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);
154 #if defined(AFS_SUN5_ENV)
155 if (!afs_suser(*credpp))
156 #elif defined(AFS_OBSD_ENV)
157 if (!afs_osi_suser(p->p_cred->pc_ucred))
162 while (osi_Time() - pag_epoch < pagCounter ) {
163 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
167 #if defined(AFS_SUN5_ENV)
168 code = AddPag(genpag(), credpp);
169 #elif defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
170 code = AddPag(p, genpag(), &p->p_rcred);
171 #elif defined(AFS_AIX41_ENV)
174 struct ucred *credp0;
178 code = AddPag(genpag(), &credp);
179 /* If AddPag() didn't make a new cred, then free our cred ref */
180 if (credp == credp0) {
184 #elif defined(AFS_HPUX110_ENV)
186 struct ucred *credp = p_cred(u.u_procp);
187 code = AddPag(genpag(), &credp);
189 #elif defined(AFS_SGI_ENV)
192 credp = OSI_GET_CURRENT_CRED();
193 code = AddPag(genpag(), &credp);
195 #elif defined(AFS_LINUX20_ENV)
197 struct AFS_UCRED *credp = crref();
198 code = AddPag(genpag(), &credp);
201 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
203 struct ucred *credp=crdup(p->p_cred->pc_ucred);
204 code=AddPag(p, genpag(), &credp);
208 code = AddPag(genpag(), &u.u_cred);
211 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
212 #if defined(KERNEL_HAVE_UERROR)
216 #if defined(AFS_SGI53_ENV) && defined(MP)
218 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
222 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
225 * This function is like setpag but sets the current thread's pag id to a
226 * caller-provided value instead of calling genpag(). This implements a
227 * form of token caching since the caller can recall a particular pag value
228 * for the thread to restore tokens, rather than reauthenticating.
231 #if defined(AFS_SUN5_ENV)
232 afs_setpag_val (struct AFS_UCRED **credpp, int pagval)
233 #elif defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
234 afs_setpag_val (struct proc *p, void *args, int *retval, int pagval)
236 afs_setpag_val (int pagval)
241 #if defined(AFS_SGI53_ENV) && defined(MP)
242 /* This is our first chance to get the global lock. */
244 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
246 AFS_STATCNT(afs_setpag);
248 if (!afs_suser(*credpp))
253 while (osi_Time() - pag_epoch < pagCounter ) {
254 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
258 #if defined(AFS_SUN5_ENV)
259 code = AddPag(pagval, credpp);
260 #elif defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
261 code = AddPag(p, pagval, &p->p_rcred);
262 #elif defined(AFS_AIX41_ENV)
265 struct ucred *credp0;
269 code = AddPag(pagval, &credp);
270 /* If AddPag() didn't make a new cred, then free our cred ref */
271 if (credp == credp0) {
275 #elif defined(AFS_HPUX110_ENV)
277 struct ucred *credp = p_cred(u.u_procp);
278 code = AddPag(pagval, &credp);
280 #elif defined(AFS_SGI_ENV)
283 credp = OSI_GET_CURRENT_CRED();
284 code = AddPag(pagval, &credp);
286 #elif defined(AFS_LINUX20_ENV)
288 struct AFS_UCRED *credp = crref();
289 code = AddPag(pagval, &credp);
292 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
294 struct ucred *credp=crdup(p->p_cred->pc_ucred);
295 code=AddPag(p, pagval, &credp);
299 code = AddPag(pagval, &u.u_cred);
302 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
303 #if defined(KERNEL_HAVE_UERROR)
307 #if defined(AFS_SGI53_ENV) && defined(MP)
309 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
312 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
314 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
318 struct AFS_UCRED *credp = u.u_cred;
319 int gidset0, gidset1;
321 gidset0 = credp->cr_groups[0];
322 gidset1 = credp->cr_groups[1];
323 pagvalue=afs_get_pag_from_groups(gidset0, gidset1);
326 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
329 /* Note - needs to be available on AIX, others can be static - rework this */
330 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
331 int AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
333 int AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
336 afs_int32 newpag, code;
338 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
339 if ((code = setpag(p, credpp, aval, &newpag, 0)))
340 #else /* AFS_OSF_ENV */
341 if ((code = setpag(credpp, aval, &newpag, 0)))
343 #if defined(KERNEL_HAVE_UERROR)
344 return (setuerror(code), code);
352 int afs_InitReq(register struct vrequest *av, struct AFS_UCRED *acred)
354 AFS_STATCNT(afs_InitReq);
355 if (afs_shuttingdown) return EIO;
356 av->uid = PagInCred(acred);
357 if (av->uid == NOPAG) {
358 /* Afs doesn't use the unix uid for anuthing except a handle
359 * with which to find the actual authentication tokens so I
360 * think it's ok to use the real uid to make setuid
361 * programs (without setpag) to work properly.
363 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
365 av->uid = -2; /* XXX nobody... ?*/
367 av->uid = acred->cr_uid; /* bsd creds don't have ruid */
369 av->uid = acred->cr_ruid; /* default when no pag is set */
378 afs_uint32 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
382 afs_uint32 h, l, ret;
384 AFS_STATCNT(afs_get_pag_from_groups);
387 if (g0 < 0xc000 && g1 < 0xc000) {
388 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
390 h = (g1 >> 14) + h + h + h;
391 ret = ((h << 28) | l);
392 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
395 /* Additional testing */
396 if (((ret >> 24) & 0xff) == 'A')
400 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
406 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
408 unsigned short g0, g1;
411 AFS_STATCNT(afs_get_groups_from_pag);
412 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
414 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
415 g0 = 0x3fff & (pag >> 14);
417 g0 |= ((pag >> 28) / 3) << 14;
418 g1 |= ((pag >> 28) % 3) << 14;
424 afs_int32 PagInCred(const struct AFS_UCRED *cred)
429 AFS_STATCNT(PagInCred);
433 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
434 if (cred == NOCRED || cred == FSCRED) {
437 if (cred->cr_ngroups < 3) return NOPAG;
438 /* gid is stored in cr_groups[0] */
439 g0 = cred->cr_groups[1];
440 g1 = cred->cr_groups[2];
444 if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
448 if (cred->cr_ngrps < 2) {
453 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
454 if (cred->cr_ngroups < 2) return NOPAG;
458 g0 = cred->cr_groupset.gs_union.un_groups[0];
459 g1 = cred->cr_groupset.gs_union.un_groups[1];
461 g0 = cred->cr_groups[0];
462 g1 = cred->cr_groups[1];
465 pag = (afs_int32)afs_get_pag_from_groups(g0, g1);