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