blob: a7a3e93010874dc1fdd04b89c21a930f630af9e6 [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"
Damien Millere4340be2000-09-16 13:29:08 +110018RCSID("$OpenBSD: ttymodes.c,v 1.8 2000/09/07 20:27:55 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "packet.h"
21#include "ssh.h"
22
23#define TTY_OP_END 0
Damien Miller95def091999-11-25 00:26:21 +110024#define TTY_OP_ISPEED 192 /* int follows */
25#define TTY_OP_OSPEED 193 /* int follows */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller95def091999-11-25 00:26:21 +110027/*
28 * Converts POSIX speed_t to a baud rate. The values of the
29 * constants for speed_t are not themselves portable.
30 */
Damien Miller4af51302000-04-16 11:18:38 +100031static int
Damien Miller95def091999-11-25 00:26:21 +110032speed_to_baud(speed_t speed)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033{
Damien Miller95def091999-11-25 00:26:21 +110034 switch (speed) {
35 case B0:
36 return 0;
37 case B50:
38 return 50;
39 case B75:
40 return 75;
41 case B110:
42 return 110;
43 case B134:
44 return 134;
45 case B150:
46 return 150;
47 case B200:
48 return 200;
49 case B300:
50 return 300;
51 case B600:
52 return 600;
53 case B1200:
54 return 1200;
55 case B1800:
56 return 1800;
57 case B2400:
58 return 2400;
59 case B4800:
60 return 4800;
61 case B9600:
62 return 9600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
64#ifdef B19200
Damien Miller95def091999-11-25 00:26:21 +110065 case B19200:
66 return 19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#else /* B19200 */
68#ifdef EXTA
Damien Miller95def091999-11-25 00:26:21 +110069 case EXTA:
70 return 19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#endif /* EXTA */
72#endif /* B19200 */
73
74#ifdef B38400
Damien Miller95def091999-11-25 00:26:21 +110075 case B38400:
76 return 38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077#else /* B38400 */
78#ifdef EXTB
Damien Miller95def091999-11-25 00:26:21 +110079 case EXTB:
80 return 38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081#endif /* EXTB */
82#endif /* B38400 */
83
84#ifdef B7200
Damien Miller95def091999-11-25 00:26:21 +110085 case B7200:
86 return 7200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087#endif /* B7200 */
88#ifdef B14400
Damien Miller95def091999-11-25 00:26:21 +110089 case B14400:
90 return 14400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#endif /* B14400 */
92#ifdef B28800
Damien Miller95def091999-11-25 00:26:21 +110093 case B28800:
94 return 28800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095#endif /* B28800 */
96#ifdef B57600
Damien Miller95def091999-11-25 00:26:21 +110097 case B57600:
98 return 57600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099#endif /* B57600 */
100#ifdef B76800
Damien Miller95def091999-11-25 00:26:21 +1100101 case B76800:
102 return 76800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103#endif /* B76800 */
104#ifdef B115200
Damien Miller95def091999-11-25 00:26:21 +1100105 case B115200:
106 return 115200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107#endif /* B115200 */
108#ifdef B230400
Damien Miller95def091999-11-25 00:26:21 +1100109 case B230400:
110 return 230400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111#endif /* B230400 */
Damien Miller95def091999-11-25 00:26:21 +1100112 default:
113 return 9600;
114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115}
116
Damien Miller95def091999-11-25 00:26:21 +1100117/*
118 * Converts a numeric baud rate to a POSIX speed_t.
119 */
Damien Miller4af51302000-04-16 11:18:38 +1000120static speed_t
Damien Miller95def091999-11-25 00:26:21 +1100121baud_to_speed(int baud)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122{
Damien Miller95def091999-11-25 00:26:21 +1100123 switch (baud) {
124 case 0:
125 return B0;
126 case 50:
127 return B50;
128 case 75:
129 return B75;
130 case 110:
131 return B110;
132 case 134:
133 return B134;
134 case 150:
135 return B150;
136 case 200:
137 return B200;
138 case 300:
139 return B300;
140 case 600:
141 return B600;
142 case 1200:
143 return B1200;
144 case 1800:
145 return B1800;
146 case 2400:
147 return B2400;
148 case 4800:
149 return B4800;
150 case 9600:
151 return B9600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
153#ifdef B19200
Damien Miller95def091999-11-25 00:26:21 +1100154 case 19200:
155 return B19200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156#else /* B19200 */
157#ifdef EXTA
Damien Miller95def091999-11-25 00:26:21 +1100158 case 19200:
159 return EXTA;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160#endif /* EXTA */
161#endif /* B19200 */
162
163#ifdef B38400
Damien Miller95def091999-11-25 00:26:21 +1100164 case 38400:
165 return B38400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166#else /* B38400 */
167#ifdef EXTB
Damien Miller95def091999-11-25 00:26:21 +1100168 case 38400:
169 return EXTB;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170#endif /* EXTB */
171#endif /* B38400 */
172
173#ifdef B7200
Damien Miller95def091999-11-25 00:26:21 +1100174 case 7200:
175 return B7200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176#endif /* B7200 */
177#ifdef B14400
Damien Miller95def091999-11-25 00:26:21 +1100178 case 14400:
179 return B14400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180#endif /* B14400 */
181#ifdef B28800
Damien Miller95def091999-11-25 00:26:21 +1100182 case 28800:
183 return B28800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184#endif /* B28800 */
185#ifdef B57600
Damien Miller95def091999-11-25 00:26:21 +1100186 case 57600:
187 return B57600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188#endif /* B57600 */
189#ifdef B76800
Damien Miller95def091999-11-25 00:26:21 +1100190 case 76800:
191 return B76800;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192#endif /* B76800 */
193#ifdef B115200
Damien Miller95def091999-11-25 00:26:21 +1100194 case 115200:
195 return B115200;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196#endif /* B115200 */
197#ifdef B230400
Damien Miller95def091999-11-25 00:26:21 +1100198 case 230400:
199 return B230400;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200#endif /* B230400 */
Damien Miller95def091999-11-25 00:26:21 +1100201 default:
202 return B9600;
203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204}
205
Damien Miller95def091999-11-25 00:26:21 +1100206/*
207 * Encodes terminal modes for the terminal referenced by fd
208 * in a portable manner, and appends the modes to a packet
209 * being constructed.
210 */
Damien Miller4af51302000-04-16 11:18:38 +1000211void
Damien Miller95def091999-11-25 00:26:21 +1100212tty_make_modes(int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 struct termios tio;
215 int baud;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller95def091999-11-25 00:26:21 +1100217 if (tcgetattr(fd, &tio) < 0) {
218 packet_put_char(TTY_OP_END);
219 log("tcgetattr: %.100s", strerror(errno));
220 return;
221 }
222 /* Store input and output baud rates. */
223 baud = speed_to_baud(cfgetospeed(&tio));
224 packet_put_char(TTY_OP_OSPEED);
225 packet_put_int(baud);
226 baud = speed_to_baud(cfgetispeed(&tio));
227 packet_put_char(TTY_OP_ISPEED);
228 packet_put_int(baud);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Miller95def091999-11-25 00:26:21 +1100230 /* Store values of mode flags. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231#define TTYCHAR(NAME, OP) \
232 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
233#define TTYMODE(NAME, FIELD, OP) \
234 packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
235#define SGTTYCHAR(NAME, OP)
236#define SGTTYMODE(NAME, FIELD, OP)
237#define SGTTYMODEN(NAME, FIELD, OP)
238
239#include "ttymodes.h"
240
241#undef TTYCHAR
242#undef TTYMODE
243#undef SGTTYCHAR
244#undef SGTTYMODE
245#undef SGTTYMODEN
246
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Mark end of mode data. */
248 packet_put_char(TTY_OP_END);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
250
Damien Miller95def091999-11-25 00:26:21 +1100251/*
252 * Decodes terminal modes for the terminal referenced by fd in a portable
253 * manner from a packet being read.
254 */
Damien Miller4af51302000-04-16 11:18:38 +1000255void
Damien Miller95def091999-11-25 00:26:21 +1100256tty_parse_modes(int fd, int *n_bytes_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257{
Damien Miller95def091999-11-25 00:26:21 +1100258 struct termios tio;
259 int opcode, baud;
260 int n_bytes = 0;
261 int failure = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Damien Miller95def091999-11-25 00:26:21 +1100263 /*
264 * Get old attributes for the terminal. We will modify these
265 * flags. I am hoping that if there are any machine-specific
266 * modes, they will initially have reasonable values.
267 */
268 if (tcgetattr(fd, &tio) < 0)
269 failure = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller95def091999-11-25 00:26:21 +1100271 for (;;) {
272 n_bytes += 1;
273 opcode = packet_get_char();
274 switch (opcode) {
275 case TTY_OP_END:
276 goto set;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 case TTY_OP_ISPEED:
279 n_bytes += 4;
280 baud = packet_get_int();
281 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
282 error("cfsetispeed failed for %d", baud);
283 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller95def091999-11-25 00:26:21 +1100285 case TTY_OP_OSPEED:
286 n_bytes += 4;
287 baud = packet_get_int();
288 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
289 error("cfsetospeed failed for %d", baud);
290 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
292#define TTYCHAR(NAME, OP) \
293 case OP: \
294 n_bytes += 1; \
295 tio.c_cc[NAME] = packet_get_char(); \
296 break;
297#define TTYMODE(NAME, FIELD, OP) \
298 case OP: \
299 n_bytes += 1; \
300 if (packet_get_char()) \
301 tio.FIELD |= NAME; \
302 else \
303 tio.FIELD &= ~NAME; \
304 break;
305#define SGTTYCHAR(NAME, OP)
306#define SGTTYMODE(NAME, FIELD, OP)
307#define SGTTYMODEN(NAME, FIELD, OP)
308
309#include "ttymodes.h"
310
311#undef TTYCHAR
312#undef TTYMODE
313#undef SGTTYCHAR
314#undef SGTTYMODE
315#undef SGTTYMODEN
316
Damien Miller95def091999-11-25 00:26:21 +1100317 default:
318 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
319 opcode, opcode);
320 /*
321 * Opcodes 0 to 127 are defined to have
322 * a one-byte argument.
323 */
324 if (opcode >= 0 && opcode < 128) {
325 n_bytes += 1;
326 (void) packet_get_char();
327 break;
328 } else {
329 /*
330 * Opcodes 128 to 159 are defined to have
331 * an integer argument.
332 */
333 if (opcode >= 128 && opcode < 160) {
334 n_bytes += 4;
335 (void) packet_get_int();
336 break;
337 }
338 }
339 /*
340 * It is a truly undefined opcode (160 to 255).
341 * We have no idea about its arguments. So we
342 * must stop parsing. Note that some data may be
343 * left in the packet; hopefully there is nothing
344 * more coming after the mode data.
345 */
346 log("parse_tty_modes: unknown opcode %d", opcode);
347 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
348 goto set;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller95def091999-11-25 00:26:21 +1100352set:
353 if (*n_bytes_ptr != n_bytes) {
354 *n_bytes_ptr = n_bytes;
355 return; /* Don't process bytes passed */
356 }
357 if (failure == -1)
358 return; /* Packet parsed ok but tty stuff failed */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Damien Miller95def091999-11-25 00:26:21 +1100360 /* Set the new modes for the terminal. */
361 if (tcsetattr(fd, TCSANOW, &tio) < 0)
362 log("Setting tty modes failed: %.100s", strerror(errno));
363 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364}