1 /*********************************************************************
2 * RPC for the Windows NT Operating System
3 * 1993 by Martin F. Gergeleit
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating
5 * System according to the Sun copyright below.
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10 *********************************************************************/
12 /* @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC */
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15 * unrestricted use provided that this legend is included on all tape
16 * media and as a part of the software program in whole or part. Users
17 * may copy or modify Sun RPC without charge, but are not authorized
18 * to license or distribute it to anyone else except as part of a product or
19 * program developed by the user.
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
25 * Sun RPC is provided with no support and without any obligation on the
26 * part of Sun Microsystems, Inc. to assist in its use, correction,
27 * modification or enhancement.
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31 * OR ANY PART THEREOF.
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34 * or profits or other special, indirect and consequential damages, even if
35 * Sun has been advised of the possibility of such damages.
37 * Sun Microsystems, Inc.
39 * Mountain View, California 94043
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
46 * auth_unix.c, Implements UNIX style authentication parameters.
48 * Copyright (C) 1984, Sun Microsystems, Inc.
50 * The system is very weak. The client uses no encryption for it's
51 * credentials and only sends null verifiers. The server sends backs
52 * null verifiers or optionally a verifier that suggests a new short hand
53 * for the credentials.
63 #include <rpc/auth_unix.h>
65 #include <rpc/types.h>
69 #include <rpc/auth_unix.h>
73 * Unix authenticator operations vector
75 static void authunix_nextverf();
76 static bool_t authunix_marshal();
77 static bool_t authunix_validate();
78 static bool_t authunix_refresh();
79 static void authunix_destroy();
81 static struct auth_ops auth_unix_ops = {
90 * This struct is pointed to by the ah_private field of an auth_handle.
93 struct opaque_auth au_origcred; /* original credentials */
94 struct opaque_auth au_shcred; /* short hand cred */
95 u_long au_shfaults; /* short hand cache faults */
96 char au_marshed[MAX_AUTH_BYTES];
97 u_int au_mpos; /* xdr pos at end of marshed */
99 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
101 static bool_t marshal_new_auth();
105 * Create a unix style authenticator.
106 * Returns an auth handle with the given stuff in it.
109 authunix_create(machname, uid, gid, len, aup_gids)
116 struct authunix_parms aup;
117 char mymem[MAX_AUTH_BYTES];
121 register struct audata *au;
124 * Allocate and set up auth handle
126 auth = (AUTH *)mem_alloc(sizeof(*auth));
130 nt_rpc_report("authunix_create: out of memory\n");
132 (void)fprintf(stderr, "authunix_create: out of memory\n");
137 au = (struct audata *)mem_alloc(sizeof(*au));
141 nt_rpc_report("authunix_create: out of memory\n");
143 (void)fprintf(stderr, "authunix_create: out of memory\n");
148 auth->ah_ops = &auth_unix_ops;
149 auth->ah_private = (caddr_t)au;
150 auth->ah_verf = au->au_shcred = _null_auth;
154 * fill in param struct from the given params
156 (void)gettimeofday(&now, (struct timezone *)0);
157 aup.aup_time = now.tv_sec;
158 aup.aup_machname = machname;
161 aup.aup_len = (u_int)len;
162 aup.aup_gids = aup_gids;
165 * Serialize the parameters into origcred
167 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
168 if (! xdr_authunix_parms(&xdrs, &aup))
170 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
171 au->au_origcred.oa_flavor = AUTH_UNIX;
173 au->au_origcred.oa_base = mem_alloc((u_int) len);
175 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
177 nt_rpc_report("authunix_create: out of memory\n");
179 (void)fprintf(stderr, "authunix_create: out of memory\n");
184 bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
187 * set auth handle to reflect new cred.
189 auth->ah_cred = au->au_origcred;
190 marshal_new_auth(auth);
195 * Returns an auth handle with parameters determined by doing lots of
199 authunix_create_default()
202 char machname[MAX_MACHINE_NAME + 1];
207 if (gethostname(machname, MAX_MACHINE_NAME) == -1)
209 machname[MAX_MACHINE_NAME] = 0;
211 /* who knows anything better? */
219 if ((len = getgroups(NGRPS, gids)) < 0)
222 return (authunix_create(machname, uid, gid, len, gids));
226 * authunix operations
230 authunix_nextverf(auth)
233 /* no action necessary */
237 authunix_marshal(auth, xdrs)
241 register struct audata *au = AUTH_PRIVATE(auth);
243 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
247 authunix_validate(auth, verf)
249 struct opaque_auth verf;
251 register struct audata *au;
254 if (verf.oa_flavor == AUTH_SHORT) {
255 au = AUTH_PRIVATE(auth);
256 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
258 if (au->au_shcred.oa_base != NULL) {
259 mem_free(au->au_shcred.oa_base,
260 au->au_shcred.oa_length);
261 au->au_shcred.oa_base = NULL;
263 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
264 auth->ah_cred = au->au_shcred;
266 xdrs.x_op = XDR_FREE;
267 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
268 au->au_shcred.oa_base = NULL;
269 auth->ah_cred = au->au_origcred;
271 marshal_new_auth(auth);
277 authunix_refresh(auth)
280 register struct audata *au = AUTH_PRIVATE(auth);
281 struct authunix_parms aup;
286 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
287 /* there is no hope. Punt */
292 /* first deserialize the creds back into a struct authunix_parms */
293 aup.aup_machname = NULL;
294 aup.aup_gids = (int *)NULL;
295 xdrmem_create(&xdrs, au->au_origcred.oa_base,
296 au->au_origcred.oa_length, XDR_DECODE);
297 stat = xdr_authunix_parms(&xdrs, &aup);
301 /* update the time and serialize in place */
302 (void)gettimeofday(&now, (struct timezone *)0);
303 aup.aup_time = now.tv_sec;
304 xdrs.x_op = XDR_ENCODE;
305 XDR_SETPOS(&xdrs, 0);
306 stat = xdr_authunix_parms(&xdrs, &aup);
309 auth->ah_cred = au->au_origcred;
310 marshal_new_auth(auth);
312 /* free the struct authunix_parms created by deserializing */
313 xdrs.x_op = XDR_FREE;
314 (void)xdr_authunix_parms(&xdrs, &aup);
320 authunix_destroy(auth)
323 register struct audata *au = AUTH_PRIVATE(auth);
325 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
327 if (au->au_shcred.oa_base != NULL)
328 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
330 mem_free(auth->ah_private, sizeof(struct audata));
332 if (auth->ah_verf.oa_base != NULL)
333 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
335 mem_free((caddr_t)auth, sizeof(*auth));
339 * Marshals (pre-serializes) an auth struct.
340 * sets private data, au_marshed and au_mpos
343 marshal_new_auth(auth)
347 register XDR *xdrs = &xdr_stream;
348 register struct audata *au = AUTH_PRIVATE(auth);
350 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
351 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
352 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
353 perror("auth_none.c - Fatal marshalling problem");
357 au->au_mpos = XDR_GETPOS(xdrs);