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