Imported Upstream version 2.7
[ossec-hids.git] / src / shared / agent_op.c
index 2ff49b7..229043b 100755 (executable)
@@ -1,4 +1,5 @@
-/* @(#) $Id$ */
+/* @(#) $Id: ./src/shared/agent_op.c, 2011/09/08 dcid Exp $
+ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
@@ -17,7 +18,7 @@
 /** Checks if syscheck is to be executed/restarted.
  *  Returns 1 on success or 0 on failure (shouldn't be executed now).
  */
-int os_check_restart_syscheck() 
+int os_check_restart_syscheck()
 {
     struct stat restart_status;
 
@@ -28,19 +29,19 @@ int os_check_restart_syscheck()
     {
         if(stat(SYSCHECK_RESTART, &restart_status) == -1)
             return(0);
-        
-        unlink(SYSCHECK_RESTART);    
+
+        unlink(SYSCHECK_RESTART);
     }
     else
     {
         if(stat(SYSCHECK_RESTART_PATH, &restart_status) == -1)
             return(0);
-        
-        unlink(SYSCHECK_RESTART_PATH);    
+
+        unlink(SYSCHECK_RESTART_PATH);
     }
-    
 
-    return(1);    
+
+    return(1);
 }
 
 
@@ -77,12 +78,14 @@ char* os_read_agent_name()
     char buf[1024 + 1];
     FILE *fp = NULL;
 
+    debug2("%s: calling os_read_agent_name().", ARGV0);
+
     if(isChroot())
         fp = fopen(AGENT_INFO_FILE, "r");
     else
         fp = fopen(AGENT_INFO_FILEP, "r");
-        
-    /* We give 1 second for the file to be created... */ 
+
+    /* We give 1 second for the file to be created... */
     if(!fp)
     {
         sleep(1);
@@ -90,9 +93,9 @@ char* os_read_agent_name()
         if(isChroot())
             fp = fopen(AGENT_INFO_FILE, "r");
         else
-            fp = fopen(AGENT_INFO_FILEP, "r");        
+            fp = fopen(AGENT_INFO_FILEP, "r");
     }
-    
+
     if(!fp)
     {
         debug1(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
@@ -108,7 +111,9 @@ char* os_read_agent_name()
         char *ret = NULL;
         os_strdup(buf, ret);
         fclose(fp);
-        
+
+        debug2("%s: os_read_agent_name returned (%s).", __local_name, ret);
+
         return(ret);
     }
 
@@ -127,6 +132,8 @@ char *os_read_agent_ip()
     char buf[1024 + 1];
     FILE *fp;
 
+    debug2("%s: calling os_read_agent_ip().", ARGV0);
+
     fp = fopen(AGENT_INFO_FILE, "r");
     if(!fp)
     {
@@ -162,6 +169,8 @@ char *os_read_agent_id()
     char buf[1024 + 1];
     FILE *fp;
 
+    debug2("%s: calling os_read_agent_id().", ARGV0);
+
     fp = fopen(AGENT_INFO_FILE, "r");
     if(!fp)
     {
@@ -187,12 +196,72 @@ char *os_read_agent_id()
 }
 
 
+/*  cmoraes: begin add */
+
+/** char *os_read_agent_profile()
+ *  Reads the agent profile name for the current agent.
+ *  Returns NULL on error.
+ *
+ *  Description:
+ *  Comma separated list of strings that used to identify what type
+ *  of configuration is used for this agent.
+ *  The profile name is set in the agent's etc/ossec.conf file
+ *  It is matched with the ossec manager's agent.conf file to read
+ *  configuration only applicable to this profile name.
+ *
+ */
+char* os_read_agent_profile()
+{
+    char buf[1024 + 1];
+    FILE *fp;
+
+    debug2("%s: calling os_read_agent_profile().", __local_name);
+
+    if(isChroot())
+        fp = fopen(AGENT_INFO_FILE, "r");
+    else
+        fp = fopen(AGENT_INFO_FILEP, "r");
+
+    if(!fp)
+    {
+        debug2("%s: Failed to open file. Errno=%d.", ARGV0, errno);
+        merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
+        return(NULL);
+    }
+
+    buf[1024] = '\0';
+
+
+    /* Getting profile */
+    if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp) &&
+       fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
+    {
+        char *ret = NULL;
+
+        /* Trim the /n and/or /r at the end of the string */
+        os_trimcrlf(buf);
+
+        os_strdup(buf, ret);
+        debug2("%s: os_read_agent_profile() = [%s]", __local_name, ret);
+
+        fclose(fp);
+
+        return(ret);
+    }
+
+    fclose(fp);
+    return(NULL);
+}
+/* cmoraes: end add */
+
 
 /** int os_write_agent_info(char *agent_name, char *agent_ip, char *agent_id)
  *  Writes the agent info inside the queue, for the other processes to read.
  *  Returns 1 on success or <= 0 on failure.
  */
-int os_write_agent_info(char *agent_name, char *agent_ip, char *agent_id)
+/* cmoraes: changed function. added cfg_profile_name parameter */
+int os_write_agent_info(char *agent_name, char *agent_ip,
+                        char *agent_id,   char *cfg_profile_name)
 {
     FILE *fp;
 
@@ -203,7 +272,8 @@ int os_write_agent_info(char *agent_name, char *agent_ip, char *agent_id)
         return(0);
     }
 
-    fprintf(fp, "%s\n-\n%s\n", agent_name, agent_id);
+    /*cmoraes: added cfg_profile_name parameter*/
+    fprintf(fp, "%s\n-\n%s\n%s\n", agent_name, agent_id, cfg_profile_name);
     fclose(fp);
     return(1);
 }