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
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.3 2006/01/12 13:43:10 corecode Exp $
39 #include "pathnames.h"
48 #include <arpa/inet.h>
50 #include <libtelnet/misc.h>
52 char remote_hostname[MAXHOSTNAMELEN];
53 size_t utmp_len = sizeof(remote_hostname) - 1;
54 int registerd_host_only = 0;
59 * pointers, and counters.
61 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
62 char ptyibuf2[BUFSIZ];
64 int readstream(int, char *, int);
65 void doit(struct sockaddr *);
66 int terminaltypeok(char *);
68 int hostinfo = 1; /* do we print login banner? */
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);
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().
86 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
100 int family = AF_INET;
102 #ifndef MAXHOSTNAMELEN
103 #define MAXHOSTNAMELEN 256
104 #endif /* MAXHOSTNAMELEN */
107 char host_name[MAXHOSTNAMELEN];
109 extern void telnet(int, int, char *);
115 main(int argc, char *argv[])
117 struct sockaddr_storage from;
120 #if defined(IPPROTO_IP) && defined(IP_TOS)
124 pfrontp = pbackp = ptyobuf;
126 nfrontp = nbackp = netobuf;
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.
142 linemode=1; /*Default to mode that works on bulk of clients*/
144 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
152 #endif /* BFTPDAEMON */
155 if (strcmp(optarg, "ebug") == 0) {
166 * Check for desired diagnostics capabilities.
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;
183 #endif /* DIAGNOSTICS */
194 #endif /* LINEMODE */
197 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
198 lmodetype = NO_AUTOKLUDGE;
200 /* ignore -k option if built without kludge linemode */
201 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
214 if ((tos = parsetos(optarg, "tcp")) < 0)
216 "bad TOS argument '", optarg,
217 "'; will try to use default TOS");
219 warnx("TOS option unavailable; -S flag not supported");
224 utmp_len = (size_t)atoi(optarg);
225 if (utmp_len >= sizeof(remote_hostname))
226 utmp_len = sizeof(remote_hostname) - 1;
230 registerd_host_only = 1;
245 warnx("%c: unknown option", ch);
257 int s, ns, foo, error;
258 const char *service = "telnet";
259 struct addrinfo hints, *res;
264 } else if (argc == 1)
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);
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));
281 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
284 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
285 (char *)&on, sizeof(on));
286 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
288 if (listen(s, 1) < 0)
290 foo = res->ai_addrlen;
291 ns = accept(s, res->ai_addr, &foo);
298 } else if (argc == 1) {
299 ; /* VOID*/ /* Just ignore the host/port name */
301 } else if (argc > 0) {
306 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
307 fromlen = sizeof (from);
308 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
313 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
314 (char *)&on, sizeof (on)) < 0) {
315 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
318 #if defined(IPPROTO_IP) && defined(IP_TOS)
319 if (from.ss_family == AF_INET) {
320 # if defined(HAS_GETTOS)
322 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
326 tos = 020; /* Low Delay bit */
328 && (setsockopt(0, IPPROTO_IP, IP_TOS,
329 (char *)&tos, sizeof(tos)) < 0)
330 && (errno != ENOPROTOOPT) )
331 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
333 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
335 doit((struct sockaddr *)&from);
343 fprintf(stderr, "usage: telnetd");
345 fprintf(stderr, " [-B]");
347 fprintf(stderr, " [-debug]");
349 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
351 fprintf(stderr, " [-h]");
352 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
353 fprintf(stderr, " [-k]");
356 fprintf(stderr, " [-l]");
358 fprintf(stderr, " [-n]");
359 fprintf(stderr, "\n\t");
361 fprintf(stderr, " [-S tos]");
363 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
364 fprintf(stderr, " [port]\n");
371 * Ask the other end to send along its terminal type and speed.
372 * Output is the variable terminaltype filled in.
374 static unsigned char ttytype_sbbuf[] = {
375 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
379 #define undef2 __unused
382 getterminaltype(char *name undef2)
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);
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)) {
401 if (his_state_is_will(TELOPT_TSPEED)) {
402 static unsigned char sb[] =
403 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
405 output_datalen(sb, sizeof sb);
406 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
408 if (his_state_is_will(TELOPT_XDISPLOC)) {
409 static unsigned char sb[] =
410 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
412 output_datalen(sb, sizeof sb);
413 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
415 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
416 static unsigned char sb[] =
417 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
419 output_datalen(sb, sizeof sb);
420 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
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 };
426 output_datalen(sb, sizeof sb);
427 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
429 if (his_state_is_will(TELOPT_TTYPE)) {
431 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
432 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
433 sizeof ttytype_sbbuf - 2););
435 if (his_state_is_will(TELOPT_TSPEED)) {
436 while (sequenceIs(tspeedsubopt, baseline))
439 if (his_state_is_will(TELOPT_XDISPLOC)) {
440 while (sequenceIs(xdisplocsubopt, baseline))
443 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
444 while (sequenceIs(environsubopt, baseline))
447 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
448 while (sequenceIs(oenvironsubopt, baseline))
451 if (his_state_is_will(TELOPT_TTYPE)) {
452 char first[256], last[256];
454 while (sequenceIs(ttypesubopt, baseline))
458 * If the other side has already disabled the option, then
459 * we have to just go with what we (might) have already gotten.
461 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
462 (void) strncpy(first, terminaltype, sizeof(first)-1);
463 first[sizeof(first)-1] = '\0';
466 * Save the unknown name, and request the next name.
468 (void) strncpy(last, terminaltype, sizeof(last)-1);
469 last[sizeof(last)-1] = '\0';
471 if (terminaltypeok(terminaltype))
473 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
474 his_state_is_wont(TELOPT_TTYPE)) {
476 * We've hit the end. If this is the same as
477 * the first name, just go with it.
479 if (strncmp(first, terminaltype, sizeof(first)) == 0)
482 * Get the terminal name one more time, so that
483 * RFC1091 compliant telnets will cycle back to
484 * the start of the list.
487 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
488 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
489 terminaltype[sizeof(terminaltype)-1] = '\0';
497 } /* end of getterminaltype */
503 * If the client turned off the option,
504 * we can't send another request, so we
507 if (his_state_is_wont(TELOPT_TTYPE))
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))
518 terminaltypeok(char *s)
522 if (terminaltype == NULL)
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.
532 if (tgetent(buf, s) == 0)
538 * Get a pty, scan input lines.
541 doit(struct sockaddr *who)
547 * Find an available pty to use.
550 pty = getpty(&ptynum);
552 fatal(net, "All network ports in use");
557 if ((lp = getpty()) == NULL)
558 fatal(net, "Out of ptys");
560 if ((pty = open(lp, 2)) >= 0) {
561 strlcpy(line,lp,sizeof(line));
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';
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 */
582 (void) gethostname(host_name, sizeof(host_name) - 1);
583 host_name[sizeof(host_name) - 1] = '\0';
584 hostname = host_name;
592 level = getterminaltype(user_name);
593 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1)
594 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network");
596 telnet(net, pty, remote_hostname); /* begin server process */
602 * Main loop. Select from pty and network, and
603 * hand data to telnet receiver finite state machine.
606 telnet(int f, int p, char *host)
609 #define TABBUFSIZ 512
610 char defent[TABBUFSIZ];
611 char defstrs[TABBUFSIZ];
619 * Initialize the slc mapping table.
624 * Do some tests where it is desireable to wait for a response.
625 * Rather than doing them slowly, one at a time, do them all
628 if (my_state_is_wont(TELOPT_SGA))
629 send_will(TELOPT_SGA, 1);
631 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
632 * because 4.2 clients are unable to deal with TCP urgent data.
634 * To find out, we send out a "DO ECHO". If the remote system
635 * answers "WILL ECHO" it is probably a 4.2 client, and we note
636 * that fact ("WILL ECHO" ==> that the client will echo what
637 * WE, the server, sends it; it does NOT mean that the client will
638 * echo the terminal input).
640 send_do(TELOPT_ECHO, 1);
643 if (his_state_is_wont(TELOPT_LINEMODE)) {
644 /* Query the peer for linemode support by trying to negotiate
645 * the linemode option.
649 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
651 #endif /* LINEMODE */
654 * Send along a couple of other options that we wish to negotiate.
656 send_do(TELOPT_NAWS, 1);
657 send_will(TELOPT_STATUS, 1);
658 flowmode = 1; /* default flow control state */
659 restartany = -1; /* uninitialized... */
660 send_do(TELOPT_LFLOW, 1);
663 * Spin, waiting for a response from the DO ECHO. However,
664 * some REALLY DUMB telnets out there might not respond
665 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
666 * telnets so far seem to respond with WONT for a DO that
667 * they don't understand...) because by the time we get the
668 * response, it will already have processed the DO ECHO.
669 * Kludge upon kludge.
671 while (his_will_wont_is_changing(TELOPT_NAWS))
676 * The client might have sent a WILL NAWS as part of its
677 * startup code; if so, we'll be here before we get the
678 * response to the DO ECHO. We'll make the assumption
679 * that any implementation that understands about NAWS
680 * is a modern enough implementation that it will respond
681 * to our DO ECHO request; hence we'll do another spin
682 * waiting for the ECHO option to settle down, which is
683 * what we wanted to do in the first place...
685 if (his_want_state_is_will(TELOPT_ECHO) &&
686 his_state_is_will(TELOPT_NAWS)) {
687 while (his_will_wont_is_changing(TELOPT_ECHO))
691 * On the off chance that the telnet client is broken and does not
692 * respond to the DO ECHO we sent, (after all, we did send the
693 * DO NAWS negotiation after the DO ECHO, and we won't get here
694 * until a response to the DO NAWS comes back) simulate the
695 * receipt of a will echo. This will also send a WONT ECHO
696 * to the client, since we assume that the client failed to
697 * respond because it believes that it is already in DO ECHO
698 * mode, which we do not want.
700 if (his_want_state_is_will(TELOPT_ECHO)) {
701 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
702 willoption(TELOPT_ECHO);
706 * Finally, to clean things up, we turn on our echo. This
707 * will break stupid 4.2 telnets out of local terminal echo.
710 if (my_state_is_wont(TELOPT_ECHO))
711 send_will(TELOPT_ECHO, 1);
714 * Turn on packet mode
716 (void) ioctl(p, TIOCPKT, (char *)&on);
718 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
720 * Continuing line mode support. If client does not support
721 * real linemode, attempt to negotiate kludge linemode by sending
722 * the do timing mark sequence.
724 if (lmodetype < REAL_LINEMODE)
725 send_do(TELOPT_TM, 1);
726 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
729 * Call telrcv() once to pick up anything received during
730 * terminal type negotiation, 4.2/4.3 determination, and
731 * linemode negotiation.
735 (void) ioctl(f, FIONBIO, (char *)&on);
736 (void) ioctl(p, FIONBIO, (char *)&on);
738 #if defined(SO_OOBINLINE)
739 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
740 (char *)&on, sizeof on);
741 #endif /* defined(SO_OOBINLINE) */
744 (void) signal(SIGTSTP, SIG_IGN);
748 * Ignoring SIGTTOU keeps the kernel from blocking us
749 * in ttioct() in /sys/tty.c.
751 (void) signal(SIGTTOU, SIG_IGN);
754 (void) signal(SIGCHLD, cleanup);
759 t = open(_PATH_TTY, O_RDWR);
761 (void) ioctl(t, TIOCNOTTY, NULL);
768 * Show banner that getty never gave.
770 * We put the banner in the pty input buffer. This way, it
771 * gets carriage return null processing, etc., just like all
772 * other pty --> client data.
775 if (getent(defent, "default") == 1) {
778 HE = Getstr("he", &cp);
779 HN = Getstr("hn", &cp);
780 IM = Getstr("im", &cp);
782 (void) strlcpy(host_name, HN, sizeof(host_name));
786 IM = strdup(DEFAULT_IM);
789 edithost(HE, host_name);
794 (void) strncat(ptyibuf2, ptyip, pcc+1);
799 * Last check to make sure all our states are correct.
803 #endif /* LINEMODE */
805 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
808 * Startup the login process on the slave side of the terminal
809 * now. We delay this until here to insure option negotiation
812 startslave(host, level, user_name);
814 nfd = ((f > p) ? f : p) + 1;
816 fd_set ibits, obits, xbits;
819 if (ncc < 0 && pcc < 0)
826 * Never look for input if there's still
827 * stuff in the corresponding output buffer
829 if (nfrontp - nbackp || pcc > 0) {
834 if (pfrontp - pbackp || ncc > 0) {
842 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) {
844 if (errno == EINTR) {
855 if (FD_ISSET(net, &xbits)) {
860 * Something to read from the network...
862 if (FD_ISSET(net, &ibits)) {
863 #if !defined(SO_OOBINLINE)
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
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.
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.
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").
901 (void) ioctl(net, SIOCATMARK, (char *)&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);
911 ncc = read(net, netibuf, sizeof (netibuf));
914 ncc = read(net, netibuf, sizeof (netibuf));
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)
928 DIAG((TD_REPORT | TD_NETDATA),
929 output_data("td: netread %d chars\r\n", ncc));
930 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
934 * Something to read from the pty...
936 if (FD_ISSET(p, &ibits)) {
937 pcc = read(p, ptyibuf, BUFSIZ);
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.
943 if (pcc < 0 && (errno == EWOULDBLOCK ||
954 * If ioctl from pty, pass it through net
956 if (ptyibuf[0] & TIOCPKT_IOCTL) {
957 copy_termbuf(ptyibuf+1, pcc-1);
961 #endif /* LINEMODE */
962 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
963 netclear(); /* clear buffer back */
966 * There are client telnets on some
967 * operating systems get screwed up
968 * royally if we send them urgent
971 output_data("%c%c", IAC, DM);
972 neturg = nfrontp-1; /* off by one XXX */
974 printoption("td: send IAC", DM));
978 if (his_state_is_will(TELOPT_LFLOW) &&
980 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
982 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
983 if (newflow != flowmode) {
985 output_data("%c%c%c%c%c%c",
986 IAC, SB, TELOPT_LFLOW,
990 DIAG(TD_OPTIONS, printsub('>',
991 (unsigned char *)nfrontp-4,
1001 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1003 c = *ptyip++ & 0377, pcc--;
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);
1012 output_data("%c", '\0');
1016 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1020 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1024 } /* end of telnet */
1028 # define TCSIG TIOCSIG
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.
1040 ptyflush(); /* half-hearted */
1043 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1046 *pfrontp++ = slctab[SLC_IP].sptr ?
1047 (unsigned char)*slctab[SLC_IP].sptr : '\177';
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.
1059 ptyflush(); /* half-hearted */
1061 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1064 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1065 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1073 ptyflush(); /* half-hearted */
1075 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1077 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1078 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1080 #endif /* SIGTSTP */
1084 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1085 * just send back "[Yes]".
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);
1096 output_data("\r\n[Yes]\r\n");
1104 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1105 if (!tty_isediting()) {
1106 extern char oldeofc;
1107 *pfrontp++ = oldeofc;
1111 *pfrontp++ = slctab[SLC_EOF].sptr ?
1112 (unsigned char)*slctab[SLC_EOF].sptr : '\004';