2 static char sfs_auth_unix_c_id[] = "@(#)auth_unix.c 2.1 97/10/23";
4 /* @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC */
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
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.
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.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
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.
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.
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.
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.
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.
47 * Sun Microsystems, Inc.
49 * Mountain View, California 94043
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
56 * auth_unix.c, Implements UNIX style authentication parameters.
58 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
75 extern getmyhostname(char *, int);
78 * Unix authenticator operations vector
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 *);
86 static struct auth_ops auth_unix_ops = {
95 * This struct is pointed to by the ah_private field of an auth_handle.
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 */
104 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
106 static bool_t marshal_new_auth(AUTH *);
110 * Create a unix style authenticator.
111 * Returns an auth handle with the given stuff in it.
121 struct authunix_parms aup;
122 char mymem[MAX_AUTH_BYTES];
126 register struct audata *au;
129 * Allocate and set up auth handle
131 auth = (AUTH *)mem_alloc(sizeof(AUTH));
134 (void)fprintf(stderr, "authunix_create: out of memory\n");
138 au = (struct audata *)mem_alloc(sizeof(struct audata));
141 (void)fprintf(stderr, "authunix_create: out of memory\n");
145 auth->ah_ops = &auth_unix_ops;
146 auth->ah_private = (void *)au;
147 auth->ah_verf = au->au_shcred = _null_auth;
151 * fill in param struct from the given params
153 (void)gettimeofday(&now, (struct timezone *)0);
154 aup.aup_time = now.tv_sec;
155 aup.aup_machname = machname;
158 aup.aup_len = (uint_t)len;
159 aup.aup_gids = (int *)aup_gids;
162 * Serialize the parameters into origcred
164 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
165 if (! xdr_authunix_parms(&xdrs, &aup))
167 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
168 au->au_origcred.oa_flavor = AUTH_UNIX;
170 au->au_origcred.oa_base = mem_alloc((uint_t) len);
172 if ((au->au_origcred.oa_base = mem_alloc((uint_t) len)) == NULL) {
173 (void)fprintf(stderr, "authunix_create: out of memory\n");
177 memmove(au->au_origcred.oa_base, mymem, (uint_t)len);
180 * set auth handle to reflect new cred.
182 auth->ah_cred = au->au_origcred;
183 marshal_new_auth(auth);
188 * Returns an auth handle with parameters determined by doing lots of
192 authunix_create_default(void)
195 char machname[MAX_MACHINE_NAME + 1];
200 if (getmyhostname(machname, MAX_MACHINE_NAME) == -1)
202 machname[MAX_MACHINE_NAME] = 0;
205 if ((len = getgroups(NGRPS, gids)) < 0)
207 return (authunix_create(machname, uid, gid, len, gids));
211 * authunix operations
219 /* no action necessary */
227 register struct audata *au = AUTH_PRIVATE(auth);
229 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
235 struct opaque_auth verf)
237 register struct audata *au;
240 if (verf.oa_flavor == AUTH_SHORT) {
241 au = AUTH_PRIVATE(auth);
242 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
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;
249 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
250 auth->ah_cred = au->au_shcred;
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;
257 marshal_new_auth(auth);
266 register struct audata *au = AUTH_PRIVATE(auth);
267 struct authunix_parms aup;
272 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
273 /* there is no hope. Punt */
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);
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);
295 auth->ah_cred = au->au_origcred;
296 marshal_new_auth(auth);
298 /* free the struct authunix_parms created by deserializing */
299 xdrs.x_op = XDR_FREE;
300 (void)xdr_authunix_parms(&xdrs, &aup);
309 register struct audata *au = AUTH_PRIVATE(auth);
311 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
313 if (au->au_shcred.oa_base != NULL)
314 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
316 mem_free(auth->ah_private, sizeof(struct audata));
318 if (auth->ah_verf.oa_base != NULL)
319 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
321 mem_free((void *)auth, sizeof(AUTH));
325 * Marshals (pre-serializes) an auth struct.
326 * sets private data, au_marshed and au_mpos
333 register XDR *xdrs = &xdr_stream;
334 register struct audata *au = AUTH_PRIVATE(auth);
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");
341 au->au_mpos = XDR_GETPOS(xdrs);