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