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[])
134 struct sockaddr_storage from;
137 #if defined(IPPROTO_IP) && defined(IP_TOS)
141 pfrontp = pbackp = ptyobuf;
143 nfrontp = nbackp = netobuf;
146 #endif /* ENCRYPTION */
149 * This initialization causes linemode to default to a configuration
150 * that works on all telnet clients, including the FreeBSD client.
151 * This is not quite the same as the telnet client issuing a "mode
152 * character" command, but has most of the same benefits, and is
153 * preferable since some clients (like usofts) don't have the
154 * mode character command anyway and linemode breaks things.
155 * The most notable symptom of fix is that csh "set filec" operations
156 * like <ESC> (filename completion) and ^D (choices) keys now work
157 * in telnet sessions and can be used more than once on the same line.
158 * CR/LF handling is also corrected in some termio modes. This
159 * change resolves problem reports bin/771 and bin/1037.
162 linemode=1; /*Default to mode that works on bulk of clients*/
164 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
167 #ifdef AUTHENTICATION
170 * Check for required authentication level
172 if (strcmp(optarg, "debug") == 0) {
173 extern int auth_debug_mode;
175 } else if (strcasecmp(optarg, "none") == 0) {
177 } else if (strcasecmp(optarg, "other") == 0) {
178 auth_level = AUTH_OTHER;
179 } else if (strcasecmp(optarg, "user") == 0) {
180 auth_level = AUTH_USER;
181 } else if (strcasecmp(optarg, "valid") == 0) {
182 auth_level = AUTH_VALID;
183 } else if (strcasecmp(optarg, "off") == 0) {
185 * This hack turns off authentication
189 warnx("unknown authorization level for -a");
192 #endif /* AUTHENTICATION */
198 #endif /* BFTPDAEMON */
201 if (strcmp(optarg, "ebug") == 0) {
212 * Check for desired diagnostics capabilities.
214 if (!strcmp(optarg, "report")) {
215 diagnostic |= TD_REPORT|TD_OPTIONS;
216 } else if (!strcmp(optarg, "exercise")) {
217 diagnostic |= TD_EXERCISE;
218 } else if (!strcmp(optarg, "netdata")) {
219 diagnostic |= TD_NETDATA;
220 } else if (!strcmp(optarg, "ptydata")) {
221 diagnostic |= TD_PTYDATA;
222 } else if (!strcmp(optarg, "options")) {
223 diagnostic |= TD_OPTIONS;
229 #endif /* DIAGNOSTICS */
233 if (strcmp(optarg, "debug") == 0) {
234 extern int encrypt_debug_mode;
235 encrypt_debug_mode = 1;
241 #endif /* ENCRYPTION */
251 #endif /* LINEMODE */
254 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
255 lmodetype = NO_AUTOKLUDGE;
257 /* ignore -k option if built without kludge linemode */
258 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
271 if ((tos = parsetos(optarg, "tcp")) < 0)
273 "bad TOS argument '", optarg,
274 "'; will try to use default TOS");
276 warnx("TOS option unavailable; -S flag not supported");
281 utmp_len = (size_t)atoi(optarg);
282 if (utmp_len >= sizeof(remote_hostname))
283 utmp_len = sizeof(remote_hostname) - 1;
287 registerd_host_only = 1;
290 #ifdef AUTHENTICATION
293 * Check for invalid authentication types
295 auth_disable_name(optarg);
297 #endif /* AUTHENTICATION */
310 warnx("%c: unknown option", ch);
322 int s, ns, foo, error;
323 const char *service = "telnet";
324 struct addrinfo hints, *res;
329 } else if (argc == 1)
332 memset(&hints, 0, sizeof(hints));
333 hints.ai_flags = AI_PASSIVE;
334 hints.ai_family = family;
335 hints.ai_socktype = SOCK_STREAM;
336 hints.ai_protocol = 0;
337 error = getaddrinfo(NULL, service, &hints, &res);
340 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
341 if (error == EAI_SYSTEM)
342 errx(1, "tcp/%s: %s\n", service, strerror(errno));
346 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
349 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
350 (char *)&on, sizeof(on));
352 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
353 (char *)&on, sizeof(on));
354 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
356 if (listen(s, 1) < 0)
358 foo = res->ai_addrlen;
359 ns = accept(s, res->ai_addr, &foo);
362 (void) setsockopt(ns, SOL_SOCKET, SO_DEBUG,
363 (char *)&on, sizeof(on));
368 } else if (argc == 1) {
369 ; /* VOID*/ /* Just ignore the host/port name */
371 } else if (argc > 0) {
376 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
377 fromlen = sizeof (from);
378 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
383 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
384 (char *)&on, sizeof (on)) < 0) {
385 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
388 #if defined(IPPROTO_IP) && defined(IP_TOS)
389 if (from.ss_family == AF_INET) {
390 # if defined(HAS_GETTOS)
392 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
396 tos = 020; /* Low Delay bit */
398 && (setsockopt(0, IPPROTO_IP, IP_TOS,
399 (char *)&tos, sizeof(tos)) < 0)
400 && (errno != ENOPROTOOPT) )
401 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
403 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
405 doit((struct sockaddr *)&from);
413 fprintf(stderr, "usage: telnetd");
414 #ifdef AUTHENTICATION
415 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
418 fprintf(stderr, " [-B]");
420 fprintf(stderr, " [-debug]");
422 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
424 #ifdef AUTHENTICATION
425 fprintf(stderr, " [-edebug]");
427 fprintf(stderr, " [-h]");
428 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
429 fprintf(stderr, " [-k]");
432 fprintf(stderr, " [-l]");
434 fprintf(stderr, " [-n]");
435 fprintf(stderr, "\n\t");
437 fprintf(stderr, " [-S tos]");
439 #ifdef AUTHENTICATION
440 fprintf(stderr, " [-X auth-type]");
442 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
443 fprintf(stderr, " [port]\n");
450 * Ask the other end to send along its terminal type and speed.
451 * Output is the variable terminaltype filled in.
453 static unsigned char ttytype_sbbuf[] = {
454 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
458 #ifndef AUTHENTICATION
459 #define undef2 __unused
465 getterminaltype(char *name undef2)
470 #ifdef AUTHENTICATION
472 * Handle the Authentication option before we do anything else.
474 send_do(TELOPT_AUTHENTICATION, 1);
475 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
477 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
478 retval = auth_wait(name);
483 send_will(TELOPT_ENCRYPT, 1);
484 #endif /* ENCRYPTION */
485 send_do(TELOPT_TTYPE, 1);
486 send_do(TELOPT_TSPEED, 1);
487 send_do(TELOPT_XDISPLOC, 1);
488 send_do(TELOPT_NEW_ENVIRON, 1);
489 send_do(TELOPT_OLD_ENVIRON, 1);
492 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
493 #endif /* ENCRYPTION */
494 his_will_wont_is_changing(TELOPT_TTYPE) ||
495 his_will_wont_is_changing(TELOPT_TSPEED) ||
496 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
497 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
498 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
503 * Wait for the negotiation of what type of encryption we can
504 * send with. If autoencrypt is not set, this will just return.
506 if (his_state_is_will(TELOPT_ENCRYPT)) {
509 #endif /* ENCRYPTION */
510 if (his_state_is_will(TELOPT_TSPEED)) {
511 static unsigned char sb[] =
512 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
514 output_datalen(sb, sizeof sb);
515 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
517 if (his_state_is_will(TELOPT_XDISPLOC)) {
518 static unsigned char sb[] =
519 { IAC, SB, TELOPT_XDISPLOC, 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_NEW_ENVIRON)) {
525 static unsigned char sb[] =
526 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
528 output_datalen(sb, sizeof sb);
529 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
531 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
532 static unsigned char sb[] =
533 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
535 output_datalen(sb, sizeof sb);
536 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
538 if (his_state_is_will(TELOPT_TTYPE)) {
540 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
541 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
542 sizeof ttytype_sbbuf - 2););
544 if (his_state_is_will(TELOPT_TSPEED)) {
545 while (sequenceIs(tspeedsubopt, baseline))
548 if (his_state_is_will(TELOPT_XDISPLOC)) {
549 while (sequenceIs(xdisplocsubopt, baseline))
552 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
553 while (sequenceIs(environsubopt, baseline))
556 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
557 while (sequenceIs(oenvironsubopt, baseline))
560 if (his_state_is_will(TELOPT_TTYPE)) {
561 char first[256], last[256];
563 while (sequenceIs(ttypesubopt, baseline))
567 * If the other side has already disabled the option, then
568 * we have to just go with what we (might) have already gotten.
570 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
571 (void) strncpy(first, terminaltype, sizeof(first)-1);
572 first[sizeof(first)-1] = '\0';
575 * Save the unknown name, and request the next name.
577 (void) strncpy(last, terminaltype, sizeof(last)-1);
578 last[sizeof(last)-1] = '\0';
580 if (terminaltypeok(terminaltype))
582 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
583 his_state_is_wont(TELOPT_TTYPE)) {
585 * We've hit the end. If this is the same as
586 * the first name, just go with it.
588 if (strncmp(first, terminaltype, sizeof(first)) == 0)
591 * Get the terminal name one more time, so that
592 * RFC1091 compliant telnets will cycle back to
593 * the start of the list.
596 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
597 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
598 terminaltype[sizeof(terminaltype)-1] = '\0';
606 } /* end of getterminaltype */
612 * If the client turned off the option,
613 * we can't send another request, so we
616 if (his_state_is_wont(TELOPT_TTYPE))
619 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
620 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
621 sizeof ttytype_sbbuf - 2););
622 while (sequenceIs(ttypesubopt, baseline))
627 terminaltypeok(char *s)
631 if (terminaltype == NULL)
635 * tgetent() will return 1 if the type is known, and
636 * 0 if it is not known. If it returns -1, it couldn't
637 * open the database. But if we can't open the database,
638 * it won't help to say we failed, because we won't be
639 * able to verify anything else. So, we treat -1 like 1.
641 if (tgetent(buf, s) == 0)
647 * Get a pty, scan input lines.
650 doit(struct sockaddr *who)
656 * Find an available pty to use.
659 pty = getpty(&ptynum);
661 fatal(net, "All network ports in use");
666 if ((lp = getpty()) == NULL)
667 fatal(net, "Out of ptys");
669 if ((pty = open(lp, 2)) >= 0) {
670 strlcpy(line,lp,sizeof(line));
677 /* get name of connected client */
678 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
679 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
680 fatal(net, "Couldn't resolve your address into a host name.\r\n\
681 Please contact your net administrator");
682 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
684 trimdomain(remote_hostname, UT_HOSTSIZE);
685 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
686 err_ = getnameinfo(who, who->sa_len, remote_hostname,
687 sizeof(remote_hostname), NULL, 0,
688 NI_NUMERICHOST|NI_WITHSCOPEID);
689 /* XXX: do 'err_' check */
691 (void) gethostname(host_name, sizeof(host_name) - 1);
692 host_name[sizeof(host_name) - 1] = '\0';
693 hostname = host_name;
695 #ifdef AUTHENTICATION
697 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
698 * This is a byproduct of needing "#ifdef" and not "#if defined()"
699 * for unifdef. XXX MarkM
701 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
710 level = getterminaltype(user_name);
711 setenv("TERM", terminaltype ? terminaltype : "network", 1);
713 telnet(net, pty, remote_hostname); /* begin server process */
719 * Main loop. Select from pty and network, and
720 * hand data to telnet receiver finite state machine.
723 telnet(int f, int p, char *host)
726 #define TABBUFSIZ 512
727 char defent[TABBUFSIZ];
728 char defstrs[TABBUFSIZ];
736 * Initialize the slc mapping table.
741 * Do some tests where it is desireable to wait for a response.
742 * Rather than doing them slowly, one at a time, do them all
745 if (my_state_is_wont(TELOPT_SGA))
746 send_will(TELOPT_SGA, 1);
748 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
749 * because 4.2 clients are unable to deal with TCP urgent data.
751 * To find out, we send out a "DO ECHO". If the remote system
752 * answers "WILL ECHO" it is probably a 4.2 client, and we note
753 * that fact ("WILL ECHO" ==> that the client will echo what
754 * WE, the server, sends it; it does NOT mean that the client will
755 * echo the terminal input).
757 send_do(TELOPT_ECHO, 1);
760 if (his_state_is_wont(TELOPT_LINEMODE)) {
761 /* Query the peer for linemode support by trying to negotiate
762 * the linemode option.
766 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
768 #endif /* LINEMODE */
771 * Send along a couple of other options that we wish to negotiate.
773 send_do(TELOPT_NAWS, 1);
774 send_will(TELOPT_STATUS, 1);
775 flowmode = 1; /* default flow control state */
776 restartany = -1; /* uninitialized... */
777 send_do(TELOPT_LFLOW, 1);
780 * Spin, waiting for a response from the DO ECHO. However,
781 * some REALLY DUMB telnets out there might not respond
782 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
783 * telnets so far seem to respond with WONT for a DO that
784 * they don't understand...) because by the time we get the
785 * response, it will already have processed the DO ECHO.
786 * Kludge upon kludge.
788 while (his_will_wont_is_changing(TELOPT_NAWS))
793 * The client might have sent a WILL NAWS as part of its
794 * startup code; if so, we'll be here before we get the
795 * response to the DO ECHO. We'll make the assumption
796 * that any implementation that understands about NAWS
797 * is a modern enough implementation that it will respond
798 * to our DO ECHO request; hence we'll do another spin
799 * waiting for the ECHO option to settle down, which is
800 * what we wanted to do in the first place...
802 if (his_want_state_is_will(TELOPT_ECHO) &&
803 his_state_is_will(TELOPT_NAWS)) {
804 while (his_will_wont_is_changing(TELOPT_ECHO))
808 * On the off chance that the telnet client is broken and does not
809 * respond to the DO ECHO we sent, (after all, we did send the
810 * DO NAWS negotiation after the DO ECHO, and we won't get here
811 * until a response to the DO NAWS comes back) simulate the
812 * receipt of a will echo. This will also send a WONT ECHO
813 * to the client, since we assume that the client failed to
814 * respond because it believes that it is already in DO ECHO
815 * mode, which we do not want.
817 if (his_want_state_is_will(TELOPT_ECHO)) {
818 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
819 willoption(TELOPT_ECHO);
823 * Finally, to clean things up, we turn on our echo. This
824 * will break stupid 4.2 telnets out of local terminal echo.
827 if (my_state_is_wont(TELOPT_ECHO))
828 send_will(TELOPT_ECHO, 1);
831 * Turn on packet mode
833 (void) ioctl(p, TIOCPKT, (char *)&on);
835 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
837 * Continuing line mode support. If client does not support
838 * real linemode, attempt to negotiate kludge linemode by sending
839 * the do timing mark sequence.
841 if (lmodetype < REAL_LINEMODE)
842 send_do(TELOPT_TM, 1);
843 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
846 * Call telrcv() once to pick up anything received during
847 * terminal type negotiation, 4.2/4.3 determination, and
848 * linemode negotiation.
852 (void) ioctl(f, FIONBIO, (char *)&on);
853 (void) ioctl(p, FIONBIO, (char *)&on);
855 #if defined(SO_OOBINLINE)
856 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
857 (char *)&on, sizeof on);
858 #endif /* defined(SO_OOBINLINE) */
861 (void) signal(SIGTSTP, SIG_IGN);
865 * Ignoring SIGTTOU keeps the kernel from blocking us
866 * in ttioct() in /sys/tty.c.
868 (void) signal(SIGTTOU, SIG_IGN);
871 (void) signal(SIGCHLD, cleanup);
876 t = open(_PATH_TTY, O_RDWR);
878 (void) ioctl(t, TIOCNOTTY, (char *)0);
885 * Show banner that getty never gave.
887 * We put the banner in the pty input buffer. This way, it
888 * gets carriage return null processing, etc., just like all
889 * other pty --> client data.
892 if (getent(defent, "default") == 1) {
895 HE = Getstr("he", &cp);
896 HN = Getstr("hn", &cp);
897 IM = Getstr("im", &cp);
899 (void) strlcpy(host_name, HN, sizeof(host_name));
903 IM = strdup(DEFAULT_IM);
906 edithost(HE, host_name);
911 (void) strncat(ptyibuf2, ptyip, pcc+1);
916 * Last check to make sure all our states are correct.
920 #endif /* LINEMODE */
922 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
925 * Startup the login process on the slave side of the terminal
926 * now. We delay this until here to insure option negotiation
929 startslave(host, level, user_name);
931 nfd = ((f > p) ? f : p) + 1;
933 fd_set ibits, obits, xbits;
936 if (ncc < 0 && pcc < 0)
943 * Never look for input if there's still
944 * stuff in the corresponding output buffer
946 if (nfrontp - nbackp || pcc > 0) {
951 if (pfrontp - pbackp || ncc > 0) {
959 if ((c = select(nfd, &ibits, &obits, &xbits,
960 (struct timeval *)0)) < 1) {
962 if (errno == EINTR) {
973 if (FD_ISSET(net, &xbits)) {
978 * Something to read from the network...
980 if (FD_ISSET(net, &ibits)) {
981 #if !defined(SO_OOBINLINE)
983 * In 4.2 (and 4.3 beta) systems, the
984 * OOB indication and data handling in the kernel
985 * is such that if two separate TCP Urgent requests
986 * come in, one byte of TCP data will be overlaid.
987 * This is fatal for Telnet, but we try to live
990 * In addition, in 4.2 (and...), a special protocol
991 * is needed to pick up the TCP Urgent data in
992 * the correct sequence.
994 * What we do is: if we think we are in urgent
995 * mode, we look to see if we are "at the mark".
996 * If we are, we do an OOB receive. If we run
997 * this twice, we will do the OOB receive twice,
998 * but the second will fail, since the second
999 * time we were "at the mark", but there wasn't
1000 * any data there (the kernel doesn't reset
1001 * "at the mark" until we do a normal read).
1002 * Once we've read the OOB data, we go ahead
1003 * and do normal reads.
1005 * There is also another problem, which is that
1006 * since the OOB byte we read doesn't put us
1007 * out of OOB state, and since that byte is most
1008 * likely the TELNET DM (data mark), we would
1009 * stay in the TELNET SYNCH (SYNCHing) state.
1010 * So, clocks to the rescue. If we've "just"
1011 * received a DM, then we test for the
1012 * presence of OOB data when the receive OOB
1013 * fails (and AFTER we did the normal mode read
1014 * to clear "at the mark").
1019 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1021 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1022 if ((ncc == -1) && (errno == EINVAL)) {
1023 ncc = read(net, netibuf, sizeof (netibuf));
1024 if (sequenceIs(didnetreceive, gotDM)) {
1025 SYNCHing = stilloob(net);
1029 ncc = read(net, netibuf, sizeof (netibuf));
1032 ncc = read(net, netibuf, sizeof (netibuf));
1034 settimer(didnetreceive);
1035 #else /* !defined(SO_OOBINLINE)) */
1036 ncc = read(net, netibuf, sizeof (netibuf));
1037 #endif /* !defined(SO_OOBINLINE)) */
1038 if (ncc < 0 && errno == EWOULDBLOCK)
1046 DIAG((TD_REPORT | TD_NETDATA),
1047 output_data("td: netread %d chars\r\n", ncc));
1048 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1052 * Something to read from the pty...
1054 if (FD_ISSET(p, &ibits)) {
1055 pcc = read(p, ptyibuf, BUFSIZ);
1057 * On some systems, if we try to read something
1058 * off the master side before the slave side is
1059 * opened, we get EIO.
1061 if (pcc < 0 && (errno == EWOULDBLOCK ||
1072 * If ioctl from pty, pass it through net
1074 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1075 copy_termbuf(ptyibuf+1, pcc-1);
1079 #endif /* LINEMODE */
1080 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1081 netclear(); /* clear buffer back */
1084 * There are client telnets on some
1085 * operating systems get screwed up
1086 * royally if we send them urgent
1089 output_data("%c%c", IAC, DM);
1090 neturg = nfrontp-1; /* off by one XXX */
1092 printoption("td: send IAC", DM));
1096 if (his_state_is_will(TELOPT_LFLOW) &&
1098 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1100 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1101 if (newflow != flowmode) {
1103 output_data("%c%c%c%c%c%c",
1104 IAC, SB, TELOPT_LFLOW,
1108 DIAG(TD_OPTIONS, printsub('>',
1109 (unsigned char *)nfrontp-4,
1119 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1121 c = *ptyip++ & 0377, pcc--;
1123 output_data("%c", c);
1124 output_data("%c", c);
1125 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1126 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1127 output_data("%c", *ptyip++ & 0377);
1130 output_data("%c", '\0');
1134 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1138 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1142 } /* end of telnet */
1146 # define TCSIG TIOCSIG
1151 * Send interrupt to process on other side of pty.
1152 * If it is in raw mode, just write NULL;
1153 * otherwise, write intr char.
1158 ptyflush(); /* half-hearted */
1161 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1164 *pfrontp++ = slctab[SLC_IP].sptr ?
1165 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1170 * Send quit to process on other side of pty.
1171 * If it is in raw mode, just write NULL;
1172 * otherwise, write quit char.
1177 ptyflush(); /* half-hearted */
1179 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1182 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1183 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1191 ptyflush(); /* half-hearted */
1193 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1195 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1196 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1198 #endif /* SIGTSTP */
1202 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1203 * just send back "[Yes]".
1208 #if defined(SIGINFO) && defined(TCSIG)
1209 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1210 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1214 output_data("\r\n[Yes]\r\n");
1222 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1223 if (!tty_isediting()) {
1224 extern char oldeofc;
1225 *pfrontp++ = oldeofc;
1229 *pfrontp++ = slctab[SLC_EOF].sptr ?
1230 (unsigned char)*slctab[SLC_EOF].sptr : '\004';