pandorafms/pandora_agents/win32/pandora_agent_conf.cc

248 lines
6.1 KiB
C++
Raw Normal View History

/* Library to manage a list of key-value options.
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 <fstream>
#include "pandora_agent_conf.h"
#include "pandora_strutils.h"
#include <iostream>
#include "pandora.h"
using namespace std;
2006-08-15 Esteban Sanchez <estebans@artica.es> * autogen.sh, configure.in, Makefile.am, Doxyfile.in: Added to repository. They are used to generate documentation, not to compile. * main.cc: Added some comments. Style correction. * pandora.[cc,h]: Added documentation comments. Changed visibility of some attributes. * pandora_agent_conf.[cc,h]: Added to Pandora namespace. Added documentation comments. * pandora_windows_service.[cc,h], pandora_strutils.[cc,h], windows/pandora_windows_infp.[cc,h], ssh/pandora_ssh_test.[cc,h]: Added documentation comments. * ssh/pandora_ssh_client.[cc,h]:Added documentation comments. Removed old method to connect with user and password. Style correction. * misc/pandora_file.[cc,h]: Added documentation comments. Renamed some parameters. * modules/pandora_module.[cc,h]: Added documentation comments. Put a name to the enumerators. Added a new class to agroupate all exceptions produced by Pandora_Module child class objects. Changed visibility of some attributes. Added some new methods and renamed others. * modules/pandora_module_cpuusage.[cc,h], modules/pandora_module_exec.[cc,h], modules/pandora_module_freememory.[cc,h], modules/pandora_module_freedisk.[cc,h], modules/pandora_module_proc.[cc,h], modules/pandora_module_service.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. * modules/pandora_module_list.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. Added to Pandora_Modules namespace. * windows/pandora_windows_wmi.[cc,h]: Added documentation comments. Renamed Pandora_Wmi_Error to Pandora_Wmi_Exception. * windows_service.[cc,h]: Added documentation comments. Changed visibility of some attributes. Style correction. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@150 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2006-08-25 15:02:03 +02:00
using namespace Pandora;
using namespace Pandora_Strutils;
#define MAX_KEYS 100
Pandora::Pandora_Agent_Conf::Pandora_Agent_Conf () {
this->key_values = NULL;
this->collection_list = NULL;
}
2006-08-15 Esteban Sanchez <estebans@artica.es> * autogen.sh, configure.in, Makefile.am, Doxyfile.in: Added to repository. They are used to generate documentation, not to compile. * main.cc: Added some comments. Style correction. * pandora.[cc,h]: Added documentation comments. Changed visibility of some attributes. * pandora_agent_conf.[cc,h]: Added to Pandora namespace. Added documentation comments. * pandora_windows_service.[cc,h], pandora_strutils.[cc,h], windows/pandora_windows_infp.[cc,h], ssh/pandora_ssh_test.[cc,h]: Added documentation comments. * ssh/pandora_ssh_client.[cc,h]:Added documentation comments. Removed old method to connect with user and password. Style correction. * misc/pandora_file.[cc,h]: Added documentation comments. Renamed some parameters. * modules/pandora_module.[cc,h]: Added documentation comments. Put a name to the enumerators. Added a new class to agroupate all exceptions produced by Pandora_Module child class objects. Changed visibility of some attributes. Added some new methods and renamed others. * modules/pandora_module_cpuusage.[cc,h], modules/pandora_module_exec.[cc,h], modules/pandora_module_freememory.[cc,h], modules/pandora_module_freedisk.[cc,h], modules/pandora_module_proc.[cc,h], modules/pandora_module_service.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. * modules/pandora_module_list.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. Added to Pandora_Modules namespace. * windows/pandora_windows_wmi.[cc,h]: Added documentation comments. Renamed Pandora_Wmi_Error to Pandora_Wmi_Exception. * windows_service.[cc,h]: Added documentation comments. Changed visibility of some attributes. Style correction. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@150 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2006-08-25 15:02:03 +02:00
/**
* Destroy a Pandora_Agent_Conf object.
*/
Pandora::Pandora_Agent_Conf::~Pandora_Agent_Conf () {
delete key_values;
delete collection_list;
}
Pandora_Agent_Conf *
Pandora::Pandora_Agent_Conf::getInstance () {
static Pandora_Agent_Conf *conf = NULL;
if (conf)
return conf;
conf = new Pandora_Agent_Conf ();
return conf;
}
/**
* Sets configuration file to Pandora_Agent_Conf object instance.
2006-08-15 Esteban Sanchez <estebans@artica.es> * autogen.sh, configure.in, Makefile.am, Doxyfile.in: Added to repository. They are used to generate documentation, not to compile. * main.cc: Added some comments. Style correction. * pandora.[cc,h]: Added documentation comments. Changed visibility of some attributes. * pandora_agent_conf.[cc,h]: Added to Pandora namespace. Added documentation comments. * pandora_windows_service.[cc,h], pandora_strutils.[cc,h], windows/pandora_windows_infp.[cc,h], ssh/pandora_ssh_test.[cc,h]: Added documentation comments. * ssh/pandora_ssh_client.[cc,h]:Added documentation comments. Removed old method to connect with user and password. Style correction. * misc/pandora_file.[cc,h]: Added documentation comments. Renamed some parameters. * modules/pandora_module.[cc,h]: Added documentation comments. Put a name to the enumerators. Added a new class to agroupate all exceptions produced by Pandora_Module child class objects. Changed visibility of some attributes. Added some new methods and renamed others. * modules/pandora_module_cpuusage.[cc,h], modules/pandora_module_exec.[cc,h], modules/pandora_module_freememory.[cc,h], modules/pandora_module_freedisk.[cc,h], modules/pandora_module_proc.[cc,h], modules/pandora_module_service.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. * modules/pandora_module_list.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. Added to Pandora_Modules namespace. * windows/pandora_windows_wmi.[cc,h]: Added documentation comments. Renamed Pandora_Wmi_Error to Pandora_Wmi_Exception. * windows_service.[cc,h]: Added documentation comments. Changed visibility of some attributes. Style correction. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@150 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2006-08-25 15:02:03 +02:00
*
* It parses the filename and initialize the internal structures
* of configuration values. The configuration file consist of a number of
* lines of some of these forms:
* - <code>name value</code>
* - <code>name "value with blankspaces"</code>
*
* @param filename Configuration file to open.
*/
void
Pandora::Pandora_Agent_Conf::setFile (string filename) {
ifstream file (filename.c_str ());
string buffer;
unsigned int pos;
Collection *aux;
if (this->key_values)
delete this->key_values;
this->key_values = new list<Key_Value> ();
if (this->collection_list)
delete this->collection_list;
this->collection_list = new list<Collection> ();
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 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 ();
}
2006-08-15 Esteban Sanchez <estebans@artica.es> * autogen.sh, configure.in, Makefile.am, Doxyfile.in: Added to repository. They are used to generate documentation, not to compile. * main.cc: Added some comments. Style correction. * pandora.[cc,h]: Added documentation comments. Changed visibility of some attributes. * pandora_agent_conf.[cc,h]: Added to Pandora namespace. Added documentation comments. * pandora_windows_service.[cc,h], pandora_strutils.[cc,h], windows/pandora_windows_infp.[cc,h], ssh/pandora_ssh_test.[cc,h]: Added documentation comments. * ssh/pandora_ssh_client.[cc,h]:Added documentation comments. Removed old method to connect with user and password. Style correction. * misc/pandora_file.[cc,h]: Added documentation comments. Renamed some parameters. * modules/pandora_module.[cc,h]: Added documentation comments. Put a name to the enumerators. Added a new class to agroupate all exceptions produced by Pandora_Module child class objects. Changed visibility of some attributes. Added some new methods and renamed others. * modules/pandora_module_cpuusage.[cc,h], modules/pandora_module_exec.[cc,h], modules/pandora_module_freememory.[cc,h], modules/pandora_module_freedisk.[cc,h], modules/pandora_module_proc.[cc,h], modules/pandora_module_service.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. * modules/pandora_module_list.[cc,h]: Added documentation comments. Some changes to adapt the objects to the Pandora_Module changes. Added to Pandora_Modules namespace. * windows/pandora_windows_wmi.[cc,h]: Added documentation comments. Renamed Pandora_Wmi_Error to Pandora_Wmi_Exception. * windows_service.[cc,h]: Added documentation comments. Changed visibility of some attributes. Style correction. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@150 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2006-08-25 15:02:03 +02:00
/**
* Queries for a configuration value.
*
* This method search in the key_values attribute for a
* configuration value which match the key supplied.
*
* @param key Key to look for.
*
* @return The value of the configuration key looked for.
* If it could not be found then an empty string is returned.
*/
string
Pandora::Pandora_Agent_Conf::getValue (const string key)
{
std::list<Key_Value>::iterator i;
for (i = key_values->begin (); i != key_values->end (); i++) {
if ((*i).getKey () == key) {
return (*i).getValue ();
}
}
return "";
}
/**
* Queries for a collection name.
*
* This method returns the name of the current
* collection pointed by an iterator.
*
* @return The name of the current colletion
*
*/
string
Pandora::Pandora_Agent_Conf::getCurrentCollectionName() {
string aux;
aux = collection_it->name;
return aux;
}
/**
* Queries for a collection check of added to PATH.
*
* This method returns 1 if the collections is in the PATH
*
* @return 1 if the collections is added to PATH
*
*/
unsigned char
Pandora::Pandora_Agent_Conf::getCurrentCollectionVerify() {
unsigned char aux;
aux = collection_it->verify;
return aux;
}
/**
* Set check path add field to 1.
*
*/
void
Pandora::Pandora_Agent_Conf::setCurrentCollectionVerify() {
collection_it->verify = 1;
}
/**
* Check is there is a collection with the same name in the list
*
* @param The name of the collection to check.
*
* @return True if there is a collection with the same name.
*/
bool
Pandora::Pandora_Agent_Conf::isInCollectionList(string name) {
list<Collection>::iterator p;
string name_md5;
for (p = collection_list->begin();p != collection_list->end();p++) {
name_md5 = p->name+".md5";
if ( (strcmp(p->name.c_str(), name.c_str()) == 0) ||
(strcmp(name_md5.c_str(), name.c_str()) == 0)){
return true;
}
}
return false;
}
/**
* Set iterator pointing to the first collection of the list.
*
* This method set the iterator pointing to the first collection of the list.
*
*/
void
Pandora::Pandora_Agent_Conf::goFirstCollection() {
collection_it = collection_list->begin();
}
/**
* Move the collection iterator to the next item
*
* This method move the iterator to the next item.
*
*/
void
Pandora::Pandora_Agent_Conf::goNextCollection() {
this->collection_it++;
}
/**
* Compare the iterator with the last collection.
*
* This method return true if the iterator is pointing to the last collection
*
* @return True if the iterator is pointing to the last collection
*
*/
bool
Pandora::Pandora_Agent_Conf::isLastCollection() {
return collection_it == collection_list->end();
}