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