auth_unix.c revision 180743
1/*	$NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $	*/
2
3/*
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.
10 *
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.
14 *
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.
18 *
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.
22 *
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.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California  94043
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33static char *sccsid2 = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
34static char *sccsid = "@(#)auth_unix.c	2.2 88/08/01 4.0 RPCSRC";
35#endif
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/rpc/auth_unix.c 180743 2008-07-23 09:18:08Z dfr $");
38
39/*
40 * auth_unix.c, Implements UNIX style authentication parameters.
41 *
42 * Copyright (C) 1984, Sun Microsystems, Inc.
43 *
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.
48 *
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/hash.h>
54#include <sys/kernel.h>
55#include <sys/lock.h>
56#include <sys/malloc.h>
57#include <sys/pcpu.h>
58#include <sys/refcount.h>
59#include <sys/sx.h>
60#include <sys/ucred.h>
61
62#include <rpc/types.h>
63#include <rpc/xdr.h>
64#include <rpc/auth.h>
65
66#include <rpc/rpc_com.h>
67
68/* auth_unix.c */
69static void authunix_nextverf (AUTH *);
70static bool_t authunix_marshal (AUTH *, XDR *);
71static bool_t authunix_validate (AUTH *, struct opaque_auth *);
72static bool_t authunix_refresh (AUTH *, void *);
73static void authunix_destroy (AUTH *);
74static void marshal_new_auth (AUTH *);
75
76static 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
82};
83
84/*
85 * This struct is pointed to by the ah_private field of an auth_handle.
86 */
87struct audata {
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 */
98};
99TAILQ_HEAD(audata_list, audata);
100#define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
101
102#define AUTH_UNIX_HASH_SIZE	16
103#define AUTH_UNIX_MAX		256
104static struct audata_list auth_unix_cache[AUTH_UNIX_HASH_SIZE];
105static struct audata_list auth_unix_all;
106static struct sx auth_unix_lock;
107static int auth_unix_count;
108
109static void
110authunix_init(void *dummy)
111{
112	int i;
113
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");
118}
119SYSINIT(authunix_init, SI_SUB_KMEM, SI_ORDER_ANY, authunix_init, NULL);
120
121/*
122 * Create a unix style authenticator.
123 * Returns an auth handle with the given stuff in it.
124 */
125AUTH *
126authunix_create(struct ucred *cred)
127{
128	uint32_t h, th;
129	struct xucred xcr;
130	char mymem[MAX_AUTH_BYTES];
131	XDR xdrs;
132	AUTH *auth;
133	struct audata *au, *tau;
134	struct timeval now;
135	uint32_t time;
136	int len;
137
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);
146			auth_unix_count--;
147			sx_xunlock(&auth_unix_lock);
148			AUTH_DESTROY(tau->au_auth);
149		}
150	}
151
152	/*
153	 * Hash the uid to see if we already have an AUTH with this cred.
154	 */
155	h = HASHSTEP(HASHINIT, cred->cr_uid) % AUTH_UNIX_HASH_SIZE;
156	cru2x(cred, &xcr);
157again:
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)) {
163				/*
164				 * Keep auth_unix_all LRU sorted.
165				 */
166				TAILQ_REMOVE(&auth_unix_all, au, au_alllink);
167				TAILQ_INSERT_TAIL(&auth_unix_all, au,
168				    au_alllink);
169				sx_xunlock(&auth_unix_lock);
170			} else {
171				sx_sunlock(&auth_unix_lock);
172			}
173			return (au->au_auth);
174		}
175	}
176
177	sx_sunlock(&auth_unix_lock);
178
179	/*
180	 * Allocate and set up auth handle
181	 */
182	au = NULL;
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);
189	au->au_xcred = xcr;
190	au->au_shfaults = 0;
191	au->au_origcred.oa_base = NULL;
192	au->au_auth = auth;
193
194	getmicrotime(&now);
195	time = now.tv_sec;
196
197	/*
198	 * Serialize the parameters into origcred
199	 */
200	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
201	cru2x(cred, &xcr);
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);
208
209	/*
210	 * set auth handle to reflect new cred.
211	 */
212	auth->ah_cred = au->au_origcred;
213	marshal_new_auth(auth);
214
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))) {
218			/*
219			 * We lost a race to create the AUTH that
220			 * matches this cred.
221			 */
222			sx_xunlock(&auth_unix_lock);
223			AUTH_DESTROY(auth);
224			goto again;
225		}
226	}
227
228	auth_unix_count++;
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);
233
234	return (auth);
235}
236
237/*
238 * authunix operations
239 */
240
241/* ARGSUSED */
242static void
243authunix_nextverf(AUTH *auth)
244{
245	/* no action necessary */
246}
247
248static bool_t
249authunix_marshal(AUTH *auth, XDR *xdrs)
250{
251	struct audata *au;
252
253	au = AUTH_PRIVATE(auth);
254	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
255}
256
257static bool_t
258authunix_validate(AUTH *auth, struct opaque_auth *verf)
259{
260	struct audata *au;
261	XDR xdrs;
262
263	if (verf->oa_flavor == AUTH_SHORT) {
264		au = AUTH_PRIVATE(auth);
265		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
266		    XDR_DECODE);
267
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;
272		}
273		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
274			auth->ah_cred = au->au_shcred;
275		} else {
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;
280		}
281		marshal_new_auth(auth);
282	}
283	return (TRUE);
284}
285
286static bool_t
287authunix_refresh(AUTH *auth, void *dummy)
288{
289	struct audata *au = AUTH_PRIVATE(auth);
290	struct xucred xcr;
291	uint32_t time;
292	struct timeval now;
293	XDR xdrs;
294	int stat;
295
296	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
297		/* there is no hope.  Punt */
298		return (FALSE);
299	}
300	au->au_shfaults ++;
301
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);
306	if (! stat)
307		goto done;
308
309	/* update the time and serialize in place */
310	getmicrotime(&now);
311	time = now.tv_sec;
312	xdrs.x_op = XDR_ENCODE;
313	XDR_SETPOS(&xdrs, 0);
314
315	stat = xdr_authunix_parms(&xdrs, &time, &xcr);
316	if (! stat)
317		goto done;
318	auth->ah_cred = au->au_origcred;
319	marshal_new_auth(auth);
320done:
321	XDR_DESTROY(&xdrs);
322	return (stat);
323}
324
325static void
326authunix_destroy(AUTH *auth)
327{
328	struct audata *au;
329
330	au = AUTH_PRIVATE(auth);
331
332	if (!refcount_release(&au->au_refs))
333		return;
334
335	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
336
337	if (au->au_shcred.oa_base != NULL)
338		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
339
340	mem_free(auth->ah_private, sizeof(struct audata));
341
342	if (auth->ah_verf.oa_base != NULL)
343		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
344
345	mem_free(auth, sizeof(*auth));
346}
347
348/*
349 * Marshals (pre-serializes) an auth struct.
350 * sets private data, au_marshed and au_mpos
351 */
352static void
353marshal_new_auth(AUTH *auth)
354{
355	XDR	xdr_stream;
356	XDR	*xdrs = &xdr_stream;
357	struct audata *au;
358
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");
364	else
365		au->au_mpos = XDR_GETPOS(xdrs);
366	XDR_DESTROY(xdrs);
367}
368