1 /* $NetBSD: telnetd.c,v 1.55 2014/02/27 18:20:21 joerg Exp $ */
4 * Copyright (C) 1997 and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
63 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
64 The Regents of the University of California. All rights reserved.");
66 static char sccsid
[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
68 __RCSID("$NetBSD: telnetd.c,v 1.55 2014/02/27 18:20:21 joerg Exp $");
73 #include "pathnames.h"
75 #include <arpa/inet.h>
83 #define Authenticator k5_Authenticator
86 #include <krb5/com_err.h>
93 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
94 #include <libtelnet/misc.h>
98 int require_secure_login
= 0;
101 extern int require_hwpreauth
;
103 extern krb5_context telnet_context
;
105 int registerd_host_only
= 0;
110 * pointers, and counters.
112 char ptyibuf
[BUFSIZ
], *ptyip
= ptyibuf
;
113 char ptyibuf2
[BUFSIZ
];
116 int hostinfo
= 1; /* do we print login banner? */
119 static int debug
= 0;
121 const char *gettyname
= "default";
124 void usage(void) __dead
;
125 int getterminaltype(char *, size_t);
126 int getent(char *, const char *);
127 static void doit(struct sockaddr
*) __dead
;
128 void _gettermname(void);
129 int terminaltypeok(char *);
130 char *getstr(const char *, char **);
133 * The string to pass to getopt(). We do it this way so
134 * that only the actual options that we support will be
135 * passed off to getopt().
137 char valid_opts
[] = {
138 'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
140 #ifdef AUTHENTICATION
161 int family
= AF_INET
;
162 struct sockaddr_storage from
;
165 main(int argc
, char *argv
[])
170 #if defined(IPPROTO_IP) && defined(IP_TOS)
174 pfrontp
= pbackp
= ptyobuf
;
176 nfrontp
= nbackp
= netobuf
;
179 #endif /* ENCRYPTION */
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
, "other") == 0) {
197 auth_level
= AUTH_OTHER
;
198 } else if (strcasecmp(optarg
, "user") == 0) {
199 auth_level
= AUTH_USER
;
200 } else if (strcasecmp(optarg
, "valid") == 0) {
201 auth_level
= AUTH_VALID
;
202 } else if (strcasecmp(optarg
, "off") == 0) {
204 * This hack turns off authentication
209 "telnetd: unknown authorization level for -a\n");
212 #endif /* AUTHENTICATION */
216 if (strcmp(optarg
, "ebug") == 0) {
227 * Check for desired diagnostics capabilities.
229 if (!strcmp(optarg
, "report")) {
230 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
231 } else if (!strcmp(optarg
, "exercise")) {
232 diagnostic
|= TD_EXERCISE
;
233 } else if (!strcmp(optarg
, "netdata")) {
234 diagnostic
|= TD_NETDATA
;
235 } else if (!strcmp(optarg
, "ptydata")) {
236 diagnostic
|= TD_PTYDATA
;
237 } else if (!strcmp(optarg
, "options")) {
238 diagnostic
|= TD_OPTIONS
;
244 #endif /* DIAGNOSTICS */
248 if (strcmp(optarg
, "debug") == 0) {
249 encrypt_debug_mode
= 1;
255 #endif /* ENCRYPTION */
268 require_hwpreauth
= 1;
278 #endif /* LINEMODE */
281 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
282 lmodetype
= NO_AUTOKLUDGE
;
284 /* ignore -k option if built without kludge linemode */
285 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
296 krb5_error_code retval
;
298 if (telnet_context
== 0) {
299 retval
= krb5_init_context(&telnet_context
);
301 com_err("telnetd", retval
,
302 "while initializing krb5");
306 krb5_set_default_realm(telnet_context
, optarg
);
313 /* Secure login required */
314 require_secure_login
= 1;
316 #endif /* SECURELOGIN */
318 fprintf(stderr
, "%s%s\n", "TOS option unavailable; ",
319 "-S flag not supported\n");
323 fprintf(stderr
, "telnetd: -u option unneeded\n");
327 registerd_host_only
= 1;
330 #ifdef AUTHENTICATION
333 * Check for invalid authentication types
335 auth_disable_name(optarg
);
337 #endif /* AUTHENTICATION */
348 fprintf(stderr
, "telnetd: %c: unknown option\n", ch
);
362 const char *service
= "telnet";
363 struct addrinfo hints
, *res
;
368 } else if (argc
== 1)
371 memset(&hints
, 0, sizeof(hints
));
372 hints
.ai_flags
= AI_PASSIVE
;
373 hints
.ai_family
= family
;
374 hints
.ai_socktype
= SOCK_STREAM
;
375 hints
.ai_protocol
= 0;
376 error
= getaddrinfo(NULL
, service
, &hints
, &res
);
379 fprintf(stderr
, "tcp/%s: %s\n", service
, gai_strerror(error
));
383 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
385 perror("telnetd: socket");
388 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
389 (char *)&on
, sizeof(on
));
390 if (bind(s
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
394 if (listen(s
, 1) < 0) {
398 foo
= res
->ai_addrlen
;
399 ns
= accept(s
, res
->ai_addr
, &foo
);
407 } else if (argc
> 0) {
412 openlog("telnetd", LOG_PID
, LOG_DAEMON
);
413 fromlen
= sizeof (from
);
414 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
415 fprintf(stderr
, "%s: ", progname
);
416 perror("getpeername");
420 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
421 (char *)&on
, sizeof (on
)) < 0) {
422 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
425 #if defined(IPPROTO_IP) && defined(IP_TOS)
426 if (((struct sockaddr
*)&from
)->sa_family
== AF_INET
) {
428 tos
= 020; /* Low Delay bit */
430 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
431 (char *)&tos
, sizeof(tos
)) < 0)
432 && (errno
!= ENOPROTOOPT
) )
433 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
435 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
438 doit((struct sockaddr
*)&from
);
448 fprintf(stderr
, "Usage: telnetd");
449 #ifdef AUTHENTICATION
450 fprintf(stderr
, " [-a (debug|other|user|valid|off|none)]\n\t");
452 fprintf(stderr
, " [-debug]");
454 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
457 fprintf(stderr
, " [-edebug]");
459 fprintf(stderr
, " [-h]");
460 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
461 fprintf(stderr
, " [-k]");
464 fprintf(stderr
, " [-l]");
466 fprintf(stderr
, " [-n]");
467 fprintf(stderr
, "\n\t");
469 fprintf(stderr
, " [-s]");
471 #ifdef AUTHENTICATION
472 fprintf(stderr
, " [-X auth-type]");
474 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
475 fprintf(stderr
, " [port]\n");
482 * Ask the other end to send along its terminal type and speed.
483 * Output is the variable terminaltype filled in.
485 static unsigned char ttytype_sbbuf
[] = {
486 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
490 getterminaltype(char *name
, size_t l
)
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
, l
);
508 send_will(TELOPT_ENCRYPT
, 1);
509 #endif /* ENCRYPTION */
510 send_do(TELOPT_TTYPE
, 1);
511 send_do(TELOPT_TSPEED
, 1);
512 send_do(TELOPT_XDISPLOC
, 1);
513 send_do(TELOPT_NEW_ENVIRON
, 1);
514 send_do(TELOPT_OLD_ENVIRON
, 1);
517 his_do_dont_is_changing(TELOPT_ENCRYPT
) ||
518 #endif /* ENCRYPTION */
519 his_will_wont_is_changing(TELOPT_TTYPE
) ||
520 his_will_wont_is_changing(TELOPT_TSPEED
) ||
521 his_will_wont_is_changing(TELOPT_XDISPLOC
) ||
522 his_will_wont_is_changing(TELOPT_NEW_ENVIRON
) ||
523 his_will_wont_is_changing(TELOPT_OLD_ENVIRON
)) {
526 if (his_state_is_will(TELOPT_TSPEED
)) {
527 static unsigned char sb
[] =
528 { IAC
, SB
, TELOPT_TSPEED
, TELQUAL_SEND
, IAC
, SE
};
530 output_datalen((const char *)sb
, sizeof sb
);
531 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
535 * Wait for the negotiation of what type of encryption we can
536 * send with. If autoencrypt is not set, this will just return.
538 if (his_state_is_will(TELOPT_ENCRYPT
)) {
541 #endif /* ENCRYPTION */
542 if (his_state_is_will(TELOPT_XDISPLOC
)) {
543 static unsigned char sb
[] =
544 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
546 output_datalen((const char *)sb
, sizeof sb
);
547 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
549 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
550 static unsigned char sb
[] =
551 { IAC
, SB
, TELOPT_NEW_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
553 output_datalen((const char *)sb
, sizeof sb
);
554 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
556 else if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
557 static unsigned char sb
[] =
558 { IAC
, SB
, TELOPT_OLD_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
560 output_datalen((const char *)sb
, sizeof sb
);
561 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
563 if (his_state_is_will(TELOPT_TTYPE
)) {
565 output_datalen((const char *)ttytype_sbbuf
, sizeof ttytype_sbbuf
);
566 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
567 sizeof ttytype_sbbuf
- 2););
569 if (his_state_is_will(TELOPT_TSPEED
)) {
570 while (sequenceIs(tspeedsubopt
, baseline
))
573 if (his_state_is_will(TELOPT_XDISPLOC
)) {
574 while (sequenceIs(xdisplocsubopt
, baseline
))
577 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
578 while (sequenceIs(environsubopt
, baseline
))
581 if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
582 while (sequenceIs(oenvironsubopt
, baseline
))
585 if (his_state_is_will(TELOPT_TTYPE
)) {
586 char first
[256], last
[256];
588 while (sequenceIs(ttypesubopt
, baseline
))
592 * If the other side has already disabled the option, then
593 * we have to just go with what we (might) have already gotten.
595 if (his_state_is_will(TELOPT_TTYPE
) && !terminaltypeok(terminaltype
)) {
596 (void) strlcpy(first
, terminaltype
, sizeof(first
));
599 * Save the unknown name, and request the next name.
601 (void) strlcpy(last
, terminaltype
, sizeof(last
));
603 if (terminaltypeok(terminaltype
))
605 if ((strncmp(last
, terminaltype
, sizeof(last
)) == 0) ||
606 his_state_is_wont(TELOPT_TTYPE
)) {
608 * We've hit the end. If this is the same as
609 * the first name, just go with it.
611 if (strncmp(first
, terminaltype
, sizeof(first
)) == 0)
614 * Get the terminal name one more time, so that
615 * RFC1091 compliant telnets will cycle back to
616 * the start of the list.
619 if (strncmp(first
, terminaltype
, sizeof(first
)) != 0) {
620 (void) strlcpy(terminaltype
, first
, sizeof(terminaltype
));
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 output_datalen((const char *)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
)
654 * tgetent() will return 1 if the type is known, and
655 * 0 if it is not known. If it returns -1, it couldn't
656 * open the database. But if we can't open the database,
657 * it won't help to say we failed, because we won't be
658 * able to verify anything else. So, we treat -1 like 1.
660 if (tgetent(buf
, s
) == 0)
666 char host_name
[MAXHOSTNAMELEN
+ 1];
667 char remote_host_name
[MAXHOSTNAMELEN
+ 1];
669 static void telnet(int, int) __dead
;
672 * Get a pty, scan input lines.
675 doit(struct sockaddr
*who
)
685 * Find an available pty to use.
687 pty
= getpty(&ptynum
);
689 fatal(net
, "All network ports in use");
691 flags
= registerd_host_only
? NI_NAMEREQD
: 0;
693 /* get name of connected client */
694 error
= getnameinfo(who
, who
->sa_len
, remote_host_name
,
695 sizeof(remote_host_name
), NULL
, 0, flags
);
698 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
699 Please contact your net administrator");
701 host
= NULL
; /* XXX gcc */
705 remote_host_name
[sizeof(remote_host_name
)-1] = 0;
706 host
= remote_host_name
;
708 (void)gethostname(host_name
, sizeof(host_name
));
709 host_name
[sizeof(host_name
) - 1] = '\0';
710 hostname
= host_name
;
712 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
713 auth_encrypt_init(hostname
, host
, "TELNETD", 1);
721 level
= getterminaltype(user_name
, sizeof(user_name
));
722 setenv("TERM", terminaltype
[0] ? terminaltype
: "network", 1);
725 * Start up the login process on the slave side of the terminal
727 startslave(host
, level
, user_name
);
729 telnet(net
, pty
); /* begin server processing */
735 * Main loop. Select from pty and network, and
736 * hand data to telnet receiver finite state machine.
742 #define TABBUFSIZ 512
743 char defent
[TABBUFSIZ
];
744 char defstrs
[TABBUFSIZ
];
746 char *HE
, *HN
, *IF
, *ptyibuf2ptr
;
748 struct pollfd set
[2];
751 * Initialize the slc mapping table.
756 * Do some tests where it is desireable to wait for a response.
757 * Rather than doing them slowly, one at a time, do them all
760 if (my_state_is_wont(TELOPT_SGA
))
761 send_will(TELOPT_SGA
, 1);
763 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
764 * because 4.2 clients are unable to deal with TCP urgent data.
766 * To find out, we send out a "DO ECHO". If the remote system
767 * answers "WILL ECHO" it is probably a 4.2 client, and we note
768 * that fact ("WILL ECHO" ==> that the client will echo what
769 * WE, the server, sends it; it does NOT mean that the client will
770 * echo the terminal input).
772 send_do(TELOPT_ECHO
, 1);
775 if (his_state_is_wont(TELOPT_LINEMODE
)) {
776 /* Query the peer for linemode support by trying to negotiate
777 * the linemode option.
781 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
783 #endif /* LINEMODE */
786 * Send along a couple of other options that we wish to negotiate.
788 send_do(TELOPT_NAWS
, 1);
789 send_will(TELOPT_STATUS
, 1);
790 flowmode
= 1; /* default flow control state */
791 restartany
= -1; /* uninitialized... */
792 send_do(TELOPT_LFLOW
, 1);
795 * Spin, waiting for a response from the DO ECHO. However,
796 * some REALLY DUMB telnets out there might not respond
797 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
798 * telnets so far seem to respond with WONT for a DO that
799 * they don't understand...) because by the time we get the
800 * response, it will already have processed the DO ECHO.
801 * Kludge upon kludge.
803 while (his_will_wont_is_changing(TELOPT_NAWS
))
808 * The client might have sent a WILL NAWS as part of its
809 * startup code; if so, we'll be here before we get the
810 * response to the DO ECHO. We'll make the assumption
811 * that any implementation that understands about NAWS
812 * is a modern enough implementation that it will respond
813 * to our DO ECHO request; hence we'll do another spin
814 * waiting for the ECHO option to settle down, which is
815 * what we wanted to do in the first place...
817 if (his_want_state_is_will(TELOPT_ECHO
) &&
818 his_state_is_will(TELOPT_NAWS
)) {
819 while (his_will_wont_is_changing(TELOPT_ECHO
))
823 * On the off chance that the telnet client is broken and does not
824 * respond to the DO ECHO we sent, (after all, we did send the
825 * DO NAWS negotiation after the DO ECHO, and we won't get here
826 * until a response to the DO NAWS comes back) simulate the
827 * receipt of a will echo. This will also send a WONT ECHO
828 * to the client, since we assume that the client failed to
829 * respond because it believes that it is already in DO ECHO
830 * mode, which we do not want.
832 if (his_want_state_is_will(TELOPT_ECHO
)) {
834 {output_data("td: simulating recv\r\n");});
835 willoption(TELOPT_ECHO
);
839 * Finally, to clean things up, we turn on our echo. This
840 * will break stupid 4.2 telnets out of local terminal echo.
843 if (my_state_is_wont(TELOPT_ECHO
))
844 send_will(TELOPT_ECHO
, 1);
847 * Turn on packet mode
849 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
851 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
853 * Continuing line mode support. If client does not support
854 * real linemode, attempt to negotiate kludge linemode by sending
855 * the do timing mark sequence.
857 if (lmodetype
< REAL_LINEMODE
)
858 send_do(TELOPT_TM
, 1);
859 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
862 * Call telrcv() once to pick up anything received during
863 * terminal type negotiation, 4.2/4.3 determination, and
864 * linemode negotiation.
868 (void) ioctl(f
, FIONBIO
, (char *)&on
);
869 (void) ioctl(p
, FIONBIO
, (char *)&on
);
871 (void) setsockopt(f
, SOL_SOCKET
, SO_OOBINLINE
, (char *)&on
, sizeof on
);
873 (void) signal(SIGTSTP
, SIG_IGN
);
875 * Ignoring SIGTTOU keeps the kernel from blocking us
876 * in ttioct() in /sys/tty.c.
878 (void) signal(SIGTTOU
, SIG_IGN
);
880 (void) signal(SIGCHLD
, cleanup
);
885 t
= open(_PATH_TTY
, O_RDWR
);
887 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
894 * Show banner that getty never gave.
896 * We put the banner in the pty input buffer. This way, it
897 * gets carriage return null processing, etc., just like all
898 * other pty --> client data.
901 if (getent(defent
, gettyname
) == 1) {
904 HE
= getstr("he", &cp
);
905 HN
= getstr("hn", &cp
);
906 IM
= getstr("im", &cp
);
907 IF
= getstr("if", &cp
);
909 (void)strlcpy(host_name
, HN
, sizeof(host_name
));
917 edithost(HE
, host_name
);
918 ptyibuf2ptr
= ptyibuf2
;
921 char buf
[_POSIX2_LINE_MAX
];
924 if ((fd
= fopen(IF
, "r")) != NULL
) {
925 while (fgets(buf
, sizeof(buf
) - 1, fd
) != NULL
)
926 ptyibuf2ptr
= putf(buf
, ptyibuf2ptr
);
931 ptyibuf2ptr
= putf(IM
, ptyibuf2ptr
);
935 strncpy(ptyibuf2ptr
, ptyip
, pcc
+1);
940 * Last check to make sure all our states are correct.
944 #endif /* LINEMODE */
947 {output_data("td: Entering processing loop\r\n");});
955 if (ncc
< 0 && pcc
< 0)
959 * Never look for input if there's still
960 * stuff in the corresponding output buffer
964 if (nfrontp
- nbackp
|| pcc
> 0)
965 set
[0].events
|= POLLOUT
;
967 set
[1].events
|= POLLIN
;
968 if (pfrontp
- pbackp
|| ncc
> 0)
969 set
[1].events
|= POLLOUT
;
971 set
[0].events
|= POLLIN
;
973 set
[0].events
|= POLLPRI
;
975 if ((c
= poll(set
, 2, INFTIM
)) < 1) {
977 if (errno
== EINTR
) {
988 if (set
[0].revents
& POLLPRI
) {
993 * Something to read from the network...
995 if (set
[0].revents
& POLLIN
) {
996 ncc
= read(f
, netibuf
, sizeof (netibuf
));
997 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1005 DIAG((TD_REPORT
| TD_NETDATA
),
1006 {output_data("td: netread %d chars\r\n", ncc
);});
1007 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1011 * Something to read from the pty...
1013 if (set
[1].revents
& POLLIN
) {
1014 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1016 * On some systems, if we try to read something
1017 * off the master side before the slave side is
1018 * opened, we get EIO.
1020 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1029 * If ioctl from pty, pass it through net
1031 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1032 copy_termbuf(ptyibuf
+1, pcc
-1);
1036 #endif /* LINEMODE */
1037 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1038 netclear(); /* clear buffer back */
1040 * There are client telnets on some
1041 * operating systems get screwed up
1042 * royally if we send them urgent
1045 output_data("%c%c", IAC
, DM
);
1046 neturg
= nfrontp
- 1; /* off by one XXX */
1048 printoption("td: send IAC", DM
));
1050 if (his_state_is_will(TELOPT_LFLOW
) &&
1052 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1054 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1055 if (newflow
!= flowmode
) {
1059 IAC
, SB
, TELOPT_LFLOW
,
1063 DIAG(TD_OPTIONS
, printsub('>',
1064 (unsigned char *)nfrontp
- 4,
1074 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1076 c
= *ptyip
++ & 0377, pcc
--;
1078 output_data("%c", c
);
1079 output_data("%c", c
);
1080 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1081 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1082 output_data("%c", *ptyip
++ & 0377);
1085 output_datalen("\0", 1);
1089 if ((set
[0].revents
& POLLOUT
) && (nfrontp
- nbackp
) > 0)
1093 if ((set
[1].revents
& POLLOUT
) && (pfrontp
- pbackp
) > 0)
1097 } /* end of telnet */
1100 * Send interrupt to process on other side of pty.
1101 * If it is in raw mode, just write NULL;
1102 * otherwise, write intr char.
1107 ptyflush(); /* half-hearted */
1109 (void) ioctl(pty
, TIOCSIG
, (char *)SIGINT
);
1113 * Send quit to process on other side of pty.
1114 * If it is in raw mode, just write NULL;
1115 * otherwise, write quit char.
1120 ptyflush(); /* half-hearted */
1121 (void) ioctl(pty
, TIOCSIG
, (char *)SIGQUIT
);
1127 ptyflush(); /* half-hearted */
1128 (void) ioctl(pty
, TIOCSIG
, (char *)SIGTSTP
);
1132 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1133 * just send back "[Yes]".
1138 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1139 (void) ioctl(pty
, TIOCSIG
, (char *)SIGINFO
);
1142 (void) output_data("\r\n[Yes]\r\n");
1150 #if defined(LINEMODE) && (VEOF == VMIN)
1151 if (!tty_isediting()) {
1152 extern char oldeofc
;
1153 *pfrontp
++ = oldeofc
;
1157 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1158 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';