Blame src/svc_auth_des.c

Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Copyright (c) 1988 by Sun Microsystems, Inc.
Packit 00408a
 */
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Copyright (c) 2009, Sun Microsystems, Inc.
Packit 00408a
 * All rights reserved.
Packit 00408a
 *
Packit 00408a
 * Redistribution and use in source and binary forms, with or without
Packit 00408a
 * modification, are permitted provided that the following conditions are met:
Packit 00408a
 * - Redistributions of source code must retain the above copyright notice,
Packit 00408a
 *   this list of conditions and the following disclaimer.
Packit 00408a
 * - Redistributions in binary form must reproduce the above copyright notice,
Packit 00408a
 *   this list of conditions and the following disclaimer in the documentation
Packit 00408a
 *   and/or other materials provided with the distribution.
Packit 00408a
 * - Neither the name of Sun Microsystems, Inc. nor the names of its
Packit 00408a
 *   contributors may be used to endorse or promote products derived
Packit 00408a
 *   from this software without specific prior written permission.
Packit 00408a
 *
Packit 00408a
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 00408a
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 00408a
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 00408a
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Packit 00408a
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 00408a
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 00408a
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 00408a
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 00408a
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 00408a
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 00408a
 * POSSIBILITY OF SUCH DAMAGE.
Packit 00408a
 */
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * svcauth_des.c, server-side des authentication
Packit 00408a
 *
Packit 00408a
 * We insure for the service the following:
Packit 00408a
 * (1) The timestamp microseconds do not exceed 1 million.
Packit 00408a
 * (2) The timestamp plus the window is less than the current time.
Packit 00408a
 * (3) The timestamp is not less than the one previously
Packit 00408a
 *     seen in the current session.
Packit 00408a
 *
Packit 00408a
 * It is up to the server to determine if the window size is
Packit 00408a
 * too small .
Packit 00408a
 *
Packit 00408a
 */
Packit 00408a
#include <pthread.h>
Packit 00408a
#include <reentrant.h>
Packit 00408a
#include <string.h>
Packit 00408a
#include <stdlib.h>
Packit 00408a
#include <stdio.h>
Packit 00408a
#include <unistd.h>
Packit 00408a
#include <rpc/des_crypt.h>
Packit 00408a
#include <sys/param.h>
Packit 00408a
#include <netinet/in.h>
Packit 00408a
#include <rpc/types.h>
Packit 00408a
#include <rpc/xdr.h>
Packit 00408a
#include <rpc/auth.h>
Packit 00408a
#include <rpc/auth_des.h>
Packit 00408a
#include <rpc/svc.h>
Packit 00408a
#include <rpc/rpc_msg.h>
Packit 00408a
#include <rpc/svc_auth.h>
Packit 00408a
#if defined(__FreeBSD__) || defined(__NetBSD__)
Packit 00408a
#include <libc_private.h>
Packit 00408a
#endif
Packit 00408a
Packit 00408a
#include "debug.h"
Packit 00408a
Packit 00408a
extern int key_decryptsession_pk(const char *, netobj *, des_block *);
Packit 00408a
Packit 00408a
#define USEC_PER_SEC ((u_long) 1000000L)
Packit 00408a
#define BEFORE(t1, t2) timercmp(t1, t2, <)
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * LRU cache of conversation keys and some other useful items.
Packit 00408a
 */
Packit 00408a
#define AUTHDES_CACHESZ 64
Packit 00408a
struct cache_entry {
Packit 00408a
	des_block key;		/* conversation key */
Packit 00408a
	char *rname;		/* client's name */
Packit 00408a
	u_int window;		/* credential lifetime window */
Packit 00408a
	struct timeval laststamp;	/* detect replays of creds */
Packit 00408a
	char *localcred;	/* generic local credential */
Packit 00408a
};
Packit 00408a
static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */;
Packit 00408a
static short *authdes_lru/* [AUTHDES_CACHESZ] */;
Packit 00408a
Packit 00408a
static void cache_init();	/* initialize the cache */
Packit 00408a
static short cache_spot(des_block *key, char *name, struct timeval *timestamp);  /* find an entry in the cache */
Packit 00408a
static void cache_ref(short sid);	/* note that sid was ref'd */
Packit 00408a
Packit 00408a
static void invalidate(char *cred);	/* invalidate entry in cache */
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * cache statistics
Packit 00408a
 */
Packit 00408a
static struct {
Packit 00408a
	u_long ncachehits;	/* times cache hit, and is not replay */
Packit 00408a
	u_long ncachereplays;	/* times cache hit, and is replay */
Packit 00408a
	u_long ncachemisses;	/* times cache missed */
Packit 00408a
} svcauthdes_stats;
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Service side authenticator for AUTH_DES
Packit 00408a
 */
Packit 00408a
enum auth_stat
Packit 00408a
_svcauth_des(rqst, msg)
Packit 00408a
	struct svc_req *rqst;
Packit 00408a
	struct rpc_msg *msg;
Packit 00408a
{
Packit 00408a
Packit 00408a
	long *ixdr;
Packit 00408a
	des_block cryptbuf[2];
Packit 00408a
	struct authdes_cred *cred;
Packit 00408a
	struct authdes_verf verf;
Packit 00408a
	int status;
Packit 00408a
	struct cache_entry *entry;
Packit 00408a
	short sid = 0;
Packit 00408a
	des_block *sessionkey;
Packit 00408a
	des_block ivec;
Packit 00408a
	u_int window;
Packit 00408a
	struct timeval timestamp;
Packit 00408a
	u_long namelen;
Packit 00408a
	struct area {
Packit 00408a
		struct authdes_cred area_cred;
Packit 00408a
		char area_netname[MAXNETNAMELEN+1];
Packit 00408a
	} *area;
Packit 00408a
Packit 00408a
	if (authdes_cache == NULL) {
Packit 00408a
		cache_init();
Packit 00408a
	}
Packit 00408a
Packit 00408a
	area = (struct area *)rqst->rq_clntcred;
Packit 00408a
	cred = (struct authdes_cred *)&area->area_cred;
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Get the credential
Packit 00408a
	 */
Packit 00408a
	ixdr = (long *)msg->rm_call.cb_cred.oa_base;
Packit 00408a
	cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind);
Packit 00408a
	switch (cred->adc_namekind) {
Packit 00408a
	case ADN_FULLNAME:
Packit 00408a
		namelen = IXDR_GET_U_LONG(ixdr);
Packit 00408a
		if (namelen > MAXNETNAMELEN) {
Packit 00408a
			return (AUTH_BADCRED);
Packit 00408a
		}
Packit 00408a
		cred->adc_fullname.name = area->area_netname;
Packit 00408a
		bcopy((char *)ixdr, cred->adc_fullname.name, 
Packit 00408a
			(u_int)namelen);
Packit 00408a
		cred->adc_fullname.name[namelen] = 0;
Packit 00408a
		ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT);
Packit 00408a
		cred->adc_fullname.key.key.high = (u_long)*ixdr++;
Packit 00408a
		cred->adc_fullname.key.key.low = (u_long)*ixdr++;
Packit 00408a
		cred->adc_fullname.window = (u_long)*ixdr++;
Packit 00408a
		break;
Packit 00408a
	case ADN_NICKNAME:
Packit 00408a
		cred->adc_nickname = (u_long)*ixdr++;
Packit 00408a
		break;
Packit 00408a
	default:
Packit 00408a
		return (AUTH_BADCRED);	
Packit 00408a
	}
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Get the verifier
Packit 00408a
	 */
Packit 00408a
	ixdr = (long *)msg->rm_call.cb_verf.oa_base;
Packit 00408a
	verf.adv_xtimestamp.key.high = (u_long)*ixdr++;
Packit 00408a
	verf.adv_xtimestamp.key.low =  (u_long)*ixdr++;
Packit 00408a
	verf.adv_int_u = (u_long)*ixdr++;
Packit 00408a
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Get the conversation key
Packit 00408a
 	 */
Packit 00408a
	if (cred->adc_namekind == ADN_FULLNAME) {
Packit 00408a
		netobj pkey;
Packit 00408a
		char pkey_data[1024];
Packit 00408a
Packit 00408a
		sessionkey = &cred->adc_fullname.key;
Packit 00408a
		if (! getpublickey(cred->adc_fullname.name, pkey_data)) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: getpublickey failed"));
Packit 00408a
			return(AUTH_BADCRED);
Packit 00408a
		}
Packit 00408a
		pkey.n_bytes = pkey_data;
Packit 00408a
		pkey.n_len = strlen(pkey_data) + 1;
Packit 00408a
		if (key_decryptsession_pk(cred->adc_fullname.name, &pkey,
Packit 00408a
				       sessionkey) < 0) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: key_decryptsessionkey failed"));
Packit 00408a
			return (AUTH_BADCRED); /* key not found */
Packit 00408a
		}
Packit 00408a
	} else { /* ADN_NICKNAME */	
Packit 00408a
		sid = (short)cred->adc_nickname;
Packit 00408a
		if (sid < 0 || sid >= AUTHDES_CACHESZ) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: bad nickname"));
Packit 00408a
			return (AUTH_BADCRED);	/* garbled credential */
Packit 00408a
		}
Packit 00408a
		sessionkey = &authdes_cache[sid].key;
Packit 00408a
	}
Packit 00408a
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Decrypt the timestamp
Packit 00408a
	 */
Packit 00408a
	cryptbuf[0] = verf.adv_xtimestamp; 
Packit 00408a
	if (cred->adc_namekind == ADN_FULLNAME) {
Packit 00408a
		cryptbuf[1].key.high = cred->adc_fullname.window;
Packit 00408a
		cryptbuf[1].key.low = verf.adv_winverf;
Packit 00408a
		ivec.key.high = ivec.key.low = 0;	
Packit 00408a
		status = cbc_crypt((char *)sessionkey, (char *)cryptbuf,
Packit 00408a
			2*sizeof(des_block), DES_DECRYPT | DES_HW, 
Packit 00408a
			(char *)&ivec);
Packit 00408a
	} else {
Packit 00408a
		status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
Packit 00408a
			sizeof(des_block), DES_DECRYPT | DES_HW);
Packit 00408a
	}
Packit 00408a
	if (DES_FAILED(status)) {
Packit 00408a
		LIBTIRPC_DEBUG(1, ("_svcauth_des: decryption failure"));
Packit 00408a
		return (AUTH_FAILED);	/* system error */
Packit 00408a
	}
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * XDR the decrypted timestamp
Packit 00408a
	 */
Packit 00408a
	ixdr = (long *)cryptbuf;
Packit 00408a
	timestamp.tv_sec = IXDR_GET_LONG(ixdr);
Packit 00408a
	timestamp.tv_usec = IXDR_GET_LONG(ixdr);
Packit 00408a
Packit 00408a
	/*
Packit 00408a
 	 * Check for valid credentials and verifiers.
Packit 00408a
	 * They could be invalid because the key was flushed
Packit 00408a
	 * out of the cache, and so a new session should begin.
Packit 00408a
	 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
Packit 00408a
	 */
Packit 00408a
	{
Packit 00408a
		struct timeval current;
Packit 00408a
		int nick;
Packit 00408a
		int winverf;
Packit 00408a
Packit 00408a
		if (cred->adc_namekind == ADN_FULLNAME) {
Packit 00408a
			window = IXDR_GET_U_LONG(ixdr);
Packit 00408a
			winverf = IXDR_GET_U_LONG(ixdr);
Packit 00408a
			if (winverf != window - 1) {
Packit 00408a
				LIBTIRPC_DEBUG(1, ("_svcauth_des: window verifier mismatch"));
Packit 00408a
				return (AUTH_BADCRED);	/* garbled credential */
Packit 00408a
			}
Packit 00408a
			sid = cache_spot(sessionkey, cred->adc_fullname.name, 
Packit 00408a
			    &timestamp);
Packit 00408a
			if (sid < 0) {
Packit 00408a
				LIBTIRPC_DEBUG(1, ("_svcauth_des: replayed credential"));
Packit 00408a
				return (AUTH_REJECTEDCRED);	/* replay */
Packit 00408a
			}
Packit 00408a
			nick = 0;
Packit 00408a
		} else {	/* ADN_NICKNAME */
Packit 00408a
			window = authdes_cache[sid].window;
Packit 00408a
			nick = 1;
Packit 00408a
		}
Packit 00408a
Packit 00408a
		if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: invalid usecs"));
Packit 00408a
			/* cached out (bad key), or garbled verifier */
Packit 00408a
			return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF);
Packit 00408a
		}
Packit 00408a
		if (nick && BEFORE(&timestamp, 
Packit 00408a
				   &authdes_cache[sid].laststamp)) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: timestamp before last seen"));
Packit 00408a
			return (AUTH_REJECTEDVERF);	/* replay */
Packit 00408a
		}
Packit 00408a
		(void) gettimeofday(&current, (struct timezone *)NULL);
Packit 00408a
		current.tv_sec -= window;	/* allow for expiration */
Packit 00408a
		if (!BEFORE(&current, &timestamp)) {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: timestamp expired"));
Packit 00408a
			/* replay, or garbled credential */
Packit 00408a
			return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
Packit 00408a
		}
Packit 00408a
	}
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Set up the reply verifier
Packit 00408a
	 */
Packit 00408a
	verf.adv_nickname = (u_long)sid;
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * xdr the timestamp before encrypting
Packit 00408a
	 */
Packit 00408a
	ixdr = (long *)cryptbuf;
Packit 00408a
	IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1);
Packit 00408a
	IXDR_PUT_LONG(ixdr, timestamp.tv_usec);
Packit 00408a
Packit 00408a
	/*	 
Packit 00408a
	 * encrypt the timestamp
Packit 00408a
	 */
Packit 00408a
	status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
Packit 00408a
	    sizeof(des_block), DES_ENCRYPT | DES_HW);
Packit 00408a
	if (DES_FAILED(status)) {
Packit 00408a
		LIBTIRPC_DEBUG(1, ("_svcauth_des: encryption failure"));
Packit 00408a
		return (AUTH_FAILED);	/* system error */
Packit 00408a
	}
Packit 00408a
	verf.adv_xtimestamp = cryptbuf[0];
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * Serialize the reply verifier, and update rqst
Packit 00408a
	 */
Packit 00408a
	ixdr = (long *)msg->rm_call.cb_verf.oa_base;
Packit 00408a
	*ixdr++ = (long)verf.adv_xtimestamp.key.high;
Packit 00408a
	*ixdr++ = (long)verf.adv_xtimestamp.key.low;
Packit 00408a
	*ixdr++ = (long)verf.adv_int_u;
Packit 00408a
Packit 00408a
	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
Packit 00408a
	rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
Packit 00408a
	rqst->rq_xprt->xp_verf.oa_length = 
Packit 00408a
		(char *)ixdr - msg->rm_call.cb_verf.oa_base;
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * We succeeded, commit the data to the cache now and
Packit 00408a
	 * finish cooking the credential.
Packit 00408a
	 */
Packit 00408a
	entry = &authdes_cache[sid];
Packit 00408a
	entry->laststamp = timestamp;
Packit 00408a
	cache_ref(sid);
Packit 00408a
	if (cred->adc_namekind == ADN_FULLNAME) {
Packit 00408a
		cred->adc_fullname.window = window;
Packit 00408a
		cred->adc_nickname = (u_long)sid;	/* save nickname */
Packit 00408a
		if (entry->rname != NULL) {
Packit 00408a
			mem_free(entry->rname, strlen(entry->rname) + 1);
Packit 00408a
		}
Packit 00408a
		entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name)
Packit 00408a
					 + 1);
Packit 00408a
		if (entry->rname != NULL) {
Packit 00408a
			(void) strcpy(entry->rname, cred->adc_fullname.name);
Packit 00408a
		} else {
Packit 00408a
			LIBTIRPC_DEBUG(1, ("_svcauth_des: out of memory"));
Packit 00408a
		}
Packit 00408a
		entry->key = *sessionkey;
Packit 00408a
		entry->window = window;
Packit 00408a
		invalidate(entry->localcred); /* mark any cached cred invalid */
Packit 00408a
	} else { /* ADN_NICKNAME */
Packit 00408a
		/*
Packit 00408a
		 * nicknames are cooked into fullnames
Packit 00408a
		 */	
Packit 00408a
		cred->adc_namekind = ADN_FULLNAME;
Packit 00408a
		cred->adc_fullname.name = entry->rname;
Packit 00408a
		cred->adc_fullname.key = entry->key;
Packit 00408a
		cred->adc_fullname.window = entry->window;
Packit 00408a
	}
Packit 00408a
	return (AUTH_OK);	/* we made it!*/
Packit 00408a
}
Packit 00408a
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Initialize the cache
Packit 00408a
 */
Packit 00408a
static void
Packit 00408a
cache_init()
Packit 00408a
{
Packit 00408a
	int i;
Packit 00408a
Packit 00408a
	authdes_cache = (struct cache_entry *)
Packit 00408a
		mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ);	
Packit 00408a
	memset(authdes_cache, 0,
Packit 00408a
		sizeof(struct cache_entry) * AUTHDES_CACHESZ);
Packit 00408a
Packit 00408a
	authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ);
Packit 00408a
	/*
Packit 00408a
	 * Initialize the lru list
Packit 00408a
	 */
Packit 00408a
	for (i = 0; i < AUTHDES_CACHESZ; i++) {
Packit 00408a
		authdes_lru[i] = i;
Packit 00408a
	}
Packit 00408a
}
Packit 00408a
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Find the lru victim
Packit 00408a
 */
Packit 00408a
static short
Packit 00408a
cache_victim()
Packit 00408a
{
Packit 00408a
	return (authdes_lru[AUTHDES_CACHESZ-1]);
Packit 00408a
}
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Note that sid was referenced
Packit 00408a
 */
Packit 00408a
static void
Packit 00408a
cache_ref(sid)
Packit 00408a
	short sid;
Packit 00408a
{
Packit 00408a
	int i;
Packit 00408a
	short curr;
Packit 00408a
	short prev;
Packit 00408a
Packit 00408a
	prev = authdes_lru[0];
Packit 00408a
	authdes_lru[0] = sid;
Packit 00408a
	for (i = 1; prev != sid; i++) {
Packit 00408a
		curr = authdes_lru[i];
Packit 00408a
		authdes_lru[i] = prev;
Packit 00408a
		prev = curr;
Packit 00408a
	}
Packit 00408a
}
Packit 00408a
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Find a spot in the cache for a credential containing
Packit 00408a
 * the items given.  Return -1 if a replay is detected, otherwise
Packit 00408a
 * return the spot in the cache.
Packit 00408a
 */
Packit 00408a
static short
Packit 00408a
cache_spot(key, name, timestamp)
Packit 00408a
	des_block *key;
Packit 00408a
	char *name;
Packit 00408a
	struct timeval *timestamp;
Packit 00408a
{
Packit 00408a
	struct cache_entry *cp;
Packit 00408a
	int i;
Packit 00408a
	u_long hi;
Packit 00408a
Packit 00408a
	hi = key->key.high;
Packit 00408a
	for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) {
Packit 00408a
		if (cp->key.key.high == hi && 
Packit 00408a
		    cp->key.key.low == key->key.low &&
Packit 00408a
		    cp->rname != NULL &&
Packit 00408a
		    bcmp(cp->rname, name, strlen(name) + 1) == 0) {
Packit 00408a
			if (BEFORE(timestamp, &cp->laststamp)) {
Packit 00408a
				svcauthdes_stats.ncachereplays++;
Packit 00408a
				return (-1); /* replay */
Packit 00408a
			}
Packit 00408a
			svcauthdes_stats.ncachehits++;
Packit 00408a
			return (i);	/* refresh */
Packit 00408a
		}
Packit 00408a
	}
Packit 00408a
	svcauthdes_stats.ncachemisses++;
Packit 00408a
	return (cache_victim());	/* new credential */
Packit 00408a
}
Packit 00408a
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Local credential handling stuff.
Packit 00408a
 * NOTE: bsd unix dependent.
Packit 00408a
 * Other operating systems should put something else here.
Packit 00408a
 */
Packit 00408a
#define UNKNOWN 	-2	/* grouplen, if cached cred is unknown user */
Packit 00408a
#define INVALID		-1 	/* grouplen, if cache entry is invalid */
Packit 00408a
Packit 00408a
struct bsdcred {
Packit 00408a
	short uid;		/* cached uid */
Packit 00408a
	short gid;		/* cached gid */
Packit 00408a
	short grouplen;	/* length of cached groups */
Packit 00408a
	short groups[NGROUPS];	/* cached groups */
Packit 00408a
};
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * Map a des credential into a unix cred.
Packit 00408a
 * We cache the credential here so the application does
Packit 00408a
 * not have to make an rpc call every time to interpret
Packit 00408a
 * the credential.
Packit 00408a
 */
Packit 00408a
int
Packit 00408a
authdes_getucred(adc, uid, gid, grouplen, groups)
Packit 00408a
	struct authdes_cred *adc;
Packit 00408a
	uid_t *uid;
Packit 00408a
	gid_t *gid;
Packit 00408a
	int *grouplen;
Packit 00408a
	gid_t *groups;
Packit 00408a
{
Packit 00408a
	unsigned sid;
Packit 00408a
	int i;
Packit 00408a
	uid_t i_uid;	
Packit 00408a
	gid_t i_gid;
Packit 00408a
	int i_grouplen;
Packit 00408a
	struct bsdcred *cred;
Packit 00408a
Packit 00408a
	sid = adc->adc_nickname;
Packit 00408a
	if (sid >= AUTHDES_CACHESZ) {
Packit 00408a
		LIBTIRPC_DEBUG(1, ("authdes_getucred: invalid nickname"));
Packit 00408a
		return (0);
Packit 00408a
	}
Packit 00408a
	cred = (struct bsdcred *)authdes_cache[sid].localcred;
Packit 00408a
	if (cred == NULL) {
Packit 00408a
		cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred));
Packit 00408a
		authdes_cache[sid].localcred = (char *)cred;
Packit 00408a
		cred->grouplen = INVALID;
Packit 00408a
	}
Packit 00408a
	if (cred->grouplen == INVALID) {
Packit 00408a
		/*
Packit 00408a
		 * not in cache: lookup
Packit 00408a
		 */
Packit 00408a
		if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid, 
Packit 00408a
			&i_grouplen, groups))
Packit 00408a
		{
Packit 00408a
			LIBTIRPC_DEBUG(1, ("authdes_getucred: unknown netname"));
Packit 00408a
			cred->grouplen = UNKNOWN;	/* mark as lookup up, but not found */
Packit 00408a
			return (0);
Packit 00408a
		}
Packit 00408a
		LIBTIRPC_DEBUG(1, ("authdes_getucred: missed ucred cache"));
Packit 00408a
		*uid = cred->uid = i_uid;
Packit 00408a
		*gid = cred->gid = i_gid;
Packit 00408a
		*grouplen = cred->grouplen = i_grouplen;
Packit 00408a
		for (i = i_grouplen - 1; i >= 0; i--) {
Packit 00408a
			cred->groups[i] = groups[i]; /* int to short */
Packit 00408a
		}
Packit 00408a
		return (1);
Packit 00408a
	} else if (cred->grouplen == UNKNOWN) {
Packit 00408a
		/*
Packit 00408a
		 * Already lookup up, but no match found
Packit 00408a
		 */	
Packit 00408a
		return (0);
Packit 00408a
	}
Packit 00408a
Packit 00408a
	/*
Packit 00408a
	 * cached credentials
Packit 00408a
	 */
Packit 00408a
	*uid = cred->uid;
Packit 00408a
	*gid = cred->gid;
Packit 00408a
	*grouplen = cred->grouplen;
Packit 00408a
	for (i = cred->grouplen - 1; i >= 0; i--) {
Packit 00408a
		groups[i] = cred->groups[i];	/* short to int */
Packit 00408a
	}
Packit 00408a
	return (1);
Packit 00408a
}
Packit 00408a
Packit 00408a
static void
Packit 00408a
invalidate(cred)
Packit 00408a
	char *cred;
Packit 00408a
{
Packit 00408a
	if (cred == NULL) {
Packit 00408a
		return;
Packit 00408a
	}
Packit 00408a
	((struct bsdcred *)cred)->grouplen = INVALID;
Packit 00408a
}