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"
42 #include <arpa/inet.h>
45 #include <libtelnet/auth.h>
49 #include <libtelnet/encrypt.h>
51 #include <libtelnet/misc.h>
53 char remote_hostname[MAXHOSTNAMELEN];
54 int registerd_host_only = 0;
59 * pointers, and counters.
61 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
62 char ptyibuf2[BUFSIZ];
64 int readstream(int, char *, int);
65 void doit(struct sockaddr *) __dead2;
66 int terminaltypeok(char *);
68 int hostinfo = 1; /* do we print login banner? */
74 void startslave(char *, int, char *);
75 extern void usage(void) __dead2;
76 static void _gettermname(void);
79 * The string to pass to getopt(). We do it this way so
80 * that only the actual options that we support will be
81 * passed off to getopt().
84 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
104 int family = AF_INET;
106 #ifndef MAXHOSTNAMELEN
107 #define MAXHOSTNAMELEN 256
108 #endif /* MAXHOSTNAMELEN */
111 char host_name[MAXHOSTNAMELEN];
113 extern void telnet(int, int, char *) __dead2;
114 extern char *terminaltype;
120 main(int argc, char *argv[])
122 struct sockaddr_storage from;
125 #if defined(IPPROTO_IP) && defined(IP_TOS)
129 pfrontp = pbackp = ptyobuf;
131 nfrontp = nbackp = netobuf;
134 #endif /* ENCRYPTION */
137 * This initialization causes linemode to default to a configuration
138 * that works on all telnet clients, including the FreeBSD client.
139 * This is not quite the same as the telnet client issuing a "mode
140 * character" command, but has most of the same benefits, and is
141 * preferable since some clients (like usofts) don't have the
142 * mode character command anyway and linemode breaks things.
143 * The most notable symptom of fix is that csh "set filec" operations
144 * like <ESC> (filename completion) and ^D (choices) keys now work
145 * in telnet sessions and can be used more than once on the same line.
146 * CR/LF handling is also corrected in some termio modes. This
147 * change resolves problem reports bin/771 and bin/1037.
150 linemode=1; /*Default to mode that works on bulk of clients*/
152 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
155 #ifdef AUTHENTICATION
158 * Check for required authentication level
160 if (strcmp(optarg, "debug") == 0) {
161 extern int auth_debug_mode;
163 } else if (strcasecmp(optarg, "none") == 0) {
165 } else if (strcasecmp(optarg, "other") == 0) {
166 auth_level = AUTH_OTHER;
167 } else if (strcasecmp(optarg, "user") == 0) {
168 auth_level = AUTH_USER;
169 } else if (strcasecmp(optarg, "valid") == 0) {
170 auth_level = AUTH_VALID;
171 } else if (strcasecmp(optarg, "off") == 0) {
173 * This hack turns off authentication
177 warnx("unknown authorization level for -a");
180 #endif /* AUTHENTICATION */
186 #endif /* BFTPDAEMON */
189 if (strcmp(optarg, "ebug") == 0) {
200 * Check for desired diagnostics capabilities.
202 if (!strcmp(optarg, "report")) {
203 diagnostic |= TD_REPORT|TD_OPTIONS;
204 } else if (!strcmp(optarg, "exercise")) {
205 diagnostic |= TD_EXERCISE;
206 } else if (!strcmp(optarg, "netdata")) {
207 diagnostic |= TD_NETDATA;
208 } else if (!strcmp(optarg, "ptydata")) {
209 diagnostic |= TD_PTYDATA;
210 } else if (!strcmp(optarg, "options")) {
211 diagnostic |= TD_OPTIONS;
217 #endif /* DIAGNOSTICS */
221 if (strcmp(optarg, "debug") == 0) {
222 extern int encrypt_debug_mode;
223 encrypt_debug_mode = 1;
229 #endif /* ENCRYPTION */
239 #endif /* LINEMODE */
242 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
243 lmodetype = NO_AUTOKLUDGE;
245 /* ignore -k option if built without kludge linemode */
246 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
259 if ((tos = parsetos(optarg, "tcp")) < 0)
261 "bad TOS argument '", optarg,
262 "'; will try to use default TOS");
264 warnx("TOS option unavailable; -S flag not supported");
269 fprintf(stderr, "telnetd: -u option unneeded\n");
273 registerd_host_only = 1;
276 #ifdef AUTHENTICATION
279 * Check for invalid authentication types
281 auth_disable_name(optarg);
283 #endif /* AUTHENTICATION */
296 warnx("%c: unknown option", ch);
308 int s, ns, foo, error;
309 const char *service = "telnet";
310 struct addrinfo hints, *res;
315 } else if (argc == 1)
318 memset(&hints, 0, sizeof(hints));
319 hints.ai_flags = AI_PASSIVE;
320 hints.ai_family = family;
321 hints.ai_socktype = SOCK_STREAM;
322 hints.ai_protocol = 0;
323 error = getaddrinfo(NULL, service, &hints, &res);
326 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
327 if (error == EAI_SYSTEM)
328 errx(1, "tcp/%s: %s\n", service, strerror(errno));
332 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
335 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
336 (char *)&on, sizeof(on));
337 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
339 if (listen(s, 1) < 0)
341 foo = res->ai_addrlen;
342 ns = accept(s, res->ai_addr, &foo);
349 } else if (argc == 1) {
350 ; /* VOID*/ /* Just ignore the host/port name */
352 } else if (argc > 0) {
357 openlog("telnetd", LOG_PID, LOG_DAEMON);
358 fromlen = sizeof (from);
359 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
364 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
365 (char *)&on, sizeof (on)) < 0) {
366 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
369 #if defined(IPPROTO_IP) && defined(IP_TOS)
370 if (from.ss_family == AF_INET) {
371 # if defined(HAS_GETTOS)
373 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
377 tos = 020; /* Low Delay bit */
379 && (setsockopt(0, IPPROTO_IP, IP_TOS,
380 (char *)&tos, sizeof(tos)) < 0)
381 && (errno != ENOPROTOOPT) )
382 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
384 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
386 doit((struct sockaddr *)&from);
394 fprintf(stderr, "usage: telnetd");
395 #ifdef AUTHENTICATION
396 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
399 fprintf(stderr, " [-B]");
401 fprintf(stderr, " [-debug]");
403 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
405 #ifdef AUTHENTICATION
406 fprintf(stderr, " [-edebug]");
408 fprintf(stderr, " [-h]");
409 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
410 fprintf(stderr, " [-k]");
413 fprintf(stderr, " [-l]");
415 fprintf(stderr, " [-n]");
416 fprintf(stderr, "\n\t");
418 fprintf(stderr, " [-S tos]");
420 #ifdef AUTHENTICATION
421 fprintf(stderr, " [-X auth-type]");
423 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
424 fprintf(stderr, " [port]\n");
431 * Ask the other end to send along its terminal type and speed.
432 * Output is the variable terminaltype filled in.
434 static unsigned char ttytype_sbbuf[] = {
435 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
439 #ifndef AUTHENTICATION
440 #define undef2 __unused
446 getterminaltype(char *name undef2)
451 #ifdef AUTHENTICATION
453 * Handle the Authentication option before we do anything else.
455 send_do(TELOPT_AUTHENTICATION, 1);
456 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
458 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
459 retval = auth_wait(name);
464 send_will(TELOPT_ENCRYPT, 1);
465 #endif /* ENCRYPTION */
466 send_do(TELOPT_TTYPE, 1);
467 send_do(TELOPT_TSPEED, 1);
468 send_do(TELOPT_XDISPLOC, 1);
469 send_do(TELOPT_NEW_ENVIRON, 1);
470 send_do(TELOPT_OLD_ENVIRON, 1);
473 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
474 #endif /* ENCRYPTION */
475 his_will_wont_is_changing(TELOPT_TTYPE) ||
476 his_will_wont_is_changing(TELOPT_TSPEED) ||
477 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
478 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
479 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
484 * Wait for the negotiation of what type of encryption we can
485 * send with. If autoencrypt is not set, this will just return.
487 if (his_state_is_will(TELOPT_ENCRYPT)) {
490 #endif /* ENCRYPTION */
491 if (his_state_is_will(TELOPT_TSPEED)) {
492 static unsigned char sb[] =
493 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
495 output_datalen(sb, sizeof sb);
496 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
498 if (his_state_is_will(TELOPT_XDISPLOC)) {
499 static unsigned char sb[] =
500 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
502 output_datalen(sb, sizeof sb);
503 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
505 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
506 static unsigned char sb[] =
507 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
509 output_datalen(sb, sizeof sb);
510 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
512 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
513 static unsigned char sb[] =
514 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
516 output_datalen(sb, sizeof sb);
517 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
519 if (his_state_is_will(TELOPT_TTYPE)) {
521 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
522 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
523 sizeof ttytype_sbbuf - 2););
525 if (his_state_is_will(TELOPT_TSPEED)) {
526 while (sequenceIs(tspeedsubopt, baseline))
529 if (his_state_is_will(TELOPT_XDISPLOC)) {
530 while (sequenceIs(xdisplocsubopt, baseline))
533 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
534 while (sequenceIs(environsubopt, baseline))
537 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
538 while (sequenceIs(oenvironsubopt, baseline))
541 if (his_state_is_will(TELOPT_TTYPE)) {
542 char first[256], last[256];
544 while (sequenceIs(ttypesubopt, baseline))
548 * If the other side has already disabled the option, then
549 * we have to just go with what we (might) have already gotten.
551 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
552 (void) strncpy(first, terminaltype, sizeof(first)-1);
553 first[sizeof(first)-1] = '\0';
556 * Save the unknown name, and request the next name.
558 (void) strncpy(last, terminaltype, sizeof(last)-1);
559 last[sizeof(last)-1] = '\0';
561 if (terminaltypeok(terminaltype))
563 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
564 his_state_is_wont(TELOPT_TTYPE)) {
566 * We've hit the end. If this is the same as
567 * the first name, just go with it.
569 if (strncmp(first, terminaltype, sizeof(first)) == 0)
572 * Get the terminal name one more time, so that
573 * RFC1091 compliant telnets will cycle back to
574 * the start of the list.
577 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
578 (void) strncpy(terminaltype, first, TERMINAL_TYPE_SIZE-1);
579 terminaltype[TERMINAL_TYPE_SIZE-1] = '\0';
587 } /* end of getterminaltype */
593 * If the client turned off the option,
594 * we can't send another request, so we
597 if (his_state_is_wont(TELOPT_TTYPE))
600 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
601 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
602 sizeof ttytype_sbbuf - 2););
603 while (sequenceIs(ttypesubopt, baseline))
608 terminaltypeok(char *s)
612 if (terminaltype == NULL)
616 * tgetent() will return 1 if the type is known, and
617 * 0 if it is not known. If it returns -1, it couldn't
618 * open the database. But if we can't open the database,
619 * it won't help to say we failed, because we won't be
620 * able to verify anything else. So, we treat -1 like 1.
622 if (tgetent(buf, s) == 0)
628 * Get a pty, scan input lines.
631 doit(struct sockaddr *who)
636 * Find an available pty to use.
639 pty = getpty(&ptynum);
641 fatalmsg(net, "All network ports in use");
646 if ((lp = getpty()) == NULL)
647 fatalmsg(net, "Out of ptys");
649 if ((pty = open(lp, 2)) >= 0) {
650 strlcpy(line,lp,sizeof(line));
657 /* get name of connected client */
658 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
659 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
660 fatalmsg(net, "Couldn't resolve your address into a host name.\r\n\
661 Please contact your net administrator");
662 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
664 (void) gethostname(host_name, sizeof(host_name) - 1);
665 host_name[sizeof(host_name) - 1] = '\0';
666 hostname = host_name;
668 #ifdef AUTHENTICATION
670 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
671 * This is a byproduct of needing "#ifdef" and not "#if defined()"
672 * for unifdef. XXX MarkM
674 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
683 level = getterminaltype(user_name);
684 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1)
685 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network");
687 telnet(net, pty, remote_hostname); /* begin server process */
693 * Main loop. Select from pty and network, and
694 * hand data to telnet receiver finite state machine.
697 telnet(int f, int p, char *host)
700 #define TABBUFSIZ 512
701 char defent[TABBUFSIZ];
702 char defstrs[TABBUFSIZ];
710 * Initialize the slc mapping table.
715 * Do some tests where it is desireable to wait for a response.
716 * Rather than doing them slowly, one at a time, do them all
719 if (my_state_is_wont(TELOPT_SGA))
720 send_will(TELOPT_SGA, 1);
722 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
723 * because 4.2 clients are unable to deal with TCP urgent data.
725 * To find out, we send out a "DO ECHO". If the remote system
726 * answers "WILL ECHO" it is probably a 4.2 client, and we note
727 * that fact ("WILL ECHO" ==> that the client will echo what
728 * WE, the server, sends it; it does NOT mean that the client will
729 * echo the terminal input).
731 send_do(TELOPT_ECHO, 1);
734 if (his_state_is_wont(TELOPT_LINEMODE)) {
735 /* Query the peer for linemode support by trying to negotiate
736 * the linemode option.
740 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
742 #endif /* LINEMODE */
745 * Send along a couple of other options that we wish to negotiate.
747 send_do(TELOPT_NAWS, 1);
748 send_will(TELOPT_STATUS, 1);
749 flowmode = 1; /* default flow control state */
750 restartany = -1; /* uninitialized... */
751 send_do(TELOPT_LFLOW, 1);
754 * Spin, waiting for a response from the DO ECHO. However,
755 * some REALLY DUMB telnets out there might not respond
756 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
757 * telnets so far seem to respond with WONT for a DO that
758 * they don't understand...) because by the time we get the
759 * response, it will already have processed the DO ECHO.
760 * Kludge upon kludge.
762 while (his_will_wont_is_changing(TELOPT_NAWS))
767 * The client might have sent a WILL NAWS as part of its
768 * startup code; if so, we'll be here before we get the
769 * response to the DO ECHO. We'll make the assumption
770 * that any implementation that understands about NAWS
771 * is a modern enough implementation that it will respond
772 * to our DO ECHO request; hence we'll do another spin
773 * waiting for the ECHO option to settle down, which is
774 * what we wanted to do in the first place...
776 if (his_want_state_is_will(TELOPT_ECHO) &&
777 his_state_is_will(TELOPT_NAWS)) {
778 while (his_will_wont_is_changing(TELOPT_ECHO))
782 * On the off chance that the telnet client is broken and does not
783 * respond to the DO ECHO we sent, (after all, we did send the
784 * DO NAWS negotiation after the DO ECHO, and we won't get here
785 * until a response to the DO NAWS comes back) simulate the
786 * receipt of a will echo. This will also send a WONT ECHO
787 * to the client, since we assume that the client failed to
788 * respond because it believes that it is already in DO ECHO
789 * mode, which we do not want.
791 if (his_want_state_is_will(TELOPT_ECHO)) {
792 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
793 willoption(TELOPT_ECHO);
797 * Finally, to clean things up, we turn on our echo. This
798 * will break stupid 4.2 telnets out of local terminal echo.
801 if (my_state_is_wont(TELOPT_ECHO))
802 send_will(TELOPT_ECHO, 1);
805 * Turn on packet mode
807 (void) ioctl(p, TIOCPKT, (char *)&on);
809 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
811 * Continuing line mode support. If client does not support
812 * real linemode, attempt to negotiate kludge linemode by sending
813 * the do timing mark sequence.
815 if (lmodetype < REAL_LINEMODE)
816 send_do(TELOPT_TM, 1);
817 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
820 * Call telrcv() once to pick up anything received during
821 * terminal type negotiation, 4.2/4.3 determination, and
822 * linemode negotiation.
826 (void) ioctl(f, FIONBIO, (char *)&on);
827 (void) ioctl(p, FIONBIO, (char *)&on);
829 #if defined(SO_OOBINLINE)
830 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
831 (char *)&on, sizeof on);
832 #endif /* defined(SO_OOBINLINE) */
835 (void) signal(SIGTSTP, SIG_IGN);
839 * Ignoring SIGTTOU keeps the kernel from blocking us
840 * in ttioct() in /sys/tty.c.
842 (void) signal(SIGTTOU, SIG_IGN);
845 (void) signal(SIGCHLD, cleanup);
850 t = open(_PATH_TTY, O_RDWR);
852 (void) ioctl(t, TIOCNOTTY, NULL);
859 * Show banner that getty never gave.
861 * We put the banner in the pty input buffer. This way, it
862 * gets carriage return null processing, etc., just like all
863 * other pty --> client data.
866 if (getent(defent, "default") == 1) {
869 HE = Getstr("he", &cp);
870 HN = Getstr("hn", &cp);
871 IM = Getstr("im", &cp);
873 (void) strlcpy(host_name, HN, sizeof(host_name));
877 IM = strdup(DEFAULT_IM);
880 edithost(HE, host_name);
885 (void) strncat(ptyibuf2, ptyip, pcc+1);
890 * Last check to make sure all our states are correct.
894 #endif /* LINEMODE */
896 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
899 * Startup the login process on the slave side of the terminal
900 * now. We delay this until here to insure option negotiation
903 startslave(host, level, user_name);
905 nfd = ((f > p) ? f : p) + 1;
907 fd_set ibits, obits, xbits;
910 if (ncc < 0 && pcc < 0)
917 * Never look for input if there's still
918 * stuff in the corresponding output buffer
920 if (nfrontp - nbackp || pcc > 0) {
925 if (pfrontp - pbackp || ncc > 0) {
933 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) {
935 if (errno == EINTR) {
946 if (FD_ISSET(net, &xbits)) {
951 * Something to read from the network...
953 if (FD_ISSET(net, &ibits)) {
954 #if !defined(SO_OOBINLINE)
956 * In 4.2 (and 4.3 beta) systems, the
957 * OOB indication and data handling in the kernel
958 * is such that if two separate TCP Urgent requests
959 * come in, one byte of TCP data will be overlaid.
960 * This is fatal for Telnet, but we try to live
963 * In addition, in 4.2 (and...), a special protocol
964 * is needed to pick up the TCP Urgent data in
965 * the correct sequence.
967 * What we do is: if we think we are in urgent
968 * mode, we look to see if we are "at the mark".
969 * If we are, we do an OOB receive. If we run
970 * this twice, we will do the OOB receive twice,
971 * but the second will fail, since the second
972 * time we were "at the mark", but there wasn't
973 * any data there (the kernel doesn't reset
974 * "at the mark" until we do a normal read).
975 * Once we've read the OOB data, we go ahead
976 * and do normal reads.
978 * There is also another problem, which is that
979 * since the OOB byte we read doesn't put us
980 * out of OOB state, and since that byte is most
981 * likely the TELNET DM (data mark), we would
982 * stay in the TELNET SYNCH (SYNCHing) state.
983 * So, clocks to the rescue. If we've "just"
984 * received a DM, then we test for the
985 * presence of OOB data when the receive OOB
986 * fails (and AFTER we did the normal mode read
987 * to clear "at the mark").
992 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
994 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
995 if ((ncc == -1) && (errno == EINVAL)) {
996 ncc = read(net, netibuf, sizeof (netibuf));
997 if (sequenceIs(didnetreceive, gotDM)) {
998 SYNCHing = stilloob(net);
1002 ncc = read(net, netibuf, sizeof (netibuf));
1005 ncc = read(net, netibuf, sizeof (netibuf));
1007 settimer(didnetreceive);
1008 #else /* !defined(SO_OOBINLINE)) */
1009 ncc = read(net, netibuf, sizeof (netibuf));
1010 #endif /* !defined(SO_OOBINLINE)) */
1011 if (ncc < 0 && errno == EWOULDBLOCK)
1019 DIAG((TD_REPORT | TD_NETDATA),
1020 output_data("td: netread %d chars\r\n", ncc));
1021 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1025 * Something to read from the pty...
1027 if (FD_ISSET(p, &ibits)) {
1028 pcc = read(p, ptyibuf, BUFSIZ);
1030 * On some systems, if we try to read something
1031 * off the master side before the slave side is
1032 * opened, we get EIO.
1034 if (pcc < 0 && (errno == EWOULDBLOCK ||
1045 * If ioctl from pty, pass it through net
1047 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1048 copy_termbuf(ptyibuf+1, pcc-1);
1052 #endif /* LINEMODE */
1053 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1054 netclear(); /* clear buffer back */
1057 * There are client telnets on some
1058 * operating systems get screwed up
1059 * royally if we send them urgent
1062 output_data("%c%c", IAC, DM);
1063 neturg = nfrontp-1; /* off by one XXX */
1065 printoption("td: send IAC", DM));
1069 if (his_state_is_will(TELOPT_LFLOW) &&
1071 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1073 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1074 if (newflow != flowmode) {
1076 output_data("%c%c%c%c%c%c",
1077 IAC, SB, TELOPT_LFLOW,
1081 DIAG(TD_OPTIONS, printsub('>',
1082 (unsigned char *)nfrontp-4,
1092 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1094 c = *ptyip++ & 0377, pcc--;
1096 output_data("%c", c);
1097 output_data("%c", c);
1098 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1099 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1100 output_data("%c", *ptyip++ & 0377);
1103 output_data("%c", '\0');
1107 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1111 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1115 } /* end of telnet */
1119 # define TCSIG TIOCSIG
1124 * Send interrupt to process on other side of pty.
1125 * If it is in raw mode, just write NULL;
1126 * otherwise, write intr char.
1131 ptyflush(); /* half-hearted */
1134 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1137 *pfrontp++ = slctab[SLC_IP].sptr ?
1138 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1143 * Send quit to process on other side of pty.
1144 * If it is in raw mode, just write NULL;
1145 * otherwise, write quit char.
1150 ptyflush(); /* half-hearted */
1152 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1155 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1156 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1164 ptyflush(); /* half-hearted */
1166 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1168 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1169 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1171 #endif /* SIGTSTP */
1175 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1176 * just send back "[Yes]".
1181 #if defined(SIGINFO) && defined(TCSIG)
1182 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1183 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1187 output_data("\r\n[Yes]\r\n");
1195 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1196 if (!tty_isediting()) {
1197 extern char oldeofc;
1198 *pfrontp++ = oldeofc;
1202 *pfrontp++ = slctab[SLC_EOF].sptr ?
1203 (unsigned char)*slctab[SLC_EOF].sptr : '\004';