Imported Upstream version 2.7
[ossec-hids.git] / src / config / csyslogd-config.c
1 /* @(#) $Id: ./src/config/csyslogd-config.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  */
12
13 /* Functions to handle the configuration files
14  */
15
16
17 #include "csyslogd-config.h"
18 #include "config.h"
19
20
21 int Read_CSyslog(XML_NODE node, void *config, void *config2)
22 {
23     int i = 0,s = 0;
24
25     /* XML definitions */
26     char *xml_syslog_server = "server";
27     char *xml_syslog_port = "port";
28     char *xml_syslog_format = "format";
29     char *xml_syslog_level = "level";
30     char *xml_syslog_id = "rule_id";
31     char *xml_syslog_group = "group";
32     char *xml_syslog_location = "event_location";
33
34
35     GeneralConfig *gen_config = (GeneralConfig *)config;
36     SyslogConfig **syslog_config = (SyslogConfig **)gen_config->data;
37
38
39     /* Getting Granular mail_to size */
40     if(syslog_config)
41     {
42         while(syslog_config[s])
43             s++;
44     }
45
46
47     /* Allocating the memory for the config. */
48     os_realloc(syslog_config, (s + 2) * sizeof(SyslogConfig *), syslog_config);
49     os_calloc(1, sizeof(SyslogConfig), syslog_config[s]);
50     syslog_config[s + 1] = NULL;
51
52
53     /* Zeroing the elements. */
54     syslog_config[s]->server = NULL;
55     syslog_config[s]->rule_id = NULL;
56     syslog_config[s]->group = NULL;
57     syslog_config[s]->location = NULL;
58     syslog_config[s]->level = 0;
59     syslog_config[s]->port = 514;
60     syslog_config[s]->format = DEFAULT_CSYSLOG;
61     /* local 0 facility (16) + severity 4 - warning. --default */
62     syslog_config[s]->priority = (16 * 8) + 4;
63
64     while(node[i])
65     {
66         if(!node[i]->element)
67         {
68             merror(XML_ELEMNULL, ARGV0);
69             return(OS_INVALID);
70         }
71         else if(!node[i]->content)
72         {
73             merror(XML_VALUENULL, ARGV0, node[i]->element);
74             return(OS_INVALID);
75         }
76         else if(strcmp(node[i]->element, xml_syslog_level) == 0)
77         {
78             if(!OS_StrIsNum(node[i]->content))
79             {
80                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
81                 return(OS_INVALID);
82             }
83
84             syslog_config[s]->level = atoi(node[i]->content);
85         }
86         else if(strcmp(node[i]->element, xml_syslog_port) == 0)
87         {
88             if(!OS_StrIsNum(node[i]->content))
89             {
90                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
91                 return(OS_INVALID);
92             }
93
94             syslog_config[s]->port = atoi(node[i]->content);
95         }
96         else if(strcmp(node[i]->element, xml_syslog_server) == 0)
97         {
98             os_strdup(node[i]->content, syslog_config[s]->server);
99         }
100         else if(strcmp(node[i]->element, xml_syslog_id) == 0)
101         {
102             int r_id = 0;
103             char *str_pt = node[i]->content;
104
105             while(*str_pt != '\0')
106             {
107                 /* We allow spaces in between */
108                 if(*str_pt == ' ')
109                 {
110                     str_pt++;
111                     continue;
112                 }
113
114                 /* If is digit, we get the value
115                  * and search for the next digit
116                  * available
117                  */
118                 else if(isdigit((int)*str_pt))
119                 {
120                     int id_i = 0;
121
122                     r_id = atoi(str_pt);
123                     debug1("%s: DEBUG: Adding '%d' to syslog alerting",
124                            ARGV0, r_id);
125
126                     if(syslog_config[s]->rule_id)
127                     {
128                         while(syslog_config[s]->rule_id[id_i])
129                             id_i++;
130                     }
131
132                     os_realloc(syslog_config[s]->rule_id,
133                                (id_i +2) * sizeof(int),
134                                syslog_config[s]->rule_id);
135
136                     syslog_config[s]->rule_id[id_i + i] = 0;
137                     syslog_config[s]->rule_id[id_i] = r_id;
138
139                     str_pt = strchr(str_pt, ',');
140                     if(str_pt)
141                     {
142                         str_pt++;
143                     }
144                     else
145                     {
146                         break;
147                     }
148                 }
149
150                 /* Checking for duplicate commas */
151                 else if(*str_pt == ',')
152                 {
153                     str_pt++;
154                     continue;
155                 }
156
157                 else
158                 {
159                     break;
160                 }
161             }
162
163         }
164         else if(strcmp(node[i]->element, xml_syslog_format) == 0)
165         {
166             if(strcmp(node[i]->content, "default") == 0)
167             {
168                 /* Default is full format */
169             }
170             else if (strcmp(node[i]->content, "cef") == 0)
171             {
172                 /* Enable the CEF format */
173                 syslog_config[s]->format = CEF_CSYSLOG;
174             }
175             else if (strcmp(node[i]->content, "json") == 0)
176             {
177                 /* Enable the JSON format */
178                 syslog_config[s]->format = JSON_CSYSLOG;
179             }
180             else if (strcmp(node[i]->content, "splunk") == 0)
181             {
182                 /* Enable the Splunk Key/Value format */
183                 syslog_config[s]->format = SPLUNK_CSYSLOG;
184             }
185             else
186             {
187                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
188                 return(OS_INVALID);
189             }
190         }
191         else if(strcmp(node[i]->element, xml_syslog_location) == 0)
192         {
193             os_calloc(1, sizeof(OSMatch),syslog_config[s]->location);
194             if(!OSMatch_Compile(node[i]->content,
195                                 syslog_config[s]->location, 0))
196             {
197                 merror(REGEX_COMPILE, ARGV0, node[i]->content,
198                        syslog_config[s]->location->error);
199                 return(-1);
200             }
201         }
202         else if(strcmp(node[i]->element, xml_syslog_group) == 0)
203         {
204             os_calloc(1, sizeof(OSMatch),syslog_config[s]->group);
205             if(!OSMatch_Compile(node[i]->content,
206                                 syslog_config[s]->group, 0))
207             {
208                 merror(REGEX_COMPILE, ARGV0, node[i]->content,
209                        syslog_config[s]->group->error);
210                 return(-1);
211             }
212         }
213         else
214         {
215             merror(XML_INVELEM, ARGV0, node[i]->element);
216             return(OS_INVALID);
217         }
218         i++;
219     }
220
221
222     /* We must have at least one entry set */
223     if(!syslog_config[s]->server)
224     {
225         merror(XML_INV_CSYSLOG, ARGV0);
226         return(OS_INVALID);
227     }
228
229
230     gen_config->data = syslog_config;
231     return(0);
232 }
233
234
235 /* EOF */