2 * ssh-base has base utility functions to connect to hosts via ssh
4 * Copyright 2016, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <extcap/extcap-base.h>
31 #define verbose_print(...) { if (verbose) printf(__VA_ARGS__); }
33 ssh_session create_ssh_connection(const char* hostname, const unsigned int port, const char* username,
34 const char* password, const char* sshkey_path, const char* sshkey_passphrase, char** err_info)
38 /* Open session and set options */
41 *err_info = g_strdup_printf("Can't create ssh session");
46 *err_info = g_strdup_printf("Hostname needed");
50 if (ssh_options_set(sshs, SSH_OPTIONS_HOST, hostname)) {
51 *err_info = g_strdup_printf("Can't set the hostname: %s", hostname);
56 if (ssh_options_set(sshs, SSH_OPTIONS_PORT, &port)) {
57 *err_info = g_strdup_printf("Can't set the port: %d", port);
63 username = g_get_user_name();
65 if (ssh_options_set(sshs, SSH_OPTIONS_USER, username)) {
66 *err_info = g_strdup_printf("Can't set the username: %s", username);
70 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", username, hostname, port);
72 /* Connect to server */
73 if (ssh_connect(sshs) != SSH_OK) {
74 *err_info = g_strdup_printf("Error connecting to %s@%s:%u (%s)", username, hostname, port,
79 #ifdef HAVE_LIBSSH_USERAUTH_AGENT
80 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, ("Connecting using ssh-agent...");
81 /* Try to authenticate using ssh agent */
82 if (ssh_userauth_agent(sshs, NULL) == SSH_AUTH_SUCCESS) {
83 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done");
86 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, ("failed");
89 /* If a public key path has been provided, try to authenticate using it */
91 ssh_key pkey = ssh_key_new();
94 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using public key in %s...", sshkey_path);
95 ret = ssh_pki_import_privkey_file(sshkey_path, sshkey_passphrase, NULL, NULL, &pkey);
98 if (ssh_userauth_publickey(sshs, NULL, pkey) == SSH_AUTH_SUCCESS) {
99 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done");
105 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed (%s)", ssh_get_error(sshs));
108 /* Try to authenticate using standard public key */
109 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using standard public key...");
110 if (ssh_userauth_publickey_auto(sshs, NULL, NULL) == SSH_AUTH_SUCCESS) {
111 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done");
114 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed");
116 /* If a password has been provided and all previous attempts failed, try to use it */
118 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using password...");
119 if (ssh_userauth_password(sshs, username, password) == SSH_AUTH_SUCCESS) {
120 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done");
123 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed");
126 *err_info = g_strdup_printf("Can't find a valid authentication. Disconnecting.");
128 /* All authentication failed. Disconnect and return */
129 ssh_disconnect(sshs);
136 int ssh_channel_printf(ssh_channel channel, const char* fmt, ...)
140 int ret = EXIT_SUCCESS;
143 buf = g_strdup_vprintf(fmt, arg);
144 if (ssh_channel_write(channel, buf, (guint32)strlen(buf)) == SSH_ERROR)
152 void ssh_cleanup(ssh_session* sshs, ssh_channel* channel)
155 ssh_channel_send_eof(*channel);
156 ssh_channel_close(*channel);
157 ssh_channel_free(*channel);
162 ssh_disconnect(*sshs);
169 * Editor modelines - https://www.wireshark.org/tools/modelines.html
174 * indent-tabs-mode: t
177 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
178 * :indentSize=8:tabSize=8:noTabs=false: