summaryrefslogtreecommitdiff
path: root/src/cssh.c
blob: e5a01c4d015fed9fa8032208b6ef51e0217aac0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#include <libssh/libssh.h> 
#include <libssh/callbacks.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

#include "msleep.h"

#include "cssh_options.h"
#include "version.h"

static int parse_options_and_arguments( int argc, char *argv[], struct gengetopt_args_info *args_info ) {
        cmdline_parser_init( args_info );

        if( cmdline_parser2( argc, argv, args_info, 1, 0, 1 ) != 0 ) {
        	cmdline_parser_free( args_info );
        	return 1;
        }

        return 0;
}

static bool get_yes_or_no( )
{
	char buf[10];
	
ASK_AGAIN:
	if( fgets( buf, sizeof( buf ), stdin ) == NULL ) {
		fprintf( stderr, "ERROR: fgets failed in 'accept_server'\n" );
		return -1;
	}
	
	if( strncmp( buf, "yes", 3 ) == 0 ) {
		return true;
	} else if( strncmp( buf, "no", 2 ) == 0 ) {
		return false;
	} else {
		fprintf( stderr, "ERROR: expecting 'yes' or 'no' as answer\n" );
		goto ASK_AGAIN;
	}
	
	return true;
}

static int accept_server( ssh_session session, unsigned char *hash, size_t hlen )
{
	char *hexa;
	
	hexa = ssh_get_hexa( hash, hlen );
	fprintf( stderr, "The server is unknown. Do you trust the host key?\n" );
	fprintf( stderr, "The server public hash key is: %s\n", hexa );
	ssh_string_free_char( hexa );
	
	if( !get_yes_or_no( ) ) {
		fprintf( stderr, "INFO: Server key not accepted.. terminating now.\n" );
		return -1;
	}
	
	fprintf( stderr, "Do you want to write this new key into the known host file for future usage?\n" );
	if( get_yes_or_no( ) ) {
		if( ssh_write_knownhost( session ) < 0 ) {
			fprintf( stderr, "ERROR: error while writing known host file: %s\n",
				strerror( errno ) );
			return -1;
		}
	}
	
	return 0;
}

static int verify_knownhost( ssh_session session )
{
	int state;
	
	state = ssh_is_server_known( session );

	ssh_key srv_pubkey;
	int rc = ssh_get_publickey( session, &srv_pubkey );
	if( rc < 0 ) {
		fprintf( stderr, "ERROR: Failed to get public key of host: %s\n",
			ssh_get_error( session ) );
		return -1;
	}

	unsigned char *hash;
	size_t hlen;
	rc = ssh_get_publickey_hash( srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen );
	if( rc < 0 ) {
		fprintf( stderr, "ERROR: Failed to get hash of public key of host: %s\n",
			ssh_get_error( session ) );
		return -1;
	}

	ssh_key_free( srv_pubkey );

	switch( state ) {
		case SSH_SERVER_KNOWN_OK:
			ssh_clean_pubkey_hash( &hash );
			return 0;
		
		case SSH_SERVER_KNOWN_CHANGED:
			fprintf( stderr, "ERROR: Host key for server changed : server's one is now:\n" );
			ssh_print_hexa( "Public key hash", hash, hlen );
			ssh_clean_pubkey_hash( &hash );
			return -1;		

		case SSH_SERVER_FOUND_OTHER:
			fprintf( stderr, "ERROR: The server gave us an unexpected host key, this could be an attack\n" );
			return -1;
		
		case SSH_SERVER_FILE_NOT_FOUND:
			fprintf( stderr, "ERROR: Could not find a known host file.\n" );
			rc = accept_server( session, hash, hlen );
			ssh_clean_pubkey_hash( &hash );
			return rc;
			
		case SSH_SERVER_NOT_KNOWN:
			fprintf( stderr, "ERROR: The server is unknown.\n" );
			rc = accept_server( session, hash, hlen );
			ssh_clean_pubkey_hash( &hash );
			return rc;

		case SSH_SERVER_ERROR:
			ssh_clean_pubkey_hash( &hash );
			fprintf( stderr, "ERROR: 'ssh_is_server_known' failed with: %s",
				ssh_get_error( session ) );
			return -1;

		default:
			ssh_clean_pubkey_hash( &hash );
			fprintf( stderr, "ERROR: Unknown check host state %d\n", state );
			return -1;
			
	}
	
	return 0;
}

static int authenticate_pubkey( ssh_session session )
{
	int rc = ssh_userauth_publickey_auto( session, NULL, NULL );
	switch( rc ) {
		case SSH_AUTH_SUCCESS:
			return SSH_AUTH_SUCCESS;
			
		case SSH_AUTH_DENIED:
			fprintf( stderr, "ERROR: authentication with public key denied\n" );
			return SSH_AUTH_DENIED;
		
		case SSH_AUTH_PARTIAL:
			return SSH_AUTH_PARTIAL;
			
		case SSH_AUTH_AGAIN:
			return SSH_AUTH_AGAIN;
			
		case SSH_AUTH_ERROR:
			fprintf( stderr, "ERROR: authentication with public key failed: %s\n",
				ssh_get_error( session ) );
			return SSH_AUTH_ERROR;
	}
	
	return -1;
}

static int authenticate_password( ssh_session session, const char *host, const unsigned short port, const char *user )
{	
	char prompt[128];
	char pass[128];

	const char *prompt_user;
	if( user != NULL ) {
		prompt_user = user;
	} else {
		uid_t uid = getuid( );
		struct passwd *pw = getpwuid( uid );
		if( pw != NULL ) {
			prompt_user = pw->pw_name;
		} else {
			prompt_user = "(unknown)";
		}		
	}
	
	snprintf( prompt, sizeof( prompt ), "%s@%s:%d's password:",
		prompt_user, host, port );
	memset( pass, 0, sizeof( pass ) );
	if( ssh_getpass( prompt, pass, sizeof( pass ), 0, 0 ) < 0 ) {
		fprintf( stderr, "ERROR: ssh_getpass failed\n" );
		ssh_disconnect( session );
		ssh_free( session );
		return -1;
	}	

	int rc = ssh_userauth_password( session, user, pass );
		
	switch( rc ) {
		case SSH_AUTH_SUCCESS:
			return SSH_AUTH_SUCCESS;
		
		case SSH_AUTH_PARTIAL:
			return SSH_AUTH_PARTIAL;
			
		case SSH_AUTH_DENIED:
			fprintf( stderr, "ERROR: Password authentication failed.\n" );
			return SSH_AUTH_DENIED;
			
		case SSH_AUTH_AGAIN:
			return SSH_AUTH_AGAIN;
			
		case SSH_AUTH_ERROR:
			fprintf( stderr, "ERROR: Failed to authenticate with password: %s\n",
				ssh_get_error( session ) );
			ssh_disconnect( session );
			ssh_free( session );
			return SSH_AUTH_ERROR;
	}
	
	return -1;
}

static int read_hosts_file( const char *hosts_file, unsigned short default_port, char ***host, unsigned short **port, unsigned int *nof_hosts )
{
	FILE *f;
	char buf[255];
	
	f = fopen( hosts_file, "r" );
	if( f == NULL ) {
		fprintf( stderr, "ERROR: Opening the host file '%s' failed: %s\n",
			hosts_file, strerror( errno ) );
		return -1;
	}
	
	*nof_hosts = 0;
	unsigned int size_hosts = 2;
	*host = (char **)malloc( size_hosts * sizeof( char * ) );
	*port = (unsigned short *)malloc( size_hosts * sizeof( unsigned short ) );
	if( *host == NULL || *port == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed in 'read_hosts_file'" );
		return -1;
	}
	
	while( fgets( buf, sizeof( buf ), f ) != NULL ) {
		if( strlen( buf ) > 0 && buf[strlen( buf )-1] == '\n' ) {
			buf[strlen(buf)-1] = '\0';
		}
		// the user can be part of the hostname, libssh allows this,
		// but we must parse the port separately
		char *p = strchr( buf, ':' );
		if( p != NULL ) {
			(*port)[*nof_hosts] = atoi( p+1 );
			*p = '\0';
		} else { 
			(*port)[*nof_hosts] = default_port;
		}
		(*host)[*nof_hosts] = strdup( buf );
		(*nof_hosts)++;
		if( *nof_hosts > size_hosts ) {
			size_hosts *= 2;
			*host = (char **)realloc( *host, size_hosts * sizeof( char *) );
			*port = (unsigned short *)realloc( *port, size_hosts * sizeof( unsigned short ) );
			if( *host == NULL || *port == NULL ) {
				fprintf( stderr, "ERROR: Memory allocation failed in 'read_hosts_file'" );
				return -1;
			}
		}
	}
	
	fclose( f );
	
	return 0;
}

static void cleanup_sessions( ssh_session **session, ssh_channel **channel, char **host, unsigned short *port, const int nof_sessions, bool verbose )
{
	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		if( ssh_is_connected( (*session)[i] ) ) {
			if( verbose ) {
				fprintf( stderr, "Disconnecting from '%s', port %d..\n", host[i], port[i] );
			}
			if( channel != NULL ) {	
				if( ssh_channel_is_open( (*channel)[i] ) ) {
					ssh_channel_send_eof( (*channel)[i] );
					ssh_channel_close( (*channel)[i] );
					ssh_channel_free( (*channel)[i] );	
				}
			}
			ssh_disconnect( (*session)[i] );
			if( verbose ) {
				fprintf( stderr, "Disconnected from '%s', port %d..\n", host[i], port[i] );
			}				
		}
		ssh_free( (*session)[i] );
	}
	if( channel != NULL ) {
		free( *channel );
	}
	free( *session );
	free( *host );
	free( port );
}

typedef enum execution_mode_e {
	CSSH_EXECUTE_UNKNOWN,
	CSSH_EXECUTE_AS_SSH,
	CSSH_EXECUTE_AS_SCP	
} execution_mode_e;

static execution_mode_e determine_execution_mode( const char *argv0 )
{
	char *b = ssh_basename( argv0 );
	if( b == NULL ) {
		return CSSH_EXECUTE_UNKNOWN;
	}

	if( strcmp( b, "cssh" ) == 0 ) {
		free( b );
		return CSSH_EXECUTE_AS_SSH;
	} else if( strcmp( b, "cscp" ) == 0 ) {
		free( b );
		return CSSH_EXECUTE_AS_SCP;
	}

	free( b );
	return CSSH_EXECUTE_UNKNOWN;
}

int main( int argc, char *argv[] )
{
	struct gengetopt_args_info args_info;
	ssh_session *session;
	int rc;

	execution_mode_e execution_mode = determine_execution_mode( argv[0] );
	switch( execution_mode ) {
		case CSSH_EXECUTE_UNKNOWN:
			fprintf( stderr, "ERROR: binary name unknown, can't determine execution mode\n" );
			exit( EXIT_FAILURE );

		case CSSH_EXECUTE_AS_SSH:
			break;

		case CSSH_EXECUTE_AS_SCP:
			break;
	}

	if( parse_options_and_arguments( argc, argv, &args_info ) != 0 ) {
		exit( EXIT_FAILURE );
	}

	ssh_threads_set_callbacks( ssh_threads_get_pthread( ) );
	ssh_init( );
	
	if( args_info.long_version_given ) {
		printf( "cssh version: %s\n", CSSH_VERSION );
		printf( "libssh version: %s\n", ssh_copyright( ) );
		exit( EXIT_SUCCESS );
	}
	
	unsigned int nof_sessions = 0;
	unsigned int command_pos = 0;
	char **host = NULL;
	unsigned short *port = NULL;
	if( args_info.hosts_file_given ) {
		unsigned nof_hosts;
		unsigned short default_port = 22;
		if( args_info.port_given ) {
			default_port = args_info.port_arg;
		}		
		rc = read_hosts_file( args_info.hosts_file_arg, default_port, &host, &port, &nof_hosts );
		if( rc < 0 ) {
			exit( EXIT_SUCCESS );
		}
		nof_sessions = nof_hosts;
	} else {
		unsigned short default_port = 22;
		host = (char **)malloc( sizeof( char * ) );
		port = (unsigned short *)malloc( sizeof( unsigned short ) );
		if( args_info.inputs_num > 1 ) {
			command_pos++;
			nof_sessions = 1;
			host[0] = strdup( args_info.inputs[0] );
			port[0] = default_port;
		} else {
			host[0] = strdup( "localhost" );
			port[0] = default_port;
		}
		if( args_info.port_given ) {
			port[0] = args_info.port_arg;
		}
	}
		
	session = (ssh_session *)malloc( nof_sessions * sizeof( ssh_session ) );
	if( session == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for ssh_sessions" );
		return -1;
	}
	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		session[i] = ssh_new( );
		if( session == NULL ) {
			exit( EXIT_FAILURE );
		}
	}

	int verbosity = SSH_LOG_NOLOG;
	if( args_info.verbose_given > 0 ) {
		verbosity += args_info.verbose_given;
	}

	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		ssh_set_blocking( session[i], 0 );
		ssh_options_set( session[i], SSH_OPTIONS_HOST, host[i] );
		ssh_options_set( session[i], SSH_OPTIONS_PORT, &port[i] );
		ssh_options_set( session[i], SSH_OPTIONS_LOG_VERBOSITY, &verbosity );
	}

	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		if( args_info.verbose_given ) {
			fprintf( stderr, "Connecting to '%s', port %d..\n", host[i], port[i] );
		}
	}
	
	bool all_connected = false;
	bool *is_connected = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) );
	if( is_connected == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for 'is_connected'" );
		return -1;
	}
	memset( is_connected, false, ( nof_sessions + 1 ) * sizeof( bool ) );
	while( !all_connected ) {
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			if( !is_connected[i] ) {
				rc = ssh_connect( session[i] );
				if( rc == SSH_OK ) {
					is_connected[i] = true;
					//~ ssh_set_blocking( session[i], 1 );
					if( args_info.verbose_given ) {
						fprintf( stderr, "Connected to '%s', port %d..\n", host[i], port[i] );
					}
				} else if( rc == SSH_AGAIN ) {
					// not connected yet
				} else {
					fprintf( stderr, "ERROR: error connecting to '%s', port '%d': %s\n",
						host[i], port[i], ssh_get_error( session[i] ) );
					cleanup_sessions( &session, NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
					exit( EXIT_FAILURE );
				}
			}
		}

		all_connected = true;
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			if( !is_connected[i] ) {
				all_connected = false;
			}
		}
		
		cssh_msleep( 10 );
	}

	bool all_authenticated = false;
	typedef enum auth_state_e {
		CSSH_AUTH_INIT,
		CSSH_AUTH_VERIFY_HOST_DONE,
		CSSH_AUTH_NONE_DONE,
		CSSH_AUTH_BANNER_DONE,
		CSSH_AUTH_PUBKEY_DONE_SUCCESS,
		CSSH_AUTH_PUBKEY_DONE_FAILED,
		CSSH_AUTH_PASSWORD_DONE_SUCCESS,
		CSSH_AUTH_PASSWORD_DONE_FAILED,
		CSSH_AUTH_DONE_SUCCESS,
		CSSH_AUTH_DONE_FAILED
	} auth_state_e;
	auth_state_e *auth_state = (auth_state_e *)malloc( ( nof_sessions + 1 ) * sizeof( auth_state_e ) );
	if( auth_state == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for 'auth_state'" );
		return -1;
	}
	memset( auth_state, CSSH_AUTH_INIT, ( nof_sessions + 1 ) * sizeof( auth_state_e ) );
	while( !all_authenticated ) {	
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			switch( auth_state[i] ) {
				case CSSH_AUTH_INIT:
					if( verify_knownhost( session[i] ) < 0 ) {
						fprintf( stderr, "ERROR: closing connection to '%s', port '%d' due to security reasons\n",
							host[i], port[i] );
						cleanup_sessions( &session,  NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
					auth_state[i] = CSSH_AUTH_VERIFY_HOST_DONE;
					break;
					
				case CSSH_AUTH_VERIFY_HOST_DONE:
					// returns Failed to request "ssh-userauth" service, not handling SSH_AUTH_AGAIN correctly internally?
					ssh_set_blocking( session[i], 1 );
					rc = ssh_userauth_none( session[i], NULL );
					if( rc == SSH_AUTH_SUCCESS ) {
						auth_state[i] = CSSH_AUTH_NONE_DONE;
						ssh_set_blocking( session[i], 1 );
					} else if( rc == SSH_AUTH_AGAIN ) {
						// ok, wait next iteration
					} else if( rc == SSH_AUTH_DENIED ) {
						auth_state[i] = CSSH_AUTH_NONE_DONE;
						//~ ssh_set_blocking( session[i], 0 );
					} else {
						fprintf( stderr, "ERROR: ssh_userauth_none to '%s', port '%d' failed: %s\n",
							host[i], port[i], ssh_get_error( session[i] ) );
						cleanup_sessions( &session,  NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
					break;
				
				case CSSH_AUTH_NONE_DONE: {
					ssh_set_blocking( session[i], 1 );
					char *banner = ssh_get_issue_banner( session[i] );
					if( banner ) {
						puts( banner );
						free( banner );
					}
					auth_state[i] = CSSH_AUTH_BANNER_DONE;
					} break;
				
				case CSSH_AUTH_BANNER_DONE:
					rc = authenticate_pubkey( session[i] );

					if( rc == SSH_AUTH_SUCCESS ) {
						auth_state[i] = CSSH_AUTH_PUBKEY_DONE_SUCCESS;
					} else if( rc == SSH_AUTH_AGAIN ) {
						// ok, wait next iteration
					} else if( rc == SSH_AUTH_DENIED ) {
						auth_state[i] = CSSH_AUTH_PUBKEY_DONE_FAILED;
					} else {
						cleanup_sessions( &session,  NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
					break;
				
				case CSSH_AUTH_PUBKEY_DONE_SUCCESS:
					auth_state[i] = CSSH_AUTH_DONE_SUCCESS;
					break;
				
				case CSSH_AUTH_PUBKEY_DONE_FAILED:
					rc = authenticate_password( session[i], host[i], port[i], args_info.login_given ? args_info.login_arg : NULL );
					
					if( rc == SSH_AUTH_SUCCESS ) {
						auth_state[i] = CSSH_AUTH_PASSWORD_DONE_SUCCESS;
					} else if( rc == SSH_AUTH_AGAIN ) {
						// ok, wait next iteration
					} else if( rc == SSH_AUTH_DENIED ) {
						auth_state[i] = CSSH_AUTH_PASSWORD_DONE_FAILED;
					} else {
						cleanup_sessions( &session, NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
					break;
				
				case CSSH_AUTH_PASSWORD_DONE_SUCCESS:
					auth_state[i] = CSSH_AUTH_DONE_SUCCESS;
					break;

				case CSSH_AUTH_PASSWORD_DONE_FAILED:
					auth_state[i] = CSSH_AUTH_DONE_FAILED;
					break;
					
				case CSSH_AUTH_DONE_SUCCESS:
					// ok
					break;
				
				case CSSH_AUTH_DONE_FAILED:
					// one authentication failed, bail out for now
					fprintf( stderr, "ERROR: authentication failed for one host ('%s'), aborting now", host[i] );
					cleanup_sessions( &session, NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
					exit( EXIT_FAILURE );
			}
		}

		all_authenticated = true;
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			if( auth_state[i] != CSSH_AUTH_DONE_SUCCESS ) {
				all_authenticated = false;
			}
		}
		
		cssh_msleep( 10 );
	}

	ssh_channel *channel = (ssh_channel *)malloc( ( nof_sessions + 1 ) * sizeof( ssh_channel ) );
	memset( channel, 0, nof_sessions + 1 );
	if( channel == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for ssh_channels" );
		cleanup_sessions( &session, NULL, host, port, nof_sessions, args_info.verbose_given > 0 );
		exit( EXIT_FAILURE );
	}
	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		channel[i] = ssh_channel_new( session[i] );
		if( channel[i] == NULL ) {
			fprintf( stderr, "ERROR: Unable to open SSH channel: %s\n",
				ssh_get_error( session[i] ) );
			cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
			exit( EXIT_FAILURE );
		}
	}

	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		rc = ssh_channel_open_session( channel[i] );
		if( rc != SSH_OK ) {
			cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
			exit( EXIT_FAILURE );
		}
	}

	char cmd[1024];
	cmd[0] = '\0';
	if( args_info.inputs_num > 0 ) {
		for( int i = command_pos; i < args_info.inputs_num; i++ ) {
			if( i != command_pos ) {
				strncat( cmd, " ", sizeof( cmd ) - strlen( cmd ) - 1 );
			}
			strncat( cmd, args_info.inputs[i], sizeof( cmd ) - strlen( cmd ) - 1 );
		}
	}
	if( cmd[0] == '\0' ) {
		fprintf( stderr, "ERROR: Empty command, no interactive CLI supported currently\n" );
		cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
		exit( EXIT_FAILURE );
	}

	for( unsigned int i = 0; i < nof_sessions; i++ ) {
		rc = ssh_channel_request_exec( channel[i], cmd );
		if( rc != SSH_OK ) {
			fprintf( stderr, "ERROR: Executing SSH command '%s' failed: %s\n",
				cmd, ssh_get_error( session[i] ) );
			cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
			exit( EXIT_FAILURE );
		}
	}

	bool all_eof = false;
	bool *eof_sent = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) );
	if( eof_sent == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for 'eof_sent'" );
		return -1;
	}
	memset( eof_sent, false, ( nof_sessions + 1 ) * sizeof( bool ) );
	bool *is_eof = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) );
	if( is_eof == NULL ) {
		fprintf( stderr, "ERROR: Memory allocation failed for 'is_eof'" );
		return -1;
	}
	memset( is_eof, false, ( nof_sessions + 1 ) * sizeof( bool ) );
	
	while( !all_eof ) {

		all_eof = true;
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			if( !ssh_channel_is_closed( channel[i] ) && !ssh_channel_is_eof( channel[i] ) ) {
				all_eof = false;
			}
		}
		
		// no stdin sent to commands on remote machines for now
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			if( is_eof[i] && !eof_sent[i] ) {
				ssh_channel_send_eof( channel[i] );
				eof_sent[i] = true;
				continue;
			}
		}
		
		for( unsigned int i = 0; i < nof_sessions; i++ ) {
			char buffer[4096];
			rc = ssh_channel_poll( channel[i], 0 );
			if( rc == SSH_ERROR ) {
				fprintf( stderr, "ERROR: ssh_channel_poll on stdout failed: %s\n",
					ssh_get_error( session[i] ) );
				cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
				exit( EXIT_FAILURE );
			}
								
			if( rc > 0 ) {
				unsigned int nread = ssh_channel_read_nonblocking( channel[i], buffer, sizeof( buffer ), 0 );
				if( nread == SSH_ERROR ) {
					fprintf( stderr, "ERROR: ssh_channel_read_nonblocking on stdout failed: %s\n",
						ssh_get_error( session[i] ) );
					cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
					exit( EXIT_FAILURE );
				}
				
				if( nread == 0 ) {
					is_eof[i] = true;
				}

				if( nread > 0 ) {
					size_t wrc = fwrite( buffer, 1, nread, stdout );
					if( wrc < 0 ) {
						fprintf( stderr, "ERROR: while writing to stdout: %s\n",
							strerror( errno ) );
						cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}

					if( wrc != nread ) {
						fprintf( stderr, "ERROR: Write mismatch on stdout (%zu != %d)\n",
							wrc, nread );
						cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
				}
			}

			rc = ssh_channel_poll( channel[i], 1 );
			if( rc == SSH_ERROR ) {
				fprintf( stderr, "ERROR: ssh_channel_poll on stderr failed: %s\n",
					ssh_get_error( session[i] ) );
				cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
				exit( EXIT_FAILURE );
			}
					
			if( rc > 0 ) {
				unsigned int nread = ssh_channel_read_nonblocking( channel[i], buffer, sizeof( buffer ), 1 );
				if( nread == SSH_ERROR ) {
					fprintf( stderr, "ERROR: ssh_channel_read_nonblocking on stderr failed: %s\n",
						ssh_get_error( session[i] ) );
					cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
					exit( EXIT_FAILURE );
				}

				if( nread > 0 ) {
					size_t wrc = fwrite( buffer, 1, nread, stderr );
					if( wrc < 0 ) {
						fprintf( stderr, "ERROR: while writting to stderr: %s\n",
							strerror( errno ) );
						cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}

					if( wrc != nread ) {
						fprintf( stderr, "ERROR: Write mismatch on stderr (%zu != %d)\n",
							wrc, nread );
						cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
						exit( EXIT_FAILURE );
					}
				}

				if( nread == 0 ) {
					is_eof[i] = true;
				}
			}
		}
		
		cssh_msleep( 1 );
	}
	
	cleanup_sessions( &session, &channel, host, port, nof_sessions, args_info.verbose_given > 0 );
	
	exit( EXIT_SUCCESS );
}