Imported Upstream version 2.3
[ossec-hids.git] / src / config / config.c
1 /* @(#) $Id: config.c,v 1.25 2009/08/27 18:17:41 dcid Exp $ */
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 3) as published by the FSF - Free Software
9  * Foundation
10  */
11
12 /* Unified function to read the config.
13  *
14  */
15
16
17 #include "shared.h"
18 #include "os_xml/os_xml.h"
19 #include "config.h"
20
21
22 /* Read the main elements of the configuration.
23  */
24 int read_main_elements(OS_XML xml, int modules, 
25                                    XML_NODE node, 
26                                    void *d1, 
27                                    void *d2)
28 {
29     int i = 0;
30     char *osglobal = "global";
31     char *osrules = "rules";
32     char *ossyscheck = "syscheck";
33     char *osrootcheck = "rootcheck";
34     char *osalerts = "alerts";
35     char *osemailalerts = "email_alerts";
36     char *osdbd = "database_output";
37     char *oscsyslogd = "syslog_output";
38     char *oscagentless = "agentless";
39     char *oslocalfile = "localfile";
40     char *osremote = "remote";
41     char *osclient = "client";
42     char *oscommand = "command";
43     char *osactive_response = "active-response";
44
45     
46     while(node[i])
47     {
48         XML_NODE chld_node = NULL;
49         
50         chld_node = OS_GetElementsbyNode(&xml,node[i]);
51         
52         if(!node[i]->element)
53         {
54             merror(XML_ELEMNULL, ARGV0);
55             return(OS_INVALID);
56         }
57         else if(!chld_node)
58         {
59             merror(XML_INVELEM, ARGV0, node[i]->element);
60             return(OS_INVALID);
61         }
62         else if(strcmp(node[i]->element, osglobal) == 0)
63         {
64             if(((modules & CGLOBAL) || (modules & CMAIL)) 
65                 && (Read_Global(chld_node, d1, d2) < 0))
66                 return(OS_INVALID);
67         }
68         else if(strcmp(node[i]->element, osemailalerts) == 0)
69         {
70             if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
71                 return(OS_INVALID);
72         }
73         else if(strcmp(node[i]->element, osdbd) == 0)
74         {
75             if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
76                 return(OS_INVALID);
77         }
78         else if(strcmp(node[i]->element, oscsyslogd) == 0)
79         {
80             if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
81                 return(OS_INVALID);
82         }
83         else if(strcmp(node[i]->element, oscagentless) == 0)
84         {
85             if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
86                 return(OS_INVALID);
87         }
88         else if(strcmp(node[i]->element, osrules) == 0)
89         {
90             if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
91                 return(OS_INVALID);
92         }
93         else if(strcmp(node[i]->element, ossyscheck) == 0)
94         {
95             if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
96                 return(OS_INVALID);
97             if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
98                 return(OS_INVALID);    
99         }
100         else if(strcmp(node[i]->element, osrootcheck) == 0)
101         {
102             if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
103                 return(OS_INVALID);
104         }
105         else if(strcmp(node[i]->element, osalerts) == 0)
106         {
107             if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
108                 return(OS_INVALID);
109         }
110         else if(strcmp(node[i]->element, oslocalfile) == 0)
111         {
112             if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
113                 return(OS_INVALID);
114         }
115         else if(strcmp(node[i]->element, osremote) == 0)
116         {
117             if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
118                 return(OS_INVALID);
119         }
120         else if(strcmp(node[i]->element, osclient) == 0)
121         {
122             if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
123                 return(OS_INVALID);
124         }
125         else if(strcmp(node[i]->element, oscommand) == 0)
126         {
127             if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
128                 return(OS_INVALID);
129         }
130         else if(strcmp(node[i]->element, osactive_response) == 0)
131         {
132             if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
133                 return(OS_INVALID);
134         }
135         else
136         {
137             merror(XML_INVELEM, ARGV0, node[i]->element);
138             return(OS_INVALID);
139         }
140         
141         OS_ClearNode(chld_node);
142         i++;
143     }
144
145     return(0);
146 }
147
148
149 /* ReadConfig(int modules, char *cfgfile)
150  * Read the config files
151  */
152 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2) 
153 {
154     int i;
155     OS_XML xml;
156     XML_NODE node;
157
158
159     /** XML definitions **/
160     /* Global */
161     char *xml_start_ossec = "ossec_config";
162     char *xml_start_agent = "agent_config";
163
164     char *xml_agent_name = "name";
165     char *xml_agent_os = "os";
166     char *xml_agent_overwrite = "overwrite";
167     
168
169     if(OS_ReadXML(cfgfile,&xml) < 0)
170     {
171         if(modules & CAGENT_CONFIG)
172         {
173         }
174         else
175         {
176             merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
177         }
178         return(OS_INVALID);
179     }
180     
181
182     node = OS_GetElementsbyNode(&xml, NULL);
183     if(!node)
184     {
185         return(0);
186     }
187
188
189     /* Reading the main configuration */
190     i = 0;
191     while(node[i])
192     {
193         if(!node[i]->element)
194         {
195             merror(XML_ELEMNULL, ARGV0);
196             return(OS_INVALID);
197         }
198         else if(!(modules & CAGENT_CONFIG) &&
199                 (strcmp(node[i]->element, xml_start_ossec) == 0))
200         {
201             XML_NODE chld_node = NULL;
202             chld_node = OS_GetElementsbyNode(&xml,node[i]);
203
204             /* Main element does not need to have any child */
205             if(chld_node)
206             {
207                 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
208                 {
209                     merror(CONFIG_ERROR, ARGV0, cfgfile);
210                     return(OS_INVALID);
211                 }
212
213                 OS_ClearNode(chld_node);    
214             }
215         }
216         else if((modules & CAGENT_CONFIG) &&
217                 (strcmp(node[i]->element, xml_start_agent) == 0))
218         {
219             int passed_agent_test = 1;
220             int attrs = 0;
221             XML_NODE chld_node = NULL;
222             chld_node = OS_GetElementsbyNode(&xml,node[i]);
223
224
225             /* Checking if this is specific to any agent. */
226             if(node[i]->attributes && node[i]->values)
227             {    
228                 while(node[i]->attributes[attrs] && node[i]->values[attrs])
229                 {
230                     if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
231                     {
232                         char *agentname = os_read_agent_name();
233
234                         if(!agentname)
235                         {
236                             passed_agent_test = 0;
237                         }
238                         else
239                         {
240                             if(!OS_Match2(node[i]->values[attrs], agentname))
241                             {
242                                 passed_agent_test = 0;
243                             }
244                             free(agentname);
245                         }
246                     }
247                     else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
248                     {
249                         char *agentos = getuname();
250
251                         if(agentos)
252                         {
253                             if(!OS_Match2(node[i]->values[attrs], agentos))
254                             {
255                                 passed_agent_test = 0;
256                             }
257                             free(agentos);
258                         }
259                         else
260                         {
261                             passed_agent_test = 0;
262                             merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
263                         }
264                     }
265                     else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
266                     {
267                     }
268                     else
269                     {
270                         merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
271                                 cfgfile);
272                     }
273                     attrs++;
274                 }
275             }
276
277             
278             /* Main element does not need to have any child */
279             if(chld_node)
280             {
281                 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
282                 {
283                     merror(CONFIG_ERROR, ARGV0, cfgfile);
284                     return(OS_INVALID);
285                 }
286
287                 OS_ClearNode(chld_node);    
288             }
289         }
290         else
291         {
292             merror(XML_INVELEM, ARGV0, node[i]->element);
293             return(OS_INVALID);
294         }
295         i++;
296     }
297     
298     /* Clearing node and xml */
299     OS_ClearNode(node);
300     OS_ClearXML(&xml);  
301     return(0);
302 }
303
304
305
306 /* EOF */