2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/libexec/telnetd/telnetd.c,v 1.22.2.8 2002/04/13 11:07:12 markm Exp $");
39 static const char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
43 #include "pathnames.h"
52 #include <arpa/inet.h>
54 #include <libtelnet/misc.h>
56 char remote_hostname[MAXHOSTNAMELEN];
57 size_t utmp_len = sizeof(remote_hostname) - 1;
58 int registerd_host_only = 0;
63 * pointers, and counters.
65 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
66 char ptyibuf2[BUFSIZ];
68 int readstream(int, char *, int);
69 void doit(struct sockaddr *);
70 int terminaltypeok(char *);
72 int hostinfo = 1; /* do we print login banner? */
78 void doit(struct sockaddr *);
79 int terminaltypeok(char *);
80 void startslave(char *, int, char *);
81 extern void usage(void);
82 static void _gettermname(void);
85 * The string to pass to getopt(). We do it this way so
86 * that only the actual options that we support will be
87 * passed off to getopt().
90 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
104 int family = AF_INET;
106 #ifndef MAXHOSTNAMELEN
107 #define MAXHOSTNAMELEN 256
108 #endif /* MAXHOSTNAMELEN */
111 char host_name[MAXHOSTNAMELEN];
113 extern void telnet(int, int, char *);
119 main(int argc, char *argv[])
121 struct sockaddr_storage from;
124 #if defined(IPPROTO_IP) && defined(IP_TOS)
128 pfrontp = pbackp = ptyobuf;
130 nfrontp = nbackp = netobuf;
133 * This initialization causes linemode to default to a configuration
134 * that works on all telnet clients, including the FreeBSD client.
135 * This is not quite the same as the telnet client issuing a "mode
136 * character" command, but has most of the same benefits, and is
137 * preferable since some clients (like usofts) don't have the
138 * mode character command anyway and linemode breaks things.
139 * The most notable symptom of fix is that csh "set filec" operations
140 * like <ESC> (filename completion) and ^D (choices) keys now work
141 * in telnet sessions and can be used more than once on the same line.
142 * CR/LF handling is also corrected in some termio modes. This
143 * change resolves problem reports bin/771 and bin/1037.
146 linemode=1; /*Default to mode that works on bulk of clients*/
148 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
156 #endif /* BFTPDAEMON */
159 if (strcmp(optarg, "ebug") == 0) {
170 * Check for desired diagnostics capabilities.
172 if (!strcmp(optarg, "report")) {
173 diagnostic |= TD_REPORT|TD_OPTIONS;
174 } else if (!strcmp(optarg, "exercise")) {
175 diagnostic |= TD_EXERCISE;
176 } else if (!strcmp(optarg, "netdata")) {
177 diagnostic |= TD_NETDATA;
178 } else if (!strcmp(optarg, "ptydata")) {
179 diagnostic |= TD_PTYDATA;
180 } else if (!strcmp(optarg, "options")) {
181 diagnostic |= TD_OPTIONS;
187 #endif /* DIAGNOSTICS */
198 #endif /* LINEMODE */
201 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
202 lmodetype = NO_AUTOKLUDGE;
204 /* ignore -k option if built without kludge linemode */
205 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
218 if ((tos = parsetos(optarg, "tcp")) < 0)
220 "bad TOS argument '", optarg,
221 "'; will try to use default TOS");
223 warnx("TOS option unavailable; -S flag not supported");
228 utmp_len = (size_t)atoi(optarg);
229 if (utmp_len >= sizeof(remote_hostname))
230 utmp_len = sizeof(remote_hostname) - 1;
234 registerd_host_only = 1;
249 warnx("%c: unknown option", ch);
261 int s, ns, foo, error;
262 const char *service = "telnet";
263 struct addrinfo hints, *res;
268 } else if (argc == 1)
271 memset(&hints, 0, sizeof(hints));
272 hints.ai_flags = AI_PASSIVE;
273 hints.ai_family = family;
274 hints.ai_socktype = SOCK_STREAM;
275 hints.ai_protocol = 0;
276 error = getaddrinfo(NULL, service, &hints, &res);
279 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
280 if (error == EAI_SYSTEM)
281 errx(1, "tcp/%s: %s\n", service, strerror(errno));
285 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
288 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
289 (char *)&on, sizeof(on));
290 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
292 if (listen(s, 1) < 0)
294 foo = res->ai_addrlen;
295 ns = accept(s, res->ai_addr, &foo);
302 } else if (argc == 1) {
303 ; /* VOID*/ /* Just ignore the host/port name */
305 } else if (argc > 0) {
310 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
311 fromlen = sizeof (from);
312 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
317 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
318 (char *)&on, sizeof (on)) < 0) {
319 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
322 #if defined(IPPROTO_IP) && defined(IP_TOS)
323 if (from.ss_family == AF_INET) {
324 # if defined(HAS_GETTOS)
326 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
330 tos = 020; /* Low Delay bit */
332 && (setsockopt(0, IPPROTO_IP, IP_TOS,
333 (char *)&tos, sizeof(tos)) < 0)
334 && (errno != ENOPROTOOPT) )
335 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
337 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
339 doit((struct sockaddr *)&from);
347 fprintf(stderr, "usage: telnetd");
349 fprintf(stderr, " [-B]");
351 fprintf(stderr, " [-debug]");
353 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
355 fprintf(stderr, " [-h]");
356 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
357 fprintf(stderr, " [-k]");
360 fprintf(stderr, " [-l]");
362 fprintf(stderr, " [-n]");
363 fprintf(stderr, "\n\t");
365 fprintf(stderr, " [-S tos]");
367 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
368 fprintf(stderr, " [port]\n");
375 * Ask the other end to send along its terminal type and speed.
376 * Output is the variable terminaltype filled in.
378 static unsigned char ttytype_sbbuf[] = {
379 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
383 #define undef2 __unused
386 getterminaltype(char *name undef2)
392 send_do(TELOPT_TTYPE, 1);
393 send_do(TELOPT_TSPEED, 1);
394 send_do(TELOPT_XDISPLOC, 1);
395 send_do(TELOPT_NEW_ENVIRON, 1);
396 send_do(TELOPT_OLD_ENVIRON, 1);
398 his_will_wont_is_changing(TELOPT_TTYPE) ||
399 his_will_wont_is_changing(TELOPT_TSPEED) ||
400 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
401 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
402 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
405 if (his_state_is_will(TELOPT_TSPEED)) {
406 static unsigned char sb[] =
407 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
409 output_datalen(sb, sizeof sb);
410 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
412 if (his_state_is_will(TELOPT_XDISPLOC)) {
413 static unsigned char sb[] =
414 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
416 output_datalen(sb, sizeof sb);
417 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
419 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
420 static unsigned char sb[] =
421 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
423 output_datalen(sb, sizeof sb);
424 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
426 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
427 static unsigned char sb[] =
428 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
430 output_datalen(sb, sizeof sb);
431 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
433 if (his_state_is_will(TELOPT_TTYPE)) {
435 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
436 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
437 sizeof ttytype_sbbuf - 2););
439 if (his_state_is_will(TELOPT_TSPEED)) {
440 while (sequenceIs(tspeedsubopt, baseline))
443 if (his_state_is_will(TELOPT_XDISPLOC)) {
444 while (sequenceIs(xdisplocsubopt, baseline))
447 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
448 while (sequenceIs(environsubopt, baseline))
451 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
452 while (sequenceIs(oenvironsubopt, baseline))
455 if (his_state_is_will(TELOPT_TTYPE)) {
456 char first[256], last[256];
458 while (sequenceIs(ttypesubopt, baseline))
462 * If the other side has already disabled the option, then
463 * we have to just go with what we (might) have already gotten.
465 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
466 (void) strncpy(first, terminaltype, sizeof(first)-1);
467 first[sizeof(first)-1] = '\0';
470 * Save the unknown name, and request the next name.
472 (void) strncpy(last, terminaltype, sizeof(last)-1);
473 last[sizeof(last)-1] = '\0';
475 if (terminaltypeok(terminaltype))
477 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
478 his_state_is_wont(TELOPT_TTYPE)) {
480 * We've hit the end. If this is the same as
481 * the first name, just go with it.
483 if (strncmp(first, terminaltype, sizeof(first)) == 0)
486 * Get the terminal name one more time, so that
487 * RFC1091 compliant telnets will cycle back to
488 * the start of the list.
491 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
492 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
493 terminaltype[sizeof(terminaltype)-1] = '\0';
501 } /* end of getterminaltype */
507 * If the client turned off the option,
508 * we can't send another request, so we
511 if (his_state_is_wont(TELOPT_TTYPE))
514 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
515 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
516 sizeof ttytype_sbbuf - 2););
517 while (sequenceIs(ttypesubopt, baseline))
522 terminaltypeok(char *s)
526 if (terminaltype == NULL)
530 * tgetent() will return 1 if the type is known, and
531 * 0 if it is not known. If it returns -1, it couldn't
532 * open the database. But if we can't open the database,
533 * it won't help to say we failed, because we won't be
534 * able to verify anything else. So, we treat -1 like 1.
536 if (tgetent(buf, s) == 0)
542 * Get a pty, scan input lines.
545 doit(struct sockaddr *who)
551 * Find an available pty to use.
554 pty = getpty(&ptynum);
556 fatal(net, "All network ports in use");
561 if ((lp = getpty()) == NULL)
562 fatal(net, "Out of ptys");
564 if ((pty = open(lp, 2)) >= 0) {
565 strlcpy(line,lp,sizeof(line));
572 /* get name of connected client */
573 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
574 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
575 fatal(net, "Couldn't resolve your address into a host name.\r\n\
576 Please contact your net administrator");
577 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
579 trimdomain(remote_hostname, UT_HOSTSIZE);
580 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
581 err_ = getnameinfo(who, who->sa_len, remote_hostname,
582 sizeof(remote_hostname), NULL, 0,
583 NI_NUMERICHOST|NI_WITHSCOPEID);
584 /* XXX: do 'err_' check */
586 (void) gethostname(host_name, sizeof(host_name) - 1);
587 host_name[sizeof(host_name) - 1] = '\0';
588 hostname = host_name;
596 level = getterminaltype(user_name);
597 setenv("TERM", terminaltype ? terminaltype : "network", 1);
599 telnet(net, pty, remote_hostname); /* begin server process */
605 * Main loop. Select from pty and network, and
606 * hand data to telnet receiver finite state machine.
609 telnet(int f, int p, char *host)
612 #define TABBUFSIZ 512
613 char defent[TABBUFSIZ];
614 char defstrs[TABBUFSIZ];
622 * Initialize the slc mapping table.
627 * Do some tests where it is desireable to wait for a response.
628 * Rather than doing them slowly, one at a time, do them all
631 if (my_state_is_wont(TELOPT_SGA))
632 send_will(TELOPT_SGA, 1);
634 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
635 * because 4.2 clients are unable to deal with TCP urgent data.
637 * To find out, we send out a "DO ECHO". If the remote system
638 * answers "WILL ECHO" it is probably a 4.2 client, and we note
639 * that fact ("WILL ECHO" ==> that the client will echo what
640 * WE, the server, sends it; it does NOT mean that the client will
641 * echo the terminal input).
643 send_do(TELOPT_ECHO, 1);
646 if (his_state_is_wont(TELOPT_LINEMODE)) {
647 /* Query the peer for linemode support by trying to negotiate
648 * the linemode option.
652 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
654 #endif /* LINEMODE */
657 * Send along a couple of other options that we wish to negotiate.
659 send_do(TELOPT_NAWS, 1);
660 send_will(TELOPT_STATUS, 1);
661 flowmode = 1; /* default flow control state */
662 restartany = -1; /* uninitialized... */
663 send_do(TELOPT_LFLOW, 1);
666 * Spin, waiting for a response from the DO ECHO. However,
667 * some REALLY DUMB telnets out there might not respond
668 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
669 * telnets so far seem to respond with WONT for a DO that
670 * they don't understand...) because by the time we get the
671 * response, it will already have processed the DO ECHO.
672 * Kludge upon kludge.
674 while (his_will_wont_is_changing(TELOPT_NAWS))
679 * The client might have sent a WILL NAWS as part of its
680 * startup code; if so, we'll be here before we get the
681 * response to the DO ECHO. We'll make the assumption
682 * that any implementation that understands about NAWS
683 * is a modern enough implementation that it will respond
684 * to our DO ECHO request; hence we'll do another spin
685 * waiting for the ECHO option to settle down, which is
686 * what we wanted to do in the first place...
688 if (his_want_state_is_will(TELOPT_ECHO) &&
689 his_state_is_will(TELOPT_NAWS)) {
690 while (his_will_wont_is_changing(TELOPT_ECHO))
694 * On the off chance that the telnet client is broken and does not
695 * respond to the DO ECHO we sent, (after all, we did send the
696 * DO NAWS negotiation after the DO ECHO, and we won't get here
697 * until a response to the DO NAWS comes back) simulate the
698 * receipt of a will echo. This will also send a WONT ECHO
699 * to the client, since we assume that the client failed to
700 * respond because it believes that it is already in DO ECHO
701 * mode, which we do not want.
703 if (his_want_state_is_will(TELOPT_ECHO)) {
704 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
705 willoption(TELOPT_ECHO);
709 * Finally, to clean things up, we turn on our echo. This
710 * will break stupid 4.2 telnets out of local terminal echo.
713 if (my_state_is_wont(TELOPT_ECHO))
714 send_will(TELOPT_ECHO, 1);
717 * Turn on packet mode
719 (void) ioctl(p, TIOCPKT, (char *)&on);
721 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
723 * Continuing line mode support. If client does not support
724 * real linemode, attempt to negotiate kludge linemode by sending
725 * the do timing mark sequence.
727 if (lmodetype < REAL_LINEMODE)
728 send_do(TELOPT_TM, 1);
729 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
732 * Call telrcv() once to pick up anything received during
733 * terminal type negotiation, 4.2/4.3 determination, and
734 * linemode negotiation.
738 (void) ioctl(f, FIONBIO, (char *)&on);
739 (void) ioctl(p, FIONBIO, (char *)&on);
741 #if defined(SO_OOBINLINE)
742 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
743 (char *)&on, sizeof on);
744 #endif /* defined(SO_OOBINLINE) */
747 (void) signal(SIGTSTP, SIG_IGN);
751 * Ignoring SIGTTOU keeps the kernel from blocking us
752 * in ttioct() in /sys/tty.c.
754 (void) signal(SIGTTOU, SIG_IGN);
757 (void) signal(SIGCHLD, cleanup);
762 t = open(_PATH_TTY, O_RDWR);
764 (void) ioctl(t, TIOCNOTTY, (char *)0);
771 * Show banner that getty never gave.
773 * We put the banner in the pty input buffer. This way, it
774 * gets carriage return null processing, etc., just like all
775 * other pty --> client data.
778 if (getent(defent, "default") == 1) {
781 HE = Getstr("he", &cp);
782 HN = Getstr("hn", &cp);
783 IM = Getstr("im", &cp);
785 (void) strlcpy(host_name, HN, sizeof(host_name));
789 IM = strdup(DEFAULT_IM);
792 edithost(HE, host_name);
797 (void) strncat(ptyibuf2, ptyip, pcc+1);
802 * Last check to make sure all our states are correct.
806 #endif /* LINEMODE */
808 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
811 * Startup the login process on the slave side of the terminal
812 * now. We delay this until here to insure option negotiation
815 startslave(host, level, user_name);
817 nfd = ((f > p) ? f : p) + 1;
819 fd_set ibits, obits, xbits;
822 if (ncc < 0 && pcc < 0)
829 * Never look for input if there's still
830 * stuff in the corresponding output buffer
832 if (nfrontp - nbackp || pcc > 0) {
837 if (pfrontp - pbackp || ncc > 0) {
845 if ((c = select(nfd, &ibits, &obits, &xbits,
846 (struct timeval *)0)) < 1) {
848 if (errno == EINTR) {
859 if (FD_ISSET(net, &xbits)) {
864 * Something to read from the network...
866 if (FD_ISSET(net, &ibits)) {
867 #if !defined(SO_OOBINLINE)
869 * In 4.2 (and 4.3 beta) systems, the
870 * OOB indication and data handling in the kernel
871 * is such that if two separate TCP Urgent requests
872 * come in, one byte of TCP data will be overlaid.
873 * This is fatal for Telnet, but we try to live
876 * In addition, in 4.2 (and...), a special protocol
877 * is needed to pick up the TCP Urgent data in
878 * the correct sequence.
880 * What we do is: if we think we are in urgent
881 * mode, we look to see if we are "at the mark".
882 * If we are, we do an OOB receive. If we run
883 * this twice, we will do the OOB receive twice,
884 * but the second will fail, since the second
885 * time we were "at the mark", but there wasn't
886 * any data there (the kernel doesn't reset
887 * "at the mark" until we do a normal read).
888 * Once we've read the OOB data, we go ahead
889 * and do normal reads.
891 * There is also another problem, which is that
892 * since the OOB byte we read doesn't put us
893 * out of OOB state, and since that byte is most
894 * likely the TELNET DM (data mark), we would
895 * stay in the TELNET SYNCH (SYNCHing) state.
896 * So, clocks to the rescue. If we've "just"
897 * received a DM, then we test for the
898 * presence of OOB data when the receive OOB
899 * fails (and AFTER we did the normal mode read
900 * to clear "at the mark").
905 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
907 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
908 if ((ncc == -1) && (errno == EINVAL)) {
909 ncc = read(net, netibuf, sizeof (netibuf));
910 if (sequenceIs(didnetreceive, gotDM)) {
911 SYNCHing = stilloob(net);
915 ncc = read(net, netibuf, sizeof (netibuf));
918 ncc = read(net, netibuf, sizeof (netibuf));
920 settimer(didnetreceive);
921 #else /* !defined(SO_OOBINLINE)) */
922 ncc = read(net, netibuf, sizeof (netibuf));
923 #endif /* !defined(SO_OOBINLINE)) */
924 if (ncc < 0 && errno == EWOULDBLOCK)
932 DIAG((TD_REPORT | TD_NETDATA),
933 output_data("td: netread %d chars\r\n", ncc));
934 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
938 * Something to read from the pty...
940 if (FD_ISSET(p, &ibits)) {
941 pcc = read(p, ptyibuf, BUFSIZ);
943 * On some systems, if we try to read something
944 * off the master side before the slave side is
945 * opened, we get EIO.
947 if (pcc < 0 && (errno == EWOULDBLOCK ||
958 * If ioctl from pty, pass it through net
960 if (ptyibuf[0] & TIOCPKT_IOCTL) {
961 copy_termbuf(ptyibuf+1, pcc-1);
965 #endif /* LINEMODE */
966 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
967 netclear(); /* clear buffer back */
970 * There are client telnets on some
971 * operating systems get screwed up
972 * royally if we send them urgent
975 output_data("%c%c", IAC, DM);
976 neturg = nfrontp-1; /* off by one XXX */
978 printoption("td: send IAC", DM));
982 if (his_state_is_will(TELOPT_LFLOW) &&
984 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
986 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
987 if (newflow != flowmode) {
989 output_data("%c%c%c%c%c%c",
990 IAC, SB, TELOPT_LFLOW,
994 DIAG(TD_OPTIONS, printsub('>',
995 (unsigned char *)nfrontp-4,
1005 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1007 c = *ptyip++ & 0377, pcc--;
1009 output_data("%c", c);
1010 output_data("%c", c);
1011 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1012 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1013 output_data("%c", *ptyip++ & 0377);
1016 output_data("%c", '\0');
1020 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1024 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1028 } /* end of telnet */
1032 # define TCSIG TIOCSIG
1037 * Send interrupt to process on other side of pty.
1038 * If it is in raw mode, just write NULL;
1039 * otherwise, write intr char.
1044 ptyflush(); /* half-hearted */
1047 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1050 *pfrontp++ = slctab[SLC_IP].sptr ?
1051 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1056 * Send quit to process on other side of pty.
1057 * If it is in raw mode, just write NULL;
1058 * otherwise, write quit char.
1063 ptyflush(); /* half-hearted */
1065 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1068 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1069 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1077 ptyflush(); /* half-hearted */
1079 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1081 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1082 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1084 #endif /* SIGTSTP */
1088 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1089 * just send back "[Yes]".
1094 #if defined(SIGINFO) && defined(TCSIG)
1095 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1096 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1100 output_data("\r\n[Yes]\r\n");
1108 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1109 if (!tty_isediting()) {
1110 extern char oldeofc;
1111 *pfrontp++ = oldeofc;
1115 *pfrontp++ = slctab[SLC_EOF].sptr ?
1116 (unsigned char)*slctab[SLC_EOF].sptr : '\004';