summaryrefslogtreecommitdiff
path: root/ttymodes.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-11-25 00:26:21 +1100
committerDamien Miller <djm@mindrot.org>1999-11-25 00:26:21 +1100
commit95def09838fc61b37b6ea7cd5c234a465b4b129b (patch)
tree042744f76f40a326b873cb1c3690a6d7d966bc3e /ttymodes.c
parent4d2f15f895f4c795afc008aeff3fd2ceffbc44f4 (diff)
- Merged very large OpenBSD source code reformat
- OpenBSD CVS updates - [channels.c cipher.c compat.c log-client.c scp.c serverloop.c] [ssh.h sshd.8 sshd.c] syslog changes: * Unified Logmessage for all auth-types, for success and for failed * Standard connections get only ONE line in the LOG when level==LOG: Auth-attempts are logged only, if authentication is: a) successfull or b) with passwd or c) we had more than AUTH_FAIL_LOG failues * many log() became verbose() * old behaviour with level=VERBOSE - [readconf.c readconf.h ssh.1 ssh.h sshconnect.c sshd.c] tranfer s/key challenge/response data in SSH_SMSG_AUTH_TIS_CHALLENGE messages. allows use of s/key in windows (ttssh, securecrt) and ssh-1.2.27 clients without 'ssh -v', ok: niels@ - [sshd.8] -V, for fallback to openssh in SSH2 compatibility mode - [sshd.c] fix sigchld race; cjc5@po.cwru.edu
Diffstat (limited to 'ttymodes.c')
-rw-r--r--ttymodes.c479
1 files changed, 240 insertions, 239 deletions
diff --git a/ttymodes.c b/ttymodes.c
index cbb7f2f64..6810ce26c 100644
--- a/ttymodes.c
+++ b/ttymodes.c
@@ -1,233 +1,229 @@
1/* 1/*
2 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3ttymodes.c 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 4 * All rights reserved
5Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Created: Tue Mar 21 15:59:15 1995 ylo
6 6 * Encoding and decoding of terminal modes in a portable way.
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 7 * Much of the format is defined in ttymodes.h; it is included multiple times
8 All rights reserved 8 * into this file with the appropriate macro definitions to generate the
9 9 * suitable code.
10Created: Tue Mar 21 15:59:15 1995 ylo 10 */
11
12Encoding and decoding of terminal modes in a portable way.
13Much of the format is defined in ttymodes.h; it is included multiple times
14into this file with the appropriate macro definitions to generate the
15suitable code.
16
17*/
18 11
19#include "includes.h" 12#include "includes.h"
20RCSID("$Id: ttymodes.c,v 1.1 1999/10/27 03:42:46 damien Exp $"); 13RCSID("$Id: ttymodes.c,v 1.2 1999/11/24 13:26:23 damien Exp $");
21 14
22#include "packet.h" 15#include "packet.h"
23#include "ssh.h" 16#include "ssh.h"
24 17
25#define TTY_OP_END 0 18#define TTY_OP_END 0
26#define TTY_OP_ISPEED 192 /* int follows */ 19#define TTY_OP_ISPEED 192 /* int follows */
27#define TTY_OP_OSPEED 193 /* int follows */ 20#define TTY_OP_OSPEED 193 /* int follows */
28 21
29/* Converts POSIX speed_t to a baud rate. The values of the constants 22/*
30 for speed_t are not themselves portable. */ 23 * Converts POSIX speed_t to a baud rate. The values of the
31 24 * constants for speed_t are not themselves portable.
32static int speed_to_baud(speed_t speed) 25 */
26static int
27speed_to_baud(speed_t speed)
33{ 28{
34 switch (speed) 29 switch (speed) {
35 { 30 case B0:
36 case B0: 31 return 0;
37 return 0; 32 case B50:
38 case B50: 33 return 50;
39 return 50; 34 case B75:
40 case B75: 35 return 75;
41 return 75; 36 case B110:
42 case B110: 37 return 110;
43 return 110; 38 case B134:
44 case B134: 39 return 134;
45 return 134; 40 case B150:
46 case B150: 41 return 150;
47 return 150; 42 case B200:
48 case B200: 43 return 200;
49 return 200; 44 case B300:
50 case B300: 45 return 300;
51 return 300; 46 case B600:
52 case B600: 47 return 600;
53 return 600; 48 case B1200:
54 case B1200: 49 return 1200;
55 return 1200; 50 case B1800:
56 case B1800: 51 return 1800;
57 return 1800; 52 case B2400:
58 case B2400: 53 return 2400;
59 return 2400; 54 case B4800:
60 case B4800: 55 return 4800;
61 return 4800; 56 case B9600:
62 case B9600: 57 return 9600;
63 return 9600;
64 58
65#ifdef B19200 59#ifdef B19200
66 case B19200: 60 case B19200:
67 return 19200; 61 return 19200;
68#else /* B19200 */ 62#else /* B19200 */
69#ifdef EXTA 63#ifdef EXTA
70 case EXTA: 64 case EXTA:
71 return 19200; 65 return 19200;
72#endif /* EXTA */ 66#endif /* EXTA */
73#endif /* B19200 */ 67#endif /* B19200 */
74 68
75#ifdef B38400 69#ifdef B38400
76 case B38400: 70 case B38400:
77 return 38400; 71 return 38400;
78#else /* B38400 */ 72#else /* B38400 */
79#ifdef EXTB 73#ifdef EXTB
80 case EXTB: 74 case EXTB:
81 return 38400; 75 return 38400;
82#endif /* EXTB */ 76#endif /* EXTB */
83#endif /* B38400 */ 77#endif /* B38400 */
84 78
85#ifdef B7200 79#ifdef B7200
86 case B7200: 80 case B7200:
87 return 7200; 81 return 7200;
88#endif /* B7200 */ 82#endif /* B7200 */
89#ifdef B14400 83#ifdef B14400
90 case B14400: 84 case B14400:
91 return 14400; 85 return 14400;
92#endif /* B14400 */ 86#endif /* B14400 */
93#ifdef B28800 87#ifdef B28800
94 case B28800: 88 case B28800:
95 return 28800; 89 return 28800;
96#endif /* B28800 */ 90#endif /* B28800 */
97#ifdef B57600 91#ifdef B57600
98 case B57600: 92 case B57600:
99 return 57600; 93 return 57600;
100#endif /* B57600 */ 94#endif /* B57600 */
101#ifdef B76800 95#ifdef B76800
102 case B76800: 96 case B76800:
103 return 76800; 97 return 76800;
104#endif /* B76800 */ 98#endif /* B76800 */
105#ifdef B115200 99#ifdef B115200
106 case B115200: 100 case B115200:
107 return 115200; 101 return 115200;
108#endif /* B115200 */ 102#endif /* B115200 */
109#ifdef B230400 103#ifdef B230400
110 case B230400: 104 case B230400:
111 return 230400; 105 return 230400;
112#endif /* B230400 */ 106#endif /* B230400 */
113 default: 107 default:
114 return 9600; 108 return 9600;
115 } 109 }
116} 110}
117 111
118/* Converts a numeric baud rate to a POSIX speed_t. */ 112/*
119 113 * Converts a numeric baud rate to a POSIX speed_t.
120static speed_t baud_to_speed(int baud) 114 */
115static speed_t
116baud_to_speed(int baud)
121{ 117{
122 switch (baud) 118 switch (baud) {
123 { 119 case 0:
124 case 0: 120 return B0;
125 return B0; 121 case 50:
126 case 50: 122 return B50;
127 return B50; 123 case 75:
128 case 75: 124 return B75;
129 return B75; 125 case 110:
130 case 110: 126 return B110;
131 return B110; 127 case 134:
132 case 134: 128 return B134;
133 return B134; 129 case 150:
134 case 150: 130 return B150;
135 return B150; 131 case 200:
136 case 200: 132 return B200;
137 return B200; 133 case 300:
138 case 300: 134 return B300;
139 return B300; 135 case 600:
140 case 600: 136 return B600;
141 return B600; 137 case 1200:
142 case 1200: 138 return B1200;
143 return B1200; 139 case 1800:
144 case 1800: 140 return B1800;
145 return B1800; 141 case 2400:
146 case 2400: 142 return B2400;
147 return B2400; 143 case 4800:
148 case 4800: 144 return B4800;
149 return B4800; 145 case 9600:
150 case 9600: 146 return B9600;
151 return B9600;
152 147
153#ifdef B19200 148#ifdef B19200
154 case 19200: 149 case 19200:
155 return B19200; 150 return B19200;
156#else /* B19200 */ 151#else /* B19200 */
157#ifdef EXTA 152#ifdef EXTA
158 case 19200: 153 case 19200:
159 return EXTA; 154 return EXTA;
160#endif /* EXTA */ 155#endif /* EXTA */
161#endif /* B19200 */ 156#endif /* B19200 */
162 157
163#ifdef B38400 158#ifdef B38400
164 case 38400: 159 case 38400:
165 return B38400; 160 return B38400;
166#else /* B38400 */ 161#else /* B38400 */
167#ifdef EXTB 162#ifdef EXTB
168 case 38400: 163 case 38400:
169 return EXTB; 164 return EXTB;
170#endif /* EXTB */ 165#endif /* EXTB */
171#endif /* B38400 */ 166#endif /* B38400 */
172 167
173#ifdef B7200 168#ifdef B7200
174 case 7200: 169 case 7200:
175 return B7200; 170 return B7200;
176#endif /* B7200 */ 171#endif /* B7200 */
177#ifdef B14400 172#ifdef B14400
178 case 14400: 173 case 14400:
179 return B14400; 174 return B14400;
180#endif /* B14400 */ 175#endif /* B14400 */
181#ifdef B28800 176#ifdef B28800
182 case 28800: 177 case 28800:
183 return B28800; 178 return B28800;
184#endif /* B28800 */ 179#endif /* B28800 */
185#ifdef B57600 180#ifdef B57600
186 case 57600: 181 case 57600:
187 return B57600; 182 return B57600;
188#endif /* B57600 */ 183#endif /* B57600 */
189#ifdef B76800 184#ifdef B76800
190 case 76800: 185 case 76800:
191 return B76800; 186 return B76800;
192#endif /* B76800 */ 187#endif /* B76800 */
193#ifdef B115200 188#ifdef B115200
194 case 115200: 189 case 115200:
195 return B115200; 190 return B115200;
196#endif /* B115200 */ 191#endif /* B115200 */
197#ifdef B230400 192#ifdef B230400
198 case 230400: 193 case 230400:
199 return B230400; 194 return B230400;
200#endif /* B230400 */ 195#endif /* B230400 */
201 default: 196 default:
202 return B9600; 197 return B9600;
203 } 198 }
204} 199}
205 200
206/* Encodes terminal modes for the terminal referenced by fd in a portable 201/*
207 manner, and appends the modes to a packet being constructed. */ 202 * Encodes terminal modes for the terminal referenced by fd
208 203 * in a portable manner, and appends the modes to a packet
209void tty_make_modes(int fd) 204 * being constructed.
205 */
206void
207tty_make_modes(int fd)
210{ 208{
211 struct termios tio; 209 struct termios tio;
212 int baud; 210 int baud;
213 211
214 /* Get the modes. */ 212 /* Get the modes. */
215 if (tcgetattr(fd, &tio) < 0) 213 if (tcgetattr(fd, &tio) < 0) {
216 { 214 packet_put_char(TTY_OP_END);
217 packet_put_char(TTY_OP_END); 215 log("tcgetattr: %.100s", strerror(errno));
218 log("tcgetattr: %.100s", strerror(errno)); 216 return;
219 return; 217 }
220 } 218 /* Store input and output baud rates. */
221 219 baud = speed_to_baud(cfgetospeed(&tio));
222 /* Store input and output baud rates. */ 220 packet_put_char(TTY_OP_OSPEED);
223 baud = speed_to_baud(cfgetospeed(&tio)); 221 packet_put_int(baud);
224 packet_put_char(TTY_OP_OSPEED); 222 baud = speed_to_baud(cfgetispeed(&tio));
225 packet_put_int(baud); 223 packet_put_char(TTY_OP_ISPEED);
226 baud = speed_to_baud(cfgetispeed(&tio)); 224 packet_put_int(baud);
227 packet_put_char(TTY_OP_ISPEED); 225
228 packet_put_int(baud); 226 /* Store values of mode flags. */
229
230 /* Store values of mode flags. */
231#define TTYCHAR(NAME, OP) \ 227#define TTYCHAR(NAME, OP) \
232 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]); 228 packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
233#define TTYMODE(NAME, FIELD, OP) \ 229#define TTYMODE(NAME, FIELD, OP) \
@@ -244,48 +240,50 @@ void tty_make_modes(int fd)
244#undef SGTTYMODE 240#undef SGTTYMODE
245#undef SGTTYMODEN 241#undef SGTTYMODEN
246 242
247 /* Mark end of mode data. */ 243 /* Mark end of mode data. */
248 packet_put_char(TTY_OP_END); 244 packet_put_char(TTY_OP_END);
249} 245}
250 246
251/* Decodes terminal modes for the terminal referenced by fd in a portable 247/*
252 manner from a packet being read. */ 248 * Decodes terminal modes for the terminal referenced by fd in a portable
253 249 * manner from a packet being read.
254void tty_parse_modes(int fd, int *n_bytes_ptr) 250 */
251void
252tty_parse_modes(int fd, int *n_bytes_ptr)
255{ 253{
256 struct termios tio; 254 struct termios tio;
257 int opcode, baud; 255 int opcode, baud;
258 int n_bytes = 0; 256 int n_bytes = 0;
259 int failure = 0; 257 int failure = 0;
260 258
261 /* Get old attributes for the terminal. We will modify these flags. 259 /*
262 I am hoping that if there are any machine-specific modes, they will 260 * Get old attributes for the terminal. We will modify these
263 initially have reasonable values. */ 261 * flags. I am hoping that if there are any machine-specific
264 if (tcgetattr(fd, &tio) < 0) 262 * modes, they will initially have reasonable values.
265 failure = -1; 263 */
266 264 if (tcgetattr(fd, &tio) < 0)
267 for (;;) 265 failure = -1;
268 { 266
269 n_bytes += 1; 267 for (;;) {
270 opcode = packet_get_char(); 268 n_bytes += 1;
271 switch (opcode) 269 opcode = packet_get_char();
272 { 270 switch (opcode) {
273 case TTY_OP_END: 271 case TTY_OP_END:
274 goto set; 272 goto set;
275 273
276 case TTY_OP_ISPEED: 274 case TTY_OP_ISPEED:
277 n_bytes += 4; 275 n_bytes += 4;
278 baud = packet_get_int(); 276 baud = packet_get_int();
279 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0) 277 if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
280 error("cfsetispeed failed for %d", baud); 278 error("cfsetispeed failed for %d", baud);
281 break; 279 break;
282 280
283 case TTY_OP_OSPEED: 281 case TTY_OP_OSPEED:
284 n_bytes += 4; 282 n_bytes += 4;
285 baud = packet_get_int(); 283 baud = packet_get_int();
286 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0) 284 if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
287 error("cfsetospeed failed for %d", baud); 285 error("cfsetospeed failed for %d", baud);
288 break; 286 break;
289 287
290#define TTYCHAR(NAME, OP) \ 288#define TTYCHAR(NAME, OP) \
291 case OP: \ 289 case OP: \
@@ -312,48 +310,51 @@ void tty_parse_modes(int fd, int *n_bytes_ptr)
312#undef SGTTYMODE 310#undef SGTTYMODE
313#undef SGTTYMODEN 311#undef SGTTYMODEN
314 312
315 default: 313 default:
316 debug("Ignoring unsupported tty mode opcode %d (0x%x)", 314 debug("Ignoring unsupported tty mode opcode %d (0x%x)",
317 opcode, opcode); 315 opcode, opcode);
318 /* Opcodes 0 to 127 are defined to have a one-byte argument. */ 316 /*
319 if (opcode >= 0 && opcode < 128) 317 * Opcodes 0 to 127 are defined to have
320 { 318 * a one-byte argument.
321 n_bytes += 1; 319 */
322 (void)packet_get_char(); 320 if (opcode >= 0 && opcode < 128) {
323 break; 321 n_bytes += 1;
324 } 322 (void) packet_get_char();
325 else 323 break;
326 { 324 } else {
327 /* Opcodes 128 to 159 are defined to have an integer argument. */ 325 /*
328 if (opcode >= 128 && opcode < 160) 326 * Opcodes 128 to 159 are defined to have
329 { 327 * an integer argument.
330 n_bytes += 4; 328 */
331 (void)packet_get_int(); 329 if (opcode >= 128 && opcode < 160) {
332 break; 330 n_bytes += 4;
331 (void) packet_get_int();
332 break;
333 }
334 }
335 /*
336 * It is a truly undefined opcode (160 to 255).
337 * We have no idea about its arguments. So we
338 * must stop parsing. Note that some data may be
339 * left in the packet; hopefully there is nothing
340 * more coming after the mode data.
341 */
342 log("parse_tty_modes: unknown opcode %d", opcode);
343 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
344 goto set;
333 } 345 }
334 }
335 /* It is a truly undefined opcode (160 to 255). We have no idea
336 about its arguments. So we must stop parsing. Note that some
337 data may be left in the packet; hopefully there is nothing more
338 coming after the mode data. */
339 log("parse_tty_modes: unknown opcode %d", opcode);
340 packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
341 goto set;
342 } 346 }
343 }
344 347
345 set: 348set:
346 if (*n_bytes_ptr != n_bytes) 349 if (*n_bytes_ptr != n_bytes) {
347 { 350 *n_bytes_ptr = n_bytes;
348 *n_bytes_ptr = n_bytes; 351 return; /* Don't process bytes passed */
349 return; /* Don't process bytes passed */ 352 }
350 } 353 if (failure == -1)
354 return; /* Packet parsed ok but tty stuff failed */
351 355
352 if (failure == -1) 356 /* Set the new modes for the terminal. */
353 return; /* Packet parsed ok but tty stuff failed */ 357 if (tcsetattr(fd, TCSANOW, &tio) < 0)
354 358 log("Setting tty modes failed: %.100s", strerror(errno));
355 /* Set the new modes for the terminal. */ 359 return;
356 if (tcsetattr(fd, TCSANOW, &tio) < 0)
357 log("Setting tty modes failed: %.100s", strerror(errno));
358 return;
359} 360}