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"
45 #include <arpa/inet.h>
48 #include <libtelnet/auth.h>
52 #include <libtelnet/encrypt.h>
54 #include <libtelnet/misc.h>
56 char remote_hostname[MAXHOSTNAMELEN];
58 size_t utmp_len = sizeof(remote_hostname) - 1;
60 int registerd_host_only = 0;
65 * pointers, and counters.
67 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
68 char ptyibuf2[BUFSIZ];
70 int readstream(int, char *, int);
71 void doit(struct sockaddr *) __dead2;
72 int terminaltypeok(char *);
74 int hostinfo = 1; /* do we print login banner? */
80 void startslave(char *, int, char *);
81 extern void usage(void) __dead2;
82 static void _gettermname(void);
85 * The string to pass to getopt(). We do it this way so
86 * that only the actual options that we support will be
87 * passed off to getopt().
90 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
110 int family = AF_INET;
112 #ifndef MAXHOSTNAMELEN
113 #define MAXHOSTNAMELEN 256
114 #endif /* MAXHOSTNAMELEN */
117 char host_name[MAXHOSTNAMELEN];
119 extern void telnet(int, int, char *) __dead2;
125 main(int argc, char *argv[])
127 struct sockaddr_storage from;
130 #if defined(IPPROTO_IP) && defined(IP_TOS)
134 pfrontp = pbackp = ptyobuf;
136 nfrontp = nbackp = netobuf;
139 #endif /* ENCRYPTION */
142 * This initialization causes linemode to default to a configuration
143 * that works on all telnet clients, including the FreeBSD client.
144 * This is not quite the same as the telnet client issuing a "mode
145 * character" command, but has most of the same benefits, and is
146 * preferable since some clients (like usofts) don't have the
147 * mode character command anyway and linemode breaks things.
148 * The most notable symptom of fix is that csh "set filec" operations
149 * like <ESC> (filename completion) and ^D (choices) keys now work
150 * in telnet sessions and can be used more than once on the same line.
151 * CR/LF handling is also corrected in some termio modes. This
152 * change resolves problem reports bin/771 and bin/1037.
155 linemode=1; /*Default to mode that works on bulk of clients*/
157 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
160 #ifdef AUTHENTICATION
163 * Check for required authentication level
165 if (strcmp(optarg, "debug") == 0) {
166 extern int auth_debug_mode;
168 } else if (strcasecmp(optarg, "none") == 0) {
170 } else if (strcasecmp(optarg, "other") == 0) {
171 auth_level = AUTH_OTHER;
172 } else if (strcasecmp(optarg, "user") == 0) {
173 auth_level = AUTH_USER;
174 } else if (strcasecmp(optarg, "valid") == 0) {
175 auth_level = AUTH_VALID;
176 } else if (strcasecmp(optarg, "off") == 0) {
178 * This hack turns off authentication
182 warnx("unknown authorization level for -a");
185 #endif /* AUTHENTICATION */
191 #endif /* BFTPDAEMON */
194 if (strcmp(optarg, "ebug") == 0) {
205 * Check for desired diagnostics capabilities.
207 if (!strcmp(optarg, "report")) {
208 diagnostic |= TD_REPORT|TD_OPTIONS;
209 } else if (!strcmp(optarg, "exercise")) {
210 diagnostic |= TD_EXERCISE;
211 } else if (!strcmp(optarg, "netdata")) {
212 diagnostic |= TD_NETDATA;
213 } else if (!strcmp(optarg, "ptydata")) {
214 diagnostic |= TD_PTYDATA;
215 } else if (!strcmp(optarg, "options")) {
216 diagnostic |= TD_OPTIONS;
222 #endif /* DIAGNOSTICS */
226 if (strcmp(optarg, "debug") == 0) {
227 extern int encrypt_debug_mode;
228 encrypt_debug_mode = 1;
234 #endif /* ENCRYPTION */
244 #endif /* LINEMODE */
247 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
248 lmodetype = NO_AUTOKLUDGE;
250 /* ignore -k option if built without kludge linemode */
251 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
264 if ((tos = parsetos(optarg, "tcp")) < 0)
266 "bad TOS argument '", optarg,
267 "'; will try to use default TOS");
269 warnx("TOS option unavailable; -S flag not supported");
275 utmp_len = (size_t)atoi(optarg);
276 if (utmp_len >= sizeof(remote_hostname))
277 utmp_len = sizeof(remote_hostname) - 1;
279 fprintf(stderr, "telnetd: -u option unneeded\n");
284 registerd_host_only = 1;
287 #ifdef AUTHENTICATION
290 * Check for invalid authentication types
292 auth_disable_name(optarg);
294 #endif /* AUTHENTICATION */
307 warnx("%c: unknown option", ch);
319 int s, ns, foo, error;
320 const char *service = "telnet";
321 struct addrinfo hints, *res;
326 } else if (argc == 1)
329 memset(&hints, 0, sizeof(hints));
330 hints.ai_flags = AI_PASSIVE;
331 hints.ai_family = family;
332 hints.ai_socktype = SOCK_STREAM;
333 hints.ai_protocol = 0;
334 error = getaddrinfo(NULL, service, &hints, &res);
337 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
338 if (error == EAI_SYSTEM)
339 errx(1, "tcp/%s: %s\n", service, strerror(errno));
343 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
346 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
347 (char *)&on, sizeof(on));
348 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
350 if (listen(s, 1) < 0)
352 foo = res->ai_addrlen;
353 ns = accept(s, res->ai_addr, &foo);
360 } else if (argc == 1) {
361 ; /* VOID*/ /* Just ignore the host/port name */
363 } else if (argc > 0) {
368 openlog("telnetd", LOG_PID, LOG_DAEMON);
369 fromlen = sizeof (from);
370 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
375 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
376 (char *)&on, sizeof (on)) < 0) {
377 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
380 #if defined(IPPROTO_IP) && defined(IP_TOS)
381 if (from.ss_family == AF_INET) {
382 # if defined(HAS_GETTOS)
384 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
388 tos = 020; /* Low Delay bit */
390 && (setsockopt(0, IPPROTO_IP, IP_TOS,
391 (char *)&tos, sizeof(tos)) < 0)
392 && (errno != ENOPROTOOPT) )
393 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
395 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
397 doit((struct sockaddr *)&from);
405 fprintf(stderr, "usage: telnetd");
406 #ifdef AUTHENTICATION
407 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
410 fprintf(stderr, " [-B]");
412 fprintf(stderr, " [-debug]");
414 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
416 #ifdef AUTHENTICATION
417 fprintf(stderr, " [-edebug]");
419 fprintf(stderr, " [-h]");
420 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
421 fprintf(stderr, " [-k]");
424 fprintf(stderr, " [-l]");
426 fprintf(stderr, " [-n]");
427 fprintf(stderr, "\n\t");
429 fprintf(stderr, " [-S tos]");
431 #ifdef AUTHENTICATION
432 fprintf(stderr, " [-X auth-type]");
434 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
435 fprintf(stderr, " [port]\n");
442 * Ask the other end to send along its terminal type and speed.
443 * Output is the variable terminaltype filled in.
445 static unsigned char ttytype_sbbuf[] = {
446 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
450 #ifndef AUTHENTICATION
451 #define undef2 __unused
457 getterminaltype(char *name undef2)
462 #ifdef AUTHENTICATION
464 * Handle the Authentication option before we do anything else.
466 send_do(TELOPT_AUTHENTICATION, 1);
467 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
469 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
470 retval = auth_wait(name);
475 send_will(TELOPT_ENCRYPT, 1);
476 #endif /* ENCRYPTION */
477 send_do(TELOPT_TTYPE, 1);
478 send_do(TELOPT_TSPEED, 1);
479 send_do(TELOPT_XDISPLOC, 1);
480 send_do(TELOPT_NEW_ENVIRON, 1);
481 send_do(TELOPT_OLD_ENVIRON, 1);
484 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
485 #endif /* ENCRYPTION */
486 his_will_wont_is_changing(TELOPT_TTYPE) ||
487 his_will_wont_is_changing(TELOPT_TSPEED) ||
488 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
489 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
490 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
495 * Wait for the negotiation of what type of encryption we can
496 * send with. If autoencrypt is not set, this will just return.
498 if (his_state_is_will(TELOPT_ENCRYPT)) {
501 #endif /* ENCRYPTION */
502 if (his_state_is_will(TELOPT_TSPEED)) {
503 static unsigned char sb[] =
504 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
506 output_datalen(sb, sizeof sb);
507 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
509 if (his_state_is_will(TELOPT_XDISPLOC)) {
510 static unsigned char sb[] =
511 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
513 output_datalen(sb, sizeof sb);
514 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
516 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
517 static unsigned char sb[] =
518 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
520 output_datalen(sb, sizeof sb);
521 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
523 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
524 static unsigned char sb[] =
525 { IAC, SB, TELOPT_OLD_ENVIRON, 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_TTYPE)) {
532 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
533 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
534 sizeof ttytype_sbbuf - 2););
536 if (his_state_is_will(TELOPT_TSPEED)) {
537 while (sequenceIs(tspeedsubopt, baseline))
540 if (his_state_is_will(TELOPT_XDISPLOC)) {
541 while (sequenceIs(xdisplocsubopt, baseline))
544 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
545 while (sequenceIs(environsubopt, baseline))
548 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
549 while (sequenceIs(oenvironsubopt, baseline))
552 if (his_state_is_will(TELOPT_TTYPE)) {
553 char first[256], last[256];
555 while (sequenceIs(ttypesubopt, baseline))
559 * If the other side has already disabled the option, then
560 * we have to just go with what we (might) have already gotten.
562 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
563 (void) strncpy(first, terminaltype, sizeof(first)-1);
564 first[sizeof(first)-1] = '\0';
567 * Save the unknown name, and request the next name.
569 (void) strncpy(last, terminaltype, sizeof(last)-1);
570 last[sizeof(last)-1] = '\0';
572 if (terminaltypeok(terminaltype))
574 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
575 his_state_is_wont(TELOPT_TTYPE)) {
577 * We've hit the end. If this is the same as
578 * the first name, just go with it.
580 if (strncmp(first, terminaltype, sizeof(first)) == 0)
583 * Get the terminal name one more time, so that
584 * RFC1091 compliant telnets will cycle back to
585 * the start of the list.
588 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
589 (void) strncpy(terminaltype, first, TERMINAL_TYPE_SIZE-1);
590 terminaltype[TERMINAL_TYPE_SIZE-1] = '\0';
598 } /* end of getterminaltype */
604 * If the client turned off the option,
605 * we can't send another request, so we
608 if (his_state_is_wont(TELOPT_TTYPE))
611 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
612 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
613 sizeof ttytype_sbbuf - 2););
614 while (sequenceIs(ttypesubopt, baseline))
619 terminaltypeok(char *s)
623 if (terminaltype == NULL)
627 * tgetent() will return 1 if the type is known, and
628 * 0 if it is not known. If it returns -1, it couldn't
629 * open the database. But if we can't open the database,
630 * it won't help to say we failed, because we won't be
631 * able to verify anything else. So, we treat -1 like 1.
633 if (tgetent(buf, s) == 0)
639 * Get a pty, scan input lines.
642 doit(struct sockaddr *who)
648 * Find an available pty to use.
651 pty = getpty(&ptynum);
653 fatalmsg(net, "All network ports in use");
658 if ((lp = getpty()) == NULL)
659 fatalmsg(net, "Out of ptys");
661 if ((pty = open(lp, 2)) >= 0) {
662 strlcpy(line,lp,sizeof(line));
669 /* get name of connected client */
670 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
671 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
672 fatalmsg(net, "Couldn't resolve your address into a host name.\r\n\
673 Please contact your net administrator");
674 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
677 trimdomain(remote_hostname, UT_HOSTSIZE);
678 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
679 err_ = getnameinfo(who, who->sa_len, remote_hostname,
680 sizeof(remote_hostname), NULL, 0,
681 NI_NUMERICHOST|NI_WITHSCOPEID);
682 /* XXX: do 'err_' check */
685 (void) gethostname(host_name, sizeof(host_name) - 1);
686 host_name[sizeof(host_name) - 1] = '\0';
687 hostname = host_name;
689 #ifdef AUTHENTICATION
691 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
692 * This is a byproduct of needing "#ifdef" and not "#if defined()"
693 * for unifdef. XXX MarkM
695 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
704 level = getterminaltype(user_name);
705 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1)
706 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network");
708 telnet(net, pty, remote_hostname); /* begin server process */
714 * Main loop. Select from pty and network, and
715 * hand data to telnet receiver finite state machine.
718 telnet(int f, int p, char *host)
721 #define TABBUFSIZ 512
722 char defent[TABBUFSIZ];
723 char defstrs[TABBUFSIZ];
731 * Initialize the slc mapping table.
736 * Do some tests where it is desireable to wait for a response.
737 * Rather than doing them slowly, one at a time, do them all
740 if (my_state_is_wont(TELOPT_SGA))
741 send_will(TELOPT_SGA, 1);
743 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
744 * because 4.2 clients are unable to deal with TCP urgent data.
746 * To find out, we send out a "DO ECHO". If the remote system
747 * answers "WILL ECHO" it is probably a 4.2 client, and we note
748 * that fact ("WILL ECHO" ==> that the client will echo what
749 * WE, the server, sends it; it does NOT mean that the client will
750 * echo the terminal input).
752 send_do(TELOPT_ECHO, 1);
755 if (his_state_is_wont(TELOPT_LINEMODE)) {
756 /* Query the peer for linemode support by trying to negotiate
757 * the linemode option.
761 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
763 #endif /* LINEMODE */
766 * Send along a couple of other options that we wish to negotiate.
768 send_do(TELOPT_NAWS, 1);
769 send_will(TELOPT_STATUS, 1);
770 flowmode = 1; /* default flow control state */
771 restartany = -1; /* uninitialized... */
772 send_do(TELOPT_LFLOW, 1);
775 * Spin, waiting for a response from the DO ECHO. However,
776 * some REALLY DUMB telnets out there might not respond
777 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
778 * telnets so far seem to respond with WONT for a DO that
779 * they don't understand...) because by the time we get the
780 * response, it will already have processed the DO ECHO.
781 * Kludge upon kludge.
783 while (his_will_wont_is_changing(TELOPT_NAWS))
788 * The client might have sent a WILL NAWS as part of its
789 * startup code; if so, we'll be here before we get the
790 * response to the DO ECHO. We'll make the assumption
791 * that any implementation that understands about NAWS
792 * is a modern enough implementation that it will respond
793 * to our DO ECHO request; hence we'll do another spin
794 * waiting for the ECHO option to settle down, which is
795 * what we wanted to do in the first place...
797 if (his_want_state_is_will(TELOPT_ECHO) &&
798 his_state_is_will(TELOPT_NAWS)) {
799 while (his_will_wont_is_changing(TELOPT_ECHO))
803 * On the off chance that the telnet client is broken and does not
804 * respond to the DO ECHO we sent, (after all, we did send the
805 * DO NAWS negotiation after the DO ECHO, and we won't get here
806 * until a response to the DO NAWS comes back) simulate the
807 * receipt of a will echo. This will also send a WONT ECHO
808 * to the client, since we assume that the client failed to
809 * respond because it believes that it is already in DO ECHO
810 * mode, which we do not want.
812 if (his_want_state_is_will(TELOPT_ECHO)) {
813 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
814 willoption(TELOPT_ECHO);
818 * Finally, to clean things up, we turn on our echo. This
819 * will break stupid 4.2 telnets out of local terminal echo.
822 if (my_state_is_wont(TELOPT_ECHO))
823 send_will(TELOPT_ECHO, 1);
826 * Turn on packet mode
828 (void) ioctl(p, TIOCPKT, (char *)&on);
830 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
832 * Continuing line mode support. If client does not support
833 * real linemode, attempt to negotiate kludge linemode by sending
834 * the do timing mark sequence.
836 if (lmodetype < REAL_LINEMODE)
837 send_do(TELOPT_TM, 1);
838 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
841 * Call telrcv() once to pick up anything received during
842 * terminal type negotiation, 4.2/4.3 determination, and
843 * linemode negotiation.
847 (void) ioctl(f, FIONBIO, (char *)&on);
848 (void) ioctl(p, FIONBIO, (char *)&on);
850 #if defined(SO_OOBINLINE)
851 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
852 (char *)&on, sizeof on);
853 #endif /* defined(SO_OOBINLINE) */
856 (void) signal(SIGTSTP, SIG_IGN);
860 * Ignoring SIGTTOU keeps the kernel from blocking us
861 * in ttioct() in /sys/tty.c.
863 (void) signal(SIGTTOU, SIG_IGN);
866 (void) signal(SIGCHLD, cleanup);
871 t = open(_PATH_TTY, O_RDWR);
873 (void) ioctl(t, TIOCNOTTY, NULL);
880 * Show banner that getty never gave.
882 * We put the banner in the pty input buffer. This way, it
883 * gets carriage return null processing, etc., just like all
884 * other pty --> client data.
887 if (getent(defent, "default") == 1) {
890 HE = Getstr("he", &cp);
891 HN = Getstr("hn", &cp);
892 IM = Getstr("im", &cp);
894 (void) strlcpy(host_name, HN, sizeof(host_name));
898 IM = strdup(DEFAULT_IM);
901 edithost(HE, host_name);
906 (void) strncat(ptyibuf2, ptyip, pcc+1);
911 * Last check to make sure all our states are correct.
915 #endif /* LINEMODE */
917 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
920 * Startup the login process on the slave side of the terminal
921 * now. We delay this until here to insure option negotiation
924 startslave(host, level, user_name);
926 nfd = ((f > p) ? f : p) + 1;
928 fd_set ibits, obits, xbits;
931 if (ncc < 0 && pcc < 0)
938 * Never look for input if there's still
939 * stuff in the corresponding output buffer
941 if (nfrontp - nbackp || pcc > 0) {
946 if (pfrontp - pbackp || ncc > 0) {
954 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) {
956 if (errno == EINTR) {
967 if (FD_ISSET(net, &xbits)) {
972 * Something to read from the network...
974 if (FD_ISSET(net, &ibits)) {
975 #if !defined(SO_OOBINLINE)
977 * In 4.2 (and 4.3 beta) systems, the
978 * OOB indication and data handling in the kernel
979 * is such that if two separate TCP Urgent requests
980 * come in, one byte of TCP data will be overlaid.
981 * This is fatal for Telnet, but we try to live
984 * In addition, in 4.2 (and...), a special protocol
985 * is needed to pick up the TCP Urgent data in
986 * the correct sequence.
988 * What we do is: if we think we are in urgent
989 * mode, we look to see if we are "at the mark".
990 * If we are, we do an OOB receive. If we run
991 * this twice, we will do the OOB receive twice,
992 * but the second will fail, since the second
993 * time we were "at the mark", but there wasn't
994 * any data there (the kernel doesn't reset
995 * "at the mark" until we do a normal read).
996 * Once we've read the OOB data, we go ahead
997 * and do normal reads.
999 * There is also another problem, which is that
1000 * since the OOB byte we read doesn't put us
1001 * out of OOB state, and since that byte is most
1002 * likely the TELNET DM (data mark), we would
1003 * stay in the TELNET SYNCH (SYNCHing) state.
1004 * So, clocks to the rescue. If we've "just"
1005 * received a DM, then we test for the
1006 * presence of OOB data when the receive OOB
1007 * fails (and AFTER we did the normal mode read
1008 * to clear "at the mark").
1013 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1015 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1016 if ((ncc == -1) && (errno == EINVAL)) {
1017 ncc = read(net, netibuf, sizeof (netibuf));
1018 if (sequenceIs(didnetreceive, gotDM)) {
1019 SYNCHing = stilloob(net);
1023 ncc = read(net, netibuf, sizeof (netibuf));
1026 ncc = read(net, netibuf, sizeof (netibuf));
1028 settimer(didnetreceive);
1029 #else /* !defined(SO_OOBINLINE)) */
1030 ncc = read(net, netibuf, sizeof (netibuf));
1031 #endif /* !defined(SO_OOBINLINE)) */
1032 if (ncc < 0 && errno == EWOULDBLOCK)
1040 DIAG((TD_REPORT | TD_NETDATA),
1041 output_data("td: netread %d chars\r\n", ncc));
1042 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1046 * Something to read from the pty...
1048 if (FD_ISSET(p, &ibits)) {
1049 pcc = read(p, ptyibuf, BUFSIZ);
1051 * On some systems, if we try to read something
1052 * off the master side before the slave side is
1053 * opened, we get EIO.
1055 if (pcc < 0 && (errno == EWOULDBLOCK ||
1066 * If ioctl from pty, pass it through net
1068 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1069 copy_termbuf(ptyibuf+1, pcc-1);
1073 #endif /* LINEMODE */
1074 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1075 netclear(); /* clear buffer back */
1078 * There are client telnets on some
1079 * operating systems get screwed up
1080 * royally if we send them urgent
1083 output_data("%c%c", IAC, DM);
1084 neturg = nfrontp-1; /* off by one XXX */
1086 printoption("td: send IAC", DM));
1090 if (his_state_is_will(TELOPT_LFLOW) &&
1092 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1094 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1095 if (newflow != flowmode) {
1097 output_data("%c%c%c%c%c%c",
1098 IAC, SB, TELOPT_LFLOW,
1102 DIAG(TD_OPTIONS, printsub('>',
1103 (unsigned char *)nfrontp-4,
1113 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1115 c = *ptyip++ & 0377, pcc--;
1117 output_data("%c", c);
1118 output_data("%c", c);
1119 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1120 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1121 output_data("%c", *ptyip++ & 0377);
1124 output_data("%c", '\0');
1128 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1132 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1136 } /* end of telnet */
1140 # define TCSIG TIOCSIG
1145 * Send interrupt to process on other side of pty.
1146 * If it is in raw mode, just write NULL;
1147 * otherwise, write intr char.
1152 ptyflush(); /* half-hearted */
1155 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1158 *pfrontp++ = slctab[SLC_IP].sptr ?
1159 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1164 * Send quit to process on other side of pty.
1165 * If it is in raw mode, just write NULL;
1166 * otherwise, write quit char.
1171 ptyflush(); /* half-hearted */
1173 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1176 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1177 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1185 ptyflush(); /* half-hearted */
1187 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1189 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1190 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1192 #endif /* SIGTSTP */
1196 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1197 * just send back "[Yes]".
1202 #if defined(SIGINFO) && defined(TCSIG)
1203 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1204 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1208 output_data("\r\n[Yes]\r\n");
1216 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1217 if (!tty_isediting()) {
1218 extern char oldeofc;
1219 *pfrontp++ = oldeofc;
1223 *pfrontp++ = slctab[SLC_EOF].sptr ?
1224 (unsigned char)*slctab[SLC_EOF].sptr : '\004';