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