diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index b211f64f43..25a5ccc289 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,30 @@ +2007-08-07 Esteban Sanchez + + * pandora.cc: Updated agent version. + + * pandora.h, modules/pandora_module_exec.cc: Redefine WINVER, so + CreateJobProject can be found without modifying windef.h. + + * pandora_agent_conf.cc: Rewritten a loop. + + * ftp/pandora_ftp_client.[cc,h]: Added to repository. Implemented FTP + transfer protocol using libcurl. It adds a library dependency. + + * pandora_windows_service.cc: Added support for FTP transfers. Select + between SSH (default) or FTP. Improved file structure to be cleaner. + + * pandora_windows_service.h: Added new private functions and removed + unneccessary private attribute. + + * misc/pandora_file.[cc,h]: Added a function to get the filename of a + path. + + * main.cc: Indentation fixed. + + * bin/libcurl.dll: Added to repository. New dependency. + + * bin/PandoraAgent.exe: Updated to last commit. + 2007-05-23 Raul Mateos * installer/Pandora_Windows_Agent-1.2.1-Setup.exe: Updated installer. diff --git a/pandora_agents/win32/PandoraAgent.dev b/pandora_agents/win32/PandoraAgent.dev index 07bc29c49c..d04d601137 100644 --- a/pandora_agents/win32/PandoraAgent.dev +++ b/pandora_agents/win32/PandoraAgent.dev @@ -1,7 +1,7 @@ [Project] FileName=PandoraAgent.dev Name=PandoraAgent -UnitCount=65 +UnitCount=67 Type=1 Ver=1 ObjFiles= @@ -12,7 +12,7 @@ ResourceIncludes= MakeIncludes= Compiler= CppCompiler= -Linker=-lole32_@@_-loleaut32_@@_-luuid_@@_-lpsapi_@@_-lwsock32_@@_-lz_@@_-liphlpapi_@@_-lnetapi32_@@_-lws2_32_@@_-lcrypto_@@_-lgdi32_@@__@@_ +Linker=-lole32_@@_-loleaut32_@@_-luuid_@@_-lpsapi_@@_-lwsock32_@@_-lz_@@_-liphlpapi_@@_-lnetapi32_@@_-lws2_32_@@_-lcrypto_@@_-lgdi32_@@_-lcurldll_@@_ IsCpp=1 Icon= ExeOutput= @@ -20,7 +20,7 @@ ObjectOutput= OverrideOutput=0 OverrideOutputName=PandoraAgent.exe HostApplication= -Folders=Misc,Modules,Modules/Utils,SSH,SSH/libssh2,Windows,Windows/WMI,XML +Folders=FTP,Misc,Modules,Modules/Utils,SSH,SSH/libssh2,Windows,Windows/WMI,XML CommandLine= UseCustomMakefile=0 CustomMakefile= @@ -697,3 +697,23 @@ Priority=1000 OverrideBuildCmd=0 BuildCmd= +[Unit66] +FileName=ftp\pandora_ftp_client.cc +CompileCpp=1 +Folder=FTP +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit67] +FileName=ftp\pandora_ftp_client.h +CompileCpp=1 +Folder=FTP +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/pandora_agents/win32/bin/PandoraAgent.exe b/pandora_agents/win32/bin/PandoraAgent.exe index 0c1af0cd6e..ab4872db21 100755 Binary files a/pandora_agents/win32/bin/PandoraAgent.exe and b/pandora_agents/win32/bin/PandoraAgent.exe differ diff --git a/pandora_agents/win32/bin/libcurl.dll b/pandora_agents/win32/bin/libcurl.dll new file mode 100644 index 0000000000..eb5792f436 Binary files /dev/null and b/pandora_agents/win32/bin/libcurl.dll differ diff --git a/pandora_agents/win32/ftp/pandora_ftp_client.cc b/pandora_agents/win32/ftp/pandora_ftp_client.cc new file mode 100644 index 0000000000..161d082fa5 --- /dev/null +++ b/pandora_agents/win32/ftp/pandora_ftp_client.cc @@ -0,0 +1,196 @@ +/* Class to abstract an FTP client. It uses libcurl. + + Copyright (C) 2006 Artica ST. + Written by Esteban Sanchez. + + 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#include +using namespace std; + +#include +#include +#include "pandora_ftp_client.h" +#include "../misc/pandora_file.h" +#include "../pandora_strutils.h" + +using namespace std; +using namespace FTP; +using namespace Pandora; + +/** + * Creates a FTP client object and initialize its attributes. + */ +Pandora_Ftp_Client::Pandora_Ftp_Client () +{ + curl = NULL; + + return; +} + +/** + * Destroy a FTP client object. + * + * It also disconnect the client from the host if connected. + * + * @see disconnect + */ +Pandora_Ftp_Client::~Pandora_Ftp_Client () +{ + this->disconnect (); + + return; +} + +/** + * Disconnects from remote host. + * + * It will close all open connections and channels. + */ +void +Pandora_Ftp_Client::disconnect () +{ + if (curl != NULL) { + curl_easy_cleanup (curl); + curl = NULL; + } +} + +/** + * Connects to specified host and port using a username and a + * password. + * + * @param host Host to connect to. + * @param port Port of FTP server in host + * @param username FTP username in server. + * @param password Username's password in server + */ +void +Pandora_Ftp_Client::connect (const string host, + const int port, + const string username, + const string password) +{ + this->username = username; + this->password = password; + this->host = host; +} + +size_t +read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return fread (ptr, size, nmemb, stream); +} + +/** + * Copy a file using a FTP connection. + * + * The function receives a filename in the local filesystem and copies all + * its content to the remote host. The remote filename will be the + * basename of the local file and will be copied in the remote actual + * directory. + * + * @param remote_filename Remote path to copy the local file in. + * @param filename Path to the local file. + */ +void +Pandora_Ftp_Client::ftpFileFilename (const string remote_filename, + const string filepath) +{ + FILE *fd; + string operation1; + string operation2; + struct stat file_info; + int file; + struct curl_slist *headerlist = NULL; + string filename; + string url; + + if (this->host == "") + throw Unknown_Host (); + + filename = Pandora_File::fileName (filepath); + + url = "ftp://"; + url += username; + url += ':'; + url += password; + url += '@'; + url += host; + url += '/'; + url += filename; + + file = open (filepath.c_str (), O_RDONLY); + fstat (file, &file_info); + close (file); + + fd = fopen (filepath.c_str (), "rb"); + + curl_global_init (CURL_GLOBAL_ALL); + + this->curl = curl_easy_init (); + if (this->curl) { + + pandoraLog ("Copying %s to %s%s", filepath.c_str (), this->host.c_str (), + remote_filename.c_str ()); + + operation1 = "RNFR " + filename; + headerlist = curl_slist_append (headerlist, operation1.c_str ()); + + operation2 = "RNTO " + remote_filename; + headerlist = curl_slist_append (headerlist, operation2.c_str ()); + + curl_easy_setopt (this->curl, CURLOPT_UPLOAD, 1) ; + curl_easy_setopt (this->curl, CURLOPT_URL, url.c_str ()); + curl_easy_setopt (this->curl, CURLOPT_POSTQUOTE, headerlist); + curl_easy_setopt (this->curl, CURLOPT_READFUNCTION, read_func); + curl_easy_setopt (this->curl, CURLOPT_READDATA, fd); + curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE, + (curl_off_t) file_info.st_size); + + this->result = curl_easy_perform (this->curl); + + curl_slist_free_all (headerlist); + curl_easy_cleanup (this->curl); + + this->curl = NULL; + } + + curl_global_cleanup (); + fclose (fd); + + switch (this->result) { + case CURLE_OK: + break; + case CURLE_COULDNT_CONNECT: + throw Unknown_Host (); + + break; + case CURLE_FTP_ACCESS_DENIED: + throw Authentication_Failed (); + + break; + default: + throw FTP_Exception (); + } +} + +string +Pandora_Ftp_Client::getError () +{ + string error (curl_easy_strerror (this->result)); + + return error; +} diff --git a/pandora_agents/win32/ftp/pandora_ftp_client.h b/pandora_agents/win32/ftp/pandora_ftp_client.h new file mode 100644 index 0000000000..728875d97e --- /dev/null +++ b/pandora_agents/win32/ftp/pandora_ftp_client.h @@ -0,0 +1,80 @@ +/* Class to abstract an FTP client. It uses libcurl. + + Copyright (C) 2006 Artica ST. + Written by Esteban Sanchez. + + 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef __PANDORA_FTP_CLIENT__ +#define __PANDORA_FTP_CLIENT__ + +#include +#include "../pandora.h" +#include + +using namespace std; + +/** + * FTP connection classes. + */ +namespace FTP { + /** + * A FTP super-class exception. + */ + class FTP_Exception : public Pandora::Pandora_Exception { + }; + + /** + * The FTP authentication fails when connecting. + */ + class Authentication_Failed : public FTP::FTP_Exception { + }; + + /** + * The FTP host is unknown. + */ + class Unknown_Host : public FTP::FTP_Exception { + }; + + /** + * Client to perform a FTP connection to a host. + */ + class Pandora_Ftp_Client { + private: + string host; + string username; + string password; + + CURL *curl; + CURLcode result; + public: + Pandora_Ftp_Client (); + ~Pandora_Ftp_Client (); + + void connect (const string host, + const int port, + const string username, + const string password); + + void disconnect (); + + void ftpFileFilename (const string remote_filename, + const string filepath); + + string getError (); + }; +} +#endif diff --git a/pandora_agents/win32/main.cc b/pandora_agents/win32/main.cc index 62e25838d9..aa3518d88d 100644 --- a/pandora_agents/win32/main.cc +++ b/pandora_agents/win32/main.cc @@ -30,51 +30,51 @@ int main (int argc, char *argv[]) { - Pandora_Windows_Service *service; - char buffer[PATH_SIZE]; - string aux; - unsigned int pos; - - service = new Pandora_Windows_Service (Pandora::name, Pandora::display_name, - Pandora::description); - - GetModuleFileName (NULL, buffer, MAX_PATH); - aux = buffer; - Pandora::setPandoraInstallPath (aux); - pos = aux.rfind ("\\"); - aux.erase (pos + 1); - Pandora::setPandoraInstallDir (aux); + Pandora_Windows_Service *service; + char buffer[PATH_SIZE]; + string aux; + unsigned int pos; + + service = new Pandora_Windows_Service (Pandora::name, Pandora::display_name, + Pandora::description); + + GetModuleFileName (NULL, buffer, MAX_PATH); + aux = buffer; + Pandora::setPandoraInstallPath (aux); + pos = aux.rfind ("\\"); + aux.erase (pos + 1); + Pandora::setPandoraInstallDir (aux); /* Check the parameters */ - for (int i = 1; i < argc; i++) { - if (_stricmp(argv[i], SERVICE_INSTALL_CMDLINE_PARAM) == 0) { + for (int i = 1; i < argc; i++) { + if (_stricmp(argv[i], SERVICE_INSTALL_CMDLINE_PARAM) == 0) { /* Install parameter */ - service->install (Pandora::getPandoraInstallPath ().c_str ()); - - delete service; - - return 0; - } else if (_stricmp(argv[i], SERVICE_UNINSTALL_CMDLINE_PARAM) == 0) { + service->install (Pandora::getPandoraInstallPath ().c_str ()); + + delete service; + + return 0; + } else if (_stricmp(argv[i], SERVICE_UNINSTALL_CMDLINE_PARAM) == 0) { /* Uninstall parameter */ - service->uninstall (); - - delete service; - - return 0; + service->uninstall (); + + delete service; + + return 0; } else if (_stricmp(argv[i], SSH_TEST_CMDLINE_PARAM) == 0) { /* SSH test parameter */ - SSH::Pandora_SSH_Test ssh_test; - - delete service; - - try { - ssh_test.test (); - } catch (Pandora_Exception e) { - return 1; - } - - return 0; - } else if (_stricmp(argv[i], HELP_CMDLINE_PARAM) == 0) { + SSH::Pandora_SSH_Test ssh_test; + + delete service; + + try { + ssh_test.test (); + } catch (Pandora_Exception e) { + return 1; + } + + return 0; + } else if (_stricmp(argv[i], HELP_CMDLINE_PARAM) == 0) { /* Help parameter */ cout << "Usage: " << argv[0] << " [OPTION]" << endl << endl; cout << "Available options are:" << endl; @@ -84,21 +84,21 @@ main (int argc, char *argv[]) { cout << ": Uninstall the Pandora Agent service." << endl; cout << "\t" << SSH_TEST_CMDLINE_PARAM; cout << ": Test the SSH Pandora Agent configuration." << endl; - - return 0; - } else { + + return 0; + } else { /* No parameter recognized */ - cout << "Usage: " << argv[0] << " [" << SERVICE_INSTALL_CMDLINE_PARAM; + cout << "Usage: " << argv[0] << " [" << SERVICE_INSTALL_CMDLINE_PARAM; cout << "] [" << SERVICE_UNINSTALL_CMDLINE_PARAM << "]" << endl; cout << "Run " << argv[0] << "with " << HELP_CMDLINE_PARAM; cout << " parameter for more info." << endl; - - return 1; - } - } - service->run (); - + + return 1; + } + } + service->run (); + delete service; - - return 0; + + return 0; } diff --git a/pandora_agents/win32/misc/pandora_file.cc b/pandora_agents/win32/misc/pandora_file.cc index 8c98a0b612..ea7997e3f9 100644 --- a/pandora_agents/win32/misc/pandora_file.cc +++ b/pandora_agents/win32/misc/pandora_file.cc @@ -20,6 +20,7 @@ #include "pandora_file.h" #include +#include #include #include @@ -106,3 +107,25 @@ Pandora_File::writeFile (const string filepath, const string data) { file.write (data.c_str (), data.length ()); file.close (); } + +/** + * Returns the filename of a complete filepath. + * + * @param filepath + */ +string +Pandora_File::fileName (const string filepath) +{ + string filename; + int pos; + + pos = filepath.find_last_of ("\\"); + + if (pos != string::npos) { + filename = filepath.substr (pos + 1); + } else { + filename = filepath; + } + + return filename; +} diff --git a/pandora_agents/win32/misc/pandora_file.h b/pandora_agents/win32/misc/pandora_file.h index 528ff1835a..8e6df82e47 100644 --- a/pandora_agents/win32/misc/pandora_file.h +++ b/pandora_agents/win32/misc/pandora_file.h @@ -54,6 +54,7 @@ namespace Pandora_File { string readFile (const string filename); void removeFile (const string filename); void writeFile (const string filename, const string data); + string fileName (const string filepath); } #endif diff --git a/pandora_agents/win32/modules/pandora_module_exec.cc b/pandora_agents/win32/modules/pandora_module_exec.cc index 4bfccaf10b..c3e0cbf2e2 100644 --- a/pandora_agents/win32/modules/pandora_module_exec.cc +++ b/pandora_agents/win32/modules/pandora_module_exec.cc @@ -64,7 +64,9 @@ Pandora_Module_Exec::run () { /* Create a job to kill the child tree if it become zombie */ /* CAUTION: In order to work this, WINVER should be defined to 0x0500. - It is defined in */ + This may need no change, since it was redefined by the + program, but if needed, the macro is defined + in */ job = CreateJobObject (&attributes, this->module_name.c_str ()); if (job == NULL) { pandoraLog ("CreateJobObject bad. Err: %d", GetLastError ()); diff --git a/pandora_agents/win32/pandora.cc b/pandora_agents/win32/pandora.cc index 851e4ffcb3..a25c2a5e0f 100644 --- a/pandora_agents/win32/pandora.cc +++ b/pandora_agents/win32/pandora.cc @@ -30,7 +30,7 @@ using namespace Pandora; using namespace Pandora_Strutils; #define PATH_SIZE _MAX_PATH+1 -#define PANDORA_VERSION ("1.2Beta") +#define PANDORA_VERSION ("1.3(Build 070807)") string pandora_path; string pandora_dir; @@ -46,33 +46,33 @@ string pandora_version = PANDORA_VERSION; */ void Key_Value::parseLine (string str) { - unsigned int pos; - list tokens; - list::iterator iter; - string trimmedstr; + unsigned int pos; + list tokens; + list::iterator iter; + string trimmedstr; - trimmedstr = trim (str); + trimmedstr = trim (str); - /* Check if the string has " */ - pos = trimmedstr.find ("\""); - if (pos == string::npos) { - stringtok (tokens, trimmedstr, " \t"); - } else { - stringtok (tokens, trimmedstr, "\""); - } +/* Check if the string has " */ + pos = trimmedstr.find ("\""); + if (pos == string::npos) { + stringtok (tokens, trimmedstr, " \t"); + } else { + stringtok (tokens, trimmedstr, "\""); + } - /* Pick the first and the last value of the token list */ - iter = tokens.begin (); - key = trim (*iter); - transform (key.begin(), key.end(), key.begin(), (int(*)(int)) tolower); - iter = tokens.end (); - iter--; - /* Check if the line has only one token */ - if (iter != tokens.begin ()) { - value = trim (*iter); - } else { - value = ""; - } +/* Pick the first and the last value of the token list */ + iter = tokens.begin (); + key = trim (*iter); + transform (key.begin(), key.end(), key.begin(), (int(*)(int)) tolower); + iter = tokens.end (); + iter--; +/* Check if the line has only one token */ + if (iter != tokens.begin ()) { + value = trim (*iter); + } else { + value = ""; + } } /** @@ -82,7 +82,7 @@ Key_Value::parseLine (string str) { */ string Key_Value::getKey () { - return key; + return key; } /** @@ -92,32 +92,32 @@ Key_Value::getKey () { */ string Key_Value::getValue () { - return value; + return value; } void pandoraWriteLog (string filename, string line) { - string buffer; - char str_time[25]; - FILE *file; - string filepath; - time_t now; - struct tm *gmtime; + string buffer; + char str_time[25]; + FILE *file; + string filepath; + time_t now; + struct tm *gmtime; - now = time (0); - gmtime = localtime (&now); - strftime (str_time, 25, "%m-%d-%y %H:%M:%S: ", gmtime); + now = time (0); + gmtime = localtime (&now); + strftime (str_time, 25, "%m-%d-%y %H:%M:%S: ", gmtime); - buffer = (char *) str_time; - buffer += line; + buffer = (char *) str_time; + buffer += line; - filepath = pandora_dir + filename; + filepath = pandora_dir + filename; - file = fopen (filepath.c_str (), "a+"); - if (file != NULL) { - fprintf (file, "%s\n", buffer.c_str ()); - fclose (file); - } + file = fopen (filepath.c_str (), "a+"); + if (file != NULL) { + fprintf (file, "%s\n", buffer.c_str ()); + fclose (file); + } } /** @@ -131,14 +131,14 @@ pandoraWriteLog (string filename, string line) { */ void Pandora::pandoraLog (char *format, ...) { - va_list args; - char msg[5000]; + va_list args; + char msg[5000]; - va_start (args, format); - vsprintf (msg, format, args); - va_end (args); + va_start (args, format); + vsprintf (msg, format, args); + va_end (args); - pandoraWriteLog ("pandora-log.log", (char *) msg); + pandoraWriteLog ("pandora-log.log", (char *) msg); } /** @@ -152,17 +152,17 @@ Pandora::pandoraLog (char *format, ...) { */ void Pandora::pandoraDebug (char *format, ...) { - if (pandora_debug) { - va_list args; - char msg[5000]; + if (pandora_debug) { + va_list args; + char msg[5000]; - va_start (args, format); - vsprintf (msg, format, args); - va_end (args); + va_start (args, format); + vsprintf (msg, format, args); + va_end (args); - pandoraWriteLog ("pandora-debug.dbg", (char *) msg); - } - return; + pandoraWriteLog ("pandora-debug.dbg", (char *) msg); + } + return; } /** @@ -172,9 +172,9 @@ Pandora::pandoraDebug (char *format, ...) { */ void Pandora::pandoraFree (void * pointer) { - if (pointer != NULL) - free (pointer); - return; + if (pointer != NULL) + free (pointer); + return; } /** @@ -189,7 +189,7 @@ Pandora::pandoraFree (void * pointer) { */ void Pandora::setPandoraInstallDir (string dir) { - pandora_dir = dir; + pandora_dir = dir; } /** @@ -204,7 +204,7 @@ Pandora::setPandoraInstallDir (string dir) { */ string Pandora::getPandoraInstallDir () { - return pandora_dir; + return pandora_dir; } /** @@ -218,7 +218,7 @@ Pandora::getPandoraInstallDir () { */ void Pandora::setPandoraInstallPath (string path) { - pandora_path = path; + pandora_path = path; } /** @@ -232,7 +232,7 @@ Pandora::setPandoraInstallPath (string path) { */ string Pandora::getPandoraInstallPath () { - return pandora_path; + return pandora_path; } /** @@ -247,7 +247,7 @@ Pandora::getPandoraInstallPath () { */ void Pandora::setPandoraDebug (bool dbg) { - pandora_debug = dbg; + pandora_debug = dbg; } /** @@ -271,5 +271,5 @@ Pandora::getPandoraDebug () { */ string Pandora::getPandoraAgentVersion () { - return pandora_version; + return pandora_version; } diff --git a/pandora_agents/win32/pandora.h b/pandora_agents/win32/pandora.h index 3143e77280..a34cdbe5e1 100644 --- a/pandora_agents/win32/pandora.h +++ b/pandora_agents/win32/pandora.h @@ -22,6 +22,9 @@ #include #include + +#undef WINVER +#define WINVER 0x0500 #include #include "windows_service.h" diff --git a/pandora_agents/win32/pandora_agent_conf.cc b/pandora_agents/win32/pandora_agent_conf.cc index 673ad0128c..58a4c44b84 100644 --- a/pandora_agents/win32/pandora_agent_conf.cc +++ b/pandora_agents/win32/pandora_agent_conf.cc @@ -87,14 +87,14 @@ Pandora::Pandora_Agent_Conf::~Pandora_Agent_Conf () { * If it could not be found then an empty string is returned. */ string -Pandora::Pandora_Agent_Conf::getValue (const string key) { - std::list::iterator i = key_values->begin (); +Pandora::Pandora_Agent_Conf::getValue (const string key) +{ + std::list::iterator i; - while (i != key_values->end ()) { + for (i = key_values->begin (); i != key_values->end (); i++) { if ((*i).getKey () == key) { return (*i).getValue (); } - i++; } return ""; diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index b04a5a74e5..28bf0dcb72 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -24,6 +24,7 @@ #include "windows_service.h" #include "modules/pandora_module_factory.h" #include "ssh/pandora_ssh_client.h" +#include "ftp/pandora_ftp_client.h" #include "misc/pandora_file.h" #include "windows/pandora_windows_info.h" @@ -99,6 +100,7 @@ Pandora_Windows_Service::pandora_init () { conf_file = Pandora::getPandoraInstallDir (); conf_file += "pandora_agent.conf"; + this->conf = new Pandora::Pandora_Agent_Conf (conf_file); this->modules = new Pandora_Module_List (conf_file); @@ -156,13 +158,14 @@ Pandora_Windows_Service::getXmlHeader () { return agent; } - void -Pandora_Windows_Service::copyDataFile (string filename) +Pandora_Windows_Service::copyScpDataFile (string host, + string remote_path, + string filename) { - string remote_host, remote_filepath; - string tmp_dir, filepath; - string pubkey_file, privkey_file; + SSH::Pandora_Ssh_Client ssh_client; + string tmp_dir, filepath; + string pubkey_file, privkey_file; tmp_dir = conf->getValue ("temporal"); if (tmp_dir[tmp_dir.length () - 1] != '\\') { @@ -170,69 +173,127 @@ Pandora_Windows_Service::copyDataFile (string filename) } filepath = tmp_dir + filename; - remote_host = conf->getValue ("server_ip"); - ssh_client = new SSH::Pandora_Ssh_Client (); - pandoraDebug ("Connecting with %s", remote_host.c_str ()); + pandoraDebug ("Connecting with %s", host.c_str ()); try { - pubkey_file = Pandora::getPandoraInstallDir (); - pubkey_file += "key\\id_dsa.pub"; - privkey_file = Pandora::getPandoraInstallDir (); + pubkey_file = Pandora::getPandoraInstallDir (); + pubkey_file += "key\\id_dsa.pub"; + privkey_file = Pandora::getPandoraInstallDir (); privkey_file += "key\\id_dsa"; - ssh_client->connectWithPublicKey (remote_host.c_str (), 22, "pandora", - pubkey_file, privkey_file, ""); + ssh_client.connectWithPublicKey (host.c_str (), 22, "pandora", + pubkey_file, privkey_file, ""); } catch (SSH::Authentication_Failed e) { - delete ssh_client; - pandoraLog ("Pandora Agent: Authentication Failed when connecting to %s", - remote_host.c_str ()); - if (getPandoraDebug () == false) { - try { - Pandora_File::removeFile (filepath); - } catch (Pandora_File::Delete_Error e) { - } - } - return; + pandoraLog ("Pandora Agent: Authentication Failed " + "when connecting to %s", + host.c_str ()); + throw e; } catch (Pandora_Exception e) { - delete ssh_client; pandoraLog ("Pandora Agent: Failed when copying to %s", - remote_host.c_str ()); + host.c_str ()); + throw e; + } + + pandoraDebug ("Remote copying XML %s on server %s at %s%s", + filepath.c_str (), host.c_str (), + remote_path.c_str (), filename.c_str ()); + try { + ssh_client.scpFileFilename (remote_path + filename, + filepath); + } catch (Pandora_Exception e) { + pandoraLog ("Unable to copy at %s%s", remote_path.c_str (), + filename.c_str ()); + ssh_client.disconnect(); + + throw e; + } + + ssh_client.disconnect(); +} + +void +Pandora_Windows_Service::copyFtpDataFile (string host, + string remote_path, + string filename) +{ + FTP::Pandora_Ftp_Client ftp_client; + string filepath; + string password; + + filepath = conf->getValue ("temporal"); + if (filepath[filepath.length () - 1] != '\\') { + filepath += "\\"; + } + filepath += filename; + + password = conf->getValue ("ftp_password"); + + ftp_client.connect (host, + 22, + "pandora", + password); + + try { + ftp_client.ftpFileFilename (remote_path + filename, + filepath); + } catch (FTP::Unknown_Host e) { + pandoraLog ("Failed when copying to %s (%s)", host.c_str (), + ftp_client.getError ().c_str ()); + throw e; + } catch (FTP::Authentication_Failed e) { + pandoraLog ("Pandora Agent: Authentication Failed " + "when connecting to %s (%s)", + host.c_str (), ftp_client.getError ().c_str ()); + throw e; + } catch (FTP::FTP_Exception e) { + pandoraLog ("Pandora Agent: Failed when copying to %s (%s)", + host.c_str (), ftp_client.getError ().c_str ()); + + throw e; + } + + ftp_client.disconnect (); +} + +void +Pandora_Windows_Service::copyDataFile (string filename) +{ + string mode, host, remote_path; + + mode = conf->getValue ("transfer_mode"); + host = conf->getValue ("server_ip"); + remote_path = conf->getValue ("server_path"); + if (remote_path[remote_path.length () - 1] != '/') { + remote_path += "/"; + } + + try { + if (mode == "ftp") { + copyFtpDataFile (host, remote_path, filename); + } else if (mode == "ssh" || mode == "") { + copyScpDataFile (host, remote_path, filename); + } else { + pandoraLog ("Invalid transfer mode: %s." + "Please rechak transfer_mode option " + "in configuration file."); + } + + pandoraLog ("Successfuly copied XML file to server."); + } catch (Pandora_Exception e) { if (getPandoraDebug () == false) { + string filepath; + + filepath = conf->getValue ("temporal"); + if (filepath[filepath.length () - 1] != '\\') { + filepath += "\\"; + } + filepath += filename; try { Pandora_File::removeFile (filepath); } catch (Pandora_File::Delete_Error e) { } } - return; - } - - remote_filepath = conf->getValue ("server_path"); - if (remote_filepath[remote_filepath.length () - 1] != '/') { - remote_filepath += "/"; - } - - pandoraDebug ("Remote copying XML %s on server %s at %s%s", - filepath.c_str (), remote_host.c_str (), - remote_filepath.c_str (), filename.c_str ()); - try { - ssh_client->scpFileFilename (remote_filepath + filename, - filepath); - } catch (Pandora_Exception e) { - pandoraLog ("Unable to copy at %s%s", remote_filepath.c_str (), - filename.c_str ()); - ssh_client->disconnect(); - delete ssh_client; - if (getPandoraDebug () == false) { - try { - Pandora_File::removeFile (filepath); - } catch (Pandora_File::Delete_Error e) { - } - } - return; - } - - ssh_client->disconnect(); - delete ssh_client; + } } void diff --git a/pandora_agents/win32/pandora_windows_service.h b/pandora_agents/win32/pandora_windows_service.h index aa3db3475f..0bbd4974d6 100644 --- a/pandora_agents/win32/pandora_windows_service.h +++ b/pandora_agents/win32/pandora_windows_service.h @@ -36,14 +36,19 @@ namespace Pandora { */ class Pandora_Windows_Service : public Windows_Service { private: - SSH::Pandora_Ssh_Client *ssh_client; Pandora_Agent_Conf *conf; Pandora_Modules::Pandora_Module_List *modules; long execution_number; string agent_name; - TiXmlElement *getXmlHeader (); - void copyDataFile (string filename); + TiXmlElement *getXmlHeader (); + void copyDataFile (string filename); + void copyScpDataFile (string host, + string remote_path, + string filename); + void copyFtpDataFile (string host, + string remote_path, + string filename); void pandora_run (); void pandora_init ();