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