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