2008-11-28 11:51:03 +01:00
|
|
|
/* Common functions to any Pandora program.
|
2020-11-27 13:52:35 +01:00
|
|
|
Copyright (c) 2006-2021 Artica ST.
|
2008-11-28 11:51:03 +01:00
|
|
|
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 <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cctype>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "pandora.h"
|
|
|
|
#include "pandora_strutils.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Pandora;
|
|
|
|
using namespace Pandora_Strutils;
|
|
|
|
|
|
|
|
#define PATH_SIZE _MAX_PATH+1
|
2021-01-26 01:00:11 +01:00
|
|
|
#define PANDORA_VERSION ("7.0NG.752(Build 210126)")
|
2008-11-28 11:51:03 +01:00
|
|
|
|
|
|
|
string pandora_path;
|
|
|
|
string pandora_dir;
|
|
|
|
bool pandora_debug;
|
2015-09-22 18:35:19 +02:00
|
|
|
bool pandora_log_disable;
|
2008-11-28 11:51:03 +01:00
|
|
|
string pandora_version = PANDORA_VERSION;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a string and initialize the key and the value.
|
|
|
|
*
|
|
|
|
* The string should be in the format:
|
|
|
|
* - <code>key value</code>
|
|
|
|
* - <code>key "value with blankspaces"</code>
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Key_Value::parseLine (string str) {
|
2014-06-20 13:16:42 +02:00
|
|
|
int pos;
|
2008-11-28 11:51:03 +01:00
|
|
|
list<string> tokens;
|
|
|
|
list<string>::iterator iter;
|
|
|
|
string trimmedstr;
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
trimmedstr = trim (str);
|
2008-12-01 14:40:19 +01:00
|
|
|
|
|
|
|
/* Check if the string has " */
|
2008-11-28 11:51:03 +01:00
|
|
|
pos = trimmedstr.find ("\"");
|
|
|
|
if (pos == string::npos) {
|
|
|
|
stringtok (tokens, trimmedstr, " \t");
|
|
|
|
} else {
|
|
|
|
stringtok (tokens, trimmedstr, "\"");
|
|
|
|
}
|
2008-12-01 14:40:19 +01:00
|
|
|
|
|
|
|
/* Pick the first and the last value of the token list */
|
2008-11-28 11:51:03 +01:00
|
|
|
iter = tokens.begin ();
|
|
|
|
key = trim (*iter);
|
|
|
|
transform (key.begin(), key.end(), key.begin(), (int(*)(int)) tolower);
|
|
|
|
iter = tokens.end ();
|
|
|
|
iter--;
|
2008-12-01 14:40:19 +01:00
|
|
|
/* Check if the line has only one token */
|
2008-11-28 11:51:03 +01:00
|
|
|
if (iter != tokens.begin ()) {
|
|
|
|
value = trim (*iter);
|
|
|
|
} else {
|
|
|
|
value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 04:04:00 +01:00
|
|
|
/**
|
|
|
|
* Parses a string by position and initialize the key and the value.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Key_Value::parseLineByPosition (string str, int pos) {
|
|
|
|
string trimmedstr;
|
|
|
|
|
|
|
|
trimmedstr = trim (str);
|
|
|
|
|
|
|
|
key = trim (trimmedstr.substr (0, pos));
|
|
|
|
value = trim (trimmedstr.substr (pos));
|
|
|
|
}
|
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
/**
|
|
|
|
* Get the key of the object.
|
|
|
|
*
|
|
|
|
* @return The key
|
|
|
|
*/
|
|
|
|
string
|
|
|
|
Key_Value::getKey () {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2016-09-29 12:50:04 +02:00
|
|
|
/**
|
|
|
|
* Set the key of the object.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Key_Value::setKey (const string key) {
|
|
|
|
this->key = key;
|
|
|
|
}
|
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
/**
|
|
|
|
* Get the value of the object.
|
|
|
|
*
|
|
|
|
* @return The value
|
|
|
|
*/
|
|
|
|
string
|
|
|
|
Key_Value::getValue () {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-09-29 12:50:04 +02:00
|
|
|
/**
|
|
|
|
* Set the value of the object.
|
|
|
|
*
|
|
|
|
* @return The value
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Key_Value::setValue (const string value) {
|
|
|
|
this->value = value;
|
|
|
|
}
|
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
void
|
|
|
|
pandoraWriteLog (string filename, string line) {
|
|
|
|
string buffer;
|
|
|
|
char str_time[25];
|
|
|
|
FILE *file;
|
|
|
|
string filepath;
|
|
|
|
SYSTEMTIME st;
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2011-05-06 12:38:30 +02:00
|
|
|
GetLocalTime(&st);
|
2008-12-01 14:40:19 +01:00
|
|
|
sprintf (str_time, "%d-%02d-%02d %02d:%02d:%02d ", st.wYear, st.wMonth, st.wDay,
|
|
|
|
st.wHour, st.wMinute, st.wSecond);
|
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
buffer = (char *) str_time;
|
|
|
|
buffer += line;
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
filepath = pandora_dir + filename;
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
file = fopen (filepath.c_str (), "a+");
|
|
|
|
if (file != NULL) {
|
|
|
|
fprintf (file, "%s\n", buffer.c_str ());
|
|
|
|
fclose (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a message in the log file.
|
|
|
|
*
|
|
|
|
* The log file is used to write the output of errors and problems of the
|
|
|
|
* agent.
|
|
|
|
*
|
|
|
|
* @param format String output format (like printf).
|
|
|
|
* @param ... Variable argument list
|
|
|
|
*/
|
|
|
|
void
|
2009-09-22 Ramon Novoa <rnovoa@artica.es>
* debug_new.h, fast_mutex.h,
debug_new.cpp, static_assert.h: Added to repository. Wu Yongwei's
memory leak detection tool (part of Nvwa). Only compiled when
debugging is enabled.
* bin/util/tentacle_client.exe: Added to repository. Tentacle client
needed to build the installer.
* installer/pandora_2.0.mpi: moved to installer/pandora.mpi. Updated.
The installer can now be built directly from the installer
subdirectory.
* configure.in, Makefile.am, autogen.sh: Created a proper
configure.in and Makefile.am. The agent can now be cross-compiled
from Linux :-D
* bin/pandora_agent.conf: Fixed. A local configuration had been
uploaded.
* pandora_windows_service.h,pandora_windows_service.cc,
udp_server/udp_server.cc, udp_server/udp_server.h: Properly shutdown
the UDP server.
* modules/pandora_module_regexp.cc,
modules/pandora_module_inventory.cc,
modules/pandora_module_factory.cc,
modules/pandora_module.cc, pandora_strutils.cc,
pandora.h, pandora.cc: Fixed a couple of memory leaks. Small changes
to avoid compile warnings when cross-compiling from Linux.
* windows_service.cc: Removed the interactive service flag to avoid
'black windows'.
* main.cc: Include the memory leak detection tool if debugging is
enabled.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1966 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-09-22 13:48:25 +02:00
|
|
|
Pandora::pandoraLog (const char *format, ...) {
|
2015-09-22 18:35:19 +02:00
|
|
|
if (!pandora_log_disable) {
|
|
|
|
va_list args;
|
|
|
|
char msg[5000];
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2015-09-22 18:35:19 +02:00
|
|
|
va_start (args, format);
|
|
|
|
vsprintf (msg, format, args);
|
|
|
|
va_end (args);
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2015-09-22 18:35:19 +02:00
|
|
|
pandoraWriteLog ("pandora_agent.log", (char *) msg);
|
|
|
|
}
|
|
|
|
return;
|
2008-11-28 11:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a message in the debug file.
|
|
|
|
*
|
|
|
|
* The log file is used to write the output of debugging information of the
|
|
|
|
* agent.
|
|
|
|
*
|
|
|
|
* @param format String output format.
|
|
|
|
* @param ... Variable argument list
|
|
|
|
*/
|
|
|
|
void
|
2009-09-22 Ramon Novoa <rnovoa@artica.es>
* debug_new.h, fast_mutex.h,
debug_new.cpp, static_assert.h: Added to repository. Wu Yongwei's
memory leak detection tool (part of Nvwa). Only compiled when
debugging is enabled.
* bin/util/tentacle_client.exe: Added to repository. Tentacle client
needed to build the installer.
* installer/pandora_2.0.mpi: moved to installer/pandora.mpi. Updated.
The installer can now be built directly from the installer
subdirectory.
* configure.in, Makefile.am, autogen.sh: Created a proper
configure.in and Makefile.am. The agent can now be cross-compiled
from Linux :-D
* bin/pandora_agent.conf: Fixed. A local configuration had been
uploaded.
* pandora_windows_service.h,pandora_windows_service.cc,
udp_server/udp_server.cc, udp_server/udp_server.h: Properly shutdown
the UDP server.
* modules/pandora_module_regexp.cc,
modules/pandora_module_inventory.cc,
modules/pandora_module_factory.cc,
modules/pandora_module.cc, pandora_strutils.cc,
pandora.h, pandora.cc: Fixed a couple of memory leaks. Small changes
to avoid compile warnings when cross-compiling from Linux.
* windows_service.cc: Removed the interactive service flag to avoid
'black windows'.
* main.cc: Include the memory leak detection tool if debugging is
enabled.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1966 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-09-22 13:48:25 +02:00
|
|
|
Pandora::pandoraDebug (const char *format, ...) {
|
2008-11-28 11:51:03 +01:00
|
|
|
if (pandora_debug) {
|
|
|
|
va_list args;
|
|
|
|
char msg[5000];
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
va_start (args, format);
|
|
|
|
vsprintf (msg, format, args);
|
|
|
|
va_end (args);
|
2008-12-01 14:40:19 +01:00
|
|
|
|
2008-11-28 11:51:03 +01:00
|
|
|
pandoraWriteLog ("pandora_debug.log", (char *) msg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Secure free of a pointer.
|
|
|
|
*
|
|
|
|
* @param pointer pointer to free.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Pandora::pandoraFree (void * pointer) {
|
|
|
|
if (pointer != NULL)
|
|
|
|
free (pointer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the installation directory of the application.
|
|
|
|
*
|
|
|
|
* This directory is the path to the directory which holds
|
|
|
|
* the binary file.
|
|
|
|
*
|
|
|
|
* @param dir The path to the directory.
|
|
|
|
*
|
|
|
|
* @see getPandoraInstallDir
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Pandora::setPandoraInstallDir (string dir) {
|
|
|
|
pandora_dir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the installation directory of the application.
|
|
|
|
*
|
|
|
|
* This directory is the path to the directory which holds
|
|
|
|
* the binary file.
|
|
|
|
*
|
|
|
|
* @return The path to the directory.
|
|
|
|
*
|
|
|
|
* @see setPandoraInstallDir
|
|
|
|
*/
|
|
|
|
string
|
|
|
|
Pandora::getPandoraInstallDir () {
|
|
|
|
return pandora_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the installation path of the application.
|
|
|
|
*
|
|
|
|
* This the complete path to the binary file.
|
|
|
|
*
|
|
|
|
* @param path The path to the binary file.
|
|
|
|
*
|
|
|
|
* @see getPandoraInstallPath
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Pandora::setPandoraInstallPath (string path) {
|
|
|
|
pandora_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the installation path of the application.
|
|
|
|
*
|
|
|
|
* This the complete path to the binary file.
|
|
|
|
*
|
|
|
|
* @return The path.
|
|
|
|
*
|
|
|
|
* @see setPandoraInstallPath
|
|
|
|
*/
|
|
|
|
string
|
|
|
|
Pandora::getPandoraInstallPath () {
|
|
|
|
return pandora_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the debug flag.
|
|
|
|
*
|
|
|
|
* If the flag is true output wil be generated and XML files will not be deleted.
|
|
|
|
*
|
|
|
|
* @param dbg Turns the debug flag on/off.
|
|
|
|
*
|
|
|
|
* @see getPandoraDebug
|
|
|
|
* @see pandoraDebug
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Pandora::setPandoraDebug (bool dbg) {
|
|
|
|
pandora_debug = dbg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the debug flag value.
|
|
|
|
*
|
|
|
|
* If the flag is truee output wil be generated and XML files will not be deleted.
|
|
|
|
*
|
|
|
|
* @see setPandoraDebug
|
|
|
|
* @see pandoraDebug
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Pandora::getPandoraDebug () {
|
|
|
|
return pandora_debug;
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:35:19 +02:00
|
|
|
/**
|
|
|
|
* Set the disable logfile flag.
|
|
|
|
*
|
|
|
|
* If the flag is true no logs will be written.
|
|
|
|
*
|
|
|
|
* @param dbg Turns the debug flag on/off.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Pandora::setPandoraLogDisable (bool dbg) {
|
|
|
|
pandora_log_disable = dbg;
|
|
|
|
}
|
2008-11-28 11:51:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the version of the agent.
|
|
|
|
*
|
|
|
|
* @return The version.
|
|
|
|
*/
|
|
|
|
string
|
|
|
|
Pandora::getPandoraAgentVersion () {
|
|
|
|
return pandora_version;
|
|
|
|
}
|
2008-12-03 13:04:48 +01:00
|
|
|
|
|
|
|
|
2011-05-06 12:38:30 +02:00
|
|
|
bool
|
2008-12-03 13:04:48 +01:00
|
|
|
Pandora::is_enabled (string value) {
|
|
|
|
static string enabled_values[] = {"enabled", "1", "on", "yes", "si", "sí", "ok", "true", ""};
|
2011-05-06 12:38:30 +02:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (value == "") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (enabled_values[i] != "") {
|
|
|
|
if (enabled_values[i] == value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return false;
|
2008-12-03 13:04:48 +01:00
|
|
|
}
|