1 /* $NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * auth_unix.c, Implements UNIX style authentication parameters.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
44 * The system is very weak. The client uses no encryption for it's
45 * credentials and only sends null verifiers. The server sends backs
46 * null verifiers or optionally a verifier that suggests a new short hand
47 * for the credentials.
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include <sys/param.h>
62 #include <rpc/types.h>
65 #include <rpc/auth_unix.h>
66 #include "un-namespace.h"
70 static void authunix_nextverf (AUTH *);
71 static bool_t authunix_marshal (AUTH *, XDR *);
72 static bool_t authunix_validate (AUTH *, struct opaque_auth *);
73 static bool_t authunix_refresh (AUTH *, void *);
74 static void authunix_destroy (AUTH *);
75 static void marshal_new_auth (AUTH *);
76 static struct auth_ops *authunix_ops (void);
79 * This struct is pointed to by the ah_private field of an auth_handle.
82 struct opaque_auth au_origcred; /* original credentials */
83 struct opaque_auth au_shcred; /* short hand cred */
84 u_long au_shfaults; /* short hand cache faults */
85 char au_marshed[MAX_AUTH_BYTES];
86 u_int au_mpos; /* xdr pos at end of marshed */
88 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
91 * Create a unix style authenticator.
92 * Returns an auth handle with the given stuff in it.
95 authunix_create(machname, uid, gid, len, aup_gids)
102 struct authunix_parms aup;
103 char mymem[MAX_AUTH_BYTES];
110 * Allocate and set up auth handle
113 auth = mem_alloc(sizeof(*auth));
116 warnx("authunix_create: out of memory");
117 goto cleanup_authunix_create;
120 au = mem_alloc(sizeof(*au));
123 warnx("authunix_create: out of memory");
124 goto cleanup_authunix_create;
127 auth->ah_ops = authunix_ops();
128 auth->ah_private = (caddr_t)au;
129 auth->ah_verf = au->au_shcred = _null_auth;
131 au->au_origcred.oa_base = NULL;
134 * fill in param struct from the given params
136 (void)gettimeofday(&now, NULL);
137 aup.aup_time = now.tv_sec;
138 aup.aup_machname = machname;
141 aup.aup_len = (u_int)len;
142 aup.aup_gids = aup_gids;
145 * Serialize the parameters into origcred
147 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
148 if (! xdr_authunix_parms(&xdrs, &aup))
150 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
151 au->au_origcred.oa_flavor = AUTH_UNIX;
153 au->au_origcred.oa_base = mem_alloc((u_int) len);
155 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
156 warnx("authunix_create: out of memory");
157 goto cleanup_authunix_create;
160 memmove(au->au_origcred.oa_base, mymem, (size_t)len);
163 * set auth handle to reflect new cred.
165 auth->ah_cred = au->au_origcred;
166 marshal_new_auth(auth);
169 cleanup_authunix_create:
171 mem_free(auth, sizeof(*auth));
173 if (au->au_origcred.oa_base)
174 mem_free(au->au_origcred.oa_base, (u_int)len);
175 mem_free(au, sizeof(*au));
182 * Returns an auth handle with parameters determined by doing lots of
186 authunix_create_default()
189 char machname[MAXHOSTNAMELEN + 1];
192 gid_t gids[NGROUPS_MAX];
194 if (gethostname(machname, sizeof machname) == -1)
196 machname[sizeof(machname) - 1] = 0;
199 if ((len = getgroups(NGROUPS_MAX, gids)) < 0)
203 /* XXX: interface problem; those should all have been unsigned */
204 return (authunix_create(machname, (int)uid, (int)gid, len,
209 * authunix operations
214 authunix_nextverf(auth)
217 /* no action necessary */
221 authunix_marshal(auth, xdrs)
227 assert(auth != NULL);
228 assert(xdrs != NULL);
230 au = AUTH_PRIVATE(auth);
231 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
235 authunix_validate(auth, verf)
237 struct opaque_auth *verf;
242 assert(auth != NULL);
243 assert(verf != NULL);
245 if (verf->oa_flavor == AUTH_SHORT) {
246 au = AUTH_PRIVATE(auth);
247 xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
250 if (au->au_shcred.oa_base != NULL) {
251 mem_free(au->au_shcred.oa_base,
252 au->au_shcred.oa_length);
253 au->au_shcred.oa_base = NULL;
255 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
256 auth->ah_cred = au->au_shcred;
258 xdrs.x_op = XDR_FREE;
259 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
260 au->au_shcred.oa_base = NULL;
261 auth->ah_cred = au->au_origcred;
263 marshal_new_auth(auth);
269 authunix_refresh(AUTH *auth, void *dummy)
271 struct audata *au = AUTH_PRIVATE(auth);
272 struct authunix_parms aup;
277 assert(auth != NULL);
279 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
280 /* there is no hope. Punt */
285 /* first deserialize the creds back into a struct authunix_parms */
286 aup.aup_machname = NULL;
288 xdrmem_create(&xdrs, au->au_origcred.oa_base,
289 au->au_origcred.oa_length, XDR_DECODE);
290 stat = xdr_authunix_parms(&xdrs, &aup);
294 /* update the time and serialize in place */
295 (void)gettimeofday(&now, NULL);
296 aup.aup_time = now.tv_sec;
297 xdrs.x_op = XDR_ENCODE;
298 XDR_SETPOS(&xdrs, 0);
299 stat = xdr_authunix_parms(&xdrs, &aup);
302 auth->ah_cred = au->au_origcred;
303 marshal_new_auth(auth);
305 /* free the struct authunix_parms created by deserializing */
306 xdrs.x_op = XDR_FREE;
307 (void)xdr_authunix_parms(&xdrs, &aup);
313 authunix_destroy(auth)
318 assert(auth != NULL);
320 au = AUTH_PRIVATE(auth);
321 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
323 if (au->au_shcred.oa_base != NULL)
324 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
326 mem_free(auth->ah_private, sizeof(struct audata));
328 if (auth->ah_verf.oa_base != NULL)
329 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
331 mem_free(auth, sizeof(*auth));
335 * Marshals (pre-serializes) an auth struct.
336 * sets private data, au_marshed and au_mpos
339 marshal_new_auth(auth)
343 XDR *xdrs = &xdr_stream;
346 assert(auth != NULL);
348 au = AUTH_PRIVATE(auth);
349 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
350 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
351 (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
352 warnx("auth_none.c - Fatal marshalling problem");
354 au->au_mpos = XDR_GETPOS(xdrs);
358 static struct auth_ops *
361 static struct auth_ops ops;
363 /* VARIABLES PROTECTED BY ops_lock: ops */
365 mutex_lock(&ops_lock);
366 if (ops.ah_nextverf == NULL) {
367 ops.ah_nextverf = authunix_nextverf;
368 ops.ah_marshal = authunix_marshal;
369 ops.ah_validate = authunix_validate;
370 ops.ah_refresh = authunix_refresh;
371 ops.ah_destroy = authunix_destroy;
373 mutex_unlock(&ops_lock);