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