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