Debian Codegraph @

libtelnet/forward.c

raw
1 /*
2 Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /* General-purpose forwarding routines. These routines may be put into */
49 /* libkrb5.a to allow widespread use */
50
51 #include <config.h>
52
53 #if defined KRB5 && defined FORWARD
54 # include <stdio.h>
55 # include <pwd.h>
56 # include <netdb.h>
57
58 # ifdef HAVE_KRB5_KRB5_H
59 # include <krb5/krb5.h>
60 # endif
61 # ifdef HAVE_KRB5_ASN1_H
62 # include <krb5/asn1.h>
63 # endif
64 # ifdef HAVE_KRB5_CRC_32_H
65 # include <krb5/crc-32.h>
66 # endif
67 # ifdef HAVE_KRB5_LOS_PROTO_H
68 # include <krb5/los-proto.h>
69 # endif
70 # ifdef HAVE_KRB5_EXT_PROTO_H
71 # include <krb5/ext-proto.h>
72 # endif
73
74 # define KRB5_DEFAULT_LIFE 60*60*8 /* 8 hours */
75 /* helper function: convert flags to necessary KDC options */
76 # define flags2options(flags) (flags & KDC_TKT_COMMON_MASK)
77
78
79
80 /* Get a TGT for use at the remote host */
81 krb5_error_code
82 get_for_creds (etype, sumtype, rhost, client, enc_key, forwardable, outbuf)
83 const krb5_enctype etype;
84 const krb5_cksumtype sumtype;
85 char *rhost;
86 krb5_principal client;
87 krb5_keyblock *enc_key;
88 int forwardable; /* Should forwarded TGT also be forwardable? */
89 krb5_data *outbuf;
90 {
91 struct hostent *hp;
92 krb5_address **addrs;
93 krb5_error_code retval;
94 krb5_data *scratch;
95 krb5_kdc_rep *dec_rep;
96 krb5_error *err_reply;
97 krb5_response tgsrep;
98 krb5_creds creds, tgt;
99 krb5_ccache cc;
100 krb5_flags kdcoptions;
101 krb5_timestamp now;
102 char *remote_host;
103 char **hrealms;
104 int i;
105
106 if (!rhost || !(hp = gethostbyname (rhost)))
107 return KRB5_ERR_BAD_HOSTNAME;
108
109 remote_host = (char *) malloc (strlen (hp->h_name) + 1);
110 if (!remote_host)
111 return ENOMEM;
112 strcpy (remote_host, hp->h_name);
113
114 if (retval = krb5_get_host_realm (remote_host, &hrealms))
115 {
116 free (remote_host);
117 return retval;
118 }
119 if (!hrealms[0])
120 {
121 free (remote_host);
122 krb5_xfree (hrealms);
123 return KRB5_ERR_HOST_REALM_UNKNOWN;
124 }
125
126 /* Count elements */
127 for (i = 0; hp->h_addr_list[i]; i++);
128
129 addrs = (krb5_address **) malloc ((i + 1) * sizeof (*addrs));
130 if (!addrs)
131 return ENOMEM;
132
133 for (i = 0; hp->h_addr_list[i]; i++)
134 {
135 addrs[i] = (krb5_address *) malloc (sizeof (krb5_address));
136 if (addrs[i])
137 {
138 addrs[i]->addrtype = hp->h_addrtype;
139 addrs[i]->length = hp->h_length;
140 addrs[i]->contents = (unsigned char *) malloc (addrs[i]->length);
141 if (!addrs[i]->contents)
142 {
143 krb5_free_addresses (addrs);
144 return ENOMEM;
145 }
146 else
147 memmove ((char *) addrs[i]->contents, hp->h_addr_list[i],
148 addrs[i]->length);
149 }
150 else
151 {
152 return ENOMEM;
153 }
154 }
155 addrs[i] = 0;
156
157 memset ((char *) &creds, 0, sizeof (creds));
158 if (retval = krb5_copy_principal (client, &creds.client))
159 return retval;
160
161 if (retval = krb5_build_principal_ext (&creds.server,
162 strlen (hrealms[0]),
163 hrealms[0],
164 KRB5_TGS_NAME_SIZE,
165 KRB5_TGS_NAME,
166 client->realm.length,
167 client->realm.data, 0))
168 return retval;
169
170 creds.times.starttime = 0;
171 if (retval = krb5_timeofday (&now))
172 {
173 return retval;
174 }
175 creds.times.endtime = now + KRB5_DEFAULT_LIFE;
176 creds.times.renew_till = 0;
177
178 if (retval = krb5_cc_default (&cc))
179 {
180 return retval;
181 }
182
183 /* fetch tgt directly from cache */
184 if (retval = krb5_cc_retrieve_cred (cc,
185 KRB5_TC_MATCH_SRV_NAMEONLY,
186 &creds, &tgt))
187 {
188 return retval;
189 }
190
191 /* tgt->client must be equal to creds.client */
192 if (!krb5_principal_compare (tgt.client, creds.client))
193 return KRB5_PRINC_NOMATCH;
194
195 if (!tgt.ticket.length)
196 return (KRB5_NO_TKT_SUPPLIED);
197
198 kdcoptions = flags2options (tgt.ticket_flags) | KDC_OPT_FORWARDED;
199
200 if (!forwardable) /* Reset KDC_OPT_FORWARDABLE */
201 kdcoptions &= ~(KDC_OPT_FORWARDABLE);
202
203 if (retval = krb5_send_tgs (kdcoptions, &creds.times, etype, sumtype, creds.server, addrs, creds.authdata, 0, /* no padata */
204 0, /* no second ticket */
205 &tgt, &tgsrep))
206 return retval;
207
208 # undef cleanup
209 # define cleanup() free(tgsrep.response.data)
210
211 switch (tgsrep.message_type)
212 {
213 case KRB5_TGS_REP:
214 break;
215 case KRB5_ERROR:
216 default:
217 if (!krb5_is_krb_error (&tgsrep.response))
218 {
219 retval = KRB5KRB_AP_ERR_MSG_TYPE;
220 }
221 else
222 retval = decode_krb5_error (&tgsrep.response, &err_reply);
223 if (retval)
224 {
225 cleanup ();
226 return retval; /* neither proper reply nor error! */
227 }
228
229 retval = err_reply->error + ERROR_TABLE_BASE_krb5;
230
231 krb5_free_error (err_reply);
232 cleanup ();
233 return retval;
234 }
235 retval = krb5_decode_kdc_rep (&tgsrep.response, &tgt.keyblock, etype, /* enctype */
236 &dec_rep);
237
238 cleanup ();
239 if (retval)
240 return retval;
241 # undef cleanup
242 # define cleanup() {\
243 memset((char *)dec_rep->enc_part2->session->contents, 0,\
244 dec_rep->enc_part2->session->length);\
245 krb5_free_kdc_rep(dec_rep); }
246
247 if (dec_rep->msg_type != KRB5_TGS_REP)
248 {
249 retval = KRB5KRB_AP_ERR_MSG_TYPE;
250 cleanup ();
251 return retval;
252 }
253
254 /* now it's decrypted and ready for prime time */
255
256 if (!krb5_principal_compare (dec_rep->client, tgt.client))
257 {
258 cleanup ();
259 return KRB5_KDCREP_MODIFIED;
260 }
261
262 if (retval = mk_cred (dec_rep, etype, enc_key, 0, 0, outbuf))
263 return retval;
264
265 krb5_free_kdc_rep (dec_rep);
266
267 return retval;
268 # undef cleanup
269 }
270
271
272
273 /* Create asn.1 encoded KRB-CRED message from the kdc reply. */
274 krb5_error_code
275 mk_cred (dec_rep, etype, key, sender_addr, recv_addr, outbuf)
276 krb5_kdc_rep *dec_rep;
277 krb5_enctype etype;
278 krb5_keyblock *key;
279 krb5_address *sender_addr;
280 krb5_address *recv_addr;
281 krb5_data *outbuf;
282 {
283 krb5_error_code retval;
284 krb5_encrypt_block eblock;
285 krb5_cred ret_cred;
286 krb5_cred_enc_part cred_enc_part;
287 krb5_data *scratch;
288
289 if (!valid_etype (etype))
290 return KRB5_PROG_ETYPE_NOSUPP;
291
292 ret_cred.tickets = (krb5_ticket **) calloc (2, sizeof (*ret_cred.tickets));
293 if (!ret_cred.tickets)
294 return ENOMEM;
295 ret_cred.tickets[0] = dec_rep->ticket;
296 ret_cred.tickets[1] = 0;
297
298 ret_cred.enc_part.etype = etype;
299 ret_cred.enc_part.kvno = 0;
300
301 cred_enc_part.ticket_info = (krb5_cred_info **)
302 calloc (2, sizeof (*cred_enc_part.ticket_info));
303 if (!cred_enc_part.ticket_info)
304 {
305 krb5_free_tickets (ret_cred.tickets);
306 return ENOMEM;
307 }
308 cred_enc_part.ticket_info[0] = (krb5_cred_info *)
309 malloc (sizeof (*cred_enc_part.ticket_info[0]));
310 if (!cred_enc_part.ticket_info[0])
311 {
312 krb5_free_tickets (ret_cred.tickets);
313 krb5_free_cred_enc_part (cred_enc_part);
314 return ENOMEM;
315 }
316 cred_enc_part.nonce = 0;
317
318 if (retval = krb5_us_timeofday (&cred_enc_part.timestamp,
319 &cred_enc_part.usec))
320 return retval;
321
322 cred_enc_part.s_address = (krb5_address *) sender_addr;
323 cred_enc_part.r_address = (krb5_address *) recv_addr;
324
325 cred_enc_part.ticket_info[0]->session = dec_rep->enc_part2->session;
326 cred_enc_part.ticket_info[0]->client = dec_rep->client;
327 cred_enc_part.ticket_info[0]->server = dec_rep->enc_part2->server;
328 cred_enc_part.ticket_info[0]->flags = dec_rep->enc_part2->flags;
329 cred_enc_part.ticket_info[0]->times = dec_rep->enc_part2->times;
330 cred_enc_part.ticket_info[0]->caddrs = dec_rep->enc_part2->caddrs;
331
332 cred_enc_part.ticket_info[1] = 0;
333
334 /* start by encoding to-be-encrypted part of the message */
335
336 if (retval = encode_krb5_enc_cred_part (&cred_enc_part, &scratch))
337 return retval;
338
339 # define cleanup_scratch() { memset(scratch->data, 0, scratch->length); krb5_free_data(scratch); }
340
341 /* put together an eblock for this encryption */
342
343 krb5_use_cstype (&eblock, etype);
344 ret_cred.enc_part.ciphertext.length = krb5_encrypt_size (scratch->length,
345 eblock.crypto_entry);
346 /* add padding area, and zero it */
347 if (!(scratch->data = realloc (scratch->data,
348 ret_cred.enc_part.ciphertext.length)))
349 {
350 /* may destroy scratch->data */
351 krb5_xfree (scratch);
352 return ENOMEM;
353 }
354 memset (scratch->data + scratch->length, 0,
355 ret_cred.enc_part.ciphertext.length - scratch->length);
356 if (!(ret_cred.enc_part.ciphertext.data =
357 malloc (ret_cred.enc_part.ciphertext.length)))
358 {
359 retval = ENOMEM;
360 goto clean_scratch;
361 }
362
363 # define cleanup_encpart() {\
364 memset(ret_cred.enc_part.ciphertext.data, 0, \
365 ret_cred.enc_part.ciphertext.length); \
366 free(ret_cred.enc_part.ciphertext.data); \
367 ret_cred.enc_part.ciphertext.length = 0; \
368 ret_cred.enc_part.ciphertext.data = 0;}
369
370 /* do any necessary key pre-processing */
371 if (retval = krb5_process_key (&eblock, key))
372 {
373 goto clean_encpart;
374 }
375
376 # define cleanup_prockey() { krb5_finish_key(&eblock);}
377
378 /* call the encryption routine */
379 if (retval = krb5_encrypt ((krb5_pointer) scratch->data,
380 (krb5_pointer)
381 ret_cred.enc_part.ciphertext.data,
382 scratch->length, &eblock, 0))
383 {
384 goto clean_prockey;
385 }
386
387 /* private message is now assembled-- do some cleanup */
388 cleanup_scratch ();
389
390 if (retval = krb5_finish_key (&eblock))
391 {
392 cleanup_encpart ();
393 return retval;
394 }
395 /* encode private message */
396 if (retval = encode_krb5_cred (&ret_cred, &scratch))
397 {
398 cleanup_encpart ();
399 return retval;
400 }
401
402 cleanup_encpart ();
403
404 *outbuf = *scratch;
405 krb5_xfree (scratch);
406 return 0;
407
408 clean_prockey:
409 cleanup_prockey ();
410 clean_encpart:
411 cleanup_encpart ();
412 clean_scratch:
413 cleanup_scratch ();
414
415 return retval;
416 # undef cleanup_prockey
417 # undef cleanup_encpart
418 # undef cleanup_scratch
419 }
420
421
422
423 /* Decode, decrypt and store the forwarded creds in the local ccache. */
424 krb5_error_code
425 rd_and_store_for_creds (inbuf, ticket, lusername)
426 krb5_data *inbuf;
427 krb5_ticket *ticket;
428 char *lusername;
429 {
430 krb5_encrypt_block eblock;
431 krb5_creds creds;
432 krb5_error_code retval;
433 char ccname[35];
434 krb5_ccache ccache = NULL;
435 struct passwd *pwd;
436
437 if (retval = rd_cred (inbuf, ticket->enc_part2->session, &creds, 0, 0))
438 {
439 return (retval);
440 }
441
442 if (!(pwd = (struct passwd *) getpwnam (lusername)))
443 {
444 return -1;
445 }
446
447 sprintf (ccname, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
448
449 if (retval = krb5_cc_resolve (ccname, &ccache))
450 {
451 return (retval);
452 }
453
454 if (retval = krb5_cc_initialize (ccache, ticket->enc_part2->client))
455 {
456 return (retval);
457 }
458
459 if (retval = krb5_cc_store_cred (ccache, &creds))
460 {
461 return (retval);
462 }
463
464 if (retval = lchown (ccname + 5, pwd->pw_uid, -1))
465 {
466 return (retval);
467 }
468
469 return retval;
470 }
471
472
473
474 extern krb5_deltat krb5_clockskew;
475 # define in_clock_skew(date) (abs((date)-currenttime) < krb5_clockskew)
476
477 /* Decode the KRB-CRED message, and return creds */
478 krb5_error_code
479 rd_cred (inbuf, key, creds, sender_addr, recv_addr)
480 const krb5_data *inbuf;
481 const krb5_keyblock *key;
482 krb5_creds *creds; /* Filled in */
483 const krb5_address *sender_addr; /* optional */
484 const krb5_address *recv_addr; /* optional */
485 {
486 krb5_error_code retval;
487 krb5_encrypt_block eblock;
488 krb5_cred *credmsg;
489 krb5_cred_enc_part *credmsg_enc_part;
490 krb5_data *scratch;
491 krb5_timestamp currenttime;
492
493 if (!krb5_is_krb_cred (inbuf))
494 return KRB5KRB_AP_ERR_MSG_TYPE;
495
496 /* decode private message */
497 if (retval = decode_krb5_cred (inbuf, &credmsg))
498 {
499 return retval;
500 }
501
502 # define cleanup_credmsg() {krb5_xfree(credmsg->enc_part.ciphertext.data); krb5_xfree(credmsg);}
503
504 if (!(scratch = (krb5_data *) malloc (sizeof (*scratch))))
505 {
506 cleanup_credmsg ();
507 return ENOMEM;
508 }
509
510 # define cleanup_scratch() {memset(scratch->data, 0, scratch->length); krb5_xfree(scratch->data);}
511
512 if (retval = encode_krb5_ticket (credmsg->tickets[0], &scratch))
513 {
514 cleanup_credmsg ();
515 cleanup_scratch ();
516 return (retval);
517 }
518
519 creds->ticket = *scratch;
520 if (!(creds->ticket.data = malloc (scratch->length)))
521 {
522 krb5_xfree (creds->ticket.data);
523 return ENOMEM;
524 }
525 memmove ((char *) creds->ticket.data, (char *) scratch->data,
526 scratch->length);
527
528 cleanup_scratch ();
529
530 if (!valid_etype (credmsg->enc_part.etype))
531 {
532 cleanup_credmsg ();
533 return KRB5_PROG_ETYPE_NOSUPP;
534 }
535
536 /* put together an eblock for this decryption */
537
538 krb5_use_cstype (&eblock, credmsg->enc_part.etype);
539 scratch->length = credmsg->enc_part.ciphertext.length;
540
541 if (!(scratch->data = malloc (scratch->length)))
542 {
543 cleanup_credmsg ();
544 return ENOMEM;
545 }
546
547 /* do any necessary key pre-processing */
548 if (retval = krb5_process_key (&eblock, key))
549 {
550 cleanup_credmsg ();
551 cleanup_scratch ();
552 return retval;
553 }
554
555 # define cleanup_prockey() { krb5_finish_key(&eblock);}
556
557 /* call the decryption routine */
558 if (retval = krb5_decrypt ((krb5_pointer) credmsg->enc_part.ciphertext.data,
559 (krb5_pointer) scratch->data,
560 scratch->length, &eblock, 0))
561 {
562 cleanup_credmsg ();
563 cleanup_scratch ();
564 cleanup_prockey ();
565 return retval;
566 }
567
568 /* cred message is now decrypted -- do some cleanup */
569
570 cleanup_credmsg ();
571
572 if (retval = krb5_finish_key (&eblock))
573 {
574 cleanup_scratch ();
575 return retval;
576 }
577
578 /* now decode the decrypted stuff */
579 if (retval = decode_krb5_enc_cred_part (scratch, &credmsg_enc_part))
580 {
581 cleanup_scratch ();
582 return retval;
583 }
584 cleanup_scratch ();
585
586 # define cleanup_mesg() {krb5_xfree(credmsg_enc_part);}
587
588 if (retval = krb5_timeofday (&currenttime))
589 {
590 cleanup_mesg ();
591 return retval;
592 }
593 if (!in_clock_skew (credmsg_enc_part->timestamp))
594 {
595 cleanup_mesg ();
596 return KRB5KRB_AP_ERR_SKEW;
597 }
598
599 if (sender_addr && credmsg_enc_part->s_address &&
600 !krb5_address_compare (sender_addr, credmsg_enc_part->s_address))
601 {
602 cleanup_mesg ();
603 return KRB5KRB_AP_ERR_BADADDR;
604 }
605 if (recv_addr && credmsg_enc_part->r_address &&
606 !krb5_address_compare (recv_addr, credmsg_enc_part->r_address))
607 {
608 cleanup_mesg ();
609 return KRB5KRB_AP_ERR_BADADDR;
610 }
611
612 if (credmsg_enc_part->r_address)
613 {
614 krb5_address **our_addrs;
615
616 if (retval = krb5_os_localaddr (&our_addrs))
617 {
618 cleanup_mesg ();
619 return retval;
620 }
621 if (!krb5_address_search (credmsg_enc_part->r_address, our_addrs))
622 {
623 krb5_free_addresses (our_addrs);
624 cleanup_mesg ();
625 return KRB5KRB_AP_ERR_BADADDR;
626 }
627 krb5_free_addresses (our_addrs);
628 }
629
630 if (retval = krb5_copy_principal (credmsg_enc_part->ticket_info[0]->client,
631 &creds->client))
632 {
633 return (retval);
634 }
635
636 if (retval = krb5_copy_principal (credmsg_enc_part->ticket_info[0]->server,
637 &creds->server))
638 {
639 return (retval);
640 }
641
642 if (retval =
643 krb5_copy_keyblock_contents (credmsg_enc_part->ticket_info[0]->session,
644 &creds->keyblock))
645 {
646 return (retval);
647 }
648
649 # undef clean
650 # define clean() {\
651 memset((char *)creds->keyblock.contents, 0, creds->keyblock.length);}
652
653 creds->times = credmsg_enc_part->ticket_info[0]->times;
654 creds->is_skey = FALSE;
655 creds->ticket_flags = credmsg_enc_part->ticket_info[0]->flags;
656
657 if (retval = krb5_copy_addresses (credmsg_enc_part->ticket_info[0]->caddrs,
658 &creds->addresses))
659 {
660 clean ();
661 return (retval);
662 }
663
664 creds->second_ticket.length = 0;
665
666 creds->authdata = 0;
667
668 cleanup_mesg ();
669 return 0;
670 # undef clean
671 # undef cleanup_credmsg
672 # undef cleanup_scratch
673 # undef cleanup_prockey
674 # undef cleanup_mesg
675 }
676
677 #endif /* defined(KRB5) && defined(FORWARD) */