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 "../afs/param.h" /* Should be always first */
23 #include "../afs/sysincludes.h" /* Standard vendor system headers */
24 #include "../afs/afsincludes.h" /* Afs-based standard headers */
25 #include "../afs/afs_stats.h" /* statistics */
26 #include "../afs/afs_cbqueue.h"
27 #include "../afs/nfsclient.h"
28 #include "../afs/afs_osidnlc.h"
31 /* Imported variables */
32 extern int afs_shuttingdown;
34 /* Exported variables */
36 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
37 afs_uint32 pagCounter = 1;
39 afs_uint32 pagCounter = 0;
40 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
45 * Pags are implemented as follows: the set of groups whose long
46 * representation is '41XXXXXX' hex are used to represent the pags.
47 * Being a member of such a group means you are authenticated as pag
48 * XXXXXX (0x41 == 'A', for Andrew). You are never authenticated as
49 * multiple pags at once.
51 * The function afs_InitReq takes a credential field and formats the
52 * corresponding venus request structure. The uid field in the
53 * vrequest structure is set to the *pag* you are authenticated as, or
54 * the uid, if you aren't authenticated with a pag.
56 * The basic motivation behind pags is this: just because your unix
57 * uid is N doesn't mean that you should have the same privileges as
58 * anyone logged in on the machine as user N, since this would enable
59 * the superuser on the machine to sneak in and make use of anyone's
60 * authentication info, even that which is only accidentally left
61 * behind when someone leaves a public workstation.
63 * AFS doesn't use the unix uid for anything except
64 * a handle with which to find the actual authentication tokens
65 * anyway, so the pag is an alternative handle which is somewhat more
66 * secure (although of course not absolutely secure).
68 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
69 afs_uint32 genpag(void) {
71 #ifdef AFS_LINUX20_ENV
72 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
73 return (('A' << 24) + ((pag_epoch + pagCounter++) & 0xffffff));
74 #else /* AFS_LINUX20_ENV */
75 return (('A' << 24) + (pagCounter++ & 0xffffff));
76 #endif /* AFS_LINUX20_ENV */
79 afs_uint32 getpag(void) {
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));
85 return (('A' << 24) + (pagCounter & 0xffffff));
91 /* Web enhancement: we don't need to restrict pags to 41XXXXXX since
92 * we are not sharing the space with anyone. So we use the full 32 bits. */
94 afs_uint32 genpag(void) {
96 #ifdef AFS_LINUX20_ENV
97 return (pag_epoch + pagCounter++);
99 return (pagCounter++);
100 #endif /* AFS_LINUX20_ENV */
103 afs_uint32 getpag(void) {
105 #ifdef AFS_LINUX20_ENV
106 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
107 return (pag_epoch + pagCounter);
112 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
114 /* used to require 10 seconds between each setpag to guarantee that
115 * PAGs never wrap - which would be a security hole. If we presume
116 * that in ordinary operation, the average rate of PAG allocation
117 * will not exceed one per second, the 24 bits provided will be
118 * sufficient for ~200 days. Unfortunately, if we merely assume that,
119 * there will be an opportunity for attack. So we must enforce it.
120 * If we need to increase the average rate of PAG allocation, we
121 * should increase the number of bits in a PAG, and/or reduce our
122 * window in which we guarantee that the PAG counter won't wrap.
123 * By permitting an average of one new PAG per second, new PAGs can
124 * be allocated VERY frequently over a short period relative to total uptime.
125 * Of course, there's only an issue here if one user stays logged (and re-
126 * activates tokens repeatedly) for that entire period.
130 #if defined(AFS_SUN5_ENV)
131 afs_setpag (struct AFS_UCRED **credpp)
132 #elif defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
133 afs_setpag (struct proc *p, void *args, int *retval)
140 #if defined(AFS_SGI53_ENV) && defined(MP)
141 /* This is our first chance to get the global lock. */
143 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
145 AFS_STATCNT(afs_setpag);
147 if (!afs_suser(*credpp))
152 while (osi_Time() - pag_epoch < pagCounter ) {
153 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
157 #if defined(AFS_SUN5_ENV)
158 code = AddPag(genpag(), credpp);
159 #elif defined(AFS_OSF_ENV) || defined(AFS_FBSD_ENV)
160 code = AddPag(p, genpag(), &p->p_rcred);
161 #elif defined(AFS_AIX41_ENV)
164 struct ucred *credp0;
168 code = AddPag(genpag(), &credp);
169 /* If AddPag() didn't make a new cred, then free our cred ref */
170 if (credp == credp0) {
174 #elif defined(AFS_HPUX110_ENV)
176 struct ucred *credp = p_cred(u.u_procp);
177 code = AddPag(genpag(), &credp);
179 #elif defined(AFS_SGI_ENV)
182 credp = OSI_GET_CURRENT_CRED();
183 code = AddPag(genpag(), &credp);
185 #elif defined(AFS_LINUX20_ENV)
187 struct AFS_UCRED *credp = crref();
188 code = AddPag(genpag(), &credp);
191 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
193 struct ucred *credp=crdup(p->p_cred->pc_ucred);
194 code=AddPag(p, genpag(), &credp);
198 code = AddPag(genpag(), &u.u_cred);
201 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
202 #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)
203 #if defined(AFS_SGI53_ENV) && defined(MP)
205 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
214 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
217 * This function is like setpag but sets the current thread's pag id to a
218 * caller-provided value instead of calling genpag(). This implements a
219 * form of token caching since the caller can recall a particular pag value
220 * for the thread to restore tokens, rather than reauthenticating.
223 #if defined(AFS_SUN5_ENV)
224 afs_setpag_val (struct AFS_UCRED **credpp, int pagval)
225 #elif defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
226 afs_setpag_val (struct proc *p, void *args, int *retval, int pagval)
228 afs_setpag_val (int pagval)
233 #if defined(AFS_SGI53_ENV) && defined(MP)
234 /* This is our first chance to get the global lock. */
236 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
238 AFS_STATCNT(afs_setpag);
240 if (!afs_suser(*credpp))
245 while (osi_Time() - pag_epoch < pagCounter ) {
246 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
250 #if defined(AFS_SUN5_ENV)
251 code = AddPag(pagval, credpp);
252 #elif defined(AFS_OSF_ENV) || defined(AFS_FBSD_ENV)
253 code = AddPag(p, pagval, &p->p_rcred);
254 #elif defined(AFS_AIX41_ENV)
257 struct ucred *credp0;
261 code = AddPag(pagval, &credp);
262 /* If AddPag() didn't make a new cred, then free our cred ref */
263 if (credp == credp0) {
267 #elif defined(AFS_HPUX110_ENV)
269 struct ucred *credp = p_cred(u.u_procp);
270 code = AddPag(pagval, &credp);
272 #elif defined(AFS_SGI_ENV)
275 credp = OSI_GET_CURRENT_CRED();
276 code = AddPag(pagval, &credp);
278 #elif defined(AFS_LINUX20_ENV)
280 struct AFS_UCRED *credp = crref();
281 code = AddPag(pagval, &credp);
284 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
286 struct ucred *credp=crdup(p->p_cred->pc_ucred);
287 code=AddPag(p, pagval, &credp);
291 code = AddPag(pagval, &u.u_cred);
294 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
295 #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)
296 #if defined(AFS_SGI53_ENV) && defined(MP)
298 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
306 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
308 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
312 struct AFS_UCRED *credp = u.u_cred;
313 int gidset0, gidset1;
315 gidset0 = credp->cr_groups[0];
316 gidset1 = credp->cr_groups[1];
317 pagvalue=afs_get_pag_from_groups(gidset0, gidset1);
320 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
323 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
324 int AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
325 #else /* AFS_OSF_ENV || AFS_FBSD_ENV */
326 int AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
329 afs_int32 newpag, code;
331 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
332 if (code = setpag(p, credpp, aval, &newpag, 0))
333 #else /* AFS_OSF_ENV */
334 if (code = setpag(credpp, aval, &newpag, 0))
336 #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)
339 return (setuerror(code), code);
345 afs_InitReq(av, acred)
346 register struct vrequest *av;
347 struct AFS_UCRED *acred; {
349 AFS_STATCNT(afs_InitReq);
350 if (afs_shuttingdown) return EIO;
351 av->uid = PagInCred(acred);
352 if (av->uid == NOPAG) {
353 /* Afs doesn't use the unix uid for anuthing except a handle
354 * with which to find the actual authentication tokens so I
355 * think it's ok to use the real uid to make setuid
356 * programs (without setpag) to work properly.
358 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
359 av->uid = acred->cr_uid; /* default when no pag is set */
360 /* bsd creds don't have ruid */
362 av->uid = acred->cr_ruid; /* default when no pag is set */
371 afs_uint32 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
375 afs_uint32 h, l, ret;
377 AFS_STATCNT(afs_get_pag_from_groups);
380 if (g0 < 0xc000 && g1 < 0xc000) {
381 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
383 h = (g1 >> 14) + h + h + h;
384 ret = ((h << 28) | l);
385 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
388 /* Additional testing */
389 if (((ret >> 24) & 0xff) == 'A')
393 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
399 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
401 unsigned short g0, g1;
404 AFS_STATCNT(afs_get_groups_from_pag);
405 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
407 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
408 g0 = 0x3fff & (pag >> 14);
410 g0 |= ((pag >> 28) / 3) << 14;
411 g1 |= ((pag >> 28) % 3) << 14;
417 afs_int32 PagInCred(const struct AFS_UCRED *cred)
422 AFS_STATCNT(PagInCred);
426 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
427 if (cred == NOCRED || cred == FSCRED) {
430 if (cred->cr_ngroups < 3) return NOPAG;
431 /* gid is stored in cr_groups[0] */
432 g0 = cred->cr_groups[1];
433 g1 = cred->cr_groups[2];
436 if (cred->cr_ngrps < 2) {
440 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX_ENV) || defined(AFS_FBSD_ENV)
441 if (cred->cr_ngroups < 2) return NOPAG;
444 g0 = cred->cr_groups[0];
445 g1 = cred->cr_groups[1];
447 pag = (afs_int32)afs_get_pag_from_groups(g0, g1);