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