1 /* $NetBSD: telnetd.c,v 1.50 2006/05/09 20:18:07 mrg 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.50 2006/05/09 20:18:07 mrg Exp $");
73 #include "pathnames.h"
75 #include <arpa/inet.h>
83 #define Authenticator k5_Authenticator
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 char *gettyname
= "default";
124 int main(int, char *[]);
126 int getterminaltype(char *, size_t);
127 int getent(char *, char *);
128 void doit(struct sockaddr
*);
129 void _gettermname(void);
130 int terminaltypeok(char *);
131 char *getstr(const char *, char **);
134 * The string to pass to getopt(). We do it this way so
135 * that only the actual options that we support will be
136 * passed off to getopt().
138 char valid_opts
[] = {
139 'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
141 #ifdef AUTHENTICATION
162 int family
= AF_INET
;
163 struct sockaddr_storage from
;
166 main(int argc
, char *argv
[])
171 #if defined(IPPROTO_IP) && defined(IP_TOS)
175 pfrontp
= pbackp
= ptyobuf
;
177 nfrontp
= nbackp
= netobuf
;
180 #endif /* ENCRYPTION */
185 while ((ch
= getopt(argc
, argv
, valid_opts
)) != -1) {
188 #ifdef AUTHENTICATION
191 * Check for required authentication level
193 if (strcmp(optarg
, "debug") == 0) {
195 } else if (strcasecmp(optarg
, "none") == 0) {
197 } else if (strcasecmp(optarg
, "other") == 0) {
198 auth_level
= AUTH_OTHER
;
199 } else if (strcasecmp(optarg
, "user") == 0) {
200 auth_level
= AUTH_USER
;
201 } else if (strcasecmp(optarg
, "valid") == 0) {
202 auth_level
= AUTH_VALID
;
203 } else if (strcasecmp(optarg
, "off") == 0) {
205 * This hack turns off authentication
210 "telnetd: unknown authorization level for -a\n");
213 #endif /* AUTHENTICATION */
217 if (strcmp(optarg
, "ebug") == 0) {
228 * Check for desired diagnostics capabilities.
230 if (!strcmp(optarg
, "report")) {
231 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
232 } else if (!strcmp(optarg
, "exercise")) {
233 diagnostic
|= TD_EXERCISE
;
234 } else if (!strcmp(optarg
, "netdata")) {
235 diagnostic
|= TD_NETDATA
;
236 } else if (!strcmp(optarg
, "ptydata")) {
237 diagnostic
|= TD_PTYDATA
;
238 } else if (!strcmp(optarg
, "options")) {
239 diagnostic
|= TD_OPTIONS
;
245 #endif /* DIAGNOSTICS */
249 if (strcmp(optarg
, "debug") == 0) {
250 encrypt_debug_mode
= 1;
256 #endif /* ENCRYPTION */
269 require_hwpreauth
= 1;
279 #endif /* LINEMODE */
282 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
283 lmodetype
= NO_AUTOKLUDGE
;
285 /* ignore -k option if built without kludge linemode */
286 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
297 krb5_error_code retval
;
299 if (telnet_context
== 0) {
300 retval
= krb5_init_context(&telnet_context
);
302 com_err("telnetd", retval
,
303 "while initializing krb5");
307 krb5_set_default_realm(telnet_context
, optarg
);
314 /* Secure login required */
315 require_secure_login
= 1;
317 #endif /* SECURELOGIN */
319 fprintf(stderr
, "%s%s\n", "TOS option unavailable; ",
320 "-S flag not supported\n");
324 fprintf(stderr
, "telnetd: -u option unneeded\n");
328 registerd_host_only
= 1;
331 #ifdef AUTHENTICATION
334 * Check for invalid authentication types
336 auth_disable_name(optarg
);
338 #endif /* AUTHENTICATION */
349 fprintf(stderr
, "telnetd: %c: unknown option\n", ch
);
363 char *service
= "telnet";
364 struct addrinfo hints
, *res
;
369 } else if (argc
== 1)
372 memset(&hints
, 0, sizeof(hints
));
373 hints
.ai_flags
= AI_PASSIVE
;
374 hints
.ai_family
= family
;
375 hints
.ai_socktype
= SOCK_STREAM
;
376 hints
.ai_protocol
= 0;
377 error
= getaddrinfo(NULL
, service
, &hints
, &res
);
380 fprintf(stderr
, "tcp/%s: %s\n", service
, gai_strerror(error
));
384 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
386 perror("telnetd: socket");
389 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
390 (char *)&on
, sizeof(on
));
391 if (bind(s
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
395 if (listen(s
, 1) < 0) {
399 foo
= res
->ai_addrlen
;
400 ns
= accept(s
, res
->ai_addr
, &foo
);
408 } else if (argc
> 0) {
413 openlog("telnetd", LOG_PID
, LOG_DAEMON
);
414 fromlen
= sizeof (from
);
415 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
416 fprintf(stderr
, "%s: ", progname
);
417 perror("getpeername");
421 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
422 (char *)&on
, sizeof (on
)) < 0) {
423 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
426 #if defined(IPPROTO_IP) && defined(IP_TOS)
427 if (((struct sockaddr
*)&from
)->sa_family
== AF_INET
) {
429 tos
= 020; /* Low Delay bit */
431 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
432 (char *)&tos
, sizeof(tos
)) < 0)
433 && (errno
!= ENOPROTOOPT
) )
434 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
436 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
439 doit((struct sockaddr
*)&from
);
449 fprintf(stderr
, "Usage: telnetd");
450 #ifdef AUTHENTICATION
451 fprintf(stderr
, " [-a (debug|other|user|valid|off|none)]\n\t");
453 fprintf(stderr
, " [-debug]");
455 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
458 fprintf(stderr
, " [-edebug]");
460 fprintf(stderr
, " [-h]");
461 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
462 fprintf(stderr
, " [-k]");
465 fprintf(stderr
, " [-l]");
467 fprintf(stderr
, " [-n]");
468 fprintf(stderr
, "\n\t");
470 fprintf(stderr
, " [-s]");
472 #ifdef AUTHENTICATION
473 fprintf(stderr
, " [-X auth-type]");
475 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
476 fprintf(stderr
, " [port]\n");
483 * Ask the other end to send along its terminal type and speed.
484 * Output is the variable terminaltype filled in.
486 static unsigned char ttytype_sbbuf
[] = {
487 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
491 getterminaltype(char *name
, size_t l
)
496 #ifdef AUTHENTICATION
498 * Handle the Authentication option before we do anything else.
500 send_do(TELOPT_AUTHENTICATION
, 1);
501 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION
))
503 if (his_state_is_will(TELOPT_AUTHENTICATION
)) {
504 retval
= auth_wait(name
, l
);
509 send_will(TELOPT_ENCRYPT
, 1);
510 #endif /* ENCRYPTION */
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
) ||
519 #endif /* ENCRYPTION */
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
)) {
527 if (his_state_is_will(TELOPT_TSPEED
)) {
528 static unsigned char sb
[] =
529 { IAC
, SB
, TELOPT_TSPEED
, TELQUAL_SEND
, IAC
, SE
};
531 output_datalen((const char *)sb
, sizeof sb
);
532 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
536 * Wait for the negotiation of what type of encryption we can
537 * send with. If autoencrypt is not set, this will just return.
539 if (his_state_is_will(TELOPT_ENCRYPT
)) {
542 #endif /* ENCRYPTION */
543 if (his_state_is_will(TELOPT_XDISPLOC
)) {
544 static unsigned char sb
[] =
545 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
547 output_datalen((const char *)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 output_datalen((const char *)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 output_datalen((const char *)sb
, sizeof sb
);
562 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
564 if (his_state_is_will(TELOPT_TTYPE
)) {
566 output_datalen((const char *)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 (void) strlcpy(first
, terminaltype
, sizeof(first
));
600 * Save the unknown name, and request the next name.
602 (void) 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 (void) strlcpy(terminaltype
, first
, sizeof(terminaltype
));
629 } /* end of getterminaltype */
635 * If the client turned off the option,
636 * we can't send another request, so we
639 if (his_state_is_wont(TELOPT_TTYPE
))
642 output_datalen((const char *)ttytype_sbbuf
, sizeof ttytype_sbbuf
);
643 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
644 sizeof ttytype_sbbuf
- 2););
645 while (sequenceIs(ttypesubopt
, baseline
))
650 terminaltypeok(char *s
)
654 if (terminaltype
== NULL
)
658 * tgetent() will return 1 if the type is known, and
659 * 0 if it is not known. If it returns -1, it couldn't
660 * open the database. But if we can't open the database,
661 * it won't help to say we failed, because we won't be
662 * able to verify anything else. So, we treat -1 like 1.
664 if (tgetent(buf
, s
) == 0)
670 char host_name
[MAXHOSTNAMELEN
+ 1];
671 char remote_host_name
[MAXHOSTNAMELEN
+ 1];
673 extern void telnet(int, int);
676 * Get a pty, scan input lines.
679 doit(struct sockaddr
*who
)
689 * Find an available pty to use.
691 pty
= getpty(&ptynum
);
693 fatal(net
, "All network ports in use");
695 flags
= registerd_host_only
? NI_NAMEREQD
: 0;
697 /* get name of connected client */
698 error
= getnameinfo(who
, who
->sa_len
, remote_host_name
,
699 sizeof(remote_host_name
), NULL
, 0, flags
);
702 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
703 Please contact your net administrator");
705 host
= NULL
; /* XXX gcc */
709 remote_host_name
[sizeof(remote_host_name
)-1] = 0;
710 host
= remote_host_name
;
712 (void)gethostname(host_name
, sizeof(host_name
));
713 host_name
[sizeof(host_name
) - 1] = '\0';
714 hostname
= host_name
;
716 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
717 auth_encrypt_init(hostname
, host
, "TELNETD", 1);
725 level
= getterminaltype(user_name
, sizeof(user_name
));
726 setenv("TERM", terminaltype
[0] ? terminaltype
: "network", 1);
729 * Start up the login process on the slave side of the terminal
731 startslave(host
, level
, user_name
);
733 telnet(net
, pty
); /* begin server processing */
739 * Main loop. Select from pty and network, and
740 * hand data to telnet receiver finite state machine.
746 #define TABBUFSIZ 512
747 char defent
[TABBUFSIZ
];
748 char defstrs
[TABBUFSIZ
];
750 char *HE
, *HN
, *IM
, *IF
, *ptyibuf2ptr
;
751 struct pollfd set
[2];
754 * Initialize the slc mapping table.
759 * Do some tests where it is desireable to wait for a response.
760 * Rather than doing them slowly, one at a time, do them all
763 if (my_state_is_wont(TELOPT_SGA
))
764 send_will(TELOPT_SGA
, 1);
766 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
767 * because 4.2 clients are unable to deal with TCP urgent data.
769 * To find out, we send out a "DO ECHO". If the remote system
770 * answers "WILL ECHO" it is probably a 4.2 client, and we note
771 * that fact ("WILL ECHO" ==> that the client will echo what
772 * WE, the server, sends it; it does NOT mean that the client will
773 * echo the terminal input).
775 send_do(TELOPT_ECHO
, 1);
778 if (his_state_is_wont(TELOPT_LINEMODE
)) {
779 /* Query the peer for linemode support by trying to negotiate
780 * the linemode option.
784 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
786 #endif /* LINEMODE */
789 * Send along a couple of other options that we wish to negotiate.
791 send_do(TELOPT_NAWS
, 1);
792 send_will(TELOPT_STATUS
, 1);
793 flowmode
= 1; /* default flow control state */
794 restartany
= -1; /* uninitialized... */
795 send_do(TELOPT_LFLOW
, 1);
798 * Spin, waiting for a response from the DO ECHO. However,
799 * some REALLY DUMB telnets out there might not respond
800 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
801 * telnets so far seem to respond with WONT for a DO that
802 * they don't understand...) because by the time we get the
803 * response, it will already have processed the DO ECHO.
804 * Kludge upon kludge.
806 while (his_will_wont_is_changing(TELOPT_NAWS
))
811 * The client might have sent a WILL NAWS as part of its
812 * startup code; if so, we'll be here before we get the
813 * response to the DO ECHO. We'll make the assumption
814 * that any implementation that understands about NAWS
815 * is a modern enough implementation that it will respond
816 * to our DO ECHO request; hence we'll do another spin
817 * waiting for the ECHO option to settle down, which is
818 * what we wanted to do in the first place...
820 if (his_want_state_is_will(TELOPT_ECHO
) &&
821 his_state_is_will(TELOPT_NAWS
)) {
822 while (his_will_wont_is_changing(TELOPT_ECHO
))
826 * On the off chance that the telnet client is broken and does not
827 * respond to the DO ECHO we sent, (after all, we did send the
828 * DO NAWS negotiation after the DO ECHO, and we won't get here
829 * until a response to the DO NAWS comes back) simulate the
830 * receipt of a will echo. This will also send a WONT ECHO
831 * to the client, since we assume that the client failed to
832 * respond because it believes that it is already in DO ECHO
833 * mode, which we do not want.
835 if (his_want_state_is_will(TELOPT_ECHO
)) {
837 {output_data("td: simulating recv\r\n");});
838 willoption(TELOPT_ECHO
);
842 * Finally, to clean things up, we turn on our echo. This
843 * will break stupid 4.2 telnets out of local terminal echo.
846 if (my_state_is_wont(TELOPT_ECHO
))
847 send_will(TELOPT_ECHO
, 1);
850 * Turn on packet mode
852 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
854 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
856 * Continuing line mode support. If client does not support
857 * real linemode, attempt to negotiate kludge linemode by sending
858 * the do timing mark sequence.
860 if (lmodetype
< REAL_LINEMODE
)
861 send_do(TELOPT_TM
, 1);
862 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
865 * Call telrcv() once to pick up anything received during
866 * terminal type negotiation, 4.2/4.3 determination, and
867 * linemode negotiation.
871 (void) ioctl(f
, FIONBIO
, (char *)&on
);
872 (void) ioctl(p
, FIONBIO
, (char *)&on
);
874 (void) setsockopt(f
, SOL_SOCKET
, SO_OOBINLINE
, (char *)&on
, sizeof on
);
876 (void) signal(SIGTSTP
, SIG_IGN
);
878 * Ignoring SIGTTOU keeps the kernel from blocking us
879 * in ttioct() in /sys/tty.c.
881 (void) signal(SIGTTOU
, SIG_IGN
);
883 (void) signal(SIGCHLD
, cleanup
);
888 t
= open(_PATH_TTY
, O_RDWR
);
890 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
897 * Show banner that getty never gave.
899 * We put the banner in the pty input buffer. This way, it
900 * gets carriage return null processing, etc., just like all
901 * other pty --> client data.
904 if (getent(defent
, gettyname
) == 1) {
907 HE
= getstr("he", &cp
);
908 HN
= getstr("hn", &cp
);
909 IM
= getstr("im", &cp
);
910 IF
= getstr("if", &cp
);
912 (void)strlcpy(host_name
, HN
, sizeof(host_name
));
920 edithost(HE
, host_name
);
921 ptyibuf2ptr
= ptyibuf2
;
924 char buf
[_POSIX2_LINE_MAX
];
927 if ((fd
= fopen(IF
, "r")) != NULL
) {
928 while (fgets(buf
, sizeof(buf
) - 1, fd
) != NULL
)
929 ptyibuf2ptr
= putf(buf
, ptyibuf2ptr
);
934 ptyibuf2ptr
= putf(IM
, ptyibuf2ptr
);
938 strncpy(ptyibuf2ptr
, ptyip
, pcc
+1);
943 * Last check to make sure all our states are correct.
947 #endif /* LINEMODE */
950 {output_data("td: Entering processing loop\r\n");});
958 if (ncc
< 0 && pcc
< 0)
962 * Never look for input if there's still
963 * stuff in the corresponding output buffer
967 if (nfrontp
- nbackp
|| pcc
> 0)
968 set
[0].events
|= POLLOUT
;
970 set
[1].events
|= POLLIN
;
971 if (pfrontp
- pbackp
|| ncc
> 0)
972 set
[1].events
|= POLLOUT
;
974 set
[0].events
|= POLLIN
;
976 set
[0].events
|= POLLPRI
;
978 if ((c
= poll(set
, 2, INFTIM
)) < 1) {
980 if (errno
== EINTR
) {
991 if (set
[0].revents
& POLLPRI
) {
996 * Something to read from the network...
998 if (set
[0].revents
& POLLIN
) {
999 ncc
= read(f
, netibuf
, sizeof (netibuf
));
1000 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1008 DIAG((TD_REPORT
| TD_NETDATA
),
1009 {output_data("td: netread %d chars\r\n", ncc
);});
1010 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1014 * Something to read from the pty...
1016 if (set
[1].revents
& POLLIN
) {
1017 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1019 * On some systems, if we try to read something
1020 * off the master side before the slave side is
1021 * opened, we get EIO.
1023 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1032 * If ioctl from pty, pass it through net
1034 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1035 copy_termbuf(ptyibuf
+1, pcc
-1);
1039 #endif /* LINEMODE */
1040 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1041 netclear(); /* clear buffer back */
1043 * There are client telnets on some
1044 * operating systems get screwed up
1045 * royally if we send them urgent
1048 output_data("%c%c", IAC
, DM
);
1049 neturg
= nfrontp
- 1; /* off by one XXX */
1051 printoption("td: send IAC", DM
));
1053 if (his_state_is_will(TELOPT_LFLOW
) &&
1055 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1057 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1058 if (newflow
!= flowmode
) {
1062 IAC
, SB
, TELOPT_LFLOW
,
1066 DIAG(TD_OPTIONS
, printsub('>',
1067 (unsigned char *)nfrontp
- 4,
1077 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1079 c
= *ptyip
++ & 0377, pcc
--;
1081 output_data("%c", c
);
1082 output_data("%c", c
);
1083 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1084 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1085 output_data("%c", *ptyip
++ & 0377);
1088 output_datalen("\0", 1);
1092 if ((set
[0].revents
& POLLOUT
) && (nfrontp
- nbackp
) > 0)
1096 if ((set
[1].revents
& POLLOUT
) && (pfrontp
- pbackp
) > 0)
1100 } /* end of telnet */
1103 * Send interrupt to process on other side of pty.
1104 * If it is in raw mode, just write NULL;
1105 * otherwise, write intr char.
1110 ptyflush(); /* half-hearted */
1112 (void) ioctl(pty
, TIOCSIG
, (char *)SIGINT
);
1116 * Send quit to process on other side of pty.
1117 * If it is in raw mode, just write NULL;
1118 * otherwise, write quit char.
1123 ptyflush(); /* half-hearted */
1124 (void) ioctl(pty
, TIOCSIG
, (char *)SIGQUIT
);
1130 ptyflush(); /* half-hearted */
1131 (void) ioctl(pty
, TIOCSIG
, (char *)SIGTSTP
);
1135 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1136 * just send back "[Yes]".
1141 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1142 (void) ioctl(pty
, TIOCSIG
, (char *)SIGINFO
);
1145 (void) output_data("\r\n[Yes]\r\n");
1153 #if defined(LINEMODE) && (VEOF == VMIN)
1154 if (!tty_isediting()) {
1155 extern char oldeofc
;
1156 *pfrontp
++ = oldeofc
;
1160 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1161 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';