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