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: src/contrib/telnet/telnetd/telnetd.c,v 1.28 2005/05/21 15:28:42 ume Exp $");
43 #include "pathnames.h"
45 //#include <sys/mman.h>
50 #if (!defined(__BEOS__) && !defined(__HAIKU__))
54 #include <arpa/inet.h>
57 #include <libtelnet/auth.h>
61 #include <libtelnet/encrypt.h>
63 #include <libtelnet/misc.h>
65 char remote_hostname
[MAXHOSTNAMELEN
];
66 size_t utmp_len
= sizeof(remote_hostname
) - 1;
67 int registerd_host_only
= 0;
72 * pointers, and counters.
74 char ptyibuf
[BUFSIZ
], *ptyip
= ptyibuf
;
75 char ptyibuf2
[BUFSIZ
];
77 int readstream(int, char *, int);
78 void doit(struct sockaddr
*);
79 int terminaltypeok(char *);
81 int hostinfo
= 1; /* do we print login banner? */
87 void doit(struct sockaddr
*);
88 int terminaltypeok(char *);
89 void startslave(char *, int, char *);
90 extern void usage(void);
91 static void _gettermname(void);
94 * The string to pass to getopt(). We do it this way so
95 * that only the actual options that we support will be
96 * passed off to getopt().
99 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
101 #ifdef AUTHENTICATION
119 int family
= AF_INET
;
121 #ifndef MAXHOSTNAMELEN
122 #define MAXHOSTNAMELEN 256
123 #endif /* MAXHOSTNAMELEN */
126 char host_name
[MAXHOSTNAMELEN
];
128 extern void telnet(int, int, char *);
134 main(int argc
, char *argv
[])
136 struct sockaddr_storage from
;
139 #if (!defined(__BEOS__) && !defined(__HAIKU__))
143 #if defined(IPPROTO_IP) && defined(IP_TOS)
147 pfrontp
= pbackp
= ptyobuf
;
149 nfrontp
= nbackp
= netobuf
;
152 #endif /* ENCRYPTION */
155 * This initialization causes linemode to default to a configuration
156 * that works on all telnet clients, including the FreeBSD client.
157 * This is not quite the same as the telnet client issuing a "mode
158 * character" command, but has most of the same benefits, and is
159 * preferable since some clients (like usofts) don't have the
160 * mode character command anyway and linemode breaks things.
161 * The most notable symptom of fix is that csh "set filec" operations
162 * like <ESC> (filename completion) and ^D (choices) keys now work
163 * in telnet sessions and can be used more than once on the same line.
164 * CR/LF handling is also corrected in some termio modes. This
165 * change resolves problem reports bin/771 and bin/1037.
168 linemode
=1; /*Default to mode that works on bulk of clients*/
170 while ((ch
= getopt(argc
, argv
, valid_opts
)) != -1) {
173 #ifdef AUTHENTICATION
176 * Check for required authentication level
178 if (strcmp(optarg
, "debug") == 0) {
179 extern int auth_debug_mode
;
181 } else if (strcasecmp(optarg
, "none") == 0) {
183 } else if (strcasecmp(optarg
, "other") == 0) {
184 auth_level
= AUTH_OTHER
;
185 } else if (strcasecmp(optarg
, "user") == 0) {
186 auth_level
= AUTH_USER
;
187 } else if (strcasecmp(optarg
, "valid") == 0) {
188 auth_level
= AUTH_VALID
;
189 } else if (strcasecmp(optarg
, "off") == 0) {
191 * This hack turns off authentication
195 warnx("unknown authorization level for -a");
198 #endif /* AUTHENTICATION */
204 #endif /* BFTPDAEMON */
207 if (strcmp(optarg
, "ebug") == 0) {
218 * Check for desired diagnostics capabilities.
220 if (!strcmp(optarg
, "report")) {
221 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
222 } else if (!strcmp(optarg
, "exercise")) {
223 diagnostic
|= TD_EXERCISE
;
224 } else if (!strcmp(optarg
, "netdata")) {
225 diagnostic
|= TD_NETDATA
;
226 } else if (!strcmp(optarg
, "ptydata")) {
227 diagnostic
|= TD_PTYDATA
;
228 } else if (!strcmp(optarg
, "options")) {
229 diagnostic
|= TD_OPTIONS
;
235 #endif /* DIAGNOSTICS */
239 if (strcmp(optarg
, "debug") == 0) {
240 extern int encrypt_debug_mode
;
241 encrypt_debug_mode
= 1;
247 #endif /* ENCRYPTION */
257 #endif /* LINEMODE */
260 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
261 lmodetype
= NO_AUTOKLUDGE
;
263 /* ignore -k option if built without kludge linemode */
264 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
276 #if (defined(__BEOS__) || defined(__HAIKU__))
277 fprintf(stderr
, "-S option is not supported\n");
280 if ((tos
= parsetos(optarg
, "tcp")) < 0)
282 "bad TOS argument '", optarg
,
283 "'; will try to use default TOS");
286 ultmp
= strtoul(optarg
, &ep
, 0);
287 if (*ep
|| ep
== optarg
|| ultmp
> MAXTOS
)
289 "bad TOS argument '", optarg
,
290 "'; will try to use default TOS");
294 #endif /* !__BEOS__ */
298 utmp_len
= (size_t)atoi(optarg
);
299 if (utmp_len
>= sizeof(remote_hostname
))
300 utmp_len
= sizeof(remote_hostname
) - 1;
304 registerd_host_only
= 1;
307 #ifdef AUTHENTICATION
310 * Check for invalid authentication types
312 auth_disable_name(optarg
);
314 #endif /* AUTHENTICATION */
327 warnx("%c: unknown option", ch
);
339 int s
, ns
, foo
, error
;
340 const char *service
= "telnet";
341 struct addrinfo hints
, *res
;
346 } else if (argc
== 1)
349 memset(&hints
, 0, sizeof(hints
));
350 hints
.ai_flags
= AI_PASSIVE
;
351 hints
.ai_family
= family
;
352 hints
.ai_socktype
= SOCK_STREAM
;
353 hints
.ai_protocol
= 0;
354 error
= getaddrinfo(NULL
, service
, &hints
, &res
);
357 errx(1, "tcp/%s: %s\n", service
, gai_strerror(error
));
358 if (error
== EAI_SYSTEM
)
359 errx(1, "tcp/%s: %s\n", service
, strerror(errno
));
363 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
366 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
367 (char *)&on
, sizeof(on
));
369 (void) setsockopt(s
, SOL_SOCKET
, SO_DEBUG
,
370 (char *)&on
, sizeof(on
));
371 if (bind(s
, res
->ai_addr
, res
->ai_addrlen
) < 0)
373 if (listen(s
, 1) < 0)
375 foo
= res
->ai_addrlen
;
376 ns
= accept(s
, res
->ai_addr
, &foo
);
379 (void) setsockopt(ns
, SOL_SOCKET
, SO_DEBUG
,
380 (char *)&on
, sizeof(on
));
385 } else if (argc
== 1) {
386 ; /* VOID*/ /* Just ignore the host/port name */
388 } else if (argc
> 0) {
393 openlog("telnetd", LOG_PID
| LOG_ODELAY
, LOG_DAEMON
);
394 fromlen
= sizeof (from
);
395 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
400 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
401 (char *)&on
, sizeof (on
)) < 0) {
402 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
405 #if defined(IPPROTO_IP) && defined(IP_TOS)
406 if (from
.ss_family
== AF_INET
) {
407 # if defined(HAS_GETTOS)
409 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
413 tos
= 020; /* Low Delay bit */
415 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
416 (char *)&tos
, sizeof(tos
)) < 0)
417 && (errno
!= ENOPROTOOPT
) )
418 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
420 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
422 doit((struct sockaddr
*)&from
);
430 fprintf(stderr
, "usage: telnetd");
431 #ifdef AUTHENTICATION
433 " [-4] [-6] [-a (debug|other|user|valid|off|none)]\n\t");
436 fprintf(stderr
, " [-B]");
438 fprintf(stderr
, " [-debug]");
440 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
442 #ifdef AUTHENTICATION
443 fprintf(stderr
, " [-edebug]");
445 fprintf(stderr
, " [-h]");
446 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
447 fprintf(stderr
, " [-k]");
450 fprintf(stderr
, " [-l]");
452 fprintf(stderr
, " [-n]");
453 fprintf(stderr
, "\n\t");
455 fprintf(stderr
, " [-S tos]");
457 #ifdef AUTHENTICATION
458 fprintf(stderr
, " [-X auth-type]");
460 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
461 fprintf(stderr
, " [port]\n");
468 * Ask the other end to send along its terminal type and speed.
469 * Output is the variable terminaltype filled in.
471 static unsigned char ttytype_sbbuf
[] = {
472 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
476 #ifndef AUTHENTICATION
477 #define undef2 __unused
483 getterminaltype(char *name undef2
)
488 #ifdef AUTHENTICATION
490 * Handle the Authentication option before we do anything else.
492 send_do(TELOPT_AUTHENTICATION
, 1);
493 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION
))
495 if (his_state_is_will(TELOPT_AUTHENTICATION
)) {
496 retval
= auth_wait(name
);
501 send_will(TELOPT_ENCRYPT
, 1);
502 #endif /* ENCRYPTION */
503 send_do(TELOPT_TTYPE
, 1);
504 send_do(TELOPT_TSPEED
, 1);
505 send_do(TELOPT_XDISPLOC
, 1);
506 send_do(TELOPT_NEW_ENVIRON
, 1);
507 send_do(TELOPT_OLD_ENVIRON
, 1);
510 his_do_dont_is_changing(TELOPT_ENCRYPT
) ||
511 #endif /* ENCRYPTION */
512 his_will_wont_is_changing(TELOPT_TTYPE
) ||
513 his_will_wont_is_changing(TELOPT_TSPEED
) ||
514 his_will_wont_is_changing(TELOPT_XDISPLOC
) ||
515 his_will_wont_is_changing(TELOPT_NEW_ENVIRON
) ||
516 his_will_wont_is_changing(TELOPT_OLD_ENVIRON
)) {
521 * Wait for the negotiation of what type of encryption we can
522 * send with. If autoencrypt is not set, this will just return.
524 if (his_state_is_will(TELOPT_ENCRYPT
)) {
527 #endif /* ENCRYPTION */
528 if (his_state_is_will(TELOPT_TSPEED
)) {
529 static unsigned char sb
[] =
530 { IAC
, SB
, TELOPT_TSPEED
, 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_XDISPLOC
)) {
536 static unsigned char sb
[] =
537 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
539 output_datalen(sb
, sizeof sb
);
540 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
542 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
543 static unsigned char sb
[] =
544 { IAC
, SB
, TELOPT_NEW_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
546 output_datalen(sb
, sizeof sb
);
547 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
549 else if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
550 static unsigned char sb
[] =
551 { IAC
, SB
, TELOPT_OLD_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
553 output_datalen(sb
, sizeof sb
);
554 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
556 if (his_state_is_will(TELOPT_TTYPE
)) {
558 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
559 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
560 sizeof ttytype_sbbuf
- 2););
562 if (his_state_is_will(TELOPT_TSPEED
)) {
563 while (sequenceIs(tspeedsubopt
, baseline
))
566 if (his_state_is_will(TELOPT_XDISPLOC
)) {
567 while (sequenceIs(xdisplocsubopt
, baseline
))
570 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
571 while (sequenceIs(environsubopt
, baseline
))
574 if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
575 while (sequenceIs(oenvironsubopt
, baseline
))
578 if (his_state_is_will(TELOPT_TTYPE
)) {
579 char first
[256], last
[256];
581 while (sequenceIs(ttypesubopt
, baseline
))
585 * If the other side has already disabled the option, then
586 * we have to just go with what we (might) have already gotten.
588 if (his_state_is_will(TELOPT_TTYPE
) && !terminaltypeok(terminaltype
)) {
589 (void) strncpy(first
, terminaltype
, sizeof(first
)-1);
590 first
[sizeof(first
)-1] = '\0';
593 * Save the unknown name, and request the next name.
595 (void) strncpy(last
, terminaltype
, sizeof(last
)-1);
596 last
[sizeof(last
)-1] = '\0';
598 if (terminaltypeok(terminaltype
))
600 if ((strncmp(last
, terminaltype
, sizeof(last
)) == 0) ||
601 his_state_is_wont(TELOPT_TTYPE
)) {
603 * We've hit the end. If this is the same as
604 * the first name, just go with it.
606 if (strncmp(first
, terminaltype
, sizeof(first
)) == 0)
609 * Get the terminal name one more time, so that
610 * RFC1091 compliant telnets will cycle back to
611 * the start of the list.
614 if (strncmp(first
, terminaltype
, sizeof(first
)) != 0) {
615 (void) strncpy(terminaltype
, first
, sizeof(terminaltype
)-1);
616 terminaltype
[sizeof(terminaltype
)-1] = '\0';
624 } /* end of getterminaltype */
630 * If the client turned off the option,
631 * we can't send another request, so we
634 if (his_state_is_wont(TELOPT_TTYPE
))
637 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
638 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
639 sizeof ttytype_sbbuf
- 2););
640 while (sequenceIs(ttypesubopt
, baseline
))
645 terminaltypeok(char *s
)
649 if (terminaltype
== NULL
)
653 * tgetent() will return 1 if the type is known, and
654 * 0 if it is not known. If it returns -1, it couldn't
655 * open the database. But if we can't open the database,
656 * it won't help to say we failed, because we won't be
657 * able to verify anything else. So, we treat -1 like 1.
659 if (tgetent(buf
, s
) == 0)
665 * Get a pty, scan input lines.
668 doit(struct sockaddr
*who
)
674 * Find an available pty to use.
677 pty
= getpty(&ptynum
);
679 fatal(net
, "All network ports in use");
684 if ((lp
= getpty()) == NULL
)
685 fatal(net
, "Out of ptys");
687 if ((pty
= open(lp
, 2)) >= 0) {
688 strlcpy(line
,lp
,sizeof(line
));
695 /* get name of connected client */
696 if (realhostname_sa(remote_hostname
, sizeof(remote_hostname
) - 1,
697 who
, who
->sa_len
) == HOSTNAME_INVALIDADDR
&& registerd_host_only
)
698 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
699 Please contact your net administrator");
700 remote_hostname
[sizeof(remote_hostname
) - 1] = '\0';
702 #if (!defined(__BEOS__) && !defined(__HAIKU__))
703 trimdomain(remote_hostname
, UT_HOSTSIZE
);
705 if (!isdigit(remote_hostname
[0]) && strlen(remote_hostname
) > utmp_len
)
706 err_
= getnameinfo(who
, who
->sa_len
, remote_hostname
,
707 sizeof(remote_hostname
), NULL
, 0,
709 /* XXX: do 'err_' check */
711 (void) gethostname(host_name
, sizeof(host_name
) - 1);
712 host_name
[sizeof(host_name
) - 1] = '\0';
713 hostname
= host_name
;
715 #ifdef AUTHENTICATION
717 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
718 * This is a byproduct of needing "#ifdef" and not "#if defined()"
719 * for unifdef. XXX MarkM
721 auth_encrypt_init(hostname
, remote_hostname
, "TELNETD", 1);
730 level
= getterminaltype(user_name
);
731 setenv("TERM", terminaltype
? terminaltype
: "network", 1);
733 telnet(net
, pty
, remote_hostname
); /* begin server process */
739 * Main loop. Select from pty and network, and
740 * hand data to telnet receiver finite state machine.
743 telnet(int f
, int p
, char *host
)
746 #define TABBUFSIZ 512
747 char defent
[TABBUFSIZ
];
748 char defstrs
[TABBUFSIZ
];
756 * Initialize the slc mapping table.
761 * Do some tests where it is desireable to wait for a response.
762 * Rather than doing them slowly, one at a time, do them all
765 if (my_state_is_wont(TELOPT_SGA
))
766 send_will(TELOPT_SGA
, 1);
768 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
769 * because 4.2 clients are unable to deal with TCP urgent data.
771 * To find out, we send out a "DO ECHO". If the remote system
772 * answers "WILL ECHO" it is probably a 4.2 client, and we note
773 * that fact ("WILL ECHO" ==> that the client will echo what
774 * WE, the server, sends it; it does NOT mean that the client will
775 * echo the terminal input).
777 send_do(TELOPT_ECHO
, 1);
780 if (his_state_is_wont(TELOPT_LINEMODE
)) {
781 /* Query the peer for linemode support by trying to negotiate
782 * the linemode option.
786 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
788 #endif /* LINEMODE */
791 * Send along a couple of other options that we wish to negotiate.
793 send_do(TELOPT_NAWS
, 1);
794 send_will(TELOPT_STATUS
, 1);
795 flowmode
= 1; /* default flow control state */
796 restartany
= -1; /* uninitialized... */
797 send_do(TELOPT_LFLOW
, 1);
800 * Spin, waiting for a response from the DO ECHO. However,
801 * some REALLY DUMB telnets out there might not respond
802 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
803 * telnets so far seem to respond with WONT for a DO that
804 * they don't understand...) because by the time we get the
805 * response, it will already have processed the DO ECHO.
806 * Kludge upon kludge.
808 while (his_will_wont_is_changing(TELOPT_NAWS
))
813 * The client might have sent a WILL NAWS as part of its
814 * startup code; if so, we'll be here before we get the
815 * response to the DO ECHO. We'll make the assumption
816 * that any implementation that understands about NAWS
817 * is a modern enough implementation that it will respond
818 * to our DO ECHO request; hence we'll do another spin
819 * waiting for the ECHO option to settle down, which is
820 * what we wanted to do in the first place...
822 if (his_want_state_is_will(TELOPT_ECHO
) &&
823 his_state_is_will(TELOPT_NAWS
)) {
824 while (his_will_wont_is_changing(TELOPT_ECHO
))
828 * On the off chance that the telnet client is broken and does not
829 * respond to the DO ECHO we sent, (after all, we did send the
830 * DO NAWS negotiation after the DO ECHO, and we won't get here
831 * until a response to the DO NAWS comes back) simulate the
832 * receipt of a will echo. This will also send a WONT ECHO
833 * to the client, since we assume that the client failed to
834 * respond because it believes that it is already in DO ECHO
835 * mode, which we do not want.
837 if (his_want_state_is_will(TELOPT_ECHO
)) {
838 DIAG(TD_OPTIONS
, output_data("td: simulating recv\r\n"));
839 willoption(TELOPT_ECHO
);
843 * Finally, to clean things up, we turn on our echo. This
844 * will break stupid 4.2 telnets out of local terminal echo.
847 if (my_state_is_wont(TELOPT_ECHO
))
848 send_will(TELOPT_ECHO
, 1);
850 #if (!defined(__BEOS__) && !defined(__HAIKU__))
852 * Turn on packet mode
854 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
857 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
859 * Continuing line mode support. If client does not support
860 * real linemode, attempt to negotiate kludge linemode by sending
861 * the do timing mark sequence.
863 if (lmodetype
< REAL_LINEMODE
)
864 send_do(TELOPT_TM
, 1);
865 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
868 * Call telrcv() once to pick up anything received during
869 * terminal type negotiation, 4.2/4.3 determination, and
870 * linemode negotiation.
874 (void) ioctl(f
, FIONBIO
, (char *)&on
);
875 (void) ioctl(p
, FIONBIO
, (char *)&on
);
877 #if defined(SO_OOBINLINE)
878 (void) setsockopt(net
, SOL_SOCKET
, SO_OOBINLINE
,
879 (char *)&on
, sizeof on
);
880 #endif /* defined(SO_OOBINLINE) */
883 (void) signal(SIGTSTP
, SIG_IGN
);
887 * Ignoring SIGTTOU keeps the kernel from blocking us
888 * in ttioct() in /sys/tty.c.
890 (void) signal(SIGTTOU
, SIG_IGN
);
893 (void) signal(SIGCHLD
, cleanup
);
898 t
= open(_PATH_TTY
, O_RDWR
);
900 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
907 * Show banner that getty never gave.
909 * We put the banner in the pty input buffer. This way, it
910 * gets carriage return null processing, etc., just like all
911 * other pty --> client data.
914 if (getent(defent
, "default") == 1) {
917 HE
= Getstr("he", &cp
);
918 HN
= Getstr("hn", &cp
);
919 IM
= Getstr("im", &cp
);
921 (void) strlcpy(host_name
, HN
, sizeof(host_name
));
925 IM
= strdup(DEFAULT_IM
);
928 edithost(HE
, host_name
);
933 (void) strncat(ptyibuf2
, ptyip
, pcc
+1);
938 * Last check to make sure all our states are correct.
942 #endif /* LINEMODE */
944 DIAG(TD_REPORT
, output_data("td: Entering processing loop\r\n"));
947 * Startup the login process on the slave side of the terminal
948 * now. We delay this until here to insure option negotiation
951 startslave(host
, level
, user_name
);
953 nfd
= ((f
> p
) ? f
: p
) + 1;
955 fd_set ibits
, obits
, xbits
;
958 if (ncc
< 0 && pcc
< 0)
965 * Never look for input if there's still
966 * stuff in the corresponding output buffer
968 if (nfrontp
- nbackp
|| pcc
> 0) {
973 if (pfrontp
- pbackp
|| ncc
> 0) {
981 if ((c
= select(nfd
, &ibits
, &obits
, &xbits
,
982 (struct timeval
*)0)) < 1) {
984 if (errno
== EINTR
) {
995 if (FD_ISSET(net
, &xbits
)) {
1000 * Something to read from the network...
1002 if (FD_ISSET(net
, &ibits
)) {
1003 #if !defined(SO_OOBINLINE)
1005 * In 4.2 (and 4.3 beta) systems, the
1006 * OOB indication and data handling in the kernel
1007 * is such that if two separate TCP Urgent requests
1008 * come in, one byte of TCP data will be overlaid.
1009 * This is fatal for Telnet, but we try to live
1012 * In addition, in 4.2 (and...), a special protocol
1013 * is needed to pick up the TCP Urgent data in
1014 * the correct sequence.
1016 * What we do is: if we think we are in urgent
1017 * mode, we look to see if we are "at the mark".
1018 * If we are, we do an OOB receive. If we run
1019 * this twice, we will do the OOB receive twice,
1020 * but the second will fail, since the second
1021 * time we were "at the mark", but there wasn't
1022 * any data there (the kernel doesn't reset
1023 * "at the mark" until we do a normal read).
1024 * Once we've read the OOB data, we go ahead
1025 * and do normal reads.
1027 * There is also another problem, which is that
1028 * since the OOB byte we read doesn't put us
1029 * out of OOB state, and since that byte is most
1030 * likely the TELNET DM (data mark), we would
1031 * stay in the TELNET SYNCH (SYNCHing) state.
1032 * So, clocks to the rescue. If we've "just"
1033 * received a DM, then we test for the
1034 * presence of OOB data when the receive OOB
1035 * fails (and AFTER we did the normal mode read
1036 * to clear "at the mark").
1041 (void) ioctl(net
, SIOCATMARK
, (char *)&atmark
);
1043 ncc
= recv(net
, netibuf
, sizeof (netibuf
), MSG_OOB
);
1044 if ((ncc
== -1) && (errno
== EINVAL
)) {
1045 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1046 if (sequenceIs(didnetreceive
, gotDM
)) {
1047 SYNCHing
= stilloob(net
);
1051 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1054 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1056 settimer(didnetreceive
);
1057 #else /* !defined(SO_OOBINLINE)) */
1058 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1059 #endif /* !defined(SO_OOBINLINE)) */
1060 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1068 DIAG((TD_REPORT
| TD_NETDATA
),
1069 output_data("td: netread %d chars\r\n", ncc
));
1070 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1074 * Something to read from the pty...
1076 if (FD_ISSET(p
, &ibits
)) {
1077 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1079 * On some systems, if we try to read something
1080 * off the master side before the slave side is
1081 * opened, we get EIO.
1083 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1094 * If ioctl from pty, pass it through net
1096 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1097 copy_termbuf(ptyibuf
+1, pcc
-1);
1101 #endif /* LINEMODE */
1102 #if (!defined(__BEOS__) && !defined(__HAIKU__))
1103 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1104 netclear(); /* clear buffer back */
1107 * There are client telnets on some
1108 * operating systems get screwed up
1109 * royally if we send them urgent
1112 output_data("%c%c", IAC
, DM
);
1113 neturg
= nfrontp
-1; /* off by one XXX */
1115 printoption("td: send IAC", DM
));
1119 if (his_state_is_will(TELOPT_LFLOW
) &&
1121 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1123 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1124 if (newflow
!= flowmode
) {
1126 output_data("%c%c%c%c%c%c",
1127 IAC
, SB
, TELOPT_LFLOW
,
1131 DIAG(TD_OPTIONS
, printsub('>',
1132 (unsigned char *)nfrontp
-4,
1137 #endif /* !__BEOS__ */
1138 //ptyip = ptyibuf+1;
1144 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1146 c
= *ptyip
++ & 0377, pcc
--;
1148 output_data("%c", c
);
1149 output_data("%c", c
);
1150 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1151 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1152 output_data("%c", *ptyip
++ & 0377);
1155 output_data("%c", '\0');
1159 if (FD_ISSET(f
, &obits
) && (nfrontp
- nbackp
) > 0)
1163 if (FD_ISSET(p
, &obits
) && (pfrontp
- pbackp
) > 0)
1167 } /* end of telnet */
1171 # define TCSIG TIOCSIG
1176 * Send interrupt to process on other side of pty.
1177 * If it is in raw mode, just write NULL;
1178 * otherwise, write intr char.
1183 ptyflush(); /* half-hearted */
1186 (void) ioctl(pty
, TCSIG
, (char *)SIGINT
);
1189 *pfrontp
++ = slctab
[SLC_IP
].sptr
?
1190 (unsigned char)*slctab
[SLC_IP
].sptr
: '\177';
1195 * Send quit to process on other side of pty.
1196 * If it is in raw mode, just write NULL;
1197 * otherwise, write quit char.
1202 ptyflush(); /* half-hearted */
1204 (void) ioctl(pty
, TCSIG
, (char *)SIGQUIT
);
1207 *pfrontp
++ = slctab
[SLC_ABORT
].sptr
?
1208 (unsigned char)*slctab
[SLC_ABORT
].sptr
: '\034';
1216 ptyflush(); /* half-hearted */
1218 (void) ioctl(pty
, TCSIG
, (char *)SIGTSTP
);
1220 *pfrontp
++ = slctab
[SLC_SUSP
].sptr
?
1221 (unsigned char)*slctab
[SLC_SUSP
].sptr
: '\032';
1223 #endif /* SIGTSTP */
1227 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1228 * just send back "[Yes]".
1233 #if defined(SIGINFO) && defined(TCSIG)
1234 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1235 (void) ioctl(pty
, TCSIG
, (char *)SIGINFO
);
1239 output_data("\r\n[Yes]\r\n");
1247 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1248 if (!tty_isediting()) {
1249 extern char oldeofc
;
1250 *pfrontp
++ = oldeofc
;
1254 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1255 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';