Imported Upstream version 2.7
[ossec-hids.git] / src / addagent / manage_agents.c
index 504ffeb..aa28808 100755 (executable)
@@ -1,4 +1,5 @@
-/* @(#) $Id$ */
+/* @(#) $Id: ./src/addagent/manage_agents.c, 2012/02/07 dcid Exp $
+ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
@@ -8,7 +9,7 @@
  * License (version 2) as published by the FSF - Free Software
  * Foundation.
  *
- * License details at the LICENSE file included with OSSEC or 
+ * License details at the LICENSE file included with OSSEC or
  * online at: http://www.ossec.net/en/licensing.html
  */
 
@@ -20,7 +21,7 @@
 
 #include "manage_agents.h"
 #include "os_crypto/md5/md5_op.h"
-
+#include <stdlib.h>
 
 
 /* Global internal variables */
@@ -36,8 +37,8 @@ char *chomp(char *str)
     /* Removing spaces from the beginning */
     while(*str == ' ' || *str == '\t')
         str++;
-    
-    
+
+
     /* Removing any trailing new lines or \r */
     do
     {
@@ -55,17 +56,17 @@ char *chomp(char *str)
         }
     }while(tmp_str != NULL);
 
-    
+
     /* Removing spaces at the end of the string */
     tmp_str = str;
     size = strlen(str)-1;
-    
+
     while((size >= 0) && (tmp_str[size] == ' ' || tmp_str[size] == '\t'))
     {
         tmp_str[size] = '\0';
         size--;
     }
-    
+
     return(str);
 }
 
@@ -78,10 +79,10 @@ int add_agent()
     FILE *fp;
     char str1[STR_SIZE +1];
     char str2[STR_SIZE +1];
-    
+
     os_md5 md1;
     os_md5 md2;
-    
+
     char *user_input;
     char *_name;
     char *_id;
@@ -104,16 +105,16 @@ int add_agent()
 
     /* Allocating for c_ip */
     os_calloc(1, sizeof(os_ip), c_ip);
-    
-    
+
+
     #ifndef WIN32
     chmod(AUTH_FILE, 0440);
     #endif
-    
+
     /* Setting time 2 */
     time2 = time(0);
 
-    
+
     /* Source is time1+ time2 +pid + ppid */
     #ifndef WIN32
         #ifdef __OpenBSD__
@@ -127,7 +128,7 @@ int add_agent()
 
     rand1 = random();
 
-    
+
     /* Zeroing strings */
     memset(str1,'\0', STR_SIZE +1);
     memset(str2,'\0', STR_SIZE +1);
@@ -135,7 +136,7 @@ int add_agent()
 
     printf(ADD_NEW);
 
-    
+
     /* Getting the name */
     memset(name, '\0', FILE_SIZE +1);
 
@@ -143,7 +144,11 @@ int add_agent()
     {
         printf(ADD_NAME);
         fflush(stdout);
-        _name = read_from_user();
+        /* Read the agent's name from user environment. If it is invalid
+         * we should force user to provide a name from input device. */
+        _name = getenv("OSSEC_AGENT_NAME");
+        if (_name == NULL || NameExist(_name) || !OS_IsValidName(_name))
+          _name = read_from_user();
 
         if(strcmp(_name, QUIT) == 0)
             return(0);
@@ -168,15 +173,19 @@ int add_agent()
     {
       printf(ADD_IP);
       fflush(stdout);
-    
-      _ip = read_from_user();
-      
+
+      /* Read IP address from user's environment. If that IP is invalid,
+       * force user to provide IP from input device */
+      _ip = getenv("OSSEC_AGENT_IP");
+      if (_ip == NULL || !OS_IsValidIP(_ip, c_ip))
+        _ip = read_from_user();
+
       /* quit */
       if(strcmp(_ip, QUIT) == 0)
           return(0);
-                              
+
       strncpy(ip, _ip, FILE_SIZE -1);
-      
+
       if(!OS_IsValidIP(ip, c_ip))
       {
           printf(IP_ERROR, ip);
@@ -184,12 +193,12 @@ int add_agent()
       }
 
     } while(!_ip);
-   
-    
+
+
     do
     {
         /* Default ID */
-        i = 1024;
+        i = MAX_AGENTS + 768;
         snprintf(id, 8, "%03d", i);
         while(!IDExist(id))
         {
@@ -209,9 +218,20 @@ int add_agent()
         printf(ADD_ID, id);
         fflush(stdout);
 
-        _id = read_from_user();
-
+        /* Get Agent id from environment. If 0, use default ID. If null,
+         * get from user input. If value from environment is invalid,
+         * we force user to specify an ID from the terminal. Otherwise,
+         * our program goes to infinite loop. */
+        _id = getenv("OSSEC_AGENT_ID");
+        if (_id == NULL || IDExist(_id) || !OS_IsValidID(_id)) {
+          _id = read_from_user();
+        }
 
+        /* If user specified 0 as Agent ID, he meant use default value.
+         * NOTE: a bad condistion can cause infinite loop. */
+        if (strcmp(_id,"0") == 0) {
+          strncpy(_id, id, FILE_SIZE -1);
+        }
 
         /* quit */
         if(strcmp(_id, QUIT) == 0)
@@ -231,8 +251,8 @@ int add_agent()
             printf(ADD_ERROR_ID, id);
 
     } while(IDExist(id) || !OS_IsValidID(id));
-    
-    
+
+
 
     printf(AGENT_INFO, id, name, ip);
     fflush(stdout);
@@ -240,9 +260,15 @@ int add_agent()
     do
     {
       printf(ADD_CONFIRM);
-      user_input = read_from_user();
-   
-      /* If user accepts to add */ 
+      /* Confirmation by an environment variable. The valid value is y/Y.
+       * If the user provide anything other string, it is considered as
+       * n/N; please note that the old code only accepts y/Y/n/N. So if
+       * the variable OSSEC_ACTION_CONFIRMED is 'foobar', the program will
+       * go into an infinite loop. */
+      user_input = getenv("OSSEC_ACTION_CONFIRMED");
+      if (user_input == NULL) user_input = read_from_user();
+
+      /* If user accepts to add */
       if(user_input[0] == 'y' || user_input[0] == 'Y')
       {
         time3 = time(0);
@@ -256,22 +282,22 @@ int add_agent()
         #ifndef WIN32
         chmod(AUTH_FILE, 0440);
         #endif
-                
-        
+
+
         /* Random 1: Time took to write the agent information.
          * Random 2: Time took to choose the action.
          * Random 3: All of this + time + pid
          * Random 4: Md5 all of this + the name, key and ip
          * Random 5: Final key
          */
-        
+
         snprintf(str1, STR_SIZE, "%d%s%d",time3-time2, name, rand1);
         snprintf(str2, STR_SIZE, "%d%s%s%d", time2-time1, ip, id, rand2);
 
         OS_MD5_Str(str1, md1);
         OS_MD5_Str(str2, md2);
 
-        snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(), 
+        snprintf(str1, STR_SIZE, "%s%d%d%d",md1,(int)getpid(), (int)random(),
                                             time3);
         OS_MD5_Str(str1, md1);
 
@@ -283,7 +309,7 @@ int add_agent()
         restart_necessary = 1;
         break;
       }
-      else if(user_input[0] == 'n' || user_input[0] == 'N')
+      else /* if(user_input[0] == 'n' || user_input[0] == 'N') */
       {
         printf(ADD_NOT);
         break;
@@ -301,7 +327,7 @@ int remove_agent()
     FILE *fp;
     char *user_input;
     char u_id[FILE_SIZE +1];
-    
+
     u_id[FILE_SIZE] = '\0';
 
     if(!print_agents(0, 0, 0))
@@ -315,7 +341,10 @@ int remove_agent()
       printf(REMOVE_ID);
       fflush(stdout);
 
-      user_input = read_from_user();
+      user_input = getenv("OSSEC_AGENT_ID");
+      if (user_input == NULL || !IDExist(user_input)) {
+        user_input = read_from_user();
+      }
 
       if(strcmp(user_input, QUIT) == 0)
           return(0);
@@ -327,14 +356,16 @@ int remove_agent()
         printf(NO_ID, user_input);
       }
     } while(!IDExist(user_input));
-    
+
     do
     {
         printf(REMOVE_CONFIRM);
         fflush(stdout);
 
-        user_input = read_from_user();
-
+        user_input = getenv("OSSEC_ACTION_CONFIRMED");
+        if (user_input == NULL) {
+          user_input = read_from_user();
+        }
         /* If user confirm */
         if(user_input[0] == 'y' || user_input[0] == 'Y')
         {
@@ -344,7 +375,7 @@ int remove_agent()
             {
                 ErrorExit(MEM_ERROR, ARGV0);
             }
-            
+
             fp = fopen(AUTH_FILE, "r+");
             if(!fp)
             {
@@ -364,7 +395,7 @@ int remove_agent()
 
 
             /* Remove counter for id */
-            delete_agentinfo(full_name); 
+            delete_agentinfo(full_name);
             OS_RemoveCounter(u_id);
             free(full_name);
             full_name = NULL;
@@ -374,7 +405,7 @@ int remove_agent()
             restart_necessary = 1;
             break;
         }
-        else if(user_input[0] == 'n' || user_input[0] == 'N')
+        else /* if(user_input[0] == 'n' || user_input[0] == 'N') */
         {
             printf(REMOVE_NOT);
             break;