blob: 2516e9310655a731dc21748bc6ebf6953ea4f423 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * 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 Millere4340be2000-09-16 13:29:08 +11009 *
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 Miller95def091999-11-25 00:26:21 +110015 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000018RCSID("$OpenBSD: ttymodes.c,v 1.10 2001/01/21 19:06:01 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "log.h"
22#include "ssh1.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023
24#define TTY_OP_END 0
Damien Miller95def091999-11-25 00:26:21 +110025#define TTY_OP_ISPEED 192 /* int follows */
26#define TTY_OP_OSPEED 193 /* int follows */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller95def091999-11-25 00:26:21 +110028/*
29 * Converts POSIX speed_t to a baud rate. The values of the
30 * constants for speed_t are not themselves portable.
31 */
Damien Miller4af51302000-04-16 11:18:38 +100032static int
Damien Miller95def091999-11-25 00:26:21 +110033speed_to_baud(speed_t speed)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034{
Damien Miller95def091999-11-25 00:26:21 +110035 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 Millerd4a8b7e1999-10-27 13:42:43 +100064
65#ifdef B19200
Damien Miller95def091999-11-25 00:26:21 +110066 case B19200:
67 return 19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#else /* B19200 */
69#ifdef EXTA
Damien Miller95def091999-11-25 00:26:21 +110070 case EXTA:
71 return 19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#endif /* EXTA */
73#endif /* B19200 */
74
75#ifdef B38400
Damien Miller95def091999-11-25 00:26:21 +110076 case B38400:
77 return 38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#else /* B38400 */
79#ifdef EXTB
Damien Miller95def091999-11-25 00:26:21 +110080 case EXTB:
81 return 38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082#endif /* EXTB */
83#endif /* B38400 */
84
85#ifdef B7200
Damien Miller95def091999-11-25 00:26:21 +110086 case B7200:
87 return 7200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088#endif /* B7200 */
89#ifdef B14400
Damien Miller95def091999-11-25 00:26:21 +110090 case B14400:
91 return 14400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092#endif /* B14400 */
93#ifdef B28800
Damien Miller95def091999-11-25 00:26:21 +110094 case B28800:
95 return 28800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096#endif /* B28800 */
97#ifdef B57600
Damien Miller95def091999-11-25 00:26:21 +110098 case B57600:
99 return 57600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100#endif /* B57600 */
101#ifdef B76800
Damien Miller95def091999-11-25 00:26:21 +1100102 case B76800:
103 return 76800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104#endif /* B76800 */
105#ifdef B115200
Damien Miller95def091999-11-25 00:26:21 +1100106 case B115200:
107 return 115200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108#endif /* B115200 */
109#ifdef B230400
Damien Miller95def091999-11-25 00:26:21 +1100110 case B230400:
111 return 230400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112#endif /* B230400 */
Damien Miller95def091999-11-25 00:26:21 +1100113 default:
114 return 9600;
115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116}
117
Damien Miller95def091999-11-25 00:26:21 +1100118/*
119 * Converts a numeric baud rate to a POSIX speed_t.
120 */
Damien Miller4af51302000-04-16 11:18:38 +1000121static speed_t
Damien Miller95def091999-11-25 00:26:21 +1100122baud_to_speed(int baud)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Damien Miller95def091999-11-25 00:26:21 +1100124 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 Millerd4a8b7e1999-10-27 13:42:43 +1000153
154#ifdef B19200
Damien Miller95def091999-11-25 00:26:21 +1100155 case 19200:
156 return B19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157#else /* B19200 */
158#ifdef EXTA
Damien Miller95def091999-11-25 00:26:21 +1100159 case 19200:
160 return EXTA;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161#endif /* EXTA */
162#endif /* B19200 */
163
164#ifdef B38400
Damien Miller95def091999-11-25 00:26:21 +1100165 case 38400:
166 return B38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167#else /* B38400 */
168#ifdef EXTB
Damien Miller95def091999-11-25 00:26:21 +1100169 case 38400:
170 return EXTB;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171#endif /* EXTB */
172#endif /* B38400 */
173
174#ifdef B7200
Damien Miller95def091999-11-25 00:26:21 +1100175 case 7200:
176 return B7200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177#endif /* B7200 */
178#ifdef B14400
Damien Miller95def091999-11-25 00:26:21 +1100179 case 14400:
180 return B14400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181#endif /* B14400 */
182#ifdef B28800
Damien Miller95def091999-11-25 00:26:21 +1100183 case 28800:
184 return B28800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185#endif /* B28800 */
186#ifdef B57600
Damien Miller95def091999-11-25 00:26:21 +1100187 case 57600:
188 return B57600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189#endif /* B57600 */
190#ifdef B76800
Damien Miller95def091999-11-25 00:26:21 +1100191 case 76800:
192 return B76800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193#endif /* B76800 */
194#ifdef B115200
Damien Miller95def091999-11-25 00:26:21 +1100195 case 115200:
196 return B115200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197#endif /* B115200 */
198#ifdef B230400
Damien Miller95def091999-11-25 00:26:21 +1100199 case 230400:
200 return B230400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201#endif /* B230400 */
Damien Miller95def091999-11-25 00:26:21 +1100202 default:
203 return B9600;
204 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205}
206
Damien Miller95def091999-11-25 00:26:21 +1100207/*
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 Miller4af51302000-04-16 11:18:38 +1000212void
Damien Miller95def091999-11-25 00:26:21 +1100213tty_make_modes(int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 struct termios tio;
216 int baud;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 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 Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 /* Store values of mode flags. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232#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);
236#define SGTTYCHAR(NAME, OP)
237#define SGTTYMODE(NAME, FIELD, OP)
238#define SGTTYMODEN(NAME, FIELD, OP)
239
240#include "ttymodes.h"
241
242#undef TTYCHAR
243#undef TTYMODE
244#undef SGTTYCHAR
245#undef SGTTYMODE
246#undef SGTTYMODEN
247
Damien Miller95def091999-11-25 00:26:21 +1100248 /* Mark end of mode data. */
249 packet_put_char(TTY_OP_END);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250}
251
Damien Miller95def091999-11-25 00:26:21 +1100252/*
253 * Decodes terminal modes for the terminal referenced by fd in a portable
254 * manner from a packet being read.
255 */
Damien Miller4af51302000-04-16 11:18:38 +1000256void
Damien Miller95def091999-11-25 00:26:21 +1100257tty_parse_modes(int fd, int *n_bytes_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258{
Damien Miller95def091999-11-25 00:26:21 +1100259 struct termios tio;
260 int opcode, baud;
261 int n_bytes = 0;
262 int failure = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller95def091999-11-25 00:26:21 +1100264 /*
265 * Get old attributes for the terminal. We will modify these
266 * flags. I am hoping that if there are any machine-specific
267 * modes, they will initially have reasonable values.
268 */
269 if (tcgetattr(fd, &tio) < 0)
270 failure = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller95def091999-11-25 00:26:21 +1100272 for (;;) {
273 n_bytes += 1;
274 opcode = packet_get_char();
275 switch (opcode) {
276 case TTY_OP_END:
277 goto set;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller95def091999-11-25 00:26:21 +1100279 case TTY_OP_ISPEED:
280 n_bytes += 4;
281 baud = packet_get_int();
282 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
283 error("cfsetispeed failed for %d", baud);
284 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller95def091999-11-25 00:26:21 +1100286 case TTY_OP_OSPEED:
287 n_bytes += 4;
288 baud = packet_get_int();
289 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
290 error("cfsetospeed failed for %d", baud);
291 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
293#define TTYCHAR(NAME, OP) \
294 case OP: \
295 n_bytes += 1; \
296 tio.c_cc[NAME] = packet_get_char(); \
297 break;
298#define TTYMODE(NAME, FIELD, OP) \
299 case OP: \
300 n_bytes += 1; \
301 if (packet_get_char()) \
302 tio.FIELD |= NAME; \
303 else \
304 tio.FIELD &= ~NAME; \
305 break;
306#define SGTTYCHAR(NAME, OP)
307#define SGTTYMODE(NAME, FIELD, OP)
308#define SGTTYMODEN(NAME, FIELD, OP)
309
310#include "ttymodes.h"
311
312#undef TTYCHAR
313#undef TTYMODE
314#undef SGTTYCHAR
315#undef SGTTYMODE
316#undef SGTTYMODEN
317
Damien Miller95def091999-11-25 00:26:21 +1100318 default:
319 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
320 opcode, opcode);
321 /*
322 * Opcodes 0 to 127 are defined to have
323 * a one-byte argument.
324 */
325 if (opcode >= 0 && opcode < 128) {
326 n_bytes += 1;
327 (void) packet_get_char();
328 break;
329 } else {
330 /*
331 * Opcodes 128 to 159 are defined to have
332 * an integer argument.
333 */
334 if (opcode >= 128 && opcode < 160) {
335 n_bytes += 4;
336 (void) packet_get_int();
337 break;
338 }
339 }
340 /*
341 * It is a truly undefined opcode (160 to 255).
342 * We have no idea about its arguments. So we
343 * must stop parsing. Note that some data may be
344 * left in the packet; hopefully there is nothing
345 * more coming after the mode data.
346 */
347 log("parse_tty_modes: unknown opcode %d", opcode);
348 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
349 goto set;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller95def091999-11-25 00:26:21 +1100353set:
354 if (*n_bytes_ptr != n_bytes) {
355 *n_bytes_ptr = n_bytes;
356 return; /* Don't process bytes passed */
357 }
358 if (failure == -1)
359 return; /* Packet parsed ok but tty stuff failed */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Set the new modes for the terminal. */
362 if (tcsetattr(fd, TCSANOW, &tio) < 0)
363 log("Setting tty modes failed: %.100s", strerror(errno));
364 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365}