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) 68d3d20c82SDavid E. O'Brien static char sccsid[] = "@(#)svcauth_des.c 2.3 89/07/11 4.0 RPCSRC; from 1.15 88/02/08 SMI"; 69e8636dfdSBill Paul #endif 70d3d20c82SDavid E. O'Brien #include <sys/cdefs.h> 71d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$"); 72e8636dfdSBill Paul 738d630135SAlfred Perlstein extern int key_decryptsession_pk(const char *, netobj *, des_block *); 748d630135SAlfred Perlstein 75e8636dfdSBill Paul #define debug(msg) printf("svcauth_des: %s\n", msg) 76e8636dfdSBill Paul 77e8636dfdSBill Paul #define USEC_PER_SEC ((u_long) 1000000L) 78e8636dfdSBill Paul #define BEFORE(t1, t2) timercmp(t1, t2, <) 79e8636dfdSBill Paul 80e8636dfdSBill Paul /* 81e8636dfdSBill Paul * LRU cache of conversation keys and some other useful items. 82e8636dfdSBill Paul */ 83e8636dfdSBill Paul #define AUTHDES_CACHESZ 64 84e8636dfdSBill Paul struct cache_entry { 85e8636dfdSBill Paul des_block key; /* conversation key */ 86e8636dfdSBill Paul char *rname; /* client's name */ 87e8636dfdSBill Paul u_int window; /* credential lifetime window */ 88e8636dfdSBill Paul struct timeval laststamp; /* detect replays of creds */ 89e8636dfdSBill Paul char *localcred; /* generic local credential */ 90e8636dfdSBill Paul }; 91e8636dfdSBill Paul static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */; 92e8636dfdSBill Paul static short *authdes_lru/* [AUTHDES_CACHESZ] */; 93e8636dfdSBill Paul 94e8636dfdSBill Paul static void cache_init(); /* initialize the cache */ 95e8636dfdSBill Paul static short cache_spot(); /* find an entry in the cache */ 96e8636dfdSBill Paul static void cache_ref(/*short sid*/); /* note that sid was ref'd */ 97e8636dfdSBill Paul 98e8636dfdSBill Paul static void invalidate(); /* invalidate entry in cache */ 99e8636dfdSBill Paul 100e8636dfdSBill Paul /* 101e8636dfdSBill Paul * cache statistics 102e8636dfdSBill Paul */ 103e8636dfdSBill Paul static struct { 104e8636dfdSBill Paul u_long ncachehits; /* times cache hit, and is not replay */ 105e8636dfdSBill Paul u_long ncachereplays; /* times cache hit, and is replay */ 106e8636dfdSBill Paul u_long ncachemisses; /* times cache missed */ 107e8636dfdSBill Paul } svcauthdes_stats; 108e8636dfdSBill Paul 109e8636dfdSBill Paul /* 110e8636dfdSBill Paul * Service side authenticator for AUTH_DES 111e8636dfdSBill Paul */ 112e8636dfdSBill Paul enum auth_stat 113e8636dfdSBill Paul _svcauth_des(rqst, msg) 1148fb3f3f6SDavid E. O'Brien struct svc_req *rqst; 1158fb3f3f6SDavid E. O'Brien struct rpc_msg *msg; 116e8636dfdSBill Paul { 117e8636dfdSBill Paul 1188fb3f3f6SDavid E. O'Brien long *ixdr; 119e8636dfdSBill Paul des_block cryptbuf[2]; 1208fb3f3f6SDavid E. O'Brien struct authdes_cred *cred; 121e8636dfdSBill Paul struct authdes_verf verf; 122e8636dfdSBill Paul int status; 1238fb3f3f6SDavid E. O'Brien struct cache_entry *entry; 124e8636dfdSBill Paul short sid = 0; 125e8636dfdSBill Paul des_block *sessionkey; 126e8636dfdSBill Paul des_block ivec; 127e8636dfdSBill Paul u_int window; 128e8636dfdSBill Paul struct timeval timestamp; 129e8636dfdSBill Paul u_long namelen; 130e8636dfdSBill Paul struct area { 131e8636dfdSBill Paul struct authdes_cred area_cred; 132e8636dfdSBill Paul char area_netname[MAXNETNAMELEN+1]; 133e8636dfdSBill Paul } *area; 134e8636dfdSBill Paul 135e8636dfdSBill Paul if (authdes_cache == NULL) { 136e8636dfdSBill Paul cache_init(); 137e8636dfdSBill Paul } 138e8636dfdSBill Paul 139e8636dfdSBill Paul area = (struct area *)rqst->rq_clntcred; 140e8636dfdSBill Paul cred = (struct authdes_cred *)&area->area_cred; 141e8636dfdSBill Paul 142e8636dfdSBill Paul /* 143e8636dfdSBill Paul * Get the credential 144e8636dfdSBill Paul */ 145e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_cred.oa_base; 146e8636dfdSBill Paul cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind); 147e8636dfdSBill Paul switch (cred->adc_namekind) { 148e8636dfdSBill Paul case ADN_FULLNAME: 149e8636dfdSBill Paul namelen = IXDR_GET_U_LONG(ixdr); 150e8636dfdSBill Paul if (namelen > MAXNETNAMELEN) { 151e8636dfdSBill Paul return (AUTH_BADCRED); 152e8636dfdSBill Paul } 153e8636dfdSBill Paul cred->adc_fullname.name = area->area_netname; 154e8636dfdSBill Paul bcopy((char *)ixdr, cred->adc_fullname.name, 155e8636dfdSBill Paul (u_int)namelen); 156e8636dfdSBill Paul cred->adc_fullname.name[namelen] = 0; 157e8636dfdSBill Paul ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT); 158e8636dfdSBill Paul cred->adc_fullname.key.key.high = (u_long)*ixdr++; 159e8636dfdSBill Paul cred->adc_fullname.key.key.low = (u_long)*ixdr++; 160e8636dfdSBill Paul cred->adc_fullname.window = (u_long)*ixdr++; 161e8636dfdSBill Paul break; 162e8636dfdSBill Paul case ADN_NICKNAME: 163e8636dfdSBill Paul cred->adc_nickname = (u_long)*ixdr++; 164e8636dfdSBill Paul break; 165e8636dfdSBill Paul default: 166e8636dfdSBill Paul return (AUTH_BADCRED); 167e8636dfdSBill Paul } 168e8636dfdSBill Paul 169e8636dfdSBill Paul /* 170e8636dfdSBill Paul * Get the verifier 171e8636dfdSBill Paul */ 172e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_verf.oa_base; 173e8636dfdSBill Paul verf.adv_xtimestamp.key.high = (u_long)*ixdr++; 174e8636dfdSBill Paul verf.adv_xtimestamp.key.low = (u_long)*ixdr++; 175e8636dfdSBill Paul verf.adv_int_u = (u_long)*ixdr++; 176e8636dfdSBill Paul 177e8636dfdSBill Paul 178e8636dfdSBill Paul /* 179e8636dfdSBill Paul * Get the conversation key 180e8636dfdSBill Paul */ 181e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 182e8636dfdSBill Paul netobj pkey; 183e8636dfdSBill Paul char pkey_data[1024]; 184e8636dfdSBill Paul 185e8636dfdSBill Paul sessionkey = &cred->adc_fullname.key; 186e8636dfdSBill Paul if (! getpublickey(cred->adc_fullname.name, pkey_data)) { 187e8636dfdSBill Paul debug("getpublickey"); 188e8636dfdSBill Paul return(AUTH_BADCRED); 189e8636dfdSBill Paul } 190e8636dfdSBill Paul pkey.n_bytes = pkey_data; 191e8636dfdSBill Paul pkey.n_len = strlen(pkey_data) + 1; 192e8636dfdSBill Paul if (key_decryptsession_pk(cred->adc_fullname.name, &pkey, 193e8636dfdSBill Paul sessionkey) < 0) { 194e8636dfdSBill Paul debug("decryptsessionkey"); 195e8636dfdSBill Paul return (AUTH_BADCRED); /* key not found */ 196e8636dfdSBill Paul } 197e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 198e8636dfdSBill Paul sid = (short)cred->adc_nickname; 1998360efbdSAlfred Perlstein if (sid < 0 || sid >= AUTHDES_CACHESZ) { 200e8636dfdSBill Paul debug("bad nickname"); 201e8636dfdSBill Paul return (AUTH_BADCRED); /* garbled credential */ 202e8636dfdSBill Paul } 203e8636dfdSBill Paul sessionkey = &authdes_cache[sid].key; 204e8636dfdSBill Paul } 205e8636dfdSBill Paul 206e8636dfdSBill Paul 207e8636dfdSBill Paul /* 208e8636dfdSBill Paul * Decrypt the timestamp 209e8636dfdSBill Paul */ 210e8636dfdSBill Paul cryptbuf[0] = verf.adv_xtimestamp; 211e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 212e8636dfdSBill Paul cryptbuf[1].key.high = cred->adc_fullname.window; 213e8636dfdSBill Paul cryptbuf[1].key.low = verf.adv_winverf; 214e8636dfdSBill Paul ivec.key.high = ivec.key.low = 0; 215e8636dfdSBill Paul status = cbc_crypt((char *)sessionkey, (char *)cryptbuf, 216e8636dfdSBill Paul 2*sizeof(des_block), DES_DECRYPT | DES_HW, 217e8636dfdSBill Paul (char *)&ivec); 218e8636dfdSBill Paul } else { 219e8636dfdSBill Paul status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, 220e8636dfdSBill Paul sizeof(des_block), DES_DECRYPT | DES_HW); 221e8636dfdSBill Paul } 222e8636dfdSBill Paul if (DES_FAILED(status)) { 223e8636dfdSBill Paul debug("decryption failure"); 224e8636dfdSBill Paul return (AUTH_FAILED); /* system error */ 225e8636dfdSBill Paul } 226e8636dfdSBill Paul 227e8636dfdSBill Paul /* 228e8636dfdSBill Paul * XDR the decrypted timestamp 229e8636dfdSBill Paul */ 230e8636dfdSBill Paul ixdr = (long *)cryptbuf; 231e8636dfdSBill Paul timestamp.tv_sec = IXDR_GET_LONG(ixdr); 232e8636dfdSBill Paul timestamp.tv_usec = IXDR_GET_LONG(ixdr); 233e8636dfdSBill Paul 234e8636dfdSBill Paul /* 235e8636dfdSBill Paul * Check for valid credentials and verifiers. 236e8636dfdSBill Paul * They could be invalid because the key was flushed 237e8636dfdSBill Paul * out of the cache, and so a new session should begin. 238e8636dfdSBill Paul * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case. 239e8636dfdSBill Paul */ 240e8636dfdSBill Paul { 241e8636dfdSBill Paul struct timeval current; 242e8636dfdSBill Paul int nick; 243e8636dfdSBill Paul int winverf; 244e8636dfdSBill Paul 245e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 246e8636dfdSBill Paul window = IXDR_GET_U_LONG(ixdr); 247e8636dfdSBill Paul winverf = IXDR_GET_U_LONG(ixdr); 248e8636dfdSBill Paul if (winverf != window - 1) { 249e8636dfdSBill Paul debug("window verifier mismatch"); 250e8636dfdSBill Paul return (AUTH_BADCRED); /* garbled credential */ 251e8636dfdSBill Paul } 252e8636dfdSBill Paul sid = cache_spot(sessionkey, cred->adc_fullname.name, 253e8636dfdSBill Paul ×tamp); 254e8636dfdSBill Paul if (sid < 0) { 255e8636dfdSBill Paul debug("replayed credential"); 256e8636dfdSBill Paul return (AUTH_REJECTEDCRED); /* replay */ 257e8636dfdSBill Paul } 258e8636dfdSBill Paul nick = 0; 259e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 260e8636dfdSBill Paul window = authdes_cache[sid].window; 261e8636dfdSBill Paul nick = 1; 262e8636dfdSBill Paul } 263e8636dfdSBill Paul 264e8636dfdSBill Paul if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) { 265e8636dfdSBill Paul debug("invalid usecs"); 266e8636dfdSBill Paul /* cached out (bad key), or garbled verifier */ 267e8636dfdSBill Paul return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF); 268e8636dfdSBill Paul } 269e8636dfdSBill Paul if (nick && BEFORE(×tamp, 270e8636dfdSBill Paul &authdes_cache[sid].laststamp)) { 271e8636dfdSBill Paul debug("timestamp before last seen"); 272e8636dfdSBill Paul return (AUTH_REJECTEDVERF); /* replay */ 273e8636dfdSBill Paul } 274e8636dfdSBill Paul (void) gettimeofday(¤t, (struct timezone *)NULL); 275e8636dfdSBill Paul current.tv_sec -= window; /* allow for expiration */ 276e8636dfdSBill Paul if (!BEFORE(¤t, ×tamp)) { 277e8636dfdSBill Paul debug("timestamp expired"); 278e8636dfdSBill Paul /* replay, or garbled credential */ 279e8636dfdSBill Paul return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED); 280e8636dfdSBill Paul } 281e8636dfdSBill Paul } 282e8636dfdSBill Paul 283e8636dfdSBill Paul /* 284e8636dfdSBill Paul * Set up the reply verifier 285e8636dfdSBill Paul */ 286e8636dfdSBill Paul verf.adv_nickname = (u_long)sid; 287e8636dfdSBill Paul 288e8636dfdSBill Paul /* 289e8636dfdSBill Paul * xdr the timestamp before encrypting 290e8636dfdSBill Paul */ 291e8636dfdSBill Paul ixdr = (long *)cryptbuf; 292e8636dfdSBill Paul IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1); 293e8636dfdSBill Paul IXDR_PUT_LONG(ixdr, timestamp.tv_usec); 294e8636dfdSBill Paul 295e8636dfdSBill Paul /* 296e8636dfdSBill Paul * encrypt the timestamp 297e8636dfdSBill Paul */ 298e8636dfdSBill Paul status = ecb_crypt((char *)sessionkey, (char *)cryptbuf, 299e8636dfdSBill Paul sizeof(des_block), DES_ENCRYPT | DES_HW); 300e8636dfdSBill Paul if (DES_FAILED(status)) { 301e8636dfdSBill Paul debug("encryption failure"); 302e8636dfdSBill Paul return (AUTH_FAILED); /* system error */ 303e8636dfdSBill Paul } 304e8636dfdSBill Paul verf.adv_xtimestamp = cryptbuf[0]; 305e8636dfdSBill Paul 306e8636dfdSBill Paul /* 307e8636dfdSBill Paul * Serialize the reply verifier, and update rqst 308e8636dfdSBill Paul */ 309e8636dfdSBill Paul ixdr = (long *)msg->rm_call.cb_verf.oa_base; 310e8636dfdSBill Paul *ixdr++ = (long)verf.adv_xtimestamp.key.high; 311e8636dfdSBill Paul *ixdr++ = (long)verf.adv_xtimestamp.key.low; 312e8636dfdSBill Paul *ixdr++ = (long)verf.adv_int_u; 313e8636dfdSBill Paul 314e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES; 315e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; 316e8636dfdSBill Paul rqst->rq_xprt->xp_verf.oa_length = 317e8636dfdSBill Paul (char *)ixdr - msg->rm_call.cb_verf.oa_base; 318e8636dfdSBill Paul 319e8636dfdSBill Paul /* 320e8636dfdSBill Paul * We succeeded, commit the data to the cache now and 321e8636dfdSBill Paul * finish cooking the credential. 322e8636dfdSBill Paul */ 323e8636dfdSBill Paul entry = &authdes_cache[sid]; 324e8636dfdSBill Paul entry->laststamp = timestamp; 325e8636dfdSBill Paul cache_ref(sid); 326e8636dfdSBill Paul if (cred->adc_namekind == ADN_FULLNAME) { 327e8636dfdSBill Paul cred->adc_fullname.window = window; 328e8636dfdSBill Paul cred->adc_nickname = (u_long)sid; /* save nickname */ 329e8636dfdSBill Paul if (entry->rname != NULL) { 330e8636dfdSBill Paul mem_free(entry->rname, strlen(entry->rname) + 1); 331e8636dfdSBill Paul } 332e8636dfdSBill Paul entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name) 333e8636dfdSBill Paul + 1); 334e8636dfdSBill Paul if (entry->rname != NULL) { 335e8636dfdSBill Paul (void) strcpy(entry->rname, cred->adc_fullname.name); 336e8636dfdSBill Paul } else { 337e8636dfdSBill Paul debug("out of memory"); 338e8636dfdSBill Paul } 339e8636dfdSBill Paul entry->key = *sessionkey; 340e8636dfdSBill Paul entry->window = window; 341e8636dfdSBill Paul invalidate(entry->localcred); /* mark any cached cred invalid */ 342e8636dfdSBill Paul } else { /* ADN_NICKNAME */ 343e8636dfdSBill Paul /* 344e8636dfdSBill Paul * nicknames are cooked into fullnames 345e8636dfdSBill Paul */ 346e8636dfdSBill Paul cred->adc_namekind = ADN_FULLNAME; 347e8636dfdSBill Paul cred->adc_fullname.name = entry->rname; 348e8636dfdSBill Paul cred->adc_fullname.key = entry->key; 349e8636dfdSBill Paul cred->adc_fullname.window = entry->window; 350e8636dfdSBill Paul } 351e8636dfdSBill Paul return (AUTH_OK); /* we made it!*/ 352e8636dfdSBill Paul } 353e8636dfdSBill Paul 354e8636dfdSBill Paul 355e8636dfdSBill Paul /* 356e8636dfdSBill Paul * Initialize the cache 357e8636dfdSBill Paul */ 358e8636dfdSBill Paul static void 359e8636dfdSBill Paul cache_init() 360e8636dfdSBill Paul { 3618fb3f3f6SDavid E. O'Brien int i; 362e8636dfdSBill Paul 363e8636dfdSBill Paul authdes_cache = (struct cache_entry *) 364e8636dfdSBill Paul mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ); 365e8636dfdSBill Paul bzero((char *)authdes_cache, 366e8636dfdSBill Paul sizeof(struct cache_entry) * AUTHDES_CACHESZ); 367e8636dfdSBill Paul 368e8636dfdSBill Paul authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ); 369e8636dfdSBill Paul /* 370e8636dfdSBill Paul * Initialize the lru list 371e8636dfdSBill Paul */ 372e8636dfdSBill Paul for (i = 0; i < AUTHDES_CACHESZ; i++) { 373e8636dfdSBill Paul authdes_lru[i] = i; 374e8636dfdSBill Paul } 375e8636dfdSBill Paul } 376e8636dfdSBill Paul 377e8636dfdSBill Paul 378e8636dfdSBill Paul /* 379e8636dfdSBill Paul * Find the lru victim 380e8636dfdSBill Paul */ 381e8636dfdSBill Paul static short 382e8636dfdSBill Paul cache_victim() 383e8636dfdSBill Paul { 384e8636dfdSBill Paul return (authdes_lru[AUTHDES_CACHESZ-1]); 385e8636dfdSBill Paul } 386e8636dfdSBill Paul 387e8636dfdSBill Paul /* 388e8636dfdSBill Paul * Note that sid was referenced 389e8636dfdSBill Paul */ 390e8636dfdSBill Paul static void 391e8636dfdSBill Paul cache_ref(sid) 3928fb3f3f6SDavid E. O'Brien short sid; 393e8636dfdSBill Paul { 3948fb3f3f6SDavid E. O'Brien int i; 3958fb3f3f6SDavid E. O'Brien short curr; 3968fb3f3f6SDavid E. O'Brien short prev; 397e8636dfdSBill Paul 398e8636dfdSBill Paul prev = authdes_lru[0]; 399e8636dfdSBill Paul authdes_lru[0] = sid; 400e8636dfdSBill Paul for (i = 1; prev != sid; i++) { 401e8636dfdSBill Paul curr = authdes_lru[i]; 402e8636dfdSBill Paul authdes_lru[i] = prev; 403e8636dfdSBill Paul prev = curr; 404e8636dfdSBill Paul } 405e8636dfdSBill Paul } 406e8636dfdSBill Paul 407e8636dfdSBill Paul 408e8636dfdSBill Paul /* 409e8636dfdSBill Paul * Find a spot in the cache for a credential containing 410e8636dfdSBill Paul * the items given. Return -1 if a replay is detected, otherwise 411e8636dfdSBill Paul * return the spot in the cache. 412e8636dfdSBill Paul */ 413e8636dfdSBill Paul static short 414e8636dfdSBill Paul cache_spot(key, name, timestamp) 4158fb3f3f6SDavid E. O'Brien des_block *key; 416e8636dfdSBill Paul char *name; 417e8636dfdSBill Paul struct timeval *timestamp; 418e8636dfdSBill Paul { 4198fb3f3f6SDavid E. O'Brien struct cache_entry *cp; 4208fb3f3f6SDavid E. O'Brien int i; 4218fb3f3f6SDavid E. O'Brien u_long hi; 422e8636dfdSBill Paul 423e8636dfdSBill Paul hi = key->key.high; 424e8636dfdSBill Paul for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) { 425e8636dfdSBill Paul if (cp->key.key.high == hi && 426e8636dfdSBill Paul cp->key.key.low == key->key.low && 427e8636dfdSBill Paul cp->rname != NULL && 428e8636dfdSBill Paul bcmp(cp->rname, name, strlen(name) + 1) == 0) { 429e8636dfdSBill Paul if (BEFORE(timestamp, &cp->laststamp)) { 430e8636dfdSBill Paul svcauthdes_stats.ncachereplays++; 431e8636dfdSBill Paul return (-1); /* replay */ 432e8636dfdSBill Paul } 433e8636dfdSBill Paul svcauthdes_stats.ncachehits++; 434e8636dfdSBill Paul return (i); /* refresh */ 435e8636dfdSBill Paul } 436e8636dfdSBill Paul } 437e8636dfdSBill Paul svcauthdes_stats.ncachemisses++; 438e8636dfdSBill Paul return (cache_victim()); /* new credential */ 439e8636dfdSBill Paul } 440e8636dfdSBill Paul 441e8636dfdSBill Paul 442e8636dfdSBill Paul #if (defined(sun) || defined(vax) || defined(__FreeBSD__)) 443e8636dfdSBill Paul /* 444e8636dfdSBill Paul * Local credential handling stuff. 445e8636dfdSBill Paul * NOTE: bsd unix dependent. 446e8636dfdSBill Paul * Other operating systems should put something else here. 447e8636dfdSBill Paul */ 448e8636dfdSBill Paul #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */ 449e8636dfdSBill Paul #define INVALID -1 /* grouplen, if cache entry is invalid */ 450e8636dfdSBill Paul 451e8636dfdSBill Paul struct bsdcred { 452e8636dfdSBill Paul short uid; /* cached uid */ 453e8636dfdSBill Paul short gid; /* cached gid */ 454e8636dfdSBill Paul short grouplen; /* length of cached groups */ 455e8636dfdSBill Paul short groups[NGROUPS]; /* cached groups */ 456e8636dfdSBill Paul }; 457e8636dfdSBill Paul 458e8636dfdSBill Paul /* 459e8636dfdSBill Paul * Map a des credential into a unix cred. 460e8636dfdSBill Paul * We cache the credential here so the application does 461e8636dfdSBill Paul * not have to make an rpc call every time to interpret 462e8636dfdSBill Paul * the credential. 463e8636dfdSBill Paul */ 464e8636dfdSBill Paul int 465e8636dfdSBill Paul authdes_getucred(adc, uid, gid, grouplen, groups) 466e8636dfdSBill Paul struct authdes_cred *adc; 467e8636dfdSBill Paul uid_t *uid; 468e8636dfdSBill Paul gid_t *gid; 469e8636dfdSBill Paul int *grouplen; 4708fb3f3f6SDavid E. O'Brien gid_t *groups; 471e8636dfdSBill Paul { 472e8636dfdSBill Paul unsigned sid; 4738fb3f3f6SDavid E. O'Brien int i; 474e8636dfdSBill Paul uid_t i_uid; 475e8636dfdSBill Paul gid_t i_gid; 476e8636dfdSBill Paul int i_grouplen; 477e8636dfdSBill Paul struct bsdcred *cred; 478e8636dfdSBill Paul 479e8636dfdSBill Paul sid = adc->adc_nickname; 480e8636dfdSBill Paul if (sid >= AUTHDES_CACHESZ) { 481e8636dfdSBill Paul debug("invalid nickname"); 482e8636dfdSBill Paul return (0); 483e8636dfdSBill Paul } 484e8636dfdSBill Paul cred = (struct bsdcred *)authdes_cache[sid].localcred; 485e8636dfdSBill Paul if (cred == NULL) { 486e8636dfdSBill Paul cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred)); 487e8636dfdSBill Paul authdes_cache[sid].localcred = (char *)cred; 488e8636dfdSBill Paul cred->grouplen = INVALID; 489e8636dfdSBill Paul } 490e8636dfdSBill Paul if (cred->grouplen == INVALID) { 491e8636dfdSBill Paul /* 492e8636dfdSBill Paul * not in cache: lookup 493e8636dfdSBill Paul */ 494e8636dfdSBill Paul if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid, 495e8636dfdSBill Paul &i_grouplen, groups)) 496e8636dfdSBill Paul { 497e8636dfdSBill Paul debug("unknown netname"); 498e8636dfdSBill Paul cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */ 499e8636dfdSBill Paul return (0); 500e8636dfdSBill Paul } 501e8636dfdSBill Paul debug("missed ucred cache"); 502e8636dfdSBill Paul *uid = cred->uid = i_uid; 503e8636dfdSBill Paul *gid = cred->gid = i_gid; 504e8636dfdSBill Paul *grouplen = cred->grouplen = i_grouplen; 505e8636dfdSBill Paul for (i = i_grouplen - 1; i >= 0; i--) { 506e8636dfdSBill Paul cred->groups[i] = groups[i]; /* int to short */ 507e8636dfdSBill Paul } 508e8636dfdSBill Paul return (1); 509e8636dfdSBill Paul } else if (cred->grouplen == UNKNOWN) { 510e8636dfdSBill Paul /* 511e8636dfdSBill Paul * Already lookup up, but no match found 512e8636dfdSBill Paul */ 513e8636dfdSBill Paul return (0); 514e8636dfdSBill Paul } 515e8636dfdSBill Paul 516e8636dfdSBill Paul /* 517e8636dfdSBill Paul * cached credentials 518e8636dfdSBill Paul */ 519e8636dfdSBill Paul *uid = cred->uid; 520e8636dfdSBill Paul *gid = cred->gid; 521e8636dfdSBill Paul *grouplen = cred->grouplen; 522e8636dfdSBill Paul for (i = cred->grouplen - 1; i >= 0; i--) { 523e8636dfdSBill Paul groups[i] = cred->groups[i]; /* short to int */ 524e8636dfdSBill Paul } 525e8636dfdSBill Paul return (1); 526e8636dfdSBill Paul } 527e8636dfdSBill Paul 528e8636dfdSBill Paul static void 529e8636dfdSBill Paul invalidate(cred) 530e8636dfdSBill Paul char *cred; 531e8636dfdSBill Paul { 532e8636dfdSBill Paul if (cred == NULL) { 533e8636dfdSBill Paul return; 534e8636dfdSBill Paul } 535e8636dfdSBill Paul ((struct bsdcred *)cred)->grouplen = INVALID; 536e8636dfdSBill Paul } 537e8636dfdSBill Paul #endif 538e8636dfdSBill Paul 539