]>
git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/telnetd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char copyright
[] =
59 "@(#) Copyright (c) 1989, 1993\n\
60 The Regents of the University of California. All rights reserved.\n";
64 static char sccsid
[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
68 #include "pathnames.h"
70 #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY)
72 * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can
73 * use it to tell us to turn off all the socket security code,
74 * since that is only used in UNICOS 7.0 and later.
76 # undef _SC_CRAY_SECURE_SYS
79 #if defined(_SC_CRAY_SECURE_SYS)
81 #include <sys/secdev.h>
82 # ifdef SO_SEC_MULTI /* 8.0 code */
83 #include <sys/secparm.h>
85 # endif /* SO_SEC_MULTI */
90 # ifdef SO_SEC_MULTI /* 8.0 code */
92 # else /* SO_SEC_MULTI */ /* 7.0 code */
93 struct socket_security ss
;
94 # endif /* SO_SEC_MULTI */
95 #endif /* _SC_CRAY_SECURE_SYS */
97 #if defined(AUTHENTICATION)
98 #include <libtelnet/auth.h>
102 int require_SecurID
= 0;
106 int registerd_host_only
= 0;
109 # include <stropts.h>
111 /* make sure we don't get the bsd version */
112 # include "/usr/include/sys/tty.h"
113 # include <sys/ptyvar.h>
116 * Because of the way ptyibuf is used with streams messages, we need
117 * ptyibuf+1 to be on a full-word boundary. The following wierdness
118 * is simply to make that happen.
120 long ptyibufbuf
[BUFSIZ
/sizeof(long)+1];
121 char *ptyibuf
= ((char *)&ptyibufbuf
[1])-1;
122 char *ptyip
= ((char *)&ptyibufbuf
[1])-1;
123 char ptyibuf2
[BUFSIZ
];
124 unsigned char ctlbuf
[BUFSIZ
];
125 struct strbuf strbufc
, strbufd
;
129 #else /* ! STREAMPTY */
133 * pointers, and counters.
135 char ptyibuf
[BUFSIZ
], *ptyip
= ptyibuf
;
136 char ptyibuf2
[BUFSIZ
];
138 #endif /* ! STREAMPTY */
140 int hostinfo
= 1; /* do we print login banner? */
143 extern int newmap
; /* nonzero if \n maps to ^M^J */
144 int lowpty
= 0, highpty
; /* low, high pty numbers */
151 extern void usage
P((void));
152 void doit(struct sockaddr_in
*);
153 int terminaltypeok(char *);
154 void startslave(char *, int, char *);
158 * The string to pass to getopt(). We do it this way so
159 * that only the actual options that we support will be
160 * passed off to getopt().
162 char valid_opts
[] = {
163 'd', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
164 #ifdef AUTHENTICATION
176 #if defined(CRAY) && defined(NEWINIT)
195 struct sockaddr_in from
;
200 #if defined(IPPROTO_IP) && defined(IP_TOS)
204 pfrontp
= pbackp
= ptyobuf
;
206 nfrontp
= nbackp
= netobuf
;
209 #endif /* ENCRYPTION */
215 * Get number of pty's before trying to process options,
216 * which may include changing pty range.
221 while ((ch
= getopt(argc
, argv
, valid_opts
)) != EOF
) {
224 #ifdef AUTHENTICATION
227 * Check for required authentication level
229 if (strcmp(optarg
, "debug") == 0) {
230 extern int auth_debug_mode
;
232 } else if (strcasecmp(optarg
, "none") == 0) {
234 } else if (strcasecmp(optarg
, "other") == 0) {
235 auth_level
= AUTH_OTHER
;
236 } else if (strcasecmp(optarg
, "user") == 0) {
237 auth_level
= AUTH_USER
;
238 } else if (strcasecmp(optarg
, "valid") == 0) {
239 auth_level
= AUTH_VALID
;
240 } else if (strcasecmp(optarg
, "off") == 0) {
242 * This hack turns off authentication
247 "telnetd: unknown authorization level for -a\n");
250 #endif /* AUTHENTICATION */
256 #endif /* BFTPDAEMON */
259 if (strcmp(optarg
, "ebug") == 0) {
270 * Check for desired diagnostics capabilities.
272 if (!strcmp(optarg
, "report")) {
273 diagnostic
|= TD_REPORT
|TD_OPTIONS
;
274 } else if (!strcmp(optarg
, "exercise")) {
275 diagnostic
|= TD_EXERCISE
;
276 } else if (!strcmp(optarg
, "netdata")) {
277 diagnostic
|= TD_NETDATA
;
278 } else if (!strcmp(optarg
, "ptydata")) {
279 diagnostic
|= TD_PTYDATA
;
280 } else if (!strcmp(optarg
, "options")) {
281 diagnostic
|= TD_OPTIONS
;
287 #endif /* DIAGNOSTICS */
291 if (strcmp(optarg
, "debug") == 0) {
292 extern int encrypt_debug_mode
;
293 encrypt_debug_mode
= 1;
299 #endif /* ENCRYPTION */
305 #if defined(CRAY) && defined(NEWINIT)
312 #endif /* defined(CRAY) && defined(NEWINIT) */
318 #endif /* LINEMODE */
321 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
322 lmodetype
= NO_AUTOKLUDGE
;
324 /* ignore -k option if built without kludge linemode */
325 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
339 * Allow the specification of alterations
340 * to the pty search range. It is legal to
341 * specify only one, and not change the
342 * other from its default.
344 c
= strchr(optarg
, '-');
350 lowpty
= atoi(optarg
);
351 if ((lowpty
> highpty
) || (lowpty
< 0) ||
362 /* SecurID required */
368 if ((tos
= parsetos(optarg
, "tcp")) < 0)
369 fprintf(stderr
, "%s%s%s\n",
370 "telnetd: Bad TOS argument '", optarg
,
371 "'; will try to use default TOS");
373 fprintf(stderr
, "%s%s\n", "TOS option unavailable; ",
374 "-S flag not supported\n");
379 utmp_len
= atoi(optarg
);
383 registerd_host_only
= 1;
386 #ifdef AUTHENTICATION
389 * Check for invalid authentication types
391 auth_disable_name(optarg
);
393 #endif /* AUTHENTICATION */
396 fprintf(stderr
, "telnetd: %c: unknown option\n", ch
);
410 static struct sockaddr_in sin
= { AF_INET
};
415 } else if (argc
== 1) {
416 if (sp
= getservbyname(*argv
, "tcp")) {
417 sin
.sin_port
= sp
->s_port
;
419 sin
.sin_port
= atoi(*argv
);
420 if ((int)sin
.sin_port
<= 0) {
421 fprintf(stderr
, "telnetd: %s: bad port #\n", *argv
);
425 sin
.sin_port
= htons((u_short
)sin
.sin_port
);
428 sp
= getservbyname("telnet", "tcp");
430 fprintf(stderr
, "telnetd: tcp/telnet: unknown service\n");
433 sin
.sin_port
= sp
->s_port
;
436 s
= socket(AF_INET
, SOCK_STREAM
, 0);
438 perror("telnetd: socket");;
441 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
442 (char *)&on
, sizeof(on
));
443 if (bind(s
, (struct sockaddr
*)&sin
, sizeof sin
) < 0) {
447 if (listen(s
, 1) < 0) {
452 ns
= accept(s
, (struct sockaddr
*)&sin
, &foo
);
461 } else if (argc
== 1) {
462 ; /* VOID*/ /* Just ignore the host/port name */
464 } else if (argc
> 0) {
469 #if defined(_SC_CRAY_SECURE_SYS)
470 secflag
= sysconf(_SC_CRAY_SECURE_SYS
);
473 * Get socket's security label
476 int szss
= sizeof(ss
);
477 #ifdef SO_SEC_MULTI /* 8.0 code */
479 int szi
= sizeof(int);
480 #endif /* SO_SEC_MULTI */
482 memset((char *)&dv
, 0, sizeof(dv
));
484 if (getsysv(&sysv
, sizeof(struct sysv
)) != 0) {
490 * Get socket security label and set device values
491 * {security label to be set on ttyp device}
493 #ifdef SO_SEC_MULTI /* 8.0 code */
494 if ((getsockopt(0, SOL_SOCKET
, SO_SECURITY
,
495 (char *)&ss
, &szss
) < 0) ||
496 (getsockopt(0, SOL_SOCKET
, SO_SEC_MULTI
,
497 (char *)&sock_multi
, &szi
) < 0)) {
498 perror("getsockopt");
501 dv
.dv_actlvl
= ss
.ss_actlabel
.lt_level
;
502 dv
.dv_actcmp
= ss
.ss_actlabel
.lt_compart
;
504 dv
.dv_minlvl
= dv
.dv_maxlvl
= dv
.dv_actlvl
;
505 dv
.dv_valcmp
= dv
.dv_actcmp
;
507 dv
.dv_minlvl
= ss
.ss_minlabel
.lt_level
;
508 dv
.dv_maxlvl
= ss
.ss_maxlabel
.lt_level
;
509 dv
.dv_valcmp
= ss
.ss_maxlabel
.lt_compart
;
513 #else /* SO_SEC_MULTI */ /* 7.0 code */
514 if (getsockopt(0, SOL_SOCKET
, SO_SECURITY
,
515 (char *)&ss
, &szss
) >= 0) {
516 dv
.dv_actlvl
= ss
.ss_slevel
;
517 dv
.dv_actcmp
= ss
.ss_compart
;
518 dv
.dv_minlvl
= ss
.ss_minlvl
;
519 dv
.dv_maxlvl
= ss
.ss_maxlvl
;
520 dv
.dv_valcmp
= ss
.ss_maxcmp
;
522 #endif /* SO_SEC_MULTI */
524 #endif /* _SC_CRAY_SECURE_SYS */
526 openlog("telnetd", LOG_PID
| LOG_ODELAY
, LOG_DAEMON
);
527 fromlen
= sizeof (from
);
528 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
529 fprintf(stderr
, "%s: ", progname
);
530 perror("getpeername");
534 setsockopt(0, SOL_SOCKET
, SO_KEEPALIVE
,
535 (char *)&on
, sizeof (on
)) < 0) {
536 syslog(LOG_WARNING
, "setsockopt (SO_KEEPALIVE): %m");
539 #if defined(IPPROTO_IP) && defined(IP_TOS)
541 # if defined(HAS_GETTOS)
543 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
547 tos
= 020; /* Low Delay bit */
549 && (setsockopt(0, IPPROTO_IP
, IP_TOS
,
550 (char *)&tos
, sizeof(tos
)) < 0)
551 && (errno
!= ENOPROTOOPT
) )
552 syslog(LOG_WARNING
, "setsockopt (IP_TOS): %m");
554 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
564 fprintf(stderr
, "Usage: telnetd");
565 #ifdef AUTHENTICATION
566 fprintf(stderr
, " [-a (debug|other|user|valid|off|none)]\n\t");
569 fprintf(stderr
, " [-B]");
571 fprintf(stderr
, " [-debug]");
573 fprintf(stderr
, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
575 #ifdef AUTHENTICATION
576 fprintf(stderr
, " [-edebug]");
578 fprintf(stderr
, " [-h]");
579 #if defined(CRAY) && defined(NEWINIT)
580 fprintf(stderr
, " [-Iinitid]");
582 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
583 fprintf(stderr
, " [-k]");
586 fprintf(stderr
, " [-l]");
588 fprintf(stderr
, " [-n]");
590 fprintf(stderr
, " [-r[lowpty]-[highpty]]");
592 fprintf(stderr
, "\n\t");
594 fprintf(stderr
, " [-s]");
597 fprintf(stderr
, " [-S tos]");
599 #ifdef AUTHENTICATION
600 fprintf(stderr
, " [-X auth-type]");
602 fprintf(stderr
, " [-u utmp_hostname_length] [-U]");
603 fprintf(stderr
, " [port]\n");
610 * Ask the other end to send along its terminal type and speed.
611 * Output is the variable terminaltype filled in.
613 static unsigned char ttytype_sbbuf
[] = {
614 IAC
, SB
, TELOPT_TTYPE
, TELQUAL_SEND
, IAC
, SE
618 getterminaltype(name
)
625 #if defined(AUTHENTICATION)
627 * Handle the Authentication option before we do anything else.
629 send_do(TELOPT_AUTHENTICATION
, 1);
630 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION
))
632 if (his_state_is_will(TELOPT_AUTHENTICATION
)) {
633 retval
= auth_wait(name
);
638 send_will(TELOPT_ENCRYPT
, 1);
639 #endif /* ENCRYPTION */
640 send_do(TELOPT_TTYPE
, 1);
641 send_do(TELOPT_TSPEED
, 1);
642 send_do(TELOPT_XDISPLOC
, 1);
643 send_do(TELOPT_NEW_ENVIRON
, 1);
644 send_do(TELOPT_OLD_ENVIRON
, 1);
647 his_do_dont_is_changing(TELOPT_ENCRYPT
) ||
648 #endif /* ENCRYPTION */
649 his_will_wont_is_changing(TELOPT_TTYPE
) ||
650 his_will_wont_is_changing(TELOPT_TSPEED
) ||
651 his_will_wont_is_changing(TELOPT_XDISPLOC
) ||
652 his_will_wont_is_changing(TELOPT_NEW_ENVIRON
) ||
653 his_will_wont_is_changing(TELOPT_OLD_ENVIRON
)) {
658 * Wait for the negotiation of what type of encryption we can
659 * send with. If autoencrypt is not set, this will just return.
661 if (his_state_is_will(TELOPT_ENCRYPT
)) {
664 #endif /* ENCRYPTION */
665 if (his_state_is_will(TELOPT_TSPEED
)) {
666 static unsigned char sb
[] =
667 { IAC
, SB
, TELOPT_TSPEED
, TELQUAL_SEND
, IAC
, SE
};
669 memmove(nfrontp
, sb
, sizeof sb
);
670 nfrontp
+= sizeof sb
;
671 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
673 if (his_state_is_will(TELOPT_XDISPLOC
)) {
674 static unsigned char sb
[] =
675 { IAC
, SB
, TELOPT_XDISPLOC
, TELQUAL_SEND
, IAC
, SE
};
677 memmove(nfrontp
, sb
, sizeof sb
);
678 nfrontp
+= sizeof sb
;
679 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
681 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
682 static unsigned char sb
[] =
683 { IAC
, SB
, TELOPT_NEW_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
685 memmove(nfrontp
, sb
, sizeof sb
);
686 nfrontp
+= sizeof sb
;
687 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
689 else if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
690 static unsigned char sb
[] =
691 { IAC
, SB
, TELOPT_OLD_ENVIRON
, TELQUAL_SEND
, IAC
, SE
};
693 memmove(nfrontp
, sb
, sizeof sb
);
694 nfrontp
+= sizeof sb
;
695 DIAG(TD_OPTIONS
, printsub('>', sb
+ 2, sizeof sb
- 2););
697 if (his_state_is_will(TELOPT_TTYPE
)) {
699 memmove(nfrontp
, ttytype_sbbuf
, sizeof ttytype_sbbuf
);
700 nfrontp
+= sizeof ttytype_sbbuf
;
701 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
702 sizeof ttytype_sbbuf
- 2););
704 if (his_state_is_will(TELOPT_TSPEED
)) {
705 while (sequenceIs(tspeedsubopt
, baseline
))
708 if (his_state_is_will(TELOPT_XDISPLOC
)) {
709 while (sequenceIs(xdisplocsubopt
, baseline
))
712 if (his_state_is_will(TELOPT_NEW_ENVIRON
)) {
713 while (sequenceIs(environsubopt
, baseline
))
716 if (his_state_is_will(TELOPT_OLD_ENVIRON
)) {
717 while (sequenceIs(oenvironsubopt
, baseline
))
720 if (his_state_is_will(TELOPT_TTYPE
)) {
721 char first
[256], last
[256];
723 while (sequenceIs(ttypesubopt
, baseline
))
727 * If the other side has already disabled the option, then
728 * we have to just go with what we (might) have already gotten.
730 if (his_state_is_will(TELOPT_TTYPE
) && !terminaltypeok(terminaltype
)) {
731 (void) strncpy(first
, terminaltype
, sizeof(first
));
734 * Save the unknown name, and request the next name.
736 (void) strncpy(last
, terminaltype
, sizeof(last
));
738 if (terminaltypeok(terminaltype
))
740 if ((strncmp(last
, terminaltype
, sizeof(last
)) == 0) ||
741 his_state_is_wont(TELOPT_TTYPE
)) {
743 * We've hit the end. If this is the same as
744 * the first name, just go with it.
746 if (strncmp(first
, terminaltype
, sizeof(first
)) == 0)
749 * Get the terminal name one more time, so that
750 * RFC1091 compliant telnets will cycle back to
751 * the start of the list.
754 if (strncmp(first
, terminaltype
, sizeof(first
)) != 0)
755 (void) strncpy(terminaltype
, first
, sizeof(first
));
762 } /* end of getterminaltype */
768 * If the client turned off the option,
769 * we can't send another request, so we
772 if (his_state_is_wont(TELOPT_TTYPE
))
775 memmove(nfrontp
, ttytype_sbbuf
, sizeof ttytype_sbbuf
);
776 nfrontp
+= sizeof ttytype_sbbuf
;
777 DIAG(TD_OPTIONS
, printsub('>', ttytype_sbbuf
+ 2,
778 sizeof ttytype_sbbuf
- 2););
779 while (sequenceIs(ttypesubopt
, baseline
))
789 if (terminaltype
== NULL
)
793 * tgetent() will return 1 if the type is known, and
794 * 0 if it is not known. If it returns -1, it couldn't
795 * open the database. But if we can't open the database,
796 * it won't help to say we failed, because we won't be
797 * able to verify anything else. So, we treat -1 like 1.
799 if (tgetent(buf
, s
) == 0)
804 #ifndef MAXHOSTNAMELEN
805 #define MAXHOSTNAMELEN 64
806 #endif /* MAXHOSTNAMELEN */
809 char host_name
[MAXHOSTNAMELEN
];
810 char remote_host_name
[MAXHOSTNAMELEN
];
813 extern void telnet
P((int, int));
815 extern void telnet
P((int, int, char *));
819 * Get a pty, scan input lines.
823 struct sockaddr_in
*who
;
825 char *host
= NULL
, *inet_ntoa();
832 * Find an available pty to use.
835 pty
= getpty(&ptynum
);
837 fatal(net
, "All network ports in use");
841 extern char *line
, *getpty();
843 if ((lp
= getpty()) == NULL
)
844 fatal(net
, "Out of ptys");
846 if ((pty
= open(lp
, O_RDWR
)) >= 0) {
847 strncpy(line
,lp
, sizeof(line
)-1);
848 line
[sizeof(line
)-1] = '\0';
855 #if defined(_SC_CRAY_SECURE_SYS)
857 * set ttyp line security label
862 sprintf(tty_dev
, "/dev/pty/%03d", ptynum
);
863 if (setdevs(tty_dev
, &dv
) < 0)
864 fatal(net
, "cannot set pty security");
865 sprintf(slave_dev
, "/dev/ttyp%03d", ptynum
);
866 if (setdevs(slave_dev
, &dv
) < 0)
867 fatal(net
, "cannot set tty security");
869 #endif /* _SC_CRAY_SECURE_SYS */
871 /* get name of connected client */
872 hp
= gethostbyaddr((char *)&who
->sin_addr
, sizeof (struct in_addr
),
875 if (hp
== NULL
&& registerd_host_only
) {
876 fatal(net
, "Couldn't resolve your address into a host name.\r\n\
877 Please contact your net administrator");
879 (strlen(hp
->h_name
) <= (unsigned int)((utmp_len
< 0) ? -utmp_len
883 host
= inet_ntoa(who
->sin_addr
);
886 * We must make a copy because Kerberos is probably going
887 * to also do a gethost* and overwrite the static data...
889 strncpy(remote_host_name
, host
, sizeof(remote_host_name
)-1);
890 remote_host_name
[sizeof(remote_host_name
)-1] = 0;
891 host
= remote_host_name
;
893 (void) gethostname(host_name
, sizeof (host_name
));
894 hostname
= host_name
;
896 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
897 auth_encrypt_init(hostname
, host
, "TELNETD", 1);
905 level
= getterminaltype(user_name
);
906 setenv("TERM", terminaltype
? terminaltype
: "network", 1);
909 * Start up the login process on the slave side of the terminal
912 startslave(host
, level
, user_name
);
914 #if defined(_SC_CRAY_SECURE_SYS)
916 if (setulvl(dv
.dv_actlvl
) < 0)
917 fatal(net
,"cannot setulvl()");
918 if (setucmp(dv
.dv_actcmp
) < 0)
919 fatal(net
, "cannot setucmp()");
921 #endif /* _SC_CRAY_SECURE_SYS */
923 telnet(net
, pty
); /* begin server processing */
925 telnet(net
, pty
, host
);
930 #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
932 Xterm_output(ibufp
, obuf
, icountp
, ocount
)
934 int *icountp
, ocount
;
937 ret
= term_output(*ibufp
, obuf
, *icountp
, ocount
);
942 #define term_output Xterm_output
943 #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
946 * Main loop. Select from pty and network, and
947 * hand data to telnet receiver finite state machine.
961 #define TABBUFSIZ 512
962 char defent
[TABBUFSIZ
];
963 char defstrs
[TABBUFSIZ
];
972 * Initialize the slc mapping table.
977 * Do some tests where it is desireable to wait for a response.
978 * Rather than doing them slowly, one at a time, do them all
981 if (my_state_is_wont(TELOPT_SGA
))
982 send_will(TELOPT_SGA
, 1);
984 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
985 * because 4.2 clients are unable to deal with TCP urgent data.
987 * To find out, we send out a "DO ECHO". If the remote system
988 * answers "WILL ECHO" it is probably a 4.2 client, and we note
989 * that fact ("WILL ECHO" ==> that the client will echo what
990 * WE, the server, sends it; it does NOT mean that the client will
991 * echo the terminal input).
993 send_do(TELOPT_ECHO
, 1);
996 if (his_state_is_wont(TELOPT_LINEMODE
)) {
997 /* Query the peer for linemode support by trying to negotiate
998 * the linemode option.
1002 send_do(TELOPT_LINEMODE
, 1); /* send do linemode */
1004 #endif /* LINEMODE */
1007 * Send along a couple of other options that we wish to negotiate.
1009 send_do(TELOPT_NAWS
, 1);
1010 send_will(TELOPT_STATUS
, 1);
1011 flowmode
= 1; /* default flow control state */
1012 restartany
= -1; /* uninitialized... */
1013 send_do(TELOPT_LFLOW
, 1);
1016 * Spin, waiting for a response from the DO ECHO. However,
1017 * some REALLY DUMB telnets out there might not respond
1018 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
1019 * telnets so far seem to respond with WONT for a DO that
1020 * they don't understand...) because by the time we get the
1021 * response, it will already have processed the DO ECHO.
1022 * Kludge upon kludge.
1024 while (his_will_wont_is_changing(TELOPT_NAWS
))
1029 * The client might have sent a WILL NAWS as part of its
1030 * startup code; if so, we'll be here before we get the
1031 * response to the DO ECHO. We'll make the assumption
1032 * that any implementation that understands about NAWS
1033 * is a modern enough implementation that it will respond
1034 * to our DO ECHO request; hence we'll do another spin
1035 * waiting for the ECHO option to settle down, which is
1036 * what we wanted to do in the first place...
1038 if (his_want_state_is_will(TELOPT_ECHO
) &&
1039 his_state_is_will(TELOPT_NAWS
)) {
1040 while (his_will_wont_is_changing(TELOPT_ECHO
))
1044 * On the off chance that the telnet client is broken and does not
1045 * respond to the DO ECHO we sent, (after all, we did send the
1046 * DO NAWS negotiation after the DO ECHO, and we won't get here
1047 * until a response to the DO NAWS comes back) simulate the
1048 * receipt of a will echo. This will also send a WONT ECHO
1049 * to the client, since we assume that the client failed to
1050 * respond because it believes that it is already in DO ECHO
1051 * mode, which we do not want.
1053 if (his_want_state_is_will(TELOPT_ECHO
)) {
1055 {sprintf(nfrontp
, "td: simulating recv\r\n");
1056 nfrontp
+= strlen(nfrontp
);});
1057 willoption(TELOPT_ECHO
);
1061 * Finally, to clean things up, we turn on our echo. This
1062 * will break stupid 4.2 telnets out of local terminal echo.
1065 if (my_state_is_wont(TELOPT_ECHO
))
1066 send_will(TELOPT_ECHO
, 1);
1070 * Turn on packet mode
1072 (void) ioctl(p
, TIOCPKT
, (char *)&on
);
1075 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1077 * Continuing line mode support. If client does not support
1078 * real linemode, attempt to negotiate kludge linemode by sending
1079 * the do timing mark sequence.
1081 if (lmodetype
< REAL_LINEMODE
)
1082 send_do(TELOPT_TM
, 1);
1083 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1086 * Call telrcv() once to pick up anything received during
1087 * terminal type negotiation, 4.2/4.3 determination, and
1088 * linemode negotiation.
1092 (void) ioctl(f
, FIONBIO
, (char *)&on
);
1093 (void) ioctl(p
, FIONBIO
, (char *)&on
);
1094 #if defined(CRAY2) && defined(UNICOS5)
1095 init_termdriver(f
, p
, interrupt
, sendbrk
);
1098 #if defined(SO_OOBINLINE)
1099 (void) setsockopt(net
, SOL_SOCKET
, SO_OOBINLINE
,
1100 (char *)&on
, sizeof on
);
1101 #endif /* defined(SO_OOBINLINE) */
1104 (void) signal(SIGTSTP
, SIG_IGN
);
1108 * Ignoring SIGTTOU keeps the kernel from blocking us
1109 * in ttioct() in /sys/tty.c.
1111 (void) signal(SIGTTOU
, SIG_IGN
);
1114 (void) signal(SIGCHLD
, cleanup
);
1116 #if defined(CRAY2) && defined(UNICOS5)
1118 * Cray-2 will send a signal when pty modes are changed by slave
1119 * side. Set up signal handler now.
1121 if ((int)signal(SIGUSR1
, termstat
) < 0)
1123 else if (ioctl(p
, TCSIGME
, (char *)SIGUSR1
) < 0)
1124 perror("ioctl:TCSIGME");
1126 * Make processing loop check terminal characteristics early on.
1134 t
= open(_PATH_TTY
, O_RDWR
);
1136 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
1142 #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
1144 ioctl(p
, TIOCSCTTY
, 0);
1148 * Show banner that getty never gave.
1150 * We put the banner in the pty input buffer. This way, it
1151 * gets carriage return null processing, etc., just like all
1152 * other pty --> client data.
1155 #if !defined(CRAY) || !defined(NEWINIT)
1160 if (getent(defent
, "default") == 1) {
1164 HE
= Getstr("he", &cp
);
1165 HN
= Getstr("hn", &cp
);
1166 IM
= Getstr("im", &cp
);
1168 (void) strncpy(host_name
, HN
, sizeof(host_name
)-1);
1169 hostname
[sizeof(host_name
)-1] = '\0';
1177 edithost(HE
, host_name
);
1178 if (hostinfo
&& *IM
)
1182 (void) strncat(ptyibuf2
, ptyip
, pcc
+1);
1184 pcc
= strlen(ptyip
);
1187 * Last check to make sure all our states are correct.
1191 #endif /* LINEMODE */
1194 {sprintf(nfrontp
, "td: Entering processing loop\r\n");
1195 nfrontp
+= strlen(nfrontp
);});
1201 nfd
= ((f
> p
) ? f
: p
) + 1;
1203 fd_set ibits
, obits
, xbits
;
1206 if (ncc
< 0 && pcc
< 0)
1209 #if defined(CRAY2) && defined(UNICOS5)
1212 #endif /* defined(CRAY2) && defined(UNICOS5) */
1217 * Never look for input if there's still
1218 * stuff in the corresponding output buffer
1220 if (nfrontp
- nbackp
|| pcc
> 0) {
1225 if (pfrontp
- pbackp
|| ncc
> 0) {
1233 if ((c
= select(nfd
, &ibits
, &obits
, &xbits
,
1234 (struct timeval
*)0)) < 1) {
1236 if (errno
== EINTR
) {
1247 if (FD_ISSET(net
, &xbits
)) {
1252 * Something to read from the network...
1254 if (FD_ISSET(net
, &ibits
)) {
1255 #if !defined(SO_OOBINLINE)
1257 * In 4.2 (and 4.3 beta) systems, the
1258 * OOB indication and data handling in the kernel
1259 * is such that if two separate TCP Urgent requests
1260 * come in, one byte of TCP data will be overlaid.
1261 * This is fatal for Telnet, but we try to live
1264 * In addition, in 4.2 (and...), a special protocol
1265 * is needed to pick up the TCP Urgent data in
1266 * the correct sequence.
1268 * What we do is: if we think we are in urgent
1269 * mode, we look to see if we are "at the mark".
1270 * If we are, we do an OOB receive. If we run
1271 * this twice, we will do the OOB receive twice,
1272 * but the second will fail, since the second
1273 * time we were "at the mark", but there wasn't
1274 * any data there (the kernel doesn't reset
1275 * "at the mark" until we do a normal read).
1276 * Once we've read the OOB data, we go ahead
1277 * and do normal reads.
1279 * There is also another problem, which is that
1280 * since the OOB byte we read doesn't put us
1281 * out of OOB state, and since that byte is most
1282 * likely the TELNET DM (data mark), we would
1283 * stay in the TELNET SYNCH (SYNCHing) state.
1284 * So, clocks to the rescue. If we've "just"
1285 * received a DM, then we test for the
1286 * presence of OOB data when the receive OOB
1287 * fails (and AFTER we did the normal mode read
1288 * to clear "at the mark").
1293 (void) ioctl(net
, SIOCATMARK
, (char *)&atmark
);
1295 ncc
= recv(net
, netibuf
, sizeof (netibuf
), MSG_OOB
);
1296 if ((ncc
== -1) && (errno
== EINVAL
)) {
1297 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1298 if (sequenceIs(didnetreceive
, gotDM
)) {
1299 SYNCHing
= stilloob(net
);
1303 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1306 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1308 settimer(didnetreceive
);
1309 #else /* !defined(SO_OOBINLINE)) */
1310 ncc
= read(net
, netibuf
, sizeof (netibuf
));
1311 #endif /* !defined(SO_OOBINLINE)) */
1312 if (ncc
< 0 && errno
== EWOULDBLOCK
)
1320 DIAG((TD_REPORT
| TD_NETDATA
),
1321 {sprintf(nfrontp
, "td: netread %d chars\r\n", ncc
);
1322 nfrontp
+= strlen(nfrontp
);});
1323 DIAG(TD_NETDATA
, printdata("nd", netip
, ncc
));
1327 * Something to read from the pty...
1329 if (FD_ISSET(p
, &ibits
)) {
1331 pcc
= read(p
, ptyibuf
, BUFSIZ
);
1333 pcc
= readstream(p
, ptyibuf
, BUFSIZ
);
1336 * On some systems, if we try to read something
1337 * off the master side before the slave side is
1338 * opened, we get EIO.
1340 if (pcc
< 0 && (errno
== EWOULDBLOCK
||
1349 #if !defined(CRAY2) || !defined(UNICOS5)
1352 * If ioctl from pty, pass it through net
1354 if (ptyibuf
[0] & TIOCPKT_IOCTL
) {
1355 copy_termbuf(ptyibuf
+1, pcc
-1);
1359 #endif /* LINEMODE */
1360 if (ptyibuf
[0] & TIOCPKT_FLUSHWRITE
) {
1361 netclear(); /* clear buffer back */
1364 * There are client telnets on some
1365 * operating systems get screwed up
1366 * royally if we send them urgent
1371 neturg
= nfrontp
-1; /* off by one XXX */
1373 printoption("td: send IAC", DM
));
1377 if (his_state_is_will(TELOPT_LFLOW
) &&
1379 (TIOCPKT_NOSTOP
|TIOCPKT_DOSTOP
))) {
1381 ptyibuf
[0] & TIOCPKT_DOSTOP
? 1 : 0;
1382 if (newflow
!= flowmode
) {
1384 (void) sprintf(nfrontp
,
1386 IAC
, SB
, TELOPT_LFLOW
,
1391 DIAG(TD_OPTIONS
, printsub('>',
1392 (unsigned char *)nfrontp
-4,
1398 #else /* defined(CRAY2) && defined(UNICOS5) */
1402 pcc
= term_output(&unptyip
, ptyibuf2
,
1407 #endif /* defined(CRAY2) && defined(UNICOS5) */
1412 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 2)
1414 c
= *ptyip
++ & 0377, pcc
--;
1417 #if defined(CRAY2) && defined(UNICOS5)
1418 else if (c
== '\n' &&
1419 my_state_is_wont(TELOPT_BINARY
) && newmap
)
1421 #endif /* defined(CRAY2) && defined(UNICOS5) */
1423 if ((c
== '\r') && (my_state_is_wont(TELOPT_BINARY
))) {
1424 if (pcc
> 0 && ((*ptyip
& 0377) == '\n')) {
1425 *nfrontp
++ = *ptyip
++ & 0377;
1431 #if defined(CRAY2) && defined(UNICOS5)
1433 * If chars were left over from the terminal driver,
1434 * note their existence.
1436 if (!uselinemode
&& unpcc
) {
1441 #endif /* defined(CRAY2) && defined(UNICOS5) */
1443 if (FD_ISSET(f
, &obits
) && (nfrontp
- nbackp
) > 0)
1447 if (FD_ISSET(p
, &obits
) && (pfrontp
- pbackp
) > 0)
1451 } /* end of telnet */
1455 # define TCSIG TIOCSIG
1461 int flowison
= -1; /* current state of flow: -1 is unknown */
1463 int readstream(p
, ibuf
, bufsize
)
1470 struct termios
*tsp
;
1477 strbufc
.maxlen
= BUFSIZ
;
1478 strbufc
.buf
= (char *)ctlbuf
;
1479 strbufd
.maxlen
= bufsize
-1;
1481 strbufd
.buf
= ibuf
+1;
1484 ret
= getmsg(p
, &strbufc
, &strbufd
, &flags
);
1485 if (ret
< 0) /* error of some sort -- probably EAGAIN */
1488 if (strbufc
.len
<= 0 || ctlbuf
[0] == M_DATA
) {
1490 if (strbufd
.len
> 0) { /* real data */
1491 return(strbufd
.len
+ 1); /* count header char */
1500 * It's a control message. Return 1, to look at the flag we set
1503 switch (ctlbuf
[0]) {
1505 if (ibuf
[1] & FLUSHW
)
1506 ibuf
[0] = TIOCPKT_FLUSHWRITE
;
1510 ip
= (struct iocblk
*) (ibuf
+1);
1512 switch (ip
->ioc_cmd
) {
1516 tsp
= (struct termios
*)
1517 (ibuf
+1 + sizeof(struct iocblk
));
1518 vstop
= tsp
->c_cc
[VSTOP
];
1519 vstart
= tsp
->c_cc
[VSTART
];
1520 ixon
= tsp
->c_iflag
& IXON
;
1525 tp
= (struct termio
*) (ibuf
+1 + sizeof(struct iocblk
));
1526 vstop
= tp
->c_cc
[VSTOP
];
1527 vstart
= tp
->c_cc
[VSTART
];
1528 ixon
= tp
->c_iflag
& IXON
;
1535 newflow
= (ixon
&& (vstart
== 021) && (vstop
== 023)) ? 1 : 0;
1536 if (newflow
!= flowison
) { /* it's a change */
1538 ibuf
[0] = newflow
? TIOCPKT_DOSTOP
: TIOCPKT_NOSTOP
;
1543 /* nothing worth doing anything about */
1547 #endif /* STREAMSPTY */
1550 * Send interrupt to process on other side of pty.
1551 * If it is in raw mode, just write NULL;
1552 * otherwise, write intr char.
1557 ptyflush(); /* half-hearted */
1559 #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1560 /* Streams PTY style ioctl to post a signal */
1563 (void) ioctl(pty
, TIOCSIGNAL
, &sig
);
1564 (void) ioctl(pty
, I_FLUSH
, FLUSHR
);
1568 (void) ioctl(pty
, TCSIG
, (char *)SIGINT
);
1571 *pfrontp
++ = slctab
[SLC_IP
].sptr
?
1572 (unsigned char)*slctab
[SLC_IP
].sptr
: '\177';
1578 * Send quit to process on other side of pty.
1579 * If it is in raw mode, just write NULL;
1580 * otherwise, write quit char.
1585 ptyflush(); /* half-hearted */
1587 (void) ioctl(pty
, TCSIG
, (char *)SIGQUIT
);
1590 *pfrontp
++ = slctab
[SLC_ABORT
].sptr
?
1591 (unsigned char)*slctab
[SLC_ABORT
].sptr
: '\034';
1599 ptyflush(); /* half-hearted */
1601 (void) ioctl(pty
, TCSIG
, (char *)SIGTSTP
);
1603 *pfrontp
++ = slctab
[SLC_SUSP
].sptr
?
1604 (unsigned char)*slctab
[SLC_SUSP
].sptr
: '\032';
1606 #endif /* SIGTSTP */
1610 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1611 * just send back "[Yes]".
1616 #if defined(SIGINFO) && defined(TCSIG)
1617 if (slctab
[SLC_AYT
].sptr
&& *slctab
[SLC_AYT
].sptr
!= _POSIX_VDISABLE
) {
1618 (void) ioctl(pty
, TCSIG
, (char *)SIGINFO
);
1622 (void) strcpy(nfrontp
, "\r\n[Yes]\r\n");
1631 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1632 if (!tty_isediting()) {
1633 extern char oldeofc
;
1634 *pfrontp
++ = oldeofc
;
1638 *pfrontp
++ = slctab
[SLC_EOF
].sptr
?
1639 (unsigned char)*slctab
[SLC_EOF
].sptr
: '\004';