Imported Upstream version 2.7
[ossec-hids.git] / src / config / email-alerts-config.c
1 /* @(#) $Id: ./src/config/email-alerts-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 "mail-config.h"
19
20
21 int Read_EmailAlerts(XML_NODE node, void *configp, void *mailp)
22 {
23     int i = 0;
24     int granto_size = 1;
25
26     /* XML definitions */
27     char *xml_email_to = "email_to";
28     char *xml_email_format = "format";
29     char *xml_email_level = "level";
30     char *xml_email_id = "rule_id";
31     char *xml_email_group = "group";
32     char *xml_email_location = "event_location";
33     char *xml_email_donotdelay = "do_not_delay";
34     char *xml_email_donotgroup = "do_not_group";
35
36     MailConfig *Mail;
37
38     Mail = (MailConfig *)mailp;
39     if(!Mail)
40     {
41         return(0);
42     }
43
44
45     /* Getting Granular mail_to size */
46     if(Mail && Mail->gran_to)
47     {
48         char **ww;
49         ww = Mail->gran_to;
50         while(*ww != NULL)
51         {
52             ww++;
53             granto_size++;
54         }
55     }
56
57
58     if(Mail)
59     {
60         os_realloc(Mail->gran_to,
61                    sizeof(char *)*(granto_size +1), Mail->gran_to);
62         os_realloc(Mail->gran_id,
63                    sizeof(int *)*(granto_size +1), Mail->gran_id);
64         os_realloc(Mail->gran_level,
65                    sizeof(int)*(granto_size +1), Mail->gran_level);
66         os_realloc(Mail->gran_set,
67                    sizeof(int)*(granto_size +1), Mail->gran_set);
68         os_realloc(Mail->gran_format,
69                    sizeof(int)*(granto_size +1), Mail->gran_format);
70         os_realloc(Mail->gran_location,
71                    sizeof(OSMatch)*(granto_size +1), Mail->gran_location);
72         os_realloc(Mail->gran_group,
73                    sizeof(OSMatch)*(granto_size +1), Mail->gran_group);
74
75         Mail->gran_to[granto_size -1] = NULL;
76         Mail->gran_to[granto_size] = NULL;
77
78         Mail->gran_id[granto_size -1] = NULL;
79         Mail->gran_id[granto_size] = NULL;
80
81         Mail->gran_location[granto_size -1] = NULL;
82         Mail->gran_location[granto_size] = NULL;
83
84         Mail->gran_group[granto_size -1] = NULL;
85         Mail->gran_group[granto_size] = NULL;
86
87         Mail->gran_level[granto_size -1] = 0;
88         Mail->gran_level[granto_size] = 0;
89
90         Mail->gran_format[granto_size -1] = FULL_FORMAT;
91         Mail->gran_format[granto_size] = FULL_FORMAT;
92
93         Mail->gran_set[granto_size -1] = 0;
94         Mail->gran_set[granto_size] = 0;
95     }
96
97
98     while(node[i])
99     {
100         if(!node[i]->element)
101         {
102             merror(XML_ELEMNULL, ARGV0);
103             return(OS_INVALID);
104         }
105         else if(!node[i]->content)
106         {
107             merror(XML_VALUENULL, ARGV0, node[i]->element);
108             return(OS_INVALID);
109         }
110         /* Mail notification */
111         else if(strcmp(node[i]->element, xml_email_level) == 0)
112         {
113             if(!OS_StrIsNum(node[i]->content))
114             {
115                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
116                 return(OS_INVALID);
117             }
118
119             Mail->gran_level[granto_size -1] = atoi(node[i]->content);
120         }
121         else if(strcmp(node[i]->element, xml_email_to) == 0)
122         {
123             os_strdup(node[i]->content, Mail->gran_to[granto_size -1]);
124         }
125         else if(strcmp(node[i]->element, xml_email_id) == 0)
126         {
127             int r_id = 0;
128             char *str_pt = node[i]->content;
129
130             while(*str_pt != '\0')
131             {
132                 /* We allow spaces in between */
133                 if(*str_pt == ' ')
134                 {
135                     str_pt++;
136                     continue;
137                 }
138
139                 /* If is digit, we get the value
140                  * and search for the next digit
141                  * available
142                  */
143                 else if(isdigit((int)*str_pt))
144                 {
145                     int id_i = 0;
146
147                     r_id = atoi(str_pt);
148                     debug1("%s: DEBUG: Adding '%d' to granular e-mail",
149                            ARGV0, r_id);
150
151                     if(!Mail->gran_id[granto_size -1])
152                     {
153                         os_calloc(2,sizeof(int),Mail->gran_id[granto_size -1]);
154                         Mail->gran_id[granto_size -1][0] = 0;
155                         Mail->gran_id[granto_size -1][1] = 0;
156                     }
157                     else
158                     {
159                         while(Mail->gran_id[granto_size -1][id_i] != 0)
160                         {
161                             id_i++;
162                         }
163
164                         os_realloc(Mail->gran_id[granto_size -1],
165                                    (id_i +2) * sizeof(int),
166                                    Mail->gran_id[granto_size -1]);
167                         Mail->gran_id[granto_size -1][id_i +1] = 0;
168                     }
169                     Mail->gran_id[granto_size -1][id_i] = r_id;
170
171
172                     str_pt = strchr(str_pt, ',');
173                     if(str_pt)
174                     {
175                         str_pt++;
176                     }
177                     else
178                     {
179                         break;
180                     }
181                 }
182
183                 /* Checking for duplicate commas */
184                 else if(*str_pt == ',')
185                 {
186                     str_pt++;
187                     continue;
188                 }
189
190                 else
191                 {
192                     break;
193                 }
194             }
195
196         }
197         else if(strcmp(node[i]->element, xml_email_format) == 0)
198         {
199             if(strcmp(node[i]->content, "sms") == 0)
200             {
201                 Mail->gran_format[granto_size -1] = SMS_FORMAT;
202             }
203             else if(strcmp(node[i]->content, "default") == 0)
204             {
205                 /* Default is full format */
206             }
207             else
208             {
209                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
210                 return(OS_INVALID);
211             }
212         }
213         else if(strcmp(node[i]->element, xml_email_donotdelay) == 0)
214         {
215             if((Mail->gran_format[granto_size -1] != SMS_FORMAT) &&
216                (Mail->gran_format[granto_size -1] != DONOTGROUP))
217             {
218                 Mail->gran_format[granto_size -1] = FORWARD_NOW;
219             }
220         }
221         else if(strcmp(node[i]->element, xml_email_donotgroup) == 0)
222         {
223             if(Mail->gran_format[granto_size -1] != SMS_FORMAT)
224             {
225                 Mail->gran_format[granto_size -1] = DONOTGROUP;
226             }
227         }
228         else if(strcmp(node[i]->element, xml_email_location) == 0)
229         {
230             os_calloc(1, sizeof(OSMatch),Mail->gran_location[granto_size -1]);
231             if(!OSMatch_Compile(node[i]->content,
232                                 Mail->gran_location[granto_size -1], 0))
233             {
234                 merror(REGEX_COMPILE, ARGV0, node[i]->content,
235                         Mail->gran_location[granto_size -1]->error);
236                 return(-1);
237             }
238         }
239         else if(strcmp(node[i]->element, xml_email_group) == 0)
240         {
241             os_calloc(1, sizeof(OSMatch),Mail->gran_group[granto_size -1]);
242             if(!OSMatch_Compile(node[i]->content,
243                                 Mail->gran_group[granto_size -1], 0))
244             {
245                 merror(REGEX_COMPILE, ARGV0, node[i]->content,
246                         Mail->gran_group[granto_size -1]->error);
247                 return(-1);
248             }
249         }
250         else
251         {
252             merror(XML_INVELEM, ARGV0, node[i]->element);
253             return(OS_INVALID);
254         }
255         i++;
256     }
257
258     /* We must have at least one entry set */
259     if((Mail->gran_location[granto_size -1] == NULL &&
260        Mail->gran_level[granto_size -1] == 0 &&
261        Mail->gran_group[granto_size -1] == NULL &&
262        Mail->gran_id[granto_size -1] == NULL &&
263        Mail->gran_format[granto_size -1] == FULL_FORMAT) ||
264        Mail->gran_to[granto_size -1] == NULL)
265        {
266            merror(XML_INV_GRAN_MAIL, ARGV0);
267            return(OS_INVALID);
268        }
269
270     return(0);
271 }
272
273
274 /* EOF */