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 <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/refcount.h>
60 #include <sys/ucred.h>
62 #include <rpc/types.h>
67 #include <rpc/rpc_com.h>
70 static void authunix_nextverf (AUTH *);
71 static bool_t authunix_marshal (AUTH *, uint32_t, XDR *, struct mbuf *);
72 static bool_t authunix_validate (AUTH *, uint32_t, struct opaque_auth *,
74 static bool_t authunix_refresh (AUTH *, void *);
75 static void authunix_destroy (AUTH *);
76 static void marshal_new_auth (AUTH *);
78 static struct auth_ops authunix_ops = {
79 .ah_nextverf = authunix_nextverf,
80 .ah_marshal = authunix_marshal,
81 .ah_validate = authunix_validate,
82 .ah_refresh = authunix_refresh,
83 .ah_destroy = authunix_destroy,
87 * This struct is pointed to by the ah_private field of an auth_handle.
90 TAILQ_ENTRY(audata) au_link;
91 TAILQ_ENTRY(audata) au_alllink;
92 volatile u_int au_refs;
93 struct xucred au_xcred;
94 struct opaque_auth au_origcred; /* original credentials */
95 struct opaque_auth au_shcred; /* short hand cred */
96 u_long au_shfaults; /* short hand cache faults */
97 char au_marshed[MAX_AUTH_BYTES];
98 u_int au_mpos; /* xdr pos at end of marshed */
99 AUTH *au_auth; /* link back to AUTH */
101 TAILQ_HEAD(audata_list, audata);
102 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
104 #define AUTH_UNIX_HASH_SIZE 16
105 #define AUTH_UNIX_MAX 256
106 static struct audata_list auth_unix_cache[AUTH_UNIX_HASH_SIZE];
107 static struct audata_list auth_unix_all;
108 static struct sx auth_unix_lock;
109 static int auth_unix_count;
112 authunix_init(void *dummy)
116 for (i = 0; i < AUTH_UNIX_HASH_SIZE; i++)
117 TAILQ_INIT(&auth_unix_cache[i]);
118 TAILQ_INIT(&auth_unix_all);
119 sx_init(&auth_unix_lock, "auth_unix_lock");
121 SYSINIT(authunix_init, SI_SUB_KMEM, SI_ORDER_ANY, authunix_init, NULL);
124 * Create a unix style authenticator.
125 * Returns an auth handle with the given stuff in it.
128 authunix_create(struct ucred *cred)
132 char mymem[MAX_AUTH_BYTES];
135 struct audata *au, *tau;
140 if (auth_unix_count > AUTH_UNIX_MAX) {
141 while (auth_unix_count > AUTH_UNIX_MAX) {
142 sx_xlock(&auth_unix_lock);
143 tau = TAILQ_FIRST(&auth_unix_all);
144 th = HASHSTEP(HASHINIT, tau->au_xcred.cr_uid)
145 % AUTH_UNIX_HASH_SIZE;
146 TAILQ_REMOVE(&auth_unix_cache[th], tau, au_link);
147 TAILQ_REMOVE(&auth_unix_all, tau, au_alllink);
149 sx_xunlock(&auth_unix_lock);
150 AUTH_DESTROY(tau->au_auth);
155 * Hash the uid to see if we already have an AUTH with this cred.
157 h = HASHSTEP(HASHINIT, cred->cr_uid) % AUTH_UNIX_HASH_SIZE;
160 sx_slock(&auth_unix_lock);
161 TAILQ_FOREACH(au, &auth_unix_cache[h], au_link) {
162 if (!memcmp(&xcr, &au->au_xcred, sizeof(xcr))) {
163 refcount_acquire(&au->au_refs);
164 if (sx_try_upgrade(&auth_unix_lock)) {
166 * Keep auth_unix_all LRU sorted.
168 TAILQ_REMOVE(&auth_unix_all, au, au_alllink);
169 TAILQ_INSERT_TAIL(&auth_unix_all, au,
171 sx_xunlock(&auth_unix_lock);
173 sx_sunlock(&auth_unix_lock);
175 return (au->au_auth);
179 sx_sunlock(&auth_unix_lock);
182 * Allocate and set up auth handle
185 auth = mem_alloc(sizeof(*auth));
186 au = mem_alloc(sizeof(*au));
187 auth->ah_ops = &authunix_ops;
188 auth->ah_private = (caddr_t)au;
189 auth->ah_verf = au->au_shcred = _null_auth;
190 refcount_init(&au->au_refs, 1);
193 au->au_origcred.oa_base = NULL;
200 * Serialize the parameters into origcred
202 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
204 if (! xdr_authunix_parms(&xdrs, &time, &xcr))
205 panic("authunix_create: failed to encode creds");
206 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
207 au->au_origcred.oa_flavor = AUTH_UNIX;
208 au->au_origcred.oa_base = mem_alloc((u_int) len);
209 memcpy(au->au_origcred.oa_base, mymem, (size_t)len);
212 * set auth handle to reflect new cred.
214 auth->ah_cred = au->au_origcred;
215 marshal_new_auth(auth);
217 sx_xlock(&auth_unix_lock);
218 TAILQ_FOREACH(tau, &auth_unix_cache[h], au_link) {
219 if (!memcmp(&xcr, &tau->au_xcred, sizeof(xcr))) {
221 * We lost a race to create the AUTH that
224 sx_xunlock(&auth_unix_lock);
231 TAILQ_INSERT_TAIL(&auth_unix_cache[h], au, au_link);
232 TAILQ_INSERT_TAIL(&auth_unix_all, au, au_alllink);
233 refcount_acquire(&au->au_refs); /* one for the cache, one for user */
234 sx_xunlock(&auth_unix_lock);
240 * authunix operations
245 authunix_nextverf(AUTH *auth)
247 /* no action necessary */
251 authunix_marshal(AUTH *auth, uint32_t xid, XDR *xdrs, struct mbuf *args)
255 au = AUTH_PRIVATE(auth);
256 if (!XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos))
259 xdrmbuf_append(xdrs, args);
265 authunix_validate(AUTH *auth, uint32_t xid, struct opaque_auth *verf,
274 if (verf->oa_flavor == AUTH_SHORT) {
275 au = AUTH_PRIVATE(auth);
276 xdrmem_create(&txdrs, verf->oa_base, verf->oa_length,
279 if (au->au_shcred.oa_base != NULL) {
280 mem_free(au->au_shcred.oa_base,
281 au->au_shcred.oa_length);
282 au->au_shcred.oa_base = NULL;
284 if (xdr_opaque_auth(&txdrs, &au->au_shcred)) {
285 auth->ah_cred = au->au_shcred;
287 txdrs.x_op = XDR_FREE;
288 (void)xdr_opaque_auth(&txdrs, &au->au_shcred);
289 au->au_shcred.oa_base = NULL;
290 auth->ah_cred = au->au_origcred;
292 marshal_new_auth(auth);
299 authunix_refresh(AUTH *auth, void *dummy)
301 struct audata *au = AUTH_PRIVATE(auth);
308 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
309 /* there is no hope. Punt */
314 /* first deserialize the creds back into a struct ucred */
315 xdrmem_create(&xdrs, au->au_origcred.oa_base,
316 au->au_origcred.oa_length, XDR_DECODE);
317 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
321 /* update the time and serialize in place */
324 xdrs.x_op = XDR_ENCODE;
325 XDR_SETPOS(&xdrs, 0);
327 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
330 auth->ah_cred = au->au_origcred;
331 marshal_new_auth(auth);
338 authunix_destroy(AUTH *auth)
342 au = AUTH_PRIVATE(auth);
344 if (!refcount_release(&au->au_refs))
347 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
349 if (au->au_shcred.oa_base != NULL)
350 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
352 mem_free(auth->ah_private, sizeof(struct audata));
354 if (auth->ah_verf.oa_base != NULL)
355 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
357 mem_free(auth, sizeof(*auth));
361 * Marshals (pre-serializes) an auth struct.
362 * sets private data, au_marshed and au_mpos
365 marshal_new_auth(AUTH *auth)
368 XDR *xdrs = &xdr_stream;
371 au = AUTH_PRIVATE(auth);
372 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
373 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
374 (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
375 printf("auth_none.c - Fatal marshalling problem");
377 au->au_mpos = XDR_GETPOS(xdrs);