Imported Upstream version 2.7
[ossec-hids.git] / src / config / config.c
1 /* @(#) $Id: ./src/config/config.c, 2011/11/01 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 /* Unified function to read the config.
14  *
15  */
16
17
18 #include "shared.h"
19 #include "os_xml/os_xml.h"
20 #include "config.h"
21
22
23 /* Read the main elements of the configuration.
24  */
25 int read_main_elements(OS_XML xml, int modules,
26                                    XML_NODE node,
27                                    void *d1,
28                                    void *d2)
29 {
30     int i = 0;
31     char *osglobal = "global";                    /*Server Config*/
32     char *osrules = "rules";                      /*Server Config*/
33     char *ossyscheck = "syscheck";                /*Agent Config*/
34     char *osrootcheck = "rootcheck";              /*Agent Config*/
35     char *osalerts = "alerts";                    /*Server Config*/
36     char *osemailalerts = "email_alerts";         /*Server Config*/
37     char *osdbd = "database_output";              /*Server Config*/
38     char *oscsyslogd = "syslog_output";           /*Server Config*/
39     char *oscagentless = "agentless";             /*Server Config*/
40     char *oslocalfile = "localfile";              /*Agent Config*/
41     char *osremote = "remote";                    /*Agent Config*/
42     char *osclient = "client";                    /*Agent Config*/
43     char *oscommand = "command";                  /*? Config*/
44     char *osreports = "reports";                  /*Server Config*/
45     char *osactive_response = "active-response";  /*Agent Config*/
46
47
48     while(node[i])
49     {
50         XML_NODE chld_node = NULL;
51
52         chld_node = OS_GetElementsbyNode(&xml,node[i]);
53
54         if(!node[i]->element)
55         {
56             merror(XML_ELEMNULL, ARGV0);
57             return(OS_INVALID);
58         }
59         else if(!chld_node)
60         {
61             merror(XML_INVELEM, ARGV0, node[i]->element);
62             return(OS_INVALID);
63         }
64         else if(strcmp(node[i]->element, osglobal) == 0)
65         {
66             if(((modules & CGLOBAL) || (modules & CMAIL))
67                 && (Read_Global(chld_node, d1, d2) < 0))
68                 return(OS_INVALID);
69         }
70         else if(strcmp(node[i]->element, osemailalerts) == 0)
71         {
72             if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
73                 return(OS_INVALID);
74         }
75         else if(strcmp(node[i]->element, osdbd) == 0)
76         {
77             if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
78                 return(OS_INVALID);
79         }
80         else if(strcmp(node[i]->element, oscsyslogd) == 0)
81         {
82             if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
83                 return(OS_INVALID);
84         }
85         else if(strcmp(node[i]->element, oscagentless) == 0)
86         {
87             if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
88                 return(OS_INVALID);
89         }
90         else if(strcmp(node[i]->element, osrules) == 0)
91         {
92             if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
93                 return(OS_INVALID);
94         }
95         else if(strcmp(node[i]->element, ossyscheck) == 0)
96         {
97             if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
98                 return(OS_INVALID);
99             if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
100                 return(OS_INVALID);
101         }
102         else if(strcmp(node[i]->element, osrootcheck) == 0)
103         {
104             if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
105                 return(OS_INVALID);
106         }
107         else if(strcmp(node[i]->element, osalerts) == 0)
108         {
109             if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
110                 return(OS_INVALID);
111         }
112         else if(strcmp(node[i]->element, oslocalfile) == 0)
113         {
114             if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
115                 return(OS_INVALID);
116         }
117         else if(strcmp(node[i]->element, osremote) == 0)
118         {
119             if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
120                 return(OS_INVALID);
121         }
122         else if(strcmp(node[i]->element, osclient) == 0)
123         {
124             if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
125                 return(OS_INVALID);
126         }
127         else if(strcmp(node[i]->element, oscommand) == 0)
128         {
129             if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
130                 return(OS_INVALID);
131         }
132         else if(strcmp(node[i]->element, osactive_response) == 0)
133         {
134             if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
135                 return(OS_INVALID);
136         }
137         else if(strcmp(node[i]->element, osreports) == 0)
138         {
139             if((modules & CREPORTS)&&(Read_CReports(chld_node, d1, d2)<0))
140                 return(OS_INVALID);
141         }
142         else
143         {
144             merror(XML_INVELEM, ARGV0, node[i]->element);
145             return(OS_INVALID);
146         }
147
148         //printf("before\n");
149         OS_ClearNode(chld_node);
150         //printf("after\n");
151         i++;
152     }
153
154     return(0);
155 }
156
157
158 /* ReadConfig(int modules, char *cfgfile)
159  * Read the config files
160  */
161 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
162 {
163     int i;
164     OS_XML xml;
165     XML_NODE node;
166
167
168     /** XML definitions **/
169     /* Global */
170     char *xml_start_ossec = "ossec_config";
171     char *xml_start_agent = "agent_config";
172
173     /* Attributes of the <agent_config> tag */
174     char *xml_agent_name = "name";
175     char *xml_agent_os = "os";
176     char *xml_agent_overwrite = "overwrite";
177     /* cmoraes */
178     char *xml_agent_profile = "profile";
179
180
181     if(OS_ReadXML(cfgfile,&xml) < 0)
182     {
183         if(modules & CAGENT_CONFIG)
184         {
185             #ifndef CLIENT
186             merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
187             #endif
188         }
189         else
190         {
191             merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
192         }
193         return(OS_INVALID);
194     }
195
196
197     node = OS_GetElementsbyNode(&xml, NULL);
198     if(!node)
199     {
200         return(0);
201     }
202
203
204     /* Reading the main configuration */
205     i = 0;
206     while(node[i])
207     {
208         if(!node[i]->element)
209         {
210             merror(XML_ELEMNULL, ARGV0);
211             return(OS_INVALID);
212         }
213         else if(!(modules & CAGENT_CONFIG) &&
214                 (strcmp(node[i]->element, xml_start_ossec) == 0))
215         {
216             XML_NODE chld_node = NULL;
217             chld_node = OS_GetElementsbyNode(&xml,node[i]);
218
219             /* Main element does not need to have any child */
220             if(chld_node)
221             {
222                 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
223                 {
224                     merror(CONFIG_ERROR, ARGV0, cfgfile);
225                     return(OS_INVALID);
226                 }
227
228                 OS_ClearNode(chld_node);
229             }
230         }
231         else if((modules & CAGENT_CONFIG) &&
232                 (strcmp(node[i]->element, xml_start_agent) == 0))
233         {
234             int passed_agent_test = 1;
235             int attrs = 0;
236             XML_NODE chld_node = NULL;
237             chld_node = OS_GetElementsbyNode(&xml,node[i]);
238
239
240             /* Checking if this is specific to any agent. */
241             if(node[i]->attributes && node[i]->values)
242             {
243                 while(node[i]->attributes[attrs] && node[i]->values[attrs])
244                 {
245                     /* Checking if there is an "name=" attribute */
246                     if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
247                     {
248                         #ifdef CLIENT
249                         char *agentname = os_read_agent_name();
250
251                         if(!agentname)
252                         {
253                             passed_agent_test = 0;
254                         }
255                         else
256                         {
257                             if(!OS_Match2(node[i]->values[attrs], agentname))
258                             {
259                                 passed_agent_test = 0;
260                             }
261                             free(agentname);
262                         }
263                         #endif
264                     }
265                     else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
266                     {
267                         #ifdef CLIENT
268                         char *agentos = getuname();
269
270                         if(agentos)
271                         {
272                             if(!OS_Match2(node[i]->values[attrs], agentos))
273                             {
274                                 passed_agent_test = 0;
275                             }
276                             free(agentos);
277                         }
278                         else
279                         {
280                             passed_agent_test = 0;
281                             merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
282                         }
283                         #endif
284                     }
285                     else if(strcmp(xml_agent_profile, node[i]->attributes[attrs]) == 0)
286                     {
287                         #ifdef CLIENT
288                         char *agentprofile = os_read_agent_profile();
289                         debug2("Read agent config profile name [%s]", agentprofile);
290
291                         if(!agentprofile)
292                         {
293                             passed_agent_test = 0;
294                         }
295                         else
296                         {
297                             /* match the profile name of this <agent_config> section
298                              * with a comma separated list of values in agent's
299                              * <config-profile> tag.
300                              */
301                             if(!OS_Match2(node[i]->values[attrs], agentprofile))
302                             {
303                                 passed_agent_test = 0;
304                                 debug2("[%s] did not match agent config profile name [%s]",
305                                        node[i]->values[attrs], agentprofile);
306                             }
307                             else
308                             {
309                                 debug2("Matched agent config profile name [%s]", agentprofile);
310                             }
311                             free(agentprofile);
312                         }
313                         #endif
314                     }
315                     /* cmoraes: end add */
316                     else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
317                     {
318                     }
319                     else
320                     {
321                         merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
322                                 cfgfile);
323                     }
324                     attrs++;
325                 }
326             }
327             #ifdef CLIENT
328             else
329             {
330                 debug2("agent_config element does not have any attributes.");
331
332                 /* if node does not have any attributes, it is a generic config block.
333                  * check if agent has a profile name
334                  * if agent does not have profile name, then only read this generic
335                  * agent_config block
336                  */
337
338                 if (!os_read_agent_profile())
339                 {
340                     debug2("but agent has a profile name.");
341                     passed_agent_test = 0;
342                 }
343             }
344             #endif
345
346             /* Main element does not need to have any child */
347             if(chld_node)
348             {
349                 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
350                 {
351                     merror(CONFIG_ERROR, ARGV0, cfgfile);
352                     return(OS_INVALID);
353                 }
354
355                 OS_ClearNode(chld_node);
356             }
357         }
358         else
359         {
360             merror(XML_INVELEM, ARGV0, node[i]->element);
361             return(OS_INVALID);
362         }
363         i++;
364     }
365
366     /* Clearing node and xml */
367     OS_ClearNode(node);
368     OS_ClearXML(&xml);  
369     return(0);
370 }
371
372
373
374 /* EOF */