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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "pathnames.h"
47 #include <arpa/inet.h>
50 #include <libtelnet/auth.h>
53 #include <libtelnet/encrypt.h>
55 #include <libtelnet/misc.h>
57 char remote_hostname[MAXHOSTNAMELEN];
58 size_t utmp_len = sizeof(remote_hostname) - 1;
59 int registerd_host_only = 0;
64 * pointers, and counters.
66 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
67 char ptyibuf2[BUFSIZ];
69 int readstream(int, char *, int);
70 void doit(struct sockaddr *);
71 int terminaltypeok(char *);
73 int hostinfo = 1; /* do we print login banner? */
79 void doit(struct sockaddr *);
80 int terminaltypeok(char *);
81 void startslave(char *, int, char *);
82 extern void usage(void);
83 static void _gettermname(void);
86 * The string to pass to getopt(). We do it this way so
87 * that only the actual options that we support will be
88 * passed off to getopt().
91 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
111 int family = AF_INET;
113 #ifndef MAXHOSTNAMELEN
114 #define MAXHOSTNAMELEN 256
115 #endif /* MAXHOSTNAMELEN */
118 char host_name[MAXHOSTNAMELEN];
120 extern void telnet(int, int, char *);
126 main(int argc, char *argv[])
129 struct sockaddr_storage from;
132 #if defined(IPPROTO_IP) && defined(IP_TOS)
137 pfrontp = pbackp = ptyobuf;
139 nfrontp = nbackp = netobuf;
142 #endif /* ENCRYPTION */
145 * This initialization causes linemode to default to a configuration
146 * that works on all telnet clients, including the FreeBSD client.
147 * This is not quite the same as the telnet client issuing a "mode
148 * character" command, but has most of the same benefits, and is
149 * preferable since some clients (like usofts) don't have the
150 * mode character command anyway and linemode breaks things.
151 * The most notable symptom of fix is that csh "set filec" operations
152 * like <ESC> (filename completion) and ^D (choices) keys now work
153 * in telnet sessions and can be used more than once on the same line.
154 * CR/LF handling is also corrected in some termio modes. This
155 * change resolves problem reports bin/771 and bin/1037.
158 linemode=1; /*Default to mode that works on bulk of clients*/
160 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
163 #ifdef AUTHENTICATION
166 * Check for required authentication level
168 if (strcmp(optarg, "debug") == 0) {
169 extern int auth_debug_mode;
171 } else if (strcasecmp(optarg, "none") == 0) {
173 } else if (strcasecmp(optarg, "other") == 0) {
174 auth_level = AUTH_OTHER;
175 } else if (strcasecmp(optarg, "user") == 0) {
176 auth_level = AUTH_USER;
177 } else if (strcasecmp(optarg, "valid") == 0) {
178 auth_level = AUTH_VALID;
179 } else if (strcasecmp(optarg, "off") == 0) {
181 * This hack turns off authentication
185 warnx("unknown authorization level for -a");
188 #endif /* AUTHENTICATION */
194 #endif /* BFTPDAEMON */
197 if (strcmp(optarg, "ebug") == 0) {
208 * Check for desired diagnostics capabilities.
210 if (!strcmp(optarg, "report")) {
211 diagnostic |= TD_REPORT|TD_OPTIONS;
212 } else if (!strcmp(optarg, "exercise")) {
213 diagnostic |= TD_EXERCISE;
214 } else if (!strcmp(optarg, "netdata")) {
215 diagnostic |= TD_NETDATA;
216 } else if (!strcmp(optarg, "ptydata")) {
217 diagnostic |= TD_PTYDATA;
218 } else if (!strcmp(optarg, "options")) {
219 diagnostic |= TD_OPTIONS;
225 #endif /* DIAGNOSTICS */
229 if (strcmp(optarg, "debug") == 0) {
230 extern int encrypt_debug_mode;
231 encrypt_debug_mode = 1;
237 #endif /* ENCRYPTION */
247 #endif /* LINEMODE */
250 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
251 lmodetype = NO_AUTOKLUDGE;
253 /* ignore -k option if built without kludge linemode */
254 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
267 if ((tos = parsetos(optarg, "tcp")) < 0)
269 "bad TOS argument '", optarg,
270 "'; will try to use default TOS");
273 ultmp = strtoul(optarg, &ep, 0);
274 if (*ep || ep == optarg || ultmp > MAXTOS)
276 "bad TOS argument '", optarg,
277 "'; will try to use default TOS");
284 utmp_len = (size_t)atoi(optarg);
285 if (utmp_len >= sizeof(remote_hostname))
286 utmp_len = sizeof(remote_hostname) - 1;
290 registerd_host_only = 1;
293 #ifdef AUTHENTICATION
296 * Check for invalid authentication types
298 auth_disable_name(optarg);
300 #endif /* AUTHENTICATION */
313 warnx("%c: unknown option", ch);
325 int s, ns, foo, error;
326 const char *service = "telnet";
327 struct addrinfo hints, *res;
332 } else if (argc == 1)
335 memset(&hints, 0, sizeof(hints));
336 hints.ai_flags = AI_PASSIVE;
337 hints.ai_family = family;
338 hints.ai_socktype = SOCK_STREAM;
339 hints.ai_protocol = 0;
340 error = getaddrinfo(NULL, service, &hints, &res);
343 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
344 if (error == EAI_SYSTEM)
345 errx(1, "tcp/%s: %s\n", service, strerror(errno));
349 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
352 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
353 (char *)&on, sizeof(on));
355 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
356 (char *)&on, sizeof(on));
357 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
359 if (listen(s, 1) < 0)
361 foo = res->ai_addrlen;
362 ns = accept(s, res->ai_addr, &foo);
365 (void) setsockopt(ns, SOL_SOCKET, SO_DEBUG,
366 (char *)&on, sizeof(on));
371 } else if (argc == 1) {
372 ; /* VOID*/ /* Just ignore the host/port name */
374 } else if (argc > 0) {
379 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
380 fromlen = sizeof (from);
381 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
386 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
387 (char *)&on, sizeof (on)) < 0) {
388 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
391 #if defined(IPPROTO_IP) && defined(IP_TOS)
392 if (from.ss_family == AF_INET) {
393 # if defined(HAS_GETTOS)
395 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
399 tos = 020; /* Low Delay bit */
401 && (setsockopt(0, IPPROTO_IP, IP_TOS,
402 (char *)&tos, sizeof(tos)) < 0)
403 && (errno != ENOPROTOOPT) )
404 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
406 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
408 doit((struct sockaddr *)&from);
416 fprintf(stderr, "usage: telnetd");
417 #ifdef AUTHENTICATION
419 " [-4] [-6] [-a (debug|other|user|valid|off|none)]\n\t");
422 fprintf(stderr, " [-B]");
424 fprintf(stderr, " [-debug]");
426 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
428 #ifdef AUTHENTICATION
429 fprintf(stderr, " [-edebug]");
431 fprintf(stderr, " [-h]");
432 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
433 fprintf(stderr, " [-k]");
436 fprintf(stderr, " [-l]");
438 fprintf(stderr, " [-n]");
439 fprintf(stderr, "\n\t");
441 fprintf(stderr, " [-S tos]");
443 #ifdef AUTHENTICATION
444 fprintf(stderr, " [-X auth-type]");
446 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
447 fprintf(stderr, " [port]\n");
454 * Ask the other end to send along its terminal type and speed.
455 * Output is the variable terminaltype filled in.
457 static unsigned char ttytype_sbbuf[] = {
458 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
462 #ifndef AUTHENTICATION
463 #define undef2 __unused
469 getterminaltype(char *name undef2)
474 #ifdef AUTHENTICATION
476 * Handle the Authentication option before we do anything else.
478 if (auth_level >= 0) {
479 send_do(TELOPT_AUTHENTICATION, 1);
480 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
482 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
483 retval = auth_wait(name);
489 send_will(TELOPT_ENCRYPT, 1);
490 #endif /* ENCRYPTION */
491 send_do(TELOPT_TTYPE, 1);
492 send_do(TELOPT_TSPEED, 1);
493 send_do(TELOPT_XDISPLOC, 1);
494 send_do(TELOPT_NEW_ENVIRON, 1);
495 send_do(TELOPT_OLD_ENVIRON, 1);
498 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
499 #endif /* ENCRYPTION */
500 his_will_wont_is_changing(TELOPT_TTYPE) ||
501 his_will_wont_is_changing(TELOPT_TSPEED) ||
502 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
503 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
504 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
509 * Wait for the negotiation of what type of encryption we can
510 * send with. If autoencrypt is not set, this will just return.
512 if (his_state_is_will(TELOPT_ENCRYPT)) {
515 #endif /* ENCRYPTION */
516 if (his_state_is_will(TELOPT_TSPEED)) {
517 static unsigned char sb[] =
518 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
520 output_datalen(sb, sizeof sb);
521 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
523 if (his_state_is_will(TELOPT_XDISPLOC)) {
524 static unsigned char sb[] =
525 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
527 output_datalen(sb, sizeof sb);
528 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
530 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
531 static unsigned char sb[] =
532 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
534 output_datalen(sb, sizeof sb);
535 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
537 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
538 static unsigned char sb[] =
539 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
541 output_datalen(sb, sizeof sb);
542 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
544 if (his_state_is_will(TELOPT_TTYPE)) {
546 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
547 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
548 sizeof ttytype_sbbuf - 2););
550 if (his_state_is_will(TELOPT_TSPEED)) {
551 while (sequenceIs(tspeedsubopt, baseline))
554 if (his_state_is_will(TELOPT_XDISPLOC)) {
555 while (sequenceIs(xdisplocsubopt, baseline))
558 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
559 while (sequenceIs(environsubopt, baseline))
562 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
563 while (sequenceIs(oenvironsubopt, baseline))
566 if (his_state_is_will(TELOPT_TTYPE)) {
567 char first[256], last[256];
569 while (sequenceIs(ttypesubopt, baseline))
573 * If the other side has already disabled the option, then
574 * we have to just go with what we (might) have already gotten.
576 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
577 (void) strncpy(first, terminaltype, sizeof(first)-1);
578 first[sizeof(first)-1] = '\0';
581 * Save the unknown name, and request the next name.
583 (void) strncpy(last, terminaltype, sizeof(last)-1);
584 last[sizeof(last)-1] = '\0';
586 if (terminaltypeok(terminaltype))
588 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
589 his_state_is_wont(TELOPT_TTYPE)) {
591 * We've hit the end. If this is the same as
592 * the first name, just go with it.
594 if (strncmp(first, terminaltype, sizeof(first)) == 0)
597 * Get the terminal name one more time, so that
598 * RFC1091 compliant telnets will cycle back to
599 * the start of the list.
602 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
603 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
604 terminaltype[sizeof(terminaltype)-1] = '\0';
612 } /* end of getterminaltype */
618 * If the client turned off the option,
619 * we can't send another request, so we
622 if (his_state_is_wont(TELOPT_TTYPE))
625 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
626 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
627 sizeof ttytype_sbbuf - 2););
628 while (sequenceIs(ttypesubopt, baseline))
633 terminaltypeok(char *s)
637 if (terminaltype == NULL)
641 * tgetent() will return 1 if the type is known, and
642 * 0 if it is not known. If it returns -1, it couldn't
643 * open the database. But if we can't open the database,
644 * it won't help to say we failed, because we won't be
645 * able to verify anything else. So, we treat -1 like 1.
647 if (tgetent(buf, s) == 0)
653 * Get a pty, scan input lines.
656 doit(struct sockaddr *who)
662 * Find an available pty to use.
665 pty = getpty(&ptynum);
667 fatal(net, "All network ports in use");
672 if ((lp = getpty()) == NULL)
673 fatal(net, "Out of ptys");
675 if ((pty = open(lp, 2)) >= 0) {
676 strlcpy(line,lp,sizeof(line));
683 /* get name of connected client */
684 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
685 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
686 fatal(net, "Couldn't resolve your address into a host name.\r\n\
687 Please contact your net administrator");
688 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
690 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
691 err_ = getnameinfo(who, who->sa_len, remote_hostname,
692 sizeof(remote_hostname), NULL, 0,
694 /* XXX: do 'err_' check */
696 (void) gethostname(host_name, sizeof(host_name) - 1);
697 host_name[sizeof(host_name) - 1] = '\0';
698 hostname = host_name;
700 #ifdef AUTHENTICATION
702 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
703 * This is a byproduct of needing "#ifdef" and not "#if defined()"
704 * for unifdef. XXX MarkM
706 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
715 level = getterminaltype(user_name);
716 setenv("TERM", terminaltype ? terminaltype : "network", 1);
718 telnet(net, pty, remote_hostname); /* begin server process */
724 * Main loop. Select from pty and network, and
725 * hand data to telnet receiver finite state machine.
728 telnet(int f, int p, char *host)
731 #define TABBUFSIZ 512
732 char defent[TABBUFSIZ];
733 char defstrs[TABBUFSIZ];
745 * Initialize the slc mapping table.
750 * Do some tests where it is desireable to wait for a response.
751 * Rather than doing them slowly, one at a time, do them all
754 if (my_state_is_wont(TELOPT_SGA))
755 send_will(TELOPT_SGA, 1);
757 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
758 * because 4.2 clients are unable to deal with TCP urgent data.
760 * To find out, we send out a "DO ECHO". If the remote system
761 * answers "WILL ECHO" it is probably a 4.2 client, and we note
762 * that fact ("WILL ECHO" ==> that the client will echo what
763 * WE, the server, sends it; it does NOT mean that the client will
764 * echo the terminal input).
766 send_do(TELOPT_ECHO, 1);
769 if (his_state_is_wont(TELOPT_LINEMODE)) {
770 /* Query the peer for linemode support by trying to negotiate
771 * the linemode option.
775 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
777 #endif /* LINEMODE */
780 * Send along a couple of other options that we wish to negotiate.
782 send_do(TELOPT_NAWS, 1);
783 send_will(TELOPT_STATUS, 1);
784 flowmode = 1; /* default flow control state */
785 restartany = -1; /* uninitialized... */
786 send_do(TELOPT_LFLOW, 1);
789 * Spin, waiting for a response from the DO ECHO. However,
790 * some REALLY DUMB telnets out there might not respond
791 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
792 * telnets so far seem to respond with WONT for a DO that
793 * they don't understand...) because by the time we get the
794 * response, it will already have processed the DO ECHO.
795 * Kludge upon kludge.
797 while (his_will_wont_is_changing(TELOPT_NAWS))
802 * The client might have sent a WILL NAWS as part of its
803 * startup code; if so, we'll be here before we get the
804 * response to the DO ECHO. We'll make the assumption
805 * that any implementation that understands about NAWS
806 * is a modern enough implementation that it will respond
807 * to our DO ECHO request; hence we'll do another spin
808 * waiting for the ECHO option to settle down, which is
809 * what we wanted to do in the first place...
811 if (his_want_state_is_will(TELOPT_ECHO) &&
812 his_state_is_will(TELOPT_NAWS)) {
813 while (his_will_wont_is_changing(TELOPT_ECHO))
817 * On the off chance that the telnet client is broken and does not
818 * respond to the DO ECHO we sent, (after all, we did send the
819 * DO NAWS negotiation after the DO ECHO, and we won't get here
820 * until a response to the DO NAWS comes back) simulate the
821 * receipt of a will echo. This will also send a WONT ECHO
822 * to the client, since we assume that the client failed to
823 * respond because it believes that it is already in DO ECHO
824 * mode, which we do not want.
826 if (his_want_state_is_will(TELOPT_ECHO)) {
827 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
828 willoption(TELOPT_ECHO);
832 * Finally, to clean things up, we turn on our echo. This
833 * will break stupid 4.2 telnets out of local terminal echo.
836 if (my_state_is_wont(TELOPT_ECHO))
837 send_will(TELOPT_ECHO, 1);
840 * Turn on packet mode
842 (void) ioctl(p, TIOCPKT, (char *)&on);
844 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
846 * Continuing line mode support. If client does not support
847 * real linemode, attempt to negotiate kludge linemode by sending
848 * the do timing mark sequence.
850 if (lmodetype < REAL_LINEMODE)
851 send_do(TELOPT_TM, 1);
852 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
855 * Call telrcv() once to pick up anything received during
856 * terminal type negotiation, 4.2/4.3 determination, and
857 * linemode negotiation.
861 (void) ioctl(f, FIONBIO, (char *)&on);
862 (void) ioctl(p, FIONBIO, (char *)&on);
864 #if defined(SO_OOBINLINE)
865 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
866 (char *)&on, sizeof on);
867 #endif /* defined(SO_OOBINLINE) */
870 (void) signal(SIGTSTP, SIG_IGN);
874 * Ignoring SIGTTOU keeps the kernel from blocking us
875 * in ttioct() in /sys/tty.c.
877 (void) signal(SIGTTOU, SIG_IGN);
880 (void) signal(SIGCHLD, cleanup);
885 t = open(_PATH_TTY, O_RDWR);
887 (void) ioctl(t, TIOCNOTTY, (char *)0);
894 * Show banner that getty never gave.
896 * We put the banner in the pty input buffer. This way, it
897 * gets carriage return null processing, etc., just like all
898 * other pty --> client data.
901 if (getent(defent, "default") == 1) {
904 HE = Getstr("he", &cp);
905 HN = Getstr("hn", &cp);
906 IM = Getstr("im", &cp);
907 IF = Getstr("if", &cp);
909 (void) strlcpy(host_name, HN, sizeof(host_name));
911 if_fd = open(IF, O_RDONLY, 000);
917 IM = strdup(DEFAULT_IM);
920 edithost(HE, host_name);
924 if (fstat(if_fd, &statbuf) != -1 && statbuf.st_size > 0) {
925 if_buf = (char *) mmap (0, statbuf.st_size,
926 PROT_READ, 0, if_fd, 0);
927 if (if_buf != MAP_FAILED) {
928 putf(if_buf, ptyibuf2);
929 munmap(if_buf, statbuf.st_size);
936 (void) strncat(ptyibuf2, ptyip, pcc+1);
941 * Last check to make sure all our states are correct.
945 #endif /* LINEMODE */
947 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
950 * Startup the login process on the slave side of the terminal
951 * now. We delay this until here to insure option negotiation
954 startslave(host, level, user_name);
956 nfd = ((f > p) ? f : p) + 1;
958 fd_set ibits, obits, xbits;
961 if (ncc < 0 && pcc < 0)
968 * Never look for input if there's still
969 * stuff in the corresponding output buffer
971 if (nfrontp - nbackp || pcc > 0) {
976 if (pfrontp - pbackp || ncc > 0) {
984 if ((c = select(nfd, &ibits, &obits, &xbits,
985 (struct timeval *)0)) < 1) {
987 if (errno == EINTR) {
998 if (FD_ISSET(net, &xbits)) {
1003 * Something to read from the network...
1005 if (FD_ISSET(net, &ibits)) {
1006 #if !defined(SO_OOBINLINE)
1008 * In 4.2 (and 4.3 beta) systems, the
1009 * OOB indication and data handling in the kernel
1010 * is such that if two separate TCP Urgent requests
1011 * come in, one byte of TCP data will be overlaid.
1012 * This is fatal for Telnet, but we try to live
1015 * In addition, in 4.2 (and...), a special protocol
1016 * is needed to pick up the TCP Urgent data in
1017 * the correct sequence.
1019 * What we do is: if we think we are in urgent
1020 * mode, we look to see if we are "at the mark".
1021 * If we are, we do an OOB receive. If we run
1022 * this twice, we will do the OOB receive twice,
1023 * but the second will fail, since the second
1024 * time we were "at the mark", but there wasn't
1025 * any data there (the kernel doesn't reset
1026 * "at the mark" until we do a normal read).
1027 * Once we've read the OOB data, we go ahead
1028 * and do normal reads.
1030 * There is also another problem, which is that
1031 * since the OOB byte we read doesn't put us
1032 * out of OOB state, and since that byte is most
1033 * likely the TELNET DM (data mark), we would
1034 * stay in the TELNET SYNCH (SYNCHing) state.
1035 * So, clocks to the rescue. If we've "just"
1036 * received a DM, then we test for the
1037 * presence of OOB data when the receive OOB
1038 * fails (and AFTER we did the normal mode read
1039 * to clear "at the mark").
1044 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1046 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1047 if ((ncc == -1) && (errno == EINVAL)) {
1048 ncc = read(net, netibuf, sizeof (netibuf));
1049 if (sequenceIs(didnetreceive, gotDM)) {
1050 SYNCHing = stilloob(net);
1054 ncc = read(net, netibuf, sizeof (netibuf));
1057 ncc = read(net, netibuf, sizeof (netibuf));
1059 settimer(didnetreceive);
1060 #else /* !defined(SO_OOBINLINE)) */
1061 ncc = read(net, netibuf, sizeof (netibuf));
1062 #endif /* !defined(SO_OOBINLINE)) */
1063 if (ncc < 0 && errno == EWOULDBLOCK)
1071 DIAG((TD_REPORT | TD_NETDATA),
1072 output_data("td: netread %d chars\r\n", ncc));
1073 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1077 * Something to read from the pty...
1079 if (FD_ISSET(p, &ibits)) {
1080 pcc = read(p, ptyibuf, BUFSIZ);
1082 * On some systems, if we try to read something
1083 * off the master side before the slave side is
1084 * opened, we get EIO.
1086 if (pcc < 0 && (errno == EWOULDBLOCK ||
1097 * If ioctl from pty, pass it through net
1099 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1100 copy_termbuf(ptyibuf+1, pcc-1);
1104 #endif /* LINEMODE */
1105 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1106 netclear(); /* clear buffer back */
1109 * There are client telnets on some
1110 * operating systems get screwed up
1111 * royally if we send them urgent
1114 output_data("%c%c", IAC, DM);
1115 neturg = nfrontp-1; /* off by one XXX */
1117 printoption("td: send IAC", DM));
1121 if (his_state_is_will(TELOPT_LFLOW) &&
1123 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1125 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1126 if (newflow != flowmode) {
1128 output_data("%c%c%c%c%c%c",
1129 IAC, SB, TELOPT_LFLOW,
1133 DIAG(TD_OPTIONS, printsub('>',
1134 (unsigned char *)nfrontp-4,
1144 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1146 c = *ptyip++ & 0377, pcc--;
1148 output_data("%c", c);
1149 output_data("%c", c);
1150 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1151 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1152 output_data("%c", *ptyip++ & 0377);
1155 output_data("%c", '\0');
1159 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1163 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1167 } /* end of telnet */
1171 # define TCSIG TIOCSIG
1176 * Send interrupt to process on other side of pty.
1177 * If it is in raw mode, just write NULL;
1178 * otherwise, write intr char.
1183 ptyflush(); /* half-hearted */
1186 (void) ioctl(pty, TCSIG, SIGINT);
1189 *pfrontp++ = slctab[SLC_IP].sptr ?
1190 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1195 * Send quit to process on other side of pty.
1196 * If it is in raw mode, just write NULL;
1197 * otherwise, write quit char.
1202 ptyflush(); /* half-hearted */
1204 (void) ioctl(pty, TCSIG, SIGQUIT);
1207 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1208 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1216 ptyflush(); /* half-hearted */
1218 (void) ioctl(pty, TCSIG, SIGTSTP);
1220 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1221 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1223 #endif /* SIGTSTP */
1227 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1228 * just send back "[Yes]".
1233 #if defined(SIGINFO) && defined(TCSIG)
1234 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1235 (void) ioctl(pty, TCSIG, SIGINFO);
1239 output_data("\r\n[Yes]\r\n");
1247 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1248 if (!tty_isediting()) {
1249 extern char oldeofc;
1250 *pfrontp++ = oldeofc;
1254 *pfrontp++ = slctab[SLC_EOF].sptr ?
1255 (unsigned char)*slctab[SLC_EOF].sptr : '\004';