xref: /openbsd-src/usr.bin/ssh/ttymodes.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /* $OpenBSD: ttymodes.c,v 1.33 2018/02/16 04:43:11 dtucker Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
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".
12  */
13 
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 
46 #include <sys/types.h>
47 
48 #include <errno.h>
49 #include <string.h>
50 #include <termios.h>
51 #include <stdarg.h>
52 
53 #include "packet.h"
54 #include "log.h"
55 #include "compat.h"
56 #include "buffer.h"
57 #include "compat.h"
58 
59 #define TTY_OP_END		0
60 /*
61  * uint32 (u_int) follows speed.
62  */
63 #define TTY_OP_ISPEED	128
64 #define TTY_OP_OSPEED	129
65 
66 /*
67  * Converts POSIX speed_t to a baud rate.  The values of the
68  * constants for speed_t are not themselves portable.
69  */
70 static int
71 speed_to_baud(speed_t speed)
72 {
73 	switch (speed) {
74 	case B0:
75 		return 0;
76 	case B50:
77 		return 50;
78 	case B75:
79 		return 75;
80 	case B110:
81 		return 110;
82 	case B134:
83 		return 134;
84 	case B150:
85 		return 150;
86 	case B200:
87 		return 200;
88 	case B300:
89 		return 300;
90 	case B600:
91 		return 600;
92 	case B1200:
93 		return 1200;
94 	case B1800:
95 		return 1800;
96 	case B2400:
97 		return 2400;
98 	case B4800:
99 		return 4800;
100 	case B9600:
101 		return 9600;
102 
103 #ifdef B19200
104 	case B19200:
105 		return 19200;
106 #else /* B19200 */
107 #ifdef EXTA
108 	case EXTA:
109 		return 19200;
110 #endif /* EXTA */
111 #endif /* B19200 */
112 
113 #ifdef B38400
114 	case B38400:
115 		return 38400;
116 #else /* B38400 */
117 #ifdef EXTB
118 	case EXTB:
119 		return 38400;
120 #endif /* EXTB */
121 #endif /* B38400 */
122 
123 #ifdef B7200
124 	case B7200:
125 		return 7200;
126 #endif /* B7200 */
127 #ifdef B14400
128 	case B14400:
129 		return 14400;
130 #endif /* B14400 */
131 #ifdef B28800
132 	case B28800:
133 		return 28800;
134 #endif /* B28800 */
135 #ifdef B57600
136 	case B57600:
137 		return 57600;
138 #endif /* B57600 */
139 #ifdef B76800
140 	case B76800:
141 		return 76800;
142 #endif /* B76800 */
143 #ifdef B115200
144 	case B115200:
145 		return 115200;
146 #endif /* B115200 */
147 #ifdef B230400
148 	case B230400:
149 		return 230400;
150 #endif /* B230400 */
151 	default:
152 		return 9600;
153 	}
154 }
155 
156 /*
157  * Converts a numeric baud rate to a POSIX speed_t.
158  */
159 static speed_t
160 baud_to_speed(int baud)
161 {
162 	switch (baud) {
163 	case 0:
164 		return B0;
165 	case 50:
166 		return B50;
167 	case 75:
168 		return B75;
169 	case 110:
170 		return B110;
171 	case 134:
172 		return B134;
173 	case 150:
174 		return B150;
175 	case 200:
176 		return B200;
177 	case 300:
178 		return B300;
179 	case 600:
180 		return B600;
181 	case 1200:
182 		return B1200;
183 	case 1800:
184 		return B1800;
185 	case 2400:
186 		return B2400;
187 	case 4800:
188 		return B4800;
189 	case 9600:
190 		return B9600;
191 
192 #ifdef B19200
193 	case 19200:
194 		return B19200;
195 #else /* B19200 */
196 #ifdef EXTA
197 	case 19200:
198 		return EXTA;
199 #endif /* EXTA */
200 #endif /* B19200 */
201 
202 #ifdef B38400
203 	case 38400:
204 		return B38400;
205 #else /* B38400 */
206 #ifdef EXTB
207 	case 38400:
208 		return EXTB;
209 #endif /* EXTB */
210 #endif /* B38400 */
211 
212 #ifdef B7200
213 	case 7200:
214 		return B7200;
215 #endif /* B7200 */
216 #ifdef B14400
217 	case 14400:
218 		return B14400;
219 #endif /* B14400 */
220 #ifdef B28800
221 	case 28800:
222 		return B28800;
223 #endif /* B28800 */
224 #ifdef B57600
225 	case 57600:
226 		return B57600;
227 #endif /* B57600 */
228 #ifdef B76800
229 	case 76800:
230 		return B76800;
231 #endif /* B76800 */
232 #ifdef B115200
233 	case 115200:
234 		return B115200;
235 #endif /* B115200 */
236 #ifdef B230400
237 	case 230400:
238 		return B230400;
239 #endif /* B230400 */
240 	default:
241 		return B9600;
242 	}
243 }
244 
245 /*
246  * Encodes terminal modes for the terminal referenced by fd
247  * or tiop in a portable manner, and appends the modes to a packet
248  * being constructed.
249  */
250 void
251 tty_make_modes(int fd, struct termios *tiop)
252 {
253 	struct termios tio;
254 	int baud;
255 	Buffer buf;
256 
257 	buffer_init(&buf);
258 
259 	if (tiop == NULL) {
260 		if (fd == -1) {
261 			debug("tty_make_modes: no fd or tio");
262 			goto end;
263 		}
264 		if (tcgetattr(fd, &tio) == -1) {
265 			logit("tcgetattr: %.100s", strerror(errno));
266 			goto end;
267 		}
268 	} else
269 		tio = *tiop;
270 
271 	/* Store input and output baud rates. */
272 	baud = speed_to_baud(cfgetospeed(&tio));
273 	buffer_put_char(&buf, TTY_OP_OSPEED);
274 	buffer_put_int(&buf, baud);
275 	baud = speed_to_baud(cfgetispeed(&tio));
276 	buffer_put_char(&buf, TTY_OP_ISPEED);
277 	buffer_put_int(&buf, baud);
278 
279 	/* Store values of mode flags. */
280 #define TTYCHAR(NAME, OP) \
281 	buffer_put_char(&buf, OP); \
282 	buffer_put_int(&buf, tio.c_cc[NAME]);
283 
284 #define SSH_TTYMODE_IUTF8 42  /* for SSH_BUG_UTF8TTYMODE */
285 
286 #define TTYMODE(NAME, FIELD, OP) \
287 	if (OP == SSH_TTYMODE_IUTF8 && (datafellows & SSH_BUG_UTF8TTYMODE)) { \
288 		debug3("%s: SSH_BUG_UTF8TTYMODE", __func__); \
289 	} else { \
290 		buffer_put_char(&buf, OP); \
291 		buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); \
292 	}
293 
294 #include "ttymodes.h"
295 
296 #undef TTYCHAR
297 #undef TTYMODE
298 
299 end:
300 	/* Mark end of mode data. */
301 	buffer_put_char(&buf, TTY_OP_END);
302 	packet_put_string(buffer_ptr(&buf), buffer_len(&buf));
303 	buffer_free(&buf);
304 }
305 
306 /*
307  * Decodes terminal modes for the terminal referenced by fd in a portable
308  * manner from a packet being read.
309  */
310 void
311 tty_parse_modes(int fd, int *n_bytes_ptr)
312 {
313 	struct termios tio;
314 	int opcode, baud;
315 	int n_bytes = 0;
316 	int failure = 0;
317 
318 	*n_bytes_ptr = packet_get_int();
319 	if (*n_bytes_ptr == 0)
320 		return;
321 
322 	/*
323 	 * Get old attributes for the terminal.  We will modify these
324 	 * flags. I am hoping that if there are any machine-specific
325 	 * modes, they will initially have reasonable values.
326 	 */
327 	if (tcgetattr(fd, &tio) == -1) {
328 		logit("tcgetattr: %.100s", strerror(errno));
329 		failure = -1;
330 	}
331 
332 	for (;;) {
333 		n_bytes += 1;
334 		opcode = packet_get_char();
335 		switch (opcode) {
336 		case TTY_OP_END:
337 			goto set;
338 
339 		case TTY_OP_ISPEED:
340 			n_bytes += 4;
341 			baud = packet_get_int();
342 			if (failure != -1 &&
343 			    cfsetispeed(&tio, baud_to_speed(baud)) == -1)
344 				error("cfsetispeed failed for %d", baud);
345 			break;
346 
347 		case TTY_OP_OSPEED:
348 			n_bytes += 4;
349 			baud = packet_get_int();
350 			if (failure != -1 &&
351 			    cfsetospeed(&tio, baud_to_speed(baud)) == -1)
352 				error("cfsetospeed failed for %d", baud);
353 			break;
354 
355 #define TTYCHAR(NAME, OP) \
356 	case OP: \
357 	  n_bytes += 4; \
358 	  tio.c_cc[NAME] = packet_get_int(); \
359 	  break;
360 #define TTYMODE(NAME, FIELD, OP) \
361 	case OP: \
362 	  n_bytes += 4; \
363 	  if (packet_get_int()) \
364 	    tio.FIELD |= NAME; \
365 	  else \
366 	    tio.FIELD &= ~NAME;	\
367 	  break;
368 
369 #include "ttymodes.h"
370 
371 #undef TTYCHAR
372 #undef TTYMODE
373 
374 		default:
375 			debug("Ignoring unsupported tty mode opcode %d (0x%x)",
376 			    opcode, opcode);
377 			/*
378 			 * SSH2:
379 			 * Opcodes 1 to 159 are defined to have a uint32
380 			 * argument.
381 			 * Opcodes 160 to 255 are undefined and cause parsing
382 			 * to stop.
383 			 */
384 			if (opcode > 0 && opcode < 160) {
385 				n_bytes += 4;
386 				(void) packet_get_int();
387 				break;
388 			} else {
389 				logit("parse_tty_modes: unknown opcode %d",
390 				    opcode);
391 				goto set;
392 			}
393 		}
394 	}
395 
396 set:
397 	if (*n_bytes_ptr != n_bytes) {
398 		*n_bytes_ptr = n_bytes;
399 		logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d",
400 		    *n_bytes_ptr, n_bytes);
401 		return;		/* Don't process bytes passed */
402 	}
403 	if (failure == -1)
404 		return;		/* Packet parsed ok but tcgetattr() failed */
405 
406 	/* Set the new modes for the terminal. */
407 	if (tcsetattr(fd, TCSANOW, &tio) == -1)
408 		logit("Setting tty modes failed: %.100s", strerror(errno));
409 }
410