Imported Upstream version 2.5.1
[ossec-hids.git] / src / config / reports-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 "shared.h"
17 #include "reports-config.h"
18
19
20 /* Filter argument. */
21 static int _filter_arg(char *mystr)
22 {
23     if(!mystr)
24     {
25         return(0);
26     }
27
28     while(*mystr)
29     {
30         if((*mystr >= 'a' && *mystr <= 'z') ||
31            (*mystr >= 'A' && *mystr <= 'Z') ||
32            (*mystr >= '0' && *mystr <= '9') ||
33            *mystr == '-' || *mystr == '_')
34         {
35             mystr++;
36         }
37         else
38         {
39             *mystr = '-';
40             mystr++;
41         }
42     }
43
44     return(1);
45 }
46
47
48 int Read_CReports(XML_NODE node, void *config, void *config2)
49 {
50     int i = 0,s = 0;
51
52     /* XML definitions */
53     char *xml_title = "title";
54     char *xml_type = "type";
55     char *xml_categories = "category";
56     char *xml_group = "group";
57     char *xml_rule = "rule";
58     char *xml_level = "level";
59     char *xml_location = "location";
60     char *xml_showlogs = "showlogs";
61     char *xml_srcip = "srcip";
62     char *xml_user = "user";
63     char *xml_frequency = "frequency";
64     char *xml_email = "email_to";
65
66
67     monitor_config *mon_config = (monitor_config *)config;
68
69      
70     /* Getting any configured entry. */
71     if(mon_config->reports)
72     {
73         while(mon_config->reports[s])
74             s++;
75     }
76
77     
78     /* Allocating the memory for the config. */
79     os_realloc(mon_config->reports, (s + 2) * sizeof(report_config *), 
80                mon_config->reports);
81     os_calloc(1, sizeof(report_config), mon_config->reports[s]);
82     mon_config->reports[s + 1] = NULL;
83
84
85     /* Zeroing the elements. */
86     mon_config->reports[s]->title = NULL;
87     mon_config->reports[s]->args = NULL;
88     mon_config->reports[s]->relations = NULL;
89     mon_config->reports[s]->type = NULL;
90     mon_config->reports[s]->emailto = NULL;
91
92     mon_config->reports[s]->r_filter.group = NULL;
93     mon_config->reports[s]->r_filter.rule = NULL;
94     mon_config->reports[s]->r_filter.level = NULL;
95     mon_config->reports[s]->r_filter.location = NULL;
96     mon_config->reports[s]->r_filter.srcip = NULL;
97     mon_config->reports[s]->r_filter.user = NULL;
98     mon_config->reports[s]->r_filter.related_group = 0;
99     mon_config->reports[s]->r_filter.related_rule = 0;
100     mon_config->reports[s]->r_filter.related_level = 0;
101     mon_config->reports[s]->r_filter.related_location = 0;
102     mon_config->reports[s]->r_filter.related_srcip = 0;
103     mon_config->reports[s]->r_filter.related_user = 0;
104     mon_config->reports[s]->r_filter.report_name = NULL;
105     mon_config->reports[s]->r_filter.show_alerts = 0;
106
107
108     
109     /* Reading the XML. */
110     while(node[i])
111     {
112         if(!node[i]->element)
113         {
114             merror(XML_ELEMNULL, __local_name);
115             return(OS_INVALID);
116         }
117         else if(!node[i]->content)
118         {
119             merror(XML_VALUENULL, __local_name, node[i]->element);
120             return(OS_INVALID);
121         }
122         else if(strcmp(node[i]->element, xml_title) == 0)
123         {
124             if(!mon_config->reports[s]->title)
125             {
126                 os_strdup(node[i]->content, mon_config->reports[s]->title);
127             }
128         }
129         else if(strcmp(node[i]->element, xml_type) == 0)
130         {
131             if(strcmp(node[i]->content, "email") == 0)
132             {
133                 if(!mon_config->reports[s]->type)
134                 {
135                     os_strdup(node[i]->content, mon_config->reports[s]->type);
136                 }
137             }
138             else
139             {
140                 merror(XML_VALUEERR, __local_name,node[i]->element,node[i]->content);
141             }
142         }
143         else if(strcmp(node[i]->element, xml_frequency) == 0)
144         {
145         }
146         else if(strcmp(node[i]->element, xml_showlogs) == 0)
147         {
148             if(strcasecmp(node[i]->content, "yes") == 0)
149             {
150                 mon_config->reports[s]->r_filter.show_alerts = 1;
151             }
152         }
153         else if(strcmp(node[i]->element, xml_categories) == 0)
154         {
155             char *ncat = NULL;
156             _filter_arg(node[i]->content);
157
158
159             os_strdup(node[i]->content, ncat);
160
161             if(os_report_configfilter("group", ncat,
162                                       &mon_config->reports[s]->r_filter, REPORT_FILTER) < 0)
163             {
164                 merror(CONFIG_ERROR, __local_name, "user argument");
165             }
166         }
167         else if((strcmp(node[i]->element, xml_group) == 0)||
168                 (strcmp(node[i]->element, xml_rule) == 0)||
169                 (strcmp(node[i]->element, xml_level) == 0)||
170                 (strcmp(node[i]->element, xml_location) == 0)||
171                 (strcmp(node[i]->element, xml_srcip) == 0)||
172                 (strcmp(node[i]->element, xml_user) == 0))
173         {
174             int reportf = REPORT_FILTER;
175             char *ncat = NULL;
176             _filter_arg(node[i]->content);
177
178             if(node[i]->attributes && node[i]->values)
179             {
180                 if(node[i]->attributes[0] && node[i]->values[0])
181                 {
182                     if(strcmp(node[i]->attributes[0], "type") == 0)
183                     {
184                         if(strcmp(node[i]->values[0], "relation") == 0)
185                         {
186                             reportf = REPORT_RELATED;
187                         }
188                         else
189                         {
190                             merror("%s: WARN: Invalid value for 'relation' attribute: '%s'. (ignored).", __local_name, node[i]->values[0]);
191                             i++;
192                             continue;
193                         }
194                     }
195                     else
196                     {
197                         merror("%s: WARN: Invalid attribute: %s (ignored). ", __local_name, node[i]->attributes[0]);
198                         i++;
199                         continue;
200                     }
201                 }
202             }
203
204             os_strdup(node[i]->content, ncat);
205
206             if(os_report_configfilter(node[i]->element, ncat, 
207                                       &mon_config->reports[s]->r_filter, reportf) < 0)
208             {
209                 merror("%s: Invalid filter: %s:%s (ignored).", __local_name, node[i]->element, node[i]->content);
210             }
211         }
212         else if(strcmp(node[i]->element, xml_email) == 0)
213         {
214             mon_config->reports[s]->emailto = os_AddStrArray(node[i]->content, mon_config->reports[s]->emailto);
215         }
216         else
217         {
218             merror(XML_INVELEM, __local_name, node[i]->element);
219             return(OS_INVALID);
220         }
221         i++;
222     }
223
224
225     /* Setting proper report type. */
226     mon_config->reports[s]->r_filter.report_type = REPORT_TYPE_DAILY;
227
228     if(mon_config->reports[s]->emailto == NULL)
229     {
230         if(mon_config->reports[s]->title)
231             merror("%s: No \"email to\" configured for the report '%s'. Ignoring it.", __local_name, mon_config->reports[s]->title);
232         else
233             merror("%s: No \"email to\" and title configured for report. Ignoring it.", __local_name);    
234     }
235
236     if(!mon_config->reports[s]->title)
237     {
238         os_strdup("OSSEC Report (unnamed)", mon_config->reports[s]->title);
239     }
240     mon_config->reports[s]->r_filter.report_name = mon_config->reports[s]->title;
241
242     return(0);
243 }
244
245
246 /* EOF */