10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54321Scasper * Common Development and Distribution License (the "License").
64321Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate * under license from the Regents of the University of California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * svcauth_des.c, server-side des authentication
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * We insure for the service the following:
380Sstevel@tonic-gate * (1) The timestamp microseconds do not exceed 1 million.
390Sstevel@tonic-gate * (2) The timestamp plus the window is less than the current time.
400Sstevel@tonic-gate * (3) The timestamp is not less than the one previously
410Sstevel@tonic-gate * seen in the current session.
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * It is up to the server to determine if the window size is
440Sstevel@tonic-gate * too small.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/time.h>
490Sstevel@tonic-gate #include <sys/systm.h>
500Sstevel@tonic-gate #include <sys/param.h>
510Sstevel@tonic-gate #include <sys/stream.h>
520Sstevel@tonic-gate #include <sys/stropts.h>
530Sstevel@tonic-gate #include <sys/strsubr.h>
540Sstevel@tonic-gate #include <sys/tiuser.h>
550Sstevel@tonic-gate #include <sys/tihdr.h>
560Sstevel@tonic-gate #include <sys/t_kuser.h>
570Sstevel@tonic-gate #include <sys/t_lock.h>
580Sstevel@tonic-gate #include <sys/debug.h>
590Sstevel@tonic-gate #include <sys/kmem.h>
600Sstevel@tonic-gate #include <sys/time.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate #include <rpc/types.h>
640Sstevel@tonic-gate #include <rpc/xdr.h>
650Sstevel@tonic-gate #include <rpc/auth.h>
660Sstevel@tonic-gate #include <rpc/auth_des.h>
670Sstevel@tonic-gate #include <rpc/rpc_msg.h>
680Sstevel@tonic-gate #include <rpc/svc.h>
690Sstevel@tonic-gate #include <rpc/svc_auth.h>
700Sstevel@tonic-gate #include <rpc/clnt.h>
710Sstevel@tonic-gate #include <rpc/des_crypt.h>
720Sstevel@tonic-gate
730Sstevel@tonic-gate #define USEC_PER_SEC 1000000
740Sstevel@tonic-gate #define BEFORE(t1, t2) timercmp(t1, t2, < /* COMMENT HERE TO DEFEAT CSTYLE */)
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * Cache of conversation keys and some other useful items.
780Sstevel@tonic-gate * The hash table size is controled via authdes_cachesz variable.
790Sstevel@tonic-gate * The authdes_cachesz has to be the power of 2.
800Sstevel@tonic-gate */
810Sstevel@tonic-gate #define AUTHDES_CACHE_TABLE_SZ 1024
820Sstevel@tonic-gate static int authdes_cachesz = AUTHDES_CACHE_TABLE_SZ;
830Sstevel@tonic-gate #define HASH(key) ((key) & (authdes_cachesz - 1))
840Sstevel@tonic-gate
850Sstevel@tonic-gate /* low water mark for the number of cache entries */
860Sstevel@tonic-gate static int low_cache_entries = 128;
870Sstevel@tonic-gate
880Sstevel@tonic-gate struct authdes_cache_entry {
890Sstevel@tonic-gate uint32_t nickname; /* nick name id */
900Sstevel@tonic-gate uint32_t window; /* credential lifetime window */
910Sstevel@tonic-gate des_block key; /* conversation key */
920Sstevel@tonic-gate time_t ref_time; /* time referenced previously */
930Sstevel@tonic-gate char *rname; /* client's name */
940Sstevel@tonic-gate caddr_t localcred; /* generic local credential */
950Sstevel@tonic-gate struct authdes_cache_entry *prev, *next; /* hash table linked list */
960Sstevel@tonic-gate struct authdes_cache_entry *lru_prev, *lru_next; /* LRU linked list */
970Sstevel@tonic-gate kmutex_t lock; /* cache entry lock */
980Sstevel@tonic-gate };
990Sstevel@tonic-gate static struct authdes_cache_entry **authdes_cache; /* [authdes_cachesz] */
1000Sstevel@tonic-gate static struct authdes_cache_entry *lru_first = NULL;
1010Sstevel@tonic-gate static struct authdes_cache_entry *lru_last = NULL;
1020Sstevel@tonic-gate static kmutex_t authdes_lock; /* cache table lock */
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static struct kmem_cache *authdes_cache_handle;
1050Sstevel@tonic-gate static uint32_t Nickname = 0;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static struct authdes_cache_entry *authdes_cache_new(char *,
1080Sstevel@tonic-gate des_block *, uint32_t);
1090Sstevel@tonic-gate static struct authdes_cache_entry *authdes_cache_get(uint32_t);
1100Sstevel@tonic-gate static void authdes_cache_reclaim(void *);
1110Sstevel@tonic-gate static void sweep_cache();
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * After 12 hours, check and delete cache entries that have been
1150Sstevel@tonic-gate * idled for more than 10 hours.
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate static time_t authdes_sweep_interval = 12*60*60;
1180Sstevel@tonic-gate static time_t authdes_cache_time = 10*60*60;
1190Sstevel@tonic-gate static time_t authdes_last_swept = 0;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * cache statistics
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate static int authdes_ncache = 0; /* number of current cached entries */
1250Sstevel@tonic-gate static int authdes_ncachehits = 0; /* #times cache hit */
1260Sstevel@tonic-gate static int authdes_ncachemisses = 0; /* #times cache missed */
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate #define NOT_DEAD(ptr) ASSERT((((intptr_t)(ptr)) != 0xdeadbeef))
1290Sstevel@tonic-gate #define IS_ALIGNED(ptr) ASSERT((((intptr_t)(ptr)) & 3) == 0)
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * Service side authenticator for AUTH_DES
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate enum auth_stat
_svcauth_des(struct svc_req * rqst,struct rpc_msg * msg)1350Sstevel@tonic-gate _svcauth_des(struct svc_req *rqst, struct rpc_msg *msg)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate int32_t *ixdr;
1380Sstevel@tonic-gate des_block cryptbuf[2];
1390Sstevel@tonic-gate struct authdes_cred *cred;
1400Sstevel@tonic-gate struct authdes_verf verf;
1410Sstevel@tonic-gate int status;
1420Sstevel@tonic-gate des_block *sessionkey;
1430Sstevel@tonic-gate des_block ivec;
1440Sstevel@tonic-gate uint32_t window, winverf, namelen;
1450Sstevel@tonic-gate bool_t nick;
1460Sstevel@tonic-gate struct timeval timestamp, current_time;
1470Sstevel@tonic-gate struct authdes_cache_entry *nick_entry;
1480Sstevel@tonic-gate struct area {
1490Sstevel@tonic-gate struct authdes_cred area_cred;
1500Sstevel@tonic-gate char area_netname[MAXNETNAMELEN+1];
1510Sstevel@tonic-gate } *area;
1520Sstevel@tonic-gate timestruc_t now;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate mutex_enter(&authdes_lock);
1550Sstevel@tonic-gate if (authdes_cache == NULL) {
1560Sstevel@tonic-gate authdes_cache = kmem_zalloc(
1570Sstevel@tonic-gate sizeof (struct authdes_cache_entry *) * authdes_cachesz,
1580Sstevel@tonic-gate KM_SLEEP);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate mutex_exit(&authdes_lock);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /* LINTED pointer alignment */
1630Sstevel@tonic-gate area = (struct area *)rqst->rq_clntcred;
1640Sstevel@tonic-gate cred = (struct authdes_cred *)&area->area_cred;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate * Get the credential
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate /* LINTED pointer alignment */
1700Sstevel@tonic-gate ixdr = (int32_t *)msg->rm_call.cb_cred.oa_base;
1710Sstevel@tonic-gate cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind);
1720Sstevel@tonic-gate switch (cred->adc_namekind) {
1730Sstevel@tonic-gate case ADN_FULLNAME:
1740Sstevel@tonic-gate namelen = IXDR_GET_U_INT32(ixdr);
1750Sstevel@tonic-gate if (namelen > MAXNETNAMELEN)
1760Sstevel@tonic-gate return (AUTH_BADCRED);
1770Sstevel@tonic-gate cred->adc_fullname.name = area->area_netname;
1780Sstevel@tonic-gate bcopy(ixdr, cred->adc_fullname.name, namelen);
1790Sstevel@tonic-gate cred->adc_fullname.name[namelen] = 0;
1800Sstevel@tonic-gate ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT);
1810Sstevel@tonic-gate cred->adc_fullname.key.key.high = (uint32_t)*ixdr++;
1820Sstevel@tonic-gate cred->adc_fullname.key.key.low = (uint32_t)*ixdr++;
1830Sstevel@tonic-gate cred->adc_fullname.window = (uint32_t)*ixdr++;
1840Sstevel@tonic-gate nick = FALSE;
1850Sstevel@tonic-gate break;
1860Sstevel@tonic-gate case ADN_NICKNAME:
1870Sstevel@tonic-gate cred->adc_nickname = (uint32_t)*ixdr++;
1880Sstevel@tonic-gate nick = TRUE;
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate default:
1910Sstevel@tonic-gate return (AUTH_BADCRED);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate * Get the verifier
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate /* LINTED pointer alignment */
1980Sstevel@tonic-gate ixdr = (int32_t *)msg->rm_call.cb_verf.oa_base;
1990Sstevel@tonic-gate verf.adv_xtimestamp.key.high = (uint32_t)*ixdr++;
2000Sstevel@tonic-gate verf.adv_xtimestamp.key.low = (uint32_t)*ixdr++;
2010Sstevel@tonic-gate verf.adv_int_u = (uint32_t)*ixdr++;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate * Get the conversation key
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate if (!nick) { /* ADN_FULLNAME */
2080Sstevel@tonic-gate sessionkey = &cred->adc_fullname.key;
2090Sstevel@tonic-gate if (key_decryptsession(cred->adc_fullname.name, sessionkey) !=
2100Sstevel@tonic-gate RPC_SUCCESS) {
2110Sstevel@tonic-gate return (AUTH_BADCRED); /* key not found */
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate } else { /* ADN_NICKNAME */
2140Sstevel@tonic-gate mutex_enter(&authdes_lock);
2150Sstevel@tonic-gate if (!(nick_entry = authdes_cache_get(cred->adc_nickname))) {
2160Sstevel@tonic-gate RPCLOG(1, "_svcauth_des: nickname %d not in the cache\n",
2170Sstevel@tonic-gate cred->adc_nickname);
2180Sstevel@tonic-gate mutex_exit(&authdes_lock);
2190Sstevel@tonic-gate return (AUTH_BADCRED); /* need refresh */
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate sessionkey = &nick_entry->key;
2220Sstevel@tonic-gate mutex_enter(&nick_entry->lock);
2230Sstevel@tonic-gate mutex_exit(&authdes_lock);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * Decrypt the timestamp
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate cryptbuf[0] = verf.adv_xtimestamp;
2300Sstevel@tonic-gate if (!nick) { /* ADN_FULLNAME */
2310Sstevel@tonic-gate cryptbuf[1].key.high = cred->adc_fullname.window;
2320Sstevel@tonic-gate cryptbuf[1].key.low = verf.adv_winverf;
2330Sstevel@tonic-gate ivec.key.high = ivec.key.low = 0;
2340Sstevel@tonic-gate status = cbc_crypt((char *)sessionkey, (char *)cryptbuf,
2350Sstevel@tonic-gate 2 * sizeof (des_block), DES_DECRYPT, (char *)&ivec);
2360Sstevel@tonic-gate } else { /* ADN_NICKNAME */
2370Sstevel@tonic-gate status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
2380Sstevel@tonic-gate sizeof (des_block), DES_DECRYPT);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate if (DES_FAILED(status)) {
2410Sstevel@tonic-gate RPCLOG0(1, "_svcauth_des: decryption failure\n");
2420Sstevel@tonic-gate if (nick) {
2430Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate return (AUTH_FAILED); /* system error */
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * XDR the decrypted timestamp
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate ixdr = (int32_t *)cryptbuf;
2520Sstevel@tonic-gate timestamp.tv_sec = IXDR_GET_INT32(ixdr);
2530Sstevel@tonic-gate timestamp.tv_usec = IXDR_GET_INT32(ixdr);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * Check for valid credentials and verifiers.
2570Sstevel@tonic-gate * They could be invalid because the key was flushed
2580Sstevel@tonic-gate * out of the cache, and so a new session should begin.
2590Sstevel@tonic-gate * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
2600Sstevel@tonic-gate */
2610Sstevel@tonic-gate if (!nick) { /* ADN_FULLNAME */
2620Sstevel@tonic-gate window = IXDR_GET_U_INT32(ixdr);
2630Sstevel@tonic-gate winverf = IXDR_GET_U_INT32(ixdr);
2640Sstevel@tonic-gate if (winverf != window - 1) {
2650Sstevel@tonic-gate RPCLOG(1, "_svcauth_des: window verifier mismatch %d\n",
2660Sstevel@tonic-gate winverf);
2670Sstevel@tonic-gate return (AUTH_BADCRED); /* garbled credential */
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate } else { /* ADN_NICKNAME */
2700Sstevel@tonic-gate window = nick_entry->window;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate if (timestamp.tv_usec >= USEC_PER_SEC) {
2740Sstevel@tonic-gate RPCLOG(1, "_svcauth_des: invalid usecs %ld\n",
2750Sstevel@tonic-gate timestamp.tv_usec);
2760Sstevel@tonic-gate /* cached out (bad key), or garbled verifier */
2770Sstevel@tonic-gate if (nick) {
2780Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate gethrestime(&now);
2840Sstevel@tonic-gate current_time.tv_sec = now.tv_sec;
2850Sstevel@tonic-gate current_time.tv_usec = now.tv_nsec / 1000;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate current_time.tv_sec -= window; /* allow for expiration */
2880Sstevel@tonic-gate if (!BEFORE(¤t_time, ×tamp)) {
2890Sstevel@tonic-gate RPCLOG0(1, "_svcauth_des: timestamp expired\n");
2900Sstevel@tonic-gate /* replay, or garbled credential */
2910Sstevel@tonic-gate if (nick) {
2920Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate * xdr the timestamp before encrypting
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate ixdr = (int32_t *)cryptbuf;
3010Sstevel@tonic-gate IXDR_PUT_INT32(ixdr, timestamp.tv_sec - 1);
3020Sstevel@tonic-gate IXDR_PUT_INT32(ixdr, timestamp.tv_usec);
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * encrypt the timestamp
3060Sstevel@tonic-gate */
3070Sstevel@tonic-gate status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
3080Sstevel@tonic-gate sizeof (des_block), DES_ENCRYPT);
3090Sstevel@tonic-gate if (DES_FAILED(status)) {
3100Sstevel@tonic-gate RPCLOG0(1, "_svcauth_des: encryption failure\n");
3110Sstevel@tonic-gate if (nick) {
3120Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate return (AUTH_FAILED); /* system error */
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate verf.adv_xtimestamp = cryptbuf[0];
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate * If a ADN_FULLNAME, create a new nickname cache entry.
3200Sstevel@tonic-gate */
3210Sstevel@tonic-gate if (!nick) {
3220Sstevel@tonic-gate mutex_enter(&authdes_lock);
3230Sstevel@tonic-gate if (!(nick_entry = authdes_cache_new(cred->adc_fullname.name,
3240Sstevel@tonic-gate sessionkey, window))) {
3250Sstevel@tonic-gate RPCLOG0(1, "_svcauth_des: can not create new cache entry\n");
3260Sstevel@tonic-gate mutex_exit(&authdes_lock);
3270Sstevel@tonic-gate return (AUTH_FAILED);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate mutex_enter(&nick_entry->lock);
3300Sstevel@tonic-gate mutex_exit(&authdes_lock);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate verf.adv_nickname = nick_entry->nickname;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * Serialize the reply verifier, and update rqst
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate /* LINTED pointer alignment */
3380Sstevel@tonic-gate ixdr = (int32_t *)msg->rm_call.cb_verf.oa_base;
3390Sstevel@tonic-gate *ixdr++ = (int32_t)verf.adv_xtimestamp.key.high;
3400Sstevel@tonic-gate *ixdr++ = (int32_t)verf.adv_xtimestamp.key.low;
3410Sstevel@tonic-gate *ixdr++ = (int32_t)verf.adv_int_u;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
3440Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
3450Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_length =
3460Sstevel@tonic-gate (uint_t)((char *)ixdr - msg->rm_call.cb_verf.oa_base);
3470Sstevel@tonic-gate if (rqst->rq_xprt->xp_verf.oa_length > MAX_AUTH_BYTES) {
3480Sstevel@tonic-gate RPCLOG0(1, "_svcauth_des: invalid oa length\n");
3490Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
3500Sstevel@tonic-gate return (AUTH_BADVERF);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate * We succeeded and finish cooking the credential.
3550Sstevel@tonic-gate * nicknames are cooked into fullnames
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate if (!nick) {
3580Sstevel@tonic-gate cred->adc_nickname = nick_entry->nickname;
3590Sstevel@tonic-gate cred->adc_fullname.window = window;
3600Sstevel@tonic-gate } else { /* ADN_NICKNAME */
3610Sstevel@tonic-gate cred->adc_namekind = ADN_FULLNAME;
3620Sstevel@tonic-gate cred->adc_fullname.name = nick_entry->rname;
3630Sstevel@tonic-gate cred->adc_fullname.key = nick_entry->key;
3640Sstevel@tonic-gate cred->adc_fullname.window = nick_entry->window;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate mutex_exit(&nick_entry->lock);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * For every authdes_sweep_interval, delete cache entries that have been
3700Sstevel@tonic-gate * idled for authdes_cache_time.
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate mutex_enter(&authdes_lock);
3730Sstevel@tonic-gate if ((gethrestime_sec() - authdes_last_swept) > authdes_sweep_interval)
3740Sstevel@tonic-gate sweep_cache();
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate mutex_exit(&authdes_lock);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate return (AUTH_OK); /* we made it! */
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate * Initialization upon loading the rpcsec module.
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate void
svcauthdes_init(void)3850Sstevel@tonic-gate svcauthdes_init(void)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate mutex_init(&authdes_lock, NULL, MUTEX_DEFAULT, NULL);
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Allocate des cache handle
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate authdes_cache_handle = kmem_cache_create("authdes_cache_handle",
3920Sstevel@tonic-gate sizeof (struct authdes_cache_entry), 0, NULL, NULL,
3930Sstevel@tonic-gate authdes_cache_reclaim, NULL, NULL, 0);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate * Final actions upon exiting the rpcsec module.
3980Sstevel@tonic-gate */
3990Sstevel@tonic-gate void
svcauthdes_fini(void)4000Sstevel@tonic-gate svcauthdes_fini(void)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate mutex_destroy(&authdes_lock);
4030Sstevel@tonic-gate kmem_cache_destroy(authdes_cache_handle);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * Local credential handling stuff.
4080Sstevel@tonic-gate * NOTE: bsd unix dependent.
4090Sstevel@tonic-gate * Other operating systems should put something else here.
4100Sstevel@tonic-gate */
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate struct bsdcred {
4130Sstevel@tonic-gate uid_t uid; /* cached uid */
4140Sstevel@tonic-gate gid_t gid; /* cached gid */
4150Sstevel@tonic-gate short valid; /* valid creds */
4160Sstevel@tonic-gate short grouplen; /* length of cached groups */
417*11134SCasper.Dik@Sun.COM gid_t groups[1]; /* cached groups - allocate ngroups_max */
4180Sstevel@tonic-gate };
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate * Map a des credential into a unix cred.
4220Sstevel@tonic-gate * We cache the credential here so the application does
4230Sstevel@tonic-gate * not have to make an rpc call every time to interpret
4240Sstevel@tonic-gate * the credential.
4250Sstevel@tonic-gate */
4260Sstevel@tonic-gate int
kauthdes_getucred(const struct authdes_cred * adc,cred_t * cr)4270Sstevel@tonic-gate kauthdes_getucred(const struct authdes_cred *adc, cred_t *cr)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate uid_t i_uid;
4300Sstevel@tonic-gate gid_t i_gid;
4310Sstevel@tonic-gate int i_grouplen;
4320Sstevel@tonic-gate struct bsdcred *cred;
4330Sstevel@tonic-gate struct authdes_cache_entry *nickentry;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate mutex_enter(&authdes_lock);
4360Sstevel@tonic-gate if (!(nickentry = authdes_cache_get(adc->adc_nickname))) {
4370Sstevel@tonic-gate RPCLOG0(1, "authdes_getucred: invalid nickname\n");
4380Sstevel@tonic-gate mutex_exit(&authdes_lock);
4390Sstevel@tonic-gate return (0);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate mutex_enter(&nickentry->lock);
4430Sstevel@tonic-gate mutex_exit(&authdes_lock);
4440Sstevel@tonic-gate /* LINTED pointer alignment */
4450Sstevel@tonic-gate cred = (struct bsdcred *)nickentry->localcred;
4460Sstevel@tonic-gate if (!cred->valid) {
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate * not in cache: lookup
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate if (netname2user(adc->adc_fullname.name, &i_uid, &i_gid,
4510Sstevel@tonic-gate &i_grouplen, &cred->groups[0]) != RPC_SUCCESS) {
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate * Probably a host principal, since at this
4540Sstevel@tonic-gate * point we have valid keys. Note that later
4550Sstevel@tonic-gate * if the principal is not in the root list
4560Sstevel@tonic-gate * for NFS, we will be mapped to that exported
4570Sstevel@tonic-gate * file system's anonymous user, typically
4580Sstevel@tonic-gate * NOBODY. keyserv KEY_GETCRED will fail for a
4590Sstevel@tonic-gate * root-netnames so we assume root here.
4600Sstevel@tonic-gate * Currently NFS is the only caller of this
4610Sstevel@tonic-gate * routine. If other RPC services call this
4620Sstevel@tonic-gate * routine, it is up to that service to
4630Sstevel@tonic-gate * differentiate between local and remote
4640Sstevel@tonic-gate * roots.
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate i_uid = 0;
4670Sstevel@tonic-gate i_gid = 0;
4680Sstevel@tonic-gate i_grouplen = 0;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate RPCLOG0(2, "authdes_getucred: missed ucred cache\n");
4710Sstevel@tonic-gate cred->uid = i_uid;
4720Sstevel@tonic-gate cred->gid = i_gid;
4730Sstevel@tonic-gate cred->grouplen = (short)i_grouplen;
4740Sstevel@tonic-gate cred->valid = 1;
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * cached credentials
4790Sstevel@tonic-gate */
4800Sstevel@tonic-gate if (crsetugid(cr, cred->uid, cred->gid) != 0 ||
4810Sstevel@tonic-gate crsetgroups(cr, cred->grouplen, &cred->groups[0]) != 0) {
4820Sstevel@tonic-gate mutex_exit(&nickentry->lock);
4830Sstevel@tonic-gate return (0);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate mutex_exit(&nickentry->lock);
4860Sstevel@tonic-gate return (1);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate * Create a new cache_entry and put it in authdes_cache table.
4910Sstevel@tonic-gate * Caller should have already locked the authdes_cache table.
4920Sstevel@tonic-gate */
4930Sstevel@tonic-gate struct authdes_cache_entry *
authdes_cache_new(char * fullname,des_block * sessionkey,uint32_t window)4940Sstevel@tonic-gate authdes_cache_new(char *fullname, des_block *sessionkey, uint32_t window) {
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate struct authdes_cache_entry *new, *head;
4970Sstevel@tonic-gate struct bsdcred *ucred;
4980Sstevel@tonic-gate int index;
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate if (!(new = kmem_cache_alloc(authdes_cache_handle, KM_SLEEP))) {
5010Sstevel@tonic-gate return (NULL);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate if (!(new->rname = kmem_alloc(strlen(fullname) + 1, KM_NOSLEEP))) {
5050Sstevel@tonic-gate kmem_cache_free(authdes_cache_handle, new);
5060Sstevel@tonic-gate return (NULL);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate
509*11134SCasper.Dik@Sun.COM if (!(ucred = kmem_alloc(sizeof (struct bsdcred) +
510*11134SCasper.Dik@Sun.COM (ngroups_max - 1) * sizeof (gid_t), KM_NOSLEEP))) {
5110Sstevel@tonic-gate kmem_free(new->rname, strlen(fullname) + 1);
5120Sstevel@tonic-gate kmem_cache_free(authdes_cache_handle, new);
5130Sstevel@tonic-gate return (NULL);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate (void) strcpy(new->rname, fullname);
5170Sstevel@tonic-gate ucred->valid = 0;
5180Sstevel@tonic-gate new->localcred = (caddr_t)ucred;
5190Sstevel@tonic-gate new->key = *sessionkey;
5200Sstevel@tonic-gate new->window = window;
5210Sstevel@tonic-gate new->ref_time = gethrestime_sec();
5220Sstevel@tonic-gate new->nickname = Nickname++;
5230Sstevel@tonic-gate mutex_init(&new->lock, NULL, MUTEX_DEFAULT, NULL);
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /* put new into the hash table */
5260Sstevel@tonic-gate index = HASH(new->nickname);
5270Sstevel@tonic-gate head = authdes_cache[index];
5280Sstevel@tonic-gate if ((new->next = head) != NULL) {
5290Sstevel@tonic-gate head->prev = new;
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate authdes_cache[index] = new;
5320Sstevel@tonic-gate new->prev = NULL;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate /* update the LRU list */
5350Sstevel@tonic-gate new->lru_prev = NULL;
5360Sstevel@tonic-gate if ((new->lru_next = lru_first) != NULL) {
5370Sstevel@tonic-gate lru_first->lru_prev = new;
5380Sstevel@tonic-gate } else {
5390Sstevel@tonic-gate lru_last = new;
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate lru_first = new;
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate authdes_ncache++;
5440Sstevel@tonic-gate return (new);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * Get an existing cache entry from authdes_cache table.
5490Sstevel@tonic-gate * The caller should have locked the authdes_cache table.
5500Sstevel@tonic-gate */
5510Sstevel@tonic-gate struct authdes_cache_entry *
authdes_cache_get(uint32_t nickname)5520Sstevel@tonic-gate authdes_cache_get(uint32_t nickname) {
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate struct authdes_cache_entry *cur = NULL;
5550Sstevel@tonic-gate int index = HASH(nickname);
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate ASSERT(MUTEX_HELD(&authdes_lock));
5580Sstevel@tonic-gate for (cur = authdes_cache[index]; cur; cur = cur->next) {
5590Sstevel@tonic-gate if ((cur->nickname == nickname)) {
5600Sstevel@tonic-gate /* find it, update the LRU list */
5610Sstevel@tonic-gate if (cur != lru_first) {
5620Sstevel@tonic-gate cur->lru_prev->lru_next = cur->lru_next;
5630Sstevel@tonic-gate if (cur->lru_next != NULL) {
5640Sstevel@tonic-gate cur->lru_next->lru_prev = cur->lru_prev;
5650Sstevel@tonic-gate } else {
5660Sstevel@tonic-gate lru_last = cur->lru_prev;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate cur->lru_prev = NULL;
5690Sstevel@tonic-gate cur->lru_next = lru_first;
5700Sstevel@tonic-gate lru_first->lru_prev = cur;
5710Sstevel@tonic-gate lru_first = cur;
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate cur->ref_time = gethrestime_sec();
5750Sstevel@tonic-gate authdes_ncachehits++;
5760Sstevel@tonic-gate return (cur);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate authdes_ncachemisses++;
5810Sstevel@tonic-gate return (NULL);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * authdes_cache_reclaim() is called by the kernel memory allocator
5860Sstevel@tonic-gate * when memory is low. This routine will reclaim 25% of the least recent
5870Sstevel@tonic-gate * used cache entries above the low water mark (low_cache_entries).
5880Sstevel@tonic-gate * If the cache entries have already hit the low water mark, it will
5890Sstevel@tonic-gate * return 1 cache entry.
5900Sstevel@tonic-gate */
5910Sstevel@tonic-gate /*ARGSUSED*/
5920Sstevel@tonic-gate void
authdes_cache_reclaim(void * pdata)5930Sstevel@tonic-gate authdes_cache_reclaim(void *pdata) {
5940Sstevel@tonic-gate struct authdes_cache_entry *p;
5950Sstevel@tonic-gate int n, i;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate mutex_enter(&authdes_lock);
5980Sstevel@tonic-gate n = authdes_ncache - low_cache_entries;
5990Sstevel@tonic-gate n = n > 0 ? n/4 : 1;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate for (i = 0; i < n; i++) {
6020Sstevel@tonic-gate if ((p = lru_last) == lru_first)
6030Sstevel@tonic-gate break;
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate /* Update the hash linked list */
6060Sstevel@tonic-gate if (p->prev == NULL) {
6070Sstevel@tonic-gate authdes_cache[HASH(p->nickname)] = p->next;
6080Sstevel@tonic-gate } else {
6090Sstevel@tonic-gate p->prev->next = p->next;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate if (p->next != NULL) {
6120Sstevel@tonic-gate p->next->prev = p->prev;
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate /* update the LRU linked list */
6160Sstevel@tonic-gate p->lru_prev->lru_next = NULL;
6170Sstevel@tonic-gate lru_last = p->lru_prev;
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate kmem_free(p->rname, strlen(p->rname) + 1);
620*11134SCasper.Dik@Sun.COM kmem_free(p->localcred, sizeof (struct bsdcred) +
621*11134SCasper.Dik@Sun.COM (ngroups_max - 1) * sizeof (gid_t));
6220Sstevel@tonic-gate mutex_destroy(&p->lock);
6230Sstevel@tonic-gate kmem_cache_free(authdes_cache_handle, p);
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate authdes_ncache--;
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate mutex_exit(&authdes_lock);
6280Sstevel@tonic-gate RPCLOG(4, "_svcauth_des: %d cache entries reclaimed...\n",
6290Sstevel@tonic-gate authdes_ncache);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate * Walk through the LRU doubly-linked list and delete the cache
6340Sstevel@tonic-gate * entries that have not been used for more than authdes_cache_time.
6350Sstevel@tonic-gate *
6360Sstevel@tonic-gate * Caller should have locked the cache table.
6370Sstevel@tonic-gate */
6380Sstevel@tonic-gate void
sweep_cache()6390Sstevel@tonic-gate sweep_cache() {
6400Sstevel@tonic-gate struct authdes_cache_entry *p;
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate ASSERT(MUTEX_HELD(&authdes_lock));
6430Sstevel@tonic-gate while ((p = lru_last) != lru_first) {
6440Sstevel@tonic-gate IS_ALIGNED(p);
6450Sstevel@tonic-gate NOT_DEAD(p);
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate * If the last LRU entry idled less than authdes_cache_time,
6490Sstevel@tonic-gate * we are done with the sweeping.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate if (p->ref_time + authdes_cache_time > gethrestime_sec())
6520Sstevel@tonic-gate break;
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate /* update the hash linked list */
6550Sstevel@tonic-gate if (p->prev == NULL) {
6560Sstevel@tonic-gate authdes_cache[HASH(p->nickname)] = p->next;
6570Sstevel@tonic-gate } else {
6580Sstevel@tonic-gate p->prev->next = p->next;
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate if (p->next != NULL) {
6610Sstevel@tonic-gate p->next->prev = p->prev;
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate /* update the LRU linked list */
6650Sstevel@tonic-gate p->lru_prev->lru_next = NULL;
6660Sstevel@tonic-gate lru_last = p->lru_prev;
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate kmem_free(p->rname, strlen(p->rname) + 1);
669*11134SCasper.Dik@Sun.COM kmem_free(p->localcred, sizeof (struct bsdcred) +
670*11134SCasper.Dik@Sun.COM (ngroups_max - 1) * sizeof (gid_t));
6710Sstevel@tonic-gate mutex_destroy(&p->lock);
6720Sstevel@tonic-gate kmem_cache_free(authdes_cache_handle, p);
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate authdes_ncache--;
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate authdes_last_swept = gethrestime_sec();
6780Sstevel@tonic-gate RPCLOG(4, "_svcauth_des: sweeping cache...#caches left = %d\n",
6790Sstevel@tonic-gate authdes_ncache);
6800Sstevel@tonic-gate }
681