Home | History | Annotate | Line # | Download | only in telnetd
state.c revision 1.8
      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.5 (Berkeley) 5/30/95"; */
     36 static char rcsid[] = "$NetBSD: state.c,v 1.8 1996/02/24 01:22:19 jtk 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 		memmove(xptyobuf, opfrontp, 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 # endif	/* KLUDGELINEMODE */
    695 			clientstat(TELOPT_LINEMODE, WONT, 0);
    696 			break;
    697 #endif	/* LINEMODE */
    698 
    699 		case TELOPT_TM:
    700 			/*
    701 			 * If we get a WONT TM, and had sent a DO TM,
    702 			 * don't respond with a DONT TM, just leave it
    703 			 * as is.  Short circut the state machine to
    704 			 * achive this.
    705 			 */
    706 			set_his_want_state_wont(TELOPT_TM);
    707 			return;
    708 
    709 		case TELOPT_LFLOW:
    710 			/*
    711 			 * If we are not going to support flow control
    712 			 * option, then let peer know that we can't
    713 			 * change the flow control characters.
    714 			 */
    715 			slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
    716 			slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
    717 			slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
    718 			slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
    719 			break;
    720 
    721 #if	defined(AUTHENTICATION)
    722 		case TELOPT_AUTHENTICATION:
    723 			auth_finished(0, AUTH_REJECT);
    724 			break;
    725 #endif
    726 
    727 		/*
    728 		 * For options that we might spin waiting for
    729 		 * sub-negotiation, if the client turns off the
    730 		 * option rather than responding to the request,
    731 		 * we have to treat it here as if we got a response
    732 		 * to the sub-negotiation, (by updating the timers)
    733 		 * so that we'll break out of the loop.
    734 		 */
    735 		case TELOPT_TTYPE:
    736 			settimer(ttypesubopt);
    737 			break;
    738 
    739 		case TELOPT_TSPEED:
    740 			settimer(tspeedsubopt);
    741 			break;
    742 
    743 		case TELOPT_XDISPLOC:
    744 			settimer(xdisplocsubopt);
    745 			break;
    746 
    747 		case TELOPT_OLD_ENVIRON:
    748 			settimer(oenvironsubopt);
    749 			break;
    750 
    751 		case TELOPT_NEW_ENVIRON:
    752 			settimer(environsubopt);
    753 			break;
    754 
    755 		default:
    756 			break;
    757 		}
    758 		set_his_want_state_wont(option);
    759 		if (his_state_is_will(option))
    760 			send_dont(option, 0);
    761 	    } else {
    762 		switch (option) {
    763 		case TELOPT_TM:
    764 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    765 			if (lmodetype < NO_AUTOKLUDGE) {
    766 				lmodetype = NO_LINEMODE;
    767 				clientstat(TELOPT_LINEMODE, WONT, 0);
    768 				send_will(TELOPT_SGA, 1);
    769 				send_will(TELOPT_ECHO, 1);
    770 			}
    771 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    772 			break;
    773 
    774 #if	defined(AUTHENTICATION)
    775 		case TELOPT_AUTHENTICATION:
    776 			auth_finished(0, AUTH_REJECT);
    777 			break;
    778 #endif
    779 		default:
    780 			break;
    781 		}
    782 	    }
    783 	}
    784 	set_his_state_wont(option);
    785 
    786 }  /* end of wontoption */
    787 
    788 	void
    789 send_will(option, init)
    790 	int option, init;
    791 {
    792 	if (init) {
    793 		if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
    794 		    my_want_state_is_will(option))
    795 			return;
    796 		set_my_want_state_will(option);
    797 		will_wont_resp[option]++;
    798 	}
    799 	(void) sprintf(nfrontp, (char *)will, option);
    800 	nfrontp += sizeof (doopt) - 2;
    801 
    802 	DIAG(TD_OPTIONS, printoption("td: send will", option));
    803 }
    804 
    805 #if	!defined(LINEMODE) || !defined(KLUDGELINEMODE)
    806 /*
    807  * When we get a DONT SGA, we will try once to turn it
    808  * back on.  If the other side responds DONT SGA, we
    809  * leave it at that.  This is so that when we talk to
    810  * clients that understand KLUDGELINEMODE but not LINEMODE,
    811  * we'll keep them in char-at-a-time mode.
    812  */
    813 int turn_on_sga = 0;
    814 #endif
    815 
    816 	void
    817 dooption(option)
    818 	int option;
    819 {
    820 	int changeok = 0;
    821 
    822 	/*
    823 	 * Process client input.
    824 	 */
    825 
    826 	DIAG(TD_OPTIONS, printoption("td: recv do", option));
    827 
    828 	if (will_wont_resp[option]) {
    829 		will_wont_resp[option]--;
    830 		if (will_wont_resp[option] && my_state_is_will(option))
    831 			will_wont_resp[option]--;
    832 	}
    833 	if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
    834 		switch (option) {
    835 		case TELOPT_ECHO:
    836 #ifdef	LINEMODE
    837 # ifdef	KLUDGELINEMODE
    838 			if (lmodetype == NO_LINEMODE)
    839 # else
    840 			if (his_state_is_wont(TELOPT_LINEMODE))
    841 # endif
    842 #endif
    843 			{
    844 				init_termbuf();
    845 				tty_setecho(1);
    846 				set_termbuf();
    847 			}
    848 			changeok++;
    849 			break;
    850 
    851 		case TELOPT_BINARY:
    852 			init_termbuf();
    853 			tty_binaryout(1);
    854 			set_termbuf();
    855 			changeok++;
    856 			break;
    857 
    858 		case TELOPT_SGA:
    859 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    860 			/*
    861 			 * If kludge linemode is in use, then we must
    862 			 * process an incoming do SGA for linemode
    863 			 * purposes.
    864 			 */
    865 			if (lmodetype == KLUDGE_LINEMODE) {
    866 				/*
    867 				 * Receipt of "do SGA" in kludge
    868 				 * linemode is the peer asking us to
    869 				 * turn off linemode.  Make note of
    870 				 * the request.
    871 				 */
    872 				clientstat(TELOPT_LINEMODE, WONT, 0);
    873 				/*
    874 				 * If linemode did not get turned off
    875 				 * then don't tell peer that we did.
    876 				 * Breaking here forces a wont SGA to
    877 				 * be returned.
    878 				 */
    879 				if (linemode)
    880 					break;
    881 			}
    882 #else
    883 			turn_on_sga = 0;
    884 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    885 			changeok++;
    886 			break;
    887 
    888 		case TELOPT_STATUS:
    889 			changeok++;
    890 			break;
    891 
    892 		case TELOPT_TM:
    893 			/*
    894 			 * Special case for TM.  We send a WILL, but
    895 			 * pretend we sent a WONT.
    896 			 */
    897 			send_will(option, 0);
    898 			set_my_want_state_wont(option);
    899 			set_my_state_wont(option);
    900 			return;
    901 
    902 		case TELOPT_LOGOUT:
    903 			/*
    904 			 * When we get a LOGOUT option, respond
    905 			 * with a WILL LOGOUT, make sure that
    906 			 * it gets written out to the network,
    907 			 * and then just go away...
    908 			 */
    909 			set_my_want_state_will(TELOPT_LOGOUT);
    910 			send_will(TELOPT_LOGOUT, 0);
    911 			set_my_state_will(TELOPT_LOGOUT);
    912 			(void)netflush();
    913 			cleanup(0);
    914 			/* NOT REACHED */
    915 			break;
    916 
    917 		case TELOPT_LINEMODE:
    918 		case TELOPT_TTYPE:
    919 		case TELOPT_NAWS:
    920 		case TELOPT_TSPEED:
    921 		case TELOPT_LFLOW:
    922 		case TELOPT_XDISPLOC:
    923 #ifdef	TELOPT_ENVIRON
    924 		case TELOPT_NEW_ENVIRON:
    925 #endif
    926 		case TELOPT_OLD_ENVIRON:
    927 		default:
    928 			break;
    929 		}
    930 		if (changeok) {
    931 			set_my_want_state_will(option);
    932 			send_will(option, 0);
    933 		} else {
    934 			will_wont_resp[option]++;
    935 			send_wont(option, 0);
    936 		}
    937 	}
    938 	set_my_state_will(option);
    939 
    940 }  /* end of dooption */
    941 
    942 	void
    943 send_wont(option, init)
    944 	int option, init;
    945 {
    946 	if (init) {
    947 		if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
    948 		    my_want_state_is_wont(option))
    949 			return;
    950 		set_my_want_state_wont(option);
    951 		will_wont_resp[option]++;
    952 	}
    953 	(void) sprintf(nfrontp, (char *)wont, option);
    954 	nfrontp += sizeof (wont) - 2;
    955 
    956 	DIAG(TD_OPTIONS, printoption("td: send wont", option));
    957 }
    958 
    959 	void
    960 dontoption(option)
    961 	int option;
    962 {
    963 	/*
    964 	 * Process client input.
    965 	 */
    966 
    967 
    968 	DIAG(TD_OPTIONS, printoption("td: recv dont", option));
    969 
    970 	if (will_wont_resp[option]) {
    971 		will_wont_resp[option]--;
    972 		if (will_wont_resp[option] && my_state_is_wont(option))
    973 			will_wont_resp[option]--;
    974 	}
    975 	if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
    976 		switch (option) {
    977 		case TELOPT_BINARY:
    978 			init_termbuf();
    979 			tty_binaryout(0);
    980 			set_termbuf();
    981 			break;
    982 
    983 		case TELOPT_ECHO:	/* we should stop echoing */
    984 #ifdef	LINEMODE
    985 # ifdef	KLUDGELINEMODE
    986 			if ((lmodetype != REAL_LINEMODE) &&
    987 			    (lmodetype != KLUDGE_LINEMODE))
    988 # else
    989 			if (his_state_is_wont(TELOPT_LINEMODE))
    990 # endif
    991 #endif
    992 			{
    993 				init_termbuf();
    994 				tty_setecho(0);
    995 				set_termbuf();
    996 			}
    997 			break;
    998 
    999 		case TELOPT_SGA:
   1000 #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
   1001 			/*
   1002 			 * If kludge linemode is in use, then we
   1003 			 * must process an incoming do SGA for
   1004 			 * linemode purposes.
   1005 			 */
   1006 			if ((lmodetype == KLUDGE_LINEMODE) ||
   1007 			    (lmodetype == KLUDGE_OK)) {
   1008 				/*
   1009 				 * The client is asking us to turn
   1010 				 * linemode on.
   1011 				 */
   1012 				lmodetype = KLUDGE_LINEMODE;
   1013 				clientstat(TELOPT_LINEMODE, WILL, 0);
   1014 				/*
   1015 				 * If we did not turn line mode on,
   1016 				 * then what do we say?  Will SGA?
   1017 				 * This violates design of telnet.
   1018 				 * Gross.  Very Gross.
   1019 				 */
   1020 			}
   1021 			break;
   1022 #else
   1023 			set_my_want_state_wont(option);
   1024 			if (my_state_is_will(option))
   1025 				send_wont(option, 0);
   1026 			set_my_state_wont(option);
   1027 			if (turn_on_sga ^= 1)
   1028 				send_will(option, 1);
   1029 			return;
   1030 #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
   1031 
   1032 		default:
   1033 			break;
   1034 		}
   1035 
   1036 		set_my_want_state_wont(option);
   1037 		if (my_state_is_will(option))
   1038 			send_wont(option, 0);
   1039 	}
   1040 	set_my_state_wont(option);
   1041 
   1042 }  /* end of dontoption */
   1043 
   1044 #ifdef	ENV_HACK
   1045 int env_ovar = -1;
   1046 int env_ovalue = -1;
   1047 #else	/* ENV_HACK */
   1048 # define env_ovar OLD_ENV_VAR
   1049 # define env_ovalue OLD_ENV_VALUE
   1050 #endif	/* ENV_HACK */
   1051 
   1052 /* envvarok(char*) */
   1053 /* check that variable is safe to pass to login or shell */
   1054 static int
   1055 envvarok(varp)
   1056 	char *varp;
   1057 {
   1058 	return (strncmp(varp, "LD_", strlen("LD_")) &&
   1059 		strncmp(varp, "_RLD_", strlen("_RLD_")) &&
   1060 		strcmp(varp, "LIBPATH") &&
   1061 		strcmp(varp, "IFS"));
   1062 }
   1063 
   1064 /*
   1065  * suboption()
   1066  *
   1067  *	Look at the sub-option buffer, and try to be helpful to the other
   1068  * side.
   1069  *
   1070  *	Currently we recognize:
   1071  *
   1072  *	Terminal type is
   1073  *	Linemode
   1074  *	Window size
   1075  *	Terminal speed
   1076  */
   1077 	void
   1078 suboption()
   1079 {
   1080     register int subchar;
   1081 
   1082     DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
   1083 
   1084     subchar = SB_GET();
   1085     switch (subchar) {
   1086     case TELOPT_TSPEED: {
   1087 	register int xspeed, rspeed;
   1088 
   1089 	if (his_state_is_wont(TELOPT_TSPEED))	/* Ignore if option disabled */
   1090 		break;
   1091 
   1092 	settimer(tspeedsubopt);
   1093 
   1094 	if (SB_EOF() || SB_GET() != TELQUAL_IS)
   1095 		return;
   1096 
   1097 	xspeed = atoi((char *)subpointer);
   1098 
   1099 	while (SB_GET() != ',' && !SB_EOF());
   1100 	if (SB_EOF())
   1101 		return;
   1102 
   1103 	rspeed = atoi((char *)subpointer);
   1104 	clientstat(TELOPT_TSPEED, xspeed, rspeed);
   1105 
   1106 	break;
   1107 
   1108     }  /* end of case TELOPT_TSPEED */
   1109 
   1110     case TELOPT_TTYPE: {		/* Yaaaay! */
   1111 	static char terminalname[41];
   1112 
   1113 	if (his_state_is_wont(TELOPT_TTYPE))	/* Ignore if option disabled */
   1114 		break;
   1115 	settimer(ttypesubopt);
   1116 
   1117 	if (SB_EOF() || SB_GET() != TELQUAL_IS) {
   1118 	    return;		/* ??? XXX but, this is the most robust */
   1119 	}
   1120 
   1121 	terminaltype = terminalname;
   1122 
   1123 	while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
   1124 								    !SB_EOF()) {
   1125 	    register int c;
   1126 
   1127 	    c = SB_GET();
   1128 	    if (isupper(c)) {
   1129 		c = tolower(c);
   1130 	    }
   1131 	    *terminaltype++ = c;    /* accumulate name */
   1132 	}
   1133 	*terminaltype = 0;
   1134 	terminaltype = terminalname;
   1135 	break;
   1136     }  /* end of case TELOPT_TTYPE */
   1137 
   1138     case TELOPT_NAWS: {
   1139 	register int xwinsize, ywinsize;
   1140 
   1141 	if (his_state_is_wont(TELOPT_NAWS))	/* Ignore if option disabled */
   1142 		break;
   1143 
   1144 	if (SB_EOF())
   1145 		return;
   1146 	xwinsize = SB_GET() << 8;
   1147 	if (SB_EOF())
   1148 		return;
   1149 	xwinsize |= SB_GET();
   1150 	if (SB_EOF())
   1151 		return;
   1152 	ywinsize = SB_GET() << 8;
   1153 	if (SB_EOF())
   1154 		return;
   1155 	ywinsize |= SB_GET();
   1156 	clientstat(TELOPT_NAWS, xwinsize, ywinsize);
   1157 
   1158 	break;
   1159 
   1160     }  /* end of case TELOPT_NAWS */
   1161 
   1162 #ifdef	LINEMODE
   1163     case TELOPT_LINEMODE: {
   1164 	register int request;
   1165 
   1166 	if (his_state_is_wont(TELOPT_LINEMODE))	/* Ignore if option disabled */
   1167 		break;
   1168 	/*
   1169 	 * Process linemode suboptions.
   1170 	 */
   1171 	if (SB_EOF())
   1172 	    break;		/* garbage was sent */
   1173 	request = SB_GET();	/* get will/wont */
   1174 
   1175 	if (SB_EOF())
   1176 	    break;		/* another garbage check */
   1177 
   1178 	if (request == LM_SLC) {  /* SLC is not preceeded by WILL or WONT */
   1179 		/*
   1180 		 * Process suboption buffer of slc's
   1181 		 */
   1182 		start_slc(1);
   1183 		do_opt_slc(subpointer, subend - subpointer);
   1184 		(void) end_slc(0);
   1185 		break;
   1186 	} else if (request == LM_MODE) {
   1187 		if (SB_EOF())
   1188 		    return;
   1189 		useeditmode = SB_GET();  /* get mode flag */
   1190 		clientstat(LM_MODE, 0, 0);
   1191 		break;
   1192 	}
   1193 
   1194 	if (SB_EOF())
   1195 	    break;
   1196 	switch (SB_GET()) {  /* what suboption? */
   1197 	case LM_FORWARDMASK:
   1198 		/*
   1199 		 * According to spec, only server can send request for
   1200 		 * forwardmask, and client can only return a positive response.
   1201 		 * So don't worry about it.
   1202 		 */
   1203 
   1204 	default:
   1205 		break;
   1206 	}
   1207 	break;
   1208     }  /* end of case TELOPT_LINEMODE */
   1209 #endif
   1210     case TELOPT_STATUS: {
   1211 	int mode;
   1212 
   1213 	if (SB_EOF())
   1214 	    break;
   1215 	mode = SB_GET();
   1216 	switch (mode) {
   1217 	case TELQUAL_SEND:
   1218 	    if (my_state_is_will(TELOPT_STATUS))
   1219 		send_status();
   1220 	    break;
   1221 
   1222 	case TELQUAL_IS:
   1223 	    break;
   1224 
   1225 	default:
   1226 	    break;
   1227 	}
   1228 	break;
   1229     }  /* end of case TELOPT_STATUS */
   1230 
   1231     case TELOPT_XDISPLOC: {
   1232 	if (SB_EOF() || SB_GET() != TELQUAL_IS)
   1233 		return;
   1234 	settimer(xdisplocsubopt);
   1235 	subpointer[SB_LEN()] = '\0';
   1236 	(void)setenv("DISPLAY", (char *)subpointer, 1);
   1237 	break;
   1238     }  /* end of case TELOPT_XDISPLOC */
   1239 
   1240 #ifdef	TELOPT_NEW_ENVIRON
   1241     case TELOPT_NEW_ENVIRON:
   1242 #endif
   1243     case TELOPT_OLD_ENVIRON: {
   1244 	register int c;
   1245 	register char *cp, *varp, *valp;
   1246 
   1247 	if (SB_EOF())
   1248 		return;
   1249 	c = SB_GET();
   1250 	if (c == TELQUAL_IS) {
   1251 		if (subchar == TELOPT_OLD_ENVIRON)
   1252 			settimer(oenvironsubopt);
   1253 		else
   1254 			settimer(environsubopt);
   1255 	} else if (c != TELQUAL_INFO) {
   1256 		return;
   1257 	}
   1258 
   1259 #ifdef	TELOPT_NEW_ENVIRON
   1260 	if (subchar == TELOPT_NEW_ENVIRON) {
   1261 	    while (!SB_EOF()) {
   1262 		c = SB_GET();
   1263 		if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
   1264 			break;
   1265 	    }
   1266 	} else
   1267 #endif
   1268 	{
   1269 #ifdef	ENV_HACK
   1270 	    /*
   1271 	     * We only want to do this if we haven't already decided
   1272 	     * whether or not the other side has its VALUE and VAR
   1273 	     * reversed.
   1274 	     */
   1275 	    if (env_ovar < 0) {
   1276 		register int last = -1;		/* invalid value */
   1277 		int empty = 0;
   1278 		int got_var = 0, got_value = 0, got_uservar = 0;
   1279 
   1280 		/*
   1281 		 * The other side might have its VALUE and VAR values
   1282 		 * reversed.  To be interoperable, we need to determine
   1283 		 * which way it is.  If the first recognized character
   1284 		 * is a VAR or VALUE, then that will tell us what
   1285 		 * type of client it is.  If the fist recognized
   1286 		 * character is a USERVAR, then we continue scanning
   1287 		 * the suboption looking for two consecutive
   1288 		 * VAR or VALUE fields.  We should not get two
   1289 		 * consecutive VALUE fields, so finding two
   1290 		 * consecutive VALUE or VAR fields will tell us
   1291 		 * what the client is.
   1292 		 */
   1293 		SB_SAVE();
   1294 		while (!SB_EOF()) {
   1295 			c = SB_GET();
   1296 			switch(c) {
   1297 			case OLD_ENV_VAR:
   1298 				if (last < 0 || last == OLD_ENV_VAR
   1299 				    || (empty && (last == OLD_ENV_VALUE)))
   1300 					goto env_ovar_ok;
   1301 				got_var++;
   1302 				last = OLD_ENV_VAR;
   1303 				break;
   1304 			case OLD_ENV_VALUE:
   1305 				if (last < 0 || last == OLD_ENV_VALUE
   1306 				    || (empty && (last == OLD_ENV_VAR)))
   1307 					goto env_ovar_wrong;
   1308 				got_value++;
   1309 				last = OLD_ENV_VALUE;
   1310 				break;
   1311 			case ENV_USERVAR:
   1312 				/* count strings of USERVAR as one */
   1313 				if (last != ENV_USERVAR)
   1314 					got_uservar++;
   1315 				if (empty) {
   1316 					if (last == OLD_ENV_VALUE)
   1317 						goto env_ovar_ok;
   1318 					if (last == OLD_ENV_VAR)
   1319 						goto env_ovar_wrong;
   1320 				}
   1321 				last = ENV_USERVAR;
   1322 				break;
   1323 			case ENV_ESC:
   1324 				if (!SB_EOF())
   1325 					c = SB_GET();
   1326 				/* FALL THROUGH */
   1327 			default:
   1328 				empty = 0;
   1329 				continue;
   1330 			}
   1331 			empty = 1;
   1332 		}
   1333 		if (empty) {
   1334 			if (last == OLD_ENV_VALUE)
   1335 				goto env_ovar_ok;
   1336 			if (last == OLD_ENV_VAR)
   1337 				goto env_ovar_wrong;
   1338 		}
   1339 		/*
   1340 		 * Ok, the first thing was a USERVAR, and there
   1341 		 * are not two consecutive VAR or VALUE commands,
   1342 		 * and none of the VAR or VALUE commands are empty.
   1343 		 * If the client has sent us a well-formed option,
   1344 		 * then the number of VALUEs received should always
   1345 		 * be less than or equal to the number of VARs and
   1346 		 * USERVARs received.
   1347 		 *
   1348 		 * If we got exactly as many VALUEs as VARs and
   1349 		 * USERVARs, the client has the same definitions.
   1350 		 *
   1351 		 * If we got exactly as many VARs as VALUEs and
   1352 		 * USERVARS, the client has reversed definitions.
   1353 		 */
   1354 		if (got_uservar + got_var == got_value) {
   1355 	    env_ovar_ok:
   1356 			env_ovar = OLD_ENV_VAR;
   1357 			env_ovalue = OLD_ENV_VALUE;
   1358 		} else if (got_uservar + got_value == got_var) {
   1359 	    env_ovar_wrong:
   1360 			env_ovar = OLD_ENV_VALUE;
   1361 			env_ovalue = OLD_ENV_VAR;
   1362 			DIAG(TD_OPTIONS, {sprintf(nfrontp,
   1363 				"ENVIRON VALUE and VAR are reversed!\r\n");
   1364 				nfrontp += strlen(nfrontp);});
   1365 
   1366 		}
   1367 	    }
   1368 	    SB_RESTORE();
   1369 #endif
   1370 
   1371 	    while (!SB_EOF()) {
   1372 		c = SB_GET();
   1373 		if ((c == env_ovar) || (c == ENV_USERVAR))
   1374 			break;
   1375 	    }
   1376 	}
   1377 
   1378 	if (SB_EOF())
   1379 		return;
   1380 
   1381 	cp = varp = (char *)subpointer;
   1382 	valp = 0;
   1383 
   1384 	while (!SB_EOF()) {
   1385 		c = SB_GET();
   1386 		if (subchar == TELOPT_OLD_ENVIRON) {
   1387 			if (c == env_ovar)
   1388 				c = NEW_ENV_VAR;
   1389 			else if (c == env_ovalue)
   1390 				c = NEW_ENV_VALUE;
   1391 		}
   1392 		switch (c) {
   1393 
   1394 		case NEW_ENV_VALUE:
   1395 			*cp = '\0';
   1396 			cp = valp = (char *)subpointer;
   1397 			break;
   1398 
   1399 		case NEW_ENV_VAR:
   1400 		case ENV_USERVAR:
   1401 			*cp = '\0';
   1402 			if (envvarok(varp)) {
   1403 				if (valp)
   1404 					(void)setenv(varp, valp, 1);
   1405 				else
   1406 					unsetenv(varp);
   1407 			}
   1408 			cp = varp = (char *)subpointer;
   1409 			valp = 0;
   1410 			break;
   1411 
   1412 		case ENV_ESC:
   1413 			if (SB_EOF())
   1414 				break;
   1415 			c = SB_GET();
   1416 			/* FALL THROUGH */
   1417 		default:
   1418 			*cp++ = c;
   1419 			break;
   1420 		}
   1421 	}
   1422 	*cp = '\0';
   1423 	if (envvarok(varp)) {
   1424 		if (valp)
   1425 			(void)setenv(varp, valp, 1);
   1426 		else
   1427 			unsetenv(varp);
   1428 	}
   1429 	break;
   1430     }  /* end of case TELOPT_NEW_ENVIRON */
   1431 #if	defined(AUTHENTICATION)
   1432     case TELOPT_AUTHENTICATION:
   1433 	if (SB_EOF())
   1434 		break;
   1435 	switch(SB_GET()) {
   1436 	case TELQUAL_SEND:
   1437 	case TELQUAL_REPLY:
   1438 		/*
   1439 		 * These are sent by us and cannot be sent by
   1440 		 * the client.
   1441 		 */
   1442 		break;
   1443 	case TELQUAL_IS:
   1444 		auth_is(subpointer, SB_LEN());
   1445 		break;
   1446 	case TELQUAL_NAME:
   1447 		auth_name(subpointer, SB_LEN());
   1448 		break;
   1449 	}
   1450 	break;
   1451 #endif
   1452 
   1453     default:
   1454 	break;
   1455     }  /* end of switch */
   1456 
   1457 }  /* end of suboption */
   1458 
   1459 	void
   1460 doclientstat()
   1461 {
   1462 	clientstat(TELOPT_LINEMODE, WILL, 0);
   1463 }
   1464 
   1465 #define	ADD(c)	 *ncp++ = c
   1466 #define	ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
   1467 	void
   1468 send_status()
   1469 {
   1470 	unsigned char statusbuf[256];
   1471 	register unsigned char *ncp;
   1472 	register unsigned char i;
   1473 
   1474 	ncp = statusbuf;
   1475 
   1476 	netflush();	/* get rid of anything waiting to go out */
   1477 
   1478 	ADD(IAC);
   1479 	ADD(SB);
   1480 	ADD(TELOPT_STATUS);
   1481 	ADD(TELQUAL_IS);
   1482 
   1483 	/*
   1484 	 * We check the want_state rather than the current state,
   1485 	 * because if we received a DO/WILL for an option that we
   1486 	 * don't support, and the other side didn't send a DONT/WONT
   1487 	 * in response to our WONT/DONT, then the "state" will be
   1488 	 * WILL/DO, and the "want_state" will be WONT/DONT.  We
   1489 	 * need to go by the latter.
   1490 	 */
   1491 	for (i = 0; i < (unsigned char)NTELOPTS; i++) {
   1492 		if (my_want_state_is_will(i)) {
   1493 			ADD(WILL);
   1494 			ADD_DATA(i);
   1495 		}
   1496 		if (his_want_state_is_will(i)) {
   1497 			ADD(DO);
   1498 			ADD_DATA(i);
   1499 		}
   1500 	}
   1501 
   1502 	if (his_want_state_is_will(TELOPT_LFLOW)) {
   1503 		ADD(SB);
   1504 		ADD(TELOPT_LFLOW);
   1505 		if (flowmode) {
   1506 			ADD(LFLOW_ON);
   1507 		} else {
   1508 			ADD(LFLOW_OFF);
   1509 		}
   1510 		ADD(SE);
   1511 
   1512 		if (restartany >= 0) {
   1513 			ADD(SB);
   1514 			ADD(TELOPT_LFLOW);
   1515 			if (restartany) {
   1516 				ADD(LFLOW_RESTART_ANY);
   1517 			} else {
   1518 				ADD(LFLOW_RESTART_XON);
   1519 			}
   1520 			ADD(SE);
   1521 		}
   1522 	}
   1523 
   1524 #ifdef	LINEMODE
   1525 	if (his_want_state_is_will(TELOPT_LINEMODE)) {
   1526 		unsigned char *cp, *cpe;
   1527 		int len;
   1528 
   1529 		ADD(SB);
   1530 		ADD(TELOPT_LINEMODE);
   1531 		ADD(LM_MODE);
   1532 		ADD_DATA(editmode);
   1533 		ADD(SE);
   1534 
   1535 		ADD(SB);
   1536 		ADD(TELOPT_LINEMODE);
   1537 		ADD(LM_SLC);
   1538 		start_slc(0);
   1539 		send_slc();
   1540 		len = end_slc(&cp);
   1541 		for (cpe = cp + len; cp < cpe; cp++)
   1542 			ADD_DATA(*cp);
   1543 		ADD(SE);
   1544 	}
   1545 #endif	/* LINEMODE */
   1546 
   1547 	ADD(IAC);
   1548 	ADD(SE);
   1549 
   1550 	writenet(statusbuf, ncp - statusbuf);
   1551 	netflush();	/* Send it on its way */
   1552 
   1553 	DIAG(TD_OPTIONS,
   1554 		{printsub('>', statusbuf, ncp - statusbuf); netflush();});
   1555 }
   1556