[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ossec-cvs] ossec-hids: ossec-batch-manager.pl (HEAD) [jeff]



Module name:	ossec-hids
Changes by:	jeff	07/08/08 19:34:48

Modified files:
	ossec-batch-manager.pl

Log message:
Changes:
	- Removed useless import_key sub, --import just calls add_agent anyways
	- Changed a few open( statements to multiline where printing the error
	  message causes the code to be > 80 characters wide
	- Fixed some whitespace / tab issues
Bugs fixed:
	- When adding an agent and the client.keys doesn't exist, create a
	  new file instead of complaining it doesn't exist and exiting.
	- Change a few areas that open the client.keys to work better when
	  it is empty, or doesn't exist
Diffstat:
 ossec-batch-manager.pl |   95 +++++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 41 deletions(-)

Index: ossec-batch-manager.pl
===================================================================
RCS file: /usr/cvsroot/ossec-hids/contrib/ossec-batch-manager.pl,v
diff -u -r1.1 -r1.2
--- ossec-batch-manager.pl	8 Aug 2007 00:03:18 -0000	1.1
+++ ossec-batch-manager.pl	8 Aug 2007 22:34:47 -0000	1.2
@@ -32,7 +32,7 @@
   'a|add'       => \$add,         # Add a new agent
   'r|remove=s'  => \$remove,      # Remove an agent
   'e|extract=s' => \$extract,     # Extract a key
-  'm|import'    => \$import,        # Import a key
+  'm|import'    => \$import,      # Import a key
   'l|list'      => \$listagents,  # List all agents
   'i|id=s'      => \$agentid,     # Unique agent id
   'n|name=s'    => \$agentname,   # Agent name. 32 char max
@@ -41,7 +41,6 @@
 
 # Spit out a list of available agents, their names, and ip information
 if ($listagents) {
-  print "Available agents:\n";
   list_agents();
 }
 # Decode and extract the key for $agentid
@@ -64,25 +63,25 @@
       # Autogenerate an id incremented 1 from the last in a sorted list of
       # all current ones if it isn't specified from the command line.
       if (!$agentid) {
-        my @used_agent_ids = ();
 
         # Make a list of all of the used agentids and then sort it.
-        open (FH, "<", AUTH_KEY_FILE) or die "Error: $!\n";
-        while (<FH>) {
-            my ($id, $name, $ip, $key) = split;
-            push(@used_agent_ids, $id);
+        if (-r AUTH_KEY_FILE) {
+          my @used_agent_ids = ();
+          open (FH, "<", AUTH_KEY_FILE);
+          while (<FH>) {
+              my ($id, $name, $ip, $key) = split;
+              push(@used_agent_ids, $id);
+          }
+          close(FH);
+
+          if (@used_agent_ids) {
+            @used_agent_ids = sort(@used_agent_ids);
+            $agentid = sprintf("%03d", $used_agent_ids[-1] + 1);
+          }
+        }
+        # If the client.keys is empty or doesn't exist set the id to 001
+        $agentid = sprintf("%03d", 001) if (!$agentid);
         }
-        close(FH);
-
-      if (@used_agent_ids) {
-        @used_agent_ids = sort(@used_agent_ids);
-        $agentid = sprintf("%03d", $used_agent_ids[-1] + 1);
-      }
-      else {
-        # If the client.keys is empty, create the first entry
-        $agentid = sprintf("%03d", 001);
-      }
-    }
 
     # Autogenerate a key unless one was specified on the command line
     if (!$key) {
@@ -149,11 +148,12 @@
 	  open (FH, "<", AUTH_KEY_FILE);
   }
   else {
-    die "No ".AUTH_KEY_FILE."!\n";
+    die "Error reading ".AUTH_KEY_FILE.": $!\n";
   }
-  print "ID",    " " x (25 - length('ID')),
-        "NAME",  " " x (25 - length('NAME')),
-        "IP",    " " x (25 - length('IP'));
+  print "Available Agents:\n";
+  print "ID",     " " x (25 - length('ID')),
+        "NAME",   " " x (25 - length('NAME')),
+        "IP",     " " x (25 - length('IP'));
   print "\n";
 	while (<FH>) {
 		chomp;
@@ -195,7 +195,7 @@
   my $ip = shift;
   my $agentkey = shift;
 
-  if ($name && $ip && $agentkey && -e AUTH_KEY_FILE) {
+  if ($name && $ip && $agentkey) {
     # Valid example key:
     # 5a832efb8f93660857ce2acf8eec66a19fd9d4fa58e3221bbd2927ca8a0b40c3
     if ($agentkey !~ m/[a-z0-9]{64}/) { 
@@ -207,7 +207,13 @@
     my $exists = check_if_exists(\@newagent);
 
     if ($exists == 0) {
-      open (FH, ">>", AUTH_KEY_FILE) or die "Error: $!\n";
+      # Append if client.keys exists and create it if it doesn't
+      if (-e AUTH_KEY_FILE) {
+        open(FH, ">>", AUTH_KEY_FILE) or die AUTH_KEY_FILE." error: $!\n";
+      }
+      else {
+        open(FH, ">", AUTH_KEY_FILE) or die AUTH_KEY_FILE." error: $!\n";
+      }
       print FH join(' ', @newagent), "\n";
       close(FH);
     }
@@ -222,7 +228,7 @@
     }
   }
   else {
-    warn "Missing options to add agent or problem with ".AUTH_KEY_FILE."!\n";
+    warn "Missing options to --add or problem with ".AUTH_KEY_FILE.": $!\n";
     usage();
   }
 }
@@ -231,13 +237,23 @@
   my $removeid = shift;
   my @agent_array;
 
-	open (FH, "<", AUTH_KEY_FILE) if -e AUTH_KEY_FILE or die "No ".AUTH_KEY_FILE."!\n";
+  if (-r AUTH_KEY_FILE) {
+	  open (FH, "<", AUTH_KEY_FILE);
+  }
+  else {
+    die "Error: with ".AUTH_KEY_FILE.": $!\n";
+  }
   while (<FH>) {
     push(@agent_array, $_);
   }
   close(FH);
 
-	open (FHRW, ">", AUTH_KEY_FILE) if -e AUTH_KEY_FILE or die "No ".AUTH_KEY_FILE."!\n";
+  if (-w AUTH_KEY_FILE) {
+	  open (FHRW, ">", AUTH_KEY_FILE);
+  }
+  else {
+    die "Error writing ".AUTH_KEY_FILE.": $!\n";
+  }
   foreach my $line (@agent_array) {
     if ($line !~ $removeid) {
       print FHRW "$line";
@@ -247,12 +263,6 @@
   exit 0;
 }
 
-sub import_key {
-  my $keydata = shift;
-  warn "Importing keys not implemented yet!\n";
-  exit 0;
-}
-
 sub check_if_exists {
 	my $agentlist_ref = shift;
   my ($newid, $newname, $newip);
@@ -262,14 +272,17 @@
   $newname = $agentlist_ref->[1];
   $newip = $agentlist_ref->[2];
 
-	open (FH, AUTH_KEY_FILE) if -e AUTH_KEY_FILE or die "No ".AUTH_KEY_FILE."!\n";
-	while (<FH>) {
-    chomp;
-    my ($id, $name, $ip, $key) = split;
-    $rval = 1 if ($id == $newid && $rval == 0);
-    $rval = 2 if ($name eq $newname && $rval == 0); 
-    $rval = 3 if ($ip eq $newip && $rval == 0);
+  # If the file isn't readable, the id probably isn't already in it
+  if (-r AUTH_KEY_FILE) {
+	  open (FH, "<", AUTH_KEY_FILE);
+	  while (<FH>) {
+      chomp;
+      my ($id, $name, $ip, $key) = split;
+      $rval = 1 if ($id == $newid && $rval == 0);
+      $rval = 2 if ($name eq $newname && $rval == 0); 
+      $rval = 3 if ($ip eq $newip && $rval == 0);
+    }
+	  close(FH);
   }
-	close(FH);
   return $rval;
 }


OSSEC home | Main Index | Thread Index


OSSEC project: www.ossec.net.
Mailling list information: http://www.ossec.net/en/mailing_lists.html.