Home | History | Annotate | Line # | Download | only in telnetd
state.c revision 1.25
      1 /*	$NetBSD: state.c,v 1.25 2005/02/06 05:58:21 perry Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)state.c	8.5 (Berkeley) 5/30/95";
     36 #else
     37 __RCSID("$NetBSD: state.c,v 1.25 2005/02/06 05:58:21 perry Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <stdarg.h>
     42 
     43 #include "telnetd.h"
     44 
     45 static int envvarok(char *);
     46 
     47 unsigned const char	doopt[] = { IAC, DO, '%', 'c', 0 };
     48 unsigned const char	dont[] = { IAC, DONT, '%', 'c', 0 };
     49 unsigned const char	will[] = { IAC, WILL, '%', 'c', 0 };
     50 unsigned const char	wont[] = { IAC, WONT, '%', 'c', 0 };
     51 int	not42 = 1;
     52 
     53 /*
     54  * Buffer for sub-options, and macros
     55  * for suboptions buffer manipulations
     56  */
     57 unsigned char subbuffer[4096], *subpointer= subbuffer, *subend= subbuffer;
     58 
     59 #define	SB_CLEAR()	subpointer = subbuffer
     60 #define	SB_TERM()	{ subend = subpointer; SB_CLEAR(); }
     61 #define	SB_ACCUM(c)	if (subpointer < (subbuffer+sizeof subbuffer)) { \
     62 				*subpointer++ = (c); \
     63 			}
     64 #define	SB_GET()	((*subpointer++)&0xff)
     65 #define	SB_EOF()	(subpointer >= subend)
     66 #define	SB_LEN()	(subend - subpointer)
     67 
     68 #ifdef	ENV_HACK
     69 unsigned char *subsave;
     70 #define SB_SAVE()	subsave = subpointer;
     71 #define	SB_RESTORE()	subpointer = subsave;
     72 #endif
     73 
     74 
     75 /*
     76  * State for recv fsm
     77  */
     78 #define	TS_DATA		0	/* base state */
     79 #define	TS_IAC		1	/* look for double IAC's */
     80 #define	TS_CR		2	/* CR-LF ->'s CR */
     81 #define	TS_SB		3	/* throw away begin's... */
     82 #define	TS_SE		4	/* ...end's (suboption negotiation) */
     83 #define	TS_WILL		5	/* will option negotiation */
     84 #define	TS_WONT		6	/* wont " */
     85 #define	TS_DO		7	/* do " */
     86 #define	TS_DONT		8	/* dont " */
     87 
     88 void
     89 telrcv(void)
     90 {
     91 	int c;
     92 	static int state = TS_DATA;
     93 
     94 	while (ncc > 0) {
     95 		if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
     96 			break;
     97 		c = *netip++ & 0377, ncc--;
     98 #ifdef	ENCRYPTION
     99 		if (decrypt_input)
    100 			c = (*decrypt_input)(c);
    101 #endif	/* ENCRYPTION */
    102 		switch (state) {
    103 
    104 		case TS_CR:
    105 			state = TS_DATA;
    106 			/* Strip off \n or \0 after a \r */
    107 			if ((c == 0) || (c == '\n')) {
    108 				break;
    109 			}
    110 			/* FALL THROUGH */
    111 
    112 		case TS_DATA:
    113 			if (c == IAC) {
    114 				state = TS_IAC;
    115 				break;
    116 			}
    117 			/*
    118 			 * We now map \r\n ==> \r for pragmatic reasons.
    119 			 * Many client implementations send \r\n when
    120 			 * the user hits the CarriageReturn key.
    121 			 *
    122 			 * We USED to map \r\n ==> \n, since \r\n says
    123 			 * that we want to be in column 1 of the next
    124 			 * printable line, and \n is the standard
    125 			 * unix way of saying that (\r is only good
    126 			 * if CRMOD is set, which it normally is).
    127 			 */
    128 			if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
    129 				int nc = *netip;
    130 #ifdef	ENCRYPTION
    131 				if (decrypt_input)
    132 					nc = (*decrypt_input)(nc & 0xff);
    133 #endif	/* ENCRYPTION */
    134 #ifdef	LINEMODE
    135 				/*
    136 				 * If we are operating in linemode,
    137 				 * convert to local end-of-line.
    138 				 */
    139 				if (linemode && (ncc > 0) && (('\n' == nc) ||
    140 					 ((0 == nc) && tty_iscrnl())) ) {
    141 					netip++; ncc--;
    142 					c = '\n';
    143 				} else
    144 #endif
    145 				{
    146 #ifdef	ENCRYPTION
    147 					if (decrypt_input)
    148 						(void)(*decrypt_input)(-1);
    149 #endif	/* ENCRYPTION */
    150 					state = TS_CR;
    151 				}
    152 			}
    153 			*pfrontp++ = c;
    154 			break;
    155 
    156 		case TS_IAC:
    157 gotiac:			switch (c) {
    158 
    159 			/*
    160 			 * Send the process on the pty side an
    161 			 * interrupt.  Do this with a NULL or
    162 			 * interrupt char; depending on the tty mode.
    163 			 */
    164 			case IP:
    165 				DIAG(TD_OPTIONS,
    166 					printoption("td: recv IAC", c));
    167 				interrupt();
    168 				break;
    169 
    170 			case BREAK:
    171 				DIAG(TD_OPTIONS,
    172 					printoption("td: recv IAC", c));
    173 				sendbrk();
    174 				break;
    175 
    176 			/*
    177 			 * Are You There?
    178 			 */
    179 			case AYT:
    180 				DIAG(TD_OPTIONS,
    181 					printoption("td: recv IAC", c));
    182 				recv_ayt();
    183 				break;
    184 
    185 			/*
    186 			 * Abort Output
    187 			 */
    188 			case AO:
    189 			    {
    190 				DIAG(TD_OPTIONS,
    191 					printoption("td: recv IAC", c));
    192 				ptyflush();	/* half-hearted */
    193 				init_termbuf();
    194 
    195 				if (slctab[SLC_AO].sptr &&
    196 				    *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
    197 				    *pfrontp++ =
    198 					(unsigned char)*slctab[SLC_AO].sptr;
    199 				}
    200 
    201 				netclear();	/* clear buffer back */
    202 				output_data("%c%c", IAC, DM);
    203 				neturg = nfrontp - 1; /* off by one XXX */
    204 				DIAG(TD_OPTIONS,
    205 					printoption("td: send IAC", DM));
    206 				break;
    207 			    }
    208 
    209 			/*
    210 			 * Erase Character and
    211 			 * Erase Line
    212 			 */
    213 			case EC:
    214 			case EL:
    215 			    {
    216 				cc_t ch;
    217 
    218 				DIAG(TD_OPTIONS,
    219 					printoption("td: recv IAC", c));
    220 				ptyflush();	/* half-hearted */
    221 				init_termbuf();
    222 				if (c == EC)
    223 					ch = *slctab[SLC_EC].sptr;
    224 				else
    225 					ch = *slctab[SLC_EL].sptr;
    226 				if (ch != (cc_t)(_POSIX_VDISABLE))
    227 					*pfrontp++ = (unsigned char)ch;
    228 				break;
    229 			    }
    230 
    231 			/*
    232 			 * Check for urgent data...
    233 			 */
    234 			case DM:
    235 				DIAG(TD_OPTIONS,
    236 					printoption("td: recv IAC", c));
    237 				SYNCHing = stilloob(net);
    238 				settimer(gotDM);
    239 				break;
    240 
    241 
    242 			/*
    243 			 * Begin option subnegotiation...
    244 			 */
    245 			case SB:
    246 				state = TS_SB;
    247 				SB_CLEAR();
    248 				continue;
    249 
    250 			case WILL:
    251 				state = TS_WILL;
    252 				continue;
    253 
    254 			case WONT:
    255 				state = TS_WONT;
    256 				continue;
    257 
    258 			case DO:
    259 				state = TS_DO;
    260 				continue;
    261 
    262 			case DONT:
    263 				state = TS_DONT;
    264 				continue;
    265 			case EOR:
    266 				if (his_state_is_will(TELOPT_EOR))
    267 					doeof();
    268 				break;
    269 
    270 			/*
    271 			 * Handle RFC 10xx Telnet linemode option additions
    272 			 * to command stream (EOF, SUSP, ABORT).
    273 			 */
    274 			case xEOF:
    275 				doeof();
    276 				break;
    277 
    278 			case SUSP:
    279 				sendsusp();
    280 				break;
    281 
    282 			case ABORT:
    283 				sendbrk();
    284 				break;
    285 
    286 			case IAC:
    287 				*pfrontp++ = c;
    288 				break;
    289 			}
    290 			state = TS_DATA;
    291 			break;
    292 
    293 		case TS_SB:
    294 			if (c == IAC) {
    295 				state = TS_SE;
    296 			} else {
    297 				SB_ACCUM(c);
    298 			}
    299 			break;
    300 
    301 		case TS_SE:
    302 			if (c != SE) {
    303 				if (c != IAC) {
    304 					/*
    305 					 * bad form of suboption negotiation.
    306 					 * handle it in such a way as to avoid
    307 					 * damage to local state.  Parse
    308 					 * suboption buffer found so far,
    309 					 * then treat remaining stream as
    310 					 * another command sequence.
    311 					 */
    312 
    313 					/* for DIAGNOSTICS */
    314 					SB_ACCUM(IAC);
    315 					SB_ACCUM(c);
    316 					subpointer -= 2;
    317 
    318 					SB_TERM();
    319 					suboption();
    320 					state = TS_IAC;
    321 					goto gotiac;
    322 				}
    323 				SB_ACCUM(c);
    324 				state = TS_SB;
    325 			} else {
    326 				/* for DIAGNOSTICS */
    327 				SB_ACCUM(IAC);
    328 				SB_ACCUM(SE);
    329 				subpointer -= 2;
    330 
    331 				SB_TERM();
    332 				suboption();	/* handle sub-option */
    333 				state = TS_DATA;
    334 			}
    335 			break;
    336 
    337 		case TS_WILL:
    338 			willoption(c);
    339 			state = TS_DATA;
    340 			continue;
    341 
    342 		case TS_WONT:
    343 			wontoption(c);
    344 			state = TS_DATA;
    345 			continue;
    346 
    347 		case TS_DO:
    348 			dooption(c);
    349 			state = TS_DATA;
    350 			continue;
    351 
    352 		case TS_DONT:
    353 			dontoption(c);
    354 			state = TS_DATA;
    355 			continue;
    356 
    357 		default:
    358 			syslog(LOG_ERR, "panic state=%d", state);
    359 			printf("telnetd: panic state=%d\n", state);
    360 			exit(1);
    361 		}
    362 	}
    363 }  /* end of telrcv */
    364 
    365 /*
    366  * The will/wont/do/dont state machines are based on Dave Borman's
    367  * Telnet option processing state machine.
    368  *
    369  * These correspond to the following states:
    370  *	my_state = the last negotiated state
    371  *	want_state = what I want the state to go to
    372  *	want_resp = how many requests I have sent
    373  * All state defaults are negative, and resp defaults to 0.
    374  *
    375  * When initiating a request to change state to new_state:
    376  *
    377  * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
    378  *	do nothing;
    379  * } else {
    380  *	want_state = new_state;
    381  *	send new_state;
    382  *	want_resp++;
    383  * }
    384  *
    385  * When receiving new_state:
    386  *
    387  * if (want_resp) {
    388  *	want_resp--;
    389  *	if (want_resp && (new_state == my_state))
    390  *		want_resp--;
    391  * }
    392  * if ((want_resp == 0) && (new_state != want_state)) {
    393  *	if (ok_to_switch_to new_state)
    394  *		want_state = new_state;
    395  *	else
    396  *		want_resp++;
    397  *	send want_state;
    398  * }
    399  * my_state = new_state;
    400  *
    401  * Note that new_state is implied in these functions by the function itself.
    402  * will and do imply positive new_state, wont and dont imply negative.
    403  *
    404  * Finally, there is one catch.  If we send a negative response to a
    405  * positive request, my_state will be the positive while want_state will
    406  * remain negative.  my_state will revert to negative when the negative
    407  * acknowlegment arrives from the peer.  Thus, my_state generally tells
    408  * us not only the last negotiated state, but also tells us what the peer
    409  * wants to be doing as well.  It is important to understand this difference
    410  * as we may wish to be processing data streams based on our desired state
    411  * (want_state) or based on what the peer thinks the state is (my_state).
    412  *
    413  * This all works fine because if the peer sends a positive request, the data
    414  * that we receive prior to negative acknowlegment will probably be affected
    415  * by the positive state, and we can process it as such (if we can; if we
    416  * can't then it really doesn't matter).  If it is that important, then the
    417  * peer probably should be buffering until this option state negotiation
    418  * is complete.
    419  *
    420  */
    421 void
    422 send_do(int option, int init)
    423 {
    424 	if (init) {
    425 		if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
    426 		    his_want_state_is_will(option))
    427 			return;
    428 		/*
    429 		 * Special case for TELOPT_TM:  We send a DO, but pretend
    430 		 * that we sent a DONT, so that we can send more DOs if
    431 		 * we want to.
    432 		 */
    433 		if (option == TELOPT_TM)
    434 			set_his_want_state_wont(option);
    435 		else
    436 			set_his_want_state_will(option);
    437 		do_dont_resp[option]++;
    438 	}
    439 	(void) output_data((const char *)doopt, option);
    440 
    441 	DIAG(TD_OPTIONS, printoption("td: send do", option));
    442 }
    443 
    444 #ifdef	LINEMODE
    445 extern void doclientstat(void);
    446 #endif
    447 #if 0
    448 #ifdef	AUTHENTICATION
    449 extern void auth_request(void);	/* libtelnet */
    450 #endif
    451 #ifdef	ENCRYPTION
    452 extern void encrypt_send_support(void);
    453 #endif	/* ENCRYPTION */
    454 #endif
    455 
    456 void
    457 willoption(int option)
    458 {
    459 	int changeok = 0;
    460 	void (*func)(void) = 0;
    461 
    462 	/*
    463 	 * process input from peer.
    464 	 */
    465 
    466 	DIAG(TD_OPTIONS, printoption("td: recv will", option));
    467 
    468 	if (do_dont_resp[option]) {
    469 		do_dont_resp[option]--;
    470 		if (do_dont_resp[option] && his_state_is_will(option))
    471 			do_dont_resp[option]--;
    472 	}
    473 	if (do_dont_resp[option] == 0) {
    474 	    if (his_want_state_is_wont(option)) {
    475 		switch (option) {
    476 
    477 		case TELOPT_BINARY:
    478 			init_termbuf();
    479 			tty_binaryin(1);
    480 			set_termbuf();
    481 			changeok++;
    482 			break;
    483 
    484 		case TELOPT_ECHO:
    485 			/*
    486 			 * See comments below for more info.
    487 			 */
    488 			not42 = 0;	/* looks like a 4.2 system */
    489 			break;
    490 
    491 		case TELOPT_TM:
    492 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    493 			/*
    494 			 * This telnetd implementation does not really
    495 			 * support timing marks, it just uses them to
    496 			 * support the kludge linemode stuff.  If we
    497 			 * receive a will or wont TM in response to our
    498 			 * do TM request that may have been sent to
    499 			 * determine kludge linemode support, process
    500 			 * it, otherwise TM should get a negative
    501 			 * response back.
    502 			 */
    503 			/*
    504 			 * Handle the linemode kludge stuff.
    505 			 * If we are not currently supporting any
    506 			 * linemode at all, then we assume that this
    507 			 * is the client telling us to use kludge
    508 			 * linemode in response to our query.  Set the
    509 			 * linemode type that is to be supported, note
    510 			 * that the client wishes to use linemode, and
    511 			 * eat the will TM as though it never arrived.
    512 			 */
    513 			if (lmodetype < KLUDGE_LINEMODE) {
    514 				lmodetype = KLUDGE_LINEMODE;
    515 				clientstat(TELOPT_LINEMODE, WILL, 0);
    516 				send_wont(TELOPT_SGA, 1);
    517 			} else if (lmodetype == NO_AUTOKLUDGE) {
    518 				lmodetype = KLUDGE_OK;
    519 			}
    520 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    521 			/*
    522 			 * We never respond to a WILL TM, and
    523 			 * we leave the state WONT.
    524 			 */
    525 			return;
    526 
    527 		case TELOPT_LFLOW:
    528 			/*
    529 			 * If we are going to support flow control
    530 			 * option, then don't worry peer that we can't
    531 			 * change the flow control characters.
    532 			 */
    533 			slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
    534 			slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
    535 			slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
    536 			slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
    537 		case TELOPT_TTYPE:
    538 		case TELOPT_SGA:
    539 		case TELOPT_NAWS:
    540 		case TELOPT_TSPEED:
    541 		case TELOPT_XDISPLOC:
    542 		case TELOPT_NEW_ENVIRON:
    543 		case TELOPT_OLD_ENVIRON:
    544 			changeok++;
    545 			break;
    546 
    547 #ifdef	LINEMODE
    548 		case TELOPT_LINEMODE:
    549 # ifdef	KLUDGELINEMODE
    550 			/*
    551 			 * Note client's desire to use linemode.
    552 			 */
    553 			lmodetype = REAL_LINEMODE;
    554 # endif	/* KLUDGELINEMODE */
    555 			func = doclientstat;
    556 			changeok++;
    557 			break;
    558 #endif	/* LINEMODE */
    559 
    560 #ifdef	AUTHENTICATION
    561 		case TELOPT_AUTHENTICATION:
    562 			func = auth_request;
    563 			changeok++;
    564 			break;
    565 #endif
    566 
    567 #ifdef	ENCRYPTION
    568 		case TELOPT_ENCRYPT:
    569 			func = encrypt_send_support;
    570 			changeok++;
    571 			break;
    572 #endif	/* ENCRYPTION */
    573 
    574 		default:
    575 			break;
    576 		}
    577 		if (changeok) {
    578 			set_his_want_state_will(option);
    579 			send_do(option, 0);
    580 		} else {
    581 			do_dont_resp[option]++;
    582 			send_dont(option, 0);
    583 		}
    584 	    } else {
    585 		/*
    586 		 * Option processing that should happen when
    587 		 * we receive conformation of a change in
    588 		 * state that we had requested.
    589 		 */
    590 		switch (option) {
    591 		case TELOPT_ECHO:
    592 			not42 = 0;	/* looks like a 4.2 system */
    593 			/*
    594 			 * Egads, he responded "WILL ECHO".  Turn
    595 			 * it off right now!
    596 			 */
    597 			send_dont(option, 1);
    598 			/*
    599 			 * "WILL ECHO".  Kludge upon kludge!
    600 			 * A 4.2 client is now echoing user input at
    601 			 * the tty.  This is probably undesireable and
    602 			 * it should be stopped.  The client will
    603 			 * respond WONT TM to the DO TM that we send to
    604 			 * check for kludge linemode.  When the WONT TM
    605 			 * arrives, linemode will be turned off and a
    606 			 * change propogated to the pty.  This change
    607 			 * will cause us to process the new pty state
    608 			 * in localstat(), which will notice that
    609 			 * linemode is off and send a WILL ECHO
    610 			 * so that we are properly in character mode and
    611 			 * all is well.
    612 			 */
    613 			break;
    614 #ifdef	LINEMODE
    615 		case TELOPT_LINEMODE:
    616 # ifdef	KLUDGELINEMODE
    617 			/*
    618 			 * Note client's desire to use linemode.
    619 			 */
    620 			lmodetype = REAL_LINEMODE;
    621 # endif	/* KLUDGELINEMODE */
    622 			func = doclientstat;
    623 			break;
    624 #endif	/* LINEMODE */
    625 
    626 #ifdef	AUTHENTICATION
    627 		case TELOPT_AUTHENTICATION:
    628 			func = auth_request;
    629 			break;
    630 #endif
    631 
    632 #ifdef	ENCRYPTION
    633 		case TELOPT_ENCRYPT:
    634 			func = encrypt_send_support;
    635 			break;
    636 #endif	/* ENCRYPTION */
    637 
    638 		case TELOPT_LFLOW:
    639 			func = flowstat;
    640 			break;
    641 		}
    642 	    }
    643 	}
    644 	set_his_state_will(option);
    645 	if (func)
    646 		(*func)();
    647 }  /* end of willoption */
    648 
    649 void
    650 send_dont(int option, int init)
    651 {
    652 	if (init) {
    653 		if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
    654 		    his_want_state_is_wont(option))
    655 			return;
    656 		set_his_want_state_wont(option);
    657 		do_dont_resp[option]++;
    658 	}
    659 	(void) output_data((const char *)dont, option);
    660 
    661 	DIAG(TD_OPTIONS, printoption("td: send dont", option));
    662 }
    663 
    664 void
    665 wontoption(int option)
    666 {
    667 	/*
    668 	 * Process client input.
    669 	 */
    670 
    671 	DIAG(TD_OPTIONS, printoption("td: recv wont", option));
    672 
    673 	if (do_dont_resp[option]) {
    674 		do_dont_resp[option]--;
    675 		if (do_dont_resp[option] && his_state_is_wont(option))
    676 			do_dont_resp[option]--;
    677 	}
    678 	if (do_dont_resp[option] == 0) {
    679 	    if (his_want_state_is_will(option)) {
    680 		/* it is always ok to change to negative state */
    681 		switch (option) {
    682 		case TELOPT_ECHO:
    683 			not42 = 1; /* doesn't seem to be a 4.2 system */
    684 			break;
    685 
    686 		case TELOPT_BINARY:
    687 			init_termbuf();
    688 			tty_binaryin(0);
    689 			set_termbuf();
    690 			break;
    691 
    692 #ifdef	LINEMODE
    693 		case TELOPT_LINEMODE:
    694 # ifdef	KLUDGELINEMODE
    695 			/*
    696 			 * If real linemode is supported, then client is
    697 			 * asking to turn linemode off.
    698 			 */
    699 			if (lmodetype != REAL_LINEMODE)
    700 				break;
    701 			/* XXX double-check this --thorpej */
    702 			lmodetype = KLUDGE_LINEMODE;
    703 # endif	/* KLUDGELINEMODE */
    704 			clientstat(TELOPT_LINEMODE, WONT, 0);
    705 			break;
    706 #endif	/* LINEMODE */
    707 
    708 		case TELOPT_TM:
    709 			/*
    710 			 * If we get a WONT TM, and had sent a DO TM,
    711 			 * don't respond with a DONT TM, just leave it
    712 			 * as is.  Short circut the state machine to
    713 			 * achieve this.
    714 			 */
    715 			set_his_want_state_wont(TELOPT_TM);
    716 			return;
    717 
    718 		case TELOPT_LFLOW:
    719 			/*
    720 			 * If we are not going to support flow control
    721 			 * option, then let peer know that we can't
    722 			 * change the flow control characters.
    723 			 */
    724 			slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
    725 			slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
    726 			slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
    727 			slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
    728 			break;
    729 
    730 #ifdef AUTHENTICATION
    731 		case TELOPT_AUTHENTICATION:
    732 			auth_finished(0, AUTH_REJECT);
    733 			break;
    734 #endif
    735 
    736 		/*
    737 		 * For options that we might spin waiting for
    738 		 * sub-negotiation, if the client turns off the
    739 		 * option rather than responding to the request,
    740 		 * we have to treat it here as if we got a response
    741 		 * to the sub-negotiation, (by updating the timers)
    742 		 * so that we'll break out of the loop.
    743 		 */
    744 		case TELOPT_TTYPE:
    745 			settimer(ttypesubopt);
    746 			break;
    747 
    748 		case TELOPT_TSPEED:
    749 			settimer(tspeedsubopt);
    750 			break;
    751 
    752 		case TELOPT_XDISPLOC:
    753 			settimer(xdisplocsubopt);
    754 			break;
    755 
    756 		case TELOPT_OLD_ENVIRON:
    757 			settimer(oenvironsubopt);
    758 			break;
    759 
    760 		case TELOPT_NEW_ENVIRON:
    761 			settimer(environsubopt);
    762 			break;
    763 
    764 		default:
    765 			break;
    766 		}
    767 		set_his_want_state_wont(option);
    768 		if (his_state_is_will(option))
    769 			send_dont(option, 0);
    770 	    } else {
    771 		switch (option) {
    772 		case TELOPT_TM:
    773 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    774 			if (lmodetype < NO_AUTOKLUDGE) {
    775 				lmodetype = NO_LINEMODE;
    776 				clientstat(TELOPT_LINEMODE, WONT, 0);
    777 				send_will(TELOPT_SGA, 1);
    778 				send_will(TELOPT_ECHO, 1);
    779 			}
    780 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    781 			break;
    782 
    783 #ifdef AUTHENTICATION
    784 		case TELOPT_AUTHENTICATION:
    785 			auth_finished(0, AUTH_REJECT);
    786 			break;
    787 #endif
    788 		default:
    789 			break;
    790 		}
    791 	    }
    792 	}
    793 	set_his_state_wont(option);
    794 
    795 }  /* end of wontoption */
    796 
    797 void
    798 send_will(int option, int init)
    799 {
    800 	if (init) {
    801 		if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
    802 		    my_want_state_is_will(option))
    803 			return;
    804 		set_my_want_state_will(option);
    805 		will_wont_resp[option]++;
    806 	}
    807 	(void) output_data((const char *)will, option);
    808 
    809 	DIAG(TD_OPTIONS, printoption("td: send will", option));
    810 }
    811 
    812 #if	!defined(LINEMODE) || !defined(KLUDGELINEMODE)
    813 /*
    814  * When we get a DONT SGA, we will try once to turn it
    815  * back on.  If the other side responds DONT SGA, we
    816  * leave it at that.  This is so that when we talk to
    817  * clients that understand KLUDGELINEMODE but not LINEMODE,
    818  * we'll keep them in char-at-a-time mode.
    819  */
    820 int turn_on_sga = 0;
    821 #endif
    822 
    823 void
    824 dooption(int option)
    825 {
    826 	int changeok = 0;
    827 
    828 	/*
    829 	 * Process client input.
    830 	 */
    831 
    832 	DIAG(TD_OPTIONS, printoption("td: recv do", option));
    833 
    834 	if (will_wont_resp[option]) {
    835 		will_wont_resp[option]--;
    836 		if (will_wont_resp[option] && my_state_is_will(option))
    837 			will_wont_resp[option]--;
    838 	}
    839 	if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
    840 		switch (option) {
    841 		case TELOPT_ECHO:
    842 #ifdef	LINEMODE
    843 # ifdef	KLUDGELINEMODE
    844 			if (lmodetype == NO_LINEMODE)
    845 # else
    846 			if (his_state_is_wont(TELOPT_LINEMODE))
    847 # endif
    848 #endif
    849 			{
    850 				init_termbuf();
    851 				tty_setecho(1);
    852 				set_termbuf();
    853 			}
    854 			changeok++;
    855 			break;
    856 
    857 		case TELOPT_BINARY:
    858 			init_termbuf();
    859 			tty_binaryout(1);
    860 			set_termbuf();
    861 			changeok++;
    862 			break;
    863 
    864 		case TELOPT_SGA:
    865 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    866 			/*
    867 			 * If kludge linemode is in use, then we must
    868 			 * process an incoming do SGA for linemode
    869 			 * purposes.
    870 			 */
    871 			if (lmodetype == KLUDGE_LINEMODE) {
    872 				/*
    873 				 * Receipt of "do SGA" in kludge
    874 				 * linemode is the peer asking us to
    875 				 * turn off linemode.  Make note of
    876 				 * the request.
    877 				 */
    878 				clientstat(TELOPT_LINEMODE, WONT, 0);
    879 				/*
    880 				 * If linemode did not get turned off
    881 				 * then don't tell peer that we did.
    882 				 * Breaking here forces a wont SGA to
    883 				 * be returned.
    884 				 */
    885 				if (linemode)
    886 					break;
    887 			}
    888 #else
    889 			turn_on_sga = 0;
    890 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    891 			changeok++;
    892 			break;
    893 
    894 		case TELOPT_STATUS:
    895 			changeok++;
    896 			break;
    897 
    898 		case TELOPT_TM:
    899 			/*
    900 			 * Special case for TM.  We send a WILL, but
    901 			 * pretend we sent a WONT.
    902 			 */
    903 			send_will(option, 0);
    904 			set_my_want_state_wont(option);
    905 			set_my_state_wont(option);
    906 			return;
    907 
    908 		case TELOPT_LOGOUT:
    909 			/*
    910 			 * When we get a LOGOUT option, respond
    911 			 * with a WILL LOGOUT, make sure that
    912 			 * it gets written out to the network,
    913 			 * and then just go away...
    914 			 */
    915 			set_my_want_state_will(TELOPT_LOGOUT);
    916 			send_will(TELOPT_LOGOUT, 0);
    917 			set_my_state_will(TELOPT_LOGOUT);
    918 			(void)netflush();
    919 			cleanup(0);
    920 			/* NOT REACHED */
    921 			break;
    922 
    923 #ifdef	ENCRYPTION
    924 		case TELOPT_ENCRYPT:
    925 			changeok++;
    926 			break;
    927 #endif	/* ENCRYPTION */
    928 
    929 		case TELOPT_LINEMODE:
    930 		case TELOPT_TTYPE:
    931 		case TELOPT_NAWS:
    932 		case TELOPT_TSPEED:
    933 		case TELOPT_LFLOW:
    934 		case TELOPT_XDISPLOC:
    935 		case TELOPT_OLD_ENVIRON:
    936 		default:
    937 			break;
    938 		}
    939 		if (changeok) {
    940 			set_my_want_state_will(option);
    941 			send_will(option, 0);
    942 		} else {
    943 			will_wont_resp[option]++;
    944 			send_wont(option, 0);
    945 		}
    946 	}
    947 	set_my_state_will(option);
    948 
    949 }  /* end of dooption */
    950 
    951 void
    952 send_wont(int option, int init)
    953 {
    954 	if (init) {
    955 		if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
    956 		    my_want_state_is_wont(option))
    957 			return;
    958 		set_my_want_state_wont(option);
    959 		will_wont_resp[option]++;
    960 	}
    961 	(void) output_data((const char *)wont, option);
    962 
    963 	DIAG(TD_OPTIONS, printoption("td: send wont", option));
    964 }
    965 
    966 void
    967 dontoption(int option)
    968 {
    969 	/*
    970 	 * Process client input.
    971 	 */
    972 
    973 
    974 	DIAG(TD_OPTIONS, printoption("td: recv dont", option));
    975 
    976 	if (will_wont_resp[option]) {
    977 		will_wont_resp[option]--;
    978 		if (will_wont_resp[option] && my_state_is_wont(option))
    979 			will_wont_resp[option]--;
    980 	}
    981 	if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
    982 		switch (option) {
    983 		case TELOPT_BINARY:
    984 			init_termbuf();
    985 			tty_binaryout(0);
    986 			set_termbuf();
    987 			break;
    988 
    989 		case TELOPT_ECHO:	/* we should stop echoing */
    990 #ifdef	LINEMODE
    991 # ifdef	KLUDGELINEMODE
    992 			if ((lmodetype != REAL_LINEMODE) &&
    993 			    (lmodetype != KLUDGE_LINEMODE))
    994 # else
    995 			if (his_state_is_wont(TELOPT_LINEMODE))
    996 # endif
    997 #endif
    998 			{
    999 				init_termbuf();
   1000 				tty_setecho(0);
   1001 				set_termbuf();
   1002 			}
   1003 			break;
   1004 
   1005 		case TELOPT_SGA:
   1006 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
   1007 			/*
   1008 			 * If kludge linemode is in use, then we
   1009 			 * must process an incoming do SGA for
   1010 			 * linemode purposes.
   1011 			 */
   1012 			if ((lmodetype == KLUDGE_LINEMODE) ||
   1013 			    (lmodetype == KLUDGE_OK)) {
   1014 				/*
   1015 				 * The client is asking us to turn
   1016 				 * linemode on.
   1017 				 */
   1018 				lmodetype = KLUDGE_LINEMODE;
   1019 				clientstat(TELOPT_LINEMODE, WILL, 0);
   1020 				/*
   1021 				 * If we did not turn line mode on,
   1022 				 * then what do we say?  Will SGA?
   1023 				 * This violates design of telnet.
   1024 				 * Gross.  Very Gross.
   1025 				 */
   1026 			}
   1027 			break;
   1028 #else
   1029 			set_my_want_state_wont(option);
   1030 			if (my_state_is_will(option))
   1031 				send_wont(option, 0);
   1032 			set_my_state_wont(option);
   1033 			if (turn_on_sga ^= 1)
   1034 				send_will(option, 1);
   1035 			return;
   1036 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
   1037 
   1038 		default:
   1039 			break;
   1040 		}
   1041 
   1042 		set_my_want_state_wont(option);
   1043 		if (my_state_is_will(option))
   1044 			send_wont(option, 0);
   1045 	}
   1046 	set_my_state_wont(option);
   1047 
   1048 }  /* end of dontoption */
   1049 
   1050 #ifdef	ENV_HACK
   1051 int env_ovar = -1;
   1052 int env_ovalue = -1;
   1053 #else	/* ENV_HACK */
   1054 # define env_ovar OLD_ENV_VAR
   1055 # define env_ovalue OLD_ENV_VALUE
   1056 #endif	/* ENV_HACK */
   1057 
   1058 /* envvarok(char*) */
   1059 /* check that variable is safe to pass to login or shell */
   1060 static int
   1061 envvarok(char *varp)
   1062 {
   1063 
   1064 	if (strcmp(varp, "TERMCAP") &&	/* to prevent a security hole */
   1065 	    strcmp(varp, "TERMINFO") &&	/* with tgetent */
   1066 	    strcmp(varp, "TERMPATH") &&
   1067 	    strcmp(varp, "HOME") &&	/* to prevent the tegetent bug  */
   1068 	    strncmp(varp, "LD_", strlen("LD_")) &&	/* most systems */
   1069 	    strncmp(varp, "_RLD_", strlen("_RLD_")) &&	/* IRIX */
   1070 	    strcmp(varp, "LIBPATH") &&			/* AIX */
   1071 	    strcmp(varp, "ENV") &&
   1072 	    strcmp(varp, "BASH_ENV") &&
   1073 	    strcmp(varp, "IFS") &&
   1074 	    strncmp(varp, "KRB5", strlen("KRB5")) &&	/* Krb5 */
   1075 	    /*
   1076 	     * The above case is a catch-all for now.  Here are some of
   1077 	     * the specific ones we must avoid passing, at least until
   1078 	     * we can prove it can be done safely.  Keep this list
   1079 	     * around un case someone wants to remove the catch-all.
   1080 	     */
   1081 	    strcmp(varp, "KRB5_CONFIG") &&		/* Krb5 */
   1082 	    strcmp(varp, "KRB5CCNAME") &&		/* Krb5 */
   1083 	    strcmp(varp, "KRB5_KTNAME") &&		/* Krb5 */
   1084 	    strcmp(varp, "KRBTKFILE") &&		/* Krb4 */
   1085 	    strcmp(varp, "KRB_CONF") &&			/* CNS 4 */
   1086 	    strcmp(varp, "KRB_REALMS") &&		/* CNS 4 */
   1087 	    strcmp(varp, "RESOLV_HOST_CONF"))		/* Linux */
   1088 		return (1);
   1089 	else {
   1090 		syslog(LOG_INFO, "Rejected the attempt to modify the "
   1091 		    "environment variable \"%s\"", varp);
   1092 		return (0);
   1093 	}
   1094 }
   1095 
   1096 /*
   1097  * suboption()
   1098  *
   1099  *	Look at the sub-option buffer, and try to be helpful to the other
   1100  * side.
   1101  *
   1102  *	Currently we recognize:
   1103  *
   1104  *	Terminal type is
   1105  *	Linemode
   1106  *	Window size
   1107  *	Terminal speed
   1108  */
   1109 void
   1110 suboption(void)
   1111 {
   1112     int subchar;
   1113 
   1114     DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
   1115 
   1116     subchar = SB_GET();
   1117     switch (subchar) {
   1118     case TELOPT_TSPEED: {
   1119 	int xspeed, rspeed;
   1120 
   1121 	if (his_state_is_wont(TELOPT_TSPEED))	/* Ignore if option disabled */
   1122 		break;
   1123 
   1124 	settimer(tspeedsubopt);
   1125 
   1126 	if (SB_EOF() || SB_GET() != TELQUAL_IS)
   1127 		return;
   1128 
   1129 	xspeed = atoi((char *)subpointer);
   1130 
   1131 	while (SB_GET() != ',' && !SB_EOF());
   1132 	if (SB_EOF())
   1133 		return;
   1134 
   1135 	rspeed = atoi((char *)subpointer);
   1136 	clientstat(TELOPT_TSPEED, xspeed, rspeed);
   1137 
   1138 	break;
   1139 
   1140     }  /* end of case TELOPT_TSPEED */
   1141 
   1142     case TELOPT_TTYPE: {		/* Yaaaay! */
   1143 	static char terminalname[41];
   1144 
   1145 	if (his_state_is_wont(TELOPT_TTYPE))	/* Ignore if option disabled */
   1146 		break;
   1147 	settimer(ttypesubopt);
   1148 
   1149 	if (SB_EOF() || SB_GET() != TELQUAL_IS) {
   1150 	    return;		/* ??? XXX but, this is the most robust */
   1151 	}
   1152 
   1153 	terminaltype = terminalname;
   1154 
   1155 	while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
   1156 								    !SB_EOF()) {
   1157 	    int c;
   1158 
   1159 	    c = SB_GET();
   1160 	    if (isupper(c)) {
   1161 		c = tolower(c);
   1162 	    }
   1163 	    *terminaltype++ = c;    /* accumulate name */
   1164 	}
   1165 	*terminaltype = 0;
   1166 	terminaltype = terminalname;
   1167 	break;
   1168     }  /* end of case TELOPT_TTYPE */
   1169 
   1170     case TELOPT_NAWS: {
   1171 	int xwinsize, ywinsize;
   1172 
   1173 	if (his_state_is_wont(TELOPT_NAWS))	/* Ignore if option disabled */
   1174 		break;
   1175 
   1176 	if (SB_EOF())
   1177 		return;
   1178 	xwinsize = SB_GET() << 8;
   1179 	if (SB_EOF())
   1180 		return;
   1181 	xwinsize |= SB_GET();
   1182 	if (SB_EOF())
   1183 		return;
   1184 	ywinsize = SB_GET() << 8;
   1185 	if (SB_EOF())
   1186 		return;
   1187 	ywinsize |= SB_GET();
   1188 	clientstat(TELOPT_NAWS, xwinsize, ywinsize);
   1189 
   1190 	break;
   1191 
   1192     }  /* end of case TELOPT_NAWS */
   1193 
   1194 #ifdef	LINEMODE
   1195     case TELOPT_LINEMODE: {
   1196 	int request;
   1197 
   1198 	if (his_state_is_wont(TELOPT_LINEMODE))	/* Ignore if option disabled */
   1199 		break;
   1200 	/*
   1201 	 * Process linemode suboptions.
   1202 	 */
   1203 	if (SB_EOF())
   1204 	    break;		/* garbage was sent */
   1205 	request = SB_GET();	/* get will/wont */
   1206 
   1207 	if (SB_EOF())
   1208 	    break;		/* another garbage check */
   1209 
   1210 	if (request == LM_SLC) {  /* SLC is not preceded by WILL or WONT */
   1211 		/*
   1212 		 * Process suboption buffer of slc's
   1213 		 */
   1214 		start_slc(1);
   1215 		do_opt_slc(subpointer, subend - subpointer);
   1216 		(void) end_slc(0);
   1217 		break;
   1218 	} else if (request == LM_MODE) {
   1219 		if (SB_EOF())
   1220 		    return;
   1221 		useeditmode = SB_GET();  /* get mode flag */
   1222 		clientstat(LM_MODE, 0, 0);
   1223 		break;
   1224 	}
   1225 
   1226 	if (SB_EOF())
   1227 	    break;
   1228 	switch (SB_GET()) {  /* what suboption? */
   1229 	case LM_FORWARDMASK:
   1230 		/*
   1231 		 * According to spec, only server can send request for
   1232 		 * forwardmask, and client can only return a positive response.
   1233 		 * So don't worry about it.
   1234 		 */
   1235 
   1236 	default:
   1237 		break;
   1238 	}
   1239 	break;
   1240     }  /* end of case TELOPT_LINEMODE */
   1241 #endif
   1242     case TELOPT_STATUS: {
   1243 	int mode;
   1244 
   1245 	if (SB_EOF())
   1246 	    break;
   1247 	mode = SB_GET();
   1248 	switch (mode) {
   1249 	case TELQUAL_SEND:
   1250 	    if (my_state_is_will(TELOPT_STATUS))
   1251 		send_status();
   1252 	    break;
   1253 
   1254 	case TELQUAL_IS:
   1255 	    break;
   1256 
   1257 	default:
   1258 	    break;
   1259 	}
   1260 	break;
   1261     }  /* end of case TELOPT_STATUS */
   1262 
   1263     case TELOPT_XDISPLOC: {
   1264 	if (SB_EOF() || SB_GET() != TELQUAL_IS)
   1265 		return;
   1266 	settimer(xdisplocsubopt);
   1267 	subpointer[SB_LEN()] = '\0';
   1268 	(void)setenv("DISPLAY", (char *)subpointer, 1);
   1269 	break;
   1270     }  /* end of case TELOPT_XDISPLOC */
   1271 
   1272     case TELOPT_NEW_ENVIRON:
   1273     case TELOPT_OLD_ENVIRON: {
   1274 	int c;
   1275 	char *cp, *varp, *valp;
   1276 
   1277 	if (SB_EOF())
   1278 		return;
   1279 	c = SB_GET();
   1280 	if (c == TELQUAL_IS) {
   1281 		if (subchar == TELOPT_OLD_ENVIRON)
   1282 			settimer(oenvironsubopt);
   1283 		else
   1284 			settimer(environsubopt);
   1285 	} else if (c != TELQUAL_INFO) {
   1286 		return;
   1287 	}
   1288 
   1289 	if (subchar == TELOPT_NEW_ENVIRON) {
   1290 	    while (!SB_EOF()) {
   1291 		c = SB_GET();
   1292 		if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
   1293 			break;
   1294 	    }
   1295 	} else
   1296 	{
   1297 #ifdef	ENV_HACK
   1298 	    /*
   1299 	     * We only want to do this if we haven't already decided
   1300 	     * whether or not the other side has its VALUE and VAR
   1301 	     * reversed.
   1302 	     */
   1303 	    if (env_ovar < 0) {
   1304 		int last = -1;		/* invalid value */
   1305 		int empty = 0;
   1306 		int got_var = 0, got_value = 0, got_uservar = 0;
   1307 
   1308 		/*
   1309 		 * The other side might have its VALUE and VAR values
   1310 		 * reversed.  To be interoperable, we need to determine
   1311 		 * which way it is.  If the first recognized character
   1312 		 * is a VAR or VALUE, then that will tell us what
   1313 		 * type of client it is.  If the fist recognized
   1314 		 * character is a USERVAR, then we continue scanning
   1315 		 * the suboption looking for two consecutive
   1316 		 * VAR or VALUE fields.  We should not get two
   1317 		 * consecutive VALUE fields, so finding two
   1318 		 * consecutive VALUE or VAR fields will tell us
   1319 		 * what the client is.
   1320 		 */
   1321 		SB_SAVE();
   1322 		while (!SB_EOF()) {
   1323 			c = SB_GET();
   1324 			switch(c) {
   1325 			case OLD_ENV_VAR:
   1326 				if (last < 0 || last == OLD_ENV_VAR
   1327 				    || (empty && (last == OLD_ENV_VALUE)))
   1328 					goto env_ovar_ok;
   1329 				got_var++;
   1330 				last = OLD_ENV_VAR;
   1331 				break;
   1332 			case OLD_ENV_VALUE:
   1333 				if (last < 0 || last == OLD_ENV_VALUE
   1334 				    || (empty && (last == OLD_ENV_VAR)))
   1335 					goto env_ovar_wrong;
   1336 				got_value++;
   1337 				last = OLD_ENV_VALUE;
   1338 				break;
   1339 			case ENV_USERVAR:
   1340 				/* count strings of USERVAR as one */
   1341 				if (last != ENV_USERVAR)
   1342 					got_uservar++;
   1343 				if (empty) {
   1344 					if (last == OLD_ENV_VALUE)
   1345 						goto env_ovar_ok;
   1346 					if (last == OLD_ENV_VAR)
   1347 						goto env_ovar_wrong;
   1348 				}
   1349 				last = ENV_USERVAR;
   1350 				break;
   1351 			case ENV_ESC:
   1352 				if (!SB_EOF())
   1353 					c = SB_GET();
   1354 				/* FALL THROUGH */
   1355 			default:
   1356 				empty = 0;
   1357 				continue;
   1358 			}
   1359 			empty = 1;
   1360 		}
   1361 		if (empty) {
   1362 			if (last == OLD_ENV_VALUE)
   1363 				goto env_ovar_ok;
   1364 			if (last == OLD_ENV_VAR)
   1365 				goto env_ovar_wrong;
   1366 		}
   1367 		/*
   1368 		 * Ok, the first thing was a USERVAR, and there
   1369 		 * are not two consecutive VAR or VALUE commands,
   1370 		 * and none of the VAR or VALUE commands are empty.
   1371 		 * If the client has sent us a well-formed option,
   1372 		 * then the number of VALUEs received should always
   1373 		 * be less than or equal to the number of VARs and
   1374 		 * USERVARs received.
   1375 		 *
   1376 		 * If we got exactly as many VALUEs as VARs and
   1377 		 * USERVARs, the client has the same definitions.
   1378 		 *
   1379 		 * If we got exactly as many VARs as VALUEs and
   1380 		 * USERVARS, the client has reversed definitions.
   1381 		 */
   1382 		if (got_uservar + got_var == got_value) {
   1383 	    env_ovar_ok:
   1384 			env_ovar = OLD_ENV_VAR;
   1385 			env_ovalue = OLD_ENV_VALUE;
   1386 		} else if (got_uservar + got_value == got_var) {
   1387 	    env_ovar_wrong:
   1388 			env_ovar = OLD_ENV_VALUE;
   1389 			env_ovalue = OLD_ENV_VAR;
   1390 			DIAG(TD_OPTIONS, {output_data(
   1391 				"ENVIRON VALUE and VAR are reversed!\r\n");});
   1392 
   1393 		}
   1394 	    }
   1395 	    SB_RESTORE();
   1396 #endif
   1397 
   1398 	    while (!SB_EOF()) {
   1399 		c = SB_GET();
   1400 		if ((c == env_ovar) || (c == ENV_USERVAR))
   1401 			break;
   1402 	    }
   1403 	}
   1404 
   1405 	if (SB_EOF())
   1406 		return;
   1407 
   1408 	cp = varp = (char *)subpointer;
   1409 	valp = 0;
   1410 
   1411 	while (!SB_EOF()) {
   1412 		c = SB_GET();
   1413 		if (subchar == TELOPT_OLD_ENVIRON) {
   1414 			if (c == env_ovar)
   1415 				c = NEW_ENV_VAR;
   1416 			else if (c == env_ovalue)
   1417 				c = NEW_ENV_VALUE;
   1418 		}
   1419 		switch (c) {
   1420 
   1421 		case NEW_ENV_VALUE:
   1422 			*cp = '\0';
   1423 			cp = valp = (char *)subpointer;
   1424 			break;
   1425 
   1426 		case NEW_ENV_VAR:
   1427 		case ENV_USERVAR:
   1428 			*cp = '\0';
   1429 			if (envvarok(varp)) {
   1430 				if (valp)
   1431 					(void)setenv(varp, valp, 1);
   1432 				else
   1433 					unsetenv(varp);
   1434 			}
   1435 			cp = varp = (char *)subpointer;
   1436 			valp = 0;
   1437 			break;
   1438 
   1439 		case ENV_ESC:
   1440 			if (SB_EOF())
   1441 				break;
   1442 			c = SB_GET();
   1443 			/* FALL THROUGH */
   1444 		default:
   1445 			*cp++ = c;
   1446 			break;
   1447 		}
   1448 	}
   1449 	*cp = '\0';
   1450 	if (envvarok(varp)) {
   1451 		if (valp)
   1452 			(void)setenv(varp, valp, 1);
   1453 		else
   1454 			unsetenv(varp);
   1455 	}
   1456 	break;
   1457     }  /* end of case TELOPT_NEW_ENVIRON */
   1458 #ifdef AUTHENTICATION
   1459     case TELOPT_AUTHENTICATION:
   1460 	if (SB_EOF())
   1461 		break;
   1462 	switch(SB_GET()) {
   1463 	case TELQUAL_SEND:
   1464 	case TELQUAL_REPLY:
   1465 		/*
   1466 		 * These are sent by us and cannot be sent by
   1467 		 * the client.
   1468 		 */
   1469 		break;
   1470 	case TELQUAL_IS:
   1471 		auth_is(subpointer, SB_LEN());
   1472 		break;
   1473 	case TELQUAL_NAME:
   1474 		auth_name(subpointer, SB_LEN());
   1475 		break;
   1476 	}
   1477 	break;
   1478 #endif
   1479 #ifdef	ENCRYPTION
   1480     case TELOPT_ENCRYPT:
   1481 	if (SB_EOF())
   1482 		break;
   1483 	switch(SB_GET()) {
   1484 	case ENCRYPT_SUPPORT:
   1485 		encrypt_support(subpointer, SB_LEN());
   1486 		break;
   1487 	case ENCRYPT_IS:
   1488 		encrypt_is(subpointer, SB_LEN());
   1489 		break;
   1490 	case ENCRYPT_REPLY:
   1491 		encrypt_reply(subpointer, SB_LEN());
   1492 		break;
   1493 	case ENCRYPT_START:
   1494 		encrypt_start(subpointer, SB_LEN());
   1495 		break;
   1496 	case ENCRYPT_END:
   1497 		encrypt_end();
   1498 		break;
   1499 	case ENCRYPT_REQSTART:
   1500 		encrypt_request_start(subpointer, SB_LEN());
   1501 		break;
   1502 	case ENCRYPT_REQEND:
   1503 		/*
   1504 		 * We can always send an REQEND so that we cannot
   1505 		 * get stuck encrypting.  We should only get this
   1506 		 * if we have been able to get in the correct mode
   1507 		 * anyhow.
   1508 		 */
   1509 		encrypt_request_end();
   1510 		break;
   1511 	case ENCRYPT_ENC_KEYID:
   1512 		encrypt_enc_keyid(subpointer, SB_LEN());
   1513 		break;
   1514 	case ENCRYPT_DEC_KEYID:
   1515 		encrypt_dec_keyid(subpointer, SB_LEN());
   1516 		break;
   1517 	default:
   1518 		break;
   1519 	}
   1520 	break;
   1521 #endif	/* ENCRYPTION */
   1522 
   1523     default:
   1524 	break;
   1525     }  /* end of switch */
   1526 
   1527 }  /* end of suboption */
   1528 
   1529 #ifdef LINEMODE
   1530 void
   1531 doclientstat(void)
   1532 {
   1533 	clientstat(TELOPT_LINEMODE, WILL, 0);
   1534 }
   1535 #endif /* LINEMODE */
   1536 
   1537 void
   1538 send_status(void)
   1539 {
   1540 #define	ADD(c) \
   1541 	do { \
   1542 		if (ep > ncp) \
   1543 			*ncp++ = c; \
   1544 		else \
   1545 			goto trunc; \
   1546 	} while (0)
   1547 #define	ADD_DATA(c) \
   1548 	do { \
   1549 		ADD(c); if (c == SE || c == IAC) ADD(c); \
   1550 	} while (0)
   1551 
   1552 	unsigned char statusbuf[256];
   1553 	unsigned char *ep;
   1554 	unsigned char *ncp;
   1555 	unsigned char i;
   1556 
   1557 	ncp = statusbuf;
   1558 	ep = statusbuf + sizeof(statusbuf);
   1559 
   1560 	netflush();	/* get rid of anything waiting to go out */
   1561 
   1562 	ADD(IAC);
   1563 	ADD(SB);
   1564 	ADD(TELOPT_STATUS);
   1565 	ADD(TELQUAL_IS);
   1566 
   1567 	/*
   1568 	 * We check the want_state rather than the current state,
   1569 	 * because if we received a DO/WILL for an option that we
   1570 	 * don't support, and the other side didn't send a DONT/WONT
   1571 	 * in response to our WONT/DONT, then the "state" will be
   1572 	 * WILL/DO, and the "want_state" will be WONT/DONT.  We
   1573 	 * need to go by the latter.
   1574 	 */
   1575 	for (i = 0; i < (unsigned char)NTELOPTS; i++) {
   1576 		if (my_want_state_is_will(i)) {
   1577 			ADD(WILL);
   1578 			ADD_DATA(i);
   1579 		}
   1580 		if (his_want_state_is_will(i)) {
   1581 			ADD(DO);
   1582 			ADD_DATA(i);
   1583 		}
   1584 	}
   1585 
   1586 	if (his_want_state_is_will(TELOPT_LFLOW)) {
   1587 		ADD(SB);
   1588 		ADD(TELOPT_LFLOW);
   1589 		if (flowmode) {
   1590 			ADD(LFLOW_ON);
   1591 		} else {
   1592 			ADD(LFLOW_OFF);
   1593 		}
   1594 		ADD(SE);
   1595 
   1596 		if (restartany >= 0) {
   1597 			ADD(SB);
   1598 			ADD(TELOPT_LFLOW);
   1599 			if (restartany) {
   1600 				ADD(LFLOW_RESTART_ANY);
   1601 			} else {
   1602 				ADD(LFLOW_RESTART_XON);
   1603 			}
   1604 			ADD(SE);
   1605 		}
   1606 	}
   1607 
   1608 #ifdef	LINEMODE
   1609 	if (his_want_state_is_will(TELOPT_LINEMODE)) {
   1610 		unsigned char *cp, *cpe;
   1611 		int len;
   1612 
   1613 		ADD(SB);
   1614 		ADD(TELOPT_LINEMODE);
   1615 		ADD(LM_MODE);
   1616 		ADD_DATA(editmode);
   1617 		ADD(SE);
   1618 
   1619 		ADD(SB);
   1620 		ADD(TELOPT_LINEMODE);
   1621 		ADD(LM_SLC);
   1622 		start_slc(0);
   1623 		send_slc();
   1624 		len = end_slc(&cp);
   1625 		for (cpe = cp + len; cp < cpe; cp++)
   1626 			ADD_DATA(*cp);
   1627 		ADD(SE);
   1628 	}
   1629 #endif	/* LINEMODE */
   1630 
   1631 	ADD(IAC);
   1632 	ADD(SE);
   1633 
   1634 	writenet(statusbuf, ncp - statusbuf);
   1635 	netflush();	/* Send it on its way */
   1636 
   1637 	DIAG(TD_OPTIONS,
   1638 		{printsub('>', statusbuf, ncp - statusbuf); netflush();});
   1639 	return;
   1640 
   1641 trunc:
   1642 	/* XXX bark? */
   1643 	return;
   1644 #undef ADD
   1645 #undef ADD_DATA
   1646 }
   1647 
   1648 int
   1649 output_data(const char *format, ...)
   1650 {
   1651 	va_list args;
   1652 	size_t remaining, ret;
   1653 
   1654 	va_start(args, format);
   1655 	remaining = BUFSIZ - (nfrontp - netobuf);
   1656 	/* try a netflush() if the room is too low */
   1657 	if (strlen(format) > remaining || BUFSIZ / 4 > remaining) {
   1658 		netflush();
   1659 		remaining = BUFSIZ - (nfrontp - netobuf);
   1660 	}
   1661 	ret = vsnprintf(nfrontp, remaining, format, args);
   1662 	nfrontp += ((ret < remaining - 1) ? ret : remaining - 1);
   1663 	va_end(args);
   1664 	return ret;
   1665 }
   1666 
   1667 int
   1668 output_datalen(const char *buf, size_t l)
   1669 {
   1670 	size_t remaining;
   1671 
   1672 	remaining = BUFSIZ - (nfrontp - netobuf);
   1673 	if (remaining < l) {
   1674 		netflush();
   1675 		remaining = BUFSIZ - (nfrontp - netobuf);
   1676 	}
   1677 	if (remaining < l)
   1678 		return -1;
   1679 	memmove(nfrontp, buf, l);
   1680 	nfrontp += l;
   1681 	return (int)l;
   1682 }
   1683