xref: /netbsd-src/libexec/telnetd/termstat.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: termstat.c,v 1.7 2000/06/22 06:47:49 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)termstat.c	8.2 (Berkeley) 5/30/95";
40 #else
41 __RCSID("$NetBSD: termstat.c,v 1.7 2000/06/22 06:47:49 thorpej Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include "telnetd.h"
46 
47 #if defined(ENCRYPTION)
48 #include <libtelnet/encrypt.h>
49 #endif
50 
51 /*
52  * local variables
53  */
54 int def_tspeed = -1, def_rspeed = -1;
55 #ifdef	TIOCSWINSZ
56 int def_row = 0, def_col = 0;
57 #endif
58 #ifdef	LINEMODE
59 static int _terminit = 0;
60 #endif	/* LINEMODE */
61 
62 #if	defined(CRAY2) && defined(UNICOS5)
63 int	newmap = 1;	/* nonzero if \n maps to ^M^J */
64 #endif
65 
66 #ifdef	LINEMODE
67 /*
68  * localstat
69  *
70  * This function handles all management of linemode.
71  *
72  * Linemode allows the client to do the local editing of data
73  * and send only complete lines to the server.  Linemode state is
74  * based on the state of the pty driver.  If the pty is set for
75  * external processing, then we can use linemode.  Further, if we
76  * can use real linemode, then we can look at the edit control bits
77  * in the pty to determine what editing the client should do.
78  *
79  * Linemode support uses the following state flags to keep track of
80  * current and desired linemode state.
81  *	alwayslinemode : true if -l was specified on the telnetd
82  * 	command line.  It means to have linemode on as much as
83  *	possible.
84  *
85  * 	lmodetype: signifies whether the client can
86  *	handle real linemode, or if use of kludgeomatic linemode
87  *	is preferred.  It will be set to one of the following:
88  *		REAL_LINEMODE : use linemode option
89  *		NO_KLUDGE : don't initiate kludge linemode.
90  *		KLUDGE_LINEMODE : use kludge linemode
91  *		NO_LINEMODE : client is ignorant of linemode
92  *
93  *	linemode, uselinemode : linemode is true if linemode
94  *	is currently on, uselinemode is the state that we wish
95  *	to be in.  If another function wishes to turn linemode
96  *	on or off, it sets or clears uselinemode.
97  *
98  *	editmode, useeditmode : like linemode/uselinemode, but
99  *	these contain the edit mode states (edit and trapsig).
100  *
101  * The state variables correspond to some of the state information
102  * in the pty.
103  *	linemode:
104  *		In real linemode, this corresponds to whether the pty
105  *		expects external processing of incoming data.
106  *		In kludge linemode, this more closely corresponds to the
107  *		whether normal processing is on or not.  (ICANON in
108  *		system V, or COOKED mode in BSD.)
109  *		If the -l option was specified (alwayslinemode), then
110  *		an attempt is made to force external processing on at
111  *		all times.
112  *
113  * The following heuristics are applied to determine linemode
114  * handling within the server.
115  *	1) Early on in starting up the server, an attempt is made
116  *	   to negotiate the linemode option.  If this succeeds
117  *	   then lmodetype is set to REAL_LINEMODE and all linemode
118  *	   processing occurs in the context of the linemode option.
119  *	2) If the attempt to negotiate the linemode option failed,
120  *	   and the "-k" (don't initiate kludge linemode) isn't set,
121  *	   then we try to use kludge linemode.  We test for this
122  *	   capability by sending "do Timing Mark".  If a positive
123  *	   response comes back, then we assume that the client
124  *	   understands kludge linemode (ech!) and the
125  *	   lmodetype flag is set to KLUDGE_LINEMODE.
126  *	3) Otherwise, linemode is not supported at all and
127  *	   lmodetype remains set to NO_LINEMODE (which happens
128  *	   to be 0 for convenience).
129  *	4) At any time a command arrives that implies a higher
130  *	   state of linemode support in the client, we move to that
131  *	   linemode support.
132  *
133  * A short explanation of kludge linemode is in order here.
134  *	1) The heuristic to determine support for kludge linemode
135  *	   is to send a do timing mark.  We assume that a client
136  *	   that supports timing marks also supports kludge linemode.
137  *	   A risky proposition at best.
138  *	2) Further negotiation of linemode is done by changing the
139  *	   the server's state regarding SGA.  If server will SGA,
140  *	   then linemode is off, if server won't SGA, then linemode
141  *	   is on.
142  */
143 	void
144 localstat()
145 {
146 	int need_will_echo = 0;
147 
148 #if	defined(CRAY2) && defined(UNICOS5)
149 	/*
150 	 * Keep track of that ol' CR/NL mapping while we're in the
151 	 * neighborhood.
152 	 */
153 	newmap = tty_isnewmap();
154 #endif	/* defined(CRAY2) && defined(UNICOS5) */
155 
156 	/*
157 	 * Check for state of BINARY options.
158 	 */
159 	if (tty_isbinaryin()) {
160 		if (his_want_state_is_wont(TELOPT_BINARY))
161 			send_do(TELOPT_BINARY, 1);
162 	} else {
163 		if (his_want_state_is_will(TELOPT_BINARY))
164 			send_dont(TELOPT_BINARY, 1);
165 	}
166 
167 	if (tty_isbinaryout()) {
168 		if (my_want_state_is_wont(TELOPT_BINARY))
169 			send_will(TELOPT_BINARY, 1);
170 	} else {
171 		if (my_want_state_is_will(TELOPT_BINARY))
172 			send_wont(TELOPT_BINARY, 1);
173 	}
174 
175 	/*
176 	 * Check for changes to flow control if client supports it.
177 	 */
178 	flowstat();
179 
180 	/*
181 	 * Check linemode on/off state
182 	 */
183 	uselinemode = tty_linemode();
184 
185 	/*
186 	 * If alwayslinemode is on, and pty is changing to turn it off, then
187 	 * force linemode back on.
188 	 */
189 	if (alwayslinemode && linemode && !uselinemode) {
190 		uselinemode = 1;
191 		tty_setlinemode(uselinemode);
192 	}
193 
194 #ifdef	ENCRYPTION
195 	/*
196 	 * If the terminal is not echoing, but editing is enabled,
197 	 * something like password input is going to happen, so
198 	 * if we the other side is not currently sending encrypted
199 	 * data, ask the other side to start encrypting.
200 	 */
201 	if (his_state_is_will(TELOPT_ENCRYPT)) {
202 		static int enc_passwd = 0;
203 		if (uselinemode && !tty_isecho() && tty_isediting()
204 		    && (enc_passwd == 0) && !decrypt_input) {
205 			encrypt_send_request_start();
206 			enc_passwd = 1;
207 		} else if (enc_passwd) {
208 			encrypt_send_request_end();
209 			enc_passwd = 0;
210 		}
211 	}
212 #endif	/* ENCRYPTION */
213 
214 	/*
215 	 * Do echo mode handling as soon as we know what the
216 	 * linemode is going to be.
217 	 * If the pty has echo turned off, then tell the client that
218 	 * the server will echo.  If echo is on, then the server
219 	 * will echo if in character mode, but in linemode the
220 	 * client should do local echoing.  The state machine will
221 	 * not send anything if it is unnecessary, so don't worry
222 	 * about that here.
223 	 *
224 	 * If we need to send the WILL ECHO (because echo is off),
225 	 * then delay that until after we have changed the MODE.
226 	 * This way, when the user is turning off both editing
227 	 * and echo, the client will get editing turned off first.
228 	 * This keeps the client from going into encryption mode
229 	 * and then right back out if it is doing auto-encryption
230 	 * when passwords are being typed.
231 	 */
232 	if (uselinemode) {
233 		if (tty_isecho())
234 			send_wont(TELOPT_ECHO, 1);
235 		else
236 			need_will_echo = 1;
237 #ifdef	KLUDGELINEMODE
238 		if (lmodetype == KLUDGE_OK)
239 			lmodetype = KLUDGE_LINEMODE;
240 #endif
241 	}
242 
243 	/*
244 	 * If linemode is being turned off, send appropriate
245 	 * command and then we're all done.
246 	 */
247 	 if (!uselinemode && linemode) {
248 # ifdef	KLUDGELINEMODE
249 		if (lmodetype == REAL_LINEMODE) {
250 # endif	/* KLUDGELINEMODE */
251 			send_dont(TELOPT_LINEMODE, 1);
252 # ifdef	KLUDGELINEMODE
253 		} else if (lmodetype == KLUDGE_LINEMODE)
254 			send_will(TELOPT_SGA, 1);
255 # endif	/* KLUDGELINEMODE */
256 		send_will(TELOPT_ECHO, 1);
257 		linemode = uselinemode;
258 		goto done;
259 	}
260 
261 # ifdef	KLUDGELINEMODE
262 	/*
263 	 * If using real linemode check edit modes for possible later use.
264 	 * If we are in kludge linemode, do the SGA negotiation.
265 	 */
266 	if (lmodetype == REAL_LINEMODE) {
267 # endif	/* KLUDGELINEMODE */
268 		useeditmode = 0;
269 		if (tty_isediting())
270 			useeditmode |= MODE_EDIT;
271 		if (tty_istrapsig())
272 			useeditmode |= MODE_TRAPSIG;
273 		if (tty_issofttab())
274 			useeditmode |= MODE_SOFT_TAB;
275 		if (tty_islitecho())
276 			useeditmode |= MODE_LIT_ECHO;
277 # ifdef	KLUDGELINEMODE
278 	} else if (lmodetype == KLUDGE_LINEMODE) {
279 		if (tty_isediting() && uselinemode)
280 			send_wont(TELOPT_SGA, 1);
281 		else
282 			send_will(TELOPT_SGA, 1);
283 	}
284 # endif	/* KLUDGELINEMODE */
285 
286 	/*
287 	 * Negotiate linemode on if pty state has changed to turn it on.
288 	 * Send appropriate command and send along edit mode, then all done.
289 	 */
290 	if (uselinemode && !linemode) {
291 # ifdef	KLUDGELINEMODE
292 		if (lmodetype == KLUDGE_LINEMODE) {
293 			send_wont(TELOPT_SGA, 1);
294 		} else if (lmodetype == REAL_LINEMODE) {
295 # endif	/* KLUDGELINEMODE */
296 			send_do(TELOPT_LINEMODE, 1);
297 			/* send along edit modes */
298 			(void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC, SB,
299 				TELOPT_LINEMODE, LM_MODE, useeditmode,
300 				IAC, SE);
301 			nfrontp += 7;
302 			editmode = useeditmode;
303 # ifdef	KLUDGELINEMODE
304 		}
305 # endif	/* KLUDGELINEMODE */
306 		linemode = uselinemode;
307 		goto done;
308 	}
309 
310 # ifdef	KLUDGELINEMODE
311 	/*
312 	 * None of what follows is of any value if not using
313 	 * real linemode.
314 	 */
315 	if (lmodetype < REAL_LINEMODE)
316 		goto done;
317 # endif	/* KLUDGELINEMODE */
318 
319 	if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
320 		/*
321 		 * If edit mode changed, send edit mode.
322 		 */
323 		 if (useeditmode != editmode) {
324 			/*
325 			 * Send along appropriate edit mode mask.
326 			 */
327 			(void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC, SB,
328 				TELOPT_LINEMODE, LM_MODE, useeditmode,
329 				IAC, SE);
330 			nfrontp += 7;
331 			editmode = useeditmode;
332 		}
333 
334 
335 		/*
336 		 * Check for changes to special characters in use.
337 		 */
338 		start_slc(0);
339 		check_slc();
340 		(void) end_slc(0);
341 	}
342 
343 done:
344 	if (need_will_echo)
345 		send_will(TELOPT_ECHO, 1);
346 	/*
347 	 * Some things should be deferred until after the pty state has
348 	 * been set by the local process.  Do those things that have been
349 	 * deferred now.  This only happens once.
350 	 */
351 	if (_terminit == 0) {
352 		_terminit = 1;
353 		defer_terminit();
354 	}
355 
356 	netflush();
357 	set_termbuf();
358 	return;
359 
360 }  /* end of localstat */
361 #endif	/* LINEMODE */
362 
363 /*
364  * flowstat
365  *
366  * Check for changes to flow control
367  */
368 	void
369 flowstat()
370 {
371 	if (his_state_is_will(TELOPT_LFLOW)) {
372 		if (tty_flowmode() != flowmode) {
373 			flowmode = tty_flowmode();
374 			(void) sprintf(nfrontp, "%c%c%c%c%c%c",
375 					IAC, SB, TELOPT_LFLOW,
376 					flowmode ? LFLOW_ON : LFLOW_OFF,
377 					IAC, SE);
378 			nfrontp += 6;
379 		}
380 		if (tty_restartany() != restartany) {
381 			restartany = tty_restartany();
382 			(void) sprintf(nfrontp, "%c%c%c%c%c%c",
383 					IAC, SB, TELOPT_LFLOW,
384 					restartany ? LFLOW_RESTART_ANY
385 						   : LFLOW_RESTART_XON,
386 					IAC, SE);
387 			nfrontp += 6;
388 		}
389 	}
390 }
391 
392 /*
393  * clientstat
394  *
395  * Process linemode related requests from the client.
396  * Client can request a change to only one of linemode, editmode or slc's
397  * at a time, and if using kludge linemode, then only linemode may be
398  * affected.
399  */
400 	void
401 clientstat(code, parm1, parm2)
402 	register int code, parm1, parm2;
403 {
404 
405 	/*
406 	 * Get a copy of terminal characteristics.
407 	 */
408 	init_termbuf();
409 
410 	/*
411 	 * Process request from client. code tells what it is.
412 	 */
413 	switch (code) {
414 #ifdef	LINEMODE
415 	case TELOPT_LINEMODE:
416 		/*
417 		 * Don't do anything unless client is asking us to change
418 		 * modes.
419 		 */
420 		uselinemode = (parm1 == WILL);
421 		if (uselinemode != linemode) {
422 # ifdef	KLUDGELINEMODE
423 			/*
424 			 * If using kludge linemode, make sure that
425 			 * we can do what the client asks.
426 			 * We can not turn off linemode if alwayslinemode
427 			 * and the ICANON bit is set.
428 			 */
429 			if (lmodetype == KLUDGE_LINEMODE) {
430 				if (alwayslinemode && tty_isediting()) {
431 					uselinemode = 1;
432 				}
433 			}
434 
435 			/*
436 			 * Quit now if we can't do it.
437 			 */
438 			if (uselinemode == linemode)
439 				return;
440 
441 			/*
442 			 * If using real linemode and linemode is being
443 			 * turned on, send along the edit mode mask.
444 			 */
445 			if (lmodetype == REAL_LINEMODE && uselinemode)
446 # else	/* KLUDGELINEMODE */
447 			if (uselinemode)
448 # endif	/* KLUDGELINEMODE */
449 			{
450 				useeditmode = 0;
451 				if (tty_isediting())
452 					useeditmode |= MODE_EDIT;
453 				if (tty_istrapsig)
454 					useeditmode |= MODE_TRAPSIG;
455 				if (tty_issofttab())
456 					useeditmode |= MODE_SOFT_TAB;
457 				if (tty_islitecho())
458 					useeditmode |= MODE_LIT_ECHO;
459 				(void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC,
460 					SB, TELOPT_LINEMODE, LM_MODE,
461 							useeditmode, IAC, SE);
462 				nfrontp += 7;
463 				editmode = useeditmode;
464 			}
465 
466 
467 			tty_setlinemode(uselinemode);
468 
469 			linemode = uselinemode;
470 
471 			if (!linemode)
472 				send_will(TELOPT_ECHO, 1);
473 		}
474 		break;
475 
476 	case LM_MODE:
477 	    {
478 		register int ack, changed;
479 
480 		/*
481 		 * Client has sent along a mode mask.  If it agrees with
482 		 * what we are currently doing, ignore it; if not, it could
483 		 * be viewed as a request to change.  Note that the server
484 		 * will change to the modes in an ack if it is different from
485 		 * what we currently have, but we will not ack the ack.
486 		 */
487 		 useeditmode &= MODE_MASK;
488 		 ack = (useeditmode & MODE_ACK);
489 		 useeditmode &= ~MODE_ACK;
490 
491 		 if ((changed = (useeditmode ^ editmode))) {
492 			/*
493 			 * This check is for a timing problem.  If the
494 			 * state of the tty has changed (due to the user
495 			 * application) we need to process that info
496 			 * before we write in the state contained in the
497 			 * ack!!!  This gets out the new MODE request,
498 			 * and when the ack to that command comes back
499 			 * we'll set it and be in the right mode.
500 			 */
501 			if (ack)
502 				localstat();
503 			if (changed & MODE_EDIT)
504 				tty_setedit(useeditmode & MODE_EDIT);
505 
506 			if (changed & MODE_TRAPSIG)
507 				tty_setsig(useeditmode & MODE_TRAPSIG);
508 
509 			if (changed & MODE_SOFT_TAB)
510 				tty_setsofttab(useeditmode & MODE_SOFT_TAB);
511 
512 			if (changed & MODE_LIT_ECHO)
513 				tty_setlitecho(useeditmode & MODE_LIT_ECHO);
514 
515 			set_termbuf();
516 
517  			if (!ack) {
518  				(void) sprintf(nfrontp, "%c%c%c%c%c%c%c", IAC,
519 					SB, TELOPT_LINEMODE, LM_MODE,
520  					useeditmode|MODE_ACK,
521  					IAC, SE);
522  				nfrontp += 7;
523  			}
524 
525 			editmode = useeditmode;
526 		}
527 
528 		break;
529 
530 	    }  /* end of case LM_MODE */
531 #endif	/* LINEMODE */
532 
533 	case TELOPT_NAWS:
534 #ifdef	TIOCSWINSZ
535 	    {
536 		struct winsize ws;
537 
538 		def_col = parm1;
539 		def_row = parm2;
540 #ifdef	LINEMODE
541 		/*
542 		 * Defer changing window size until after terminal is
543 		 * initialized.
544 		 */
545 		if (terminit() == 0)
546 			return;
547 #endif	/* LINEMODE */
548 
549 		/*
550 		 * Change window size as requested by client.
551 		 */
552 
553 		ws.ws_col = parm1;
554 		ws.ws_row = parm2;
555 		(void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
556 	    }
557 #endif	/* TIOCSWINSZ */
558 
559 		break;
560 
561 	case TELOPT_TSPEED:
562 	    {
563 		def_tspeed = parm1;
564 		def_rspeed = parm2;
565 #ifdef	LINEMODE
566 		/*
567 		 * Defer changing the terminal speed.
568 		 */
569 		if (terminit() == 0)
570 			return;
571 #endif	/* LINEMODE */
572 		/*
573 		 * Change terminal speed as requested by client.
574 		 * We set the receive speed first, so that if we can't
575 		 * store seperate receive and transmit speeds, the transmit
576 		 * speed will take precedence.
577 		 */
578 		tty_rspeed(parm2);
579 		tty_tspeed(parm1);
580 		set_termbuf();
581 
582 		break;
583 
584 	    }  /* end of case TELOPT_TSPEED */
585 
586 	default:
587 		/* What? */
588 		break;
589 	}  /* end of switch */
590 
591 #if	defined(CRAY2) && defined(UNICOS5)
592 	/*
593 	 * Just in case of the likely event that we changed the pty state.
594 	 */
595 	rcv_ioctl();
596 #endif	/* defined(CRAY2) && defined(UNICOS5) */
597 
598 	netflush();
599 
600 }  /* end of clientstat */
601 
602 #if	defined(CRAY2) && defined(UNICOS5)
603 	void
604 termstat()
605 {
606 	needtermstat = 1;
607 }
608 
609 	void
610 _termstat()
611 {
612 	needtermstat = 0;
613 	init_termbuf();
614 	localstat();
615 	rcv_ioctl();
616 }
617 #endif	/* defined(CRAY2) && defined(UNICOS5) */
618 
619 #ifdef	LINEMODE
620 /*
621  * defer_terminit
622  *
623  * Some things should not be done until after the login process has started
624  * and all the pty modes are set to what they are supposed to be.  This
625  * function is called when the pty state has been processed for the first time.
626  * It calls other functions that do things that were deferred in each module.
627  */
628 	void
629 defer_terminit()
630 {
631 
632 	/*
633 	 * local stuff that got deferred.
634 	 */
635 	if (def_tspeed != -1) {
636 		clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed);
637 		def_tspeed = def_rspeed = 0;
638 	}
639 
640 #ifdef	TIOCSWINSZ
641 	if (def_col || def_row) {
642 		struct winsize ws;
643 
644 		memset((char *)&ws, 0, sizeof(ws));
645 		ws.ws_col = def_col;
646 		ws.ws_row = def_row;
647 		(void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
648 	}
649 #endif
650 
651 	/*
652 	 * The only other module that currently defers anything.
653 	 */
654 	deferslc();
655 
656 }  /* end of defer_terminit */
657 
658 /*
659  * terminit
660  *
661  * Returns true if the pty state has been processed yet.
662  */
663 	int
664 terminit()
665 {
666 	return(_terminit);
667 
668 }  /* end of terminit */
669 #endif	/* LINEMODE */
670