support-dux-51-20010305
[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 "../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"
29
30
31 /* Imported variables */
32 extern int afs_shuttingdown;
33
34 /* Exported variables */
35 afs_uint32 pag_epoch;
36 afs_uint32 pagCounter = 0;
37
38 /* Local variables */
39
40 /*
41  * Pags are implemented as follows: the set of groups whose long
42  * representation is '41XXXXXX' hex are used to represent the pags.
43  * Being a member of such a group means you are authenticated as pag
44  * XXXXXX (0x41 == 'A', for Andrew).  You are never authenticated as
45  * multiple pags at once.
46  *
47  * The function afs_InitReq takes a credential field and formats the
48  * corresponding venus request structure.  The uid field in the
49  * vrequest structure is set to the *pag* you are authenticated as, or
50  * the uid, if you aren't authenticated with a pag.
51  *
52  * The basic motivation behind pags is this: just because your unix
53  * uid is N doesn't mean that you should have the same privileges as
54  * anyone logged in on the machine as user N, since this would enable
55  * the superuser on the machine to sneak in and make use of anyone's
56  * authentication info, even that which is only accidentally left
57  * behind when someone leaves a public workstation.
58  *
59  * AFS doesn't use the unix uid for anything except
60  * a handle with which to find the actual authentication tokens
61  * anyway, so the pag is an alternative handle which is somewhat more
62  * secure (although of course not absolutely secure).
63 */
64 afs_uint32 genpag(void) {
65     AFS_STATCNT(genpag);
66 #ifdef AFS_LINUX20_ENV
67     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
68     return (('A' << 24) + ((pag_epoch + pagCounter++) & 0xffffff));
69 #else
70     return (('A' << 24) + (pagCounter++ & 0xffffff));
71 #endif
72 }
73
74 afs_uint32 getpag(void) {
75     AFS_STATCNT(getpag);
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
80     return (('A' << 24) + (pagCounter & 0xffffff));
81 #endif
82 }
83
84
85 /* used to require 10 seconds between each setpag to guarantee that
86  * PAGs never wrap - which would be a security hole.  If we presume
87  * that in ordinary operation, the average rate of PAG allocation
88  * will not exceed one per second, the 24 bits provided will be
89  * sufficient for ~200 days.  Unfortunately, if we merely assume that,
90  * there will be an opportunity for attack.  So we must enforce it.
91  * If we need to increase the average rate of PAG allocation, we
92  * should increase the number of bits in a PAG, and/or reduce our
93  * window in which we guarantee that the PAG counter won't wrap.
94  * By permitting an average of one new PAG per second, new PAGs can
95  * be allocated VERY frequently over a short period relative to total uptime.
96  * Of course, there's only an issue here if one user stays logged (and re-
97  * activates tokens repeatedly) for that entire period.
98  */
99
100 int
101 #if     defined(AFS_SUN5_ENV)
102 afs_setpag (struct AFS_UCRED **credpp)
103 #elif   defined(AFS_OSF_ENV)
104 afs_setpag (struct proc *p, void *args, int *retval)
105 #else
106 afs_setpag (void) 
107 #endif
108 {
109     int code = 0;
110
111 #if defined(AFS_SGI53_ENV) && defined(MP)
112     /* This is our first chance to get the global lock. */
113     AFS_GLOCK();
114 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */    
115
116     AFS_STATCNT(afs_setpag);
117 #ifdef AFS_SUN5_ENV
118     if (!afs_suser(*credpp))
119 #else
120     if (!afs_suser())
121 #endif
122     {
123         while (osi_Time() - pag_epoch < pagCounter ) {
124             afs_osi_Wait(1000, (struct afs_osi_WaitHandle *) 0, 0);
125         }       
126     }
127
128 #if     defined(AFS_SUN5_ENV)
129     code = AddPag(genpag(), credpp);
130 #elif   defined(AFS_OSF_ENV)
131     code = AddPag(p, genpag(), &p->p_rcred);
132 #elif   defined(AFS_AIX41_ENV)
133     {
134         struct ucred *credp;
135         struct ucred *credp0;
136         
137         credp = crref();
138         credp0 = credp;
139         code = AddPag(genpag(), &credp);
140         /* If AddPag() didn't make a new cred, then free our cred ref */
141         if (credp == credp0) {
142             crfree(credp);
143         }
144     }
145 #elif   defined(AFS_HPUX110_ENV)
146     {
147         struct ucred *credp = p_cred(u.u_procp);
148         code = AddPag(genpag(), &credp);
149     }
150 #elif   defined(AFS_SGI_ENV)
151     {
152         cred_t *credp;
153         credp = OSI_GET_CURRENT_CRED();
154         code = AddPag(genpag(), &credp);
155     }
156 #elif   defined(AFS_LINUX20_ENV)
157     {
158         struct AFS_UCRED *credp = crref();
159         code = AddPag(genpag(), &credp);
160         crfree(credp);
161     }
162 #else
163     code = AddPag(genpag(), &u.u_cred);
164 #endif
165
166     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
167 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_LINUX20_ENV)
168 #if defined(AFS_SGI53_ENV) && defined(MP)
169     AFS_GUNLOCK();
170 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */    
171     return (code);
172 #else
173     if (!getuerror())
174         setuerror(code);
175     return (code);
176 #endif
177 }
178
179 #ifdef  AFS_OSF_ENV
180 int AddPag(struct proc *p, afs_int32 aval, struct AFS_UCRED **credpp)
181 #else   /* AFS_OSF_ENV */
182 int AddPag(afs_int32 aval, struct AFS_UCRED **credpp)
183 #endif
184 {
185     afs_int32 newpag, code;
186     AFS_STATCNT(AddPag);
187 #ifdef  AFS_OSF_ENV
188     if (code = setpag(p, credpp, aval, &newpag, 0))
189 #else   /* AFS_OSF_ENV */
190     if (code = setpag(credpp, aval, &newpag, 0))
191 #endif
192 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_LINUX20_ENV)
193         return (code);
194 #else
195         return (setuerror(code), code);
196 #endif
197     return 0;
198 }
199
200
201 afs_InitReq(av, acred)
202     register struct vrequest *av;
203     struct AFS_UCRED *acred; {
204
205     AFS_STATCNT(afs_InitReq);
206     if (afs_shuttingdown) return EIO;
207     av->uid = PagInCred(acred);
208     if (av->uid == NOPAG) {
209         /* Afs doesn't use the unix uid for anuthing except a handle
210          * with which to find the actual authentication tokens so I
211          * think it's ok to use the real uid to make setuid
212          * programs (without setpag) to work properly.
213          */
214         av->uid = acred->cr_ruid;    /* default when no pag is set */
215     }
216     av->initd = 0;
217     return 0;
218 }
219
220
221
222 afs_uint32 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
223 {
224     afs_uint32 g0 = g0a;
225     afs_uint32 g1 = g1a;
226     afs_uint32 h, l, ret;
227
228     AFS_STATCNT(afs_get_pag_from_groups);
229     g0 -= 0x3f00;
230     g1 -= 0x3f00;
231     if (g0 < 0xc000 && g1 < 0xc000) {
232         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
233         h = (g0 >> 14);
234         h = (g1 >> 14) + h + h + h;
235         ret = ((h << 28) | l);
236         /* Additional testing */
237         if (((ret >> 24) & 0xff) == 'A')
238             return ret;
239         else
240             return NOPAG;
241     }
242     return NOPAG;
243 }
244
245
246 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
247 {
248     unsigned short g0, g1;
249
250
251     AFS_STATCNT(afs_get_groups_from_pag);
252     pag &= 0x7fffffff;
253     g0 = 0x3fff & (pag >> 14);
254     g1 = 0x3fff & pag;
255     g0 |= ((pag >> 28) / 3) << 14;
256     g1 |= ((pag >> 28) % 3) << 14;
257     *g0p = g0 + 0x3f00;
258     *g1p = g1 + 0x3f00;
259 }
260
261
262 afs_int32 PagInCred(const struct AFS_UCRED *cred)
263 {
264     afs_int32 pag;
265     gid_t g0, g1;
266
267     AFS_STATCNT(PagInCred);
268     if (cred == NULL) {
269         return NOPAG;
270     }
271 #ifdef  AFS_AIX_ENV
272     if (cred->cr_ngrps < 2) {
273         return NOPAG;
274     }
275 #else
276 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX_ENV)
277     if (cred->cr_ngroups < 2) return NOPAG;
278 #endif
279 #endif
280     g0 = cred->cr_groups[0];
281     g1 = cred->cr_groups[1];
282     pag = (afs_int32)afs_get_pag_from_groups(g0, g1);
283     return pag;
284 }