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