186d7f5d3SJohn Marino /* $OpenBSD: sshtty.c,v 1.14 2010/01/09 05:04:24 djm Exp $ */
286d7f5d3SJohn Marino /*
386d7f5d3SJohn Marino * Author: Tatu Ylonen <ylo@cs.hut.fi>
486d7f5d3SJohn Marino * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
586d7f5d3SJohn Marino * All rights reserved
686d7f5d3SJohn Marino *
786d7f5d3SJohn Marino * As far as I am concerned, the code I have written for this software
886d7f5d3SJohn Marino * can be used freely for any purpose. Any derived versions of this
986d7f5d3SJohn Marino * software must be clearly marked as such, and if the derived work is
1086d7f5d3SJohn Marino * incompatible with the protocol description in the RFC file, it must be
1186d7f5d3SJohn Marino * called by a name other than "ssh" or "Secure Shell".
1286d7f5d3SJohn Marino */
1386d7f5d3SJohn Marino /*
1486d7f5d3SJohn Marino * Copyright (c) 2001 Markus Friedl. All rights reserved.
1586d7f5d3SJohn Marino * Copyright (c) 2001 Kevin Steves. All rights reserved.
1686d7f5d3SJohn Marino *
1786d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
1886d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
1986d7f5d3SJohn Marino * are met:
2086d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
2186d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
2286d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
2386d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
2486d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
2586d7f5d3SJohn Marino *
2686d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2786d7f5d3SJohn Marino * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2886d7f5d3SJohn Marino * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2986d7f5d3SJohn Marino * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3086d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3186d7f5d3SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3286d7f5d3SJohn Marino * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3386d7f5d3SJohn Marino * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3486d7f5d3SJohn Marino * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3586d7f5d3SJohn Marino * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3686d7f5d3SJohn Marino */
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino #include "includes.h"
3986d7f5d3SJohn Marino
4086d7f5d3SJohn Marino #include <sys/types.h>
4186d7f5d3SJohn Marino #include <stdio.h>
4286d7f5d3SJohn Marino #include <termios.h>
4386d7f5d3SJohn Marino #include <pwd.h>
4486d7f5d3SJohn Marino
4586d7f5d3SJohn Marino #include "sshpty.h"
4686d7f5d3SJohn Marino
4786d7f5d3SJohn Marino static struct termios _saved_tio;
4886d7f5d3SJohn Marino static int _in_raw_mode = 0;
4986d7f5d3SJohn Marino
5086d7f5d3SJohn Marino struct termios *
get_saved_tio(void)5186d7f5d3SJohn Marino get_saved_tio(void)
5286d7f5d3SJohn Marino {
5386d7f5d3SJohn Marino return _in_raw_mode ? &_saved_tio : NULL;
5486d7f5d3SJohn Marino }
5586d7f5d3SJohn Marino
5686d7f5d3SJohn Marino void
leave_raw_mode(int quiet)5786d7f5d3SJohn Marino leave_raw_mode(int quiet)
5886d7f5d3SJohn Marino {
5986d7f5d3SJohn Marino if (!_in_raw_mode)
6086d7f5d3SJohn Marino return;
6186d7f5d3SJohn Marino if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1) {
6286d7f5d3SJohn Marino if (!quiet)
6386d7f5d3SJohn Marino perror("tcsetattr");
6486d7f5d3SJohn Marino } else
6586d7f5d3SJohn Marino _in_raw_mode = 0;
6686d7f5d3SJohn Marino }
6786d7f5d3SJohn Marino
6886d7f5d3SJohn Marino void
enter_raw_mode(int quiet)6986d7f5d3SJohn Marino enter_raw_mode(int quiet)
7086d7f5d3SJohn Marino {
7186d7f5d3SJohn Marino struct termios tio;
7286d7f5d3SJohn Marino
7386d7f5d3SJohn Marino if (tcgetattr(fileno(stdin), &tio) == -1) {
7486d7f5d3SJohn Marino if (!quiet)
7586d7f5d3SJohn Marino perror("tcgetattr");
7686d7f5d3SJohn Marino return;
7786d7f5d3SJohn Marino }
7886d7f5d3SJohn Marino _saved_tio = tio;
7986d7f5d3SJohn Marino tio.c_iflag |= IGNPAR;
8086d7f5d3SJohn Marino tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
8186d7f5d3SJohn Marino #ifdef IUCLC
8286d7f5d3SJohn Marino tio.c_iflag &= ~IUCLC;
8386d7f5d3SJohn Marino #endif
8486d7f5d3SJohn Marino tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
8586d7f5d3SJohn Marino #ifdef IEXTEN
8686d7f5d3SJohn Marino tio.c_lflag &= ~IEXTEN;
8786d7f5d3SJohn Marino #endif
8886d7f5d3SJohn Marino tio.c_oflag &= ~OPOST;
8986d7f5d3SJohn Marino tio.c_cc[VMIN] = 1;
9086d7f5d3SJohn Marino tio.c_cc[VTIME] = 0;
9186d7f5d3SJohn Marino if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1) {
9286d7f5d3SJohn Marino if (!quiet)
9386d7f5d3SJohn Marino perror("tcsetattr");
9486d7f5d3SJohn Marino } else
9586d7f5d3SJohn Marino _in_raw_mode = 1;
9686d7f5d3SJohn Marino }
97