xref: /dragonfly/crypto/openssh/sshtty.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* $OpenBSD: sshtty.c,v 1.14 2010/01/09 05:04:24 djm Exp $ */
2*86d7f5d3SJohn Marino /*
3*86d7f5d3SJohn Marino  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4*86d7f5d3SJohn Marino  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5*86d7f5d3SJohn Marino  *                    All rights reserved
6*86d7f5d3SJohn Marino  *
7*86d7f5d3SJohn Marino  * As far as I am concerned, the code I have written for this software
8*86d7f5d3SJohn Marino  * can be used freely for any purpose.  Any derived versions of this
9*86d7f5d3SJohn Marino  * software must be clearly marked as such, and if the derived work is
10*86d7f5d3SJohn Marino  * incompatible with the protocol description in the RFC file, it must be
11*86d7f5d3SJohn Marino  * called by a name other than "ssh" or "Secure Shell".
12*86d7f5d3SJohn Marino  */
13*86d7f5d3SJohn Marino /*
14*86d7f5d3SJohn Marino  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
15*86d7f5d3SJohn Marino  * Copyright (c) 2001 Kevin Steves.  All rights reserved.
16*86d7f5d3SJohn Marino  *
17*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
18*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
19*86d7f5d3SJohn Marino  * are met:
20*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
21*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
22*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
23*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
24*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
25*86d7f5d3SJohn Marino  *
26*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*86d7f5d3SJohn Marino  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*86d7f5d3SJohn Marino  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30*86d7f5d3SJohn Marino  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31*86d7f5d3SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*86d7f5d3SJohn Marino  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*86d7f5d3SJohn Marino  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*86d7f5d3SJohn Marino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*86d7f5d3SJohn Marino  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*86d7f5d3SJohn Marino  */
37*86d7f5d3SJohn Marino 
38*86d7f5d3SJohn Marino #include "includes.h"
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino #include <sys/types.h>
41*86d7f5d3SJohn Marino #include <stdio.h>
42*86d7f5d3SJohn Marino #include <termios.h>
43*86d7f5d3SJohn Marino #include <pwd.h>
44*86d7f5d3SJohn Marino 
45*86d7f5d3SJohn Marino #include "sshpty.h"
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino static struct termios _saved_tio;
48*86d7f5d3SJohn Marino static int _in_raw_mode = 0;
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino struct termios *
get_saved_tio(void)51*86d7f5d3SJohn Marino get_saved_tio(void)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino 	return _in_raw_mode ? &_saved_tio : NULL;
54*86d7f5d3SJohn Marino }
55*86d7f5d3SJohn Marino 
56*86d7f5d3SJohn Marino void
leave_raw_mode(int quiet)57*86d7f5d3SJohn Marino leave_raw_mode(int quiet)
58*86d7f5d3SJohn Marino {
59*86d7f5d3SJohn Marino 	if (!_in_raw_mode)
60*86d7f5d3SJohn Marino 		return;
61*86d7f5d3SJohn Marino 	if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1) {
62*86d7f5d3SJohn Marino 		if (!quiet)
63*86d7f5d3SJohn Marino 			perror("tcsetattr");
64*86d7f5d3SJohn Marino 	} else
65*86d7f5d3SJohn Marino 		_in_raw_mode = 0;
66*86d7f5d3SJohn Marino }
67*86d7f5d3SJohn Marino 
68*86d7f5d3SJohn Marino void
enter_raw_mode(int quiet)69*86d7f5d3SJohn Marino enter_raw_mode(int quiet)
70*86d7f5d3SJohn Marino {
71*86d7f5d3SJohn Marino 	struct termios tio;
72*86d7f5d3SJohn Marino 
73*86d7f5d3SJohn Marino 	if (tcgetattr(fileno(stdin), &tio) == -1) {
74*86d7f5d3SJohn Marino 		if (!quiet)
75*86d7f5d3SJohn Marino 			perror("tcgetattr");
76*86d7f5d3SJohn Marino 		return;
77*86d7f5d3SJohn Marino 	}
78*86d7f5d3SJohn Marino 	_saved_tio = tio;
79*86d7f5d3SJohn Marino 	tio.c_iflag |= IGNPAR;
80*86d7f5d3SJohn Marino 	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
81*86d7f5d3SJohn Marino #ifdef IUCLC
82*86d7f5d3SJohn Marino 	tio.c_iflag &= ~IUCLC;
83*86d7f5d3SJohn Marino #endif
84*86d7f5d3SJohn Marino 	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
85*86d7f5d3SJohn Marino #ifdef IEXTEN
86*86d7f5d3SJohn Marino 	tio.c_lflag &= ~IEXTEN;
87*86d7f5d3SJohn Marino #endif
88*86d7f5d3SJohn Marino 	tio.c_oflag &= ~OPOST;
89*86d7f5d3SJohn Marino 	tio.c_cc[VMIN] = 1;
90*86d7f5d3SJohn Marino 	tio.c_cc[VTIME] = 0;
91*86d7f5d3SJohn Marino 	if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1) {
92*86d7f5d3SJohn Marino 		if (!quiet)
93*86d7f5d3SJohn Marino 			perror("tcsetattr");
94*86d7f5d3SJohn Marino 	} else
95*86d7f5d3SJohn Marino 		_in_raw_mode = 1;
96*86d7f5d3SJohn Marino }
97