SSH_SESSION *connect_host(const char *hostname){ SSH_SESSION *session; SSH_OPTIONS *options; int auth=0; int state; options=ssh_options_new(); ssh_options_set_host(options,hostname); session=ssh_new(); ssh_set_options(session,options); if(ssh_connect(session)){ fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } state = ssh_session_is_known_server(session); switch(state){ case SSH_SERVER_KNOWN_OK: break; /* ok */ case SSH_SERVER_KNOWN_CHANGED: fprintf(stderr,"Host key for server changed : server's one is now :\n"); fprintf(stderr,"For security reason, connection will be stopped\n"); ssh_disconnect(session); ssh_finalize(); return NULL; case SSH_SERVER_FOUND_OTHER: fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n"); fprintf(stderr,"An attacker might change the default server key to confuse your client" "into thinking the key does not exist\n" "We advise you to rerun the client with -d or -r for more safety.\n"); ssh_disconnect(session); ssh_finalize(); return NULL; case SSH_SERVER_NOT_KNOWN: fprintf(stderr,"The server is unknown. Leaving now"); ssh_disconnect(session); return NULL; case SSH_SERVER_ERROR: fprintf(stderr,"%s",ssh_get_error(session)); ssh_disconnect(session); return NULL; } ssh_userauth_none(session, NULL); auth=ssh_userauth_autopubkey(session, NULL); if(auth==SSH_AUTH_ERROR){ fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } if(auth!=SSH_AUTH_SUCCESS){ fprintf(stderr,"Authentication failed: %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } ssh_log(session, SSH_LOG_FUNCTIONS, "Authentication success"); return session; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test(torture_growing_buffer), cmocka_unit_test(torture_growing_buffer_shifting), cmocka_unit_test(torture_buffer_prepend), cmocka_unit_test(torture_ssh_buffer_get_ssh_string), cmocka_unit_test(torture_ssh_buffer_add_format), cmocka_unit_test(torture_ssh_buffer_get_format), cmocka_unit_test(torture_ssh_buffer_get_format_error), cmocka_unit_test(torture_buffer_pack_badformat), cmocka_unit_test(torture_mixed), }; /* * If the library is statically linked, ssh_init() is not called * automatically */ ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, NULL, NULL); ssh_finalize(); return rc; }
int main(int argc, char **argv){ ssh_session session; session = ssh_new(); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session)); usage(); } opts(argc,argv); #ifdef WITH_PCAP set_pcap(session); #endif client(session); ssh_disconnect(session); ssh_free(session); #ifdef WITH_PCAP cleanup_pcap(); #endif ssh_finalize(); return 0; }
int main(int argc, char **argv) { int i = 0; int rc = 0; unsetenv("SSH_AUTH_SOCK"); rc = ssh_init(); if (rc != 0) { rc = SSH_ERROR; goto out; } #ifdef HAVE_ARGP_H argp_parse(&parser, argc, argv, 0, 0, NULL); #else /* HAVE_ARGP_H */ (void) argc; (void) argv; #endif /* HAVE_ARGP_H */ if (pkd_dargs.opts.list != 0) { while (testmap[i].testname != NULL) { printf("%s\n", testmap[i++].testname); } } else { rc = pkd_run_tests(); } rc = ssh_finalize(); if (rc != 0) { fprintf(stderr, "ssh_finalize: %d\n", rc); } out: return rc; }
/** * curl_global_cleanup() globally cleanups curl, uses the value of * "init_flags" to determine what needs to be cleaned up and what doesn't. */ void curl_global_cleanup(void) { if(!initialized) return; if(--initialized) return; Curl_global_host_cache_dtor(); Curl_ssl_cleanup(); Curl_resolver_global_cleanup(); if(init_flags & CURL_GLOBAL_WIN32) win32_cleanup(); Curl_amiga_cleanup(); #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT) (void)libssh2_exit(); #endif #if defined(USE_LIBSSH) (void)ssh_finalize(); #endif init_flags = 0; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_knownhosts_export, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_knownhosts_write_and_verify, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_knownhosts_precheck, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_knownhosts_other, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_knownhosts_unknown, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_knownhosts_conflict, session_setup, session_teardown), }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, sshd_group_setup, sshd_group_teardown); ssh_finalize(); return rc; }
static void torture_log_callback(void **state) { struct test_mock_state t = { .executed = 0, }; (void)state; /* unused */ ssh_set_log_callback(test_mock_ssh_logging_callback); ssh_set_log_userdata(&t); ssh_set_log_level(1); expect_value(test_mock_ssh_logging_callback, priority, 1); expect_string(test_mock_ssh_logging_callback, function, "torture_log_callback"); expect_string(test_mock_ssh_logging_callback, buffer, "torture_log_callback: test"); SSH_LOG(SSH_LOG_WARN, "test"); assert_int_equal(t.executed, 1); } int torture_run_tests(void) { int rc; UnitTest tests[] = { unit_test_setup_teardown(torture_callbacks_size, setup, teardown), unit_test_setup_teardown(torture_callbacks_exists, setup, teardown), unit_test(torture_log_callback), }; ssh_init(); torture_filter_tests(tests); rc=run_tests(tests); ssh_finalize(); return rc; }
/** * curl_global_cleanup() globally cleanups curl, uses the value of * "init_flags" to determine what needs to be cleaned up and what doesn't. */ void curl_global_cleanup(void) { if(!initialized) return; if(--initialized) return; Curl_ssl_cleanup(); Curl_resolver_global_cleanup(); #ifdef WIN32 Curl_win32_cleanup(init_flags); #endif Curl_amiga_cleanup(); #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT) (void)libssh2_exit(); #endif #if defined(USE_LIBSSH) (void)ssh_finalize(); #endif init_flags = 0; }
int main(int argc, char **argv) { ssh_session_t *session = NULL; session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session)); usage(); } opts(argc,argv); signal(SIGTERM, do_exit); client(session); ssh_disconnect(session); ssh_free(session); ssh_finalize(); return 0; }
int doCopy(int argc, char **argv){ struct location *dest, *src; int i; int r; if(opts(argc,argv)<0) return EXIT_FAILURE; dest=parse_location(destination); if(open_location(dest,WRITE)<0) return EXIT_FAILURE; for(i=0;i<nsources;++i){ src=parse_location(sources[i]); if(open_location(src,READ)<0){ return EXIT_FAILURE; } if(do_copy(src,dest,0) < 0){ break; } } if(dest->is_ssh){ r=ssh_scp_close(dest->scp); if(r == SSH_ERROR){ fprintf(stderr,"Error closing scp: %s\n",ssh_get_error(dest->session)); ssh_scp_free(dest->scp); dest->scp=NULL; return -1; } } else { fclose(dest->file); dest->file=NULL; } ssh_disconnect(dest->session); ssh_finalize(); return 0; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_hostkey_rsa, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_hostkey_ed25519, session_setup, session_teardown), #ifdef HAVE_ECC cmocka_unit_test_setup_teardown(torture_hostkey_ecdsa, session_setup, session_teardown), #endif #ifdef HAVE_DSA cmocka_unit_test_setup_teardown(torture_hostkey_dss, session_setup, session_teardown), #endif /* the client is able to handle SHA2 extension (if negotiated) */ cmocka_unit_test_setup_teardown(torture_hostkey_rsa_sha256, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_hostkey_rsa_sha512, session_setup, session_teardown), }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); ssh_finalize(); return rc; }
int process_ssh_target(char * pcAddress, char * pcLogin, char * pcPasswd,char * pcDatafile) { ssh_session session; if ( ( NULL == pcAddress) || ( NULL == pcLogin) || ( NULL == pcPasswd) || ( NULL == pcDatafile) ) { printf("ERROR: Incorrect data; somewhere empty string.\n");//TODO return -8;//TODO } session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); user_host_file(pcLogin, pcAddress, pcDatafile); signal(SIGTERM, do_exit); /* Create input pipe between two endpoints */ pipe(input_pipe); #if defined(OUT_PIPE) /* Create output pipe between two endpoints */ pipe(output_pipe); #endif /* OUT_PIPE */ /* Launch Successor to push commands into tray */ iInput_Start("none", 25); #if defined(OUT_PIPE) // iOutput_Start("", 25); #endif /* OUT_PIPE */ client_ssh(session, pcPasswd); ssh_disconnect(session); ssh_free(session); ssh_finalize(); /* allocated in <assign_host_file> */ free (user); /* allocated in <assign_host_file> */ free (host); /* Free memory occupied by dynamically stored raw data */ DeleteCmds(&pCmdChain); return 0; }
int spatch() { ssh_session session = NULL; ssh_bind sshbind = NULL; sthreadList* list = NULL; int port = SERVER_PORT; sshbind=ssh_bind_new(); //session=ssh_new(); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key"); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key"); if (ssh_bind_listen(sshbind)<0) { printf("Error listening to socket: %s\n", ssh_get_error(sshbind)); return 1; } printf("Server start on port %d\n", port); while(1) { session=ssh_new(); if (ssh_bind_accept(sshbind, session) == SSH_ERROR) { printf("Error accepting a connection: %s\n", ssh_get_error(sshbind)); ssh_free(session); } else { list = newNodeList(list, session); if (pthread_create(&(list->thread), NULL, (void*)NewSessionLoop, (void*)&(list->sesData)) != 0) { sthreadList* tmp; ssh_disconnect(session); ssh_free(session); tmp = list; list = list->next; free(tmp); } } } if (session) ssh_free(session); cleanList(list); ssh_bind_free(sshbind); ssh_finalize(); return 0; }
// // dirty workaround here: miscptr is the ptr to the logins, and the first one is used // to test if password authentication is enabled!! // int32_t service_ssh_init(char *ip, int32_t sp, unsigned char options, char *miscptr, FILE * fp, int32_t port, char *hostname) { // called before the childrens are forked off, so this is the function // which should be filled if initial connections and service setup has to be // performed once only. // // fill if needed. // // return codes: // 0 all OK // 1 skip target without generating an error // 2 skip target because of protocol problems // 3 skip target because its unreachable #ifdef LIBSSH int32_t rc, method; ssh_session session = ssh_new(); if (verbose || debug) printf("[INFO] Testing if password authentication is supported by ssh://%s@%s:%d\n", miscptr == NULL ? "hydra" : miscptr, hydra_address2string_beautiful(ip), port); ssh_options_set(session, SSH_OPTIONS_PORT, &port); ssh_options_set(session, SSH_OPTIONS_HOST, hydra_address2string(ip)); if (miscptr == NULL) ssh_options_set(session, SSH_OPTIONS_USER, "hydra"); else ssh_options_set(session, SSH_OPTIONS_USER, miscptr); ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &hydra_options.waittime); ssh_options_set(session, SSH_OPTIONS_COMPRESSION_C_S, "none"); ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, "none"); if (ssh_connect(session) != 0) { fprintf(stderr, "[ERROR] could not connect to ssh://%s:%d - %s\n", hydra_address2string_beautiful(ip), port, ssh_get_error(session)); return 2; } rc = ssh_userauth_none(session, NULL); method = ssh_userauth_list(session, NULL); ssh_disconnect(session); ssh_finalize(); ssh_free(session); if (debug) printf("[DEBUG] SSH method check: %08x\n", method); if ((method & SSH_AUTH_METHOD_INTERACTIVE) || (method & SSH_AUTH_METHOD_PASSWORD)) { if (verbose || debug) printf("[INFO] Successful, password authentication is supported by ssh://%s:%d\n", hydra_address2string_beautiful(ip), port); return 0; } else if (method == 0) { if (verbose || debug) fprintf(stderr, "[WARNING] invalid SSH method reply from ssh://%s:%d, continuing anyway ... (check for empty password!)\n", hydra_address2string_beautiful(ip), port); return 0; } fprintf(stderr, "[ERROR] target ssh://%s:%d/ does not support password authentication (method reply %d).\n", hydra_address2string_beautiful(ip), port, method); return 1; #else return 0; #endif }
void service_ssh(char *ip, int32_t sp, unsigned char options, char *miscptr, FILE * fp, int32_t port, char *hostname) { int32_t run = 1, next_run = 1, sock = -1; hydra_register_socket(sp); if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0) return; while (1) { switch (run) { case 1: /* connect and service init function */ next_run = start_ssh(sock, ip, port, options, miscptr, fp); break; case 2: ssh_disconnect(session); ssh_finalize(); ssh_free(session); hydra_child_exit(0); break; case 3: ssh_disconnect(session); ssh_finalize(); ssh_free(session); if (verbose) fprintf(stderr, "[ERROR] ssh protocol error\n"); hydra_child_exit(2); break; case 4: ssh_disconnect(session); ssh_finalize(); ssh_free(session); fprintf(stderr, "[ERROR] ssh target does not support password auth\n"); hydra_child_exit(2); break; default: ssh_disconnect(session); ssh_finalize(); ssh_free(session); hydra_report(stderr, "[ERROR] Caught unknown return code, exiting!\n"); hydra_child_exit(2); } run = next_run; } }
static int teardown(void **state) { struct hostkey_state *h = (struct hostkey_state *)*state; unlink(h->hostkey); free(h->hostkey_path); free(h); ssh_finalize(); return 0; }
static int session_teardown(void **state) { struct torture_state *s = *state; ssh_disconnect(s->ssh.session); ssh_free(s->ssh.session); ssh_finalize(); return 0; }
int torture_run_tests(void) { int rc; const UnitTest tests[] = { unit_test(torture_ssh_is_ipaddr) }; ssh_init(); rc=run_tests(tests); ssh_finalize(); return rc; }
int process_target(int argc, char **argv) { ssh_session session; printf("process [%d] 0:%s 1:%s 2:%s 3:%s \n", argc, argv[0], argv[1], argv[2], argv[3] ); return 0;//+++ session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session) ); usage(); } opts(argc,argv); signal(SIGTERM, do_exit); /* Create input pipe between two endpoints */ pipe(input_pipe); #if defined(OUT_PIPE) /* Create output pipe between two endpoints */ pipe(output_pipe); #endif /* OUT_PIPE */ /* Launch Successor to push commands into tray */ iInput_Start("none", 25); #if defined(OUT_PIPE) // iOutput_Start("", 25); #endif /* OUT_PIPE */ client(session); ssh_disconnect(session); ssh_free(session); ssh_finalize(); /* Free memory occupied by dynamically stored raw data */ DeleteCmds(&pCmdChain); return 0; }
int main(int argc, char **argv){ ssh_session session; if(opts(argc,argv)<0) return EXIT_FAILURE; session=connect_ssh(host,NULL,verbosity); if(session == NULL) return EXIT_FAILURE; create_files(session); fetch_files(session); ssh_disconnect(session); ssh_finalize(); return 0; }
int torture_run_tests(void) { int rc; const UnitTest tests[] = { unit_test_setup_teardown(torture_growing_buffer, setup, teardown), unit_test_setup_teardown(torture_growing_buffer_shifting, setup, teardown), unit_test_setup_teardown(torture_buffer_prepend, setup, teardown), }; ssh_init(); rc=run_tests(tests); ssh_finalize(); return rc; }
int torture_run_tests(void) { int rc; const UnitTest tests[] = { unit_test_setup_teardown(torture_channel_read_error, setup, teardown), }; ssh_init(); rc = run_tests(tests); ssh_finalize(); return rc; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_auth_none, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_none_nonblocking, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_password, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_password_nonblocking, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_kbdint, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_kbdint_nonblocking, session_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_autopubkey, pubkey_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_autopubkey_nonblocking, pubkey_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_agent, agent_setup, agent_teardown), cmocka_unit_test_setup_teardown(torture_auth_agent_nonblocking, agent_setup, agent_teardown), cmocka_unit_test_setup_teardown(torture_auth_cert, pubkey_setup, session_teardown), cmocka_unit_test_setup_teardown(torture_auth_agent_cert, agent_cert_setup, agent_teardown), cmocka_unit_test_setup_teardown(torture_auth_agent_cert_nonblocking, agent_cert_setup, agent_teardown), }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); ssh_finalize(); return rc; }
// Shutdown the Context for this connection bool SSHClient::sshShutdown() { // GNASH_REPORT_FUNCTION; if (_session) { ssh_disconnect(_session); ssh_finalize(); } free(_session); _session = 0; return true; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test(torture_sftp_ext_new), }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, NULL, NULL); ssh_finalize(); return rc; }
int torture_run_tests(void) { int rc; UnitTest tests[] = { unit_test_setup_teardown(torture_sftp_mkdir, setup, teardown) }; ssh_init(); torture_filter_tests(tests); rc = run_tests(tests); ssh_finalize(); return rc; }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_request_env, setup, teardown), }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, NULL, NULL); ssh_finalize(); return rc; }
void vio_module_shutdown(csync_vio_method_t *method) { (void) method; if (_sftp_session) { sftp_free(_sftp_session); } if (_ssh_session) { ssh_disconnect(_ssh_session); } if (_ssh_callbacks) { free(_ssh_callbacks); } ssh_finalize(); }
// Connect to a SSH server. // When the connection is established, read data from stdin and send it to the server. void client_pipe(char *host, int port) { ssh_session s = ssh_new(); ssh_options_set(s, SSH_OPTIONS_HOST, host); ssh_options_set(s, SSH_OPTIONS_PORT, &port); ssh_options_set(s, SSH_OPTIONS_USER, "xya"); //ssh_options_set(s, SSH_OPTIONS_LOG_VERBOSITY_STR, "5"); if(ssh_connect(s) != SSH_OK) return session_error(s, "connect"); char *hash = pubkey_hash(ssh_get_pubkey(s)); if(authenticate(hash, 0)) { session_event(s, "authenticated", hash); free(hash); } else { free(hash); exit(1); } int keytype; ssh_string pub = publickey_from_file(s, "test-client-key.pub", &keytype); if(!pub) session_error(s, "open-public-key"); if(SSH_AUTH_SUCCESS != ssh_userauth_offer_pubkey(s, NULL, keytype, pub)) session_error(s, "offer-public-key"); ssh_private_key priv = privatekey_from_file(s, "test-client-key", keytype, NULL); if(!priv) session_error(s, "open-private-key"); if(SSH_AUTH_SUCCESS != ssh_userauth_pubkey(s, NULL, pub, priv)) session_error(s, "user-auth"); string_free(pub); privatekey_free(priv); ssh_channel chan = channel_new(s); if(!chan) session_error(s, "create-channel"); if(channel_open_session(chan) < 0) session_error(s, "open-channel"); session_event(s, "channel-opened", NULL); channel_from_file(chan, 0); channel_free(chan); ssh_disconnect(s); ssh_finalize(); }
int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(torture_sftp_read_blocking, session_setup, session_teardown) }; ssh_init(); torture_filter_tests(tests); rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); ssh_finalize(); return rc; }