From cd0a98e221ad70798a9b0efecb82e5a49b0ed3b7 Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Fri, 9 Nov 2018 17:41:36 +0100 Subject: [PATCH] ssh-base: support libssh config file. It's operating system dependent, but the library takes care of it on different operating systems. Options are set with this precedence: - if user-provided, use it - if not, take the one from config file - (username only) if none in the config file, take the current user from OS Change-Id: I00dcc1c9a8613e6d1250b6404bf2100f6ccff7b7 Reviewed-on: https://code.wireshark.org/review/30558 Petri-Dish: Dario Lombardo Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- extcap/ssh-base.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/extcap/ssh-base.c b/extcap/ssh-base.c index 4f07e6f15f..68732a8331 100644 --- a/extcap/ssh-base.c +++ b/extcap/ssh-base.c @@ -23,6 +23,8 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, char** err_info) { ssh_session sshs; + gchar* user_set = NULL; + guint port_set; /* Open session and set options */ sshs = ssh_new(); @@ -41,6 +43,13 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, goto failure; } + /* Load the configurations already present in the system configuration file. */ + /* They will be overwritten by the user-provided configurations. */ + if (ssh_options_parse_config(sshs, NULL) != 0) { + *err_info = g_strdup("Unable to load the configuration file"); + goto failure; + } + if (port != 0) { if (ssh_options_set(sshs, SSH_OPTIONS_PORT, &port)) { *err_info = g_strdup_printf("Can't set the port: %d", port); @@ -48,14 +57,6 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, } } - if (!username) - username = g_get_user_name(); - - if (ssh_options_set(sshs, SSH_OPTIONS_USER, username)) { - *err_info = g_strdup_printf("Can't set the username: %s", username); - goto failure; - } - if (proxycommand) { if (ssh_options_set(sshs, SSH_OPTIONS_PROXYCOMMAND, proxycommand)) { *err_info = g_strdup_printf("Can't set the ProxyCommand: %s", proxycommand); @@ -63,12 +64,23 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, } } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", username, hostname, port); + if (username) { + if (ssh_options_set(sshs, SSH_OPTIONS_USER, username)) { + *err_info = g_strdup_printf("Can't set the username: %s", username); + goto failure; + } + } + + ssh_options_get(sshs, SSH_OPTIONS_USER, &user_set); + ssh_options_get_port(sshs, &port_set); + + g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", user_set, hostname, port_set); + + ssh_string_free_char(user_set); /* Connect to server */ if (ssh_connect(sshs) != SSH_OK) { - *err_info = g_strdup_printf("Error connecting to %s@%s:%u (%s)", username, hostname, port, - ssh_get_error(sshs)); + *err_info = g_strdup_printf("Connection error: %s", ssh_get_error(sshs)); goto failure; }