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