3 * Copyright (c) 1988 by Sun Microsystems, Inc.
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/libcr/rpc/Attic/svc_auth_des.c,v 1.4 2004/10/25 19:38:25 drhodus Exp $
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.
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.
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.
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.
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.
34 * Sun Microsystems, Inc.
36 * Mountain View, California 94043
40 * svcauth_des.c, server-side des authentication
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.
48 * It is up to the server to determine if the window size is
56 #include <rpc/des_crypt.h>
57 #include <sys/param.h>
58 #include <netinet/in.h>
59 #include <rpc/types.h>
62 #include <rpc/auth_des.h>
64 #include <rpc/rpc_msg.h>
65 #include <rpc/svc_auth.h>
67 #define debug(msg) printf("svcauth_des: %s\n", msg)
69 #define USEC_PER_SEC ((u_long) 1000000L)
70 #define BEFORE(t1, t2) timercmp(t1, t2, <)
73 * LRU cache of conversation keys and some other useful items.
75 #define AUTHDES_CACHESZ 64
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 */
83 static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */;
84 static short *authdes_lru/* [AUTHDES_CACHESZ] */;
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 */
90 static void invalidate(); /* invalidate entry in cache */
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 */
102 * Service side authenticator for AUTH_DES
105 _svcauth_des(rqst, msg)
106 struct svc_req *rqst;
111 des_block cryptbuf[2];
112 struct authdes_cred *cred;
113 struct authdes_verf verf;
115 struct cache_entry *entry;
117 des_block *sessionkey;
120 struct timeval timestamp;
123 struct authdes_cred area_cred;
124 char area_netname[MAXNETNAMELEN+1];
127 if (authdes_cache == NULL) {
131 area = (struct area *)rqst->rq_clntcred;
132 cred = (struct authdes_cred *)&area->area_cred;
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 namelen = IXDR_GET_U_LONG(ixdr);
142 if (namelen > MAXNETNAMELEN) {
143 return (AUTH_BADCRED);
145 cred->adc_fullname.name = area->area_netname;
146 bcopy((char *)ixdr, cred->adc_fullname.name,
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++;
155 cred->adc_nickname = (u_long)*ixdr++;
158 return (AUTH_BADCRED);
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++;
171 * Get the conversation key
173 if (cred->adc_namekind == ADN_FULLNAME) {
175 char pkey_data[1024];
177 sessionkey = &cred->adc_fullname.key;
178 if (! getpublickey(cred->adc_fullname.name, pkey_data)) {
179 debug("getpublickey");
180 return(AUTH_BADCRED);
182 pkey.n_bytes = pkey_data;
183 pkey.n_len = strlen(pkey_data) + 1;
184 if (key_decryptsession_pk(cred->adc_fullname.name, &pkey,
186 debug("decryptsessionkey");
187 return (AUTH_BADCRED); /* key not found */
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 */
195 sessionkey = &authdes_cache[sid].key;
200 * Decrypt the timestamp
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,
211 status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
212 sizeof(des_block), DES_DECRYPT | DES_HW);
214 if (DES_FAILED(status)) {
215 debug("decryption failure");
216 return (AUTH_FAILED); /* system error */
220 * XDR the decrypted timestamp
222 ixdr = (long *)cryptbuf;
223 timestamp.tv_sec = IXDR_GET_LONG(ixdr);
224 timestamp.tv_usec = IXDR_GET_LONG(ixdr);
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.
233 struct timeval current;
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 */
244 sid = cache_spot(sessionkey, cred->adc_fullname.name,
247 debug("replayed credential");
248 return (AUTH_REJECTEDCRED); /* replay */
251 } else { /* ADN_NICKNAME */
252 window = authdes_cache[sid].window;
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);
261 if (nick && BEFORE(×tamp,
262 &authdes_cache[sid].laststamp)) {
263 debug("timestamp before last seen");
264 return (AUTH_REJECTEDVERF); /* replay */
266 (void) gettimeofday(¤t, (struct timezone *)NULL);
267 current.tv_sec -= window; /* allow for expiration */
268 if (!BEFORE(¤t, ×tamp)) {
269 debug("timestamp expired");
270 /* replay, or garbled credential */
271 return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
276 * Set up the reply verifier
278 verf.adv_nickname = (u_long)sid;
281 * xdr the timestamp before encrypting
283 ixdr = (long *)cryptbuf;
284 IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1);
285 IXDR_PUT_LONG(ixdr, timestamp.tv_usec);
288 * encrypt the timestamp
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 */
296 verf.adv_xtimestamp = cryptbuf[0];
299 * Serialize the reply verifier, and update rqst
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;
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;
312 * We succeeded, commit the data to the cache now and
313 * finish cooking the credential.
315 entry = &authdes_cache[sid];
316 entry->laststamp = timestamp;
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);
324 entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name)
326 if (entry->rname != NULL) {
327 (void) strcpy(entry->rname, cred->adc_fullname.name);
329 debug("out of memory");
331 entry->key = *sessionkey;
332 entry->window = window;
333 invalidate(entry->localcred); /* mark any cached cred invalid */
334 } else { /* ADN_NICKNAME */
336 * nicknames are cooked into fullnames
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;
343 return (AUTH_OK); /* we made it!*/
348 * Initialize the cache
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);
360 authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ);
362 * Initialize the lru list
364 for (i = 0; i < AUTHDES_CACHESZ; i++) {
371 * Find the lru victim
376 return (authdes_lru[AUTHDES_CACHESZ-1]);
380 * Note that sid was referenced
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;
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.
406 cache_spot(key, name, timestamp)
409 struct timeval *timestamp;
411 struct cache_entry *cp;
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 &&
420 bcmp(cp->rname, name, strlen(name) + 1) == 0) {
421 if (BEFORE(timestamp, &cp->laststamp)) {
422 svcauthdes_stats.ncachereplays++;
423 return (-1); /* replay */
425 svcauthdes_stats.ncachehits++;
426 return (i); /* refresh */
429 svcauthdes_stats.ncachemisses++;
430 return (cache_victim()); /* new credential */
434 #if defined(sun) || defined(vax) || \
435 defined(__FreeBSD__) || defined(__DragonFly__)
437 * Local credential handling stuff.
438 * NOTE: bsd unix dependent.
439 * Other operating systems should put something else here.
441 #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
442 #define INVALID -1 /* grouplen, if cache entry is invalid */
445 short uid; /* cached uid */
446 short gid; /* cached gid */
447 short grouplen; /* length of cached groups */
448 short groups[NGROUPS]; /* cached groups */
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
458 authdes_getucred(adc, uid, gid, grouplen, groups)
459 struct authdes_cred *adc;
470 struct bsdcred *cred;
472 sid = adc->adc_nickname;
473 if (sid >= AUTHDES_CACHESZ) {
474 debug("invalid nickname");
477 cred = (struct bsdcred *)authdes_cache[sid].localcred;
479 cred = (struct bsdcred *)mem_alloc(sizeof(struct bsdcred));
480 authdes_cache[sid].localcred = (char *)cred;
481 cred->grouplen = INVALID;
483 if (cred->grouplen == INVALID) {
485 * not in cache: lookup
487 if (!netname2user(adc->adc_fullname.name, &i_uid, &i_gid,
488 &i_grouplen, groups))
490 debug("unknown netname");
491 cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */
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 */
502 } else if (cred->grouplen == UNKNOWN) {
504 * Already lookup up, but no match found
514 *grouplen = cred->grouplen;
515 for (i = cred->grouplen - 1; i >= 0; i--) {
516 groups[i] = cred->groups[i]; /* short to int */
528 ((struct bsdcred *)cred)->grouplen = INVALID;