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 | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Encoding and decoding of terminal modes in a portable way. |
| 6 | * Much of the format is defined in ttymodes.h; it is included multiple times |
| 7 | * into this file with the appropriate macro definitions to generate the |
| 8 | * suitable code. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * |
| 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Ben Lindstrom | 0026154 | 2001-03-10 17:17:28 +0000 | [diff] [blame] | 18 | RCSID("$OpenBSD: ttymodes.c,v 1.11 2001/03/10 15:02:05 stevesk Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 21 | #include "log.h" |
| 22 | #include "ssh1.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 23 | |
| 24 | #define TTY_OP_END 0 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 25 | #define TTY_OP_ISPEED 192 /* int follows */ |
| 26 | #define TTY_OP_OSPEED 193 /* int follows */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 28 | /* |
| 29 | * Converts POSIX speed_t to a baud rate. The values of the |
| 30 | * constants for speed_t are not themselves portable. |
| 31 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 32 | static int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | speed_to_baud(speed_t speed) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 34 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | switch (speed) { |
| 36 | case B0: |
| 37 | return 0; |
| 38 | case B50: |
| 39 | return 50; |
| 40 | case B75: |
| 41 | return 75; |
| 42 | case B110: |
| 43 | return 110; |
| 44 | case B134: |
| 45 | return 134; |
| 46 | case B150: |
| 47 | return 150; |
| 48 | case B200: |
| 49 | return 200; |
| 50 | case B300: |
| 51 | return 300; |
| 52 | case B600: |
| 53 | return 600; |
| 54 | case B1200: |
| 55 | return 1200; |
| 56 | case B1800: |
| 57 | return 1800; |
| 58 | case B2400: |
| 59 | return 2400; |
| 60 | case B4800: |
| 61 | return 4800; |
| 62 | case B9600: |
| 63 | return 9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
| 65 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | case B19200: |
| 67 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | #else /* B19200 */ |
| 69 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | case EXTA: |
| 71 | return 19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #endif /* EXTA */ |
| 73 | #endif /* B19200 */ |
| 74 | |
| 75 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | case B38400: |
| 77 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | #else /* B38400 */ |
| 79 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | case EXTB: |
| 81 | return 38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | #endif /* EXTB */ |
| 83 | #endif /* B38400 */ |
| 84 | |
| 85 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | case B7200: |
| 87 | return 7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | #endif /* B7200 */ |
| 89 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | case B14400: |
| 91 | return 14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | #endif /* B14400 */ |
| 93 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | case B28800: |
| 95 | return 28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | #endif /* B28800 */ |
| 97 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 98 | case B57600: |
| 99 | return 57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | #endif /* B57600 */ |
| 101 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | case B76800: |
| 103 | return 76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | #endif /* B76800 */ |
| 105 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | case B115200: |
| 107 | return 115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 108 | #endif /* B115200 */ |
| 109 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | case B230400: |
| 111 | return 230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | default: |
| 114 | return 9600; |
| 115 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | } |
| 117 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | /* |
| 119 | * Converts a numeric baud rate to a POSIX speed_t. |
| 120 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 121 | static speed_t |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 122 | baud_to_speed(int baud) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | switch (baud) { |
| 125 | case 0: |
| 126 | return B0; |
| 127 | case 50: |
| 128 | return B50; |
| 129 | case 75: |
| 130 | return B75; |
| 131 | case 110: |
| 132 | return B110; |
| 133 | case 134: |
| 134 | return B134; |
| 135 | case 150: |
| 136 | return B150; |
| 137 | case 200: |
| 138 | return B200; |
| 139 | case 300: |
| 140 | return B300; |
| 141 | case 600: |
| 142 | return B600; |
| 143 | case 1200: |
| 144 | return B1200; |
| 145 | case 1800: |
| 146 | return B1800; |
| 147 | case 2400: |
| 148 | return B2400; |
| 149 | case 4800: |
| 150 | return B4800; |
| 151 | case 9600: |
| 152 | return B9600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
| 154 | #ifdef B19200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | case 19200: |
| 156 | return B19200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | #else /* B19200 */ |
| 158 | #ifdef EXTA |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | case 19200: |
| 160 | return EXTA; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | #endif /* EXTA */ |
| 162 | #endif /* B19200 */ |
| 163 | |
| 164 | #ifdef B38400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | case 38400: |
| 166 | return B38400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 167 | #else /* B38400 */ |
| 168 | #ifdef EXTB |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | case 38400: |
| 170 | return EXTB; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | #endif /* EXTB */ |
| 172 | #endif /* B38400 */ |
| 173 | |
| 174 | #ifdef B7200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | case 7200: |
| 176 | return B7200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 177 | #endif /* B7200 */ |
| 178 | #ifdef B14400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | case 14400: |
| 180 | return B14400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | #endif /* B14400 */ |
| 182 | #ifdef B28800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 183 | case 28800: |
| 184 | return B28800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | #endif /* B28800 */ |
| 186 | #ifdef B57600 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | case 57600: |
| 188 | return B57600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | #endif /* B57600 */ |
| 190 | #ifdef B76800 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 191 | case 76800: |
| 192 | return B76800; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | #endif /* B76800 */ |
| 194 | #ifdef B115200 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 195 | case 115200: |
| 196 | return B115200; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 197 | #endif /* B115200 */ |
| 198 | #ifdef B230400 |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 199 | case 230400: |
| 200 | return B230400; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 201 | #endif /* B230400 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 202 | default: |
| 203 | return B9600; |
| 204 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 205 | } |
| 206 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 207 | /* |
| 208 | * Encodes terminal modes for the terminal referenced by fd |
| 209 | * in a portable manner, and appends the modes to a packet |
| 210 | * being constructed. |
| 211 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 212 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 213 | tty_make_modes(int fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | struct termios tio; |
| 216 | int baud; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 217 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | if (tcgetattr(fd, &tio) < 0) { |
| 219 | packet_put_char(TTY_OP_END); |
| 220 | log("tcgetattr: %.100s", strerror(errno)); |
| 221 | return; |
| 222 | } |
| 223 | /* Store input and output baud rates. */ |
| 224 | baud = speed_to_baud(cfgetospeed(&tio)); |
| 225 | packet_put_char(TTY_OP_OSPEED); |
| 226 | packet_put_int(baud); |
| 227 | baud = speed_to_baud(cfgetispeed(&tio)); |
| 228 | packet_put_char(TTY_OP_ISPEED); |
| 229 | packet_put_int(baud); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | /* Store values of mode flags. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | #define TTYCHAR(NAME, OP) \ |
| 233 | packet_put_char(OP); packet_put_char(tio.c_cc[NAME]); |
| 234 | #define TTYMODE(NAME, FIELD, OP) \ |
| 235 | packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 236 | |
| 237 | #include "ttymodes.h" |
| 238 | |
| 239 | #undef TTYCHAR |
| 240 | #undef TTYMODE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 242 | /* Mark end of mode data. */ |
| 243 | packet_put_char(TTY_OP_END); |
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 | /* |
| 247 | * Decodes terminal modes for the terminal referenced by fd in a portable |
| 248 | * manner from a packet being read. |
| 249 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 250 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | tty_parse_modes(int fd, int *n_bytes_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 252 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 253 | struct termios tio; |
| 254 | int opcode, baud; |
| 255 | int n_bytes = 0; |
| 256 | int failure = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 257 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 258 | /* |
| 259 | * Get old attributes for the terminal. We will modify these |
| 260 | * flags. I am hoping that if there are any machine-specific |
| 261 | * modes, they will initially have reasonable values. |
| 262 | */ |
| 263 | if (tcgetattr(fd, &tio) < 0) |
| 264 | failure = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 265 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 266 | for (;;) { |
| 267 | n_bytes += 1; |
| 268 | opcode = packet_get_char(); |
| 269 | switch (opcode) { |
| 270 | case TTY_OP_END: |
| 271 | goto set; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 272 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | case TTY_OP_ISPEED: |
| 274 | n_bytes += 4; |
| 275 | baud = packet_get_int(); |
| 276 | if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0) |
| 277 | error("cfsetispeed failed for %d", baud); |
| 278 | break; |
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 | case TTY_OP_OSPEED: |
| 281 | n_bytes += 4; |
| 282 | baud = packet_get_int(); |
| 283 | if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0) |
| 284 | error("cfsetospeed failed for %d", baud); |
| 285 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | |
| 287 | #define TTYCHAR(NAME, OP) \ |
| 288 | case OP: \ |
| 289 | n_bytes += 1; \ |
| 290 | tio.c_cc[NAME] = packet_get_char(); \ |
| 291 | break; |
| 292 | #define TTYMODE(NAME, FIELD, OP) \ |
| 293 | case OP: \ |
| 294 | n_bytes += 1; \ |
| 295 | if (packet_get_char()) \ |
| 296 | tio.FIELD |= NAME; \ |
| 297 | else \ |
| 298 | tio.FIELD &= ~NAME; \ |
| 299 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 300 | |
| 301 | #include "ttymodes.h" |
| 302 | |
| 303 | #undef TTYCHAR |
| 304 | #undef TTYMODE |
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 | default: |
| 307 | debug("Ignoring unsupported tty mode opcode %d (0x%x)", |
| 308 | opcode, opcode); |
| 309 | /* |
| 310 | * Opcodes 0 to 127 are defined to have |
| 311 | * a one-byte argument. |
| 312 | */ |
| 313 | if (opcode >= 0 && opcode < 128) { |
| 314 | n_bytes += 1; |
| 315 | (void) packet_get_char(); |
| 316 | break; |
| 317 | } else { |
| 318 | /* |
| 319 | * Opcodes 128 to 159 are defined to have |
| 320 | * an integer argument. |
| 321 | */ |
| 322 | if (opcode >= 128 && opcode < 160) { |
| 323 | n_bytes += 4; |
| 324 | (void) packet_get_int(); |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | /* |
| 329 | * It is a truly undefined opcode (160 to 255). |
| 330 | * We have no idea about its arguments. So we |
| 331 | * must stop parsing. Note that some data may be |
| 332 | * left in the packet; hopefully there is nothing |
| 333 | * more coming after the mode data. |
| 334 | */ |
| 335 | log("parse_tty_modes: unknown opcode %d", opcode); |
| 336 | packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY); |
| 337 | goto set; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 339 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 340 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 341 | set: |
| 342 | if (*n_bytes_ptr != n_bytes) { |
| 343 | *n_bytes_ptr = n_bytes; |
| 344 | return; /* Don't process bytes passed */ |
| 345 | } |
| 346 | if (failure == -1) |
| 347 | return; /* Packet parsed ok but tty stuff failed */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 348 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 349 | /* Set the new modes for the terminal. */ |
| 350 | if (tcsetattr(fd, TCSANOW, &tio) < 0) |
| 351 | log("Setting tty modes failed: %.100s", strerror(errno)); |
| 352 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 353 | } |