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