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