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.58.2.1 2000/10/10 13:12:08 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;
62 #endif /* HAVE_SYS_UIO_H */
63 #ifdef HAVE_SYS_STREAM_H
64 #include <sys/stream.h>
67 #include <sys/termio.h>
69 # ifdef HAVE_SYS_STRTTY_H
70 # include <sys/strtty.h>
72 # ifdef HAVE_SYS_STR_TTY_H
73 # include <sys/str_tty.h>
75 /* make sure we don't get the bsd version */
76 /* what is this here for? solaris? /joda */
77 # ifdef HAVE_SYS_TTY_H
78 # include "/usr/include/sys/tty.h"
80 # ifdef HAVE_SYS_PTYVAR_H
81 # include <sys/ptyvar.h>
85 * Because of the way ptyibuf is used with streams messages, we need
86 * ptyibuf+1 to be on a full-word boundary. The following wierdness
87 * is simply to make that happen.
89 long ptyibufbuf[BUFSIZ/sizeof(long)+1];
90 char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
91 char *ptyip = ((char *)&ptyibufbuf[1])-1;
92 char ptyibuf2[BUFSIZ];
93 unsigned char ctlbuf[BUFSIZ];
94 struct strbuf strbufc, strbufd;
96 int readstream(int, char*, int);
98 #else /* ! STREAMPTY */
102 * pointers, and counters.
104 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
105 char ptyibuf2[BUFSIZ];
107 #endif /* ! STREAMPTY */
109 int hostinfo = 1; /* do we print login banner? */
112 extern int newmap; /* nonzero if \n maps to ^M^J */
113 int lowpty = 0, highpty; /* low, high pty numbers */
120 static void usage (void);
123 * The string to pass to getopt(). We do it this way so
124 * that only the actual options that we support will be
125 * passed off to getopt().
127 char valid_opts[] = "Bd:hklnS:u:UL:y"
128 #ifdef AUTHENTICATION
139 static void doit(struct sockaddr*, int);
142 main(int argc, char **argv)
144 struct sockaddr_storage __ss;
145 struct sockaddr *sa = (struct sockaddr *)&__ss;
148 #if defined(IPPROTO_IP) && defined(IP_TOS)
152 extern int des_check_key;
153 des_check_key = 1; /* Kludge for Mac NCSA telnet 2.6 /bg */
155 pfrontp = pbackp = ptyobuf;
157 nfrontp = nbackp = netobuf;
166 * Get number of pty's before trying to process options,
167 * which may include changing pty range.
172 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
175 #ifdef AUTHENTICATION
178 * Check for required authentication level
180 if (strcmp(optarg, "debug") == 0) {
182 } else if (strcasecmp(optarg, "none") == 0) {
184 } else if (strcasecmp(optarg, "otp") == 0) {
187 } else if (strcasecmp(optarg, "other") == 0) {
188 auth_level = AUTH_OTHER;
189 } else if (strcasecmp(optarg, "user") == 0) {
190 auth_level = AUTH_USER;
191 } else if (strcasecmp(optarg, "valid") == 0) {
192 auth_level = AUTH_VALID;
193 } else if (strcasecmp(optarg, "off") == 0) {
195 * This hack turns off authentication
200 "telnetd: unknown authorization level for -a\n");
203 #endif /* AUTHENTICATION */
205 case 'B': /* BFTP mode is not supported any more */
208 if (strcmp(optarg, "ebug") == 0) {
219 * Check for desired diagnostics capabilities.
221 if (!strcmp(optarg, "report")) {
222 diagnostic |= TD_REPORT|TD_OPTIONS;
223 } else if (!strcmp(optarg, "exercise")) {
224 diagnostic |= TD_EXERCISE;
225 } else if (!strcmp(optarg, "netdata")) {
226 diagnostic |= TD_NETDATA;
227 } else if (!strcmp(optarg, "ptydata")) {
228 diagnostic |= TD_PTYDATA;
229 } else if (!strcmp(optarg, "options")) {
230 diagnostic |= TD_OPTIONS;
236 #endif /* DIAGNOSTICS */
243 case 'k': /* Linemode is not supported any more */
258 * Allow the specification of alterations
259 * to the pty search range. It is legal to
260 * specify only one, and not change the
261 * other from its default.
263 c = strchr(optarg, '-');
269 lowpty = atoi(optarg);
270 if ((lowpty > highpty) || (lowpty < 0) ||
281 if ((tos = parsetos(optarg, "tcp")) < 0)
282 fprintf(stderr, "%s%s%s\n",
283 "telnetd: Bad TOS argument '", optarg,
284 "'; will try to use default TOS");
286 fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
287 "-S flag not supported\n");
292 utmp_len = atoi(optarg);
296 registerd_host_only = 1;
299 #ifdef AUTHENTICATION
302 * Check for invalid authentication types
304 auth_disable_name(optarg);
310 #ifdef AUTHENTICATION
315 #endif /* AUTHENTICATION */
322 fprintf(stderr, "telnetd: %c: unknown option\n", ch);
339 } else if (argc == 1) {
340 sp = roken_getservbyname (*argv, "tcp");
344 port = htons(atoi(*argv));
347 port = krb5_getportbyname (NULL, "telnet", "tcp", 23);
349 port = k_getportbyname("telnet", "tcp", htons(23));
353 } else if (argc > 0) {
358 #ifdef _SC_CRAY_SECURE_SYS
359 secflag = sysconf(_SC_CRAY_SECURE_SYS);
362 * Get socket's security label
365 int szss = sizeof(ss);
367 int szi = sizeof(int);
369 memset(&dv, 0, sizeof(dv));
371 if (getsysv(&sysv, sizeof(struct sysv)) != 0)
372 fatalperror(net, "getsysv");
375 * Get socket security label and set device values
376 * {security label to be set on ttyp device}
378 #ifdef SO_SEC_MULTI /* 8.0 code */
379 if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
380 (void *)&ss, &szss) < 0) ||
381 (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
382 (void *)&sock_multi, &szi) < 0))
383 fatalperror(net, "getsockopt");
385 dv.dv_actlvl = ss.ss_actlabel.lt_level;
386 dv.dv_actcmp = ss.ss_actlabel.lt_compart;
388 dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
389 dv.dv_valcmp = dv.dv_actcmp;
391 dv.dv_minlvl = ss.ss_minlabel.lt_level;
392 dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
393 dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
397 #else /* SO_SEC_MULTI */ /* 7.0 code */
398 if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
399 (void *)&ss, &szss) >= 0) {
400 dv.dv_actlvl = ss.ss_slevel;
401 dv.dv_actcmp = ss.ss_compart;
402 dv.dv_minlvl = ss.ss_minlvl;
403 dv.dv_maxlvl = ss.ss_maxlvl;
404 dv.dv_valcmp = ss.ss_maxcmp;
406 #endif /* SO_SEC_MULTI */
408 #endif /* _SC_CRAY_SECURE_SYS */
410 roken_openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
411 sa_size = sizeof (__ss);
412 if (getpeername(STDIN_FILENO, sa, &sa_size) < 0) {
413 fprintf(stderr, "%s: ", progname);
414 perror("getpeername");
418 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE,
419 (void *)&on, sizeof (on)) < 0) {
420 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
423 #if defined(IPPROTO_IP) && defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
425 # ifdef HAVE_GETTOSBYNAME
427 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
431 tos = 020; /* Low Delay bit */
433 && sa->sa_family == AF_INET
434 && (setsockopt(STDIN_FILENO, IPPROTO_IP, IP_TOS,
435 (void *)&tos, sizeof(tos)) < 0)
436 && (errno != ENOPROTOOPT) )
437 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
439 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
449 fprintf(stderr, "Usage: telnetd");
450 #ifdef AUTHENTICATION
451 fprintf(stderr, " [-a (debug|other|otp|user|valid|off|none)]\n\t");
453 fprintf(stderr, " [-debug]");
455 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
457 #ifdef AUTHENTICATION
458 fprintf(stderr, " [-edebug]");
460 fprintf(stderr, " [-h]");
461 fprintf(stderr, " [-L login]");
462 fprintf(stderr, " [-n]");
464 fprintf(stderr, " [-r[lowpty]-[highpty]]");
466 fprintf(stderr, "\n\t");
467 #ifdef HAVE_GETTOSBYNAME
468 fprintf(stderr, " [-S tos]");
470 #ifdef AUTHENTICATION
471 fprintf(stderr, " [-X auth-type] [-y] [-z]");
473 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
474 fprintf(stderr, " [port]\n");
481 * Ask the other end to send along its terminal type and speed.
482 * Output is the variable terminaltype filled in.
484 static unsigned char ttytype_sbbuf[] = {
485 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
489 getterminaltype(char *name, size_t name_sz)
495 #ifdef AUTHENTICATION
497 * Handle the Authentication option before we do anything else.
499 send_do(TELOPT_AUTHENTICATION, 1);
500 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
502 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
503 retval = auth_wait(name, name_sz);
508 send_will(TELOPT_ENCRYPT, 1);
509 send_do(TELOPT_ENCRYPT, 1); /* esc@magic.fi */
511 send_do(TELOPT_TTYPE, 1);
512 send_do(TELOPT_TSPEED, 1);
513 send_do(TELOPT_XDISPLOC, 1);
514 send_do(TELOPT_NEW_ENVIRON, 1);
515 send_do(TELOPT_OLD_ENVIRON, 1);
518 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
520 his_will_wont_is_changing(TELOPT_TTYPE) ||
521 his_will_wont_is_changing(TELOPT_TSPEED) ||
522 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
523 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
524 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
529 * Wait for the negotiation of what type of encryption we can
530 * send with. If autoencrypt is not set, this will just return.
532 if (his_state_is_will(TELOPT_ENCRYPT)) {
536 if (his_state_is_will(TELOPT_TSPEED)) {
537 static unsigned char sb[] =
538 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
540 telnet_net_write (sb, sizeof sb);
541 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
543 if (his_state_is_will(TELOPT_XDISPLOC)) {
544 static unsigned char sb[] =
545 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
547 telnet_net_write (sb, sizeof sb);
548 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
550 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
551 static unsigned char sb[] =
552 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
554 telnet_net_write (sb, sizeof sb);
555 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
557 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
558 static unsigned char sb[] =
559 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
561 telnet_net_write (sb, sizeof sb);
562 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
564 if (his_state_is_will(TELOPT_TTYPE)) {
566 telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
567 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
568 sizeof ttytype_sbbuf - 2););
570 if (his_state_is_will(TELOPT_TSPEED)) {
571 while (sequenceIs(tspeedsubopt, baseline))
574 if (his_state_is_will(TELOPT_XDISPLOC)) {
575 while (sequenceIs(xdisplocsubopt, baseline))
578 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
579 while (sequenceIs(environsubopt, baseline))
582 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
583 while (sequenceIs(oenvironsubopt, baseline))
586 if (his_state_is_will(TELOPT_TTYPE)) {
587 char first[256], last[256];
589 while (sequenceIs(ttypesubopt, baseline))
593 * If the other side has already disabled the option, then
594 * we have to just go with what we (might) have already gotten.
596 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
597 strlcpy(first, terminaltype, sizeof(first));
600 * Save the unknown name, and request the next name.
602 strlcpy(last, terminaltype, sizeof(last));
604 if (terminaltypeok(terminaltype))
606 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
607 his_state_is_wont(TELOPT_TTYPE)) {
609 * We've hit the end. If this is the same as
610 * the first name, just go with it.
612 if (strncmp(first, terminaltype, sizeof(first)) == 0)
615 * Get the terminal name one more time, so that
616 * RFC1091 compliant telnets will cycle back to
617 * the start of the list.
620 if (strncmp(first, terminaltype, sizeof(first)) != 0)
621 strcpy(terminaltype, first);
628 } /* end of getterminaltype */
634 * If the client turned off the option,
635 * we can't send another request, so we
638 if (his_state_is_wont(TELOPT_TTYPE))
641 telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf);
642 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
643 sizeof ttytype_sbbuf - 2););
644 while (sequenceIs(ttypesubopt, baseline))
649 terminaltypeok(char *s)
656 char host_name[MaxHostNameLen];
657 char remote_host_name[MaxHostNameLen];
660 * Get a pty, scan input lines.
663 doit(struct sockaddr *who, int who_len)
666 struct hostent *hp = NULL;
678 * Find an available pty to use.
680 ourpty = getpty(&ptynum);
682 fatal(net, "All network ports in use");
684 #ifdef _SC_CRAY_SECURE_SYS
686 * set ttyp line security label
691 snprintf(tty_dev, sizeof(tty_dev), "/dev/pty/%03d", ptynum);
692 if (setdevs(tty_dev, &dv) < 0)
693 fatal(net, "cannot set pty security");
694 snprintf(slave_dev, sizeof(slave_dev), "/dev/ttyp%03d", ptynum);
695 if (setdevs(slave_dev, &dv) < 0)
696 fatal(net, "cannot set tty security");
698 #endif /* _SC_CRAY_SECURE_SYS */
703 struct sockaddr_in *sin = (struct sockaddr_in *)who;
705 addr = &sin->sin_addr;
706 addr_sz = sizeof(sin->sin_addr);
711 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)who;
713 addr = &sin6->sin6_addr;
714 addr_sz = sizeof(sin6->sin6_addr);
719 fatal (net, "Unknown address family\r\n");
723 hp = getipnodebyaddr (addr, addr_sz, af, &error);
725 if (hp == NULL && registerd_host_only) {
726 fatal(net, "Couldn't resolve your address into a host name.\r\n\
727 Please contact your net administrator");
728 } else if (hp != NULL) {
732 tmp = inet_ntop(af, addr, host_addr, sizeof(host_addr));
734 strlcpy (host_addr, "unknown address", sizeof(host_addr));
740 * We must make a copy because Kerberos is probably going
741 * to also do a gethost* and overwrite the static data...
743 strlcpy(remote_host_name, host, sizeof(remote_host_name));
746 host = remote_host_name;
748 /* XXX - should be k_gethostname? */
749 gethostname(host_name, sizeof (host_name));
750 hostname = host_name;
752 /* Only trim if too long (and possible) */
753 if (strlen(remote_host_name) > abs(utmp_len)) {
754 char *domain = strchr(host_name, '.');
755 char *p = strchr(remote_host_name, '.');
756 if (domain && p && (strcmp(p, domain) == 0))
757 *p = 0; /* remove domain part */
762 * If hostname still doesn't fit utmp, use ipaddr.
764 if (strlen(remote_host_name) > abs(utmp_len))
765 strlcpy(remote_host_name,
767 sizeof(remote_host_name));
769 #ifdef AUTHENTICATION
770 auth_encrypt_init(hostname, host, "TELNETD", 1);
778 level = getterminaltype(user_name, sizeof(user_name));
779 setenv("TERM", terminaltype ? terminaltype : "network", 1);
781 #ifdef _SC_CRAY_SECURE_SYS
783 if (setulvl(dv.dv_actlvl) < 0)
784 fatal(net,"cannot setulvl()");
785 if (setucmp(dv.dv_actcmp) < 0)
786 fatal(net, "cannot setucmp()");
788 #endif /* _SC_CRAY_SECURE_SYS */
790 /* begin server processing */
791 my_telnet(net, ourpty, host, level, user_name);
795 /* output contents of /etc/issue.net, or /etc/issue */
801 f = fopen("/etc/issue.net", "r");
803 f = fopen("/etc/issue", "r");
805 while(fgets(buf, sizeof(buf)-2, f)){
806 strcpy(buf + strcspn(buf, "\r\n"), "\r\n");
807 writenet((unsigned char*)buf, strlen(buf));
814 * Main loop. Select from pty and network, and
815 * hand data to telnet receiver finite state machine.
818 my_telnet(int f, int p, char *host, int level, char *autoname)
824 int startslave_called = 0;
828 * Initialize the slc mapping table.
833 * Do some tests where it is desireable to wait for a response.
834 * Rather than doing them slowly, one at a time, do them all
837 if (my_state_is_wont(TELOPT_SGA))
838 send_will(TELOPT_SGA, 1);
840 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
841 * because 4.2 clients are unable to deal with TCP urgent data.
843 * To find out, we send out a "DO ECHO". If the remote system
844 * answers "WILL ECHO" it is probably a 4.2 client, and we note
845 * that fact ("WILL ECHO" ==> that the client will echo what
846 * WE, the server, sends it; it does NOT mean that the client will
847 * echo the terminal input).
849 send_do(TELOPT_ECHO, 1);
852 * Send along a couple of other options that we wish to negotiate.
854 send_do(TELOPT_NAWS, 1);
855 send_will(TELOPT_STATUS, 1);
856 flowmode = 1; /* default flow control state */
857 restartany = -1; /* uninitialized... */
858 send_do(TELOPT_LFLOW, 1);
861 * Spin, waiting for a response from the DO ECHO. However,
862 * some REALLY DUMB telnets out there might not respond
863 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
864 * telnets so far seem to respond with WONT for a DO that
865 * they don't understand...) because by the time we get the
866 * response, it will already have processed the DO ECHO.
867 * Kludge upon kludge.
869 while (his_will_wont_is_changing(TELOPT_NAWS))
874 * The client might have sent a WILL NAWS as part of its
875 * startup code; if so, we'll be here before we get the
876 * response to the DO ECHO. We'll make the assumption
877 * that any implementation that understands about NAWS
878 * is a modern enough implementation that it will respond
879 * to our DO ECHO request; hence we'll do another spin
880 * waiting for the ECHO option to settle down, which is
881 * what we wanted to do in the first place...
883 if (his_want_state_is_will(TELOPT_ECHO) &&
884 his_state_is_will(TELOPT_NAWS)) {
885 while (his_will_wont_is_changing(TELOPT_ECHO))
889 * On the off chance that the telnet client is broken and does not
890 * respond to the DO ECHO we sent, (after all, we did send the
891 * DO NAWS negotiation after the DO ECHO, and we won't get here
892 * until a response to the DO NAWS comes back) simulate the
893 * receipt of a will echo. This will also send a WONT ECHO
894 * to the client, since we assume that the client failed to
895 * respond because it believes that it is already in DO ECHO
896 * mode, which we do not want.
898 if (his_want_state_is_will(TELOPT_ECHO)) {
900 {output_data("td: simulating recv\r\n");
902 willoption(TELOPT_ECHO);
906 * Finally, to clean things up, we turn on our echo. This
907 * will break stupid 4.2 telnets out of local terminal echo.
910 if (my_state_is_wont(TELOPT_ECHO))
911 send_will(TELOPT_ECHO, 1);
918 * Turn on packet mode
920 ioctl(p, TIOCPKT, (char *)&on);
925 * Call telrcv() once to pick up anything received during
926 * terminal type negotiation, 4.2/4.3 determination, and
927 * linemode negotiation.
931 ioctl(f, FIONBIO, (char *)&on);
932 ioctl(p, FIONBIO, (char *)&on);
934 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
935 setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
936 (void *)&on, sizeof on);
937 #endif /* defined(SO_OOBINLINE) */
940 signal(SIGTSTP, SIG_IGN);
944 * Ignoring SIGTTOU keeps the kernel from blocking us
945 * in ttioct() in /sys/tty.c.
947 signal(SIGTTOU, SIG_IGN);
950 signal(SIGCHLD, cleanup);
955 t = open(_PATH_TTY, O_RDWR);
957 ioctl(t, TIOCNOTTY, (char *)0);
965 * Show banner that getty never gave.
967 * We put the banner in the pty input buffer. This way, it
968 * gets carriage return null processing, etc., just like all
969 * other pty --> client data.
977 edithost(he, host_name);
982 strncat(ptyibuf2, ptyip, pcc+1);
987 output_data("td: Entering processing loop\r\n");
991 nfd = ((f > p) ? f : p) + 1;
992 timeout = time(NULL) + 5;
994 fd_set ibits, obits, xbits;
997 /* wait for encryption to be turned on, but don't wait
999 if(!startslave_called && (!encrypt_delay() || timeout > time(NULL))){
1000 startslave_called = 1;
1001 startslave(host, level, autoname);
1004 if (ncc < 0 && pcc < 0)
1013 fatal(net, "fd too large");
1016 * Never look for input if there's still
1017 * stuff in the corresponding output buffer
1019 if (nfrontp - nbackp || pcc > 0) {
1024 if (pfrontp - pbackp || ncc > 0) {
1032 if ((c = select(nfd, &ibits, &obits, &xbits,
1033 (struct timeval *)0)) < 1) {
1035 if (errno == EINTR) {
1046 if (FD_ISSET(net, &xbits)) {
1051 * Something to read from the network...
1053 if (FD_ISSET(net, &ibits)) {
1054 #ifndef SO_OOBINLINE
1056 * In 4.2 (and 4.3 beta) systems, the
1057 * OOB indication and data handling in the kernel
1058 * is such that if two separate TCP Urgent requests
1059 * come in, one byte of TCP data will be overlaid.
1060 * This is fatal for Telnet, but we try to live
1063 * In addition, in 4.2 (and...), a special protocol
1064 * is needed to pick up the TCP Urgent data in
1065 * the correct sequence.
1067 * What we do is: if we think we are in urgent
1068 * mode, we look to see if we are "at the mark".
1069 * If we are, we do an OOB receive. If we run
1070 * this twice, we will do the OOB receive twice,
1071 * but the second will fail, since the second
1072 * time we were "at the mark", but there wasn't
1073 * any data there (the kernel doesn't reset
1074 * "at the mark" until we do a normal read).
1075 * Once we've read the OOB data, we go ahead
1076 * and do normal reads.
1078 * There is also another problem, which is that
1079 * since the OOB byte we read doesn't put us
1080 * out of OOB state, and since that byte is most
1081 * likely the TELNET DM (data mark), we would
1082 * stay in the TELNET SYNCH (SYNCHing) state.
1083 * So, clocks to the rescue. If we've "just"
1084 * received a DM, then we test for the
1085 * presence of OOB data when the receive OOB
1086 * fails (and AFTER we did the normal mode read
1087 * to clear "at the mark").
1092 ioctl(net, SIOCATMARK, (char *)&atmark);
1094 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1095 if ((ncc == -1) && (errno == EINVAL)) {
1096 ncc = read(net, netibuf, sizeof (netibuf));
1097 if (sequenceIs(didnetreceive, gotDM)) {
1098 SYNCHing = stilloob(net);
1102 ncc = read(net, netibuf, sizeof (netibuf));
1105 ncc = read(net, netibuf, sizeof (netibuf));
1107 settimer(didnetreceive);
1108 #else /* !defined(SO_OOBINLINE)) */
1109 ncc = read(net, netibuf, sizeof (netibuf));
1110 #endif /* !defined(SO_OOBINLINE)) */
1111 if (ncc < 0 && errno == EWOULDBLOCK)
1119 DIAG((TD_REPORT | TD_NETDATA), {
1120 output_data("td: netread %d chars\r\n", ncc);
1122 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1126 * Something to read from the pty...
1128 if (FD_ISSET(p, &ibits)) {
1131 pcc = readstream(p, ptyibuf, BUFSIZ);
1134 pcc = read(p, ptyibuf, BUFSIZ);
1137 * On some systems, if we try to read something
1138 * off the master side before the slave side is
1139 * opened, we get EIO.
1141 if (pcc < 0 && (errno == EWOULDBLOCK ||
1150 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1151 netclear(); /* clear buffer back */
1154 * There are client telnets on some
1155 * operating systems get screwed up
1156 * royally if we send them urgent
1159 output_data ("%c%c", IAC, DM);
1161 neturg = nfrontp-1; /* off by one XXX */
1163 printoption("td: send IAC", DM));
1167 if (his_state_is_will(TELOPT_LFLOW) &&
1169 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1171 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1172 if (newflow != flowmode) {
1174 output_data("%c%c%c%c%c%c",
1175 IAC, SB, TELOPT_LFLOW,
1179 DIAG(TD_OPTIONS, printsub('>',
1180 (unsigned char *)nfrontp-4,
1190 if ((&netobuf[BUFSIZ] - nfrontp) < 3)
1192 c = *ptyip++ & 0377, pcc--;
1196 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1197 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1198 *nfrontp++ = *ptyip++ & 0377;
1205 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1209 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1217 # define TCSIG TIOCSIG
1223 int flowison = -1; /* current state of flow: -1 is unknown */
1226 readstream(int p, char *ibuf, int bufsize)
1230 struct termios *tsp;
1239 strbufc.maxlen = BUFSIZ;
1240 strbufc.buf = (char *)ctlbuf;
1241 strbufd.maxlen = bufsize-1;
1243 strbufd.buf = ibuf+1;
1246 ret = getmsg(p, &strbufc, &strbufd, &flags);
1247 if (ret < 0) /* error of some sort -- probably EAGAIN */
1250 if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
1252 if (strbufd.len > 0) { /* real data */
1253 return(strbufd.len + 1); /* count header char */
1262 * It's a control message. Return 1, to look at the flag we set
1265 switch (ctlbuf[0]) {
1267 if (ibuf[1] & FLUSHW)
1268 ibuf[0] = TIOCPKT_FLUSHWRITE;
1272 ip = (struct iocblk *) (ibuf+1);
1274 switch (ip->ioc_cmd) {
1279 tsp = (struct termios *)
1280 (ibuf+1 + sizeof(struct iocblk));
1281 vstop = tsp->c_cc[VSTOP];
1282 vstart = tsp->c_cc[VSTART];
1283 ixon = tsp->c_iflag & IXON;
1290 tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
1291 vstop = tp->c_cc[VSTOP];
1292 vstart = tp->c_cc[VSTART];
1293 ixon = tp->c_iflag & IXON;
1301 newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
1302 if (newflow != flowison) { /* it's a change */
1304 ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
1309 /* nothing worth doing anything about */
1313 #endif /* STREAMSPTY */
1316 * Send interrupt to process on other side of pty.
1317 * If it is in raw mode, just write NULL;
1318 * otherwise, write intr char.
1323 ptyflush(); /* half-hearted */
1325 #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1326 /* Streams PTY style ioctl to post a signal */
1330 ioctl(ourpty, TIOCSIGNAL, &sig);
1331 ioctl(ourpty, I_FLUSH, FLUSHR);
1335 ioctl(ourpty, TCSIG, (char *)SIGINT);
1338 *pfrontp++ = slctab[SLC_IP].sptr ?
1339 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1345 * Send quit to process on other side of pty.
1346 * If it is in raw mode, just write NULL;
1347 * otherwise, write quit char.
1352 ptyflush(); /* half-hearted */
1354 ioctl(ourpty, TCSIG, (char *)SIGQUIT);
1357 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1358 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1366 ptyflush(); /* half-hearted */
1368 ioctl(ourpty, TCSIG, (char *)SIGTSTP);
1370 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1371 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1373 #endif /* SIGTSTP */
1377 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1378 * just send back "[Yes]".
1383 #if defined(SIGINFO) && defined(TCSIG)
1384 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1385 ioctl(ourpty, TCSIG, (char *)SIGINFO);
1389 output_data("\r\n[Yes]\r\n");
1397 *pfrontp++ = slctab[SLC_EOF].sptr ?
1398 (unsigned char)*slctab[SLC_EOF].sptr : '\004';