telnet.c revision 1.34.2.1 1 /* $NetBSD: telnet.c,v 1.34.2.1 2012/04/17 00:09:40 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1990, 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
36 #else
37 __RCSID("$NetBSD: telnet.c,v 1.34.2.1 2012/04/17 00:09:40 yamt Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42
43 #include <signal.h>
44 #include <term.h>
45 #include <unistd.h>
46 /* By the way, we need to include curses.h before telnet.h since,
47 * among other things, telnet.h #defines 'DO', which is a variable
48 * declared in curses.h.
49 */
50
51 #include <arpa/telnet.h>
52
53 #include <ctype.h>
54
55 #include "ring.h"
56 #include "defines.h"
57 #include "externs.h"
58 #include "types.h"
59 #include "general.h"
60
61 #include <libtelnet/misc.h>
62 #ifdef AUTHENTICATION
63 #include <libtelnet/auth.h>
64 #endif
65 #ifdef ENCRYPTION
66 #include <libtelnet/encrypt.h>
67 #endif
68
69 #define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
70
71 static unsigned char subbuffer[SUBBUFSIZE],
72 *subpointer, *subend; /* buffer for sub-options */
73 #define SB_CLEAR() subpointer = subbuffer;
74 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
75 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
76 *subpointer++ = (c); \
77 }
78
79 #define SB_GET() ((*subpointer++)&0xff)
80 #define SB_PEEK() ((*subpointer)&0xff)
81 #define SB_EOF() (subpointer >= subend)
82 #define SB_LEN() (subend - subpointer)
83
84 char options[256]; /* The combined options */
85 char do_dont_resp[256];
86 char will_wont_resp[256];
87
88 int
89 eight = 0,
90 autologin = 0, /* Autologin anyone? */
91 skiprc = 0,
92 connected,
93 showoptions,
94 In3270, /* Are we in 3270 mode? */
95 ISend, /* trying to send network data in */
96 telnet_debug = 0,
97 crmod,
98 netdata, /* Print out network data flow */
99 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
100 #ifdef TN3270
101 noasynchtty = 0,/* User specified "-noasynch" on command line */
102 noasynchnet = 0,/* User specified "-noasynch" on command line */
103 askedSGA = 0, /* We have talked about suppress go ahead */
104 #endif /* defined(TN3270) */
105 telnetport,
106 SYNCHing, /* we are in TELNET SYNCH mode */
107 flushout, /* flush output */
108 autoflush = 0, /* flush output when interrupting? */
109 autosynch, /* send interrupt characters with SYNCH? */
110 localflow, /* we handle flow control locally */
111 restartany, /* if flow control enabled, restart on any character */
112 localchars, /* we recognize interrupt/quit */
113 donelclchars, /* the user has set "localchars" */
114 donebinarytoggle, /* the user has put us in binary */
115 dontlecho, /* do we suppress local echoing right now? */
116 globalmode,
117 doaddrlookup = 1, /* do a reverse address lookup? */
118 clienteof = 0;
119
120 char *prompt = 0;
121
122 cc_t escape;
123 cc_t rlogin;
124 #ifdef KLUDGELINEMODE
125 cc_t echoc;
126 #endif
127
128 /*
129 * Telnet receiver states for fsm
130 */
131 #define TS_DATA 0
132 #define TS_IAC 1
133 #define TS_WILL 2
134 #define TS_WONT 3
135 #define TS_DO 4
136 #define TS_DONT 5
137 #define TS_CR 6
138 #define TS_SB 7 /* sub-option collection */
139 #define TS_SE 8 /* looking for sub-option end */
140
141 static int telrcv_state;
142 #ifdef OLD_ENVIRON
143 unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
144 #else
145 # define telopt_environ TELOPT_NEW_ENVIRON
146 #endif
147
148 jmp_buf toplevel = { 0 };
149
150 int flushline;
151 int linemode;
152
153 #ifdef KLUDGELINEMODE
154 int kludgelinemode = 1;
155 #endif
156
157 static void dooption(int);
158 static void dontoption(int);
159 static void suboption(void);
160 static int telsnd(void);
161 static void netclear(void);
162 static void doflush(void);
163
164 /*
165 * The following are some clocks used to decide how to interpret
166 * the relationship between various variables.
167 */
168
169 Clocks clocks;
170
171 #ifdef notdef
173 Modelist modelist[] = {
174 { "telnet command mode", COMMAND_LINE },
175 { "character-at-a-time mode", 0 },
176 { "character-at-a-time mode (local echo)", LOCAL_ECHO|LOCAL_CHARS },
177 { "line-by-line mode (remote echo)", LINE | LOCAL_CHARS },
178 { "line-by-line mode", LINE | LOCAL_ECHO | LOCAL_CHARS },
179 { "line-by-line mode (local echoing suppressed)", LINE | LOCAL_CHARS },
180 { "3270 mode", 0 },
181 };
182 #endif
183
184
185 /*
187 * Initialize telnet environment.
188 */
189
190 void
191 init_telnet(void)
192 {
193 env_init();
194
195 SB_CLEAR();
196 ClearArray(options);
197
198 connected = In3270 = ISend = localflow = donebinarytoggle = 0;
199 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
200 auth_encrypt_connect(connected);
201 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
202 restartany = -1;
203
204 SYNCHing = 0;
205
206 /* Don't change NetTrace */
207
208 escape = CONTROL(']');
209 rlogin = _POSIX_VDISABLE;
210 #ifdef KLUDGELINEMODE
211 echoc = CONTROL('E');
212 #endif
213
214 flushline = 1;
215 telrcv_state = TS_DATA;
216 }
217
218
220 #ifdef notdef
221 #include <stdarg.h>
222
223 /*VARARGS*/
224 static void
225 printring(Ring *ring, char *format, ...)
226 va_dcl
227 {
228 va_list ap;
229 char buffer[100]; /* where things go */
230 char *ptr;
231 char *string;
232 int i;
233
234 va_start(ap, format);
235
236 ptr = buffer;
237
238 while ((i = *format++) != 0) {
239 if (i == '%') {
240 i = *format++;
241 switch (i) {
242 case 'c':
243 *ptr++ = va_arg(ap, int);
244 break;
245 case 's':
246 string = va_arg(ap, char *);
247 ring_supply_data(ring, buffer, ptr-buffer);
248 ring_supply_data(ring, string, strlen(string));
249 ptr = buffer;
250 break;
251 case 0:
252 ExitString("printring: trailing %%.\n", 1);
253 /*NOTREACHED*/
254 default:
255 ExitString("printring: unknown format character.\n", 1);
256 /*NOTREACHED*/
257 }
258 } else {
259 *ptr++ = i;
260 }
261 }
262 va_end(ap);
263 ring_supply_data(ring, buffer, ptr-buffer);
264 }
265 #endif
266
267 /*
268 * These routines are in charge of sending option negotiations
269 * to the other side.
270 *
271 * The basic idea is that we send the negotiation if either side
272 * is in disagreement as to what the current state should be.
273 */
274
275 void
276 send_do(int c, int init)
277 {
278 if (init) {
279 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
280 my_want_state_is_do(c))
281 return;
282 set_my_want_state_do(c);
283 do_dont_resp[c]++;
284 }
285 NET2ADD(IAC, DO);
286 NETADD(c);
287 printoption("SENT", DO, c);
288 }
289
290 void
291 send_dont(int c, int init)
292 {
293 if (init) {
294 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
295 my_want_state_is_dont(c))
296 return;
297 set_my_want_state_dont(c);
298 do_dont_resp[c]++;
299 }
300 NET2ADD(IAC, DONT);
301 NETADD(c);
302 printoption("SENT", DONT, c);
303 }
304
305 void
306 send_will(int c, int init)
307 {
308 if (init) {
309 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
310 my_want_state_is_will(c))
311 return;
312 set_my_want_state_will(c);
313 will_wont_resp[c]++;
314 }
315 NET2ADD(IAC, WILL);
316 NETADD(c);
317 printoption("SENT", WILL, c);
318 }
319
320 void
321 send_wont(int c, int init)
322 {
323 if (init) {
324 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
325 my_want_state_is_wont(c))
326 return;
327 set_my_want_state_wont(c);
328 will_wont_resp[c]++;
329 }
330 NET2ADD(IAC, WONT);
331 NETADD(c);
332 printoption("SENT", WONT, c);
333 }
334
335
336 void
337 willoption(int option)
338 {
339 int new_state_ok = 0;
340
341 if (do_dont_resp[option]) {
342 --do_dont_resp[option];
343 if (do_dont_resp[option] && my_state_is_do(option))
344 --do_dont_resp[option];
345 }
346
347 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
348
349 switch (option) {
350
351 case TELOPT_ECHO:
352 # if defined(TN3270)
353 /*
354 * The following is a pain in the rear-end.
355 * Various IBM servers (some versions of Wiscnet,
356 * possibly Fibronics/Spartacus, and who knows who
357 * else) will NOT allow us to send "DO SGA" too early
358 * in the setup proceedings. On the other hand,
359 * 4.2 servers (telnetd) won't set SGA correctly.
360 * So, we are stuck. Empirically (but, based on
361 * a VERY small sample), the IBM servers don't send
362 * out anything about ECHO, so we postpone our sending
363 * "DO SGA" until we see "WILL ECHO" (which 4.2 servers
364 * DO send).
365 */
366 {
367 if (askedSGA == 0) {
368 askedSGA = 1;
369 if (my_want_state_is_dont(TELOPT_SGA))
370 send_do(TELOPT_SGA, 1);
371 }
372 }
373 /* Fall through */
374 case TELOPT_EOR:
375 #endif /* defined(TN3270) */
376 case TELOPT_BINARY:
377 case TELOPT_SGA:
378 settimer(modenegotiated);
379 /* FALL THROUGH */
380 case TELOPT_STATUS:
381 #ifdef AUTHENTICATION
382 case TELOPT_AUTHENTICATION:
383 #ifdef ENCRYPTION
384 case TELOPT_ENCRYPT:
385 #endif /* ENCRYPTION */
386 #endif
387 new_state_ok = 1;
388 break;
389
390 case TELOPT_TM:
391 if (flushout)
392 flushout = 0;
393 /*
394 * Special case for TM. If we get back a WILL,
395 * pretend we got back a WONT.
396 */
397 set_my_want_state_dont(option);
398 set_my_state_dont(option);
399 return; /* Never reply to TM will's/wont's */
400
401 case TELOPT_LINEMODE:
402 default:
403 break;
404 }
405
406 if (new_state_ok) {
407 set_my_want_state_do(option);
408 send_do(option, 0);
409 setconnmode(0); /* possibly set new tty mode */
410 } else {
411 do_dont_resp[option]++;
412 send_dont(option, 0);
413 }
414 }
415 set_my_state_do(option);
416 #ifdef ENCRYPTION
417 if (option == TELOPT_ENCRYPT)
418 encrypt_send_support();
419 #endif /* ENCRYPTION */
420 }
421
422 void
423 wontoption(int option)
424 {
425 if (do_dont_resp[option]) {
426 --do_dont_resp[option];
427 if (do_dont_resp[option] && my_state_is_dont(option))
428 --do_dont_resp[option];
429 }
430
431 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
432
433 switch (option) {
434
435 #ifdef KLUDGELINEMODE
436 case TELOPT_SGA:
437 if (!kludgelinemode)
438 break;
439 /* FALL THROUGH */
440 #endif
441 case TELOPT_ECHO:
442 settimer(modenegotiated);
443 break;
444
445 case TELOPT_TM:
446 if (flushout)
447 flushout = 0;
448 set_my_want_state_dont(option);
449 set_my_state_dont(option);
450 return; /* Never reply to TM will's/wont's */
451
452 default:
453 break;
454 }
455 set_my_want_state_dont(option);
456 if (my_state_is_do(option))
457 send_dont(option, 0);
458 setconnmode(0); /* Set new tty mode */
459 } else if (option == TELOPT_TM) {
460 /*
461 * Special case for TM.
462 */
463 if (flushout)
464 flushout = 0;
465 set_my_want_state_dont(option);
466 }
467 set_my_state_dont(option);
468 }
469
470 static void
471 dooption(int option)
472 {
473 int new_state_ok = 0;
474
475 if (will_wont_resp[option]) {
476 --will_wont_resp[option];
477 if (will_wont_resp[option] && my_state_is_will(option))
478 --will_wont_resp[option];
479 }
480
481 if (will_wont_resp[option] == 0) {
482 if (my_want_state_is_wont(option)) {
483
484 switch (option) {
485
486 case TELOPT_TM:
487 /*
488 * Special case for TM. We send a WILL, but pretend
489 * we sent WONT.
490 */
491 send_will(option, 0);
492 set_my_want_state_wont(TELOPT_TM);
493 set_my_state_wont(TELOPT_TM);
494 return;
495
496 # if defined(TN3270)
497 case TELOPT_EOR: /* end of record */
498 # endif /* defined(TN3270) */
499 case TELOPT_BINARY: /* binary mode */
500 case TELOPT_NAWS: /* window size */
501 case TELOPT_TSPEED: /* terminal speed */
502 case TELOPT_LFLOW: /* local flow control */
503 case TELOPT_TTYPE: /* terminal type option */
504 case TELOPT_SGA: /* no big deal */
505 #ifdef ENCRYPTION
506 case TELOPT_ENCRYPT: /* encryption variable option */
507 #endif /* ENCRYPTION */
508 new_state_ok = 1;
509 break;
510
511 case TELOPT_NEW_ENVIRON: /* New environment variable option */
512 #ifdef OLD_ENVIRON
513 if (my_state_is_will(TELOPT_OLD_ENVIRON))
514 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
515 goto env_common;
516 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
517 if (my_state_is_will(TELOPT_NEW_ENVIRON))
518 break; /* Don't enable if new one is in use! */
519 env_common:
520 telopt_environ = option;
521 #endif
522 new_state_ok = 1;
523 break;
524
525 #ifdef AUTHENTICATION
526 case TELOPT_AUTHENTICATION:
527 if (autologin)
528 new_state_ok = 1;
529 break;
530 #endif
531
532 case TELOPT_XDISPLOC: /* X Display location */
533 if (env_getvalue((const unsigned char *)"DISPLAY"))
534 new_state_ok = 1;
535 break;
536
537 case TELOPT_LINEMODE:
538 #ifdef KLUDGELINEMODE
539 kludgelinemode = 0;
540 send_do(TELOPT_SGA, 1);
541 #endif
542 set_my_want_state_will(TELOPT_LINEMODE);
543 send_will(option, 0);
544 set_my_state_will(TELOPT_LINEMODE);
545 slc_init();
546 return;
547
548 case TELOPT_ECHO: /* We're never going to echo... */
549 default:
550 break;
551 }
552
553 if (new_state_ok) {
554 set_my_want_state_will(option);
555 send_will(option, 0);
556 setconnmode(0); /* Set new tty mode */
557 } else {
558 will_wont_resp[option]++;
559 send_wont(option, 0);
560 }
561 } else {
562 /*
563 * Handle options that need more things done after the
564 * other side has acknowledged the option.
565 */
566 switch (option) {
567 case TELOPT_LINEMODE:
568 #ifdef KLUDGELINEMODE
569 kludgelinemode = 0;
570 send_do(TELOPT_SGA, 1);
571 #endif
572 set_my_state_will(option);
573 slc_init();
574 send_do(TELOPT_SGA, 0);
575 return;
576 }
577 }
578 }
579 set_my_state_will(option);
580 }
581
582 static void
583 dontoption(int option)
584 {
585
586 if (will_wont_resp[option]) {
587 --will_wont_resp[option];
588 if (will_wont_resp[option] && my_state_is_wont(option))
589 --will_wont_resp[option];
590 }
591
592 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
593 switch (option) {
594 case TELOPT_LINEMODE:
595 linemode = 0; /* put us back to the default state */
596 break;
597 #ifdef OLD_ENVIRON
598 case TELOPT_NEW_ENVIRON:
599 /*
600 * The new environ option wasn't recognized, try
601 * the old one.
602 */
603 send_will(TELOPT_OLD_ENVIRON, 1);
604 telopt_environ = TELOPT_OLD_ENVIRON;
605 break;
606 #endif
607 }
608 /* we always accept a DONT */
609 set_my_want_state_wont(option);
610 if (my_state_is_will(option))
611 send_wont(option, 0);
612 setconnmode(0); /* Set new tty mode */
613 }
614 set_my_state_wont(option);
615 }
616
617 /*
618 * Given a buffer returned by tgetent(), this routine will turn
619 * the pipe separated list of names in the buffer into an array
620 * of pointers to null terminated names. We toss out any bad,
621 * duplicate, or verbose names (names with spaces).
622 */
623
624 static char name_unknown[] = "UNKNOWN";
625 static char *unknown[] = { 0, 0 };
626
627 char **
628 mklist(char *buf, char *name)
629 {
630 int n;
631 char c, *cp, **argvp, *cp2, **argv, **avt;
632
633 if (name) {
634 if ((int)strlen(name) > 40) {
635 name = 0;
636 unknown[0] = name_unknown;
637 } else {
638 unknown[0] = name;
639 upcase(name);
640 }
641 } else
642 unknown[0] = name_unknown;
643 /*
644 * Count up the number of names.
645 */
646 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
647 if (*cp == '|')
648 n++;
649 }
650 /*
651 * Allocate an array to put the name pointers into
652 */
653 argv = (char **)malloc((n+3)*sizeof(char *));
654 if (argv == 0)
655 return(unknown);
656
657 /*
658 * Fill up the array of pointers to names.
659 */
660 *argv = 0;
661 argvp = argv+1;
662 n = 0;
663 for (cp = cp2 = buf; (c = *cp); cp++) {
664 if (c == '|' || c == ':') {
665 *cp++ = '\0';
666 /*
667 * Skip entries that have spaces or are over 40
668 * characters long. If this is our environment
669 * name, then put it up front. Otherwise, as
670 * long as this is not a duplicate name (case
671 * insensitive) add it to the list.
672 */
673 if (n || (cp - cp2 > 41))
674 ;
675 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
676 *argv = cp2;
677 else if (is_unique(cp2, argv+1, argvp))
678 *argvp++ = cp2;
679 if (c == ':')
680 break;
681 /*
682 * Skip multiple delimiters. Reset cp2 to
683 * the beginning of the next name. Reset n,
684 * the flag for names with spaces.
685 */
686 while ((c = *cp) == '|')
687 cp++;
688 cp2 = cp;
689 n = 0;
690 }
691 /*
692 * Skip entries with spaces or non-ascii values.
693 * Convert lower case letters to upper case.
694 */
695 if ((c == ' ') || !isascii(c))
696 n = 1;
697 else if (islower((unsigned char)c))
698 *cp = toupper((unsigned char)c);
699 }
700
701 /*
702 * Check for an old V6 2 character name. If the second
703 * name points to the beginning of the buffer, and is
704 * only 2 characters long, move it to the end of the array.
705 */
706 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
707 --argvp;
708 for (avt = &argv[1]; avt < argvp; avt++)
709 *avt = *(avt+1);
710 *argvp++ = buf;
711 }
712
713 /*
714 * Duplicate last name, for TTYPE option, and null
715 * terminate the array. If we didn't find a match on
716 * our terminal name, put that name at the beginning.
717 */
718 cp = *(argvp-1);
719 *argvp++ = cp;
720 *argvp = 0;
721
722 if (*argv == 0) {
723 if (name)
724 *argv = name;
725 else {
726 --argvp;
727 for (avt = argv; avt < argvp; avt++)
728 *avt = *(avt+1);
729 }
730 }
731 if (*argv)
732 return(argv);
733 else
734 return(unknown);
735 }
736
737 int
738 is_unique(char *name, char **as, char **ae)
739 {
740 char **ap;
741 int n;
742
743 n = strlen(name) + 1;
744 for (ap = as; ap < ae; ap++)
745 if (strncasecmp(*ap, name, n) == 0)
746 return(0);
747 return (1);
748 }
749
750 #ifdef TERMCAP
751 char *termbuf;
752
753 /*ARGSUSED*/
754 int
755 setupterm(char *tname, int fd, int *errp)
756 {
757 char zz[1024], *zz_ptr;
758 char *ext_tc, *newptr;
759 size_t len;
760
761 if ((termbuf = malloc(1024)) == NULL)
762 goto error;
763
764 if (tgetent(termbuf, tname) == 1) {
765 /* check for ZZ capability, indicating termcap truncated */
766 zz_ptr = zz;
767 if (tgetstr("ZZ", &zz_ptr) != NULL) {
768 /* it was, fish back the full termcap */
769 sscanf(zz, "%p", &ext_tc);
770 len = strlen(ext_tc) + 1;
771 if ((newptr = realloc(termbuf, len)) == NULL)
772 goto error;
773
774 memcpy(newptr, ext_tc, len);
775 termbuf = newptr;
776 }
777
778 if (errp)
779 *errp = 1;
780 return(0);
781 }
782 error:
783 if (errp)
784 *errp = 0;
785 return(-1);
786 }
787 #else
788 #define termbuf ttytype
789 extern char ttytype[];
790 #endif
791
792 int resettermname = 1;
793
794 char *
795 gettermname(void)
796 {
797 char *tname;
798 static char **tnamep = 0;
799 static char **next;
800 int err;
801
802 if (resettermname) {
803 resettermname = 0;
804 if (tnamep && tnamep != unknown)
805 free(tnamep);
806 if ((tname = (char *)env_getvalue((const unsigned char *)"TERM")) &&
807 (setupterm(tname, 1, &err) == 0)) {
808 tnamep = mklist(termbuf, tname);
809 } else {
810 if (tname && ((int)strlen(tname) <= 40)) {
811 unknown[0] = tname;
812 upcase(tname);
813 } else
814 unknown[0] = name_unknown;
815 tnamep = unknown;
816 }
817 next = tnamep;
818 }
819 if (*next == 0)
820 next = tnamep;
821 return(*next++);
822 }
823 /*
824 * suboption()
825 *
826 * Look at the sub-option buffer, and try to be helpful to the other
827 * side.
828 *
829 * Currently we recognize:
830 *
831 * Terminal type, send request.
832 * Terminal speed (send request).
833 * Local flow control (is request).
834 * Linemode
835 */
836
837 static void
838 suboption(void)
839 {
840 unsigned char subchar;
841
842 printsub('<', subbuffer, SB_LEN()+2);
843 switch (subchar = SB_GET()) {
844 case TELOPT_TTYPE:
845 if (my_want_state_is_wont(TELOPT_TTYPE))
846 return;
847 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
848 return;
849 } else {
850 char *name;
851 unsigned char temp[50];
852 int len;
853
854 #ifdef TN3270
855 if (tn3270_ttype()) {
856 return;
857 }
858 #endif /* defined(TN3270) */
859 name = gettermname();
860 len = strlen(name) + 4 + 2;
861 if (len < NETROOM()) {
862 sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
863 TELQUAL_IS, name, IAC, SE);
864 ring_supply_data(&netoring, temp, len);
865 printsub('>', &temp[2], len-2);
866 } else {
867 ExitString("No room in buffer for terminal type.\n", 1);
868 /*NOTREACHED*/
869 }
870 }
871 break;
872 case TELOPT_TSPEED:
873 if (my_want_state_is_wont(TELOPT_TSPEED))
874 return;
875 if (SB_EOF())
876 return;
877 if (SB_GET() == TELQUAL_SEND) {
878 long osp, isp;
879 unsigned char temp[50];
880 int len;
881
882 TerminalSpeeds(&isp, &osp);
883
884 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
885 TELQUAL_IS, osp, isp, IAC, SE);
886 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
887
888 if (len < NETROOM()) {
889 ring_supply_data(&netoring, temp, len);
890 printsub('>', temp+2, len - 2);
891 }
892 /*@*/ else printf("lm_will: not enough room in buffer\n");
893 }
894 break;
895 case TELOPT_LFLOW:
896 if (my_want_state_is_wont(TELOPT_LFLOW))
897 return;
898 if (SB_EOF())
899 return;
900 switch(SB_GET()) {
901 case LFLOW_RESTART_ANY:
902 restartany = 1;
903 break;
904 case LFLOW_RESTART_XON:
905 restartany = 0;
906 break;
907 case LFLOW_ON:
908 localflow = 1;
909 break;
910 case LFLOW_OFF:
911 localflow = 0;
912 break;
913 default:
914 return;
915 }
916 setcommandmode();
917 setconnmode(0);
918 break;
919
920 case TELOPT_LINEMODE:
921 if (my_want_state_is_wont(TELOPT_LINEMODE))
922 return;
923 if (SB_EOF())
924 return;
925 switch (SB_GET()) {
926 case WILL:
927 lm_will(subpointer, SB_LEN());
928 break;
929 case WONT:
930 lm_wont(subpointer, SB_LEN());
931 break;
932 case DO:
933 lm_do(subpointer, SB_LEN());
934 break;
935 case DONT:
936 lm_dont(subpointer, SB_LEN());
937 break;
938 case LM_SLC:
939 slc(subpointer, SB_LEN());
940 break;
941 case LM_MODE:
942 lm_mode(subpointer, SB_LEN(), 0);
943 break;
944 default:
945 break;
946 }
947 break;
948
949 #ifdef OLD_ENVIRON
950 case TELOPT_OLD_ENVIRON:
951 #endif
952 case TELOPT_NEW_ENVIRON:
953 if (SB_EOF())
954 return;
955 switch(SB_PEEK()) {
956 case TELQUAL_IS:
957 case TELQUAL_INFO:
958 if (my_want_state_is_dont(subchar))
959 return;
960 break;
961 case TELQUAL_SEND:
962 if (my_want_state_is_wont(subchar)) {
963 return;
964 }
965 break;
966 default:
967 return;
968 }
969 env_opt(subpointer, SB_LEN());
970 break;
971
972 case TELOPT_XDISPLOC:
973 if (my_want_state_is_wont(TELOPT_XDISPLOC))
974 return;
975 if (SB_EOF())
976 return;
977 if (SB_GET() == TELQUAL_SEND) {
978 unsigned char temp[50], *dp;
979 int len;
980
981 if ((dp = env_getvalue((const unsigned char *)"DISPLAY")) == NULL) {
982 /*
983 * Something happened, we no longer have a DISPLAY
984 * variable. So, turn off the option.
985 */
986 send_wont(TELOPT_XDISPLOC, 1);
987 break;
988 }
989 sprintf((char *)temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_XDISPLOC,
990 TELQUAL_IS, dp, IAC, SE);
991 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
992
993 if (len < NETROOM()) {
994 ring_supply_data(&netoring, temp, len);
995 printsub('>', temp+2, len - 2);
996 }
997 /*@*/ else printf("lm_will: not enough room in buffer\n");
998 }
999 break;
1000
1001 #ifdef AUTHENTICATION
1002 case TELOPT_AUTHENTICATION: {
1003 if (!autologin)
1004 break;
1005 if (SB_EOF())
1006 return;
1007 switch(SB_GET()) {
1008 case TELQUAL_IS:
1009 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
1010 return;
1011 auth_is(subpointer, SB_LEN());
1012 break;
1013 case TELQUAL_SEND:
1014 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
1015 return;
1016 auth_send(subpointer, SB_LEN());
1017 break;
1018 case TELQUAL_REPLY:
1019 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
1020 return;
1021 auth_reply(subpointer, SB_LEN());
1022 break;
1023 case TELQUAL_NAME:
1024 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
1025 return;
1026 auth_name(subpointer, SB_LEN());
1027 break;
1028 }
1029 }
1030 break;
1031 #endif
1032 #ifdef ENCRYPTION
1033 case TELOPT_ENCRYPT:
1034 if (SB_EOF())
1035 return;
1036 switch(SB_GET()) {
1037 case ENCRYPT_START:
1038 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1039 return;
1040 encrypt_start(subpointer, SB_LEN());
1041 break;
1042 case ENCRYPT_END:
1043 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1044 return;
1045 encrypt_end();
1046 break;
1047 case ENCRYPT_SUPPORT:
1048 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1049 return;
1050 encrypt_support(subpointer, SB_LEN());
1051 break;
1052 case ENCRYPT_REQSTART:
1053 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1054 return;
1055 encrypt_request_start(subpointer, SB_LEN());
1056 break;
1057 case ENCRYPT_REQEND:
1058 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1059 return;
1060 /*
1061 * We can always send an REQEND so that we cannot
1062 * get stuck encrypting. We should only get this
1063 * if we have been able to get in the correct mode
1064 * anyhow.
1065 */
1066 encrypt_request_end();
1067 break;
1068 case ENCRYPT_IS:
1069 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1070 return;
1071 encrypt_is(subpointer, SB_LEN());
1072 break;
1073 case ENCRYPT_REPLY:
1074 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1075 return;
1076 encrypt_reply(subpointer, SB_LEN());
1077 break;
1078 case ENCRYPT_ENC_KEYID:
1079 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1080 return;
1081 encrypt_enc_keyid(subpointer, SB_LEN());
1082 break;
1083 case ENCRYPT_DEC_KEYID:
1084 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1085 return;
1086 encrypt_dec_keyid(subpointer, SB_LEN());
1087 break;
1088 default:
1089 break;
1090 }
1091 break;
1092 #endif /* ENCRYPTION */
1093 default:
1094 break;
1095 }
1096 }
1097
1098 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1099
1100 void
1101 lm_will(unsigned char *cmd, int len)
1102 {
1103 if (len < 1) {
1104 /*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1105 return;
1106 }
1107 switch(cmd[0]) {
1108 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1109 default:
1110 str_lm[3] = DONT;
1111 str_lm[4] = cmd[0];
1112 if ((size_t)NETROOM() > sizeof(str_lm)) {
1113 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1114 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1115 }
1116 /*@*/ else printf("lm_will: not enough room in buffer\n");
1117 break;
1118 }
1119 }
1120
1121 void
1122 lm_wont(unsigned char *cmd, int len)
1123 {
1124 if (len < 1) {
1125 /*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1126 return;
1127 }
1128 switch(cmd[0]) {
1129 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1130 default:
1131 /* We are always DONT, so don't respond */
1132 return;
1133 }
1134 }
1135
1136 void
1137 lm_do(unsigned char *cmd, int len)
1138 {
1139 if (len < 1) {
1140 /*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1141 return;
1142 }
1143 switch(cmd[0]) {
1144 case LM_FORWARDMASK:
1145 default:
1146 str_lm[3] = WONT;
1147 str_lm[4] = cmd[0];
1148 if ((size_t)NETROOM() > sizeof(str_lm)) {
1149 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1150 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1151 }
1152 /*@*/ else printf("lm_do: not enough room in buffer\n");
1153 break;
1154 }
1155 }
1156
1157 void
1158 lm_dont(unsigned char *cmd, int len)
1159 {
1160 if (len < 1) {
1161 /*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1162 return;
1163 }
1164 switch(cmd[0]) {
1165 case LM_FORWARDMASK:
1166 default:
1167 /* we are always WONT, so don't respond */
1168 break;
1169 }
1170 }
1171
1172 static unsigned char str_lm_mode[] = {
1173 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1174 };
1175
1176 void
1177 lm_mode(unsigned char *cmd, int len, int init)
1178 {
1179 if (len != 1)
1180 return;
1181 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1182 return;
1183 if (*cmd&MODE_ACK)
1184 return;
1185 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1186 str_lm_mode[4] = linemode;
1187 if (!init)
1188 str_lm_mode[4] |= MODE_ACK;
1189 if ((size_t)NETROOM() > sizeof(str_lm_mode)) {
1190 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1191 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1192 }
1193 /*@*/ else printf("lm_mode: not enough room in buffer\n");
1194 setconnmode(0); /* set changed mode */
1195 }
1196
1197
1198
1200 /*
1201 * slc()
1202 * Handle special character suboption of LINEMODE.
1203 */
1204
1205 struct spc {
1206 cc_t val;
1207 cc_t *valp;
1208 char flags; /* Current flags & level */
1209 char mylevel; /* Maximum level & flags */
1210 } spc_data[NSLC+1];
1211
1212 #define SLC_IMPORT 0
1213 #define SLC_EXPORT 1
1214 #define SLC_RVALUE 2
1215 static int slc_mode = SLC_EXPORT;
1216
1217 void
1218 slc_init(void)
1219 {
1220 struct spc *spcp;
1221
1222 localchars = 1;
1223 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1224 spcp->val = 0;
1225 spcp->valp = 0;
1226 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1227 }
1228
1229 #define initfunc(func, flags) { \
1230 spcp = &spc_data[func]; \
1231 if ((spcp->valp = tcval(func)) != NULL){ \
1232 spcp->val = *spcp->valp; \
1233 spcp->mylevel = SLC_VARIABLE|flags; \
1234 } else { \
1235 spcp->val = 0; \
1236 spcp->mylevel = SLC_DEFAULT; \
1237 } \
1238 }
1239
1240 initfunc(SLC_SYNCH, 0);
1241 /* No BRK */
1242 initfunc(SLC_AO, 0);
1243 initfunc(SLC_AYT, 0);
1244 /* No EOR */
1245 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1246 initfunc(SLC_EOF, 0);
1247 initfunc(SLC_SUSP, SLC_FLUSHIN);
1248 initfunc(SLC_EC, 0);
1249 initfunc(SLC_EL, 0);
1250 initfunc(SLC_EW, 0);
1251 initfunc(SLC_RP, 0);
1252 initfunc(SLC_LNEXT, 0);
1253 initfunc(SLC_XON, 0);
1254 initfunc(SLC_XOFF, 0);
1255 initfunc(SLC_FORW1, 0);
1256 initfunc(SLC_FORW2, 0);
1257 /* No FORW2 */
1258
1259 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1260 #undef initfunc
1261
1262 if (slc_mode == SLC_EXPORT)
1263 slc_export();
1264 else
1265 slc_import(1);
1266
1267 }
1268
1269 void
1270 slcstate(void)
1271 {
1272 printf("Special characters are %s values\n",
1273 slc_mode == SLC_IMPORT ? "remote default" :
1274 slc_mode == SLC_EXPORT ? "local" :
1275 "remote");
1276 }
1277
1278 void
1279 slc_mode_export(int n)
1280 {
1281 slc_mode = SLC_EXPORT;
1282 if (my_state_is_will(TELOPT_LINEMODE))
1283 slc_export();
1284 }
1285
1286 void
1287 slc_mode_import(int def)
1288 {
1289 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1290 if (my_state_is_will(TELOPT_LINEMODE))
1291 slc_import(def);
1292 }
1293
1294 unsigned char slc_import_val[] = {
1295 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1296 };
1297 unsigned char slc_import_def[] = {
1298 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1299 };
1300
1301 void
1302 slc_import(int def)
1303 {
1304 if ((size_t)NETROOM() > sizeof(slc_import_val)) {
1305 if (def) {
1306 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1307 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1308 } else {
1309 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1310 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1311 }
1312 }
1313 /*@*/ else printf("slc_import: not enough room\n");
1314 }
1315
1316 void
1317 slc_export(void)
1318 {
1319 struct spc *spcp;
1320
1321 TerminalDefaultChars();
1322
1323 slc_start_reply();
1324 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1325 if (spcp->mylevel != SLC_NOSUPPORT) {
1326 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1327 spcp->flags = SLC_NOSUPPORT;
1328 else
1329 spcp->flags = spcp->mylevel;
1330 if (spcp->valp)
1331 spcp->val = *spcp->valp;
1332 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1333 }
1334 }
1335 slc_end_reply();
1336 (void)slc_update();
1337 setconnmode(1); /* Make sure the character values are set */
1338 }
1339
1340 void
1341 slc(unsigned char *cp, int len)
1342 {
1343 struct spc *spcp;
1344 int func,level;
1345
1346 slc_start_reply();
1347
1348 for (; len >= 3; len -=3, cp +=3) {
1349
1350 func = cp[SLC_FUNC];
1351
1352 if (func == 0) {
1353 /*
1354 * Client side: always ignore 0 function.
1355 */
1356 continue;
1357 }
1358 if (func > NSLC) {
1359 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1360 slc_add_reply(func, SLC_NOSUPPORT, 0);
1361 continue;
1362 }
1363
1364 spcp = &spc_data[func];
1365
1366 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1367
1368 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1369 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1370 continue;
1371 }
1372
1373 if (level == (SLC_DEFAULT|SLC_ACK)) {
1374 /*
1375 * This is an error condition, the SLC_ACK
1376 * bit should never be set for the SLC_DEFAULT
1377 * level. Our best guess to recover is to
1378 * ignore the SLC_ACK bit.
1379 */
1380 cp[SLC_FLAGS] &= ~SLC_ACK;
1381 }
1382
1383 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1384 spcp->val = (cc_t)cp[SLC_VALUE];
1385 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1386 continue;
1387 }
1388
1389 level &= ~SLC_ACK;
1390
1391 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1392 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1393 spcp->val = (cc_t)cp[SLC_VALUE];
1394 }
1395 if (level == SLC_DEFAULT) {
1396 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1397 spcp->flags = spcp->mylevel;
1398 else
1399 spcp->flags = SLC_NOSUPPORT;
1400 }
1401 slc_add_reply(func, spcp->flags, spcp->val);
1402 }
1403 slc_end_reply();
1404 if (slc_update())
1405 setconnmode(1); /* set the new character values */
1406 }
1407
1408 void
1409 slc_check(void)
1410 {
1411 struct spc *spcp;
1412
1413 slc_start_reply();
1414 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1415 if (spcp->valp && spcp->val != *spcp->valp) {
1416 spcp->val = *spcp->valp;
1417 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1418 spcp->flags = SLC_NOSUPPORT;
1419 else
1420 spcp->flags = spcp->mylevel;
1421 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1422 }
1423 }
1424 slc_end_reply();
1425 setconnmode(1);
1426 }
1427
1428
1429 unsigned char slc_reply[128];
1430 unsigned char *slc_replyp;
1431
1432 void
1433 slc_start_reply(void)
1434 {
1435 slc_replyp = slc_reply;
1436 *slc_replyp++ = IAC;
1437 *slc_replyp++ = SB;
1438 *slc_replyp++ = TELOPT_LINEMODE;
1439 *slc_replyp++ = LM_SLC;
1440 }
1441
1442 void
1443 slc_add_reply(unsigned int func, unsigned int flags, cc_t value)
1444 {
1445 if ((size_t)(slc_replyp - slc_reply) + 6 > sizeof(slc_reply))
1446 return;
1447 if ((*slc_replyp++ = func) == IAC)
1448 *slc_replyp++ = IAC;
1449 if ((*slc_replyp++ = flags) == IAC)
1450 *slc_replyp++ = IAC;
1451 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1452 *slc_replyp++ = IAC;
1453 }
1454
1455 void
1456 slc_end_reply(void)
1457 {
1458 int len;
1459
1460 len = slc_replyp - slc_reply;
1461 if (len <= 4 || ((size_t)len + 2 > sizeof(slc_reply)))
1462 return;
1463 *slc_replyp++ = IAC;
1464 *slc_replyp++ = SE;
1465 len += 2;
1466 if (NETROOM() > len) {
1467 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1468 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1469 }
1470 /*@*/else printf("slc_end_reply: not enough room\n");
1471 }
1472
1473 int
1474 slc_update(void)
1475 {
1476 struct spc *spcp;
1477 int need_update = 0;
1478
1479 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1480 if (!(spcp->flags&SLC_ACK))
1481 continue;
1482 spcp->flags &= ~SLC_ACK;
1483 if (spcp->valp && (*spcp->valp != spcp->val)) {
1484 *spcp->valp = spcp->val;
1485 need_update = 1;
1486 }
1487 }
1488 return(need_update);
1489 }
1490
1491 #ifdef OLD_ENVIRON
1492 # ifdef ENV_HACK
1493 /*
1494 * Earlier version of telnet/telnetd from the BSD code had
1495 * the definitions of VALUE and VAR reversed. To ensure
1496 * maximum interoperability, we assume that the server is
1497 * an older BSD server, until proven otherwise. The newer
1498 * BSD servers should be able to handle either definition,
1499 * so it is better to use the wrong values if we don't
1500 * know what type of server it is.
1501 */
1502 int env_auto = 1;
1503 int old_env_var = OLD_ENV_VAR;
1504 int old_env_value = OLD_ENV_VALUE;
1505 # else
1506 # define old_env_var OLD_ENV_VAR
1507 # define old_env_value OLD_ENV_VALUE
1508 # endif
1509 #endif
1510
1511 void
1512 env_opt(unsigned char *buf, int len)
1513 {
1514 unsigned char *ep = 0, *epc = 0;
1515 int i;
1516
1517 switch(buf[0]&0xff) {
1518 case TELQUAL_SEND:
1519 env_opt_start();
1520 if (len == 1) {
1521 env_opt_add(NULL);
1522 } else for (i = 1; i < len; i++) {
1523 switch (buf[i]&0xff) {
1524 #ifdef OLD_ENVIRON
1525 case OLD_ENV_VAR:
1526 # ifdef ENV_HACK
1527 if (telopt_environ == TELOPT_OLD_ENVIRON
1528 && env_auto) {
1529 /* Server has the same definitions */
1530 old_env_var = OLD_ENV_VAR;
1531 old_env_value = OLD_ENV_VALUE;
1532 }
1533 /* FALL THROUGH */
1534 # endif
1535 case OLD_ENV_VALUE:
1536 /*
1537 * Although OLD_ENV_VALUE is not legal, we will
1538 * still recognize it, just in case it is an
1539 * old server that has VAR & VALUE mixed up...
1540 */
1541 /* FALL THROUGH */
1542 #else
1543 case NEW_ENV_VAR:
1544 #endif
1545 case ENV_USERVAR:
1546 if (ep) {
1547 *epc = 0;
1548 env_opt_add(ep);
1549 }
1550 ep = epc = &buf[i+1];
1551 break;
1552 case ENV_ESC:
1553 i++;
1554 /*FALL THROUGH*/
1555 default:
1556 if (epc)
1557 *epc++ = buf[i];
1558 break;
1559 }
1560 }
1561 if (ep) {
1562 *epc = 0;
1563 env_opt_add(ep);
1564 }
1565 env_opt_end(1);
1566 break;
1567
1568 case TELQUAL_IS:
1569 case TELQUAL_INFO:
1570 /* Ignore for now. We shouldn't get it anyway. */
1571 break;
1572
1573 default:
1574 break;
1575 }
1576 }
1577
1578 #define OPT_REPLY_SIZE 256
1579 unsigned char *opt_reply;
1580 unsigned char *opt_replyp;
1581 unsigned char *opt_replyend;
1582
1583 void
1584 env_opt_start(void)
1585 {
1586 unsigned char *p;
1587
1588 if (opt_reply) {
1589 p = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1590 if (p == NULL)
1591 free(opt_reply);
1592 } else
1593 p = (unsigned char *)malloc(OPT_REPLY_SIZE);
1594 opt_reply = p;
1595 if (opt_reply == NULL) {
1596 /*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1597 opt_reply = opt_replyp = opt_replyend = NULL;
1598 return;
1599 }
1600 opt_replyp = opt_reply;
1601 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1602 *opt_replyp++ = IAC;
1603 *opt_replyp++ = SB;
1604 *opt_replyp++ = telopt_environ;
1605 *opt_replyp++ = TELQUAL_IS;
1606 }
1607
1608 void
1609 env_opt_start_info(void)
1610 {
1611 env_opt_start();
1612 if (opt_replyp)
1613 opt_replyp[-1] = TELQUAL_INFO;
1614 }
1615
1616 void
1617 env_opt_add(unsigned char *ep)
1618 {
1619 unsigned char *vp, c;
1620 unsigned int len, olen, elen;
1621
1622 if (opt_reply == NULL) /*XXX*/
1623 return; /*XXX*/
1624
1625 if (ep == NULL || *ep == '\0') {
1626 /* Send user defined variables first. */
1627 env_default(1, 0);
1628 while ((ep = env_default(0, 0)) != NULL)
1629 env_opt_add(ep);
1630
1631 /* Now add the list of well know variables. */
1632 env_default(1, 1);
1633 while ((ep = env_default(0, 1)) != NULL)
1634 env_opt_add(ep);
1635 return;
1636 }
1637 vp = env_getvalue(ep);
1638 elen = 2 * (vp ? strlen((char *)vp) : 0) +
1639 2 * strlen((char *)ep) + 6;
1640 if ((unsigned int)(opt_replyend - opt_replyp) < elen)
1641 {
1642 unsigned char *p;
1643 len = opt_replyend - opt_reply + elen;
1644 olen = opt_replyp - opt_reply;
1645 p = (unsigned char *)realloc(opt_reply, len);
1646 if (p == NULL)
1647 free(opt_reply);
1648 opt_reply = p;
1649 if (opt_reply == NULL) {
1650 /*@*/ printf("env_opt_add: realloc() failed!!!\n");
1651 opt_reply = opt_replyp = opt_replyend = NULL;
1652 return;
1653 }
1654 opt_replyp = opt_reply + olen;
1655 opt_replyend = opt_reply + len;
1656 }
1657 if (opt_welldefined(ep))
1658 #ifdef OLD_ENVIRON
1659 if (telopt_environ == TELOPT_OLD_ENVIRON)
1660 *opt_replyp++ = old_env_var;
1661 else
1662 #endif
1663 *opt_replyp++ = NEW_ENV_VAR;
1664 else
1665 *opt_replyp++ = ENV_USERVAR;
1666 for (;;) {
1667 while ((c = *ep++) != '\0') {
1668 switch(c&0xff) {
1669 case IAC:
1670 *opt_replyp++ = IAC;
1671 break;
1672 case NEW_ENV_VAR:
1673 case NEW_ENV_VALUE:
1674 case ENV_ESC:
1675 case ENV_USERVAR:
1676 *opt_replyp++ = ENV_ESC;
1677 break;
1678 }
1679 *opt_replyp++ = c;
1680 }
1681 if ((ep = vp) != NULL) {
1682 #ifdef OLD_ENVIRON
1683 if (telopt_environ == TELOPT_OLD_ENVIRON)
1684 *opt_replyp++ = old_env_value;
1685 else
1686 #endif
1687 *opt_replyp++ = NEW_ENV_VALUE;
1688 vp = NULL;
1689 } else
1690 break;
1691 }
1692 }
1693
1694 int
1695 opt_welldefined(const char *ep)
1696 {
1697 if ((strcmp(ep, "USER") == 0) ||
1698 (strcmp(ep, "DISPLAY") == 0) ||
1699 (strcmp(ep, "PRINTER") == 0) ||
1700 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1701 (strcmp(ep, "JOB") == 0) ||
1702 (strcmp(ep, "ACCT") == 0))
1703 return(1);
1704 return(0);
1705 }
1706 void
1707 env_opt_end(int emptyok)
1708 {
1709 int len;
1710
1711 len = opt_replyp - opt_reply + 2;
1712 if (emptyok || len > 6) {
1713 *opt_replyp++ = IAC;
1714 *opt_replyp++ = SE;
1715 if (NETROOM() > len) {
1716 ring_supply_data(&netoring, opt_reply, len);
1717 printsub('>', &opt_reply[2], len - 2);
1718 }
1719 /*@*/ else printf("slc_end_reply: not enough room\n");
1720 }
1721 if (opt_reply) {
1722 free(opt_reply);
1723 opt_reply = opt_replyp = opt_replyend = NULL;
1724 }
1725 }
1726
1727
1728
1730 int
1731 telrcv(void)
1732 {
1733 int c;
1734 int scc;
1735 unsigned char *sbp = NULL;
1736 int count;
1737 int returnValue = 0;
1738
1739 scc = 0;
1740 count = 0;
1741 while (TTYROOM() > 2) {
1742 if (scc == 0) {
1743 if (count) {
1744 ring_consumed(&netiring, count);
1745 returnValue = 1;
1746 count = 0;
1747 }
1748 sbp = netiring.consume;
1749 scc = ring_full_consecutive(&netiring);
1750 if (scc == 0) {
1751 /* No more data coming in */
1752 break;
1753 }
1754 }
1755
1756 c = *sbp++ & 0xff, scc--; count++;
1757 #ifdef ENCRYPTION
1758 if (decrypt_input)
1759 c = (*decrypt_input)(c);
1760 #endif /* ENCRYPTION */
1761
1762 switch (telrcv_state) {
1763
1764 case TS_CR:
1765 telrcv_state = TS_DATA;
1766 if (c == '\0') {
1767 break; /* Ignore \0 after CR */
1768 }
1769 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1770 TTYADD(c);
1771 break;
1772 }
1773 /* Else, fall through */
1774
1775 case TS_DATA:
1776 if (c == IAC) {
1777 telrcv_state = TS_IAC;
1778 break;
1779 }
1780 # if defined(TN3270)
1781 if (In3270) {
1782 *Ifrontp++ = c;
1783 while (scc > 0) {
1784 c = *sbp++ & 0377, scc--; count++;
1785 #ifdef ENCRYPTION
1786 if (decrypt_input)
1787 c = (*decrypt_input)(c);
1788 #endif /* ENCRYPTION */
1789 if (c == IAC) {
1790 telrcv_state = TS_IAC;
1791 break;
1792 }
1793 *Ifrontp++ = c;
1794 }
1795 } else
1796 # endif /* defined(TN3270) */
1797 /*
1798 * The 'crmod' hack (see following) is needed
1799 * since we can't * set CRMOD on output only.
1800 * Machines like MULTICS like to send \r without
1801 * \n; since we must turn off CRMOD to get proper
1802 * input, the mapping is done here (sigh).
1803 */
1804 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1805 if (scc > 0) {
1806 c = *sbp&0xff;
1807 #ifdef ENCRYPTION
1808 if (decrypt_input)
1809 c = (*decrypt_input)(c);
1810 #endif /* ENCRYPTION */
1811 if (c == 0) {
1812 sbp++, scc--; count++;
1813 /* a "true" CR */
1814 TTYADD('\r');
1815 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1816 (c == '\n')) {
1817 sbp++, scc--; count++;
1818 TTYADD('\n');
1819 } else {
1820 #ifdef ENCRYPTION
1821 if (decrypt_input)
1822 (*decrypt_input)(-1);
1823 #endif /* ENCRYPTION */
1824
1825 TTYADD('\r');
1826 if (crmod) {
1827 TTYADD('\n');
1828 }
1829 }
1830 } else {
1831 telrcv_state = TS_CR;
1832 TTYADD('\r');
1833 if (crmod) {
1834 TTYADD('\n');
1835 }
1836 }
1837 } else {
1838 TTYADD(c);
1839 }
1840 continue;
1841
1842 case TS_IAC:
1843 process_iac:
1844 switch (c) {
1845
1846 case WILL:
1847 telrcv_state = TS_WILL;
1848 continue;
1849
1850 case WONT:
1851 telrcv_state = TS_WONT;
1852 continue;
1853
1854 case DO:
1855 telrcv_state = TS_DO;
1856 continue;
1857
1858 case DONT:
1859 telrcv_state = TS_DONT;
1860 continue;
1861
1862 case DM:
1863 /*
1864 * We may have missed an urgent notification,
1865 * so make sure we flush whatever is in the
1866 * buffer currently.
1867 */
1868 printoption("RCVD", IAC, DM);
1869 SYNCHing = 1;
1870 (void) ttyflush(1);
1871 SYNCHing = stilloob();
1872 settimer(gotDM);
1873 break;
1874
1875 case SB:
1876 SB_CLEAR();
1877 telrcv_state = TS_SB;
1878 continue;
1879
1880 # if defined(TN3270)
1881 case EOR:
1882 if (In3270) {
1883 if (Ibackp == Ifrontp) {
1884 Ibackp = Ifrontp = Ibuf;
1885 ISend = 0; /* should have been! */
1886 } else {
1887 Ibackp += DataFromNetwork(Ibackp, Ifrontp-Ibackp, 1);
1888 ISend = 1;
1889 }
1890 }
1891 printoption("RCVD", IAC, EOR);
1892 break;
1893 # endif /* defined(TN3270) */
1894
1895 case IAC:
1896 # if !defined(TN3270)
1897 TTYADD(IAC);
1898 # else /* !defined(TN3270) */
1899 if (In3270) {
1900 *Ifrontp++ = IAC;
1901 } else {
1902 TTYADD(IAC);
1903 }
1904 # endif /* !defined(TN3270) */
1905 break;
1906
1907 case NOP:
1908 case GA:
1909 default:
1910 printoption("RCVD", IAC, c);
1911 break;
1912 }
1913 telrcv_state = TS_DATA;
1914 continue;
1915
1916 case TS_WILL:
1917 printoption("RCVD", WILL, c);
1918 willoption(c);
1919 SetIn3270();
1920 telrcv_state = TS_DATA;
1921 continue;
1922
1923 case TS_WONT:
1924 printoption("RCVD", WONT, c);
1925 wontoption(c);
1926 SetIn3270();
1927 telrcv_state = TS_DATA;
1928 continue;
1929
1930 case TS_DO:
1931 printoption("RCVD", DO, c);
1932 dooption(c);
1933 SetIn3270();
1934 if (c == TELOPT_NAWS) {
1935 sendnaws();
1936 } else if (c == TELOPT_LFLOW) {
1937 localflow = 1;
1938 setcommandmode();
1939 setconnmode(0);
1940 }
1941 telrcv_state = TS_DATA;
1942 continue;
1943
1944 case TS_DONT:
1945 printoption("RCVD", DONT, c);
1946 dontoption(c);
1947 flushline = 1;
1948 setconnmode(0); /* set new tty mode (maybe) */
1949 SetIn3270();
1950 telrcv_state = TS_DATA;
1951 continue;
1952
1953 case TS_SB:
1954 if (c == IAC) {
1955 telrcv_state = TS_SE;
1956 } else {
1957 SB_ACCUM(c);
1958 }
1959 continue;
1960
1961 case TS_SE:
1962 if (c != SE) {
1963 if (c != IAC) {
1964 /*
1965 * This is an error. We only expect to get
1966 * "IAC IAC" or "IAC SE". Several things may
1967 * have happened. An IAC was not doubled, the
1968 * IAC SE was left off, or another option got
1969 * inserted into the suboption are all possibilities.
1970 * If we assume that the IAC was not doubled,
1971 * and really the IAC SE was left off, we could
1972 * get into an infinite loop here. So, instead,
1973 * we terminate the suboption, and process the
1974 * partial suboption if we can.
1975 */
1976 SB_ACCUM(IAC);
1977 SB_ACCUM(c);
1978 subpointer -= 2;
1979 SB_TERM();
1980
1981 printoption("In SUBOPTION processing, RCVD", IAC, c);
1982 suboption(); /* handle sub-option */
1983 SetIn3270();
1984 telrcv_state = TS_IAC;
1985 goto process_iac;
1986 }
1987 SB_ACCUM(c);
1988 telrcv_state = TS_SB;
1989 } else {
1990 SB_ACCUM(IAC);
1991 SB_ACCUM(SE);
1992 subpointer -= 2;
1993 SB_TERM();
1994 suboption(); /* handle sub-option */
1995 SetIn3270();
1996 telrcv_state = TS_DATA;
1997 }
1998 }
1999 }
2000 if (count)
2001 ring_consumed(&netiring, count);
2002 return returnValue||count;
2003 }
2004
2005 static int bol = 1, local = 0;
2006
2007 int
2008 rlogin_susp(void)
2009 {
2010 if (local) {
2011 local = 0;
2012 bol = 1;
2013 command(0, "z\n", 2);
2014 return(1);
2015 }
2016 return(0);
2017 }
2018
2019 static int
2020 telsnd(void)
2021 {
2022 int tcc;
2023 int count;
2024 int returnValue = 0;
2025 unsigned char *tbp = NULL;
2026
2027 tcc = 0;
2028 count = 0;
2029 while (NETROOM() > 2) {
2030 int sc;
2031 int c;
2032
2033 if (tcc == 0) {
2034 if (count) {
2035 ring_consumed(&ttyiring, count);
2036 returnValue = 1;
2037 count = 0;
2038 }
2039 tbp = ttyiring.consume;
2040 tcc = ring_full_consecutive(&ttyiring);
2041 if (tcc == 0) {
2042 break;
2043 }
2044 }
2045 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
2046 if (rlogin != _POSIX_VDISABLE) {
2047 if (bol) {
2048 bol = 0;
2049 if (sc == rlogin) {
2050 local = 1;
2051 continue;
2052 }
2053 } else if (local) {
2054 local = 0;
2055 if (sc == '.' || c == termEofChar) {
2056 bol = 1;
2057 command(0, "close\n", 6);
2058 continue;
2059 }
2060 if (sc == termSuspChar) {
2061 bol = 1;
2062 command(0, "z\n", 2);
2063 continue;
2064 }
2065 if (sc == escape) {
2066 command(0, (char *)tbp, tcc);
2067 bol = 1;
2068 count += tcc;
2069 tcc = 0;
2070 flushline = 1;
2071 break;
2072 }
2073 if (sc != rlogin) {
2074 ++tcc;
2075 --tbp;
2076 --count;
2077 c = sc = rlogin;
2078 }
2079 }
2080 if ((sc == '\n') || (sc == '\r'))
2081 bol = 1;
2082 } else if (sc == escape && escape != _POSIX_VDISABLE) {
2083 /*
2084 * Double escape is a pass through of a single escape character.
2085 */
2086 if (tcc && strip(*tbp) == escape) {
2087 tbp++;
2088 tcc--;
2089 count++;
2090 bol = 0;
2091 } else {
2092 command(0, (char *)tbp, tcc);
2093 bol = 1;
2094 count += tcc;
2095 tcc = 0;
2096 flushline = 1;
2097 break;
2098 }
2099 } else
2100 bol = 0;
2101 #ifdef KLUDGELINEMODE
2102 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
2103 if (tcc > 0 && strip(*tbp) == echoc) {
2104 tcc--; tbp++; count++;
2105 } else {
2106 dontlecho = !dontlecho;
2107 settimer(echotoggle);
2108 setconnmode(0);
2109 flushline = 1;
2110 break;
2111 }
2112 }
2113 #endif
2114 if (sc != _POSIX_VDISABLE && MODE_LOCAL_CHARS(globalmode)) {
2115 if (TerminalSpecialChars(sc) == 0) {
2116 bol = 1;
2117 break;
2118 }
2119 }
2120 if (my_want_state_is_wont(TELOPT_BINARY)) {
2121 switch (c) {
2122 case '\n':
2123 /*
2124 * If we are in CRMOD mode (\r ==> \n)
2125 * on our local machine, then probably
2126 * a newline (unix) is CRLF (TELNET).
2127 */
2128 if (MODE_LOCAL_CHARS(globalmode)) {
2129 NETADD('\r');
2130 }
2131 NETADD('\n');
2132 bol = flushline = 1;
2133 break;
2134 case '\r':
2135 if (!crlf) {
2136 NET2ADD('\r', '\0');
2137 } else {
2138 NET2ADD('\r', '\n');
2139 }
2140 bol = flushline = 1;
2141 break;
2142 case IAC:
2143 NET2ADD(IAC, IAC);
2144 break;
2145 default:
2146 NETADD(c);
2147 break;
2148 }
2149 } else if (c == IAC) {
2150 NET2ADD(IAC, IAC);
2151 } else {
2152 NETADD(c);
2153 }
2154 }
2155 if (count)
2156 ring_consumed(&ttyiring, count);
2157 return returnValue||count; /* Non-zero if we did anything */
2158 }
2159
2160 /*
2162 * Scheduler()
2163 *
2164 * Try to do something.
2165 *
2166 * If we do something useful, return 1; else return 0.
2167 *
2168 */
2169
2170
2171 int
2172 Scheduler(int block) /* should we block in the select ? */
2173 {
2174 /* One wants to be a bit careful about setting returnValue
2175 * to one, since a one implies we did some useful work,
2176 * and therefore probably won't be called to block next
2177 * time (TN3270 mode only).
2178 */
2179 int returnValue;
2180 int netin, netout, netex, ttyin, ttyout;
2181
2182 /* Decide which rings should be processed */
2183
2184 netout = ring_full_count(&netoring) &&
2185 (flushline ||
2186 (my_want_state_is_wont(TELOPT_LINEMODE)
2187 #ifdef KLUDGELINEMODE
2188 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2189 #endif
2190 ) ||
2191 my_want_state_is_will(TELOPT_BINARY));
2192 ttyout = ring_full_count(&ttyoring);
2193
2194 #ifdef TN3270
2195 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0) && (shell_active == 0);
2196 #else /* defined(TN3270) */
2197 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
2198 #endif /* defined(TN3270) */
2199
2200 #ifdef TN3270
2201 netin = ring_empty_count(&netiring);
2202 # else /* !defined(TN3270) */
2203 netin = !ISend && ring_empty_count(&netiring);
2204 # endif /* !defined(TN3270) */
2205
2206 netex = !SYNCHing;
2207
2208 /* If we have seen a signal recently, reset things */
2209 # ifdef TN3270
2210 if (HaveInput) {
2211 HaveInput = 0;
2212 (void) signal(SIGIO, inputAvailable);
2213 }
2214 #endif /* defined(TN3270) */
2215
2216 /* Call to system code to process rings */
2217
2218 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2219
2220 /* Now, look at the input rings, looking for work to do. */
2221
2222 if (ring_full_count(&ttyiring)) {
2223 # if defined(TN3270)
2224 if (In3270) {
2225 int c;
2226
2227 c = DataFromTerminal(ttyiring.consume,
2228 ring_full_consecutive(&ttyiring));
2229 if (c) {
2230 returnValue = 1;
2231 ring_consumed(&ttyiring, c);
2232 }
2233 } else {
2234 # endif /* defined(TN3270) */
2235 returnValue |= telsnd();
2236 # if defined(TN3270)
2237 }
2238 # endif /* defined(TN3270) */
2239 }
2240
2241 if (ring_full_count(&netiring)) {
2242 # if !defined(TN3270)
2243 returnValue |= telrcv();
2244 # else /* !defined(TN3270) */
2245 returnValue = Push3270();
2246 # endif /* !defined(TN3270) */
2247 }
2248 return returnValue;
2249 }
2250
2251 /*
2253 * Select from tty and network...
2254 */
2255 void
2256 telnet(const char *user)
2257 {
2258 sys_telnet_init();
2259
2260 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2261 {
2262 static char local_host[MAXHOSTNAMELEN + 1] = { 0 };
2263
2264 if (!local_host[0]) {
2265 gethostname(local_host, sizeof(local_host));
2266 local_host[sizeof(local_host)-1] = 0;
2267 }
2268 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2269 auth_encrypt_user(user);
2270 }
2271 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2272 # if !defined(TN3270)
2273 if (telnetport) {
2274 #ifdef AUTHENTICATION
2275 if (autologin)
2276 send_will(TELOPT_AUTHENTICATION, 1);
2277 #endif
2278 #ifdef ENCRYPTION
2279 send_do(TELOPT_ENCRYPT, 1);
2280 send_will(TELOPT_ENCRYPT, 1);
2281 #endif /* ENCRYPTION */
2282 send_do(TELOPT_SGA, 1);
2283 send_will(TELOPT_TTYPE, 1);
2284 send_will(TELOPT_NAWS, 1);
2285 send_will(TELOPT_TSPEED, 1);
2286 send_will(TELOPT_LFLOW, 1);
2287 send_will(TELOPT_LINEMODE, 1);
2288 send_will(TELOPT_NEW_ENVIRON, 1);
2289 send_do(TELOPT_STATUS, 1);
2290 if (env_getvalue((const unsigned char *)"DISPLAY"))
2291 send_will(TELOPT_XDISPLOC, 1);
2292 if (eight)
2293 tel_enter_binary(eight);
2294 }
2295 # endif /* !defined(TN3270) */
2296
2297 # if !defined(TN3270)
2298 for (;;) {
2299 int schedValue;
2300
2301 while ((schedValue = Scheduler(0)) != 0) {
2302 if (schedValue == -1) {
2303 setcommandmode();
2304 return;
2305 }
2306 }
2307
2308 if (Scheduler(1) == -1) {
2309 setcommandmode();
2310 return;
2311 }
2312 }
2313 # else /* !defined(TN3270) */
2314 for (;;) {
2315 int schedValue;
2316
2317 while (!In3270 && !shell_active) {
2318 if (Scheduler(1) == -1) {
2319 setcommandmode();
2320 return;
2321 }
2322 }
2323
2324 while ((schedValue = Scheduler(0)) != 0) {
2325 if (schedValue == -1) {
2326 setcommandmode();
2327 return;
2328 }
2329 }
2330 /* If there is data waiting to go out to terminal, don't
2331 * schedule any more data for the terminal.
2332 */
2333 if (ring_full_count(&ttyoring)) {
2334 schedValue = 1;
2335 } else {
2336 if (shell_active) {
2337 if (shell_continue() == 0) {
2338 ConnectScreen();
2339 }
2340 } else if (In3270) {
2341 schedValue = DoTerminalOutput();
2342 }
2343 }
2344 if (schedValue && (shell_active == 0)) {
2345 if (Scheduler(1) == -1) {
2346 setcommandmode();
2347 return;
2348 }
2349 }
2350 }
2351 # endif /* !defined(TN3270) */
2352 }
2353
2354 #if 0 /* XXX - this not being in is a bug */
2356 /*
2357 * nextitem()
2358 *
2359 * Return the address of the next "item" in the TELNET data
2360 * stream. This will be the address of the next character if
2361 * the current address is a user data character, or it will
2362 * be the address of the character following the TELNET command
2363 * if the current address is a TELNET IAC ("I Am a Command")
2364 * character.
2365 */
2366
2367 static char *
2368 nextitem(char *current)
2369 {
2370 if ((*current&0xff) != IAC) {
2371 return current+1;
2372 }
2373 switch (*(current+1)&0xff) {
2374 case DO:
2375 case DONT:
2376 case WILL:
2377 case WONT:
2378 return current+3;
2379 case SB: /* loop forever looking for the SE */
2380 {
2381 char *look = current+2;
2382
2383 for (;;) {
2384 if ((*look++&0xff) == IAC) {
2385 if ((*look++&0xff) == SE) {
2386 return look;
2387 }
2388 }
2389 }
2390 }
2391 default:
2392 return current+2;
2393 }
2394 }
2395 #endif /* 0 */
2396
2397 /*
2398 * netclear()
2399 *
2400 * We are about to do a TELNET SYNCH operation. Clear
2401 * the path to the network.
2402 *
2403 * Things are a bit tricky since we may have sent the first
2404 * byte or so of a previous TELNET command into the network.
2405 * So, we have to scan the network buffer from the beginning
2406 * until we are up to where we want to be.
2407 *
2408 * A side effect of what we do, just to keep things
2409 * simple, is to clear the urgent data pointer. The principal
2410 * caller should be setting the urgent data pointer AFTER calling
2411 * us in any case.
2412 */
2413
2414 static void
2415 netclear(void)
2416 {
2417 #if 0 /* XXX */
2418 char *thisitem, *next;
2419 char *good;
2420 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2421 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2422
2423 thisitem = netobuf;
2424
2425 while ((next = nextitem(thisitem)) <= netobuf.send) {
2426 thisitem = next;
2427 }
2428
2429 /* Now, thisitem is first before/at boundary. */
2430
2431 good = netobuf; /* where the good bytes go */
2432
2433 while (netoring.add > thisitem) {
2434 if (wewant(thisitem)) {
2435 int length;
2436
2437 next = thisitem;
2438 do {
2439 next = nextitem(next);
2440 } while (wewant(next) && (nfrontp > next));
2441 length = next-thisitem;
2442 memmove(good, thisitem, length);
2443 good += length;
2444 thisitem = next;
2445 } else {
2446 thisitem = nextitem(thisitem);
2447 }
2448 }
2449
2450 #endif /* 0 */
2451 }
2452
2453 /*
2455 * These routines add various telnet commands to the data stream.
2456 */
2457
2458 static void
2459 doflush(void)
2460 {
2461 NET2ADD(IAC, DO);
2462 NETADD(TELOPT_TM);
2463 flushline = 1;
2464 flushout = 1;
2465 (void) ttyflush(1); /* Flush/drop output */
2466 /* do printoption AFTER flush, otherwise the output gets tossed... */
2467 printoption("SENT", DO, TELOPT_TM);
2468 }
2469
2470 void
2471 xmitAO(void)
2472 {
2473 NET2ADD(IAC, AO);
2474 printoption("SENT", IAC, AO);
2475 if (autoflush) {
2476 doflush();
2477 }
2478 }
2479
2480
2481 void
2482 xmitEL(void)
2483 {
2484 NET2ADD(IAC, EL);
2485 printoption("SENT", IAC, EL);
2486 }
2487
2488 void
2489 xmitEC(void)
2490 {
2491 NET2ADD(IAC, EC);
2492 printoption("SENT", IAC, EC);
2493 }
2494
2495
2496 int
2497 dosynch(char *s)
2498 {
2499 netclear(); /* clear the path to the network */
2500 NETADD(IAC);
2501 setneturg();
2502 NETADD(DM);
2503 printoption("SENT", IAC, DM);
2504 return 1;
2505 }
2506
2507 int want_status_response = 0;
2508
2509 int
2510 get_status(char *s)
2511 {
2512 unsigned char tmp[16];
2513 unsigned char *cp;
2514
2515 if (my_want_state_is_dont(TELOPT_STATUS)) {
2516 printf("Remote side does not support STATUS option\n");
2517 return 0;
2518 }
2519 cp = tmp;
2520
2521 *cp++ = IAC;
2522 *cp++ = SB;
2523 *cp++ = TELOPT_STATUS;
2524 *cp++ = TELQUAL_SEND;
2525 *cp++ = IAC;
2526 *cp++ = SE;
2527 if (NETROOM() >= cp - tmp) {
2528 ring_supply_data(&netoring, tmp, cp-tmp);
2529 printsub('>', tmp+2, cp - tmp - 2);
2530 }
2531 ++want_status_response;
2532 return 1;
2533 }
2534
2535 void
2536 intp(void)
2537 {
2538 NET2ADD(IAC, IP);
2539 printoption("SENT", IAC, IP);
2540 flushline = 1;
2541 if (autoflush) {
2542 doflush();
2543 }
2544 if (autosynch) {
2545 dosynch(NULL);
2546 }
2547 }
2548
2549 void
2550 sendbrk(void)
2551 {
2552 NET2ADD(IAC, BREAK);
2553 printoption("SENT", IAC, BREAK);
2554 flushline = 1;
2555 if (autoflush) {
2556 doflush();
2557 }
2558 if (autosynch) {
2559 dosynch(NULL);
2560 }
2561 }
2562
2563 void
2564 sendabort(void)
2565 {
2566 NET2ADD(IAC, ABORT);
2567 printoption("SENT", IAC, ABORT);
2568 flushline = 1;
2569 if (autoflush) {
2570 doflush();
2571 }
2572 if (autosynch) {
2573 dosynch(NULL);
2574 }
2575 }
2576
2577 void
2578 sendsusp(void)
2579 {
2580 NET2ADD(IAC, SUSP);
2581 printoption("SENT", IAC, SUSP);
2582 flushline = 1;
2583 if (autoflush) {
2584 doflush();
2585 }
2586 if (autosynch) {
2587 dosynch(NULL);
2588 }
2589 }
2590
2591 void
2592 sendeof(void)
2593 {
2594 NET2ADD(IAC, xEOF);
2595 printoption("SENT", IAC, xEOF);
2596 }
2597
2598 void
2599 sendayt(void)
2600 {
2601 NET2ADD(IAC, AYT);
2602 printoption("SENT", IAC, AYT);
2603 }
2604
2605 /*
2606 * Send a window size update to the remote system.
2607 */
2608
2609 void
2610 sendnaws(void)
2611 {
2612 long rows, cols;
2613 unsigned char tmp[16];
2614 unsigned char *cp;
2615
2616 if (my_state_is_wont(TELOPT_NAWS))
2617 return;
2618
2619 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2620 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2621
2622 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2623 return;
2624 }
2625
2626 cp = tmp;
2627
2628 *cp++ = IAC;
2629 *cp++ = SB;
2630 *cp++ = TELOPT_NAWS;
2631 PUTSHORT(cp, cols);
2632 PUTSHORT(cp, rows);
2633 *cp++ = IAC;
2634 *cp++ = SE;
2635 if (NETROOM() >= cp - tmp) {
2636 ring_supply_data(&netoring, tmp, cp-tmp);
2637 printsub('>', tmp+2, cp - tmp - 2);
2638 }
2639 }
2640
2641 void
2642 tel_enter_binary(int rw)
2643 {
2644 if (rw&1)
2645 send_do(TELOPT_BINARY, 1);
2646 if (rw&2)
2647 send_will(TELOPT_BINARY, 1);
2648 }
2649
2650 void
2651 tel_leave_binary(int rw)
2652 {
2653 if (rw&1)
2654 send_dont(TELOPT_BINARY, 1);
2655 if (rw&2)
2656 send_wont(TELOPT_BINARY, 1);
2657 }
2658