]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
bcbf86ec | 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". | |
5260325f | 11 | */ |
8efc0c15 | 12 | |
30dcc918 | 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 | ||
8efc0c15 | 45 | #include "includes.h" |
1d3e026f | 46 | |
47 | #include <termios.h> | |
8efc0c15 | 48 | |
49 | #include "packet.h" | |
42f11eb2 | 50 | #include "log.h" |
51 | #include "ssh1.h" | |
30dcc918 | 52 | #include "compat.h" |
53 | #include "buffer.h" | |
54 | #include "bufaux.h" | |
8efc0c15 | 55 | |
30dcc918 | 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 | |
8efc0c15 | 64 | |
5260325f | 65 | /* |
66 | * Converts POSIX speed_t to a baud rate. The values of the | |
67 | * constants for speed_t are not themselves portable. | |
68 | */ | |
6ae2364d | 69 | static int |
5260325f | 70 | speed_to_baud(speed_t speed) |
8efc0c15 | 71 | { |
5260325f | 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; | |
8efc0c15 | 101 | |
102 | #ifdef B19200 | |
5260325f | 103 | case B19200: |
104 | return 19200; | |
8efc0c15 | 105 | #else /* B19200 */ |
106 | #ifdef EXTA | |
5260325f | 107 | case EXTA: |
108 | return 19200; | |
8efc0c15 | 109 | #endif /* EXTA */ |
110 | #endif /* B19200 */ | |
111 | ||
112 | #ifdef B38400 | |
5260325f | 113 | case B38400: |
114 | return 38400; | |
8efc0c15 | 115 | #else /* B38400 */ |
116 | #ifdef EXTB | |
5260325f | 117 | case EXTB: |
118 | return 38400; | |
8efc0c15 | 119 | #endif /* EXTB */ |
120 | #endif /* B38400 */ | |
121 | ||
122 | #ifdef B7200 | |
5260325f | 123 | case B7200: |
124 | return 7200; | |
8efc0c15 | 125 | #endif /* B7200 */ |
126 | #ifdef B14400 | |
5260325f | 127 | case B14400: |
128 | return 14400; | |
8efc0c15 | 129 | #endif /* B14400 */ |
130 | #ifdef B28800 | |
5260325f | 131 | case B28800: |
132 | return 28800; | |
8efc0c15 | 133 | #endif /* B28800 */ |
134 | #ifdef B57600 | |
5260325f | 135 | case B57600: |
136 | return 57600; | |
8efc0c15 | 137 | #endif /* B57600 */ |
138 | #ifdef B76800 | |
5260325f | 139 | case B76800: |
140 | return 76800; | |
8efc0c15 | 141 | #endif /* B76800 */ |
142 | #ifdef B115200 | |
5260325f | 143 | case B115200: |
144 | return 115200; | |
8efc0c15 | 145 | #endif /* B115200 */ |
146 | #ifdef B230400 | |
5260325f | 147 | case B230400: |
148 | return 230400; | |
8efc0c15 | 149 | #endif /* B230400 */ |
5260325f | 150 | default: |
151 | return 9600; | |
152 | } | |
8efc0c15 | 153 | } |
154 | ||
5260325f | 155 | /* |
156 | * Converts a numeric baud rate to a POSIX speed_t. | |
157 | */ | |
6ae2364d | 158 | static speed_t |
5260325f | 159 | baud_to_speed(int baud) |
8efc0c15 | 160 | { |
5260325f | 161 | switch (baud) { |
30dcc918 | 162 | case 0: |
5260325f | 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; | |
8efc0c15 | 190 | |
191 | #ifdef B19200 | |
5260325f | 192 | case 19200: |
193 | return B19200; | |
8efc0c15 | 194 | #else /* B19200 */ |
195 | #ifdef EXTA | |
5260325f | 196 | case 19200: |
197 | return EXTA; | |
8efc0c15 | 198 | #endif /* EXTA */ |
199 | #endif /* B19200 */ | |
200 | ||
201 | #ifdef B38400 | |
5260325f | 202 | case 38400: |
203 | return B38400; | |
8efc0c15 | 204 | #else /* B38400 */ |
205 | #ifdef EXTB | |
5260325f | 206 | case 38400: |
207 | return EXTB; | |
8efc0c15 | 208 | #endif /* EXTB */ |
209 | #endif /* B38400 */ | |
210 | ||
211 | #ifdef B7200 | |
5260325f | 212 | case 7200: |
213 | return B7200; | |
8efc0c15 | 214 | #endif /* B7200 */ |
215 | #ifdef B14400 | |
5260325f | 216 | case 14400: |
217 | return B14400; | |
8efc0c15 | 218 | #endif /* B14400 */ |
219 | #ifdef B28800 | |
5260325f | 220 | case 28800: |
221 | return B28800; | |
8efc0c15 | 222 | #endif /* B28800 */ |
223 | #ifdef B57600 | |
5260325f | 224 | case 57600: |
225 | return B57600; | |
8efc0c15 | 226 | #endif /* B57600 */ |
227 | #ifdef B76800 | |
5260325f | 228 | case 76800: |
229 | return B76800; | |
8efc0c15 | 230 | #endif /* B76800 */ |
231 | #ifdef B115200 | |
5260325f | 232 | case 115200: |
233 | return B115200; | |
8efc0c15 | 234 | #endif /* B115200 */ |
235 | #ifdef B230400 | |
5260325f | 236 | case 230400: |
237 | return B230400; | |
8efc0c15 | 238 | #endif /* B230400 */ |
5260325f | 239 | default: |
240 | return B9600; | |
241 | } | |
8efc0c15 | 242 | } |
243 | ||
879abf01 | 244 | /* |
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 | ||
5260325f | 270 | /* |
271 | * Encodes terminal modes for the terminal referenced by fd | |
30dcc918 | 272 | * or tiop in a portable manner, and appends the modes to a packet |
5260325f | 273 | * being constructed. |
274 | */ | |
6ae2364d | 275 | void |
30dcc918 | 276 | tty_make_modes(int fd, struct termios *tiop) |
8efc0c15 | 277 | { |
5260325f | 278 | struct termios tio; |
279 | int baud; | |
30dcc918 | 280 | Buffer buf; |
281 | int tty_op_ospeed, tty_op_ispeed; | |
282 | void (*put_arg)(Buffer *, u_int); | |
5260325f | 283 | |
30dcc918 | 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; | |
5260325f | 293 | } |
30dcc918 | 294 | |
295 | if (tiop == NULL) { | |
296 | if (tcgetattr(fd, &tio) == -1) { | |
bbe88b6d | 297 | logit("tcgetattr: %.100s", strerror(errno)); |
30dcc918 | 298 | goto end; |
299 | } | |
300 | } else | |
301 | tio = *tiop; | |
302 | ||
5260325f | 303 | /* Store input and output baud rates. */ |
304 | baud = speed_to_baud(cfgetospeed(&tio)); | |
b4e7177c | 305 | debug3("tty_make_modes: ospeed %d", baud); |
30dcc918 | 306 | buffer_put_char(&buf, tty_op_ospeed); |
307 | buffer_put_int(&buf, baud); | |
5260325f | 308 | baud = speed_to_baud(cfgetispeed(&tio)); |
b4e7177c | 309 | debug3("tty_make_modes: ispeed %d", baud); |
30dcc918 | 310 | buffer_put_char(&buf, tty_op_ispeed); |
311 | buffer_put_int(&buf, baud); | |
5260325f | 312 | |
313 | /* Store values of mode flags. */ | |
8efc0c15 | 314 | #define TTYCHAR(NAME, OP) \ |
b4e7177c | 315 | debug3("tty_make_modes: %d %d", OP, tio.c_cc[NAME]); \ |
30dcc918 | 316 | buffer_put_char(&buf, OP); \ |
879abf01 | 317 | put_arg(&buf, special_char_encode(tio.c_cc[NAME])); |
30dcc918 | 318 | |
8efc0c15 | 319 | #define TTYMODE(NAME, FIELD, OP) \ |
b4e7177c | 320 | debug3("tty_make_modes: %d %d", OP, ((tio.FIELD & NAME) != 0)); \ |
30dcc918 | 321 | buffer_put_char(&buf, OP); \ |
322 | put_arg(&buf, ((tio.FIELD & NAME) != 0)); | |
8efc0c15 | 323 | |
324 | #include "ttymodes.h" | |
325 | ||
326 | #undef TTYCHAR | |
327 | #undef TTYMODE | |
8efc0c15 | 328 | |
30dcc918 | 329 | end: |
5260325f | 330 | /* Mark end of mode data. */ |
30dcc918 | 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); | |
8efc0c15 | 337 | } |
338 | ||
5260325f | 339 | /* |
340 | * Decodes terminal modes for the terminal referenced by fd in a portable | |
341 | * manner from a packet being read. | |
342 | */ | |
6ae2364d | 343 | void |
5260325f | 344 | tty_parse_modes(int fd, int *n_bytes_ptr) |
8efc0c15 | 345 | { |
5260325f | 346 | struct termios tio; |
347 | int opcode, baud; | |
348 | int n_bytes = 0; | |
349 | int failure = 0; | |
30dcc918 | 350 | u_int (*get_arg)(void); |
351 | int arg, arg_size; | |
352 | ||
353 | if (compat20) { | |
354 | *n_bytes_ptr = packet_get_int(); | |
b4e7177c | 355 | debug3("tty_parse_modes: SSH2 n_bytes %d", *n_bytes_ptr); |
30dcc918 | 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 | } | |
5260325f | 364 | |
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 | */ | |
30dcc918 | 370 | if (tcgetattr(fd, &tio) == -1) { |
bbe88b6d | 371 | logit("tcgetattr: %.100s", strerror(errno)); |
5260325f | 372 | failure = -1; |
30dcc918 | 373 | } |
5260325f | 374 | |
375 | for (;;) { | |
376 | n_bytes += 1; | |
377 | opcode = packet_get_char(); | |
378 | switch (opcode) { | |
379 | case TTY_OP_END: | |
380 | goto set; | |
381 | ||
30dcc918 | 382 | /* XXX: future conflict possible */ |
383 | case TTY_OP_ISPEED_PROTO1: | |
384 | case TTY_OP_ISPEED_PROTO2: | |
5260325f | 385 | n_bytes += 4; |
386 | baud = packet_get_int(); | |
b4e7177c | 387 | debug3("tty_parse_modes: ispeed %d", baud); |
30dcc918 | 388 | if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) == -1) |
5260325f | 389 | error("cfsetispeed failed for %d", baud); |
390 | break; | |
391 | ||
30dcc918 | 392 | /* XXX: future conflict possible */ |
393 | case TTY_OP_OSPEED_PROTO1: | |
394 | case TTY_OP_OSPEED_PROTO2: | |
5260325f | 395 | n_bytes += 4; |
396 | baud = packet_get_int(); | |
b4e7177c | 397 | debug3("tty_parse_modes: ospeed %d", baud); |
30dcc918 | 398 | if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) == -1) |
5260325f | 399 | error("cfsetospeed failed for %d", baud); |
400 | break; | |
8efc0c15 | 401 | |
30dcc918 | 402 | #define TTYCHAR(NAME, OP) \ |
403 | case OP: \ | |
404 | n_bytes += arg_size; \ | |
879abf01 | 405 | tio.c_cc[NAME] = special_char_decode(get_arg()); \ |
b4e7177c | 406 | debug3("tty_parse_modes: %d %d", OP, tio.c_cc[NAME]); \ |
8efc0c15 | 407 | break; |
30dcc918 | 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; \ | |
b4e7177c | 415 | debug3("tty_parse_modes: %d %d", OP, arg); \ |
8efc0c15 | 416 | break; |
8efc0c15 | 417 | |
418 | #include "ttymodes.h" | |
419 | ||
420 | #undef TTYCHAR | |
421 | #undef TTYMODE | |
8efc0c15 | 422 | |
5260325f | 423 | default: |
424 | debug("Ignoring unsupported tty mode opcode %d (0x%x)", | |
184eed6a | 425 | opcode, opcode); |
30dcc918 | 426 | if (!compat20) { |
427 | /* | |
428 | * SSH1: | |
429 | * Opcodes 1 to 127 are defined to have | |
430 | * a one-byte argument. | |
762715ce | 431 | * Opcodes 128 to 159 are defined to have |
432 | * an integer argument. | |
433 | */ | |
30dcc918 | 434 | if (opcode > 0 && opcode < 128) { |
435 | n_bytes += 1; | |
436 | (void) packet_get_char(); | |
437 | break; | |
438 | } else if (opcode >= 128 && opcode < 160) { | |
762715ce | 439 | n_bytes += 4; |
440 | (void) packet_get_int(); | |
441 | break; | |
30dcc918 | 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 | */ | |
bbe88b6d | 450 | logit("parse_tty_modes: unknown opcode %d", opcode); |
30dcc918 | 451 | goto set; |
762715ce | 452 | } |
5260325f | 453 | } else { |
454 | /* | |
30dcc918 | 455 | * SSH2: |
e4664c3e | 456 | * Opcodes 1 to 159 are defined to have |
30dcc918 | 457 | * a uint32 argument. |
458 | * Opcodes 160 to 255 are undefined and | |
459 | * cause parsing to stop. | |
5260325f | 460 | */ |
30dcc918 | 461 | if (opcode > 0 && opcode < 160) { |
5260325f | 462 | n_bytes += 4; |
463 | (void) packet_get_int(); | |
464 | break; | |
30dcc918 | 465 | } else { |
bbe88b6d | 466 | logit("parse_tty_modes: unknown opcode %d", opcode); |
30dcc918 | 467 | goto set; |
5260325f | 468 | } |
762715ce | 469 | } |
8efc0c15 | 470 | } |
8efc0c15 | 471 | } |
8efc0c15 | 472 | |
5260325f | 473 | set: |
474 | if (*n_bytes_ptr != n_bytes) { | |
475 | *n_bytes_ptr = n_bytes; | |
bbe88b6d | 476 | logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d", |
30dcc918 | 477 | *n_bytes_ptr, n_bytes); |
5260325f | 478 | return; /* Don't process bytes passed */ |
479 | } | |
480 | if (failure == -1) | |
e4664c3e | 481 | return; /* Packet parsed ok but tcgetattr() failed */ |
8efc0c15 | 482 | |
5260325f | 483 | /* Set the new modes for the terminal. */ |
30dcc918 | 484 | if (tcsetattr(fd, TCSANOW, &tio) == -1) |
bbe88b6d | 485 | logit("Setting tty modes failed: %.100s", strerror(errno)); |
8efc0c15 | 486 | } |