c0cda8908a1a9d91e2971ad89226c778b8cb147c
[ossec-hids.git] / src / config / 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 /* 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 *osreports = "reports";
44     char *osactive_response = "active-response";
45
46     
47     while(node[i])
48     {
49         XML_NODE chld_node = NULL;
50         
51         chld_node = OS_GetElementsbyNode(&xml,node[i]);
52
53         if(!node[i]->element)
54         {
55             merror(XML_ELEMNULL, ARGV0);
56             return(OS_INVALID);
57         }
58         else if(!chld_node)
59         {
60             merror(XML_INVELEM, ARGV0, node[i]->element);
61             return(OS_INVALID);
62         }
63         else if(strcmp(node[i]->element, osglobal) == 0)
64         {
65             if(((modules & CGLOBAL) || (modules & CMAIL)) 
66                 && (Read_Global(chld_node, d1, d2) < 0))
67                 return(OS_INVALID);
68         }
69         else if(strcmp(node[i]->element, osemailalerts) == 0)
70         {
71             if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
72                 return(OS_INVALID);
73         }
74         else if(strcmp(node[i]->element, osdbd) == 0)
75         {
76             if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
77                 return(OS_INVALID);
78         }
79         else if(strcmp(node[i]->element, oscsyslogd) == 0)
80         {
81             if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
82                 return(OS_INVALID);
83         }
84         else if(strcmp(node[i]->element, oscagentless) == 0)
85         {
86             if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
87                 return(OS_INVALID);
88         }
89         else if(strcmp(node[i]->element, osrules) == 0)
90         {
91             if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
92                 return(OS_INVALID);
93         }
94         else if(strcmp(node[i]->element, ossyscheck) == 0)
95         {
96             if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
97                 return(OS_INVALID);
98             if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
99                 return(OS_INVALID);    
100         }
101         else if(strcmp(node[i]->element, osrootcheck) == 0)
102         {
103             if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
104                 return(OS_INVALID);
105         }
106         else if(strcmp(node[i]->element, osalerts) == 0)
107         {
108             if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
109                 return(OS_INVALID);
110         }
111         else if(strcmp(node[i]->element, oslocalfile) == 0)
112         {
113             if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
114                 return(OS_INVALID);
115         }
116         else if(strcmp(node[i]->element, osremote) == 0)
117         {
118             if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
119                 return(OS_INVALID);
120         }
121         else if(strcmp(node[i]->element, osclient) == 0)
122         {
123             if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
124                 return(OS_INVALID);
125         }
126         else if(strcmp(node[i]->element, oscommand) == 0)
127         {
128             if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
129                 return(OS_INVALID);
130         }
131         else if(strcmp(node[i]->element, osactive_response) == 0)
132         {
133             if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
134                 return(OS_INVALID);
135         }
136         else if(strcmp(node[i]->element, osreports) == 0)
137         {
138             if((modules & CREPORTS)&&(Read_CReports(chld_node, d1, d2)<0))
139                 return(OS_INVALID);
140         }
141         else
142         {
143             merror(XML_INVELEM, ARGV0, node[i]->element);
144             return(OS_INVALID);
145         }
146         
147         //printf("before\n");
148         OS_ClearNode(chld_node);
149         //printf("after\n");
150         i++;
151     }
152
153     return(0);
154 }
155
156
157 /* ReadConfig(int modules, char *cfgfile)
158  * Read the config files
159  */
160 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2) 
161 {
162     int i;
163     OS_XML xml;
164     XML_NODE node;
165
166
167     /** XML definitions **/
168     /* Global */
169     char *xml_start_ossec = "ossec_config";
170     char *xml_start_agent = "agent_config";
171
172     char *xml_agent_name = "name";
173     char *xml_agent_os = "os";
174     char *xml_agent_overwrite = "overwrite";
175     
176
177     if(OS_ReadXML(cfgfile,&xml) < 0)
178     {
179         if(modules & CAGENT_CONFIG)
180         {
181             #ifndef CLIENT
182             merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
183             #endif
184         }
185         else
186         {
187             merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
188         }
189         return(OS_INVALID);
190     }
191     
192
193     node = OS_GetElementsbyNode(&xml, NULL);
194     if(!node)
195     {
196         return(0);
197     }
198
199
200     /* Reading the main configuration */
201     i = 0;
202     while(node[i])
203     {
204         if(!node[i]->element)
205         {
206             merror(XML_ELEMNULL, ARGV0);
207             return(OS_INVALID);
208         }
209         else if(!(modules & CAGENT_CONFIG) &&
210                 (strcmp(node[i]->element, xml_start_ossec) == 0))
211         {
212             XML_NODE chld_node = NULL;
213             chld_node = OS_GetElementsbyNode(&xml,node[i]);
214
215             /* Main element does not need to have any child */
216             if(chld_node)
217             {
218                 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
219                 {
220                     merror(CONFIG_ERROR, ARGV0, cfgfile);
221                     return(OS_INVALID);
222                 }
223
224                 OS_ClearNode(chld_node);    
225             }
226         }
227         else if((modules & CAGENT_CONFIG) &&
228                 (strcmp(node[i]->element, xml_start_agent) == 0))
229         {
230             int passed_agent_test = 1;
231             int attrs = 0;
232             XML_NODE chld_node = NULL;
233             chld_node = OS_GetElementsbyNode(&xml,node[i]);
234
235
236             /* Checking if this is specific to any agent. */
237             if(node[i]->attributes && node[i]->values)
238             {    
239                 while(node[i]->attributes[attrs] && node[i]->values[attrs])
240                 {
241                     if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
242                     {
243                         #ifdef CLIENT
244                         char *agentname = os_read_agent_name();
245
246                         if(!agentname)
247                         {
248                             passed_agent_test = 0;
249                         }
250                         else
251                         {
252                             if(!OS_Match2(node[i]->values[attrs], agentname))
253                             {
254                                 passed_agent_test = 0;
255                             }
256                             free(agentname);
257                         }
258                         #endif
259                     }
260                     else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
261                     {
262                         #ifdef CLIENT
263                         char *agentos = getuname();
264
265                         if(agentos)
266                         {
267                             if(!OS_Match2(node[i]->values[attrs], agentos))
268                             {
269                                 passed_agent_test = 0;
270                             }
271                             free(agentos);
272                         }
273                         else
274                         {
275                             passed_agent_test = 0;
276                             merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
277                         }
278                         #endif
279                     }
280                     else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
281                     {
282                     }
283                     else
284                     {
285                         merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
286                                 cfgfile);
287                     }
288                     attrs++;
289                 }
290             }
291
292             
293             /* Main element does not need to have any child */
294             if(chld_node)
295             {
296                 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
297                 {
298                     merror(CONFIG_ERROR, ARGV0, cfgfile);
299                     return(OS_INVALID);
300                 }
301
302                 OS_ClearNode(chld_node);    
303             }
304         }
305         else
306         {
307             merror(XML_INVELEM, ARGV0, node[i]->element);
308             return(OS_INVALID);
309         }
310         i++;
311     }
312     
313     /* Clearing node and xml */
314     OS_ClearNode(node);
315     OS_ClearXML(&xml);  
316     return(0);
317 }
318
319
320
321 /* EOF */