Blame src/ssh.cpp

Packit Service 21b5d1
// ***************************************************************** -*- C++ -*-
Packit Service 21b5d1
/*
Packit Service 21b5d1
 * Copyright (C) 2004-2018 Exiv2 authors
Packit Service 21b5d1
 * This program is part of the Exiv2 distribution.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * This program is free software; you can redistribute it and/or
Packit Service 21b5d1
 * modify it under the terms of the GNU General Public License
Packit Service 21b5d1
 * as published by the Free Software Foundation; either version 2
Packit Service 21b5d1
 * of the License, or (at your option) any later version.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * This program is distributed in the hope that it will be useful,
Packit Service 21b5d1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 21b5d1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 21b5d1
 * GNU General Public License for more details.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * You should have received a copy of the GNU General Public License
Packit Service 21b5d1
 * along with this program; if not, write to the Free Software
Packit Service 21b5d1
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
Packit Service 21b5d1
 */
Packit Service 21b5d1
/*
Packit Service 21b5d1
  File:      ssh.cpp
Packit Service 21b5d1
  Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
Packit Service 21b5d1
  History:   06-Jan-09, ahu: created
Packit Service 21b5d1
Packit Service 21b5d1
 */
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// included header files
Packit Service 21b5d1
#include "config.h"
Packit Service 21b5d1
#include "ssh.hpp"
Packit Service 21b5d1
Packit Service 21b5d1
#ifdef EXV_USE_SSH
Packit Service 21b5d1
// class member definitions
Packit Service 21b5d1
namespace Exiv2 {
Packit Service 21b5d1
Packit Service 21b5d1
    SSH::SSH(const std::string& host, const std::string& user, const std::string& pass, const std::string port):
Packit Service 21b5d1
        host_(host),user_(user),pass_(pass),sftp_(0) {
Packit Service 21b5d1
Packit Service 21b5d1
        std::string timeout = getEnv(envTIMEOUT);
Packit Service 21b5d1
        timeout_ = atol(timeout.c_str());
Packit Service 21b5d1
        if (timeout_ == 0) {
Packit Service 21b5d1
            throw Error(kerErrorMessage, "Timeout Environmental Variable must be a positive integer.");
Packit Service 21b5d1
        }
Packit Service 21b5d1
Packit Service 21b5d1
        session_ = ssh_new();
Packit Service 21b5d1
        if (session_ == NULL) {
Packit Service 21b5d1
            throw Error(kerErrorMessage, "Unable to create the the ssh session");
Packit Service 21b5d1
        }
Packit Service 21b5d1
Packit Service 21b5d1
        // try to connect
Packit Service 21b5d1
        ssh_options_set(session_, SSH_OPTIONS_HOST, host_.c_str());
Packit Service 21b5d1
        ssh_options_set(session_, SSH_OPTIONS_USER, user_.c_str());
Packit Service 21b5d1
        ssh_options_set(session_, SSH_OPTIONS_TIMEOUT, &timeout_);
Packit Service 21b5d1
        if (port != "") ssh_options_set(session_, SSH_OPTIONS_PORT_STR, port.c_str());
Packit Service 21b5d1
Packit Service 21b5d1
        if (ssh_connect(session_) != SSH_OK) {
Packit Service 21b5d1
            throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
        }
Packit Service 21b5d1
        // Authentication
Packit Service 21b5d1
        if (ssh_userauth_password(session_, NULL, pass_.c_str()) != SSH_AUTH_SUCCESS) {
Packit Service 21b5d1
            throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
        }
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    int SSH::runCommand(const std::string& cmd, std::string* output) {
Packit Service 21b5d1
        int rc;
Packit Service 21b5d1
        ssh_channel channel;
Packit Service 21b5d1
        channel = ssh_channel_new(session_);
Packit Service 21b5d1
        if (channel == NULL) {
Packit Service 21b5d1
           rc = SSH_ERROR;
Packit Service 21b5d1
        } else {
Packit Service 21b5d1
            rc = ssh_channel_open_session(channel);
Packit Service 21b5d1
            if (rc != SSH_OK) {
Packit Service 21b5d1
                ssh_channel_free(channel);
Packit Service 21b5d1
            } else {
Packit Service 21b5d1
                char buffer[256];
Packit Service 21b5d1
                rc = ssh_channel_request_exec(channel, cmd.c_str());
Packit Service 21b5d1
                if (rc == SSH_OK) {
Packit Service 21b5d1
                    while ((rc = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
Packit Service 21b5d1
                        output->append(buffer, rc);
Packit Service 21b5d1
                    }
Packit Service 21b5d1
                }
Packit Service 21b5d1
                ssh_channel_send_eof(channel);
Packit Service 21b5d1
                ssh_channel_close(channel);
Packit Service 21b5d1
                ssh_channel_free(channel);
Packit Service 21b5d1
            }
Packit Service 21b5d1
        }
Packit Service 21b5d1
        return rc;
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    int SSH::scp(const std::string& filePath, const byte* data, size_t size) {
Packit Service 21b5d1
        ssh_scp scp;
Packit Service 21b5d1
        int rc;
Packit Service 21b5d1
Packit Service 21b5d1
        size_t found = filePath.find_last_of("/\\");
Packit Service 21b5d1
        std::string filename = filePath.substr(found+1);
Packit Service 21b5d1
        std::string path = filePath.substr(0, found+1);
Packit Service 21b5d1
Packit Service 21b5d1
        scp = ssh_scp_new(session_, SSH_SCP_WRITE, path.c_str());
Packit Service 21b5d1
        if (scp == NULL) {
Packit Service 21b5d1
            rc = SSH_ERROR;
Packit Service 21b5d1
            throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
        } else {
Packit Service 21b5d1
            rc = ssh_scp_init(scp);
Packit Service 21b5d1
            if (rc != SSH_OK) {
Packit Service 21b5d1
                throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
            } else {
Packit Service 21b5d1
#ifdef  _MSC_VER
Packit Service 21b5d1
// S_IRUSR & S_IWUSR not in MSVC (0000400 & 0000200 in /usr/include/sys/stat.h on MacOS-X 10.8)
Packit Service 21b5d1
#define S_IRUSR S_IREAD
Packit Service 21b5d1
#define S_IWUSR S_IWRITE
Packit Service 21b5d1
#endif
Packit Service 21b5d1
                rc = ssh_scp_push_file (scp, filename.c_str(), size, S_IRUSR |  S_IWUSR);
Packit Service 21b5d1
                if (rc != SSH_OK) {
Packit Service 21b5d1
                    throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
                } else {
Packit Service 21b5d1
                    rc = ssh_scp_write(scp, data, size);
Packit Service 21b5d1
                    if (rc != SSH_OK) {
Packit Service 21b5d1
                        throw Error(kerErrorMessage, ssh_get_error(session_));
Packit Service 21b5d1
                    }
Packit Service 21b5d1
                }
Packit Service 21b5d1
                ssh_scp_close(scp);
Packit Service 21b5d1
            }
Packit Service 21b5d1
            ssh_scp_free(scp);
Packit Service 21b5d1
        }
Packit Service 21b5d1
Packit Service 21b5d1
        return rc;
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void SSH::openSftp() {
Packit Service 21b5d1
        if (sftp_) return;
Packit Service 21b5d1
Packit Service 21b5d1
        sftp_ = sftp_new(session_);
Packit Service 21b5d1
        if (sftp_ == NULL) {
Packit Service 21b5d1
            throw Error(kerErrorMessage, "Unable to create the the sftp session");
Packit Service 21b5d1
        }
Packit Service 21b5d1
        if (sftp_init(sftp_) != SSH_OK) {
Packit Service 21b5d1
            sftp_free(sftp_);
Packit Service 21b5d1
            throw Error(kerErrorMessage, "Error initializing SFTP session");
Packit Service 21b5d1
        }
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void SSH::getFileSftp(const std::string& filePath, sftp_file& handle) {
Packit Service 21b5d1
        if (!sftp_) openSftp();
Packit Service 21b5d1
        handle = sftp_open(sftp_, ("/"+filePath).c_str(), 0x0000, 0); // read only
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    SSH::~SSH() {
Packit Service 21b5d1
        if (sftp_) sftp_free(sftp_);
Packit Service 21b5d1
        ssh_disconnect(session_);
Packit Service 21b5d1
        ssh_free(session_);
Packit Service 21b5d1
    }
Packit Service 21b5d1
}
Packit Service 21b5d1
#endif // EXV_USE_SSH == 1