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