xref: /freebsd/lib/libc/rpc/svc_auth_des.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1e8636dfdSBill Paul 
2e8636dfdSBill Paul /*
3e8636dfdSBill Paul  * Copyright (c) 1988 by Sun Microsystems, Inc.
4e8636dfdSBill Paul  */
5e8636dfdSBill Paul 
62e322d37SHiroki Sato /*-
7*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
8*8a16b7a1SPedro F. Giffuni  *
92e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
102e322d37SHiroki Sato  * All rights reserved.
11e8636dfdSBill Paul  *
122e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
132e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
142e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
152e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
162e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
172e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
182e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
192e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
202e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
212e322d37SHiroki Sato  *   from this software without specific prior written permission.
22e8636dfdSBill Paul  *
232e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
242e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
252e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
262e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
272e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
282e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
292e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
302e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
312e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
322e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
332e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
34e8636dfdSBill Paul  */
35e8636dfdSBill Paul 
36e8636dfdSBill Paul /*
37e8636dfdSBill Paul  * svcauth_des.c, server-side des authentication
38e8636dfdSBill Paul  *
39e8636dfdSBill Paul  * We insure for the service the following:
40e8636dfdSBill Paul  * (1) The timestamp microseconds do not exceed 1 million.
41e8636dfdSBill Paul  * (2) The timestamp plus the window is less than the current time.
42e8636dfdSBill Paul  * (3) The timestamp is not less than the one previously
43e8636dfdSBill Paul  *     seen in the current session.
44e8636dfdSBill Paul  *
45e8636dfdSBill Paul  * It is up to the server to determine if the window size is
46e8636dfdSBill Paul  * too small .
47e8636dfdSBill Paul  *
48e8636dfdSBill Paul  */
49e8636dfdSBill Paul 
508360efbdSAlfred Perlstein #include "namespace.h"
519f5afc13SIan Dowse #include "reentrant.h"
52e8636dfdSBill Paul #include <string.h>
53e8636dfdSBill Paul #include <stdlib.h>
54d201fe46SDaniel Eischen #include <stdio.h>
55e8636dfdSBill Paul #include <unistd.h>
56e8636dfdSBill Paul #include <rpc/des_crypt.h>
57e8636dfdSBill Paul #include <sys/param.h>
58e8636dfdSBill Paul #include <netinet/in.h>
59e8636dfdSBill Paul #include <rpc/types.h>
60e8636dfdSBill Paul #include <rpc/xdr.h>
61e8636dfdSBill Paul #include <rpc/auth.h>
62e8636dfdSBill Paul #include <rpc/auth_des.h>
63e8636dfdSBill Paul #include <rpc/svc.h>
64e8636dfdSBill Paul #include <rpc/rpc_msg.h>
65e8636dfdSBill Paul #include <rpc/svc_auth.h>
668360efbdSAlfred Perlstein #include "libc_private.h"
67e8636dfdSBill Paul 
688d630135SAlfred Perlstein extern int key_decryptsession_pk(const char *, netobj *, des_block *);
698d630135SAlfred Perlstein 
70e8636dfdSBill Paul #define debug(msg)	 printf("svcauth_des: %s\n", msg)
71e8636dfdSBill Paul 
72e8636dfdSBill Paul #define USEC_PER_SEC ((u_long) 1000000L)
73e8636dfdSBill Paul #define BEFORE(t1, t2) timercmp(t1, t2, <)
74e8636dfdSBill Paul 
75e8636dfdSBill Paul /*
76e8636dfdSBill Paul  * LRU cache of conversation keys and some other useful items.
77e8636dfdSBill Paul  */
78e8636dfdSBill Paul #define AUTHDES_CACHESZ 64
79e8636dfdSBill Paul struct cache_entry {
80e8636dfdSBill Paul 	des_block key;		/* conversation key */
81e8636dfdSBill Paul 	char *rname;		/* client's name */
82e8636dfdSBill Paul 	u_int window;		/* credential lifetime window */
83e8636dfdSBill Paul 	struct timeval laststamp;	/* detect replays of creds */
84e8636dfdSBill Paul 	char *localcred;	/* generic local credential */
85e8636dfdSBill Paul };
86e8636dfdSBill Paul static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */;
87e8636dfdSBill Paul static short *authdes_lru/* [AUTHDES_CACHESZ] */;
88e8636dfdSBill Paul 
8977601543SCraig Rodrigues static void cache_init(void);	/* initialize the cache */
9077601543SCraig Rodrigues static short cache_spot(des_block *, char *, struct timeval *); /* find an entry in the cache */
9168895e38SCraig Rodrigues static void cache_ref(short sid);	/* note that sid was ref'd */
92e8636dfdSBill Paul 
9377601543SCraig Rodrigues static void invalidate(char *);	/* invalidate entry in cache */
94e8636dfdSBill Paul 
95e8636dfdSBill Paul /*
96e8636dfdSBill Paul  * cache statistics
97e8636dfdSBill Paul  */
98e8636dfdSBill Paul static struct {
99e8636dfdSBill Paul 	u_long ncachehits;	/* times cache hit, and is not replay */
100e8636dfdSBill Paul 	u_long ncachereplays;	/* times cache hit, and is replay */
101e8636dfdSBill Paul 	u_long ncachemisses;	/* times cache missed */
102e8636dfdSBill Paul } svcauthdes_stats;
103e8636dfdSBill Paul 
104e8636dfdSBill Paul /*
105e8636dfdSBill Paul  * Service side authenticator for AUTH_DES
106e8636dfdSBill Paul  */
107e8636dfdSBill Paul enum auth_stat
_svcauth_des(struct svc_req * rqst,struct rpc_msg * msg)10868895e38SCraig Rodrigues _svcauth_des(struct svc_req *rqst, struct rpc_msg *msg)
109e8636dfdSBill Paul {
110e8636dfdSBill Paul 
1118fb3f3f6SDavid E. O'Brien 	long *ixdr;
112e8636dfdSBill Paul 	des_block cryptbuf[2];
1138fb3f3f6SDavid E. O'Brien 	struct authdes_cred *cred;
114e8636dfdSBill Paul 	struct authdes_verf verf;
115e8636dfdSBill Paul 	int status;
1168fb3f3f6SDavid E. O'Brien 	struct cache_entry *entry;
117e8636dfdSBill Paul 	short sid = 0;
118e8636dfdSBill Paul 	des_block *sessionkey;
119e8636dfdSBill Paul 	des_block ivec;
120e8636dfdSBill Paul 	u_int window;
121e8636dfdSBill Paul 	struct timeval timestamp;
122e8636dfdSBill Paul 	u_long namelen;
123e8636dfdSBill Paul 	struct area {
124e8636dfdSBill Paul 		struct authdes_cred area_cred;
125e8636dfdSBill Paul 		char area_netname[MAXNETNAMELEN+1];
126e8636dfdSBill Paul 	} *area;
127e8636dfdSBill Paul 
128e8636dfdSBill Paul 	if (authdes_cache == NULL) {
129e8636dfdSBill Paul 		cache_init();
130e8636dfdSBill Paul 	}
131e8636dfdSBill Paul 
132e8636dfdSBill Paul 	area = (struct area *)rqst->rq_clntcred;
133e8636dfdSBill Paul 	cred = (struct authdes_cred *)&area->area_cred;
134e8636dfdSBill Paul 
135e8636dfdSBill Paul 	/*
136e8636dfdSBill Paul 	 * Get the credential
137e8636dfdSBill Paul 	 */
138e8636dfdSBill Paul 	ixdr = (long *)msg->rm_call.cb_cred.oa_base;
139e8636dfdSBill Paul 	cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind);
140e8636dfdSBill Paul 	switch (cred->adc_namekind) {
141e8636dfdSBill Paul 	case ADN_FULLNAME:
142e8636dfdSBill Paul 		namelen = IXDR_GET_U_LONG(ixdr);
143e8636dfdSBill Paul 		if (namelen > MAXNETNAMELEN) {
144e8636dfdSBill Paul 			return (AUTH_BADCRED);
145e8636dfdSBill Paul 		}
146e8636dfdSBill Paul 		cred->adc_fullname.name = area->area_netname;
147e8636dfdSBill Paul 		bcopy((char *)ixdr, cred->adc_fullname.name,
148e8636dfdSBill Paul 			(u_int)namelen);
149e8636dfdSBill Paul 		cred->adc_fullname.name[namelen] = 0;
150e8636dfdSBill Paul 		ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT);
151e8636dfdSBill Paul 		cred->adc_fullname.key.key.high = (u_long)*ixdr++;
152e8636dfdSBill Paul 		cred->adc_fullname.key.key.low = (u_long)*ixdr++;
153e8636dfdSBill Paul 		cred->adc_fullname.window = (u_long)*ixdr++;
154e8636dfdSBill Paul 		break;
155e8636dfdSBill Paul 	case ADN_NICKNAME:
156e8636dfdSBill Paul 		cred->adc_nickname = (u_long)*ixdr++;
157e8636dfdSBill Paul 		break;
158e8636dfdSBill Paul 	default:
159e8636dfdSBill Paul 		return (AUTH_BADCRED);
160e8636dfdSBill Paul 	}
161e8636dfdSBill Paul 
162e8636dfdSBill Paul 	/*
163e8636dfdSBill Paul 	 * Get the verifier
164e8636dfdSBill Paul 	 */
165e8636dfdSBill Paul 	ixdr = (long *)msg->rm_call.cb_verf.oa_base;
166e8636dfdSBill Paul 	verf.adv_xtimestamp.key.high = (u_long)*ixdr++;
167e8636dfdSBill Paul 	verf.adv_xtimestamp.key.low =  (u_long)*ixdr++;
168e8636dfdSBill Paul 	verf.adv_int_u = (u_long)*ixdr++;
169e8636dfdSBill Paul 
170e8636dfdSBill Paul 
171e8636dfdSBill Paul 	/*
172e8636dfdSBill Paul 	 * Get the conversation key
173e8636dfdSBill Paul  	 */
174e8636dfdSBill Paul 	if (cred->adc_namekind == ADN_FULLNAME) {
175e8636dfdSBill Paul 		netobj pkey;
176e8636dfdSBill Paul 		char pkey_data[1024];
177e8636dfdSBill Paul 
178e8636dfdSBill Paul 		sessionkey = &cred->adc_fullname.key;
179e8636dfdSBill Paul 		if (! getpublickey(cred->adc_fullname.name, pkey_data)) {
180e8636dfdSBill Paul 			debug("getpublickey");
181e8636dfdSBill Paul 			return(AUTH_BADCRED);
182e8636dfdSBill Paul 		}
183e8636dfdSBill Paul 		pkey.n_bytes = pkey_data;
184e8636dfdSBill Paul 		pkey.n_len = strlen(pkey_data) + 1;
185e8636dfdSBill Paul 		if (key_decryptsession_pk(cred->adc_fullname.name, &pkey,
186e8636dfdSBill Paul 				       sessionkey) < 0) {
187e8636dfdSBill Paul 			debug("decryptsessionkey");
188e8636dfdSBill Paul 			return (AUTH_BADCRED); /* key not found */
189e8636dfdSBill Paul 		}
190e8636dfdSBill Paul 	} else { /* ADN_NICKNAME */
191e8636dfdSBill Paul 		sid = (short)cred->adc_nickname;
1928360efbdSAlfred Perlstein 		if (sid < 0 || sid >= AUTHDES_CACHESZ) {
193e8636dfdSBill Paul 			debug("bad nickname");
194e8636dfdSBill Paul 			return (AUTH_BADCRED);	/* garbled credential */
195e8636dfdSBill Paul 		}
196e8636dfdSBill Paul 		sessionkey = &authdes_cache[sid].key;
197e8636dfdSBill Paul 	}
198e8636dfdSBill Paul 
199e8636dfdSBill Paul 
200e8636dfdSBill Paul 	/*
201e8636dfdSBill Paul 	 * Decrypt the timestamp
202e8636dfdSBill Paul 	 */
203e8636dfdSBill Paul 	cryptbuf[0] = verf.adv_xtimestamp;
204e8636dfdSBill Paul 	if (cred->adc_namekind == ADN_FULLNAME) {
205e8636dfdSBill Paul 		cryptbuf[1].key.high = cred->adc_fullname.window;
206e8636dfdSBill Paul 		cryptbuf[1].key.low = verf.adv_winverf;
207e8636dfdSBill Paul 		ivec.key.high = ivec.key.low = 0;
208e8636dfdSBill Paul 		status = cbc_crypt((char *)sessionkey, (char *)cryptbuf,
209e8636dfdSBill Paul 			2*sizeof(des_block), DES_DECRYPT | DES_HW,
210e8636dfdSBill Paul 			(char *)&ivec);
211e8636dfdSBill Paul 	} else {
212e8636dfdSBill Paul 		status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
213e8636dfdSBill Paul 			sizeof(des_block), DES_DECRYPT | DES_HW);
214e8636dfdSBill Paul 	}
215e8636dfdSBill Paul 	if (DES_FAILED(status)) {
216e8636dfdSBill Paul 		debug("decryption failure");
217e8636dfdSBill Paul 		return (AUTH_FAILED);	/* system error */
218e8636dfdSBill Paul 	}
219e8636dfdSBill Paul 
220e8636dfdSBill Paul 	/*
221e8636dfdSBill Paul 	 * XDR the decrypted timestamp
222e8636dfdSBill Paul 	 */
223e8636dfdSBill Paul 	ixdr = (long *)cryptbuf;
224e8636dfdSBill Paul 	timestamp.tv_sec = IXDR_GET_LONG(ixdr);
225e8636dfdSBill Paul 	timestamp.tv_usec = IXDR_GET_LONG(ixdr);
226e8636dfdSBill Paul 
227e8636dfdSBill Paul 	/*
228e8636dfdSBill Paul  	 * Check for valid credentials and verifiers.
229e8636dfdSBill Paul 	 * They could be invalid because the key was flushed
230e8636dfdSBill Paul 	 * out of the cache, and so a new session should begin.
231e8636dfdSBill Paul 	 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
232e8636dfdSBill Paul 	 */
233e8636dfdSBill Paul 	{
234e8636dfdSBill Paul 		struct timeval current;
235e8636dfdSBill Paul 		int nick;
236e8636dfdSBill Paul 		int winverf;
237e8636dfdSBill Paul 
238e8636dfdSBill Paul 		if (cred->adc_namekind == ADN_FULLNAME) {
239e8636dfdSBill Paul 			window = IXDR_GET_U_LONG(ixdr);
240e8636dfdSBill Paul 			winverf = IXDR_GET_U_LONG(ixdr);
241e8636dfdSBill Paul 			if (winverf != window - 1) {
242e8636dfdSBill Paul 				debug("window verifier mismatch");
243e8636dfdSBill Paul 				return (AUTH_BADCRED);	/* garbled credential */
244e8636dfdSBill Paul 			}
245e8636dfdSBill Paul 			sid = cache_spot(sessionkey, cred->adc_fullname.name,
246e8636dfdSBill Paul 			    &timestamp);
247e8636dfdSBill Paul 			if (sid < 0) {
248e8636dfdSBill Paul 				debug("replayed credential");
249e8636dfdSBill Paul 				return (AUTH_REJECTEDCRED);	/* replay */
250e8636dfdSBill Paul 			}
251e8636dfdSBill Paul 			nick = 0;
252e8636dfdSBill Paul 		} else {	/* ADN_NICKNAME */
253e8636dfdSBill Paul 			window = authdes_cache[sid].window;
254e8636dfdSBill Paul 			nick = 1;
255e8636dfdSBill Paul 		}
256e8636dfdSBill Paul 
257e8636dfdSBill Paul 		if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) {
258e8636dfdSBill Paul 			debug("invalid usecs");
259e8636dfdSBill Paul 			/* cached out (bad key), or garbled verifier */
260e8636dfdSBill Paul 			return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF);
261e8636dfdSBill Paul 		}
262e8636dfdSBill Paul 		if (nick && BEFORE(&timestamp,
263e8636dfdSBill Paul 				   &authdes_cache[sid].laststamp)) {
264e8636dfdSBill Paul 			debug("timestamp before last seen");
265e8636dfdSBill Paul 			return (AUTH_REJECTEDVERF);	/* replay */
266e8636dfdSBill Paul 		}
267902d9eafSEd Schouten 		(void)gettimeofday(&current, NULL);
268e8636dfdSBill Paul 		current.tv_sec -= window;	/* allow for expiration */
269e8636dfdSBill Paul 		if (!BEFORE(&current, &timestamp)) {
270e8636dfdSBill Paul 			debug("timestamp expired");
271e8636dfdSBill Paul 			/* replay, or garbled credential */
272e8636dfdSBill Paul 			return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
273e8636dfdSBill Paul 		}
274e8636dfdSBill Paul 	}
275e8636dfdSBill Paul 
276e8636dfdSBill Paul 	/*
277e8636dfdSBill Paul 	 * Set up the reply verifier
278e8636dfdSBill Paul 	 */
279e8636dfdSBill Paul 	verf.adv_nickname = (u_long)sid;
280e8636dfdSBill Paul 
281e8636dfdSBill Paul 	/*
282e8636dfdSBill Paul 	 * xdr the timestamp before encrypting
283e8636dfdSBill Paul 	 */
284e8636dfdSBill Paul 	ixdr = (long *)cryptbuf;
285e8636dfdSBill Paul 	IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1);
286e8636dfdSBill Paul 	IXDR_PUT_LONG(ixdr, timestamp.tv_usec);
287e8636dfdSBill Paul 
288e8636dfdSBill Paul 	/*
289e8636dfdSBill Paul 	 * encrypt the timestamp
290e8636dfdSBill Paul 	 */
291e8636dfdSBill Paul 	status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
292e8636dfdSBill Paul 	    sizeof(des_block), DES_ENCRYPT | DES_HW);
293e8636dfdSBill Paul 	if (DES_FAILED(status)) {
294e8636dfdSBill Paul 		debug("encryption failure");
295e8636dfdSBill Paul 		return (AUTH_FAILED);	/* system error */
296e8636dfdSBill Paul 	}
297e8636dfdSBill Paul 	verf.adv_xtimestamp = cryptbuf[0];
298e8636dfdSBill Paul 
299e8636dfdSBill Paul 	/*
300e8636dfdSBill Paul 	 * Serialize the reply verifier, and update rqst
301e8636dfdSBill Paul 	 */
302e8636dfdSBill Paul 	ixdr = (long *)msg->rm_call.cb_verf.oa_base;
303e8636dfdSBill Paul 	*ixdr++ = (long)verf.adv_xtimestamp.key.high;
304e8636dfdSBill Paul 	*ixdr++ = (long)verf.adv_xtimestamp.key.low;
305e8636dfdSBill Paul 	*ixdr++ = (long)verf.adv_int_u;
306e8636dfdSBill Paul 
307e8636dfdSBill Paul 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
308e8636dfdSBill Paul 	rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
309e8636dfdSBill Paul 	rqst->rq_xprt->xp_verf.oa_length =
310e8636dfdSBill Paul 		(char *)ixdr - msg->rm_call.cb_verf.oa_base;
311e8636dfdSBill Paul 
312e8636dfdSBill Paul 	/*
313e8636dfdSBill Paul 	 * We succeeded, commit the data to the cache now and
314e8636dfdSBill Paul 	 * finish cooking the credential.
315e8636dfdSBill Paul 	 */
316e8636dfdSBill Paul 	entry = &authdes_cache[sid];
317e8636dfdSBill Paul 	entry->laststamp = timestamp;
318e8636dfdSBill Paul 	cache_ref(sid);
319e8636dfdSBill Paul 	if (cred->adc_namekind == ADN_FULLNAME) {
320e8636dfdSBill Paul 		cred->adc_fullname.window = window;
321e8636dfdSBill Paul 		cred->adc_nickname = (u_long)sid;	/* save nickname */
322e8636dfdSBill Paul 		if (entry->rname != NULL) {
323e8636dfdSBill Paul 			mem_free(entry->rname, strlen(entry->rname) + 1);
324e8636dfdSBill Paul 		}
325e8636dfdSBill Paul 		entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name)
326e8636dfdSBill Paul 					 + 1);
327e8636dfdSBill Paul 		if (entry->rname != NULL) {
328e8636dfdSBill Paul 			(void) strcpy(entry->rname, cred->adc_fullname.name);
329e8636dfdSBill Paul 		} else {
330e8636dfdSBill Paul 			debug("out of memory");
331e8636dfdSBill Paul 		}
332e8636dfdSBill Paul 		entry->key = *sessionkey;
333e8636dfdSBill Paul 		entry->window = window;
334e8636dfdSBill Paul 		invalidate(entry->localcred); /* mark any cached cred invalid */
335e8636dfdSBill Paul 	} else { /* ADN_NICKNAME */
336e8636dfdSBill Paul 		/*
337e8636dfdSBill Paul 		 * nicknames are cooked into fullnames
338e8636dfdSBill Paul 		 */
339e8636dfdSBill Paul 		cred->adc_namekind = ADN_FULLNAME;
340e8636dfdSBill Paul 		cred->adc_fullname.name = entry->rname;
341e8636dfdSBill Paul 		cred->adc_fullname.key = entry->key;
342e8636dfdSBill Paul 		cred->adc_fullname.window = entry->window;
343e8636dfdSBill Paul 	}
344e8636dfdSBill Paul 	return (AUTH_OK);	/* we made it!*/
345e8636dfdSBill Paul }
346e8636dfdSBill Paul 
347e8636dfdSBill Paul 
348e8636dfdSBill Paul /*
349e8636dfdSBill Paul  * Initialize the cache
350e8636dfdSBill Paul  */
351e8636dfdSBill Paul static void
cache_init(void)35277601543SCraig Rodrigues cache_init(void)
353e8636dfdSBill Paul {
3548fb3f3f6SDavid E. O'Brien 	int i;
355e8636dfdSBill Paul 
356e8636dfdSBill Paul 	authdes_cache = (struct cache_entry *)
357e8636dfdSBill Paul 		mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ);
358e8636dfdSBill Paul 	bzero((char *)authdes_cache,
359e8636dfdSBill Paul 		sizeof(struct cache_entry) * AUTHDES_CACHESZ);
360e8636dfdSBill Paul 
361e8636dfdSBill Paul 	authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ);
362e8636dfdSBill Paul 	/*
363e8636dfdSBill Paul 	 * Initialize the lru list
364e8636dfdSBill Paul 	 */
365e8636dfdSBill Paul 	for (i = 0; i < AUTHDES_CACHESZ; i++) {
366e8636dfdSBill Paul 		authdes_lru[i] = i;
367e8636dfdSBill Paul 	}
368e8636dfdSBill Paul }
369e8636dfdSBill Paul 
370e8636dfdSBill Paul 
371e8636dfdSBill Paul /*
372e8636dfdSBill Paul  * Find the lru victim
373e8636dfdSBill Paul  */
374e8636dfdSBill Paul static short
cache_victim(void)37577601543SCraig Rodrigues cache_victim(void)
376e8636dfdSBill Paul {
377e8636dfdSBill Paul 	return (authdes_lru[AUTHDES_CACHESZ-1]);
378e8636dfdSBill Paul }
379e8636dfdSBill Paul 
380e8636dfdSBill Paul /*
381e8636dfdSBill Paul  * Note that sid was referenced
382e8636dfdSBill Paul  */
383e8636dfdSBill Paul static void
cache_ref(short sid)38468895e38SCraig Rodrigues cache_ref(short sid)
385e8636dfdSBill Paul {
3868fb3f3f6SDavid E. O'Brien 	int i;
3878fb3f3f6SDavid E. O'Brien 	short curr;
3888fb3f3f6SDavid E. O'Brien 	short prev;
389e8636dfdSBill Paul 
390e8636dfdSBill Paul 	prev = authdes_lru[0];
391e8636dfdSBill Paul 	authdes_lru[0] = sid;
392e8636dfdSBill Paul 	for (i = 1; prev != sid; i++) {
393e8636dfdSBill Paul 		curr = authdes_lru[i];
394e8636dfdSBill Paul 		authdes_lru[i] = prev;
395e8636dfdSBill Paul 		prev = curr;
396e8636dfdSBill Paul 	}
397e8636dfdSBill Paul }
398e8636dfdSBill Paul 
399e8636dfdSBill Paul 
400e8636dfdSBill Paul /*
401e8636dfdSBill Paul  * Find a spot in the cache for a credential containing
402e8636dfdSBill Paul  * the items given.  Return -1 if a replay is detected, otherwise
403e8636dfdSBill Paul  * return the spot in the cache.
404e8636dfdSBill Paul  */
405e8636dfdSBill Paul static short
cache_spot(des_block * key,char * name,struct timeval * timestamp)40668895e38SCraig Rodrigues cache_spot(des_block *key, char *name, struct timeval *timestamp)
407e8636dfdSBill Paul {
4088fb3f3f6SDavid E. O'Brien 	struct cache_entry *cp;
4098fb3f3f6SDavid E. O'Brien 	int i;
4108fb3f3f6SDavid E. O'Brien 	u_long hi;
411e8636dfdSBill Paul 
412e8636dfdSBill Paul 	hi = key->key.high;
413e8636dfdSBill Paul 	for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) {
414e8636dfdSBill Paul 		if (cp->key.key.high == hi &&
415e8636dfdSBill Paul 		    cp->key.key.low == key->key.low &&
416e8636dfdSBill Paul 		    cp->rname != NULL &&
417e8636dfdSBill Paul 		    bcmp(cp->rname, name, strlen(name) + 1) == 0) {
418e8636dfdSBill Paul 			if (BEFORE(timestamp, &cp->laststamp)) {
419e8636dfdSBill Paul 				svcauthdes_stats.ncachereplays++;
420e8636dfdSBill Paul 				return (-1); /* replay */
421e8636dfdSBill Paul 			}
422e8636dfdSBill Paul 			svcauthdes_stats.ncachehits++;
423e8636dfdSBill Paul 			return (i);	/* refresh */
424e8636dfdSBill Paul 		}
425e8636dfdSBill Paul 	}
426e8636dfdSBill Paul 	svcauthdes_stats.ncachemisses++;
427e8636dfdSBill Paul 	return (cache_victim());	/* new credential */
428e8636dfdSBill Paul }
429e8636dfdSBill Paul 
430e8636dfdSBill Paul 
431e8636dfdSBill Paul #if (defined(sun) || defined(vax) || defined(__FreeBSD__))
432e8636dfdSBill Paul /*
433e8636dfdSBill Paul  * Local credential handling stuff.
434e8636dfdSBill Paul  * NOTE: bsd unix dependent.
435e8636dfdSBill Paul  * Other operating systems should put something else here.
436e8636dfdSBill Paul  */
437e8636dfdSBill Paul #define UNKNOWN 	-2	/* grouplen, if cached cred is unknown user */
438e8636dfdSBill Paul #define INVALID		-1 	/* grouplen, if cache entry is invalid */
439e8636dfdSBill Paul 
440e8636dfdSBill Paul struct bsdcred {
4419d8e1a83SBrooks Davis 	uid_t uid;		/* cached uid */
4429d8e1a83SBrooks Davis 	gid_t gid;		/* cached gid */
4439d8e1a83SBrooks Davis 	int grouplen;	/* length of cached groups */
4449d8e1a83SBrooks Davis 	gid_t groups[NGRPS];	/* cached groups */
445e8636dfdSBill Paul };
446e8636dfdSBill Paul 
447e8636dfdSBill Paul /*
448e8636dfdSBill Paul  * Map a des credential into a unix cred.
449e8636dfdSBill Paul  * We cache the credential here so the application does
450e8636dfdSBill Paul  * not have to make an rpc call every time to interpret
451e8636dfdSBill Paul  * the credential.
452e8636dfdSBill Paul  */
453e8636dfdSBill Paul int
authdes_getucred(struct authdes_cred * adc,uid_t * uid,gid_t * gid,int * grouplen,gid_t * groups)45468895e38SCraig Rodrigues authdes_getucred(struct authdes_cred *adc, uid_t *uid, gid_t *gid,
45568895e38SCraig Rodrigues     int *grouplen, gid_t *groups)
456e8636dfdSBill Paul {
457e8636dfdSBill Paul 	unsigned sid;
4588fb3f3f6SDavid E. O'Brien 	int i;
459e8636dfdSBill Paul 	uid_t i_uid;
460e8636dfdSBill Paul 	gid_t i_gid;
461e8636dfdSBill Paul 	int i_grouplen;
462e8636dfdSBill Paul 	struct bsdcred *cred;
463e8636dfdSBill Paul 
464e8636dfdSBill Paul 	sid = adc->adc_nickname;
465e8636dfdSBill Paul 	if (sid >= AUTHDES_CACHESZ) {
466e8636dfdSBill Paul 		debug("invalid nickname");
467e8636dfdSBill Paul 		return (0);
468e8636dfdSBill Paul 	}
469e8636dfdSBill Paul 	cred = (struct bsdcred *)authdes_cache[sid].localcred;
470e8636dfdSBill Paul 	if (cred == NULL) {
471e8636dfdSBill Paul 		cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred));
472e8636dfdSBill Paul 		authdes_cache[sid].localcred = (char *)cred;
473e8636dfdSBill Paul 		cred->grouplen = INVALID;
474e8636dfdSBill Paul 	}
475e8636dfdSBill Paul 	if (cred->grouplen == INVALID) {
476e8636dfdSBill Paul 		/*
477e8636dfdSBill Paul 		 * not in cache: lookup
478e8636dfdSBill Paul 		 */
479e8636dfdSBill Paul 		if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid,
480e8636dfdSBill Paul 			&i_grouplen, groups))
481e8636dfdSBill Paul 		{
482e8636dfdSBill Paul 			debug("unknown netname");
483e8636dfdSBill Paul 			cred->grouplen = UNKNOWN;	/* mark as lookup up, but not found */
484e8636dfdSBill Paul 			return (0);
485e8636dfdSBill Paul 		}
486e8636dfdSBill Paul 		debug("missed ucred cache");
487e8636dfdSBill Paul 		*uid = cred->uid = i_uid;
488e8636dfdSBill Paul 		*gid = cred->gid = i_gid;
489e8636dfdSBill Paul 		*grouplen = cred->grouplen = i_grouplen;
490e8636dfdSBill Paul 		for (i = i_grouplen - 1; i >= 0; i--) {
491e8636dfdSBill Paul 			cred->groups[i] = groups[i]; /* int to short */
492e8636dfdSBill Paul 		}
493e8636dfdSBill Paul 		return (1);
494e8636dfdSBill Paul 	} else if (cred->grouplen == UNKNOWN) {
495e8636dfdSBill Paul 		/*
496e8636dfdSBill Paul 		 * Already lookup up, but no match found
497e8636dfdSBill Paul 		 */
498e8636dfdSBill Paul 		return (0);
499e8636dfdSBill Paul 	}
500e8636dfdSBill Paul 
501e8636dfdSBill Paul 	/*
502e8636dfdSBill Paul 	 * cached credentials
503e8636dfdSBill Paul 	 */
504e8636dfdSBill Paul 	*uid = cred->uid;
505e8636dfdSBill Paul 	*gid = cred->gid;
506e8636dfdSBill Paul 	*grouplen = cred->grouplen;
507e8636dfdSBill Paul 	for (i = cred->grouplen - 1; i >= 0; i--) {
508e8636dfdSBill Paul 		groups[i] = cred->groups[i];	/* short to int */
509e8636dfdSBill Paul 	}
510e8636dfdSBill Paul 	return (1);
511e8636dfdSBill Paul }
512e8636dfdSBill Paul 
513e8636dfdSBill Paul static void
invalidate(char * cred)51468895e38SCraig Rodrigues invalidate(char *cred)
515e8636dfdSBill Paul {
516e8636dfdSBill Paul 	if (cred == NULL) {
517e8636dfdSBill Paul 		return;
518e8636dfdSBill Paul 	}
519e8636dfdSBill Paul 	((struct bsdcred *)cred)->grouplen = INVALID;
520e8636dfdSBill Paul }
521e8636dfdSBill Paul #endif
522e8636dfdSBill Paul 
523