root/src/noit_console_telnet.c

Revision fb10ef1ee5f5311cf9183562560fdc6f2ec418ee, 79.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 9 months ago)

Fix various coverity issues.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2007
5  *      OmnitTI Computer Consulting, Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "noit_defines.h"
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <sys/ioctl.h>
40 #include <signal.h>
41 #ifdef HAVE_SYS_IOCTL_COMPAT_H
42 #include <sys/ioctl_compat.h>
43 #endif
44 #define TELOPTS
45 #include <arpa/telnet.h>
46 #include "noit_console.h"
47 #include "noit_console_telnet.h"
48 #include "utils/noit_hash.h"
49
50 /*
51 RCSID("$Id: state.c,v 1.14.12.1 2004/06/21 08:21:58 lha Exp $");
52 */
53
54 unsigned char   doopt[] = { IAC, DO, '%', 'c', 0 };
55 unsigned char   dont[] = { IAC, DONT, '%', 'c', 0 };
56 unsigned char   will[] = { IAC, WILL, '%', 'c', 0 };
57 unsigned char   wont[] = { IAC, WONT, '%', 'c', 0 };
58 int     not42 = 1;
59
60 /*
61  * Buffer for sub-options, and macros
62  * for suboptions buffer manipulations
63  */
64
65 #define subbuffer ncct->telnet->_subbuffer
66 #define subpointer ncct->telnet->_subpointer
67 #define subend ncct->telnet->_subend
68 #define subsave ncct->telnet->_subsave
69 #define slctab ncct->telnet->_slctab
70 #define pty ncct->pty_master
71 #define do_dont_resp ncct->telnet->_do_dont_resp
72 #define will_wont_resp ncct->telnet->_will_wont_resp
73 #define termbuf ncct->telnet->_termbuf
74 #define termbuf2 ncct->telnet->_termbuf2
75 #define SYNCHing ncct->telnet->_SYNCHing
76 #define terminalname ncct->telnet->_terminalname
77 #define flowmode ncct->telnet->_flowmode
78 #define linemode ncct->telnet->_linemode
79 #define uselinemode ncct->telnet->_uselinemode
80 #define alwayslinemode ncct->telnet->_alwayslinemode
81 #define restartany ncct->telnet->_restartany
82 #define _terminit ncct->telnet->__terminit
83 #define turn_on_sga ncct->telnet->_turn_on_sga
84 #define useeditmode ncct->telnet->_useeditmode
85 #define editmode ncct->telnet->_editmode
86 #define def_slcbuf ncct->telnet->_def_slcbuf
87 #define def_slclen ncct->telnet->_def_slclen
88 #define slcchange ncct->telnet->_slcchange
89 #define slcptr ncct->telnet->_slcptr
90 #define slcbuf ncct->telnet->_slcbuf
91 #define def_tspeed ncct->telnet->_def_tspeed
92 #define def_rspeed ncct->telnet->_def_rspeed
93 #define def_row ncct->telnet->_def_row
94 #define def_col ncct->telnet->_def_col
95
96 #define settimer(x)     (ncct->telnet->_clocks.x = ++ncct->telnet->_clocks.system)
97 #define sequenceIs(x,y) (ncct->telnet->_clocks.x < ncct->telnet->_clocks.y)
98
99 #define dooption(a) noit_console_telnet_dooption(ncct,a)
100 #define dontoption(a) noit_console_telnet_dontoption(ncct,a)
101 #define willoption(a) noit_console_telnet_willoption(ncct,a)
102 #define wontoption(a) noit_console_telnet_wontoption(ncct,a)
103 #define send_do(a,i) noit_console_telnet_send_do(ncct,a,i)
104 #define send_dont(a,i) noit_console_telnet_send_dont(ncct,a,i)
105 #define send_will(a,i) noit_console_telnet_send_will(ncct,a,i)
106 #define send_wont(a,i) noit_console_telnet_send_wont(ncct,a,i)
107 #define send_status() noit_console_telnet_send_status(ncct)
108 #define doclientstat() noit_console_telnet_doclientstat(ncct)
109
110 #ifndef TCSIG
111 # ifdef TIOCSIG
112 #  define TCSIG TIOCSIG
113 # endif
114 #endif
115
116 static void
117 netflush(noit_console_closure_t ncct) {
118   int unused;
119   noit_console_continue_sending(ncct, &unused);
120 }
121 static void set_termbuf(noit_console_closure_t ncct);
122 static void init_termbuf(noit_console_closure_t ncct);
123 static void flowstat(noit_console_closure_t ncct);
124 static int spcset(noit_console_closure_t ncct, int func, cc_t *valp, cc_t **valpp);
125 static void suboption(noit_console_closure_t ncct);
126 static void get_slc_defaults(noit_console_closure_t ncct);
127
128 #ifdef LINEMODE
129 static void defer_terminit(noit_console_closure_t ncct);
130 static void send_slc(noit_console_closure_t ncct);
131 static void default_slc(noit_console_closure_t ncct);
132 static void add_slc(noit_console_closure_t ncct, char func, char flag, cc_t val);
133 static void start_slc(noit_console_closure_t ncct, int getit);
134 static int end_slc(noit_console_closure_t ncct, unsigned char **bufp);
135 static void process_slc(noit_console_closure_t ncct, unsigned char func, unsigned char flag, cc_t val);
136 static void change_slc(noit_console_closure_t ncct, char func, char flag, cc_t val);
137 static void check_slc(noit_console_closure_t ncct);
138 static void do_opt_slc(noit_console_closure_t ncct, unsigned char *ptr, int len);
139 #endif
140
141 #ifdef LINEMODE
142 static void
143 send_slc(noit_console_closure_t ncct)
144 {
145         int i;
146
147         /*
148          * Send out list of triplets of special characters
149          * to client.  We only send info on the characters
150          * that are currently supported.
151          */
152         for (i = 1; i <= NSLC; i++) {
153                 if ((slctab[i].defset.flag & SLC_LEVELBITS) == SLC_NOSUPPORT)
154                         continue;
155                 add_slc(ncct, (unsigned char)i, slctab[i].current.flag,
156                                                         slctab[i].current.val);
157         }
158
159 }  /* end of send_slc */
160
161 /*
162  * default_slc
163  *
164  * Set pty special characters to all the defaults.
165  */
166 static void
167 default_slc(noit_console_closure_t ncct)
168 {
169         int i;
170
171         for (i = 1; i <= NSLC; i++) {
172                 slctab[i].current.val = slctab[i].defset.val;
173                 if (slctab[i].current.val == (cc_t)(_POSIX_VDISABLE))
174                         slctab[i].current.flag = SLC_NOSUPPORT;
175                 else
176                         slctab[i].current.flag = slctab[i].defset.flag;
177                 if (slctab[i].sptr) {
178                         *(slctab[i].sptr) = slctab[i].defset.val;
179                 }
180         }
181         slcchange = 1;
182
183 }  /* end of default_slc */
184 #endif  /* LINEMODE */
185
186 /*
187  * get_slc_defaults
188  *
189  * Initialize the slc mapping table.
190  */
191 static void
192 get_slc_defaults(noit_console_closure_t ncct)
193 {
194         int i;
195
196         init_termbuf(ncct);
197
198         for (i = 1; i <= NSLC; i++) {
199                 slctab[i].defset.flag =
200                         spcset(ncct, i, &slctab[i].defset.val, &slctab[i].sptr);
201                 slctab[i].current.flag = SLC_NOSUPPORT;
202                 slctab[i].current.val = 0;
203         }
204
205 }  /* end of get_slc_defaults */
206
207 #ifdef  LINEMODE
208 /*
209  * add_slc
210  *
211  * Add an slc triplet to the slc buffer.
212  */
213 static void
214 add_slc(noit_console_closure_t ncct, char func, char flag, cc_t val)
215 {
216
217         if ((*slcptr++ = (unsigned char)func) == 0xff)
218                 *slcptr++ = 0xff;
219
220         if ((*slcptr++ = (unsigned char)flag) == 0xff)
221                 *slcptr++ = 0xff;
222
223         if ((*slcptr++ = (unsigned char)val) == 0xff)
224                 *slcptr++ = 0xff;
225
226 }  /* end of add_slc */
227
228 /*
229  * start_slc
230  *
231  * Get ready to process incoming slc's and respond to them.
232  *
233  * The parameter getit is non-zero if it is necessary to grab a copy
234  * of the terminal control structures.
235  */
236 static void
237 start_slc(noit_console_closure_t ncct, int getit)
238 {
239
240         slcchange = 0;
241         if (getit)
242                 init_termbuf(ncct);
243         (void) snprintf((char *)slcbuf, 5, "%c%c%c%c",
244                                         IAC, SB, TELOPT_LINEMODE, LM_SLC);
245         slcptr = slcbuf + 4;
246
247 }  /* end of start_slc */
248
249 /*
250  * end_slc
251  *
252  * Finish up the slc negotiation.  If something to send, then send it.
253  */
254 int
255 end_slc(noit_console_closure_t ncct, unsigned char **bufp)
256 {
257         int len;
258
259         /*
260          * If a change has occured, store the new terminal control
261          * structures back to the terminal driver.
262          */
263         if (slcchange) {
264                 set_termbuf(ncct);
265         }
266
267         /*
268          * If the pty state has not yet been fully processed and there is a
269          * deferred slc request from the client, then do not send any
270          * sort of slc negotiation now.  We will respond to the client's
271          * request very soon.
272          */
273         if (def_slcbuf && (_terminit == 0)) {
274                 return(0);
275         }
276
277         if (slcptr > (slcbuf + 4)) {
278                 if (bufp) {
279                         *bufp = &slcbuf[4];
280                         return(slcptr - slcbuf - 4);
281                 } else {
282                         (void) snprintf((char *)slcptr, 3, "%c%c", IAC, SE);
283                         slcptr += 2;
284                         len = slcptr - slcbuf;
285                         nc_write(ncct, slcbuf, len);
286                         netflush(ncct);  /* force it out immediately */
287                 }
288         }
289         return (0);
290
291 }  /* end of end_slc */
292
293 /*
294  * process_slc
295  *
296  * Figure out what to do about the client's slc
297  */
298 static void
299 process_slc(noit_console_closure_t ncct, unsigned char func, unsigned char flag, cc_t val)
300 {
301         int hislevel, mylevel, ack;
302
303         /*
304          * Ensure that we know something about this function
305          */
306         if (func > NSLC) {
307                 add_slc(ncct, func, SLC_NOSUPPORT, 0);
308                 return;
309         }
310
311         /*
312          * Process the special case requests of 0 SLC_DEFAULT 0
313          * and 0 SLC_VARIABLE 0.  Be a little forgiving here, don't
314          * worry about whether the value is actually 0 or not.
315          */
316         if (func == 0) {
317                 if ((flag = flag & SLC_LEVELBITS) == SLC_DEFAULT) {
318                         default_slc(ncct);
319                         send_slc(ncct);
320                 } else if (flag == SLC_VARIABLE) {
321                         send_slc(ncct);
322                 }
323                 return;
324         }
325
326         /*
327          * Appears to be a function that we know something about.  So
328          * get on with it and see what we know.
329          */
330
331         hislevel = flag & SLC_LEVELBITS;
332         mylevel = slctab[func].current.flag & SLC_LEVELBITS;
333         ack = flag & SLC_ACK;
334         /*
335          * ignore the command if:
336          * the function value and level are the same as what we already have;
337          * or the level is the same and the ack bit is set
338          */
339         if (hislevel == mylevel && (val == slctab[func].current.val || ack)) {
340                 return;
341         } else if (ack) {
342                 /*
343                  * If we get here, we got an ack, but the levels don't match.
344                  * This shouldn't happen.  If it does, it is probably because
345                  * we have sent two requests to set a variable without getting
346                  * a response between them, and this is the first response.
347                  * So, ignore it, and wait for the next response.
348                  */
349                 return;
350         } else {
351                 change_slc(ncct, func, flag, val);
352         }
353
354 }  /* end of process_slc */
355
356 /*
357  * change_slc
358  *
359  * Process a request to change one of our special characters.
360  * Compare client's request with what we are capable of supporting.
361  */
362 static void
363 change_slc(noit_console_closure_t ncct, char func, char flag, cc_t val)
364 {
365         int hislevel, mylevel;
366
367         hislevel = flag & SLC_LEVELBITS;
368         mylevel = slctab[(int)func].defset.flag & SLC_LEVELBITS;
369         /*
370          * If client is setting a function to NOSUPPORT
371          * or DEFAULT, then we can easily and directly
372          * accomodate the request.
373          */
374         if (hislevel == SLC_NOSUPPORT) {
375                 slctab[(int)func].current.flag = flag;
376                 slctab[(int)func].current.val = (cc_t)_POSIX_VDISABLE;
377                 flag |= SLC_ACK;
378                 add_slc(ncct, func, flag, val);
379                 return;
380         }
381         if (hislevel == SLC_DEFAULT) {
382                 /*
383                  * Special case here.  If client tells us to use
384                  * the default on a function we don't support, then
385                  * return NOSUPPORT instead of what we may have as a
386                  * default level of DEFAULT.
387                  */
388                 if (mylevel == SLC_DEFAULT) {
389                         slctab[(int)func].current.flag = SLC_NOSUPPORT;
390                 } else {
391                         slctab[(int)func].current.flag = slctab[(int)func].defset.flag;
392                 }
393                 slctab[(int)func].current.val = slctab[(int)func].defset.val;
394                 add_slc(ncct, func, slctab[(int)func].current.flag,
395                                                 slctab[(int)func].current.val);
396                 return;
397         }
398
399         /*
400          * Client wants us to change to a new value or he
401          * is telling us that he can't change to our value.
402          * Some of the slc's we support and can change,
403          * some we do support but can't change,
404          * and others we don't support at all.
405          * If we can change it then we have a pointer to
406          * the place to put the new value, so change it,
407          * otherwise, continue the negotiation.
408          */
409         if (slctab[(int)func].sptr) {
410                 /*
411                  * We can change this one.
412                  */
413                 slctab[(int)func].current.val = val;
414                 *(slctab[(int)func].sptr) = val;
415                 slctab[(int)func].current.flag = flag;
416                 flag |= SLC_ACK;
417                 slcchange = 1;
418                 add_slc(ncct, func, flag, val);
419         } else {
420                 /*
421                 * It is not possible for us to support this
422                 * request as he asks.
423                 *
424                 * If our level is DEFAULT, then just ack whatever was
425                 * sent.
426                 *
427                 * If he can't change and we can't change,
428                 * then degenerate to NOSUPPORT.
429                 *
430                 * Otherwise we send our level back to him, (CANTCHANGE
431                 * or NOSUPPORT) and if CANTCHANGE, send
432                 * our value as well.
433                 */
434                 if (mylevel == SLC_DEFAULT) {
435                         slctab[(int)func].current.flag = flag;
436                         slctab[(int)func].current.val = val;
437                         flag |= SLC_ACK;
438                 } else if (hislevel == SLC_CANTCHANGE &&
439                                     mylevel == SLC_CANTCHANGE) {
440                         flag &= ~SLC_LEVELBITS;
441                         flag |= SLC_NOSUPPORT;
442                         slctab[(int)func].current.flag = flag;
443                 } else {
444                         flag &= ~SLC_LEVELBITS;
445                         flag |= mylevel;
446                         slctab[(int)func].current.flag = flag;
447                         if (mylevel == SLC_CANTCHANGE) {
448                                 slctab[(int)func].current.val =
449                                         slctab[(int)func].defset.val;
450                                 val = slctab[(int)func].current.val;
451                         }
452                 }
453                 add_slc(ncct, func, flag, val);
454         }
455
456 }  /* end of change_slc */
457
458 #if     defined(USE_TERMIO) && (VEOF == VMIN)
459 cc_t oldeofc = '\004';
460 #endif
461
462 /*
463  * check_slc
464  *
465  * Check the special characters in use and notify the client if any have
466  * changed.  Only those characters that are capable of being changed are
467  * likely to have changed.  If a local change occurs, kick the support level
468  * and flags up to the defaults.
469  */
470 static void
471 check_slc(noit_console_closure_t ncct)
472 {
473         int i;
474
475         for (i = 1; i <= NSLC; i++) {
476 #if     defined(USE_TERMIO) && (VEOF == VMIN)
477                 /*
478                  * In a perfect world this would be a neat little
479                  * function.  But in this world, we should not notify
480                  * client of changes to the VEOF char when
481                  * ICANON is off, because it is not representing
482                  * a special character.
483                  */
484                 if (i == SLC_EOF) {
485                         if (!noit_console_telnet_tty_isediting(ncct))
486                                 continue;
487                         else if (slctab[i].sptr)
488                                 oldeofc = *(slctab[i].sptr);
489                 }
490 #endif  /* defined(USE_TERMIO) && defined(SYSV_TERMIO) */
491                 if (slctab[i].sptr &&
492                                 (*(slctab[i].sptr) != slctab[i].current.val)) {
493                         slctab[i].current.val = *(slctab[i].sptr);
494                         if (*(slctab[i].sptr) == (cc_t)_POSIX_VDISABLE)
495                                 slctab[i].current.flag = SLC_NOSUPPORT;
496                         else
497                                 slctab[i].current.flag = slctab[i].defset.flag;
498                         add_slc(ncct, (unsigned char)i, slctab[i].current.flag,
499                                                 slctab[i].current.val);
500                 }
501         }
502 }  /* check_slc */
503
504 /*
505  * do_opt_slc
506  *
507  * Process an slc option buffer.  Defer processing of incoming slc's
508  * until after the terminal state has been processed.  Save the first slc
509  * request that comes along, but discard all others.
510  *
511  * ptr points to the beginning of the buffer, len is the length.
512  */
513 static void
514 do_opt_slc(noit_console_closure_t ncct, unsigned char *ptr, int len)
515 {
516         unsigned char func, flag;
517         cc_t val;
518         unsigned char *end = ptr + len;
519
520         if (_terminit) {  /* go ahead */
521                 while (ptr < end) {
522                         func = *ptr++;
523                         if (ptr >= end) break;
524                         flag = *ptr++;
525                         if (ptr >= end) break;
526                         val = (cc_t)*ptr++;
527
528                         process_slc(ncct, func, flag, val);
529
530                 }
531         } else {
532                 /*
533                  * save this slc buffer if it is the first, otherwise dump
534                  * it.
535                  */
536                 if (def_slcbuf == (unsigned char *)0) {
537                         def_slclen = len;
538                         def_slcbuf = (unsigned char *)malloc((unsigned)len);
539                         if (def_slcbuf == (unsigned char *)0)
540                                 return;  /* too bad */
541                         memmove(def_slcbuf, ptr, len);
542                 }
543         }
544
545 }  /* end of do_opt_slc */
546
547 /*
548  * deferslc
549  *
550  * Do slc stuff that was deferred.
551  */
552 static void
553 deferslc(noit_console_closure_t ncct)
554 {
555         if (def_slcbuf) {
556                 start_slc(ncct, 1);
557                 do_opt_slc(ncct, def_slcbuf, def_slclen);
558                 (void) end_slc(ncct, 0);
559                 free(def_slcbuf);
560                 def_slcbuf = (unsigned char *)0;
561                 def_slclen = 0;
562         }
563
564 }  /* end of deferslc */
565 #endif
566
567 #ifdef  LINEMODE
568 /*
569  * tty_flowmode()       Find out if flow control is enabled or disabled.
570  * tty_linemode()       Find out if linemode (external processing) is enabled.
571  * tty_setlinemod(on)   Turn on/off linemode.
572  * tty_isecho()         Find out if echoing is turned on.
573  * tty_setecho(on)      Enable/disable character echoing.
574  * tty_israw()          Find out if terminal is in RAW mode.
575  * tty_binaryin(on)     Turn on/off BINARY on input.
576  * tty_binaryout(on)    Turn on/off BINARY on output.
577  * tty_isediting()      Find out if line editing is enabled.
578  * tty_istrapsig()      Find out if signal trapping is enabled.
579  * tty_setedit(on)      Turn on/off line editing.
580  * tty_setsig(on)       Turn on/off signal trapping.
581  * tty_issofttab()      Find out if tab expansion is enabled.
582  * tty_setsofttab(on)   Turn on/off soft tab expansion.
583  * tty_islitecho()      Find out if typed control chars are echoed literally
584  * tty_setlitecho()     Turn on/off literal echo of control chars
585  * tty_tspeed(val)      Set transmit speed to val.
586  * tty_rspeed(val)      Set receive speed to val.
587  */
588
589
590 int
591 noit_console_telnet_tty_linemode(noit_console_closure_t ncct)
592 {
593 #ifndef    USE_TERMIO
594 #ifdef TS_EXTPROC
595     return(termbuf.state & TS_EXTPROC);
596 #else
597     return 0;
598 #endif
599 #else
600 #ifdef EXTPROC
601     return(termbuf.c_lflag & EXTPROC);
602 #endif
603 #endif
604 }
605
606 void
607 noit_console_telnet_tty_setlinemode(noit_console_closure_t ncct, int on)
608 {
609 #ifdef    TIOCEXT
610     set_termbuf(ncct);
611     (void) ioctl(pty, TIOCEXT, (char *)&on);
612     init_termbuf(ncct);
613 #else    /* !TIOCEXT */
614 # ifdef    EXTPROC
615     if (on)
616         termbuf.c_lflag |= EXTPROC;
617     else
618         termbuf.c_lflag &= ~EXTPROC;
619 # endif
620 #endif    /* TIOCEXT */
621 }
622
623 void
624 noit_console_telnet_tty_setsig(noit_console_closure_t ncct, int on)
625 {
626 #ifndef USE_TERMIO
627         if (on)
628                 ;
629 #else
630         if (on)
631                 termbuf.c_lflag |= ISIG;
632         else
633                 termbuf.c_lflag &= ~ISIG;
634 #endif
635 }
636
637 void
638 noit_console_telnet_tty_setedit(noit_console_closure_t ncct, int on)
639 {
640 #ifndef USE_TERMIO
641     if (on)
642         termbuf.sg.sg_flags &= ~CBREAK;
643     else
644         termbuf.sg.sg_flags |= CBREAK;
645 #else
646     if (on)
647         termbuf.c_lflag |= ICANON;
648     else
649         termbuf.c_lflag &= ~ICANON;
650 #endif
651 }
652 #endif  /* LINEMODE */
653
654 int
655 noit_console_telnet_tty_istrapsig(noit_console_closure_t ncct)
656 {
657 #ifndef USE_TERMIO
658     return(!(termbuf.sg.sg_flags&RAW));
659 #else
660     return(termbuf.c_lflag & ISIG);
661 #endif
662 }
663
664 #ifdef  LINEMODE
665 int
666 noit_console_telnet_tty_isediting(noit_console_closure_t ncct)
667 {
668 #ifndef USE_TERMIO
669     return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
670 #else
671     return(termbuf.c_lflag & ICANON);
672 #endif
673 }
674 #endif
675
676 int
677 noit_console_telnet_tty_issofttab(noit_console_closure_t ncct)
678 {
679 #ifndef    USE_TERMIO
680     return (termbuf.sg.sg_flags & XTABS);
681 #else
682 # ifdef    OXTABS
683     return (termbuf.c_oflag & OXTABS);
684 # endif
685 # ifdef    TABDLY
686     return ((termbuf.c_oflag & TABDLY) == TAB3);
687 # endif
688 #endif
689 }
690
691 void
692 noit_console_telnet_tty_setsofttab(noit_console_closure_t ncct, int on)
693 {
694 #ifndef    USE_TERMIO
695     if (on)
696         termbuf.sg.sg_flags |= XTABS;
697     else
698         termbuf.sg.sg_flags &= ~XTABS;
699 #else
700     if (on) {
701 # ifdef    OXTABS
702         termbuf.c_oflag |= OXTABS;
703 # endif
704 # ifdef    TABDLY
705         termbuf.c_oflag &= ~TABDLY;
706         termbuf.c_oflag |= TAB3;
707 # endif
708     } else {
709 # ifdef    OXTABS
710         termbuf.c_oflag &= ~OXTABS;
711 # endif
712 # ifdef    TABDLY
713         termbuf.c_oflag &= ~TABDLY;
714         termbuf.c_oflag |= TAB0;
715 # endif
716     }
717 #endif
718 }
719
720 int
721 noit_console_telnet_tty_islitecho(noit_console_closure_t ncct)
722 {
723 #ifndef    USE_TERMIO
724     return (!(termbuf.lflags & LCTLECH));
725 #else
726 # ifdef    ECHOCTL
727     return (!(termbuf.c_lflag & ECHOCTL));
728 # endif
729 # ifdef    TCTLECH
730     return (!(termbuf.c_lflag & TCTLECH));
731 # endif
732 # if    !defined(ECHOCTL) && !defined(TCTLECH)
733     return (0);    /* assumes ctl chars are echoed '^x' */
734 # endif
735 #endif
736 }
737
738 void
739 noit_console_telnet_tty_setlitecho(noit_console_closure_t ncct, int on)
740 {
741 #ifndef    USE_TERMIO
742     if (on)
743         termbuf.lflags &= ~LCTLECH;
744     else
745         termbuf.lflags |= LCTLECH;
746 #else
747 # ifdef    ECHOCTL
748     if (on)
749         termbuf.c_lflag &= ~ECHOCTL;
750     else
751         termbuf.c_lflag |= ECHOCTL;
752 # endif
753 # ifdef    TCTLECH
754     if (on)
755         termbuf.c_lflag &= ~TCTLECH;
756     else
757         termbuf.c_lflag |= TCTLECH;
758 # endif
759 #endif
760 }
761
762 int
763 noit_console_telnet_tty_iscrnl(noit_console_closure_t ncct)
764 {
765 #ifndef USE_TERMIO
766     return (termbuf.sg.sg_flags & CRMOD);
767 #else
768     return (termbuf.c_iflag & ICRNL);
769 #endif
770 }
771
772 void
773 noit_console_telnet_tty_tspeed(noit_console_closure_t ncct, int val)
774 {
775 #ifdef  DECODE_BAUD
776     struct termspeeds *tp;
777
778     for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
779         ;
780     if (tp->speed == -1)        /* back up to last valid value */
781         --tp;
782     cfsetospeed(&termbuf, tp->value);
783 #else   /* DECODE_BUAD */
784     cfsetospeed(&termbuf, val);
785 #endif  /* DECODE_BUAD */
786 }
787
788 void
789 noit_console_telnet_tty_rspeed(noit_console_closure_t ncct, int val)
790 {
791 #ifdef  DECODE_BAUD
792     struct termspeeds *tp;
793
794     for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
795         ;
796     if (tp->speed == -1)        /* back up to last valid value */
797         --tp;
798     cfsetispeed(&termbuf, tp->value);
799 #else   /* DECODE_BAUD */
800     cfsetispeed(&termbuf, val);
801 #endif  /* DECODE_BAUD */
802 }
803
804
805 int
806 noit_console_telnet_tty_flowmode(noit_console_closure_t ncct)
807 {
808 #ifndef USE_TERMIO
809     return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
810 #else
811     return((termbuf.c_iflag & IXON) ? 1 : 0);
812 #endif
813 }
814
815 void
816 noit_console_telnet_tty_binaryin(noit_console_closure_t ncct, int on)
817 {
818 #ifndef USE_TERMIO
819     if (on)
820         termbuf.lflags |= LPASS8;
821     else
822         termbuf.lflags &= ~LPASS8;
823 #else
824     if (on)
825         termbuf.c_iflag &= ~ISTRIP;
826     else
827         termbuf.c_iflag |= ISTRIP;
828 #endif
829 }
830
831 int
832 noit_console_telnet_tty_isbinaryin(noit_console_closure_t ncct)
833 {
834 #ifndef USE_TERMIO
835         return(termbuf.lflags & LPASS8);
836 #else
837         return(!(termbuf.c_iflag & ISTRIP));
838 #endif
839 }
840
841 void
842 noit_console_telnet_tty_binaryout(noit_console_closure_t ncct, int on)
843 {
844 #ifndef USE_TERMIO
845     if (on)
846         termbuf.lflags |= LLITOUT;
847     else
848         termbuf.lflags &= ~LLITOUT;
849 #else
850     if (on) {
851         termbuf.c_cflag &= ~(CSIZE|PARENB);
852         termbuf.c_cflag |= CS8;
853         termbuf.c_oflag &= ~OPOST;
854     } else {
855         termbuf.c_cflag &= ~CSIZE;
856         termbuf.c_cflag |= CS7|PARENB;
857         termbuf.c_oflag |= OPOST;
858     }
859 #endif
860 }
861
862 int
863 noit_console_telnet_tty_isbinaryout(noit_console_closure_t ncct)
864 {
865 #ifndef USE_TERMIO
866         return(termbuf.lflags & LLITOUT);
867 #else
868         return(!(termbuf.c_oflag&OPOST));
869 #endif
870 }
871
872 int
873 noit_console_telnet_tty_restartany(noit_console_closure_t ncct)
874 {
875 #ifndef USE_TERMIO
876 # ifdef DECCTQ
877     return((termbuf.lflags & DECCTQ) ? 0 : 1);
878 # else
879     return(-1);
880 # endif
881 #else
882     return((termbuf.c_iflag & IXANY) ? 1 : 0);
883 #endif
884 }
885
886 int
887 noit_console_telnet_tty_isecho(noit_console_closure_t ncct)
888 {
889 #ifndef USE_TERMIO
890         return (termbuf.sg.sg_flags & ECHO);
891 #else
892         return (termbuf.c_lflag & ECHO);
893 #endif
894 }
895
896 void
897 noit_console_telnet_tty_setecho(noit_console_closure_t ncct, int on)
898 {
899     int i;
900     i = ECHO;
901 #ifdef CRMOD
902     i |= CRMOD;
903 #endif
904 #ifndef USE_TERMIO
905     if (on)
906         termbuf.sg.sg_flags |= i;
907     else
908         termbuf.sg.sg_flags &= ~(i);
909 #else
910     if (on)
911         termbuf.c_lflag |= i;
912     else
913         termbuf.c_lflag &= ~i;
914 #endif
915 }
916
917 static void
918 init_termbuf(noit_console_closure_t ncct)
919 {
920 #ifndef USE_TERMIO
921         (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
922         (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
923         (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
924 # ifdef TIOCGSTATE
925         (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
926 # endif
927 #else
928         (void) tcgetattr(pty, &termbuf);
929 #endif
930         termbuf2 = termbuf;
931 }
932
933 #if     defined(LINEMODE) && defined(TIOCPKT_IOCTL)
934 void
935 copy_termbuf(noit_console_closure_t ncct, char *cp, size_t len)
936 {
937         if (len > sizeof(termbuf))
938                 len = sizeof(termbuf);
939         memmove((char *)&termbuf, cp, len);
940         termbuf2 = termbuf;
941 }
942 #endif  /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
943
944 static void
945 set_termbuf(noit_console_closure_t ncct)
946 {
947         /*
948          * Only make the necessary changes.
949          */
950 #ifndef USE_TERMIO
951         if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
952                                                         sizeof(termbuf.sg)))
953                 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
954         if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
955                                                         sizeof(termbuf.tc)))
956                 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
957         if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
958                                                         sizeof(termbuf.ltc)))
959                 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
960         if (termbuf.lflags != termbuf2.lflags)
961                 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
962 #else   /* USE_TERMIO */
963         if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
964                 (void) tcsetattr(pty, TCSANOW, &termbuf);
965 #endif  /* USE_TERMIO */
966 }
967
968 #ifdef  LINEMODE
969 /*
970  * localstat
971  *
972  * This function handles all management of linemode.
973  *
974  * Linemode allows the client to do the local editing of data
975  * and send only complete lines to the server.  Linemode state is
976  * based on the state of the pty driver.  If the pty is set for
977  * external processing, then we can use linemode.  Further, if we
978  * can use real linemode, then we can look at the edit control bits
979  * in the pty to determine what editing the client should do.
980  *
981  * Linemode support uses the following state flags to keep track of
982  * current and desired linemode state.
983  *      alwayslinemode : true if -l was specified on the telnetd
984  *      command line.  It means to have linemode on as much as
985  *      possible.
986  *
987  *      lmodetype: signifies whether the client can
988  *      handle real linemode, or if use of kludgeomatic linemode
989  *      is preferred.  It will be set to one of the following:
990  *              REAL_LINEMODE : use linemode option
991  *              NO_KLUDGE : don't initiate kludge linemode.
992  *              KLUDGE_LINEMODE : use kludge linemode
993  *              NO_LINEMODE : client is ignorant of linemode
994  *
995  *      linemode, uselinemode : linemode is true if linemode
996  *      is currently on, uselinemode is the state that we wish
997  *      to be in.  If another function wishes to turn linemode
998  *      on or off, it sets or clears uselinemode.
999  *
1000  *      editmode, useeditmode : like linemode/uselinemode, but
1001  *      these contain the edit mode states (edit and trapsig).
1002  *
1003  * The state variables correspond to some of the state information
1004  * in the pty.
1005  *      linemode:
1006  *              In real linemode, this corresponds to whether the pty
1007  *              expects external processing of incoming data.
1008  *              In kludge linemode, this more closely corresponds to the
1009  *              whether normal processing is on or not.  (ICANON in
1010  *              system V, or COOKED mode in BSD.)
1011  *              If the -l option was specified (alwayslinemode), then
1012  *              an attempt is made to force external processing on at
1013  *              all times.
1014  *
1015  * The following heuristics are applied to determine linemode
1016  * handling within the server.
1017  *      1) Early on in starting up the server, an attempt is made
1018  *         to negotiate the linemode option.  If this succeeds
1019  *         then lmodetype is set to REAL_LINEMODE and all linemode
1020  *         processing occurs in the context of the linemode option.
1021  *      2) If the attempt to negotiate the linemode option failed,
1022  *         and the "-k" (don't initiate kludge linemode) isn't set,
1023  *         then we try to use kludge linemode.  We test for this
1024  *         capability by sending "do Timing Mark".  If a positive
1025  *         response comes back, then we assume that the client
1026  *         understands kludge linemode (ech!) and the
1027  *         lmodetype flag is set to KLUDGE_LINEMODE.
1028  *      3) Otherwise, linemode is not supported at all and
1029  *         lmodetype remains set to NO_LINEMODE (which happens
1030  *         to be 0 for convenience).
1031  *      4) At any time a command arrives that implies a higher
1032  *         state of linemode support in the client, we move to that
1033  *         linemode support.
1034  *
1035  * A short explanation of kludge linemode is in order here.
1036  *      1) The heuristic to determine support for kludge linemode
1037  *         is to send a do timing mark.  We assume that a client
1038  *         that supports timing marks also supports kludge linemode.
1039  *         A risky proposition at best.
1040  *      2) Further negotiation of linemode is done by changing the
1041  *         the server's state regarding SGA.  If server will SGA,
1042  *         then linemode is off, if server won't SGA, then linemode
1043  *         is on.
1044  */
1045 static void
1046 localstat(noit_console_closure_t ncct)
1047 {
1048         int need_will_echo = 0;
1049
1050         /*
1051          * Check for changes to flow control if client supports it.
1052          */
1053         flowstat(ncct);
1054
1055         /*
1056          * Check linemode on/off state
1057          */
1058         uselinemode = noit_console_telnet_tty_linemode(ncct);
1059
1060         /*
1061          * If alwayslinemode is on, and pty is changing to turn it off, then
1062          * force linemode back on.
1063          */
1064         if (alwayslinemode && linemode && !uselinemode) {
1065                 uselinemode = 1;
1066                 noit_console_telnet_tty_setlinemode(ncct, uselinemode);
1067         }
1068
1069         if (uselinemode) {
1070                 /*
1071                  * Check for state of BINARY options.
1072                  *
1073                  * We only need to do the binary dance if we are actually going
1074                  * to use linemode.  As this confuses some telnet clients
1075                  * that don't support linemode, and doesn't gain us
1076                  * anything, we don't do it unless we're doing linemode.
1077                  * -Crh (henrich@msu.edu)
1078                  */
1079
1080                 if (noit_console_telnet_tty_isbinaryin(ncct)) {
1081                         if (his_want_state_is_wont(TELOPT_BINARY))
1082                                 send_do(TELOPT_BINARY, 1);
1083                 } else {
1084                         if (his_want_state_is_will(TELOPT_BINARY))
1085                                 send_dont(TELOPT_BINARY, 1);
1086                 }
1087
1088                 if (noit_console_telnet_tty_isbinaryout(ncct)) {
1089                         if (my_want_state_is_wont(TELOPT_BINARY))
1090                                 send_will(TELOPT_BINARY, 1);
1091                 } else {
1092                         if (my_want_state_is_will(TELOPT_BINARY))
1093                                 send_wont(TELOPT_BINARY, 1);
1094                 }
1095         }
1096
1097
1098         /*
1099          * Do echo mode handling as soon as we know what the
1100          * linemode is going to be.
1101          * If the pty has echo turned off, then tell the client that
1102          * the server will echo.  If echo is on, then the server
1103          * will echo if in character mode, but in linemode the
1104          * client should do local echoing.  The state machine will
1105          * not send anything if it is unnecessary, so don't worry
1106          * about that here.
1107          *
1108          * If we need to send the WILL ECHO (because echo is off),
1109          * then delay that until after we have changed the MODE.
1110          * This way, when the user is turning off both editing
1111          * and echo, the client will get editing turned off first.
1112          * This keeps the client from going into encryption mode
1113          * and then right back out if it is doing auto-encryption
1114          * when passwords are being typed.
1115          */
1116         if (uselinemode) {
1117                 if (noit_console_telnet_tty_isecho(ncct))
1118                         send_wont(TELOPT_ECHO, 1);
1119                 else
1120                         need_will_echo = 1;
1121 #ifdef  KLUDGELINEMODE
1122                 if (lmodetype == KLUDGE_OK)
1123                         lmodetype = KLUDGE_LINEMODE;
1124 #endif
1125         }
1126
1127         /*
1128          * If linemode is being turned off, send appropriate
1129          * command and then we're all done.
1130          */
1131          if (!uselinemode && linemode) {
1132 # ifdef KLUDGELINEMODE
1133                 if (lmodetype == REAL_LINEMODE) {
1134 # endif /* KLUDGELINEMODE */
1135                         send_dont(TELOPT_LINEMODE, 1);
1136 # ifdef KLUDGELINEMODE
1137                 } else if (lmodetype == KLUDGE_LINEMODE)
1138                         send_will(TELOPT_SGA, 1);
1139 # endif /* KLUDGELINEMODE */
1140                 send_will(TELOPT_ECHO, 1);
1141                 linemode = uselinemode;
1142                 goto done;
1143         }
1144
1145 # ifdef KLUDGELINEMODE
1146         /*
1147          * If using real linemode check edit modes for possible later use.
1148          * If we are in kludge linemode, do the SGA negotiation.
1149          */
1150         if (lmodetype == REAL_LINEMODE) {
1151 # endif /* KLUDGELINEMODE */
1152                 useeditmode = 0;
1153                 if (noit_console_telnet_tty_isediting(ncct))
1154                         useeditmode |= MODE_EDIT;
1155                 if (noit_console_telnet_tty_istrapsig(ncct))
1156                         useeditmode |= MODE_TRAPSIG;
1157                 if (noit_console_telnet_tty_issofttab(ncct))
1158                         useeditmode |= MODE_SOFT_TAB;
1159                 if (noit_console_telnet_tty_islitecho(ncct))
1160                         useeditmode |= MODE_LIT_ECHO;
1161 # ifdef KLUDGELINEMODE
1162         } else if (lmodetype == KLUDGE_LINEMODE) {
1163                 if (noit_console_telnet_tty_isediting(ncct) && uselinemode)
1164                         send_wont(TELOPT_SGA, 1);
1165                 else
1166                         send_will(TELOPT_SGA, 1);
1167         }
1168 # endif /* KLUDGELINEMODE */
1169
1170         /*
1171          * Negotiate linemode on if pty state has changed to turn it on.
1172          * Send appropriate command and send along edit mode, then all done.
1173          */
1174         if (uselinemode && !linemode) {
1175 # ifdef KLUDGELINEMODE
1176                 if (lmodetype == KLUDGE_LINEMODE) {
1177                         send_wont(TELOPT_SGA, 1);
1178                 } else if (lmodetype == REAL_LINEMODE) {
1179 # endif /* KLUDGELINEMODE */
1180                         send_do(TELOPT_LINEMODE, 1);
1181                         /* send along edit modes */
1182                         nc_printf(ncct, "%c%c%c%c%c%c%c", IAC, SB,
1183                                 TELOPT_LINEMODE, LM_MODE, useeditmode,
1184                                 IAC, SE);
1185                         editmode = useeditmode;
1186 # ifdef KLUDGELINEMODE
1187                 }
1188 # endif /* KLUDGELINEMODE */
1189                 linemode = uselinemode;
1190                 goto done;
1191         }
1192
1193 # ifdef KLUDGELINEMODE
1194         /*
1195          * None of what follows is of any value if not using
1196          * real linemode.
1197          */
1198         if (lmodetype < REAL_LINEMODE)
1199                 goto done;
1200 # endif /* KLUDGELINEMODE */
1201
1202         if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
1203                 /*
1204                  * If edit mode changed, send edit mode.
1205                  */
1206                  if (useeditmode != editmode) {
1207                         /*
1208                          * Send along appropriate edit mode mask.
1209                          */
1210                         nc_printf(ncct, "%c%c%c%c%c%c%c", IAC, SB,
1211                                 TELOPT_LINEMODE, LM_MODE, useeditmode,
1212                                 IAC, SE);
1213                         editmode = useeditmode;
1214                 }
1215
1216
1217                 /*
1218                  * Check for changes to special characters in use.
1219                  */
1220                 start_slc(ncct, 0);
1221                 check_slc(ncct);
1222                 (void) end_slc(ncct, 0);
1223         }
1224
1225 done:
1226         if (need_will_echo)
1227                 send_will(TELOPT_ECHO, 1);
1228         /*
1229          * Some things should be deferred until after the pty state has
1230          * been set by the local process.  Do those things that have been
1231          * deferred now.  This only happens once.
1232          */
1233         if (_terminit == 0) {
1234                 _terminit = 1;
1235                 defer_terminit(ncct);
1236         }
1237
1238         netflush(ncct);
1239         set_termbuf(ncct);
1240         return;
1241
1242 }  /* end of localstat */
1243 #endif  /* LINEMODE */
1244
1245 /*
1246  * flowstat
1247  *
1248  * Check for changes to flow control
1249  */
1250 static void
1251 flowstat(noit_console_closure_t ncct)
1252 {
1253     if (his_state_is_will(TELOPT_LFLOW)) {
1254         if (noit_console_telnet_tty_flowmode(ncct) != flowmode) {
1255             flowmode = noit_console_telnet_tty_flowmode(ncct);
1256             nc_printf(ncct, "%c%c%c%c%c%c",
1257                         IAC, SB, TELOPT_LFLOW,
1258                         flowmode ? LFLOW_ON : LFLOW_OFF,
1259                         IAC, SE);
1260         }
1261         if (noit_console_telnet_tty_restartany(ncct) != restartany) {
1262             restartany = noit_console_telnet_tty_restartany(ncct);
1263             nc_printf(ncct, "%c%c%c%c%c%c",
1264                         IAC, SB, TELOPT_LFLOW,
1265                         restartany ? LFLOW_RESTART_ANY
1266                         : LFLOW_RESTART_XON,
1267                         IAC, SE);
1268         }
1269     }
1270 }
1271
1272 /*
1273  * clientstat
1274  *
1275  * Process linemode related requests from the client.
1276  * Client can request a change to only one of linemode, editmode or slc's
1277  * at a time, and if using kludge linemode, then only linemode may be
1278  * affected.
1279  */
1280 void
1281 clientstat(noit_console_closure_t ncct, int code, int parm1, int parm2)
1282 {
1283     /*
1284      * Get a copy of terminal characteristics.
1285      */
1286     init_termbuf(ncct);
1287
1288     /*
1289      * Process request from client. code tells what it is.
1290      */
1291     switch (code) {
1292 #ifdef  LINEMODE
1293         case TELOPT_LINEMODE:
1294                 /*
1295                  * Don't do anything unless client is asking us to change
1296                  * modes.
1297                  */
1298                 uselinemode = (parm1 == WILL);
1299                 if (uselinemode != linemode) {
1300 # ifdef KLUDGELINEMODE
1301                         /*
1302                          * If using kludge linemode, make sure that
1303                          * we can do what the client asks.
1304                          * We can not turn off linemode if alwayslinemode
1305                          * and the ICANON bit is set.
1306                          */
1307                         if (lmodetype == KLUDGE_LINEMODE) {
1308                                 if (alwayslinemode && noit_console_telnet_tty_isediting(ncct)) {
1309                                         uselinemode = 1;
1310                                 }
1311                         }
1312
1313                         /*
1314                          * Quit now if we can't do it.
1315                          */
1316                         if (uselinemode == linemode)
1317                                 return;
1318
1319                         /*
1320                          * If using real linemode and linemode is being
1321                          * turned on, send along the edit mode mask.
1322                          */
1323                         if (lmodetype == REAL_LINEMODE && uselinemode)
1324 # else  /* KLUDGELINEMODE */
1325                         if (uselinemode)
1326 # endif /* KLUDGELINEMODE */
1327                         {
1328                                 useeditmode = 0;
1329                                 if (noit_console_telnet_tty_isediting(ncct))
1330                                         useeditmode |= MODE_EDIT;
1331                                 if (noit_console_telnet_tty_istrapsig(ncct))
1332                                         useeditmode |= MODE_TRAPSIG;
1333                                 if (noit_console_telnet_tty_issofttab(ncct))
1334                                         useeditmode |= MODE_SOFT_TAB;
1335                                 if (noit_console_telnet_tty_islitecho(ncct))
1336                                         useeditmode |= MODE_LIT_ECHO;
1337                                 nc_printf(ncct, "%c%c%c%c%c%c%c", IAC,
1338                                         SB, TELOPT_LINEMODE, LM_MODE,
1339                                                         useeditmode, IAC, SE);
1340                                 editmode = useeditmode;
1341                         }
1342
1343
1344                         noit_console_telnet_tty_setlinemode(ncct, uselinemode);
1345
1346                         linemode = uselinemode;
1347
1348                         if (!linemode)
1349                                 send_will(TELOPT_ECHO, 1);
1350                 }
1351                 break;
1352
1353         case LM_MODE:
1354             {
1355                 int ack, changed;
1356
1357                 /*
1358                  * Client has sent along a mode mask.  If it agrees with
1359                  * what we are currently doing, ignore it; if not, it could
1360                  * be viewed as a request to change.  Note that the server
1361                  * will change to the modes in an ack if it is different from
1362                  * what we currently have, but we will not ack the ack.
1363                  */
1364                  useeditmode &= MODE_MASK;
1365                  ack = (useeditmode & MODE_ACK);
1366                  useeditmode &= ~MODE_ACK;
1367
1368                  if ((changed = (useeditmode ^ editmode))) {
1369                         /*
1370                          * This check is for a timing problem.  If the
1371                          * state of the tty has changed (due to the user
1372                          * application) we need to process that info
1373                          * before we write in the state contained in the
1374                          * ack!!!  This gets out the new MODE request,
1375                          * and when the ack to that command comes back
1376                          * we'll set it and be in the right mode.
1377                          */
1378 #ifdef LINEMODE
1379                         if (ack)
1380                                 localstat(ncct);
1381 #endif
1382                         if (changed & MODE_EDIT)
1383                                 noit_console_telnet_tty_setedit(ncct, useeditmode & MODE_EDIT);
1384
1385                         if (changed & MODE_TRAPSIG)
1386                                 noit_console_telnet_tty_setsig(ncct, useeditmode & MODE_TRAPSIG);
1387
1388                         if (changed & MODE_SOFT_TAB)
1389                                 noit_console_telnet_tty_setsofttab(ncct, useeditmode & MODE_SOFT_TAB);
1390
1391                         if (changed & MODE_LIT_ECHO)
1392                                 noit_console_telnet_tty_setlitecho(ncct, useeditmode & MODE_LIT_ECHO);
1393
1394                         set_termbuf(ncct);
1395
1396                         if (!ack) {
1397                                 nc_printf(ncct, "%c%c%c%c%c%c%c", IAC,
1398                                         SB, TELOPT_LINEMODE, LM_MODE,
1399                                         useeditmode|MODE_ACK,
1400                                         IAC, SE);
1401                         }
1402
1403                         editmode = useeditmode;
1404                 }
1405
1406                 break;
1407
1408             }  /* end of case LM_MODE */
1409 #endif  /* LINEMODE */
1410     case TELOPT_NAWS:
1411 #ifdef  TIOCSWINSZ
1412         {
1413             struct winsize ws;
1414
1415             def_col = parm1;
1416             def_row = parm2;
1417
1418             /*
1419              * Change window size as requested by client.
1420              */
1421
1422             memset(&ws, 0, sizeof(ws));
1423             ws.ws_col = parm1;
1424             ws.ws_row = parm2;
1425             ioctl(pty, TIOCSWINSZ, (char *)&ws);
1426         }
1427 #endif  /* TIOCSWINSZ */
1428
1429     break;
1430
1431     case TELOPT_TSPEED:
1432         {
1433             def_tspeed = parm1;
1434             def_rspeed = parm2;
1435             /*
1436              * Change terminal speed as requested by client.
1437              * We set the receive speed first, so that if we can't
1438              * store seperate receive and transmit speeds, the transmit
1439              * speed will take precedence.
1440              */
1441             noit_console_telnet_tty_rspeed(ncct, parm2);
1442             noit_console_telnet_tty_tspeed(ncct, parm1);
1443             set_termbuf(ncct);
1444
1445             break;
1446
1447         }  /* end of case TELOPT_TSPEED */
1448
1449     default:
1450         /* What? */
1451         break;
1452     }  /* end of switch */
1453
1454     netflush(ncct);
1455 }
1456
1457 #ifdef  LINEMODE
1458 /*
1459  * defer_terminit
1460  *
1461  * Some things should not be done until after the login process has started
1462  * and all the pty modes are set to what they are supposed to be.  This
1463  * function is called when the pty state has been processed for the first time.
1464  * It calls other functions that do things that were deferred in each module.
1465  */
1466 static void
1467 defer_terminit(noit_console_closure_t ncct)
1468 {
1469
1470         /*
1471          * local stuff that got deferred.
1472          */
1473         if (def_tspeed != -1) {
1474                 clientstat(ncct, TELOPT_TSPEED, def_tspeed, def_rspeed);
1475                 def_tspeed = def_rspeed = 0;
1476         }
1477
1478 #ifdef  TIOCSWINSZ
1479         if (def_col || def_row) {
1480                 struct winsize ws;
1481
1482                 memset((char *)&ws, 0, sizeof(ws));
1483                 ws.ws_col = def_col;
1484                 ws.ws_row = def_row;
1485                 (void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
1486         }
1487 #endif
1488
1489         /*
1490          * The only other module that currently defers anything.
1491          */
1492         deferslc(ncct);
1493
1494 }  /* end of defer_terminit */
1495 #endif
1496
1497 /*
1498  * spcset(func, valp, valpp)
1499  *
1500  * This function takes various special characters (func), and
1501  * sets *valp to the current value of that character, and
1502  * *valpp to point to where in the "termbuf" structure that
1503  * value is kept.
1504  *
1505  * It returns the SLC_ level of support for this function.
1506  */
1507
1508 #ifndef USE_TERMIO
1509 static int
1510 spcset(noit_console_closure_t ncct, int func, cc_t *valp, cc_t **valpp)
1511 {
1512         switch(func) {
1513         case SLC_EOF:
1514                 *valp = termbuf.tc.t_eofc;
1515                 *valpp = (cc_t *)&termbuf.tc.t_eofc;
1516                 return(SLC_VARIABLE);
1517         case SLC_EC:
1518                 *valp = termbuf.sg.sg_erase;
1519                 *valpp = (cc_t *)&termbuf.sg.sg_erase;
1520                 return(SLC_VARIABLE);
1521         case SLC_EL:
1522                 *valp = termbuf.sg.sg_kill;
1523                 *valpp = (cc_t *)&termbuf.sg.sg_kill;
1524                 return(SLC_VARIABLE);
1525         case SLC_IP:
1526                 *valp = termbuf.tc.t_intrc;
1527                 *valpp = (cc_t *)&termbuf.tc.t_intrc;
1528                 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
1529         case SLC_ABORT:
1530                 *valp = termbuf.tc.t_quitc;
1531                 *valpp = (cc_t *)&termbuf.tc.t_quitc;
1532                 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
1533         case SLC_XON:
1534                 *valp = termbuf.tc.t_startc;
1535                 *valpp = (cc_t *)&termbuf.tc.t_startc;
1536                 return(SLC_VARIABLE);
1537         case SLC_XOFF:
1538                 *valp = termbuf.tc.t_stopc;
1539                 *valpp = (cc_t *)&termbuf.tc.t_stopc;
1540                 return(SLC_VARIABLE);
1541         case SLC_AO:
1542                 *valp = termbuf.ltc.t_flushc;
1543                 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
1544                 return(SLC_VARIABLE);
1545         case SLC_SUSP:
1546                 *valp = termbuf.ltc.t_suspc;
1547                 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
1548                 return(SLC_VARIABLE);
1549         case SLC_EW:
1550                 *valp = termbuf.ltc.t_werasc;
1551                 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
1552                 return(SLC_VARIABLE);
1553         case SLC_RP:
1554                 *valp = termbuf.ltc.t_rprntc;
1555                 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
1556                 return(SLC_VARIABLE);
1557         case SLC_LNEXT:
1558                 *valp = termbuf.ltc.t_lnextc;
1559                 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
1560                 return(SLC_VARIABLE);
1561         case SLC_FORW1:
1562                 *valp = termbuf.tc.t_brkc;
1563                 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
1564                 return(SLC_VARIABLE);
1565         case SLC_BRK:
1566         case SLC_SYNCH:
1567         case SLC_AYT:
1568         case SLC_EOR:
1569                 *valp = (cc_t)0;
1570                 *valpp = (cc_t *)0;
1571                 return(SLC_DEFAULT);
1572         default:
1573                 *valp = (cc_t)0;
1574                 *valpp = (cc_t *)0;
1575                 return(SLC_NOSUPPORT);
1576         }
1577 }
1578
1579 #else   /* USE_TERMIO */
1580
1581
1582 #define setval(a, b)    *valp = termbuf.c_cc[a]; \
1583                         *valpp = &termbuf.c_cc[a]; \
1584                         return(b);
1585 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
1586
1587 int
1588 spcset(noit_console_closure_t ncct, int func, cc_t *valp, cc_t **valpp)
1589 {
1590         switch(func) {
1591         case SLC_EOF:
1592                 setval(VEOF, SLC_VARIABLE);
1593         case SLC_EC:
1594                 setval(VERASE, SLC_VARIABLE);
1595         case SLC_EL:
1596                 setval(VKILL, SLC_VARIABLE);
1597         case SLC_IP:
1598                 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
1599         case SLC_ABORT:
1600                 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
1601         case SLC_XON:
1602 #ifdef  VSTART
1603                 setval(VSTART, SLC_VARIABLE);
1604 #else
1605                 defval(0x13);
1606 #endif
1607         case SLC_XOFF:
1608 #ifdef  VSTOP
1609                 setval(VSTOP, SLC_VARIABLE);
1610 #else
1611                 defval(0x11);
1612 #endif
1613         case SLC_EW:
1614 #ifdef  VWERASE
1615                 setval(VWERASE, SLC_VARIABLE);
1616 #else
1617                 defval(0);
1618 #endif
1619         case SLC_RP:
1620 #ifdef  VREPRINT
1621                 setval(VREPRINT, SLC_VARIABLE);
1622 #else
1623                 defval(0);
1624 #endif
1625         case SLC_LNEXT:
1626 #ifdef  VLNEXT
1627                 setval(VLNEXT, SLC_VARIABLE);
1628 #else
1629                 defval(0);
1630 #endif
1631         case SLC_AO:
1632 #if     !defined(VDISCARD) && defined(VFLUSHO)
1633 # define VDISCARD VFLUSHO
1634 #endif
1635 #ifdef  VDISCARD
1636                 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
1637 #else
1638                 defval(0);
1639 #endif
1640         case SLC_SUSP:
1641 #ifdef  VSUSP
1642                 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
1643 #else
1644                 defval(0);
1645 #endif
1646 #ifdef  VEOL
1647         case SLC_FORW1:
1648                 setval(VEOL, SLC_VARIABLE);
1649 #endif
1650 #ifdef  VEOL2
1651         case SLC_FORW2:
1652                 setval(VEOL2, SLC_VARIABLE);
1653 #endif
1654         case SLC_AYT:
1655 #ifdef  VSTATUS
1656                 setval(VSTATUS, SLC_VARIABLE);
1657 #else
1658                 defval(0);
1659 #endif
1660
1661         case SLC_BRK:
1662         case SLC_SYNCH:
1663         case SLC_EOR:
1664                 defval(0);
1665
1666         default:
1667                 *valp = 0;
1668                 *valpp = 0;
1669                 return(SLC_NOSUPPORT);
1670         }
1671 }
1672 #endif  /* USE_TERMIO */
1673
1674 void
1675 ptyflush(noit_console_closure_t ncct) {
1676   int written;
1677   if(ncct->telnet->_pty_fill == 0) return;
1678   written = write(ncct->pty_slave, ncct->telnet->_pty_buf,
1679                   ncct->telnet->_pty_fill);
1680   if(written != ncct->telnet->_pty_fill) {
1681     /* We can't do anything useful here... just cope. */
1682   }
1683   ncct->telnet->_pty_fill = 0;
1684 }
1685
1686 static void
1687 netclear(noit_console_closure_t ncct) {
1688   ncct->outbuf_len = 0;
1689 }
1690
1691 static void
1692 pty_write(noit_console_closure_t ncct, void *buf, int len) {
1693   if(len > sizeof(ncct->telnet->_pty_buf)) {
1694     int i;
1695     /* split it up */
1696     for(i=0; i<len; i+=sizeof(ncct->telnet->_pty_buf)) {
1697       pty_write(ncct, (unsigned char *)buf + i,
1698                 MIN(sizeof(ncct->telnet->_pty_buf),len-i));
1699     }
1700   }
1701   while(ncct->telnet->_pty_fill + len > sizeof(ncct->telnet->_pty_buf)) {
1702     ptyflush(ncct);
1703   }
1704   memcpy(ncct->telnet->_pty_buf + ncct->telnet->_pty_fill, buf, len);
1705   ncct->telnet->_pty_fill += len;
1706 }
1707
1708 /*
1709  * Send interrupt to process on other side of pty.
1710  * If it is in raw mode, just write NULL;
1711  * otherwise, write intr char.
1712  */
1713 void
1714 interrupt(noit_console_closure_t ncct)
1715 {
1716 #ifdef  TCSIG
1717         ptyflush(ncct); /* half-hearted */
1718         (void) ioctl(pty, TCSIG, (char *)SIGINT);
1719 #else   /* TCSIG */
1720         unsigned char ch;
1721         ptyflush(ncct); /* half-hearted */
1722         init_termbuf(ncct);
1723         ch = slctab[SLC_IP].sptr ?
1724                         (unsigned char)*slctab[SLC_IP].sptr : '\177';
1725         pty_write(ncct, &ch, 1);
1726 #endif  /* TCSIG */
1727 }
1728
1729 /*
1730  * Send quit to process on other side of pty.
1731  * If it is in raw mode, just write NULL;
1732  * otherwise, write quit char.
1733  */
1734 void
1735 sendbrk(noit_console_closure_t ncct)
1736 {
1737 #ifdef  TCSIG
1738         ptyflush(ncct); /* half-hearted */
1739         (void) ioctl(pty, TCSIG, (char *)SIGQUIT);
1740 #else   /* TCSIG */
1741         unsigned char ch;
1742         ptyflush(ncct); /* half-hearted */
1743         init_termbuf(ncct);
1744         ch = slctab[SLC_ABORT].sptr ?
1745                         (unsigned char)*slctab[SLC_ABORT].sptr : '\034';
1746         pty_write(ncct, &ch, 1);
1747 #endif  /* TCSIG */
1748 }
1749
1750 void
1751 sendsusp(noit_console_closure_t ncct)
1752 {
1753 #ifdef  SIGTSTP
1754 # ifdef TCSIG
1755         ptyflush(ncct); /* half-hearted */
1756         (void) ioctl(pty, TCSIG, (char *)SIGTSTP);
1757 # else  /* TCSIG */
1758         unsigned char ch;
1759         ptyflush(ncct); /* half-hearted */
1760         ch = slctab[SLC_SUSP].sptr ?
1761                         (unsigned char)*slctab[SLC_SUSP].sptr : '\032';
1762         pty_write(ncct, &ch, 1);
1763 # endif /* TCSIG */
1764 #endif  /* SIGTSTP */
1765 }
1766
1767 /*
1768  * When we get an AYT, if ^T is enabled, use that.  Otherwise,
1769  * just send back "[Yes]".
1770  */
1771 void
1772 recv_ayt(noit_console_closure_t ncct)
1773 {
1774 #if     defined(SIGINFO) && defined(TCSIG)
1775         if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
1776                 (void) ioctl(pty, TCSIG, (char *)SIGINFO);
1777                 return;
1778         }
1779 #endif
1780         nc_printf(ncct, "\r\n[Yes]\r\n");
1781 }
1782
1783 void
1784 doeof(noit_console_closure_t ncct)
1785 {
1786         unsigned char ch;
1787         init_termbuf(ncct);
1788
1789 #if     defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
1790         if (!noit_console_telnet_tty_isediting(ncct)) {
1791                 extern char oldeofc;
1792                 pty_write(ncct, &oldeofc, 1);
1793                 return;
1794         }
1795 #endif
1796         ch = slctab[SLC_EOF].sptr ?
1797                         (unsigned char)*slctab[SLC_EOF].sptr : '\004';
1798         pty_write(ncct, &ch, 1);
1799 }
1800
1801 noit_console_telnet_closure_t
1802 noit_console_telnet_alloc(noit_console_closure_t ncct) {
1803   noit_console_telnet_closure_t telnet, tmp;
1804   static unsigned char ttytype_sbbuf[] = {
1805     IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
1806   };
1807   tmp = ncct->telnet;
1808
1809   ncct->telnet = calloc(1, sizeof(*telnet));
1810   subbuffer = malloc(1024*64);
1811   subpointer = subbuffer;
1812   subend= subbuffer;
1813   def_tspeed = -1;
1814   def_rspeed = -1;
1815   get_slc_defaults(ncct);
1816   if (my_state_is_wont(TELOPT_SGA))
1817     send_will(TELOPT_SGA, 1);
1818   send_do(TELOPT_ECHO, 1);
1819   send_do(TELOPT_NAWS, 1);
1820   send_will(TELOPT_STATUS, 1);
1821   flowmode = 1;         /* default flow control state */
1822   restartany = -1;      /* uninitialized... */
1823   send_do(TELOPT_LFLOW, 1);
1824   if (his_want_state_is_will(TELOPT_ECHO)) {
1825     willoption(TELOPT_ECHO);
1826   }
1827   if (my_state_is_wont(TELOPT_ECHO))
1828     send_will(TELOPT_ECHO, 1);
1829   init_termbuf(ncct);
1830 #ifdef LINEMODE
1831   localstat(ncct);
1832 #endif
1833   send_do(TELOPT_TTYPE, 1);
1834   send_do(TELOPT_TSPEED, 1);
1835   send_do(TELOPT_XDISPLOC, 1);
1836   send_do(TELOPT_NEW_ENVIRON, 1);
1837   send_do(TELOPT_OLD_ENVIRON, 1);
1838   nc_write(ncct, ttytype_sbbuf, sizeof(ttytype_sbbuf));
1839
1840   telnet = ncct->telnet;
1841   ncct->telnet = tmp;
1842   return telnet;
1843 }
1844 void
1845 noit_console_telnet_free(noit_console_telnet_closure_t telnet) {
1846   free(telnet->_subbuffer);
1847   noit_hash_destroy(&telnet->_env, free, free);
1848   free(telnet);
1849 }
1850
1851 #define SB_CLEAR()      subpointer = subbuffer
1852 #define SB_TERM()       { subend = subpointer; SB_CLEAR(); }
1853 #define SB_ACCUM(c)     if (subpointer < (subbuffer+sizeof subbuffer)) { \
1854     *subpointer++ = (c); \
1855                              }
1856 #define SB_GET()        ((*subpointer++)&0xff)
1857 #define SB_EOF()        (subpointer >= subend)
1858 #define SB_LEN()        (subend - subpointer)
1859
1860 #ifdef  ENV_HACK
1861 unsigned char *subsave;
1862 #define SB_SAVE()       subsave = subpointer;
1863 #define SB_RESTORE()    subpointer = subsave;
1864 #endif
1865
1866
1867 /*
1868  * State for recv fsm
1869  */
1870 #define TS_DATA         0       /* base state */
1871 #define TS_IAC          1       /* look for double IAC's */
1872 #define TS_CR           2       /* CR-LF ->'s CR */
1873 #define TS_SB           3       /* throw away begin's... */
1874 #define TS_SE           4       /* ...end's (suboption negotiation) */
1875 #define TS_WILL         5       /* will option negotiation */
1876 #define TS_WONT         6       /* wont -''- */
1877 #define TS_DO           7       /* do -''- */
1878 #define TS_DONT         8       /* dont -''- */
1879
1880 int
1881 noit_console_telnet_telrcv(noit_console_closure_t ncct,
1882                            const void *buf, int buflen)
1883 {
1884     int c;
1885     unsigned char cc;
1886     unsigned char *netip = (unsigned char *)buf;
1887     int ncc = buflen;
1888     static int state = TS_DATA;
1889
1890     while (ncc > 0) {
1891         c = *netip++ & 0377, ncc--;
1892 #ifdef ENCRYPTION
1893         if (decrypt_input)
1894             c = (*decrypt_input)(c);
1895 #endif
1896         switch (state) {
1897
1898         case TS_CR:
1899             state = TS_DATA;
1900             /* Strip off \n or \0 after a \r */
1901             if ((c == 0) || (c == '\n')) {
1902                 break;
1903             }
1904             /* FALL THROUGH */
1905
1906         case TS_DATA:
1907             if (c == IAC) {
1908                 state = TS_IAC;
1909                 break;
1910             }
1911             /*
1912              * We now map \r\n ==> \r for pragmatic reasons.
1913              * Many client implementations send \r\n when
1914              * the user hits the CarriageReturn key.
1915              *
1916              * We USED to map \r\n ==> \n, since \r\n says
1917              * that we want to be in column 1 of the next
1918              * printable line, and \n is the standard
1919              * unix way of saying that (\r is only good
1920              * if CRMOD is set, which it normally is).
1921              */
1922             if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
1923                 int nc = *netip;
1924 #ifdef ENCRYPTION
1925                 if (decrypt_input)
1926                     nc = (*decrypt_input)(nc & 0xff);
1927 #endif
1928                 if (linemode && (ncc > 0) && (('\n' == nc) ||
1929                          ((0 == nc) && noit_console_telnet_tty_iscrnl(ncct))) ) {
1930                         netip++; ncc--;
1931                         c = '\n';
1932                 } else
1933                 {
1934 #ifdef ENCRYPTION
1935                     if (decrypt_input)
1936                         (void)(*decrypt_input)(-1);
1937 #endif
1938                     state = TS_CR;
1939                 }
1940             }
1941             cc = (unsigned char)c;
1942             pty_write(ncct, &cc, 1);
1943             break;
1944
1945         case TS_IAC:
1946         gotiac:                 switch (c) {
1947
1948             /*
1949              * Send the process on the pty side an
1950              * interrupt.  Do this with a NULL or
1951              * interrupt char; depending on the tty mode.
1952              */
1953         case IP:
1954             interrupt(ncct);
1955             break;
1956
1957         case BREAK:
1958             sendbrk(ncct);
1959             break;
1960
1961             /*
1962              * Are You There?
1963              */
1964         case AYT:
1965             recv_ayt(ncct);
1966             break;
1967
1968             /*
1969              * Abort Output
1970              */
1971         case AO:
1972             {
1973                 ptyflush(ncct); /* half-hearted */
1974                 init_termbuf(ncct);
1975
1976                 if (slctab[SLC_AO].sptr &&
1977                     *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
1978                     cc = (unsigned char)*slctab[SLC_AO].sptr;
1979                     pty_write(ncct, &cc, 1);
1980                 }
1981
1982                 netclear(ncct); /* clear buffer back */
1983                 nc_printf (ncct, "%c%c", IAC, DM);
1984                 /* Wha?
1985                 neturg = nfrontp-1;
1986                 */
1987                 /* off by one XXX */
1988                 break;
1989             }
1990
1991         /*
1992          * Erase Character and
1993          * Erase Line
1994          */
1995         case EC:
1996         case EL:
1997             {
1998                 cc_t ch;
1999
2000                 ptyflush(ncct); /* half-hearted */
2001                 init_termbuf(ncct);
2002                 if (c == EC)
2003                     ch = *slctab[SLC_EC].sptr;
2004                 else
2005                     ch = *slctab[SLC_EL].sptr;
2006                 if (ch != (cc_t)(_POSIX_VDISABLE)) {
2007                     cc = (unsigned char)ch;
2008                     pty_write(ncct, &cc, 1);
2009                 }
2010                 break;
2011             }
2012
2013         /*
2014          * Check for urgent data...
2015          */
2016         case DM:
2017 #ifdef SUPPORT_OOB
2018             SYNCHing = stilloob(net);
2019             settimer(gotDM);
2020 #endif
2021             break;
2022
2023
2024             /*
2025              * Begin option subnegotiation...
2026              */
2027         case SB:
2028             state = TS_SB;
2029             SB_CLEAR();
2030             continue;
2031
2032         case WILL:
2033             state = TS_WILL;
2034             continue;
2035
2036         case WONT:
2037             state = TS_WONT;
2038             continue;
2039
2040         case DO:
2041             state = TS_DO;
2042             continue;
2043
2044         case DONT:
2045             state = TS_DONT;
2046             continue;
2047         case EOR:
2048             if (his_state_is_will(TELOPT_EOR))
2049                 doeof(ncct);
2050             break;
2051
2052             /*
2053              * Handle RFC 10xx Telnet linemode option additions
2054              * to command stream (EOF, SUSP, ABORT).
2055              */
2056         case xEOF:
2057             doeof(ncct);
2058             break;
2059
2060         case SUSP:
2061             sendsusp(ncct);
2062             break;
2063
2064         case ABORT:
2065             sendbrk(ncct);
2066             break;
2067
2068         case IAC:
2069             cc = (unsigned char)c;
2070             pty_write(ncct, &cc, 1);
2071             break;
2072         }
2073         state = TS_DATA;
2074         break;
2075
2076         case TS_SB:
2077             if (c == IAC) {
2078                 state = TS_SE;
2079             } else {
2080                 SB_ACCUM(c);
2081             }
2082             break;
2083
2084         case TS_SE:
2085             if (c != SE) {
2086                 if (c != IAC) {
2087                     /*
2088                      * bad form of suboption negotiation.
2089                      * handle it in such a way as to avoid
2090                      * damage to local state.  Parse
2091                      * suboption buffer found so far,
2092                      * then treat remaining stream as
2093                      * another command sequence.
2094                      */
2095
2096                     /* for DIAGNOSTICS */
2097                     SB_ACCUM(IAC);
2098                     SB_ACCUM(c);
2099                     subpointer -= 2;
2100
2101                     SB_TERM();
2102                     suboption(ncct);
2103                     state = TS_IAC;
2104                     goto gotiac;
2105                 }
2106                 SB_ACCUM(c);
2107                 state = TS_SB;
2108             } else {
2109                 /* for DIAGNOSTICS */
2110                 SB_ACCUM(IAC);
2111                 SB_ACCUM(SE);
2112                 subpointer -= 2;
2113
2114                 SB_TERM();
2115                 suboption(ncct);        /* handle sub-option */
2116                 state = TS_DATA;
2117             }
2118             break;
2119
2120         case TS_WILL:
2121             willoption(c);
2122             state = TS_DATA;
2123             continue;
2124
2125         case TS_WONT:
2126             wontoption(c);
2127             if (c==TELOPT_ENCRYPT && his_do_dont_is_changing(TELOPT_ENCRYPT) )
2128                 dontoption(c);
2129             state = TS_DATA;
2130             continue;
2131
2132         case TS_DO:
2133             dooption(c);
2134             state = TS_DATA;
2135             continue;
2136
2137         case TS_DONT:
2138             dontoption(c);
2139             state = TS_DATA;
2140             continue;
2141
2142         default:
2143             return -1;
2144         }
2145     }
2146     return 0;
2147 }  /* end of telrcv */
2148
2149 /*
2150  * The will/wont/do/dont state machines are based on Dave Borman's
2151  * Telnet option processing state machine.
2152  *
2153  * These correspond to the following states:
2154  *      my_state = the last negotiated state
2155  *      want_state = what I want the state to go to
2156  *      want_resp = how many requests I have sent
2157  * All state defaults are negative, and resp defaults to 0.
2158  *
2159  * When initiating a request to change state to new_state:
2160  *
2161  * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
2162  *      do nothing;
2163  * } else {
2164  *      want_state = new_state;
2165  *      send new_state;
2166  *      want_resp++;
2167  * }
2168  *
2169  * When receiving new_state:
2170  *
2171  * if (want_resp) {
2172  *      want_resp--;
2173  *      if (want_resp && (new_state == my_state))
2174  *              want_resp--;
2175  * }
2176  * if ((want_resp == 0) && (new_state != want_state)) {
2177  *      if (ok_to_switch_to new_state)
2178  *              want_state = new_state;
2179  *      else
2180  *              want_resp++;
2181  *      send want_state;
2182  * }
2183  * my_state = new_state;
2184  *
2185  * Note that new_state is implied in these functions by the function itself.
2186  * will and do imply positive new_state, wont and dont imply negative.
2187  *
2188  * Finally, there is one catch.  If we send a negative response to a
2189  * positive request, my_state will be the positive while want_state will
2190  * remain negative.  my_state will revert to negative when the negative
2191  * acknowlegment arrives from the peer.  Thus, my_state generally tells
2192  * us not only the last negotiated state, but also tells us what the peer
2193  * wants to be doing as well.  It is important to understand this difference
2194  * as we may wish to be processing data streams based on our desired state
2195  * (want_state) or based on what the peer thinks the state is (my_state).
2196  *
2197  * This all works fine because if the peer sends a positive request, the data
2198  * that we receive prior to negative acknowlegment will probably be affected
2199  * by the positive state, and we can process it as such (if we can; if we
2200  * can't then it really doesn't matter).  If it is that important, then the
2201  * peer probably should be buffering until this option state negotiation
2202  * is complete.
2203  *
2204  */
2205 void
2206 noit_console_telnet_send_do(noit_console_closure_t ncct, int option, int init)
2207 {
2208     if (init) {
2209         if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
2210             his_want_state_is_will(option))
2211             return;
2212         /*
2213          * Special case for TELOPT_TM:  We send a DO, but pretend
2214          * that we sent a DONT, so that we can send more DOs if
2215          * we want to.
2216          */
2217         if (option == TELOPT_TM)
2218             set_his_want_state_wont(option);
2219         else
2220             set_his_want_state_will(option);
2221         do_dont_resp[option]++;
2222     }
2223     nc_printf(ncct, (const char *)doopt, option);
2224 }
2225
2226 #ifdef  AUTHENTICATION
2227 extern void auth_request(void);
2228 #endif
2229 #ifdef  ENCRYPTION
2230 extern void encrypt_send_support();
2231 #endif
2232
2233 void
2234 noit_console_telnet_willoption(noit_console_closure_t ncct, int option)
2235 {
2236     int changeok = 0;
2237     void (*func)(noit_console_closure_t) = 0;
2238
2239     /*
2240      * process input from peer.
2241      */
2242
2243     if (do_dont_resp[option]) {
2244         do_dont_resp[option]--;
2245         if (do_dont_resp[option] && his_state_is_will(option))
2246             do_dont_resp[option]--;
2247     }
2248     if (do_dont_resp[option] == 0) {
2249         if (his_want_state_is_wont(option)) {
2250             switch (option) {
2251
2252             case TELOPT_BINARY:
2253                 init_termbuf(ncct);
2254                 noit_console_telnet_tty_binaryin(ncct, 1);
2255                 set_termbuf(ncct);
2256                 changeok++;
2257                 break;
2258
2259             case TELOPT_ECHO:
2260                 /*
2261                  * See comments below for more info.
2262                  */
2263                 not42 = 0;      /* looks like a 4.2 system */
2264                 break;
2265
2266             case TELOPT_TM:
2267 #if     defined(LINEMODE) && defined(KLUDGELINEMODE)
2268                         /*
2269                          * This telnetd implementation does not really
2270                          * support timing marks, it just uses them to
2271                          * support the kludge linemode stuff.  If we
2272                          * receive a will or wont TM in response to our
2273                          * do TM request that may have been sent to
2274                          * determine kludge linemode support, process
2275                          * it, otherwise TM should get a negative
2276                          * response back.
2277                          */
2278                         /*
2279                          * Handle the linemode kludge stuff.
2280                          * If we are not currently supporting any
2281                          * linemode at all, then we assume that this
2282                          * is the client telling us to use kludge
2283                          * linemode in response to our query.  Set the
2284                          * linemode type that is to be supported, note
2285                          * that the client wishes to use linemode, and
2286                          * eat the will TM as though it never arrived.
2287                          */
2288                         if (lmodetype < KLUDGE_LINEMODE) {
2289                                 lmodetype = KLUDGE_LINEMODE;
2290                                 clientstat(ncct, TELOPT_LINEMODE, WILL, 0);
2291                                 send_wont(TELOPT_SGA, 1);
2292                         } else if (lmodetype == NO_AUTOKLUDGE) {
2293                                 lmodetype = KLUDGE_OK;
2294                         }
2295 #endif  /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
2296                 break;
2297
2298             case TELOPT_LFLOW:
2299                 /*
2300                  * If we are going to support flow control
2301                  * option, then don't worry peer that we can't
2302                  * change the flow control characters.
2303                  */
2304                 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
2305                 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
2306                 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
2307                 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
2308       /* FALLTHROUGH */
2309             case TELOPT_TTYPE:
2310             case TELOPT_SGA:
2311             case TELOPT_NAWS:
2312             case TELOPT_TSPEED:
2313             case TELOPT_XDISPLOC:
2314             case TELOPT_NEW_ENVIRON:
2315             case TELOPT_OLD_ENVIRON:
2316                 changeok++;
2317                 break;
2318
2319 #ifdef  LINEMODE
2320                 case TELOPT_LINEMODE:
2321 # ifdef KLUDGELINEMODE
2322                         /*
2323                          * Note client's desire to use linemode.
2324                          */
2325                         lmodetype = REAL_LINEMODE;
2326 # endif /* KLUDGELINEMODE */
2327                         func = noit_console_telnet_doclientstat;
2328                         changeok++;
2329                         break;
2330 #endif  /* LINEMODE */
2331
2332 #ifdef  AUTHENTICATION
2333             case TELOPT_AUTHENTICATION:
2334                 func = auth_request;
2335                 changeok++;
2336                 break;
2337 #endif
2338
2339 #ifdef  ENCRYPTION
2340             case TELOPT_ENCRYPT:
2341                 func = encrypt_send_support;
2342                 changeok++;
2343                 break;
2344 #endif
2345                        
2346             default:
2347                 break;
2348             }
2349             if (changeok) {
2350                 set_his_want_state_will(option);
2351                 send_do(option, 0);
2352             } else {
2353                 do_dont_resp[option]++;
2354                 send_dont(option, 0);
2355             }
2356         } else {
2357             /*
2358              * Option processing that should happen when
2359              * we receive conformation of a change in
2360              * state that we had requested.
2361              */
2362             switch (option) {
2363             case TELOPT_ECHO:
2364                 not42 = 0;      /* looks like a 4.2 system */
2365                 /*
2366                  * Egads, he responded "WILL ECHO".  Turn
2367                  * it off right now!
2368                  */
2369                 send_dont(option, 1);
2370                 /*
2371                  * "WILL ECHO".  Kludge upon kludge!
2372                  * A 4.2 client is now echoing user input at
2373                  * the tty.  This is probably undesireable and
2374                  * it should be stopped.  The client will
2375                  * respond WONT TM to the DO TM that we send to
2376                  * check for kludge linemode.  When the WONT TM
2377                  * arrives, linemode will be turned off and a
2378                  * change propogated to the pty.  This change
2379                  * will cause us to process the new pty state
2380                  * in localstat(), which will notice that
2381                  * linemode is off and send a WILL ECHO
2382                  * so that we are properly in character mode and
2383                  * all is well.
2384                  */
2385                 break;
2386
2387 #ifdef  LINEMODE
2388                 case TELOPT_LINEMODE:
2389 # ifdef KLUDGELINEMODE
2390                         /*
2391                          * Note client's desire to use linemode.
2392                          */
2393                         lmodetype = REAL_LINEMODE;
2394 # endif /* KLUDGELINEMODE */
2395                         func = noit_console_telnet_doclientstat;
2396                         break;
2397 #endif  /* LINEMODE */
2398 #ifdef  AUTHENTICATION
2399             case TELOPT_AUTHENTICATION:
2400                 func = auth_request;
2401                 break;
2402 #endif
2403
2404 #ifdef  ENCRYPTION
2405             case TELOPT_ENCRYPT:
2406                 func = encrypt_send_support;
2407                 break;
2408 #endif
2409
2410             case TELOPT_LFLOW:
2411                 func = flowstat;
2412                 break;
2413             }
2414         }
2415     }
2416     set_his_state_will(option);
2417     if (func)
2418         (*func)(ncct);
2419 }  /* end of willoption */
2420
2421 void
2422 noit_console_telnet_send_dont(noit_console_closure_t ncct, int option, int init)
2423 {
2424     if (init) {
2425         if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
2426             his_want_state_is_wont(option))
2427             return;
2428         set_his_want_state_wont(option);
2429         do_dont_resp[option]++;
2430     }
2431     nc_printf(ncct, (const char *)dont, option);
2432 }
2433
2434 void
2435 noit_console_telnet_wontoption(noit_console_closure_t ncct, int option)
2436 {
2437     /*
2438      * Process client input.
2439          */
2440
2441     if (do_dont_resp[option]) {
2442         do_dont_resp[option]--;
2443         if (do_dont_resp[option] && his_state_is_wont(option))
2444             do_dont_resp[option]--;
2445     }
2446     if (do_dont_resp[option] == 0) {
2447         if (his_want_state_is_will(option)) {
2448             /* it is always ok to change to negative state */
2449             switch (option) {
2450             case TELOPT_ECHO:
2451                 not42 = 1; /* doesn't seem to be a 4.2 system */
2452                 break;
2453
2454             case TELOPT_BINARY:
2455                 init_termbuf(ncct);
2456                 noit_console_telnet_tty_binaryin(ncct, 0);
2457                 set_termbuf(ncct);
2458                 break;
2459
2460 #ifdef  LINEMODE
2461             case TELOPT_LINEMODE:
2462 # ifdef KLUDGELINEMODE
2463                 /*
2464                  * If real linemode is supported, then client is
2465                  * asking to turn linemode off.
2466                  */
2467                 if (lmodetype != REAL_LINEMODE)
2468                         break;
2469                 lmodetype = KLUDGE_LINEMODE;
2470 # endif /* KLUDGELINEMODE */
2471                 clientstat(ncct, TELOPT_LINEMODE, WONT, 0);
2472                 break;
2473 #endif  /* LINEMODE */
2474
2475             case TELOPT_TM:
2476                 /*
2477                  * If we get a WONT TM, and had sent a DO TM,
2478                  * don't respond with a DONT TM, just leave it
2479                  * as is.  Short circut the state machine to
2480                  * achive this.
2481                  */
2482                 set_his_want_state_wont(TELOPT_TM);
2483                 return;
2484
2485             case TELOPT_LFLOW:
2486                 /*
2487                  * If we are not going to support flow control
2488                  * option, then let peer know that we can't
2489                  * change the flow control characters.
2490                  */
2491                 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
2492                 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
2493                 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
2494                 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
2495                 break;
2496
2497 #ifdef AUTHENTICATION
2498             case TELOPT_AUTHENTICATION:
2499                 auth_finished(0, AUTH_REJECT);
2500                 break;
2501 #endif
2502
2503                 /*
2504                  * For options that we might spin waiting for
2505                  * sub-negotiation, if the client turns off the
2506                  * option rather than responding to the request,
2507                  * we have to treat it here as if we got a response
2508                  * to the sub-negotiation, (by updating the timers)
2509                  * so that we'll break out of the loop.
2510                  */
2511             case TELOPT_TTYPE:
2512                 settimer(ttypesubopt);
2513                 break;
2514
2515             case TELOPT_TSPEED:
2516                 settimer(tspeedsubopt);
2517                 break;
2518
2519             case TELOPT_XDISPLOC:
2520                 settimer(xdisplocsubopt);
2521                 break;
2522
2523             case TELOPT_OLD_ENVIRON:
2524                 settimer(oenvironsubopt);
2525                 break;
2526
2527             case TELOPT_NEW_ENVIRON:
2528                 settimer(environsubopt);
2529                 break;
2530
2531             default:
2532                 break;
2533             }
2534             set_his_want_state_wont(option);
2535             if (his_state_is_will(option))
2536                 send_dont(option, 0);
2537         } else {
2538             switch (option) {
2539             case TELOPT_TM:
2540 #if     defined(LINEMODE) && defined(KLUDGELINEMODE)
2541                 if (lmodetype < NO_AUTOKLUDGE) {
2542                         lmodetype = NO_LINEMODE;
2543                         clientstat(TELOPT_LINEMODE, WONT, 0);
2544                         send_will(TELOPT_SGA, 1);
2545                         send_will(TELOPT_ECHO, 1);
2546                 }
2547 #endif  /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
2548                 break;
2549
2550 #ifdef AUTHENTICATION
2551             case TELOPT_AUTHENTICATION:
2552                 auth_finished(0, AUTH_REJECT);
2553                 break;
2554 #endif
2555             default:
2556                 break;
2557             }
2558         }
2559     }
2560     set_his_state_wont(option);
2561
2562 }  /* end of wontoption */
2563
2564 void
2565 noit_console_telnet_send_will(noit_console_closure_t ncct, int option, int init)
2566 {
2567     if (init) {
2568         if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
2569             my_want_state_is_will(option))
2570             return;
2571         set_my_want_state_will(option);
2572         will_wont_resp[option]++;
2573     }
2574     nc_printf (ncct, (const char *)will, option);
2575 }
2576
2577 void
2578 noit_console_telnet_dooption(noit_console_closure_t ncct, int option)
2579 {
2580     int changeok = 0;
2581
2582     /*
2583      * Process client input.
2584      */
2585
2586     if (will_wont_resp[option]) {
2587         will_wont_resp[option]--;
2588         if (will_wont_resp[option] && my_state_is_will(option))
2589             will_wont_resp[option]--;
2590     }
2591     if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
2592         switch (option) {
2593         case TELOPT_ECHO:
2594 #ifdef  LINEMODE
2595 # ifdef KLUDGELINEMODE
2596             if (lmodetype == NO_LINEMODE)
2597 # else
2598             if (his_state_is_wont(TELOPT_LINEMODE))
2599 # endif
2600 #endif
2601             {
2602                 init_termbuf(ncct);
2603                 noit_console_telnet_tty_setecho(ncct, 1);
2604                 set_termbuf(ncct);
2605             }
2606         changeok++;
2607         break;
2608
2609         case TELOPT_BINARY:
2610             init_termbuf(ncct);
2611             noit_console_telnet_tty_binaryout(ncct, 1);
2612             set_termbuf(ncct);
2613             changeok++;
2614             break;
2615
2616         case TELOPT_SGA:
2617 #if     defined(LINEMODE) && defined(KLUDGELINEMODE)
2618                 /*
2619                  * If kludge linemode is in use, then we must
2620                  * process an incoming do SGA for linemode
2621                  * purposes.
2622                  */
2623             if (lmodetype == KLUDGE_LINEMODE) {
2624                 /*
2625                  * Receipt of "do SGA" in kludge
2626                  * linemode is the peer asking us to
2627                  * turn off linemode.  Make note of
2628                  * the request.
2629                  */
2630                 clientstat(TELOPT_LINEMODE, WONT, 0);
2631                 /*
2632                  * If linemode did not get turned off
2633                  * then don't tell peer that we did.
2634                  * Breaking here forces a wont SGA to
2635                  * be returned.
2636                  */
2637                 if (linemode)
2638                         break;
2639             }
2640 #else
2641             turn_on_sga = 0;
2642 #endif  /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
2643             changeok++;
2644             break;
2645
2646         case TELOPT_STATUS:
2647             changeok++;
2648             break;
2649
2650         case TELOPT_TM:
2651             /*
2652              * Special case for TM.  We send a WILL, but
2653              * pretend we sent a WONT.
2654              */
2655             send_will(option, 0);
2656             set_my_want_state_wont(option);
2657             set_my_state_wont(option);
2658             return;
2659
2660         case TELOPT_LOGOUT:
2661             /*
2662              * When we get a LOGOUT option, respond
2663              * with a WILL LOGOUT, make sure that
2664              * it gets written out to the network,
2665              * and then just go away...
2666              */
2667             set_my_want_state_will(TELOPT_LOGOUT);
2668             send_will(TELOPT_LOGOUT, 0);
2669             set_my_state_will(TELOPT_LOGOUT);
2670             netflush(ncct);
2671             return;
2672             break;
2673
2674 #ifdef ENCRYPTION
2675         case TELOPT_ENCRYPT:
2676             changeok++;
2677             break;
2678 #endif
2679         case TELOPT_LINEMODE:
2680         case TELOPT_TTYPE:
2681         case TELOPT_NAWS:
2682         case TELOPT_TSPEED:
2683         case TELOPT_LFLOW:
2684         case TELOPT_XDISPLOC:
2685 #ifdef  TELOPT_ENVIRON
2686         case TELOPT_NEW_ENVIRON:
2687 #endif
2688         case TELOPT_OLD_ENVIRON:
2689         default:
2690             break;
2691         }
2692         if (changeok) {
2693             set_my_want_state_will(option);
2694             send_will(option, 0);
2695         } else {
2696             will_wont_resp[option]++;
2697             send_wont(option, 0);
2698         }
2699     }
2700     set_my_state_will(option);
2701 }  /* end of dooption */
2702
2703 void
2704 noit_console_telnet_send_wont(noit_console_closure_t ncct, int option, int init)
2705 {
2706     if (init) {
2707         if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
2708             my_want_state_is_wont(option))
2709             return;
2710         set_my_want_state_wont(option);
2711         will_wont_resp[option]++;
2712     }
2713     nc_printf (ncct, (const char *)wont, option);
2714 }
2715
2716 void
2717 noit_console_telnet_dontoption(noit_console_closure_t ncct, int option)
2718 {
2719     /*
2720      * Process client input.
2721          */
2722
2723
2724     if (will_wont_resp[option]) {
2725         will_wont_resp[option]--;
2726         if (will_wont_resp[option] && my_state_is_wont(option))
2727             will_wont_resp[option]--;
2728     }
2729     if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
2730         switch (option) {
2731         case TELOPT_BINARY:
2732             init_termbuf(ncct);
2733             noit_console_telnet_tty_binaryout(ncct, 0);
2734             set_termbuf(ncct);
2735             break;
2736
2737         case TELOPT_ECHO:       /* we should stop echoing */
2738 #ifdef  LINEMODE
2739 # ifdef KLUDGELINEMODE
2740             if ((lmodetype != REAL_LINEMODE) &&
2741                     (lmodetype != KLUDGE_LINEMODE))
2742 # else
2743             if (his_state_is_wont(TELOPT_LINEMODE))
2744 # endif
2745 #endif
2746             {
2747                 init_termbuf(ncct);
2748                 noit_console_telnet_tty_setecho(ncct, 0);
2749                 set_termbuf(ncct);
2750             }
2751         break;
2752
2753         case TELOPT_SGA:
2754 #if     defined(LINEMODE) && defined(KLUDGELINEMODE)
2755                 /*
2756                  * If kludge linemode is in use, then we
2757                  * must process an incoming do SGA for
2758                  * linemode purposes.
2759                  */
2760             if ((lmodetype == KLUDGE_LINEMODE) ||
2761                     (lmodetype == KLUDGE_OK)) {
2762                 /*
2763                  * The client is asking us to turn
2764                  * linemode on.
2765                  */
2766                 lmodetype = KLUDGE_LINEMODE;
2767                 clientstat(TELOPT_LINEMODE, WILL, 0);
2768                 /*
2769                  * If we did not turn line mode on,
2770                  * then what do we say?  Will SGA?
2771                  * This violates design of telnet.
2772                  * Gross.  Very Gross.
2773                  */
2774             }
2775             break;
2776 #else
2777             set_my_want_state_wont(option);
2778             if (my_state_is_will(option))
2779                 send_wont(option, 0);
2780             set_my_state_wont(option);
2781             if (turn_on_sga ^= 1)
2782                 send_will(option, 1);
2783             return;
2784 #endif  /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
2785
2786         default:
2787             break;
2788         }
2789
2790         set_my_want_state_wont(option);
2791         if (my_state_is_will(option))
2792             send_wont(option, 0);
2793     }
2794     set_my_state_wont(option);
2795
2796 }  /* end of dontoption */
2797
2798 #ifdef  ENV_HACK
2799 int env_ovar = -1;
2800 int env_ovalue = -1;
2801 #else   /* ENV_HACK */
2802 # define env_ovar OLD_ENV_VAR
2803 # define env_ovalue OLD_ENV_VALUE
2804 #endif  /* ENV_HACK */
2805
2806 /*
2807  * suboption()
2808  *
2809  *      Look at the sub-option buffer, and try to be helpful to the other
2810  * side.
2811  *
2812  *      Currently we recognize:
2813  *
2814  *      Terminal type is
2815  *      Linemode
2816  *      Window size
2817  *      Terminal speed
2818  */
2819 static void
2820 suboption(noit_console_closure_t ncct)
2821 {
2822     int subchar;
2823
2824     subchar = SB_GET();
2825     switch (subchar) {
2826     case TELOPT_TSPEED: {
2827         int xspeed, rspeed;
2828
2829         if (his_state_is_wont(TELOPT_TSPEED))   /* Ignore if option disabled */
2830             break;
2831
2832         settimer(tspeedsubopt);
2833
2834         if (SB_EOF() || SB_GET() != TELQUAL_IS)
2835             return;
2836
2837         xspeed = atoi((char *)subpointer);
2838
2839         while (SB_GET() != ',' && !SB_EOF());
2840         if (SB_EOF())
2841             return;
2842
2843         rspeed = atoi((char *)subpointer);
2844         clientstat(ncct, TELOPT_TSPEED, xspeed, rspeed);
2845
2846         break;
2847
2848     }  /* end of case TELOPT_TSPEED */
2849
2850     case TELOPT_TTYPE: {                /* Yaaaay! */
2851         char *terminaltype;
2852         if (his_state_is_wont(TELOPT_TTYPE))    /* Ignore if option disabled */
2853             break;
2854         settimer(ttypesubopt);
2855
2856         if (SB_EOF() || SB_GET() != TELQUAL_IS) {
2857             return;             /* ??? XXX but, this is the most robust */
2858         }
2859
2860         terminaltype = terminalname;
2861
2862         while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
2863                !SB_EOF()) {
2864             int c;
2865
2866             c = SB_GET();
2867             if (isupper(c)) {
2868                 c = tolower(c);
2869             }
2870             *terminaltype++ = c;    /* accumulate name */
2871         }
2872         *terminaltype = 0;
2873         break;
2874     }  /* end of case TELOPT_TTYPE */
2875
2876     case TELOPT_NAWS: {
2877         int xwinsize, ywinsize;
2878
2879         if (his_state_is_wont(TELOPT_NAWS))     /* Ignore if option disabled */
2880             break;
2881
2882         if (SB_EOF())
2883             return;
2884         xwinsize = SB_GET() << 8;
2885         if (SB_EOF())
2886             return;
2887         xwinsize |= SB_GET();
2888         if (SB_EOF())
2889             return;
2890         ywinsize = SB_GET() << 8;
2891         if (SB_EOF())
2892             return;
2893         ywinsize |= SB_GET();
2894         clientstat(ncct, TELOPT_NAWS, xwinsize, ywinsize);
2895
2896         break;
2897
2898     }  /* end of case TELOPT_NAWS */
2899 #ifdef  LINEMODE
2900     case TELOPT_LINEMODE: {
2901         int request;
2902
2903         if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
2904                 break;
2905         /*
2906          * Process linemode suboptions.
2907          */
2908         if (SB_EOF())
2909             break;              /* garbage was sent */
2910         request = SB_GET();     /* get will/wont */
2911
2912         if (SB_EOF())
2913             break;              /* another garbage check */
2914
2915         if (request == LM_SLC) {  /* SLC is not preceeded by WILL or WONT */
2916                 /*
2917                  * Process suboption buffer of slc's
2918                  */
2919                 start_slc(ncct, 1);
2920                 do_opt_slc(ncct, subpointer, subend - subpointer);
2921                 (void) end_slc(ncct, 0);
2922                 break;
2923         } else if (request == LM_MODE) {
2924                 if (SB_EOF())
2925                     return;
2926                 useeditmode = SB_GET();  /* get mode flag */
2927                 clientstat(ncct, LM_MODE, 0, 0);
2928                 break;
2929         }
2930
2931         if (SB_EOF())
2932             break;
2933         switch (SB_GET()) {  /* what suboption? */
2934         case LM_FORWARDMASK:
2935                 /*
2936                  * According to spec, only server can send request for
2937                  * forwardmask, and client can only return a positive response.
2938                  * So don't worry about it.
2939                  */
2940
2941         default:
2942                 break;
2943         }
2944         break;
2945     }  /* end of case TELOPT_LINEMODE */
2946 #endif
2947     case TELOPT_STATUS: {
2948         int mode;
2949
2950         if (SB_EOF())
2951             break;
2952         mode = SB_GET();
2953         switch (mode) {
2954         case TELQUAL_SEND:
2955             if (my_state_is_will(TELOPT_STATUS))
2956                 send_status();
2957             break;
2958
2959         case TELQUAL_IS:
2960             break;
2961
2962         default:
2963             break;
2964         }
2965         break;
2966     }  /* end of case TELOPT_STATUS */
2967
2968     case TELOPT_XDISPLOC: {
2969         if (SB_EOF() || SB_GET() != TELQUAL_IS)
2970             return;
2971         settimer(xdisplocsubopt);
2972         subpointer[SB_LEN()] = '\0';
2973         noit_hash_replace(&ncct->telnet->_env,
2974                           strdup("DISPLAY"), strlen("DISPLAY"),
2975                           strdup((char *)subpointer),
2976                           free, free);
2977         break;
2978     }  /* end of case TELOPT_XDISPLOC */
2979
2980 #ifdef  TELOPT_NEW_ENVIRON
2981     case TELOPT_NEW_ENVIRON:
2982 #endif
2983     case TELOPT_OLD_ENVIRON: {
2984         int c;
2985         char *cp, *varp, *valp;
2986
2987         if (SB_EOF())
2988             return;
2989         c = SB_GET();
2990         if (c == TELQUAL_IS) {
2991             if (subchar == TELOPT_OLD_ENVIRON)
2992                 settimer(oenvironsubopt);
2993             else
2994                 settimer(environsubopt);
2995         } else if (c != TELQUAL_INFO) {
2996             return;
2997         }
2998
2999 #ifdef  TELOPT_NEW_ENVIRON
3000         if (subchar == TELOPT_NEW_ENVIRON) {
3001             while (!SB_EOF()) {
3002                 c = SB_GET();
3003                 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
3004                     break;
3005             }
3006         } else
3007 #endif
3008             {
3009 #ifdef  ENV_HACK
3010                 /*
3011                  * We only want to do this if we haven't already decided
3012                  * whether or not the other side has its VALUE and VAR
3013                  * reversed.
3014                  */
3015                 if (env_ovar < 0) {
3016                     int last = -1;              /* invalid value */
3017                     int empty = 0;
3018                     int got_var = 0, got_value = 0, got_uservar = 0;
3019
3020                     /*
3021                      * The other side might have its VALUE and VAR values
3022                      * reversed.  To be interoperable, we need to determine
3023                      * which way it is.  If the first recognized character
3024                      * is a VAR or VALUE, then that will tell us what
3025                      * type of client it is.  If the fist recognized
3026                      * character is a USERVAR, then we continue scanning
3027                      * the suboption looking for two consecutive
3028                      * VAR or VALUE fields.  We should not get two
3029                      * consecutive VALUE fields, so finding two
3030                      * consecutive VALUE or VAR fields will tell us
3031                      * what the client is.
3032                      */
3033                     SB_SAVE();
3034                     while (!SB_EOF()) {
3035                         c = SB_GET();
3036                         switch(c) {
3037                         case OLD_ENV_VAR:
3038                             if (last < 0 || last == OLD_ENV_VAR
3039                                 || (empty && (last == OLD_ENV_VALUE)))
3040                                 goto env_ovar_ok;
3041                             got_var++;
3042                             last = OLD_ENV_VAR;
3043                             break;
3044                         case OLD_ENV_VALUE:
3045                             if (last < 0 || last == OLD_ENV_VALUE
3046                                 || (empty && (last == OLD_ENV_VAR)))
3047                                 goto env_ovar_wrong;
3048                             got_value++;
3049                             last = OLD_ENV_VALUE;
3050                             break;
3051                         case ENV_USERVAR:
3052                             /* count strings of USERVAR as one */
3053                             if (last != ENV_USERVAR)
3054                                 got_uservar++;
3055                             if (empty) {
3056                                 if (last == OLD_ENV_VALUE)
3057                                     goto env_ovar_ok;
3058                                 if (last == OLD_ENV_VAR)
3059                                     goto env_ovar_wrong;
3060                             }
3061                             last = ENV_USERVAR;
3062                             break;
3063                         case ENV_ESC:
3064                             if (!SB_EOF())
3065                                 c = SB_GET();
3066                             /* FALL THROUGH */
3067                         default:
3068                             empty = 0;
3069                             continue;
3070                         }
3071                         empty = 1;
3072                     }
3073                     if (empty) {
3074                         if (last == OLD_ENV_VALUE)
3075                             goto env_ovar_ok;
3076                         if (last == OLD_ENV_VAR)
3077                             goto env_ovar_wrong;
3078                     }
3079                     /*
3080                      * Ok, the first thing was a USERVAR, and there
3081                      * are not two consecutive VAR or VALUE commands,
3082                      * and none of the VAR or VALUE commands are empty.
3083                      * If the client has sent us a well-formed option,
3084                      * then the number of VALUEs received should always
3085                      * be less than or equal to the number of VARs and
3086                      * USERVARs received.
3087                      *
3088                      * If we got exactly as many VALUEs as VARs and
3089                      * USERVARs, the client has the same definitions.
3090                      *
3091                      * If we got exactly as many VARs as VALUEs and
3092                      * USERVARS, the client has reversed definitions.
3093                      */
3094                     if (got_uservar + got_var == got_value) {
3095                     env_ovar_ok:
3096                         env_ovar = OLD_ENV_VAR;
3097                         env_ovalue = OLD_ENV_VALUE;
3098                     } else if (got_uservar + got_value == got_var) {
3099                     env_ovar_wrong:
3100                         env_ovar = OLD_ENV_VALUE;
3101                         env_ovalue = OLD_ENV_VAR;
3102                     }
3103                 }
3104                 SB_RESTORE();
3105 #endif
3106
3107                 while (!SB_EOF()) {
3108                     c = SB_GET();
3109                     if ((c == env_ovar) || (c == ENV_USERVAR))
3110                         break;
3111                 }
3112             }
3113
3114         if (SB_EOF())
3115             return;
3116
3117         cp = varp = (char *)subpointer;
3118         valp = 0;
3119
3120         while (!SB_EOF()) {
3121             c = SB_GET();
3122             if (subchar == TELOPT_OLD_ENVIRON) {
3123                 if (c == env_ovar)
3124                     c = NEW_ENV_VAR;
3125                 else if (c == env_ovalue)
3126                     c = NEW_ENV_VALUE;
3127             }
3128             switch (c) {
3129
3130             case NEW_ENV_VALUE:
3131                 *cp = '\0';
3132                 cp = valp = (char *)subpointer;
3133                 break;
3134
3135             case NEW_ENV_VAR:
3136             case ENV_USERVAR:
3137                 *cp = '\0';
3138                 if (valp)
3139                     noit_hash_replace(&ncct->telnet->_env,
3140                                       strdup(varp), strlen(varp),
3141                                       strdup(valp),
3142                                       free, free);
3143                 else
3144                     noit_hash_delete(&ncct->telnet->_env, varp, strlen(varp),
3145                                      free, free);
3146                 cp = varp = (char *)subpointer;
3147                 valp = 0;
3148                 break;
3149
3150             case ENV_ESC:
3151                 if (SB_EOF())
3152                     break;
3153                 c = SB_GET();
3154                 /* FALL THROUGH */
3155             default:
3156                 *cp++ = c;
3157                 break;
3158             }
3159         }
3160         *cp = '\0';
3161         if (valp)
3162             noit_hash_replace(&ncct->telnet->_env,
3163                               strdup(varp), strlen(varp),
3164                               strdup(valp),
3165                               free, free);
3166         else
3167             noit_hash_delete(&ncct->telnet->_env, varp, strlen(varp),
3168                              free, free);
3169         break;
3170     }  /* end of case TELOPT_NEW_ENVIRON */
3171 #ifdef AUTHENTICATION
3172     case TELOPT_AUTHENTICATION:
3173         if (SB_EOF())
3174             break;
3175         switch(SB_GET()) {
3176         case TELQUAL_SEND:
3177         case TELQUAL_REPLY:
3178             /*
3179              * These are sent by us and cannot be sent by
3180              * the client.
3181              */
3182             break;
3183         case TELQUAL_IS:
3184             auth_is(subpointer, SB_LEN());
3185             break;
3186         case TELQUAL_NAME:
3187             auth_name(subpointer, SB_LEN());
3188             break;
3189         }
3190         break;
3191 #endif
3192 #ifdef ENCRYPTION
3193     case TELOPT_ENCRYPT:
3194         if (SB_EOF())
3195             break;
3196         switch(SB_GET()) {
3197         case ENCRYPT_SUPPORT:
3198             encrypt_support(subpointer, SB_LEN());
3199             break;
3200         case ENCRYPT_IS:
3201             encrypt_is(subpointer, SB_LEN());
3202             break;
3203         case ENCRYPT_REPLY:
3204             encrypt_reply(subpointer, SB_LEN());
3205             break;
3206         case ENCRYPT_START:
3207             encrypt_start(subpointer, SB_LEN());
3208             break;
3209         case ENCRYPT_END:
3210             encrypt_end();
3211             break;
3212         case ENCRYPT_REQSTART:
3213             encrypt_request_start(subpointer, SB_LEN());
3214             break;
3215         case ENCRYPT_REQEND:
3216             /*
3217              * We can always send an REQEND so that we cannot
3218              * get stuck encrypting.  We should only get this
3219              * if we have been able to get in the correct mode
3220              * anyhow.
3221              */
3222             encrypt_request_end();
3223             break;
3224         case ENCRYPT_ENC_KEYID:
3225             encrypt_enc_keyid(subpointer, SB_LEN());
3226             break;
3227         case ENCRYPT_DEC_KEYID:
3228             encrypt_dec_keyid(subpointer, SB_LEN());
3229             break;
3230         default:
3231             break;
3232         }
3233         break;
3234 #endif
3235
3236     default:
3237         break;
3238     }  /* end of switch */
3239
3240 }  /* end of suboption */
3241
3242 void
3243 noit_console_telnet_doclientstat(noit_console_closure_t ncct)
3244 {
3245     clientstat(ncct, TELOPT_LINEMODE, WILL, 0);
3246 }
3247
3248 #undef ADD
3249 #define ADD(c)   *ncp++ = c
3250 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
3251
3252 void
3253 noit_console_telnet_send_status(noit_console_closure_t ncct)
3254 {
3255     unsigned char statusbuf[256];
3256     unsigned char *ncp;
3257     unsigned char i;
3258
3259     ncp = statusbuf;
3260
3261     netflush(ncct);     /* get rid of anything waiting to go out */
3262
3263     ADD(IAC);
3264     ADD(SB);
3265     ADD(TELOPT_STATUS);
3266     ADD(TELQUAL_IS);
3267
3268     /*
3269      * We check the want_state rather than the current state,
3270      * because if we received a DO/WILL for an option that we
3271      * don't support, and the other side didn't send a DONT/WONT
3272      * in response to our WONT/DONT, then the "state" will be
3273      * WILL/DO, and the "want_state" will be WONT/DONT.  We
3274      * need to go by the latter.
3275      */
3276     for (i = 0; i < (unsigned char)NTELOPTS; i++) {
3277         if (my_want_state_is_will(i)) {
3278             ADD(WILL);
3279             ADD_DATA(i);
3280         }
3281         if (his_want_state_is_will(i)) {
3282             ADD(DO);
3283             ADD_DATA(i);
3284         }
3285     }
3286
3287     if (his_want_state_is_will(TELOPT_LFLOW)) {
3288         ADD(SB);
3289         ADD(TELOPT_LFLOW);
3290         if (flowmode) {
3291             ADD(LFLOW_ON);
3292         } else {
3293             ADD(LFLOW_OFF);
3294         }
3295         ADD(SE);
3296
3297         if (restartany >= 0) {
3298             ADD(SB);
3299             ADD(TELOPT_LFLOW);
3300             if (restartany) {
3301                 ADD(LFLOW_RESTART_ANY);
3302             } else {
3303                 ADD(LFLOW_RESTART_XON);
3304             }
3305             ADD(SE);
3306         }
3307     }
3308
3309 #ifdef  LINEMODE
3310         if (his_want_state_is_will(TELOPT_LINEMODE)) {
3311                 unsigned char *cp, *cpe;
3312                 int len;
3313
3314                 ADD(SB);
3315                 ADD(TELOPT_LINEMODE);
3316                 ADD(LM_MODE);
3317                 ADD_DATA(editmode);
3318                 ADD(SE);
3319
3320                 ADD(SB);
3321                 ADD(TELOPT_LINEMODE);
3322                 ADD(LM_SLC);
3323                 start_slc(ncct, 0);
3324                 send_slc(ncct);
3325                 len = end_slc(ncct, &cp);
3326                 for (cpe = cp + len; cp < cpe; cp++)
3327                         ADD_DATA(*cp);
3328                 ADD(SE);
3329         }
3330 #endif  /* LINEMODE */
3331
3332     ADD(IAC);
3333     ADD(SE);
3334
3335     nc_write(ncct, statusbuf, ncp - statusbuf);
3336     netflush(ncct);     /* Send it on its way */
3337 }
Note: See TracBrowser for help on using the browser.