telnetd.c revision 1.27 1 /* $NetBSD: telnetd.c,v 1.27 2001/08/20 11:01:48 wiz 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.27 2001/08/20 11:01:48 wiz 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((const 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 output_datalen((const char *)sb, sizeof sb);
735 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
736 }
737 #ifdef ENCRYPTION
738 /*
739 * Wait for the negotiation of what type of encryption we can
740 * send with. If autoencrypt is not set, this will just return.
741 */
742 if (his_state_is_will(TELOPT_ENCRYPT)) {
743 encrypt_wait();
744 }
745 #endif /* ENCRYPTION */
746 if (his_state_is_will(TELOPT_XDISPLOC)) {
747 static unsigned char sb[] =
748 { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
749
750 output_datalen((const char *)sb, sizeof sb);
751 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
752 }
753 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
754 static unsigned char sb[] =
755 { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
756
757 output_datalen((const char *)sb, sizeof sb);
758 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
759 }
760 else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
761 static unsigned char sb[] =
762 { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
763
764 output_datalen((const char *)sb, sizeof sb);
765 DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
766 }
767 if (his_state_is_will(TELOPT_TTYPE)) {
768
769 output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
770 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
771 sizeof ttytype_sbbuf - 2););
772 }
773 if (his_state_is_will(TELOPT_TSPEED)) {
774 while (sequenceIs(tspeedsubopt, baseline))
775 ttloop();
776 }
777 if (his_state_is_will(TELOPT_XDISPLOC)) {
778 while (sequenceIs(xdisplocsubopt, baseline))
779 ttloop();
780 }
781 if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
782 while (sequenceIs(environsubopt, baseline))
783 ttloop();
784 }
785 if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
786 while (sequenceIs(oenvironsubopt, baseline))
787 ttloop();
788 }
789 if (his_state_is_will(TELOPT_TTYPE)) {
790 char first[256], last[256];
791
792 while (sequenceIs(ttypesubopt, baseline))
793 ttloop();
794
795 /*
796 * If the other side has already disabled the option, then
797 * we have to just go with what we (might) have already gotten.
798 */
799 if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
800 (void) strncpy(first, terminaltype, sizeof(first) - 1);
801 first[sizeof(first) - 1] = '\0';
802 for(;;) {
803 /*
804 * Save the unknown name, and request the next name.
805 */
806 (void) strncpy(last, terminaltype, sizeof(last) - 1);
807 last[sizeof(last) - 1] = '\0';
808 _gettermname();
809 if (terminaltypeok(terminaltype))
810 break;
811 if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
812 his_state_is_wont(TELOPT_TTYPE)) {
813 /*
814 * We've hit the end. If this is the same as
815 * the first name, just go with it.
816 */
817 if (strncmp(first, terminaltype, sizeof(first)) == 0)
818 break;
819 /*
820 * Get the terminal name one more time, so that
821 * RFC1091 compliant telnets will cycle back to
822 * the start of the list.
823 */
824 _gettermname();
825 if (strncmp(first, terminaltype, sizeof(first)) != 0) {
826 (void) strncpy(terminaltype, first, sizeof(first) - 1);
827 terminaltype[sizeof(terminaltype) - 1] = '\0';
828 }
829 break;
830 }
831 }
832 }
833 }
834 return(retval);
835 } /* end of getterminaltype */
836
837 void
838 _gettermname()
839 {
840 /*
841 * If the client turned off the option,
842 * we can't send another request, so we
843 * just return.
844 */
845 if (his_state_is_wont(TELOPT_TTYPE))
846 return;
847 settimer(baseline);
848 output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
849 DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
850 sizeof ttytype_sbbuf - 2););
851 while (sequenceIs(ttypesubopt, baseline))
852 ttloop();
853 }
854
855 int
856 terminaltypeok(s)
857 char *s;
858 {
859 char buf[1024];
860
861 if (terminaltype == NULL)
862 return(1);
863
864 /*
865 * tgetent() will return 1 if the type is known, and
866 * 0 if it is not known. If it returns -1, it couldn't
867 * open the database. But if we can't open the database,
868 * it won't help to say we failed, because we won't be
869 * able to verify anything else. So, we treat -1 like 1.
870 */
871 if (tgetent(buf, s) == 0)
872 return(0);
873 return(1);
874 }
875
876 #ifndef MAXHOSTNAMELEN
877 #define MAXHOSTNAMELEN 64
878 #endif /* MAXHOSTNAMELEN */
879
880 char *hostname;
881 char host_name[MAXHOSTNAMELEN + 1];
882 char remote_host_name[MAXHOSTNAMELEN + 1];
883
884 #ifndef convex
885 extern void telnet P((int, int));
886 #else
887 extern void telnet P((int, int, char *));
888 #endif
889
890 /*
891 * Get a pty, scan input lines.
892 */
893 void
894 doit(who)
895 struct sockaddr *who;
896 {
897 char *host;
898 int error;
899 int level;
900 int ptynum;
901 char user_name[256];
902
903 /*
904 * Find an available pty to use.
905 */
906 #ifndef convex
907 pty = getpty(&ptynum);
908 if (pty < 0)
909 fatal(net, "All network ports in use");
910 #else
911 for (;;) {
912 char *lp;
913
914 if ((lp = getpty()) == NULL)
915 fatal(net, "Out of ptys");
916
917 if ((pty = open(lp, 2)) >= 0) {
918 (void)strlcpy(line, lp, sizeof(NULL16STR));
919 line[5] = 't';
920 break;
921 }
922 }
923 #endif
924
925 #if defined(_SC_CRAY_SECURE_SYS)
926 /*
927 * set ttyp line security label
928 */
929 if (secflag) {
930 char slave_dev[16];
931
932 sprintf(tty_dev, "/dev/pty/%03d", ptynum);
933 if (setdevs(tty_dev, &dv) < 0)
934 fatal(net, "cannot set pty security");
935 sprintf(slave_dev, "/dev/ttyp%03d", ptynum);
936 if (setdevs(slave_dev, &dv) < 0)
937 fatal(net, "cannot set tty security");
938 }
939 #endif /* _SC_CRAY_SECURE_SYS */
940
941 /* get name of connected client */
942 error = getnameinfo(who, who->sa_len, remote_host_name,
943 sizeof(remote_host_name), NULL, 0, 0);
944
945 if (error) {
946 fatal(net, "Couldn't resolve your address into a host name.\r\n\
947 Please contact your net administrator");
948 #ifdef __GNUC__
949 host = NULL; /* XXX gcc */
950 #endif
951 }
952
953 remote_host_name[sizeof(remote_host_name)-1] = 0;
954 host = remote_host_name;
955
956 (void)gethostname(host_name, sizeof (host_name));
957 host_name[sizeof(host_name) - 1] = '\0';
958 hostname = host_name;
959
960 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
961 auth_encrypt_init(hostname, host, "TELNETD", 1);
962 #endif
963
964 init_env();
965 /*
966 * get terminal type.
967 */
968 *user_name = 0;
969 level = getterminaltype(user_name);
970 setenv("TERM", terminaltype ? terminaltype : "network", 1);
971
972 /*
973 * Start up the login process on the slave side of the terminal
974 */
975 #ifndef convex
976 startslave(host, level, user_name);
977
978 #if defined(_SC_CRAY_SECURE_SYS)
979 if (secflag) {
980 if (setulvl(dv.dv_actlvl) < 0)
981 fatal(net,"cannot setulvl()");
982 if (setucmp(dv.dv_actcmp) < 0)
983 fatal(net, "cannot setucmp()");
984 }
985 #endif /* _SC_CRAY_SECURE_SYS */
986
987 telnet(net, pty); /* begin server processing */
988 #else
989 telnet(net, pty, host);
990 #endif
991 /*NOTREACHED*/
992 } /* end of doit */
993
994 #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50)
995 int
996 Xterm_output(ibufp, obuf, icountp, ocount)
997 char **ibufp, *obuf;
998 int *icountp, ocount;
999 {
1000 int ret;
1001 ret = term_output(*ibufp, obuf, *icountp, ocount);
1002 *ibufp += *icountp;
1003 *icountp = 0;
1004 return(ret);
1005 }
1006 #define term_output Xterm_output
1007 #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */
1008
1009 /*
1010 * Main loop. Select from pty and network, and
1011 * hand data to telnet receiver finite state machine.
1012 */
1013 void
1014 #ifndef convex
1015 telnet(f, p)
1016 #else
1017 telnet(f, p, host)
1018 #endif
1019 int f, p;
1020 #ifdef convex
1021 char *host;
1022 #endif
1023 {
1024 int on = 1;
1025 #define TABBUFSIZ 512
1026 char defent[TABBUFSIZ];
1027 char defstrs[TABBUFSIZ];
1028 #undef TABBUFSIZ
1029 char *HE, *HN, *IM, *IF, *ptyibuf2ptr;
1030 int nfd;
1031
1032 /*
1033 * Initialize the slc mapping table.
1034 */
1035 get_slc_defaults();
1036
1037 /*
1038 * Do some tests where it is desireable to wait for a response.
1039 * Rather than doing them slowly, one at a time, do them all
1040 * at once.
1041 */
1042 if (my_state_is_wont(TELOPT_SGA))
1043 send_will(TELOPT_SGA, 1);
1044 /*
1045 * Is the client side a 4.2 (NOT 4.3) system? We need to know this
1046 * because 4.2 clients are unable to deal with TCP urgent data.
1047 *
1048 * To find out, we send out a "DO ECHO". If the remote system
1049 * answers "WILL ECHO" it is probably a 4.2 client, and we note
1050 * that fact ("WILL ECHO" ==> that the client will echo what
1051 * WE, the server, sends it; it does NOT mean that the client will
1052 * echo the terminal input).
1053 */
1054 send_do(TELOPT_ECHO, 1);
1055
1056 #ifdef LINEMODE
1057 if (his_state_is_wont(TELOPT_LINEMODE)) {
1058 /* Query the peer for linemode support by trying to negotiate
1059 * the linemode option.
1060 */
1061 linemode = 0;
1062 editmode = 0;
1063 send_do(TELOPT_LINEMODE, 1); /* send do linemode */
1064 }
1065 #endif /* LINEMODE */
1066
1067 /*
1068 * Send along a couple of other options that we wish to negotiate.
1069 */
1070 send_do(TELOPT_NAWS, 1);
1071 send_will(TELOPT_STATUS, 1);
1072 flowmode = 1; /* default flow control state */
1073 restartany = -1; /* uninitialized... */
1074 send_do(TELOPT_LFLOW, 1);
1075
1076 /*
1077 * Spin, waiting for a response from the DO ECHO. However,
1078 * some REALLY DUMB telnets out there might not respond
1079 * to the DO ECHO. So, we spin looking for NAWS, (most dumb
1080 * telnets so far seem to respond with WONT for a DO that
1081 * they don't understand...) because by the time we get the
1082 * response, it will already have processed the DO ECHO.
1083 * Kludge upon kludge.
1084 */
1085 while (his_will_wont_is_changing(TELOPT_NAWS))
1086 ttloop();
1087
1088 /*
1089 * But...
1090 * The client might have sent a WILL NAWS as part of its
1091 * startup code; if so, we'll be here before we get the
1092 * response to the DO ECHO. We'll make the assumption
1093 * that any implementation that understands about NAWS
1094 * is a modern enough implementation that it will respond
1095 * to our DO ECHO request; hence we'll do another spin
1096 * waiting for the ECHO option to settle down, which is
1097 * what we wanted to do in the first place...
1098 */
1099 if (his_want_state_is_will(TELOPT_ECHO) &&
1100 his_state_is_will(TELOPT_NAWS)) {
1101 while (his_will_wont_is_changing(TELOPT_ECHO))
1102 ttloop();
1103 }
1104 /*
1105 * On the off chance that the telnet client is broken and does not
1106 * respond to the DO ECHO we sent, (after all, we did send the
1107 * DO NAWS negotiation after the DO ECHO, and we won't get here
1108 * until a response to the DO NAWS comes back) simulate the
1109 * receipt of a will echo. This will also send a WONT ECHO
1110 * to the client, since we assume that the client failed to
1111 * respond because it believes that it is already in DO ECHO
1112 * mode, which we do not want.
1113 */
1114 if (his_want_state_is_will(TELOPT_ECHO)) {
1115 DIAG(TD_OPTIONS,
1116 {output_data("td: simulating recv\r\n");});
1117 willoption(TELOPT_ECHO);
1118 }
1119
1120 /*
1121 * Finally, to clean things up, we turn on our echo. This
1122 * will break stupid 4.2 telnets out of local terminal echo.
1123 */
1124
1125 if (my_state_is_wont(TELOPT_ECHO))
1126 send_will(TELOPT_ECHO, 1);
1127
1128 #ifndef STREAMSPTY
1129 /*
1130 * Turn on packet mode
1131 */
1132 (void) ioctl(p, TIOCPKT, (char *)&on);
1133 #endif
1134
1135 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1136 /*
1137 * Continuing line mode support. If client does not support
1138 * real linemode, attempt to negotiate kludge linemode by sending
1139 * the do timing mark sequence.
1140 */
1141 if (lmodetype < REAL_LINEMODE)
1142 send_do(TELOPT_TM, 1);
1143 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1144
1145 /*
1146 * Call telrcv() once to pick up anything received during
1147 * terminal type negotiation, 4.2/4.3 determination, and
1148 * linemode negotiation.
1149 */
1150 telrcv();
1151
1152 (void) ioctl(f, FIONBIO, (char *)&on);
1153 (void) ioctl(p, FIONBIO, (char *)&on);
1154 #if defined(CRAY2) && defined(UNICOS5)
1155 init_termdriver(f, p, interrupt, sendbrk);
1156 #endif
1157
1158 #if defined(SO_OOBINLINE)
1159 (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
1160 (char *)&on, sizeof on);
1161 #endif /* defined(SO_OOBINLINE) */
1162
1163 #ifdef SIGTSTP
1164 (void) signal(SIGTSTP, SIG_IGN);
1165 #endif
1166 #ifdef SIGTTOU
1167 /*
1168 * Ignoring SIGTTOU keeps the kernel from blocking us
1169 * in ttioct() in /sys/tty.c.
1170 */
1171 (void) signal(SIGTTOU, SIG_IGN);
1172 #endif
1173
1174 (void) signal(SIGCHLD, cleanup);
1175
1176 #if defined(CRAY2) && defined(UNICOS5)
1177 /*
1178 * Cray-2 will send a signal when pty modes are changed by slave
1179 * side. Set up signal handler now.
1180 */
1181 if ((int)signal(SIGUSR1, termstat) < 0)
1182 perror("signal");
1183 else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0)
1184 perror("ioctl:TCSIGME");
1185 /*
1186 * Make processing loop check terminal characteristics early on.
1187 */
1188 termstat();
1189 #endif
1190
1191 #ifdef TIOCNOTTY
1192 {
1193 register int t;
1194 t = open(_PATH_TTY, O_RDWR);
1195 if (t >= 0) {
1196 (void) ioctl(t, TIOCNOTTY, (char *)0);
1197 (void) close(t);
1198 }
1199 }
1200 #endif
1201
1202 #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY)
1203 (void) setsid();
1204 ioctl(p, TIOCSCTTY, 0);
1205 #endif
1206
1207 /*
1208 * Show banner that getty never gave.
1209 *
1210 * We put the banner in the pty input buffer. This way, it
1211 * gets carriage return null processing, etc., just like all
1212 * other pty --> client data.
1213 */
1214
1215 #if !defined(CRAY) || !defined(NEWINIT)
1216 if (getenv("USER"))
1217 hostinfo = 0;
1218 #endif
1219
1220 if (getent(defent, gettyname) == 1) {
1221 char *cp=defstrs;
1222
1223 HE = getstr("he", &cp);
1224 HN = getstr("hn", &cp);
1225 IM = getstr("im", &cp);
1226 IF = getstr("if", &cp);
1227 if (HN && *HN)
1228 (void)strlcpy(host_name, HN, sizeof(host_name));
1229 if (IM == 0)
1230 IM = "";
1231 } else {
1232 IM = DEFAULT_IM;
1233 HE = 0;
1234 }
1235 edithost(HE, host_name);
1236 ptyibuf2ptr = ptyibuf2;
1237 if (hostinfo) {
1238 if (IF) {
1239 char buf[_POSIX2_LINE_MAX];
1240 FILE *fd;
1241
1242 if ((fd = fopen(IF, "r")) != NULL) {
1243 while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
1244 ptyibuf2ptr = putf(buf, ptyibuf2ptr);
1245 fclose(fd);
1246 }
1247 }
1248 if (*IM)
1249 ptyibuf2ptr = putf(IM, ptyibuf2ptr);
1250 }
1251
1252 if (pcc)
1253 strncpy(ptyibuf2ptr, ptyip, pcc+1);
1254 ptyip = ptyibuf2;
1255 pcc = strlen(ptyip);
1256 #ifdef LINEMODE
1257 /*
1258 * Last check to make sure all our states are correct.
1259 */
1260 init_termbuf();
1261 localstat();
1262 #endif /* LINEMODE */
1263
1264 DIAG(TD_REPORT,
1265 {output_data("td: Entering processing loop\r\n");});
1266
1267 #ifdef convex
1268 startslave(host);
1269 #endif
1270
1271 nfd = ((f > p) ? f : p) + 1;
1272 for (;;) {
1273 fd_set ibits, obits, xbits;
1274 register int c;
1275
1276 if (ncc < 0 && pcc < 0)
1277 break;
1278
1279 #if defined(CRAY2) && defined(UNICOS5)
1280 if (needtermstat)
1281 _termstat();
1282 #endif /* defined(CRAY2) && defined(UNICOS5) */
1283 FD_ZERO(&ibits);
1284 FD_ZERO(&obits);
1285 FD_ZERO(&xbits);
1286
1287 if (f >= FD_SETSIZE)
1288 fatal(net, "fd too large");
1289 if (p >= FD_SETSIZE)
1290 fatal(net, "fd too large");
1291
1292 /*
1293 * Never look for input if there's still
1294 * stuff in the corresponding output buffer
1295 */
1296 if (nfrontp - nbackp || pcc > 0) {
1297 FD_SET(f, &obits);
1298 } else {
1299 FD_SET(p, &ibits);
1300 }
1301 if (pfrontp - pbackp || ncc > 0) {
1302 FD_SET(p, &obits);
1303 } else {
1304 FD_SET(f, &ibits);
1305 }
1306 if (!SYNCHing) {
1307 FD_SET(f, &xbits);
1308 }
1309 if ((c = select(nfd, &ibits, &obits, &xbits,
1310 (struct timeval *)0)) < 1) {
1311 if (c == -1) {
1312 if (errno == EINTR) {
1313 continue;
1314 }
1315 }
1316 sleep(5);
1317 continue;
1318 }
1319
1320 /*
1321 * Any urgent data?
1322 */
1323 if (FD_ISSET(net, &xbits)) {
1324 SYNCHing = 1;
1325 }
1326
1327 /*
1328 * Something to read from the network...
1329 */
1330 if (FD_ISSET(net, &ibits)) {
1331 #if !defined(SO_OOBINLINE)
1332 /*
1333 * In 4.2 (and 4.3 beta) systems, the
1334 * OOB indication and data handling in the kernel
1335 * is such that if two separate TCP Urgent requests
1336 * come in, one byte of TCP data will be overlaid.
1337 * This is fatal for Telnet, but we try to live
1338 * with it.
1339 *
1340 * In addition, in 4.2 (and...), a special protocol
1341 * is needed to pick up the TCP Urgent data in
1342 * the correct sequence.
1343 *
1344 * What we do is: if we think we are in urgent
1345 * mode, we look to see if we are "at the mark".
1346 * If we are, we do an OOB receive. If we run
1347 * this twice, we will do the OOB receive twice,
1348 * but the second will fail, since the second
1349 * time we were "at the mark", but there wasn't
1350 * any data there (the kernel doesn't reset
1351 * "at the mark" until we do a normal read).
1352 * Once we've read the OOB data, we go ahead
1353 * and do normal reads.
1354 *
1355 * There is also another problem, which is that
1356 * since the OOB byte we read doesn't put us
1357 * out of OOB state, and since that byte is most
1358 * likely the TELNET DM (data mark), we would
1359 * stay in the TELNET SYNCH (SYNCHing) state.
1360 * So, clocks to the rescue. If we've "just"
1361 * received a DM, then we test for the
1362 * presence of OOB data when the receive OOB
1363 * fails (and AFTER we did the normal mode read
1364 * to clear "at the mark").
1365 */
1366 if (SYNCHing) {
1367 int atmark;
1368
1369 (void) ioctl(net, SIOCATMARK, (char *)&atmark);
1370 if (atmark) {
1371 ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
1372 if ((ncc == -1) && (errno == EINVAL)) {
1373 ncc = read(net, netibuf, sizeof (netibuf));
1374 if (sequenceIs(didnetreceive, gotDM)) {
1375 SYNCHing = stilloob(net);
1376 }
1377 }
1378 } else {
1379 ncc = read(net, netibuf, sizeof (netibuf));
1380 }
1381 } else {
1382 ncc = read(net, netibuf, sizeof (netibuf));
1383 }
1384 settimer(didnetreceive);
1385 #else /* !defined(SO_OOBINLINE)) */
1386 ncc = read(net, netibuf, sizeof (netibuf));
1387 #endif /* !defined(SO_OOBINLINE)) */
1388 if (ncc < 0 && errno == EWOULDBLOCK)
1389 ncc = 0;
1390 else {
1391 if (ncc <= 0) {
1392 break;
1393 }
1394 netip = netibuf;
1395 }
1396 DIAG((TD_REPORT | TD_NETDATA),
1397 {output_data("td: netread %d chars\r\n", ncc);});
1398 DIAG(TD_NETDATA, printdata("nd", netip, ncc));
1399 }
1400
1401 /*
1402 * Something to read from the pty...
1403 */
1404 if (FD_ISSET(p, &ibits)) {
1405 #ifndef STREAMSPTY
1406 pcc = read(p, ptyibuf, BUFSIZ);
1407 #else
1408 pcc = readstream(p, ptyibuf, BUFSIZ);
1409 #endif
1410 /*
1411 * On some systems, if we try to read something
1412 * off the master side before the slave side is
1413 * opened, we get EIO.
1414 */
1415 if (pcc < 0 && (errno == EWOULDBLOCK ||
1416 #ifdef EAGAIN
1417 errno == EAGAIN ||
1418 #endif
1419 errno == EIO)) {
1420 pcc = 0;
1421 } else {
1422 if (pcc <= 0)
1423 break;
1424 #if !defined(CRAY2) || !defined(UNICOS5)
1425 #ifdef LINEMODE
1426 /*
1427 * If ioctl from pty, pass it through net
1428 */
1429 if (ptyibuf[0] & TIOCPKT_IOCTL) {
1430 copy_termbuf(ptyibuf+1, pcc-1);
1431 localstat();
1432 pcc = 1;
1433 }
1434 #endif /* LINEMODE */
1435 if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
1436 netclear(); /* clear buffer back */
1437 #ifndef NO_URGENT
1438 /*
1439 * There are client telnets on some
1440 * operating systems get screwed up
1441 * royally if we send them urgent
1442 * mode data.
1443 */
1444 output_data("%c%c", IAC, DM);
1445 neturg = nfrontp - 1; /* off by one XXX */
1446 DIAG(TD_OPTIONS,
1447 printoption("td: send IAC", DM));
1448
1449 #endif
1450 }
1451 if (his_state_is_will(TELOPT_LFLOW) &&
1452 (ptyibuf[0] &
1453 (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
1454 int newflow =
1455 ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
1456 if (newflow != flowmode) {
1457 flowmode = newflow;
1458 (void) output_data(
1459 "%c%c%c%c%c%c",
1460 IAC, SB, TELOPT_LFLOW,
1461 flowmode ? LFLOW_ON
1462 : LFLOW_OFF,
1463 IAC, SE);
1464 DIAG(TD_OPTIONS, printsub('>',
1465 (unsigned char *)nfrontp - 4,
1466 4););
1467 }
1468 }
1469 pcc--;
1470 ptyip = ptyibuf+1;
1471 #else /* defined(CRAY2) && defined(UNICOS5) */
1472 if (!uselinemode) {
1473 unpcc = pcc;
1474 unptyip = ptyibuf;
1475 pcc = term_output(&unptyip, ptyibuf2,
1476 &unpcc, BUFSIZ);
1477 ptyip = ptyibuf2;
1478 } else
1479 ptyip = ptyibuf;
1480 #endif /* defined(CRAY2) && defined(UNICOS5) */
1481 }
1482 }
1483
1484 while (pcc > 0) {
1485 if ((&netobuf[BUFSIZ] - nfrontp) < 2)
1486 break;
1487 c = *ptyip++ & 0377, pcc--;
1488 if (c == IAC)
1489 output_data("%c", c);
1490 #if defined(CRAY2) && defined(UNICOS5)
1491 else if (c == '\n' &&
1492 my_state_is_wont(TELOPT_BINARY) && newmap)
1493 otput_data("\r");
1494 #endif /* defined(CRAY2) && defined(UNICOS5) */
1495 output_data("%c", c);
1496 if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
1497 if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
1498 output_data("%c", *ptyip++ & 0377);
1499 pcc--;
1500 } else
1501 output_datalen("\0", 1);
1502 }
1503 }
1504 #if defined(CRAY2) && defined(UNICOS5)
1505 /*
1506 * If chars were left over from the terminal driver,
1507 * note their existence.
1508 */
1509 if (!uselinemode && unpcc) {
1510 pcc = unpcc;
1511 unpcc = 0;
1512 ptyip = unptyip;
1513 }
1514 #endif /* defined(CRAY2) && defined(UNICOS5) */
1515
1516 if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
1517 netflush();
1518 if (ncc > 0)
1519 telrcv();
1520 if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
1521 ptyflush();
1522 }
1523 cleanup(0);
1524 } /* end of telnet */
1525
1526 #ifndef TCSIG
1527 # ifdef TIOCSIG
1528 # define TCSIG TIOCSIG
1529 # endif
1530 #endif
1531
1532 #ifdef STREAMSPTY
1533
1534 int flowison = -1; /* current state of flow: -1 is unknown */
1535
1536 int readstream(p, ibuf, bufsize)
1537 int p;
1538 char *ibuf;
1539 int bufsize;
1540 {
1541 int flags = 0;
1542 int ret = 0;
1543 struct termios *tsp;
1544 struct termio *tp;
1545 struct iocblk *ip;
1546 char vstop, vstart;
1547 int ixon;
1548 int newflow;
1549
1550 strbufc.maxlen = BUFSIZ;
1551 strbufc.buf = (char *)ctlbuf;
1552 strbufd.maxlen = bufsize-1;
1553 strbufd.len = 0;
1554 strbufd.buf = ibuf+1;
1555 ibuf[0] = 0;
1556
1557 ret = getmsg(p, &strbufc, &strbufd, &flags);
1558 if (ret < 0) /* error of some sort -- probably EAGAIN */
1559 return(-1);
1560
1561 if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
1562 /* data message */
1563 if (strbufd.len > 0) { /* real data */
1564 return(strbufd.len + 1); /* count header char */
1565 } else {
1566 /* nothing there */
1567 errno = EAGAIN;
1568 return(-1);
1569 }
1570 }
1571
1572 /*
1573 * It's a control message. Return 1, to look at the flag we set
1574 */
1575
1576 switch (ctlbuf[0]) {
1577 case M_FLUSH:
1578 if (ibuf[1] & FLUSHW)
1579 ibuf[0] = TIOCPKT_FLUSHWRITE;
1580 return(1);
1581
1582 case M_IOCTL:
1583 ip = (struct iocblk *) (ibuf+1);
1584
1585 switch (ip->ioc_cmd) {
1586 case TCSETS:
1587 case TCSETSW:
1588 case TCSETSF:
1589 tsp = (struct termios *)
1590 (ibuf+1 + sizeof(struct iocblk));
1591 vstop = tsp->c_cc[VSTOP];
1592 vstart = tsp->c_cc[VSTART];
1593 ixon = tsp->c_iflag & IXON;
1594 break;
1595 case TCSETA:
1596 case TCSETAW:
1597 case TCSETAF:
1598 tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
1599 vstop = tp->c_cc[VSTOP];
1600 vstart = tp->c_cc[VSTART];
1601 ixon = tp->c_iflag & IXON;
1602 break;
1603 default:
1604 errno = EAGAIN;
1605 return(-1);
1606 }
1607
1608 newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
1609 if (newflow != flowison) { /* it's a change */
1610 flowison = newflow;
1611 ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
1612 return(1);
1613 }
1614 }
1615
1616 /* nothing worth doing anything about */
1617 errno = EAGAIN;
1618 return(-1);
1619 }
1620 #endif /* STREAMSPTY */
1621
1622 /*
1623 * Send interrupt to process on other side of pty.
1624 * If it is in raw mode, just write NULL;
1625 * otherwise, write intr char.
1626 */
1627 void
1628 interrupt()
1629 {
1630 ptyflush(); /* half-hearted */
1631
1632 #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
1633 /* Streams PTY style ioctl to post a signal */
1634 {
1635 int sig = SIGINT;
1636 (void) ioctl(pty, TIOCSIGNAL, &sig);
1637 (void) ioctl(pty, I_FLUSH, FLUSHR);
1638 }
1639 #else
1640 #ifdef TCSIG
1641 (void) ioctl(pty, TCSIG, (char *)SIGINT);
1642 #else /* TCSIG */
1643 init_termbuf();
1644 *pfrontp++ = slctab[SLC_IP].sptr ?
1645 (unsigned char)*slctab[SLC_IP].sptr : '\177';
1646 #endif /* TCSIG */
1647 #endif
1648 }
1649
1650 /*
1651 * Send quit to process on other side of pty.
1652 * If it is in raw mode, just write NULL;
1653 * otherwise, write quit char.
1654 */
1655 void
1656 sendbrk()
1657 {
1658 ptyflush(); /* half-hearted */
1659 #ifdef TCSIG
1660 (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1661 #else /* TCSIG */
1662 init_termbuf();
1663 *pfrontp++ = slctab[SLC_ABORT].sptr ?
1664 (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1665 #endif /* TCSIG */
1666 }
1667
1668 void
1669 sendsusp()
1670 {
1671 #ifdef SIGTSTP
1672 ptyflush(); /* half-hearted */
1673 # ifdef TCSIG
1674 (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1675 # else /* TCSIG */
1676 *pfrontp++ = slctab[SLC_SUSP].sptr ?
1677 (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1678 # endif /* TCSIG */
1679 #endif /* SIGTSTP */
1680 }
1681
1682 /*
1683 * When we get an AYT, if ^T is enabled, use that. Otherwise,
1684 * just send back "[Yes]".
1685 */
1686 void
1687 recv_ayt()
1688 {
1689 #if defined(SIGINFO) && defined(TCSIG)
1690 if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1691 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1692 return;
1693 }
1694 #endif
1695 (void) output_data("\r\n[Yes]\r\n");
1696 }
1697
1698 void
1699 doeof()
1700 {
1701 init_termbuf();
1702
1703 #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1704 if (!tty_isediting()) {
1705 extern char oldeofc;
1706 *pfrontp++ = oldeofc;
1707 return;
1708 }
1709 #endif
1710 *pfrontp++ = slctab[SLC_EOF].sptr ?
1711 (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1712 }
1713