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
29 * @(#)telnetd.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnetd/telnetd.c,v 1.11.2.5 2002/04/13 10:59:09 markm Exp $
34 #include "pathnames.h"
43 #include <arpa/inet.h>
46 #include <libtelnet/auth.h>
50 #include <libtelnet/encrypt.h>
52 #include <libtelnet/misc.h>
54 char remote_hostname[MAXHOSTNAMELEN];
55 size_t utmp_len = sizeof(remote_hostname) - 1;
56 int registerd_host_only = 0;
61 * pointers, and counters.
63 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
64 char ptyibuf2[BUFSIZ];
66 int readstream(int, char *, int);
67 void doit(struct sockaddr *);
68 int terminaltypeok(char *);
70 int hostinfo = 1; /* do we print login banner? */
76 void doit(struct sockaddr *);
77 int terminaltypeok(char *);
78 void startslave(char *, int, char *);
79 extern void usage(void);
80 static void _gettermname(void);
83 * The string to pass to getopt(). We do it this way so
84 * that only the actual options that we support will be
85 * passed off to getopt().
88 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
108 int family = AF_INET;
110 #ifndef MAXHOSTNAMELEN
111 #define MAXHOSTNAMELEN 256
112 #endif /* MAXHOSTNAMELEN */
115 char host_name[MAXHOSTNAMELEN];
117 extern void telnet(int, int, char *);
123 main(int argc, char *argv[])
125 struct sockaddr_storage from;
128 #if defined(IPPROTO_IP) && defined(IP_TOS)
132 pfrontp = pbackp = ptyobuf;
134 nfrontp = nbackp = netobuf;
137 #endif /* ENCRYPTION */
140 * This initialization causes linemode to default to a configuration
141 * that works on all telnet clients, including the FreeBSD client.
142 * This is not quite the same as the telnet client issuing a "mode
143 * character" command, but has most of the same benefits, and is
144 * preferable since some clients (like usofts) don't have the
145 * mode character command anyway and linemode breaks things.
146 * The most notable symptom of fix is that csh "set filec" operations
147 * like <ESC> (filename completion) and ^D (choices) keys now work
148 * in telnet sessions and can be used more than once on the same line.
149 * CR/LF handling is also corrected in some termio modes. This
150 * change resolves problem reports bin/771 and bin/1037.
153 linemode=1; /*Default to mode that works on bulk of clients*/
155 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
158 #ifdef AUTHENTICATION
161 * Check for required authentication level
163 if (strcmp(optarg, "debug") == 0) {
164 extern int auth_debug_mode;
166 } else if (strcasecmp(optarg, "none") == 0) {
168 } else if (strcasecmp(optarg, "other") == 0) {
169 auth_level = AUTH_OTHER;
170 } else if (strcasecmp(optarg, "user") == 0) {
171 auth_level = AUTH_USER;
172 } else if (strcasecmp(optarg, "valid") == 0) {
173 auth_level = AUTH_VALID;
174 } else if (strcasecmp(optarg, "off") == 0) {
176 * This hack turns off authentication
180 warnx("unknown authorization level for -a");
183 #endif /* AUTHENTICATION */
189 #endif /* BFTPDAEMON */
192 if (strcmp(optarg, "ebug") == 0) {
203 * Check for desired diagnostics capabilities.
205 if (!strcmp(optarg, "report")) {
206 diagnostic |= TD_REPORT|TD_OPTIONS;
207 } else if (!strcmp(optarg, "exercise")) {
208 diagnostic |= TD_EXERCISE;
209 } else if (!strcmp(optarg, "netdata")) {
210 diagnostic |= TD_NETDATA;
211 } else if (!strcmp(optarg, "ptydata")) {
212 diagnostic |= TD_PTYDATA;
213 } else if (!strcmp(optarg, "options")) {
214 diagnostic |= TD_OPTIONS;
220 #endif /* DIAGNOSTICS */
224 if (strcmp(optarg, "debug") == 0) {
225 extern int encrypt_debug_mode;
226 encrypt_debug_mode = 1;
232 #endif /* ENCRYPTION */
242 #endif /* LINEMODE */
245 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
246 lmodetype = NO_AUTOKLUDGE;
248 /* ignore -k option if built without kludge linemode */
249 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
262 if ((tos = parsetos(optarg, "tcp")) < 0)
264 "bad TOS argument '", optarg,
265 "'; will try to use default TOS");
267 warnx("TOS option unavailable; -S flag not supported");
272 utmp_len = (size_t)atoi(optarg);
273 if (utmp_len >= sizeof(remote_hostname))
274 utmp_len = sizeof(remote_hostname) - 1;
278 registerd_host_only = 1;
281 #ifdef AUTHENTICATION
284 * Check for invalid authentication types
286 auth_disable_name(optarg);
288 #endif /* AUTHENTICATION */
301 warnx("%c: unknown option", ch);
313 int s, ns, foo, error;
314 const char *service = "telnet";
315 struct addrinfo hints, *res;
320 } else if (argc == 1)
323 memset(&hints, 0, sizeof(hints));
324 hints.ai_flags = AI_PASSIVE;
325 hints.ai_family = family;
326 hints.ai_socktype = SOCK_STREAM;
327 hints.ai_protocol = 0;
328 error = getaddrinfo(NULL, service, &hints, &res);
331 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
332 if (error == EAI_SYSTEM)
333 errx(1, "tcp/%s: %s\n", service, strerror(errno));
337 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
340 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
341 (char *)&on, sizeof(on));
342 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
344 if (listen(s, 1) < 0)
346 foo = res->ai_addrlen;
347 ns = accept(s, res->ai_addr, &foo);
354 } else if (argc == 1) {
355 ; /* VOID*/ /* Just ignore the host/port name */
357 } else if (argc > 0) {
362 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
363 fromlen = sizeof (from);
364 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
369 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
370 (char *)&on, sizeof (on)) < 0) {
371 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
374 #if defined(IPPROTO_IP) && defined(IP_TOS)
375 if (from.ss_family == AF_INET) {
376 # if defined(HAS_GETTOS)
378 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
382 tos = 020; /* Low Delay bit */
384 && (setsockopt(0, IPPROTO_IP, IP_TOS,
385 (char *)&tos, sizeof(tos)) < 0)
386 && (errno != ENOPROTOOPT) )
387 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
389 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
391 doit((struct sockaddr *)&from);
399 fprintf(stderr, "usage: telnetd");
400 #ifdef AUTHENTICATION
401 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
404 fprintf(stderr, " [-B]");
406 fprintf(stderr, " [-debug]");
408 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
410 #ifdef AUTHENTICATION
411 fprintf(stderr, " [-edebug]");
413 fprintf(stderr, " [-h]");
414 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
415 fprintf(stderr, " [-k]");
418 fprintf(stderr, " [-l]");
420 fprintf(stderr, " [-n]");
421 fprintf(stderr, "\n\t");
423 fprintf(stderr, " [-S tos]");
425 #ifdef AUTHENTICATION
426 fprintf(stderr, " [-X auth-type]");
428 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
429 fprintf(stderr, " [port]\n");
436 * Ask the other end to send along its terminal type and speed.
437 * Output is the variable terminaltype filled in.
439 static unsigned char ttytype_sbbuf[] = {
440 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
444 #ifndef AUTHENTICATION
445 #define undef2 __unused
451 getterminaltype(char *name undef2)
456 #ifdef AUTHENTICATION
458 * Handle the Authentication option before we do anything else.
460 send_do(TELOPT_AUTHENTICATION, 1);
461 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
463 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
464 retval = auth_wait(name);
469 send_will(TELOPT_ENCRYPT, 1);
470 #endif /* ENCRYPTION */
471 send_do(TELOPT_TTYPE, 1);
472 send_do(TELOPT_TSPEED, 1);
473 send_do(TELOPT_XDISPLOC, 1);
474 send_do(TELOPT_NEW_ENVIRON, 1);
475 send_do(TELOPT_OLD_ENVIRON, 1);
478 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
479 #endif /* ENCRYPTION */
480 his_will_wont_is_changing(TELOPT_TTYPE) ||
481 his_will_wont_is_changing(TELOPT_TSPEED) ||
482 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
483 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
484 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
489 * Wait for the negotiation of what type of encryption we can
490 * send with. If autoencrypt is not set, this will just return.
492 if (his_state_is_will(TELOPT_ENCRYPT)) {
495 #endif /* ENCRYPTION */
496 if (his_state_is_will(TELOPT_TSPEED)) {
497 static unsigned char sb[] =
498 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
500 output_datalen(sb, sizeof sb);
501 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
503 if (his_state_is_will(TELOPT_XDISPLOC)) {
504 static unsigned char sb[] =
505 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
507 output_datalen(sb, sizeof sb);
508 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
510 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
511 static unsigned char sb[] =
512 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
514 output_datalen(sb, sizeof sb);
515 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
517 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
518 static unsigned char sb[] =
519 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
521 output_datalen(sb, sizeof sb);
522 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
524 if (his_state_is_will(TELOPT_TTYPE)) {
526 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
527 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
528 sizeof ttytype_sbbuf - 2););
530 if (his_state_is_will(TELOPT_TSPEED)) {
531 while (sequenceIs(tspeedsubopt, baseline))
534 if (his_state_is_will(TELOPT_XDISPLOC)) {
535 while (sequenceIs(xdisplocsubopt, baseline))
538 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
539 while (sequenceIs(environsubopt, baseline))
542 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
543 while (sequenceIs(oenvironsubopt, baseline))
546 if (his_state_is_will(TELOPT_TTYPE)) {
547 char first[256], last[256];
549 while (sequenceIs(ttypesubopt, baseline))
553 * If the other side has already disabled the option, then
554 * we have to just go with what we (might) have already gotten.
556 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
557 (void) strncpy(first, terminaltype, sizeof(first)-1);
558 first[sizeof(first)-1] = '\0';
561 * Save the unknown name, and request the next name.
563 (void) strncpy(last, terminaltype, sizeof(last)-1);
564 last[sizeof(last)-1] = '\0';
566 if (terminaltypeok(terminaltype))
568 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
569 his_state_is_wont(TELOPT_TTYPE)) {
571 * We've hit the end. If this is the same as
572 * the first name, just go with it.
574 if (strncmp(first, terminaltype, sizeof(first)) == 0)
577 * Get the terminal name one more time, so that
578 * RFC1091 compliant telnets will cycle back to
579 * the start of the list.
582 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
583 (void) strncpy(terminaltype, first, TERMINAL_TYPE_SIZE-1);
584 terminaltype[TERMINAL_TYPE_SIZE-1] = '\0';
592 } /* end of getterminaltype */
598 * If the client turned off the option,
599 * we can't send another request, so we
602 if (his_state_is_wont(TELOPT_TTYPE))
605 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
606 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
607 sizeof ttytype_sbbuf - 2););
608 while (sequenceIs(ttypesubopt, baseline))
613 terminaltypeok(char *s)
617 if (terminaltype == NULL)
621 * tgetent() will return 1 if the type is known, and
622 * 0 if it is not known. If it returns -1, it couldn't
623 * open the database. But if we can't open the database,
624 * it won't help to say we failed, because we won't be
625 * able to verify anything else. So, we treat -1 like 1.
627 if (tgetent(buf, s) == 0)
633 * Get a pty, scan input lines.
636 doit(struct sockaddr *who)
642 * Find an available pty to use.
645 pty = getpty(&ptynum);
647 fatal(net, "All network ports in use");
652 if ((lp = getpty()) == NULL)
653 fatal(net, "Out of ptys");
655 if ((pty = open(lp, 2)) >= 0) {
656 strlcpy(line,lp,sizeof(line));
663 /* get name of connected client */
664 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
665 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
666 fatal(net, "Couldn't resolve your address into a host name.\r\n\
667 Please contact your net administrator");
668 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
670 trimdomain(remote_hostname, UT_HOSTSIZE);
671 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
672 err_ = getnameinfo(who, who->sa_len, remote_hostname,
673 sizeof(remote_hostname), NULL, 0,
674 NI_NUMERICHOST|NI_WITHSCOPEID);
675 /* XXX: do 'err_' check */
677 (void) gethostname(host_name, sizeof(host_name) - 1);
678 host_name[sizeof(host_name) - 1] = '\0';
679 hostname = host_name;
681 #ifdef AUTHENTICATION
683 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
684 * This is a byproduct of needing "#ifdef" and not "#if defined()"
685 * for unifdef. XXX MarkM
687 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
696 level = getterminaltype(user_name);
697 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1)
698 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network");
700 telnet(net, pty, remote_hostname); /* begin server process */
706 * Main loop. Select from pty and network, and
707 * hand data to telnet receiver finite state machine.
710 telnet(int f, int p, char *host)
713 #define TABBUFSIZ 512
714 char defent[TABBUFSIZ];
715 char defstrs[TABBUFSIZ];
723 * Initialize the slc mapping table.
728 * Do some tests where it is desireable to wait for a response.
729 * Rather than doing them slowly, one at a time, do them all
732 if (my_state_is_wont(TELOPT_SGA))
733 send_will(TELOPT_SGA, 1);
735 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
736 * because 4.2 clients are unable to deal with TCP urgent data.
738 * To find out, we send out a "DO ECHO". If the remote system
739 * answers "WILL ECHO" it is probably a 4.2 client, and we note
740 * that fact ("WILL ECHO" ==> that the client will echo what
741 * WE, the server, sends it; it does NOT mean that the client will
742 * echo the terminal input).
744 send_do(TELOPT_ECHO, 1);
747 if (his_state_is_wont(TELOPT_LINEMODE)) {
748 /* Query the peer for linemode support by trying to negotiate
749 * the linemode option.
753 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
755 #endif /* LINEMODE */
758 * Send along a couple of other options that we wish to negotiate.
760 send_do(TELOPT_NAWS, 1);
761 send_will(TELOPT_STATUS, 1);
762 flowmode = 1; /* default flow control state */
763 restartany = -1; /* uninitialized... */
764 send_do(TELOPT_LFLOW, 1);
767 * Spin, waiting for a response from the DO ECHO. However,
768 * some REALLY DUMB telnets out there might not respond
769 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
770 * telnets so far seem to respond with WONT for a DO that
771 * they don't understand...) because by the time we get the
772 * response, it will already have processed the DO ECHO.
773 * Kludge upon kludge.
775 while (his_will_wont_is_changing(TELOPT_NAWS))
780 * The client might have sent a WILL NAWS as part of its
781 * startup code; if so, we'll be here before we get the
782 * response to the DO ECHO. We'll make the assumption
783 * that any implementation that understands about NAWS
784 * is a modern enough implementation that it will respond
785 * to our DO ECHO request; hence we'll do another spin
786 * waiting for the ECHO option to settle down, which is
787 * what we wanted to do in the first place...
789 if (his_want_state_is_will(TELOPT_ECHO) &&
790 his_state_is_will(TELOPT_NAWS)) {
791 while (his_will_wont_is_changing(TELOPT_ECHO))
795 * On the off chance that the telnet client is broken and does not
796 * respond to the DO ECHO we sent, (after all, we did send the
797 * DO NAWS negotiation after the DO ECHO, and we won't get here
798 * until a response to the DO NAWS comes back) simulate the
799 * receipt of a will echo. This will also send a WONT ECHO
800 * to the client, since we assume that the client failed to
801 * respond because it believes that it is already in DO ECHO
802 * mode, which we do not want.
804 if (his_want_state_is_will(TELOPT_ECHO)) {
805 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
806 willoption(TELOPT_ECHO);
810 * Finally, to clean things up, we turn on our echo. This
811 * will break stupid 4.2 telnets out of local terminal echo.
814 if (my_state_is_wont(TELOPT_ECHO))
815 send_will(TELOPT_ECHO, 1);
818 * Turn on packet mode
820 (void) ioctl(p, TIOCPKT, (char *)&on);
822 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
824 * Continuing line mode support. If client does not support
825 * real linemode, attempt to negotiate kludge linemode by sending
826 * the do timing mark sequence.
828 if (lmodetype < REAL_LINEMODE)
829 send_do(TELOPT_TM, 1);
830 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
833 * Call telrcv() once to pick up anything received during
834 * terminal type negotiation, 4.2/4.3 determination, and
835 * linemode negotiation.
839 (void) ioctl(f, FIONBIO, (char *)&on);
840 (void) ioctl(p, FIONBIO, (char *)&on);
842 #if defined(SO_OOBINLINE)
843 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
844 (char *)&on, sizeof on);
845 #endif /* defined(SO_OOBINLINE) */
848 (void) signal(SIGTSTP, SIG_IGN);
852 * Ignoring SIGTTOU keeps the kernel from blocking us
853 * in ttioct() in /sys/tty.c.
855 (void) signal(SIGTTOU, SIG_IGN);
858 (void) signal(SIGCHLD, cleanup);
863 t = open(_PATH_TTY, O_RDWR);
865 (void) ioctl(t, TIOCNOTTY, NULL);
872 * Show banner that getty never gave.
874 * We put the banner in the pty input buffer. This way, it
875 * gets carriage return null processing, etc., just like all
876 * other pty --> client data.
879 if (getent(defent, "default") == 1) {
882 HE = Getstr("he", &cp);
883 HN = Getstr("hn", &cp);
884 IM = Getstr("im", &cp);
886 (void) strlcpy(host_name, HN, sizeof(host_name));
890 IM = strdup(DEFAULT_IM);
893 edithost(HE, host_name);
898 (void) strncat(ptyibuf2, ptyip, pcc+1);
903 * Last check to make sure all our states are correct.
907 #endif /* LINEMODE */
909 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
912 * Startup the login process on the slave side of the terminal
913 * now. We delay this until here to insure option negotiation
916 startslave(host, level, user_name);
918 nfd = ((f > p) ? f : p) + 1;
920 fd_set ibits, obits, xbits;
923 if (ncc < 0 && pcc < 0)
930 * Never look for input if there's still
931 * stuff in the corresponding output buffer
933 if (nfrontp - nbackp || pcc > 0) {
938 if (pfrontp - pbackp || ncc > 0) {
946 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) {
948 if (errno == EINTR) {
959 if (FD_ISSET(net, &xbits)) {
964 * Something to read from the network...
966 if (FD_ISSET(net, &ibits)) {
967 #if !defined(SO_OOBINLINE)
969 * In 4.2 (and 4.3 beta) systems, the
970 * OOB indication and data handling in the kernel
971 * is such that if two separate TCP Urgent requests
972 * come in, one byte of TCP data will be overlaid.
973 * This is fatal for Telnet, but we try to live
976 * In addition, in 4.2 (and...), a special protocol
977 * is needed to pick up the TCP Urgent data in
978 * the correct sequence.
980 * What we do is: if we think we are in urgent
981 * mode, we look to see if we are "at the mark".
982 * If we are, we do an OOB receive. If we run
983 * this twice, we will do the OOB receive twice,
984 * but the second will fail, since the second
985 * time we were "at the mark", but there wasn't
986 * any data there (the kernel doesn't reset
987 * "at the mark" until we do a normal read).
988 * Once we've read the OOB data, we go ahead
989 * and do normal reads.
991 * There is also another problem, which is that
992 * since the OOB byte we read doesn't put us
993 * out of OOB state, and since that byte is most
994 * likely the TELNET DM (data mark), we would
995 * stay in the TELNET SYNCH (SYNCHing) state.
996 * So, clocks to the rescue. If we've "just"
997 * received a DM, then we test for the
998 * presence of OOB data when the receive OOB
999 * fails (and AFTER we did the normal mode read
1000 * to clear "at the mark").
1005 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1007 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1008 if ((ncc == -1) && (errno == EINVAL)) {
1009 ncc = read(net, netibuf, sizeof (netibuf));
1010 if (sequenceIs(didnetreceive, gotDM)) {
1011 SYNCHing = stilloob(net);
1015 ncc = read(net, netibuf, sizeof (netibuf));
1018 ncc = read(net, netibuf, sizeof (netibuf));
1020 settimer(didnetreceive);
1021 #else /* !defined(SO_OOBINLINE)) */
1022 ncc = read(net, netibuf, sizeof (netibuf));
1023 #endif /* !defined(SO_OOBINLINE)) */
1024 if (ncc < 0 && errno == EWOULDBLOCK)
1032 DIAG((TD_REPORT | TD_NETDATA),
1033 output_data("td: netread %d chars\r\n", ncc));
1034 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1038 * Something to read from the pty...
1040 if (FD_ISSET(p, &ibits)) {
1041 pcc = read(p, ptyibuf, BUFSIZ);
1043 * On some systems, if we try to read something
1044 * off the master side before the slave side is
1045 * opened, we get EIO.
1047 if (pcc < 0 && (errno == EWOULDBLOCK ||
1058 * If ioctl from pty, pass it through net
1060 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1061 copy_termbuf(ptyibuf+1, pcc-1);
1065 #endif /* LINEMODE */
1066 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1067 netclear(); /* clear buffer back */
1070 * There are client telnets on some
1071 * operating systems get screwed up
1072 * royally if we send them urgent
1075 output_data("%c%c", IAC, DM);
1076 neturg = nfrontp-1; /* off by one XXX */
1078 printoption("td: send IAC", DM));
1082 if (his_state_is_will(TELOPT_LFLOW) &&
1084 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1086 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1087 if (newflow != flowmode) {
1089 output_data("%c%c%c%c%c%c",
1090 IAC, SB, TELOPT_LFLOW,
1094 DIAG(TD_OPTIONS, printsub('>',
1095 (unsigned char *)nfrontp-4,
1105 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1107 c = *ptyip++ & 0377, pcc--;
1109 output_data("%c", c);
1110 output_data("%c", c);
1111 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1112 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1113 output_data("%c", *ptyip++ & 0377);
1116 output_data("%c", '\0');
1120 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1124 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1128 } /* end of telnet */
1132 # define TCSIG TIOCSIG
1137 * Send interrupt to process on other side of pty.
1138 * If it is in raw mode, just write NULL;
1139 * otherwise, write intr char.
1144 ptyflush(); /* half-hearted */
1147 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1150 *pfrontp++ = slctab[SLC_IP].sptr ?
1151 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1156 * Send quit to process on other side of pty.
1157 * If it is in raw mode, just write NULL;
1158 * otherwise, write quit char.
1163 ptyflush(); /* half-hearted */
1165 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1168 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1169 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1177 ptyflush(); /* half-hearted */
1179 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1181 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1182 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1184 #endif /* SIGTSTP */
1188 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1189 * just send back "[Yes]".
1194 #if defined(SIGINFO) && defined(TCSIG)
1195 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1196 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1200 output_data("\r\n[Yes]\r\n");
1208 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1209 if (!tty_isediting()) {
1210 extern char oldeofc;
1211 *pfrontp++ = oldeofc;
1215 *pfrontp++ = slctab[SLC_EOF].sptr ?
1216 (unsigned char)*slctab[SLC_EOF].sptr : '\004';