diff --git a/pandora_agents/ChangeLog b/pandora_agents/ChangeLog index 90988c1562..0b0b0b6425 100644 --- a/pandora_agents/ChangeLog +++ b/pandora_agents/ChangeLog @@ -1,3 +1,8 @@ +2011-07-11 Vanessa Gil + + * win32/pandora_agent_conf.cc + win32/pandora_windows_service.cc: Allow drone agents in Windows. + 2011-07-08 Vanessa Gil * unix/pandora_agent: Allow drone agents in Unix. diff --git a/pandora_agents/win32/pandora_agent_conf.cc b/pandora_agents/win32/pandora_agent_conf.cc index c3413062c5..7f5db902ce 100644 --- a/pandora_agents/win32/pandora_agent_conf.cc +++ b/pandora_agents/win32/pandora_agent_conf.cc @@ -101,6 +101,145 @@ Pandora::Pandora_Agent_Conf::parseFile(string path_file, Collection *aux){ return; } +/** + * Create configuration file for drone agents. + * @param filename Configuration file to open. + * @param path_broker Configuration file to write. + */ +void +writeBrokerConf(string path_broker, string filename, string name_broker){ + ifstream file_conf (filename.c_str ()); + ofstream file_broker (path_broker.c_str ()); + string buffer; + string comp; + unsigned int pos; + + /* Read and set the file */ + while (!file_conf.eof ()) { + /* Set the value from each line */ + getline (file_conf, buffer); + + pos = buffer.find("agent_name"); + if (pos != string::npos){ + comp = buffer.substr (0,12); + if (comp == "# agent_name"){ + buffer = "agent_name "+name_broker+"\n"; + } + } + + pos = buffer.find("broker_agent"); + if (pos != string::npos){ + continue; + } else { + buffer = buffer + "\n"; + } + file_broker << buffer; + + } + file_conf.close (); + file_broker.close(); +} + +void +Pandora::Pandora_Agent_Conf::setFile (string *all_conf){ + string buffer, filename; + unsigned int pos; + Collection *aux; + + filename = Pandora::getPandoraInstallDir (); + filename += "pandora_agent.conf"; + + ifstream file (filename.c_str ()); + + if (this->key_values) + delete this->key_values; + this->key_values = new list (); + + if (this->collection_list) + delete this->collection_list; + this->collection_list = new list (); + + if (!file.is_open ()) { + return; + } + + /* Read and set the file */ + while (!file.eof ()) { + /* Set the value from each line */ + getline (file, buffer); + + /* Ignore blank or commented lines */ + if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') { + /*Check if is a include*/ + pos = buffer.find("include"); + if (pos != string::npos){ + string path_file; + unsigned pos_c; + + path_file = buffer.substr(pos+8); + + pos_c = path_file.find("\""); + /* Remove " */ + while (pos_c != string::npos){ + path_file.replace(pos_c, 1, ""); + pos_c = path_file.find("\"",pos_c+1); + } + parseFile(path_file, aux); + } + /*Check if is a broker_agent*/ + pos = buffer.find("broker_agent"); + if (pos != string::npos){ + string path_broker, name_broker; + unsigned pos_c; + int position = 0; + + name_broker = buffer.substr(pos+13); + path_broker = name_broker+".conf"; + + all_conf[position] = Pandora::getPandoraInstallDir () + path_broker; + position += 1; + + ifstream file_br (path_broker.c_str ()); + /* Check if already exists the configuration file*/ + if (!file_br){ + file_br.close(); + writeBrokerConf(path_broker, filename, name_broker); + } + } + + /*Check if is a collection*/ + pos = buffer.find("file_collection"); + if(pos != string::npos) { + string collection_name, trimmed_str; + + /*Add collection to collection_list*/ + /*The number 15 is the number of character of string file_collection*/ + collection_name = buffer.substr(pos+15); + + aux = new Collection(); + + aux->name = trim (collection_name); + + /*Check for ".." substring for security issues*/ + if ( collection_name.find("..") == string::npos ) { + aux->verify = 0; + collection_list->push_back (*aux); + } + continue; + } + /*Check if is a module*/ + pos = buffer.find ("module_"); + if (pos == string::npos) { + Key_Value kv; + + kv.parseLine (buffer); + key_values->push_back (kv); + } + } + } + file.close (); +} + /** * Sets configuration file to Pandora_Agent_Conf object instance. * @@ -154,6 +293,7 @@ Pandora::Pandora_Agent_Conf::setFile (string filename) { } parseFile(path_file, aux); } + /*Check if is a collection*/ pos = buffer.find("file_collection"); if(pos != string::npos) { diff --git a/pandora_agents/win32/pandora_agent_conf.h b/pandora_agents/win32/pandora_agent_conf.h index b3d449c749..957e028756 100644 --- a/pandora_agents/win32/pandora_agent_conf.h +++ b/pandora_agents/win32/pandora_agent_conf.h @@ -52,6 +52,7 @@ namespace Pandora { ~Pandora_Agent_Conf (); void parseFile(string path_file, Collection *aux); + void setFile (string *all_conf); void setFile (string filename); string getValue (const string key); diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 62933d1229..6e346c027d 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -37,6 +37,7 @@ #include #include #include +#include using namespace std; using namespace Pandora; @@ -113,13 +114,98 @@ Pandora_Windows_Service::start () { this->started = true; } +void +Pandora_Windows_Service::pandora_init_broker (string file_conf) { + string interval, debug, transfer_interval, util_dir, path, env; + string udp_server_enabled, udp_server_port, udp_server_addr, udp_server_auth_addr; + int pos; + + /*setPandoraDebug (true);*/ + + // Add the util subdirectory to the PATH + util_dir = Pandora::getPandoraInstallDir (); + util_dir += "util"; + path = getenv ("PATH"); + env = "PATH=" + path + ";" + util_dir; + putenv (env.c_str ()); + + this->conf = Pandora::Pandora_Agent_Conf::getInstance (); + this->conf->setFile (file_conf); + this->modules = new Pandora_Module_List (file_conf); + + pandoraLog ("Pandora agent started"); +} + +int +Pandora_Windows_Service::count_broker_agents(){ + string buffer; + string filename; + unsigned int pos; + int num = 0; + + filename = Pandora::getPandoraInstallDir (); + filename += "pandora_agent.conf"; + ifstream file (filename.c_str ()); + + /* Read and set the file */ + while (!file.eof ()) { + /* Set the value from each line */ + getline (file, buffer); + + /* Ignore blank or commented lines */ + if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') { + /*Check if is a broker_agent*/ + pos = buffer.find("broker_agent"); + if (pos != string::npos){ + num += 1; + } + } + } + file.close (); + return num; +} + +void +Pandora_Windows_Service::check_broker_agents(string *all_conf){ + string buffer, filename; + unsigned int pos; + int pos_file = 0; + + filename = Pandora::getPandoraInstallDir (); + filename += "pandora_agent.conf"; + + ifstream file (filename.c_str ()); + + while (!file.eof ()) { + /* Set the value from each line */ + getline (file, buffer); + + /* Ignore blank or commented lines */ + if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') { + /*Check if is a broker_agent*/ + pos = buffer.find("broker_agent"); + if (pos != string::npos){ + string name_broker, path_broker; + + name_broker = buffer.substr(pos+13); + path_broker = Pandora::getPandoraInstallDir () + name_broker + ".conf"; + all_conf[pos_file] = path_broker; + pos_file += 1; + } + } + } + file.close(); +} + + void Pandora_Windows_Service::pandora_init () { string conf_file, interval, debug, transfer_interval, util_dir, path, env; string udp_server_enabled, udp_server_port, udp_server_addr, udp_server_auth_addr; + int pos, num; setPandoraDebug (true); - + // Add the util subdirectory to the PATH util_dir = Pandora::getPandoraInstallDir (); util_dir += "util"; @@ -130,10 +216,13 @@ Pandora_Windows_Service::pandora_init () { conf_file = Pandora::getPandoraInstallDir (); conf_file += "pandora_agent.conf"; + num = count_broker_agents(); + string all_conf[num]; + this->conf = Pandora::Pandora_Agent_Conf::getInstance (); - this->conf->setFile (conf_file); + this->conf->setFile (all_conf); this->modules = new Pandora_Module_List (conf_file); - + /* Get the interval value (in seconds) and set it to the service */ interval = conf->getValue ("interval"); transfer_interval = conf->getValue ("transfer_interval"); @@ -891,11 +980,11 @@ Pandora_Windows_Service::checkCollections () { } void -Pandora_Windows_Service::checkConfig () { +Pandora_Windows_Service::checkConfig (string file) { int i, conf_size; char *conf_str = NULL, *remote_conf_str = NULL, *remote_conf_md5 = NULL; char agent_md5[33], conf_md5[33], flag; - string agent_name, conf_file, conf_tmp_file, md5_tmp_file, temp_dir, tmp; + string agent_name, conf_tmp_file, md5_tmp_file, temp_dir, tmp; tmp = conf->getValue ("remote_config"); if (tmp != "1") { @@ -909,10 +998,6 @@ Pandora_Windows_Service::checkConfig () { temp_dir += "\\"; } - /* Get base install directory */ - conf_file = Pandora::getPandoraInstallDir (); - conf_file += "pandora_agent.conf"; - /* Get agent name */ tmp = conf->getValue ("agent_name"); if (tmp.empty ()) { @@ -930,7 +1015,7 @@ Pandora_Windows_Service::checkConfig () { /* Calculate md5 hashes */ try { - conf_size = Pandora_File::readBinFile (conf_file, &conf_str); + conf_size = Pandora_File::readBinFile (file, &conf_str); Pandora_File::md5 (conf_str, conf_size, conf_md5); } catch (...) { pandoraDebug ("Pandora_Windows_Service::checkConfig: Error calculating configuration md5"); @@ -1017,7 +1102,7 @@ Pandora_Windows_Service::checkConfig () { conf_size = Pandora_File::readBinFile (tmp, &conf_str); Pandora_File::removeFile (tmp); /* Save new configuration */ - Pandora_File::writeBinFile (conf_file, conf_str, conf_size); + Pandora_File::writeBinFile (file, conf_str, conf_size); } catch (...) { pandoraDebug("Pandora_Windows_Service::checkConfig: Error retrieving configuration file from server"); if (conf_str != NULL) { @@ -1154,19 +1239,20 @@ Pandora_Windows_Service::sendBufferedXml (string path) { } void -Pandora_Windows_Service::pandora_run () { +Pandora_Windows_Service::pandora_run_broker (string config) { Pandora_Agent_Conf *conf = NULL; string server_addr; - int startup_delay = 0; - static unsigned char delayed = 0; - int exe = 1; + int startup_delay = 0; + static unsigned char delayed = 0; + int exe = 1; + int i; pandoraDebug ("Run begin"); - + conf = this->getConf (); /* Sleep if a startup delay was specified */ - startup_delay = atoi (conf->getValue ("startup_delay").c_str ()) * 1000; + startup_delay = atoi (conf->getValue ("startup_delay").c_str ()) * 1000; if (startup_delay > 0 && delayed == 0) { delayed = 1; pandoraLog ("Delaying startup %d miliseconds", startup_delay); @@ -1175,7 +1261,7 @@ Pandora_Windows_Service::pandora_run () { /* Check for configuration changes */ if (getPandoraDebug () == false) { - this->checkConfig (); + this->checkConfig (config); this->checkCollections (); } @@ -1196,7 +1282,71 @@ Pandora_Windows_Service::pandora_run () { if (exe == 0) return; pandoraDebug ("Run %s", module->getName ().c_str ()); + if (module->checkCron () == 1) { + module->run (); + Sleep(10); + } + + /* Save module data to an environment variable */ + if (!module->getSave().empty ()) { + module->exportDataOutput (); + } + /* Evaluate module conditions */ + module->evaluateConditions (); + + this->modules->goNext (); + } + } + return; +} + +void +Pandora_Windows_Service::pandora_run () { + Pandora_Agent_Conf *conf = NULL; + string server_addr, conf_file; + int startup_delay = 0; + static unsigned char delayed = 0; + int exe = 1; + int i, num; + + pandoraDebug ("Run begin"); + + conf = this->getConf (); + + /* Sleep if a startup delay was specified */ + startup_delay = atoi (conf->getValue ("startup_delay").c_str ()) * 1000; + if (startup_delay > 0 && delayed == 0) { + delayed = 1; + pandoraLog ("Delaying startup %d miliseconds", startup_delay); + Sleep (startup_delay); + } + + /* Check for configuration changes */ + if (getPandoraDebug () == false) { + conf_file = Pandora::getPandoraInstallDir (); + conf_file += "pandora_agent.conf"; + this->checkConfig (conf_file); + this->checkCollections (); + } + + server_addr = conf->getValue ("server_ip"); + + execution_number++; + + if (this->modules != NULL) { + this->modules->goFirst (); + + while (! this->modules->isLast ()) { + Pandora_Module *module; + + module = this->modules->getCurrentValue (); + + exe = module->evaluatePreconditions (); + + if (exe == 0) return; + + pandoraDebug ("Run %s", module->getName ().c_str ()); if (module->checkCron () == 1) { module->run (); Sleep(10); @@ -1214,7 +1364,6 @@ Pandora_Windows_Service::pandora_run () { } } - this->elapsed_transfer_time += this->interval; if (this->elapsed_transfer_time >= this->transfer_interval) { @@ -1227,6 +1376,16 @@ Pandora_Windows_Service::pandora_run () { /* Get the interval value (in minutes) */ pandoraDebug ("Next execution on %d seconds", this->interval / 1000); + num = count_broker_agents(); + string all_conf[num]; + + check_broker_agents(all_conf); + + for (i=0;i