telnetd.c revision 1.39 1 /* $NetBSD: telnetd.c,v 1.39 2003/07/14 15:55:55 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1997 and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #ifndef lint
67 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
68 The Regents of the University of California. All rights reserved.\n");
69 #if 0
70 static char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
71 #else
72 __RCSID("$NetBSD: telnetd.c,v 1.39 2003/07/14 15:55:55 itojun Exp $");
73 #endif
74 #endif /* not lint */
75
76 #include "telnetd.h"
77 #include "pathnames.h"
78
79 #include <arpa/inet.h>
80
81 #include <err.h>
82 #include <termcap.h>
83
84 #include <limits.h>
85
86 #ifdef KRB5
87 #define Authenticator k5_Authenticator
88 #include <krb5.h>
89 #undef Authenticator
90 #include <com_err.h>
91 #endif
92
93 #ifdef AUTHENTICATION
94 int auth_level = 0;
95 #endif
96
97 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
98 #include <libtelnet/misc.h>
99 #endif
100
101 #ifdef SECURELOGIN
102 int require_secure_login = 0;
103 #endif
104
105 extern int require_hwpreauth;
106 #ifdef KRB5
107 extern krb5_context telnet_context;
108 #endif
109 int registerd_host_only = 0;
110
111
112 /*
113 * I/O data buffers,
114 * pointers, and counters.
115 */
116 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
117 char ptyibuf2[BUFSIZ];
118
119
120 int hostinfo = 1; /* do we print login banner? */
121
122
123 int debug = 0;
124 int keepalive = 1;
125 char *gettyname = "default";
126 char *progname;
127
128 int main __P((int, char *[]));
129 void usage __P((void));
130 int getterminaltype __P((char *));
131 int getent __P((char *, char *));
132 void doit __P((struct sockaddr *));
133 void _gettermname __P((void));
134 int terminaltypeok __P((char *));
135 char *getstr __P((const char *, char **));
136
137 /*
138 * The string to pass to getopt(). We do it this way so
139 * that only the actual options that we support will be
140 * passed off to getopt().
141 */
142 char valid_opts[] = {
143 'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
144 '4', '6',
145 #ifdef AUTHENTICATION
146 'a', ':', 'X', ':',
147 #endif
148 #ifdef ENCRYPTION
149 'e', ':',
150 #endif
151 #ifdef DIAGNOSTICS
152 'D', ':',
153 #endif
154 #ifdef LINEMODE
155 'l',
156 #endif
157 #ifdef SECURELOGIN
158 's',
159 #endif
160 #ifdef KRB5
161 'R', ':', 'H',
162 #endif
163 '\0'
164 };
165
166 int family = AF_INET;
167
168 int
169 main(argc, argv)
170 int argc;
171 char *argv[];
172 {
173 struct sockaddr_storage from;
174 int on = 1, fromlen;
175 register int ch;
176 #if defined(IPPROTO_IP) && defined(IP_TOS)
177 int tos = -1;
178 #endif
179
180 pfrontp = pbackp = ptyobuf;
181 netip = netibuf;
182 nfrontp = nbackp = netobuf;
183 #ifdef ENCRYPTION
184 nclearto = 0;
185 #endif /* ENCRYPTION */
186
187 progname = *argv;
188
189
190 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
191 switch (ch) {
192
193 #ifdef AUTHENTICATION
194 case 'a':
195 /*
196 * Check for required authentication level
197 */
198 if (strcmp(optarg, "debug") == 0) {
199 auth_debug_mode = 1;
200 } else if (strcasecmp(optarg, "none") == 0) {
201 auth_level = 0;
202 } else if (strcasecmp(optarg, "other") == 0) {
203 auth_level = AUTH_OTHER;
204 } else if (strcasecmp(optarg, "user") == 0) {
205 auth_level = AUTH_USER;
206 } else if (strcasecmp(optarg, "valid") == 0) {
207 auth_level = AUTH_VALID;
208 } else if (strcasecmp(optarg, "off") == 0) {
209 /*
210 * This hack turns off authentication
211 */
212 auth_level = -1;
213 } else {
214 fprintf(stderr,
215 "telnetd: unknown authorization level for -a\n");
216 }
217 break;
218 #endif /* AUTHENTICATION */
219
220
221 case 'd':
222 if (strcmp(optarg, "ebug") == 0) {
223 debug++;
224 break;
225 }
226 usage();
227 /* NOTREACHED */
228 break;
229
230 #ifdef DIAGNOSTICS
231 case 'D':
232 /*
233 * Check for desired diagnostics capabilities.
234 */
235 if (!strcmp(optarg, "report")) {
236 diagnostic |= TD_REPORT|TD_OPTIONS;
237 } else if (!strcmp(optarg, "exercise")) {
238 diagnostic |= TD_EXERCISE;
239 } else if (!strcmp(optarg, "netdata")) {
240 diagnostic |= TD_NETDATA;
241 } else if (!strcmp(optarg, "ptydata")) {
242 diagnostic |= TD_PTYDATA;
243 } else if (!strcmp(optarg, "options")) {
244 diagnostic |= TD_OPTIONS;
245 } else {
246 usage();
247 /* NOT REACHED */
248 }
249 break;
250 #endif /* DIAGNOSTICS */
251
252 #ifdef ENCRYPTION
253 case 'e':
254 if (strcmp(optarg, "debug") == 0) {
255 encrypt_debug_mode = 1;
256 break;
257 }
258 usage();
259 /* NOTREACHED */
260 break;
261 #endif /* ENCRYPTION */
262
263 case 'g':
264 gettyname = optarg;
265 break;
266
267 case 'h':
268 hostinfo = 0;
269 break;
270
271 #ifdef KRB5
272 case 'H':
273 {
274 require_hwpreauth = 1;
275 break;
276 }
277 #endif /* KRB5 */
278
279
280 #ifdef LINEMODE
281 case 'l':
282 alwayslinemode = 1;
283 break;
284 #endif /* LINEMODE */
285
286 case 'k':
287 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
288 lmodetype = NO_AUTOKLUDGE;
289 #else
290 /* ignore -k option if built without kludge linemode */
291 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
292 break;
293
294 case 'n':
295 keepalive = 0;
296 break;
297
298
299 #ifdef KRB5
300 case 'R':
301 {
302 krb5_error_code retval;
303
304 if (telnet_context == 0) {
305 retval = krb5_init_context(&telnet_context);
306 if (retval) {
307 com_err("telnetd", retval,
308 "while initializing krb5");
309 exit(1);
310 }
311 }
312 krb5_set_default_realm(telnet_context, optarg);
313 break;
314 }
315 #endif /* KRB5 */
316
317 #ifdef SECURELOGIN
318 case 's':
319 /* Secure login required */
320 require_secure_login = 1;
321 break;
322 #endif /* SECURELOGIN */
323 case 'S':
324 fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
325 "-S flag not supported\n");
326 break;
327
328 case 'u':
329 fprintf(stderr, "telnetd: -u option unneeded\n");
330 break;
331
332 case 'U':
333 registerd_host_only = 1;
334 break;
335
336 #ifdef AUTHENTICATION
337 case 'X':
338 /*
339 * Check for invalid authentication types
340 */
341 auth_disable_name(optarg);
342 break;
343 #endif /* AUTHENTICATION */
344
345 case '4':
346 family = AF_INET;
347 break;
348
349 case '6':
350 family = AF_INET6;
351 break;
352
353 default:
354 fprintf(stderr, "telnetd: %c: unknown option\n", ch);
355 /* FALLTHROUGH */
356 case '?':
357 usage();
358 /* NOTREACHED */
359 }
360 }
361
362 argc -= optind;
363 argv += optind;
364
365 if (debug) {
366 int s, ns, foo, error;
367 char *service = "telnet";
368 struct addrinfo hints, *res;
369
370 if (argc > 1) {
371 usage();
372 /* NOT REACHED */
373 } else if (argc == 1)
374 service = *argv;
375
376 memset(&hints, 0, sizeof(hints));
377 hints.ai_flags = AI_PASSIVE;
378 hints.ai_family = family;
379 hints.ai_socktype = SOCK_STREAM;
380 hints.ai_protocol = 0;
381 error = getaddrinfo(NULL, service, &hints, &res);
382
383 if (error) {
384 fprintf(stderr, "tcp/%s: %s\n", service, gai_strerror(error));
385 exit(1);
386 }
387
388 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
389 if (s < 0) {
390 perror("telnetd: socket");
391 exit(1);
392 }
393 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
394 (char *)&on, sizeof(on));
395 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
396 perror("bind");
397 exit(1);
398 }
399 if (listen(s, 1) < 0) {
400 perror("listen");
401 exit(1);
402 }
403 foo = res->ai_addrlen;
404 ns = accept(s, res->ai_addr, &foo);
405 if (ns < 0) {
406 perror("accept");
407 exit(1);
408 }
409 (void) dup2(ns, 0);
410 (void) close(ns);
411 (void) close(s);
412 } else if (argc > 0) {
413 usage();
414 /* NOT REACHED */
415 }
416
417 openlog("telnetd", LOG_PID, LOG_DAEMON);
418 fromlen = sizeof (from);
419 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
420 fprintf(stderr, "%s: ", progname);
421 perror("getpeername");
422 _exit(1);
423 }
424 if (keepalive &&
425 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
426 (char *)&on, sizeof (on)) < 0) {
427 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
428 }
429
430 #if defined(IPPROTO_IP) && defined(IP_TOS)
431 if (((struct sockaddr *)&from)->sa_family == AF_INET) {
432 if (tos < 0)
433 tos = 020; /* Low Delay bit */
434 if (tos
435 && (setsockopt(0, IPPROTO_IP, IP_TOS,
436 (char *)&tos, sizeof(tos)) < 0)
437 && (errno != ENOPROTOOPT) )
438 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
439 }
440 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
441
442 net = 0;
443 doit((struct sockaddr *)&from);
444 /* NOTREACHED */
445 #ifdef __GNUC__
446 exit(0);
447 #endif
448 } /* end of main */
449
450 void
451 usage()
452 {
453 fprintf(stderr, "Usage: telnetd");
454 #ifdef AUTHENTICATION
455 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
456 #endif
457 fprintf(stderr, " [-debug]");
458 #ifdef DIAGNOSTICS
459 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
460 #endif
461 #ifdef ENCRYPTION
462 fprintf(stderr, " [-edebug]");
463 #endif
464 fprintf(stderr, " [-h]");
465 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
466 fprintf(stderr, " [-k]");
467 #endif
468 #ifdef LINEMODE
469 fprintf(stderr, " [-l]");
470 #endif
471 fprintf(stderr, " [-n]");
472 fprintf(stderr, "\n\t");
473 #ifdef SECURELOGIN
474 fprintf(stderr, " [-s]");
475 #endif
476 #ifdef AUTHENTICATION
477 fprintf(stderr, " [-X auth-type]");
478 #endif
479 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
480 fprintf(stderr, " [port]\n");
481 exit(1);
482 }
483
484 /*
485 * getterminaltype
486 *
487 * Ask the other end to send along its terminal type and speed.
488 * Output is the variable terminaltype filled in.
489 */
490 static unsigned char ttytype_sbbuf[] = {
491 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
492 };
493
494 int
495 getterminaltype(name)
496 char *name;
497 {
498 int retval = -1;
499
500 settimer(baseline);
501 #ifdef AUTHENTICATION
502 /*
503 * Handle the Authentication option before we do anything else.
504 */
505 send_do(TELOPT_AUTHENTICATION, 1);
506 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
507 ttloop();
508 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
509 retval = auth_wait(name);
510 }
511 #endif
512
513 #ifdef ENCRYPTION
514 send_will(TELOPT_ENCRYPT, 1);
515 #endif /* ENCRYPTION */
516 send_do(TELOPT_TTYPE, 1);
517 send_do(TELOPT_TSPEED, 1);
518 send_do(TELOPT_XDISPLOC, 1);
519 send_do(TELOPT_NEW_ENVIRON, 1);
520 send_do(TELOPT_OLD_ENVIRON, 1);
521 while (
522 #ifdef ENCRYPTION
523 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
524 #endif /* ENCRYPTION */
525 his_will_wont_is_changing(TELOPT_TTYPE) ||
526 his_will_wont_is_changing(TELOPT_TSPEED) ||
527 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
528 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
529 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
530 ttloop();
531 }
532 if (his_state_is_will(TELOPT_TSPEED)) {
533 static unsigned char sb[] =
534 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
535
536 output_datalen((const char *)sb, sizeof sb);
537 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
538 }
539 #ifdef ENCRYPTION
540 /*
541 * Wait for the negotiation of what type of encryption we can
542 * send with. If autoencrypt is not set, this will just return.
543 */
544 if (his_state_is_will(TELOPT_ENCRYPT)) {
545 encrypt_wait();
546 }
547 #endif /* ENCRYPTION */
548 if (his_state_is_will(TELOPT_XDISPLOC)) {
549 static unsigned char sb[] =
550 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
551
552 output_datalen((const char *)sb, sizeof sb);
553 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
554 }
555 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
556 static unsigned char sb[] =
557 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
558
559 output_datalen((const char *)sb, sizeof sb);
560 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
561 }
562 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
563 static unsigned char sb[] =
564 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
565
566 output_datalen((const char *)sb, sizeof sb);
567 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
568 }
569 if (his_state_is_will(TELOPT_TTYPE)) {
570
571 output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
572 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
573 sizeof ttytype_sbbuf - 2););
574 }
575 if (his_state_is_will(TELOPT_TSPEED)) {
576 while (sequenceIs(tspeedsubopt, baseline))
577 ttloop();
578 }
579 if (his_state_is_will(TELOPT_XDISPLOC)) {
580 while (sequenceIs(xdisplocsubopt, baseline))
581 ttloop();
582 }
583 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
584 while (sequenceIs(environsubopt, baseline))
585 ttloop();
586 }
587 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
588 while (sequenceIs(oenvironsubopt, baseline))
589 ttloop();
590 }
591 if (his_state_is_will(TELOPT_TTYPE)) {
592 char first[256], last[256];
593
594 while (sequenceIs(ttypesubopt, baseline))
595 ttloop();
596
597 /*
598 * If the other side has already disabled the option, then
599 * we have to just go with what we (might) have already gotten.
600 */
601 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
602 (void) strlcpy(first, terminaltype, sizeof(first));
603 for(;;) {
604 /*
605 * Save the unknown name, and request the next name.
606 */
607 (void) strlcpy(last, terminaltype, sizeof(last));
608 _gettermname();
609 if (terminaltypeok(terminaltype))
610 break;
611 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
612 his_state_is_wont(TELOPT_TTYPE)) {
613 /*
614 * We've hit the end. If this is the same as
615 * the first name, just go with it.
616 */
617 if (strncmp(first, terminaltype, sizeof(first)) == 0)
618 break;
619 /*
620 * Get the terminal name one more time, so that
621 * RFC1091 compliant telnets will cycle back to
622 * the start of the list.
623 */
624 _gettermname();
625 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
626 (void) strlcpy(terminaltype, first, sizeof(first));
627 }
628 break;
629 }
630 }
631 }
632 }
633 return(retval);
634 } /* end of getterminaltype */
635
636 void
637 _gettermname()
638 {
639 /*
640 * If the client turned off the option,
641 * we can't send another request, so we
642 * just return.
643 */
644 if (his_state_is_wont(TELOPT_TTYPE))
645 return;
646 settimer(baseline);
647 output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
648 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
649 sizeof ttytype_sbbuf - 2););
650 while (sequenceIs(ttypesubopt, baseline))
651 ttloop();
652 }
653
654 int
655 terminaltypeok(s)
656 char *s;
657 {
658 char buf[1024];
659
660 if (terminaltype == NULL)
661 return(1);
662
663 /*
664 * tgetent() will return 1 if the type is known, and
665 * 0 if it is not known. If it returns -1, it couldn't
666 * open the database. But if we can't open the database,
667 * it won't help to say we failed, because we won't be
668 * able to verify anything else. So, we treat -1 like 1.
669 */
670 if (tgetent(buf, s) == 0)
671 return(0);
672 return(1);
673 }
674
675 char *hostname;
676 char host_name[MAXHOSTNAMELEN + 1];
677 char remote_host_name[MAXHOSTNAMELEN + 1];
678
679 extern void telnet __P((int, int));
680
681 /*
682 * Get a pty, scan input lines.
683 */
684 void
685 doit(who)
686 struct sockaddr *who;
687 {
688 char *host;
689 int error;
690 int level;
691 int ptynum;
692 int flags;
693 char user_name[256];
694
695 /*
696 * Find an available pty to use.
697 */
698 pty = getpty(&ptynum);
699 if (pty < 0)
700 fatal(net, "All network ports in use");
701
702 flags = registerd_host_only ? NI_NAMEREQD : 0;
703
704 /* get name of connected client */
705 error = getnameinfo(who, who->sa_len, remote_host_name,
706 sizeof(remote_host_name), NULL, 0, flags);
707
708 if (error) {
709 fatal(net, "Couldn't resolve your address into a host name.\r\n\
710 Please contact your net administrator");
711 #ifdef __GNUC__
712 host = NULL; /* XXX gcc */
713 #endif
714 }
715
716 remote_host_name[sizeof(remote_host_name)-1] = 0;
717 host = remote_host_name;
718
719 (void)gethostname(host_name, sizeof(host_name));
720 host_name[sizeof(host_name) - 1] = '\0';
721 hostname = host_name;
722
723 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
724 auth_encrypt_init(hostname, host, "TELNETD", 1);
725 #endif
726
727 init_env();
728 /*
729 * get terminal type.
730 */
731 *user_name = 0;
732 level = getterminaltype(user_name);
733 setenv("TERM", terminaltype ? terminaltype : "network", 1);
734
735 /*
736 * Start up the login process on the slave side of the terminal
737 */
738 startslave(host, level, user_name);
739
740 telnet(net, pty); /* begin server processing */
741 /*NOTREACHED*/
742 } /* end of doit */
743
744
745 /*
746 * Main loop. Select from pty and network, and
747 * hand data to telnet receiver finite state machine.
748 */
749 void
750 telnet(f, p)
751 int f, p;
752 {
753 int on = 1;
754 #define TABBUFSIZ 512
755 char defent[TABBUFSIZ];
756 char defstrs[TABBUFSIZ];
757 #undef TABBUFSIZ
758 char *HE, *HN, *IM, *IF, *ptyibuf2ptr;
759 struct pollfd set[2];
760
761 /*
762 * Initialize the slc mapping table.
763 */
764 get_slc_defaults();
765
766 /*
767 * Do some tests where it is desireable to wait for a response.
768 * Rather than doing them slowly, one at a time, do them all
769 * at once.
770 */
771 if (my_state_is_wont(TELOPT_SGA))
772 send_will(TELOPT_SGA, 1);
773 /*
774 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
775 * because 4.2 clients are unable to deal with TCP urgent data.
776 *
777 * To find out, we send out a "DO ECHO". If the remote system
778 * answers "WILL ECHO" it is probably a 4.2 client, and we note
779 * that fact ("WILL ECHO" ==> that the client will echo what
780 * WE, the server, sends it; it does NOT mean that the client will
781 * echo the terminal input).
782 */
783 send_do(TELOPT_ECHO, 1);
784
785 #ifdef LINEMODE
786 if (his_state_is_wont(TELOPT_LINEMODE)) {
787 /* Query the peer for linemode support by trying to negotiate
788 * the linemode option.
789 */
790 linemode = 0;
791 editmode = 0;
792 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
793 }
794 #endif /* LINEMODE */
795
796 /*
797 * Send along a couple of other options that we wish to negotiate.
798 */
799 send_do(TELOPT_NAWS, 1);
800 send_will(TELOPT_STATUS, 1);
801 flowmode = 1; /* default flow control state */
802 restartany = -1; /* uninitialized... */
803 send_do(TELOPT_LFLOW, 1);
804
805 /*
806 * Spin, waiting for a response from the DO ECHO. However,
807 * some REALLY DUMB telnets out there might not respond
808 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
809 * telnets so far seem to respond with WONT for a DO that
810 * they don't understand...) because by the time we get the
811 * response, it will already have processed the DO ECHO.
812 * Kludge upon kludge.
813 */
814 while (his_will_wont_is_changing(TELOPT_NAWS))
815 ttloop();
816
817 /*
818 * But...
819 * The client might have sent a WILL NAWS as part of its
820 * startup code; if so, we'll be here before we get the
821 * response to the DO ECHO. We'll make the assumption
822 * that any implementation that understands about NAWS
823 * is a modern enough implementation that it will respond
824 * to our DO ECHO request; hence we'll do another spin
825 * waiting for the ECHO option to settle down, which is
826 * what we wanted to do in the first place...
827 */
828 if (his_want_state_is_will(TELOPT_ECHO) &&
829 his_state_is_will(TELOPT_NAWS)) {
830 while (his_will_wont_is_changing(TELOPT_ECHO))
831 ttloop();
832 }
833 /*
834 * On the off chance that the telnet client is broken and does not
835 * respond to the DO ECHO we sent, (after all, we did send the
836 * DO NAWS negotiation after the DO ECHO, and we won't get here
837 * until a response to the DO NAWS comes back) simulate the
838 * receipt of a will echo. This will also send a WONT ECHO
839 * to the client, since we assume that the client failed to
840 * respond because it believes that it is already in DO ECHO
841 * mode, which we do not want.
842 */
843 if (his_want_state_is_will(TELOPT_ECHO)) {
844 DIAG(TD_OPTIONS,
845 {output_data("td: simulating recv\r\n");});
846 willoption(TELOPT_ECHO);
847 }
848
849 /*
850 * Finally, to clean things up, we turn on our echo. This
851 * will break stupid 4.2 telnets out of local terminal echo.
852 */
853
854 if (my_state_is_wont(TELOPT_ECHO))
855 send_will(TELOPT_ECHO, 1);
856
857 /*
858 * Turn on packet mode
859 */
860 (void) ioctl(p, TIOCPKT, (char *)&on);
861
862 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
863 /*
864 * Continuing line mode support. If client does not support
865 * real linemode, attempt to negotiate kludge linemode by sending
866 * the do timing mark sequence.
867 */
868 if (lmodetype < REAL_LINEMODE)
869 send_do(TELOPT_TM, 1);
870 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
871
872 /*
873 * Call telrcv() once to pick up anything received during
874 * terminal type negotiation, 4.2/4.3 determination, and
875 * linemode negotiation.
876 */
877 telrcv();
878
879 (void) ioctl(f, FIONBIO, (char *)&on);
880 (void) ioctl(p, FIONBIO, (char *)&on);
881
882 (void) setsockopt(f, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on);
883
884 (void) signal(SIGTSTP, SIG_IGN);
885 /*
886 * Ignoring SIGTTOU keeps the kernel from blocking us
887 * in ttioct() in /sys/tty.c.
888 */
889 (void) signal(SIGTTOU, SIG_IGN);
890
891 (void) signal(SIGCHLD, cleanup);
892
893
894 {
895 register int t;
896 t = open(_PATH_TTY, O_RDWR);
897 if (t >= 0) {
898 (void) ioctl(t, TIOCNOTTY, (char *)0);
899 (void) close(t);
900 }
901 }
902
903
904 /*
905 * Show banner that getty never gave.
906 *
907 * We put the banner in the pty input buffer. This way, it
908 * gets carriage return null processing, etc., just like all
909 * other pty --> client data.
910 */
911
912 if (getenv("USER"))
913 hostinfo = 0;
914
915 if (getent(defent, gettyname) == 1) {
916 char *cp=defstrs;
917
918 HE = getstr("he", &cp);
919 HN = getstr("hn", &cp);
920 IM = getstr("im", &cp);
921 IF = getstr("if", &cp);
922 if (HN && *HN)
923 (void)strlcpy(host_name, HN, sizeof(host_name));
924 if (IM == 0)
925 IM = "";
926 } else {
927 IM = DEFAULT_IM;
928 HE = 0;
929 }
930 edithost(HE, host_name);
931 ptyibuf2ptr = ptyibuf2;
932 if (hostinfo) {
933 if (IF) {
934 char buf[_POSIX2_LINE_MAX];
935 FILE *fd;
936
937 if ((fd = fopen(IF, "r")) != NULL) {
938 while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
939 ptyibuf2ptr = putf(buf, ptyibuf2ptr);
940 fclose(fd);
941 }
942 }
943 if (*IM)
944 ptyibuf2ptr = putf(IM, ptyibuf2ptr);
945 }
946
947 if (pcc)
948 strncpy(ptyibuf2ptr, ptyip, pcc+1);
949 ptyip = ptyibuf2;
950 pcc = strlen(ptyip);
951 #ifdef LINEMODE
952 /*
953 * Last check to make sure all our states are correct.
954 */
955 init_termbuf();
956 localstat();
957 #endif /* LINEMODE */
958
959 DIAG(TD_REPORT,
960 {output_data("td: Entering processing loop\r\n");});
961
962
963 set[0].fd = f;
964 set[1].fd = p;
965 for (;;) {
966 register int c;
967
968 if (ncc < 0 && pcc < 0)
969 break;
970
971 /*
972 * Never look for input if there's still
973 * stuff in the corresponding output buffer
974 */
975 set[0].events = 0;
976 set[1].events = 0;
977 if (nfrontp - nbackp || pcc > 0)
978 set[0].events |= POLLOUT;
979 else
980 set[1].events |= POLLIN;
981 if (pfrontp - pbackp || ncc > 0)
982 set[1].events |= POLLOUT;
983 else
984 set[0].events |= POLLIN;
985 if (!SYNCHing)
986 set[0].events |= POLLPRI;
987
988 if ((c = poll(set, 2, INFTIM)) < 1) {
989 if (c == -1) {
990 if (errno == EINTR) {
991 continue;
992 }
993 }
994 sleep(5);
995 continue;
996 }
997
998 /*
999 * Any urgent data?
1000 */
1001 if (set[0].revents & POLLPRI) {
1002 SYNCHing = 1;
1003 }
1004
1005 /*
1006 * Something to read from the network...
1007 */
1008 if (set[0].revents && POLLIN) {
1009 ncc = read(f, netibuf, sizeof (netibuf));
1010 if (ncc < 0 && errno == EWOULDBLOCK)
1011 ncc = 0;
1012 else {
1013 if (ncc <= 0) {
1014 break;
1015 }
1016 netip = netibuf;
1017 }
1018 DIAG((TD_REPORT | TD_NETDATA),
1019 {output_data("td: netread %d chars\r\n", ncc);});
1020 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1021 }
1022
1023 /*
1024 * Something to read from the pty...
1025 */
1026 if (set[1].revents & POLLIN) {
1027 pcc = read(p, ptyibuf, BUFSIZ);
1028 /*
1029 * On some systems, if we try to read something
1030 * off the master side before the slave side is
1031 * opened, we get EIO.
1032 */
1033 if (pcc < 0 && (errno == EWOULDBLOCK ||
1034 errno == EAGAIN ||
1035 errno == EIO)) {
1036 pcc = 0;
1037 } else {
1038 if (pcc <= 0)
1039 break;
1040 #ifdef LINEMODE
1041 /*
1042 * If ioctl from pty, pass it through net
1043 */
1044 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1045 copy_termbuf(ptyibuf+1, pcc-1);
1046 localstat();
1047 pcc = 1;
1048 }
1049 #endif /* LINEMODE */
1050 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1051 netclear(); /* clear buffer back */
1052 /*
1053 * There are client telnets on some
1054 * operating systems get screwed up
1055 * royally if we send them urgent
1056 * mode data.
1057 */
1058 output_data("%c%c", IAC, DM);
1059 neturg = nfrontp - 1; /* off by one XXX */
1060 DIAG(TD_OPTIONS,
1061 printoption("td: send IAC", DM));
1062 }
1063 if (his_state_is_will(TELOPT_LFLOW) &&
1064 (ptyibuf[0] &
1065 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1066 int newflow =
1067 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1068 if (newflow != flowmode) {
1069 flowmode = newflow;
1070 (void) output_data(
1071 "%c%c%c%c%c%c",
1072 IAC, SB, TELOPT_LFLOW,
1073 flowmode ? LFLOW_ON
1074 : LFLOW_OFF,
1075 IAC, SE);
1076 DIAG(TD_OPTIONS, printsub('>',
1077 (unsigned char *)nfrontp - 4,
1078 4););
1079 }
1080 }
1081 pcc--;
1082 ptyip = ptyibuf+1;
1083 }
1084 }
1085
1086 while (pcc > 0) {
1087 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1088 break;
1089 c = *ptyip++ & 0377, pcc--;
1090 if (c == IAC)
1091 output_data("%c", c);
1092 output_data("%c", c);
1093 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1094 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1095 output_data("%c", *ptyip++ & 0377);
1096 pcc--;
1097 } else
1098 output_datalen("\0", 1);
1099 }
1100 }
1101
1102 if (set[0].revents & POLLOUT && (nfrontp - nbackp) > 0)
1103 netflush();
1104 if (ncc > 0)
1105 telrcv();
1106 if (set[1].revents & POLLOUT && (pfrontp - pbackp) > 0)
1107 ptyflush();
1108 }
1109 cleanup(0);
1110 } /* end of telnet */
1111
1112 /*
1113 * Send interrupt to process on other side of pty.
1114 * If it is in raw mode, just write NULL;
1115 * otherwise, write intr char.
1116 */
1117 void
1118 interrupt()
1119 {
1120 ptyflush(); /* half-hearted */
1121
1122 (void) ioctl(pty, TIOCSIG, (char *)SIGINT);
1123 }
1124
1125 /*
1126 * Send quit to process on other side of pty.
1127 * If it is in raw mode, just write NULL;
1128 * otherwise, write quit char.
1129 */
1130 void
1131 sendbrk()
1132 {
1133 ptyflush(); /* half-hearted */
1134 (void) ioctl(pty, TIOCSIG, (char *)SIGQUIT);
1135 }
1136
1137 void
1138 sendsusp()
1139 {
1140 ptyflush(); /* half-hearted */
1141 (void) ioctl(pty, TIOCSIG, (char *)SIGTSTP);
1142 }
1143
1144 /*
1145 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1146 * just send back "[Yes]".
1147 */
1148 void
1149 recv_ayt()
1150 {
1151 #if defined(SIGINFO)
1152 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1153 (void) ioctl(pty, TIOCSIG, (char *)SIGINFO);
1154 return;
1155 }
1156 #endif
1157 (void) output_data("\r\n[Yes]\r\n");
1158 }
1159
1160 void
1161 doeof()
1162 {
1163 init_termbuf();
1164
1165 #if defined(LINEMODE) && (VEOF == VMIN)
1166 if (!tty_isediting()) {
1167 extern char oldeofc;
1168 *pfrontp++ = oldeofc;
1169 return;
1170 }
1171 #endif
1172 *pfrontp++ = slctab[SLC_EOF].sptr ?
1173 (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1174 }
1175