/
pandora.cc
341 lines (299 loc) · 6.69 KB
/
pandora.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Common functions to any Pandora program.
Copyright (c) 2006-2021 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 <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
#define PANDORA_VERSION ("7.0NG.765 Build 221027")
string pandora_path;
string pandora_dir;
bool pandora_debug;
bool pandora_log_disable;
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) {
int pos;
list<string> tokens;
list<string>::iterator iter;
string trimmedstr;
trimmedstr = trim (str);
/* Check if the string has " */
pos = trimmedstr.find ("\"");
if (pos == string::npos) {
stringtok (tokens, trimmedstr, " \t");
} else {
stringtok (tokens, trimmedstr, "\"");
}
/* Pick the first and the last value of the token list */
iter = tokens.begin ();
key = trim (*iter);
transform (key.begin(), key.end(), key.begin(), (int(*)(int)) tolower);
iter = tokens.end ();
iter--;
/* Check if the line has only one token */
if (iter != tokens.begin ()) {
value = trim (*iter);
} else {
value = "";
}
}
/**
* 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));
}
/**
* Get the key of the object.
*
* @return The key
*/
string
Key_Value::getKey () {
return key;
}
/**
* Set the key of the object.
*
*/
void
Key_Value::setKey (const string key) {
this->key = key;
}
/**
* Get the value of the object.
*
* @return The value
*/
string
Key_Value::getValue () {
return value;
}
/**
* Set the value of the object.
*
* @return The value
*/
void
Key_Value::setValue (const string value) {
this->value = value;
}
void
pandoraWriteLog (string filename, string line) {
string buffer;
char str_time[25];
FILE *file;
string filepath;
SYSTEMTIME st;
GetLocalTime(&st);
sprintf (str_time, "%d-%02d-%02d %02d:%02d:%02d ", st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond);
buffer = (char *) str_time;
buffer += line;
filepath = pandora_dir + filename;
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
Pandora::pandoraLog (const char *format, ...) {
if (!pandora_log_disable) {
va_list args;
char msg[5000];
va_start (args, format);
vsprintf (msg, format, args);
va_end (args);
pandoraWriteLog ("pandora_agent.log", (char *) msg);
}
return;
}
/**
* 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
Pandora::pandoraDebug (const char *format, ...) {
if (pandora_debug) {
va_list args;
char msg[5000];
va_start (args, format);
vsprintf (msg, format, args);
va_end (args);
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;
}
/**
* 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;
}
/**
* Get the version of the agent.
*
* @return The version.
*/
string
Pandora::getPandoraAgentVersion () {
return pandora_version;
}
bool
Pandora::is_enabled (string value) {
static string enabled_values[] = {"enabled", "1", "on", "yes", "si", "sí", "ok", "true", ""};
int i = 0;
if (value == "") {
return false;
}
while (enabled_values[i] != "") {
if (enabled_values[i] == value) {
return true;
}
i++;
}
return false;
}