Annotation of src/usr.bin/ssh/ttymodes.c, Revision 1.34
1.34 ! markus 1: /* $OpenBSD: ttymodes.c,v 1.33 2018/02/16 04:43:11 dtucker Exp $ */
1.1 deraadt 2: /*
1.4 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.8 deraadt 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".
1.4 deraadt 12: */
1.1 deraadt 13:
1.12 stevesk 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:
1.26 deraadt 46: #include <sys/types.h>
1.20 stevesk 47:
1.24 stevesk 48: #include <errno.h>
1.25 stevesk 49: #include <string.h>
1.20 stevesk 50: #include <termios.h>
1.26 deraadt 51: #include <stdarg.h>
1.1 deraadt 52:
53: #include "packet.h"
1.10 markus 54: #include "log.h"
1.12 stevesk 55: #include "compat.h"
1.34 ! markus 56: #include "sshbuf.h"
! 57: #include "ssherr.h"
1.1 deraadt 58:
1.12 stevesk 59: #define TTY_OP_END 0
60: /*
1.32 djm 61: * uint32 (u_int) follows speed.
1.12 stevesk 62: */
1.32 djm 63: #define TTY_OP_ISPEED 128
64: #define TTY_OP_OSPEED 129
1.1 deraadt 65:
1.4 deraadt 66: /*
67: * Converts POSIX speed_t to a baud rate. The values of the
68: * constants for speed_t are not themselves portable.
69: */
1.6 markus 70: static int
1.3 markus 71: speed_to_baud(speed_t speed)
1.1 deraadt 72: {
1.3 markus 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;
1.1 deraadt 102:
103: #ifdef B19200
1.3 markus 104: case B19200:
105: return 19200;
1.1 deraadt 106: #else /* B19200 */
107: #ifdef EXTA
1.3 markus 108: case EXTA:
109: return 19200;
1.1 deraadt 110: #endif /* EXTA */
111: #endif /* B19200 */
112:
113: #ifdef B38400
1.3 markus 114: case B38400:
115: return 38400;
1.1 deraadt 116: #else /* B38400 */
117: #ifdef EXTB
1.3 markus 118: case EXTB:
119: return 38400;
1.1 deraadt 120: #endif /* EXTB */
121: #endif /* B38400 */
122:
123: #ifdef B7200
1.3 markus 124: case B7200:
125: return 7200;
1.1 deraadt 126: #endif /* B7200 */
127: #ifdef B14400
1.3 markus 128: case B14400:
129: return 14400;
1.1 deraadt 130: #endif /* B14400 */
131: #ifdef B28800
1.3 markus 132: case B28800:
133: return 28800;
1.1 deraadt 134: #endif /* B28800 */
135: #ifdef B57600
1.3 markus 136: case B57600:
137: return 57600;
1.1 deraadt 138: #endif /* B57600 */
139: #ifdef B76800
1.3 markus 140: case B76800:
141: return 76800;
1.1 deraadt 142: #endif /* B76800 */
143: #ifdef B115200
1.3 markus 144: case B115200:
145: return 115200;
1.1 deraadt 146: #endif /* B115200 */
147: #ifdef B230400
1.3 markus 148: case B230400:
149: return 230400;
1.1 deraadt 150: #endif /* B230400 */
1.3 markus 151: default:
152: return 9600;
153: }
1.1 deraadt 154: }
155:
1.4 deraadt 156: /*
157: * Converts a numeric baud rate to a POSIX speed_t.
158: */
1.6 markus 159: static speed_t
1.3 markus 160: baud_to_speed(int baud)
1.1 deraadt 161: {
1.3 markus 162: switch (baud) {
1.12 stevesk 163: case 0:
1.3 markus 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;
1.1 deraadt 191:
192: #ifdef B19200
1.3 markus 193: case 19200:
194: return B19200;
1.1 deraadt 195: #else /* B19200 */
196: #ifdef EXTA
1.3 markus 197: case 19200:
198: return EXTA;
1.1 deraadt 199: #endif /* EXTA */
200: #endif /* B19200 */
201:
202: #ifdef B38400
1.3 markus 203: case 38400:
204: return B38400;
1.1 deraadt 205: #else /* B38400 */
206: #ifdef EXTB
1.3 markus 207: case 38400:
208: return EXTB;
1.1 deraadt 209: #endif /* EXTB */
210: #endif /* B38400 */
211:
212: #ifdef B7200
1.3 markus 213: case 7200:
214: return B7200;
1.1 deraadt 215: #endif /* B7200 */
216: #ifdef B14400
1.3 markus 217: case 14400:
218: return B14400;
1.1 deraadt 219: #endif /* B14400 */
220: #ifdef B28800
1.3 markus 221: case 28800:
222: return B28800;
1.1 deraadt 223: #endif /* B28800 */
224: #ifdef B57600
1.3 markus 225: case 57600:
226: return B57600;
1.1 deraadt 227: #endif /* B57600 */
228: #ifdef B76800
1.3 markus 229: case 76800:
230: return B76800;
1.1 deraadt 231: #endif /* B76800 */
232: #ifdef B115200
1.3 markus 233: case 115200:
234: return B115200;
1.1 deraadt 235: #endif /* B115200 */
236: #ifdef B230400
1.3 markus 237: case 230400:
238: return B230400;
1.1 deraadt 239: #endif /* B230400 */
1.3 markus 240: default:
241: return B9600;
242: }
1.1 deraadt 243: }
244:
1.4 deraadt 245: /*
246: * Encodes terminal modes for the terminal referenced by fd
1.12 stevesk 247: * or tiop in a portable manner, and appends the modes to a packet
1.4 deraadt 248: * being constructed.
249: */
1.6 markus 250: void
1.34 ! markus 251: ssh_tty_make_modes(struct ssh *ssh, int fd, struct termios *tiop)
1.1 deraadt 252: {
1.3 markus 253: struct termios tio;
1.34 ! markus 254: struct sshbuf *buf;
! 255: int r, ibaud, obaud;
1.12 stevesk 256:
1.34 ! markus 257: if ((buf = sshbuf_new()) == NULL)
! 258: fatal("%s: sshbuf_new failed", __func__);
1.12 stevesk 259:
260: if (tiop == NULL) {
1.27 djm 261: if (fd == -1) {
1.34 ! markus 262: debug("%s: no fd or tio", __func__);
1.27 djm 263: goto end;
264: }
1.12 stevesk 265: if (tcgetattr(fd, &tio) == -1) {
1.19 itojun 266: logit("tcgetattr: %.100s", strerror(errno));
1.12 stevesk 267: goto end;
268: }
269: } else
270: tio = *tiop;
1.1 deraadt 271:
1.3 markus 272: /* Store input and output baud rates. */
1.34 ! markus 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("%s: buffer error: %s", __func__, ssh_err(r));
1.1 deraadt 280:
1.3 markus 281: /* Store values of mode flags. */
1.1 deraadt 282: #define TTYCHAR(NAME, OP) \
1.34 ! markus 283: if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
! 284: (r = sshbuf_put_u32(buf, tio.c_cc[NAME])) != 0) \
! 285: fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
1.12 stevesk 286:
1.33 dtucker 287: #define SSH_TTYMODE_IUTF8 42 /* for SSH_BUG_UTF8TTYMODE */
288:
1.1 deraadt 289: #define TTYMODE(NAME, FIELD, OP) \
1.33 dtucker 290: if (OP == SSH_TTYMODE_IUTF8 && (datafellows & SSH_BUG_UTF8TTYMODE)) { \
291: debug3("%s: SSH_BUG_UTF8TTYMODE", __func__); \
1.34 ! markus 292: } else if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
! 293: (r = sshbuf_put_u32(buf, ((tio.FIELD & NAME) != 0))) != 0) \
! 294: fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
1.1 deraadt 295:
296: #include "ttymodes.h"
297:
298: #undef TTYCHAR
299: #undef TTYMODE
300:
1.12 stevesk 301: end:
1.3 markus 302: /* Mark end of mode data. */
1.34 ! markus 303: if ((r = sshbuf_put_u8(buf, TTY_OP_END)) != 0 ||
! 304: (r = sshpkt_put_stringb(ssh, buf)) != 0)
! 305: fatal("%s: packet error: %s", __func__, ssh_err(r));
! 306: sshbuf_free(buf);
1.1 deraadt 307: }
308:
1.4 deraadt 309: /*
310: * Decodes terminal modes for the terminal referenced by fd in a portable
311: * manner from a packet being read.
312: */
1.6 markus 313: void
1.34 ! markus 314: ssh_tty_parse_modes(struct ssh *ssh, int fd)
1.1 deraadt 315: {
1.3 markus 316: struct termios tio;
1.34 ! markus 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("%s: packet error: %s", __func__, ssh_err(r));
! 326: if (len == 0)
! 327: return;
! 328: if ((buf = sshbuf_from(data, len)) == NULL) {
! 329: error("%s: sshbuf_from failed", __func__);
1.31 djm 330: return;
1.34 ! markus 331: }
1.3 markus 332:
1.4 deraadt 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: */
1.12 stevesk 338: if (tcgetattr(fd, &tio) == -1) {
1.19 itojun 339: logit("tcgetattr: %.100s", strerror(errno));
1.3 markus 340: failure = -1;
1.12 stevesk 341: }
1.3 markus 342:
1.34 ! markus 343: while (sshbuf_len(buf) > 0) {
! 344: if ((r = sshbuf_get_u8(buf, &opcode)) != 0)
! 345: fatal("%s: packet error: %s", __func__, ssh_err(r));
1.3 markus 346: switch (opcode) {
347: case TTY_OP_END:
348: goto set;
349:
1.32 djm 350: case TTY_OP_ISPEED:
1.34 ! markus 351: if ((r = sshbuf_get_u32(buf, &baud)) != 0)
! 352: fatal("%s: packet error: %s",
! 353: __func__, ssh_err(r));
1.22 deraadt 354: if (failure != -1 &&
355: cfsetispeed(&tio, baud_to_speed(baud)) == -1)
1.3 markus 356: error("cfsetispeed failed for %d", baud);
357: break;
358:
1.32 djm 359: case TTY_OP_OSPEED:
1.34 ! markus 360: if ((r = sshbuf_get_u32(buf, &baud)) != 0)
! 361: fatal("%s: packet error: %s",
! 362: __func__, ssh_err(r));
1.22 deraadt 363: if (failure != -1 &&
364: cfsetospeed(&tio, baud_to_speed(baud)) == -1)
1.3 markus 365: error("cfsetospeed failed for %d", baud);
366: break;
1.1 deraadt 367:
1.12 stevesk 368: #define TTYCHAR(NAME, OP) \
1.34 ! markus 369: case OP: \
! 370: if ((r = sshbuf_get_u32(buf, &u)) != 0) \
! 371: fatal("%s: packet error: %s", __func__, \
! 372: ssh_err(r)); \
! 373: tio.c_cc[NAME] = u; \
! 374: break;
1.12 stevesk 375: #define TTYMODE(NAME, FIELD, OP) \
1.34 ! markus 376: case OP: \
! 377: if ((r = sshbuf_get_u32(buf, &u)) != 0) \
! 378: fatal("%s: packet error: %s", __func__, \
! 379: ssh_err(r)); \
! 380: if (u) \
! 381: tio.FIELD |= NAME; \
! 382: else \
! 383: tio.FIELD &= ~NAME; \
! 384: break;
1.1 deraadt 385:
386: #include "ttymodes.h"
387:
388: #undef TTYCHAR
389: #undef TTYMODE
390:
1.3 markus 391: default:
392: debug("Ignoring unsupported tty mode opcode %d (0x%x)",
1.15 deraadt 393: opcode, opcode);
1.31 djm 394: /*
395: * SSH2:
396: * Opcodes 1 to 159 are defined to have a uint32
397: * argument.
398: * Opcodes 160 to 255 are undefined and cause parsing
399: * to stop.
400: */
401: if (opcode > 0 && opcode < 160) {
1.34 ! markus 402: if ((r = sshbuf_get_u32(buf, NULL)) != 0)
! 403: fatal("%s: packet error: %s", __func__,
! 404: ssh_err(r));
1.31 djm 405: break;
1.3 markus 406: } else {
1.34 ! markus 407: logit("%s: unknown opcode %d", __func__,
1.31 djm 408: opcode);
409: goto set;
1.17 markus 410: }
1.1 deraadt 411: }
412: }
413:
1.3 markus 414: set:
1.34 ! markus 415: len = sshbuf_len(buf);
! 416: sshbuf_free(buf);
! 417: if (len > 0) {
! 418: logit("%s: %zu bytes left", __func__, len);
1.3 markus 419: return; /* Don't process bytes passed */
420: }
421: if (failure == -1)
1.13 stevesk 422: return; /* Packet parsed ok but tcgetattr() failed */
1.3 markus 423:
424: /* Set the new modes for the terminal. */
1.12 stevesk 425: if (tcsetattr(fd, TCSANOW, &tio) == -1)
1.19 itojun 426: logit("Setting tty modes failed: %.100s", strerror(errno));
1.1 deraadt 427: }