1 /* @(#) $Id: config.c,v 1.25 2009/08/27 18:17:41 dcid Exp $ */
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 3) 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 *osactive_response = "active-response";
48 XML_NODE chld_node = NULL;
50 chld_node = OS_GetElementsbyNode(&xml,node[i]);
54 merror(XML_ELEMNULL, ARGV0);
59 merror(XML_INVELEM, ARGV0, node[i]->element);
62 else if(strcmp(node[i]->element, osglobal) == 0)
64 if(((modules & CGLOBAL) || (modules & CMAIL))
65 && (Read_Global(chld_node, d1, d2) < 0))
68 else if(strcmp(node[i]->element, osemailalerts) == 0)
70 if((modules & CMAIL) && (Read_EmailAlerts(chld_node, d1, d2) < 0))
73 else if(strcmp(node[i]->element, osdbd) == 0)
75 if((modules & CDBD) && (Read_DB(chld_node, d1, d2) < 0))
78 else if(strcmp(node[i]->element, oscsyslogd) == 0)
80 if((modules & CSYSLOGD) && (Read_CSyslog(chld_node, d1, d2) < 0))
83 else if(strcmp(node[i]->element, oscagentless) == 0)
85 if((modules & CAGENTLESS) && (Read_CAgentless(chld_node, d1, d2) < 0))
88 else if(strcmp(node[i]->element, osrules) == 0)
90 if((modules & CRULES) && (Read_Rules(chld_node, d1, d2) < 0))
93 else if(strcmp(node[i]->element, ossyscheck) == 0)
95 if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
97 if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
100 else if(strcmp(node[i]->element, osrootcheck) == 0)
102 if((modules & CROOTCHECK) && (Read_Rootcheck(chld_node, d1,d2) < 0))
105 else if(strcmp(node[i]->element, osalerts) == 0)
107 if((modules & CALERTS) && (Read_Alerts(chld_node, d1,d2) < 0))
110 else if(strcmp(node[i]->element, oslocalfile) == 0)
112 if((modules & CLOCALFILE) && (Read_Localfile(chld_node, d1,d2) < 0))
115 else if(strcmp(node[i]->element, osremote) == 0)
117 if((modules & CREMOTE) && (Read_Remote(chld_node, d1,d2) < 0))
120 else if(strcmp(node[i]->element, osclient) == 0)
122 if((modules & CCLIENT) && (Read_Client(chld_node, d1,d2) < 0))
125 else if(strcmp(node[i]->element, oscommand) == 0)
127 if((modules & CAR)&&(ReadActiveCommands(chld_node, d1, d2)<0))
130 else if(strcmp(node[i]->element, osactive_response) == 0)
132 if((modules & CAR)&&(ReadActiveResponses(chld_node, d1, d2)<0))
137 merror(XML_INVELEM, ARGV0, node[i]->element);
141 OS_ClearNode(chld_node);
149 /* ReadConfig(int modules, char *cfgfile)
150 * Read the config files
152 int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
159 /** XML definitions **/
161 char *xml_start_ossec = "ossec_config";
162 char *xml_start_agent = "agent_config";
164 char *xml_agent_name = "name";
165 char *xml_agent_os = "os";
166 char *xml_agent_overwrite = "overwrite";
169 if(OS_ReadXML(cfgfile,&xml) < 0)
171 if(modules & CAGENT_CONFIG)
176 merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
182 node = OS_GetElementsbyNode(&xml, NULL);
189 /* Reading the main configuration */
193 if(!node[i]->element)
195 merror(XML_ELEMNULL, ARGV0);
198 else if(!(modules & CAGENT_CONFIG) &&
199 (strcmp(node[i]->element, xml_start_ossec) == 0))
201 XML_NODE chld_node = NULL;
202 chld_node = OS_GetElementsbyNode(&xml,node[i]);
204 /* Main element does not need to have any child */
207 if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
209 merror(CONFIG_ERROR, ARGV0, cfgfile);
213 OS_ClearNode(chld_node);
216 else if((modules & CAGENT_CONFIG) &&
217 (strcmp(node[i]->element, xml_start_agent) == 0))
219 int passed_agent_test = 1;
221 XML_NODE chld_node = NULL;
222 chld_node = OS_GetElementsbyNode(&xml,node[i]);
225 /* Checking if this is specific to any agent. */
226 if(node[i]->attributes && node[i]->values)
228 while(node[i]->attributes[attrs] && node[i]->values[attrs])
230 if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
232 char *agentname = os_read_agent_name();
236 passed_agent_test = 0;
240 if(!OS_Match2(node[i]->values[attrs], agentname))
242 passed_agent_test = 0;
247 else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
249 char *agentos = getuname();
253 if(!OS_Match2(node[i]->values[attrs], agentos))
255 passed_agent_test = 0;
261 passed_agent_test = 0;
262 merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
265 else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
270 merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
278 /* Main element does not need to have any child */
281 if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
283 merror(CONFIG_ERROR, ARGV0, cfgfile);
287 OS_ClearNode(chld_node);
292 merror(XML_INVELEM, ARGV0, node[i]->element);
298 /* Clearing node and xml */