diff options
author | Dario Lombardo <lomato@gmail.com> | 2018-11-09 23:10:46 +0100 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2018-11-14 04:58:15 +0000 |
commit | 7a29c3d2eb74d60cb6e60aa1ab89b9959370228b (patch) | |
tree | 7b33d8512f0d2be49fa413afb556028325601ca7 /extcap | |
parent | cd0a98e221ad70798a9b0efecb82e5a49b0ed3b7 (diff) |
ssh-base: define a struct for storing ssh parameters.
Update sshdump and ciscodump to use it.
Change-Id: I5fbb9e3a870ec8baa0f326ad34733743cbb981f3
Reviewed-on: https://code.wireshark.org/review/30571
Petri-Dish: Dario Lombardo <lomato@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'extcap')
-rw-r--r-- | extcap/ciscodump.c | 52 | ||||
-rw-r--r-- | extcap/ssh-base.c | 68 | ||||
-rw-r--r-- | extcap/ssh-base.h | 20 | ||||
-rw-r--r-- | extcap/sshdump.c | 55 |
4 files changed, 101 insertions, 94 deletions
diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index d0893c86c0..85c23c3580 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -405,8 +405,7 @@ error: return NULL; } -static int ssh_open_remote_connection(const char* hostname, const unsigned int port, const char* username, const char* password, - const char* sshkey, const char* sshkey_passphrase, const char* proxycommand, const char* iface, const char* cfilter, +static int ssh_open_remote_connection(const ssh_params_t* ssh_params, const char* iface, const char* cfilter, const guint32 count, const char* fifo) { ssh_session sshs; @@ -426,7 +425,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, proxycommand, &err_info); + sshs = create_ssh_connection(ssh_params, &err_info); if (!sshs) { g_warning("Error creating connection: %s", err_info); goto cleanup; @@ -518,14 +517,8 @@ int real_main(int argc, char **argv) { int result; int option_idx = 0; - char* remote_host = NULL; - guint16 remote_port = 22; - char* remote_username = NULL; - char* remote_password = NULL; + ssh_params_t* ssh_params = ssh_params_new(); char* remote_interface = NULL; - char* sshkey = NULL; - char* sshkey_passphrase = NULL; - char* proxycommand = NULL; char* remote_filter = NULL; guint32 count = 0; int ret = EXIT_FAILURE; @@ -590,42 +583,42 @@ int real_main(int argc, char **argv) goto end; case OPT_REMOTE_HOST: - g_free(remote_host); - remote_host = g_strdup(optarg); + g_free(ssh_params->host); + ssh_params->host = g_strdup(optarg); break; case OPT_REMOTE_PORT: - if (!ws_strtou16(optarg, NULL, &remote_port) || remote_port == 0) { + if (!ws_strtou16(optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { g_warning("Invalid port: %s", optarg); goto end; } break; case OPT_REMOTE_USERNAME: - g_free(remote_username); - remote_username = g_strdup(optarg); + g_free(ssh_params->username); + ssh_params->username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: - g_free(remote_password); - remote_password = g_strdup(optarg); + g_free(ssh_params->password); + ssh_params->password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: - g_free(sshkey); - sshkey = g_strdup(optarg); + g_free(ssh_params->sshkey_path); + ssh_params->sshkey_path = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: - g_free(sshkey_passphrase); - sshkey_passphrase = g_strdup(optarg); + g_free(ssh_params->sshkey_passphrase); + ssh_params->sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_PROXYCOMMAND: - g_free(proxycommand); - proxycommand = g_strdup(optarg); + g_free(ssh_params->proxycommand); + ssh_params->proxycommand = g_strdup(optarg); break; case OPT_REMOTE_INTERFACE: @@ -671,7 +664,7 @@ int real_main(int argc, char **argv) } if (extcap_conf->show_config) { - ret = list_config(extcap_conf->interface, remote_port); + ret = list_config(extcap_conf->interface, ssh_params->port); goto end; } @@ -684,7 +677,7 @@ int real_main(int argc, char **argv) #endif /* _WIN32 */ if (extcap_conf->capture) { - if (!remote_host) { + if (!ssh_params->host) { g_warning("Missing parameter: --remote-host"); goto end; } @@ -697,8 +690,7 @@ int real_main(int argc, char **argv) g_warning("ERROR: count of packets must be specified (--remote-count)"); goto end; } - ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, - remote_password, sshkey, sshkey_passphrase, proxycommand, remote_interface, + ret = ssh_open_remote_connection(ssh_params, remote_interface, remote_filter, count, extcap_conf->fifo); } else { g_debug("You should not come here... maybe some parameter missing?"); @@ -706,12 +698,8 @@ int real_main(int argc, char **argv) } end: - g_free(remote_host); - g_free(remote_username); - g_free(remote_password); + ssh_params_free(ssh_params); g_free(remote_interface); - g_free(sshkey); - g_free(sshkey_passphrase); g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; diff --git a/extcap/ssh-base.c b/extcap/ssh-base.c index 68732a8331..d67ef91fef 100644 --- a/extcap/ssh-base.c +++ b/extcap/ssh-base.c @@ -18,13 +18,11 @@ #include <log.h> #include <string.h> -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, const char* proxycommand, - char** err_info) +ssh_session create_ssh_connection(const ssh_params_t* ssh_params, char** err_info) { ssh_session sshs; - gchar* user_set = NULL; - guint port_set; + gchar* username = NULL; + guint port; /* Open session and set options */ sshs = ssh_new(); @@ -33,13 +31,13 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, return NULL; } - if (!hostname) { + if (!ssh_params->host) { *err_info = g_strdup("Hostname needed"); goto failure; } - if (ssh_options_set(sshs, SSH_OPTIONS_HOST, hostname)) { - *err_info = g_strdup_printf("Can't set the hostname: %s", hostname); + if (ssh_options_set(sshs, SSH_OPTIONS_HOST, ssh_params->host)) { + *err_info = g_strdup_printf("Can't set the host: %s", ssh_params->host); goto failure; } @@ -50,33 +48,35 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, goto failure; } - if (port != 0) { + if (ssh_params->port != 0) { + port = ssh_params->port; if (ssh_options_set(sshs, SSH_OPTIONS_PORT, &port)) { - *err_info = g_strdup_printf("Can't set the port: %d", port); + *err_info = g_strdup_printf("Can't set the port: %u", port); 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); + if (ssh_params->proxycommand) { + if (ssh_options_set(sshs, SSH_OPTIONS_PROXYCOMMAND, ssh_params->proxycommand)) { + *err_info = g_strdup_printf("Can't set the ProxyCommand: %s", ssh_params->proxycommand); goto failure; } } - if (username) { - if (ssh_options_set(sshs, SSH_OPTIONS_USER, username)) { - *err_info = g_strdup_printf("Can't set the username: %s", username); + if (ssh_params->username) { + if (ssh_options_set(sshs, SSH_OPTIONS_USER, ssh_params->username)) { + *err_info = g_strdup_printf("Can't set the username: %s", ssh_params->username); goto failure; } } - ssh_options_get(sshs, SSH_OPTIONS_USER, &user_set); - ssh_options_get_port(sshs, &port_set); + ssh_options_get(sshs, SSH_OPTIONS_USER, &username); + ssh_options_get_port(sshs, &port); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", user_set, hostname, port_set); + g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", username, + ssh_params->host, port); - ssh_string_free_char(user_set); + ssh_string_free_char(username); /* Connect to server */ if (ssh_connect(sshs) != SSH_OK) { @@ -95,12 +95,12 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, #endif /* If a public key path has been provided, try to authenticate using it */ - if (sshkey_path) { + if (ssh_params->sshkey_path) { ssh_key pkey = ssh_key_new(); int ret; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using public key in %s...", sshkey_path); - ret = ssh_pki_import_privkey_file(sshkey_path, sshkey_passphrase, NULL, NULL, &pkey); + g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using public key in %s...", ssh_params->sshkey_path); + ret = ssh_pki_import_privkey_file(ssh_params->sshkey_path, ssh_params->sshkey_passphrase, NULL, NULL, &pkey); if (ret == SSH_OK) { if (ssh_userauth_publickey(sshs, NULL, pkey) == SSH_AUTH_SUCCESS) { @@ -122,9 +122,9 @@ ssh_session create_ssh_connection(const char* hostname, const unsigned int port, g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed"); /* If a password has been provided and all previous attempts failed, try to use it */ - if (password) { + if (ssh_params->password) { g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using password..."); - if (ssh_userauth_password(sshs, username, password) == SSH_AUTH_SUCCESS) { + if (ssh_userauth_password(sshs, ssh_params->username, ssh_params->password) == SSH_AUTH_SUCCESS) { g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done"); return sshs; } @@ -173,6 +173,24 @@ void ssh_cleanup(ssh_session* sshs, ssh_channel* channel) } } +ssh_params_t* ssh_params_new(void) +{ + return g_new0(ssh_params_t, 1); +} + +void ssh_params_free(ssh_params_t* ssh_params) +{ + if (!ssh_params) + return; + g_free(ssh_params->host); + g_free(ssh_params->username); + g_free(ssh_params->password); + g_free(ssh_params->sshkey_path); + g_free(ssh_params->sshkey_passphrase); + g_free(ssh_params->proxycommand); + g_free(ssh_params); +} + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/extcap/ssh-base.h b/extcap/ssh-base.h index 2280477bbd..a19d5a2741 100644 --- a/extcap/ssh-base.h +++ b/extcap/ssh-base.h @@ -37,10 +37,18 @@ { "sshkey-passphrase", required_argument, NULL, OPT_SSHKEY_PASSPHRASE}, \ { "proxycommand", required_argument, NULL, OPT_PROXYCOMMAND} +typedef struct _ssh_params { + gchar* host; + guint16 port; + gchar* username; + gchar* password; + gchar* sshkey_path; + gchar* sshkey_passphrase; + gchar* proxycommand; +} ssh_params_t; + /* Create a ssh connection using all the possible authentication menthods */ -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, const char* proxycommand, - char** err_info); +ssh_session create_ssh_connection(const ssh_params_t* ssh_params, char** err_info); /* Write a formatted message in the channel */ int ssh_channel_printf(ssh_channel channel, const char* fmt, ...); @@ -48,6 +56,12 @@ int ssh_channel_printf(ssh_channel channel, const char* fmt, ...); /* Clean the current ssh session and channel. */ void ssh_cleanup(ssh_session* sshs, ssh_channel* channel); +/* Init the ssh_params_t structure */ +ssh_params_t* ssh_params_new(void); + +/* Clean the ssh params */ +void ssh_params_free(ssh_params_t* ssh_params); + #endif /* diff --git a/extcap/sshdump.c b/extcap/sshdump.c index 6c6a1ae30a..75c6086582 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -176,8 +176,7 @@ static ssh_channel run_ssh_command(ssh_session sshs, const char* capture_command return channel; } -static int ssh_open_remote_connection(const char* hostname, const unsigned int port, const char* username, const char* password, - const char* sshkey, const char* sshkey_passphrase, const char* proxycommand, const char* iface, const char* cfilter, +static int ssh_open_remote_connection(const ssh_params_t* params, const char* iface, const char* cfilter, const char* capture_command, const gboolean use_sudo, gboolean noprom, const guint32 count, const char* fifo) { ssh_session sshs = NULL; @@ -195,7 +194,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, proxycommand, &err_info); + sshs = create_ssh_connection(params, &err_info); if (!sshs) { g_warning("Error creating connection."); @@ -332,15 +331,9 @@ int real_main(int argc, char **argv) { int result; int option_idx = 0; - char* remote_host = NULL; - guint16 remote_port = 22; - char* remote_username = NULL; - char* remote_password = NULL; + ssh_params_t* ssh_params = ssh_params_new(); char* remote_interface = NULL; char* remote_capture_command = NULL; - char* sshkey = NULL; - char* sshkey_passphrase = NULL; - char* proxycommand = NULL; char* remote_filter = NULL; guint32 count = 0; int ret = EXIT_FAILURE; @@ -410,42 +403,42 @@ int real_main(int argc, char **argv) goto end; case OPT_REMOTE_HOST: - g_free(remote_host); - remote_host = g_strdup(optarg); + g_free(ssh_params->host); + ssh_params->host = g_strdup(optarg); break; case OPT_REMOTE_PORT: - if (!ws_strtou16(optarg, NULL, &remote_port) || remote_port == 0) { + if (!ws_strtou16(optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { g_warning("Invalid port: %s", optarg); goto end; } break; case OPT_REMOTE_USERNAME: - g_free(remote_username); - remote_username = g_strdup(optarg); + g_free(ssh_params->username); + ssh_params->username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: - g_free(remote_password); - remote_password = g_strdup(optarg); + g_free(ssh_params->password); + ssh_params->password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: - g_free(sshkey); - sshkey = g_strdup(optarg); + g_free(ssh_params->sshkey_path); + ssh_params->sshkey_path = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: - g_free(sshkey_passphrase); - sshkey_passphrase = g_strdup(optarg); + g_free(ssh_params->sshkey_passphrase); + ssh_params->sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_PROXYCOMMAND: - g_free(proxycommand); - proxycommand = g_strdup(optarg); + g_free(ssh_params->proxycommand); + ssh_params->proxycommand = g_strdup(optarg); break; case OPT_REMOTE_INTERFACE: @@ -499,7 +492,7 @@ int real_main(int argc, char **argv) } if (extcap_conf->show_config) { - ret = list_config(extcap_conf->interface, remote_port); + ret = list_config(extcap_conf->interface, ssh_params->port); goto end; } @@ -514,13 +507,12 @@ int real_main(int argc, char **argv) if (extcap_conf->capture) { char* filter; - if (!remote_host) { + if (!ssh_params->host) { g_warning("Missing parameter: --remote-host"); goto end; } filter = concat_filters(extcap_conf->capture_filter, remote_filter); - ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, - remote_password, sshkey, sshkey_passphrase, proxycommand, remote_interface, + ret = ssh_open_remote_connection(ssh_params, remote_interface, filter, remote_capture_command, use_sudo, noprom, count, extcap_conf->fifo); g_free(filter); } else { @@ -530,14 +522,9 @@ int real_main(int argc, char **argv) end: /* clean up stuff */ - g_free(remote_host); - g_free(remote_username); - g_free(remote_password); - g_free(remote_interface); + ssh_params_free(ssh_params); g_free(remote_capture_command); - g_free(sshkey); - g_free(sshkey_passphrase); - g_free(proxycommand); + g_free(remote_interface); g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; |