xref: /openbsd/usr.bin/ssh/ttymodes.c (revision e9d14800)
1 /* $OpenBSD: ttymodes.c,v 1.36 2021/01/27 09:26:54 djm 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 "sshbuf.h"
57 #include "ssherr.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
speed_to_baud(speed_t speed)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
baud_to_speed(int baud)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
ssh_tty_make_modes(struct ssh * ssh,int fd,struct termios * tiop)251 ssh_tty_make_modes(struct ssh *ssh, int fd, struct termios *tiop)
252 {
253 	struct termios tio;
254 	struct sshbuf *buf;
255 	int r, ibaud, obaud;
256 
257 	if ((buf = sshbuf_new()) == NULL)
258 		fatal_f("sshbuf_new failed");
259 
260 	if (tiop == NULL) {
261 		if (fd == -1) {
262 			debug_f("no fd or tio");
263 			goto end;
264 		}
265 		if (tcgetattr(fd, &tio) == -1) {
266 			logit("tcgetattr: %.100s", strerror(errno));
267 			goto end;
268 		}
269 	} else
270 		tio = *tiop;
271 
272 	/* Store input and output baud rates. */
273 	obaud = speed_to_baud(cfgetospeed(&tio));
274 	ibaud = speed_to_baud(cfgetispeed(&tio));
275 	if ((r = sshbuf_put_u8(buf, TTY_OP_OSPEED)) != 0 ||
276 	    (r = sshbuf_put_u32(buf, obaud)) != 0 ||
277 	    (r = sshbuf_put_u8(buf, TTY_OP_ISPEED)) != 0 ||
278 	    (r = sshbuf_put_u32(buf, ibaud)) != 0)
279 		fatal_fr(r, "compose");
280 
281 	/* Store values of mode flags. */
282 #define TTYCHAR(NAME, OP) \
283 	if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
284 	    (r = sshbuf_put_u32(buf, tio.c_cc[NAME])) != 0) \
285 		fatal_fr(r, "compose %s", #NAME);
286 
287 #define SSH_TTYMODE_IUTF8 42  /* for SSH_BUG_UTF8TTYMODE */
288 
289 #define TTYMODE(NAME, FIELD, OP) \
290 	if (OP == SSH_TTYMODE_IUTF8 && (ssh->compat & SSH_BUG_UTF8TTYMODE)) { \
291 		debug3_f("SSH_BUG_UTF8TTYMODE"); \
292 	} else if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
293 	    (r = sshbuf_put_u32(buf, ((tio.FIELD & NAME) != 0))) != 0) \
294 		fatal_fr(r, "compose %s", #NAME);
295 
296 #include "ttymodes.h"
297 
298 #undef TTYCHAR
299 #undef TTYMODE
300 
301 end:
302 	/* Mark end of mode data. */
303 	if ((r = sshbuf_put_u8(buf, TTY_OP_END)) != 0 ||
304 	    (r = sshpkt_put_stringb(ssh, buf)) != 0)
305 		fatal_fr(r, "compose end");
306 	sshbuf_free(buf);
307 }
308 
309 /*
310  * Decodes terminal modes for the terminal referenced by fd in a portable
311  * manner from a packet being read.
312  */
313 void
ssh_tty_parse_modes(struct ssh * ssh,int fd)314 ssh_tty_parse_modes(struct ssh *ssh, int fd)
315 {
316 	struct termios tio;
317 	struct sshbuf *buf;
318 	const u_char *data;
319 	u_char opcode;
320 	u_int baud, u;
321 	int r, failure = 0;
322 	size_t len;
323 
324 	if ((r = sshpkt_get_string_direct(ssh, &data, &len)) != 0)
325 		fatal_fr(r, "parse");
326 	if (len == 0)
327 		return;
328 	if ((buf = sshbuf_from(data, len)) == NULL) {
329 		error_f("sshbuf_from failed");
330 		return;
331 	}
332 
333 	/*
334 	 * Get old attributes for the terminal.  We will modify these
335 	 * flags. I am hoping that if there are any machine-specific
336 	 * modes, they will initially have reasonable values.
337 	 */
338 	if (tcgetattr(fd, &tio) == -1) {
339 		logit("tcgetattr: %.100s", strerror(errno));
340 		failure = -1;
341 	}
342 
343 	while (sshbuf_len(buf) > 0) {
344 		if ((r = sshbuf_get_u8(buf, &opcode)) != 0)
345 			fatal_fr(r, "parse opcode");
346 		switch (opcode) {
347 		case TTY_OP_END:
348 			goto set;
349 
350 		case TTY_OP_ISPEED:
351 			if ((r = sshbuf_get_u32(buf, &baud)) != 0)
352 				fatal_fr(r, "parse ispeed");
353 			if (failure != -1 &&
354 			    cfsetispeed(&tio, baud_to_speed(baud)) == -1)
355 				error("cfsetispeed failed for %d", baud);
356 			break;
357 
358 		case TTY_OP_OSPEED:
359 			if ((r = sshbuf_get_u32(buf, &baud)) != 0)
360 				fatal_fr(r, "parse ospeed");
361 			if (failure != -1 &&
362 			    cfsetospeed(&tio, baud_to_speed(baud)) == -1)
363 				error("cfsetospeed failed for %d", baud);
364 			break;
365 
366 #define TTYCHAR(NAME, OP) \
367 		case OP: \
368 			if ((r = sshbuf_get_u32(buf, &u)) != 0) \
369 				fatal_fr(r, "parse %s", #NAME); \
370 			tio.c_cc[NAME] = u; \
371 			break;
372 #define TTYMODE(NAME, FIELD, OP) \
373 		case OP: \
374 			if ((r = sshbuf_get_u32(buf, &u)) != 0) \
375 				fatal_fr(r, "parse %s", #NAME); \
376 			if (u) \
377 				tio.FIELD |= NAME; \
378 			else \
379 				tio.FIELD &= ~NAME; \
380 			break;
381 
382 #include "ttymodes.h"
383 
384 #undef TTYCHAR
385 #undef TTYMODE
386 
387 		default:
388 			debug("Ignoring unsupported tty mode opcode %d (0x%x)",
389 			    opcode, opcode);
390 			/*
391 			 * SSH2:
392 			 * Opcodes 1 to 159 are defined to have a uint32
393 			 * argument.
394 			 * Opcodes 160 to 255 are undefined and cause parsing
395 			 * to stop.
396 			 */
397 			if (opcode > 0 && opcode < 160) {
398 				if ((r = sshbuf_get_u32(buf, NULL)) != 0)
399 					fatal_fr(r, "parse arg");
400 				break;
401 			} else {
402 				logit_f("unknown opcode %d", opcode);
403 				goto set;
404 			}
405 		}
406 	}
407 
408 set:
409 	len = sshbuf_len(buf);
410 	sshbuf_free(buf);
411 	if (len > 0) {
412 		logit_f("%zu bytes left", len);
413 		return;		/* Don't process bytes passed */
414 	}
415 	if (failure == -1)
416 		return;		/* Packet parsed ok but tcgetattr() failed */
417 
418 	/* Set the new modes for the terminal. */
419 	if (tcsetattr(fd, TCSANOW, &tio) == -1)
420 		logit("Setting tty modes failed: %.100s", strerror(errno));
421 }
422