From 772c378424e364895e3ac8e416eb33a06edd6556 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Tue, 15 Sep 2015 18:19:34 +0200 Subject: [PATCH] Added config_file_encoding token to change input module_exec Change name to module_native_encoding to change output module_exec --- .../win32/modules/pandora_module_exec.cc | 58 ++++++++++++++++--- .../win32/modules/pandora_module_exec.h | 4 +- .../win32/modules/pandora_module_factory.cc | 18 +++--- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/pandora_agents/win32/modules/pandora_module_exec.cc b/pandora_agents/win32/modules/pandora_module_exec.cc index ecac4e248e..aaacbf4f99 100644 --- a/pandora_agents/win32/modules/pandora_module_exec.cc +++ b/pandora_agents/win32/modules/pandora_module_exec.cc @@ -42,6 +42,7 @@ Pandora_Module_Exec::Pandora_Module_Exec (string name, string exec) this->proc = 0; this->setKind (module_exec_str); this->native_encoding = -1; + this->config_encoding = -1; } /** @@ -76,11 +77,13 @@ Pandora_Module_Exec::Pandora_Module_Exec (string name, string exec, string nativ pandoraDebug("module_native %s in %s module is not a properly encoding", native.c_str (), name.c_str ()); } - + } else { this->output_encoding = ""; this->native_encoding = -1; } + + getConfigFileEncoding(); } void @@ -148,8 +151,10 @@ Pandora_Module_Exec::run () { to find the GNU W32 tools */ working_dir = getPandoraInstallDir () + "util\\"; - /*always change input from UTF-8 to ANSI*/ - changeInputEncoding(); + /*change input encoding if config_file_encoding token is present*/ + if (this->config_encoding != -1){ + changeInputEncoding(); + } /* Create the child process. */ if (! CreateProcess (NULL, (CHAR *) this->module_exec.c_str (), NULL, @@ -406,7 +411,38 @@ UINT Pandora_Module_Exec::getNumberEncoding (string encoding){ void Pandora_Module_Exec::getOutputEncoding(){ - this->output_encoding = "ISO-8859-1"; //initialize with default encoding + string buffer, filename; + int pos; + filename = Pandora::getPandoraInstallDir (); + filename += "pandora_agent.conf"; + + ifstream file; + file.open (filename.c_str ()); + this->output_encoding = "ISO-8859-1"; //by default + bool token_found = false; + + while (!file.eof () && !token_found) { + /* Set the value from each line */ + getline (file, buffer); + + /* Ignore blank or commented lines */ + if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0' && buffer[0] != 'm') { + /*Check if is the encoding line*/ + pos = buffer.find("encoding"); + if (pos != string::npos){ + this->output_encoding = ""; + this->output_encoding = buffer.substr(pos+9); + token_found = true; + } + } + } + file.close(); +} + +void Pandora_Module_Exec::getConfigFileEncoding(){ + + string config_encoding_string; + config_encoding_string = ""; string buffer, filename; int pos; filename = Pandora::getPandoraInstallDir (); @@ -423,23 +459,27 @@ void Pandora_Module_Exec::getOutputEncoding(){ /* Ignore blank or commented lines */ if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') { /*Check if is the encoding line*/ - pos = buffer.find("encoding"); + pos = buffer.find("config_file_encoding"); if (pos != string::npos){ - this->output_encoding = ""; - this->output_encoding = buffer.substr(pos+9); + config_encoding_string = buffer.substr (pos+21); token_found = true; } } } + if (token_found) { + this->config_encoding = getNumberEncoding (config_encoding_string); + } else { + this->config_encoding = -1; + } file.close(); } void Pandora_Module_Exec::changeInputEncoding(){ - int size_wchar = MultiByteToWideChar( CP_UTF8 , 0 , this->module_exec.c_str () , -1, NULL , 0 ); + int size_wchar = MultiByteToWideChar( this->config_encoding , 0 , this->module_exec.c_str () , -1, NULL , 0 ); wchar_t* wstr = new wchar_t[size_wchar]; if (size_wchar != 0){ - MultiByteToWideChar( CP_UTF8 , 0 , this->module_exec.c_str () , -1, wstr , size_wchar ); + MultiByteToWideChar( this->config_encoding , 0 , this->module_exec.c_str () , -1, wstr , size_wchar ); char buf[BUFSIZE + 1]; wcstombs(buf, wstr, size_wchar); buf[size_wchar] = '\0'; diff --git a/pandora_agents/win32/modules/pandora_module_exec.h b/pandora_agents/win32/modules/pandora_module_exec.h index 1d7c3d08cc..b52332c310 100644 --- a/pandora_agents/win32/modules/pandora_module_exec.h +++ b/pandora_agents/win32/modules/pandora_module_exec.h @@ -33,13 +33,15 @@ namespace Pandora_Modules { */ class Pandora_Module_Exec : public Pandora_Module { private: - string module_exec; + string module_exec; UINT native_encoding; string output_encoding; UINT getNumberEncoding(string encoding); + UINT config_encoding; void getOutputEncoding(); void changeInputEncoding(); void changeOutputEncoding(string * string_change); + void getConfigFileEncoding(); public: unsigned char proc; Pandora_Module_Exec (string name, string exec); diff --git a/pandora_agents/win32/modules/pandora_module_factory.cc b/pandora_agents/win32/modules/pandora_module_factory.cc index 00f34fcd9e..a3143c6d09 100644 --- a/pandora_agents/win32/modules/pandora_module_factory.cc +++ b/pandora_agents/win32/modules/pandora_module_factory.cc @@ -118,7 +118,7 @@ using namespace Pandora_Strutils; #define TOKEN_QUIET ("module_quiet ") #define TOKEN_MODULE_FF_INTERVAL ("module_ff_interval ") #define TOKEN_MACRO ("module_macro") -#define TOKEN_NATIVE ("module_native") +#define TOKEN_NATIVE_ENCODING ("module_native_encoding") string parseLine (string line, string token) { @@ -171,7 +171,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { string module_unit, module_group, module_custom_id, module_str_warning, module_str_critical; string module_critical_instructions, module_warning_instructions, module_unknown_instructions, module_tags; string module_critical_inverse, module_warning_inverse, module_quiet, module_ff_interval; - string module_native; + string module_native_encoding; string macro; Pandora_Module *module; bool numeric; @@ -249,7 +249,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { module_warning_inverse = ""; module_quiet = ""; module_ff_interval = ""; - module_native = ""; + module_native_encoding = ""; macro = ""; stringtok (tokens, definition, "\n"); @@ -493,8 +493,8 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { module_quiet = parseLine (line, TOKEN_QUIET); } - if (module_native == "") { - module_native = parseLine (line, TOKEN_NATIVE); + if (module_native_encoding == "") { + module_native_encoding = parseLine (line, TOKEN_NATIVE_ENCODING); } if (module_ff_interval == "") { @@ -1056,10 +1056,10 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { } } - if (module_native != "") { - pos_macro = module_native.find(macro_name); + if (module_native_encoding != "") { + pos_macro = module_native_encoding.find(macro_name); if (pos_macro != string::npos){ - module_native.replace(pos_macro, macro_name.size(), macro_value); + module_native_encoding.replace(pos_macro, macro_name.size(), macro_value); } } @@ -1076,7 +1076,7 @@ Pandora_Module_Factory::getModuleFromDefinition (string definition) { /* Create module objects */ if (module_exec != "") { module = new Pandora_Module_Exec (module_name, - module_exec, module_native); + module_exec, module_native_encoding); if (module_timeout != "") { module->setTimeout (atoi (module_timeout.c_str ())); }