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