#include "ssh_run.h"
#include <libssh2.h>
#include "zbxcacheconfig.h"
#include "zbxcomms.h"
#include "zbxfile.h"
#include "zbxstr.h"
#include "zbxtime.h"
#if !defined(HAVE_LIBSSH2_METHOD_KEX) && !defined(HAVE_LIBSSH2_METHOD_HOSTKEY) && \
!defined(HAVE_LIBSSH2_METHOD_CRYPT_CS) && !defined(HAVE_LIBSSH2_METHOD_CRYPT_SC) && \
!defined(HAVE_LIBSSH2_METHOD_MAC_CS) && !defined(HAVE_LIBSSH2_METHOD_MAC_SC)
#define HAVE_NO_LIBSSH2_METHODS 1
#endif
static ZBX_THREAD_LOCAL const char *password;
#ifndef HAVE_NO_LIBSSH2_METHODS
static int ssh_set_options(LIBSSH2_SESSION *session, int type, const char *key_str, const char *value,
char **err_msg)
{
int res, ret = SUCCEED;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_str:'%s' value:'%s'", __func__, key_str, value);
if (0 > (res = libssh2_session_method_pref(session, type, value)) && res != LIBSSH2_ERROR_EAGAIN)
{
char *err;
const char **algs;
int rc;
if (LIBSSH2_ERROR_NONE != libssh2_session_last_error(session, &err, NULL, 0))
*err_msg = zbx_dsprintf(NULL, "Cannot set SSH option \"%s\": %s.", key_str, err);
else
*err_msg = zbx_dsprintf(NULL, "Cannot set SSH option \"%s\".", key_str);
if (0 < (rc = libssh2_session_supported_algs(session, type, &algs)))
{
*err_msg = zbx_strdcat(*err_msg, " Supported values are: ");
for (int i = 0; i < rc; i++)
{
*err_msg = zbx_strdcat(*err_msg, algs[i]);
if (i < rc - 1)
*err_msg = zbx_strdcat(*err_msg, ", ");
}
*err_msg = zbx_strdcat(*err_msg, ".");
libssh2_free(session, algs);
}
else
{
if (LIBSSH2_ERROR_NONE != libssh2_session_last_error(session, &err, NULL, 0))
*err_msg = zbx_strdcatf(*err_msg, " Cannot get supported values: %s.", err);
else
*err_msg = zbx_strdcat(*err_msg, " Cannot get supported values.");
}
ret = FAIL;
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
return ret;
}
#endif
static int ssh_parse_options(LIBSSH2_SESSION *session, const char *options, char **err_msg)
{
int ret = SUCCEED;
char opt_copy[1024] = {0};
char *saveptr;
zbx_strscpy(opt_copy, options);