1 /* @(#) $Id: ./src/config/config.c, 2011/11/01 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
13 /* Unified function to read the config.
19 #include "os_xml/os_xml.h"
23 /* Read the main elements of the configuration.
25 int read_main_elements(OS_XML xml, int modules,
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*/
50 XML_NODE chld_node = NULL;
52 chld_node = OS_GetElementsbyNode(&xml,node[i]);
56 merror(XML_ELEMNULL, ARGV0);
61 merror(XML_INVELEM, ARGV0, node[i]->element);
64 else if(strcmp(node[i]->element, osglobal) == 0)
66 if(((modules & CGLOBAL) || (modules & CMAIL))
67 && (Read_Global(chld_node, d1, d2) < 0))
70 else if(strcmp(node[i]->element, osemailalerts) == 0)
72 if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
75 else if(strcmp(node[i]->element, osdbd) == 0)
77 if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
80 else if(strcmp(node[i]->element, oscsyslogd) == 0)
82 if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
85 else if(strcmp(node[i]->element, oscagentless) == 0)
87 if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
90 else if(strcmp(node[i]->element, osrules) == 0)
92 if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
95 else if(strcmp(node[i]->element, ossyscheck) == 0)
97 if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
99 if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
102 else if(strcmp(node[i]->element, osrootcheck) == 0)
104 if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
107 else if(strcmp(node[i]->element, osalerts) == 0)
109 if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
112 else if(strcmp(node[i]->element, oslocalfile) == 0)
114 if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
117 else if(strcmp(node[i]->element, osremote) == 0)
119 if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
122 else if(strcmp(node[i]->element, osclient) == 0)
124 if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
127 else if(strcmp(node[i]->element, oscommand) == 0)
129 if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
132 else if(strcmp(node[i]->element, osactive_response) == 0)
134 if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
137 else if(strcmp(node[i]->element, osreports) == 0)
139 if((modules & CREPORTS)&&(Read_CReports(chld_node, d1, d2)<0))
144 merror(XML_INVELEM, ARGV0, node[i]->element);
148 //printf("before\n");
149 OS_ClearNode(chld_node);
158 /* ReadConfig(int modules, char *cfgfile)
159 * Read the config files
161 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
168 /** XML definitions **/
170 char *xml_start_ossec = "ossec_config";
171 char *xml_start_agent = "agent_config";
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";
178 char *xml_agent_profile = "profile";
181 if(OS_ReadXML(cfgfile,&xml) < 0)
183 if(modules & CAGENT_CONFIG)
186 merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
191 merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
197 node = OS_GetElementsbyNode(&xml, NULL);
204 /* Reading the main configuration */
208 if(!node[i]->element)
210 merror(XML_ELEMNULL, ARGV0);
213 else if(!(modules & CAGENT_CONFIG) &&
214 (strcmp(node[i]->element, xml_start_ossec) == 0))
216 XML_NODE chld_node = NULL;
217 chld_node = OS_GetElementsbyNode(&xml,node[i]);
219 /* Main element does not need to have any child */
222 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
224 merror(CONFIG_ERROR, ARGV0, cfgfile);
228 OS_ClearNode(chld_node);
231 else if((modules & CAGENT_CONFIG) &&
232 (strcmp(node[i]->element, xml_start_agent) == 0))
234 int passed_agent_test = 1;
236 XML_NODE chld_node = NULL;
237 chld_node = OS_GetElementsbyNode(&xml,node[i]);
240 /* Checking if this is specific to any agent. */
241 if(node[i]->attributes && node[i]->values)
243 while(node[i]->attributes[attrs] && node[i]->values[attrs])
245 /* Checking if there is an "name=" attribute */
246 if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
249 char *agentname = os_read_agent_name();
253 passed_agent_test = 0;
257 if(!OS_Match2(node[i]->values[attrs], agentname))
259 passed_agent_test = 0;
265 else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
268 char *agentos = getuname();
272 if(!OS_Match2(node[i]->values[attrs], agentos))
274 passed_agent_test = 0;
280 passed_agent_test = 0;
281 merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
285 else if(strcmp(xml_agent_profile, node[i]->attributes[attrs]) == 0)
288 char *agentprofile = os_read_agent_profile();
289 debug2("Read agent config profile name [%s]", agentprofile);
293 passed_agent_test = 0;
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.
301 if(!OS_Match2(node[i]->values[attrs], agentprofile))
303 passed_agent_test = 0;
304 debug2("[%s] did not match agent config profile name [%s]",
305 node[i]->values[attrs], agentprofile);
309 debug2("Matched agent config profile name [%s]", agentprofile);
315 /* cmoraes: end add */
316 else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
321 merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
330 debug2("agent_config element does not have any attributes.");
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
338 if (!os_read_agent_profile())
340 debug2("but agent has a profile name.");
341 passed_agent_test = 0;
346 /* Main element does not need to have any child */
349 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
351 merror(CONFIG_ERROR, ARGV0, cfgfile);
355 OS_ClearNode(chld_node);
360 merror(XML_INVELEM, ARGV0, node[i]->element);
366 /* Clearing node and xml */