3 /* Copyright (C) 2009 Trend Micro Inc.
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
12 /* Unified function to read the config.
18 #include "os_xml/os_xml.h"
22 /* Read the main elements of the configuration.
24 int read_main_elements(OS_XML xml, int modules,
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";
49 XML_NODE chld_node = NULL;
51 chld_node = OS_GetElementsbyNode(&xml,node[i]);
55 merror(XML_ELEMNULL, ARGV0);
60 merror(XML_INVELEM, ARGV0, node[i]->element);
63 else if(strcmp(node[i]->element, osglobal) == 0)
65 if(((modules & CGLOBAL) || (modules & CMAIL))
66 && (Read_Global(chld_node, d1, d2) < 0))
69 else if(strcmp(node[i]->element, osemailalerts) == 0)
71 if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
74 else if(strcmp(node[i]->element, osdbd) == 0)
76 if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
79 else if(strcmp(node[i]->element, oscsyslogd) == 0)
81 if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
84 else if(strcmp(node[i]->element, oscagentless) == 0)
86 if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
89 else if(strcmp(node[i]->element, osrules) == 0)
91 if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
94 else if(strcmp(node[i]->element, ossyscheck) == 0)
96 if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
98 if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
101 else if(strcmp(node[i]->element, osrootcheck) == 0)
103 if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
106 else if(strcmp(node[i]->element, osalerts) == 0)
108 if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
111 else if(strcmp(node[i]->element, oslocalfile) == 0)
113 if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
116 else if(strcmp(node[i]->element, osremote) == 0)
118 if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
121 else if(strcmp(node[i]->element, osclient) == 0)
123 if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
126 else if(strcmp(node[i]->element, oscommand) == 0)
128 if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
131 else if(strcmp(node[i]->element, osactive_response) == 0)
133 if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
136 else if(strcmp(node[i]->element, osreports) == 0)
138 if((modules & CREPORTS)&&(Read_CReports(chld_node, d1, d2)<0))
143 merror(XML_INVELEM, ARGV0, node[i]->element);
147 //printf("before\n");
148 OS_ClearNode(chld_node);
157 /* ReadConfig(int modules, char *cfgfile)
158 * Read the config files
160 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
167 /** XML definitions **/
169 char *xml_start_ossec = "ossec_config";
170 char *xml_start_agent = "agent_config";
172 char *xml_agent_name = "name";
173 char *xml_agent_os = "os";
174 char *xml_agent_overwrite = "overwrite";
177 if(OS_ReadXML(cfgfile,&xml) < 0)
179 if(modules & CAGENT_CONFIG)
182 merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
187 merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
193 node = OS_GetElementsbyNode(&xml, NULL);
200 /* Reading the main configuration */
204 if(!node[i]->element)
206 merror(XML_ELEMNULL, ARGV0);
209 else if(!(modules & CAGENT_CONFIG) &&
210 (strcmp(node[i]->element, xml_start_ossec) == 0))
212 XML_NODE chld_node = NULL;
213 chld_node = OS_GetElementsbyNode(&xml,node[i]);
215 /* Main element does not need to have any child */
218 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
220 merror(CONFIG_ERROR, ARGV0, cfgfile);
224 OS_ClearNode(chld_node);
227 else if((modules & CAGENT_CONFIG) &&
228 (strcmp(node[i]->element, xml_start_agent) == 0))
230 int passed_agent_test = 1;
232 XML_NODE chld_node = NULL;
233 chld_node = OS_GetElementsbyNode(&xml,node[i]);
236 /* Checking if this is specific to any agent. */
237 if(node[i]->attributes && node[i]->values)
239 while(node[i]->attributes[attrs] && node[i]->values[attrs])
241 if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
244 char *agentname = os_read_agent_name();
248 passed_agent_test = 0;
252 if(!OS_Match2(node[i]->values[attrs], agentname))
254 passed_agent_test = 0;
260 else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
263 char *agentos = getuname();
267 if(!OS_Match2(node[i]->values[attrs], agentos))
269 passed_agent_test = 0;
275 passed_agent_test = 0;
276 merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
280 else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
285 merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
293 /* Main element does not need to have any child */
296 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
298 merror(CONFIG_ERROR, ARGV0, cfgfile);
302 OS_ClearNode(chld_node);
307 merror(XML_INVELEM, ARGV0, node[i]->element);
313 /* Clearing node and xml */