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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)telnetd.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnetd/telnetd.c,v 1.11.2.5 2002/04/13 10:59:09 markm Exp $
34 #include "pathnames.h"
43 #include <arpa/inet.h>
46 #include <libtelnet/auth.h>
50 #include <libtelnet/encrypt.h>
52 #include <libtelnet/misc.h>
54 char remote_hostname
[MAXHOSTNAMELEN
];
55 size_t utmp_len
= sizeof(remote_hostname
) - 1;
56 int registerd_host_only
= 0;
61 * pointers, and counters.
63 char ptyibuf
[BUFSIZ
], *ptyip
= ptyibuf
;
64 char ptyibuf2
[BUFSIZ
];
66 int readstream(int, char *, int);
67 void doit(struct sockaddr
*) __dead2
;
68 int terminaltypeok(char *);
70 int hostinfo
= 1; /* do we print login banner? */
76 void startslave(char *, int, char *);
77 extern void usage(void) __dead2
;
78 static void _gettermname(void);
81 * The string to pass to getopt(). We do it this way so
82 * that only the actual options that we support will be
83 * passed off to getopt().
86 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
106 int family
= AF_INET
;
108 #ifndef MAXHOSTNAMELEN
109 #define MAXHOSTNAMELEN 256
110 #endif /* MAXHOSTNAMELEN */
113 char host_name
[MAXHOSTNAMELEN
];
115 extern void telnet(int, int, char *) __dead2
;
121 main(int argc
, char *argv
[])
123 struct sockaddr_storage from
;
126 #if defined(IPPROTO_IP) && defined(IP_TOS)
130 pfrontp
= pbackp
= ptyobuf
;
132 nfrontp
= nbackp
= netobuf
;
135 #endif /* ENCRYPTION */
138 * This initialization causes linemode to default to a configuration
139 * that works on all telnet clients, including the FreeBSD client.
140 * This is not quite the same as the telnet client issuing a "mode
141 * character" command, but has most of the same benefits, and is
142 * preferable since some clients (like usofts) don't have the
143 * mode character command anyway and linemode breaks things.
144 * The most notable symptom of fix is that csh "set filec" operations
145 * like <ESC> (filename completion) and ^D (choices) keys now work
146 * in telnet sessions and can be used more than once on the same line.
147 * CR/LF handling is also corrected in some termio modes. This
148 * change resolves problem reports bin/771 and bin/1037.
151 linemode
=1; /*Default to mode that works on bulk of clients*/
153 while ((ch
= getopt(argc
, argv
, valid_opts
)) != -1) {
156 #ifdef AUTHENTICATION
159 * Check for required authentication level
161 if (strcmp(optarg
, "debug") == 0) {
162 extern int auth_debug_mode
;
164 } else if (strcasecmp(optarg
, "none") == 0) {
166 } else if (strcasecmp(optarg
, "other") == 0) {
167 auth_level
= AUTH_OTHER
;
168 } else if (strcasecmp(optarg
, "user") == 0) {
169 auth_level
= AUTH_USER
;
170 } else if (strcasecmp(optarg
, "valid") == 0) {
171 auth_level
= AUTH_VALID
;
172 } else if (strcasecmp(optarg
, "off") == 0) {
174 * This hack turns off authentication
178 warnx("unknown authorization level for -a");
181 #endif /* AUTHENTICATION */
187 #endif /* BFTPDAEMON */
190 if (strcmp(optarg
, "ebug") == 0) {
201 * Check for desired diagnostics capabilities.
203 if (!strcmp(optarg
, "report")) {
204 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
205 } else if (!strcmp(optarg
, "exercise")) {
206 diagnostic
|= TD_EXERCISE
;
207 } else if (!strcmp(optarg
, "netdata")) {
208 diagnostic
|= TD_NETDATA
;
209 } else if (!strcmp(optarg
, "ptydata")) {
210 diagnostic
|= TD_PTYDATA
;
211 } else if (!strcmp(optarg
, "options")) {
212 diagnostic
|= TD_OPTIONS
;
218 #endif /* DIAGNOSTICS */
222 if (strcmp(optarg
, "debug") == 0) {
223 extern int encrypt_debug_mode
;
224 encrypt_debug_mode
= 1;
230 #endif /* ENCRYPTION */
240 #endif /* LINEMODE */
243 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
244 lmodetype
= NO_AUTOKLUDGE
;
246 /* ignore -k option if built without kludge linemode */
247 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
260 if ((tos
= parsetos(optarg
, "tcp")) < 0)
262 "bad TOS argument '", optarg
,
263 "'; will try to use default TOS");
265 warnx("TOS option unavailable; -S flag not supported");
270 utmp_len
= (size_t)atoi(optarg
);
271 if (utmp_len
>= sizeof(remote_hostname
))
272 utmp_len
= sizeof(remote_hostname
) - 1;
276 registerd_host_only
= 1;
279 #ifdef AUTHENTICATION
282 * Check for invalid authentication types
284 auth_disable_name(optarg
);
286 #endif /* AUTHENTICATION */
299 warnx("%c: unknown option", ch
);
311 int s
, ns
, foo
, error
;
312 const char *service
= "telnet";
313 struct addrinfo hints
, *res
;
318 } else if (argc
== 1)
321 memset(&hints
, 0, sizeof(hints
));
322 hints
.ai_flags
= AI_PASSIVE
;
323 hints
.ai_family
= family
;
324 hints
.ai_socktype
= SOCK_STREAM
;
325 hints
.ai_protocol
= 0;
326 error
= getaddrinfo(NULL
, service
, &hints
, &res
);
329 errx(1, "tcp/%s: %s\n", service
, gai_strerror(error
));
330 if (error
== EAI_SYSTEM
)
331 errx(1, "tcp/%s: %s\n", service
, strerror(errno
));
335 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
338 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
339 (char *)&on
, sizeof(on
));
340 if (bind(s
, res
->ai_addr
, res
->ai_addrlen
) < 0)
342 if (listen(s
, 1) < 0)
344 foo
= res
->ai_addrlen
;
345 ns
= accept(s
, res
->ai_addr
, &foo
);
352 } else if (argc
== 1) {
353 ; /* VOID*/ /* Just ignore the host/port name */
355 } else if (argc
> 0) {
360 openlog("telnetd", LOG_PID
, LOG_DAEMON
);
361 fromlen
= sizeof (from
);
362 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
367 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
368 (char *)&on
, sizeof (on
)) < 0) {
369 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
372 #if defined(IPPROTO_IP) && defined(IP_TOS)
373 if (from
.ss_family
== AF_INET
) {
374 # if defined(HAS_GETTOS)
376 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
380 tos
= 020; /* Low Delay bit */
382 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
383 (char *)&tos
, sizeof(tos
)) < 0)
384 && (errno
!= ENOPROTOOPT
) )
385 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
387 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
389 doit((struct sockaddr
*)&from
);
397 fprintf(stderr
, "usage: telnetd");
398 #ifdef AUTHENTICATION
399 fprintf(stderr
, " [-a (debug|other|user|valid|off|none)]\n\t");
402 fprintf(stderr
, " [-B]");
404 fprintf(stderr
, " [-debug]");
406 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
408 #ifdef AUTHENTICATION
409 fprintf(stderr
, " [-edebug]");
411 fprintf(stderr
, " [-h]");
412 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
413 fprintf(stderr
, " [-k]");
416 fprintf(stderr
, " [-l]");
418 fprintf(stderr
, " [-n]");
419 fprintf(stderr
, "\n\t");
421 fprintf(stderr
, " [-S tos]");
423 #ifdef AUTHENTICATION
424 fprintf(stderr
, " [-X auth-type]");
426 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
427 fprintf(stderr
, " [port]\n");
434 * Ask the other end to send along its terminal type and speed.
435 * Output is the variable terminaltype filled in.
437 static unsigned char ttytype_sbbuf
[] = {
438 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
442 #ifndef AUTHENTICATION
443 #define undef2 __unused
449 getterminaltype(char *name undef2
)
454 #ifdef AUTHENTICATION
456 * Handle the Authentication option before we do anything else.
458 send_do(TELOPT_AUTHENTICATION
, 1);
459 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION
))
461 if (his_state_is_will(TELOPT_AUTHENTICATION
)) {
462 retval
= auth_wait(name
);
467 send_will(TELOPT_ENCRYPT
, 1);
468 #endif /* ENCRYPTION */
469 send_do(TELOPT_TTYPE
, 1);
470 send_do(TELOPT_TSPEED
, 1);
471 send_do(TELOPT_XDISPLOC
, 1);
472 send_do(TELOPT_NEW_ENVIRON
, 1);
473 send_do(TELOPT_OLD_ENVIRON
, 1);
476 his_do_dont_is_changing(TELOPT_ENCRYPT
) ||
477 #endif /* ENCRYPTION */
478 his_will_wont_is_changing(TELOPT_TTYPE
) ||
479 his_will_wont_is_changing(TELOPT_TSPEED
) ||
480 his_will_wont_is_changing(TELOPT_XDISPLOC
) ||
481 his_will_wont_is_changing(TELOPT_NEW_ENVIRON
) ||
482 his_will_wont_is_changing(TELOPT_OLD_ENVIRON
)) {
487 * Wait for the negotiation of what type of encryption we can
488 * send with. If autoencrypt is not set, this will just return.
490 if (his_state_is_will(TELOPT_ENCRYPT
)) {
493 #endif /* ENCRYPTION */
494 if (his_state_is_will(TELOPT_TSPEED
)) {
495 static unsigned char sb
[] =
496 { IAC
, SB
, TELOPT_TSPEED
, TELQUAL_SEND
, IAC
, SE
};
498 output_datalen(sb
, sizeof sb
);
499 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
501 if (his_state_is_will(TELOPT_XDISPLOC
)) {
502 static unsigned char sb
[] =
503 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
505 output_datalen(sb
, sizeof sb
);
506 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
508 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
509 static unsigned char sb
[] =
510 { IAC
, SB
, TELOPT_NEW_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
512 output_datalen(sb
, sizeof sb
);
513 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
515 else if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
516 static unsigned char sb
[] =
517 { IAC
, SB
, TELOPT_OLD_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
519 output_datalen(sb
, sizeof sb
);
520 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
522 if (his_state_is_will(TELOPT_TTYPE
)) {
524 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
525 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
526 sizeof ttytype_sbbuf
- 2););
528 if (his_state_is_will(TELOPT_TSPEED
)) {
529 while (sequenceIs(tspeedsubopt
, baseline
))
532 if (his_state_is_will(TELOPT_XDISPLOC
)) {
533 while (sequenceIs(xdisplocsubopt
, baseline
))
536 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
537 while (sequenceIs(environsubopt
, baseline
))
540 if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
541 while (sequenceIs(oenvironsubopt
, baseline
))
544 if (his_state_is_will(TELOPT_TTYPE
)) {
545 char first
[256], last
[256];
547 while (sequenceIs(ttypesubopt
, baseline
))
551 * If the other side has already disabled the option, then
552 * we have to just go with what we (might) have already gotten.
554 if (his_state_is_will(TELOPT_TTYPE
) && !terminaltypeok(terminaltype
)) {
555 (void) strncpy(first
, terminaltype
, sizeof(first
)-1);
556 first
[sizeof(first
)-1] = '\0';
559 * Save the unknown name, and request the next name.
561 (void) strncpy(last
, terminaltype
, sizeof(last
)-1);
562 last
[sizeof(last
)-1] = '\0';
564 if (terminaltypeok(terminaltype
))
566 if ((strncmp(last
, terminaltype
, sizeof(last
)) == 0) ||
567 his_state_is_wont(TELOPT_TTYPE
)) {
569 * We've hit the end. If this is the same as
570 * the first name, just go with it.
572 if (strncmp(first
, terminaltype
, sizeof(first
)) == 0)
575 * Get the terminal name one more time, so that
576 * RFC1091 compliant telnets will cycle back to
577 * the start of the list.
580 if (strncmp(first
, terminaltype
, sizeof(first
)) != 0) {
581 (void) strncpy(terminaltype
, first
, TERMINAL_TYPE_SIZE
-1);
582 terminaltype
[TERMINAL_TYPE_SIZE
-1] = '\0';
590 } /* end of getterminaltype */
596 * If the client turned off the option,
597 * we can't send another request, so we
600 if (his_state_is_wont(TELOPT_TTYPE
))
603 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
604 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
605 sizeof ttytype_sbbuf
- 2););
606 while (sequenceIs(ttypesubopt
, baseline
))
611 terminaltypeok(char *s
)
615 if (terminaltype
== NULL
)
619 * tgetent() will return 1 if the type is known, and
620 * 0 if it is not known. If it returns -1, it couldn't
621 * open the database. But if we can't open the database,
622 * it won't help to say we failed, because we won't be
623 * able to verify anything else. So, we treat -1 like 1.
625 if (tgetent(buf
, s
) == 0)
631 * Get a pty, scan input lines.
634 doit(struct sockaddr
*who
)
640 * Find an available pty to use.
643 pty
= getpty(&ptynum
);
645 fatal(net
, "All network ports in use");
650 if ((lp
= getpty()) == NULL
)
651 fatal(net
, "Out of ptys");
653 if ((pty
= open(lp
, 2)) >= 0) {
654 strlcpy(line
,lp
,sizeof(line
));
661 /* get name of connected client */
662 if (realhostname_sa(remote_hostname
, sizeof(remote_hostname
) - 1,
663 who
, who
->sa_len
) == HOSTNAME_INVALIDADDR
&& registerd_host_only
)
664 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
665 Please contact your net administrator");
666 remote_hostname
[sizeof(remote_hostname
) - 1] = '\0';
668 trimdomain(remote_hostname
, UT_HOSTSIZE
);
669 if (!isdigit(remote_hostname
[0]) && strlen(remote_hostname
) > utmp_len
)
670 err_
= getnameinfo(who
, who
->sa_len
, remote_hostname
,
671 sizeof(remote_hostname
), NULL
, 0,
672 NI_NUMERICHOST
|NI_WITHSCOPEID
);
673 /* XXX: do 'err_' check */
675 (void) gethostname(host_name
, sizeof(host_name
) - 1);
676 host_name
[sizeof(host_name
) - 1] = '\0';
677 hostname
= host_name
;
679 #ifdef AUTHENTICATION
681 /* The above #ifdefs should actually be "or"'ed, not "and"'ed.
682 * This is a byproduct of needing "#ifdef" and not "#if defined()"
683 * for unifdef. XXX MarkM
685 auth_encrypt_init(hostname
, remote_hostname
, "TELNETD", 1);
694 level
= getterminaltype(user_name
);
695 if (setenv("TERM", terminaltype
? terminaltype
: "network", 1) == -1)
696 syslog(LOG_ERR
, "setenv: cannot set TERM=%s: %m", terminaltype
? terminaltype
: "network");
698 telnet(net
, pty
, remote_hostname
); /* begin server process */
704 * Main loop. Select from pty and network, and
705 * hand data to telnet receiver finite state machine.
708 telnet(int f
, int p
, char *host
)
711 #define TABBUFSIZ 512
712 char defent
[TABBUFSIZ
];
713 char defstrs
[TABBUFSIZ
];
721 * Initialize the slc mapping table.
726 * Do some tests where it is desireable to wait for a response.
727 * Rather than doing them slowly, one at a time, do them all
730 if (my_state_is_wont(TELOPT_SGA
))
731 send_will(TELOPT_SGA
, 1);
733 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
734 * because 4.2 clients are unable to deal with TCP urgent data.
736 * To find out, we send out a "DO ECHO". If the remote system
737 * answers "WILL ECHO" it is probably a 4.2 client, and we note
738 * that fact ("WILL ECHO" ==> that the client will echo what
739 * WE, the server, sends it; it does NOT mean that the client will
740 * echo the terminal input).
742 send_do(TELOPT_ECHO
, 1);
745 if (his_state_is_wont(TELOPT_LINEMODE
)) {
746 /* Query the peer for linemode support by trying to negotiate
747 * the linemode option.
751 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
753 #endif /* LINEMODE */
756 * Send along a couple of other options that we wish to negotiate.
758 send_do(TELOPT_NAWS
, 1);
759 send_will(TELOPT_STATUS
, 1);
760 flowmode
= 1; /* default flow control state */
761 restartany
= -1; /* uninitialized... */
762 send_do(TELOPT_LFLOW
, 1);
765 * Spin, waiting for a response from the DO ECHO. However,
766 * some REALLY DUMB telnets out there might not respond
767 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
768 * telnets so far seem to respond with WONT for a DO that
769 * they don't understand...) because by the time we get the
770 * response, it will already have processed the DO ECHO.
771 * Kludge upon kludge.
773 while (his_will_wont_is_changing(TELOPT_NAWS
))
778 * The client might have sent a WILL NAWS as part of its
779 * startup code; if so, we'll be here before we get the
780 * response to the DO ECHO. We'll make the assumption
781 * that any implementation that understands about NAWS
782 * is a modern enough implementation that it will respond
783 * to our DO ECHO request; hence we'll do another spin
784 * waiting for the ECHO option to settle down, which is
785 * what we wanted to do in the first place...
787 if (his_want_state_is_will(TELOPT_ECHO
) &&
788 his_state_is_will(TELOPT_NAWS
)) {
789 while (his_will_wont_is_changing(TELOPT_ECHO
))
793 * On the off chance that the telnet client is broken and does not
794 * respond to the DO ECHO we sent, (after all, we did send the
795 * DO NAWS negotiation after the DO ECHO, and we won't get here
796 * until a response to the DO NAWS comes back) simulate the
797 * receipt of a will echo. This will also send a WONT ECHO
798 * to the client, since we assume that the client failed to
799 * respond because it believes that it is already in DO ECHO
800 * mode, which we do not want.
802 if (his_want_state_is_will(TELOPT_ECHO
)) {
803 DIAG(TD_OPTIONS
, output_data("td: simulating recv\r\n"));
804 willoption(TELOPT_ECHO
);
808 * Finally, to clean things up, we turn on our echo. This
809 * will break stupid 4.2 telnets out of local terminal echo.
812 if (my_state_is_wont(TELOPT_ECHO
))
813 send_will(TELOPT_ECHO
, 1);
816 * Turn on packet mode
818 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
820 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
822 * Continuing line mode support. If client does not support
823 * real linemode, attempt to negotiate kludge linemode by sending
824 * the do timing mark sequence.
826 if (lmodetype
< REAL_LINEMODE
)
827 send_do(TELOPT_TM
, 1);
828 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
831 * Call telrcv() once to pick up anything received during
832 * terminal type negotiation, 4.2/4.3 determination, and
833 * linemode negotiation.
837 (void) ioctl(f
, FIONBIO
, (char *)&on
);
838 (void) ioctl(p
, FIONBIO
, (char *)&on
);
840 #if defined(SO_OOBINLINE)
841 (void) setsockopt(net
, SOL_SOCKET
, SO_OOBINLINE
,
842 (char *)&on
, sizeof on
);
843 #endif /* defined(SO_OOBINLINE) */
846 (void) signal(SIGTSTP
, SIG_IGN
);
850 * Ignoring SIGTTOU keeps the kernel from blocking us
851 * in ttioct() in /sys/tty.c.
853 (void) signal(SIGTTOU
, SIG_IGN
);
856 (void) signal(SIGCHLD
, cleanup
);
861 t
= open(_PATH_TTY
, O_RDWR
);
863 (void) ioctl(t
, TIOCNOTTY
, NULL
);
870 * Show banner that getty never gave.
872 * We put the banner in the pty input buffer. This way, it
873 * gets carriage return null processing, etc., just like all
874 * other pty --> client data.
877 if (getent(defent
, "default") == 1) {
880 HE
= Getstr("he", &cp
);
881 HN
= Getstr("hn", &cp
);
882 IM
= Getstr("im", &cp
);
884 (void) strlcpy(host_name
, HN
, sizeof(host_name
));
888 IM
= strdup(DEFAULT_IM
);
891 edithost(HE
, host_name
);
896 (void) strncat(ptyibuf2
, ptyip
, pcc
+1);
901 * Last check to make sure all our states are correct.
905 #endif /* LINEMODE */
907 DIAG(TD_REPORT
, output_data("td: Entering processing loop\r\n"));
910 * Startup the login process on the slave side of the terminal
911 * now. We delay this until here to insure option negotiation
914 startslave(host
, level
, user_name
);
916 nfd
= ((f
> p
) ? f
: p
) + 1;
918 fd_set ibits
, obits
, xbits
;
921 if (ncc
< 0 && pcc
< 0)
928 * Never look for input if there's still
929 * stuff in the corresponding output buffer
931 if (nfrontp
- nbackp
|| pcc
> 0) {
936 if (pfrontp
- pbackp
|| ncc
> 0) {
944 if ((c
= select(nfd
, &ibits
, &obits
, &xbits
, NULL
)) < 1) {
946 if (errno
== EINTR
) {
957 if (FD_ISSET(net
, &xbits
)) {
962 * Something to read from the network...
964 if (FD_ISSET(net
, &ibits
)) {
965 #if !defined(SO_OOBINLINE)
967 * In 4.2 (and 4.3 beta) systems, the
968 * OOB indication and data handling in the kernel
969 * is such that if two separate TCP Urgent requests
970 * come in, one byte of TCP data will be overlaid.
971 * This is fatal for Telnet, but we try to live
974 * In addition, in 4.2 (and...), a special protocol
975 * is needed to pick up the TCP Urgent data in
976 * the correct sequence.
978 * What we do is: if we think we are in urgent
979 * mode, we look to see if we are "at the mark".
980 * If we are, we do an OOB receive. If we run
981 * this twice, we will do the OOB receive twice,
982 * but the second will fail, since the second
983 * time we were "at the mark", but there wasn't
984 * any data there (the kernel doesn't reset
985 * "at the mark" until we do a normal read).
986 * Once we've read the OOB data, we go ahead
987 * and do normal reads.
989 * There is also another problem, which is that
990 * since the OOB byte we read doesn't put us
991 * out of OOB state, and since that byte is most
992 * likely the TELNET DM (data mark), we would
993 * stay in the TELNET SYNCH (SYNCHing) state.
994 * So, clocks to the rescue. If we've "just"
995 * received a DM, then we test for the
996 * presence of OOB data when the receive OOB
997 * fails (and AFTER we did the normal mode read
998 * to clear "at the mark").
1003 (void) ioctl(net
, SIOCATMARK
, (char *)&atmark
);
1005 ncc
= recv(net
, netibuf
, sizeof (netibuf
), MSG_OOB
);
1006 if ((ncc
== -1) && (errno
== EINVAL
)) {
1007 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1008 if (sequenceIs(didnetreceive
, gotDM
)) {
1009 SYNCHing
= stilloob(net
);
1013 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1016 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1018 settimer(didnetreceive
);
1019 #else /* !defined(SO_OOBINLINE)) */
1020 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1021 #endif /* !defined(SO_OOBINLINE)) */
1022 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1030 DIAG((TD_REPORT
| TD_NETDATA
),
1031 output_data("td: netread %d chars\r\n", ncc
));
1032 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1036 * Something to read from the pty...
1038 if (FD_ISSET(p
, &ibits
)) {
1039 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1041 * On some systems, if we try to read something
1042 * off the master side before the slave side is
1043 * opened, we get EIO.
1045 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1056 * If ioctl from pty, pass it through net
1058 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1059 copy_termbuf(ptyibuf
+1, pcc
-1);
1063 #endif /* LINEMODE */
1064 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1065 netclear(); /* clear buffer back */
1068 * There are client telnets on some
1069 * operating systems get screwed up
1070 * royally if we send them urgent
1073 output_data("%c%c", IAC
, DM
);
1074 neturg
= nfrontp
-1; /* off by one XXX */
1076 printoption("td: send IAC", DM
));
1080 if (his_state_is_will(TELOPT_LFLOW
) &&
1082 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1084 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1085 if (newflow
!= flowmode
) {
1087 output_data("%c%c%c%c%c%c",
1088 IAC
, SB
, TELOPT_LFLOW
,
1092 DIAG(TD_OPTIONS
, printsub('>',
1093 (unsigned char *)nfrontp
-4,
1103 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1105 c
= *ptyip
++ & 0377, pcc
--;
1107 output_data("%c", c
);
1108 output_data("%c", c
);
1109 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1110 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1111 output_data("%c", *ptyip
++ & 0377);
1114 output_data("%c", '\0');
1118 if (FD_ISSET(f
, &obits
) && (nfrontp
- nbackp
) > 0)
1122 if (FD_ISSET(p
, &obits
) && (pfrontp
- pbackp
) > 0)
1126 } /* end of telnet */
1130 # define TCSIG TIOCSIG
1135 * Send interrupt to process on other side of pty.
1136 * If it is in raw mode, just write NULL;
1137 * otherwise, write intr char.
1142 ptyflush(); /* half-hearted */
1145 (void) ioctl(pty
, TCSIG
, (char *)SIGINT
);
1148 *pfrontp
++ = slctab
[SLC_IP
].sptr
?
1149 (unsigned char)*slctab
[SLC_IP
].sptr
: '\177';
1154 * Send quit to process on other side of pty.
1155 * If it is in raw mode, just write NULL;
1156 * otherwise, write quit char.
1161 ptyflush(); /* half-hearted */
1163 (void) ioctl(pty
, TCSIG
, (char *)SIGQUIT
);
1166 *pfrontp
++ = slctab
[SLC_ABORT
].sptr
?
1167 (unsigned char)*slctab
[SLC_ABORT
].sptr
: '\034';
1175 ptyflush(); /* half-hearted */
1177 (void) ioctl(pty
, TCSIG
, (char *)SIGTSTP
);
1179 *pfrontp
++ = slctab
[SLC_SUSP
].sptr
?
1180 (unsigned char)*slctab
[SLC_SUSP
].sptr
: '\032';
1182 #endif /* SIGTSTP */
1186 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1187 * just send back "[Yes]".
1192 #if defined(SIGINFO) && defined(TCSIG)
1193 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1194 (void) ioctl(pty
, TCSIG
, (char *)SIGINFO
);
1198 output_data("\r\n[Yes]\r\n");
1206 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1207 if (!tty_isediting()) {
1208 extern char oldeofc
;
1209 *pfrontp
++ = oldeofc
;
1213 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1214 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';