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 RCSID("$Id: telnetd.c,v 1.69 2002/08/23 19:28:01 assar Exp $");
38 #ifdef _SC_CRAY_SECURE_SYS
40 #include <sys/secdev.h>
41 #include <sys/secparm.h>
48 #endif /* _SC_CRAY_SECURE_SYS */
55 int registerd_host_only = 0;
64 #endif /* HAVE_SYS_UIO_H */
65 #ifdef HAVE_SYS_STREAM_H
66 #include <sys/stream.h>
70 #include <sys/termio.h>
72 # ifdef HAVE_SYS_STRTTY_H
73 # include <sys/strtty.h>
75 # ifdef HAVE_SYS_STR_TTY_H
76 # include <sys/str_tty.h>
78 /* make sure we don't get the bsd version */
79 /* what is this here for? solaris? /joda */
80 # ifdef HAVE_SYS_TTY_H
81 # include "/usr/include/sys/tty.h"
83 # ifdef HAVE_SYS_PTYVAR_H
84 # include <sys/ptyvar.h>
88 * Because of the way ptyibuf is used with streams messages, we need
89 * ptyibuf+1 to be on a full-word boundary. The following wierdness
90 * is simply to make that happen.
92 long ptyibufbuf[BUFSIZ/sizeof(long)+1];
93 char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
94 char *ptyip = ((char *)&ptyibufbuf[1])-1;
95 char ptyibuf2[BUFSIZ];
96 unsigned char ctlbuf[BUFSIZ];
97 struct strbuf strbufc, strbufd;
99 int readstream(int, char*, int);
101 #else /* ! STREAMPTY */
105 * pointers, and counters.
107 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
108 char ptyibuf2[BUFSIZ];
110 #endif /* ! STREAMPTY */
112 int hostinfo = 1; /* do we print login banner? */
115 extern int newmap; /* nonzero if \n maps to ^M^J */
116 int lowpty = 0, highpty; /* low, high pty numbers */
123 static void usage (void);
126 * The string to pass to getopt(). We do it this way so
127 * that only the actual options that we support will be
128 * passed off to getopt().
130 char valid_opts[] = "Bd:hklnS:u:UL:y"
131 #ifdef AUTHENTICATION
142 static void doit(struct sockaddr*, int);
145 extern int des_check_key;
149 main(int argc, char **argv)
151 struct sockaddr_storage __ss;
152 struct sockaddr *sa = (struct sockaddr *)&__ss;
156 #if defined(IPPROTO_IP) && defined(IP_TOS)
160 des_check_key = 1; /* Kludge for Mac NCSA telnet 2.6 /bg */
162 pfrontp = pbackp = ptyobuf;
164 nfrontp = nbackp = netobuf;
173 * Get number of pty's before trying to process options,
174 * which may include changing pty range.
179 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
184 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
187 #ifdef AUTHENTICATION
190 * Check for required authentication level
192 if (strcmp(optarg, "debug") == 0) {
194 } else if (strcasecmp(optarg, "none") == 0) {
196 } else if (strcasecmp(optarg, "otp") == 0) {
199 } else if (strcasecmp(optarg, "other") == 0) {
200 auth_level = AUTH_OTHER;
201 } else if (strcasecmp(optarg, "user") == 0) {
202 auth_level = AUTH_USER;
203 } else if (strcasecmp(optarg, "valid") == 0) {
204 auth_level = AUTH_VALID;
205 } else if (strcasecmp(optarg, "off") == 0) {
207 * This hack turns off authentication
212 "telnetd: unknown authorization level for -a\n");
215 #endif /* AUTHENTICATION */
217 case 'B': /* BFTP mode is not supported any more */
220 if (strcmp(optarg, "ebug") == 0) {
231 * Check for desired diagnostics capabilities.
233 if (!strcmp(optarg, "report")) {
234 diagnostic |= TD_REPORT|TD_OPTIONS;
235 } else if (!strcmp(optarg, "exercise")) {
236 diagnostic |= TD_EXERCISE;
237 } else if (!strcmp(optarg, "netdata")) {
238 diagnostic |= TD_NETDATA;
239 } else if (!strcmp(optarg, "ptydata")) {
240 diagnostic |= TD_PTYDATA;
241 } else if (!strcmp(optarg, "options")) {
242 diagnostic |= TD_OPTIONS;
248 #endif /* DIAGNOSTICS */
255 case 'k': /* Linemode is not supported any more */
270 * Allow the specification of alterations
271 * to the pty search range. It is legal to
272 * specify only one, and not change the
273 * other from its default.
275 c = strchr(optarg, '-');
281 lowpty = atoi(optarg);
282 if ((lowpty > highpty) || (lowpty < 0) ||
293 if ((tos = parsetos(optarg, "tcp")) < 0)
294 fprintf(stderr, "%s%s%s\n",
295 "telnetd: Bad TOS argument '", optarg,
296 "'; will try to use default TOS");
298 fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
299 "-S flag not supported\n");
306 utmp_len = strtol(optarg, &eptr, 0);
308 fprintf(stderr, "telnetd: unknown utmp len (%s)\n", optarg);
313 registerd_host_only = 1;
316 #ifdef AUTHENTICATION
319 * Check for invalid authentication types
321 auth_disable_name(optarg);
327 #ifdef AUTHENTICATION
332 #endif /* AUTHENTICATION */
339 fprintf(stderr, "telnetd: %c: unknown option\n", ch);
356 } else if (argc == 1) {
357 sp = roken_getservbyname (*argv, "tcp");
361 port = htons(atoi(*argv));
364 port = krb5_getportbyname (NULL, "telnet", "tcp", 23);
366 port = k_getportbyname("telnet", "tcp", htons(23));
370 } else if (argc > 0) {
375 #ifdef _SC_CRAY_SECURE_SYS
376 secflag = sysconf(_SC_CRAY_SECURE_SYS);
379 * Get socket's security label
382 socklen_t szss = sizeof(ss);
384 socklen_t szi = sizeof(int);
386 memset(&dv, 0, sizeof(dv));
388 if (getsysv(&sysv, sizeof(struct sysv)) != 0)
389 fatalperror(net, "getsysv");
392 * Get socket security label and set device values
393 * {security label to be set on ttyp device}
395 #ifdef SO_SEC_MULTI /* 8.0 code */
396 if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
397 (void *)&ss, &szss) < 0) ||
398 (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
399 (void *)&sock_multi, &szi) < 0))
400 fatalperror(net, "getsockopt");
402 dv.dv_actlvl = ss.ss_actlabel.lt_level;
403 dv.dv_actcmp = ss.ss_actlabel.lt_compart;
405 dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
406 dv.dv_valcmp = dv.dv_actcmp;
408 dv.dv_minlvl = ss.ss_minlabel.lt_level;
409 dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
410 dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
414 #else /* SO_SEC_MULTI */ /* 7.0 code */
415 if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
416 (void *)&ss, &szss) >= 0) {
417 dv.dv_actlvl = ss.ss_slevel;
418 dv.dv_actcmp = ss.ss_compart;
419 dv.dv_minlvl = ss.ss_minlvl;
420 dv.dv_maxlvl = ss.ss_maxlvl;
421 dv.dv_valcmp = ss.ss_maxcmp;
423 #endif /* SO_SEC_MULTI */
425 #endif /* _SC_CRAY_SECURE_SYS */
427 roken_openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
428 sa_size = sizeof (__ss);
429 if (getpeername(STDIN_FILENO, sa, &sa_size) < 0) {
430 fprintf(stderr, "%s: ", progname);
431 perror("getpeername");
435 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE,
436 (void *)&on, sizeof (on)) < 0) {
437 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
440 #if defined(IPPROTO_IP) && defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
442 # ifdef HAVE_GETTOSBYNAME
444 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
448 tos = 020; /* Low Delay bit */
450 && sa->sa_family == AF_INET
451 && (setsockopt(STDIN_FILENO, IPPROTO_IP, IP_TOS,
452 (void *)&tos, sizeof(tos)) < 0)
453 && (errno != ENOPROTOOPT) )
454 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
456 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
466 fprintf(stderr, "Usage: telnetd");
467 #ifdef AUTHENTICATION
468 fprintf(stderr, " [-a (debug|other|otp|user|valid|off|none)]\n\t");
470 fprintf(stderr, " [-debug]");
472 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
474 #ifdef AUTHENTICATION
475 fprintf(stderr, " [-edebug]");
477 fprintf(stderr, " [-h]");
478 fprintf(stderr, " [-L login]");
479 fprintf(stderr, " [-n]");
481 fprintf(stderr, " [-r[lowpty]-[highpty]]");
483 fprintf(stderr, "\n\t");
484 #ifdef HAVE_GETTOSBYNAME
485 fprintf(stderr, " [-S tos]");
487 #ifdef AUTHENTICATION
488 fprintf(stderr, " [-X auth-type] [-y] [-z]");
490 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
491 fprintf(stderr, " [port]\n");
498 * Ask the other end to send along its terminal type and speed.
499 * Output is the variable terminaltype filled in.
501 static unsigned char ttytype_sbbuf[] = {
502 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
506 getterminaltype(char *name, size_t name_sz)
511 #ifdef AUTHENTICATION
513 * Handle the Authentication option before we do anything else.
515 send_do(TELOPT_AUTHENTICATION, 1);
516 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
518 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
519 retval = auth_wait(name, name_sz);
524 send_will(TELOPT_ENCRYPT, 1);
525 send_do(TELOPT_ENCRYPT, 1); /* esc@magic.fi */
527 send_do(TELOPT_TTYPE, 1);
528 send_do(TELOPT_TSPEED, 1);
529 send_do(TELOPT_XDISPLOC, 1);
530 send_do(TELOPT_NEW_ENVIRON, 1);
531 send_do(TELOPT_OLD_ENVIRON, 1);
534 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
536 his_will_wont_is_changing(TELOPT_TTYPE) ||
537 his_will_wont_is_changing(TELOPT_TSPEED) ||
538 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
539 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
540 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
545 * Wait for the negotiation of what type of encryption we can
546 * send with. If autoencrypt is not set, this will just return.
548 if (his_state_is_will(TELOPT_ENCRYPT)) {
552 if (his_state_is_will(TELOPT_TSPEED)) {
553 static unsigned char sb[] =
554 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
556 telnet_net_write (sb, sizeof sb);
557 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
559 if (his_state_is_will(TELOPT_XDISPLOC)) {
560 static unsigned char sb[] =
561 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
563 telnet_net_write (sb, sizeof sb);
564 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
566 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
567 static unsigned char sb[] =
568 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
570 telnet_net_write (sb, sizeof sb);
571 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
573 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
574 static unsigned char sb[] =
575 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
577 telnet_net_write (sb, sizeof sb);
578 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
580 if (his_state_is_will(TELOPT_TTYPE)) {
582 telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
583 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
584 sizeof ttytype_sbbuf - 2););
586 if (his_state_is_will(TELOPT_TSPEED)) {
587 while (sequenceIs(tspeedsubopt, baseline))
590 if (his_state_is_will(TELOPT_XDISPLOC)) {
591 while (sequenceIs(xdisplocsubopt, baseline))
594 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
595 while (sequenceIs(environsubopt, baseline))
598 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
599 while (sequenceIs(oenvironsubopt, baseline))
602 if (his_state_is_will(TELOPT_TTYPE)) {
603 char first[256], last[256];
605 while (sequenceIs(ttypesubopt, baseline))
609 * If the other side has already disabled the option, then
610 * we have to just go with what we (might) have already gotten.
612 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
613 strlcpy(first, terminaltype, sizeof(first));
616 * Save the unknown name, and request the next name.
618 strlcpy(last, terminaltype, sizeof(last));
620 if (terminaltypeok(terminaltype))
622 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
623 his_state_is_wont(TELOPT_TTYPE)) {
625 * We've hit the end. If this is the same as
626 * the first name, just go with it.
628 if (strncmp(first, terminaltype, sizeof(first)) == 0)
631 * Get the terminal name one more time, so that
632 * RFC1091 compliant telnets will cycle back to
633 * the start of the list.
636 if (strncmp(first, terminaltype, sizeof(first)) != 0)
637 strcpy(terminaltype, first);
644 } /* end of getterminaltype */
650 * If the client turned off the option,
651 * we can't send another request, so we
654 if (his_state_is_wont(TELOPT_TTYPE))
657 telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
658 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
659 sizeof ttytype_sbbuf - 2););
660 while (sequenceIs(ttypesubopt, baseline))
665 terminaltypeok(char *s)
671 char host_name[MaxHostNameLen];
672 char remote_host_name[MaxHostNameLen];
673 char remote_utmp_name[MaxHostNameLen];
676 * Get a pty, scan input lines.
679 doit(struct sockaddr *who, int who_len)
687 * Find an available pty to use.
689 ourpty = getpty(&ptynum);
691 fatal(net, "All network ports in use");
693 #ifdef _SC_CRAY_SECURE_SYS
695 * set ttyp line security label
700 snprintf(tty_dev, sizeof(tty_dev), "/dev/pty/%03d", ptynum);
701 if (setdevs(tty_dev, &dv) < 0)
702 fatal(net, "cannot set pty security");
703 snprintf(slave_dev, sizeof(slave_dev), "/dev/ttyp%03d", ptynum);
704 if (setdevs(slave_dev, &dv) < 0)
705 fatal(net, "cannot set tty security");
707 #endif /* _SC_CRAY_SECURE_SYS */
709 error = getnameinfo_verified (who, who_len,
711 sizeof(remote_host_name),
713 registerd_host_only ? NI_NAMEREQD : 0);
715 fatal(net, "Couldn't resolve your address into a host name.\r\n\
716 Please contact your net administrator");
718 gethostname(host_name, sizeof (host_name));
720 strlcpy (remote_utmp_name, remote_host_name, sizeof(remote_utmp_name));
722 /* Only trim if too long (and possible) */
723 if (strlen(remote_utmp_name) > utmp_len) {
724 char *domain = strchr(host_name, '.');
725 char *p = strchr(remote_utmp_name, '.');
726 if (domain != NULL && p != NULL && (strcmp(p, domain) == 0))
727 *p = '\0'; /* remove domain part */
731 * If hostname still doesn't fit utmp, use ipaddr.
733 if (strlen(remote_utmp_name) > utmp_len) {
734 error = getnameinfo (who, who_len,
736 sizeof(remote_utmp_name),
740 fatal(net, "Couldn't get numeric address\r\n");
743 #ifdef AUTHENTICATION
744 auth_encrypt_init(host_name, remote_host_name, "TELNETD", 1);
752 level = getterminaltype(user_name, sizeof(user_name));
753 esetenv("TERM", terminaltype ? terminaltype : "network", 1);
755 #ifdef _SC_CRAY_SECURE_SYS
757 if (setulvl(dv.dv_actlvl) < 0)
758 fatal(net,"cannot setulvl()");
759 if (setucmp(dv.dv_actcmp) < 0)
760 fatal(net, "cannot setucmp()");
762 #endif /* _SC_CRAY_SECURE_SYS */
764 /* begin server processing */
765 my_telnet(net, ourpty, remote_host_name, remote_utmp_name,
770 /* output contents of /etc/issue.net, or /etc/issue */
776 f = fopen(SYSCONFDIR "/issue.net", "r");
778 f = fopen(SYSCONFDIR "/issue", "r");
780 while(fgets(buf, sizeof(buf)-2, f)){
781 strcpy(buf + strcspn(buf, "\r\n"), "\r\n");
782 writenet((unsigned char*)buf, strlen(buf));
789 * Main loop. Select from pty and network, and
790 * hand data to telnet receiver finite state machine.
793 my_telnet(int f, int p, const char *host, const char *utmp_host,
794 int level, char *autoname)
800 int startslave_called = 0;
804 * Initialize the slc mapping table.
809 * Do some tests where it is desireable to wait for a response.
810 * Rather than doing them slowly, one at a time, do them all
813 if (my_state_is_wont(TELOPT_SGA))
814 send_will(TELOPT_SGA, 1);
816 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
817 * because 4.2 clients are unable to deal with TCP urgent data.
819 * To find out, we send out a "DO ECHO". If the remote system
820 * answers "WILL ECHO" it is probably a 4.2 client, and we note
821 * that fact ("WILL ECHO" ==> that the client will echo what
822 * WE, the server, sends it; it does NOT mean that the client will
823 * echo the terminal input).
825 send_do(TELOPT_ECHO, 1);
828 * Send along a couple of other options that we wish to negotiate.
830 send_do(TELOPT_NAWS, 1);
831 send_will(TELOPT_STATUS, 1);
832 flowmode = 1; /* default flow control state */
833 restartany = -1; /* uninitialized... */
834 send_do(TELOPT_LFLOW, 1);
837 * Spin, waiting for a response from the DO ECHO. However,
838 * some REALLY DUMB telnets out there might not respond
839 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
840 * telnets so far seem to respond with WONT for a DO that
841 * they don't understand...) because by the time we get the
842 * response, it will already have processed the DO ECHO.
843 * Kludge upon kludge.
845 while (his_will_wont_is_changing(TELOPT_NAWS))
850 * The client might have sent a WILL NAWS as part of its
851 * startup code; if so, we'll be here before we get the
852 * response to the DO ECHO. We'll make the assumption
853 * that any implementation that understands about NAWS
854 * is a modern enough implementation that it will respond
855 * to our DO ECHO request; hence we'll do another spin
856 * waiting for the ECHO option to settle down, which is
857 * what we wanted to do in the first place...
859 if (his_want_state_is_will(TELOPT_ECHO) &&
860 his_state_is_will(TELOPT_NAWS)) {
861 while (his_will_wont_is_changing(TELOPT_ECHO))
865 * On the off chance that the telnet client is broken and does not
866 * respond to the DO ECHO we sent, (after all, we did send the
867 * DO NAWS negotiation after the DO ECHO, and we won't get here
868 * until a response to the DO NAWS comes back) simulate the
869 * receipt of a will echo. This will also send a WONT ECHO
870 * to the client, since we assume that the client failed to
871 * respond because it believes that it is already in DO ECHO
872 * mode, which we do not want.
874 if (his_want_state_is_will(TELOPT_ECHO)) {
876 {output_data("td: simulating recv\r\n");
878 willoption(TELOPT_ECHO);
882 * Finally, to clean things up, we turn on our echo. This
883 * will break stupid 4.2 telnets out of local terminal echo.
886 if (my_state_is_wont(TELOPT_ECHO))
887 send_will(TELOPT_ECHO, 1);
894 * Turn on packet mode
896 ioctl(p, TIOCPKT, (char *)&on);
901 * Call telrcv() once to pick up anything received during
902 * terminal type negotiation, 4.2/4.3 determination, and
903 * linemode negotiation.
907 ioctl(f, FIONBIO, (char *)&on);
908 ioctl(p, FIONBIO, (char *)&on);
910 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
911 setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
912 (void *)&on, sizeof on);
913 #endif /* defined(SO_OOBINLINE) */
916 signal(SIGTSTP, SIG_IGN);
920 * Ignoring SIGTTOU keeps the kernel from blocking us
921 * in ttioct() in /sys/tty.c.
923 signal(SIGTTOU, SIG_IGN);
926 signal(SIGCHLD, cleanup);
931 t = open(_PATH_TTY, O_RDWR);
933 ioctl(t, TIOCNOTTY, (char *)0);
941 * Show banner that getty never gave.
943 * We put the banner in the pty input buffer. This way, it
944 * gets carriage return null processing, etc., just like all
945 * other pty --> client data.
953 edithost(he, host_name);
958 strncat(ptyibuf2, ptyip, pcc+1);
963 output_data("td: Entering processing loop\r\n");
967 nfd = ((f > p) ? f : p) + 1;
968 timeout = time(NULL) + 5;
970 fd_set ibits, obits, xbits;
973 /* wait for encryption to be turned on, but don't wait
975 if(!startslave_called && (!encrypt_delay() || timeout > time(NULL))){
976 startslave_called = 1;
977 startslave(host, utmp_host, level, autoname);
980 if (ncc < 0 && pcc < 0)
989 fatal(net, "fd too large");
992 * Never look for input if there's still
993 * stuff in the corresponding output buffer
995 if (nfrontp - nbackp || pcc > 0) {
1000 if (pfrontp - pbackp || ncc > 0) {
1008 if ((c = select(nfd, &ibits, &obits, &xbits,
1009 (struct timeval *)0)) < 1) {
1011 if (errno == EINTR) {
1022 if (FD_ISSET(net, &xbits)) {
1027 * Something to read from the network...
1029 if (FD_ISSET(net, &ibits)) {
1030 #ifndef SO_OOBINLINE
1032 * In 4.2 (and 4.3 beta) systems, the
1033 * OOB indication and data handling in the kernel
1034 * is such that if two separate TCP Urgent requests
1035 * come in, one byte of TCP data will be overlaid.
1036 * This is fatal for Telnet, but we try to live
1039 * In addition, in 4.2 (and...), a special protocol
1040 * is needed to pick up the TCP Urgent data in
1041 * the correct sequence.
1043 * What we do is: if we think we are in urgent
1044 * mode, we look to see if we are "at the mark".
1045 * If we are, we do an OOB receive. If we run
1046 * this twice, we will do the OOB receive twice,
1047 * but the second will fail, since the second
1048 * time we were "at the mark", but there wasn't
1049 * any data there (the kernel doesn't reset
1050 * "at the mark" until we do a normal read).
1051 * Once we've read the OOB data, we go ahead
1052 * and do normal reads.
1054 * There is also another problem, which is that
1055 * since the OOB byte we read doesn't put us
1056 * out of OOB state, and since that byte is most
1057 * likely the TELNET DM (data mark), we would
1058 * stay in the TELNET SYNCH (SYNCHing) state.
1059 * So, clocks to the rescue. If we've "just"
1060 * received a DM, then we test for the
1061 * presence of OOB data when the receive OOB
1062 * fails (and AFTER we did the normal mode read
1063 * to clear "at the mark").
1068 ioctl(net, SIOCATMARK, (char *)&atmark);
1070 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1071 if ((ncc == -1) && (errno == EINVAL)) {
1072 ncc = read(net, netibuf, sizeof (netibuf));
1073 if (sequenceIs(didnetreceive, gotDM)) {
1074 SYNCHing = stilloob(net);
1078 ncc = read(net, netibuf, sizeof (netibuf));
1081 ncc = read(net, netibuf, sizeof (netibuf));
1083 settimer(didnetreceive);
1084 #else /* !defined(SO_OOBINLINE)) */
1085 ncc = read(net, netibuf, sizeof (netibuf));
1086 #endif /* !defined(SO_OOBINLINE)) */
1087 if (ncc < 0 && errno == EWOULDBLOCK)
1095 DIAG((TD_REPORT | TD_NETDATA), {
1096 output_data("td: netread %d chars\r\n", ncc);
1098 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1102 * Something to read from the pty...
1104 if (FD_ISSET(p, &ibits)) {
1107 pcc = readstream(p, ptyibuf, BUFSIZ);
1110 pcc = read(p, ptyibuf, BUFSIZ);
1113 * On some systems, if we try to read something
1114 * off the master side before the slave side is
1115 * opened, we get EIO.
1117 if (pcc < 0 && (errno == EWOULDBLOCK ||
1126 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1127 netclear(); /* clear buffer back */
1130 * There are client telnets on some
1131 * operating systems get screwed up
1132 * royally if we send them urgent
1135 output_data ("%c%c", IAC, DM);
1137 neturg = nfrontp-1; /* off by one XXX */
1139 printoption("td: send IAC", DM));
1143 if (his_state_is_will(TELOPT_LFLOW) &&
1145 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1147 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1148 if (newflow != flowmode) {
1150 output_data("%c%c%c%c%c%c",
1151 IAC, SB, TELOPT_LFLOW,
1155 DIAG(TD_OPTIONS, printsub('>',
1156 (unsigned char *)nfrontp-4,
1166 if ((&netobuf[BUFSIZ] - nfrontp) < 3)
1168 c = *ptyip++ & 0377, pcc--;
1172 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1173 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1174 *nfrontp++ = *ptyip++ & 0377;
1181 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1185 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1193 # define TCSIG TIOCSIG
1199 int flowison = -1; /* current state of flow: -1 is unknown */
1202 readstream(int p, char *ibuf, int bufsize)
1206 struct termios *tsp;
1215 strbufc.maxlen = BUFSIZ;
1216 strbufc.buf = (char *)ctlbuf;
1217 strbufd.maxlen = bufsize-1;
1219 strbufd.buf = ibuf+1;
1222 ret = getmsg(p, &strbufc, &strbufd, &flags);
1223 if (ret < 0) /* error of some sort -- probably EAGAIN */
1226 if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
1228 if (strbufd.len > 0) { /* real data */
1229 return(strbufd.len + 1); /* count header char */
1238 * It's a control message. Return 1, to look at the flag we set
1241 switch (ctlbuf[0]) {
1243 if (ibuf[1] & FLUSHW)
1244 ibuf[0] = TIOCPKT_FLUSHWRITE;
1248 ip = (struct iocblk *) (ibuf+1);
1250 switch (ip->ioc_cmd) {
1255 tsp = (struct termios *)
1256 (ibuf+1 + sizeof(struct iocblk));
1257 vstop = tsp->c_cc[VSTOP];
1258 vstart = tsp->c_cc[VSTART];
1259 ixon = tsp->c_iflag & IXON;
1266 tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
1267 vstop = tp->c_cc[VSTOP];
1268 vstart = tp->c_cc[VSTART];
1269 ixon = tp->c_iflag & IXON;
1277 newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
1278 if (newflow != flowison) { /* it's a change */
1280 ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
1285 /* nothing worth doing anything about */
1289 #endif /* STREAMSPTY */
1292 * Send interrupt to process on other side of pty.
1293 * If it is in raw mode, just write NULL;
1294 * otherwise, write intr char.
1299 ptyflush(); /* half-hearted */
1301 #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1302 /* Streams PTY style ioctl to post a signal */
1306 ioctl(ourpty, TIOCSIGNAL, &sig);
1307 ioctl(ourpty, I_FLUSH, FLUSHR);
1311 ioctl(ourpty, TCSIG, (char *)SIGINT);
1314 *pfrontp++ = slctab[SLC_IP].sptr ?
1315 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1321 * Send quit to process on other side of pty.
1322 * If it is in raw mode, just write NULL;
1323 * otherwise, write quit char.
1328 ptyflush(); /* half-hearted */
1330 ioctl(ourpty, TCSIG, (char *)SIGQUIT);
1333 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1334 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1342 ptyflush(); /* half-hearted */
1344 ioctl(ourpty, TCSIG, (char *)SIGTSTP);
1346 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1347 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1349 #endif /* SIGTSTP */
1353 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1354 * just send back "[Yes]".
1359 #if defined(SIGINFO) && defined(TCSIG)
1360 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1361 ioctl(ourpty, TCSIG, (char *)SIGINFO);
1365 output_data("\r\n[Yes]\r\n");
1373 *pfrontp++ = slctab[SLC_EOF].sptr ?
1374 (unsigned char)*slctab[SLC_EOF].sptr : '\004';