180 lines
4.9 KiB
C++
180 lines
4.9 KiB
C++
#ifndef _WIN32
|
|
#include "lib/utils/sftp.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/base64.hpp"
|
|
#include "lib/utils/defer.h"
|
|
|
|
#include <array>
|
|
#include <boost/filesystem.hpp>
|
|
#include <fmt/ostream.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
|
|
#include <libssh2.h>
|
|
#include <libssh2_sftp.h>
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
|
|
void begin_sftp_download(
|
|
const std::string host,
|
|
uint32_t port,
|
|
const std::string &user,
|
|
const std::string &password,
|
|
uint32_t /*time_out_secs*/,
|
|
const FilePicker &on_choose,
|
|
const std::function<void(DownloadResult const &)> on_done) {
|
|
|
|
utils::Dispatcher::background().post([=]() {
|
|
on_done(sftp_download(host, port, user, password, on_choose));
|
|
});
|
|
}
|
|
|
|
DownloadResult sftp_download(
|
|
const std::string host,
|
|
uint32_t port,
|
|
const std::string &user,
|
|
const std::string &password,
|
|
const FilePicker &picker) {
|
|
|
|
// resolve host
|
|
struct hostent *he;
|
|
he = gethostbyname((char *)host.c_str());
|
|
if (he == 0) {
|
|
return (DownloadFailure{"Hostname lookup failure"});
|
|
}
|
|
|
|
auto addr_list = (in_addr **)he->h_addr_list;
|
|
char ip[100];
|
|
strcpy(ip, inet_ntoa(*addr_list[0]));
|
|
|
|
in_addr addr;
|
|
if (inet_aton(ip, &addr) == 0) {
|
|
return (DownloadFailure{"Invalid address"});
|
|
}
|
|
|
|
// connect over sftp
|
|
auto rc = libssh2_init(0);
|
|
if (rc != 0) {
|
|
return (DownloadFailure{
|
|
fmt::format("Failed to initialize libssh2. rc {}", rc)});
|
|
}
|
|
DEFER(libssh2_exit());
|
|
auto sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
sockaddr_in sin;
|
|
sin.sin_family = AF_INET;
|
|
sin.sin_port = htons(port);
|
|
sin.sin_addr = addr;
|
|
rc = connect(sock, (sockaddr *)&sin, sizeof(sockaddr_in));
|
|
if (rc != 0) {
|
|
return (DownloadFailure{
|
|
fmt::format("Failed to connect to {}:{}. rc {}", host, port, rc)});
|
|
}
|
|
DEFER(close(sock));
|
|
|
|
auto session = libssh2_session_init();
|
|
if (!session) {
|
|
return (DownloadFailure{"Failed to init libssh2 session"});
|
|
}
|
|
DEFER(libssh2_session_disconnect(session, "Normal Shutdown");
|
|
libssh2_session_free(session));
|
|
|
|
libssh2_session_set_blocking(session, true);
|
|
rc = libssh2_session_handshake(session, sock);
|
|
if (rc != 0) {
|
|
return (DownloadFailure{fmt::format("ssh2 handshake failed. rc {}", rc)});
|
|
}
|
|
|
|
std::string fingerprint =
|
|
libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
|
fingerprint = websocketpp::base64_encode(fingerprint);
|
|
LOG(Gen, Info, "sftp session fingerprint for {}:{} = {}", host, port,
|
|
fingerprint);
|
|
|
|
// Authenticate
|
|
if (libssh2_userauth_password(session, user.c_str(), password.c_str())) {
|
|
return (DownloadFailure{fmt::format(
|
|
"Password authentiation failed for user {} password {}", user,
|
|
password)});
|
|
}
|
|
|
|
auto sftp_session = libssh2_sftp_init(session);
|
|
if (!sftp_session) {
|
|
return (DownloadFailure{"Failed to create sftp session"});
|
|
}
|
|
DEFER(libssh2_sftp_shutdown(sftp_session));
|
|
|
|
// get a directory listing
|
|
std::vector<std::string> candidates;
|
|
{
|
|
auto sftp_dir_handle = libssh2_sftp_opendir(sftp_session, ".");
|
|
if (!sftp_dir_handle) {
|
|
return (DownloadFailure{"Failed to open sftp directory '.'"});
|
|
}
|
|
DEFER(libssh2_sftp_closedir(sftp_dir_handle));
|
|
|
|
do {
|
|
char mem[1024];
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
|
rc = libssh2_sftp_readdir(sftp_dir_handle, mem, sizeof(mem), &attrs);
|
|
if (rc > 0) {
|
|
if (LIBSSH2_SFTP_S_ISREG(attrs.permissions)) {
|
|
candidates.push_back({mem, (size_t)rc});
|
|
}
|
|
} else
|
|
break;
|
|
} while (rc > 0);
|
|
}
|
|
|
|
auto to_download = picker(candidates);
|
|
if (!to_download.has_value()) {
|
|
return DownloadFailure{"No file selected"};
|
|
}
|
|
|
|
auto sftp_handle = libssh2_sftp_open(
|
|
sftp_session, to_download.value().c_str(), LIBSSH2_FXF_READ, 0);
|
|
if (!sftp_handle) {
|
|
return DownloadFailure{fmt::format(
|
|
"Unable to open file '{}' with sftp", to_download.value())};
|
|
}
|
|
DEFER(libssh2_sftp_close(sftp_handle));
|
|
|
|
// Get a temporary file
|
|
auto base_name =
|
|
fmt::format("bts-sftp-%%%%-%%%%-%%%%-%%%%-{}", to_download.value());
|
|
auto temp = boost::filesystem::temp_directory_path() /
|
|
boost::filesystem::unique_path(base_name);
|
|
if (temp.empty()) {
|
|
return (DownloadFailure{"Failed to create temporary file"});
|
|
}
|
|
|
|
std::ofstream out{temp.string()};
|
|
if (out.bad()) {
|
|
return DownloadFailure{
|
|
fmt::format("Failed to open temp file '{}'", temp)};
|
|
}
|
|
|
|
do {
|
|
char buffer[1024];
|
|
rc = libssh2_sftp_read(sftp_handle, buffer, sizeof(buffer));
|
|
if (rc > 0) {
|
|
out.write(buffer, rc);
|
|
} else
|
|
break;
|
|
} while (true);
|
|
|
|
return DownloadSuccess{to_download.value(), temp.string()};
|
|
}
|
|
|
|
} // namespace utils
|
|
} // namespace bts
|
|
#endif
|