xref: /netbsd-src/usr.bin/telnet/terminal.c (revision 4e424770ac2ba0a6ea3c40ec9a8d775dab9b7e82)
1*4e424770Smaya /*	$NetBSD: terminal.c,v 1.16 2018/12/14 06:08:18 maya Exp $	*/
2077a490aSthorpej 
361f28255Scgd /*
40582c913Scgd  * Copyright (c) 1988, 1990, 1993
50582c913Scgd  *	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.
1589aaa1bbSagc  * 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 
32434ea11bSchristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34077a490aSthorpej #if 0
35077a490aSthorpej static char sccsid[] = "@(#)terminal.c	8.2 (Berkeley) 2/16/95";
36077a490aSthorpej #else
37*4e424770Smaya __RCSID("$NetBSD: terminal.c,v 1.16 2018/12/14 06:08:18 maya Exp $");
38077a490aSthorpej #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include <arpa/telnet.h>
4261f28255Scgd #include <sys/types.h>
4361f28255Scgd 
4461f28255Scgd #include "ring.h"
4561f28255Scgd 
4661f28255Scgd #include "externs.h"
4761f28255Scgd #include "types.h"
4861f28255Scgd 
495c099b14Sthorpej #ifdef ENCRYPTION
505c099b14Sthorpej #include <libtelnet/encrypt.h>
515c099b14Sthorpej #endif
525c099b14Sthorpej 
5361f28255Scgd Ring		ttyoring, ttyiring;
5461f28255Scgd unsigned char	ttyobuf[2*BUFSIZ], ttyibuf[BUFSIZ];
5566e1a2c6Schristos char line[] = { '\0' };
5661f28255Scgd 
5761f28255Scgd int termdata;			/* Debugging flag */
5861f28255Scgd 
5961f28255Scgd /*
6061f28255Scgd  * initialize the terminal data structures.
6161f28255Scgd  */
6261f28255Scgd 
6361f28255Scgd void
init_terminal(void)6426583868Schristos init_terminal(void)
6561f28255Scgd {
6661f28255Scgd     if (ring_init(&ttyoring, ttyobuf, sizeof ttyobuf) != 1) {
6761f28255Scgd 	exit(1);
6861f28255Scgd     }
6961f28255Scgd     if (ring_init(&ttyiring, ttyibuf, sizeof ttyibuf) != 1) {
7061f28255Scgd 	exit(1);
7161f28255Scgd     }
7261f28255Scgd     autoflush = TerminalAutoFlush();
7361f28255Scgd }
7461f28255Scgd 
7561f28255Scgd 
7661f28255Scgd /*
77209ebbabSheas  *		Send as much data as possible to the terminal, else exits if
78209ebbabSheas  *		it encounters a permanent failure when writing to the tty.
7961f28255Scgd  *
8061f28255Scgd  *		Return value:
8161f28255Scgd  *			-1: No useful work done, data waiting to go out.
8261f28255Scgd  *			 0: No data was waiting, so nothing was done.
8361f28255Scgd  *			 1: All waiting data was written out.
8461f28255Scgd  *			 n: All data - n was written out.
8561f28255Scgd  */
8661f28255Scgd 
8761f28255Scgd 
8861f28255Scgd int
ttyflush(int drop)8926583868Schristos ttyflush(int drop)
9061f28255Scgd {
91797d779cSwiz     int n, n0, n1;
9261f28255Scgd 
9361f28255Scgd     n0 = ring_full_count(&ttyoring);
9461f28255Scgd     if ((n1 = n = ring_full_consecutive(&ttyoring)) > 0) {
9561f28255Scgd 	if (drop) {
9661f28255Scgd 	    TerminalFlushOutput();
9761f28255Scgd 	    /* we leave 'n' alone! */
9861f28255Scgd 	} else {
9961f28255Scgd 	    n = TerminalWrite(ttyoring.consume, n);
10061f28255Scgd 	}
10161f28255Scgd     }
10261f28255Scgd     if (n > 0) {
10361f28255Scgd 	if (termdata && n) {
10461f28255Scgd 	    Dump('>', ttyoring.consume, n);
10561f28255Scgd 	}
10661f28255Scgd 	/*
10761f28255Scgd 	 * If we wrote everything, and the full count is
10861f28255Scgd 	 * larger than what we wrote, then write the
10961f28255Scgd 	 * rest of the buffer.
11061f28255Scgd 	 */
11161f28255Scgd 	if (n1 == n && n0 > n) {
11261f28255Scgd 		n1 = n0 - n;
11361f28255Scgd 		if (!drop)
11461f28255Scgd 			n1 = TerminalWrite(ttyoring.bottom, n1);
115583a8146Sjtk 		if (n1 > 0)
11661f28255Scgd 			n += n1;
11761f28255Scgd 	}
11861f28255Scgd 	ring_consumed(&ttyoring, n);
11961f28255Scgd     }
120be6d6ca0Schristos     if (n < 0) {
121209ebbabSheas 	if (errno == EAGAIN || errno == EINTR) {
12261f28255Scgd 	    return -1;
123209ebbabSheas 	} else {
124209ebbabSheas 	    ring_consumed(&ttyoring, ring_full_count(&ttyoring));
125209ebbabSheas 	    setconnmode(0);
126209ebbabSheas 	    setcommandmode();
127209ebbabSheas 	    NetClose(net);
128209ebbabSheas 	    fprintf(stderr, "Connection closed by foreign host.\n");
129209ebbabSheas 	    exit(1);
130209ebbabSheas 	}
131be6d6ca0Schristos     }
13261f28255Scgd     if (n == n0) {
13361f28255Scgd 	if (n0)
13461f28255Scgd 	    return -1;
13561f28255Scgd 	return 0;
13661f28255Scgd     }
13761f28255Scgd     return n0 - n + 1;
13861f28255Scgd }
13961f28255Scgd 
14061f28255Scgd 
14161f28255Scgd /*
14261f28255Scgd  * These routines decides on what the mode should be (based on the values
14361f28255Scgd  * of various global variables).
14461f28255Scgd  */
14561f28255Scgd 
14661f28255Scgd 
14761f28255Scgd int
getconnmode(void)14826583868Schristos getconnmode(void)
14961f28255Scgd {
15061f28255Scgd     extern int linemode;
15161f28255Scgd     int mode = 0;
15261f28255Scgd #ifdef	KLUDGELINEMODE
15361f28255Scgd     extern int kludgelinemode;
15461f28255Scgd #endif
15561f28255Scgd 
15661f28255Scgd     if (my_want_state_is_dont(TELOPT_ECHO))
15761f28255Scgd 	mode |= MODE_ECHO;
15861f28255Scgd 
15961f28255Scgd     if (localflow)
16061f28255Scgd 	mode |= MODE_FLOW;
16161f28255Scgd 
16261f28255Scgd     if (my_want_state_is_will(TELOPT_BINARY))
16361f28255Scgd 	mode |= MODE_INBIN;
16461f28255Scgd 
16561f28255Scgd     if (his_want_state_is_will(TELOPT_BINARY))
16661f28255Scgd 	mode |= MODE_OUTBIN;
16761f28255Scgd 
16861f28255Scgd #ifdef	KLUDGELINEMODE
16961f28255Scgd     if (kludgelinemode) {
17061f28255Scgd 	if (my_want_state_is_dont(TELOPT_SGA)) {
17161f28255Scgd 	    mode |= (MODE_TRAPSIG|MODE_EDIT);
17261f28255Scgd 	    if (dontlecho && (clocks.echotoggle > clocks.modenegotiated)) {
17361f28255Scgd 		mode &= ~MODE_ECHO;
17461f28255Scgd 	    }
17561f28255Scgd 	}
17661f28255Scgd 	return(mode);
17761f28255Scgd     }
17861f28255Scgd #endif
17961f28255Scgd     if (my_want_state_is_will(TELOPT_LINEMODE))
18061f28255Scgd 	mode |= linemode;
18161f28255Scgd     return(mode);
18261f28255Scgd }
18361f28255Scgd 
18461f28255Scgd void
setconnmode(int force)18526583868Schristos setconnmode(int force)
18661f28255Scgd {
1875c099b14Sthorpej #ifdef	ENCRYPTION
1885c099b14Sthorpej     static int enc_passwd = 0;
1895c099b14Sthorpej #endif
190797d779cSwiz     int newmode;
19161f28255Scgd 
19261f28255Scgd     newmode = getconnmode()|(force?MODE_FORCE:0);
19361f28255Scgd 
19461f28255Scgd     TerminalNewMode(newmode);
19561f28255Scgd 
1965c099b14Sthorpej #ifdef	ENCRYPTION
1975c099b14Sthorpej     if ((newmode & (MODE_ECHO|MODE_EDIT)) == MODE_EDIT) {
1985c099b14Sthorpej 	if (my_want_state_is_will(TELOPT_ENCRYPT)
1995c099b14Sthorpej 				&& (enc_passwd == 0) && !encrypt_output) {
2005c099b14Sthorpej 	    encrypt_request_start(0, 0);
2015c099b14Sthorpej 	    enc_passwd = 1;
2025c099b14Sthorpej 	}
2035c099b14Sthorpej     } else {
2045c099b14Sthorpej 	if (enc_passwd) {
2055c099b14Sthorpej 	    encrypt_request_end();
2065c099b14Sthorpej 	    enc_passwd = 0;
2075c099b14Sthorpej 	}
2085c099b14Sthorpej     }
2095c099b14Sthorpej #endif	/* ENCRYPTION */
21061f28255Scgd }
21161f28255Scgd 
21261f28255Scgd 
21361f28255Scgd void
setcommandmode(void)21426583868Schristos setcommandmode(void)
21561f28255Scgd {
21661f28255Scgd     TerminalNewMode(-1);
21761f28255Scgd }
218