Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 11 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 12 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 13 | /* |
| 14 | * SSH2 tty modes support by Kevin Steves. |
| 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 | /* |
| 39 | * Encoding and decoding of terminal modes in a portable way. |
| 40 | * Much of the format is defined in ttymodes.h; it is included multiple times |
| 41 | * into this file with the appropriate macro definitions to generate the |
| 42 | * suitable code. |
| 43 | */ |
| 44 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | #include "includes.h" |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 46 | |
| 47 | #include <termios.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | |
| 49 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 50 | #include "log.h" |
| 51 | #include "ssh1.h" |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 52 | #include "compat.h" |
| 53 | #include "buffer.h" |
| 54 | #include "bufaux.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 56 | #define TTY_OP_END 0 |
| 57 | /* |
| 58 | * uint32 (u_int) follows speed in SSH1 and SSH2 |
| 59 | */ |
| 60 | #define TTY_OP_ISPEED_PROTO1 192 |
| 61 | #define TTY_OP_OSPEED_PROTO1 193 |
| 62 | #define TTY_OP_ISPEED_PROTO2 128 |
| 63 | #define TTY_OP_OSPEED_PROTO2 129 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 65 | /* |
| 66 | * Converts POSIX speed_t to a baud rate. The values of the |
| 67 | * constants for speed_t are not themselves portable. |
| 68 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 69 | static int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | speed_to_baud(speed_t speed) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | switch (speed) { |
| 73 | case B0: |
| 74 | return 0; |
| 75 | case B50: |
| 76 | return 50; |
| 77 | case B75: |
| 78 | return 75; |
| 79 | case B110: |
| 80 | return 110; |
| 81 | case B134: |
| 82 | return 134; |
| 83 | case B150: |
| 84 | return 150; |
| 85 | case B200: |
| 86 | return 200; |
| 87 | case B300: |
| 88 | return 300; |
| 89 | case B600: |
| 90 | return 600; |
| 91 | case B1200: |
| 92 | return 1200; |
| 93 | case B1800: |
| 94 | return 1800; |
| 95 | case B2400: |
| 96 | return 2400; |
| 97 | case B4800: |
| 98 | return 4800; |
| 99 | case B9600: |
| 100 | return 9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
| 102 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | case B19200: |
| 104 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | #else /* B19200 */ |
| 106 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 107 | case EXTA: |
| 108 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | #endif /* EXTA */ |
| 110 | #endif /* B19200 */ |
| 111 | |
| 112 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | case B38400: |
| 114 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | #else /* B38400 */ |
| 116 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | case EXTB: |
| 118 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | #endif /* EXTB */ |
| 120 | #endif /* B38400 */ |
| 121 | |
| 122 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | case B7200: |
| 124 | return 7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | #endif /* B7200 */ |
| 126 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | case B14400: |
| 128 | return 14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | #endif /* B14400 */ |
| 130 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | case B28800: |
| 132 | return 28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | #endif /* B28800 */ |
| 134 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | case B57600: |
| 136 | return 57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | #endif /* B57600 */ |
| 138 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | case B76800: |
| 140 | return 76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | #endif /* B76800 */ |
| 142 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | case B115200: |
| 144 | return 115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | #endif /* B115200 */ |
| 146 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | case B230400: |
| 148 | return 230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | default: |
| 151 | return 9600; |
| 152 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | } |
| 154 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | /* |
| 156 | * Converts a numeric baud rate to a POSIX speed_t. |
| 157 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 158 | static speed_t |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | baud_to_speed(int baud) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 160 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | switch (baud) { |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 162 | case 0: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | return B0; |
| 164 | case 50: |
| 165 | return B50; |
| 166 | case 75: |
| 167 | return B75; |
| 168 | case 110: |
| 169 | return B110; |
| 170 | case 134: |
| 171 | return B134; |
| 172 | case 150: |
| 173 | return B150; |
| 174 | case 200: |
| 175 | return B200; |
| 176 | case 300: |
| 177 | return B300; |
| 178 | case 600: |
| 179 | return B600; |
| 180 | case 1200: |
| 181 | return B1200; |
| 182 | case 1800: |
| 183 | return B1800; |
| 184 | case 2400: |
| 185 | return B2400; |
| 186 | case 4800: |
| 187 | return B4800; |
| 188 | case 9600: |
| 189 | return B9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | |
| 191 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | case 19200: |
| 193 | return B19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | #else /* B19200 */ |
| 195 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | case 19200: |
| 197 | return EXTA; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | #endif /* EXTA */ |
| 199 | #endif /* B19200 */ |
| 200 | |
| 201 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 202 | case 38400: |
| 203 | return B38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | #else /* B38400 */ |
| 205 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | case 38400: |
| 207 | return EXTB; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | #endif /* EXTB */ |
| 209 | #endif /* B38400 */ |
| 210 | |
| 211 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 212 | case 7200: |
| 213 | return B7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | #endif /* B7200 */ |
| 215 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | case 14400: |
| 217 | return B14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | #endif /* B14400 */ |
| 219 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | case 28800: |
| 221 | return B28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | #endif /* B28800 */ |
| 223 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | case 57600: |
| 225 | return B57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 226 | #endif /* B57600 */ |
| 227 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 228 | case 76800: |
| 229 | return B76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | #endif /* B76800 */ |
| 231 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 232 | case 115200: |
| 233 | return B115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 234 | #endif /* B115200 */ |
| 235 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | case 230400: |
| 237 | return B230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 239 | default: |
| 240 | return B9600; |
| 241 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 242 | } |
| 243 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 244 | /* |
Damien Miller | 1d10976 | 2005-08-16 21:32:09 +1000 | [diff] [blame] | 245 | * Encode a special character into SSH line format. |
| 246 | */ |
| 247 | static u_int |
| 248 | special_char_encode(cc_t c) |
| 249 | { |
| 250 | #ifdef _POSIX_VDISABLE |
| 251 | if (c == _POSIX_VDISABLE) |
| 252 | return 255; |
| 253 | #endif /* _POSIX_VDISABLE */ |
| 254 | return c; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Decode a special character from SSH line format. |
| 259 | */ |
| 260 | static cc_t |
| 261 | special_char_decode(u_int c) |
| 262 | { |
| 263 | #ifdef _POSIX_VDISABLE |
| 264 | if (c == 255) |
| 265 | return _POSIX_VDISABLE; |
| 266 | #endif /* _POSIX_VDISABLE */ |
| 267 | return c; |
| 268 | } |
| 269 | |
| 270 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | * Encodes terminal modes for the terminal referenced by fd |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 272 | * 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] | 273 | * being constructed. |
| 274 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 275 | void |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 276 | tty_make_modes(int fd, struct termios *tiop) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 277 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 278 | struct termios tio; |
| 279 | int baud; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 280 | Buffer buf; |
| 281 | int tty_op_ospeed, tty_op_ispeed; |
| 282 | void (*put_arg)(Buffer *, u_int); |
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); |
| 285 | if (compat20) { |
| 286 | tty_op_ospeed = TTY_OP_OSPEED_PROTO2; |
| 287 | tty_op_ispeed = TTY_OP_ISPEED_PROTO2; |
| 288 | put_arg = buffer_put_int; |
| 289 | } else { |
| 290 | tty_op_ospeed = TTY_OP_OSPEED_PROTO1; |
| 291 | tty_op_ispeed = TTY_OP_ISPEED_PROTO1; |
| 292 | put_arg = (void (*)(Buffer *, u_int)) buffer_put_char; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 293 | } |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 294 | |
| 295 | if (tiop == NULL) { |
| 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 | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 305 | debug3("tty_make_modes: ospeed %d", baud); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 306 | buffer_put_char(&buf, tty_op_ospeed); |
| 307 | buffer_put_int(&buf, baud); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 308 | baud = speed_to_baud(cfgetispeed(&tio)); |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 309 | debug3("tty_make_modes: ispeed %d", baud); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 310 | buffer_put_char(&buf, tty_op_ispeed); |
| 311 | buffer_put_int(&buf, baud); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 312 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | /* Store values of mode flags. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | #define TTYCHAR(NAME, OP) \ |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 315 | debug3("tty_make_modes: %d %d", OP, tio.c_cc[NAME]); \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 316 | buffer_put_char(&buf, OP); \ |
Damien Miller | 1d10976 | 2005-08-16 21:32:09 +1000 | [diff] [blame] | 317 | put_arg(&buf, special_char_encode(tio.c_cc[NAME])); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 318 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 319 | #define TTYMODE(NAME, FIELD, OP) \ |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 320 | debug3("tty_make_modes: %d %d", OP, ((tio.FIELD & NAME) != 0)); \ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 321 | buffer_put_char(&buf, OP); \ |
| 322 | put_arg(&buf, ((tio.FIELD & NAME) != 0)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 323 | |
| 324 | #include "ttymodes.h" |
| 325 | |
| 326 | #undef TTYCHAR |
| 327 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 328 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 329 | end: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 330 | /* Mark end of mode data. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 331 | buffer_put_char(&buf, TTY_OP_END); |
| 332 | if (compat20) |
| 333 | packet_put_string(buffer_ptr(&buf), buffer_len(&buf)); |
| 334 | else |
| 335 | packet_put_raw(buffer_ptr(&buf), buffer_len(&buf)); |
| 336 | buffer_free(&buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 337 | } |
| 338 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | /* |
| 340 | * Decodes terminal modes for the terminal referenced by fd in a portable |
| 341 | * manner from a packet being read. |
| 342 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 343 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 344 | tty_parse_modes(int fd, int *n_bytes_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 345 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 346 | struct termios tio; |
| 347 | int opcode, baud; |
| 348 | int n_bytes = 0; |
| 349 | int failure = 0; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 350 | u_int (*get_arg)(void); |
| 351 | int arg, arg_size; |
| 352 | |
| 353 | if (compat20) { |
| 354 | *n_bytes_ptr = packet_get_int(); |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 355 | debug3("tty_parse_modes: SSH2 n_bytes %d", *n_bytes_ptr); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 356 | if (*n_bytes_ptr == 0) |
| 357 | return; |
| 358 | get_arg = packet_get_int; |
| 359 | arg_size = 4; |
| 360 | } else { |
| 361 | get_arg = packet_get_char; |
| 362 | arg_size = 1; |
| 363 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | /* |
| 366 | * Get old attributes for the terminal. We will modify these |
| 367 | * flags. I am hoping that if there are any machine-specific |
| 368 | * modes, they will initially have reasonable values. |
| 369 | */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 370 | if (tcgetattr(fd, &tio) == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 371 | logit("tcgetattr: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 372 | failure = -1; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 373 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 374 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 375 | for (;;) { |
| 376 | n_bytes += 1; |
| 377 | opcode = packet_get_char(); |
| 378 | switch (opcode) { |
| 379 | case TTY_OP_END: |
| 380 | goto set; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 381 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 382 | /* XXX: future conflict possible */ |
| 383 | case TTY_OP_ISPEED_PROTO1: |
| 384 | case TTY_OP_ISPEED_PROTO2: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 385 | n_bytes += 4; |
| 386 | baud = packet_get_int(); |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 387 | debug3("tty_parse_modes: ispeed %d", baud); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 388 | if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | error("cfsetispeed failed for %d", baud); |
| 390 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 391 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 392 | /* XXX: future conflict possible */ |
| 393 | case TTY_OP_OSPEED_PROTO1: |
| 394 | case TTY_OP_OSPEED_PROTO2: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | n_bytes += 4; |
| 396 | baud = packet_get_int(); |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 397 | debug3("tty_parse_modes: ospeed %d", baud); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 398 | if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) == -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | error("cfsetospeed failed for %d", baud); |
| 400 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 401 | |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 402 | #define TTYCHAR(NAME, OP) \ |
| 403 | case OP: \ |
| 404 | n_bytes += arg_size; \ |
Damien Miller | 1d10976 | 2005-08-16 21:32:09 +1000 | [diff] [blame] | 405 | tio.c_cc[NAME] = special_char_decode(get_arg()); \ |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 406 | debug3("tty_parse_modes: %d %d", OP, tio.c_cc[NAME]); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 407 | break; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 408 | #define TTYMODE(NAME, FIELD, OP) \ |
| 409 | case OP: \ |
| 410 | n_bytes += arg_size; \ |
| 411 | if ((arg = get_arg())) \ |
| 412 | tio.FIELD |= NAME; \ |
| 413 | else \ |
| 414 | tio.FIELD &= ~NAME; \ |
Ben Lindstrom | 491bbb8 | 2001-06-25 05:24:16 +0000 | [diff] [blame] | 415 | debug3("tty_parse_modes: %d %d", OP, arg); \ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 416 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 417 | |
| 418 | #include "ttymodes.h" |
| 419 | |
| 420 | #undef TTYCHAR |
| 421 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 422 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 423 | default: |
| 424 | debug("Ignoring unsupported tty mode opcode %d (0x%x)", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 425 | opcode, opcode); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 426 | if (!compat20) { |
| 427 | /* |
| 428 | * SSH1: |
| 429 | * Opcodes 1 to 127 are defined to have |
| 430 | * a one-byte argument. |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 431 | * Opcodes 128 to 159 are defined to have |
| 432 | * an integer argument. |
| 433 | */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 434 | if (opcode > 0 && opcode < 128) { |
| 435 | n_bytes += 1; |
| 436 | (void) packet_get_char(); |
| 437 | break; |
| 438 | } else if (opcode >= 128 && opcode < 160) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 439 | n_bytes += 4; |
| 440 | (void) packet_get_int(); |
| 441 | break; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 442 | } else { |
| 443 | /* |
| 444 | * It is a truly undefined opcode (160 to 255). |
| 445 | * We have no idea about its arguments. So we |
| 446 | * must stop parsing. Note that some data may be |
| 447 | * left in the packet; hopefully there is nothing |
| 448 | * more coming after the mode data. |
| 449 | */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 450 | logit("parse_tty_modes: unknown opcode %d", opcode); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 451 | goto set; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 452 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 453 | } else { |
| 454 | /* |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 455 | * SSH2: |
Ben Lindstrom | ac2f003 | 2001-04-15 14:25:12 +0000 | [diff] [blame] | 456 | * Opcodes 1 to 159 are defined to have |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 457 | * a uint32 argument. |
| 458 | * Opcodes 160 to 255 are undefined and |
| 459 | * cause parsing to stop. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 460 | */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 461 | if (opcode > 0 && opcode < 160) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 462 | n_bytes += 4; |
| 463 | (void) packet_get_int(); |
| 464 | break; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 465 | } else { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 466 | logit("parse_tty_modes: unknown opcode %d", opcode); |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 467 | goto set; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 468 | } |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 469 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 470 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 471 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 472 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 473 | set: |
| 474 | if (*n_bytes_ptr != n_bytes) { |
| 475 | *n_bytes_ptr = n_bytes; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 476 | logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d", |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 477 | *n_bytes_ptr, n_bytes); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | return; /* Don't process bytes passed */ |
| 479 | } |
| 480 | if (failure == -1) |
Ben Lindstrom | ac2f003 | 2001-04-15 14:25:12 +0000 | [diff] [blame] | 481 | return; /* Packet parsed ok but tcgetattr() failed */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 482 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | /* Set the new modes for the terminal. */ |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 484 | if (tcsetattr(fd, TCSANOW, &tio) == -1) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 485 | logit("Setting tty modes failed: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | } |