Imported Upstream version 2.7
[ossec-hids.git] / src / config / config.c
index c0cda89..57a0afb 100755 (executable)
@@ -1,4 +1,5 @@
-/* @(#) $Id$ */
+/* @(#) $Id: ./src/config/config.c, 2011/11/01 dcid Exp $
+ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All right reserved.
 
 /* Read the main elements of the configuration.
  */
-int read_main_elements(OS_XML xml, int modules, 
-                                   XML_NODE node, 
-                                   void *d1, 
+int read_main_elements(OS_XML xml, int modules,
+                                   XML_NODE node,
+                                   void *d1,
                                    void *d2)
 {
     int i = 0;
-    char *osglobal = "global";
-    char *osrules = "rules";
-    char *ossyscheck = "syscheck";
-    char *osrootcheck = "rootcheck";
-    char *osalerts = "alerts";
-    char *osemailalerts = "email_alerts";
-    char *osdbd = "database_output";
-    char *oscsyslogd = "syslog_output";
-    char *oscagentless = "agentless";
-    char *oslocalfile = "localfile";
-    char *osremote = "remote";
-    char *osclient = "client";
-    char *oscommand = "command";
-    char *osreports = "reports";
-    char *osactive_response = "active-response";
-
-    
+    char *osglobal = "global";                    /*Server Config*/
+    char *osrules = "rules";                      /*Server Config*/
+    char *ossyscheck = "syscheck";                /*Agent Config*/
+    char *osrootcheck = "rootcheck";              /*Agent Config*/
+    char *osalerts = "alerts";                    /*Server Config*/
+    char *osemailalerts = "email_alerts";         /*Server Config*/
+    char *osdbd = "database_output";              /*Server Config*/
+    char *oscsyslogd = "syslog_output";           /*Server Config*/
+    char *oscagentless = "agentless";             /*Server Config*/
+    char *oslocalfile = "localfile";              /*Agent Config*/
+    char *osremote = "remote";                    /*Agent Config*/
+    char *osclient = "client";                    /*Agent Config*/
+    char *oscommand = "command";                  /*? Config*/
+    char *osreports = "reports";                  /*Server Config*/
+    char *osactive_response = "active-response";  /*Agent Config*/
+
+
     while(node[i])
     {
         XML_NODE chld_node = NULL;
-        
+
         chld_node = OS_GetElementsbyNode(&xml,node[i]);
 
         if(!node[i]->element)
@@ -62,7 +63,7 @@ int read_main_elements(OS_XML xml, int modules,
         }
         else if(strcmp(node[i]->element, osglobal) == 0)
         {
-            if(((modules & CGLOBAL) || (modules & CMAIL)) 
+            if(((modules & CGLOBAL) || (modules & CMAIL))
                 && (Read_Global(chld_node, d1, d2) < 0))
                 return(OS_INVALID);
         }
@@ -96,7 +97,7 @@ int read_main_elements(OS_XML xml, int modules,
             if((modules & CSYSCHECK) && (Read_Syscheck(chld_node, d1,d2) < 0))
                 return(OS_INVALID);
             if((modules & CGLOBAL) && (Read_GlobalSK(chld_node, d1, d2) < 0))
-                return(OS_INVALID);    
+                return(OS_INVALID);
         }
         else if(strcmp(node[i]->element, osrootcheck) == 0)
         {
@@ -143,7 +144,7 @@ int read_main_elements(OS_XML xml, int modules,
             merror(XML_INVELEM, ARGV0, node[i]->element);
             return(OS_INVALID);
         }
-        
+
         //printf("before\n");
         OS_ClearNode(chld_node);
         //printf("after\n");
@@ -157,7 +158,7 @@ int read_main_elements(OS_XML xml, int modules,
 /* ReadConfig(int modules, char *cfgfile)
  * Read the config files
  */
-int ReadConfig(int modules, char *cfgfile, void *d1, void *d2) 
+int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
 {
     int i;
     OS_XML xml;
@@ -169,10 +170,13 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
     char *xml_start_ossec = "ossec_config";
     char *xml_start_agent = "agent_config";
 
+    /* Attributes of the <agent_config> tag */
     char *xml_agent_name = "name";
     char *xml_agent_os = "os";
     char *xml_agent_overwrite = "overwrite";
-    
+    /* cmoraes */
+    char *xml_agent_profile = "profile";
+
 
     if(OS_ReadXML(cfgfile,&xml) < 0)
     {
@@ -188,7 +192,7 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
         }
         return(OS_INVALID);
     }
-    
+
 
     node = OS_GetElementsbyNode(&xml, NULL);
     if(!node)
@@ -221,7 +225,7 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
                     return(OS_INVALID);
                 }
 
-                OS_ClearNode(chld_node);    
+                OS_ClearNode(chld_node);
             }
         }
         else if((modules & CAGENT_CONFIG) &&
@@ -235,9 +239,10 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
 
             /* Checking if this is specific to any agent. */
             if(node[i]->attributes && node[i]->values)
-            {    
+            {
                 while(node[i]->attributes[attrs] && node[i]->values[attrs])
                 {
+                    /* Checking if there is an "name=" attribute */
                     if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
                     {
                         #ifdef CLIENT
@@ -277,6 +282,37 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
                         }
                         #endif
                     }
+                    else if(strcmp(xml_agent_profile, node[i]->attributes[attrs]) == 0)
+                    {
+                        #ifdef CLIENT
+                        char *agentprofile = os_read_agent_profile();
+                        debug2("Read agent config profile name [%s]", agentprofile);
+
+                        if(!agentprofile)
+                        {
+                            passed_agent_test = 0;
+                        }
+                        else
+                        {
+                            /* match the profile name of this <agent_config> section
+                             * with a comma separated list of values in agent's
+                             * <config-profile> tag.
+                             */
+                            if(!OS_Match2(node[i]->values[attrs], agentprofile))
+                            {
+                                passed_agent_test = 0;
+                                debug2("[%s] did not match agent config profile name [%s]",
+                                       node[i]->values[attrs], agentprofile);
+                            }
+                            else
+                            {
+                                debug2("Matched agent config profile name [%s]", agentprofile);
+                            }
+                            free(agentprofile);
+                        }
+                        #endif
+                    }
+                    /* cmoraes: end add */
                     else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
                     {
                     }
@@ -288,8 +324,25 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
                     attrs++;
                 }
             }
+            #ifdef CLIENT
+            else
+            {
+                debug2("agent_config element does not have any attributes.");
+
+                /* if node does not have any attributes, it is a generic config block.
+                 * check if agent has a profile name
+                 * if agent does not have profile name, then only read this generic
+                 * agent_config block
+                 */
+
+                if (!os_read_agent_profile())
+                {
+                    debug2("but agent has a profile name.");
+                    passed_agent_test = 0;
+                }
+            }
+            #endif
 
-            
             /* Main element does not need to have any child */
             if(chld_node)
             {
@@ -299,7 +352,7 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
                     return(OS_INVALID);
                 }
 
-                OS_ClearNode(chld_node);    
+                OS_ClearNode(chld_node);
             }
         }
         else
@@ -309,7 +362,7 @@ int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
         }
         i++;
     }
-    
+
     /* Clearing node and xml */
     OS_ClearNode(node);
     OS_ClearXML(&xml);