1 /* $NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * auth_unix.c, Implements UNIX style authentication parameters.
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * The system is very weak. The client uses no encryption for it's
46 * credentials and only sends null verifiers. The server sends backs
47 * null verifiers or optionally a verifier that suggests a new short hand
48 * for the credentials.
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/refcount.h>
61 #include <sys/ucred.h>
63 #include <rpc/types.h>
68 #include <rpc/rpc_com.h>
71 static void authunix_nextverf (AUTH *);
72 static bool_t authunix_marshal (AUTH *, uint32_t, XDR *, struct mbuf *);
73 static bool_t authunix_validate (AUTH *, uint32_t, struct opaque_auth *,
75 static bool_t authunix_refresh (AUTH *, void *);
76 static void authunix_destroy (AUTH *);
77 static void marshal_new_auth (AUTH *);
79 static struct auth_ops authunix_ops = {
80 .ah_nextverf = authunix_nextverf,
81 .ah_marshal = authunix_marshal,
82 .ah_validate = authunix_validate,
83 .ah_refresh = authunix_refresh,
84 .ah_destroy = authunix_destroy,
88 * This struct is pointed to by the ah_private field of an auth_handle.
91 TAILQ_ENTRY(audata) au_link;
92 TAILQ_ENTRY(audata) au_alllink;
93 volatile u_int au_refs;
94 struct xucred au_xcred;
95 struct opaque_auth au_origcred; /* original credentials */
96 struct opaque_auth au_shcred; /* short hand cred */
97 u_long au_shfaults; /* short hand cache faults */
98 char au_marshed[MAX_AUTH_BYTES];
99 u_int au_mpos; /* xdr pos at end of marshed */
100 AUTH *au_auth; /* link back to AUTH */
102 TAILQ_HEAD(audata_list, audata);
103 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
105 #define AUTH_UNIX_HASH_SIZE 16
106 #define AUTH_UNIX_MAX 256
107 static struct audata_list auth_unix_cache[AUTH_UNIX_HASH_SIZE];
108 static struct audata_list auth_unix_all;
109 static struct sx auth_unix_lock;
110 static int auth_unix_count;
113 authunix_init(void *dummy)
117 for (i = 0; i < AUTH_UNIX_HASH_SIZE; i++)
118 TAILQ_INIT(&auth_unix_cache[i]);
119 TAILQ_INIT(&auth_unix_all);
120 sx_init(&auth_unix_lock, "auth_unix_lock");
122 SYSINIT(authunix_init, SI_SUB_KMEM, SI_ORDER_ANY, authunix_init, NULL);
125 * Create a unix style authenticator.
126 * Returns an auth handle with the given stuff in it.
129 authunix_create(struct ucred *cred)
133 char mymem[MAX_AUTH_BYTES];
136 struct audata *au, *tau;
141 if (auth_unix_count > AUTH_UNIX_MAX) {
142 while (auth_unix_count > AUTH_UNIX_MAX) {
143 sx_xlock(&auth_unix_lock);
144 tau = TAILQ_FIRST(&auth_unix_all);
145 th = HASHSTEP(HASHINIT, tau->au_xcred.cr_uid)
146 % AUTH_UNIX_HASH_SIZE;
147 TAILQ_REMOVE(&auth_unix_cache[th], tau, au_link);
148 TAILQ_REMOVE(&auth_unix_all, tau, au_alllink);
150 sx_xunlock(&auth_unix_lock);
151 AUTH_DESTROY(tau->au_auth);
156 * Hash the uid to see if we already have an AUTH with this cred.
158 h = HASHSTEP(HASHINIT, cred->cr_uid) % AUTH_UNIX_HASH_SIZE;
161 sx_slock(&auth_unix_lock);
162 TAILQ_FOREACH(au, &auth_unix_cache[h], au_link) {
163 if (!memcmp(&xcr, &au->au_xcred, sizeof(xcr))) {
164 refcount_acquire(&au->au_refs);
165 if (sx_try_upgrade(&auth_unix_lock)) {
167 * Keep auth_unix_all LRU sorted.
169 TAILQ_REMOVE(&auth_unix_all, au, au_alllink);
170 TAILQ_INSERT_TAIL(&auth_unix_all, au,
172 sx_xunlock(&auth_unix_lock);
174 sx_sunlock(&auth_unix_lock);
176 return (au->au_auth);
180 sx_sunlock(&auth_unix_lock);
183 * Allocate and set up auth handle
186 auth = mem_alloc(sizeof(*auth));
187 au = mem_alloc(sizeof(*au));
188 auth->ah_ops = &authunix_ops;
189 auth->ah_private = (caddr_t)au;
190 auth->ah_verf = au->au_shcred = _null_auth;
191 refcount_init(&au->au_refs, 1);
194 au->au_origcred.oa_base = NULL;
201 * Serialize the parameters into origcred
203 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
205 if (! xdr_authunix_parms(&xdrs, &time, &xcr))
206 panic("authunix_create: failed to encode creds");
207 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
208 au->au_origcred.oa_flavor = AUTH_UNIX;
209 au->au_origcred.oa_base = mem_alloc((u_int) len);
210 memcpy(au->au_origcred.oa_base, mymem, (size_t)len);
213 * set auth handle to reflect new cred.
215 auth->ah_cred = au->au_origcred;
216 marshal_new_auth(auth);
218 sx_xlock(&auth_unix_lock);
219 TAILQ_FOREACH(tau, &auth_unix_cache[h], au_link) {
220 if (!memcmp(&xcr, &tau->au_xcred, sizeof(xcr))) {
222 * We lost a race to create the AUTH that
225 sx_xunlock(&auth_unix_lock);
232 TAILQ_INSERT_TAIL(&auth_unix_cache[h], au, au_link);
233 TAILQ_INSERT_TAIL(&auth_unix_all, au, au_alllink);
234 refcount_acquire(&au->au_refs); /* one for the cache, one for user */
235 sx_xunlock(&auth_unix_lock);
241 * authunix operations
246 authunix_nextverf(AUTH *auth)
248 /* no action necessary */
252 authunix_marshal(AUTH *auth, uint32_t xid, XDR *xdrs, struct mbuf *args)
256 au = AUTH_PRIVATE(auth);
257 if (!XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos))
260 xdrmbuf_append(xdrs, args);
266 authunix_validate(AUTH *auth, uint32_t xid, struct opaque_auth *verf,
275 if (verf->oa_flavor == AUTH_SHORT) {
276 au = AUTH_PRIVATE(auth);
277 xdrmem_create(&txdrs, verf->oa_base, verf->oa_length,
280 if (au->au_shcred.oa_base != NULL) {
281 mem_free(au->au_shcred.oa_base,
282 au->au_shcred.oa_length);
283 au->au_shcred.oa_base = NULL;
285 if (xdr_opaque_auth(&txdrs, &au->au_shcred)) {
286 auth->ah_cred = au->au_shcred;
288 txdrs.x_op = XDR_FREE;
289 (void)xdr_opaque_auth(&txdrs, &au->au_shcred);
290 au->au_shcred.oa_base = NULL;
291 auth->ah_cred = au->au_origcred;
293 marshal_new_auth(auth);
300 authunix_refresh(AUTH *auth, void *dummy)
302 struct audata *au = AUTH_PRIVATE(auth);
309 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
310 /* there is no hope. Punt */
315 /* first deserialize the creds back into a struct ucred */
316 xdrmem_create(&xdrs, au->au_origcred.oa_base,
317 au->au_origcred.oa_length, XDR_DECODE);
318 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
322 /* update the time and serialize in place */
325 xdrs.x_op = XDR_ENCODE;
326 XDR_SETPOS(&xdrs, 0);
328 stat = xdr_authunix_parms(&xdrs, &time, &xcr);
331 auth->ah_cred = au->au_origcred;
332 marshal_new_auth(auth);
339 authunix_destroy(AUTH *auth)
343 au = AUTH_PRIVATE(auth);
345 if (!refcount_release(&au->au_refs))
348 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
350 if (au->au_shcred.oa_base != NULL)
351 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
353 mem_free(auth->ah_private, sizeof(struct audata));
355 if (auth->ah_verf.oa_base != NULL)
356 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
358 mem_free(auth, sizeof(*auth));
362 * Marshals (pre-serializes) an auth struct.
363 * sets private data, au_marshed and au_mpos
366 marshal_new_auth(AUTH *auth)
369 XDR *xdrs = &xdr_stream;
372 au = AUTH_PRIVATE(auth);
373 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
374 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
375 (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
376 printf("auth_none.c - Fatal marshalling problem");
378 au->au_mpos = XDR_GETPOS(xdrs);