1
- /* $OpenBSD: ttymodes.c,v 1.33 2018/02/16 04:43:11 dtucker Exp $ */
1
+ /* $OpenBSD: ttymodes.c,v 1.34 2018/07/09 21:20:26 markus Exp $ */
2
2
/*
3
3
* Author: Tatu Ylonen <[email protected] >
4
4
* Copyright (c) 1995 Tatu Ylonen <[email protected] >, Espoo, Finland
55
55
#include "packet.h"
56
56
#include "log.h"
57
57
#include "compat.h"
58
- #include "buffer .h"
59
- #include "compat .h"
58
+ #include "sshbuf .h"
59
+ #include "ssherr .h"
60
60
61
61
#define TTY_OP_END 0
62
62
/*
@@ -276,17 +276,18 @@ special_char_decode(u_int c)
276
276
* being constructed.
277
277
*/
278
278
void
279
- tty_make_modes ( int fd , struct termios * tiop )
279
+ ssh_tty_make_modes ( struct ssh * ssh , int fd , struct termios * tiop )
280
280
{
281
281
struct termios tio ;
282
- int baud ;
283
- Buffer buf ;
282
+ struct sshbuf * buf ;
283
+ int r , ibaud , obaud ;
284
284
285
- buffer_init (& buf );
285
+ if ((buf = sshbuf_new ()) == NULL )
286
+ fatal ("%s: sshbuf_new failed" , __func__ );
286
287
287
288
if (tiop == NULL ) {
288
289
if (fd == -1 ) {
289
- debug ("tty_make_modes : no fd or tio" );
290
+ debug ("%s : no fd or tio" , __func__ );
290
291
goto end ;
291
292
}
292
293
if (tcgetattr (fd , & tio ) == -1 ) {
@@ -297,27 +298,29 @@ tty_make_modes(int fd, struct termios *tiop)
297
298
tio = * tiop ;
298
299
299
300
/* Store input and output baud rates. */
300
- baud = speed_to_baud (cfgetospeed (& tio ));
301
- buffer_put_char (& buf , TTY_OP_OSPEED );
302
- buffer_put_int (& buf , baud );
303
- baud = speed_to_baud (cfgetispeed (& tio ));
304
- buffer_put_char (& buf , TTY_OP_ISPEED );
305
- buffer_put_int (& buf , baud );
301
+ obaud = speed_to_baud (cfgetospeed (& tio ));
302
+ ibaud = speed_to_baud (cfgetispeed (& tio ));
303
+ if ((r = sshbuf_put_u8 (buf , TTY_OP_OSPEED )) != 0 ||
304
+ (r = sshbuf_put_u32 (buf , obaud )) != 0 ||
305
+ (r = sshbuf_put_u8 (buf , TTY_OP_ISPEED )) != 0 ||
306
+ (r = sshbuf_put_u32 (buf , ibaud )) != 0 )
307
+ fatal ("%s: buffer error: %s" , __func__ , ssh_err (r ));
306
308
307
309
/* Store values of mode flags. */
308
310
#define TTYCHAR (NAME , OP ) \
309
- buffer_put_char(&buf, OP); \
310
- buffer_put_int(&buf, special_char_encode(tio.c_cc[NAME]));
311
+ if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
312
+ (r = sshbuf_put_u32(buf, \
313
+ special_char_encode(tio.c_cc[NAME]))) != 0) \
314
+ fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
311
315
312
316
#define SSH_TTYMODE_IUTF8 42 /* for SSH_BUG_UTF8TTYMODE */
313
317
314
318
#define TTYMODE (NAME , FIELD , OP ) \
315
319
if (OP == SSH_TTYMODE_IUTF8 && (datafellows & SSH_BUG_UTF8TTYMODE)) { \
316
320
debug3("%s: SSH_BUG_UTF8TTYMODE", __func__); \
317
- } else { \
318
- buffer_put_char(&buf, OP); \
319
- buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); \
320
- }
321
+ } else if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
322
+ (r = sshbuf_put_u32(buf, ((tio.FIELD & NAME) != 0))) != 0) \
323
+ fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
321
324
322
325
#include "ttymodes.h"
323
326
@@ -326,26 +329,35 @@ tty_make_modes(int fd, struct termios *tiop)
326
329
327
330
end :
328
331
/* Mark end of mode data. */
329
- buffer_put_char (& buf , TTY_OP_END );
330
- packet_put_string (buffer_ptr (& buf ), buffer_len (& buf ));
331
- buffer_free (& buf );
332
+ if ((r = sshbuf_put_u8 (buf , TTY_OP_END )) != 0 ||
333
+ (r = sshpkt_put_stringb (ssh , buf )) != 0 )
334
+ fatal ("%s: packet error: %s" , __func__ , ssh_err (r ));
335
+ sshbuf_free (buf );
332
336
}
333
337
334
338
/*
335
339
* Decodes terminal modes for the terminal referenced by fd in a portable
336
340
* manner from a packet being read.
337
341
*/
338
342
void
339
- tty_parse_modes ( int fd , int * n_bytes_ptr )
343
+ ssh_tty_parse_modes ( struct ssh * ssh , int fd )
340
344
{
341
345
struct termios tio ;
342
- int opcode , baud ;
343
- int n_bytes = 0 ;
344
- int failure = 0 ;
345
-
346
- * n_bytes_ptr = packet_get_int ();
347
- if (* n_bytes_ptr == 0 )
346
+ struct sshbuf * buf ;
347
+ const u_char * data ;
348
+ u_char opcode ;
349
+ u_int baud , u ;
350
+ int r , failure = 0 ;
351
+ size_t len ;
352
+
353
+ if ((r = sshpkt_get_string_direct (ssh , & data , & len )) != 0 )
354
+ fatal ("%s: packet error: %s" , __func__ , ssh_err (r ));
355
+ if (len == 0 )
356
+ return ;
357
+ if ((buf = sshbuf_from (data , len )) == NULL ) {
358
+ error ("%s: sshbuf_from failed" , __func__ );
348
359
return ;
360
+ }
349
361
350
362
/*
351
363
* Get old attributes for the terminal. We will modify these
@@ -357,42 +369,48 @@ tty_parse_modes(int fd, int *n_bytes_ptr)
357
369
failure = -1 ;
358
370
}
359
371
360
- for (;; ) {
361
- n_bytes += 1 ;
362
- opcode = packet_get_char ( );
372
+ while ( sshbuf_len ( buf ) > 0 ) {
373
+ if (( r = sshbuf_get_u8 ( buf , & opcode )) != 0 )
374
+ fatal ( "%s: packet error: %s" , __func__ , ssh_err ( r ) );
363
375
switch (opcode ) {
364
376
case TTY_OP_END :
365
377
goto set ;
366
378
367
379
case TTY_OP_ISPEED :
368
- n_bytes += 4 ;
369
- baud = packet_get_int ();
380
+ if ((r = sshbuf_get_u32 (buf , & baud )) != 0 )
381
+ fatal ("%s: packet error: %s" ,
382
+ __func__ , ssh_err (r ));
370
383
if (failure != -1 &&
371
384
cfsetispeed (& tio , baud_to_speed (baud )) == -1 )
372
385
error ("cfsetispeed failed for %d" , baud );
373
386
break ;
374
387
375
388
case TTY_OP_OSPEED :
376
- n_bytes += 4 ;
377
- baud = packet_get_int ();
389
+ if ((r = sshbuf_get_u32 (buf , & baud )) != 0 )
390
+ fatal ("%s: packet error: %s" ,
391
+ __func__ , ssh_err (r ));
378
392
if (failure != -1 &&
379
393
cfsetospeed (& tio , baud_to_speed (baud )) == -1 )
380
394
error ("cfsetospeed failed for %d" , baud );
381
395
break ;
382
396
383
397
#define TTYCHAR (NAME , OP ) \
384
- case OP: \
385
- n_bytes += 4; \
386
- tio.c_cc[NAME] = special_char_decode(packet_get_int()); \
387
- break;
398
+ case OP: \
399
+ if ((r = sshbuf_get_u32(buf, &u)) != 0) \
400
+ fatal("%s: packet error: %s", __func__, \
401
+ ssh_err(r)); \
402
+ tio.c_cc[NAME] = special_char_decode(u); \
403
+ break;
388
404
#define TTYMODE (NAME , FIELD , OP ) \
389
- case OP: \
390
- n_bytes += 4; \
391
- if (packet_get_int()) \
392
- tio.FIELD |= NAME; \
393
- else \
394
- tio.FIELD &= ~NAME; \
395
- break;
405
+ case OP: \
406
+ if ((r = sshbuf_get_u32(buf, &u)) != 0) \
407
+ fatal("%s: packet error: %s", __func__, \
408
+ ssh_err(r)); \
409
+ if (u) \
410
+ tio.FIELD |= NAME; \
411
+ else \
412
+ tio.FIELD &= ~NAME; \
413
+ break;
396
414
397
415
#include "ttymodes.h"
398
416
@@ -410,22 +428,23 @@ tty_parse_modes(int fd, int *n_bytes_ptr)
410
428
* to stop.
411
429
*/
412
430
if (opcode > 0 && opcode < 160 ) {
413
- n_bytes += 4 ;
414
- (void ) packet_get_int ();
431
+ if ((r = sshbuf_get_u32 (buf , NULL )) != 0 )
432
+ fatal ("%s: packet error: %s" , __func__ ,
433
+ ssh_err (r ));
415
434
break ;
416
435
} else {
417
- logit ("parse_tty_modes : unknown opcode %d" ,
436
+ logit ("%s : unknown opcode %d" , __func__ ,
418
437
opcode );
419
438
goto set ;
420
439
}
421
440
}
422
441
}
423
442
424
443
set :
425
- if ( * n_bytes_ptr != n_bytes ) {
426
- * n_bytes_ptr = n_bytes ;
427
- logit ( "parse_tty_modes: n_bytes_ptr != n_bytes: %d %d" ,
428
- * n_bytes_ptr , n_bytes );
444
+ len = sshbuf_len ( buf );
445
+ sshbuf_free ( buf ) ;
446
+ if ( len > 0 ) {
447
+ logit ( "%s: %zu bytes left" , __func__ , len );
429
448
return ; /* Don't process bytes passed */
430
449
}
431
450
if (failure == -1 )
0 commit comments