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