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"
51 #include <arpa/inet.h>
54 #include <libtelnet/auth.h>
58 #include <libtelnet/encrypt.h>
60 #include <libtelnet/misc.h>
62 char remote_hostname[MAXHOSTNAMELEN];
63 size_t utmp_len = sizeof(remote_hostname) - 1;
64 int registerd_host_only = 0;
69 * pointers, and counters.
71 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
72 char ptyibuf2[BUFSIZ];
74 int readstream(int, char *, int);
75 void doit(struct sockaddr *);
76 int terminaltypeok(char *);
78 int hostinfo = 1; /* do we print login banner? */
84 void doit(struct sockaddr *);
85 int terminaltypeok(char *);
86 void startslave(char *, int, char *);
87 extern void usage(void);
88 static void _gettermname(void);
91 * The string to pass to getopt(). We do it this way so
92 * that only the actual options that we support will be
93 * passed off to getopt().
96 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
116 int family = AF_INET;
118 #ifndef MAXHOSTNAMELEN
119 #define MAXHOSTNAMELEN 256
120 #endif /* MAXHOSTNAMELEN */
123 char host_name[MAXHOSTNAMELEN];
125 extern void telnet(int, int, char *);
131 main(int argc, char *argv[])
134 struct sockaddr_storage from;
137 #if defined(IPPROTO_IP) && defined(IP_TOS)
142 pfrontp = pbackp = ptyobuf;
144 nfrontp = nbackp = netobuf;
147 #endif /* ENCRYPTION */
150 * This initialization causes linemode to default to a configuration
151 * that works on all telnet clients, including the FreeBSD client.
152 * This is not quite the same as the telnet client issuing a "mode
153 * character" command, but has most of the same benefits, and is
154 * preferable since some clients (like usofts) don't have the
155 * mode character command anyway and linemode breaks things.
156 * The most notable symptom of fix is that csh "set filec" operations
157 * like <ESC> (filename completion) and ^D (choices) keys now work
158 * in telnet sessions and can be used more than once on the same line.
159 * CR/LF handling is also corrected in some termio modes. This
160 * change resolves problem reports bin/771 and bin/1037.
163 linemode=1; /*Default to mode that works on bulk of clients*/
165 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
168 #ifdef AUTHENTICATION
171 * Check for required authentication level
173 if (strcmp(optarg, "debug") == 0) {
174 extern int auth_debug_mode;
176 } else if (strcasecmp(optarg, "none") == 0) {
178 } else if (strcasecmp(optarg, "other") == 0) {
179 auth_level = AUTH_OTHER;
180 } else if (strcasecmp(optarg, "user") == 0) {
181 auth_level = AUTH_USER;
182 } else if (strcasecmp(optarg, "valid") == 0) {
183 auth_level = AUTH_VALID;
184 } else if (strcasecmp(optarg, "off") == 0) {
186 * This hack turns off authentication
190 warnx("unknown authorization level for -a");
193 #endif /* AUTHENTICATION */
199 #endif /* BFTPDAEMON */
202 if (strcmp(optarg, "ebug") == 0) {
213 * Check for desired diagnostics capabilities.
215 if (!strcmp(optarg, "report")) {
216 diagnostic |= TD_REPORT|TD_OPTIONS;
217 } else if (!strcmp(optarg, "exercise")) {
218 diagnostic |= TD_EXERCISE;
219 } else if (!strcmp(optarg, "netdata")) {
220 diagnostic |= TD_NETDATA;
221 } else if (!strcmp(optarg, "ptydata")) {
222 diagnostic |= TD_PTYDATA;
223 } else if (!strcmp(optarg, "options")) {
224 diagnostic |= TD_OPTIONS;
230 #endif /* DIAGNOSTICS */
234 if (strcmp(optarg, "debug") == 0) {
235 extern int encrypt_debug_mode;
236 encrypt_debug_mode = 1;
242 #endif /* ENCRYPTION */
252 #endif /* LINEMODE */
255 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
256 lmodetype = NO_AUTOKLUDGE;
258 /* ignore -k option if built without kludge linemode */
259 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
272 if ((tos = parsetos(optarg, "tcp")) < 0)
274 "bad TOS argument '", optarg,
275 "'; will try to use default TOS");
278 ultmp = strtoul(optarg, &ep, 0);
279 if (*ep || ep == optarg || ultmp > MAXTOS)
281 "bad TOS argument '", optarg,
282 "'; will try to use default TOS");
289 utmp_len = (size_t)atoi(optarg);
290 if (utmp_len >= sizeof(remote_hostname))
291 utmp_len = sizeof(remote_hostname) - 1;
295 registerd_host_only = 1;
298 #ifdef AUTHENTICATION
301 * Check for invalid authentication types
303 auth_disable_name(optarg);
305 #endif /* AUTHENTICATION */
318 warnx("%c: unknown option", ch);
330 int s, ns, foo, error;
331 const char *service = "telnet";
332 struct addrinfo hints, *res;
337 } else if (argc == 1)
340 memset(&hints, 0, sizeof(hints));
341 hints.ai_flags = AI_PASSIVE;
342 hints.ai_family = family;
343 hints.ai_socktype = SOCK_STREAM;
344 hints.ai_protocol = 0;
345 error = getaddrinfo(NULL, service, &hints, &res);
348 errx(1, "tcp/%s: %s\n", service, gai_strerror(error));
349 if (error == EAI_SYSTEM)
350 errx(1, "tcp/%s: %s\n", service, strerror(errno));
354 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
357 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
358 (char *)&on, sizeof(on));
360 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
361 (char *)&on, sizeof(on));
362 if (bind(s, res->ai_addr, res->ai_addrlen) < 0)
364 if (listen(s, 1) < 0)
366 foo = res->ai_addrlen;
367 ns = accept(s, res->ai_addr, &foo);
370 (void) setsockopt(ns, SOL_SOCKET, SO_DEBUG,
371 (char *)&on, sizeof(on));
376 } else if (argc == 1) {
377 ; /* VOID*/ /* Just ignore the host/port name */
379 } else if (argc > 0) {
384 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
385 fromlen = sizeof (from);
386 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
391 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
392 (char *)&on, sizeof (on)) < 0) {
393 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
396 #if defined(IPPROTO_IP) && defined(IP_TOS)
397 if (from.ss_family == AF_INET) {
398 # if defined(HAS_GETTOS)
400 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
404 tos = 020; /* Low Delay bit */
406 && (setsockopt(0, IPPROTO_IP, IP_TOS,
407 (char *)&tos, sizeof(tos)) < 0)
408 && (errno != ENOPROTOOPT) )
409 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
411 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
413 doit((struct sockaddr *)&from);
421 fprintf(stderr, "usage: telnetd");
422 #ifdef AUTHENTICATION
424 " [-4] [-6] [-a (debug|other|user|valid|off|none)]\n\t");
427 fprintf(stderr, " [-B]");
429 fprintf(stderr, " [-debug]");
431 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
433 #ifdef AUTHENTICATION
434 fprintf(stderr, " [-edebug]");
436 fprintf(stderr, " [-h]");
437 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
438 fprintf(stderr, " [-k]");
441 fprintf(stderr, " [-l]");
443 fprintf(stderr, " [-n]");
444 fprintf(stderr, "\n\t");
446 fprintf(stderr, " [-S tos]");
448 #ifdef AUTHENTICATION
449 fprintf(stderr, " [-X auth-type]");
451 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
452 fprintf(stderr, " [port]\n");
459 * Ask the other end to send along its terminal type and speed.
460 * Output is the variable terminaltype filled in.
462 static unsigned char ttytype_sbbuf[] = {
463 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
467 #ifndef AUTHENTICATION
468 #define undef2 __unused
474 getterminaltype(char *name undef2)
479 #ifdef AUTHENTICATION
481 * Handle the Authentication option before we do anything else.
483 if (auth_level >= 0) {
484 send_do(TELOPT_AUTHENTICATION, 1);
485 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
487 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
488 retval = auth_wait(name);
494 send_will(TELOPT_ENCRYPT, 1);
495 #endif /* ENCRYPTION */
496 send_do(TELOPT_TTYPE, 1);
497 send_do(TELOPT_TSPEED, 1);
498 send_do(TELOPT_XDISPLOC, 1);
499 send_do(TELOPT_NEW_ENVIRON, 1);
500 send_do(TELOPT_OLD_ENVIRON, 1);
503 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
504 #endif /* ENCRYPTION */
505 his_will_wont_is_changing(TELOPT_TTYPE) ||
506 his_will_wont_is_changing(TELOPT_TSPEED) ||
507 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
508 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
509 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
514 * Wait for the negotiation of what type of encryption we can
515 * send with. If autoencrypt is not set, this will just return.
517 if (his_state_is_will(TELOPT_ENCRYPT)) {
520 #endif /* ENCRYPTION */
521 if (his_state_is_will(TELOPT_TSPEED)) {
522 static unsigned char sb[] =
523 { IAC, SB, TELOPT_TSPEED, 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_XDISPLOC)) {
529 static unsigned char sb[] =
530 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
532 output_datalen(sb, sizeof sb);
533 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
535 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
536 static unsigned char sb[] =
537 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
539 output_datalen(sb, sizeof sb);
540 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
542 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
543 static unsigned char sb[] =
544 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
546 output_datalen(sb, sizeof sb);
547 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
549 if (his_state_is_will(TELOPT_TTYPE)) {
551 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
552 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
553 sizeof ttytype_sbbuf - 2););
555 if (his_state_is_will(TELOPT_TSPEED)) {
556 while (sequenceIs(tspeedsubopt, baseline))
559 if (his_state_is_will(TELOPT_XDISPLOC)) {
560 while (sequenceIs(xdisplocsubopt, baseline))
563 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
564 while (sequenceIs(environsubopt, baseline))
567 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
568 while (sequenceIs(oenvironsubopt, baseline))
571 if (his_state_is_will(TELOPT_TTYPE)) {
572 char first[256], last[256];
574 while (sequenceIs(ttypesubopt, baseline))
578 * If the other side has already disabled the option, then
579 * we have to just go with what we (might) have already gotten.
581 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
582 (void) strncpy(first, terminaltype, sizeof(first)-1);
583 first[sizeof(first)-1] = '\0';
586 * Save the unknown name, and request the next name.
588 (void) strncpy(last, terminaltype, sizeof(last)-1);
589 last[sizeof(last)-1] = '\0';
591 if (terminaltypeok(terminaltype))
593 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
594 his_state_is_wont(TELOPT_TTYPE)) {
596 * We've hit the end. If this is the same as
597 * the first name, just go with it.
599 if (strncmp(first, terminaltype, sizeof(first)) == 0)
602 * Get the terminal name one more time, so that
603 * RFC1091 compliant telnets will cycle back to
604 * the start of the list.
607 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
608 (void) strncpy(terminaltype, first, sizeof(terminaltype)-1);
609 terminaltype[sizeof(terminaltype)-1] = '\0';
617 } /* end of getterminaltype */
623 * If the client turned off the option,
624 * we can't send another request, so we
627 if (his_state_is_wont(TELOPT_TTYPE))
630 output_datalen(ttytype_sbbuf, sizeof ttytype_sbbuf);
631 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
632 sizeof ttytype_sbbuf - 2););
633 while (sequenceIs(ttypesubopt, baseline))
638 terminaltypeok(char *s)
642 if (terminaltype == NULL)
646 * tgetent() will return 1 if the type is known, and
647 * 0 if it is not known. If it returns -1, it couldn't
648 * open the database. But if we can't open the database,
649 * it won't help to say we failed, because we won't be
650 * able to verify anything else. So, we treat -1 like 1.
652 if (tgetent(buf, s) == 0)
658 * Get a pty, scan input lines.
661 doit(struct sockaddr *who)
667 * Find an available pty to use.
670 pty = getpty(&ptynum);
672 fatal(net, "All network ports in use");
677 if ((lp = getpty()) == NULL)
678 fatal(net, "Out of ptys");
680 if ((pty = open(lp, 2)) >= 0) {
681 strlcpy(line,lp,sizeof(line));
688 /* get name of connected client */
689 if (realhostname_sa(remote_hostname, sizeof(remote_hostname) - 1,
690 who, who->sa_len) == HOSTNAME_INVALIDADDR && registerd_host_only)
691 fatal(net, "Couldn't resolve your address into a host name.\r\n\
692 Please contact your net administrator");
693 remote_hostname[sizeof(remote_hostname) - 1] = '\0';
695 if (!isdigit(remote_hostname[0]) && strlen(remote_hostname) > utmp_len)
696 err_ = getnameinfo(who, who->sa_len, remote_hostname,
697 sizeof(remote_hostname), NULL, 0,
699 /* XXX: do 'err_' check */
701 (void) gethostname(host_name, sizeof(host_name) - 1);
702 host_name[sizeof(host_name) - 1] = '\0';
703 hostname = host_name;
705 #ifdef AUTHENTICATION
707 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
708 * This is a byproduct of needing "#ifdef" and not "#if defined()"
709 * for unifdef. XXX MarkM
711 auth_encrypt_init(hostname, remote_hostname, "TELNETD", 1);
720 level = getterminaltype(user_name);
721 setenv("TERM", terminaltype ? terminaltype : "network", 1);
723 telnet(net, pty, remote_hostname); /* begin server process */
729 * Main loop. Select from pty and network, and
730 * hand data to telnet receiver finite state machine.
733 telnet(int f, int p, char *host)
736 #define TABBUFSIZ 512
737 char defent[TABBUFSIZ];
738 char defstrs[TABBUFSIZ];
750 * Initialize the slc mapping table.
755 * Do some tests where it is desireable to wait for a response.
756 * Rather than doing them slowly, one at a time, do them all
759 if (my_state_is_wont(TELOPT_SGA))
760 send_will(TELOPT_SGA, 1);
762 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
763 * because 4.2 clients are unable to deal with TCP urgent data.
765 * To find out, we send out a "DO ECHO". If the remote system
766 * answers "WILL ECHO" it is probably a 4.2 client, and we note
767 * that fact ("WILL ECHO" ==> that the client will echo what
768 * WE, the server, sends it; it does NOT mean that the client will
769 * echo the terminal input).
771 send_do(TELOPT_ECHO, 1);
774 if (his_state_is_wont(TELOPT_LINEMODE)) {
775 /* Query the peer for linemode support by trying to negotiate
776 * the linemode option.
780 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
782 #endif /* LINEMODE */
785 * Send along a couple of other options that we wish to negotiate.
787 send_do(TELOPT_NAWS, 1);
788 send_will(TELOPT_STATUS, 1);
789 flowmode = 1; /* default flow control state */
790 restartany = -1; /* uninitialized... */
791 send_do(TELOPT_LFLOW, 1);
794 * Spin, waiting for a response from the DO ECHO. However,
795 * some REALLY DUMB telnets out there might not respond
796 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
797 * telnets so far seem to respond with WONT for a DO that
798 * they don't understand...) because by the time we get the
799 * response, it will already have processed the DO ECHO.
800 * Kludge upon kludge.
802 while (his_will_wont_is_changing(TELOPT_NAWS))
807 * The client might have sent a WILL NAWS as part of its
808 * startup code; if so, we'll be here before we get the
809 * response to the DO ECHO. We'll make the assumption
810 * that any implementation that understands about NAWS
811 * is a modern enough implementation that it will respond
812 * to our DO ECHO request; hence we'll do another spin
813 * waiting for the ECHO option to settle down, which is
814 * what we wanted to do in the first place...
816 if (his_want_state_is_will(TELOPT_ECHO) &&
817 his_state_is_will(TELOPT_NAWS)) {
818 while (his_will_wont_is_changing(TELOPT_ECHO))
822 * On the off chance that the telnet client is broken and does not
823 * respond to the DO ECHO we sent, (after all, we did send the
824 * DO NAWS negotiation after the DO ECHO, and we won't get here
825 * until a response to the DO NAWS comes back) simulate the
826 * receipt of a will echo. This will also send a WONT ECHO
827 * to the client, since we assume that the client failed to
828 * respond because it believes that it is already in DO ECHO
829 * mode, which we do not want.
831 if (his_want_state_is_will(TELOPT_ECHO)) {
832 DIAG(TD_OPTIONS, output_data("td: simulating recv\r\n"));
833 willoption(TELOPT_ECHO);
837 * Finally, to clean things up, we turn on our echo. This
838 * will break stupid 4.2 telnets out of local terminal echo.
841 if (my_state_is_wont(TELOPT_ECHO))
842 send_will(TELOPT_ECHO, 1);
845 * Turn on packet mode
847 (void) ioctl(p, TIOCPKT, (char *)&on);
849 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
851 * Continuing line mode support. If client does not support
852 * real linemode, attempt to negotiate kludge linemode by sending
853 * the do timing mark sequence.
855 if (lmodetype < REAL_LINEMODE)
856 send_do(TELOPT_TM, 1);
857 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
860 * Call telrcv() once to pick up anything received during
861 * terminal type negotiation, 4.2/4.3 determination, and
862 * linemode negotiation.
866 (void) ioctl(f, FIONBIO, (char *)&on);
867 (void) ioctl(p, FIONBIO, (char *)&on);
869 #if defined(SO_OOBINLINE)
870 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
871 (char *)&on, sizeof on);
872 #endif /* defined(SO_OOBINLINE) */
875 (void) signal(SIGTSTP, SIG_IGN);
879 * Ignoring SIGTTOU keeps the kernel from blocking us
880 * in ttioct() in /sys/tty.c.
882 (void) signal(SIGTTOU, SIG_IGN);
885 (void) signal(SIGCHLD, cleanup);
890 t = open(_PATH_TTY, O_RDWR);
892 (void) ioctl(t, TIOCNOTTY, (char *)0);
899 * Show banner that getty never gave.
901 * We put the banner in the pty input buffer. This way, it
902 * gets carriage return null processing, etc., just like all
903 * other pty --> client data.
906 if (getent(defent, "default") == 1) {
909 HE = Getstr("he", &cp);
910 HN = Getstr("hn", &cp);
911 IM = Getstr("im", &cp);
912 IF = Getstr("if", &cp);
914 (void) strlcpy(host_name, HN, sizeof(host_name));
916 if_fd = open(IF, O_RDONLY, 000);
922 IM = strdup(DEFAULT_IM);
925 edithost(HE, host_name);
928 if (IF && if_fd != -1) {
929 if (fstat(if_fd, &statbuf) != -1 && statbuf.st_size > 0) {
930 if_buf = (char *) mmap (0, statbuf.st_size,
931 PROT_READ, 0, if_fd, 0);
932 if (if_buf != MAP_FAILED) {
933 putf(if_buf, ptyibuf2);
934 munmap(if_buf, statbuf.st_size);
941 (void) strncat(ptyibuf2, ptyip, pcc+1);
946 * Last check to make sure all our states are correct.
950 #endif /* LINEMODE */
952 DIAG(TD_REPORT, output_data("td: Entering processing loop\r\n"));
955 * Startup the login process on the slave side of the terminal
956 * now. We delay this until here to insure option negotiation
959 startslave(host, level, user_name);
961 nfd = ((f > p) ? f : p) + 1;
963 fd_set ibits, obits, xbits;
966 if (ncc < 0 && pcc < 0)
973 * Never look for input if there's still
974 * stuff in the corresponding output buffer
976 if (nfrontp - nbackp || pcc > 0) {
981 if (pfrontp - pbackp || ncc > 0) {
989 if ((c = select(nfd, &ibits, &obits, &xbits,
990 (struct timeval *)0)) < 1) {
992 if (errno == EINTR) {
1003 if (FD_ISSET(net, &xbits)) {
1008 * Something to read from the network...
1010 if (FD_ISSET(net, &ibits)) {
1011 #if !defined(SO_OOBINLINE)
1013 * In 4.2 (and 4.3 beta) systems, the
1014 * OOB indication and data handling in the kernel
1015 * is such that if two separate TCP Urgent requests
1016 * come in, one byte of TCP data will be overlaid.
1017 * This is fatal for Telnet, but we try to live
1020 * In addition, in 4.2 (and...), a special protocol
1021 * is needed to pick up the TCP Urgent data in
1022 * the correct sequence.
1024 * What we do is: if we think we are in urgent
1025 * mode, we look to see if we are "at the mark".
1026 * If we are, we do an OOB receive. If we run
1027 * this twice, we will do the OOB receive twice,
1028 * but the second will fail, since the second
1029 * time we were "at the mark", but there wasn't
1030 * any data there (the kernel doesn't reset
1031 * "at the mark" until we do a normal read).
1032 * Once we've read the OOB data, we go ahead
1033 * and do normal reads.
1035 * There is also another problem, which is that
1036 * since the OOB byte we read doesn't put us
1037 * out of OOB state, and since that byte is most
1038 * likely the TELNET DM (data mark), we would
1039 * stay in the TELNET SYNCH (SYNCHing) state.
1040 * So, clocks to the rescue. If we've "just"
1041 * received a DM, then we test for the
1042 * presence of OOB data when the receive OOB
1043 * fails (and AFTER we did the normal mode read
1044 * to clear "at the mark").
1049 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1051 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1052 if ((ncc == -1) && (errno == EINVAL)) {
1053 ncc = read(net, netibuf, sizeof (netibuf));
1054 if (sequenceIs(didnetreceive, gotDM)) {
1055 SYNCHing = stilloob(net);
1059 ncc = read(net, netibuf, sizeof (netibuf));
1062 ncc = read(net, netibuf, sizeof (netibuf));
1064 settimer(didnetreceive);
1065 #else /* !defined(SO_OOBINLINE)) */
1066 ncc = read(net, netibuf, sizeof (netibuf));
1067 #endif /* !defined(SO_OOBINLINE)) */
1068 if (ncc < 0 && errno == EWOULDBLOCK)
1076 DIAG((TD_REPORT | TD_NETDATA),
1077 output_data("td: netread %d chars\r\n", ncc));
1078 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1082 * Something to read from the pty...
1084 if (FD_ISSET(p, &ibits)) {
1085 pcc = read(p, ptyibuf, BUFSIZ);
1087 * On some systems, if we try to read something
1088 * off the master side before the slave side is
1089 * opened, we get EIO.
1091 if (pcc < 0 && (errno == EWOULDBLOCK ||
1102 * If ioctl from pty, pass it through net
1104 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1105 copy_termbuf(ptyibuf+1, pcc-1);
1109 #endif /* LINEMODE */
1110 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1111 netclear(); /* clear buffer back */
1114 * There are client telnets on some
1115 * operating systems get screwed up
1116 * royally if we send them urgent
1119 output_data("%c%c", IAC, DM);
1120 neturg = nfrontp-1; /* off by one XXX */
1122 printoption("td: send IAC", DM));
1126 if (his_state_is_will(TELOPT_LFLOW) &&
1128 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1130 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1131 if (newflow != flowmode) {
1133 output_data("%c%c%c%c%c%c",
1134 IAC, SB, TELOPT_LFLOW,
1138 DIAG(TD_OPTIONS, printsub('>',
1139 (unsigned char *)nfrontp-4,
1149 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1151 c = *ptyip++ & 0377, pcc--;
1153 output_data("%c", c);
1154 output_data("%c", c);
1155 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1156 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1157 output_data("%c", *ptyip++ & 0377);
1160 output_data("%c", '\0');
1164 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1168 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1172 } /* end of telnet */
1176 # define TCSIG TIOCSIG
1181 * Send interrupt to process on other side of pty.
1182 * If it is in raw mode, just write NULL;
1183 * otherwise, write intr char.
1188 ptyflush(); /* half-hearted */
1191 (void) ioctl(pty, TCSIG, SIGINT);
1194 *pfrontp++ = slctab[SLC_IP].sptr ?
1195 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1200 * Send quit to process on other side of pty.
1201 * If it is in raw mode, just write NULL;
1202 * otherwise, write quit char.
1207 ptyflush(); /* half-hearted */
1209 (void) ioctl(pty, TCSIG, SIGQUIT);
1212 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1213 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1221 ptyflush(); /* half-hearted */
1223 (void) ioctl(pty, TCSIG, SIGTSTP);
1225 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1226 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1228 #endif /* SIGTSTP */
1232 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1233 * just send back "[Yes]".
1238 #if defined(SIGINFO) && defined(TCSIG)
1239 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1240 (void) ioctl(pty, TCSIG, SIGINFO);
1244 output_data("\r\n[Yes]\r\n");
1252 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1253 if (!tty_isediting()) {
1254 extern char oldeofc;
1255 *pfrontp++ = oldeofc;
1259 *pfrontp++ = slctab[SLC_EOF].sptr ?
1260 (unsigned char)*slctab[SLC_EOF].sptr : '\004';