]> andersk Git - openssh.git/blame - ttymodes.c
- stevesk@cvs.openbsd.org 2006/07/22 19:08:54
[openssh.git] / ttymodes.c
CommitLineData
028094f4 1/* $OpenBSD: ttymodes.c,v 1.24 2006/07/11 20:07:25 stevesk Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
bcbf86ec 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".
5260325f 12 */
8efc0c15 13
30dcc918 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
8efc0c15 46#include "includes.h"
1d3e026f 47
028094f4 48#include <errno.h>
1d3e026f 49#include <termios.h>
8efc0c15 50
51#include "packet.h"
42f11eb2 52#include "log.h"
53#include "ssh1.h"
30dcc918 54#include "compat.h"
55#include "buffer.h"
56#include "bufaux.h"
8efc0c15 57
30dcc918 58#define TTY_OP_END 0
59/*
60 * uint32 (u_int) follows speed in SSH1 and SSH2
61 */
62#define TTY_OP_ISPEED_PROTO1 192
63#define TTY_OP_OSPEED_PROTO1 193
64#define TTY_OP_ISPEED_PROTO2 128
65#define TTY_OP_OSPEED_PROTO2 129
8efc0c15 66
5260325f 67/*
68 * Converts POSIX speed_t to a baud rate. The values of the
69 * constants for speed_t are not themselves portable.
70 */
6ae2364d 71static int
5260325f 72speed_to_baud(speed_t speed)
8efc0c15 73{
5260325f 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;
8efc0c15 103
104#ifdef B19200
5260325f 105 case B19200:
106 return 19200;
8efc0c15 107#else /* B19200 */
108#ifdef EXTA
5260325f 109 case EXTA:
110 return 19200;
8efc0c15 111#endif /* EXTA */
112#endif /* B19200 */
113
114#ifdef B38400
5260325f 115 case B38400:
116 return 38400;
8efc0c15 117#else /* B38400 */
118#ifdef EXTB
5260325f 119 case EXTB:
120 return 38400;
8efc0c15 121#endif /* EXTB */
122#endif /* B38400 */
123
124#ifdef B7200
5260325f 125 case B7200:
126 return 7200;
8efc0c15 127#endif /* B7200 */
128#ifdef B14400
5260325f 129 case B14400:
130 return 14400;
8efc0c15 131#endif /* B14400 */
132#ifdef B28800
5260325f 133 case B28800:
134 return 28800;
8efc0c15 135#endif /* B28800 */
136#ifdef B57600
5260325f 137 case B57600:
138 return 57600;
8efc0c15 139#endif /* B57600 */
140#ifdef B76800
5260325f 141 case B76800:
142 return 76800;
8efc0c15 143#endif /* B76800 */
144#ifdef B115200
5260325f 145 case B115200:
146 return 115200;
8efc0c15 147#endif /* B115200 */
148#ifdef B230400
5260325f 149 case B230400:
150 return 230400;
8efc0c15 151#endif /* B230400 */
5260325f 152 default:
153 return 9600;
154 }
8efc0c15 155}
156
5260325f 157/*
158 * Converts a numeric baud rate to a POSIX speed_t.
159 */
6ae2364d 160static speed_t
5260325f 161baud_to_speed(int baud)
8efc0c15 162{
5260325f 163 switch (baud) {
30dcc918 164 case 0:
5260325f 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;
8efc0c15 192
193#ifdef B19200
5260325f 194 case 19200:
195 return B19200;
8efc0c15 196#else /* B19200 */
197#ifdef EXTA
5260325f 198 case 19200:
199 return EXTA;
8efc0c15 200#endif /* EXTA */
201#endif /* B19200 */
202
203#ifdef B38400
5260325f 204 case 38400:
205 return B38400;
8efc0c15 206#else /* B38400 */
207#ifdef EXTB
5260325f 208 case 38400:
209 return EXTB;
8efc0c15 210#endif /* EXTB */
211#endif /* B38400 */
212
213#ifdef B7200
5260325f 214 case 7200:
215 return B7200;
8efc0c15 216#endif /* B7200 */
217#ifdef B14400
5260325f 218 case 14400:
219 return B14400;
8efc0c15 220#endif /* B14400 */
221#ifdef B28800
5260325f 222 case 28800:
223 return B28800;
8efc0c15 224#endif /* B28800 */
225#ifdef B57600
5260325f 226 case 57600:
227 return B57600;
8efc0c15 228#endif /* B57600 */
229#ifdef B76800
5260325f 230 case 76800:
231 return B76800;
8efc0c15 232#endif /* B76800 */
233#ifdef B115200
5260325f 234 case 115200:
235 return B115200;
8efc0c15 236#endif /* B115200 */
237#ifdef B230400
5260325f 238 case 230400:
239 return B230400;
8efc0c15 240#endif /* B230400 */
5260325f 241 default:
242 return B9600;
243 }
8efc0c15 244}
245
879abf01 246/*
247 * Encode a special character into SSH line format.
248 */
249static u_int
250special_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 */
262static cc_t
263special_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
5260325f 272/*
273 * Encodes terminal modes for the terminal referenced by fd
30dcc918 274 * or tiop in a portable manner, and appends the modes to a packet
5260325f 275 * being constructed.
276 */
6ae2364d 277void
30dcc918 278tty_make_modes(int fd, struct termios *tiop)
8efc0c15 279{
5260325f 280 struct termios tio;
281 int baud;
30dcc918 282 Buffer buf;
283 int tty_op_ospeed, tty_op_ispeed;
284 void (*put_arg)(Buffer *, u_int);
5260325f 285
30dcc918 286 buffer_init(&buf);
287 if (compat20) {
288 tty_op_ospeed = TTY_OP_OSPEED_PROTO2;
289 tty_op_ispeed = TTY_OP_ISPEED_PROTO2;
290 put_arg = buffer_put_int;
291 } else {
292 tty_op_ospeed = TTY_OP_OSPEED_PROTO1;
293 tty_op_ispeed = TTY_OP_ISPEED_PROTO1;
294 put_arg = (void (*)(Buffer *, u_int)) buffer_put_char;
5260325f 295 }
30dcc918 296
297 if (tiop == NULL) {
298 if (tcgetattr(fd, &tio) == -1) {
bbe88b6d 299 logit("tcgetattr: %.100s", strerror(errno));
30dcc918 300 goto end;
301 }
302 } else
303 tio = *tiop;
304
5260325f 305 /* Store input and output baud rates. */
306 baud = speed_to_baud(cfgetospeed(&tio));
b4e7177c 307 debug3("tty_make_modes: ospeed %d", baud);
30dcc918 308 buffer_put_char(&buf, tty_op_ospeed);
309 buffer_put_int(&buf, baud);
5260325f 310 baud = speed_to_baud(cfgetispeed(&tio));
b4e7177c 311 debug3("tty_make_modes: ispeed %d", baud);
30dcc918 312 buffer_put_char(&buf, tty_op_ispeed);
313 buffer_put_int(&buf, baud);
5260325f 314
315 /* Store values of mode flags. */
8efc0c15 316#define TTYCHAR(NAME, OP) \
b4e7177c 317 debug3("tty_make_modes: %d %d", OP, tio.c_cc[NAME]); \
30dcc918 318 buffer_put_char(&buf, OP); \
879abf01 319 put_arg(&buf, special_char_encode(tio.c_cc[NAME]));
30dcc918 320
8efc0c15 321#define TTYMODE(NAME, FIELD, OP) \
b4e7177c 322 debug3("tty_make_modes: %d %d", OP, ((tio.FIELD & NAME) != 0)); \
30dcc918 323 buffer_put_char(&buf, OP); \
324 put_arg(&buf, ((tio.FIELD & NAME) != 0));
8efc0c15 325
326#include "ttymodes.h"
327
328#undef TTYCHAR
329#undef TTYMODE
8efc0c15 330
30dcc918 331end:
5260325f 332 /* Mark end of mode data. */
30dcc918 333 buffer_put_char(&buf, TTY_OP_END);
334 if (compat20)
335 packet_put_string(buffer_ptr(&buf), buffer_len(&buf));
336 else
337 packet_put_raw(buffer_ptr(&buf), buffer_len(&buf));
338 buffer_free(&buf);
8efc0c15 339}
340
5260325f 341/*
342 * Decodes terminal modes for the terminal referenced by fd in a portable
343 * manner from a packet being read.
344 */
6ae2364d 345void
5260325f 346tty_parse_modes(int fd, int *n_bytes_ptr)
8efc0c15 347{
5260325f 348 struct termios tio;
349 int opcode, baud;
350 int n_bytes = 0;
351 int failure = 0;
30dcc918 352 u_int (*get_arg)(void);
353 int arg, arg_size;
354
355 if (compat20) {
356 *n_bytes_ptr = packet_get_int();
b4e7177c 357 debug3("tty_parse_modes: SSH2 n_bytes %d", *n_bytes_ptr);
30dcc918 358 if (*n_bytes_ptr == 0)
359 return;
360 get_arg = packet_get_int;
361 arg_size = 4;
362 } else {
363 get_arg = packet_get_char;
364 arg_size = 1;
365 }
5260325f 366
367 /*
368 * Get old attributes for the terminal. We will modify these
369 * flags. I am hoping that if there are any machine-specific
370 * modes, they will initially have reasonable values.
371 */
30dcc918 372 if (tcgetattr(fd, &tio) == -1) {
bbe88b6d 373 logit("tcgetattr: %.100s", strerror(errno));
5260325f 374 failure = -1;
30dcc918 375 }
5260325f 376
377 for (;;) {
378 n_bytes += 1;
379 opcode = packet_get_char();
380 switch (opcode) {
381 case TTY_OP_END:
382 goto set;
383
30dcc918 384 /* XXX: future conflict possible */
385 case TTY_OP_ISPEED_PROTO1:
386 case TTY_OP_ISPEED_PROTO2:
5260325f 387 n_bytes += 4;
388 baud = packet_get_int();
b4e7177c 389 debug3("tty_parse_modes: ispeed %d", baud);
1cec12db 390 if (failure != -1 &&
391 cfsetispeed(&tio, baud_to_speed(baud)) == -1)
5260325f 392 error("cfsetispeed failed for %d", baud);
393 break;
394
30dcc918 395 /* XXX: future conflict possible */
396 case TTY_OP_OSPEED_PROTO1:
397 case TTY_OP_OSPEED_PROTO2:
5260325f 398 n_bytes += 4;
399 baud = packet_get_int();
b4e7177c 400 debug3("tty_parse_modes: ospeed %d", baud);
1cec12db 401 if (failure != -1 &&
402 cfsetospeed(&tio, baud_to_speed(baud)) == -1)
5260325f 403 error("cfsetospeed failed for %d", baud);
404 break;
8efc0c15 405
30dcc918 406#define TTYCHAR(NAME, OP) \
407 case OP: \
408 n_bytes += arg_size; \
879abf01 409 tio.c_cc[NAME] = special_char_decode(get_arg()); \
b4e7177c 410 debug3("tty_parse_modes: %d %d", OP, tio.c_cc[NAME]); \
8efc0c15 411 break;
30dcc918 412#define TTYMODE(NAME, FIELD, OP) \
413 case OP: \
414 n_bytes += arg_size; \
415 if ((arg = get_arg())) \
416 tio.FIELD |= NAME; \
417 else \
418 tio.FIELD &= ~NAME; \
b4e7177c 419 debug3("tty_parse_modes: %d %d", OP, arg); \
8efc0c15 420 break;
8efc0c15 421
422#include "ttymodes.h"
423
424#undef TTYCHAR
425#undef TTYMODE
8efc0c15 426
5260325f 427 default:
428 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
184eed6a 429 opcode, opcode);
30dcc918 430 if (!compat20) {
431 /*
432 * SSH1:
433 * Opcodes 1 to 127 are defined to have
434 * a one-byte argument.
762715ce 435 * Opcodes 128 to 159 are defined to have
436 * an integer argument.
437 */
30dcc918 438 if (opcode > 0 && opcode < 128) {
439 n_bytes += 1;
440 (void) packet_get_char();
441 break;
442 } else if (opcode >= 128 && opcode < 160) {
762715ce 443 n_bytes += 4;
444 (void) packet_get_int();
445 break;
30dcc918 446 } else {
447 /*
448 * It is a truly undefined opcode (160 to 255).
449 * We have no idea about its arguments. So we
1cec12db 450 * must stop parsing. Note that some data
451 * may be left in the packet; hopefully there
452 * is nothing more coming after the mode data.
30dcc918 453 */
1cec12db 454 logit("parse_tty_modes: unknown opcode %d",
455 opcode);
30dcc918 456 goto set;
762715ce 457 }
5260325f 458 } else {
459 /*
30dcc918 460 * SSH2:
e4664c3e 461 * Opcodes 1 to 159 are defined to have
30dcc918 462 * a uint32 argument.
463 * Opcodes 160 to 255 are undefined and
464 * cause parsing to stop.
5260325f 465 */
30dcc918 466 if (opcode > 0 && opcode < 160) {
5260325f 467 n_bytes += 4;
468 (void) packet_get_int();
469 break;
30dcc918 470 } else {
1cec12db 471 logit("parse_tty_modes: unknown opcode %d",
472 opcode);
30dcc918 473 goto set;
5260325f 474 }
762715ce 475 }
8efc0c15 476 }
8efc0c15 477 }
8efc0c15 478
5260325f 479set:
480 if (*n_bytes_ptr != n_bytes) {
481 *n_bytes_ptr = n_bytes;
bbe88b6d 482 logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d",
30dcc918 483 *n_bytes_ptr, n_bytes);
5260325f 484 return; /* Don't process bytes passed */
485 }
486 if (failure == -1)
e4664c3e 487 return; /* Packet parsed ok but tcgetattr() failed */
8efc0c15 488
5260325f 489 /* Set the new modes for the terminal. */
30dcc918 490 if (tcsetattr(fd, TCSANOW, &tio) == -1)
bbe88b6d 491 logit("Setting tty modes failed: %.100s", strerror(errno));
8efc0c15 492}
This page took 0.387826 seconds and 5 git commands to generate.