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