Debian Codegraph @

telnet/telnet.c

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