telnetd.c revision 1.25 1 /* $NetBSD: telnetd.c,v 1.25 2001/02/04 22:32:17 christos Exp $ */
2
3 /*
4 * Copyright (C) 1997 and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #ifndef lint
67 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
68 The Regents of the University of California. All rights reserved.\n");
69 #if 0
70 static char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95";
71 #else
72 __RCSID("$NetBSD: telnetd.c,v 1.25 2001/02/04 22:32:17 christos Exp $");
73 #endif
74 #endif /* not lint */
75
76 #include "telnetd.h"
77 #include "pathnames.h"
78
79 #include <arpa/inet.h>
80
81 #include <err.h>
82 #include <termcap.h>
83
84 #include <limits.h>
85
86 #define P __P
87
88 #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY)
89 /*
90 * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can
91 * use it to tell us to turn off all the socket security code,
92 * since that is only used in UNICOS 7.0 and later.
93 */
94 # undef _SC_CRAY_SECURE_SYS
95 #endif
96
97 #if defined(_SC_CRAY_SECURE_SYS)
98 #include <sys/sysv.h>
99 #include <sys/secdev.h>
100 # ifdef SO_SEC_MULTI /* 8.0 code */
101 #include <sys/secparm.h>
102 #include <sys/usrv.h>
103 # endif /* SO_SEC_MULTI */
104 int secflag;
105 char tty_dev[16];
106 struct secdev dv;
107 struct sysv sysv;
108 # ifdef SO_SEC_MULTI /* 8.0 code */
109 struct socksec ss;
110 # else /* SO_SEC_MULTI */ /* 7.0 code */
111 struct socket_security ss;
112 # endif /* SO_SEC_MULTI */
113 #endif /* _SC_CRAY_SECURE_SYS */
114
115 #if defined(KRB5)
116 #define Authenticator k5_Authenticator
117 #include <krb5.h>
118 #undef Authenticator
119 #include <com_err.h>
120 #endif
121
122 #if defined(AUTHENTICATION)
123 int auth_level = 0;
124 #endif
125
126 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
127 #include <libtelnet/misc.h>
128 #endif
129
130 #if defined(SECURELOGIN)
131 int require_secure_login = 0;
132 #endif
133
134 extern int utmp_len;
135 extern int require_hwpreauth;
136 #ifdef KRB5
137 extern krb5_context telnet_context;
138 #endif
139 int registerd_host_only = 0;
140
141 #ifdef STREAMSPTY
142 # include <stropts.h>
143 # include <termio.h>
144 /* make sure we don't get the bsd version */
145 # include "/usr/include/sys/tty.h"
146 # include <sys/ptyvar.h>
147
148 /*
149 * Because of the way ptyibuf is used with streams messages, we need
150 * ptyibuf+1 to be on a full-word boundary. The following wierdness
151 * is simply to make that happen.
152 */
153 long ptyibufbuf[BUFSIZ/sizeof(long)+1];
154 char *ptyibuf = ((char *)&ptyibufbuf[1])-1;
155 char *ptyip = ((char *)&ptyibufbuf[1])-1;
156 char ptyibuf2[BUFSIZ*4];
157 unsigned char ctlbuf[BUFSIZ];
158 struct strbuf strbufc, strbufd;
159
160 int readstream();
161
162 #else /* ! STREAMPTY */
163
164 /*
165 * I/O data buffers,
166 * pointers, and counters.
167 */
168 char ptyibuf[BUFSIZ], *ptyip = ptyibuf;
169 char ptyibuf2[BUFSIZ];
170
171 #endif /* ! STREAMPTY */
172
173 int hostinfo = 1; /* do we print login banner? */
174
175 #ifdef CRAY
176 extern int newmap; /* nonzero if \n maps to ^M^J */
177 int lowpty = 0, highpty; /* low, high pty numbers */
178 #endif /* CRAY */
179
180 int debug = 0;
181 int keepalive = 1;
182 char *gettyname = "default";
183 char *progname;
184
185 int main __P((int, char *[]));
186 void usage __P((void));
187 int getterminaltype __P((char *));
188 int getent __P((char *, char *));
189 void doit __P((struct sockaddr *));
190 void _gettermname __P((void));
191 int terminaltypeok __P((char *));
192 char *getstr __P((char *, char **));
193
194 /*
195 * The string to pass to getopt(). We do it this way so
196 * that only the actual options that we support will be
197 * passed off to getopt().
198 */
199 char valid_opts[] = {
200 'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
201 '4', '6',
202 #ifdef AUTHENTICATION
203 'a', ':', 'X', ':',
204 #endif
205 #ifdef BFTPDAEMON
206 'B',
207 #endif
208 #ifdef ENCRYPTION
209 'e', ':',
210 #endif
211 #ifdef DIAGNOSTICS
212 'D', ':',
213 #endif
214 #if defined(CRAY) && defined(NEWINIT)
215 'I', ':',
216 #endif
217 #ifdef LINEMODE
218 'l',
219 #endif
220 #ifdef CRAY
221 'r', ':',
222 #endif
223 #ifdef SECURELOGIN
224 's',
225 #endif
226 #ifdef KRB5
227 'R', ':', 'H',
228 #endif
229 '\0'
230 };
231
232 int family = AF_INET;
233
234 int
235 main(argc, argv)
236 int argc;
237 char *argv[];
238 {
239 struct sockaddr_storage from;
240 int on = 1, fromlen;
241 register int ch;
242 #if defined(IPPROTO_IP) && defined(IP_TOS)
243 int tos = -1;
244 #endif
245
246 pfrontp = pbackp = ptyobuf;
247 netip = netibuf;
248 nfrontp = nbackp = netobuf;
249 #ifdef ENCRYPTION
250 nclearto = 0;
251 #endif /* ENCRYPTION */
252
253 progname = *argv;
254
255 #ifdef CRAY
256 /*
257 * Get number of pty's before trying to process options,
258 * which may include changing pty range.
259 */
260 highpty = getnpty();
261 #endif /* CRAY */
262
263 while ((ch = getopt(argc, argv, valid_opts)) != -1) {
264 switch (ch) {
265
266 #ifdef AUTHENTICATION
267 case 'a':
268 /*
269 * Check for required authentication level
270 */
271 if (strcmp(optarg, "debug") == 0) {
272 auth_debug_mode = 1;
273 } else if (strcasecmp(optarg, "none") == 0) {
274 auth_level = 0;
275 } else if (strcasecmp(optarg, "other") == 0) {
276 auth_level = AUTH_OTHER;
277 } else if (strcasecmp(optarg, "user") == 0) {
278 auth_level = AUTH_USER;
279 } else if (strcasecmp(optarg, "valid") == 0) {
280 auth_level = AUTH_VALID;
281 } else if (strcasecmp(optarg, "off") == 0) {
282 /*
283 * This hack turns off authentication
284 */
285 auth_level = -1;
286 } else {
287 fprintf(stderr,
288 "telnetd: unknown authorization level for -a\n");
289 }
290 break;
291 #endif /* AUTHENTICATION */
292
293 #ifdef BFTPDAEMON
294 case 'B':
295 bftpd++;
296 break;
297 #endif /* BFTPDAEMON */
298
299 case 'd':
300 if (strcmp(optarg, "ebug") == 0) {
301 debug++;
302 break;
303 }
304 usage();
305 /* NOTREACHED */
306 break;
307
308 #ifdef DIAGNOSTICS
309 case 'D':
310 /*
311 * Check for desired diagnostics capabilities.
312 */
313 if (!strcmp(optarg, "report")) {
314 diagnostic |= TD_REPORT|TD_OPTIONS;
315 } else if (!strcmp(optarg, "exercise")) {
316 diagnostic |= TD_EXERCISE;
317 } else if (!strcmp(optarg, "netdata")) {
318 diagnostic |= TD_NETDATA;
319 } else if (!strcmp(optarg, "ptydata")) {
320 diagnostic |= TD_PTYDATA;
321 } else if (!strcmp(optarg, "options")) {
322 diagnostic |= TD_OPTIONS;
323 } else {
324 usage();
325 /* NOT REACHED */
326 }
327 break;
328 #endif /* DIAGNOSTICS */
329
330 #ifdef ENCRYPTION
331 case 'e':
332 if (strcmp(optarg, "debug") == 0) {
333 encrypt_debug_mode = 1;
334 break;
335 }
336 usage();
337 /* NOTREACHED */
338 break;
339 #endif /* ENCRYPTION */
340
341 case 'g':
342 gettyname = optarg;
343 break;
344
345 case 'h':
346 hostinfo = 0;
347 break;
348
349 #ifdef KRB5
350 case 'H':
351 {
352 require_hwpreauth = 1;
353 break;
354 }
355 #endif /* KRB5 */
356
357 #if defined(CRAY) && defined(NEWINIT)
358 case 'I':
359 {
360 extern char *gen_id;
361 gen_id = optarg;
362 break;
363 }
364 #endif /* defined(CRAY) && defined(NEWINIT) */
365
366 #ifdef LINEMODE
367 case 'l':
368 alwayslinemode = 1;
369 break;
370 #endif /* LINEMODE */
371
372 case 'k':
373 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
374 lmodetype = NO_AUTOKLUDGE;
375 #else
376 /* ignore -k option if built without kludge linemode */
377 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
378 break;
379
380 case 'n':
381 keepalive = 0;
382 break;
383
384 #ifdef CRAY
385 case 'r':
386 {
387 char *strchr();
388 char *c;
389
390 /*
391 * Allow the specification of alterations
392 * to the pty search range. It is legal to
393 * specify only one, and not change the
394 * other from its default.
395 */
396 c = strchr(optarg, '-');
397 if (c) {
398 *c++ = '\0';
399 highpty = atoi(c);
400 }
401 if (*optarg != '\0')
402 lowpty = atoi(optarg);
403 if ((lowpty > highpty) || (lowpty < 0) ||
404 (highpty > 32767)) {
405 usage();
406 /* NOT REACHED */
407 }
408 break;
409 }
410 #endif /* CRAY */
411
412 #ifdef KRB5
413 case 'R':
414 {
415 krb5_error_code retval;
416
417 if (telnet_context == 0) {
418 retval = krb5_init_context(&telnet_context);
419 if (retval) {
420 com_err("telnetd", retval,
421 "while initializing krb5");
422 exit(1);
423 }
424 }
425 krb5_set_default_realm(telnet_context, optarg);
426 break;
427 }
428 #endif /* KRB5 */
429
430 #ifdef SECURELOGIN
431 case 's':
432 /* Secure login required */
433 require_secure_login = 1;
434 break;
435 #endif /* SECURELOGIN */
436 case 'S':
437 #ifdef HAS_GETTOS
438 if ((tos = parsetos(optarg, "tcp")) < 0)
439 fprintf(stderr, "%s%s%s\n",
440 "telnetd: Bad TOS argument '", optarg,
441 "'; will try to use default TOS");
442 #else
443 fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
444 "-S flag not supported\n");
445 #endif
446 break;
447
448 case 'u':
449 utmp_len = atoi(optarg);
450 break;
451
452 case 'U':
453 registerd_host_only = 1;
454 break;
455
456 #ifdef AUTHENTICATION
457 case 'X':
458 /*
459 * Check for invalid authentication types
460 */
461 auth_disable_name(optarg);
462 break;
463 #endif /* AUTHENTICATION */
464
465 case '4':
466 family = AF_INET;
467 break;
468
469 case '6':
470 family = AF_INET6;
471 break;
472
473 default:
474 fprintf(stderr, "telnetd: %c: unknown option\n", ch);
475 /* FALLTHROUGH */
476 case '?':
477 usage();
478 /* NOTREACHED */
479 }
480 }
481
482 argc -= optind;
483 argv += optind;
484
485 if (debug) {
486 int s, ns, foo, error;
487 char *service = "telnet";
488 struct addrinfo hints, *res;
489
490 if (argc > 1) {
491 usage();
492 /* NOT REACHED */
493 } else if (argc == 1)
494 service = *argv;
495
496 memset(&hints, 0, sizeof(hints));
497 hints.ai_flags = AI_PASSIVE;
498 hints.ai_family = family;
499 hints.ai_socktype = SOCK_STREAM;
500 hints.ai_protocol = 0;
501 error = getaddrinfo(NULL, service, &hints, &res);
502
503 if (error) {
504 fprintf(stderr, "tcp/%s: %s\n", service, gai_strerror(error));
505 exit(1);
506 }
507
508 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
509 if (s < 0) {
510 perror("telnetd: socket");;
511 exit(1);
512 }
513 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
514 (char *)&on, sizeof(on));
515 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
516 perror("bind");
517 exit(1);
518 }
519 if (listen(s, 1) < 0) {
520 perror("listen");
521 exit(1);
522 }
523 foo = res->ai_addrlen;
524 ns = accept(s, res->ai_addr, &foo);
525 if (ns < 0) {
526 perror("accept");
527 exit(1);
528 }
529 (void) dup2(ns, 0);
530 (void) close(ns);
531 (void) close(s);
532 #ifdef convex
533 } else if (argc == 1) {
534 ; /* VOID*/ /* Just ignore the host/port name */
535 #endif
536 } else if (argc > 0) {
537 usage();
538 /* NOT REACHED */
539 }
540
541 #if defined(_SC_CRAY_SECURE_SYS)
542 secflag = sysconf(_SC_CRAY_SECURE_SYS);
543
544 /*
545 * Get socket's security label
546 */
547 if (secflag) {
548 int szss = sizeof(ss);
549 #ifdef SO_SEC_MULTI /* 8.0 code */
550 int sock_multi;
551 int szi = sizeof(int);
552 #endif /* SO_SEC_MULTI */
553
554 memset((char *)&dv, 0, sizeof(dv));
555
556 if (getsysv(&sysv, sizeof(struct sysv)) != 0) {
557 perror("getsysv");
558 exit(1);
559 }
560
561 /*
562 * Get socket security label and set device values
563 * {security label to be set on ttyp device}
564 */
565 #ifdef SO_SEC_MULTI /* 8.0 code */
566 if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
567 (char *)&ss, &szss) < 0) ||
568 (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
569 (char *)&sock_multi, &szi) < 0)) {
570 perror("getsockopt");
571 exit(1);
572 } else {
573 dv.dv_actlvl = ss.ss_actlabel.lt_level;
574 dv.dv_actcmp = ss.ss_actlabel.lt_compart;
575 if (!sock_multi) {
576 dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
577 dv.dv_valcmp = dv.dv_actcmp;
578 } else {
579 dv.dv_minlvl = ss.ss_minlabel.lt_level;
580 dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
581 dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
582 }
583 dv.dv_devflg = 0;
584 }
585 #else /* SO_SEC_MULTI */ /* 7.0 code */
586 if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
587 (char *)&ss, &szss) >= 0) {
588 dv.dv_actlvl = ss.ss_slevel;
589 dv.dv_actcmp = ss.ss_compart;
590 dv.dv_minlvl = ss.ss_minlvl;
591 dv.dv_maxlvl = ss.ss_maxlvl;
592 dv.dv_valcmp = ss.ss_maxcmp;
593 }
594 #endif /* SO_SEC_MULTI */
595 }
596 #endif /* _SC_CRAY_SECURE_SYS */
597
598 openlog("telnetd", LOG_PID, LOG_DAEMON);
599 fromlen = sizeof (from);
600 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
601 fprintf(stderr, "%s: ", progname);
602 perror("getpeername");
603 _exit(1);
604 }
605 if (keepalive &&
606 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
607 (char *)&on, sizeof (on)) < 0) {
608 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
609 }
610
611 #if defined(IPPROTO_IP) && defined(IP_TOS)
612 if (((struct sockaddr *)&from)->sa_family == AF_INET) {
613 # if defined(HAS_GETTOS)
614 struct tosent *tp;
615 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
616 tos = tp->t_tos;
617 # endif
618 if (tos < 0)
619 tos = 020; /* Low Delay bit */
620 if (tos
621 && (setsockopt(0, IPPROTO_IP, IP_TOS,
622 (char *)&tos, sizeof(tos)) < 0)
623 && (errno != ENOPROTOOPT) )
624 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
625 }
626 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
627
628 net = 0;
629 doit((struct sockaddr *)&from);
630 /* NOTREACHED */
631 #ifdef __GNUC__
632 exit(0);
633 #endif
634 } /* end of main */
635
636 void
637 usage()
638 {
639 fprintf(stderr, "Usage: telnetd");
640 #ifdef AUTHENTICATION
641 fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
642 #endif
643 #ifdef BFTPDAEMON
644 fprintf(stderr, " [-B]");
645 #endif
646 fprintf(stderr, " [-debug]");
647 #ifdef DIAGNOSTICS
648 fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
649 #endif
650 #ifdef AUTHENTICATION
651 fprintf(stderr, " [-edebug]");
652 #endif
653 fprintf(stderr, " [-h]");
654 #if defined(CRAY) && defined(NEWINIT)
655 fprintf(stderr, " [-Iinitid]");
656 #endif
657 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
658 fprintf(stderr, " [-k]");
659 #endif
660 #ifdef LINEMODE
661 fprintf(stderr, " [-l]");
662 #endif
663 fprintf(stderr, " [-n]");
664 #ifdef CRAY
665 fprintf(stderr, " [-r[lowpty]-[highpty]]");
666 #endif
667 fprintf(stderr, "\n\t");
668 #ifdef SECURELOGIN
669 fprintf(stderr, " [-s]");
670 #endif
671 #ifdef HAS_GETTOS
672 fprintf(stderr, " [-S tos]");
673 #endif
674 #ifdef AUTHENTICATION
675 fprintf(stderr, " [-X auth-type]");
676 #endif
677 fprintf(stderr, " [-u utmp_hostname_length] [-U]");
678 fprintf(stderr, " [port]\n");
679 exit(1);
680 }
681
682 /*
683 * getterminaltype
684 *
685 * Ask the other end to send along its terminal type and speed.
686 * Output is the variable terminaltype filled in.
687 */
688 static unsigned char ttytype_sbbuf[] = {
689 IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
690 };
691
692 int
693 getterminaltype(name)
694 char *name;
695 {
696 int retval = -1;
697
698 settimer(baseline);
699 #if defined(AUTHENTICATION)
700 /*
701 * Handle the Authentication option before we do anything else.
702 */
703 send_do(TELOPT_AUTHENTICATION, 1);
704 while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
705 ttloop();
706 if (his_state_is_will(TELOPT_AUTHENTICATION)) {
707 retval = auth_wait(name);
708 }
709 #endif
710
711 #ifdef ENCRYPTION
712 send_will(TELOPT_ENCRYPT, 1);
713 #endif /* ENCRYPTION */
714 send_do(TELOPT_TTYPE, 1);
715 send_do(TELOPT_TSPEED, 1);
716 send_do(TELOPT_XDISPLOC, 1);
717 send_do(TELOPT_NEW_ENVIRON, 1);
718 send_do(TELOPT_OLD_ENVIRON, 1);
719 while (
720 #ifdef ENCRYPTION
721 his_do_dont_is_changing(TELOPT_ENCRYPT) ||
722 #endif /* ENCRYPTION */
723 his_will_wont_is_changing(TELOPT_TTYPE) ||
724 his_will_wont_is_changing(TELOPT_TSPEED) ||
725 his_will_wont_is_changing(TELOPT_XDISPLOC) ||
726 his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
727 his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
728 ttloop();
729 }
730 if (his_state_is_will(TELOPT_TSPEED)) {
731 static unsigned char sb[] =
732 { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
733
734 memmove(nfrontp, sb, sizeof sb);
735 nfrontp += sizeof sb;
736 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
737 }
738 #ifdef ENCRYPTION
739 /*
740 * Wait for the negotiation of what type of encryption we can
741 * send with. If autoencrypt is not set, this will just return.
742 */
743 if (his_state_is_will(TELOPT_ENCRYPT)) {
744 encrypt_wait();
745 }
746 #endif /* ENCRYPTION */
747 if (his_state_is_will(TELOPT_XDISPLOC)) {
748 static unsigned char sb[] =
749 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
750
751 memmove(nfrontp, sb, sizeof sb);
752 nfrontp += sizeof sb;
753 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
754 }
755 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
756 static unsigned char sb[] =
757 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
758
759 memmove(nfrontp, sb, sizeof sb);
760 nfrontp += sizeof sb;
761 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
762 }
763 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
764 static unsigned char sb[] =
765 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
766
767 memmove(nfrontp, sb, sizeof sb);
768 nfrontp += sizeof sb;
769 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
770 }
771 if (his_state_is_will(TELOPT_TTYPE)) {
772
773 memmove(nfrontp, ttytype_sbbuf, sizeof ttytype_sbbuf);
774 nfrontp += sizeof ttytype_sbbuf;
775 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
776 sizeof ttytype_sbbuf - 2););
777 }
778 if (his_state_is_will(TELOPT_TSPEED)) {
779 while (sequenceIs(tspeedsubopt, baseline))
780 ttloop();
781 }
782 if (his_state_is_will(TELOPT_XDISPLOC)) {
783 while (sequenceIs(xdisplocsubopt, baseline))
784 ttloop();
785 }
786 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
787 while (sequenceIs(environsubopt, baseline))
788 ttloop();
789 }
790 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
791 while (sequenceIs(oenvironsubopt, baseline))
792 ttloop();
793 }
794 if (his_state_is_will(TELOPT_TTYPE)) {
795 char first[256], last[256];
796
797 while (sequenceIs(ttypesubopt, baseline))
798 ttloop();
799
800 /*
801 * If the other side has already disabled the option, then
802 * we have to just go with what we (might) have already gotten.
803 */
804 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
805 (void) strncpy(first, terminaltype, sizeof(first) - 1);
806 first[sizeof(first) - 1] = '\0';
807 for(;;) {
808 /*
809 * Save the unknown name, and request the next name.
810 */
811 (void) strncpy(last, terminaltype, sizeof(last) - 1);
812 last[sizeof(last) - 1] = '\0';
813 _gettermname();
814 if (terminaltypeok(terminaltype))
815 break;
816 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
817 his_state_is_wont(TELOPT_TTYPE)) {
818 /*
819 * We've hit the end. If this is the same as
820 * the first name, just go with it.
821 */
822 if (strncmp(first, terminaltype, sizeof(first)) == 0)
823 break;
824 /*
825 * Get the terminal name one more time, so that
826 * RFC1091 compliant telnets will cycle back to
827 * the start of the list.
828 */
829 _gettermname();
830 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
831 (void) strncpy(terminaltype, first, sizeof(first) - 1);
832 terminaltype[sizeof(terminaltype) - 1] = '\0';
833 }
834 break;
835 }
836 }
837 }
838 }
839 return(retval);
840 } /* end of getterminaltype */
841
842 void
843 _gettermname()
844 {
845 /*
846 * If the client turned off the option,
847 * we can't send another request, so we
848 * just return.
849 */
850 if (his_state_is_wont(TELOPT_TTYPE))
851 return;
852 settimer(baseline);
853 memmove(nfrontp, ttytype_sbbuf, sizeof ttytype_sbbuf);
854 nfrontp += sizeof ttytype_sbbuf;
855 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
856 sizeof ttytype_sbbuf - 2););
857 while (sequenceIs(ttypesubopt, baseline))
858 ttloop();
859 }
860
861 int
862 terminaltypeok(s)
863 char *s;
864 {
865 char buf[1024];
866
867 if (terminaltype == NULL)
868 return(1);
869
870 /*
871 * tgetent() will return 1 if the type is known, and
872 * 0 if it is not known. If it returns -1, it couldn't
873 * open the database. But if we can't open the database,
874 * it won't help to say we failed, because we won't be
875 * able to verify anything else. So, we treat -1 like 1.
876 */
877 if (tgetent(buf, s) == 0)
878 return(0);
879 return(1);
880 }
881
882 #ifndef MAXHOSTNAMELEN
883 #define MAXHOSTNAMELEN 64
884 #endif /* MAXHOSTNAMELEN */
885
886 char *hostname;
887 char host_name[MAXHOSTNAMELEN + 1];
888 char remote_host_name[MAXHOSTNAMELEN + 1];
889
890 #ifndef convex
891 extern void telnet P((int, int));
892 #else
893 extern void telnet P((int, int, char *));
894 #endif
895
896 /*
897 * Get a pty, scan input lines.
898 */
899 void
900 doit(who)
901 struct sockaddr *who;
902 {
903 char *host;
904 int error;
905 int level;
906 int ptynum;
907 char user_name[256];
908
909 /*
910 * Find an available pty to use.
911 */
912 #ifndef convex
913 pty = getpty(&ptynum);
914 if (pty < 0)
915 fatal(net, "All network ports in use");
916 #else
917 for (;;) {
918 char *lp;
919
920 if ((lp = getpty()) == NULL)
921 fatal(net, "Out of ptys");
922
923 if ((pty = open(lp, 2)) >= 0) {
924 (void)strlcpy(line, lp, sizeof(NULL16STR));
925 line[5] = 't';
926 break;
927 }
928 }
929 #endif
930
931 #if defined(_SC_CRAY_SECURE_SYS)
932 /*
933 * set ttyp line security label
934 */
935 if (secflag) {
936 char slave_dev[16];
937
938 sprintf(tty_dev, "/dev/pty/%03d", ptynum);
939 if (setdevs(tty_dev, &dv) < 0)
940 fatal(net, "cannot set pty security");
941 sprintf(slave_dev, "/dev/ttyp%03d", ptynum);
942 if (setdevs(slave_dev, &dv) < 0)
943 fatal(net, "cannot set tty security");
944 }
945 #endif /* _SC_CRAY_SECURE_SYS */
946
947 /* get name of connected client */
948 error = getnameinfo(who, who->sa_len, remote_host_name,
949 sizeof(remote_host_name), NULL, 0, 0);
950
951 if (error) {
952 fatal(net, "Couldn't resolve your address into a host name.\r\n\
953 Please contact your net administrator");
954 #ifdef __GNUC__
955 host = NULL; /* XXX gcc */
956 #endif
957 }
958
959 remote_host_name[sizeof(remote_host_name)-1] = 0;
960 host = remote_host_name;
961
962 (void)gethostname(host_name, sizeof (host_name));
963 host_name[sizeof(host_name) - 1] = '\0';
964 hostname = host_name;
965
966 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
967 auth_encrypt_init(hostname, host, "TELNETD", 1);
968 #endif
969
970 init_env();
971 /*
972 * get terminal type.
973 */
974 *user_name = 0;
975 level = getterminaltype(user_name);
976 setenv("TERM", terminaltype ? terminaltype : "network", 1);
977
978 /*
979 * Start up the login process on the slave side of the terminal
980 */
981 #ifndef convex
982 startslave(host, level, user_name);
983
984 #if defined(_SC_CRAY_SECURE_SYS)
985 if (secflag) {
986 if (setulvl(dv.dv_actlvl) < 0)
987 fatal(net,"cannot setulvl()");
988 if (setucmp(dv.dv_actcmp) < 0)
989 fatal(net, "cannot setucmp()");
990 }
991 #endif /* _SC_CRAY_SECURE_SYS */
992
993 telnet(net, pty); /* begin server processing */
994 #else
995 telnet(net, pty, host);
996 #endif
997 /*NOTREACHED*/
998 } /* end of doit */
999
1000 #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
1001 int
1002 Xterm_output(ibufp, obuf, icountp, ocount)
1003 char **ibufp, *obuf;
1004 int *icountp, ocount;
1005 {
1006 int ret;
1007 ret = term_output(*ibufp, obuf, *icountp, ocount);
1008 *ibufp += *icountp;
1009 *icountp = 0;
1010 return(ret);
1011 }
1012 #define term_output Xterm_output
1013 #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
1014
1015 /*
1016 * Main loop. Select from pty and network, and
1017 * hand data to telnet receiver finite state machine.
1018 */
1019 void
1020 #ifndef convex
1021 telnet(f, p)
1022 #else
1023 telnet(f, p, host)
1024 #endif
1025 int f, p;
1026 #ifdef convex
1027 char *host;
1028 #endif
1029 {
1030 int on = 1;
1031 #define TABBUFSIZ 512
1032 char defent[TABBUFSIZ];
1033 char defstrs[TABBUFSIZ];
1034 #undef TABBUFSIZ
1035 char *HE, *HN, *IM, *IF, *ptyibuf2ptr;
1036 int nfd;
1037
1038 /*
1039 * Initialize the slc mapping table.
1040 */
1041 get_slc_defaults();
1042
1043 /*
1044 * Do some tests where it is desireable to wait for a response.
1045 * Rather than doing them slowly, one at a time, do them all
1046 * at once.
1047 */
1048 if (my_state_is_wont(TELOPT_SGA))
1049 send_will(TELOPT_SGA, 1);
1050 /*
1051 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
1052 * because 4.2 clients are unable to deal with TCP urgent data.
1053 *
1054 * To find out, we send out a "DO ECHO". If the remote system
1055 * answers "WILL ECHO" it is probably a 4.2 client, and we note
1056 * that fact ("WILL ECHO" ==> that the client will echo what
1057 * WE, the server, sends it; it does NOT mean that the client will
1058 * echo the terminal input).
1059 */
1060 send_do(TELOPT_ECHO, 1);
1061
1062 #ifdef LINEMODE
1063 if (his_state_is_wont(TELOPT_LINEMODE)) {
1064 /* Query the peer for linemode support by trying to negotiate
1065 * the linemode option.
1066 */
1067 linemode = 0;
1068 editmode = 0;
1069 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
1070 }
1071 #endif /* LINEMODE */
1072
1073 /*
1074 * Send along a couple of other options that we wish to negotiate.
1075 */
1076 send_do(TELOPT_NAWS, 1);
1077 send_will(TELOPT_STATUS, 1);
1078 flowmode = 1; /* default flow control state */
1079 restartany = -1; /* uninitialized... */
1080 send_do(TELOPT_LFLOW, 1);
1081
1082 /*
1083 * Spin, waiting for a response from the DO ECHO. However,
1084 * some REALLY DUMB telnets out there might not respond
1085 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
1086 * telnets so far seem to respond with WONT for a DO that
1087 * they don't understand...) because by the time we get the
1088 * response, it will already have processed the DO ECHO.
1089 * Kludge upon kludge.
1090 */
1091 while (his_will_wont_is_changing(TELOPT_NAWS))
1092 ttloop();
1093
1094 /*
1095 * But...
1096 * The client might have sent a WILL NAWS as part of its
1097 * startup code; if so, we'll be here before we get the
1098 * response to the DO ECHO. We'll make the assumption
1099 * that any implementation that understands about NAWS
1100 * is a modern enough implementation that it will respond
1101 * to our DO ECHO request; hence we'll do another spin
1102 * waiting for the ECHO option to settle down, which is
1103 * what we wanted to do in the first place...
1104 */
1105 if (his_want_state_is_will(TELOPT_ECHO) &&
1106 his_state_is_will(TELOPT_NAWS)) {
1107 while (his_will_wont_is_changing(TELOPT_ECHO))
1108 ttloop();
1109 }
1110 /*
1111 * On the off chance that the telnet client is broken and does not
1112 * respond to the DO ECHO we sent, (after all, we did send the
1113 * DO NAWS negotiation after the DO ECHO, and we won't get here
1114 * until a response to the DO NAWS comes back) simulate the
1115 * receipt of a will echo. This will also send a WONT ECHO
1116 * to the client, since we assume that the client failed to
1117 * respond because it believes that it is already in DO ECHO
1118 * mode, which we do not want.
1119 */
1120 if (his_want_state_is_will(TELOPT_ECHO)) {
1121 DIAG(TD_OPTIONS,
1122 {sprintf(nfrontp, "td: simulating recv\r\n");
1123 nfrontp += strlen(nfrontp);});
1124 willoption(TELOPT_ECHO);
1125 }
1126
1127 /*
1128 * Finally, to clean things up, we turn on our echo. This
1129 * will break stupid 4.2 telnets out of local terminal echo.
1130 */
1131
1132 if (my_state_is_wont(TELOPT_ECHO))
1133 send_will(TELOPT_ECHO, 1);
1134
1135 #ifndef STREAMSPTY
1136 /*
1137 * Turn on packet mode
1138 */
1139 (void) ioctl(p, TIOCPKT, (char *)&on);
1140 #endif
1141
1142 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1143 /*
1144 * Continuing line mode support. If client does not support
1145 * real linemode, attempt to negotiate kludge linemode by sending
1146 * the do timing mark sequence.
1147 */
1148 if (lmodetype < REAL_LINEMODE)
1149 send_do(TELOPT_TM, 1);
1150 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1151
1152 /*
1153 * Call telrcv() once to pick up anything received during
1154 * terminal type negotiation, 4.2/4.3 determination, and
1155 * linemode negotiation.
1156 */
1157 telrcv();
1158
1159 (void) ioctl(f, FIONBIO, (char *)&on);
1160 (void) ioctl(p, FIONBIO, (char *)&on);
1161 #if defined(CRAY2) && defined(UNICOS5)
1162 init_termdriver(f, p, interrupt, sendbrk);
1163 #endif
1164
1165 #if defined(SO_OOBINLINE)
1166 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
1167 (char *)&on, sizeof on);
1168 #endif /* defined(SO_OOBINLINE) */
1169
1170 #ifdef SIGTSTP
1171 (void) signal(SIGTSTP, SIG_IGN);
1172 #endif
1173 #ifdef SIGTTOU
1174 /*
1175 * Ignoring SIGTTOU keeps the kernel from blocking us
1176 * in ttioct() in /sys/tty.c.
1177 */
1178 (void) signal(SIGTTOU, SIG_IGN);
1179 #endif
1180
1181 (void) signal(SIGCHLD, cleanup);
1182
1183 #if defined(CRAY2) && defined(UNICOS5)
1184 /*
1185 * Cray-2 will send a signal when pty modes are changed by slave
1186 * side. Set up signal handler now.
1187 */
1188 if ((int)signal(SIGUSR1, termstat) < 0)
1189 perror("signal");
1190 else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0)
1191 perror("ioctl:TCSIGME");
1192 /*
1193 * Make processing loop check terminal characteristics early on.
1194 */
1195 termstat();
1196 #endif
1197
1198 #ifdef TIOCNOTTY
1199 {
1200 register int t;
1201 t = open(_PATH_TTY, O_RDWR);
1202 if (t >= 0) {
1203 (void) ioctl(t, TIOCNOTTY, (char *)0);
1204 (void) close(t);
1205 }
1206 }
1207 #endif
1208
1209 #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
1210 (void) setsid();
1211 ioctl(p, TIOCSCTTY, 0);
1212 #endif
1213
1214 /*
1215 * Show banner that getty never gave.
1216 *
1217 * We put the banner in the pty input buffer. This way, it
1218 * gets carriage return null processing, etc., just like all
1219 * other pty --> client data.
1220 */
1221
1222 #if !defined(CRAY) || !defined(NEWINIT)
1223 if (getenv("USER"))
1224 hostinfo = 0;
1225 #endif
1226
1227 if (getent(defent, gettyname) == 1) {
1228 char *cp=defstrs;
1229
1230 HE = getstr("he", &cp);
1231 HN = getstr("hn", &cp);
1232 IM = getstr("im", &cp);
1233 IF = getstr("if", &cp);
1234 if (HN && *HN)
1235 (void)strlcpy(host_name, HN, sizeof(host_name));
1236 if (IM == 0)
1237 IM = "";
1238 } else {
1239 IM = DEFAULT_IM;
1240 HE = 0;
1241 }
1242 edithost(HE, host_name);
1243 ptyibuf2ptr = ptyibuf2;
1244 if (hostinfo) {
1245 if (IF) {
1246 char buf[_POSIX2_LINE_MAX];
1247 FILE *fd;
1248
1249 if ((fd = fopen(IF, "r")) != NULL) {
1250 while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
1251 ptyibuf2ptr = putf(buf, ptyibuf2ptr);
1252 fclose(fd);
1253 }
1254 }
1255 if (*IM)
1256 ptyibuf2ptr = putf(IM, ptyibuf2ptr);
1257 }
1258
1259 if (pcc)
1260 strncpy(ptyibuf2ptr, ptyip, pcc+1);
1261 ptyip = ptyibuf2;
1262 pcc = strlen(ptyip);
1263 #ifdef LINEMODE
1264 /*
1265 * Last check to make sure all our states are correct.
1266 */
1267 init_termbuf();
1268 localstat();
1269 #endif /* LINEMODE */
1270
1271 DIAG(TD_REPORT,
1272 {sprintf(nfrontp, "td: Entering processing loop\r\n");
1273 nfrontp += strlen(nfrontp);});
1274
1275 #ifdef convex
1276 startslave(host);
1277 #endif
1278
1279 nfd = ((f > p) ? f : p) + 1;
1280 for (;;) {
1281 fd_set ibits, obits, xbits;
1282 register int c;
1283
1284 if (ncc < 0 && pcc < 0)
1285 break;
1286
1287 #if defined(CRAY2) && defined(UNICOS5)
1288 if (needtermstat)
1289 _termstat();
1290 #endif /* defined(CRAY2) && defined(UNICOS5) */
1291 FD_ZERO(&ibits);
1292 FD_ZERO(&obits);
1293 FD_ZERO(&xbits);
1294 /*
1295 * Never look for input if there's still
1296 * stuff in the corresponding output buffer
1297 */
1298 if (nfrontp - nbackp || pcc > 0) {
1299 FD_SET(f, &obits);
1300 } else {
1301 FD_SET(p, &ibits);
1302 }
1303 if (pfrontp - pbackp || ncc > 0) {
1304 FD_SET(p, &obits);
1305 } else {
1306 FD_SET(f, &ibits);
1307 }
1308 if (!SYNCHing) {
1309 FD_SET(f, &xbits);
1310 }
1311 if ((c = select(nfd, &ibits, &obits, &xbits,
1312 (struct timeval *)0)) < 1) {
1313 if (c == -1) {
1314 if (errno == EINTR) {
1315 continue;
1316 }
1317 }
1318 sleep(5);
1319 continue;
1320 }
1321
1322 /*
1323 * Any urgent data?
1324 */
1325 if (FD_ISSET(net, &xbits)) {
1326 SYNCHing = 1;
1327 }
1328
1329 /*
1330 * Something to read from the network...
1331 */
1332 if (FD_ISSET(net, &ibits)) {
1333 #if !defined(SO_OOBINLINE)
1334 /*
1335 * In 4.2 (and 4.3 beta) systems, the
1336 * OOB indication and data handling in the kernel
1337 * is such that if two separate TCP Urgent requests
1338 * come in, one byte of TCP data will be overlaid.
1339 * This is fatal for Telnet, but we try to live
1340 * with it.
1341 *
1342 * In addition, in 4.2 (and...), a special protocol
1343 * is needed to pick up the TCP Urgent data in
1344 * the correct sequence.
1345 *
1346 * What we do is: if we think we are in urgent
1347 * mode, we look to see if we are "at the mark".
1348 * If we are, we do an OOB receive. If we run
1349 * this twice, we will do the OOB receive twice,
1350 * but the second will fail, since the second
1351 * time we were "at the mark", but there wasn't
1352 * any data there (the kernel doesn't reset
1353 * "at the mark" until we do a normal read).
1354 * Once we've read the OOB data, we go ahead
1355 * and do normal reads.
1356 *
1357 * There is also another problem, which is that
1358 * since the OOB byte we read doesn't put us
1359 * out of OOB state, and since that byte is most
1360 * likely the TELNET DM (data mark), we would
1361 * stay in the TELNET SYNCH (SYNCHing) state.
1362 * So, clocks to the rescue. If we've "just"
1363 * received a DM, then we test for the
1364 * presence of OOB data when the receive OOB
1365 * fails (and AFTER we did the normal mode read
1366 * to clear "at the mark").
1367 */
1368 if (SYNCHing) {
1369 int atmark;
1370
1371 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1372 if (atmark) {
1373 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1374 if ((ncc == -1) && (errno == EINVAL)) {
1375 ncc = read(net, netibuf, sizeof (netibuf));
1376 if (sequenceIs(didnetreceive, gotDM)) {
1377 SYNCHing = stilloob(net);
1378 }
1379 }
1380 } else {
1381 ncc = read(net, netibuf, sizeof (netibuf));
1382 }
1383 } else {
1384 ncc = read(net, netibuf, sizeof (netibuf));
1385 }
1386 settimer(didnetreceive);
1387 #else /* !defined(SO_OOBINLINE)) */
1388 ncc = read(net, netibuf, sizeof (netibuf));
1389 #endif /* !defined(SO_OOBINLINE)) */
1390 if (ncc < 0 && errno == EWOULDBLOCK)
1391 ncc = 0;
1392 else {
1393 if (ncc <= 0) {
1394 break;
1395 }
1396 netip = netibuf;
1397 }
1398 DIAG((TD_REPORT | TD_NETDATA),
1399 {sprintf(nfrontp, "td: netread %d chars\r\n", ncc);
1400 nfrontp += strlen(nfrontp);});
1401 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1402 }
1403
1404 /*
1405 * Something to read from the pty...
1406 */
1407 if (FD_ISSET(p, &ibits)) {
1408 #ifndef STREAMSPTY
1409 pcc = read(p, ptyibuf, BUFSIZ);
1410 #else
1411 pcc = readstream(p, ptyibuf, BUFSIZ);
1412 #endif
1413 /*
1414 * On some systems, if we try to read something
1415 * off the master side before the slave side is
1416 * opened, we get EIO.
1417 */
1418 if (pcc < 0 && (errno == EWOULDBLOCK ||
1419 #ifdef EAGAIN
1420 errno == EAGAIN ||
1421 #endif
1422 errno == EIO)) {
1423 pcc = 0;
1424 } else {
1425 if (pcc <= 0)
1426 break;
1427 #if !defined(CRAY2) || !defined(UNICOS5)
1428 #ifdef LINEMODE
1429 /*
1430 * If ioctl from pty, pass it through net
1431 */
1432 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1433 copy_termbuf(ptyibuf+1, pcc-1);
1434 localstat();
1435 pcc = 1;
1436 }
1437 #endif /* LINEMODE */
1438 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1439 netclear(); /* clear buffer back */
1440 #ifndef NO_URGENT
1441 /*
1442 * There are client telnets on some
1443 * operating systems get screwed up
1444 * royally if we send them urgent
1445 * mode data.
1446 */
1447 *nfrontp++ = IAC;
1448 *nfrontp++ = DM;
1449 neturg = nfrontp-1; /* off by one XXX */
1450 DIAG(TD_OPTIONS,
1451 printoption("td: send IAC", DM));
1452
1453 #endif
1454 }
1455 if (his_state_is_will(TELOPT_LFLOW) &&
1456 (ptyibuf[0] &
1457 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1458 int newflow =
1459 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1460 if (newflow != flowmode) {
1461 flowmode = newflow;
1462 (void) sprintf(nfrontp,
1463 "%c%c%c%c%c%c",
1464 IAC, SB, TELOPT_LFLOW,
1465 flowmode ? LFLOW_ON
1466 : LFLOW_OFF,
1467 IAC, SE);
1468 nfrontp += 6;
1469 DIAG(TD_OPTIONS, printsub('>',
1470 (unsigned char *)nfrontp-4,
1471 4););
1472 }
1473 }
1474 pcc--;
1475 ptyip = ptyibuf+1;
1476 #else /* defined(CRAY2) && defined(UNICOS5) */
1477 if (!uselinemode) {
1478 unpcc = pcc;
1479 unptyip = ptyibuf;
1480 pcc = term_output(&unptyip, ptyibuf2,
1481 &unpcc, BUFSIZ);
1482 ptyip = ptyibuf2;
1483 } else
1484 ptyip = ptyibuf;
1485 #endif /* defined(CRAY2) && defined(UNICOS5) */
1486 }
1487 }
1488
1489 while (pcc > 0) {
1490 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1491 break;
1492 c = *ptyip++ & 0377, pcc--;
1493 if (c == IAC)
1494 *nfrontp++ = c;
1495 #if defined(CRAY2) && defined(UNICOS5)
1496 else if (c == '\n' &&
1497 my_state_is_wont(TELOPT_BINARY) && newmap)
1498 *nfrontp++ = '\r';
1499 #endif /* defined(CRAY2) && defined(UNICOS5) */
1500 *nfrontp++ = c;
1501 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1502 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1503 *nfrontp++ = *ptyip++ & 0377;
1504 pcc--;
1505 } else
1506 *nfrontp++ = '\0';
1507 }
1508 }
1509 #if defined(CRAY2) && defined(UNICOS5)
1510 /*
1511 * If chars were left over from the terminal driver,
1512 * note their existence.
1513 */
1514 if (!uselinemode && unpcc) {
1515 pcc = unpcc;
1516 unpcc = 0;
1517 ptyip = unptyip;
1518 }
1519 #endif /* defined(CRAY2) && defined(UNICOS5) */
1520
1521 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1522 netflush();
1523 if (ncc > 0)
1524 telrcv();
1525 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1526 ptyflush();
1527 }
1528 cleanup(0);
1529 } /* end of telnet */
1530
1531 #ifndef TCSIG
1532 # ifdef TIOCSIG
1533 # define TCSIG TIOCSIG
1534 # endif
1535 #endif
1536
1537 #ifdef STREAMSPTY
1538
1539 int flowison = -1; /* current state of flow: -1 is unknown */
1540
1541 int readstream(p, ibuf, bufsize)
1542 int p;
1543 char *ibuf;
1544 int bufsize;
1545 {
1546 int flags = 0;
1547 int ret = 0;
1548 struct termios *tsp;
1549 struct termio *tp;
1550 struct iocblk *ip;
1551 char vstop, vstart;
1552 int ixon;
1553 int newflow;
1554
1555 strbufc.maxlen = BUFSIZ;
1556 strbufc.buf = (char *)ctlbuf;
1557 strbufd.maxlen = bufsize-1;
1558 strbufd.len = 0;
1559 strbufd.buf = ibuf+1;
1560 ibuf[0] = 0;
1561
1562 ret = getmsg(p, &strbufc, &strbufd, &flags);
1563 if (ret < 0) /* error of some sort -- probably EAGAIN */
1564 return(-1);
1565
1566 if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
1567 /* data message */
1568 if (strbufd.len > 0) { /* real data */
1569 return(strbufd.len + 1); /* count header char */
1570 } else {
1571 /* nothing there */
1572 errno = EAGAIN;
1573 return(-1);
1574 }
1575 }
1576
1577 /*
1578 * It's a control message. Return 1, to look at the flag we set
1579 */
1580
1581 switch (ctlbuf[0]) {
1582 case M_FLUSH:
1583 if (ibuf[1] & FLUSHW)
1584 ibuf[0] = TIOCPKT_FLUSHWRITE;
1585 return(1);
1586
1587 case M_IOCTL:
1588 ip = (struct iocblk *) (ibuf+1);
1589
1590 switch (ip->ioc_cmd) {
1591 case TCSETS:
1592 case TCSETSW:
1593 case TCSETSF:
1594 tsp = (struct termios *)
1595 (ibuf+1 + sizeof(struct iocblk));
1596 vstop = tsp->c_cc[VSTOP];
1597 vstart = tsp->c_cc[VSTART];
1598 ixon = tsp->c_iflag & IXON;
1599 break;
1600 case TCSETA:
1601 case TCSETAW:
1602 case TCSETAF:
1603 tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
1604 vstop = tp->c_cc[VSTOP];
1605 vstart = tp->c_cc[VSTART];
1606 ixon = tp->c_iflag & IXON;
1607 break;
1608 default:
1609 errno = EAGAIN;
1610 return(-1);
1611 }
1612
1613 newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
1614 if (newflow != flowison) { /* it's a change */
1615 flowison = newflow;
1616 ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
1617 return(1);
1618 }
1619 }
1620
1621 /* nothing worth doing anything about */
1622 errno = EAGAIN;
1623 return(-1);
1624 }
1625 #endif /* STREAMSPTY */
1626
1627 /*
1628 * Send interrupt to process on other side of pty.
1629 * If it is in raw mode, just write NULL;
1630 * otherwise, write intr char.
1631 */
1632 void
1633 interrupt()
1634 {
1635 ptyflush(); /* half-hearted */
1636
1637 #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1638 /* Streams PTY style ioctl to post a signal */
1639 {
1640 int sig = SIGINT;
1641 (void) ioctl(pty, TIOCSIGNAL, &sig);
1642 (void) ioctl(pty, I_FLUSH, FLUSHR);
1643 }
1644 #else
1645 #ifdef TCSIG
1646 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1647 #else /* TCSIG */
1648 init_termbuf();
1649 *pfrontp++ = slctab[SLC_IP].sptr ?
1650 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1651 #endif /* TCSIG */
1652 #endif
1653 }
1654
1655 /*
1656 * Send quit to process on other side of pty.
1657 * If it is in raw mode, just write NULL;
1658 * otherwise, write quit char.
1659 */
1660 void
1661 sendbrk()
1662 {
1663 ptyflush(); /* half-hearted */
1664 #ifdef TCSIG
1665 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1666 #else /* TCSIG */
1667 init_termbuf();
1668 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1669 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1670 #endif /* TCSIG */
1671 }
1672
1673 void
1674 sendsusp()
1675 {
1676 #ifdef SIGTSTP
1677 ptyflush(); /* half-hearted */
1678 # ifdef TCSIG
1679 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1680 # else /* TCSIG */
1681 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1682 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1683 # endif /* TCSIG */
1684 #endif /* SIGTSTP */
1685 }
1686
1687 /*
1688 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1689 * just send back "[Yes]".
1690 */
1691 void
1692 recv_ayt()
1693 {
1694 #if defined(SIGINFO) && defined(TCSIG)
1695 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1696 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1697 return;
1698 }
1699 #endif
1700 (void) strcpy(nfrontp, "\r\n[Yes]\r\n");
1701 nfrontp += 9;
1702 }
1703
1704 void
1705 doeof()
1706 {
1707 init_termbuf();
1708
1709 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1710 if (!tty_isediting()) {
1711 extern char oldeofc;
1712 *pfrontp++ = oldeofc;
1713 return;
1714 }
1715 #endif
1716 *pfrontp++ = slctab[SLC_EOF].sptr ?
1717 (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1718 }
1719