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