Debian Codegraph @

libtelnet/shishi.c

raw
1 /*
2 Copyright (C) 2003-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 /* Written by Simon Josefsson and Nicolas Pouvesle, based on
20 kerberos5.c from GNU Inetutils. */
21
22 #include <config.h>
23
24 #ifdef SHISHI
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <arpa/telnet.h>
28 # include <shishi.h>
29
30 # include <netdb.h>
31 # include <ctype.h>
32 # include <syslog.h>
33 # include <string.h>
34 # include <attribute.h>
35
36 # include "auth.h"
37 # include "misc.h"
38
39 # ifdef ENCRYPTION
40 # include "encrypt.h"
41 # endif
42
43 char *dest_realm = NULL;
44
45 Shishi_key *enckey = NULL;
46
47 static unsigned char str_data[2048] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
48 AUTHTYPE_KERBEROS_V5,
49 };
50
51 # define KRB_AUTH 0 /* Authentication data follows */
52 # define KRB_REJECT 1 /* Rejected (reason might follow) */
53 # define KRB_ACCEPT 2 /* Accepted */
54 # define KRB_RESPONSE 3 /* Response for mutual auth. */
55
56 Shishi *shishi_handle = NULL;
57 Shishi_ap *auth_handle;
58
59 # define DEBUG(c) if (auth_debug_mode) printf c
60
61 /* Callback from consumer. */
62 extern void printsub (char, unsigned char *, int);
63
64 static int
65 Data (TN_Authenticator *ap, int type, void *d, int c)
66 {
67 unsigned char *p = str_data + 4;
68 unsigned char *cd = (unsigned char *) d;
69
70 /* Submitted as test data. */
71 if (c == -1)
72 c = strlen ((char *) cd);
73
74 if (auth_debug_mode)
75 {
76 printf ("%s:%d: [%d] (%d)",
77 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
78 str_data[3], type, c);
79 printd (d, c);
80 printf ("\r\n");
81 }
82
83 *p++ = ap->type;
84 *p++ = ap->way;
85 *p++ = type;
86
87 while (c-- > 0)
88 {
89 if ((*p++ = *cd++) == IAC)
90 *p++ = IAC;
91 }
92 *p++ = IAC;
93 *p++ = SE;
94 if (str_data[3] == TELQUAL_IS)
95 printsub ('>', &str_data[2], p - &str_data[2]);
96 return (net_write (str_data, p - str_data));
97 }
98
99 Shishi *shishi_telnet = NULL;
100
101 /* FIXME: Reverse return code! */
102 int
103 krb5shishi_init (TN_Authenticator *ap MAYBE_UNUSED, int server)
104 {
105 if (server)
106 str_data[3] = TELQUAL_REPLY;
107 else
108 str_data[3] = TELQUAL_IS;
109
110 if (!shishi_check_version (SHISHI_VERSION))
111 return 0;
112
113 return 1;
114 }
115
116 static int
117 delayed_shishi_init (void)
118 {
119 if (shishi_handle)
120 return 1;
121
122 if (str_data[3] == TELQUAL_REPLY)
123 {
124 if (!shishi_handle && shishi_init_server (&shishi_handle) != SHISHI_OK)
125 return 0;
126 }
127 else
128 {
129 if (!shishi_handle && shishi_init (&shishi_handle) != SHISHI_OK)
130 return 0;
131 }
132
133 return 1;
134 }
135
136 void
137 krb5shishi_cleanup (TN_Authenticator *ap MAYBE_UNUSED)
138 {
139 if (shishi_handle == NULL)
140 return;
141
142 shishi_done (shishi_handle);
143 shishi_handle = NULL;
144 }
145
146 int
147 krb5shishi_send (TN_Authenticator *ap)
148 {
149 int ap_opts;
150 char type_check[2];
151 Shishi_tkt *tkt;
152 Shishi_tkts_hint hint;
153 int rc;
154 char *tmp;
155 char *apreq;
156 size_t apreq_len;
157
158 if (!UserNameRequested)
159 {
160 DEBUG (("telnet: Kerberos V5: no user name supplied\r\n"));
161 return 0;
162 }
163
164 if (!delayed_shishi_init ())
165 {
166 DEBUG (("telnet: Kerberos V5: shishi initialization failed\r\n"));
167 return 0;
168 }
169
170 tmp = malloc (strlen ("host/") + strlen (RemoteHostName) + 1);
171 if (tmp == NULL)
172 {
173 DEBUG (("telnet: Kerberos V5: shishi memory allocation failed\r\n"));
174 return 0;
175 }
176
177 /* Check for Kerberos prefix in principal name. */
178 if (strchr (RemoteHostName, '/'))
179 strcpy (tmp, RemoteHostName);
180 else
181 sprintf (tmp, "host/%s", RemoteHostName);
182
183 memset (&hint, 0, sizeof (hint));
184 hint.server = tmp;
185 hint.client = UserNameRequested;
186
187 if (dest_realm && *dest_realm)
188 shishi_realm_default_set (shishi_handle, dest_realm);
189 else
190 {
191 /* Retrieve realm assigned to this server as per configuration. */
192 char *p = strchr (RemoteHostName, '/');
193
194 if (p)
195 ++p;
196 else
197 p = RemoteHostName;
198
199 shishi_realm_default_set (shishi_handle,
200 shishi_realm_for_server (shishi_handle, p));
201 }
202
203 tkt = shishi_tkts_get (shishi_tkts_default (shishi_handle), &hint);
204 free (tmp);
205 if (!tkt)
206 {
207 DEBUG (("telnet: Kerberos V5: no shishi ticket for server\r\n"));
208 return 0;
209 }
210
211 if (auth_debug_mode)
212 shishi_tkt_pretty_print (tkt, stdout);
213
214 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
215 ap_opts = SHISHI_APOPTIONS_MUTUAL_REQUIRED;
216 else
217 ap_opts = 0;
218
219 type_check[0] = ap->type;
220 type_check[1] = ap->way;
221
222 # ifndef DONT_ALWAYS_USE_DES
223 /* Even if we are not using a DES key, we can still try a DES
224 session-key. Then we can support DES_?FB64 encryption with 3DES
225 or AES keys against non-RFC 2952 implementations. Of course, it
226 would be better to follow RFC 2952, but enc_des.c does not
227 support this (see comment in that file). */
228 if (shishi_tkt_keytype_fast (tkt) != SHISHI_DES_CBC_CRC &&
229 shishi_tkt_keytype_fast (tkt) != SHISHI_DES_CBC_MD4 &&
230 shishi_tkt_keytype_fast (tkt) != SHISHI_DES_CBC_MD5)
231 rc = shishi_ap_etype_tktoptionsdata (shishi_handle, &auth_handle,
232 SHISHI_DES_CBC_MD5,
233 tkt, ap_opts,
234 (char *) &type_check, 2);
235 else
236 # endif
237 rc = shishi_ap_tktoptionsdata (shishi_handle, &auth_handle,
238 tkt, ap_opts, (char *) &type_check, 2);
239 if (rc != SHISHI_OK)
240 {
241 DEBUG (("telnet: Kerberos V5: Could not make AP-REQ (%s)\r\n",
242 shishi_strerror (rc)));
243 return 0;
244 }
245
246 # ifdef ENCRYPTION
247 if (enckey)
248 {
249 shishi_key_done (enckey);
250 enckey = NULL;
251 }
252
253 rc = shishi_authenticator_get_subkey
254 (shishi_handle, shishi_ap_authenticator (auth_handle), &enckey);
255 if (rc != SHISHI_OK)
256 {
257 DEBUG (("telnet: Kerberos V5: could not get encryption key (%s)\r\n",
258 shishi_strerror (rc)));
259 return 0;
260 }
261 # endif
262
263 rc = shishi_ap_req_der (auth_handle, &apreq, &apreq_len);
264 if (rc != SHISHI_OK)
265 {
266 DEBUG (("telnet: Kerberos V5: could not DER encode (%s)\r\n",
267 shishi_strerror (rc)));
268 return 0;
269 }
270
271 if (auth_debug_mode)
272 {
273 shishi_authenticator_print (shishi_handle, stdout,
274 shishi_ap_authenticator (auth_handle));
275 shishi_apreq_print (shishi_handle, stdout, shishi_ap_req (auth_handle));
276 }
277
278 if (!auth_sendname (UserNameRequested, strlen (UserNameRequested)))
279 {
280 DEBUG (("telnet: Not enough room for user name\r\n"));
281 return 0;
282 }
283
284 if (!Data (ap, KRB_AUTH, apreq, apreq_len))
285 {
286 DEBUG (("telnet: Not enough room for authentication data\r\n"));
287 return 0;
288 }
289
290 DEBUG (("telnet: Sent Kerberos V5 credentials to server\r\n"));
291
292 return 1;
293 }
294
295 # ifdef ENCRYPTION
296 static void
297 shishi_init_key (Session_Key *skey, int type)
298 {
299 int32_t etype = shishi_key_type (enckey);
300
301 if (etype == SHISHI_DES_CBC_CRC || etype == SHISHI_DES_CBC_MD4
302 || etype == SHISHI_DES_CBC_MD5)
303 skey->type = SK_DES;
304 else
305 skey->type = SK_OTHER;
306 skey->length = shishi_key_length (enckey);
307 skey->data = (unsigned char *) shishi_key_value (enckey);
308
309 encrypt_session_key (skey, type);
310 }
311 # endif
312
313 void
314 krb5shishi_reply (TN_Authenticator *ap, unsigned char *data, int cnt)
315 {
316 static int mutual_complete = 0;
317 # ifdef ENCRYPTION
318 Session_Key skey;
319 # endif
320
321 if (cnt-- < 1)
322 return;
323
324 switch (*data++)
325 {
326 case KRB_REJECT:
327 if (cnt > 0)
328 printf ("[ Kerberos V5 rejects authentication: %.*s ]\r\n",
329 cnt, data);
330 else
331 printf ("[ Kerberos V5 refuses authentication ]\r\n");
332 auth_send_retry ();
333 return;
334
335 case KRB_ACCEPT:
336 if (!mutual_complete)
337 {
338 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
339 {
340 printf ("[ Kerberos V5 accepted you, "
341 "but didn't provide mutual authentication! ]\r\n");
342 auth_send_retry ();
343 break;
344 }
345 # ifdef ENCRYPTION
346 shishi_init_key (&skey, 0);
347 # endif
348 }
349
350 if (cnt)
351 printf ("[ Kerberos V5 accepts you as ``%.*s''%s ]\r\n", cnt, data,
352 mutual_complete ?
353 " (server authenticated)" : " (server NOT authenticated)");
354 else
355 printf ("[ Kerberos V5 accepts you ]\r\n");
356
357 auth_finished (ap, AUTH_USER);
358 /* This was last access to handle on behalf of the client. */
359 shishi_done (shishi_handle);
360 shishi_handle = NULL;
361 break;
362
363 case KRB_RESPONSE:
364 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
365 {
366 if (shishi_ap_rep_verify_der (auth_handle, (char *) data, cnt)
367 != SHISHI_OK)
368 {
369 printf ("[ Mutual authentication failed ]\r\n");
370 auth_send_retry ();
371 break;
372 }
373
374 if (auth_debug_mode)
375 {
376 shishi_aprep_print (shishi_handle, stdout,
377 shishi_ap_rep (auth_handle));
378 shishi_encapreppart_print (shishi_handle, stdout,
379 shishi_ap_encapreppart
380 (auth_handle));
381 }
382
383 # ifdef ENCRYPTION
384 shishi_init_key (&skey, 0);
385 # endif
386 mutual_complete = 1;
387 }
388 break;
389
390 default:
391 DEBUG (("Unknown Kerberos option %d\r\n", data[-1]));
392 }
393 }
394
395 int
396 krb5shishi_status (TN_Authenticator *ap MAYBE_UNUSED,
397 char *name, size_t len, int level)
398 {
399 int status;
400
401 if (level < AUTH_USER)
402 return level;
403
404 if (UserNameRequested
405 && shishi_authorized_p (shishi_handle,
406 shishi_ap_tkt (auth_handle), UserNameRequested))
407 {
408 /* FIXME: Check buffer length */
409 strncpy (name, UserNameRequested, len);
410 status = AUTH_VALID;
411 }
412 else
413 status = AUTH_USER;
414
415 return status;
416 }
417
418 static int
419 krb5shishi_is_auth (TN_Authenticator *a, unsigned char *data, int cnt,
420 char *errbuf, int errbuflen)
421 {
422 Shishi_key *key;
423 int rc;
424 char *cnamerealm, *server = NULL, *realm = NULL;
425 size_t cnamerealmlen;
426 # ifdef ENCRYPTION
427 Session_Key skey;
428 # endif
429
430 if (!delayed_shishi_init ())
431 {
432 DEBUG (("telnet: Kerberos V5: shishi initialization failed\r\n"));
433 return 0;
434 }
435
436 /* Enable use of `~/.k5login'. */
437 if (shishi_check_version ("1.0.2")) /* Faulty in version 1.0.1. */
438 {
439 rc = shishi_cfg_authorizationtype_set (shishi_handle, "k5login basic");
440 if (rc != SHISHI_OK)
441 {
442 snprintf (errbuf, errbuflen,
443 "Cannot initiate authorization types: %s",
444 shishi_error (shishi_handle));
445 return rc;
446 }
447 }
448
449 if (ServerPrincipal && *ServerPrincipal)
450 {
451 rc = shishi_parse_name (shishi_handle, ServerPrincipal,
452 &server, &realm);
453 if (rc != SHISHI_OK)
454 {
455 snprintf (errbuf, errbuflen,
456 "Cannot parse server principal name: %s",
457 shishi_strerror (rc));
458 return 1;
459 }
460 if (realm)
461 shishi_realm_default_set (shishi_handle, realm);
462
463 /* Reclaim an empty server part. */
464 if (server && !*server)
465 {
466 free (server);
467 server = NULL;
468 }
469 }
470
471 if (!server)
472 {
473 server = malloc (strlen ("host/") + strlen (LocalHostName) + 1);
474 if (server)
475 sprintf (server, "host/%s", LocalHostName);
476 }
477
478 if (server)
479 {
480 /* Two possible action on `server':
481 * "srv.local" : rewrite as "host/srv.local"
482 * "tn/srv.local" : accept as is
483 */
484 char *p = strchr (server, '/');
485
486 if (!p)
487 {
488 p = server;
489 server = malloc (strlen ("host/") + strlen (p) + 1);
490 if (!server)
491 {
492 free (p); /* This old `server'. */
493 snprintf (errbuf, errbuflen,
494 "Cannot allocate memory for server name");
495 return 1;
496 }
497 sprintf (server, "host/%s", p);
498 }
499
500 if (realm)
501 key = shishi_hostkeys_for_serverrealm (shishi_handle, server, realm);
502 else
503 /* Enforce a search with the known default realm. */
504 key = shishi_hostkeys_for_serverrealm (shishi_handle,
505 server,
506 shishi_realm_default
507 (shishi_handle));
508
509 free (server);
510 }
511 else
512 key = shishi_hostkeys_for_localservicerealm (shishi_handle,
513 "host", realm);
514
515 free (realm);
516
517 if (key == NULL)
518 {
519 snprintf (errbuf, errbuflen, "Could not find key: %s",
520 shishi_error (shishi_handle));
521 return 1;
522 }
523
524 rc = shishi_ap (shishi_handle, &auth_handle);
525 if (rc != SHISHI_OK)
526 {
527 snprintf (errbuf, errbuflen,
528 "Cannot allocate authentication structures: %s",
529 shishi_strerror (rc));
530 return 1;
531 }
532
533 rc = shishi_ap_req_der_set (auth_handle, (char *) data, cnt);
534 if (rc != SHISHI_OK)
535 {
536 snprintf (errbuf, errbuflen,
537 "Cannot parse authentication information: %s",
538 shishi_strerror (rc));
539 return 1;
540 }
541
542 rc = shishi_ap_req_process (auth_handle, key);
543 if (rc != SHISHI_OK)
544 {
545 snprintf (errbuf, errbuflen, "Could not process AP-REQ: %s",
546 shishi_strerror (rc));
547 return 1;
548 }
549
550 if (shishi_apreq_mutual_required_p (shishi_handle,
551 shishi_ap_req (auth_handle)))
552 {
553 char *der;
554 size_t derlen;
555
556 rc = shishi_ap_rep_der (auth_handle, &der, &derlen);
557 if (rc != SHISHI_OK)
558 {
559 snprintf (errbuf, errbuflen, "Error DER encoding aprep: %s",
560 shishi_strerror (rc));
561 return 1;
562 }
563
564 Data (a, KRB_RESPONSE, der, derlen);
565 free (der);
566 }
567
568 rc = shishi_encticketpart_clientrealm (shishi_handle,
569 shishi_tkt_encticketpart
570 (shishi_ap_tkt (auth_handle)),
571 &cnamerealm, &cnamerealmlen);
572 if (rc != SHISHI_OK)
573 {
574 snprintf (errbuf, errbuflen, "Error getting authenticator name: %s",
575 shishi_strerror (rc));
576 return 1;
577 }
578 Data (a, KRB_ACCEPT, cnamerealm, cnamerealm ? -1 : 0);
579 DEBUG (("telnetd: Kerberos5 identifies him as ``%s''\r\n",
580 cnamerealm ? cnamerealm : ""));
581 free (cnamerealm);
582 auth_finished (a, AUTH_USER);
583
584 /* Make sure that shishi_handle is still valid,
585 * it must not be released in auth_finish()!
586 * The server side will make reference to it
587 * later on. */
588
589 # ifdef ENCRYPTION
590 if (enckey)
591 {
592 shishi_key_done (enckey);
593 enckey = NULL;
594 }
595
596 rc = shishi_authenticator_get_subkey (shishi_handle,
597 shishi_ap_authenticator (auth_handle),
598 &enckey);
599 if (rc != SHISHI_OK)
600 {
601 Shishi_tkt *tkt;
602
603 tkt = shishi_ap_tkt (auth_handle);
604 if (tkt)
605 {
606 rc = shishi_encticketpart_get_key (shishi_handle,
607 shishi_tkt_encticketpart (tkt),
608 &enckey);
609 if (rc != SHISHI_OK)
610 enckey = NULL;
611
612 shishi_tkt_done (tkt);
613 }
614 }
615
616 if (enckey == NULL)
617 {
618 snprintf (errbuf, errbuflen,
619 "telnet: Kerberos V5: could not get encryption key (%s)\r\n",
620 shishi_strerror (rc));
621 return 1;
622 }
623
624 shishi_init_key (&skey, 1);
625 # endif
626
627 return 0;
628 }
629
630 void
631 krb5shishi_is (TN_Authenticator *ap, unsigned char *data, int cnt)
632 {
633 int r = 0;
634 char errbuf[512];
635
636 if (cnt-- < 1)
637 return;
638 errbuf[0] = 0;
639 switch (*data++)
640 {
641 case KRB_AUTH:
642 r = krb5shishi_is_auth (ap, data, cnt, errbuf, sizeof errbuf);
643 break;
644
645 default:
646 DEBUG (("Unknown Kerberos option %d\r\n", data[-1]));
647 Data (ap, KRB_REJECT, 0, 0);
648 break;
649 }
650
651 if (r)
652 {
653 if (!errbuf[0])
654 snprintf (errbuf, sizeof errbuf, "kerberos_is: error");
655 Data (ap, KRB_REJECT, errbuf, -1);
656 DEBUG (("%s\r\n", errbuf));
657 syslog (LOG_ERR, "%s", errbuf);
658 }
659 }
660
661 static char *
662 req_type_str (int type)
663 {
664 switch (type)
665 {
666 case KRB_REJECT:
667 return "REJECT";
668
669 case KRB_ACCEPT:
670 return "ACCEPT";
671
672 case KRB_AUTH:
673 return "AUTH";
674
675 case KRB_RESPONSE:
676 return "RESPONSE";
677
678 }
679 return NULL;
680 }
681
682 # define ADDC(p,l,c) if ((l) > 0) {*(p)++ = (c); --(l);}
683
684 void
685 krb5shishi_printsub (unsigned char *data, int cnt, char *buf, int buflen)
686 {
687 char *p;
688 int i;
689
690 buf[buflen - 1] = '\0'; /* make sure its NULL terminated */
691 buflen -= 1;
692
693 p = req_type_str (data[3]);
694 if (!p)
695 {
696 int l = snprintf (buf, buflen, " %d (unknown)", data[3]);
697 buf += l;
698 buflen -= l;
699 }
700 else
701 {
702 while (buflen > 0 && (*buf++ = *p++) != 0)
703 buflen--;
704 }
705
706 switch (data[3])
707 {
708 case KRB_REJECT: /* Rejected (reason might follow) */
709 case KRB_ACCEPT: /* Accepted (username might follow) */
710 if (cnt <= 4)
711 break;
712 ADDC (buf, buflen, '"');
713 for (i = 4; i < cnt; i++)
714 ADDC (buf, buflen, data[i]);
715 ADDC (buf, buflen, '"');
716 ADDC (buf, buflen, '\0');
717 break;
718
719 case KRB_AUTH:
720 case KRB_RESPONSE:
721 for (i = 4; buflen > 0 && i < cnt; i++)
722 {
723 int l = snprintf (buf, buflen, " %d", data[i]);
724 buf += l;
725 buflen -= l;
726 }
727 }
728 }
729
730 #endif /* SHISHI */