-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathconfigfile.c
722 lines (564 loc) · 20.2 KB
/
configfile.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* SNOOPY COMMAND LOGGER
*
* File: configfile.c
*
* Copyright (c) 2014-2015 Bostjan Skufca <[email protected]>
*
* 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.
*/
/*
* Includes order: from local to global
*/
#include "configfile.h"
#include "snoopy.h"
#include "configuration.h"
#include "outputregistry.h"
#include "util/parser-snoopy.h"
#include "util/string-snoopy.h"
#include "util/syslog-snoopy.h"
#include "lib/inih/src/ini.h"
#ifndef _XOPEN_SOURCE // For strdup
#define _XOPEN_SOURCE 500
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
/*
* Local defines
*/
#define SNOOPY_CONFIGFILE_OPTION_NOT_SUPPORTED -1
#define SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS 1
#define SNOOPY_CONFIGFILE_PARSEVALUE_ERROR 0
/*
* Define supported config options
*/
snoopy_configfile_option_t snoopy_configfile_optionRegistry[] = {
{ "error_logging", { SNOOPY_CONFIGFILE_OPTION_TYPE_BOOL, &snoopy_configfile_parseValue_error_logging, &snoopy_configfile_getOptionValueAsString_error_logging } },
#ifdef SNOOPY_FILTERING_ENABLED
{ "filter_chain", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_filter_chain, &snoopy_configfile_getOptionValueAsString_filter_chain } },
#endif
{ "message_format", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_message_format, &snoopy_configfile_getOptionValueAsString_message_format } },
{ "output", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_output, &snoopy_configfile_getOptionValueAsString_output } },
{ "syslog_facility", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_facility, &snoopy_configfile_getOptionValueAsString_syslog_facility } },
{ "syslog_ident", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_ident, &snoopy_configfile_getOptionValueAsString_syslog_ident } },
{ "syslog_level", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_level, &snoopy_configfile_getOptionValueAsString_syslog_level } },
{ "datasource_message_max_length", { SNOOPY_CONFIGFILE_OPTION_TYPE_INT, &snoopy_configfile_parseValue_datasource_message_max_length, &snoopy_configfile_getOptionValueAsString_datasource_message_max_length } },
{ "log_message_max_length", { SNOOPY_CONFIGFILE_OPTION_TYPE_INT, &snoopy_configfile_parseValue_log_message_max_length, &snoopy_configfile_getOptionValueAsString_log_message_max_length } },
{ "", { SNOOPY_CONFIGFILE_OPTION_TYPE_NONE, NULL, NULL } },
};
/*
* snoopy_configfile_load_file
*
* Description:
* Parses INI configuration file and overrides Snoopy
* configuration with changed values.
*
* Params:
* file Path log INI configuration file
*
* Return:
* int 0 on success, -1 on error openinf file, other int for other errors
*/
int snoopy_configfile_load (
char *iniFilePath
) {
int iniParseStatus;
snoopy_configuration_t *CFG;
/* Get config pointer */
CFG = snoopy_configuration_get();
/* Tell Snoopy we are using configuration file */
CFG->configfile_path = iniFilePath;
/* Parse the INI configuration file first */
iniParseStatus = ini_parse(iniFilePath, snoopy_configfile_iniParser_callback, CFG);
if (0 != iniParseStatus) {
return -1;
}
CFG->configfile_found = SNOOPY_TRUE;
/* Housekeeping */
CFG->configfile_parsed = SNOOPY_TRUE; // We have successfully parsed configuration file
return 0;
}
/*
* snoopy_configfile_parser_callback
*
* Description:
* Callback function for each found ini value in parsed config file.
*
* Params:
* ...
*
* Return:
* ...
*/
int snoopy_configfile_iniParser_callback (
void* sth,
const char* section,
const char* name,
const char* confValString
) {
/* Qualify pointer? */
snoopy_configuration_t* CFG = (snoopy_configuration_t*)sth;
/* Skip unknown sections */
if (0 != strcmp(section, "snoopy")) {
return 1;
}
/* Handle a (supported) option */
int optionId = snoopy_configfile_optionRegistry_getIdFromName(name);
if (optionId != SNOOPY_CONFIGFILE_OPTION_NOT_SUPPORTED) {
return snoopy_configfile_optionRegistry[optionId].data.valueParserPtr(confValString, CFG);
}
/* Why are we returning 1 instead of zero everywhere? */
return 1;
}
/*
* Parse 'error_logging' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_error_logging (
const char *confValString,
snoopy_configuration_t* CFG
) {
int confValInt = snoopy_configfile_getboolean(confValString, -1);
if (-1 != confValInt) {
CFG->error_logging_enabled = confValInt;
}
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_error_logging ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
if (CFG->error_logging_enabled == SNOOPY_TRUE) {
return strdup("yes");
} else {
return strdup("no");
}
}
#ifdef SNOOPY_FILTERING_ENABLED
/*
* Parse 'filter_chain' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_filter_chain (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->filter_chain = strdup(confValString);
CFG->filter_chain_malloced = SNOOPY_TRUE;
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_filter_chain ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
return strdup(CFG->filter_chain);
}
#endif
/*
* Parse 'message_format' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_message_format (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->message_format = strdup(confValString);
CFG->message_format_malloced = SNOOPY_TRUE;
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_message_format ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
return strdup(CFG->message_format);
}
/*
* Parse 'output' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_output (
const char *confValString,
snoopy_configuration_t* CFG
) {
char *confVal;
const char * outputName;
const char * outputArg;
int outputArgFound = SNOOPY_FALSE;
// Do not assign null to it explicitly, as you get "Explicit null dereference" Coverity error.
// If you do not assign it, Coverity complains with "Uninitialized pointer read".
char *saveptr1 = "";
// First clone the config value, as it gets freed by ini parsing library
confVal = strdup(confValString);
// Check if configured value contains argument(s)
if (NULL == strchr(confVal, ':')) {
outputName = confVal;
CFG->output_arg = "";
CFG->output_arg_malloced = SNOOPY_FALSE;
outputArg = "";
} else {
// Separate output name from its arguments
// (arguments may contain further ':' characters, like "file:/var/log/snoopy-%{datetime:%Y-%m-%d}")
outputName = strtok_r(confVal, ":", &saveptr1);
outputArg = outputName + strlen(outputName) + 1;
outputArgFound = SNOOPY_TRUE;
}
// Determine output name
if (SNOOPY_TRUE == snoopy_outputregistry_doesNameExist(outputName)) {
CFG->output = strdup(outputName);
CFG->output_malloced = SNOOPY_TRUE;
if (SNOOPY_TRUE == outputArgFound) {
// THINK What if conf.output_arg was set in previous call to this function,
// and is already malloced? We need to detect that and free previous
// allocation.
CFG->output_arg = strdup(outputArg);
CFG->output_arg_malloced = SNOOPY_TRUE;
}
} else {
CFG->output = SNOOPY_OUTPUT_DEFAULT;
CFG->output_malloced = SNOOPY_FALSE;
CFG->output_arg = SNOOPY_OUTPUT_DEFAULT_ARG;
CFG->output_arg_malloced = SNOOPY_FALSE;
}
// Housekeeping
free(confVal);
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_output ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
char * outputString = NULL;
if (0 == strcmp("", CFG->output_arg)) {
outputString = strdup(CFG->output);
} else {
size_t outputStringBufSize = strlen(CFG->output) + 1 + strlen(CFG->output_arg) + 1; // First +1 for ':', second +1 for '\0'
outputString = malloc(outputStringBufSize);
snprintf(outputString, outputStringBufSize, "%s:%s", CFG->output, CFG->output_arg);
outputString[outputStringBufSize-1] = '\0';
}
return outputString;
}
/*
* Parse 'syslog_facility' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_syslog_facility (
const char *confValString,
snoopy_configuration_t* CFG
) {
char *confVal;
const char *confValCleaned;
int facilityInt;
// Duplicate the ini value, as we need to modify it
confVal = strdup(confValString);
// First cleanup the value
confValCleaned = snoopy_configfile_syslog_value_cleanup(confVal);
// Evaluate and set configuration flag
facilityInt = snoopy_util_syslog_convertFacilityToInt(confValCleaned);
if (-1 == facilityInt) {
CFG->syslog_facility = SNOOPY_SYSLOG_FACILITY;
} else {
CFG->syslog_facility = facilityInt;
}
/* Housekeeping */
free(confVal);
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_syslog_facility ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
return strdup(snoopy_util_syslog_convertFacilityToStr(CFG->syslog_facility));
}
/*
* Parse 'syslog_ident' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_syslog_ident (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->syslog_ident_format = strdup(confValString);
CFG->syslog_ident_format_malloced = SNOOPY_TRUE;
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_syslog_ident ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
return strdup(CFG->syslog_ident_format);
}
/*
* Parse 'syslog_level' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_syslog_level (
const char *confValString,
snoopy_configuration_t* CFG
) {
char *confVal;
const char *confValCleaned;
int levelInt;
// Duplicate the ini value, as we need to modify it
confVal = strdup(confValString);
// First cleanup the value
confValCleaned = snoopy_configfile_syslog_value_cleanup(confVal);
// Evaluate and set configuration flag
levelInt = snoopy_util_syslog_convertLevelToInt(confValCleaned);
if (-1 == levelInt) {
CFG->syslog_level = SNOOPY_SYSLOG_LEVEL;
} else {
CFG->syslog_level = levelInt;
}
/* Housekeeping */
free(confVal);
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_syslog_level ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
return strdup(snoopy_util_syslog_convertLevelToStr(CFG->syslog_level));
}
/*
* snoopy_configfile_syslog_value_cleanup
*
* Description:
* Convert existing string to upper case, and remove LOG_ prefix
*
* Params:
* confVal Pointer to string to change and to be operated on
*
* Return:
* char * Pointer to cleaned string (either the same as initial argument,
* or 4 characters advanced, to remove LOG_ prefix
*/
char *snoopy_configfile_syslog_value_cleanup (char *confVal)
{
char *confValCleaned;
// Convert to upper case
snoopy_util_string_toUpper(confVal);
// Remove LOG_ prefix
confValCleaned = snoopy_configfile_syslog_value_remove_prefix(confVal);
return confValCleaned;
}
/*
* snoopy_configfile_syslog_value_remove_prefix
*
* Description:
* Remove the LOG_ prefix, return pointer to new string (either equal
* or +4 chars advanced)
*
* Params:
* string Pointer to string to remove LOG_ prefix
*
* Return:
* char * Pointer to non LOG_ part of the string
*/
char *snoopy_configfile_syslog_value_remove_prefix (char *confVal)
{
if (0 == strncmp(confVal, "LOG_", 4)) {
return confVal+4;
} else {
return confVal;
}
}
/*-------------------------------------------------------------------------*/
/**
@origin Literally copy-pasted from ndevilla's iniparser
@brief Get the string associated to a key, convert to a boolean
@param d Dictionary to search
@param key Key string to look for
@param notfound Value to return in case of error
@return integer
This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the notfound value is returned.
A true boolean is found if one of the following is matched:
- A string starting with 'y'
- A string starting with 'Y'
- A string starting with 't'
- A string starting with 'T'
- A string starting with '1'
A false boolean is found if one of the following is matched:
- A string starting with 'n'
- A string starting with 'N'
- A string starting with 'f'
- A string starting with 'F'
- A string starting with '0'
The notfound value returned if no boolean is identified, does not
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
int snoopy_configfile_getboolean (const char *c, int notfound)
{
int ret;
if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
ret = SNOOPY_TRUE;
} else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
ret = SNOOPY_FALSE;
} else {
ret = notfound ;
}
return ret;
}
/*
* Parse 'datasource_message_max_length' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_datasource_message_max_length (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->datasource_message_max_length = snoopy_util_parser_strByteLength(
confValString,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMIN,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMAX,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_DEFAULT
);
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
/*
* Parse 'log_message_max_length' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_log_message_max_length (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->log_message_max_length = snoopy_util_parser_strByteLength(
confValString,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMIN,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMAX,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_DEFAULT
);
return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}
char * snoopy_configfile_getOptionValueAsString_datasource_message_max_length ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
size_t strBufSize = sizeof(CFG->datasource_message_max_length)*8 + 1;
char * strBuf = malloc(strBufSize);
snprintf(strBuf, strBufSize, "%zu", CFG->datasource_message_max_length);
return strBuf;
}
char * snoopy_configfile_getOptionValueAsString_log_message_max_length ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();
size_t strBufSize = sizeof(CFG->log_message_max_length)*8 + 1;
char * strBuf = malloc(strBufSize);
snprintf(strBuf, strBufSize, "%zu", CFG->log_message_max_length);
return strBuf;
}
/*
* optionRegistry :: getIdFromName()
*
* Return:
* int: Id of a given option, or
* SNOOPY_CONFIGFILE_OPTION_NOT_SUPPORTED (-1) when not found.
*/
int snoopy_configfile_optionRegistry_getIdFromName (char const * const optionName)
{
for (int i=0 ; 0 != strcmp(snoopy_configfile_optionRegistry[i].name, "") ; i++) {
if (strcmp(snoopy_configfile_optionRegistry[i].name, optionName) == 0) {
return i;
}
}
/* Not found */
return SNOOPY_CONFIGFILE_OPTION_NOT_SUPPORTED;
}
/*
* optionRegistry :: getAll()
*
* Returns all supported config options.
*/
__attribute__((visibility("default"))) snoopy_configfile_option_t * snoopy_configfile_optionRegistry_getAll ()
{
return snoopy_configfile_optionRegistry;
}
/*
* optionRegistry :: getOptionValueAsString()
*
* Returns a malloc()-ed string representation of option value (that can be (re)used in a config file).
*/
__attribute__((visibility("default"))) char * snoopy_configfile_optionRegistry_getOptionValueAsString (char const * const optionName)
{
for (int i=0 ; 0 != strcmp(snoopy_configfile_optionRegistry[i].name, "") ; i++) {
if (strcmp(snoopy_configfile_optionRegistry[i].name, optionName) == 0) {
return snoopy_configfile_optionRegistry[i].data.getValueAsStringPtr();
}
}
return NULL; // Option not found
}