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
33 * @(#)telnetd.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnetd/telnetd.c,v 1.11.2.5 2002/04/13 10:59:09 markm Exp $
38 #include "pathnames.h"
47 #include <arpa/inet.h>
50 #include <libtelnet/auth.h>
54 #include <libtelnet/encrypt.h>
56 #include <libtelnet/misc.h>
58 char remote_hostname[MAXHOSTNAMELEN];
59 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 *);
72 int terminaltypeok(char *);
74 int hostinfo = 1; /* do we print login banner? */
80 void doit(struct sockaddr *);
81 int terminaltypeok(char *);
82 void startslave(char *, int, char *);
83 extern void usage(void);
84 static void _gettermname(void);
87 * The string to pass to getopt(). We do it this way so
88 * that only the actual options that we support will be
89 * passed off to getopt().
92 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
112 int family = AF_INET;
114 #ifndef MAXHOSTNAMELEN
115 #define MAXHOSTNAMELEN 256
116 #endif /* MAXHOSTNAMELEN */
119 char host_name[MAXHOSTNAMELEN];
121 extern void telnet(int, int, char *);
127 main(int argc, char *argv[])
129 struct sockaddr_storage from;
132 #if defined(IPPROTO_IP) && defined(IP_TOS)
136 pfrontp = pbackp = ptyobuf;
138 nfrontp = nbackp = netobuf;
141 #endif /* ENCRYPTION */
144 * This initialization causes linemode to default to a configuration
145 * that works on all telnet clients, including the FreeBSD client.
146 * This is not quite the same as the telnet client issuing a "mode
147 * character" command, but has most of the same benefits, and is
148 * preferable since some clients (like usofts) don't have the
149 * mode character command anyway and linemode breaks things.
150 * The most notable symptom of fix is that csh "set filec" operations
151 * like <ESC> (filename completion) and ^D (choices) keys now work
152 * in telnet sessions and can be used more than once on the same line.
153 * CR/LF handling is also corrected in some termio modes. This
154 * change resolves problem reports bin/771 and bin/1037.
157 linemode=1; /*Default to mode that works on bulk of clients*/
159 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
162 #ifdef AUTHENTICATION
165 * Check for required authentication level
167 if (strcmp(optarg, "debug") == 0) {
168 extern int auth_debug_mode;
170 } else if (strcasecmp(optarg, "none") == 0) {
172 } else if (strcasecmp(optarg, "other") == 0) {
173 auth_level = AUTH_OTHER;
174 } else if (strcasecmp(optarg, "user") == 0) {
175 auth_level = AUTH_USER;
176 } else if (strcasecmp(optarg, "valid") == 0) {
177 auth_level = AUTH_VALID;
178 } else if (strcasecmp(optarg, "off") == 0) {
180 * This hack turns off authentication
184 warnx("unknown authorization level for -a");
187 #endif /* AUTHENTICATION */
193 #endif /* BFTPDAEMON */
196 if (strcmp(optarg, "ebug") == 0) {
207 * Check for desired diagnostics capabilities.
209 if (!strcmp(optarg, "report")) {
210 diagnostic |= TD_REPORT|TD_OPTIONS;
211 } else if (!strcmp(optarg, "exercise")) {
212 diagnostic |= TD_EXERCISE;
213 } else if (!strcmp(optarg, "netdata")) {
214 diagnostic |= TD_NETDATA;
215 } else if (!strcmp(optarg, "ptydata")) {
216 diagnostic |= TD_PTYDATA;
217 } else if (!strcmp(optarg, "options")) {
218 diagnostic |= TD_OPTIONS;
224 #endif /* DIAGNOSTICS */
228 if (strcmp(optarg, "debug") == 0) {
229 extern int encrypt_debug_mode;
230 encrypt_debug_mode = 1;
236 #endif /* ENCRYPTION */
246 #endif /* LINEMODE */
249 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
250 lmodetype = NO_AUTOKLUDGE;
252 /* ignore -k option if built without kludge linemode */
253 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
266 if ((tos = parsetos(optarg, "tcp")) < 0)
268 "bad TOS argument '", optarg,
269 "'; will try to use default TOS");
271 warnx("TOS option unavailable; -S flag not supported");
276 utmp_len = (size_t)atoi(optarg);
277 if (utmp_len >= sizeof(remote_hostname))
278 utmp_len = sizeof(remote_hostname) - 1;
282 registerd_host_only = 1;
285 #ifdef AUTHENTICATION
288 * Check for invalid authentication types
290 auth_disable_name(optarg);
292 #endif /* AUTHENTICATION */
305 warnx("%c: unknown option", ch);
317 int s, ns, foo, error;
318 const char *service = "telnet";
319 struct addrinfo hints, *res;
324 } else if (argc == 1)
327 memset(&hints, 0, sizeof(hints));
328 hints.ai_flags = AI_PASSIVE;
329 hints.ai_family = family;
330 hints.ai_socktype = SOCK_STREAM;
331 hints.ai_protocol = 0;
332 error = getaddrinfo(NULL, service, &hints, &res);
335 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
336 if (error == EAI_SYSTEM)
337 errx(1, "tcp/%s: %s\n", service, strerror(errno));
341 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
344 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
345 (char *)&on, sizeof(on));
346 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
348 if (listen(s, 1) < 0)
350 foo = res->ai_addrlen;
351 ns = accept(s, res->ai_addr, &foo);
358 } else if (argc == 1) {
359 ; /* VOID*/ /* Just ignore the host/port name */
361 } else if (argc > 0) {
366 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
367 fromlen = sizeof (from);
368 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
373 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
374 (char *)&on, sizeof (on)) < 0) {
375 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
378 #if defined(IPPROTO_IP) && defined(IP_TOS)
379 if (from.ss_family == AF_INET) {
380 # if defined(HAS_GETTOS)
382 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
386 tos = 020; /* Low Delay bit */
388 && (setsockopt(0, IPPROTO_IP, IP_TOS,
389 (char *)&tos, sizeof(tos)) < 0)
390 && (errno != ENOPROTOOPT) )
391 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
393 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
395 doit((struct sockaddr *)&from);
403 fprintf(stderr, "usage: telnetd");
404 #ifdef AUTHENTICATION
405 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
408 fprintf(stderr, " [-B]");
410 fprintf(stderr, " [-debug]");
412 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
414 #ifdef AUTHENTICATION
415 fprintf(stderr, " [-edebug]");
417 fprintf(stderr, " [-h]");
418 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
419 fprintf(stderr, " [-k]");
422 fprintf(stderr, " [-l]");
424 fprintf(stderr, " [-n]");
425 fprintf(stderr, "\n\t");
427 fprintf(stderr, " [-S tos]");
429 #ifdef AUTHENTICATION
430 fprintf(stderr, " [-X auth-type]");
432 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
433 fprintf(stderr, " [port]\n");
440 * Ask the other end to send along its terminal type and speed.
441 * Output is the variable terminaltype filled in.
443 static unsigned char ttytype_sbbuf[] = {
444 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
448 #ifndef AUTHENTICATION
449 #define undef2 __unused
455 getterminaltype(char *name undef2)
460 #ifdef AUTHENTICATION
462 * Handle the Authentication option before we do anything else.
464 send_do(TELOPT_AUTHENTICATION, 1);
465 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
467 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
468 retval = auth_wait(name);
473 send_will(TELOPT_ENCRYPT, 1);
474 #endif /* ENCRYPTION */
475 send_do(TELOPT_TTYPE, 1);
476 send_do(TELOPT_TSPEED, 1);
477 send_do(TELOPT_XDISPLOC, 1);
478 send_do(TELOPT_NEW_ENVIRON, 1);
479 send_do(TELOPT_OLD_ENVIRON, 1);
482 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
483 #endif /* ENCRYPTION */
484 his_will_wont_is_changing(TELOPT_TTYPE) ||
485 his_will_wont_is_changing(TELOPT_TSPEED) ||
486 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
487 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
488 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
493 * Wait for the negotiation of what type of encryption we can
494 * send with. If autoencrypt is not set, this will just return.
496 if (his_state_is_will(TELOPT_ENCRYPT)) {
499 #endif /* ENCRYPTION */
500 if (his_state_is_will(TELOPT_TSPEED)) {
501 static unsigned char sb[] =
502 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
504 output_datalen(sb, sizeof sb);
505 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
507 if (his_state_is_will(TELOPT_XDISPLOC)) {
508 static unsigned char sb[] =
509 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
511 output_datalen(sb, sizeof sb);
512 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
514 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
515 static unsigned char sb[] =
516 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
518 output_datalen(sb, sizeof sb);
519 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
521 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
522 static unsigned char sb[] =
523 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
525 output_datalen(sb, sizeof sb);
526 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
528 if (his_state_is_will(TELOPT_TTYPE)) {
530 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
531 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
532 sizeof ttytype_sbbuf - 2););
534 if (his_state_is_will(TELOPT_TSPEED)) {
535 while (sequenceIs(tspeedsubopt, baseline))
538 if (his_state_is_will(TELOPT_XDISPLOC)) {
539 while (sequenceIs(xdisplocsubopt, baseline))
542 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
543 while (sequenceIs(environsubopt, baseline))
546 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
547 while (sequenceIs(oenvironsubopt, baseline))
550 if (his_state_is_will(TELOPT_TTYPE)) {
551 char first[256], last[256];
553 while (sequenceIs(ttypesubopt, baseline))
557 * If the other side has already disabled the option, then
558 * we have to just go with what we (might) have already gotten.
560 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
561 (void) strncpy(first, terminaltype, sizeof(first)-1);
562 first[sizeof(first)-1] = '\0';
565 * Save the unknown name, and request the next name.
567 (void) strncpy(last, terminaltype, sizeof(last)-1);
568 last[sizeof(last)-1] = '\0';
570 if (terminaltypeok(terminaltype))
572 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
573 his_state_is_wont(TELOPT_TTYPE)) {
575 * We've hit the end. If this is the same as
576 * the first name, just go with it.
578 if (strncmp(first, terminaltype, sizeof(first)) == 0)
581 * Get the terminal name one more time, so that
582 * RFC1091 compliant telnets will cycle back to
583 * the start of the list.
586 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
587 (void) strncpy(terminaltype, first, TERMINAL_TYPE_SIZE-1);
588 terminaltype[TERMINAL_TYPE_SIZE-1] = '\0';
596 } /* end of getterminaltype */
602 * If the client turned off the option,
603 * we can't send another request, so we
606 if (his_state_is_wont(TELOPT_TTYPE))
609 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
610 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
611 sizeof ttytype_sbbuf - 2););
612 while (sequenceIs(ttypesubopt, baseline))
617 terminaltypeok(char *s)
621 if (terminaltype == NULL)
625 * tgetent() will return 1 if the type is known, and
626 * 0 if it is not known. If it returns -1, it couldn't
627 * open the database. But if we can't open the database,
628 * it won't help to say we failed, because we won't be
629 * able to verify anything else. So, we treat -1 like 1.
631 if (tgetent(buf, s) == 0)
637 * Get a pty, scan input lines.
640 doit(struct sockaddr *who)
646 * Find an available pty to use.
649 pty = getpty(&ptynum);
651 fatal(net, "All network ports in use");
656 if ((lp = getpty()) == NULL)
657 fatal(net, "Out of ptys");
659 if ((pty = open(lp, 2)) >= 0) {
660 strlcpy(line,lp,sizeof(line));
667 /* get name of connected client */
668 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
669 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
670 fatal(net, "Couldn't resolve your address into a host name.\r\n\
671 Please contact your net administrator");
672 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
674 trimdomain(remote_hostname, UT_HOSTSIZE);
675 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
676 err_ = getnameinfo(who, who->sa_len, remote_hostname,
677 sizeof(remote_hostname), NULL, 0,
678 NI_NUMERICHOST|NI_WITHSCOPEID);
679 /* XXX: do 'err_' check */
681 (void) gethostname(host_name, sizeof(host_name) - 1);
682 host_name[sizeof(host_name) - 1] = '\0';
683 hostname = host_name;
685 #ifdef AUTHENTICATION
687 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
688 * This is a byproduct of needing "#ifdef" and not "#if defined()"
689 * for unifdef. XXX MarkM
691 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
700 level = getterminaltype(user_name);
701 if (setenv("TERM", terminaltype ? terminaltype : "network", 1) == -1)
702 syslog(LOG_ERR, "setenv: cannot set TERM=%s: %m", terminaltype ? terminaltype : "network");
704 telnet(net, pty, remote_hostname); /* begin server process */
710 * Main loop. Select from pty and network, and
711 * hand data to telnet receiver finite state machine.
714 telnet(int f, int p, char *host)
717 #define TABBUFSIZ 512
718 char defent[TABBUFSIZ];
719 char defstrs[TABBUFSIZ];
727 * Initialize the slc mapping table.
732 * Do some tests where it is desireable to wait for a response.
733 * Rather than doing them slowly, one at a time, do them all
736 if (my_state_is_wont(TELOPT_SGA))
737 send_will(TELOPT_SGA, 1);
739 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
740 * because 4.2 clients are unable to deal with TCP urgent data.
742 * To find out, we send out a "DO ECHO". If the remote system
743 * answers "WILL ECHO" it is probably a 4.2 client, and we note
744 * that fact ("WILL ECHO" ==> that the client will echo what
745 * WE, the server, sends it; it does NOT mean that the client will
746 * echo the terminal input).
748 send_do(TELOPT_ECHO, 1);
751 if (his_state_is_wont(TELOPT_LINEMODE)) {
752 /* Query the peer for linemode support by trying to negotiate
753 * the linemode option.
757 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
759 #endif /* LINEMODE */
762 * Send along a couple of other options that we wish to negotiate.
764 send_do(TELOPT_NAWS, 1);
765 send_will(TELOPT_STATUS, 1);
766 flowmode = 1; /* default flow control state */
767 restartany = -1; /* uninitialized... */
768 send_do(TELOPT_LFLOW, 1);
771 * Spin, waiting for a response from the DO ECHO. However,
772 * some REALLY DUMB telnets out there might not respond
773 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
774 * telnets so far seem to respond with WONT for a DO that
775 * they don't understand...) because by the time we get the
776 * response, it will already have processed the DO ECHO.
777 * Kludge upon kludge.
779 while (his_will_wont_is_changing(TELOPT_NAWS))
784 * The client might have sent a WILL NAWS as part of its
785 * startup code; if so, we'll be here before we get the
786 * response to the DO ECHO. We'll make the assumption
787 * that any implementation that understands about NAWS
788 * is a modern enough implementation that it will respond
789 * to our DO ECHO request; hence we'll do another spin
790 * waiting for the ECHO option to settle down, which is
791 * what we wanted to do in the first place...
793 if (his_want_state_is_will(TELOPT_ECHO) &&
794 his_state_is_will(TELOPT_NAWS)) {
795 while (his_will_wont_is_changing(TELOPT_ECHO))
799 * On the off chance that the telnet client is broken and does not
800 * respond to the DO ECHO we sent, (after all, we did send the
801 * DO NAWS negotiation after the DO ECHO, and we won't get here
802 * until a response to the DO NAWS comes back) simulate the
803 * receipt of a will echo. This will also send a WONT ECHO
804 * to the client, since we assume that the client failed to
805 * respond because it believes that it is already in DO ECHO
806 * mode, which we do not want.
808 if (his_want_state_is_will(TELOPT_ECHO)) {
809 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
810 willoption(TELOPT_ECHO);
814 * Finally, to clean things up, we turn on our echo. This
815 * will break stupid 4.2 telnets out of local terminal echo.
818 if (my_state_is_wont(TELOPT_ECHO))
819 send_will(TELOPT_ECHO, 1);
822 * Turn on packet mode
824 (void) ioctl(p, TIOCPKT, (char *)&on);
826 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
828 * Continuing line mode support. If client does not support
829 * real linemode, attempt to negotiate kludge linemode by sending
830 * the do timing mark sequence.
832 if (lmodetype < REAL_LINEMODE)
833 send_do(TELOPT_TM, 1);
834 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
837 * Call telrcv() once to pick up anything received during
838 * terminal type negotiation, 4.2/4.3 determination, and
839 * linemode negotiation.
843 (void) ioctl(f, FIONBIO, (char *)&on);
844 (void) ioctl(p, FIONBIO, (char *)&on);
846 #if defined(SO_OOBINLINE)
847 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
848 (char *)&on, sizeof on);
849 #endif /* defined(SO_OOBINLINE) */
852 (void) signal(SIGTSTP, SIG_IGN);
856 * Ignoring SIGTTOU keeps the kernel from blocking us
857 * in ttioct() in /sys/tty.c.
859 (void) signal(SIGTTOU, SIG_IGN);
862 (void) signal(SIGCHLD, cleanup);
867 t = open(_PATH_TTY, O_RDWR);
869 (void) ioctl(t, TIOCNOTTY, NULL);
876 * Show banner that getty never gave.
878 * We put the banner in the pty input buffer. This way, it
879 * gets carriage return null processing, etc., just like all
880 * other pty --> client data.
883 if (getent(defent, "default") == 1) {
886 HE = Getstr("he", &cp);
887 HN = Getstr("hn", &cp);
888 IM = Getstr("im", &cp);
890 (void) strlcpy(host_name, HN, sizeof(host_name));
894 IM = strdup(DEFAULT_IM);
897 edithost(HE, host_name);
902 (void) strncat(ptyibuf2, ptyip, pcc+1);
907 * Last check to make sure all our states are correct.
911 #endif /* LINEMODE */
913 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
916 * Startup the login process on the slave side of the terminal
917 * now. We delay this until here to insure option negotiation
920 startslave(host, level, user_name);
922 nfd = ((f > p) ? f : p) + 1;
924 fd_set ibits, obits, xbits;
927 if (ncc < 0 && pcc < 0)
934 * Never look for input if there's still
935 * stuff in the corresponding output buffer
937 if (nfrontp - nbackp || pcc > 0) {
942 if (pfrontp - pbackp || ncc > 0) {
950 if ((c = select(nfd, &ibits, &obits, &xbits, NULL)) < 1) {
952 if (errno == EINTR) {
963 if (FD_ISSET(net, &xbits)) {
968 * Something to read from the network...
970 if (FD_ISSET(net, &ibits)) {
971 #if !defined(SO_OOBINLINE)
973 * In 4.2 (and 4.3 beta) systems, the
974 * OOB indication and data handling in the kernel
975 * is such that if two separate TCP Urgent requests
976 * come in, one byte of TCP data will be overlaid.
977 * This is fatal for Telnet, but we try to live
980 * In addition, in 4.2 (and...), a special protocol
981 * is needed to pick up the TCP Urgent data in
982 * the correct sequence.
984 * What we do is: if we think we are in urgent
985 * mode, we look to see if we are "at the mark".
986 * If we are, we do an OOB receive. If we run
987 * this twice, we will do the OOB receive twice,
988 * but the second will fail, since the second
989 * time we were "at the mark", but there wasn't
990 * any data there (the kernel doesn't reset
991 * "at the mark" until we do a normal read).
992 * Once we've read the OOB data, we go ahead
993 * and do normal reads.
995 * There is also another problem, which is that
996 * since the OOB byte we read doesn't put us
997 * out of OOB state, and since that byte is most
998 * likely the TELNET DM (data mark), we would
999 * stay in the TELNET SYNCH (SYNCHing) state.
1000 * So, clocks to the rescue. If we've "just"
1001 * received a DM, then we test for the
1002 * presence of OOB data when the receive OOB
1003 * fails (and AFTER we did the normal mode read
1004 * to clear "at the mark").
1009 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1011 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1012 if ((ncc == -1) && (errno == EINVAL)) {
1013 ncc = read(net, netibuf, sizeof (netibuf));
1014 if (sequenceIs(didnetreceive, gotDM)) {
1015 SYNCHing = stilloob(net);
1019 ncc = read(net, netibuf, sizeof (netibuf));
1022 ncc = read(net, netibuf, sizeof (netibuf));
1024 settimer(didnetreceive);
1025 #else /* !defined(SO_OOBINLINE)) */
1026 ncc = read(net, netibuf, sizeof (netibuf));
1027 #endif /* !defined(SO_OOBINLINE)) */
1028 if (ncc < 0 && errno == EWOULDBLOCK)
1036 DIAG((TD_REPORT | TD_NETDATA),
1037 output_data("td: netread %d chars\r\n", ncc));
1038 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1042 * Something to read from the pty...
1044 if (FD_ISSET(p, &ibits)) {
1045 pcc = read(p, ptyibuf, BUFSIZ);
1047 * On some systems, if we try to read something
1048 * off the master side before the slave side is
1049 * opened, we get EIO.
1051 if (pcc < 0 && (errno == EWOULDBLOCK ||
1062 * If ioctl from pty, pass it through net
1064 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1065 copy_termbuf(ptyibuf+1, pcc-1);
1069 #endif /* LINEMODE */
1070 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1071 netclear(); /* clear buffer back */
1074 * There are client telnets on some
1075 * operating systems get screwed up
1076 * royally if we send them urgent
1079 output_data("%c%c", IAC, DM);
1080 neturg = nfrontp-1; /* off by one XXX */
1082 printoption("td: send IAC", DM));
1086 if (his_state_is_will(TELOPT_LFLOW) &&
1088 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1090 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1091 if (newflow != flowmode) {
1093 output_data("%c%c%c%c%c%c",
1094 IAC, SB, TELOPT_LFLOW,
1098 DIAG(TD_OPTIONS, printsub('>',
1099 (unsigned char *)nfrontp-4,
1109 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1111 c = *ptyip++ & 0377, pcc--;
1113 output_data("%c", c);
1114 output_data("%c", c);
1115 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1116 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1117 output_data("%c", *ptyip++ & 0377);
1120 output_data("%c", '\0');
1124 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1128 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1132 } /* end of telnet */
1136 # define TCSIG TIOCSIG
1141 * Send interrupt to process on other side of pty.
1142 * If it is in raw mode, just write NULL;
1143 * otherwise, write intr char.
1148 ptyflush(); /* half-hearted */
1151 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1154 *pfrontp++ = slctab[SLC_IP].sptr ?
1155 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1160 * Send quit to process on other side of pty.
1161 * If it is in raw mode, just write NULL;
1162 * otherwise, write quit char.
1167 ptyflush(); /* half-hearted */
1169 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1172 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1173 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1181 ptyflush(); /* half-hearted */
1183 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1185 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1186 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1188 #endif /* SIGTSTP */
1192 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1193 * just send back "[Yes]".
1198 #if defined(SIGINFO) && defined(TCSIG)
1199 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1200 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1204 output_data("\r\n[Yes]\r\n");
1212 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1213 if (!tty_isediting()) {
1214 extern char oldeofc;
1215 *pfrontp++ = oldeofc;
1219 *pfrontp++ = slctab[SLC_EOF].sptr ?
1220 (unsigned char)*slctab[SLC_EOF].sptr : '\004';