1e8636dfdSBill Paul 2e8636dfdSBill Paul /* 3e8636dfdSBill Paul * Copyright (c) 1988 by Sun Microsystems, Inc. 4e8636dfdSBill Paul */ 5e8636dfdSBill Paul 6e8636dfdSBill Paul /* 7e8636dfdSBill Paul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 8e8636dfdSBill Paul * unrestricted use provided that this legend is included on all tape 9e8636dfdSBill Paul * media and as a part of the software program in whole or part. Users 10e8636dfdSBill Paul * may copy or modify Sun RPC without charge, but are not authorized 11e8636dfdSBill Paul * to license or distribute it to anyone else except as part of a product or 12e8636dfdSBill Paul * program developed by the user. 13e8636dfdSBill Paul * 14e8636dfdSBill Paul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 15e8636dfdSBill Paul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 16e8636dfdSBill Paul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 17e8636dfdSBill Paul * 18e8636dfdSBill Paul * Sun RPC is provided with no support and without any obligation on the 19e8636dfdSBill Paul * part of Sun Microsystems, Inc. to assist in its use, correction, 20e8636dfdSBill Paul * modification or enhancement. 21e8636dfdSBill Paul * 22e8636dfdSBill Paul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 23e8636dfdSBill Paul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 24e8636dfdSBill Paul * OR ANY PART THEREOF. 25e8636dfdSBill Paul * 26e8636dfdSBill Paul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 27e8636dfdSBill Paul * or profits or other special, indirect and consequential damages, even if 28e8636dfdSBill Paul * Sun has been advised of the possibility of such damages. 29e8636dfdSBill Paul * 30e8636dfdSBill Paul * Sun Microsystems, Inc. 31e8636dfdSBill Paul * 2550 Garcia Avenue 32e8636dfdSBill Paul * Mountain View, California 94043 33e8636dfdSBill Paul */ 34e8636dfdSBill Paul 35e8636dfdSBill Paul /* 36e8636dfdSBill Paul * svcauth_des.c, server-side des authentication 37e8636dfdSBill Paul * 38e8636dfdSBill Paul * We insure for the service the following: 39e8636dfdSBill Paul * (1) The timestamp microseconds do not exceed 1 million. 40e8636dfdSBill Paul * (2) The timestamp plus the window is less than the current time. 41e8636dfdSBill Paul * (3) The timestamp is not less than the one previously 42e8636dfdSBill Paul * seen in the current session. 43e8636dfdSBill Paul * 44e8636dfdSBill Paul * It is up to the server to determine if the window size is 45e8636dfdSBill Paul * too small . 46e8636dfdSBill Paul * 47e8636dfdSBill Paul */ 48e8636dfdSBill Paul 498360efbdSAlfred Perlstein #include "namespace.h" 509f5afc13SIan Dowse #include "reentrant.h" 51e8636dfdSBill Paul #include <string.h> 52e8636dfdSBill Paul #include <stdlib.h> 53d201fe46SDaniel Eischen #include <stdio.h> 54e8636dfdSBill Paul #include <unistd.h> 55e8636dfdSBill Paul #include <rpc/des_crypt.h> 56e8636dfdSBill Paul #include <sys/param.h> 57e8636dfdSBill Paul #include <netinet/in.h> 58e8636dfdSBill Paul #include <rpc/types.h> 59e8636dfdSBill Paul #include <rpc/xdr.h> 60e8636dfdSBill Paul #include <rpc/auth.h> 61e8636dfdSBill Paul #include <rpc/auth_des.h> 62e8636dfdSBill Paul #include <rpc/svc.h> 63e8636dfdSBill Paul #include <rpc/rpc_msg.h> 64e8636dfdSBill Paul #include <rpc/svc_auth.h> 658360efbdSAlfred Perlstein #include "libc_private.h" 66e8636dfdSBill Paul 67e8636dfdSBill Paul #if defined(LIBC_SCCS) && !defined(lint) 68e8636dfdSBill Paul /* from: static char sccsid[] = "@(#)svcauth_des.c 2.3 89/07/11 4.0 RPCSRC; from 1.15 88/02/08 SMI"; */ 697f3dea24SPeter Wemm static const char rcsid[] = "$FreeBSD$"; 70e8636dfdSBill Paul #endif 71e8636dfdSBill Paul 72e8636dfdSBill Paul #define debug(msg) printf("svcauth_des: %s\n", msg) 73e8636dfdSBill Paul 74e8636dfdSBill Paul #define USEC_PER_SEC ((u_long) 1000000L) 75e8636dfdSBill Paul #define BEFORE(t1, t2) timercmp(t1, t2, <) 76e8636dfdSBill Paul 77e8636dfdSBill Paul /* 78e8636dfdSBill Paul * LRU cache of conversation keys and some other useful items. 79e8636dfdSBill Paul */ 80e8636dfdSBill Paul #define AUTHDES_CACHESZ 64 81e8636dfdSBill Paul struct cache_entry { 82e8636dfdSBill Paul des_block key; /* conversation key */ 83e8636dfdSBill Paul char *rname; /* client's name */ 84e8636dfdSBill Paul u_int window; /* credential lifetime window */ 85e8636dfdSBill Paul struct timeval laststamp; /* detect replays of creds */ 86e8636dfdSBill Paul char *localcred; /* generic local credential */ 87e8636dfdSBill Paul }; 88e8636dfdSBill Paul static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */; 89e8636dfdSBill Paul static short *authdes_lru/* [AUTHDES_CACHESZ] */; 90e8636dfdSBill Paul 91e8636dfdSBill Paul static void cache_init(); /* initialize the cache */ 92e8636dfdSBill Paul static short cache_spot(); /* find an entry in the cache */ 93e8636dfdSBill Paul static void cache_ref(/*short sid*/); /* note that sid was ref'd */ 94e8636dfdSBill Paul 95e8636dfdSBill Paul static void invalidate(); /* invalidate entry in cache */ 96e8636dfdSBill Paul 97e8636dfdSBill Paul /* 98e8636dfdSBill Paul * cache statistics 99e8636dfdSBill Paul */ 100e8636dfdSBill Paul static struct { 101e8636dfdSBill Paul u_long ncachehits; /* times cache hit, and is not replay */ 102e8636dfdSBill Paul u_long ncachereplays; /* times cache hit, and is replay */ 103e8636dfdSBill Paul u_long ncachemisses; /* times cache missed */ 104e8636dfdSBill Paul } svcauthdes_stats; 105e8636dfdSBill Paul 106e8636dfdSBill Paul /* 107e8636dfdSBill Paul * Service side authenticator for AUTH_DES 108e8636dfdSBill Paul */ 109e8636dfdSBill Paul enum auth_stat 110e8636dfdSBill Paul _svcauth_des(rqst, msg) 111e8636dfdSBill Paul register struct svc_req *rqst; 112e8636dfdSBill Paul register struct rpc_msg *msg; 113e8636dfdSBill Paul { 114e8636dfdSBill Paul 115e8636dfdSBill Paul register long *ixdr; 116e8636dfdSBill Paul des_block cryptbuf[2]; 117e8636dfdSBill Paul register struct authdes_cred *cred; 118e8636dfdSBill Paul struct authdes_verf verf; 119e8636dfdSBill Paul int status; 120e8636dfdSBill Paul register struct cache_entry *entry; 121e8636dfdSBill Paul short sid = 0; 122e8636dfdSBill Paul des_block *sessionkey; 123e8636dfdSBill Paul des_block ivec; 124e8636dfdSBill Paul u_int window; 125e8636dfdSBill Paul struct timeval timestamp; 126e8636dfdSBill Paul u_long namelen; 127e8636dfdSBill Paul struct area { 128e8636dfdSBill Paul struct authdes_cred area_cred; 129e8636dfdSBill Paul char area_netname[MAXNETNAMELEN+1]; 130e8636dfdSBill Paul } *area; 131e8636dfdSBill Paul 132e8636dfdSBill Paul if (authdes_cache == NULL) { 133e8636dfdSBill Paul cache_init(); 134e8636dfdSBill Paul } 135e8636dfdSBill Paul 136e8636dfdSBill Paul area = (struct area *)rqst->rq_clntcred; 137e8636dfdSBill Paul cred = (struct authdes_cred *)&area->area_cred; 138e8636dfdSBill Paul 139e8636dfdSBill Paul /* 140e8636dfdSBill Paul * Get the credential 141e8636dfdSBill Paul */ 142e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_cred.oa_base; 143e8636dfdSBill Paul cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind); 144e8636dfdSBill Paul switch (cred->adc_namekind) { 145e8636dfdSBill Paul case ADN_FULLNAME: 146e8636dfdSBill Paul namelen = IXDR_GET_U_LONG(ixdr); 147e8636dfdSBill Paul if (namelen > MAXNETNAMELEN) { 148e8636dfdSBill Paul return (AUTH_BADCRED); 149e8636dfdSBill Paul } 150e8636dfdSBill Paul cred->adc_fullname.name = area->area_netname; 151e8636dfdSBill Paul bcopy((char *)ixdr, cred->adc_fullname.name, 152e8636dfdSBill Paul (u_int)namelen); 153e8636dfdSBill Paul cred->adc_fullname.name[namelen] = 0; 154e8636dfdSBill Paul ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT); 155e8636dfdSBill Paul cred->adc_fullname.key.key.high = (u_long)*ixdr++; 156e8636dfdSBill Paul cred->adc_fullname.key.key.low = (u_long)*ixdr++; 157e8636dfdSBill Paul cred->adc_fullname.window = (u_long)*ixdr++; 158e8636dfdSBill Paul break; 159e8636dfdSBill Paul case ADN_NICKNAME: 160e8636dfdSBill Paul cred->adc_nickname = (u_long)*ixdr++; 161e8636dfdSBill Paul break; 162e8636dfdSBill Paul default: 163e8636dfdSBill Paul return (AUTH_BADCRED); 164e8636dfdSBill Paul } 165e8636dfdSBill Paul 166e8636dfdSBill Paul /* 167e8636dfdSBill Paul * Get the verifier 168e8636dfdSBill Paul */ 169e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_verf.oa_base; 170e8636dfdSBill Paul verf.adv_xtimestamp.key.high = (u_long)*ixdr++; 171e8636dfdSBill Paul verf.adv_xtimestamp.key.low = (u_long)*ixdr++; 172e8636dfdSBill Paul verf.adv_int_u = (u_long)*ixdr++; 173e8636dfdSBill Paul 174e8636dfdSBill Paul 175e8636dfdSBill Paul /* 176e8636dfdSBill Paul * Get the conversation key 177e8636dfdSBill Paul */ 178e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 179e8636dfdSBill Paul netobj pkey; 180e8636dfdSBill Paul char pkey_data[1024]; 181e8636dfdSBill Paul 182e8636dfdSBill Paul sessionkey = &cred->adc_fullname.key; 183e8636dfdSBill Paul if (! getpublickey(cred->adc_fullname.name, pkey_data)) { 184e8636dfdSBill Paul debug("getpublickey"); 185e8636dfdSBill Paul return(AUTH_BADCRED); 186e8636dfdSBill Paul } 187e8636dfdSBill Paul pkey.n_bytes = pkey_data; 188e8636dfdSBill Paul pkey.n_len = strlen(pkey_data) + 1; 189e8636dfdSBill Paul if (key_decryptsession_pk(cred->adc_fullname.name, &pkey, 190e8636dfdSBill Paul sessionkey) < 0) { 191e8636dfdSBill Paul debug("decryptsessionkey"); 192e8636dfdSBill Paul return (AUTH_BADCRED); /* key not found */ 193e8636dfdSBill Paul } 194e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 195e8636dfdSBill Paul sid = (short)cred->adc_nickname; 1968360efbdSAlfred Perlstein if (sid < 0 || sid >= AUTHDES_CACHESZ) { 197e8636dfdSBill Paul debug("bad nickname"); 198e8636dfdSBill Paul return (AUTH_BADCRED); /* garbled credential */ 199e8636dfdSBill Paul } 200e8636dfdSBill Paul sessionkey = &authdes_cache[sid].key; 201e8636dfdSBill Paul } 202e8636dfdSBill Paul 203e8636dfdSBill Paul 204e8636dfdSBill Paul /* 205e8636dfdSBill Paul * Decrypt the timestamp 206e8636dfdSBill Paul */ 207e8636dfdSBill Paul cryptbuf[0] = verf.adv_xtimestamp; 208e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 209e8636dfdSBill Paul cryptbuf[1].key.high = cred->adc_fullname.window; 210e8636dfdSBill Paul cryptbuf[1].key.low = verf.adv_winverf; 211e8636dfdSBill Paul ivec.key.high = ivec.key.low = 0; 212e8636dfdSBill Paul status = cbc_crypt((char *)sessionkey, (char *)cryptbuf, 213e8636dfdSBill Paul 2*sizeof(des_block), DES_DECRYPT | DES_HW, 214e8636dfdSBill Paul (char *)&ivec); 215e8636dfdSBill Paul } else { 216e8636dfdSBill Paul status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, 217e8636dfdSBill Paul sizeof(des_block), DES_DECRYPT | DES_HW); 218e8636dfdSBill Paul } 219e8636dfdSBill Paul if (DES_FAILED(status)) { 220e8636dfdSBill Paul debug("decryption failure"); 221e8636dfdSBill Paul return (AUTH_FAILED); /* system error */ 222e8636dfdSBill Paul } 223e8636dfdSBill Paul 224e8636dfdSBill Paul /* 225e8636dfdSBill Paul * XDR the decrypted timestamp 226e8636dfdSBill Paul */ 227e8636dfdSBill Paul ixdr = (long *)cryptbuf; 228e8636dfdSBill Paul timestamp.tv_sec = IXDR_GET_LONG(ixdr); 229e8636dfdSBill Paul timestamp.tv_usec = IXDR_GET_LONG(ixdr); 230e8636dfdSBill Paul 231e8636dfdSBill Paul /* 232e8636dfdSBill Paul * Check for valid credentials and verifiers. 233e8636dfdSBill Paul * They could be invalid because the key was flushed 234e8636dfdSBill Paul * out of the cache, and so a new session should begin. 235e8636dfdSBill Paul * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case. 236e8636dfdSBill Paul */ 237e8636dfdSBill Paul { 238e8636dfdSBill Paul struct timeval current; 239e8636dfdSBill Paul int nick; 240e8636dfdSBill Paul int winverf; 241e8636dfdSBill Paul 242e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 243e8636dfdSBill Paul window = IXDR_GET_U_LONG(ixdr); 244e8636dfdSBill Paul winverf = IXDR_GET_U_LONG(ixdr); 245e8636dfdSBill Paul if (winverf != window - 1) { 246e8636dfdSBill Paul debug("window verifier mismatch"); 247e8636dfdSBill Paul return (AUTH_BADCRED); /* garbled credential */ 248e8636dfdSBill Paul } 249e8636dfdSBill Paul sid = cache_spot(sessionkey, cred->adc_fullname.name, 250e8636dfdSBill Paul ×tamp); 251e8636dfdSBill Paul if (sid < 0) { 252e8636dfdSBill Paul debug("replayed credential"); 253e8636dfdSBill Paul return (AUTH_REJECTEDCRED); /* replay */ 254e8636dfdSBill Paul } 255e8636dfdSBill Paul nick = 0; 256e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 257e8636dfdSBill Paul window = authdes_cache[sid].window; 258e8636dfdSBill Paul nick = 1; 259e8636dfdSBill Paul } 260e8636dfdSBill Paul 261e8636dfdSBill Paul if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) { 262e8636dfdSBill Paul debug("invalid usecs"); 263e8636dfdSBill Paul /* cached out (bad key), or garbled verifier */ 264e8636dfdSBill Paul return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF); 265e8636dfdSBill Paul } 266e8636dfdSBill Paul if (nick && BEFORE(×tamp, 267e8636dfdSBill Paul &authdes_cache[sid].laststamp)) { 268e8636dfdSBill Paul debug("timestamp before last seen"); 269e8636dfdSBill Paul return (AUTH_REJECTEDVERF); /* replay */ 270e8636dfdSBill Paul } 271e8636dfdSBill Paul (void) gettimeofday(¤t, (struct timezone *)NULL); 272e8636dfdSBill Paul current.tv_sec -= window; /* allow for expiration */ 273e8636dfdSBill Paul if (!BEFORE(¤t, ×tamp)) { 274e8636dfdSBill Paul debug("timestamp expired"); 275e8636dfdSBill Paul /* replay, or garbled credential */ 276e8636dfdSBill Paul return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED); 277e8636dfdSBill Paul } 278e8636dfdSBill Paul } 279e8636dfdSBill Paul 280e8636dfdSBill Paul /* 281e8636dfdSBill Paul * Set up the reply verifier 282e8636dfdSBill Paul */ 283e8636dfdSBill Paul verf.adv_nickname = (u_long)sid; 284e8636dfdSBill Paul 285e8636dfdSBill Paul /* 286e8636dfdSBill Paul * xdr the timestamp before encrypting 287e8636dfdSBill Paul */ 288e8636dfdSBill Paul ixdr = (long *)cryptbuf; 289e8636dfdSBill Paul IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1); 290e8636dfdSBill Paul IXDR_PUT_LONG(ixdr, timestamp.tv_usec); 291e8636dfdSBill Paul 292e8636dfdSBill Paul /* 293e8636dfdSBill Paul * encrypt the timestamp 294e8636dfdSBill Paul */ 295e8636dfdSBill Paul status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, 296e8636dfdSBill Paul sizeof(des_block), DES_ENCRYPT | DES_HW); 297e8636dfdSBill Paul if (DES_FAILED(status)) { 298e8636dfdSBill Paul debug("encryption failure"); 299e8636dfdSBill Paul return (AUTH_FAILED); /* system error */ 300e8636dfdSBill Paul } 301e8636dfdSBill Paul verf.adv_xtimestamp = cryptbuf[0]; 302e8636dfdSBill Paul 303e8636dfdSBill Paul /* 304e8636dfdSBill Paul * Serialize the reply verifier, and update rqst 305e8636dfdSBill Paul */ 306e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_verf.oa_base; 307e8636dfdSBill Paul *ixdr++ = (long)verf.adv_xtimestamp.key.high; 308e8636dfdSBill Paul *ixdr++ = (long)verf.adv_xtimestamp.key.low; 309e8636dfdSBill Paul *ixdr++ = (long)verf.adv_int_u; 310e8636dfdSBill Paul 311e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES; 312e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; 313e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_length = 314e8636dfdSBill Paul (char *)ixdr - msg->rm_call.cb_verf.oa_base; 315e8636dfdSBill Paul 316e8636dfdSBill Paul /* 317e8636dfdSBill Paul * We succeeded, commit the data to the cache now and 318e8636dfdSBill Paul * finish cooking the credential. 319e8636dfdSBill Paul */ 320e8636dfdSBill Paul entry = &authdes_cache[sid]; 321e8636dfdSBill Paul entry->laststamp = timestamp; 322e8636dfdSBill Paul cache_ref(sid); 323e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 324e8636dfdSBill Paul cred->adc_fullname.window = window; 325e8636dfdSBill Paul cred->adc_nickname = (u_long)sid; /* save nickname */ 326e8636dfdSBill Paul if (entry->rname != NULL) { 327e8636dfdSBill Paul mem_free(entry->rname, strlen(entry->rname) + 1); 328e8636dfdSBill Paul } 329e8636dfdSBill Paul entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name) 330e8636dfdSBill Paul + 1); 331e8636dfdSBill Paul if (entry->rname != NULL) { 332e8636dfdSBill Paul (void) strcpy(entry->rname, cred->adc_fullname.name); 333e8636dfdSBill Paul } else { 334e8636dfdSBill Paul debug("out of memory"); 335e8636dfdSBill Paul } 336e8636dfdSBill Paul entry->key = *sessionkey; 337e8636dfdSBill Paul entry->window = window; 338e8636dfdSBill Paul invalidate(entry->localcred); /* mark any cached cred invalid */ 339e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 340e8636dfdSBill Paul /* 341e8636dfdSBill Paul * nicknames are cooked into fullnames 342e8636dfdSBill Paul */ 343e8636dfdSBill Paul cred->adc_namekind = ADN_FULLNAME; 344e8636dfdSBill Paul cred->adc_fullname.name = entry->rname; 345e8636dfdSBill Paul cred->adc_fullname.key = entry->key; 346e8636dfdSBill Paul cred->adc_fullname.window = entry->window; 347e8636dfdSBill Paul } 348e8636dfdSBill Paul return (AUTH_OK); /* we made it!*/ 349e8636dfdSBill Paul } 350e8636dfdSBill Paul 351e8636dfdSBill Paul 352e8636dfdSBill Paul /* 353e8636dfdSBill Paul * Initialize the cache 354e8636dfdSBill Paul */ 355e8636dfdSBill Paul static void 356e8636dfdSBill Paul cache_init() 357e8636dfdSBill Paul { 358e8636dfdSBill Paul register int i; 359e8636dfdSBill Paul 360e8636dfdSBill Paul authdes_cache = (struct cache_entry *) 361e8636dfdSBill Paul mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ); 362e8636dfdSBill Paul bzero((char *)authdes_cache, 363e8636dfdSBill Paul sizeof(struct cache_entry) * AUTHDES_CACHESZ); 364e8636dfdSBill Paul 365e8636dfdSBill Paul authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ); 366e8636dfdSBill Paul /* 367e8636dfdSBill Paul * Initialize the lru list 368e8636dfdSBill Paul */ 369e8636dfdSBill Paul for (i = 0; i < AUTHDES_CACHESZ; i++) { 370e8636dfdSBill Paul authdes_lru[i] = i; 371e8636dfdSBill Paul } 372e8636dfdSBill Paul } 373e8636dfdSBill Paul 374e8636dfdSBill Paul 375e8636dfdSBill Paul /* 376e8636dfdSBill Paul * Find the lru victim 377e8636dfdSBill Paul */ 378e8636dfdSBill Paul static short 379e8636dfdSBill Paul cache_victim() 380e8636dfdSBill Paul { 381e8636dfdSBill Paul return (authdes_lru[AUTHDES_CACHESZ-1]); 382e8636dfdSBill Paul } 383e8636dfdSBill Paul 384e8636dfdSBill Paul /* 385e8636dfdSBill Paul * Note that sid was referenced 386e8636dfdSBill Paul */ 387e8636dfdSBill Paul static void 388e8636dfdSBill Paul cache_ref(sid) 389e8636dfdSBill Paul register short sid; 390e8636dfdSBill Paul { 391e8636dfdSBill Paul register int i; 392e8636dfdSBill Paul register short curr; 393e8636dfdSBill Paul register short prev; 394e8636dfdSBill Paul 395e8636dfdSBill Paul prev = authdes_lru[0]; 396e8636dfdSBill Paul authdes_lru[0] = sid; 397e8636dfdSBill Paul for (i = 1; prev != sid; i++) { 398e8636dfdSBill Paul curr = authdes_lru[i]; 399e8636dfdSBill Paul authdes_lru[i] = prev; 400e8636dfdSBill Paul prev = curr; 401e8636dfdSBill Paul } 402e8636dfdSBill Paul } 403e8636dfdSBill Paul 404e8636dfdSBill Paul 405e8636dfdSBill Paul /* 406e8636dfdSBill Paul * Find a spot in the cache for a credential containing 407e8636dfdSBill Paul * the items given. Return -1 if a replay is detected, otherwise 408e8636dfdSBill Paul * return the spot in the cache. 409e8636dfdSBill Paul */ 410e8636dfdSBill Paul static short 411e8636dfdSBill Paul cache_spot(key, name, timestamp) 412e8636dfdSBill Paul register des_block *key; 413e8636dfdSBill Paul char *name; 414e8636dfdSBill Paul struct timeval *timestamp; 415e8636dfdSBill Paul { 416e8636dfdSBill Paul register struct cache_entry *cp; 417e8636dfdSBill Paul register int i; 418e8636dfdSBill Paul register u_long hi; 419e8636dfdSBill Paul 420e8636dfdSBill Paul hi = key->key.high; 421e8636dfdSBill Paul for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) { 422e8636dfdSBill Paul if (cp->key.key.high == hi && 423e8636dfdSBill Paul cp->key.key.low == key->key.low && 424e8636dfdSBill Paul cp->rname != NULL && 425e8636dfdSBill Paul bcmp(cp->rname, name, strlen(name) + 1) == 0) { 426e8636dfdSBill Paul if (BEFORE(timestamp, &cp->laststamp)) { 427e8636dfdSBill Paul svcauthdes_stats.ncachereplays++; 428e8636dfdSBill Paul return (-1); /* replay */ 429e8636dfdSBill Paul } 430e8636dfdSBill Paul svcauthdes_stats.ncachehits++; 431e8636dfdSBill Paul return (i); /* refresh */ 432e8636dfdSBill Paul } 433e8636dfdSBill Paul } 434e8636dfdSBill Paul svcauthdes_stats.ncachemisses++; 435e8636dfdSBill Paul return (cache_victim()); /* new credential */ 436e8636dfdSBill Paul } 437e8636dfdSBill Paul 438e8636dfdSBill Paul 439e8636dfdSBill Paul #if (defined(sun) || defined(vax) || defined(__FreeBSD__)) 440e8636dfdSBill Paul /* 441e8636dfdSBill Paul * Local credential handling stuff. 442e8636dfdSBill Paul * NOTE: bsd unix dependent. 443e8636dfdSBill Paul * Other operating systems should put something else here. 444e8636dfdSBill Paul */ 445e8636dfdSBill Paul #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */ 446e8636dfdSBill Paul #define INVALID -1 /* grouplen, if cache entry is invalid */ 447e8636dfdSBill Paul 448e8636dfdSBill Paul struct bsdcred { 449e8636dfdSBill Paul short uid; /* cached uid */ 450e8636dfdSBill Paul short gid; /* cached gid */ 451e8636dfdSBill Paul short grouplen; /* length of cached groups */ 452e8636dfdSBill Paul short groups[NGROUPS]; /* cached groups */ 453e8636dfdSBill Paul }; 454e8636dfdSBill Paul 455e8636dfdSBill Paul /* 456e8636dfdSBill Paul * Map a des credential into a unix cred. 457e8636dfdSBill Paul * We cache the credential here so the application does 458e8636dfdSBill Paul * not have to make an rpc call every time to interpret 459e8636dfdSBill Paul * the credential. 460e8636dfdSBill Paul */ 461e8636dfdSBill Paul int 462e8636dfdSBill Paul authdes_getucred(adc, uid, gid, grouplen, groups) 463e8636dfdSBill Paul struct authdes_cred *adc; 464e8636dfdSBill Paul uid_t *uid; 465e8636dfdSBill Paul gid_t *gid; 466e8636dfdSBill Paul int *grouplen; 467e8636dfdSBill Paul register gid_t *groups; 468e8636dfdSBill Paul { 469e8636dfdSBill Paul unsigned sid; 470e8636dfdSBill Paul register int i; 471e8636dfdSBill Paul uid_t i_uid; 472e8636dfdSBill Paul gid_t i_gid; 473e8636dfdSBill Paul int i_grouplen; 474e8636dfdSBill Paul struct bsdcred *cred; 475e8636dfdSBill Paul 476e8636dfdSBill Paul sid = adc->adc_nickname; 477e8636dfdSBill Paul if (sid >= AUTHDES_CACHESZ) { 478e8636dfdSBill Paul debug("invalid nickname"); 479e8636dfdSBill Paul return (0); 480e8636dfdSBill Paul } 481e8636dfdSBill Paul cred = (struct bsdcred *)authdes_cache[sid].localcred; 482e8636dfdSBill Paul if (cred == NULL) { 483e8636dfdSBill Paul cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred)); 484e8636dfdSBill Paul authdes_cache[sid].localcred = (char *)cred; 485e8636dfdSBill Paul cred->grouplen = INVALID; 486e8636dfdSBill Paul } 487e8636dfdSBill Paul if (cred->grouplen == INVALID) { 488e8636dfdSBill Paul /* 489e8636dfdSBill Paul * not in cache: lookup 490e8636dfdSBill Paul */ 491e8636dfdSBill Paul if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid, 492e8636dfdSBill Paul &i_grouplen, groups)) 493e8636dfdSBill Paul { 494e8636dfdSBill Paul debug("unknown netname"); 495e8636dfdSBill Paul cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */ 496e8636dfdSBill Paul return (0); 497e8636dfdSBill Paul } 498e8636dfdSBill Paul debug("missed ucred cache"); 499e8636dfdSBill Paul *uid = cred->uid = i_uid; 500e8636dfdSBill Paul *gid = cred->gid = i_gid; 501e8636dfdSBill Paul *grouplen = cred->grouplen = i_grouplen; 502e8636dfdSBill Paul for (i = i_grouplen - 1; i >= 0; i--) { 503e8636dfdSBill Paul cred->groups[i] = groups[i]; /* int to short */ 504e8636dfdSBill Paul } 505e8636dfdSBill Paul return (1); 506e8636dfdSBill Paul } else if (cred->grouplen == UNKNOWN) { 507e8636dfdSBill Paul /* 508e8636dfdSBill Paul * Already lookup up, but no match found 509e8636dfdSBill Paul */ 510e8636dfdSBill Paul return (0); 511e8636dfdSBill Paul } 512e8636dfdSBill Paul 513e8636dfdSBill Paul /* 514e8636dfdSBill Paul * cached credentials 515e8636dfdSBill Paul */ 516e8636dfdSBill Paul *uid = cred->uid; 517e8636dfdSBill Paul *gid = cred->gid; 518e8636dfdSBill Paul *grouplen = cred->grouplen; 519e8636dfdSBill Paul for (i = cred->grouplen - 1; i >= 0; i--) { 520e8636dfdSBill Paul groups[i] = cred->groups[i]; /* short to int */ 521e8636dfdSBill Paul } 522e8636dfdSBill Paul return (1); 523e8636dfdSBill Paul } 524e8636dfdSBill Paul 525e8636dfdSBill Paul static void 526e8636dfdSBill Paul invalidate(cred) 527e8636dfdSBill Paul char *cred; 528e8636dfdSBill Paul { 529e8636dfdSBill Paul if (cred == NULL) { 530e8636dfdSBill Paul return; 531e8636dfdSBill Paul } 532e8636dfdSBill Paul ((struct bsdcred *)cred)->grouplen = INVALID; 533e8636dfdSBill Paul } 534e8636dfdSBill Paul #endif 535e8636dfdSBill Paul 536