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 <lomato@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Dario Lombardo 2018-11-09 17:41:36 +01:00 committed by Anders Broman
parent 3aec5e1a28
commit cd0a98e221
1 changed files with 23 additions and 11 deletions

View File

@ -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;
}