djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ttymodes.c,v 1.32 2017/04/30 23:26:54 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 14 | /* |
| 15 | * SSH2 tty modes support by Kevin Steves. |
| 16 | * Copyright (c) 2001 Kevin Steves. All rights reserved. |
| 17 | * |
| 18 | * Redistribution and use in source and binary forms, with or without |
| 19 | * modification, are permitted provided that the following conditions |
| 20 | * are met: |
| 21 | * 1. Redistributions of source code must retain the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer. |
| 23 | * 2. Redistributions in binary form must reproduce the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer in the |
| 25 | * documentation and/or other materials provided with the distribution. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 29 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 30 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 32 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 36 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * Encoding and decoding of terminal modes in a portable way. |
| 41 | * Much of the format is defined in ttymodes.h; it is included multiple times |
| 42 | * into this file with the appropriate macro definitions to generate the |
| 43 | * suitable code. |
| 44 | */ |
| 45 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | #include "includes.h" |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 47 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 50 | #include <errno.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 51 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 52 | #include <termios.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 53 | #include <stdarg.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
| 55 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 56 | #include "log.h" |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 57 | #include "compat.h" |
| 58 | #include "buffer.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 60 | #define TTY_OP_END 0 |
| 61 | /* |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 62 | * uint32 (u_int) follows speed. |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 63 | */ |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 64 | #define TTY_OP_ISPEED 128 |
| 65 | #define TTY_OP_OSPEED 129 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | /* |
| 68 | * Converts POSIX speed_t to a baud rate. The values of the |
| 69 | * constants for speed_t are not themselves portable. |
| 70 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 71 | static int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | speed_to_baud(speed_t speed) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 74 | switch (speed) { |
| 75 | case B0: |
| 76 | return 0; |
| 77 | case B50: |
| 78 | return 50; |
| 79 | case B75: |
| 80 | return 75; |
| 81 | case B110: |
| 82 | return 110; |
| 83 | case B134: |
| 84 | return 134; |
| 85 | case B150: |
| 86 | return 150; |
| 87 | case B200: |
| 88 | return 200; |
| 89 | case B300: |
| 90 | return 300; |
| 91 | case B600: |
| 92 | return 600; |
| 93 | case B1200: |
| 94 | return 1200; |
| 95 | case B1800: |
| 96 | return 1800; |
| 97 | case B2400: |
| 98 | return 2400; |
| 99 | case B4800: |
| 100 | return 4800; |
| 101 | case B9600: |
| 102 | return 9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
| 104 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 105 | case B19200: |
| 106 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | #else /* B19200 */ |
| 108 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | case EXTA: |
| 110 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | #endif /* EXTA */ |
| 112 | #endif /* B19200 */ |
| 113 | |
| 114 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | case B38400: |
| 116 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | #else /* B38400 */ |
| 118 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | case EXTB: |
| 120 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | #endif /* EXTB */ |
| 122 | #endif /* B38400 */ |
| 123 | |
| 124 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 125 | case B7200: |
| 126 | return 7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | #endif /* B7200 */ |
| 128 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | case B14400: |
| 130 | return 14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | #endif /* B14400 */ |
| 132 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | case B28800: |
| 134 | return 28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | #endif /* B28800 */ |
| 136 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | case B57600: |
| 138 | return 57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | #endif /* B57600 */ |
| 140 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | case B76800: |
| 142 | return 76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | #endif /* B76800 */ |
| 144 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | case B115200: |
| 146 | return 115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | #endif /* B115200 */ |
| 148 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 149 | case B230400: |
| 150 | return 230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | default: |
| 153 | return 9600; |
| 154 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | } |
| 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | /* |
| 158 | * Converts a numeric baud rate to a POSIX speed_t. |
| 159 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 160 | static speed_t |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | baud_to_speed(int baud) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | switch (baud) { |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 164 | case 0: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | return B0; |
| 166 | case 50: |
| 167 | return B50; |
| 168 | case 75: |
| 169 | return B75; |
| 170 | case 110: |
| 171 | return B110; |
| 172 | case 134: |
| 173 | return B134; |
| 174 | case 150: |
| 175 | return B150; |
| 176 | case 200: |
| 177 | return B200; |
| 178 | case 300: |
| 179 | return B300; |
| 180 | case 600: |
| 181 | return B600; |
| 182 | case 1200: |
| 183 | return B1200; |
| 184 | case 1800: |
| 185 | return B1800; |
| 186 | case 2400: |
| 187 | return B2400; |
| 188 | case 4800: |
| 189 | return B4800; |
| 190 | case 9600: |
| 191 | return B9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 192 | |
| 193 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | case 19200: |
| 195 | return B19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 196 | #else /* B19200 */ |
| 197 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | case 19200: |
| 199 | return EXTA; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 200 | #endif /* EXTA */ |
| 201 | #endif /* B19200 */ |
| 202 | |
| 203 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | case 38400: |
| 205 | return B38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 206 | #else /* B38400 */ |
| 207 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | case 38400: |
| 209 | return EXTB; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | #endif /* EXTB */ |
| 211 | #endif /* B38400 */ |
| 212 | |
| 213 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | case 7200: |
| 215 | return B7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | #endif /* B7200 */ |
| 217 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | case 14400: |
| 219 | return B14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 220 | #endif /* B14400 */ |
| 221 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 222 | case 28800: |
| 223 | return B28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 224 | #endif /* B28800 */ |
| 225 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 226 | case 57600: |
| 227 | return B57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | #endif /* B57600 */ |
| 229 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | case 76800: |
| 231 | return B76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | #endif /* B76800 */ |
| 233 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 234 | case 115200: |
| 235 | return B115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 236 | #endif /* B115200 */ |
| 237 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 238 | case 230400: |
| 239 | return B230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 240 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | default: |
| 242 | return B9600; |
| 243 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | } |
| 245 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | /* |
Damien Miller | 1d10976 | 2005-08-16 21:32:09 +1000 | [diff] [blame] | 247 | * Encode a special character into SSH line format. |
| 248 | */ |
| 249 | static u_int |
| 250 | special_char_encode(cc_t c) |
| 251 | { |
| 252 | #ifdef _POSIX_VDISABLE |
| 253 | if (c == _POSIX_VDISABLE) |
| 254 | return 255; |
| 255 | #endif /* _POSIX_VDISABLE */ |
| 256 | return c; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Decode a special character from SSH line format. |
| 261 | */ |
| 262 | static cc_t |
| 263 | special_char_decode(u_int c) |
| 264 | { |
| 265 | #ifdef _POSIX_VDISABLE |
| 266 | if (c == 255) |
| 267 | return _POSIX_VDISABLE; |
| 268 | #endif /* _POSIX_VDISABLE */ |
| 269 | return c; |
| 270 | } |
| 271 | |
| 272 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | * Encodes terminal modes for the terminal referenced by fd |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 274 | * or tiop in a portable manner, and appends the modes to a packet |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 275 | * being constructed. |
| 276 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 277 | void |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 278 | tty_make_modes(int fd, struct termios *tiop) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 279 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 280 | struct termios tio; |
| 281 | int baud; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 282 | Buffer buf; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 283 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 284 | buffer_init(&buf); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 285 | |
| 286 | if (tiop == NULL) { |
Darren Tucker | dd39264 | 2008-06-08 12:53:20 +1000 | [diff] [blame] | 287 | if (fd == -1) { |
| 288 | debug("tty_make_modes: no fd or tio"); |
| 289 | goto end; |
| 290 | } |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 291 | if (tcgetattr(fd, &tio) == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 292 | logit("tcgetattr: %.100s", strerror(errno)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 293 | goto end; |
| 294 | } |
| 295 | } else |
| 296 | tio = *tiop; |
| 297 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 298 | /* Store input and output baud rates. */ |
| 299 | baud = speed_to_baud(cfgetospeed(&tio)); |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 300 | buffer_put_char(&buf, TTY_OP_OSPEED); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 301 | buffer_put_int(&buf, baud); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 302 | baud = speed_to_baud(cfgetispeed(&tio)); |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 303 | buffer_put_char(&buf, TTY_OP_ISPEED); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 304 | buffer_put_int(&buf, baud); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 306 | /* Store values of mode flags. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | #define TTYCHAR(NAME, OP) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 308 | buffer_put_char(&buf, OP); \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 309 | buffer_put_int(&buf, special_char_encode(tio.c_cc[NAME])); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 310 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | #define TTYMODE(NAME, FIELD, OP) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 312 | buffer_put_char(&buf, OP); \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 313 | buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
| 315 | #include "ttymodes.h" |
| 316 | |
| 317 | #undef TTYCHAR |
| 318 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 319 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 320 | end: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 321 | /* Mark end of mode data. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 322 | buffer_put_char(&buf, TTY_OP_END); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 323 | packet_put_string(buffer_ptr(&buf), buffer_len(&buf)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 324 | buffer_free(&buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 325 | } |
| 326 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 327 | /* |
| 328 | * Decodes terminal modes for the terminal referenced by fd in a portable |
| 329 | * manner from a packet being read. |
| 330 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 331 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | tty_parse_modes(int fd, int *n_bytes_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 333 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 334 | struct termios tio; |
| 335 | int opcode, baud; |
| 336 | int n_bytes = 0; |
| 337 | int failure = 0; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 338 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 339 | *n_bytes_ptr = packet_get_int(); |
| 340 | if (*n_bytes_ptr == 0) |
| 341 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 342 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | /* |
| 344 | * Get old attributes for the terminal. We will modify these |
| 345 | * flags. I am hoping that if there are any machine-specific |
| 346 | * modes, they will initially have reasonable values. |
| 347 | */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 348 | if (tcgetattr(fd, &tio) == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 349 | logit("tcgetattr: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 350 | failure = -1; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 351 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 352 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | for (;;) { |
| 354 | n_bytes += 1; |
| 355 | opcode = packet_get_char(); |
| 356 | switch (opcode) { |
| 357 | case TTY_OP_END: |
| 358 | goto set; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 359 | |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 360 | case TTY_OP_ISPEED: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | n_bytes += 4; |
| 362 | baud = packet_get_int(); |
Damien Miller | a5a2859 | 2006-03-26 14:10:34 +1100 | [diff] [blame] | 363 | if (failure != -1 && |
| 364 | cfsetispeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | error("cfsetispeed failed for %d", baud); |
| 366 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 367 | |
djm@openbsd.org | aebd0ab | 2017-04-30 23:26:54 +0000 | [diff] [blame] | 368 | case TTY_OP_OSPEED: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 369 | n_bytes += 4; |
| 370 | baud = packet_get_int(); |
Damien Miller | a5a2859 | 2006-03-26 14:10:34 +1100 | [diff] [blame] | 371 | if (failure != -1 && |
| 372 | cfsetospeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | error("cfsetospeed failed for %d", baud); |
| 374 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 375 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 376 | #define TTYCHAR(NAME, OP) \ |
| 377 | case OP: \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 378 | n_bytes += 4; \ |
| 379 | tio.c_cc[NAME] = special_char_decode(packet_get_int()); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 380 | break; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 381 | #define TTYMODE(NAME, FIELD, OP) \ |
| 382 | case OP: \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 383 | n_bytes += 4; \ |
| 384 | if (packet_get_int()) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 385 | tio.FIELD |= NAME; \ |
| 386 | else \ |
| 387 | tio.FIELD &= ~NAME; \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 388 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 389 | |
| 390 | #include "ttymodes.h" |
| 391 | |
| 392 | #undef TTYCHAR |
| 393 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 394 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | default: |
| 396 | debug("Ignoring unsupported tty mode opcode %d (0x%x)", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 397 | opcode, opcode); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 398 | /* |
| 399 | * SSH2: |
| 400 | * Opcodes 1 to 159 are defined to have a uint32 |
| 401 | * argument. |
| 402 | * Opcodes 160 to 255 are undefined and cause parsing |
| 403 | * to stop. |
| 404 | */ |
| 405 | if (opcode > 0 && opcode < 160) { |
| 406 | n_bytes += 4; |
| 407 | (void) packet_get_int(); |
| 408 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | } else { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 410 | logit("parse_tty_modes: unknown opcode %d", |
| 411 | opcode); |
| 412 | goto set; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 413 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 414 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 416 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 417 | set: |
| 418 | if (*n_bytes_ptr != n_bytes) { |
| 419 | *n_bytes_ptr = n_bytes; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 420 | logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d", |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 421 | *n_bytes_ptr, n_bytes); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | return; /* Don't process bytes passed */ |
| 423 | } |
| 424 | if (failure == -1) |
Ben Lindstrom | ac2f003 | 2001-04-15 14:25:12 +0000 | [diff] [blame] | 425 | return; /* Packet parsed ok but tcgetattr() failed */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 427 | /* Set the new modes for the terminal. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 428 | if (tcsetattr(fd, TCSANOW, &tio) == -1) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 429 | logit("Setting tty modes failed: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | } |