Update.
[glibc.git] / sunrpc / svcauth_des.c
blobc2dde692f4fa29a2154282b704ef40b803bd2866
1 #if defined(LIBC_SCCS) && !defined(lint)
2 static char sccsid[] = "@(#)svcauth_des.c 2.3 89/07/11 4.0 RPCSRC; from 1.15 88/02/08 SMI";
3 #endif
5 /*
6 * Copyright (c) 1988 by Sun Microsystems, Inc.
7 */
9 /*
10 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
11 * unrestricted use provided that this legend is included on all tape
12 * media and as a part of the software program in whole or part. Users
13 * may copy or modify Sun RPC without charge, but are not authorized
14 * to license or distribute it to anyone else except as part of a product or
15 * program developed by the user.
17 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
18 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
21 * Sun RPC is provided with no support and without any obligation on the
22 * part of Sun Microsystems, Inc. to assist in its use, correction,
23 * modification or enhancement.
25 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
26 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
27 * OR ANY PART THEREOF.
29 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
30 * or profits or other special, indirect and consequential damages, even if
31 * Sun has been advised of the possibility of such damages.
33 * Sun Microsystems, Inc.
34 * 2550 Garcia Avenue
35 * Mountain View, California 94043
39 * svcauth_des.c, server-side des authentication
41 * We insure for the service the following:
42 * (1) The timestamp microseconds do not exceed 1 million.
43 * (2) The timestamp plus the window is less than the current time.
44 * (3) The timestamp is not less than the one previously
45 * seen in the current session.
47 * It is up to the server to determine if the window size is
48 * too small .
52 #include <string.h>
53 #include <sys/param.h>
54 #include <netinet/in.h>
55 #include <rpc/types.h>
56 #include <rpc/xdr.h>
57 #include <rpc/auth.h>
58 #include <rpc/auth_des.h>
59 #include <rpc/svc_auth.h>
60 #include <rpc/svc.h>
61 #include <rpc/des_crypt.h>
63 #define debug(msg) /*printf("svcauth_des: %s\n", msg) */
65 #define USEC_PER_SEC ((u_long) 1000000L)
66 #define BEFORE(t1, t2) timercmp(t1, t2, <)
69 * LRU cache of conversation keys and some other useful items.
71 #define AUTHDES_CACHESZ 64
72 struct cache_entry
74 des_block key; /* conversation key */
75 char *rname; /* client's name */
76 u_int window; /* credential lifetime window */
77 struct timeval laststamp; /* detect replays of creds */
78 char *localcred; /* generic local credential */
80 static struct cache_entry *authdes_cache /* [AUTHDES_CACHESZ] */ ;
81 static short *authdes_lru /* [AUTHDES_CACHESZ] */ ;
83 static void cache_init (void) internal_function; /* initialize the cache */
84 static short cache_spot (des_block *, char *, struct timeval *)
85 internal_function; /* find an entry in the cache */
86 static void cache_ref (short sid) internal_function;
87 /* note that sid was ref'd */
89 static void invalidate (char *cred) internal_function;
90 /* invalidate entry in cache */
93 * cache statistics
95 struct
97 u_long ncachehits; /* times cache hit, and is not replay */
98 u_long ncachereplays; /* times cache hit, and is replay */
99 u_long ncachemisses; /* times cache missed */
101 svcauthdes_stats;
104 * Service side authenticator for AUTH_DES
106 enum auth_stat
107 _svcauth_des (register struct svc_req *rqst, register struct rpc_msg *msg)
109 register long *ixdr;
110 des_block cryptbuf[2];
111 register struct authdes_cred *cred;
112 struct authdes_verf verf;
113 int status;
114 register struct cache_entry *entry;
115 short sid = 0;
116 des_block *sessionkey;
117 des_block ivec;
118 u_int window;
119 struct timeval timestamp;
120 u_long namelen;
121 struct area
123 struct authdes_cred area_cred;
124 char area_netname[MAXNETNAMELEN + 1];
126 *area;
128 if (authdes_cache == NULL)
129 cache_init ();
131 area = (struct area *) rqst->rq_clntcred;
132 cred = (struct authdes_cred *) &area->area_cred;
135 * Get the credential
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)
141 case ADN_FULLNAME:
142 namelen = IXDR_GET_U_LONG (ixdr);
143 if (namelen > MAXNETNAMELEN)
145 return AUTH_BADCRED;
147 cred->adc_fullname.name = area->area_netname;
148 bcopy ((char *) ixdr, cred->adc_fullname.name,
149 (u_int) namelen);
150 cred->adc_fullname.name[namelen] = 0;
151 ixdr += (RNDUP (namelen) / BYTES_PER_XDR_UNIT);
152 cred->adc_fullname.key.key.high = (u_long) * ixdr++;
153 cred->adc_fullname.key.key.low = (u_long) * ixdr++;
154 cred->adc_fullname.window = (u_long) * ixdr++;
155 break;
156 case ADN_NICKNAME:
157 cred->adc_nickname = (u_long) * ixdr++;
158 break;
159 default:
160 return AUTH_BADCRED;
164 * Get the verifier
166 ixdr = (long *) msg->rm_call.cb_verf.oa_base;
167 verf.adv_xtimestamp.key.high = (u_long) * ixdr++;
168 verf.adv_xtimestamp.key.low = (u_long) * ixdr++;
169 verf.adv_int_u = (u_long) * ixdr++;
173 * Get the conversation key
175 if (cred->adc_namekind == ADN_FULLNAME)
177 sessionkey = &cred->adc_fullname.key;
178 if (key_decryptsession (cred->adc_fullname.name,
179 sessionkey) < 0)
181 debug ("decryptsessionkey");
182 return AUTH_BADCRED; /* key not found */
185 else
186 { /* ADN_NICKNAME */
187 sid = (short) cred->adc_nickname;
188 if (sid >= AUTHDES_CACHESZ)
190 debug ("bad nickname");
191 return AUTH_BADCRED; /* garbled credential */
193 sessionkey = &authdes_cache[sid].key;
198 * Decrypt the timestamp
200 cryptbuf[0] = verf.adv_xtimestamp;
201 if (cred->adc_namekind == ADN_FULLNAME)
203 cryptbuf[1].key.high = cred->adc_fullname.window;
204 cryptbuf[1].key.low = verf.adv_winverf;
205 ivec.key.high = ivec.key.low = 0;
206 status = cbc_crypt ((char *) sessionkey, (char *) cryptbuf,
207 2 * sizeof (des_block), DES_DECRYPT | DES_HW,
208 (char *) &ivec);
210 else
212 status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf,
213 sizeof (des_block), DES_DECRYPT | DES_HW);
215 if (DES_FAILED (status))
217 debug ("decryption failure");
218 return AUTH_FAILED; /* system error */
222 * XDR the decrypted timestamp
224 ixdr = (long *) cryptbuf;
225 timestamp.tv_sec = IXDR_GET_LONG (ixdr);
226 timestamp.tv_usec = IXDR_GET_LONG (ixdr);
229 * Check for valid credentials and verifiers.
230 * They could be invalid because the key was flushed
231 * out of the cache, and so a new session should begin.
232 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
235 struct timeval current;
236 int nick;
237 u_int winverf;
239 if (cred->adc_namekind == ADN_FULLNAME)
241 window = IXDR_GET_U_LONG (ixdr);
242 winverf = IXDR_GET_U_LONG (ixdr);
243 if (winverf != window - 1)
245 debug ("window verifier mismatch");
246 return AUTH_BADCRED; /* garbled credential */
248 sid = cache_spot (sessionkey, cred->adc_fullname.name,
249 &timestamp);
250 if (sid < 0)
252 debug ("replayed credential");
253 return AUTH_REJECTEDCRED; /* replay */
255 nick = 0;
257 else
258 { /* ADN_NICKNAME */
259 window = authdes_cache[sid].window;
260 nick = 1;
263 if ((u_long) timestamp.tv_usec >= USEC_PER_SEC)
265 debug ("invalid usecs");
266 /* cached out (bad key), or garbled verifier */
267 return nick ? AUTH_REJECTEDVERF : AUTH_BADVERF;
269 if (nick && BEFORE (&timestamp,
270 &authdes_cache[sid].laststamp))
272 debug ("timestamp before last seen");
273 return (AUTH_REJECTEDVERF); /* replay */
275 gettimeofday (&current, (struct timezone *) NULL);
276 current.tv_sec -= window; /* allow for expiration */
277 if (!BEFORE (&current, &timestamp))
279 debug ("timestamp expired");
280 /* replay, or garbled credential */
281 return nick ? AUTH_REJECTEDVERF : AUTH_BADCRED;
286 * Set up the reply verifier
288 verf.adv_nickname = (u_long) sid;
291 * xdr the timestamp before encrypting
293 ixdr = (long *) cryptbuf;
294 IXDR_PUT_LONG (ixdr, timestamp.tv_sec - 1);
295 IXDR_PUT_LONG (ixdr, timestamp.tv_usec);
298 * encrypt the timestamp
300 status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf,
301 sizeof (des_block), DES_ENCRYPT | DES_HW);
302 if (DES_FAILED (status))
304 debug ("encryption failure");
305 return AUTH_FAILED; /* system error */
307 verf.adv_xtimestamp = cryptbuf[0];
310 * Serialize the reply verifier, and update rqst
312 ixdr = (long *) msg->rm_call.cb_verf.oa_base;
313 *ixdr++ = (long) verf.adv_xtimestamp.key.high;
314 *ixdr++ = (long) verf.adv_xtimestamp.key.low;
315 *ixdr++ = (long) verf.adv_int_u;
317 rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
318 rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
319 rqst->rq_xprt->xp_verf.oa_length =
320 (char *) ixdr - msg->rm_call.cb_verf.oa_base;
323 * We succeeded, commit the data to the cache now and
324 * finish cooking the credential.
326 entry = &authdes_cache[sid];
327 entry->laststamp = timestamp;
328 cache_ref (sid);
329 if (cred->adc_namekind == ADN_FULLNAME)
331 cred->adc_fullname.window = window;
332 cred->adc_nickname = (u_long) sid; /* save nickname */
333 if (entry->rname != NULL)
335 mem_free (entry->rname, strlen (entry->rname) + 1);
337 entry->rname = mem_alloc ((u_int) strlen (cred->adc_fullname.name)
338 + 1);
339 if (entry->rname != NULL)
341 strcpy (entry->rname, cred->adc_fullname.name);
343 else
345 debug ("out of memory");
347 entry->key = *sessionkey;
348 entry->window = window;
349 invalidate (entry->localcred); /* mark any cached cred invalid */
351 else
352 { /* ADN_NICKNAME */
354 * nicknames are cooked into fullnames
356 cred->adc_namekind = ADN_FULLNAME;
357 cred->adc_fullname.name = entry->rname;
358 cred->adc_fullname.key = entry->key;
359 cred->adc_fullname.window = entry->window;
361 return AUTH_OK; /* we made it! */
366 * Initialize the cache
368 static void
369 internal_function
370 cache_init (void)
372 register int i;
374 authdes_cache = (struct cache_entry *)
375 mem_alloc (sizeof (struct cache_entry) * AUTHDES_CACHESZ);
376 bzero ((char *) authdes_cache,
377 sizeof (struct cache_entry) * AUTHDES_CACHESZ);
379 authdes_lru = (short *) mem_alloc (sizeof (short) * AUTHDES_CACHESZ);
381 * Initialize the lru list
383 for (i = 0; i < AUTHDES_CACHESZ; i++)
385 authdes_lru[i] = i;
391 * Find the lru victim
393 static short
394 cache_victim (void)
396 return (authdes_lru[AUTHDES_CACHESZ - 1]);
400 * Note that sid was referenced
402 static void
403 internal_function
404 cache_ref (register short sid)
406 register int i;
407 register short curr;
408 register short prev;
410 prev = authdes_lru[0];
411 authdes_lru[0] = sid;
412 for (i = 1; prev != sid; i++)
414 curr = authdes_lru[i];
415 authdes_lru[i] = prev;
416 prev = curr;
422 * Find a spot in the cache for a credential containing
423 * the items given. Return -1 if a replay is detected, otherwise
424 * return the spot in the cache.
426 static short
427 internal_function
428 cache_spot (register des_block * key, char *name, struct timeval *timestamp)
430 register struct cache_entry *cp;
431 register int i;
432 register u_long hi;
434 hi = key->key.high;
435 for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++)
437 if (cp->key.key.high == hi &&
438 cp->key.key.low == key->key.low &&
439 cp->rname != NULL &&
440 bcmp (cp->rname, name, strlen (name) + 1) == 0)
442 if (BEFORE (timestamp, &cp->laststamp))
444 svcauthdes_stats.ncachereplays++;
445 return -1; /* replay */
447 svcauthdes_stats.ncachehits++;
448 return i; /* refresh */
451 svcauthdes_stats.ncachemisses++;
452 return cache_victim (); /* new credential */
457 * Local credential handling stuff.
458 * NOTE: bsd unix dependent.
459 * Other operating systems should put something else here.
461 #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
462 #define INVALID -1 /* grouplen, if cache entry is invalid */
464 struct bsdcred
466 uid_t uid; /* cached uid */
467 gid_t gid; /* cached gid */
468 short grouplen; /* length of cached groups */
469 gid_t groups[NGROUPS]; /* cached groups */
473 * Map a des credential into a unix cred.
474 * We cache the credential here so the application does
475 * not have to make an rpc call every time to interpret
476 * the credential.
479 authdes_getucred (const struct authdes_cred *adc, uid_t * uid, gid_t * gid,
480 short *grouplen, gid_t * groups)
482 unsigned sid;
483 register int i;
484 uid_t i_uid;
485 gid_t i_gid;
486 int i_grouplen;
487 struct bsdcred *cred;
489 sid = adc->adc_nickname;
490 if (sid >= AUTHDES_CACHESZ)
492 debug ("invalid nickname");
493 return 0;
495 cred = (struct bsdcred *) authdes_cache[sid].localcred;
496 if (cred == NULL)
498 cred = (struct bsdcred *) mem_alloc (sizeof (struct bsdcred));
499 authdes_cache[sid].localcred = (char *) cred;
500 cred->grouplen = INVALID;
502 if (cred->grouplen == INVALID)
505 * not in cache: lookup
507 if (!netname2user (adc->adc_fullname.name, &i_uid, &i_gid,
508 &i_grouplen, groups))
510 debug ("unknown netname");
511 cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */
512 return 0;
514 debug ("missed ucred cache");
515 *uid = cred->uid = i_uid;
516 *gid = cred->gid = i_gid;
517 *grouplen = cred->grouplen = i_grouplen;
518 for (i = i_grouplen - 1; i >= 0; i--)
520 cred->groups[i] = groups[i]; /* int to short */
522 return 1;
524 else if (cred->grouplen == UNKNOWN)
527 * Already lookup up, but no match found
529 return 0;
533 * cached credentials
535 *uid = cred->uid;
536 *gid = cred->gid;
537 *grouplen = cred->grouplen;
538 for (i = cred->grouplen - 1; i >= 0; i--)
540 groups[i] = cred->groups[i]; /* short to int */
542 return 1;
545 static void
546 internal_function
547 invalidate (char *cred)
549 if (cred == NULL)
551 return;
553 ((struct bsdcred *) cred)->grouplen = INVALID;