From d8f541c89b94ae0fe6233bd93dbd0d910a65ccdd Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Tue, 1 Mar 2016 14:13:41 +0100 Subject: extcap: move ssh common functions to ssh-base. Change-Id: Ic06882a4f914b4f89d936d5d942b50552ae4abb3 Reviewed-on: https://code.wireshark.org/review/14264 Petri-Dish: Roland Knall Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- extcap/sshdump.c | 133 ++++--------------------------------------------------- 1 file changed, 8 insertions(+), 125 deletions(-) (limited to 'extcap/sshdump.c') diff --git a/extcap/sshdump.c b/extcap/sshdump.c index f0055fc40e..814328c1ab 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -26,18 +26,11 @@ #include #include +#include -#include -#include -#include -#include #include -#include #include -#include -#include #include -#include #ifndef STDERR_FILENO #define STDERR_FILENO 2 @@ -97,121 +90,6 @@ static struct option longopts[] = { static char* interfaces_list_to_filter(GSList* if_list, const unsigned int remote_port); -static void ssh_cleanup(ssh_session sshs, ssh_channel channel) -{ - if (channel) { - ssh_channel_send_eof(channel); - ssh_channel_close(channel); - ssh_channel_free(channel); - } - - if (sshs) { - ssh_disconnect(sshs); - ssh_free(sshs); - } -} - -static ssh_session create_ssh_connection(const char* hostname, const unsigned int port, const char* username, - const char* password, const char* sshkey_path, const char* sshkey_passphrase) -{ - ssh_session sshs; - - /* Open session and set options */ - sshs = ssh_new(); - if (sshs == NULL) { - errmsg_print("Can't create ssh session"); - return NULL; - } - - if (!hostname) - return NULL; - - if (ssh_options_set(sshs, SSH_OPTIONS_HOST, hostname)) { - errmsg_print("Can't set the hostname: %s", hostname); - goto failure; - } - - if (port != 0) { - if (ssh_options_set(sshs, SSH_OPTIONS_PORT, &port)) { - errmsg_print("Can't set the port: %d", port); - goto failure; - } - } - - if (!username) - username = g_get_user_name(); - - if (ssh_options_set(sshs, SSH_OPTIONS_USER, username)) { - errmsg_print("Can't set the username: %s", username); - goto failure; - } - - verbose_print("Opening ssh connection to %s@%s:%u", username, hostname, port); - - /* Connect to server */ - if (ssh_connect(sshs) != SSH_OK) { - errmsg_print("Error connecting to %s@%s:%u (%s)", username, hostname, port, - ssh_get_error(sshs)); - goto failure; - } - -#ifdef HAVE_LIBSSH_USERAUTH_AGENT - verbose_print("Connecting using ssh-agent..."); - /* Try to authenticate using ssh agent */ - if (ssh_userauth_agent(sshs, NULL) == SSH_AUTH_SUCCESS) { - verbose_print("done\n"); - return sshs; - } - verbose_print("failed\n"); -#endif - - /* If a public key path has been provided, try to authenticate using it */ - if (sshkey_path) { - ssh_key pkey = ssh_key_new(); - int ret; - - verbose_print("Connecting using public key in %s...", sshkey_path); - ret = ssh_pki_import_privkey_file(sshkey_path, sshkey_passphrase, NULL, NULL, &pkey); - - if (ret == SSH_OK) { - if (ssh_userauth_publickey(sshs, NULL, pkey) == SSH_AUTH_SUCCESS) { - verbose_print("done\n"); - ssh_key_free(pkey); - return sshs; - } - } - ssh_key_free(pkey); - verbose_print("failed (%s)\n", ssh_get_error(sshs)); - } - - /* Try to authenticate using standard public key */ - verbose_print("Connecting using standard public key..."); - if (ssh_userauth_publickey_auto(sshs, NULL, NULL) == SSH_AUTH_SUCCESS) { - verbose_print("done\n"); - return sshs; - } - verbose_print("failed\n"); - - /* If a password has been provided and all previous attempts failed, try to use it */ - if (password) { - verbose_print("Connecting using password..."); - if (ssh_userauth_password(sshs, username, password) == SSH_AUTH_SUCCESS) { - verbose_print("done\n"); - return sshs; - } - verbose_print("failed\n"); - } - - errmsg_print("Can't find a valid authentication. Disconnecting."); - - /* All authentication failed. Disconnect and return */ - ssh_disconnect(sshs); - -failure: - ssh_free(sshs); - return NULL; -} - static void ssh_loop_read(ssh_channel channel, int fd) { int nbytes; @@ -314,6 +192,7 @@ static int ssh_open_remote_connection(const char* hostname, const unsigned int p ssh_channel channel = NULL; int fd = STDOUT_FILENO; int ret = EXIT_FAILURE; + char* err_info = NULL; if (g_strcmp0(fifo, "-")) { /* Open or create the output file */ @@ -327,7 +206,7 @@ static int ssh_open_remote_connection(const char* hostname, const unsigned int p } } - sshs = create_ssh_connection(hostname, port, username, password, sshkey, sshkey_passphrase); + sshs = create_ssh_connection(hostname, port, username, password, sshkey, sshkey_passphrase, &err_info); if (!sshs) goto cleanup; @@ -341,8 +220,12 @@ static int ssh_open_remote_connection(const char* hostname, const unsigned int p ret = EXIT_SUCCESS; cleanup: + if (err_info) + errmsg_print("%s", err_info); + g_free(err_info); + /* clean up and exit */ - ssh_cleanup(sshs, channel); + ssh_cleanup(&sshs, &channel); if (g_strcmp0(fifo, "-")) close(fd); -- cgit v1.2.3