1 /* $NetBSD: ttymodes.c,v 1.8 2017/10/07 19:39:19 christos Exp $ */ 2 /* $OpenBSD: ttymodes.c,v 1.32 2017/04/30 23:26:54 djm Exp $ */ 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 */ 14 15 /* 16 * SSH2 tty modes support by Kevin Steves. 17 * Copyright (c) 2001 Kevin Steves. All rights reserved. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Encoding and decoding of terminal modes in a portable way. 42 * Much of the format is defined in ttymodes.h; it is included multiple times 43 * into this file with the appropriate macro definitions to generate the 44 * suitable code. 45 */ 46 47 #include "includes.h" 48 __RCSID("$NetBSD: ttymodes.c,v 1.8 2017/10/07 19:39:19 christos Exp $"); 49 #include <sys/types.h> 50 51 #include <errno.h> 52 #include <string.h> 53 #include <termios.h> 54 #include <stdarg.h> 55 56 #include "packet.h" 57 #include "log.h" 58 #include "compat.h" 59 #include "buffer.h" 60 61 #define TTY_OP_END 0 62 /* 63 * uint32 (u_int) follows speed. 64 */ 65 #define TTY_OP_ISPEED 128 66 #define TTY_OP_OSPEED 129 67 68 /* 69 * Converts POSIX speed_t to a baud rate. The values of the 70 * constants for speed_t are not themselves portable. 71 */ 72 static int 73 speed_to_baud(speed_t speed) 74 { 75 switch (speed) { 76 case B0: 77 return 0; 78 case B50: 79 return 50; 80 case B75: 81 return 75; 82 case B110: 83 return 110; 84 case B134: 85 return 134; 86 case B150: 87 return 150; 88 case B200: 89 return 200; 90 case B300: 91 return 300; 92 case B600: 93 return 600; 94 case B1200: 95 return 1200; 96 case B1800: 97 return 1800; 98 case B2400: 99 return 2400; 100 case B4800: 101 return 4800; 102 case B9600: 103 return 9600; 104 105 #ifdef B19200 106 case B19200: 107 return 19200; 108 #else /* B19200 */ 109 #ifdef EXTA 110 case EXTA: 111 return 19200; 112 #endif /* EXTA */ 113 #endif /* B19200 */ 114 115 #ifdef B38400 116 case B38400: 117 return 38400; 118 #else /* B38400 */ 119 #ifdef EXTB 120 case EXTB: 121 return 38400; 122 #endif /* EXTB */ 123 #endif /* B38400 */ 124 125 #ifdef B7200 126 case B7200: 127 return 7200; 128 #endif /* B7200 */ 129 #ifdef B14400 130 case B14400: 131 return 14400; 132 #endif /* B14400 */ 133 #ifdef B28800 134 case B28800: 135 return 28800; 136 #endif /* B28800 */ 137 #ifdef B57600 138 case B57600: 139 return 57600; 140 #endif /* B57600 */ 141 #ifdef B76800 142 case B76800: 143 return 76800; 144 #endif /* B76800 */ 145 #ifdef B115200 146 case B115200: 147 return 115200; 148 #endif /* B115200 */ 149 #ifdef B230400 150 case B230400: 151 return 230400; 152 #endif /* B230400 */ 153 default: 154 return 9600; 155 } 156 } 157 158 /* 159 * Converts a numeric baud rate to a POSIX speed_t. 160 */ 161 static speed_t 162 baud_to_speed(int baud) 163 { 164 switch (baud) { 165 case 0: 166 return B0; 167 case 50: 168 return B50; 169 case 75: 170 return B75; 171 case 110: 172 return B110; 173 case 134: 174 return B134; 175 case 150: 176 return B150; 177 case 200: 178 return B200; 179 case 300: 180 return B300; 181 case 600: 182 return B600; 183 case 1200: 184 return B1200; 185 case 1800: 186 return B1800; 187 case 2400: 188 return B2400; 189 case 4800: 190 return B4800; 191 case 9600: 192 return B9600; 193 194 #ifdef B19200 195 case 19200: 196 return B19200; 197 #else /* B19200 */ 198 #ifdef EXTA 199 case 19200: 200 return EXTA; 201 #endif /* EXTA */ 202 #endif /* B19200 */ 203 204 #ifdef B38400 205 case 38400: 206 return B38400; 207 #else /* B38400 */ 208 #ifdef EXTB 209 case 38400: 210 return EXTB; 211 #endif /* EXTB */ 212 #endif /* B38400 */ 213 214 #ifdef B7200 215 case 7200: 216 return B7200; 217 #endif /* B7200 */ 218 #ifdef B14400 219 case 14400: 220 return B14400; 221 #endif /* B14400 */ 222 #ifdef B28800 223 case 28800: 224 return B28800; 225 #endif /* B28800 */ 226 #ifdef B57600 227 case 57600: 228 return B57600; 229 #endif /* B57600 */ 230 #ifdef B76800 231 case 76800: 232 return B76800; 233 #endif /* B76800 */ 234 #ifdef B115200 235 case 115200: 236 return B115200; 237 #endif /* B115200 */ 238 #ifdef B230400 239 case 230400: 240 return B230400; 241 #endif /* B230400 */ 242 default: 243 return B9600; 244 } 245 } 246 247 /* 248 * Encodes terminal modes for the terminal referenced by fd 249 * or tiop in a portable manner, and appends the modes to a packet 250 * being constructed. 251 */ 252 void 253 tty_make_modes(int fd, struct termios *tiop) 254 { 255 struct termios tio; 256 int baud; 257 Buffer buf; 258 259 buffer_init(&buf); 260 261 if (tiop == NULL) { 262 if (fd == -1) { 263 debug("tty_make_modes: no fd or tio"); 264 goto end; 265 } 266 if (tcgetattr(fd, &tio) == -1) { 267 logit("tcgetattr: %.100s", strerror(errno)); 268 goto end; 269 } 270 } else 271 tio = *tiop; 272 273 /* Store input and output baud rates. */ 274 baud = speed_to_baud(cfgetospeed(&tio)); 275 buffer_put_char(&buf, TTY_OP_OSPEED); 276 buffer_put_int(&buf, baud); 277 baud = speed_to_baud(cfgetispeed(&tio)); 278 buffer_put_char(&buf, TTY_OP_ISPEED); 279 buffer_put_int(&buf, baud); 280 281 /* Store values of mode flags. */ 282 #define TTYCHAR(NAME, OP) \ 283 buffer_put_char(&buf, OP); \ 284 buffer_put_int(&buf, tio.c_cc[NAME]); 285 286 #define TTYMODE(NAME, FIELD, OP) \ 287 buffer_put_char(&buf, OP); \ 288 buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); 289 290 #include "ttymodes.h" 291 292 #undef TTYCHAR 293 #undef TTYMODE 294 295 end: 296 /* Mark end of mode data. */ 297 buffer_put_char(&buf, TTY_OP_END); 298 packet_put_string(buffer_ptr(&buf), buffer_len(&buf)); 299 buffer_free(&buf); 300 } 301 302 /* 303 * Decodes terminal modes for the terminal referenced by fd in a portable 304 * manner from a packet being read. 305 */ 306 void 307 tty_parse_modes(int fd, int *n_bytes_ptr) 308 { 309 struct termios tio; 310 int opcode, baud; 311 int n_bytes = 0; 312 int failure = 0; 313 314 *n_bytes_ptr = packet_get_int(); 315 if (*n_bytes_ptr == 0) 316 return; 317 318 /* 319 * Get old attributes for the terminal. We will modify these 320 * flags. I am hoping that if there are any machine-specific 321 * modes, they will initially have reasonable values. 322 */ 323 if (tcgetattr(fd, &tio) == -1) { 324 logit("tcgetattr: %.100s", strerror(errno)); 325 failure = -1; 326 } 327 328 for (;;) { 329 n_bytes += 1; 330 opcode = packet_get_char(); 331 switch (opcode) { 332 case TTY_OP_END: 333 goto set; 334 335 case TTY_OP_ISPEED: 336 n_bytes += 4; 337 baud = packet_get_int(); 338 if (failure != -1 && 339 cfsetispeed(&tio, baud_to_speed(baud)) == -1) 340 error("cfsetispeed failed for %d", baud); 341 break; 342 343 case TTY_OP_OSPEED: 344 n_bytes += 4; 345 baud = packet_get_int(); 346 if (failure != -1 && 347 cfsetospeed(&tio, baud_to_speed(baud)) == -1) 348 error("cfsetospeed failed for %d", baud); 349 break; 350 351 #define TTYCHAR(NAME, OP) \ 352 case OP: \ 353 n_bytes += 4; \ 354 tio.c_cc[NAME] = packet_get_int(); \ 355 break; 356 #define TTYMODE(NAME, FIELD, OP) \ 357 case OP: \ 358 n_bytes += 4; \ 359 if (packet_get_int()) \ 360 tio.FIELD |= NAME; \ 361 else \ 362 tio.FIELD &= ~NAME; \ 363 break; 364 365 #include "ttymodes.h" 366 367 #undef TTYCHAR 368 #undef TTYMODE 369 370 default: 371 debug("Ignoring unsupported tty mode opcode %d (0x%x)", 372 opcode, opcode); 373 /* 374 * SSH2: 375 * Opcodes 1 to 159 are defined to have a uint32 376 * argument. 377 * Opcodes 160 to 255 are undefined and cause parsing 378 * to stop. 379 */ 380 if (opcode > 0 && opcode < 160) { 381 n_bytes += 4; 382 (void) packet_get_int(); 383 break; 384 } else { 385 logit("parse_tty_modes: unknown opcode %d", 386 opcode); 387 goto set; 388 } 389 } 390 } 391 392 set: 393 if (*n_bytes_ptr != n_bytes) { 394 *n_bytes_ptr = n_bytes; 395 logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d", 396 *n_bytes_ptr, n_bytes); 397 return; /* Don't process bytes passed */ 398 } 399 if (failure == -1) 400 return; /* Packet parsed ok but tcgetattr() failed */ 401 402 /* Set the new modes for the terminal. */ 403 if (tcsetattr(fd, TCSANOW, &tio) == -1) 404 logit("Setting tty modes failed: %.100s", strerror(errno)); 405 } 406