xref: /netbsd-src/libexec/telnetd/termstat.c (revision 4b90129081d9b35a633a40fa1147f089339f6cc5)
1*4b901290Sperry /*	$NetBSD: termstat.c,v 1.14 2005/02/06 05:58:21 perry Exp $	*/
214c6919fSthorpej 
361f28255Scgd /*
4290a2c4fScgd  * Copyright (c) 1989, 1993
5290a2c4fScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
158e6ab883Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32ee62445dSmrg #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3414c6919fSthorpej #if 0
3514c6919fSthorpej static char sccsid[] = "@(#)termstat.c	8.2 (Berkeley) 5/30/95";
3614c6919fSthorpej #else
37*4b901290Sperry __RCSID("$NetBSD: termstat.c,v 1.14 2005/02/06 05:58:21 perry Exp $");
3814c6919fSthorpej #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include "telnetd.h"
4261f28255Scgd 
43625dced9Sitojun #ifdef ENCRYPTION
445c099b14Sthorpej #include <libtelnet/encrypt.h>
455c099b14Sthorpej #endif
465c099b14Sthorpej 
4761f28255Scgd /*
4861f28255Scgd  * local variables
4961f28255Scgd  */
5061f28255Scgd int def_tspeed = -1, def_rspeed = -1;
5161f28255Scgd int def_row = 0, def_col = 0;
5261f28255Scgd #ifdef	LINEMODE
5361f28255Scgd static int _terminit = 0;
5461f28255Scgd #endif	/* LINEMODE */
5561f28255Scgd 
5661f28255Scgd 
5761f28255Scgd #ifdef	LINEMODE
5861f28255Scgd /*
5961f28255Scgd  * localstat
6061f28255Scgd  *
6161f28255Scgd  * This function handles all management of linemode.
6261f28255Scgd  *
6361f28255Scgd  * Linemode allows the client to do the local editing of data
6461f28255Scgd  * and send only complete lines to the server.  Linemode state is
6561f28255Scgd  * based on the state of the pty driver.  If the pty is set for
6661f28255Scgd  * external processing, then we can use linemode.  Further, if we
6761f28255Scgd  * can use real linemode, then we can look at the edit control bits
6861f28255Scgd  * in the pty to determine what editing the client should do.
6961f28255Scgd  *
7061f28255Scgd  * Linemode support uses the following state flags to keep track of
7161f28255Scgd  * current and desired linemode state.
7261f28255Scgd  *	alwayslinemode : true if -l was specified on the telnetd
7361f28255Scgd  * 	command line.  It means to have linemode on as much as
7461f28255Scgd  *	possible.
7561f28255Scgd  *
7661f28255Scgd  * 	lmodetype: signifies whether the client can
7761f28255Scgd  *	handle real linemode, or if use of kludgeomatic linemode
7861f28255Scgd  *	is preferred.  It will be set to one of the following:
7961f28255Scgd  *		REAL_LINEMODE : use linemode option
80290a2c4fScgd  *		NO_KLUDGE : don't initiate kludge linemode.
8161f28255Scgd  *		KLUDGE_LINEMODE : use kludge linemode
8261f28255Scgd  *		NO_LINEMODE : client is ignorant of linemode
8361f28255Scgd  *
8461f28255Scgd  *	linemode, uselinemode : linemode is true if linemode
8561f28255Scgd  *	is currently on, uselinemode is the state that we wish
8661f28255Scgd  *	to be in.  If another function wishes to turn linemode
8761f28255Scgd  *	on or off, it sets or clears uselinemode.
8861f28255Scgd  *
8961f28255Scgd  *	editmode, useeditmode : like linemode/uselinemode, but
9061f28255Scgd  *	these contain the edit mode states (edit and trapsig).
9161f28255Scgd  *
9261f28255Scgd  * The state variables correspond to some of the state information
9361f28255Scgd  * in the pty.
9461f28255Scgd  *	linemode:
9561f28255Scgd  *		In real linemode, this corresponds to whether the pty
9661f28255Scgd  *		expects external processing of incoming data.
9761f28255Scgd  *		In kludge linemode, this more closely corresponds to the
9861f28255Scgd  *		whether normal processing is on or not.  (ICANON in
9961f28255Scgd  *		system V, or COOKED mode in BSD.)
10061f28255Scgd  *		If the -l option was specified (alwayslinemode), then
10161f28255Scgd  *		an attempt is made to force external processing on at
10261f28255Scgd  *		all times.
10361f28255Scgd  *
10461f28255Scgd  * The following heuristics are applied to determine linemode
10561f28255Scgd  * handling within the server.
10661f28255Scgd  *	1) Early on in starting up the server, an attempt is made
10761f28255Scgd  *	   to negotiate the linemode option.  If this succeeds
10861f28255Scgd  *	   then lmodetype is set to REAL_LINEMODE and all linemode
10961f28255Scgd  *	   processing occurs in the context of the linemode option.
11061f28255Scgd  *	2) If the attempt to negotiate the linemode option failed,
111290a2c4fScgd  *	   and the "-k" (don't initiate kludge linemode) isn't set,
11261f28255Scgd  *	   then we try to use kludge linemode.  We test for this
11361f28255Scgd  *	   capability by sending "do Timing Mark".  If a positive
11461f28255Scgd  *	   response comes back, then we assume that the client
11561f28255Scgd  *	   understands kludge linemode (ech!) and the
11661f28255Scgd  *	   lmodetype flag is set to KLUDGE_LINEMODE.
11761f28255Scgd  *	3) Otherwise, linemode is not supported at all and
11861f28255Scgd  *	   lmodetype remains set to NO_LINEMODE (which happens
11961f28255Scgd  *	   to be 0 for convenience).
12061f28255Scgd  *	4) At any time a command arrives that implies a higher
12161f28255Scgd  *	   state of linemode support in the client, we move to that
12261f28255Scgd  *	   linemode support.
12361f28255Scgd  *
12461f28255Scgd  * A short explanation of kludge linemode is in order here.
12561f28255Scgd  *	1) The heuristic to determine support for kludge linemode
12661f28255Scgd  *	   is to send a do timing mark.  We assume that a client
12761f28255Scgd  *	   that supports timing marks also supports kludge linemode.
12861f28255Scgd  *	   A risky proposition at best.
12961f28255Scgd  *	2) Further negotiation of linemode is done by changing the
13061f28255Scgd  *	   the server's state regarding SGA.  If server will SGA,
13161f28255Scgd  *	   then linemode is off, if server won't SGA, then linemode
13261f28255Scgd  *	   is on.
13361f28255Scgd  */
13461f28255Scgd void
localstat(void)135*4b901290Sperry localstat(void)
13661f28255Scgd {
13761f28255Scgd 	int need_will_echo = 0;
13861f28255Scgd 
13961f28255Scgd 
14061f28255Scgd 	/*
14161f28255Scgd 	 * Check for state of BINARY options.
14261f28255Scgd 	 */
14361f28255Scgd 	if (tty_isbinaryin()) {
14461f28255Scgd 		if (his_want_state_is_wont(TELOPT_BINARY))
14561f28255Scgd 			send_do(TELOPT_BINARY, 1);
14661f28255Scgd 	} else {
14761f28255Scgd 		if (his_want_state_is_will(TELOPT_BINARY))
14861f28255Scgd 			send_dont(TELOPT_BINARY, 1);
14961f28255Scgd 	}
15061f28255Scgd 
15161f28255Scgd 	if (tty_isbinaryout()) {
15261f28255Scgd 		if (my_want_state_is_wont(TELOPT_BINARY))
15361f28255Scgd 			send_will(TELOPT_BINARY, 1);
15461f28255Scgd 	} else {
15561f28255Scgd 		if (my_want_state_is_will(TELOPT_BINARY))
15661f28255Scgd 			send_wont(TELOPT_BINARY, 1);
15761f28255Scgd 	}
15861f28255Scgd 
15961f28255Scgd 	/*
16061f28255Scgd 	 * Check for changes to flow control if client supports it.
16161f28255Scgd 	 */
162290a2c4fScgd 	flowstat();
16361f28255Scgd 
16461f28255Scgd 	/*
16561f28255Scgd 	 * Check linemode on/off state
16661f28255Scgd 	 */
16761f28255Scgd 	uselinemode = tty_linemode();
16861f28255Scgd 
16961f28255Scgd 	/*
17061f28255Scgd 	 * If alwayslinemode is on, and pty is changing to turn it off, then
17161f28255Scgd 	 * force linemode back on.
17261f28255Scgd 	 */
17361f28255Scgd 	if (alwayslinemode && linemode && !uselinemode) {
17461f28255Scgd 		uselinemode = 1;
17561f28255Scgd 		tty_setlinemode(uselinemode);
17661f28255Scgd 	}
17761f28255Scgd 
1785c099b14Sthorpej #ifdef	ENCRYPTION
1795c099b14Sthorpej 	/*
1805c099b14Sthorpej 	 * If the terminal is not echoing, but editing is enabled,
1815c099b14Sthorpej 	 * something like password input is going to happen, so
1825c099b14Sthorpej 	 * if we the other side is not currently sending encrypted
1835c099b14Sthorpej 	 * data, ask the other side to start encrypting.
1845c099b14Sthorpej 	 */
1855c099b14Sthorpej 	if (his_state_is_will(TELOPT_ENCRYPT)) {
1865c099b14Sthorpej 		static int enc_passwd = 0;
1875c099b14Sthorpej 		if (uselinemode && !tty_isecho() && tty_isediting()
1885c099b14Sthorpej 		    && (enc_passwd == 0) && !decrypt_input) {
1895c099b14Sthorpej 			encrypt_send_request_start();
1905c099b14Sthorpej 			enc_passwd = 1;
1915c099b14Sthorpej 		} else if (enc_passwd) {
1925c099b14Sthorpej 			encrypt_send_request_end();
1935c099b14Sthorpej 			enc_passwd = 0;
1945c099b14Sthorpej 		}
1955c099b14Sthorpej 	}
1965c099b14Sthorpej #endif	/* ENCRYPTION */
19761f28255Scgd 
19861f28255Scgd 	/*
19961f28255Scgd 	 * Do echo mode handling as soon as we know what the
20061f28255Scgd 	 * linemode is going to be.
20161f28255Scgd 	 * If the pty has echo turned off, then tell the client that
20261f28255Scgd 	 * the server will echo.  If echo is on, then the server
20361f28255Scgd 	 * will echo if in character mode, but in linemode the
20461f28255Scgd 	 * client should do local echoing.  The state machine will
20561f28255Scgd 	 * not send anything if it is unnecessary, so don't worry
20661f28255Scgd 	 * about that here.
20761f28255Scgd 	 *
20861f28255Scgd 	 * If we need to send the WILL ECHO (because echo is off),
20961f28255Scgd 	 * then delay that until after we have changed the MODE.
21061f28255Scgd 	 * This way, when the user is turning off both editing
21161f28255Scgd 	 * and echo, the client will get editing turned off first.
21261f28255Scgd 	 * This keeps the client from going into encryption mode
21361f28255Scgd 	 * and then right back out if it is doing auto-encryption
21461f28255Scgd 	 * when passwords are being typed.
21561f28255Scgd 	 */
21661f28255Scgd 	if (uselinemode) {
21761f28255Scgd 		if (tty_isecho())
21861f28255Scgd 			send_wont(TELOPT_ECHO, 1);
21961f28255Scgd 		else
22061f28255Scgd 			need_will_echo = 1;
221290a2c4fScgd #ifdef	KLUDGELINEMODE
222290a2c4fScgd 		if (lmodetype == KLUDGE_OK)
223290a2c4fScgd 			lmodetype = KLUDGE_LINEMODE;
224290a2c4fScgd #endif
22561f28255Scgd 	}
22661f28255Scgd 
22761f28255Scgd 	/*
22861f28255Scgd 	 * If linemode is being turned off, send appropriate
22961f28255Scgd 	 * command and then we're all done.
23061f28255Scgd 	 */
23161f28255Scgd 	 if (!uselinemode && linemode) {
23261f28255Scgd # ifdef	KLUDGELINEMODE
23361f28255Scgd 		if (lmodetype == REAL_LINEMODE) {
23461f28255Scgd # endif	/* KLUDGELINEMODE */
23561f28255Scgd 			send_dont(TELOPT_LINEMODE, 1);
23661f28255Scgd # ifdef	KLUDGELINEMODE
23761f28255Scgd 		} else if (lmodetype == KLUDGE_LINEMODE)
23861f28255Scgd 			send_will(TELOPT_SGA, 1);
23961f28255Scgd # endif	/* KLUDGELINEMODE */
24061f28255Scgd 		send_will(TELOPT_ECHO, 1);
24161f28255Scgd 		linemode = uselinemode;
24261f28255Scgd 		goto done;
24361f28255Scgd 	}
24461f28255Scgd 
24561f28255Scgd # ifdef	KLUDGELINEMODE
24661f28255Scgd 	/*
24761f28255Scgd 	 * If using real linemode check edit modes for possible later use.
24861f28255Scgd 	 * If we are in kludge linemode, do the SGA negotiation.
24961f28255Scgd 	 */
25061f28255Scgd 	if (lmodetype == REAL_LINEMODE) {
25161f28255Scgd # endif	/* KLUDGELINEMODE */
25261f28255Scgd 		useeditmode = 0;
25361f28255Scgd 		if (tty_isediting())
25461f28255Scgd 			useeditmode |= MODE_EDIT;
25561f28255Scgd 		if (tty_istrapsig())
25661f28255Scgd 			useeditmode |= MODE_TRAPSIG;
25761f28255Scgd 		if (tty_issofttab())
25861f28255Scgd 			useeditmode |= MODE_SOFT_TAB;
25961f28255Scgd 		if (tty_islitecho())
26061f28255Scgd 			useeditmode |= MODE_LIT_ECHO;
26161f28255Scgd # ifdef	KLUDGELINEMODE
26261f28255Scgd 	} else if (lmodetype == KLUDGE_LINEMODE) {
26361f28255Scgd 		if (tty_isediting() && uselinemode)
26461f28255Scgd 			send_wont(TELOPT_SGA, 1);
26561f28255Scgd 		else
26661f28255Scgd 			send_will(TELOPT_SGA, 1);
26761f28255Scgd 	}
26861f28255Scgd # endif	/* KLUDGELINEMODE */
26961f28255Scgd 
27061f28255Scgd 	/*
27161f28255Scgd 	 * Negotiate linemode on if pty state has changed to turn it on.
27261f28255Scgd 	 * Send appropriate command and send along edit mode, then all done.
27361f28255Scgd 	 */
27461f28255Scgd 	if (uselinemode && !linemode) {
27561f28255Scgd # ifdef	KLUDGELINEMODE
27661f28255Scgd 		if (lmodetype == KLUDGE_LINEMODE) {
27761f28255Scgd 			send_wont(TELOPT_SGA, 1);
27861f28255Scgd 		} else if (lmodetype == REAL_LINEMODE) {
27961f28255Scgd # endif	/* KLUDGELINEMODE */
28061f28255Scgd 			send_do(TELOPT_LINEMODE, 1);
28161f28255Scgd 			/* send along edit modes */
282eb2e3cb6Sitojun 			(void) output_data("%c%c%c%c%c%c%c", IAC, SB,
28361f28255Scgd 				TELOPT_LINEMODE, LM_MODE, useeditmode,
28461f28255Scgd 				IAC, SE);
28561f28255Scgd 			editmode = useeditmode;
28661f28255Scgd # ifdef	KLUDGELINEMODE
28761f28255Scgd 		}
28861f28255Scgd # endif	/* KLUDGELINEMODE */
28961f28255Scgd 		linemode = uselinemode;
29061f28255Scgd 		goto done;
29161f28255Scgd 	}
29261f28255Scgd 
29361f28255Scgd # ifdef	KLUDGELINEMODE
29461f28255Scgd 	/*
29561f28255Scgd 	 * None of what follows is of any value if not using
29661f28255Scgd 	 * real linemode.
29761f28255Scgd 	 */
29861f28255Scgd 	if (lmodetype < REAL_LINEMODE)
29961f28255Scgd 		goto done;
30061f28255Scgd # endif	/* KLUDGELINEMODE */
30161f28255Scgd 
30261f28255Scgd 	if (linemode && his_state_is_will(TELOPT_LINEMODE)) {
30361f28255Scgd 		/*
30461f28255Scgd 		 * If edit mode changed, send edit mode.
30561f28255Scgd 		 */
30661f28255Scgd 		 if (useeditmode != editmode) {
30761f28255Scgd 			/*
30861f28255Scgd 			 * Send along appropriate edit mode mask.
30961f28255Scgd 			 */
310eb2e3cb6Sitojun 			(void) output_data("%c%c%c%c%c%c%c", IAC, SB,
31161f28255Scgd 				TELOPT_LINEMODE, LM_MODE, useeditmode,
31261f28255Scgd 				IAC, SE);
31361f28255Scgd 			editmode = useeditmode;
31461f28255Scgd 		}
31561f28255Scgd 
31661f28255Scgd 
31761f28255Scgd 		/*
31861f28255Scgd 		 * Check for changes to special characters in use.
31961f28255Scgd 		 */
32061f28255Scgd 		start_slc(0);
32161f28255Scgd 		check_slc();
32261f28255Scgd 		(void) end_slc(0);
32361f28255Scgd 	}
32461f28255Scgd 
32561f28255Scgd done:
32661f28255Scgd 	if (need_will_echo)
32761f28255Scgd 		send_will(TELOPT_ECHO, 1);
32861f28255Scgd 	/*
32961f28255Scgd 	 * Some things should be deferred until after the pty state has
33061f28255Scgd 	 * been set by the local process.  Do those things that have been
33161f28255Scgd 	 * deferred now.  This only happens once.
33261f28255Scgd 	 */
33361f28255Scgd 	if (_terminit == 0) {
33461f28255Scgd 		_terminit = 1;
33561f28255Scgd 		defer_terminit();
33661f28255Scgd 	}
33761f28255Scgd 
33861f28255Scgd 	netflush();
33961f28255Scgd 	set_termbuf();
34061f28255Scgd 	return;
34161f28255Scgd 
34261f28255Scgd }  /* end of localstat */
34361f28255Scgd #endif	/* LINEMODE */
34461f28255Scgd 
345290a2c4fScgd /*
346290a2c4fScgd  * flowstat
347290a2c4fScgd  *
348290a2c4fScgd  * Check for changes to flow control
349290a2c4fScgd  */
350290a2c4fScgd void
flowstat(void)351*4b901290Sperry flowstat(void)
352290a2c4fScgd {
353290a2c4fScgd 	if (his_state_is_will(TELOPT_LFLOW)) {
354290a2c4fScgd 		if (tty_flowmode() != flowmode) {
355290a2c4fScgd 			flowmode = tty_flowmode();
356eb2e3cb6Sitojun 			(void) output_data("%c%c%c%c%c%c",
357290a2c4fScgd 					IAC, SB, TELOPT_LFLOW,
358290a2c4fScgd 					flowmode ? LFLOW_ON : LFLOW_OFF,
359290a2c4fScgd 					IAC, SE);
360290a2c4fScgd 		}
361290a2c4fScgd 		if (tty_restartany() != restartany) {
362290a2c4fScgd 			restartany = tty_restartany();
363eb2e3cb6Sitojun 			(void) output_data("%c%c%c%c%c%c",
364290a2c4fScgd 					IAC, SB, TELOPT_LFLOW,
365290a2c4fScgd 					restartany ? LFLOW_RESTART_ANY
366290a2c4fScgd 						   : LFLOW_RESTART_XON,
367290a2c4fScgd 					IAC, SE);
368290a2c4fScgd 		}
369290a2c4fScgd 	}
370290a2c4fScgd }
37161f28255Scgd 
37261f28255Scgd /*
37361f28255Scgd  * clientstat
37461f28255Scgd  *
37561f28255Scgd  * Process linemode related requests from the client.
37661f28255Scgd  * Client can request a change to only one of linemode, editmode or slc's
37761f28255Scgd  * at a time, and if using kludge linemode, then only linemode may be
37861f28255Scgd  * affected.
37961f28255Scgd  */
38061f28255Scgd void
clientstat(int code,int parm1,int parm2)381*4b901290Sperry clientstat(int code, int parm1, int parm2)
38261f28255Scgd {
38361f28255Scgd 
38461f28255Scgd 	/*
38561f28255Scgd 	 * Get a copy of terminal characteristics.
38661f28255Scgd 	 */
38761f28255Scgd 	init_termbuf();
38861f28255Scgd 
38961f28255Scgd 	/*
39061f28255Scgd 	 * Process request from client. code tells what it is.
39161f28255Scgd 	 */
39261f28255Scgd 	switch (code) {
39361f28255Scgd #ifdef	LINEMODE
39461f28255Scgd 	case TELOPT_LINEMODE:
39561f28255Scgd 		/*
39661f28255Scgd 		 * Don't do anything unless client is asking us to change
39761f28255Scgd 		 * modes.
39861f28255Scgd 		 */
39961f28255Scgd 		uselinemode = (parm1 == WILL);
40061f28255Scgd 		if (uselinemode != linemode) {
40161f28255Scgd # ifdef	KLUDGELINEMODE
40261f28255Scgd 			/*
40361f28255Scgd 			 * If using kludge linemode, make sure that
40461f28255Scgd 			 * we can do what the client asks.
40561f28255Scgd 			 * We can not turn off linemode if alwayslinemode
40661f28255Scgd 			 * and the ICANON bit is set.
40761f28255Scgd 			 */
40861f28255Scgd 			if (lmodetype == KLUDGE_LINEMODE) {
40961f28255Scgd 				if (alwayslinemode && tty_isediting()) {
41061f28255Scgd 					uselinemode = 1;
41161f28255Scgd 				}
41261f28255Scgd 			}
41361f28255Scgd 
41461f28255Scgd 			/*
41561f28255Scgd 			 * Quit now if we can't do it.
41661f28255Scgd 			 */
41761f28255Scgd 			if (uselinemode == linemode)
41861f28255Scgd 				return;
41961f28255Scgd 
42061f28255Scgd 			/*
42161f28255Scgd 			 * If using real linemode and linemode is being
42261f28255Scgd 			 * turned on, send along the edit mode mask.
42361f28255Scgd 			 */
42461f28255Scgd 			if (lmodetype == REAL_LINEMODE && uselinemode)
42561f28255Scgd # else	/* KLUDGELINEMODE */
42661f28255Scgd 			if (uselinemode)
42761f28255Scgd # endif	/* KLUDGELINEMODE */
42861f28255Scgd 			{
42961f28255Scgd 				useeditmode = 0;
43061f28255Scgd 				if (tty_isediting())
43161f28255Scgd 					useeditmode |= MODE_EDIT;
432f79a6c5dSwiz 				if (tty_istrapsig())
43361f28255Scgd 					useeditmode |= MODE_TRAPSIG;
43461f28255Scgd 				if (tty_issofttab())
43561f28255Scgd 					useeditmode |= MODE_SOFT_TAB;
43661f28255Scgd 				if (tty_islitecho())
43761f28255Scgd 					useeditmode |= MODE_LIT_ECHO;
438eb2e3cb6Sitojun 				(void) output_data("%c%c%c%c%c%c%c", IAC,
43961f28255Scgd 					SB, TELOPT_LINEMODE, LM_MODE,
44061f28255Scgd 							useeditmode, IAC, SE);
44161f28255Scgd 				editmode = useeditmode;
44261f28255Scgd 			}
44361f28255Scgd 
44461f28255Scgd 
44561f28255Scgd 			tty_setlinemode(uselinemode);
44661f28255Scgd 
44761f28255Scgd 			linemode = uselinemode;
44861f28255Scgd 
449290a2c4fScgd 			if (!linemode)
450290a2c4fScgd 				send_will(TELOPT_ECHO, 1);
45161f28255Scgd 		}
45261f28255Scgd 		break;
45361f28255Scgd 
45461f28255Scgd 	case LM_MODE:
45561f28255Scgd 	    {
456*4b901290Sperry 		int ack, changed;
45761f28255Scgd 
45861f28255Scgd 		/*
45961f28255Scgd 		 * Client has sent along a mode mask.  If it agrees with
46061f28255Scgd 		 * what we are currently doing, ignore it; if not, it could
46161f28255Scgd 		 * be viewed as a request to change.  Note that the server
46261f28255Scgd 		 * will change to the modes in an ack if it is different from
46361f28255Scgd 		 * what we currently have, but we will not ack the ack.
46461f28255Scgd 		 */
46561f28255Scgd 		 useeditmode &= MODE_MASK;
46661f28255Scgd 		 ack = (useeditmode & MODE_ACK);
46761f28255Scgd 		 useeditmode &= ~MODE_ACK;
46861f28255Scgd 
469ee62445dSmrg 		 if ((changed = (useeditmode ^ editmode))) {
47061f28255Scgd 			/*
47161f28255Scgd 			 * This check is for a timing problem.  If the
47261f28255Scgd 			 * state of the tty has changed (due to the user
47361f28255Scgd 			 * application) we need to process that info
47461f28255Scgd 			 * before we write in the state contained in the
47561f28255Scgd 			 * ack!!!  This gets out the new MODE request,
47661f28255Scgd 			 * and when the ack to that command comes back
47761f28255Scgd 			 * we'll set it and be in the right mode.
47861f28255Scgd 			 */
47961f28255Scgd 			if (ack)
48061f28255Scgd 				localstat();
48161f28255Scgd 			if (changed & MODE_EDIT)
48261f28255Scgd 				tty_setedit(useeditmode & MODE_EDIT);
48361f28255Scgd 
48461f28255Scgd 			if (changed & MODE_TRAPSIG)
48561f28255Scgd 				tty_setsig(useeditmode & MODE_TRAPSIG);
48661f28255Scgd 
48761f28255Scgd 			if (changed & MODE_SOFT_TAB)
48861f28255Scgd 				tty_setsofttab(useeditmode & MODE_SOFT_TAB);
48961f28255Scgd 
49061f28255Scgd 			if (changed & MODE_LIT_ECHO)
49161f28255Scgd 				tty_setlitecho(useeditmode & MODE_LIT_ECHO);
49261f28255Scgd 
49361f28255Scgd 			set_termbuf();
49461f28255Scgd 
49561f28255Scgd  			if (!ack) {
496eb2e3cb6Sitojun  				(void) output_data("%c%c%c%c%c%c%c", IAC,
49761f28255Scgd 					SB, TELOPT_LINEMODE, LM_MODE,
49861f28255Scgd  					useeditmode|MODE_ACK,
49961f28255Scgd  					IAC, SE);
50061f28255Scgd  			}
50161f28255Scgd 
50261f28255Scgd 			editmode = useeditmode;
50361f28255Scgd 		}
50461f28255Scgd 
50561f28255Scgd 		break;
50661f28255Scgd 
50761f28255Scgd 	    }  /* end of case LM_MODE */
50861f28255Scgd #endif	/* LINEMODE */
50961f28255Scgd 
51061f28255Scgd 	case TELOPT_NAWS:
51161f28255Scgd 	    {
51261f28255Scgd 		struct winsize ws;
51361f28255Scgd 
51461f28255Scgd 		def_col = parm1;
51561f28255Scgd 		def_row = parm2;
51661f28255Scgd #ifdef	LINEMODE
51761f28255Scgd 		/*
51861f28255Scgd 		 * Defer changing window size until after terminal is
51961f28255Scgd 		 * initialized.
52061f28255Scgd 		 */
52161f28255Scgd 		if (terminit() == 0)
52261f28255Scgd 			return;
52361f28255Scgd #endif	/* LINEMODE */
52461f28255Scgd 
52561f28255Scgd 		/*
52661f28255Scgd 		 * Change window size as requested by client.
52761f28255Scgd 		 */
52861f28255Scgd 
52961f28255Scgd 		ws.ws_col = parm1;
53061f28255Scgd 		ws.ws_row = parm2;
53161f28255Scgd 		(void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
53261f28255Scgd 	    }
53361f28255Scgd 
53461f28255Scgd 		break;
53561f28255Scgd 
53661f28255Scgd 	case TELOPT_TSPEED:
53761f28255Scgd 	    {
53861f28255Scgd 		def_tspeed = parm1;
53961f28255Scgd 		def_rspeed = parm2;
54061f28255Scgd #ifdef	LINEMODE
54161f28255Scgd 		/*
54261f28255Scgd 		 * Defer changing the terminal speed.
54361f28255Scgd 		 */
54461f28255Scgd 		if (terminit() == 0)
54561f28255Scgd 			return;
54661f28255Scgd #endif	/* LINEMODE */
54761f28255Scgd 		/*
54861f28255Scgd 		 * Change terminal speed as requested by client.
54961f28255Scgd 		 * We set the receive speed first, so that if we can't
550a9356936Swiz 		 * store separate receive and transmit speeds, the transmit
55161f28255Scgd 		 * speed will take precedence.
55261f28255Scgd 		 */
55361f28255Scgd 		tty_rspeed(parm2);
55461f28255Scgd 		tty_tspeed(parm1);
55561f28255Scgd 		set_termbuf();
55661f28255Scgd 
55761f28255Scgd 		break;
55861f28255Scgd 
55961f28255Scgd 	    }  /* end of case TELOPT_TSPEED */
56061f28255Scgd 
56161f28255Scgd 	default:
56261f28255Scgd 		/* What? */
56361f28255Scgd 		break;
56461f28255Scgd 	}  /* end of switch */
56561f28255Scgd 
56661f28255Scgd 
56761f28255Scgd 	netflush();
56861f28255Scgd 
56961f28255Scgd }  /* end of clientstat */
57061f28255Scgd 
57161f28255Scgd 
57261f28255Scgd #ifdef	LINEMODE
57361f28255Scgd /*
57461f28255Scgd  * defer_terminit
57561f28255Scgd  *
57661f28255Scgd  * Some things should not be done until after the login process has started
57761f28255Scgd  * and all the pty modes are set to what they are supposed to be.  This
57861f28255Scgd  * function is called when the pty state has been processed for the first time.
57961f28255Scgd  * It calls other functions that do things that were deferred in each module.
58061f28255Scgd  */
58161f28255Scgd void
defer_terminit(void)582*4b901290Sperry defer_terminit(void)
58361f28255Scgd {
58461f28255Scgd 
58561f28255Scgd 	/*
58661f28255Scgd 	 * local stuff that got deferred.
58761f28255Scgd 	 */
58861f28255Scgd 	if (def_tspeed != -1) {
58961f28255Scgd 		clientstat(TELOPT_TSPEED, def_tspeed, def_rspeed);
59061f28255Scgd 		def_tspeed = def_rspeed = 0;
59161f28255Scgd 	}
59261f28255Scgd 
59361f28255Scgd 	if (def_col || def_row) {
59461f28255Scgd 		struct winsize ws;
59561f28255Scgd 
59642e1b6b7Sjtk 		memset((char *)&ws, 0, sizeof(ws));
59761f28255Scgd 		ws.ws_col = def_col;
59861f28255Scgd 		ws.ws_row = def_row;
59961f28255Scgd 		(void) ioctl(pty, TIOCSWINSZ, (char *)&ws);
60061f28255Scgd 	}
60161f28255Scgd 
60261f28255Scgd 	/*
60361f28255Scgd 	 * The only other module that currently defers anything.
60461f28255Scgd 	 */
60561f28255Scgd 	deferslc();
60661f28255Scgd 
60761f28255Scgd }  /* end of defer_terminit */
60861f28255Scgd 
60961f28255Scgd /*
61061f28255Scgd  * terminit
61161f28255Scgd  *
61261f28255Scgd  * Returns true if the pty state has been processed yet.
61361f28255Scgd  */
61461f28255Scgd int
terminit(void)615*4b901290Sperry terminit(void)
61661f28255Scgd {
617290a2c4fScgd 	return(_terminit);
61861f28255Scgd 
61961f28255Scgd }  /* end of terminit */
62061f28255Scgd #endif	/* LINEMODE */
621