Remove deprecated SSH support
parent
384a5edfcf
commit
94b87a8dec
@ -1,109 +0,0 @@
|
||||
// ***************************************************************** -*- C++ -*-
|
||||
/*
|
||||
* Copyright (C) 2004-2021 Exiv2 authors
|
||||
* This program is part of the Exiv2 distribution.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef SSH_HPP_
|
||||
#define SSH_HPP_
|
||||
|
||||
// included header files
|
||||
#include "config.h"
|
||||
|
||||
#ifdef EXV_USE_SSH
|
||||
#include <libssh/libssh.h>
|
||||
#include <libssh/sftp.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string>
|
||||
|
||||
#include "error.hpp"
|
||||
#include "types.hpp"
|
||||
#include "futils.hpp"
|
||||
|
||||
namespace Exiv2 {
|
||||
/*!
|
||||
@brief The class provides the high-level functions related to libssh.
|
||||
It makes the libssh transparent. The functions in this class can
|
||||
be used without the requirement of understanding libssh.
|
||||
*/
|
||||
class EXIV2LIB_DEPRECATED_EXPORT SSH {
|
||||
public:
|
||||
//! @name Creators
|
||||
//@{
|
||||
/*!
|
||||
@brief Constructor to set up the connection to ssh server.
|
||||
@param host The host name of ssh server.
|
||||
@param user The username used to connect to ssh server.
|
||||
@param pass The password used to connect to ssh server.
|
||||
@param port The port to connect to ssh server. Set empty string to use the default port.
|
||||
@throw Error if it fails to connect the server.
|
||||
*/
|
||||
SSH (const std::string& host, const std::string& user, const std::string& pass, const std::string port = "");
|
||||
//! Destructor
|
||||
~SSH();
|
||||
//@}
|
||||
|
||||
//! @name Manipulators
|
||||
//@{
|
||||
/*!
|
||||
@brief Run the command on the remote machine.
|
||||
@param cmd The command
|
||||
@param output The container for the command's output
|
||||
@return 0 (SSH_OK) if there is no error.
|
||||
*/
|
||||
int runCommand(const std::string& cmd, std::string* output);
|
||||
/*!
|
||||
@brief SCP data to the remote machine.
|
||||
@param filePath The path of the new file on the remote machine where the data is saved.
|
||||
@param data The data copied to the remote machine.
|
||||
@param size The size of the data.
|
||||
@return 0 (SSH_OK) if there is no error.
|
||||
@throw Error if it is unable to copy the data.
|
||||
*/
|
||||
int scp(const std::string& filePath, const byte* data, size_t size);
|
||||
/*!
|
||||
@brief Return the sftp file handle of the file on the remote machine to read the data.
|
||||
@param filePath The path of the file on the remote machine.
|
||||
@param handle The container for the file handle.
|
||||
@throw Error if it is unable to get the sftp file handle.
|
||||
|
||||
@note Be sure to close() the file handle after use.
|
||||
*/
|
||||
void getFileSftp(const std::string& filePath, sftp_file& handle);
|
||||
//@}
|
||||
private:
|
||||
/*!
|
||||
@brief Open the sftp session.
|
||||
*/
|
||||
void openSftp();
|
||||
// DATA
|
||||
//! The number of seconds to wait while trying to connect.
|
||||
long timeout_;
|
||||
//! the ssh server host
|
||||
std::string host_;
|
||||
//! the username
|
||||
std::string user_;
|
||||
//! the password
|
||||
std::string pass_;
|
||||
//! the ssh session
|
||||
ssh_session session_;
|
||||
//! the sftp session
|
||||
sftp_session sftp_;
|
||||
}; // class SSH
|
||||
} // namespace Exiv2
|
||||
#endif
|
||||
|
||||
#endif // #ifdef EXIV2_HPP_
|
@ -1,147 +0,0 @@
|
||||
// ***************************************************************** -*- C++ -*-
|
||||
/*
|
||||
* Copyright (C) 2004-2021 Exiv2 authors
|
||||
* This program is part of the Exiv2 distribution.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
// *****************************************************************************
|
||||
// included header files
|
||||
#include "config.h"
|
||||
#include "ssh.hpp"
|
||||
|
||||
#ifdef EXV_USE_SSH
|
||||
// class member definitions
|
||||
namespace Exiv2 {
|
||||
|
||||
SSH::SSH(const std::string& host, const std::string& user, const std::string& pass, const std::string port):
|
||||
host_(host),user_(user),pass_(pass),sftp_(0) {
|
||||
|
||||
std::string timeout = getEnv(envTIMEOUT);
|
||||
timeout_ = atol(timeout.c_str());
|
||||
if (timeout_ == 0) {
|
||||
throw Error(kerErrorMessage, "Timeout Environmental Variable must be a positive integer.");
|
||||
}
|
||||
|
||||
session_ = ssh_new();
|
||||
if (session_ == NULL) {
|
||||
throw Error(kerErrorMessage, "Unable to create the the ssh session");
|
||||
}
|
||||
|
||||
// try to connect
|
||||
ssh_options_set(session_, SSH_OPTIONS_HOST, host_.c_str());
|
||||
ssh_options_set(session_, SSH_OPTIONS_USER, user_.c_str());
|
||||
ssh_options_set(session_, SSH_OPTIONS_TIMEOUT, &timeout_);
|
||||
if (port != "") ssh_options_set(session_, SSH_OPTIONS_PORT_STR, port.c_str());
|
||||
|
||||
if (ssh_connect(session_) != SSH_OK) {
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
}
|
||||
// Authentication
|
||||
if (ssh_userauth_password(session_, NULL, pass_.c_str()) != SSH_AUTH_SUCCESS) {
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
}
|
||||
}
|
||||
|
||||
int SSH::runCommand(const std::string& cmd, std::string* output) {
|
||||
int rc;
|
||||
ssh_channel channel;
|
||||
channel = ssh_channel_new(session_);
|
||||
if (channel == NULL) {
|
||||
rc = SSH_ERROR;
|
||||
} else {
|
||||
rc = ssh_channel_open_session(channel);
|
||||
if (rc != SSH_OK) {
|
||||
ssh_channel_free(channel);
|
||||
} else {
|
||||
char buffer[256];
|
||||
rc = ssh_channel_request_exec(channel, cmd.c_str());
|
||||
if (rc == SSH_OK) {
|
||||
while ((rc = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
|
||||
output->append(buffer, rc);
|
||||
}
|
||||
}
|
||||
ssh_channel_send_eof(channel);
|
||||
ssh_channel_close(channel);
|
||||
ssh_channel_free(channel);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int SSH::scp(const std::string& filePath, const byte* data, size_t size) {
|
||||
ssh_scp scp;
|
||||
int rc;
|
||||
|
||||
size_t found = filePath.find_last_of("/\\");
|
||||
std::string filename = filePath.substr(found+1);
|
||||
std::string path = filePath.substr(0, found+1);
|
||||
|
||||
scp = ssh_scp_new(session_, SSH_SCP_WRITE, path.c_str());
|
||||
if (scp == NULL) {
|
||||
rc = SSH_ERROR;
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
} else {
|
||||
rc = ssh_scp_init(scp);
|
||||
if (rc != SSH_OK) {
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
} else {
|
||||
#ifdef _MSC_VER
|
||||
// S_IRUSR & S_IWUSR not in MSVC (0000400 & 0000200 in /usr/include/sys/stat.h on MacOS-X 10.8)
|
||||
#define S_IRUSR S_IREAD
|
||||
#define S_IWUSR S_IWRITE
|
||||
#endif
|
||||
rc = ssh_scp_push_file (scp, filename.c_str(), size, S_IRUSR | S_IWUSR);
|
||||
if (rc != SSH_OK) {
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
} else {
|
||||
rc = ssh_scp_write(scp, data, size);
|
||||
if (rc != SSH_OK) {
|
||||
throw Error(kerErrorMessage, ssh_get_error(session_));
|
||||
}
|
||||
}
|
||||
ssh_scp_close(scp);
|
||||
}
|
||||
ssh_scp_free(scp);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void SSH::openSftp() {
|
||||
if (sftp_) return;
|
||||
|
||||
sftp_ = sftp_new(session_);
|
||||
if (sftp_ == NULL) {
|
||||
throw Error(kerErrorMessage, "Unable to create the the sftp session");
|
||||
}
|
||||
if (sftp_init(sftp_) != SSH_OK) {
|
||||
sftp_free(sftp_);
|
||||
throw Error(kerErrorMessage, "Error initializing SFTP session");
|
||||
}
|
||||
}
|
||||
|
||||
void SSH::getFileSftp(const std::string& filePath, sftp_file& handle) {
|
||||
if (!sftp_) openSftp();
|
||||
handle = sftp_open(sftp_, ("/"+filePath).c_str(), 0x0000, 0); // read only
|
||||
}
|
||||
|
||||
SSH::~SSH() {
|
||||
if (sftp_) sftp_free(sftp_);
|
||||
ssh_disconnect(session_);
|
||||
ssh_free(session_);
|
||||
}
|
||||
}
|
||||
#endif // EXV_USE_SSH == 1
|
Loading…
Reference in New Issue