auth_unix.c revision 180025
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 180025 2008-06-26 10:21:54Z 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/sx.h> 58#include <sys/ucred.h> 59 60#include <rpc/types.h> 61#include <rpc/xdr.h> 62#include <rpc/auth.h> 63 64#include <rpc/rpc_com.h> 65 66/* auth_unix.c */ 67static void authunix_nextverf (AUTH *); 68static bool_t authunix_marshal (AUTH *, XDR *); 69static bool_t authunix_validate (AUTH *, struct opaque_auth *); 70static bool_t authunix_refresh (AUTH *, void *); 71static void authunix_destroy (AUTH *); 72static void marshal_new_auth (AUTH *); 73 74static struct auth_ops authunix_ops = { 75 .ah_nextverf = authunix_nextverf, 76 .ah_marshal = authunix_marshal, 77 .ah_validate = authunix_validate, 78 .ah_refresh = authunix_refresh, 79 .ah_destroy = authunix_destroy 80}; 81 82/* 83 * This struct is pointed to by the ah_private field of an auth_handle. 84 */ 85struct audata { 86 TAILQ_ENTRY(audata) au_link; 87 TAILQ_ENTRY(audata) au_alllink; 88 int au_refs; 89 struct xucred au_xcred; 90 struct opaque_auth au_origcred; /* original credentials */ 91 struct opaque_auth au_shcred; /* short hand cred */ 92 u_long au_shfaults; /* short hand cache faults */ 93 char au_marshed[MAX_AUTH_BYTES]; 94 u_int au_mpos; /* xdr pos at end of marshed */ 95 AUTH *au_auth; /* link back to AUTH */ 96}; 97TAILQ_HEAD(audata_list, audata); 98#define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) 99 100#define AUTH_UNIX_HASH_SIZE 16 101#define AUTH_UNIX_MAX 256 102static struct audata_list auth_unix_cache[AUTH_UNIX_HASH_SIZE]; 103static struct audata_list auth_unix_all; 104static struct sx auth_unix_lock; 105static int auth_unix_count; 106 107static void 108authunix_init(void *dummy) 109{ 110 int i; 111 112 for (i = 0; i < AUTH_UNIX_HASH_SIZE; i++) 113 TAILQ_INIT(&auth_unix_cache[i]); 114 TAILQ_INIT(&auth_unix_all); 115 sx_init(&auth_unix_lock, "auth_unix_lock"); 116} 117SYSINIT(authunix_init, SI_SUB_KMEM, SI_ORDER_ANY, authunix_init, NULL); 118 119/* 120 * Create a unix style authenticator. 121 * Returns an auth handle with the given stuff in it. 122 */ 123AUTH * 124authunix_create(struct ucred *cred) 125{ 126 uint32_t h, th; 127 struct xucred xcr; 128 char mymem[MAX_AUTH_BYTES]; 129 XDR xdrs; 130 AUTH *auth; 131 struct audata *au, *tau; 132 struct timeval now; 133 uint32_t time; 134 int len; 135 136 if (auth_unix_count > AUTH_UNIX_MAX) { 137 while (auth_unix_count > AUTH_UNIX_MAX) { 138 sx_xlock(&auth_unix_lock); 139 tau = TAILQ_FIRST(&auth_unix_all); 140 th = HASHSTEP(HASHINIT, tau->au_xcred.cr_uid) 141 % AUTH_UNIX_HASH_SIZE; 142 TAILQ_REMOVE(&auth_unix_cache[th], tau, au_link); 143 TAILQ_REMOVE(&auth_unix_all, tau, au_alllink); 144 auth_unix_count--; 145 sx_xunlock(&auth_unix_lock); 146 AUTH_DESTROY(tau->au_auth); 147 } 148 } 149 150 /* 151 * Hash the uid to see if we already have an AUTH with this cred. 152 */ 153 h = HASHSTEP(HASHINIT, cred->cr_uid) % AUTH_UNIX_HASH_SIZE; 154 cru2x(cred, &xcr); 155again: 156 sx_slock(&auth_unix_lock); 157 TAILQ_FOREACH(au, &auth_unix_cache[h], au_link) { 158 if (!memcmp(&xcr, &au->au_xcred, sizeof(xcr))) { 159 if (sx_try_upgrade(&auth_unix_lock)) { 160 /* 161 * Keep auth_unix_all LRU sorted. 162 */ 163 TAILQ_REMOVE(&auth_unix_all, au, au_alllink); 164 TAILQ_INSERT_TAIL(&auth_unix_all, au, 165 au_alllink); 166 au->au_refs++; 167 sx_xunlock(&auth_unix_lock); 168 return (au->au_auth); 169 } else { 170 sx_sunlock(&auth_unix_lock); 171 goto again; 172 } 173 } 174 } 175 176 /* 177 * Allocate and set up auth handle 178 */ 179 au = NULL; 180 auth = mem_alloc(sizeof(*auth)); 181 au = mem_alloc(sizeof(*au)); 182 auth->ah_ops = &authunix_ops; 183 auth->ah_private = (caddr_t)au; 184 auth->ah_verf = au->au_shcred = _null_auth; 185 au->au_refs = 1; 186 au->au_xcred = xcr; 187 au->au_shfaults = 0; 188 au->au_origcred.oa_base = NULL; 189 au->au_auth = auth; 190 191 getmicrotime(&now); 192 time = now.tv_sec; 193 194 /* 195 * Serialize the parameters into origcred 196 */ 197 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); 198 cru2x(cred, &xcr); 199 if (! xdr_authunix_parms(&xdrs, &time, &xcr)) 200 panic("authunix_create: failed to encode creds"); 201 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); 202 au->au_origcred.oa_flavor = AUTH_UNIX; 203 au->au_origcred.oa_base = mem_alloc((u_int) len); 204 memcpy(au->au_origcred.oa_base, mymem, (size_t)len); 205 206 /* 207 * set auth handle to reflect new cred. 208 */ 209 auth->ah_cred = au->au_origcred; 210 marshal_new_auth(auth); 211 212 if (sx_try_upgrade(&auth_unix_lock)) { 213 auth_unix_count++; 214 TAILQ_INSERT_TAIL(&auth_unix_cache[h], au, au_link); 215 TAILQ_INSERT_TAIL(&auth_unix_all, au, au_alllink); 216 au->au_refs++; /* one for the cache, one for user */ 217 sx_xunlock(&auth_unix_lock); 218 return (auth); 219 } else { 220 sx_sunlock(&auth_unix_lock); 221 AUTH_DESTROY(auth); 222 goto again; 223 } 224} 225 226/* 227 * authunix operations 228 */ 229 230/* ARGSUSED */ 231static void 232authunix_nextverf(AUTH *auth) 233{ 234 /* no action necessary */ 235} 236 237static bool_t 238authunix_marshal(AUTH *auth, XDR *xdrs) 239{ 240 struct audata *au; 241 242 au = AUTH_PRIVATE(auth); 243 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos)); 244} 245 246static bool_t 247authunix_validate(AUTH *auth, struct opaque_auth *verf) 248{ 249 struct audata *au; 250 XDR xdrs; 251 252 if (verf->oa_flavor == AUTH_SHORT) { 253 au = AUTH_PRIVATE(auth); 254 xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, 255 XDR_DECODE); 256 257 if (au->au_shcred.oa_base != NULL) { 258 mem_free(au->au_shcred.oa_base, 259 au->au_shcred.oa_length); 260 au->au_shcred.oa_base = NULL; 261 } 262 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { 263 auth->ah_cred = au->au_shcred; 264 } else { 265 xdrs.x_op = XDR_FREE; 266 (void)xdr_opaque_auth(&xdrs, &au->au_shcred); 267 au->au_shcred.oa_base = NULL; 268 auth->ah_cred = au->au_origcred; 269 } 270 marshal_new_auth(auth); 271 } 272 return (TRUE); 273} 274 275static bool_t 276authunix_refresh(AUTH *auth, void *dummy) 277{ 278 struct audata *au = AUTH_PRIVATE(auth); 279 struct xucred xcr; 280 uint32_t time; 281 struct timeval now; 282 XDR xdrs; 283 int stat; 284 285 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) { 286 /* there is no hope. Punt */ 287 return (FALSE); 288 } 289 au->au_shfaults ++; 290 291 /* first deserialize the creds back into a struct ucred */ 292 xdrmem_create(&xdrs, au->au_origcred.oa_base, 293 au->au_origcred.oa_length, XDR_DECODE); 294 stat = xdr_authunix_parms(&xdrs, &time, &xcr); 295 if (! stat) 296 goto done; 297 298 /* update the time and serialize in place */ 299 getmicrotime(&now); 300 time = now.tv_sec; 301 xdrs.x_op = XDR_ENCODE; 302 XDR_SETPOS(&xdrs, 0); 303 304 stat = xdr_authunix_parms(&xdrs, &time, &xcr); 305 if (! stat) 306 goto done; 307 auth->ah_cred = au->au_origcred; 308 marshal_new_auth(auth); 309done: 310 XDR_DESTROY(&xdrs); 311 return (stat); 312} 313 314static void 315authunix_destroy(AUTH *auth) 316{ 317 struct audata *au; 318 int refs; 319 320 au = AUTH_PRIVATE(auth); 321 322 sx_xlock(&auth_unix_lock); 323 au->au_refs--; 324 refs = au->au_refs; 325 sx_xunlock(&auth_unix_lock); 326 327 if (refs > 0) 328 return; 329 330 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length); 331 332 if (au->au_shcred.oa_base != NULL) 333 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); 334 335 mem_free(auth->ah_private, sizeof(struct audata)); 336 337 if (auth->ah_verf.oa_base != NULL) 338 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); 339 340 mem_free(auth, sizeof(*auth)); 341} 342 343/* 344 * Marshals (pre-serializes) an auth struct. 345 * sets private data, au_marshed and au_mpos 346 */ 347static void 348marshal_new_auth(AUTH *auth) 349{ 350 XDR xdr_stream; 351 XDR *xdrs = &xdr_stream; 352 struct audata *au; 353 354 au = AUTH_PRIVATE(auth); 355 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); 356 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || 357 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) 358 printf("auth_none.c - Fatal marshalling problem"); 359 else 360 au->au_mpos = XDR_GETPOS(xdrs); 361 XDR_DESTROY(xdrs); 362} 363