From 363d80d88e25f6346e85a9e84bbb23da3107a3e7 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 4 Sep 2015 21:11:46 +0200 Subject: some rearangements of code flow --- src/cssh.c | 123 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/src/cssh.c b/src/cssh.c index ba00c5c..bb03430 100644 --- a/src/cssh.c +++ b/src/cssh.c @@ -470,11 +470,76 @@ int main( int argc, char *argv[] ) exit( EXIT_SUCCESS ); } + // command line parsing and reading of additional files like the hosts file + unsigned int nof_sessions = 0; unsigned int command_pos = 0; char **host = NULL; unsigned short *port = NULL; + char cmd[1024]; + + switch( execution_mode ) { + case CSSH_EXECUTE_AS_SSH: { + // command line arguments are '[user@]host' + if( !args_info.hosts_file_given ) { + 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; + } + } + + // compose command to be executed in SSH mode + 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 ); + } + } + + // later, this is the sign to get into CLI mode + if( cmd[0] == '\0' ) { + fprintf( stderr, "ERROR: Empty command, no interactive CLI supported currently\n" ); + exit( EXIT_FAILURE ); + } + } break; + + case CSSH_EXECUTE_AS_SCP: { + // in SCP mode read source and destination + if( args_info.inputs_num != 2 ) { + if( args_info.inputs_num < 2 ) { + fprintf( stderr, "ERROR: Expecting a source and a destination when copying, encountered less arguments\n" ); + exit( EXIT_FAILURE ); + } else { + fprintf( stderr, "ERROR: Expecting a source and a destination when copying, encountered more arguments\n" ); + exit( EXIT_FAILURE ); + } + } + } break; + + case CSSH_EXECUTE_UNKNOWN: + break; + } + + // till now we have a default host, port and user and exactly one session, + // now inspect the hosts file for hosts if( args_info.hosts_file_given ) { + // the -l and -p parameters serve as defaults + // for the hosts in the file containing hosts + unsigned nof_hosts; unsigned short default_port = 22; if( args_info.port_given ) { @@ -485,24 +550,10 @@ int main( int argc, char *argv[] ) 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; - } } - + + // initialize session with parameters read + session = (ssh_session *)malloc( nof_sessions * sizeof( ssh_session ) ); if( session == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for ssh_sessions" ); @@ -514,7 +565,7 @@ int main( int argc, char *argv[] ) exit( EXIT_FAILURE ); } } - + int verbosity = SSH_LOG_NOLOG; if( args_info.verbose_given > 0 ) { verbosity += args_info.verbose_given; @@ -536,6 +587,8 @@ int main( int argc, char *argv[] ) } } + // asynchonous connection phase + bool all_connected = false; bool *is_connected = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) ); if( is_connected == NULL ) { @@ -575,6 +628,8 @@ int main( int argc, char *argv[] ) cssh_msleep( 10 ); } + // authentication phase + bool all_authenticated = false; auth_state_e *auth_state = (auth_state_e *)malloc( ( nof_sessions + 1 ) * sizeof( auth_state_e ) ); if( auth_state == NULL ) { @@ -690,6 +745,8 @@ int main( int argc, char *argv[] ) cssh_msleep( 10 ); } + // execution/copy phase + switch( execution_mode ) { case CSSH_EXECUTE_AS_SSH: { @@ -719,23 +776,6 @@ int main( int argc, char *argv[] ) } } - // compose command to be executed in SSH mode - 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, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); - exit( EXIT_FAILURE ); - } - // execute command on all channels for( unsigned int i = 0; i < nof_sessions; i++ ) { rc = ssh_channel_request_exec( channel[i], cmd ); @@ -890,16 +930,6 @@ int main( int argc, char *argv[] ) case CSSH_EXECUTE_AS_SCP: { - if( args_info.inputs_num != 2 ) { - if( args_info.inputs_num < 2 ) { - fprintf( stderr, "ERROR: Expecting a source and a destination when copying, encountered less arguments\n" ); - } else { - fprintf( stderr, "ERROR: Expecting a source and a destination when copying, encountered more arguments\n" ); - } - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); - exit( EXIT_FAILURE ); - } - // TODO: parse 2 arguments and see where we have a local // path and where a host destination with path // TODO: host destination can be explicit (one without -H hosts @@ -1217,7 +1247,8 @@ int main( int argc, char *argv[] ) case CSSH_EXECUTE_UNKNOWN: break; } - + + // the end exit( EXIT_SUCCESS ); } -- cgit v1.2.3-54-g00ecf