3 * Copyright (c) 1988 by Sun Microsystems, Inc.
7 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
8 * unrestricted use provided that this legend is included on all tape
9 * media and as a part of the software program in whole or part. Users
10 * may copy or modify Sun RPC without charge, but are not authorized
11 * to license or distribute it to anyone else except as part of a product or
12 * program developed by the user.
14 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
15 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
18 * Sun RPC is provided with no support and without any obligation on the
19 * part of Sun Microsystems, Inc. to assist in its use, correction,
20 * modification or enhancement.
22 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
23 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
24 * OR ANY PART THEREOF.
26 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
27 * or profits or other special, indirect and consequential damages, even if
28 * Sun has been advised of the possibility of such damages.
30 * Sun Microsystems, Inc.
32 * Mountain View, California 94043
36 * svcauth_des.c, server-side des authentication
38 * We insure for the service the following:
39 * (1) The timestamp microseconds do not exceed 1 million.
40 * (2) The timestamp plus the window is less than the current time.
41 * (3) The timestamp is not less than the one previously
42 * seen in the current session.
44 * It is up to the server to determine if the window size is
52 #include <rpc/des_crypt.h>
53 #include <sys/param.h>
54 #include <netinet/in.h>
55 #include <rpc/types.h>
58 #include <rpc/auth_des.h>
60 #include <rpc/rpc_msg.h>
61 #include <rpc/svc_auth.h>
63 #if defined(LIBC_SCCS) && !defined(lint)
64 /* from: static char sccsid[] = "@(#)svcauth_des.c 2.3 89/07/11 4.0 RPCSRC; from 1.15 88/02/08 SMI"; */
65 static const char rcsid[] = "$FreeBSD: src/lib/libc/rpc/svc_auth_des.c,v 1.3 1999/08/28 00:00:48 peter Exp $";
68 #define debug(msg) printf("svcauth_des: %s\n", msg)
70 #define USEC_PER_SEC ((u_long) 1000000L)
71 #define BEFORE(t1, t2) timercmp(t1, t2, <)
74 * LRU cache of conversation keys and some other useful items.
76 #define AUTHDES_CACHESZ 64
78 des_block key; /* conversation key */
79 char *rname; /* client's name */
80 u_int window; /* credential lifetime window */
81 struct timeval laststamp; /* detect replays of creds */
82 char *localcred; /* generic local credential */
84 static struct cache_entry *authdes_cache/* [AUTHDES_CACHESZ] */;
85 static short *authdes_lru/* [AUTHDES_CACHESZ] */;
87 static void cache_init(); /* initialize the cache */
88 static short cache_spot(); /* find an entry in the cache */
89 static void cache_ref(/*short sid*/); /* note that sid was ref'd */
91 static void invalidate(); /* invalidate entry in cache */
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 */
103 * Service side authenticator for AUTH_DES
106 _svcauth_des(rqst, msg)
107 register struct svc_req *rqst;
108 register struct rpc_msg *msg;
112 des_block cryptbuf[2];
113 register struct authdes_cred *cred;
114 struct authdes_verf verf;
116 register struct cache_entry *entry;
118 des_block *sessionkey;
121 struct timeval timestamp;
124 struct authdes_cred area_cred;
125 char area_netname[MAXNETNAMELEN+1];
128 if (authdes_cache == NULL) {
132 area = (struct area *)rqst->rq_clntcred;
133 cred = (struct authdes_cred *)&area->area_cred;
138 ixdr = (long *)msg->rm_call.cb_cred.oa_base;
139 cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind);
140 switch (cred->adc_namekind) {
142 namelen = IXDR_GET_U_LONG(ixdr);
143 if (namelen > MAXNETNAMELEN) {
144 return (AUTH_BADCRED);
146 cred->adc_fullname.name = area->area_netname;
147 bcopy((char *)ixdr, cred->adc_fullname.name,
149 cred->adc_fullname.name[namelen] = 0;
150 ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT);
151 cred->adc_fullname.key.key.high = (u_long)*ixdr++;
152 cred->adc_fullname.key.key.low = (u_long)*ixdr++;
153 cred->adc_fullname.window = (u_long)*ixdr++;
156 cred->adc_nickname = (u_long)*ixdr++;
159 return (AUTH_BADCRED);
165 ixdr = (long *)msg->rm_call.cb_verf.oa_base;
166 verf.adv_xtimestamp.key.high = (u_long)*ixdr++;
167 verf.adv_xtimestamp.key.low = (u_long)*ixdr++;
168 verf.adv_int_u = (u_long)*ixdr++;
172 * Get the conversation key
174 if (cred->adc_namekind == ADN_FULLNAME) {
176 char pkey_data[1024];
178 sessionkey = &cred->adc_fullname.key;
179 if (! getpublickey(cred->adc_fullname.name, pkey_data)) {
180 debug("getpublickey");
181 return(AUTH_BADCRED);
183 pkey.n_bytes = pkey_data;
184 pkey.n_len = strlen(pkey_data) + 1;
185 if (key_decryptsession_pk(cred->adc_fullname.name, &pkey,
187 debug("decryptsessionkey");
188 return (AUTH_BADCRED); /* key not found */
190 } else { /* ADN_NICKNAME */
191 sid = (short)cred->adc_nickname;
192 if (sid >= AUTHDES_CACHESZ) {
193 debug("bad nickname");
194 return (AUTH_BADCRED); /* garbled credential */
196 sessionkey = &authdes_cache[sid].key;
201 * Decrypt the timestamp
203 cryptbuf[0] = verf.adv_xtimestamp;
204 if (cred->adc_namekind == ADN_FULLNAME) {
205 cryptbuf[1].key.high = cred->adc_fullname.window;
206 cryptbuf[1].key.low = verf.adv_winverf;
207 ivec.key.high = ivec.key.low = 0;
208 status = cbc_crypt((char *)sessionkey, (char *)cryptbuf,
209 2*sizeof(des_block), DES_DECRYPT | DES_HW,
212 status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
213 sizeof(des_block), DES_DECRYPT | DES_HW);
215 if (DES_FAILED(status)) {
216 debug("decryption failure");
217 return (AUTH_FAILED); /* system error */
221 * XDR the decrypted timestamp
223 ixdr = (long *)cryptbuf;
224 timestamp.tv_sec = IXDR_GET_LONG(ixdr);
225 timestamp.tv_usec = IXDR_GET_LONG(ixdr);
228 * Check for valid credentials and verifiers.
229 * They could be invalid because the key was flushed
230 * out of the cache, and so a new session should begin.
231 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
234 struct timeval current;
238 if (cred->adc_namekind == ADN_FULLNAME) {
239 window = IXDR_GET_U_LONG(ixdr);
240 winverf = IXDR_GET_U_LONG(ixdr);
241 if (winverf != window - 1) {
242 debug("window verifier mismatch");
243 return (AUTH_BADCRED); /* garbled credential */
245 sid = cache_spot(sessionkey, cred->adc_fullname.name,
248 debug("replayed credential");
249 return (AUTH_REJECTEDCRED); /* replay */
252 } else { /* ADN_NICKNAME */
253 window = authdes_cache[sid].window;
257 if ((u_long)timestamp.tv_usec >= USEC_PER_SEC) {
258 debug("invalid usecs");
259 /* cached out (bad key), or garbled verifier */
260 return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF);
262 if (nick && BEFORE(×tamp,
263 &authdes_cache[sid].laststamp)) {
264 debug("timestamp before last seen");
265 return (AUTH_REJECTEDVERF); /* replay */
267 (void) gettimeofday(¤t, (struct timezone *)NULL);
268 current.tv_sec -= window; /* allow for expiration */
269 if (!BEFORE(¤t, ×tamp)) {
270 debug("timestamp expired");
271 /* replay, or garbled credential */
272 return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
277 * Set up the reply verifier
279 verf.adv_nickname = (u_long)sid;
282 * xdr the timestamp before encrypting
284 ixdr = (long *)cryptbuf;
285 IXDR_PUT_LONG(ixdr, timestamp.tv_sec - 1);
286 IXDR_PUT_LONG(ixdr, timestamp.tv_usec);
289 * encrypt the timestamp
291 status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
292 sizeof(des_block), DES_ENCRYPT | DES_HW);
293 if (DES_FAILED(status)) {
294 debug("encryption failure");
295 return (AUTH_FAILED); /* system error */
297 verf.adv_xtimestamp = cryptbuf[0];
300 * Serialize the reply verifier, and update rqst
302 ixdr = (long *)msg->rm_call.cb_verf.oa_base;
303 *ixdr++ = (long)verf.adv_xtimestamp.key.high;
304 *ixdr++ = (long)verf.adv_xtimestamp.key.low;
305 *ixdr++ = (long)verf.adv_int_u;
307 rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
308 rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
309 rqst->rq_xprt->xp_verf.oa_length =
310 (char *)ixdr - msg->rm_call.cb_verf.oa_base;
313 * We succeeded, commit the data to the cache now and
314 * finish cooking the credential.
316 entry = &authdes_cache[sid];
317 entry->laststamp = timestamp;
319 if (cred->adc_namekind == ADN_FULLNAME) {
320 cred->adc_fullname.window = window;
321 cred->adc_nickname = (u_long)sid; /* save nickname */
322 if (entry->rname != NULL) {
323 mem_free(entry->rname, strlen(entry->rname) + 1);
325 entry->rname = (char *)mem_alloc((u_int)strlen(cred->adc_fullname.name)
327 if (entry->rname != NULL) {
328 (void) strcpy(entry->rname, cred->adc_fullname.name);
330 debug("out of memory");
332 entry->key = *sessionkey;
333 entry->window = window;
334 invalidate(entry->localcred); /* mark any cached cred invalid */
335 } else { /* ADN_NICKNAME */
337 * nicknames are cooked into fullnames
339 cred->adc_namekind = ADN_FULLNAME;
340 cred->adc_fullname.name = entry->rname;
341 cred->adc_fullname.key = entry->key;
342 cred->adc_fullname.window = entry->window;
344 return (AUTH_OK); /* we made it!*/
349 * Initialize the cache
356 authdes_cache = (struct cache_entry *)
357 mem_alloc(sizeof(struct cache_entry) * AUTHDES_CACHESZ);
358 bzero((char *)authdes_cache,
359 sizeof(struct cache_entry) * AUTHDES_CACHESZ);
361 authdes_lru = (short *)mem_alloc(sizeof(short) * AUTHDES_CACHESZ);
363 * Initialize the lru list
365 for (i = 0; i < AUTHDES_CACHESZ; i++) {
372 * Find the lru victim
377 return (authdes_lru[AUTHDES_CACHESZ-1]);
381 * Note that sid was referenced
391 prev = authdes_lru[0];
392 authdes_lru[0] = sid;
393 for (i = 1; prev != sid; i++) {
394 curr = authdes_lru[i];
395 authdes_lru[i] = prev;
402 * Find a spot in the cache for a credential containing
403 * the items given. Return -1 if a replay is detected, otherwise
404 * return the spot in the cache.
407 cache_spot(key, name, timestamp)
408 register des_block *key;
410 struct timeval *timestamp;
412 register struct cache_entry *cp;
417 for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; i++, cp++) {
418 if (cp->key.key.high == hi &&
419 cp->key.key.low == key->key.low &&
421 bcmp(cp->rname, name, strlen(name) + 1) == 0) {
422 if (BEFORE(timestamp, &cp->laststamp)) {
423 svcauthdes_stats.ncachereplays++;
424 return (-1); /* replay */
426 svcauthdes_stats.ncachehits++;
427 return (i); /* refresh */
430 svcauthdes_stats.ncachemisses++;
431 return (cache_victim()); /* new credential */
435 #if (defined(sun) || defined(vax) || defined(__FreeBSD__))
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;
463 register gid_t *groups;
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;