forked from a2o/snoopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigfile.c
320 lines (269 loc) · 10.1 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
/*
* SNOOPY LOGGER
*
* File: configfile.c
*
* Copyright (c) 2014 [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.
*/
/*
* Include all required C resources
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <syslog.h>
/*
* Include all snoopy-related resources
*/
#include "snoopy.h"
#include "configfile.h"
#include "configuration.h"
/*
* Include iniparser-related resources
*/
#include "lib/iniparser/src/iniparser.h"
/*
* 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
) {
dictionary *ini ;
char *confValString; // Temporary query result space
int confValInt; // Temporary query result space
/* Tell snoopy we are using configuration file */
snoopy_configuration.config_file_path = iniFilePath;
/* Parse the INI configuration file first */
ini = iniparser_load(iniFilePath);
if (NULL == ini) {
// TODO snoopy error handling
return -1;
}
snoopy_configuration.config_file_found = SNOOPY_TRUE;
/* Pick out snoopy configuration variables */
confValInt = iniparser_getboolean(ini, "snoopy:error_logging", -1);
if (-1 != confValInt) {
snoopy_configuration.error_logging_enabled = confValInt;
}
confValString = iniparser_getstring(ini, "snoopy:message_format", NULL);
if (NULL != confValString) {
snoopy_configuration.message_format = strdup(confValString);
snoopy_configuration.message_format_malloced = SNOOPY_TRUE;
}
confValString = iniparser_getstring(ini, "snoopy:filter_chain", NULL);
if (NULL != confValString) {
snoopy_configuration.filter_chain = strdup(confValString);
snoopy_configuration.filter_chain_malloced = SNOOPY_TRUE;
}
confValString = iniparser_getstring(ini, "snoopy:output", NULL);
if (NULL != confValString) {
snoopy_configfile_parse_output(confValString);
}
confValString = iniparser_getstring(ini, "snoopy:syslog_facility", NULL);
if (NULL != confValString) {
snoopy_configfile_parse_syslog_facility(confValString);
}
confValString = iniparser_getstring(ini, "snoopy:syslog_level", NULL);
if (NULL != confValString) {
snoopy_configfile_parse_syslog_level(confValString);
}
/* Housekeeping */
snoopy_configuration.config_file_parsed = SNOOPY_TRUE; // We have sucessfully parsed configuration file
iniparser_freedict(ini);
return 0;
}
/*
* snoopy_configfile_parse_output
*
* Description:
* Parses configuration setting syslog_output and
* sets appropriate internal configuration variable(s).
* Uses default setting if unknown value.
*
* Params:
* confVal Value from configuration file
*
* Return:
* void
*/
void snoopy_configfile_parse_output (
char *confVal
) {
if (strcmp(confVal, SNOOPY_OUTPUT_PROVIDER_DEVLOG) == 0) { snoopy_configuration.output_provider = SNOOPY_OUTPUT_PROVIDER_DEVLOG; }
if (strcmp(confVal, SNOOPY_OUTPUT_PROVIDER_SYSLOG) == 0) { snoopy_configuration.output_provider = SNOOPY_OUTPUT_PROVIDER_SYSLOG; }
// TODO other outputs
// Clone string
// Check if colon character is present, split into left and right string
// If right string is non-empty, malloc it, and store path in there
// TODO configure output at ./configue time
}
/*
* snoopy_configfile_parse_syslog_facility
*
* Description:
* Parses configuration setting syslog_facility and
* sets appropriate config variable.
* Uses default setting if unknown value.
*
* Params:
* confVal Value from configuration file
*
* Return:
* void
*/
void snoopy_configfile_parse_syslog_facility (
char *confVal
) {
char *confValCleaned;
// First cleanup the value
confValCleaned = snoopy_configfile_syslog_value_cleanup(confVal);
// Evaluate and set configuration flag
if (strcmp(confValCleaned, "AUTH") == 0) { snoopy_configuration.syslog_facility = LOG_AUTH; }
else if (strcmp(confValCleaned, "AUTHPRIV") == 0) { snoopy_configuration.syslog_facility = LOG_AUTHPRIV; }
else if (strcmp(confValCleaned, "CRON") == 0) { snoopy_configuration.syslog_facility = LOG_CRON; }
else if (strcmp(confValCleaned, "DAEMON") == 0) { snoopy_configuration.syslog_facility = LOG_DAEMON; }
else if (strcmp(confValCleaned, "FTP") == 0) { snoopy_configuration.syslog_facility = LOG_FTP; }
else if (strcmp(confValCleaned, "KERN") == 0) { snoopy_configuration.syslog_facility = LOG_KERN; }
else if (strcmp(confValCleaned, "LOCAL0") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL0; }
else if (strcmp(confValCleaned, "LOCAL1") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL1; }
else if (strcmp(confValCleaned, "LOCAL2") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL2; }
else if (strcmp(confValCleaned, "LOCAL3") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL3; }
else if (strcmp(confValCleaned, "LOCAL4") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL4; }
else if (strcmp(confValCleaned, "LOCAL5") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL5; }
else if (strcmp(confValCleaned, "LOCAL6") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL6; }
else if (strcmp(confValCleaned, "LOCAL7") == 0) { snoopy_configuration.syslog_facility = LOG_LOCAL7; }
else if (strcmp(confValCleaned, "LPR") == 0) { snoopy_configuration.syslog_facility = LOG_LPR; }
else if (strcmp(confValCleaned, "MAIL") == 0) { snoopy_configuration.syslog_facility = LOG_MAIL; }
else if (strcmp(confValCleaned, "NEWS") == 0) { snoopy_configuration.syslog_facility = LOG_NEWS; }
else if (strcmp(confValCleaned, "SYSLOG") == 0) { snoopy_configuration.syslog_facility = LOG_SYSLOG; }
else if (strcmp(confValCleaned, "USER") == 0) { snoopy_configuration.syslog_facility = LOG_USER; }
else if (strcmp(confValCleaned, "UUCP") == 0) { snoopy_configuration.syslog_facility = LOG_UUCP; }
else {
snoopy_configuration.syslog_facility = SNOOPY_SYSLOG_FACILITY;
}
}
/*
* snoopy_configfile_parse_syslog_level
*
* Description:
* Parses configuration setting syslog_level and
* sets appropriate config variable.
* Uses default setting if unknown value.
*
* Params:
* confVal Value from configuration file
*
* Return:
* void
*/
void snoopy_configfile_parse_syslog_level (
char *confVal
) {
char *confValCleaned;
// First cleanup the value
confValCleaned = snoopy_configfile_syslog_value_cleanup(confVal);
// Evaluate and set configuration flag
if (strcmp(confValCleaned, "EMERG") == 0) { snoopy_configuration.syslog_level = LOG_EMERG; }
else if (strcmp(confValCleaned, "ALERT") == 0) { snoopy_configuration.syslog_level = LOG_ALERT; }
else if (strcmp(confValCleaned, "CRIT") == 0) { snoopy_configuration.syslog_level = LOG_CRIT; }
else if (strcmp(confValCleaned, "ERR") == 0) { snoopy_configuration.syslog_level = LOG_ERR; }
else if (strcmp(confValCleaned, "WARNING") == 0) { snoopy_configuration.syslog_level = LOG_WARNING; }
else if (strcmp(confValCleaned, "NOTICE") == 0) { snoopy_configuration.syslog_level = LOG_NOTICE; }
else if (strcmp(confValCleaned, "INFO") == 0) { snoopy_configuration.syslog_level = LOG_INFO; }
else if (strcmp(confValCleaned, "DEBUG") == 0) { snoopy_configuration.syslog_level = LOG_DEBUG; }
else {
snoopy_configuration.syslog_level = SNOOPY_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;
// Initialize - just in case
confValCleaned = confVal;
// Convert to upper case
snoopy_configfile_strtoupper(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;
}
}
/*
* snoopy_configfile_strtoupper
*
* Description:
* Convert existing string to upper case
*
* Params:
* string Pointer to string to change and to be operated on
*
* Return:
* void
*/
void snoopy_configfile_strtoupper (char *s)
{
while (*s) {
if ((*s >= 'a' ) && (*s <= 'z')) {
*s -= ('a'-'A');
}
s++;
}
}