djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: ttymodes.c,v 1.31 2017/04/30 23:13:25 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 | /* |
| 62 | * uint32 (u_int) follows speed in SSH1 and SSH2 |
| 63 | */ |
| 64 | #define TTY_OP_ISPEED_PROTO1 192 |
| 65 | #define TTY_OP_OSPEED_PROTO1 193 |
| 66 | #define TTY_OP_ISPEED_PROTO2 128 |
| 67 | #define TTY_OP_OSPEED_PROTO2 129 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | /* |
| 70 | * Converts POSIX speed_t to a baud rate. The values of the |
| 71 | * constants for speed_t are not themselves portable. |
| 72 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 73 | static int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 74 | speed_to_baud(speed_t speed) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | switch (speed) { |
| 77 | case B0: |
| 78 | return 0; |
| 79 | case B50: |
| 80 | return 50; |
| 81 | case B75: |
| 82 | return 75; |
| 83 | case B110: |
| 84 | return 110; |
| 85 | case B134: |
| 86 | return 134; |
| 87 | case B150: |
| 88 | return 150; |
| 89 | case B200: |
| 90 | return 200; |
| 91 | case B300: |
| 92 | return 300; |
| 93 | case B600: |
| 94 | return 600; |
| 95 | case B1200: |
| 96 | return 1200; |
| 97 | case B1800: |
| 98 | return 1800; |
| 99 | case B2400: |
| 100 | return 2400; |
| 101 | case B4800: |
| 102 | return 4800; |
| 103 | case B9600: |
| 104 | return 9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | |
| 106 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 107 | case B19200: |
| 108 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | #else /* B19200 */ |
| 110 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 111 | case EXTA: |
| 112 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | #endif /* EXTA */ |
| 114 | #endif /* B19200 */ |
| 115 | |
| 116 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | case B38400: |
| 118 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | #else /* B38400 */ |
| 120 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | case EXTB: |
| 122 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | #endif /* EXTB */ |
| 124 | #endif /* B38400 */ |
| 125 | |
| 126 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | case B7200: |
| 128 | return 7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | #endif /* B7200 */ |
| 130 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | case B14400: |
| 132 | return 14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | #endif /* B14400 */ |
| 134 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | case B28800: |
| 136 | return 28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | #endif /* B28800 */ |
| 138 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | case B57600: |
| 140 | return 57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | #endif /* B57600 */ |
| 142 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | case B76800: |
| 144 | return 76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | #endif /* B76800 */ |
| 146 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | case B115200: |
| 148 | return 115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | #endif /* B115200 */ |
| 150 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | case B230400: |
| 152 | return 230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | default: |
| 155 | return 9600; |
| 156 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | } |
| 158 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | /* |
| 160 | * Converts a numeric baud rate to a POSIX speed_t. |
| 161 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 162 | static speed_t |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | baud_to_speed(int baud) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | switch (baud) { |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 166 | case 0: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | return B0; |
| 168 | case 50: |
| 169 | return B50; |
| 170 | case 75: |
| 171 | return B75; |
| 172 | case 110: |
| 173 | return B110; |
| 174 | case 134: |
| 175 | return B134; |
| 176 | case 150: |
| 177 | return B150; |
| 178 | case 200: |
| 179 | return B200; |
| 180 | case 300: |
| 181 | return B300; |
| 182 | case 600: |
| 183 | return B600; |
| 184 | case 1200: |
| 185 | return B1200; |
| 186 | case 1800: |
| 187 | return B1800; |
| 188 | case 2400: |
| 189 | return B2400; |
| 190 | case 4800: |
| 191 | return B4800; |
| 192 | case 9600: |
| 193 | return B9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | |
| 195 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | case 19200: |
| 197 | return B19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | #else /* B19200 */ |
| 199 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 200 | case 19200: |
| 201 | return EXTA; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | #endif /* EXTA */ |
| 203 | #endif /* B19200 */ |
| 204 | |
| 205 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | case 38400: |
| 207 | return B38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | #else /* B38400 */ |
| 209 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | case 38400: |
| 211 | return EXTB; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | #endif /* EXTB */ |
| 213 | #endif /* B38400 */ |
| 214 | |
| 215 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | case 7200: |
| 217 | return B7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | #endif /* B7200 */ |
| 219 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | case 14400: |
| 221 | return B14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | #endif /* B14400 */ |
| 223 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | case 28800: |
| 225 | return B28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 226 | #endif /* B28800 */ |
| 227 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 228 | case 57600: |
| 229 | return B57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | #endif /* B57600 */ |
| 231 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 232 | case 76800: |
| 233 | return B76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 234 | #endif /* B76800 */ |
| 235 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | case 115200: |
| 237 | return B115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | #endif /* B115200 */ |
| 239 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | case 230400: |
| 241 | return B230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 242 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | default: |
| 244 | return B9600; |
| 245 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 246 | } |
| 247 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 248 | /* |
Damien Miller | 1d10976 | 2005-08-16 21:32:09 +1000 | [diff] [blame] | 249 | * Encode a special character into SSH line format. |
| 250 | */ |
| 251 | static u_int |
| 252 | special_char_encode(cc_t c) |
| 253 | { |
| 254 | #ifdef _POSIX_VDISABLE |
| 255 | if (c == _POSIX_VDISABLE) |
| 256 | return 255; |
| 257 | #endif /* _POSIX_VDISABLE */ |
| 258 | return c; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Decode a special character from SSH line format. |
| 263 | */ |
| 264 | static cc_t |
| 265 | special_char_decode(u_int c) |
| 266 | { |
| 267 | #ifdef _POSIX_VDISABLE |
| 268 | if (c == 255) |
| 269 | return _POSIX_VDISABLE; |
| 270 | #endif /* _POSIX_VDISABLE */ |
| 271 | return c; |
| 272 | } |
| 273 | |
| 274 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 275 | * Encodes terminal modes for the terminal referenced by fd |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 276 | * 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] | 277 | * being constructed. |
| 278 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 279 | void |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 280 | tty_make_modes(int fd, struct termios *tiop) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 281 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 282 | struct termios tio; |
| 283 | int baud; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 284 | Buffer buf; |
| 285 | int tty_op_ospeed, tty_op_ispeed; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 287 | buffer_init(&buf); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 288 | tty_op_ospeed = TTY_OP_OSPEED_PROTO2; |
| 289 | tty_op_ispeed = TTY_OP_ISPEED_PROTO2; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 290 | |
| 291 | if (tiop == NULL) { |
Darren Tucker | dd39264 | 2008-06-08 12:53:20 +1000 | [diff] [blame] | 292 | if (fd == -1) { |
| 293 | debug("tty_make_modes: no fd or tio"); |
| 294 | goto end; |
| 295 | } |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 296 | if (tcgetattr(fd, &tio) == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 297 | logit("tcgetattr: %.100s", strerror(errno)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 298 | goto end; |
| 299 | } |
| 300 | } else |
| 301 | tio = *tiop; |
| 302 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | /* Store input and output baud rates. */ |
| 304 | baud = speed_to_baud(cfgetospeed(&tio)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 305 | buffer_put_char(&buf, tty_op_ospeed); |
| 306 | buffer_put_int(&buf, baud); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 307 | baud = speed_to_baud(cfgetispeed(&tio)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 308 | buffer_put_char(&buf, tty_op_ispeed); |
| 309 | buffer_put_int(&buf, baud); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 310 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | /* Store values of mode flags. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 312 | #define TTYCHAR(NAME, OP) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 313 | buffer_put_char(&buf, OP); \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 314 | buffer_put_int(&buf, special_char_encode(tio.c_cc[NAME])); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 315 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 316 | #define TTYMODE(NAME, FIELD, OP) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 317 | buffer_put_char(&buf, OP); \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 318 | buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 319 | |
| 320 | #include "ttymodes.h" |
| 321 | |
| 322 | #undef TTYCHAR |
| 323 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 324 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 325 | end: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 326 | /* Mark end of mode data. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 327 | buffer_put_char(&buf, TTY_OP_END); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 328 | packet_put_string(buffer_ptr(&buf), buffer_len(&buf)); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 329 | buffer_free(&buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 330 | } |
| 331 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | /* |
| 333 | * Decodes terminal modes for the terminal referenced by fd in a portable |
| 334 | * manner from a packet being read. |
| 335 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 336 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 337 | tty_parse_modes(int fd, int *n_bytes_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | struct termios tio; |
| 340 | int opcode, baud; |
| 341 | int n_bytes = 0; |
| 342 | int failure = 0; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 343 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 344 | *n_bytes_ptr = packet_get_int(); |
| 345 | if (*n_bytes_ptr == 0) |
| 346 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | /* |
| 349 | * Get old attributes for the terminal. We will modify these |
| 350 | * flags. I am hoping that if there are any machine-specific |
| 351 | * modes, they will initially have reasonable values. |
| 352 | */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 353 | if (tcgetattr(fd, &tio) == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 354 | logit("tcgetattr: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 355 | failure = -1; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 356 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 357 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 358 | for (;;) { |
| 359 | n_bytes += 1; |
| 360 | opcode = packet_get_char(); |
| 361 | switch (opcode) { |
| 362 | case TTY_OP_END: |
| 363 | goto set; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 365 | /* XXX: future conflict possible */ |
| 366 | case TTY_OP_ISPEED_PROTO1: |
| 367 | case TTY_OP_ISPEED_PROTO2: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 368 | n_bytes += 4; |
| 369 | baud = packet_get_int(); |
Damien Miller | a5a2859 | 2006-03-26 14:10:34 +1100 | [diff] [blame] | 370 | if (failure != -1 && |
| 371 | cfsetispeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 372 | error("cfsetispeed failed for %d", baud); |
| 373 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 374 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 375 | /* XXX: future conflict possible */ |
| 376 | case TTY_OP_OSPEED_PROTO1: |
| 377 | case TTY_OP_OSPEED_PROTO2: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 378 | n_bytes += 4; |
| 379 | baud = packet_get_int(); |
Damien Miller | a5a2859 | 2006-03-26 14:10:34 +1100 | [diff] [blame] | 380 | if (failure != -1 && |
| 381 | cfsetospeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 382 | error("cfsetospeed failed for %d", baud); |
| 383 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 384 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 385 | #define TTYCHAR(NAME, OP) \ |
| 386 | case OP: \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 387 | n_bytes += 4; \ |
| 388 | tio.c_cc[NAME] = special_char_decode(packet_get_int()); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 389 | break; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 390 | #define TTYMODE(NAME, FIELD, OP) \ |
| 391 | case OP: \ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 392 | n_bytes += 4; \ |
| 393 | if (packet_get_int()) \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 394 | tio.FIELD |= NAME; \ |
| 395 | else \ |
| 396 | tio.FIELD &= ~NAME; \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 397 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 398 | |
| 399 | #include "ttymodes.h" |
| 400 | |
| 401 | #undef TTYCHAR |
| 402 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 403 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | default: |
| 405 | debug("Ignoring unsupported tty mode opcode %d (0x%x)", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 406 | opcode, opcode); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 407 | /* |
| 408 | * SSH2: |
| 409 | * Opcodes 1 to 159 are defined to have a uint32 |
| 410 | * argument. |
| 411 | * Opcodes 160 to 255 are undefined and cause parsing |
| 412 | * to stop. |
| 413 | */ |
| 414 | if (opcode > 0 && opcode < 160) { |
| 415 | n_bytes += 4; |
| 416 | (void) packet_get_int(); |
| 417 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 418 | } else { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame^] | 419 | logit("parse_tty_modes: unknown opcode %d", |
| 420 | opcode); |
| 421 | goto set; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 422 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 426 | set: |
| 427 | if (*n_bytes_ptr != n_bytes) { |
| 428 | *n_bytes_ptr = n_bytes; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 429 | logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d", |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 430 | *n_bytes_ptr, n_bytes); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | return; /* Don't process bytes passed */ |
| 432 | } |
| 433 | if (failure == -1) |
Ben Lindstrom | ac2f003 | 2001-04-15 14:25:12 +0000 | [diff] [blame] | 434 | return; /* Packet parsed ok but tcgetattr() failed */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 435 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | /* Set the new modes for the terminal. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 437 | if (tcsetattr(fd, TCSANOW, &tio) == -1) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 438 | logit("Setting tty modes failed: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 439 | } |