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
36 static const char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include "pathnames.h"
52 #include <arpa/inet.h>
55 #include <libtelnet/auth.h>
59 #include <libtelnet/encrypt.h>
61 #include <libtelnet/misc.h>
63 char remote_hostname[MAXHOSTNAMELEN];
64 size_t utmp_len = sizeof(remote_hostname) - 1;
65 int registerd_host_only = 0;
70 * pointers, and counters.
72 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
73 char ptyibuf2[BUFSIZ];
75 int readstream(int, char *, int);
76 void doit(struct sockaddr *);
77 int terminaltypeok(char *);
79 int hostinfo = 1; /* do we print login banner? */
85 void doit(struct sockaddr *);
86 int terminaltypeok(char *);
87 void startslave(char *, int, char *);
88 extern void usage(void);
89 static void _gettermname(void);
92 * The string to pass to getopt(). We do it this way so
93 * that only the actual options that we support will be
94 * passed off to getopt().
97 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
117 int family = AF_INET;
119 #ifndef MAXHOSTNAMELEN
120 #define MAXHOSTNAMELEN 256
121 #endif /* MAXHOSTNAMELEN */
124 char host_name[MAXHOSTNAMELEN];
126 extern void telnet(int, int, char *);
132 main(int argc, char *argv[])
135 struct sockaddr_storage from;
138 #if defined(IPPROTO_IP) && defined(IP_TOS)
143 pfrontp = pbackp = ptyobuf;
145 nfrontp = nbackp = netobuf;
148 #endif /* ENCRYPTION */
151 * This initialization causes linemode to default to a configuration
152 * that works on all telnet clients, including the FreeBSD client.
153 * This is not quite the same as the telnet client issuing a "mode
154 * character" command, but has most of the same benefits, and is
155 * preferable since some clients (like usofts) don't have the
156 * mode character command anyway and linemode breaks things.
157 * The most notable symptom of fix is that csh "set filec" operations
158 * like <ESC> (filename completion) and ^D (choices) keys now work
159 * in telnet sessions and can be used more than once on the same line.
160 * CR/LF handling is also corrected in some termio modes. This
161 * change resolves problem reports bin/771 and bin/1037.
164 linemode=1; /*Default to mode that works on bulk of clients*/
166 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
169 #ifdef AUTHENTICATION
172 * Check for required authentication level
174 if (strcmp(optarg, "debug") == 0) {
175 extern int auth_debug_mode;
177 } else if (strcasecmp(optarg, "none") == 0) {
179 } else if (strcasecmp(optarg, "other") == 0) {
180 auth_level = AUTH_OTHER;
181 } else if (strcasecmp(optarg, "user") == 0) {
182 auth_level = AUTH_USER;
183 } else if (strcasecmp(optarg, "valid") == 0) {
184 auth_level = AUTH_VALID;
185 } else if (strcasecmp(optarg, "off") == 0) {
187 * This hack turns off authentication
191 warnx("unknown authorization level for -a");
194 #endif /* AUTHENTICATION */
200 #endif /* BFTPDAEMON */
203 if (strcmp(optarg, "ebug") == 0) {
214 * Check for desired diagnostics capabilities.
216 if (!strcmp(optarg, "report")) {
217 diagnostic |= TD_REPORT|TD_OPTIONS;
218 } else if (!strcmp(optarg, "exercise")) {
219 diagnostic |= TD_EXERCISE;
220 } else if (!strcmp(optarg, "netdata")) {
221 diagnostic |= TD_NETDATA;
222 } else if (!strcmp(optarg, "ptydata")) {
223 diagnostic |= TD_PTYDATA;
224 } else if (!strcmp(optarg, "options")) {
225 diagnostic |= TD_OPTIONS;
231 #endif /* DIAGNOSTICS */
235 if (strcmp(optarg, "debug") == 0) {
236 extern int encrypt_debug_mode;
237 encrypt_debug_mode = 1;
243 #endif /* ENCRYPTION */
253 #endif /* LINEMODE */
256 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
257 lmodetype = NO_AUTOKLUDGE;
259 /* ignore -k option if built without kludge linemode */
260 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
273 if ((tos = parsetos(optarg, "tcp")) < 0)
275 "bad TOS argument '", optarg,
276 "'; will try to use default TOS");
279 ultmp = strtoul(optarg, &ep, 0);
280 if (*ep || ep == optarg || ultmp > MAXTOS)
282 "bad TOS argument '", optarg,
283 "'; will try to use default TOS");
290 utmp_len = (size_t)atoi(optarg);
291 if (utmp_len >= sizeof(remote_hostname))
292 utmp_len = sizeof(remote_hostname) - 1;
296 registerd_host_only = 1;
299 #ifdef AUTHENTICATION
302 * Check for invalid authentication types
304 auth_disable_name(optarg);
306 #endif /* AUTHENTICATION */
319 warnx("%c: unknown option", ch);
331 int s, ns, foo, error;
332 const char *service = "telnet";
333 struct addrinfo hints, *res;
338 } else if (argc == 1)
341 memset(&hints, 0, sizeof(hints));
342 hints.ai_flags = AI_PASSIVE;
343 hints.ai_family = family;
344 hints.ai_socktype = SOCK_STREAM;
345 hints.ai_protocol = 0;
346 error = getaddrinfo(NULL, service, &hints, &res);
349 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
350 if (error == EAI_SYSTEM)
351 errx(1, "tcp/%s: %s\n", service, strerror(errno));
355 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
358 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
359 (char *)&on, sizeof(on));
361 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
362 (char *)&on, sizeof(on));
363 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
365 if (listen(s, 1) < 0)
367 foo = res->ai_addrlen;
368 ns = accept(s, res->ai_addr, &foo);
371 (void) setsockopt(ns, SOL_SOCKET, SO_DEBUG,
372 (char *)&on, sizeof(on));
377 } else if (argc == 1) {
378 ; /* VOID*/ /* Just ignore the host/port name */
380 } else if (argc > 0) {
385 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
386 fromlen = sizeof (from);
387 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
392 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
393 (char *)&on, sizeof (on)) < 0) {
394 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
397 #if defined(IPPROTO_IP) && defined(IP_TOS)
398 if (from.ss_family == AF_INET) {
399 # if defined(HAS_GETTOS)
401 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
405 tos = 020; /* Low Delay bit */
407 && (setsockopt(0, IPPROTO_IP, IP_TOS,
408 (char *)&tos, sizeof(tos)) < 0)
409 && (errno != ENOPROTOOPT) )
410 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
412 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
414 doit((struct sockaddr *)&from);
422 fprintf(stderr, "usage: telnetd");
423 #ifdef AUTHENTICATION
425 " [-4] [-6] [-a (debug|other|user|valid|off|none)]\n\t");
428 fprintf(stderr, " [-B]");
430 fprintf(stderr, " [-debug]");
432 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
434 #ifdef AUTHENTICATION
435 fprintf(stderr, " [-edebug]");
437 fprintf(stderr, " [-h]");
438 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
439 fprintf(stderr, " [-k]");
442 fprintf(stderr, " [-l]");
444 fprintf(stderr, " [-n]");
445 fprintf(stderr, "\n\t");
447 fprintf(stderr, " [-S tos]");
449 #ifdef AUTHENTICATION
450 fprintf(stderr, " [-X auth-type]");
452 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
453 fprintf(stderr, " [port]\n");
460 * Ask the other end to send along its terminal type and speed.
461 * Output is the variable terminaltype filled in.
463 static unsigned char ttytype_sbbuf[] = {
464 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
468 #ifndef AUTHENTICATION
469 #define undef2 __unused
475 getterminaltype(char *name undef2)
480 #ifdef AUTHENTICATION
482 * Handle the Authentication option before we do anything else.
484 send_do(TELOPT_AUTHENTICATION, 1);
485 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
487 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
488 retval = auth_wait(name);
493 send_will(TELOPT_ENCRYPT, 1);
494 #endif /* ENCRYPTION */
495 send_do(TELOPT_TTYPE, 1);
496 send_do(TELOPT_TSPEED, 1);
497 send_do(TELOPT_XDISPLOC, 1);
498 send_do(TELOPT_NEW_ENVIRON, 1);
499 send_do(TELOPT_OLD_ENVIRON, 1);
502 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
503 #endif /* ENCRYPTION */
504 his_will_wont_is_changing(TELOPT_TTYPE) ||
505 his_will_wont_is_changing(TELOPT_TSPEED) ||
506 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
507 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
508 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
513 * Wait for the negotiation of what type of encryption we can
514 * send with. If autoencrypt is not set, this will just return.
516 if (his_state_is_will(TELOPT_ENCRYPT)) {
519 #endif /* ENCRYPTION */
520 if (his_state_is_will(TELOPT_TSPEED)) {
521 static unsigned char sb[] =
522 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
524 output_datalen(sb, sizeof sb);
525 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
527 if (his_state_is_will(TELOPT_XDISPLOC)) {
528 static unsigned char sb[] =
529 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
531 output_datalen(sb, sizeof sb);
532 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
534 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
535 static unsigned char sb[] =
536 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
538 output_datalen(sb, sizeof sb);
539 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
541 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
542 static unsigned char sb[] =
543 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
545 output_datalen(sb, sizeof sb);
546 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
548 if (his_state_is_will(TELOPT_TTYPE)) {
550 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
551 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
552 sizeof ttytype_sbbuf - 2););
554 if (his_state_is_will(TELOPT_TSPEED)) {
555 while (sequenceIs(tspeedsubopt, baseline))
558 if (his_state_is_will(TELOPT_XDISPLOC)) {
559 while (sequenceIs(xdisplocsubopt, baseline))
562 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
563 while (sequenceIs(environsubopt, baseline))
566 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
567 while (sequenceIs(oenvironsubopt, baseline))
570 if (his_state_is_will(TELOPT_TTYPE)) {
571 char first[256], last[256];
573 while (sequenceIs(ttypesubopt, baseline))
577 * If the other side has already disabled the option, then
578 * we have to just go with what we (might) have already gotten.
580 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
581 (void) strncpy(first, terminaltype, sizeof(first)-1);
582 first[sizeof(first)-1] = '\0';
585 * Save the unknown name, and request the next name.
587 (void) strncpy(last, terminaltype, sizeof(last)-1);
588 last[sizeof(last)-1] = '\0';
590 if (terminaltypeok(terminaltype))
592 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
593 his_state_is_wont(TELOPT_TTYPE)) {
595 * We've hit the end. If this is the same as
596 * the first name, just go with it.
598 if (strncmp(first, terminaltype, sizeof(first)) == 0)
601 * Get the terminal name one more time, so that
602 * RFC1091 compliant telnets will cycle back to
603 * the start of the list.
606 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
607 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
608 terminaltype[sizeof(terminaltype)-1] = '\0';
616 } /* end of getterminaltype */
622 * If the client turned off the option,
623 * we can't send another request, so we
626 if (his_state_is_wont(TELOPT_TTYPE))
629 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
630 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
631 sizeof ttytype_sbbuf - 2););
632 while (sequenceIs(ttypesubopt, baseline))
637 terminaltypeok(char *s)
641 if (terminaltype == NULL)
645 * tgetent() will return 1 if the type is known, and
646 * 0 if it is not known. If it returns -1, it couldn't
647 * open the database. But if we can't open the database,
648 * it won't help to say we failed, because we won't be
649 * able to verify anything else. So, we treat -1 like 1.
651 if (tgetent(buf, s) == 0)
657 * Get a pty, scan input lines.
660 doit(struct sockaddr *who)
666 * Find an available pty to use.
669 pty = getpty(&ptynum);
671 fatal(net, "All network ports in use");
676 if ((lp = getpty()) == NULL)
677 fatal(net, "Out of ptys");
679 if ((pty = open(lp, 2)) >= 0) {
680 strlcpy(line,lp,sizeof(line));
687 /* get name of connected client */
688 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
689 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
690 fatal(net, "Couldn't resolve your address into a host name.\r\n\
691 Please contact your net administrator");
692 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
694 trimdomain(remote_hostname, UT_HOSTSIZE);
695 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
696 err_ = getnameinfo(who, who->sa_len, remote_hostname,
697 sizeof(remote_hostname), NULL, 0,
699 /* XXX: do 'err_' check */
701 (void) gethostname(host_name, sizeof(host_name) - 1);
702 host_name[sizeof(host_name) - 1] = '\0';
703 hostname = host_name;
705 #ifdef AUTHENTICATION
707 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
708 * This is a byproduct of needing "#ifdef" and not "#if defined()"
709 * for unifdef. XXX MarkM
711 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
720 level = getterminaltype(user_name);
721 setenv("TERM", terminaltype ? terminaltype : "network", 1);
723 telnet(net, pty, remote_hostname); /* begin server process */
729 * Main loop. Select from pty and network, and
730 * hand data to telnet receiver finite state machine.
733 telnet(int f, int p, char *host)
736 #define TABBUFSIZ 512
737 char defent[TABBUFSIZ];
738 char defstrs[TABBUFSIZ];
746 * Initialize the slc mapping table.
751 * Do some tests where it is desireable to wait for a response.
752 * Rather than doing them slowly, one at a time, do them all
755 if (my_state_is_wont(TELOPT_SGA))
756 send_will(TELOPT_SGA, 1);
758 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
759 * because 4.2 clients are unable to deal with TCP urgent data.
761 * To find out, we send out a "DO ECHO". If the remote system
762 * answers "WILL ECHO" it is probably a 4.2 client, and we note
763 * that fact ("WILL ECHO" ==> that the client will echo what
764 * WE, the server, sends it; it does NOT mean that the client will
765 * echo the terminal input).
767 send_do(TELOPT_ECHO, 1);
770 if (his_state_is_wont(TELOPT_LINEMODE)) {
771 /* Query the peer for linemode support by trying to negotiate
772 * the linemode option.
776 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
778 #endif /* LINEMODE */
781 * Send along a couple of other options that we wish to negotiate.
783 send_do(TELOPT_NAWS, 1);
784 send_will(TELOPT_STATUS, 1);
785 flowmode = 1; /* default flow control state */
786 restartany = -1; /* uninitialized... */
787 send_do(TELOPT_LFLOW, 1);
790 * Spin, waiting for a response from the DO ECHO. However,
791 * some REALLY DUMB telnets out there might not respond
792 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
793 * telnets so far seem to respond with WONT for a DO that
794 * they don't understand...) because by the time we get the
795 * response, it will already have processed the DO ECHO.
796 * Kludge upon kludge.
798 while (his_will_wont_is_changing(TELOPT_NAWS))
803 * The client might have sent a WILL NAWS as part of its
804 * startup code; if so, we'll be here before we get the
805 * response to the DO ECHO. We'll make the assumption
806 * that any implementation that understands about NAWS
807 * is a modern enough implementation that it will respond
808 * to our DO ECHO request; hence we'll do another spin
809 * waiting for the ECHO option to settle down, which is
810 * what we wanted to do in the first place...
812 if (his_want_state_is_will(TELOPT_ECHO) &&
813 his_state_is_will(TELOPT_NAWS)) {
814 while (his_will_wont_is_changing(TELOPT_ECHO))
818 * On the off chance that the telnet client is broken and does not
819 * respond to the DO ECHO we sent, (after all, we did send the
820 * DO NAWS negotiation after the DO ECHO, and we won't get here
821 * until a response to the DO NAWS comes back) simulate the
822 * receipt of a will echo. This will also send a WONT ECHO
823 * to the client, since we assume that the client failed to
824 * respond because it believes that it is already in DO ECHO
825 * mode, which we do not want.
827 if (his_want_state_is_will(TELOPT_ECHO)) {
828 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
829 willoption(TELOPT_ECHO);
833 * Finally, to clean things up, we turn on our echo. This
834 * will break stupid 4.2 telnets out of local terminal echo.
837 if (my_state_is_wont(TELOPT_ECHO))
838 send_will(TELOPT_ECHO, 1);
841 * Turn on packet mode
843 (void) ioctl(p, TIOCPKT, (char *)&on);
845 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
847 * Continuing line mode support. If client does not support
848 * real linemode, attempt to negotiate kludge linemode by sending
849 * the do timing mark sequence.
851 if (lmodetype < REAL_LINEMODE)
852 send_do(TELOPT_TM, 1);
853 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
856 * Call telrcv() once to pick up anything received during
857 * terminal type negotiation, 4.2/4.3 determination, and
858 * linemode negotiation.
862 (void) ioctl(f, FIONBIO, (char *)&on);
863 (void) ioctl(p, FIONBIO, (char *)&on);
865 #if defined(SO_OOBINLINE)
866 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
867 (char *)&on, sizeof on);
868 #endif /* defined(SO_OOBINLINE) */
871 (void) signal(SIGTSTP, SIG_IGN);
875 * Ignoring SIGTTOU keeps the kernel from blocking us
876 * in ttioct() in /sys/tty.c.
878 (void) signal(SIGTTOU, SIG_IGN);
881 (void) signal(SIGCHLD, cleanup);
886 t = open(_PATH_TTY, O_RDWR);
888 (void) ioctl(t, TIOCNOTTY, (char *)0);
895 * Show banner that getty never gave.
897 * We put the banner in the pty input buffer. This way, it
898 * gets carriage return null processing, etc., just like all
899 * other pty --> client data.
902 if (getent(defent, "default") == 1) {
905 HE = Getstr("he", &cp);
906 HN = Getstr("hn", &cp);
907 IM = Getstr("im", &cp);
909 (void) strlcpy(host_name, HN, sizeof(host_name));
913 IM = strdup(DEFAULT_IM);
916 edithost(HE, host_name);
921 (void) strncat(ptyibuf2, ptyip, pcc+1);
926 * Last check to make sure all our states are correct.
930 #endif /* LINEMODE */
932 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
935 * Startup the login process on the slave side of the terminal
936 * now. We delay this until here to insure option negotiation
939 startslave(host, level, user_name);
941 nfd = ((f > p) ? f : p) + 1;
943 fd_set ibits, obits, xbits;
946 if (ncc < 0 && pcc < 0)
953 * Never look for input if there's still
954 * stuff in the corresponding output buffer
956 if (nfrontp - nbackp || pcc > 0) {
961 if (pfrontp - pbackp || ncc > 0) {
969 if ((c = select(nfd, &ibits, &obits, &xbits,
970 (struct timeval *)0)) < 1) {
972 if (errno == EINTR) {
983 if (FD_ISSET(net, &xbits)) {
988 * Something to read from the network...
990 if (FD_ISSET(net, &ibits)) {
991 #if !defined(SO_OOBINLINE)
993 * In 4.2 (and 4.3 beta) systems, the
994 * OOB indication and data handling in the kernel
995 * is such that if two separate TCP Urgent requests
996 * come in, one byte of TCP data will be overlaid.
997 * This is fatal for Telnet, but we try to live
1000 * In addition, in 4.2 (and...), a special protocol
1001 * is needed to pick up the TCP Urgent data in
1002 * the correct sequence.
1004 * What we do is: if we think we are in urgent
1005 * mode, we look to see if we are "at the mark".
1006 * If we are, we do an OOB receive. If we run
1007 * this twice, we will do the OOB receive twice,
1008 * but the second will fail, since the second
1009 * time we were "at the mark", but there wasn't
1010 * any data there (the kernel doesn't reset
1011 * "at the mark" until we do a normal read).
1012 * Once we've read the OOB data, we go ahead
1013 * and do normal reads.
1015 * There is also another problem, which is that
1016 * since the OOB byte we read doesn't put us
1017 * out of OOB state, and since that byte is most
1018 * likely the TELNET DM (data mark), we would
1019 * stay in the TELNET SYNCH (SYNCHing) state.
1020 * So, clocks to the rescue. If we've "just"
1021 * received a DM, then we test for the
1022 * presence of OOB data when the receive OOB
1023 * fails (and AFTER we did the normal mode read
1024 * to clear "at the mark").
1029 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1031 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1032 if ((ncc == -1) && (errno == EINVAL)) {
1033 ncc = read(net, netibuf, sizeof (netibuf));
1034 if (sequenceIs(didnetreceive, gotDM)) {
1035 SYNCHing = stilloob(net);
1039 ncc = read(net, netibuf, sizeof (netibuf));
1042 ncc = read(net, netibuf, sizeof (netibuf));
1044 settimer(didnetreceive);
1045 #else /* !defined(SO_OOBINLINE)) */
1046 ncc = read(net, netibuf, sizeof (netibuf));
1047 #endif /* !defined(SO_OOBINLINE)) */
1048 if (ncc < 0 && errno == EWOULDBLOCK)
1056 DIAG((TD_REPORT | TD_NETDATA),
1057 output_data("td: netread %d chars\r\n", ncc));
1058 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1062 * Something to read from the pty...
1064 if (FD_ISSET(p, &ibits)) {
1065 pcc = read(p, ptyibuf, BUFSIZ);
1067 * On some systems, if we try to read something
1068 * off the master side before the slave side is
1069 * opened, we get EIO.
1071 if (pcc < 0 && (errno == EWOULDBLOCK ||
1082 * If ioctl from pty, pass it through net
1084 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1085 copy_termbuf(ptyibuf+1, pcc-1);
1089 #endif /* LINEMODE */
1090 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1091 netclear(); /* clear buffer back */
1094 * There are client telnets on some
1095 * operating systems get screwed up
1096 * royally if we send them urgent
1099 output_data("%c%c", IAC, DM);
1100 neturg = nfrontp-1; /* off by one XXX */
1102 printoption("td: send IAC", DM));
1106 if (his_state_is_will(TELOPT_LFLOW) &&
1108 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1110 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1111 if (newflow != flowmode) {
1113 output_data("%c%c%c%c%c%c",
1114 IAC, SB, TELOPT_LFLOW,
1118 DIAG(TD_OPTIONS, printsub('>',
1119 (unsigned char *)nfrontp-4,
1129 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1131 c = *ptyip++ & 0377, pcc--;
1133 output_data("%c", c);
1134 output_data("%c", c);
1135 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1136 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1137 output_data("%c", *ptyip++ & 0377);
1140 output_data("%c", '\0');
1144 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1148 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1152 } /* end of telnet */
1156 # define TCSIG TIOCSIG
1161 * Send interrupt to process on other side of pty.
1162 * If it is in raw mode, just write NULL;
1163 * otherwise, write intr char.
1168 ptyflush(); /* half-hearted */
1171 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1174 *pfrontp++ = slctab[SLC_IP].sptr ?
1175 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1180 * Send quit to process on other side of pty.
1181 * If it is in raw mode, just write NULL;
1182 * otherwise, write quit char.
1187 ptyflush(); /* half-hearted */
1189 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1192 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1193 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1201 ptyflush(); /* half-hearted */
1203 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1205 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1206 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1208 #endif /* SIGTSTP */
1212 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1213 * just send back "[Yes]".
1218 #if defined(SIGINFO) && defined(TCSIG)
1219 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1220 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1224 output_data("\r\n[Yes]\r\n");
1232 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1233 if (!tty_isediting()) {
1234 extern char oldeofc;
1235 *pfrontp++ = oldeofc;
1239 *pfrontp++ = slctab[SLC_EOF].sptr ?
1240 (unsigned char)*slctab[SLC_EOF].sptr : '\004';