Imported Upstream version 2.7
[ossec-hids.git] / src / config / global-config.c
1 /* @(#) $Id: ./src/config/global-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 "os_net/os_net.h"
19 #include "global-config.h"
20 #include "mail-config.h"
21
22
23 void AssignIgnore_Global(char **ignores, void *configp)
24 {
25     _Config *Config;
26
27     Config = (_Config *)configp;
28     if(Config)
29     {
30         Config->syscheck_ignore = ignores;
31     }
32 }
33
34
35 /* GlobalConfSK v0.1: 2006/04/26
36  * v0.1 Getting the ignore fields.
37  */
38 int Read_GlobalSK(XML_NODE node, void *configp, void *mailp)
39 {
40     int i = 0;
41     int ign_size = 1;
42     char *xml_ignore = "ignore";
43     char *xml_auto_ignore = "auto_ignore";
44     char *xml_alert_new_files = "alert_new_files";
45
46     _Config *Config;
47
48     Config = (_Config *)configp;
49
50
51     /* Shouldn't be here if !Config */
52     if(!Config)
53         return(0);
54
55
56     /* Getting right white_size */
57     if(Config && Config->syscheck_ignore)
58     {
59         char **ww;
60         ww = Config->syscheck_ignore;
61
62         while(*ww != NULL)
63         {
64             ign_size++;
65             ww++;
66         }
67     }
68
69     while(node[i])
70     {
71         if(!node[i]->element)
72         {
73             merror(XML_ELEMNULL, ARGV0);
74             return(OS_INVALID);
75         }
76         else if(!node[i]->content)
77         {
78             merror(XML_VALUENULL, ARGV0, node[i]->element);
79             return(OS_INVALID);
80         }
81         else if(strcmp(node[i]->element,xml_auto_ignore) == 0)
82         {
83             if(strcmp(node[i]->content, "yes") == 0)
84             {
85                 Config->syscheck_auto_ignore = 1;
86             }
87             else if(strcmp(node[i]->content, "no") == 0)
88             {
89                 Config->syscheck_auto_ignore = 0;
90             }
91             else
92             {
93                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
94                 return(OS_INVALID);
95             }
96         }
97         else if(strcmp(node[i]->element,xml_alert_new_files) == 0)
98         {
99             if(strcmp(node[i]->content, "yes") == 0)
100             {
101                 Config->syscheck_alert_new = 1;
102             }
103             else if(strcmp(node[i]->content, "no") == 0)
104             {
105                 Config->syscheck_alert_new = 0;
106             }
107             else
108             {
109                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
110                 return(OS_INVALID);
111             }
112         }
113         else if(strcmp(node[i]->element,xml_ignore) == 0)
114         {
115             ign_size++;
116             Config->syscheck_ignore =
117                 realloc(Config->syscheck_ignore, sizeof(char *)*ign_size);
118             if(!Config->syscheck_ignore)
119             {
120                 merror(MEM_ERROR, ARGV0);
121                 return(OS_INVALID);
122             }
123
124             os_strdup(node[i]->content,Config->syscheck_ignore[ign_size -2]);
125             Config->syscheck_ignore[ign_size -1] = NULL;
126         }
127         i++;
128     }
129
130     return(0);
131 }
132
133
134 /* GlobalConf v0.2: 2005/03/03
135  * v0.2: Changing to support the new OS_XML
136  */
137 int Read_Global(XML_NODE node, void *configp, void *mailp)
138 {
139     int i = 0;
140
141     /* White list size */
142     int white_size = 1;
143     int hostname_white_size = 1;
144     int mailto_size = 1;
145
146
147     /* XML definitions */
148     char *xml_mailnotify = "email_notification";
149     char *xml_logall = "logall";
150     char *xml_integrity = "integrity_checking";
151     char *xml_rootcheckd = "rootkit_detection";
152     char *xml_hostinfo = "host_information";
153     char *xml_picviz = "picviz_output";
154     char *xml_picviz_socket = "picviz_socket";
155     char *xml_prelude = "prelude_output";
156     char *xml_prelude_profile = "prelude_profile";
157     char *xml_prelude_log_level = "prelude_log_level";
158     char *xml_stats = "stats";
159     char *xml_memorysize = "memory_size";
160     char *xml_white_list = "white_list";
161     char *xml_compress_alerts = "compress_alerts";
162
163     char *xml_emailto = "email_to";
164     char *xml_emailfrom = "email_from";
165     char *xml_smtpserver = "smtp_server";
166     char *xml_mailmaxperhour = "email_maxperhour";
167
168 #ifdef GEOIP
169     /* GeoIP */
170     char *xml_geoip_db_path = "geoip_db_path";
171     char *xml_geoip6_db_path = "geoip6_db_path";
172 #endif
173
174     _Config *Config;
175     MailConfig *Mail;
176
177     Config = (_Config *)configp;
178     Mail = (MailConfig *)mailp;
179
180     /* Getting right white_size */
181     if(Config && Config->white_list)
182     {
183         os_ip **ww;
184         ww = Config->white_list;
185
186         while(*ww != NULL)
187         {
188             white_size++;
189             ww++;
190         }
191     }
192
193      /* Getting right white_size */
194     if(Config && Config->hostname_white_list)
195     {
196         OSMatch **ww;
197         ww = Config->hostname_white_list;
198
199         while(*ww != NULL)
200         {
201             hostname_white_size++;
202             ww++;
203         }
204     }
205
206     /* Getting mail_to size */
207     if(Mail && Mail->to)
208     {
209         char **ww;
210         ww = Mail->to;
211         while(*ww != NULL)
212         {
213             mailto_size++;
214             ww++;
215         }
216     }
217
218     while(node[i])
219     {
220         if(!node[i]->element)
221         {
222             merror(XML_ELEMNULL, ARGV0);
223             return(OS_INVALID);
224         }
225         else if(!node[i]->content)
226         {
227             merror(XML_VALUENULL, ARGV0, node[i]->element);
228             return(OS_INVALID);
229         }
230         /* Mail notification */
231         else if(strcmp(node[i]->element, xml_mailnotify) == 0)
232         {
233             if(strcmp(node[i]->content, "yes") == 0)
234             {
235                 if(Config) Config->mailnotify = 1;
236                 if(Mail) Mail->mn = 1;
237             }
238             else if(strcmp(node[i]->content, "no") == 0)
239             {
240                 if(Config) Config->mailnotify = 0;
241                 if(Mail) Mail->mn = 0;
242             }
243             else
244             {
245                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
246                 return(OS_INVALID);
247             }
248         }
249         /* Picviz support */
250         else if(strcmp(node[i]->element, xml_picviz) == 0)
251         {
252             if(strcmp(node[i]->content, "yes") == 0)
253             {
254                 if(Config) Config->picviz = 1;
255             }
256             else if(strcmp(node[i]->content, "no") == 0)
257             {
258                 if(Config) Config->picviz = 0;
259             }
260             else
261             {
262                 merror(XML_VALUEERR,ARGV0,node[i]->element, node[i]->content);
263                 return(OS_INVALID);
264             }
265         }
266         else if(strcmp(node[i]->element, xml_picviz_socket) == 0)
267         {
268             if(Config)
269             {
270                 os_strdup(node[i]->content, Config->picviz_socket);
271             }
272         }
273         /* Prelude support */
274         else if(strcmp(node[i]->element, xml_prelude) == 0)
275         {
276             if(strcmp(node[i]->content, "yes") == 0)
277             {
278                 if(Config) Config->prelude = 1;
279             }
280             else if(strcmp(node[i]->content, "no") == 0)
281             {
282                 if(Config) Config->prelude = 0;
283             }
284             else
285             {
286                 merror(XML_VALUEERR,ARGV0,node[i]->element, node[i]->content);
287                 return(OS_INVALID);
288             }
289         }
290         else if(strcmp(node[i]->element, xml_prelude_profile) == 0)
291         {
292             if(Config)
293             {
294                 Config->prelude_profile = strdup(node[i]->content);
295             }
296         }
297         else if(strcmp(node[i]->element, xml_prelude_log_level) == 0)
298         {
299             if(!OS_StrIsNum(node[i]->content))
300             {
301                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
302                 return(OS_INVALID);
303             }
304
305             if(Config)
306             {
307                 Config->prelude_log_level = atoi(node[i]->content);
308             }
309         }
310         /* Log all */
311         else if(strcmp(node[i]->element, xml_logall) == 0)
312         {
313             if(strcmp(node[i]->content, "yes") == 0)
314                 { if(Config) Config->logall = 1;}
315             else if(strcmp(node[i]->content, "no") == 0)
316                 {if(Config) Config->logall = 0;}
317             else
318             {
319                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
320                 return(OS_INVALID);
321             }
322         }
323         /* compress alerts */
324         else if(strcmp(node[i]->element, xml_compress_alerts) == 0)
325         {
326             /* removed from here -- compatility issues only */
327         }
328         /* Integrity */
329         else if(strcmp(node[i]->element, xml_integrity) == 0)
330         {
331             if(!OS_StrIsNum(node[i]->content))
332             {
333                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
334                 return(OS_INVALID);
335             }
336             if(Config)
337             {
338                 Config->integrity = atoi(node[i]->content);
339             }
340         }
341         /* rootcheck */
342         else if(strcmp(node[i]->element, xml_rootcheckd) == 0)
343         {
344             if(!OS_StrIsNum(node[i]->content))
345             {
346                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
347                 return(OS_INVALID);
348             }
349             if(Config)
350             {
351                 Config->rootcheck = atoi(node[i]->content);
352             }
353         }
354         /* hostinfo */
355         else if(strcmp(node[i]->element, xml_hostinfo) == 0)
356         {
357             if(!OS_StrIsNum(node[i]->content))
358             {
359                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
360                 return(OS_INVALID);
361             }
362             if(Config)
363             {
364                 Config->hostinfo = atoi(node[i]->content);
365             }
366         }
367         /* stats */
368         else if(strcmp(node[i]->element, xml_stats) == 0)
369         {
370             if(!OS_StrIsNum(node[i]->content))
371             {
372                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
373                 return(OS_INVALID);
374             }
375             if(Config)
376             {
377                 Config->stats = atoi(node[i]->content);
378             }
379         }
380         else if(strcmp(node[i]->element, xml_memorysize) == 0)
381         {
382             if(!OS_StrIsNum(node[i]->content))
383             {
384                 merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
385                 return(OS_INVALID);
386             }
387             if(Config)
388             {
389                 Config->memorysize = atoi(node[i]->content);
390             }
391         }
392         /* whitelist */
393         else if(strcmp(node[i]->element, xml_white_list) == 0)
394         {
395             /* Windows do not need it */
396             #ifndef WIN32
397
398             char *ip_address_regex =
399              "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/?"
400              "([0-9]{0,2}|[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})$";
401
402             if(Config && OS_PRegex(node[i]->content, ip_address_regex))
403             {
404                 white_size++;
405                 Config->white_list =
406                     realloc(Config->white_list, sizeof(os_ip *)*white_size);
407                 if(!Config->white_list)
408                 {
409                     merror(MEM_ERROR, ARGV0);
410                     return(OS_INVALID);
411                 }
412
413                 os_calloc(1, sizeof(os_ip), Config->white_list[white_size -2]);
414                 Config->white_list[white_size -1] = NULL;
415
416                 if(!OS_IsValidIP(node[i]->content,
417                                  Config->white_list[white_size -2]))
418                 {
419                     merror(INVALID_IP, ARGV0,
420                                        node[i]->content);
421                     return(OS_INVALID);
422                 }
423             }
424             /* Adding hostname */
425             else if(Config)
426             {
427                 hostname_white_size++;
428                 Config->hostname_white_list =
429                     realloc(Config->hostname_white_list,
430                     sizeof(OSMatch *)*hostname_white_size);
431
432                 if(!Config->hostname_white_list)
433                 {
434                     merror(MEM_ERROR, ARGV0);
435                     return(OS_INVALID);
436                 }
437                 os_calloc(1,
438                           sizeof(OSMatch),
439                           Config->hostname_white_list[hostname_white_size -2]);
440                 Config->hostname_white_list[hostname_white_size -1] = NULL;
441
442                 if(!OSMatch_Compile(
443                         node[i]->content,
444                         Config->hostname_white_list[hostname_white_size -2],
445                         0))
446                 {
447                     merror(REGEX_COMPILE, ARGV0, node[i]->content,
448                            Config->hostname_white_list
449                            [hostname_white_size -2]->error);
450                     return(-1);
451                 }
452             }
453
454             #endif
455
456         }
457
458         /* For the email now
459          * email_to, email_from, smtp_Server and maxperhour.
460          * We will use a separate structure for that.
461          */
462         else if(strcmp(node[i]->element, xml_emailto) == 0)
463         {
464             #ifndef WIN32
465             if(!OS_PRegex(node[i]->content, "[a-zA-Z0-9\\._-]+@[a-zA-Z0-9\\._-]"))
466             {
467                 merror("%s: ERROR: Invalid Email address: %s.", ARGV0, node[i]->content);
468                 return(OS_INVALID);
469             }
470             #endif
471
472             if(Mail)
473             {
474                 mailto_size++;
475                 Mail->to = realloc(Mail->to, sizeof(char *)*mailto_size);
476                 if(!Mail->to)
477                 {
478                     merror(MEM_ERROR, ARGV0);
479                     return(OS_INVALID);
480                 }
481
482                 os_strdup(node[i]->content, Mail->to[mailto_size - 2]);
483                 Mail->to[mailto_size - 1] = NULL;
484             }
485         }
486         else if(strcmp(node[i]->element, xml_emailfrom) == 0)
487         {
488             if(Mail)
489             {
490                 if(Mail->from)
491                 {
492                     free(Mail->from);
493                 }
494                 os_strdup(node[i]->content, Mail->from);
495             }
496         }
497         else if(strcmp(node[i]->element, xml_smtpserver) == 0)
498         {
499             #ifndef WIN32
500             if(Mail && (Mail->mn))
501             {
502                 Mail->smtpserver = OS_GetHost(node[i]->content, 5);
503                 if(!Mail->smtpserver)
504                 {
505                     merror(INVALID_SMTP, ARGV0, node[i]->content);
506                     return(OS_INVALID);
507                 }
508             }
509             #endif
510         }
511         else if(strcmp(node[i]->element, xml_mailmaxperhour) == 0)
512         {
513             if(Mail)
514             {
515                 if(!OS_StrIsNum(node[i]->content))
516                 {
517                    merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
518                    return(OS_INVALID);
519                 }
520                 Mail->maxperhour = atoi(node[i]->content);
521
522                 if((Mail->maxperhour <= 0) || (Mail->maxperhour > 9999))
523                 {
524                    merror(XML_VALUEERR,ARGV0,node[i]->element,node[i]->content);
525                    return(OS_INVALID);
526                 }
527             }
528         }
529 #ifdef GEOIP
530         /* GeoIP v4 DB location */
531         else if(strcmp(node[i]->element, xml_geoip_db_path) == 0)
532         {
533             if(Config)
534             {
535                 os_strdup(node[i]->content, Config->geoip_db_path);
536             }
537         }
538         /* GeoIP v6 DB location */
539         else if(strcmp(node[i]->element, xml_geoip6_db_path) == 0)
540         {
541             if(Config)
542             {
543                 os_strdup(node[i]->content, Config->geoip6_db_path);
544             }
545         }
546 #endif
547         else
548         {
549             merror(XML_INVELEM, ARGV0, node[i]->element);
550             return(OS_INVALID);
551         }
552         i++;
553     }
554
555     return(0);
556 }
557
558
559 /* EOF */