]>
git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/telnetd.c
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)telnetd.c 8.2 (Berkeley) 12/15/93";
44 static const char rcsid
[] =
45 "$FreeBSD: src/libexec/telnetd/telnetd.c,v 1.28 2001/07/20 15:14:03 ru Exp $";
49 #include "pathnames.h"
51 #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY)
53 * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can
54 * use it to tell us to turn off all the socket security code,
55 * since that is only used in UNICOS 7.0 and later.
57 # undef _SC_CRAY_SECURE_SYS
61 #include <arpa/inet.h>
66 #include <sys/types.h>
68 #if defined(_SC_CRAY_SECURE_SYS)
70 #include <sys/secdev.h>
71 # ifdef SO_SEC_MULTI /* 8.0 code */
72 #include <sys/secparm.h>
74 # endif /* SO_SEC_MULTI */
76 /* wrapper for KAME-special getnameinfo() */
77 #ifndef NI_WITHSCOPEID
78 #define NI_WITHSCOPEID 0
85 # ifdef SO_SEC_MULTI /* 8.0 code */
87 # else /* SO_SEC_MULTI */ /* 7.0 code */
88 struct socket_security ss
;
89 # endif /* SO_SEC_MULTI */
90 #endif /* _SC_CRAY_SECURE_SYS */
92 #if defined(AUTHENTICATION)
93 #include <libtelnet/auth.h>
97 int require_SecurID
= 0;
100 char remote_hostname
[MAXHOSTNAMELEN
];
101 int utmp_len
= sizeof(remote_hostname
) - 1;
102 int registerd_host_only
= 0;
105 # include <stropts.h>
107 /* make sure we don't get the bsd version */
108 # include "/usr/include/sys/tty.h"
109 # include <sys/ptyvar.h>
112 * Because of the way ptyibuf is used with streams messages, we need
113 * ptyibuf+1 to be on a full-word boundary. The following weirdness
114 * is simply to make that happen.
116 long ptyibufbuf
[BUFSIZ
/sizeof(long)+1];
117 char *ptyibuf
= ((char *)&ptyibufbuf
[1])-1;
118 char *ptyip
= ((char *)&ptyibufbuf
[1])-1;
119 char ptyibuf2
[BUFSIZ
];
120 unsigned char ctlbuf
[BUFSIZ
];
121 struct strbuf strbufc
, strbufd
;
125 #else /* ! STREAMPTY */
129 * pointers, and counters.
131 char ptyibuf
[BUFSIZ
], *ptyip
= ptyibuf
;
132 char ptyibuf2
[BUFSIZ
];
134 #endif /* ! STREAMPTY */
136 int hostinfo
= 1; /* do we print login banner? */
139 extern int newmap
; /* nonzero if \n maps to ^M^J */
140 int lowpty
= 0, highpty
; /* low, high pty numbers */
147 void doit
__P((struct sockaddr
*));
148 int terminaltypeok
__P((char *));
149 void startslave
__P((char *, int, char *));
150 extern void usage
P((void));
153 * The string to pass to getopt(). We do it this way so
154 * that only the actual options that we support will be
155 * passed off to getopt().
157 char valid_opts
[] = {
158 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U',
160 #ifdef AUTHENTICATION
169 #if defined(CRAY) && defined(NEWINIT)
184 int family
= AF_INET
;
190 struct sockaddr_storage from
;
193 #if defined(IPPROTO_IP) && defined(IP_TOS)
197 pfrontp
= pbackp
= ptyobuf
;
199 nfrontp
= nbackp
= netobuf
;
202 * This initialization causes linemode to default to a configuration
203 * that works on all telnet clients, including the FreeBSD client.
204 * This is not quite the same as the telnet client issuing a "mode
205 * character" command, but has most of the same benefits, and is
206 * preferable since some clients (like usofts) don't have the
207 * mode character command anyway and linemode breaks things.
208 * The most notable symptom of fix is that csh "set filec" operations
209 * like <ESC> (filename completion) and ^D (choices) keys now work
210 * in telnet sessions and can be used more than once on the same line.
211 * CR/LF handling is also corrected in some termio modes. This
212 * change resolves problem reports bin/771 and bin/1037.
215 linemode
=1; /*Default to mode that works on bulk of clients*/
219 * Get number of pty's before trying to process options,
220 * which may include changing pty range.
225 while ((ch
= getopt(argc
, argv
, valid_opts
)) != -1) {
228 #ifdef AUTHENTICATION
231 * Check for required authentication level
233 if (strcmp(optarg
, "debug") == 0) {
234 extern int auth_debug_mode
;
236 } else if (strcasecmp(optarg
, "none") == 0) {
238 } else if (strcasecmp(optarg
, "other") == 0) {
239 auth_level
= AUTH_OTHER
;
240 } else if (strcasecmp(optarg
, "user") == 0) {
241 auth_level
= AUTH_USER
;
242 } else if (strcasecmp(optarg
, "valid") == 0) {
243 auth_level
= AUTH_VALID
;
244 } else if (strcasecmp(optarg
, "off") == 0) {
246 * This hack turns off authentication
250 warnx("unknown authorization level for -a");
253 #endif /* AUTHENTICATION */
259 #endif /* BFTPDAEMON */
262 if (strcmp(optarg
, "ebug") == 0) {
273 * Check for desired diagnostics capabilities.
275 if (!strcmp(optarg
, "report")) {
276 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
277 } else if (!strcmp(optarg
, "exercise")) {
278 diagnostic
|= TD_EXERCISE
;
279 } else if (!strcmp(optarg
, "netdata")) {
280 diagnostic
|= TD_NETDATA
;
281 } else if (!strcmp(optarg
, "ptydata")) {
282 diagnostic
|= TD_PTYDATA
;
283 } else if (!strcmp(optarg
, "options")) {
284 diagnostic
|= TD_OPTIONS
;
290 #endif /* DIAGNOSTICS */
297 #if defined(CRAY) && defined(NEWINIT)
304 #endif /* defined(CRAY) && defined(NEWINIT) */
310 #endif /* LINEMODE */
313 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
314 lmodetype
= NO_AUTOKLUDGE
;
316 /* ignore -k option if built without kludge linemode */
317 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
335 * Allow the specification of alterations
336 * to the pty search range. It is legal to
337 * specify only one, and not change the
338 * other from its default.
340 c
= strchr(optarg
, '-');
346 lowpty
= atoi(optarg
);
347 if ((lowpty
> highpty
) || (lowpty
< 0) ||
358 /* SecurID required */
364 if ((tos
= parsetos(optarg
, "tcp")) < 0)
366 "bad TOS argument '", optarg
,
367 "'; will try to use default TOS");
369 warnx("TOS option unavailable; -S flag not supported");
374 utmp_len
= atoi(optarg
);
376 utmp_len
= -utmp_len
;
377 if (utmp_len
>= sizeof(remote_hostname
))
378 utmp_len
= sizeof(remote_hostname
) - 1;
382 registerd_host_only
= 1;
385 #ifdef AUTHENTICATION
388 * Check for invalid authentication types
390 auth_disable_name(optarg
);
392 #endif /* AUTHENTICATION */
405 warnx("%c: unknown option", ch
);
417 int s
, ns
, foo
, error
;
418 char *service
= "telnet";
419 struct addrinfo hints
, *res
;
424 } else if (argc
== 1)
427 memset(&hints
, 0, sizeof(hints
));
428 hints
.ai_flags
= AI_PASSIVE
;
429 hints
.ai_family
= family
;
430 hints
.ai_socktype
= SOCK_STREAM
;
431 hints
.ai_protocol
= 0;
432 error
= getaddrinfo(NULL
, service
, &hints
, &res
);
435 errx(1, "tcp/%s: %s\n", service
, gai_strerror(error
));
436 if (error
== EAI_SYSTEM
)
437 errx(1, "tcp/%s: %s\n", service
, strerror(errno
));
441 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
444 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
445 (char *)&on
, sizeof(on
));
446 if (bind(s
, res
->ai_addr
, res
->ai_addrlen
) < 0)
448 if (listen(s
, 1) < 0)
450 foo
= res
->ai_addrlen
;
451 ns
= accept(s
, res
->ai_addr
, &foo
);
458 } else if (argc
== 1) {
459 ; /* VOID*/ /* Just ignore the host/port name */
461 } else if (argc
> 0) {
466 #if defined(_SC_CRAY_SECURE_SYS)
467 secflag
= sysconf(_SC_CRAY_SECURE_SYS
);
470 * Get socket's security label
473 int szss
= sizeof(ss
);
474 #ifdef SO_SEC_MULTI /* 8.0 code */
476 int szi
= sizeof(int);
477 #endif /* SO_SEC_MULTI */
479 bzero((char *)&dv
, sizeof(dv
));
481 if (getsysv(&sysv
, sizeof(struct sysv
)) != 0)
485 * Get socket security label and set device values
486 * {security label to be set on ttyp device}
488 #ifdef SO_SEC_MULTI /* 8.0 code */
489 if ((getsockopt(0, SOL_SOCKET
, SO_SECURITY
,
490 (char *)&ss
, &szss
) < 0) ||
491 (getsockopt(0, SOL_SOCKET
, SO_SEC_MULTI
,
492 (char *)&sock_multi
, &szi
) < 0)) {
493 err(1, "getsockopt");
495 dv
.dv_actlvl
= ss
.ss_actlabel
.lt_level
;
496 dv
.dv_actcmp
= ss
.ss_actlabel
.lt_compart
;
498 dv
.dv_minlvl
= dv
.dv_maxlvl
= dv
.dv_actlvl
;
499 dv
.dv_valcmp
= dv
.dv_actcmp
;
501 dv
.dv_minlvl
= ss
.ss_minlabel
.lt_level
;
502 dv
.dv_maxlvl
= ss
.ss_maxlabel
.lt_level
;
503 dv
.dv_valcmp
= ss
.ss_maxlabel
.lt_compart
;
507 #else /* SO_SEC_MULTI */ /* 7.0 code */
508 if (getsockopt(0, SOL_SOCKET
, SO_SECURITY
,
509 (char *)&ss
, &szss
) >= 0) {
510 dv
.dv_actlvl
= ss
.ss_slevel
;
511 dv
.dv_actcmp
= ss
.ss_compart
;
512 dv
.dv_minlvl
= ss
.ss_minlvl
;
513 dv
.dv_maxlvl
= ss
.ss_maxlvl
;
514 dv
.dv_valcmp
= ss
.ss_maxcmp
;
516 #endif /* SO_SEC_MULTI */
518 #endif /* _SC_CRAY_SECURE_SYS */
520 openlog("telnetd", LOG_PID
| LOG_ODELAY
, LOG_DAEMON
);
521 fromlen
= sizeof (from
);
522 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
527 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
528 (char *)&on
, sizeof (on
)) < 0) {
529 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
532 #if defined(IPPROTO_IP) && defined(IP_TOS)
533 if (from
.ss_family
== AF_INET
) {
534 # if defined(HAS_GETTOS)
536 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
540 tos
= 020; /* Low Delay bit */
542 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
543 (char *)&tos
, sizeof(tos
)) < 0)
544 && (errno
!= ENOPROTOOPT
) )
545 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
547 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
549 doit((struct sockaddr
*)&from
);
557 fprintf(stderr
, "usage: telnetd");
558 #ifdef AUTHENTICATION
559 fprintf(stderr
, " [-a (debug|other|user|valid|off|none)]\n\t");
562 fprintf(stderr
, " [-B]");
564 fprintf(stderr
, " [-debug]");
566 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
568 #ifdef AUTHENTICATION
569 fprintf(stderr
, " [-edebug]");
571 fprintf(stderr
, " [-h]");
572 #if defined(CRAY) && defined(NEWINIT)
573 fprintf(stderr
, " [-Iinitid]");
575 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
576 fprintf(stderr
, " [-k]");
579 fprintf(stderr
, " [-l]");
581 fprintf(stderr
, " [-n]");
583 fprintf(stderr
, " [-r[lowpty]-[highpty]]");
585 fprintf(stderr
, "\n\t");
587 fprintf(stderr
, " [-s]");
590 fprintf(stderr
, " [-S tos]");
592 #ifdef AUTHENTICATION
593 fprintf(stderr
, " [-X auth-type]");
595 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
596 fprintf(stderr
, " [port]\n");
603 * Ask the other end to send along its terminal type and speed.
604 * Output is the variable terminaltype filled in.
606 static unsigned char ttytype_sbbuf
[] = {
607 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
611 getterminaltype(name
)
618 #if defined(AUTHENTICATION)
620 * Handle the Authentication option before we do anything else.
622 send_do(TELOPT_AUTHENTICATION
, 1);
623 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION
))
625 if (his_state_is_will(TELOPT_AUTHENTICATION
)) {
626 retval
= auth_wait(name
);
630 send_do(TELOPT_TTYPE
, 1);
631 send_do(TELOPT_TSPEED
, 1);
632 send_do(TELOPT_XDISPLOC
, 1);
633 send_do(TELOPT_NEW_ENVIRON
, 1);
634 send_do(TELOPT_OLD_ENVIRON
, 1);
636 his_will_wont_is_changing(TELOPT_TTYPE
) ||
637 his_will_wont_is_changing(TELOPT_TSPEED
) ||
638 his_will_wont_is_changing(TELOPT_XDISPLOC
) ||
639 his_will_wont_is_changing(TELOPT_NEW_ENVIRON
) ||
640 his_will_wont_is_changing(TELOPT_OLD_ENVIRON
)) {
643 if (his_state_is_will(TELOPT_TSPEED
)) {
644 static unsigned char sb
[] =
645 { IAC
, SB
, TELOPT_TSPEED
, TELQUAL_SEND
, IAC
, SE
};
647 output_datalen(sb
, sizeof sb
);
649 if (his_state_is_will(TELOPT_XDISPLOC
)) {
650 static unsigned char sb
[] =
651 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
653 output_datalen(sb
, sizeof sb
);
655 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
656 static unsigned char sb
[] =
657 { IAC
, SB
, TELOPT_NEW_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
659 output_datalen(sb
, sizeof sb
);
661 else if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
662 static unsigned char sb
[] =
663 { IAC
, SB
, TELOPT_OLD_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
665 output_datalen(sb
, sizeof sb
);
667 if (his_state_is_will(TELOPT_TTYPE
)) {
669 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
671 if (his_state_is_will(TELOPT_TSPEED
)) {
672 while (sequenceIs(tspeedsubopt
, baseline
))
675 if (his_state_is_will(TELOPT_XDISPLOC
)) {
676 while (sequenceIs(xdisplocsubopt
, baseline
))
679 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
680 while (sequenceIs(environsubopt
, baseline
))
683 if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
684 while (sequenceIs(oenvironsubopt
, baseline
))
687 if (his_state_is_will(TELOPT_TTYPE
)) {
688 char first
[256], last
[256];
690 while (sequenceIs(ttypesubopt
, baseline
))
694 * If the other side has already disabled the option, then
695 * we have to just go with what we (might) have already gotten.
697 if (his_state_is_will(TELOPT_TTYPE
) && !terminaltypeok(terminaltype
)) {
698 (void) strncpy(first
, terminaltype
, sizeof(first
)-1);
699 first
[sizeof(first
)-1] = '\0';
702 * Save the unknown name, and request the next name.
704 (void) strncpy(last
, terminaltype
, sizeof(last
)-1);
705 last
[sizeof(last
)-1] = '\0';
707 if (terminaltypeok(terminaltype
))
709 if ((strncmp(last
, terminaltype
, sizeof(last
)) == 0) ||
710 his_state_is_wont(TELOPT_TTYPE
)) {
712 * We've hit the end. If this is the same as
713 * the first name, just go with it.
715 if (strncmp(first
, terminaltype
, sizeof(first
)) == 0)
718 * Get the terminal name one more time, so that
719 * RFC1091 compliant telnets will cycle back to
720 * the start of the list.
723 if (strncmp(first
, terminaltype
, sizeof(first
)) != 0) {
724 (void) strncpy(terminaltype
, first
, sizeof(terminaltype
)-1);
725 terminaltype
[sizeof(terminaltype
)-1] = '\0';
733 } /* end of getterminaltype */
739 * If the client turned off the option,
740 * we can't send another request, so we
743 if (his_state_is_wont(TELOPT_TTYPE
))
746 output_datalen(ttytype_sbbuf
, sizeof ttytype_sbbuf
);
747 while (sequenceIs(ttypesubopt
, baseline
))
757 if (terminaltype
== NULL
)
761 * tgetent() will return 1 if the type is known, and
762 * 0 if it is not known. If it returns -1, it couldn't
763 * open the database. But if we can't open the database,
764 * it won't help to say we failed, because we won't be
765 * able to verify anything else. So, we treat -1 like 1.
767 if (tgetent(buf
, s
) == 0)
772 #ifndef MAXHOSTNAMELEN
773 #define MAXHOSTNAMELEN 256
774 #endif /* MAXHOSTNAMELEN */
777 char host_name
[MAXHOSTNAMELEN
];
779 extern void telnet
P((int, int, char *));
784 * Get a pty, scan input lines.
788 struct sockaddr
*who
;
796 * Find an available pty to use.
799 pty
= getpty(&ptynum
);
801 fatal(net
, "All network ports in use");
806 if ((lp
= getpty()) == NULL
)
807 fatal(net
, "Out of ptys");
809 if ((pty
= open(lp
, 2)) >= 0) {
810 strlcpy(line
,lp
,sizeof(line
));
817 #if defined(_SC_CRAY_SECURE_SYS)
819 * set ttyp line security label
824 sprintf(tty_dev
, "%spty/%03d", _PATH_DEV
, ptynum
);
825 if (setdevs(tty_dev
, &dv
) < 0)
826 fatal(net
, "cannot set pty security");
827 sprintf(slave_dev
, "%sp%03d", _PATH_TTY
, ptynum
);
828 if (setdevs(slave_dev
, &dv
) < 0)
829 fatal(net
, "cannot set tty security");
831 #endif /* _SC_CRAY_SECURE_SYS */
833 /* get name of connected client */
834 hp
= gethostbyaddr((char *)&((struct sockaddr_in
*)who
)->sin_addr
, sizeof (struct in_addr
),
835 ((struct sockaddr_in
*)who
)->sin_family
);
837 if (hp
== NULL
&& registerd_host_only
) {
838 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
839 Please contact your net administrator");
841 (strlen(hp
->h_name
) <= (unsigned int)((utmp_len
< 0) ? -utmp_len
845 host
= inet_ntoa(((struct sockaddr_in
*)who
)->sin_addr
);
848 * We must make a copy because Kerberos is probably going
849 * to also do a gethost* and overwrite the static data...
851 strncpy(remote_hostname
, host
, sizeof(remote_hostname
)-1);
852 remote_hostname
[sizeof(remote_hostname
)-1] = 0;
853 host
= remote_hostname
;
855 (void) gethostname(host_name
, sizeof(host_name
) - 1);
856 host_name
[sizeof(host_name
) - 1] = '\0';
857 hostname
= host_name
;
859 #if defined(AUTHENTICATION)
860 auth_encrypt_init(hostname
, remote_hostname
, "TELNETD", 1);
868 level
= getterminaltype(user_name
);
869 setenv("TERM", terminaltype
? terminaltype
: "network", 1);
871 #if defined(_SC_CRAY_SECURE_SYS)
873 if (setulvl(dv
.dv_actlvl
) < 0)
874 fatal(net
,"cannot setulvl()");
875 if (setucmp(dv
.dv_actcmp
) < 0)
876 fatal(net
, "cannot setucmp()");
878 #endif /* _SC_CRAY_SECURE_SYS */
880 telnet(net
, pty
, remote_hostname
); /* begin server process */
885 #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
887 Xterm_output(ibufp
, obuf
, icountp
, ocount
)
889 int *icountp
, ocount
;
892 ret
= term_output(*ibufp
, obuf
, *icountp
, ocount
);
897 #define term_output Xterm_output
898 #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
901 * Main loop. Select from pty and network, and
902 * hand data to telnet receiver finite state machine.
910 #define TABBUFSIZ 512
911 char defent
[TABBUFSIZ
];
912 char defstrs
[TABBUFSIZ
];
925 * Initialize the slc mapping table.
930 * Do some tests where it is desireable to wait for a response.
931 * Rather than doing them slowly, one at a time, do them all
934 if (my_state_is_wont(TELOPT_SGA
))
935 send_will(TELOPT_SGA
, 1);
937 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
938 * because 4.2 clients are unable to deal with TCP urgent data.
940 * To find out, we send out a "DO ECHO". If the remote system
941 * answers "WILL ECHO" it is probably a 4.2 client, and we note
942 * that fact ("WILL ECHO" ==> that the client will echo what
943 * WE, the server, sends it; it does NOT mean that the client will
944 * echo the terminal input).
946 send_do(TELOPT_ECHO
, 1);
949 if (his_state_is_wont(TELOPT_LINEMODE
)) {
950 /* Query the peer for linemode support by trying to negotiate
951 * the linemode option.
955 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
957 #endif /* LINEMODE */
960 * Send along a couple of other options that we wish to negotiate.
962 send_do(TELOPT_NAWS
, 1);
963 send_will(TELOPT_STATUS
, 1);
964 flowmode
= 1; /* default flow control state */
965 restartany
= -1; /* uninitialized... */
966 send_do(TELOPT_LFLOW
, 1);
969 * Spin, waiting for a response from the DO ECHO. However,
970 * some REALLY DUMB telnets out there might not respond
971 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
972 * telnets so far seem to respond with WONT for a DO that
973 * they don't understand...) because by the time we get the
974 * response, it will already have processed the DO ECHO.
975 * Kludge upon kludge.
977 while (his_will_wont_is_changing(TELOPT_NAWS
))
982 * The client might have sent a WILL NAWS as part of its
983 * startup code; if so, we'll be here before we get the
984 * response to the DO ECHO. We'll make the assumption
985 * that any implementation that understands about NAWS
986 * is a modern enough implementation that it will respond
987 * to our DO ECHO request; hence we'll do another spin
988 * waiting for the ECHO option to settle down, which is
989 * what we wanted to do in the first place...
991 if (his_want_state_is_will(TELOPT_ECHO
) &&
992 his_state_is_will(TELOPT_NAWS
)) {
993 while (his_will_wont_is_changing(TELOPT_ECHO
))
997 * On the off chance that the telnet client is broken and does not
998 * respond to the DO ECHO we sent, (after all, we did send the
999 * DO NAWS negotiation after the DO ECHO, and we won't get here
1000 * until a response to the DO NAWS comes back) simulate the
1001 * receipt of a will echo. This will also send a WONT ECHO
1002 * to the client, since we assume that the client failed to
1003 * respond because it believes that it is already in DO ECHO
1004 * mode, which we do not want.
1006 if (his_want_state_is_will(TELOPT_ECHO
)) {
1007 DIAG(TD_OPTIONS
, output_data("td: simulating recv\r\n"));
1008 willoption(TELOPT_ECHO
);
1012 * Finally, to clean things up, we turn on our echo. This
1013 * will break stupid 4.2 telnets out of local terminal echo.
1016 if (my_state_is_wont(TELOPT_ECHO
))
1017 send_will(TELOPT_ECHO
, 1);
1021 * Turn on packet mode
1023 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
1026 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1028 * Continuing line mode support. If client does not support
1029 * real linemode, attempt to negotiate kludge linemode by sending
1030 * the do timing mark sequence.
1032 if (lmodetype
< REAL_LINEMODE
)
1033 send_do(TELOPT_TM
, 1);
1034 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1037 * Call telrcv() once to pick up anything received during
1038 * terminal type negotiation, 4.2/4.3 determination, and
1039 * linemode negotiation.
1043 (void) ioctl(f
, FIONBIO
, (char *)&on
);
1044 (void) ioctl(p
, FIONBIO
, (char *)&on
);
1045 #if defined(CRAY2) && defined(UNICOS5)
1046 init_termdriver(f
, p
, interrupt
, sendbrk
);
1049 #if defined(SO_OOBINLINE)
1050 (void) setsockopt(net
, SOL_SOCKET
, SO_OOBINLINE
,
1051 (char *)&on
, sizeof on
);
1052 #endif /* defined(SO_OOBINLINE) */
1055 (void) signal(SIGTSTP
, SIG_IGN
);
1059 * Ignoring SIGTTOU keeps the kernel from blocking us
1060 * in ttioct() in /sys/tty.c.
1062 (void) signal(SIGTTOU
, SIG_IGN
);
1065 (void) signal(SIGCHLD
, cleanup
);
1067 #if defined(CRAY2) && defined(UNICOS5)
1069 * Cray-2 will send a signal when pty modes are changed by slave
1070 * side. Set up signal handler now.
1072 if ((int)signal(SIGUSR1
, termstat
) < 0)
1074 else if (ioctl(p
, TCSIGME
, (char *)SIGUSR1
) < 0)
1075 warn("ioctl:TCSIGME");
1077 * Make processing loop check terminal characteristics early on.
1085 t
= open(_PATH_TTY
, O_RDWR
);
1087 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
1093 #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
1095 ioctl(p
, TIOCSCTTY
, 0);
1099 * Show banner that getty never gave.
1101 * We put the banner in the pty input buffer. This way, it
1102 * gets carriage return null processing, etc., just like all
1103 * other pty --> client data.
1106 #if !defined(CRAY) || !defined(NEWINIT)
1111 if (getent(defent
, "default") == 1) {
1115 HE
= Getstr("he", &cp
);
1116 HN
= Getstr("hn", &cp
);
1117 IM
= Getstr("im", &cp
);
1118 IF
= Getstr("if", &cp
);
1120 (void) strlcpy(host_name
, HN
, sizeof(host_name
));
1121 if (IF
&& (if_fd
= open(IF
, O_RDONLY
, 000)) != -1)
1129 edithost(HE
, host_name
);
1130 if (hostinfo
&& *IM
)
1132 else if (IF
&& if_fd
!= -1) {
1133 fstat (if_fd
, &statbuf
);
1134 if_buf
= (char *) mmap (0, statbuf
.st_size
, PROT_READ
,
1136 putf(if_buf
, ptyibuf2
);
1137 munmap (if_buf
, statbuf
.st_size
);
1142 (void) strncat(ptyibuf2
, ptyip
, pcc
+1);
1144 pcc
= strlen(ptyip
);
1147 * Last check to make sure all our states are correct.
1151 #endif /* LINEMODE */
1153 DIAG(TD_REPORT
, output_data("td: Entering processing loop\r\n"));
1156 * Startup the login process on the slave side of the terminal
1157 * now. We delay this until here to insure option negotiation
1160 startslave(host
, level
, user_name
);
1163 fd_set ibits
, obits
, xbits
;
1166 if (ncc
< 0 && pcc
< 0)
1169 #if defined(CRAY2) && defined(UNICOS5)
1172 #endif /* defined(CRAY2) && defined(UNICOS5) */
1177 * Never look for input if there's still
1178 * stuff in the corresponding output buffer
1180 if (nfrontp
- nbackp
|| pcc
> 0) {
1185 if (pfrontp
- pbackp
|| ncc
> 0) {
1193 if ((c
= select(16, &ibits
, &obits
, &xbits
,
1194 (struct timeval
*)0)) < 1) {
1196 if (errno
== EINTR
) {
1207 if (FD_ISSET(net
, &xbits
)) {
1212 * Something to read from the network...
1214 if (FD_ISSET(net
, &ibits
)) {
1215 #if !defined(SO_OOBINLINE)
1217 * In 4.2 (and 4.3 beta) systems, the
1218 * OOB indication and data handling in the kernel
1219 * is such that if two separate TCP Urgent requests
1220 * come in, one byte of TCP data will be overlaid.
1221 * This is fatal for Telnet, but we try to live
1224 * In addition, in 4.2 (and...), a special protocol
1225 * is needed to pick up the TCP Urgent data in
1226 * the correct sequence.
1228 * What we do is: if we think we are in urgent
1229 * mode, we look to see if we are "at the mark".
1230 * If we are, we do an OOB receive. If we run
1231 * this twice, we will do the OOB receive twice,
1232 * but the second will fail, since the second
1233 * time we were "at the mark", but there wasn't
1234 * any data there (the kernel doesn't reset
1235 * "at the mark" until we do a normal read).
1236 * Once we've read the OOB data, we go ahead
1237 * and do normal reads.
1239 * There is also another problem, which is that
1240 * since the OOB byte we read doesn't put us
1241 * out of OOB state, and since that byte is most
1242 * likely the TELNET DM (data mark), we would
1243 * stay in the TELNET SYNCH (SYNCHing) state.
1244 * So, clocks to the rescue. If we've "just"
1245 * received a DM, then we test for the
1246 * presence of OOB data when the receive OOB
1247 * fails (and AFTER we did the normal mode read
1248 * to clear "at the mark").
1253 (void) ioctl(net
, SIOCATMARK
, (char *)&atmark
);
1255 ncc
= recv(net
, netibuf
, sizeof (netibuf
), MSG_OOB
);
1256 if ((ncc
== -1) && (errno
== EINVAL
)) {
1257 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1258 if (sequenceIs(didnetreceive
, gotDM
)) {
1259 SYNCHing
= stilloob(net
);
1263 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1266 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1268 settimer(didnetreceive
);
1269 #else /* !defined(SO_OOBINLINE)) */
1270 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1271 #endif /* !defined(SO_OOBINLINE)) */
1272 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1280 DIAG((TD_REPORT
| TD_NETDATA
),
1281 output_data("td: netread %d chars\r\n", ncc
));
1282 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1286 * Something to read from the pty...
1288 if (FD_ISSET(p
, &ibits
)) {
1290 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1292 pcc
= readstream(p
, ptyibuf
, BUFSIZ
);
1295 * On some systems, if we try to read something
1296 * off the master side before the slave side is
1297 * opened, we get EIO.
1299 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1308 #if !defined(CRAY2) || !defined(UNICOS5)
1311 * If ioctl from pty, pass it through net
1313 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1314 copy_termbuf(ptyibuf
+1, pcc
-1);
1318 #endif /* LINEMODE */
1319 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1320 netclear(); /* clear buffer back */
1323 * There are client telnets on some
1324 * operating systems get screwed up
1325 * royally if we send them urgent
1328 output_data("%c%c", IAC
, DM
);
1329 neturg
= nfrontp
-1; /* off by one XXX */
1332 if (his_state_is_will(TELOPT_LFLOW
) &&
1334 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1336 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1337 if (newflow
!= flowmode
) {
1339 output_data("%c%c%c%c%c%c",
1340 IAC
, SB
, TELOPT_LFLOW
,
1348 #else /* defined(CRAY2) && defined(UNICOS5) */
1352 pcc
= term_output(&unptyip
, ptyibuf2
,
1357 #endif /* defined(CRAY2) && defined(UNICOS5) */
1362 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1364 c
= *ptyip
++ & 0377, pcc
--;
1366 output_data("%c", c
);
1367 #if defined(CRAY2) && defined(UNICOS5)
1368 else if (c
== '\n' &&
1369 my_state_is_wont(TELOPT_BINARY
) && newmap
)
1371 #endif /* defined(CRAY2) && defined(UNICOS5) */
1372 output_data("%c", c
);
1373 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1374 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1375 output_data("%c", *ptyip
++ & 0377);
1378 output_data("%c", '\0');
1381 #if defined(CRAY2) && defined(UNICOS5)
1383 * If chars were left over from the terminal driver,
1384 * note their existence.
1386 if (!uselinemode
&& unpcc
) {
1391 #endif /* defined(CRAY2) && defined(UNICOS5) */
1393 if (FD_ISSET(f
, &obits
) && (nfrontp
- nbackp
) > 0)
1397 if (FD_ISSET(p
, &obits
) && (pfrontp
- pbackp
) > 0)
1401 } /* end of telnet */
1405 # define TCSIG TIOCSIG
1411 int flowison
= -1; /* current state of flow: -1 is unknown */
1413 int readstream(p
, ibuf
, bufsize
)
1420 struct termios
*tsp
;
1427 strbufc
.maxlen
= BUFSIZ
;
1428 strbufc
.buf
= (char *)ctlbuf
;
1429 strbufd
.maxlen
= bufsize
-1;
1431 strbufd
.buf
= ibuf
+1;
1434 ret
= getmsg(p
, &strbufc
, &strbufd
, &flags
);
1435 if (ret
< 0) /* error of some sort -- probably EAGAIN */
1438 if (strbufc
.len
<= 0 || ctlbuf
[0] == M_DATA
) {
1440 if (strbufd
.len
> 0) { /* real data */
1441 return(strbufd
.len
+ 1); /* count header char */
1450 * It's a control message. Return 1, to look at the flag we set
1453 switch (ctlbuf
[0]) {
1455 if (ibuf
[1] & FLUSHW
)
1456 ibuf
[0] = TIOCPKT_FLUSHWRITE
;
1460 ip
= (struct iocblk
*) (ibuf
+1);
1462 switch (ip
->ioc_cmd
) {
1466 tsp
= (struct termios
*)
1467 (ibuf
+1 + sizeof(struct iocblk
));
1468 vstop
= tsp
->c_cc
[VSTOP
];
1469 vstart
= tsp
->c_cc
[VSTART
];
1470 ixon
= tsp
->c_iflag
& IXON
;
1475 tp
= (struct termio
*) (ibuf
+1 + sizeof(struct iocblk
));
1476 vstop
= tp
->c_cc
[VSTOP
];
1477 vstart
= tp
->c_cc
[VSTART
];
1478 ixon
= tp
->c_iflag
& IXON
;
1485 newflow
= (ixon
&& (vstart
== 021) && (vstop
== 023)) ? 1 : 0;
1486 if (newflow
!= flowison
) { /* it's a change */
1488 ibuf
[0] = newflow
? TIOCPKT_DOSTOP
: TIOCPKT_NOSTOP
;
1493 /* nothing worth doing anything about */
1497 #endif /* STREAMSPTY */
1500 * Send interrupt to process on other side of pty.
1501 * If it is in raw mode, just write NULL;
1502 * otherwise, write intr char.
1507 ptyflush(); /* half-hearted */
1510 (void) ioctl(pty
, TCSIG
, (char *)SIGINT
);
1513 *pfrontp
++ = slctab
[SLC_IP
].sptr
?
1514 (unsigned char)*slctab
[SLC_IP
].sptr
: '\177';
1519 * Send quit to process on other side of pty.
1520 * If it is in raw mode, just write NULL;
1521 * otherwise, write quit char.
1526 ptyflush(); /* half-hearted */
1528 (void) ioctl(pty
, TCSIG
, (char *)SIGQUIT
);
1531 *pfrontp
++ = slctab
[SLC_ABORT
].sptr
?
1532 (unsigned char)*slctab
[SLC_ABORT
].sptr
: '\034';
1540 ptyflush(); /* half-hearted */
1542 (void) ioctl(pty
, TCSIG
, (char *)SIGTSTP
);
1544 *pfrontp
++ = slctab
[SLC_SUSP
].sptr
?
1545 (unsigned char)*slctab
[SLC_SUSP
].sptr
: '\032';
1547 #endif /* SIGTSTP */
1551 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1552 * just send back "[Yes]".
1557 #if defined(SIGINFO) && defined(TCSIG)
1558 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1559 (void) ioctl(pty
, TCSIG
, (char *)SIGINFO
);
1563 output_data("\r\n[Yes]\r\n");
1571 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1572 if (!tty_isediting()) {
1573 extern char oldeofc
;
1574 *pfrontp
++ = oldeofc
;
1578 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1579 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';