Debian Codegraph @

libtelnet/auth.c

raw
1 /*
2 Copyright (C) 1993-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) 1991, 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 /*
49 * Copyright (C) 1990 by the Massachusetts Institute of Technology
50 *
51 * Export of this software from the United States of America is assumed
52 * to require a specific license from the United States Government.
53 * It is the responsibility of any person or organization contemplating
54 * export to obtain such a license before exporting.
55 *
56 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
57 * distribute this software and its documentation for any purpose and
58 * without fee is hereby granted, provided that the above copyright
59 * notice appear in all copies and that both that copyright notice and
60 * this permission notice appear in supporting documentation, and that
61 * the name of M.I.T. not be used in advertising or publicity pertaining
62 * to distribution of the software without specific, written prior
63 * permission. M.I.T. makes no representations about the suitability of
64 * this software for any purpose. It is provided "as is" without express
65 * or implied warranty.
66 */
67
68 #include <config.h>
69
70 #include "attribute.h"
71
72 #if defined AUTHENTICATION
73 # include <stdio.h>
74 # include <sys/types.h>
75 # include <signal.h>
76 # define AUTH_NAMES
77 # define AUTHTYPE_NAMES /* Needed by Solaris. */
78 # include <arpa/telnet.h>
79 # include <stdlib.h>
80 # ifdef NO_STRING_H
81 # include <strings.h>
82 # else
83 # include <string.h>
84 # endif
85
86 # include <unistd.h>
87
88 # include "encrypt.h"
89 # include "auth.h"
90 # include "misc-proto.h"
91 # include "auth-proto.h"
92
93 # define typemask(x) (1<<((x)-1))
94
95 # ifdef KRB4_ENCPWD
96 extern krb4encpwd_init ();
97 extern krb4encpwd_send ();
98 extern krb4encpwd_is ();
99 extern krb4encpwd_reply ();
100 extern krb4encpwd_status ();
101 extern krb4encpwd_printsub ();
102 # endif
103
104 # ifdef RSA_ENCPWD
105 extern rsaencpwd_init ();
106 extern rsaencpwd_send ();
107 extern rsaencpwd_is ();
108 extern rsaencpwd_reply ();
109 extern rsaencpwd_status ();
110 extern rsaencpwd_printsub ();
111 # endif
112
113 int auth_debug_mode = 0;
114 static char *Name = "Noname";
115 static int Server = 0;
116 static TN_Authenticator *authenticated = NULL;
117 static int authenticating = 0;
118 static int validuser = 0;
119 static unsigned char _auth_send_data[256];
120 static unsigned char *auth_send_data;
121 static int auth_send_cnt = 0;
122
123 /*
124 * Authentication types supported. Please note that these are stored
125 * in priority order, i.e. try the first one first.
126 */
127 TN_Authenticator authenticators[] = {
128 # ifdef SPX
129 {AUTHTYPE_SPX, AUTH_WHO_CLIENT | AUTH_HOW_MUTUAL,
130 spx_init,
131 spx_send,
132 spx_is,
133 spx_reply,
134 spx_status,
135 spx_printsub},
136 {AUTHTYPE_SPX, AUTH_WHO_CLIENT | AUTH_HOW_ONE_WAY,
137 spx_init,
138 spx_send,
139 spx_is,
140 spx_reply,
141 spx_status,
142 spx_printsub},
143 # endif
144 # ifdef SHISHI
145 {AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT | AUTH_HOW_MUTUAL,
146 krb5shishi_init,
147 krb5shishi_send,
148 krb5shishi_is,
149 krb5shishi_reply,
150 krb5shishi_status,
151 krb5shishi_printsub,
152 },
153 {AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT | AUTH_HOW_ONE_WAY,
154 krb5shishi_init,
155 krb5shishi_send,
156 krb5shishi_is,
157 krb5shishi_reply,
158 krb5shishi_status,
159 krb5shishi_printsub,
160 },
161 # endif
162 # ifdef KRB5
163 # ifdef ENCRYPTION
164 {AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT | AUTH_HOW_MUTUAL,
165 kerberos5_init,
166 kerberos5_send,
167 kerberos5_is,
168 kerberos5_reply,
169 kerberos5_status,
170 kerberos5_printsub},
171 # endif
172 /* ENCRYPTION */
173 {AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT | AUTH_HOW_ONE_WAY,
174 kerberos5_init,
175 kerberos5_send,
176 kerberos5_is,
177 kerberos5_reply,
178 kerberos5_status,
179 kerberos5_printsub},
180 # endif
181 # ifdef KRB4
182 # ifdef ENCRYPTION
183 {AUTHTYPE_KERBEROS_V4, AUTH_WHO_CLIENT | AUTH_HOW_MUTUAL,
184 kerberos4_init,
185 kerberos4_send,
186 kerberos4_is,
187 kerberos4_reply,
188 kerberos4_status,
189 kerberos4_printsub},
190 # endif
191 /* ENCRYPTION */
192 {AUTHTYPE_KERBEROS_V4, AUTH_WHO_CLIENT | AUTH_HOW_ONE_WAY,
193 kerberos4_init,
194 kerberos4_send,
195 kerberos4_is,
196 kerberos4_reply,
197 kerberos4_status,
198 kerberos4_printsub},
199 # endif
200 # ifdef KRB4_ENCPWD
201 {AUTHTYPE_KRB4_ENCPWD, AUTH_WHO_CLIENT | AUTH_HOW_MUTUAL,
202 krb4encpwd_init,
203 krb4encpwd_send,
204 krb4encpwd_is,
205 krb4encpwd_reply,
206 krb4encpwd_status,
207 krb4encpwd_printsub},
208 # endif
209 # ifdef RSA_ENCPWD
210 {AUTHTYPE_RSA_ENCPWD, AUTH_WHO_CLIENT | AUTH_HOW_ONE_WAY,
211 rsaencpwd_init,
212 rsaencpwd_send,
213 rsaencpwd_is,
214 rsaencpwd_reply,
215 rsaencpwd_status,
216 rsaencpwd_printsub},
217 # endif
218 {0,},
219 };
220
221 static TN_Authenticator NoAuth = { 0 };
222
223 static int i_support = 0;
224 static int i_wont_support = 0;
225
226 TN_Authenticator *
227 findauthenticator (int type, int way)
228 {
229 TN_Authenticator *ap = authenticators;
230
231 while (ap->type && (ap->type != type || ap->way != way))
232 ++ap;
233 return (ap->type ? ap : 0);
234 }
235
236 void
237 auth_init (char *name, int server)
238 {
239 TN_Authenticator *ap = authenticators;
240
241 Server = server;
242 Name = name;
243
244 i_support = 0;
245 authenticated = NULL;
246 authenticating = 0;
247 while (ap->type)
248 {
249 if (!ap->init || (*ap->init) (ap, server))
250 {
251 i_support |= typemask (ap->type);
252 if (auth_debug_mode)
253 printf (">>>%s: I support auth type %s (%d) %s (%d)\r\n",
254 Name,
255 (AUTHTYPE_NAME_OK (ap->type) && AUTHTYPE_NAME (ap->type))
256 ? AUTHTYPE_NAME (ap->type) : "unknown",
257 ap->type,
258 (ap->way & AUTH_HOW_MASK & AUTH_HOW_MUTUAL)
259 ? "MUTUAL" : "ONEWAY", ap->way);
260 }
261 else if (auth_debug_mode)
262 printf (">>>%s: Init failed: auth type %d %d\r\n",
263 Name, ap->type, ap->way);
264 ++ap;
265 }
266 }
267
268 void
269 auth_disable_name (char *name)
270 {
271 int x;
272
273 for (x = 0; x < AUTHTYPE_CNT; ++x)
274 {
275 if (AUTHTYPE_NAME (x) && !strcasecmp (name, AUTHTYPE_NAME (x)))
276 {
277 i_wont_support |= typemask (x);
278 break;
279 }
280 }
281 }
282
283 int
284 getauthmask (char *type, int *maskp)
285 {
286 int x;
287
288 if (AUTHTYPE_NAME (0) && !strcasecmp (type, AUTHTYPE_NAME (0)))
289 {
290 *maskp = -1;
291 return (1);
292 }
293
294 for (x = 1; x < AUTHTYPE_CNT; ++x)
295 {
296 if (AUTHTYPE_NAME (x) && !strcasecmp (type, AUTHTYPE_NAME (x)))
297 {
298 *maskp = typemask (x);
299 return (1);
300 }
301 }
302 return (0);
303 }
304
305 int auth_onoff (char *, int);
306
307 int
308 auth_enable (char *type)
309 {
310 return (auth_onoff (type, 1));
311 }
312
313 int
314 auth_disable (char *type)
315 {
316 return (auth_onoff (type, 0));
317 }
318
319 int
320 auth_onoff (char *type, int on)
321 {
322 int i, mask = -1;
323 TN_Authenticator *ap;
324
325 if (!strcasecmp (type, "?") || !strcasecmp (type, "help"))
326 {
327 printf ("auth %s 'type'\n", on ? "enable" : "disable");
328 printf ("Where 'type' is one of:\n");
329 printf ("\t%s\n", AUTHTYPE_NAME (0));
330 mask = 0;
331 for (ap = authenticators; ap->type; ap++)
332 {
333 i = typemask (ap->type);
334 if ((mask & i) != 0)
335 continue;
336 mask |= i;
337 printf ("\t%s\n", AUTHTYPE_NAME (ap->type));
338 }
339 return (0);
340 }
341
342 if (!getauthmask (type, &mask))
343 {
344 printf ("%s: invalid authentication type\n", type);
345 return (0);
346 }
347 if (on)
348 i_wont_support &= ~mask;
349 else
350 i_wont_support |= mask;
351 return (1);
352 }
353
354 int
355 auth_togdebug (int on)
356 {
357 if (on < 0)
358 auth_debug_mode ^= 1;
359 else
360 auth_debug_mode = on;
361 printf ("auth debugging %s\n", auth_debug_mode ? "enabled" : "disabled");
362 return (1);
363 }
364
365 int
366 auth_status (void)
367 {
368 TN_Authenticator *ap;
369 int i, mask;
370
371 if (i_wont_support == -1)
372 printf ("Authentication disabled\n");
373 else
374 printf ("Authentication enabled\n");
375
376 mask = 0;
377 for (ap = authenticators; ap->type; ap++)
378 {
379 i = typemask (ap->type);
380 if ((mask & i) != 0)
381 continue;
382 mask |= i;
383 printf ("%s: %s\n", AUTHTYPE_NAME (ap->type),
384 (i_wont_support & typemask (ap->type)) ?
385 "disabled" : "enabled");
386 }
387 return (1);
388 }
389
390 /*
391 * This routine is called by the server to start authentication
392 * negotiation.
393 */
394 void
395 auth_request (void)
396 {
397 static unsigned char str_request[64] = { IAC, SB,
398 TELOPT_AUTHENTICATION,
399 TELQUAL_SEND,
400 };
401 TN_Authenticator *ap = authenticators;
402 unsigned char *e = str_request + 4;
403
404 if (!authenticating)
405 {
406 authenticating = 1;
407 while (ap->type)
408 {
409 if (i_support & ~i_wont_support & typemask (ap->type))
410 {
411 if (auth_debug_mode)
412 {
413 printf (">>>%s: Sending type %d %d\r\n",
414 Name, ap->type, ap->way);
415 }
416 *e++ = ap->type;
417 *e++ = ap->way;
418 }
419 ++ap;
420 }
421 *e++ = IAC;
422 *e++ = SE;
423 net_write (str_request, e - str_request);
424 }
425 }
426
427 /*
428 * This is called when an AUTH SEND is received.
429 * It should never arrive on the server side (as only the server can
430 * send an AUTH SEND).
431 * You should probably respond to it if you can...
432 *
433 * If you want to respond to the types out of order (i.e. even
434 * if he sends LOGIN KERBEROS and you support both, you respond
435 * with KERBEROS instead of LOGIN (which is against what the
436 * protocol says)) you will have to hack this code...
437 */
438 void
439 auth_send (unsigned char *data, int cnt)
440 {
441 TN_Authenticator *ap;
442 static unsigned char str_none[] = { IAC, SB, TELOPT_AUTHENTICATION,
443 TELQUAL_IS, AUTHTYPE_NULL, 0,
444 IAC, SE
445 };
446 if (Server)
447 {
448 if (auth_debug_mode)
449 {
450 printf (">>>%s: auth_send called!\r\n", Name);
451 }
452 return;
453 }
454
455 if (auth_debug_mode)
456 {
457 printf (">>>%s: auth_send got:", Name);
458 printd (data, cnt);
459 printf ("\r\n");
460 }
461
462 /*
463 * Save the data, if it is new, so that we can continue looking
464 * at it if the authorization we try doesn't work
465 */
466 if (data < _auth_send_data ||
467 data > _auth_send_data + sizeof (_auth_send_data))
468 {
469 auth_send_cnt = (cnt > (int) sizeof (_auth_send_data))
470 ? (int) sizeof (_auth_send_data) : cnt;
471 memmove ((void *) _auth_send_data, (void *) data, auth_send_cnt);
472 auth_send_data = _auth_send_data;
473 }
474 else
475 {
476 /*
477 * This is probably a no-op, but we just make sure
478 */
479 auth_send_data = data;
480 auth_send_cnt = cnt;
481 }
482 while ((auth_send_cnt -= 2) >= 0)
483 {
484 if (auth_debug_mode)
485 printf (">>>%s: He supports %s (%d) %s (%d)\r\n",
486 Name,
487 (AUTHTYPE_NAME_OK (auth_send_data[0])
488 && AUTHTYPE_NAME (auth_send_data[0]))
489 ? AUTHTYPE_NAME (auth_send_data[0]) : "unknown",
490 auth_send_data[0],
491 (auth_send_data[1] & AUTH_HOW_MASK & AUTH_HOW_MUTUAL)
492 ? "MUTUAL" : "ONEWAY", auth_send_data[1]);
493 if ((i_support & ~i_wont_support) & typemask (*auth_send_data))
494 {
495 ap = findauthenticator (auth_send_data[0], auth_send_data[1]);
496 if (ap && ap->send)
497 {
498 if (auth_debug_mode)
499 printf (">>>%s: Trying %s (%d) %s (%d)\r\n",
500 Name,
501 (AUTHTYPE_NAME_OK (auth_send_data[0])
502 && AUTHTYPE_NAME (auth_send_data[0]))
503 ? AUTHTYPE_NAME (auth_send_data[0]) : "unknown",
504 auth_send_data[0],
505 (auth_send_data[1] & AUTH_HOW_MASK & AUTH_HOW_MUTUAL)
506 ? "MUTUAL" : "ONEWAY", auth_send_data[1]);
507 if ((*ap->send) (ap))
508 {
509 /*
510 * Okay, we found one we like
511 * and did it.
512 * we can go home now.
513 */
514 if (auth_debug_mode)
515 printf (">>>%s: Using type %s (%d)\r\n",
516 Name,
517 (AUTHTYPE_NAME_OK (*auth_send_data)
518 && AUTHTYPE_NAME (*auth_send_data))
519 ? AUTHTYPE_NAME (*auth_send_data) : "unknown",
520 *auth_send_data);
521 auth_send_data += 2;
522 return;
523 }
524 }
525 /* else
526 * just continue on and look for the
527 * next one if we didn't do anything.
528 */
529 }
530 auth_send_data += 2;
531 }
532 net_write (str_none, sizeof (str_none));
533 if (auth_debug_mode)
534 printf (">>>%s: Sent failure message\r\n", Name);
535 auth_finished (0, AUTH_REJECT);
536 # ifdef KANNAN
537 /*
538 * We requested strong authentication, however no mechanisms worked.
539 * Therefore, exit on client end.
540 */
541 printf ("Unable to securely authenticate user ... exit\n");
542 exit (EXIT_SUCCESS);
543 # endif/* KANNAN */
544 }
545
546 void
547 auth_send_retry (void)
548 {
549 /*
550 * if auth_send_cnt <= 0 then auth_send will end up rejecting
551 * the authentication and informing the other side of this.
552 */
553 auth_send (auth_send_data, auth_send_cnt);
554 }
555
556 void
557 auth_is (unsigned char *data, int cnt)
558 {
559 TN_Authenticator *ap;
560
561 if (cnt < 2)
562 return;
563
564 if (data[0] == AUTHTYPE_NULL)
565 {
566 auth_finished (0, AUTH_REJECT);
567 return;
568 }
569
570 ap = findauthenticator (data[0], data[1]);
571 if (ap)
572 {
573 if (ap->is)
574 (*ap->is) (ap, data + 2, cnt - 2);
575 }
576 else if (auth_debug_mode)
577 printf (">>>%s: Invalid authentication in IS: %d\r\n", Name, *data);
578 }
579
580 void
581 auth_reply (unsigned char *data, int cnt)
582 {
583 TN_Authenticator *ap;
584
585 if (cnt < 2)
586 return;
587
588 ap = findauthenticator (data[0], data[1]);
589 if (ap)
590 {
591 if (ap->reply)
592 (*ap->reply) (ap, data + 2, cnt - 2);
593 }
594 else if (auth_debug_mode)
595 printf (">>>%s: Invalid authentication in SEND: %d\r\n", Name, *data);
596 }
597
598 void
599 auth_name (unsigned char *data, int cnt)
600 {
601 char savename[256];
602
603 if (cnt < 1)
604 {
605 if (auth_debug_mode)
606 printf (">>>%s: Empty name in NAME\r\n", Name);
607 return;
608 }
609 if (cnt + 1 > (int) sizeof (savename))
610 {
611 if (auth_debug_mode)
612 printf
613 (">>>%s: Name in NAME (len %d) overflows buffer (len %zu).\r\n",
614 Name, cnt, sizeof (savename) - 1);
615 return;
616 }
617 memmove ((void *) savename, (void *) data, cnt);
618 savename[cnt] = '\0'; /* Null terminate */
619 if (auth_debug_mode)
620 printf (">>>%s: Got NAME [%s]\r\n", Name, savename);
621 auth_encrypt_user (savename);
622 }
623
624 int
625 auth_sendname (char *name, int len)
626 {
627 static unsigned char str_request[256 + 6]
628 = { IAC, SB, TELOPT_AUTHENTICATION, TELQUAL_NAME, };
629 unsigned char *e = str_request + 4;
630 unsigned char *ee = &str_request[sizeof (str_request) - 2];
631 unsigned char *cp = (unsigned char *) name;
632
633 while (--len >= 0)
634 {
635 if ((*e++ = *cp++) == IAC)
636 *e++ = IAC;
637 if (e >= ee)
638 return (0);
639 }
640 *e++ = IAC;
641 *e++ = SE;
642 net_write (str_request, e - str_request);
643 return (1);
644 }
645
646 void
647 auth_finished (TN_Authenticator *ap, int result)
648 {
649 if (ap && ap->cleanup)
650 (*ap->cleanup) (ap);
651
652 authenticated = ap;
653 if (!authenticated)
654 authenticated = &NoAuth;
655
656 validuser = result;
657 }
658
659 static void
660 auth_intr (int sig MAYBE_UNUSED)
661 {
662 auth_finished (0, AUTH_REJECT);
663 }
664
665 int
666 auth_wait (char *name, size_t len)
667 {
668 if (auth_debug_mode)
669 printf (">>>%s: in auth_wait.\r\n", Name);
670
671 if (Server && !authenticating)
672 return (0);
673
674 signal (SIGALRM, auth_intr);
675 alarm (30);
676 while (!authenticated)
677 if (telnet_spin ())
678 break;
679 alarm (0);
680 signal (SIGALRM, SIG_DFL);
681
682 /*
683 * Now check to see if the user is valid or not
684 */
685 if (!authenticated || authenticated == &NoAuth)
686 return (AUTH_REJECT);
687
688 if (validuser == AUTH_VALID)
689 validuser = AUTH_USER;
690
691 if (authenticated->status)
692 validuser = (*authenticated->status) (authenticated, name, len,
693 validuser);
694 return (validuser);
695 }
696
697 void
698 auth_debug (int mode)
699 {
700 auth_debug_mode = mode;
701 }
702
703 static void
704 auth_gen_printsub (unsigned char *data, int cnt, char *buf, int buflen)
705 {
706 char *cp;
707 char tbuf[16];
708
709 cnt -= 3;
710 data += 3;
711 buf[buflen - 1] = '\0';
712 buf[buflen - 2] = '*';
713 buflen -= 2;
714 for (; cnt > 0; cnt--, data++)
715 {
716 sprintf (tbuf, " %d", *data);
717 for (cp = tbuf; *cp && buflen > 0; --buflen)
718 *buf++ = *cp++;
719 if (buflen <= 0)
720 return;
721 }
722 *buf = '\0';
723 }
724
725 void
726 auth_printsub (unsigned char *data, int cnt, char *buf, int buflen)
727 {
728 TN_Authenticator *ap;
729
730 ap = findauthenticator (data[1], data[2]);
731 if (ap && ap->printsub)
732 (*ap->printsub) (data, cnt, buf, buflen);
733 else
734 auth_gen_printsub (data, cnt, buf, buflen);
735 }
736 #endif