From 9c2ad13d0388b0e2b53d76e946e33d5c501cac48 Mon Sep 17 00:00:00 2001 From: Tim Warren Date: Wed, 8 Apr 2015 11:06:24 -0400 Subject: [PATCH] Make SFTP class more C++ like --- src/common.h | 1 + src/network/SFTP.cpp | 45 ++++++++++++++++++++++++++++---------------- src/network/SFTP.h | 1 + tests/SFTPTest.cpp | 11 ++++++----- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/common.h b/src/common.h index 4e7b5df..e1f61fc 100644 --- a/src/common.h +++ b/src/common.h @@ -5,6 +5,7 @@ #define TYRO_COMMON_H #include +#include #include using namespace std; diff --git a/src/network/SFTP.cpp b/src/network/SFTP.cpp index 2f6d9ed..78b1710 100644 --- a/src/network/SFTP.cpp +++ b/src/network/SFTP.cpp @@ -16,8 +16,30 @@ SFTP::~SFTP() { freeaddrinfo(&host_info); }; +string SFTP::getFingerprint() +{ + // Get the host fingerprint to check against known hosts + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); + + cerr << "Fingerprint: "; + + for (int i = 0; i < 20; i++) + { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + cerr << endl; + + return (string)fingerprint; +} + string SFTP::getFile(const char *path) { + if ( ! fingerprint) + { + cerr << "Check fingerprint before grabbing files!" << endl; + return ""; + } + string output = ""; LIBSSH2_SFTP_HANDLE *sftp_handle; @@ -25,7 +47,8 @@ string SFTP::getFile(const char *path) if ( ! sftp_handle) { - fprintf(stderr, "Unable to open file with SFTP: %ld\n", libssh2_sftp_last_error(sftp_session)); + cerr << "Unable to open file with SFTP: "; + cerr << libssh2_sftp_last_error(sftp_session) << endl; return output; } @@ -60,7 +83,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con if (status != 0) { - cout << "getaddrinfo error" << gai_strerror(status) << endl; + cerr << "getaddrinfo error" << gai_strerror(status) << endl; } // Start libssh2 @@ -68,7 +91,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con if (rc != 0) { - cout << "Libssh2 initialization failed " << "(" << rc << ")" << endl; + cerr << "Libssh2 initialization failed " << "(" << rc << ")" << endl; return; } @@ -76,7 +99,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con if (connect(sock, host_info_list->ai_addr, host_info_list->ai_addrlen) != 0) { - cout << "Failed to connect to socket." << endl; + cerr << "Failed to connect to socket." << endl; return; } @@ -94,22 +117,12 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con return; } - // Get the host fingerprint to check against known hosts - fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); - - fprintf(stderr, "Fingerprint: "); - for (int i = 0; i < 20; i++) - { - fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); - } - fprintf(stderr, "\n"); - //@TODO do something with the handling of known hosts // Authenticate (by password) if (libssh2_userauth_password(session, user, pass)) { - fprintf(stderr, "Authentication by password failed.\n"); + cerr << "Authentication by password failed." << endl; return; } } @@ -120,7 +133,7 @@ void SFTP::sftp_connect() if ( ! sftp_session) { - fprintf(stderr, "Unable to start SFTP session \n"); + cerr << "Unable to start SFTP session" << endl; return; } } diff --git a/src/network/SFTP.h b/src/network/SFTP.h index 211eaf0..b5a17fc 100644 --- a/src/network/SFTP.h +++ b/src/network/SFTP.h @@ -24,6 +24,7 @@ class SFTP { public: SFTP(const char *host, const char *user, const char *pass, const char *port="22"); ~SFTP(); + string getFingerprint(); string getFile(const char *path); private: struct addrinfo host_info; diff --git a/tests/SFTPTest.cpp b/tests/SFTPTest.cpp index 988ff6f..63199e5 100644 --- a/tests/SFTPTest.cpp +++ b/tests/SFTPTest.cpp @@ -4,17 +4,18 @@ TEST_CASE("ssh connections work", "[SFTP]") { - SFTP *sftp = new SFTP("timshomepage.net", "test", "testpassword"); + SFTP *sftp = new SFTP("shell.xshellz.com", "twarren", "of0fmk2dsj"); + sftp->getFingerprint(); SECTION("sftp object is not null") { REQUIRE(sftp != NULL); } - /*SECTION("can retreive a file") { + SECTION("can retreive a file") { string file; - file = sftp->getFile("~/test.txt"); + file = sftp->getFile("test.txt"); REQUIRE(file != ""); - REQUIRE(file == "SFTP works!"); - }*/ + REQUIRE(file == "SFTP works!\n"); + } }