From cafe7ef8e55dc93471c4330cc7af09d8c4186bdc Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Fri, 12 Nov 2010 13:41:24 +0000 Subject: [PATCH] 2010-11-12 Ramon Novoa * windows/pandora_wmi.cc: isServiceRunning returns -1 for module states other than "Running" and "Stopped". * modules/pandora_module_logevent.cc, modules/pandora_module_logevent.h: Return a non empty string if the event description can not be read. Save the event id as an unsigned long int to avoid overflows. * modules/pandora_module_service.cc, modules/pandora_module_service.h: Aesthetic fix. Removed carriage returns. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3580 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_agents/win32/ChangeLog | 14 ++ .../win32/modules/pandora_module_logevent.cc | 12 +- .../win32/modules/pandora_module_logevent.h | 14 +- .../win32/modules/pandora_module_service.cc | 124 +++++++++--------- .../win32/modules/pandora_module_service.h | 84 ++++++------ pandora_agents/win32/windows/pandora_wmi.cc | 10 +- 6 files changed, 142 insertions(+), 116 deletions(-) diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index 6f08331d1d..8cdfd4dbc8 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,17 @@ +2010-11-12 Ramon Novoa + + * windows/pandora_wmi.cc: isServiceRunning returns -1 for module + states other than "Running" and "Stopped". + + * modules/pandora_module_logevent.cc, + modules/pandora_module_logevent.h: Return a non empty string if + the event description can not be read. Save the event id as an + unsigned long int to avoid overflows. + + * modules/pandora_module_service.cc, + modules/pandora_module_service.h: Aesthetic fix. Removed carriage + returns. + 2010-11-08 Ramon Novoa * modules/pandora_module_logevent.cc, diff --git a/pandora_agents/win32/modules/pandora_module_logevent.cc b/pandora_agents/win32/modules/pandora_module_logevent.cc index a8b8a3c720..cf76e72d89 100755 --- a/pandora_agents/win32/modules/pandora_module_logevent.cc +++ b/pandora_agents/win32/modules/pandora_module_logevent.cc @@ -59,7 +59,7 @@ Pandora_Module_Logevent::Pandora_Module_Logevent (string name, string source, st this->type = -1; } - this->id = atoi (id.c_str ()); + this->id = strtoul (id.c_str (), NULL, 0); this->source = source; this->pattern = pattern; this->application = application; @@ -224,7 +224,10 @@ Pandora_Module_Logevent::getLogEvents (list &event_list, unsigned char d // Retrieve the event description getEventDescription (pevlr, description); - + if (description == "") { + strcpy (description, "N/A"); + } + // Filter the event if (filterEvent (pevlr, description) == 0) { @@ -310,6 +313,7 @@ Pandora_Module_Logevent::getEventDescription (PEVENTLOGRECORD pevlr, char *messa module = LoadLibraryEx (exe_file_path, 0, DONT_RESOLVE_DLL_REFERENCES); if(module == NULL) { RegCloseKey(hk); + pandoraDebug("LoadLibraryEx error %d. Exe file path %s.", GetLastError(), exe_file_path); return; } @@ -331,8 +335,8 @@ Pandora_Module_Logevent::getEventDescription (PEVENTLOGRECORD pevlr, char *messa } } } - strcpy(strings[i], (TCHAR *)pevlr + offset); - offset += len + 1; + strcpy(strings[i], (TCHAR *)pevlr + offset); + offset += len + 1; } // Get the description diff --git a/pandora_agents/win32/modules/pandora_module_logevent.h b/pandora_agents/win32/modules/pandora_module_logevent.h index d9bc2c788a..2e4c97d066 100755 --- a/pandora_agents/win32/modules/pandora_module_logevent.h +++ b/pandora_agents/win32/modules/pandora_module_logevent.h @@ -39,13 +39,13 @@ namespace Pandora_Modules { class Pandora_Module_Logevent : public Pandora_Module { private: - int id; - int type; - string source; - string application; - string pattern; - HANDLE log_event; - HANDLE messages_dll; + unsigned long id; + int type; + string source; + string application; + string pattern; + HANDLE log_event; + HANDLE messages_dll; HANDLE openLogEvent (); void closeLogEvent (); diff --git a/pandora_agents/win32/modules/pandora_module_service.cc b/pandora_agents/win32/modules/pandora_module_service.cc index eb75646aa0..bbbe799f9e 100644 --- a/pandora_agents/win32/modules/pandora_module_service.cc +++ b/pandora_agents/win32/modules/pandora_module_service.cc @@ -1,54 +1,54 @@ -/* Pandora service module. These modules check if a service is running in the - system. - - 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. -*/ - +/* Pandora service module. These modules check if a service is running in the + system. + + 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 "pandora_module_service.h" -#include "pandora_module_list.h" -#include "../windows/pandora_wmi.h" +#include "pandora_module_list.h" +#include "../windows/pandora_wmi.h" #include "../pandora_strutils.h" -#include "../pandora_windows_service.h" -#include -#include - -using namespace Pandora; -using namespace Pandora_Modules; -using namespace Pandora_Strutils; - -/** - * Creates a Pandora_Module_Service object. - * - * @param name Module name. - * @param service_name Service internal name to check. - */ -Pandora_Module_Service::Pandora_Module_Service (string name, string service_name) - : Pandora_Module (name) { - - this->service_name = service_name; - - transform (service_name.begin (), service_name.end (), - this->service_name.begin (), (int (*) (int)) tolower); - +#include "../pandora_windows_service.h" +#include +#include + +using namespace Pandora; +using namespace Pandora_Modules; +using namespace Pandora_Strutils; + +/** + * Creates a Pandora_Module_Service object. + * + * @param name Module name. + * @param service_name Service internal name to check. + */ +Pandora_Module_Service::Pandora_Module_Service (string name, string service_name) + : Pandora_Module (name) { + + this->service_name = service_name; + + transform (service_name.begin (), service_name.end (), + this->service_name.begin (), (int (*) (int)) tolower); + this->setKind (module_service_str); this->thread = 0; - this->watchdog = false; -} + this->watchdog = false; +} string Pandora_Module_Service::getServiceName () const { @@ -130,7 +130,7 @@ async_run (Pandora_Module_Service *module) { if (service_event) { res = Pandora_Wmi::isServiceRunning (module->getServiceName ()); str_res = inttostr (res); - if (str_res != prev_res) { + if (str_res != prev_res) { module->setOutput (str_res); prev_res = str_res; Pandora_Windows_Service::getInstance ()->sendXml (modules); @@ -145,18 +145,18 @@ async_run (Pandora_Module_Service *module) { } delete modules; } - -void -Pandora_Module_Service::run () { - int res; - - try { - Pandora_Module::run (); - } catch (Interval_Not_Fulfilled e) { - return; - } - - res = Pandora_Wmi::isServiceRunning (this->service_name); + +void +Pandora_Module_Service::run () { + int res; + + try { + Pandora_Module::run (); + } catch (Interval_Not_Fulfilled e) { + return; + } + + res = Pandora_Wmi::isServiceRunning (this->service_name); this->setOutput (inttostr (res)); /* Launch thread if it's asynchronous */ @@ -165,5 +165,5 @@ Pandora_Module_Service::run () { (LPTHREAD_START_ROUTINE) async_run, this, 0, NULL); this->async = false; - } -} + } +} diff --git a/pandora_agents/win32/modules/pandora_module_service.h b/pandora_agents/win32/modules/pandora_module_service.h index 3ebf2fdf86..6e82697985 100644 --- a/pandora_agents/win32/modules/pandora_module_service.h +++ b/pandora_agents/win32/modules/pandora_module_service.h @@ -1,48 +1,48 @@ -/* Pandora service module. These modules check if a service is running in the - system. - - 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_MODULE_SERVICE_H__ -#define __PANDORA_MODULE_SERVICE_H__ - -#include "pandora_module.h" - -namespace Pandora_Modules { - /** - * Module to check that a windows service is running on the - * system. - */ - class Pandora_Module_Service : public Pandora_Module { - private: +/* Pandora service module. These modules check if a service is running in the + system. + + 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_MODULE_SERVICE_H__ +#define __PANDORA_MODULE_SERVICE_H__ + +#include "pandora_module.h" + +namespace Pandora_Modules { + /** + * Module to check that a windows service is running on the + * system. + */ + class Pandora_Module_Service : public Pandora_Module { + private: string service_name; HANDLE thread; - bool watchdog; - public: - Pandora_Module_Service (string name, string service_name); - + bool watchdog; + public: + Pandora_Module_Service (string name, string service_name); + void run (); string getServiceName () const; bool isWatchdog () const; - void setWatchdog (bool watchdog); - }; -} - -#endif + void setWatchdog (bool watchdog); + }; +} + +#endif diff --git a/pandora_agents/win32/windows/pandora_wmi.cc b/pandora_agents/win32/windows/pandora_wmi.cc index 9bc81af00e..7b04031edc 100644 --- a/pandora_agents/win32/windows/pandora_wmi.cc +++ b/pandora_agents/win32/windows/pandora_wmi.cc @@ -113,7 +113,15 @@ Pandora_Wmi::isServiceRunning (string service_name) { dhGetValue (L"%s", &state, quickfix, L".State"); str_state = state; - retval = (str_state == "Running") ? 1 : 0; + if (str_state == "Running") { + retval = 1; + } + else if (str_state == "Stopped") { + retval = 0; + } + else { + retval = -1; + } dhFreeString (state); return retval;