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>
66 #include <rpc/rpc_com.h>
69 static void authunix_nextverf (AUTH *);
70 static bool_t authunix_marshal (AUTH *, XDR *);
71 static bool_t authunix_validate (AUTH *, struct opaque_auth *);
72 static bool_t authunix_refresh (AUTH *, void *);
73 static void authunix_destroy (AUTH *);
74 static void marshal_new_auth (AUTH *);
76 static struct auth_ops authunix_ops = {
77 .ah_nextverf = authunix_nextverf,
78 .ah_marshal = authunix_marshal,
79 .ah_validate = authunix_validate,
80 .ah_refresh = authunix_refresh,
81 .ah_destroy = authunix_destroy
85 * This struct is pointed to by the ah_private field of an auth_handle.
88 TAILQ_ENTRY(audata) au_link;
89 TAILQ_ENTRY(audata) au_alllink;
90 volatile u_int au_refs;
91 struct xucred au_xcred;
92 struct opaque_auth au_origcred; /* original credentials */
93 struct opaque_auth au_shcred; /* short hand cred */
94 u_long au_shfaults; /* short hand cache faults */
95 char au_marshed[MAX_AUTH_BYTES];
96 u_int au_mpos; /* xdr pos at end of marshed */
97 AUTH *au_auth; /* link back to AUTH */
99 TAILQ_HEAD(audata_list, audata);
100 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
102 #define AUTH_UNIX_HASH_SIZE 16
103 #define AUTH_UNIX_MAX 256
104 static struct audata_list auth_unix_cache[AUTH_UNIX_HASH_SIZE];
105 static struct audata_list auth_unix_all;
106 static struct sx auth_unix_lock;
107 static int auth_unix_count;
110 authunix_init(void *dummy)
114 for (i = 0; i < AUTH_UNIX_HASH_SIZE; i++)
115 TAILQ_INIT(&auth_unix_cache[i]);
116 TAILQ_INIT(&auth_unix_all);
117 sx_init(&auth_unix_lock, "auth_unix_lock");
119 SYSINIT(authunix_init, SI_SUB_KMEM, SI_ORDER_ANY, authunix_init, NULL);
122 * Create a unix style authenticator.
123 * Returns an auth handle with the given stuff in it.
126 authunix_create(struct ucred *cred)
130 char mymem[MAX_AUTH_BYTES];
133 struct audata *au, *tau;
138 if (auth_unix_count > AUTH_UNIX_MAX) {
139 while (auth_unix_count > AUTH_UNIX_MAX) {
140 sx_xlock(&auth_unix_lock);
141 tau = TAILQ_FIRST(&auth_unix_all);
142 th = HASHSTEP(HASHINIT, tau->au_xcred.cr_uid)
143 % AUTH_UNIX_HASH_SIZE;
144 TAILQ_REMOVE(&auth_unix_cache[th], tau, au_link);
145 TAILQ_REMOVE(&auth_unix_all, tau, au_alllink);
147 sx_xunlock(&auth_unix_lock);
148 AUTH_DESTROY(tau->au_auth);
153 * Hash the uid to see if we already have an AUTH with this cred.
155 h = HASHSTEP(HASHINIT, cred->cr_uid) % AUTH_UNIX_HASH_SIZE;
158 sx_slock(&auth_unix_lock);
159 TAILQ_FOREACH(au, &auth_unix_cache[h], au_link) {
160 if (!memcmp(&xcr, &au->au_xcred, sizeof(xcr))) {
161 refcount_acquire(&au->au_refs);
162 if (sx_try_upgrade(&auth_unix_lock)) {
164 * Keep auth_unix_all LRU sorted.
166 TAILQ_REMOVE(&auth_unix_all, au, au_alllink);
167 TAILQ_INSERT_TAIL(&auth_unix_all, au,
169 sx_xunlock(&auth_unix_lock);
171 sx_sunlock(&auth_unix_lock);
173 return (au->au_auth);
177 sx_sunlock(&auth_unix_lock);
180 * Allocate and set up auth handle
183 auth = mem_alloc(sizeof(*auth));
184 au = mem_alloc(sizeof(*au));
185 auth->ah_ops = &authunix_ops;
186 auth->ah_private = (caddr_t)au;
187 auth->ah_verf = au->au_shcred = _null_auth;
188 refcount_init(&au->au_refs, 1);
191 au->au_origcred.oa_base = NULL;
198 * Serialize the parameters into origcred
200 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
202 if (! xdr_authunix_parms(&xdrs, &time, &xcr))
203 panic("authunix_create: failed to encode creds");
204 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
205 au->au_origcred.oa_flavor = AUTH_UNIX;
206 au->au_origcred.oa_base = mem_alloc((u_int) len);
207 memcpy(au->au_origcred.oa_base, mymem, (size_t)len);
210 * set auth handle to reflect new cred.
212 auth->ah_cred = au->au_origcred;
213 marshal_new_auth(auth);
215 sx_xlock(&auth_unix_lock);
216 TAILQ_FOREACH(tau, &auth_unix_cache[h], au_link) {
217 if (!memcmp(&xcr, &tau->au_xcred, sizeof(xcr))) {
219 * We lost a race to create the AUTH that
222 sx_xunlock(&auth_unix_lock);
229 TAILQ_INSERT_TAIL(&auth_unix_cache[h], au, au_link);
230 TAILQ_INSERT_TAIL(&auth_unix_all, au, au_alllink);
231 refcount_acquire(&au->au_refs); /* one for the cache, one for user */
232 sx_xunlock(&auth_unix_lock);
238 * authunix operations
243 authunix_nextverf(AUTH *auth)
245 /* no action necessary */
249 authunix_marshal(AUTH *auth, XDR *xdrs)
253 au = AUTH_PRIVATE(auth);
254 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
258 authunix_validate(AUTH *auth, struct opaque_auth *verf)
263 if (verf->oa_flavor == AUTH_SHORT) {
264 au = AUTH_PRIVATE(auth);
265 xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
268 if (au->au_shcred.oa_base != NULL) {
269 mem_free(au->au_shcred.oa_base,
270 au->au_shcred.oa_length);
271 au->au_shcred.oa_base = NULL;
273 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
274 auth->ah_cred = au->au_shcred;
276 xdrs.x_op = XDR_FREE;
277 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
278 au->au_shcred.oa_base = NULL;
279 auth->ah_cred = au->au_origcred;
281 marshal_new_auth(auth);
287 authunix_refresh(AUTH *auth, void *dummy)
289 struct audata *au = AUTH_PRIVATE(auth);
296 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
297 /* there is no hope. Punt */
302 /* first deserialize the creds back into a struct ucred */
303 xdrmem_create(&xdrs, au->au_origcred.oa_base,
304 au->au_origcred.oa_length, XDR_DECODE);
305 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
309 /* update the time and serialize in place */
312 xdrs.x_op = XDR_ENCODE;
313 XDR_SETPOS(&xdrs, 0);
315 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
318 auth->ah_cred = au->au_origcred;
319 marshal_new_auth(auth);
326 authunix_destroy(AUTH *auth)
330 au = AUTH_PRIVATE(auth);
332 if (!refcount_release(&au->au_refs))
335 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
337 if (au->au_shcred.oa_base != NULL)
338 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
340 mem_free(auth->ah_private, sizeof(struct audata));
342 if (auth->ah_verf.oa_base != NULL)
343 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
345 mem_free(auth, sizeof(*auth));
349 * Marshals (pre-serializes) an auth struct.
350 * sets private data, au_marshed and au_mpos
353 marshal_new_auth(AUTH *auth)
356 XDR *xdrs = &xdr_stream;
359 au = AUTH_PRIVATE(auth);
360 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
361 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
362 (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
363 printf("auth_none.c - Fatal marshalling problem");
365 au->au_mpos = XDR_GETPOS(xdrs);