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