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