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