1 | /* |
2 | * Copyright (c) 2010, Oracle America, Inc. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following |
12 | * disclaimer in the documentation and/or other materials |
13 | * provided with the distribution. |
14 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
15 | * contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | * |
31 | * svcauth_des.c, server-side des authentication |
32 | * |
33 | * We insure for the service the following: |
34 | * (1) The timestamp microseconds do not exceed 1 million. |
35 | * (2) The timestamp plus the window is less than the current time. |
36 | * (3) The timestamp is not less than the one previously |
37 | * seen in the current session. |
38 | * |
39 | * It is up to the server to determine if the window size is |
40 | * too small . |
41 | * |
42 | */ |
43 | |
44 | #include <limits.h> |
45 | #include <string.h> |
46 | #include <stdint.h> |
47 | #include <sys/param.h> |
48 | #include <netinet/in.h> |
49 | #include <rpc/rpc.h> |
50 | #include <rpc/xdr.h> |
51 | #include <rpc/auth.h> |
52 | #include <rpc/auth_des.h> |
53 | #include <rpc/svc_auth.h> |
54 | #include <rpc/svc.h> |
55 | #include <rpc/des_crypt.h> |
56 | #include <shlib-compat.h> |
57 | |
58 | #define debug(msg) /*printf("svcauth_des: %s\n", msg) */ |
59 | |
60 | #define USEC_PER_SEC ((uint32_t) 1000000L) |
61 | #define BEFORE(t1, t2) timercmp(t1, t2, <) |
62 | |
63 | /* |
64 | * LRU cache of conversation keys and some other useful items. |
65 | */ |
66 | #define AUTHDES_CACHESZ 64 |
67 | struct cache_entry |
68 | { |
69 | des_block key; /* conversation key */ |
70 | char *rname; /* client's name */ |
71 | u_int window; /* credential lifetime window */ |
72 | struct rpc_timeval laststamp; /* detect replays of creds */ |
73 | char *localcred; /* generic local credential */ |
74 | }; |
75 | #ifdef _RPC_THREAD_SAFE_ |
76 | #define authdes_cache RPC_THREAD_VARIABLE(authdes_cache_s) |
77 | #define authdes_lru RPC_THREAD_VARIABLE(authdes_lru_s) |
78 | #else |
79 | static struct cache_entry *authdes_cache; |
80 | static int *authdes_lru; |
81 | #endif |
82 | |
83 | static void cache_init (void); /* initialize the cache */ |
84 | static short cache_spot (des_block *, char *, struct rpc_timeval *); |
85 | /* find an entry in the cache */ |
86 | static void cache_ref (uint32_t sid); /* note that sid was ref'd */ |
87 | |
88 | static void invalidate (char *cred); /* invalidate entry in cache */ |
89 | |
90 | /* Cache statistics. Accidental historic export without a matching |
91 | declaration in any header file. */ |
92 | #ifndef SHARED |
93 | static |
94 | #endif |
95 | struct |
96 | { |
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 */ |
100 | } |
101 | svcauthdes_stats __attribute__ ((nocommon)); |
102 | #ifdef SHARED |
103 | compat_symbol (libc, svcauthdes_stats, svcauthdes_stats, GLIBC_2_0); |
104 | #endif |
105 | |
106 | /* |
107 | * Service side authenticator for AUTH_DES |
108 | */ |
109 | enum auth_stat |
110 | _svcauth_des (register struct svc_req *rqst, register struct rpc_msg *msg) |
111 | { |
112 | register uint32_t *ixdr; |
113 | des_block cryptbuf[2]; |
114 | register struct authdes_cred *cred; |
115 | struct authdes_verf verf; |
116 | int status; |
117 | register struct cache_entry *entry; |
118 | uint32_t sid = 0; |
119 | des_block *sessionkey; |
120 | des_block ivec; |
121 | u_int window; |
122 | struct rpc_timeval timestamp; |
123 | uint32_t namelen; |
124 | struct area |
125 | { |
126 | struct authdes_cred area_cred; |
127 | char area_netname[MAXNETNAMELEN + 1]; |
128 | } |
129 | *area; |
130 | |
131 | if (authdes_cache == NULL) |
132 | cache_init (); |
133 | if (authdes_cache == NULL) /* No free memory */ |
134 | return AUTH_FAILED; |
135 | |
136 | area = (struct area *) rqst->rq_clntcred; |
137 | cred = (struct authdes_cred *) &area->area_cred; |
138 | |
139 | /* |
140 | * Get the credential |
141 | */ |
142 | if (msg->rm_call.cb_cred.oa_length <= 0 || |
143 | msg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) |
144 | return AUTH_BADCRED; |
145 | |
146 | ixdr = (uint32_t *) msg->rm_call.cb_cred.oa_base; |
147 | cred->adc_namekind = IXDR_GET_ENUM (ixdr, enum authdes_namekind); |
148 | switch (cred->adc_namekind) |
149 | { |
150 | case ADN_FULLNAME: |
151 | namelen = IXDR_GET_U_INT32 (ixdr); |
152 | if (namelen > MAXNETNAMELEN) |
153 | { |
154 | return AUTH_BADCRED; |
155 | } |
156 | cred->adc_fullname.name = area->area_netname; |
157 | memcpy (cred->adc_fullname.name, (char *) ixdr, namelen); |
158 | cred->adc_fullname.name[namelen] = 0; |
159 | ixdr += (RNDUP (namelen) / BYTES_PER_XDR_UNIT); |
160 | cred->adc_fullname.key.key.high = *ixdr++; |
161 | cred->adc_fullname.key.key.low = *ixdr++; |
162 | cred->adc_fullname.window = *ixdr++; |
163 | break; |
164 | case ADN_NICKNAME: |
165 | cred->adc_nickname = *ixdr++; |
166 | break; |
167 | default: |
168 | return AUTH_BADCRED; |
169 | } |
170 | |
171 | /* |
172 | * Get the verifier |
173 | */ |
174 | if (msg->rm_call.cb_verf.oa_length <= 0 || |
175 | msg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) |
176 | return AUTH_BADCRED; |
177 | |
178 | ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base; |
179 | verf.adv_xtimestamp.key.high = *ixdr++; |
180 | verf.adv_xtimestamp.key.low = *ixdr++; |
181 | verf.adv_int_u = *ixdr++; |
182 | |
183 | /* |
184 | * Get the conversation key |
185 | */ |
186 | if (cred->adc_namekind == ADN_FULLNAME) |
187 | { |
188 | netobj pkey; |
189 | char pkey_data[1024]; |
190 | |
191 | sessionkey = &cred->adc_fullname.key; |
192 | if (!getpublickey (cred->adc_fullname.name, pkey_data)) |
193 | { |
194 | debug("getpublickey" ); |
195 | return AUTH_BADCRED; |
196 | } |
197 | pkey.n_bytes = pkey_data; |
198 | pkey.n_len = strlen (pkey_data) + 1; |
199 | if (key_decryptsession_pk (cred->adc_fullname.name, &pkey, |
200 | sessionkey) < 0) |
201 | { |
202 | debug ("decryptsessionkey" ); |
203 | return AUTH_BADCRED; /* key not found */ |
204 | } |
205 | } |
206 | else |
207 | { /* ADN_NICKNAME */ |
208 | if (cred->adc_nickname >= AUTHDES_CACHESZ) |
209 | { |
210 | debug ("bad nickname" ); |
211 | return AUTH_BADCRED; /* garbled credential */ |
212 | } |
213 | else |
214 | sid = cred->adc_nickname; |
215 | |
216 | /* XXX This could be wrong, but else we have a |
217 | security problem */ |
218 | if (authdes_cache[sid].rname == NULL) |
219 | return AUTH_BADCRED; |
220 | sessionkey = &authdes_cache[sid].key; |
221 | } |
222 | |
223 | |
224 | /* |
225 | * Decrypt the timestamp |
226 | */ |
227 | cryptbuf[0] = verf.adv_xtimestamp; |
228 | if (cred->adc_namekind == ADN_FULLNAME) |
229 | { |
230 | cryptbuf[1].key.high = cred->adc_fullname.window; |
231 | cryptbuf[1].key.low = verf.adv_winverf; |
232 | ivec.key.high = ivec.key.low = 0; |
233 | status = cbc_crypt ((char *) sessionkey, (char *) cryptbuf, |
234 | 2 * sizeof (des_block), DES_DECRYPT | DES_HW, |
235 | (char *) &ivec); |
236 | } |
237 | else |
238 | status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf, |
239 | sizeof (des_block), DES_DECRYPT | DES_HW); |
240 | |
241 | if (DES_FAILED (status)) |
242 | { |
243 | debug ("decryption failure" ); |
244 | return AUTH_FAILED; /* system error */ |
245 | } |
246 | |
247 | /* |
248 | * XDR the decrypted timestamp |
249 | */ |
250 | ixdr = (uint32_t *) cryptbuf; |
251 | timestamp.tv_sec = IXDR_GET_INT32 (ixdr); |
252 | timestamp.tv_usec = IXDR_GET_INT32 (ixdr); |
253 | |
254 | /* |
255 | * Check for valid credentials and verifiers. |
256 | * They could be invalid because the key was flushed |
257 | * out of the cache, and so a new session should begin. |
258 | * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case. |
259 | */ |
260 | { |
261 | struct timeval current; |
262 | int nick; |
263 | u_int winverf; |
264 | |
265 | if (cred->adc_namekind == ADN_FULLNAME) |
266 | { |
267 | short tmp_spot; |
268 | |
269 | window = IXDR_GET_U_INT32 (ixdr); |
270 | winverf = IXDR_GET_U_INT32 (ixdr); |
271 | if (winverf != window - 1) |
272 | { |
273 | debug ("window verifier mismatch" ); |
274 | return AUTH_BADCRED; /* garbled credential */ |
275 | } |
276 | tmp_spot = cache_spot (sessionkey, cred->adc_fullname.name, |
277 | ×tamp); |
278 | if (tmp_spot < 0 || tmp_spot > AUTHDES_CACHESZ) |
279 | { |
280 | debug ("replayed credential" ); |
281 | return AUTH_REJECTEDCRED; /* replay */ |
282 | } |
283 | sid = tmp_spot; |
284 | nick = 0; |
285 | } |
286 | else |
287 | { /* ADN_NICKNAME */ |
288 | window = authdes_cache[sid].window; |
289 | nick = 1; |
290 | } |
291 | |
292 | if (timestamp.tv_usec >= USEC_PER_SEC) |
293 | { |
294 | debug ("invalid usecs" ); |
295 | /* cached out (bad key), or garbled verifier */ |
296 | return nick ? AUTH_REJECTEDVERF : AUTH_BADVERF; |
297 | } |
298 | if (nick && BEFORE (×tamp, &authdes_cache[sid].laststamp)) |
299 | { |
300 | debug ("timestamp before last seen" ); |
301 | return AUTH_REJECTEDVERF; /* replay */ |
302 | } |
303 | __gettimeofday (¤t, (struct timezone *) NULL); |
304 | current.tv_sec -= window; /* allow for expiration */ |
305 | if (!BEFORE (¤t, ×tamp)) |
306 | { |
307 | debug ("timestamp expired" ); |
308 | /* replay, or garbled credential */ |
309 | return nick ? AUTH_REJECTEDVERF : AUTH_BADCRED; |
310 | } |
311 | } |
312 | |
313 | /* |
314 | * Set up the reply verifier |
315 | */ |
316 | verf.adv_nickname = sid; |
317 | |
318 | /* |
319 | * xdr the timestamp before encrypting |
320 | */ |
321 | ixdr = (uint32_t *) cryptbuf; |
322 | IXDR_PUT_INT32 (ixdr, timestamp.tv_sec - 1); |
323 | IXDR_PUT_INT32 (ixdr, timestamp.tv_usec); |
324 | |
325 | /* |
326 | * encrypt the timestamp |
327 | */ |
328 | status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf, |
329 | sizeof (des_block), DES_ENCRYPT | DES_HW); |
330 | if (DES_FAILED (status)) |
331 | { |
332 | debug ("encryption failure" ); |
333 | return AUTH_FAILED; /* system error */ |
334 | } |
335 | verf.adv_xtimestamp = cryptbuf[0]; |
336 | |
337 | /* |
338 | * Serialize the reply verifier, and update rqst |
339 | */ |
340 | ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base; |
341 | *ixdr++ = verf.adv_xtimestamp.key.high; |
342 | *ixdr++ = verf.adv_xtimestamp.key.low; |
343 | *ixdr++ = verf.adv_int_u; |
344 | |
345 | rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES; |
346 | rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; |
347 | rqst->rq_xprt->xp_verf.oa_length = |
348 | (char *) ixdr - msg->rm_call.cb_verf.oa_base; |
349 | |
350 | /* |
351 | * We succeeded, commit the data to the cache now and |
352 | * finish cooking the credential. |
353 | */ |
354 | entry = &authdes_cache[sid]; |
355 | entry->laststamp = timestamp; |
356 | cache_ref (sid); |
357 | if (cred->adc_namekind == ADN_FULLNAME) |
358 | { |
359 | size_t full_len; |
360 | |
361 | cred->adc_fullname.window = window; |
362 | cred->adc_nickname = sid; /* save nickname */ |
363 | if (entry->rname != NULL) |
364 | mem_free (entry->rname, strlen (entry->rname) + 1); |
365 | full_len = strlen (cred->adc_fullname.name) + 1; |
366 | entry->rname = mem_alloc ((u_int) full_len); |
367 | if (entry->rname != NULL) |
368 | memcpy (entry->rname, cred->adc_fullname.name, full_len); |
369 | else |
370 | { |
371 | debug ("out of memory" ); |
372 | return AUTH_FAILED; /* out of memory is bad */ |
373 | } |
374 | entry->key = *sessionkey; |
375 | entry->window = window; |
376 | invalidate (entry->localcred); /* mark any cached cred invalid */ |
377 | } |
378 | else |
379 | { /* ADN_NICKNAME */ |
380 | /* |
381 | * nicknames are cooked into fullnames |
382 | */ |
383 | cred->adc_namekind = ADN_FULLNAME; |
384 | cred->adc_fullname.name = entry->rname; |
385 | cred->adc_fullname.key = entry->key; |
386 | cred->adc_fullname.window = entry->window; |
387 | } |
388 | return AUTH_OK; /* we made it! */ |
389 | } |
390 | |
391 | |
392 | /* |
393 | * Initialize the cache |
394 | */ |
395 | static void |
396 | cache_init (void) |
397 | { |
398 | register int i; |
399 | |
400 | authdes_cache = (struct cache_entry *) |
401 | calloc (sizeof (struct cache_entry) * AUTHDES_CACHESZ, 1); |
402 | if (authdes_cache == NULL) |
403 | return; |
404 | |
405 | authdes_lru = (int *) mem_alloc (sizeof (int) * AUTHDES_CACHESZ); |
406 | /* |
407 | * Initialize the lru list |
408 | */ |
409 | for (i = 0; i < AUTHDES_CACHESZ; ++i) |
410 | authdes_lru[i] = i; |
411 | } |
412 | |
413 | |
414 | /* |
415 | * Find the lru victim |
416 | */ |
417 | static short |
418 | cache_victim (void) |
419 | { |
420 | return authdes_lru[AUTHDES_CACHESZ - 1]; |
421 | } |
422 | |
423 | /* |
424 | * Note that sid was referenced |
425 | */ |
426 | static void |
427 | cache_ref (register uint32_t sid) |
428 | { |
429 | register int i; |
430 | register int curr; |
431 | register int prev; |
432 | |
433 | prev = authdes_lru[0]; |
434 | authdes_lru[0] = sid; |
435 | for (i = 1; prev != sid; ++i) |
436 | { |
437 | curr = authdes_lru[i]; |
438 | authdes_lru[i] = prev; |
439 | prev = curr; |
440 | } |
441 | } |
442 | |
443 | /* |
444 | * Find a spot in the cache for a credential containing |
445 | * the items given. Return -1 if a replay is detected, otherwise |
446 | * return the spot in the cache. |
447 | */ |
448 | static short |
449 | cache_spot (register des_block *key, char *name, |
450 | struct rpc_timeval *timestamp) |
451 | { |
452 | register struct cache_entry *cp; |
453 | register int i; |
454 | register uint32_t hi; |
455 | |
456 | hi = key->key.high; |
457 | for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; ++i, ++cp) |
458 | { |
459 | if (cp->key.key.high == hi && |
460 | cp->key.key.low == key->key.low && |
461 | cp->rname != NULL && |
462 | memcmp (cp->rname, name, strlen (name) + 1) == 0) |
463 | { |
464 | if (BEFORE (timestamp, &cp->laststamp)) |
465 | { |
466 | ++svcauthdes_stats.ncachereplays; |
467 | return -1; /* replay */ |
468 | } |
469 | ++svcauthdes_stats.ncachehits; |
470 | return i; /* refresh */ |
471 | } |
472 | } |
473 | ++svcauthdes_stats.ncachemisses; |
474 | return cache_victim (); /* new credential */ |
475 | } |
476 | |
477 | /* |
478 | * Local credential handling stuff. |
479 | * NOTE: bsd unix dependent. |
480 | * Other operating systems should put something else here. |
481 | */ |
482 | #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */ |
483 | #define INVALID -1 /* grouplen, if cache entry is invalid */ |
484 | |
485 | struct bsdcred |
486 | { |
487 | uid_t uid; /* cached uid */ |
488 | gid_t gid; /* cached gid */ |
489 | int grouplen; /* length of cached groups */ |
490 | int grouplen_max; /* length of allocated cached groups */ |
491 | gid_t groups[0]; /* cached groups */ |
492 | }; |
493 | |
494 | /* |
495 | * Map a des credential into a unix cred. |
496 | * We cache the credential here so the application does |
497 | * not have to make an rpc call every time to interpret |
498 | * the credential. |
499 | */ |
500 | int |
501 | authdes_getucred (const struct authdes_cred *adc, uid_t * uid, gid_t * gid, |
502 | short *grouplen, gid_t * groups) |
503 | { |
504 | unsigned sid; |
505 | register int i; |
506 | uid_t i_uid; |
507 | gid_t i_gid; |
508 | int i_grouplen; |
509 | struct bsdcred *cred; |
510 | |
511 | sid = adc->adc_nickname; |
512 | if (sid >= AUTHDES_CACHESZ) |
513 | { |
514 | debug ("invalid nickname" ); |
515 | return 0; |
516 | } |
517 | cred = (struct bsdcred *) authdes_cache[sid].localcred; |
518 | if (cred == NULL || cred->grouplen == INVALID) |
519 | { |
520 | /* |
521 | * not in cache: lookup |
522 | */ |
523 | if (!netname2user (adc->adc_fullname.name, &i_uid, &i_gid, |
524 | &i_grouplen, groups)) |
525 | { |
526 | debug ("unknown netname" ); |
527 | if (cred != NULL) |
528 | cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */ |
529 | return 0; |
530 | } |
531 | |
532 | if (cred != NULL && cred->grouplen_max < i_grouplen) |
533 | { |
534 | /* We already have an allocated data structure. But it is |
535 | too small. */ |
536 | free (cred); |
537 | authdes_cache[sid].localcred = NULL; |
538 | cred = NULL; |
539 | } |
540 | |
541 | if (cred == NULL) |
542 | { |
543 | /* We should allocate room for at least NGROUPS groups. */ |
544 | int ngroups_max = MAX (i_grouplen, NGROUPS); |
545 | |
546 | cred = (struct bsdcred *) mem_alloc (sizeof (struct bsdcred) |
547 | + ngroups_max * sizeof (gid_t)); |
548 | if (cred == NULL) |
549 | return 0; |
550 | |
551 | authdes_cache[sid].localcred = (char *) cred; |
552 | cred->grouplen = INVALID; |
553 | cred->grouplen_max = ngroups_max; |
554 | } |
555 | |
556 | debug ("missed ucred cache" ); |
557 | *uid = cred->uid = i_uid; |
558 | *gid = cred->gid = i_gid; |
559 | cred->grouplen = i_grouplen; |
560 | for (i = i_grouplen - 1; i >= 0; --i) |
561 | cred->groups[i] = groups[i]; |
562 | /* Make sure no too large values are reported. */ |
563 | *grouplen = MIN (SHRT_MAX, i_grouplen); |
564 | return 1; |
565 | } |
566 | else if (cred->grouplen == UNKNOWN) |
567 | { |
568 | /* |
569 | * Already lookup up, but no match found |
570 | */ |
571 | return 0; |
572 | } |
573 | |
574 | /* |
575 | * cached credentials |
576 | */ |
577 | *uid = cred->uid; |
578 | *gid = cred->gid; |
579 | |
580 | /* Another stupidity in the interface: *grouplen is of type short. |
581 | So we might have to cut the information passed up short. */ |
582 | int grouplen_copy = MIN (SHRT_MAX, cred->grouplen); |
583 | *grouplen = grouplen_copy; |
584 | for (i = grouplen_copy - 1; i >= 0; --i) |
585 | groups[i] = cred->groups[i]; |
586 | return 1; |
587 | } |
588 | libc_hidden_nolink_sunrpc (authdes_getucred, GLIBC_2_1) |
589 | |
590 | static void |
591 | invalidate (char *cred) |
592 | { |
593 | if (cred == NULL) |
594 | return; |
595 | ((struct bsdcred *) cred)->grouplen = INVALID; |
596 | } |
597 | |