Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / auth_unix.c
1 #ifndef lint
2 static char sfs_auth_unix_c_id[] = "@(#)auth_unix.c     2.1     97/10/23";
3 #endif
4 /* @(#)auth_unix.c      2.2 88/08/01 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  *
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * auth_unix.c, Implements UNIX style authentication parameters. 
57  *  
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  *
60  * The system is very weak.  The client uses no encryption for it's
61  * credentials and only sends null verifiers.  The server sends backs
62  * null verifiers or optionally a verifier that suggests a new short hand
63  * for the credentials.
64  *
65  */
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <string.h>
71 #include <netdb.h>
72
73 #include "rpc/rpc.h"
74
75 extern getmyhostname(char *, int);
76
77 /*
78  * Unix authenticator operations vector
79  */
80 static void     authunix_nextverf(AUTH *);
81 static bool_t   authunix_marshal(AUTH *, XDR *);
82 static bool_t   authunix_validate(AUTH *, struct opaque_auth);
83 static bool_t   authunix_refresh(AUTH *);
84 static void     authunix_destroy(AUTH *);
85
86 static struct auth_ops auth_unix_ops = {
87         authunix_nextverf,
88         authunix_marshal,
89         authunix_validate,
90         authunix_refresh,
91         authunix_destroy
92 };
93
94 /*
95  * This struct is pointed to by the ah_private field of an auth_handle.
96  */
97 struct audata {
98         struct opaque_auth      au_origcred;    /* original credentials */
99         struct opaque_auth      au_shcred;      /* short hand cred */
100         uint32_t                au_shfaults;    /* short hand cache faults */
101         char                    au_marshed[MAX_AUTH_BYTES];
102         uint_t                  au_mpos;        /* xdr pos at end of marshed */
103 };
104 #define AUTH_PRIVATE(auth)      ((struct audata *)auth->ah_private)
105
106 static bool_t marshal_new_auth(AUTH *);
107
108
109 /*
110  * Create a unix style authenticator.
111  * Returns an auth handle with the given stuff in it.
112  */
113 AUTH *
114 authunix_create(
115         char *machname,
116         uid_t uid,
117         gid_t gid,
118         int len,
119         gid_t *aup_gids)
120 {
121         struct authunix_parms aup;
122         char mymem[MAX_AUTH_BYTES];
123         struct timeval now;
124         XDR xdrs;
125         register AUTH *auth;
126         register struct audata *au;
127
128         /*
129          * Allocate and set up auth handle
130          */
131         auth = (AUTH *)mem_alloc(sizeof(AUTH));
132 #ifndef KERNEL
133         if (auth == NULL) {
134                 (void)fprintf(stderr, "authunix_create: out of memory\n");
135                 return (NULL);
136         }
137 #endif
138         au = (struct audata *)mem_alloc(sizeof(struct audata));
139 #ifndef KERNEL
140         if (au == NULL) {
141                 (void)fprintf(stderr, "authunix_create: out of memory\n");
142                 return (NULL);
143         }
144 #endif
145         auth->ah_ops = &auth_unix_ops;
146         auth->ah_private = (void *)au;
147         auth->ah_verf = au->au_shcred = _null_auth;
148         au->au_shfaults = 0;
149
150         /*
151          * fill in param struct from the given params
152          */
153         (void)gettimeofday(&now,  (struct timezone *)0);
154         aup.aup_time = now.tv_sec;
155         aup.aup_machname = machname;
156         aup.aup_uid = uid;
157         aup.aup_gid = gid;
158         aup.aup_len = (uint_t)len;
159         aup.aup_gids = (int *)aup_gids;
160
161         /*
162          * Serialize the parameters into origcred
163          */
164         xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
165         if (! xdr_authunix_parms(&xdrs, &aup)) 
166                 abort();
167         au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
168         au->au_origcred.oa_flavor = AUTH_UNIX;
169 #ifdef KERNEL
170         au->au_origcred.oa_base = mem_alloc((uint_t) len);
171 #else
172         if ((au->au_origcred.oa_base = mem_alloc((uint_t) len)) == NULL) {
173                 (void)fprintf(stderr, "authunix_create: out of memory\n");
174                 return (NULL);
175         }
176 #endif
177         memmove(au->au_origcred.oa_base, mymem, (uint_t)len);
178
179         /*
180          * set auth handle to reflect new cred.
181          */
182         auth->ah_cred = au->au_origcred;
183         marshal_new_auth(auth);
184         return (auth);
185 }
186
187 /*
188  * Returns an auth handle with parameters determined by doing lots of
189  * syscalls.
190  */
191 AUTH *
192 authunix_create_default(void)
193 {
194         register int len;
195         char machname[MAX_MACHINE_NAME + 1];
196         uid_t uid;
197         gid_t gid;
198         gid_t gids[NGRPS];
199
200         if (getmyhostname(machname, MAX_MACHINE_NAME) == -1)
201                 abort();
202         machname[MAX_MACHINE_NAME] = 0;
203         uid = geteuid();
204         gid = getegid();
205         if ((len = getgroups(NGRPS, gids)) < 0)
206                 abort();
207         return (authunix_create(machname, uid, gid, len, gids));
208 }
209
210 /*
211  * authunix operations
212  */
213
214 /* ARGSUSED */
215 static void
216 authunix_nextverf(
217         AUTH *auth)
218 {
219         /* no action necessary */
220 }
221
222 static bool_t
223 authunix_marshal(
224         AUTH *auth,
225         XDR *xdrs)
226 {
227         register struct audata *au = AUTH_PRIVATE(auth);
228
229         return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
230 }
231
232 static bool_t
233 authunix_validate(
234         AUTH *auth,
235         struct opaque_auth verf)
236 {
237         register struct audata *au;
238         XDR xdrs;
239
240         if (verf.oa_flavor == AUTH_SHORT) {
241                 au = AUTH_PRIVATE(auth);
242                 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
243
244                 if (au->au_shcred.oa_base != NULL) {
245                         mem_free(au->au_shcred.oa_base,
246                             au->au_shcred.oa_length);
247                         au->au_shcred.oa_base = NULL;
248                 }
249                 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
250                         auth->ah_cred = au->au_shcred;
251                 } else {
252                         xdrs.x_op = XDR_FREE;
253                         (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
254                         au->au_shcred.oa_base = NULL;
255                         auth->ah_cred = au->au_origcred;
256                 }
257                 marshal_new_auth(auth);
258         }
259         return (TRUE);
260 }
261
262 static bool_t
263 authunix_refresh(
264         AUTH *auth)
265 {
266         register struct audata *au = AUTH_PRIVATE(auth);
267         struct authunix_parms aup;
268         struct timeval now;
269         XDR xdrs;
270         register int stat;
271
272         if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
273                 /* there is no hope.  Punt */
274                 return (FALSE);
275         }
276         au->au_shfaults ++;
277
278         /* first deserialize the creds back into a struct authunix_parms */
279         aup.aup_machname = NULL;
280         aup.aup_gids = (int *)NULL;
281         xdrmem_create(&xdrs, au->au_origcred.oa_base,
282             au->au_origcred.oa_length, XDR_DECODE);
283         stat = xdr_authunix_parms(&xdrs, &aup);
284         if (! stat) 
285                 goto done;
286
287         /* update the time and serialize in place */
288         (void)gettimeofday(&now, (struct timezone *)0);
289         aup.aup_time = now.tv_sec;
290         xdrs.x_op = XDR_ENCODE;
291         XDR_SETPOS(&xdrs, 0);
292         stat = xdr_authunix_parms(&xdrs, &aup);
293         if (! stat)
294                 goto done;
295         auth->ah_cred = au->au_origcred;
296         marshal_new_auth(auth);
297 done:
298         /* free the struct authunix_parms created by deserializing */
299         xdrs.x_op = XDR_FREE;
300         (void)xdr_authunix_parms(&xdrs, &aup);
301         XDR_DESTROY(&xdrs);
302         return (stat);
303 }
304
305 static void
306 authunix_destroy(
307         AUTH *auth)
308 {
309         register struct audata *au = AUTH_PRIVATE(auth);
310
311         mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
312
313         if (au->au_shcred.oa_base != NULL)
314                 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
315
316         mem_free(auth->ah_private, sizeof(struct audata));
317
318         if (auth->ah_verf.oa_base != NULL)
319                 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
320
321         mem_free((void *)auth, sizeof(AUTH));
322 }
323
324 /*
325  * Marshals (pre-serializes) an auth struct.
326  * sets private data, au_marshed and au_mpos
327  */
328 static bool_t
329 marshal_new_auth(
330         AUTH *auth)
331 {
332         XDR             xdr_stream;
333         register XDR    *xdrs = &xdr_stream;
334         register struct audata *au = AUTH_PRIVATE(auth);
335
336         xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
337         if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
338             (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
339                 perror("auth_none.c - Fatal marshalling problem");
340         } else {
341                 au->au_mpos = XDR_GETPOS(xdrs);
342         }
343         XDR_DESTROY(xdrs);
344         return (TRUE);
345 }