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