2009-01-20 Ramon Novoa <rnovoa@artica.es>

* win32/ftp/pandora_ftp_client.cc,
          win32/ftp/pandora_ftp_client.h,
          win32/windows/pandora_wmi.cc,
          win32/pandora_windows_service.h,
          win32/pandora.h,
          win32/udp_server/udp_server.cc,
          win32/misc/pandora_file.cc,
          win32/misc/pandora_file.h,
          win32/ssh/pandora_ssh_client.cc,
          win32/ssh/pandora_ssh_client.h,
          win32/pandora_windows_service.cc: Removed some exceptions that made
          the agent crash when the watchdog thread was running.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1371 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2009-01-20 22:21:31 +00:00
parent 845fe9c90c
commit c9d82e5b6c
12 changed files with 158 additions and 128 deletions

View File

@ -1,3 +1,18 @@
2009-01-20 Ramon Novoa <rnovoa@artica.es>
* win32/ftp/pandora_ftp_client.cc,
win32/ftp/pandora_ftp_client.h,
win32/windows/pandora_wmi.cc,
win32/pandora_windows_service.h,
win32/pandora.h,
win32/udp_server/udp_server.cc,
win32/misc/pandora_file.cc,
win32/misc/pandora_file.h,
win32/ssh/pandora_ssh_client.cc,
win32/ssh/pandora_ssh_client.h,
win32/pandora_windows_service.cc: Removed some exceptions that made
the agent crash when the watchdog thread was running.
2008-12-24 Ramon Novoa <rnovoa@artica.es>
* win32/pandora_windows_service.h,

View File

@ -105,7 +105,7 @@ read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
* @param remote_filename Remote path to copy the local file in.
* @param filename Path to the local file.
*/
void
int
Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
const string filepath)
{
@ -119,7 +119,7 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
string url;
if (this->host == "")
throw Unknown_Host ();
return UNKNOWN_HOST;
filename = Pandora_File::fileName (filepath);
@ -179,15 +179,15 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename,
Transfer was OK, moving wasn't. */
break;
case CURLE_COULDNT_CONNECT:
throw Unknown_Host ();
return UNKNOWN_HOST;
break;
case CURLE_FTP_ACCESS_DENIED:
throw Authentication_Failed ();
return AUTHENTICATION_FAILED;
break;
default:
throw FTP_Exception ();
return FTP_EXCEPTION;
}
}

View File

@ -71,7 +71,7 @@ namespace FTP {
void disconnect ();
void ftpFileFilename (const string remote_filename,
int ftpFileFilename (const string remote_filename,
const string filepath);
string getError ();

View File

@ -55,13 +55,13 @@ Pandora_File::fileExists (const string filepath) {
* @exception File_Not_Found throwed if the path is incorrect or the
* file does not exists or could not be opened.
**/
string
Pandora_File::readFile (const string filepath) {
string line, result;
int
Pandora_File::readFile (const string filepath, string &result) {
string line;
ifstream myfile (filepath.c_str ());
if (! myfile.is_open ()) {
throw File_Not_Found ();
return FILE_NOT_FOUND;
}
if (myfile.is_open()) {
@ -71,7 +71,7 @@ Pandora_File::readFile (const string filepath) {
}
myfile.close();
}
return result;
return 0;
}
/**
@ -126,10 +126,10 @@ Pandora_File::readBinFile (const string filepath, char **buffer) {
*
* @exception Delete_Error if the file could not be deleted.
*/
void
int
Pandora_File::removeFile (const string filepath) {
if (remove (filepath.c_str ()) == -1) {
throw Delete_Error ();
return DELETE_ERROR;
}
}

View File

@ -52,9 +52,9 @@ namespace Pandora_File {
};
bool fileExists (const string filename);
string readFile (const string filename);
int readFile (const string filepath, string &result);
int readBinFile (const string filepath, char **buffer);
void removeFile (const string filename);
int removeFile (const string filename);
void writeFile (const string filename, const string data);
void writeBinFile (const string filepath, const char *buffer, int size);

View File

@ -32,6 +32,19 @@ using namespace std;
#define PANDORA_DEBUG 1
#define PANDORA_EXCEPTION 11
#define AUTHENTICATION_FAILED 12
#define UNKNOWN_HOST 13
#define FTP_EXCEPTION 14
#define SESSION_ALREADY_OPENED 15
#define RESOLV_FAILED 16
#define CONNECTION_FAILED 17
#define SESSION_ERROR 18
#define SESSION_NOT_OPENED 19
#define FILE_NOT_FOUND 20
#define SCP_FAILED 21
#define DELETE_ERROR 22
/**
* Main application.
*/
@ -66,7 +79,7 @@ namespace Pandora {
void pandoraDebug (char *format, ...);
void pandoraLog (char *format, ...);
void pandoraFree (void * e);
bool is_enabled (string value);
/**
* Super-class exception.

View File

@ -199,7 +199,7 @@ Pandora_Windows_Service::getXmlHeader () {
return agent;
}
void
int
Pandora_Windows_Service::copyTentacleDataFile (string host,
string filename,
string port,
@ -207,7 +207,7 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
string pass,
string opts)
{
int rc;
int rc = 0;
string var, filepath;
string tentacle_cmd;
@ -250,7 +250,7 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
/* system() error */
case -1:
pandoraLog ("Unable to copy %s", filename.c_str ());
throw Pandora_Exception ();
break;
/* tentacle_client.exe returned OK */
case 0:
@ -260,17 +260,18 @@ Pandora_Windows_Service::copyTentacleDataFile (string host,
default:
pandoraDebug ("Tentacle client was unable to copy %s",
filename.c_str ());
throw Pandora_Exception ();
break;
}
return;
return rc;
}
void
int
Pandora_Windows_Service::copyScpDataFile (string host,
string remote_path,
string filename)
{
int rc = 0;
SSH::Pandora_Ssh_Client ssh_client;
string tmp_dir, filepath;
string pubkey_file, privkey_file;
@ -283,48 +284,48 @@ Pandora_Windows_Service::copyScpDataFile (string host,
pandoraDebug ("Connecting with %s", host.c_str ());
try {
pubkey_file = Pandora::getPandoraInstallDir ();
pubkey_file += "key\\id_dsa.pub";
privkey_file = Pandora::getPandoraInstallDir ();
privkey_file += "key\\id_dsa";
pubkey_file = Pandora::getPandoraInstallDir ();
pubkey_file += "key\\id_dsa.pub";
privkey_file = Pandora::getPandoraInstallDir ();
privkey_file += "key\\id_dsa";
ssh_client.connectWithPublicKey (host.c_str (), 22, "pandora",
rc = ssh_client.connectWithPublicKey (host.c_str (), 22, "pandora",
pubkey_file, privkey_file, "");
} catch (SSH::Authentication_Failed e) {
if (rc == AUTHENTICATION_FAILED) {
pandoraLog ("Pandora Agent: Authentication Failed "
"when connecting to %s",
host.c_str ());
throw e;
} catch (Pandora_Exception e) {
return rc;
} else if (rc == PANDORA_EXCEPTION) {
pandoraLog ("Pandora Agent: Failed when copying to %s",
host.c_str ());
throw e;
return rc;
}
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,
rc = ssh_client.scpFileFilename (remote_path + filename,
filepath);
} catch (Pandora_Exception e) {
if (rc = PANDORA_EXCEPTION) {
pandoraLog ("Unable to copy at %s%s", remote_path.c_str (),
filename.c_str ());
ssh_client.disconnect();
throw e;
return rc;
}
ssh_client.disconnect();
return rc;
}
void
int
Pandora_Windows_Service::copyFtpDataFile (string host,
string remote_path,
string filename,
string password)
{
int rc = 0;
FTP::Pandora_Ftp_Client ftp_client;
string filepath;
@ -339,33 +340,34 @@ Pandora_Windows_Service::copyFtpDataFile (string host,
"pandora",
password);
try {
ftp_client.ftpFileFilename (remote_path + filename,
rc = ftp_client.ftpFileFilename (remote_path + filename,
filepath);
} catch (FTP::Unknown_Host e) {
if (rc == UNKNOWN_HOST) {
pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
host.c_str (), ftp_client.getError ().c_str ());
ftp_client.disconnect ();
throw e;
} catch (FTP::Authentication_Failed e) {
return rc;
} else if (rc == AUTHENTICATION_FAILED) {
pandoraLog ("Pandora Agent: Authentication Failed "
"when connecting to %s (%s)",
host.c_str (), ftp_client.getError ().c_str ());
ftp_client.disconnect ();
throw e;
} catch (FTP::FTP_Exception e) {
return rc;
} else if (rc == FTP_EXCEPTION) {
pandoraLog ("Pandora Agent: Failed when copying to %s (%s)",
host.c_str (), ftp_client.getError ().c_str ());
ftp_client.disconnect ();
throw e;
return rc;
}
ftp_client.disconnect ();
return rc;
}
void
int
Pandora_Windows_Service::copyDataFile (string filename)
{
int rc = 0;
unsigned char copy_to_secondary = 0;
string mode, host, remote_path;
@ -379,28 +381,27 @@ Pandora_Windows_Service::copyDataFile (string filename)
remote_path += "\\";
}
try {
if (mode == "ftp") {
copyFtpDataFile (host, remote_path, filename, conf->getValue ("server_pwd"));
} else if (mode == "tentacle" || mode == "") {
copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
if (mode == "ftp") {
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("server_pwd"));
} else if (mode == "tentacle" || mode == "") {
rc = copyTentacleDataFile (host, filename, conf->getValue ("server_port"),
conf->getValue ("server_ssl"), conf->getValue ("server_pwd"),
conf->getValue ("server_opts"));
} else if (mode == "ssh") {
copyScpDataFile (host, remote_path, filename);
} else if (mode == "local") {
copyLocalDataFile (remote_path, filename);
} else {
pandoraLog ("Invalid transfer mode: %s."
"Please recheck transfer_mode option "
"in configuration file.");
}
} else if (mode == "ssh") {
rc =copyScpDataFile (host, remote_path, filename);
} else if (mode == "local") {
rc = copyLocalDataFile (remote_path, filename);
} else {
rc = PANDORA_EXCEPTION;
pandoraLog ("Invalid transfer mode: %s."
"Please recheck transfer_mode option "
"in configuration file.");
}
if (rc == 0) {
pandoraDebug ("Successfuly copied XML file to server.");
} catch (Pandora_Exception e) {
if (conf->getValue ("secondary_mode") == "on_error") {
copy_to_secondary = 1;
}
} else if (conf->getValue ("secondary_mode") == "on_error") {
copy_to_secondary = 1;
}
if (conf->getValue ("secondary_mode") == "always") {
@ -409,27 +410,29 @@ Pandora_Windows_Service::copyDataFile (string filename)
// Copy the file to the secondary server if needed
if (copy_to_secondary == 0) {
return;
return rc;
}
try {
if (mode == "ftp") {
copyFtpDataFile (host, remote_path, filename, conf->getValue ("secondary_server_pwd"));
} else if (mode == "tentacle") {
copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
if (mode == "ftp") {
rc = copyFtpDataFile (host, remote_path, filename, conf->getValue ("secondary_server_pwd"));
} else if (mode == "tentacle" || mode == "") {
rc = copyTentacleDataFile (host, filename, conf->getValue ("secondary_server_port"),
conf->getValue ("secondary_server_ssl"), conf->getValue ("secondary_server_pwd"),
conf->getValue ("secondary_server_opts"));
} else if (mode == "ssh" || mode == "") {
copyScpDataFile (host, remote_path, filename);
} else {
pandoraLog ("Invalid transfer mode: %s."
"Please recheck transfer_mode option "
"in configuration file.");
}
pandoraDebug ("Successfuly copied XML file to secondary server.");
} catch (Pandora_Exception e) {
} else if (mode == "ssh") {
rc = copyScpDataFile (host, remote_path, filename);
} else {
rc = PANDORA_EXCEPTION;
pandoraLog ("Invalid transfer mode: %s."
"Please recheck transfer_mode option "
"in configuration file.");
}
if (rc == 0) {
pandoraDebug ("Successfuly copied XML file to secondary server.");
}
return rc;
}
void
@ -523,7 +526,7 @@ Pandora_Windows_Service::recvDataFile (string filename) {
}
}
void
int
Pandora_Windows_Service::copyLocalDataFile (string remote_path,
string filename)
{
@ -536,7 +539,7 @@ Pandora_Windows_Service::copyLocalDataFile (string remote_path,
local_file = local_path + filename;
remote_file = remote_path + filename;
if (!CopyFile (local_file.c_str (), remote_file.c_str (), TRUE)) {
throw Pandora_Exception ();
return PANDORA_EXCEPTION;
}
}
@ -675,8 +678,9 @@ Pandora_Windows_Service::checkConfig () {
this->pandora_init ();
}
void
int
Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
int rc = 0;
TiXmlDeclaration *decl;
TiXmlDocument *doc;
TiXmlElement *local_xml, *agent;
@ -746,17 +750,13 @@ Pandora_Windows_Service::sendXml (Pandora_Module_List *modules) {
pandoraLog ("Error when saving the XML in %s",
tmp_filepath.c_str ());
ReleaseMutex (mutex);
return;
return PANDORA_EXCEPTION;
}
/* Only send if debug is not activated */
if (getPandoraDebug () == false) {
this->copyDataFile (tmp_filename);
try {
Pandora_File::removeFile (tmp_filepath);
} catch (Pandora_File::Delete_Error e) {
}
Pandora_File::removeFile (tmp_filepath);
}
ReleaseMutex (mutex);

View File

@ -47,21 +47,21 @@ namespace Pandora {
bool started;
TiXmlElement *getXmlHeader ();
void copyDataFile (string filename);
void copyTentacleDataFile (string host,
int copyDataFile (string filename);
int copyTentacleDataFile (string host,
string filename,
string port,
string ssl,
string pass,
string opts);
void copyScpDataFile (string host,
int copyScpDataFile (string host,
string remote_path,
string filename);
void copyFtpDataFile (string host,
int copyFtpDataFile (string host,
string remote_path,
string filename,
string password);
void copyLocalDataFile (string remote_path,
int copyLocalDataFile (string remote_path,
string filename);
void recvDataFile (string filename);
void recvTentacleDataFile (string host,
@ -82,7 +82,7 @@ namespace Pandora {
const char *svc_description);
void start ();
void sendXml (Pandora_Module_List *modules);
int sendXml (Pandora_Module_List *modules);
Pandora_Agent_Conf *getConf ();
};
}

View File

@ -117,7 +117,7 @@ Pandora_Ssh_Client::disconnect () {
}
}
void
int
Pandora_Ssh_Client::newConnection (const string host, const int port) {
struct sockaddr_in sin;
struct hostent *resolv = NULL;
@ -126,21 +126,21 @@ Pandora_Ssh_Client::newConnection (const string host, const int port) {
char char_aux[3];
if (session != NULL) {
throw Session_Already_Opened ();
return SESSION_ALREADY_OPENED;
}
WSAStartup (2, &wsadata);
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
throw Socket_Error ();
return SOCKET_ERROR;
}
resolv = (struct hostent *) gethostbyname (host.c_str ());
if (resolv == NULL) {
disconnect ();
throw Resolv_Failed ();
return RESOLV_FAILED;
}
sin.sin_family = AF_INET;
@ -150,13 +150,13 @@ Pandora_Ssh_Client::newConnection (const string host, const int port) {
if (connect (sock, (struct sockaddr*) (&sin),
sizeof (struct sockaddr_in)) == -1) {
disconnect ();
throw Connection_Failed (WSAGetLastError ());
return CONNECTION_FAILED;
}
session = libssh2_session_init();
if (libssh2_session_startup (session, sock) != 0) {
disconnect ();
throw Session_Error ();
return SESSION_ERROR;
}
/* Get the fingerprint and transform it to a hexadecimal readable
@ -170,6 +170,7 @@ Pandora_Ssh_Client::newConnection (const string host, const int port) {
}
fingerprint.erase (fingerprint.length () - 1, 2);
return 0;
}
/**
@ -188,14 +189,13 @@ Pandora_Ssh_Client::newConnection (const string host, const int port) {
* @exception Authentication_Failed throwed when the atuhentication could not
* be done.
*/
void
int
Pandora_Ssh_Client::connectWithPublicKey (const string host, const int port,
const string username, const string filename_pubkey,
const string filename_privkey, const string passphrase) {
try {
newConnection (host, port);
} catch (Session_Already_Opened e) {
}
int rc = 0;
newConnection (host, port);
if (session != NULL) {
if (libssh2_userauth_publickey_fromfile (session,
@ -204,10 +204,10 @@ Pandora_Ssh_Client::connectWithPublicKey (const string host, const int port,
filename_privkey.c_str (),
passphrase.c_str ())) {
disconnect ();
throw Authentication_Failed ();
return AUTHENTICATION_FAILED;
}
}
return;
return 0;
}
/**
@ -229,9 +229,10 @@ Pandora_Ssh_Client::connectWithPublicKey (const string host, const int port,
* @exception Scp_Failed Throwed if the scp operations failed when copying the
* file.
*/
void
int
Pandora_Ssh_Client::scpFileFilename (const string remote_filename,
const string filename) {
int rc = 0;
LIBSSH2_CHANNEL *scp_channel;
size_t to_send, sent;
char *errmsg;
@ -239,14 +240,14 @@ Pandora_Ssh_Client::scpFileFilename (const string remote_filename,
string buffer;
if (session == NULL) {
throw Session_Not_Opened ();
return SESSION_NOT_OPENED;
}
try {
buffer = Pandora_File::readFile (filename);
} catch (Pandora_File::File_Not_Found e) {
rc = Pandora_File::readFile (filename, buffer);
if (rc == FILE_NOT_FOUND) {
pandoraLog ("Pandora_Ssh_Client: File %s not found",
filename.c_str());
throw e;
return rc;
}
to_send = buffer.length ();
@ -263,23 +264,22 @@ Pandora_Ssh_Client::scpFileFilename (const string remote_filename,
sent = libssh2_channel_write (scp_channel, buffer.c_str (), to_send);
if (sent < 0) {
Scp_Failed *e;
errmsg = (char *) malloc (sizeof (char) * 1000);
libssh2_session_last_error (session, &errmsg, &errmsg_len, 1);
pandoraLog ("Error %d on SCP %s", sent, errmsg);
e = new Scp_Failed (errmsg);
Pandora::pandoraFree (errmsg);
libssh2_channel_close (scp_channel);
libssh2_channel_wait_closed (scp_channel);
libssh2_channel_free (scp_channel);
Pandora::pandoraFree (errmsg);
throw *e;
return SCP_FAILED;
}
libssh2_channel_send_eof (scp_channel);
libssh2_channel_close (scp_channel);
libssh2_channel_wait_closed (scp_channel);
libssh2_channel_free (scp_channel);
return 0;
}
/**

View File

@ -119,19 +119,19 @@ namespace SSH {
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;
void newConnection (const string host, const int port);
int newConnection (const string host, const int port);
public:
Pandora_Ssh_Client ();
~Pandora_Ssh_Client ();
void connectWithPublicKey (const string host, const int port,
int connectWithPublicKey (const string host, const int port,
const string username, const string filename_pubkey,
const string filename_privkey, const string passphrase);
void disconnect ();
void scpFileFilename (const string remote_filename,
int scpFileFilename (const string remote_filename,
const string filename);
string getFingerprint ();

View File

@ -233,8 +233,7 @@ int Pandora::process_command (Pandora_Windows_Service *service, char *command) {
pandoraLog ("UDP Server: Unauthorised access to process %s", target);
return 1;
}
system (value.c_str());
Pandora_Wmi::runProgram (value.c_str());
}
return 0;

View File

@ -603,7 +603,8 @@ bool
Pandora_Wmi::stopService (string service_name) {
SC_HANDLE manager, service;
bool success;
SERVICE_STATUS ssStatus;
manager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (manager == NULL) {
pandoraLog ("Could not access to service \"%s\" to stop.",
@ -619,7 +620,7 @@ Pandora_Wmi::stopService (string service_name) {
return false;
}
success = ControlService (service, SERVICE_CONTROL_STOP, NULL);
success = ControlService (service, SERVICE_CONTROL_STOP, &ssStatus);
CloseServiceHandle (service);
CloseServiceHandle (manager);
@ -659,7 +660,9 @@ Pandora_Wmi::runWMIQuery (string wmi_query, string column, list<string> &rows) {
FOR_EACH (quickfix, quickfixes, NULL) {
dhGetValue (L"%s", &value, quickfix,
column_w.c_str ());
rows.push_back (value);
if (value != NULL) {
rows.push_back (value);
}
dhFreeString (value);
} NEXT_THROW (quickfix);
} catch (string errstr) {