From af5398ede57a1a3664068ef92adc523ae7872520 Mon Sep 17 00:00:00 2001
From: vgilc <noreply@pandorafms.org>
Date: Wed, 8 Jun 2011 14:12:57 +0000
Subject: [PATCH] 2011-06-07  Vanessa Gil  <vanessa.gil@artica.es>

	* win32/pandora_agent_conf.cc
	  win32/pandora_agent_conf.h
	  win32/modules/pandora_module_list.cc
	  win32/modules/pandora_module_list.h: Allow the windows agent to include additional configuration files.


git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4419 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_agents/ChangeLog                      |  7 ++
 .../win32/modules/pandora_module_list.cc      | 69 +++++++++++++++++++
 .../win32/modules/pandora_module_list.h       |  9 +--
 pandora_agents/win32/pandora_agent_conf.cc    | 66 +++++++++++++++++-
 pandora_agents/win32/pandora_agent_conf.h     |  1 +
 5 files changed, 147 insertions(+), 5 deletions(-)

diff --git a/pandora_agents/ChangeLog b/pandora_agents/ChangeLog
index efa7b6e209..5868403054 100644
--- a/pandora_agents/ChangeLog
+++ b/pandora_agents/ChangeLog
@@ -1,3 +1,10 @@
+2011-06-07  Vanessa Gil  <vanessa.gil@artica.es>
+
+	* win32/pandora_agent_conf.cc
+	  win32/pandora_agent_conf.h
+	  win32/modules/pandora_module_list.cc
+	  win32/modules/pandora_module_list.h: Allow the windows agent to include additional configuration files.
+
 2011-06-01  Vanessa Gil  <vanessa.gil@artica.es>
 
 	* unix/pandora_agent: Allow the agent to include additional configuration files.
diff --git a/pandora_agents/win32/modules/pandora_module_list.cc b/pandora_agents/win32/modules/pandora_module_list.cc
index 2891502adb..456db06d0e 100644
--- a/pandora_agents/win32/modules/pandora_module_list.cc
+++ b/pandora_agents/win32/modules/pandora_module_list.cc
@@ -40,6 +40,58 @@
 
 using namespace std;
 
+/**
+ * Additional configuration file.
+ */
+void
+Pandora_Modules::Pandora_Module_List::parseModuleConf (string path_file, list<Pandora_Module *> *modules) {
+	ifstream     file_conf (path_file.c_str ());
+	string       buffer;
+	unsigned int pos;
+	
+	if (!file_conf.is_open ()) {
+		return;
+	}
+	
+	/* Read and set the file */
+	while (!file_conf.eof ()) {
+		/* Set the value from each line */
+		getline (file_conf, buffer);
+		
+		/* Ignore blank or commented lines */
+		if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') {
+			/* Module */
+			pos = buffer.find ("module_begin");  
+			if (pos != string::npos) {
+				string str_module = buffer + "\n";
+				bool   module_end = false;
+				
+				while (!module_end) {
+					if (file_conf.eof ()) {
+						break;
+					}
+					getline (file_conf, buffer);
+					pos = buffer.find ("module_end");
+					module_end = (pos != string::npos);
+					str_module += buffer + "\n";
+				}
+				
+				this->parseModuleDefinition (str_module);
+				continue;
+			}
+			
+			/* Plugin */
+			pos = buffer.find ("module_plugin");  
+			if (pos != string::npos) {
+				this->parseModuleDefinition (buffer);
+				continue;
+			}
+			
+		}
+	}
+	file_conf.close ();
+	return;
+}
 /** 
  * Read and set a key-value set from a file.
  *
@@ -66,6 +118,23 @@ Pandora_Modules::Pandora_Module_List::Pandora_Module_List (string filename) {
 		
 		/* 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);
+					}
+
+					parseModuleConf(path_file, modules);
+			}
 			
 			/* Module */
 			pos = buffer.find ("module_begin");  
diff --git a/pandora_agents/win32/modules/pandora_module_list.h b/pandora_agents/win32/modules/pandora_module_list.h
index 68c92ebdc8..75fd94a36c 100644
--- a/pandora_agents/win32/modules/pandora_module_list.h
+++ b/pandora_agents/win32/modules/pandora_module_list.h
@@ -40,17 +40,18 @@ namespace Pandora_Modules {
 	private:
 		list<Pandora_Module *>           *modules;
 		list<Pandora_Module *>::iterator *current;
+		void		 parseModuleConf (string path_file, list<Pandora_Module *> *modules);
 		void             parseModuleDefinition (string definition);
 	public:
-		Pandora_Module_List                    (string filename);
-		Pandora_Module_List                    ();
+		Pandora_Module_List                    (string filename);
+		Pandora_Module_List                    ();
 		
 		~Pandora_Module_List                   ();
 		
 		Pandora_Module * getCurrentValue       ();
-		
+		
 		/* Add a module to the list */
-		void             addModule             (Pandora_Module *module);
+		void             addModule             (Pandora_Module *module);
 		
 		/* Move to the first element of the list */
 		void             goFirst               ();
diff --git a/pandora_agents/win32/pandora_agent_conf.cc b/pandora_agents/win32/pandora_agent_conf.cc
index 3798069812..c3413062c5 100644
--- a/pandora_agents/win32/pandora_agent_conf.cc
+++ b/pandora_agents/win32/pandora_agent_conf.cc
@@ -53,6 +53,54 @@ Pandora::Pandora_Agent_Conf::getInstance () {
 	return conf;
 }
 
+/**
+ * Additional configuration file.
+ */
+void
+Pandora::Pandora_Agent_Conf::parseFile(string path_file, Collection *aux){
+	ifstream     file_conf (path_file.c_str ());
+	string buffer;
+	unsigned int pos;
+
+	if (!file_conf.is_open ()) {
+		return;
+	}
+	
+	/* Read and set the file */
+	while (!file_conf.eof ()) {
+		/* Set the value from each line */
+		getline (file_conf, buffer);
+		
+		/* Ignore blank or commented lines */
+		if (buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != '\0') {		
+			/*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;
+			}
+		}
+	}
+	
+	file_conf.close();
+	return;
+}
+
 /**
  * Sets configuration file to Pandora_Agent_Conf object instance.
  * 
@@ -70,7 +118,7 @@ Pandora::Pandora_Agent_Conf::setFile (string filename) {
 	string       buffer;
 	unsigned int pos;
 	Collection *aux;
-
+	
 	if (this->key_values)
 		delete this->key_values;
 	this->key_values = new list<Key_Value> ();
@@ -90,6 +138,22 @@ Pandora::Pandora_Agent_Conf::setFile (string filename) {
 		
 		/* 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 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 542714c693..b3d449c749 100644
--- a/pandora_agents/win32/pandora_agent_conf.h
+++ b/pandora_agents/win32/pandora_agent_conf.h
@@ -51,6 +51,7 @@ namespace Pandora {
 		static Pandora_Agent_Conf *getInstance ();
 		
 		~Pandora_Agent_Conf            ();
+		void 				parseFile(string path_file, Collection *aux);
 		void               setFile     (string filename);
 		string             getValue    (const string key);