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