2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 * Copyright (c) 1988 by Sun Microsystems, Inc.
33 * svcauth_des.c, server-side des authentication
35 * We insure for the service the following:
36 * (1) The timestamp microseconds do not exceed 1 million.
37 * (2) The timestamp plus the window is less than the current time.
38 * (3) The timestamp is not less than the one previously
39 * seen in the current session.
41 * It is up to the server to determine if the window size is
47 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <rpc/types.h>
52 #include <rpc/auth_des.h>
53 #include <rpc/svc_auth.h>
55 #include <rpc/des_crypt.h>
57 #define debug(msg) /*printf("svcauth_des: %s\n", msg) */
59 #define USEC_PER_SEC ((uint32_t) 1000000L)
60 #define BEFORE(t1, t2) timercmp(t1, t2, <)
63 * LRU cache of conversation keys and some other useful items.
65 #define AUTHDES_CACHESZ 64
68 des_block key
; /* conversation key */
69 char *rname
; /* client's name */
70 u_int window
; /* credential lifetime window */
71 struct rpc_timeval laststamp
; /* detect replays of creds */
72 char *localcred
; /* generic local credential */
74 static struct cache_entry
*authdes_cache
/* [AUTHDES_CACHESZ] */ ;
75 static int *authdes_lru
/* [AUTHDES_CACHESZ] */ ;
77 static void cache_init (void) internal_function
; /* initialize the cache */
78 static short cache_spot (des_block
*, char *, struct rpc_timeval
*)
79 internal_function
; /* find an entry in the cache */
80 static void cache_ref (uint32_t sid
) internal_function
;
81 /* note that sid was ref'd */
83 static void invalidate (char *cred
) internal_function
;
84 /* invalidate entry in cache */
91 u_long ncachehits
; /* times cache hit, and is not replay */
92 u_long ncachereplays
; /* times cache hit, and is replay */
93 u_long ncachemisses
; /* times cache missed */
98 * Service side authenticator for AUTH_DES
101 _svcauth_des (register struct svc_req
*rqst
, register struct rpc_msg
*msg
)
103 register uint32_t *ixdr
;
104 des_block cryptbuf
[2];
105 register struct authdes_cred
*cred
;
106 struct authdes_verf verf
;
108 register struct cache_entry
*entry
;
110 des_block
*sessionkey
;
113 struct rpc_timeval timestamp
;
117 struct authdes_cred area_cred
;
118 char area_netname
[MAXNETNAMELEN
+ 1];
122 if (authdes_cache
== NULL
)
124 if (authdes_cache
== NULL
) /* No free memory */
127 area
= (struct area
*) rqst
->rq_clntcred
;
128 cred
= (struct authdes_cred
*) &area
->area_cred
;
133 if (msg
->rm_call
.cb_cred
.oa_length
<= 0 ||
134 msg
->rm_call
.cb_cred
.oa_length
> MAX_AUTH_BYTES
)
137 ixdr
= (uint32_t *) msg
->rm_call
.cb_cred
.oa_base
;
138 cred
->adc_namekind
= IXDR_GET_ENUM (ixdr
, enum authdes_namekind
);
139 switch (cred
->adc_namekind
)
142 namelen
= IXDR_GET_U_INT32 (ixdr
);
143 if (namelen
> MAXNETNAMELEN
)
147 cred
->adc_fullname
.name
= area
->area_netname
;
148 bcopy ((char *) ixdr
, cred
->adc_fullname
.name
, namelen
);
149 cred
->adc_fullname
.name
[namelen
] = 0;
150 ixdr
+= (RNDUP (namelen
) / BYTES_PER_XDR_UNIT
);
151 cred
->adc_fullname
.key
.key
.high
= *ixdr
++;
152 cred
->adc_fullname
.key
.key
.low
= *ixdr
++;
153 cred
->adc_fullname
.window
= *ixdr
++;
156 cred
->adc_nickname
= *ixdr
++;
165 if (msg
->rm_call
.cb_verf
.oa_length
<= 0 ||
166 msg
->rm_call
.cb_verf
.oa_length
> MAX_AUTH_BYTES
)
169 ixdr
= (uint32_t *) msg
->rm_call
.cb_verf
.oa_base
;
170 verf
.adv_xtimestamp
.key
.high
= *ixdr
++;
171 verf
.adv_xtimestamp
.key
.low
= *ixdr
++;
172 verf
.adv_int_u
= *ixdr
++;
175 * Get the conversation key
177 if (cred
->adc_namekind
== ADN_FULLNAME
)
180 char pkey_data
[1024];
182 sessionkey
= &cred
->adc_fullname
.key
;
183 if (!getpublickey (cred
->adc_fullname
.name
, pkey_data
))
185 debug("getpublickey");
188 pkey
.n_bytes
= pkey_data
;
189 pkey
.n_len
= strlen (pkey_data
) + 1;
190 if (key_decryptsession_pk (cred
->adc_fullname
.name
, &pkey
,
193 debug ("decryptsessionkey");
194 return AUTH_BADCRED
; /* key not found */
199 if (cred
->adc_nickname
>= AUTHDES_CACHESZ
)
201 debug ("bad nickname");
202 return AUTH_BADCRED
; /* garbled credential */
205 sid
= cred
->adc_nickname
;
207 /* XXX This could be wrong, but else we have a
209 if (authdes_cache
[sid
].rname
== NULL
)
211 sessionkey
= &authdes_cache
[sid
].key
;
216 * Decrypt the timestamp
218 cryptbuf
[0] = verf
.adv_xtimestamp
;
219 if (cred
->adc_namekind
== ADN_FULLNAME
)
221 cryptbuf
[1].key
.high
= cred
->adc_fullname
.window
;
222 cryptbuf
[1].key
.low
= verf
.adv_winverf
;
223 ivec
.key
.high
= ivec
.key
.low
= 0;
224 status
= cbc_crypt ((char *) sessionkey
, (char *) cryptbuf
,
225 2 * sizeof (des_block
), DES_DECRYPT
| DES_HW
,
229 status
= ecb_crypt ((char *) sessionkey
, (char *) cryptbuf
,
230 sizeof (des_block
), DES_DECRYPT
| DES_HW
);
232 if (DES_FAILED (status
))
234 debug ("decryption failure");
235 return AUTH_FAILED
; /* system error */
239 * XDR the decrypted timestamp
241 ixdr
= (uint32_t *) cryptbuf
;
242 timestamp
.tv_sec
= IXDR_GET_INT32 (ixdr
);
243 timestamp
.tv_usec
= IXDR_GET_INT32 (ixdr
);
246 * Check for valid credentials and verifiers.
247 * They could be invalid because the key was flushed
248 * out of the cache, and so a new session should begin.
249 * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
252 struct timeval current
;
256 if (cred
->adc_namekind
== ADN_FULLNAME
)
260 window
= IXDR_GET_U_INT32 (ixdr
);
261 winverf
= IXDR_GET_U_INT32 (ixdr
);
262 if (winverf
!= window
- 1)
264 debug ("window verifier mismatch");
265 return AUTH_BADCRED
; /* garbled credential */
267 tmp_spot
= cache_spot (sessionkey
, cred
->adc_fullname
.name
,
269 if (tmp_spot
< 0 || tmp_spot
> AUTHDES_CACHESZ
)
271 debug ("replayed credential");
272 return AUTH_REJECTEDCRED
; /* replay */
279 window
= authdes_cache
[sid
].window
;
283 if (timestamp
.tv_usec
>= USEC_PER_SEC
)
285 debug ("invalid usecs");
286 /* cached out (bad key), or garbled verifier */
287 return nick
? AUTH_REJECTEDVERF
: AUTH_BADVERF
;
289 if (nick
&& BEFORE (×tamp
, &authdes_cache
[sid
].laststamp
))
291 debug ("timestamp before last seen");
292 return AUTH_REJECTEDVERF
; /* replay */
294 __gettimeofday (¤t
, (struct timezone
*) NULL
);
295 current
.tv_sec
-= window
; /* allow for expiration */
296 if (!BEFORE (¤t
, ×tamp
))
298 debug ("timestamp expired");
299 /* replay, or garbled credential */
300 return nick
? AUTH_REJECTEDVERF
: AUTH_BADCRED
;
305 * Set up the reply verifier
307 verf
.adv_nickname
= sid
;
310 * xdr the timestamp before encrypting
312 ixdr
= (int32_t *) cryptbuf
;
313 IXDR_PUT_INT32 (ixdr
, timestamp
.tv_sec
- 1);
314 IXDR_PUT_INT32 (ixdr
, timestamp
.tv_usec
);
317 * encrypt the timestamp
319 status
= ecb_crypt ((char *) sessionkey
, (char *) cryptbuf
,
320 sizeof (des_block
), DES_ENCRYPT
| DES_HW
);
321 if (DES_FAILED (status
))
323 debug ("encryption failure");
324 return AUTH_FAILED
; /* system error */
326 verf
.adv_xtimestamp
= cryptbuf
[0];
329 * Serialize the reply verifier, and update rqst
331 ixdr
= (uint32_t *) msg
->rm_call
.cb_verf
.oa_base
;
332 *ixdr
++ = verf
.adv_xtimestamp
.key
.high
;
333 *ixdr
++ = verf
.adv_xtimestamp
.key
.low
;
334 *ixdr
++ = verf
.adv_int_u
;
336 rqst
->rq_xprt
->xp_verf
.oa_flavor
= AUTH_DES
;
337 rqst
->rq_xprt
->xp_verf
.oa_base
= msg
->rm_call
.cb_verf
.oa_base
;
338 rqst
->rq_xprt
->xp_verf
.oa_length
=
339 (char *) ixdr
- msg
->rm_call
.cb_verf
.oa_base
;
342 * We succeeded, commit the data to the cache now and
343 * finish cooking the credential.
345 entry
= &authdes_cache
[sid
];
346 entry
->laststamp
= timestamp
;
348 if (cred
->adc_namekind
== ADN_FULLNAME
)
352 cred
->adc_fullname
.window
= window
;
353 cred
->adc_nickname
= sid
; /* save nickname */
354 if (entry
->rname
!= NULL
)
355 mem_free (entry
->rname
, strlen (entry
->rname
) + 1);
356 full_len
= strlen (cred
->adc_fullname
.name
) + 1;
357 entry
->rname
= mem_alloc ((u_int
) full_len
);
358 if (entry
->rname
!= NULL
)
359 memcpy (entry
->rname
, cred
->adc_fullname
.name
, full_len
);
362 debug ("out of memory");
363 return AUTH_FAILED
; /* out of memory is bad */
365 entry
->key
= *sessionkey
;
366 entry
->window
= window
;
367 invalidate (entry
->localcred
); /* mark any cached cred invalid */
372 * nicknames are cooked into fullnames
374 cred
->adc_namekind
= ADN_FULLNAME
;
375 cred
->adc_fullname
.name
= entry
->rname
;
376 cred
->adc_fullname
.key
= entry
->key
;
377 cred
->adc_fullname
.window
= entry
->window
;
379 return AUTH_OK
; /* we made it! */
384 * Initialize the cache
392 authdes_cache
= (struct cache_entry
*)
393 mem_alloc (sizeof (struct cache_entry
) * AUTHDES_CACHESZ
);
394 if (authdes_cache
== NULL
)
396 __bzero ((char *) authdes_cache
,
397 sizeof (struct cache_entry
) * AUTHDES_CACHESZ
);
399 authdes_lru
= (int *) mem_alloc (sizeof (int) * AUTHDES_CACHESZ
);
401 * Initialize the lru list
403 for (i
= 0; i
< AUTHDES_CACHESZ
; ++i
)
409 * Find the lru victim
414 return authdes_lru
[AUTHDES_CACHESZ
- 1];
418 * Note that sid was referenced
422 cache_ref (register uint32_t sid
)
428 prev
= authdes_lru
[0];
429 authdes_lru
[0] = sid
;
430 for (i
= 1; prev
!= sid
; ++i
)
432 curr
= authdes_lru
[i
];
433 authdes_lru
[i
] = prev
;
439 * Find a spot in the cache for a credential containing
440 * the items given. Return -1 if a replay is detected, otherwise
441 * return the spot in the cache.
445 cache_spot (register des_block
*key
, char *name
,
446 struct rpc_timeval
*timestamp
)
448 register struct cache_entry
*cp
;
450 register uint32_t hi
;
453 for (cp
= authdes_cache
, i
= 0; i
< AUTHDES_CACHESZ
; ++i
, ++cp
)
455 if (cp
->key
.key
.high
== hi
&&
456 cp
->key
.key
.low
== key
->key
.low
&&
458 memcmp (cp
->rname
, name
, strlen (name
) + 1) == 0)
460 if (BEFORE (timestamp
, &cp
->laststamp
))
462 ++svcauthdes_stats
.ncachereplays
;
463 return -1; /* replay */
465 ++svcauthdes_stats
.ncachehits
;
466 return i
; /* refresh */
469 ++svcauthdes_stats
.ncachemisses
;
470 return cache_victim (); /* new credential */
474 * Local credential handling stuff.
475 * NOTE: bsd unix dependent.
476 * Other operating systems should put something else here.
478 #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
479 #define INVALID -1 /* grouplen, if cache entry is invalid */
483 uid_t uid
; /* cached uid */
484 gid_t gid
; /* cached gid */
485 short grouplen
; /* length of cached groups */
486 gid_t groups
[NGROUPS
]; /* cached groups */
490 * Map a des credential into a unix cred.
491 * We cache the credential here so the application does
492 * not have to make an rpc call every time to interpret
496 authdes_getucred (const struct authdes_cred
*adc
, uid_t
* uid
, gid_t
* gid
,
497 short *grouplen
, gid_t
* groups
)
504 struct bsdcred
*cred
;
506 sid
= adc
->adc_nickname
;
507 if (sid
>= AUTHDES_CACHESZ
)
509 debug ("invalid nickname");
512 cred
= (struct bsdcred
*) authdes_cache
[sid
].localcred
;
515 cred
= (struct bsdcred
*) mem_alloc (sizeof (struct bsdcred
));
516 authdes_cache
[sid
].localcred
= (char *) cred
;
517 cred
->grouplen
= INVALID
;
519 if (cred
->grouplen
== INVALID
)
522 * not in cache: lookup
524 if (!netname2user (adc
->adc_fullname
.name
, &i_uid
, &i_gid
,
525 &i_grouplen
, groups
))
527 debug ("unknown netname");
528 cred
->grouplen
= UNKNOWN
; /* mark as lookup up, but not found */
531 debug ("missed ucred cache");
532 *uid
= cred
->uid
= i_uid
;
533 *gid
= cred
->gid
= i_gid
;
534 *grouplen
= cred
->grouplen
= i_grouplen
;
535 for (i
= i_grouplen
- 1; i
>= 0; --i
)
536 cred
->groups
[i
] = groups
[i
]; /* int to short */
539 else if (cred
->grouplen
== UNKNOWN
)
542 * Already lookup up, but no match found
552 *grouplen
= cred
->grouplen
;
553 for (i
= cred
->grouplen
- 1; i
>= 0; --i
)
554 groups
[i
] = cred
->groups
[i
]; /* short to int */
560 invalidate (char *cred
)
564 ((struct bsdcred
*) cred
)->grouplen
= INVALID
;