]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - modules/Events.pm
Add ability to reload the ignore list.
[people/stevee/guardian.git] / modules / Events.pm
index a5da8e45d9ef802d4a72c8c89a274012439b0b14..376438fb25681119ac6df1537e046d8994f01a1d 100644 (file)
@@ -13,6 +13,7 @@ my %commands = (
        'unblock' => \&CallUnblock,
        'flush' => \&CallFlush,
        'reload' => \&main::Reload,
+       'reload-ignore-list' => \&main::ReloadIgnoreList,
 );
 
 # Hash to store addresses and their current count.
@@ -303,6 +304,11 @@ sub CallUnblock ($) {
 #
 sub GenerateIgnoreList($) {
        my $file = shift;
+       my @include_files;
+
+       # Reset current ignore hash and add
+       # localhost related IP addresses.
+       %ignorehash = &_whitelist_localhost();
 
        # Check if the given IgnoreFile could be opened.
        unless(-e $file) {
@@ -310,11 +316,7 @@ sub GenerateIgnoreList($) {
                return;
        }
 
-       # Reset current ignore hash and add
-       # localhost related IP addresses.
-       %ignorehash = &_whitelist_localhost();
-
-       # Open the given IgnoreFile. 
+       # Open the given IgnoreFile.
        open (IGNORE, $file);
 
        # Read-in the file line by line.
@@ -328,28 +330,94 @@ sub GenerateIgnoreList($) {
                # Remove any newlines.
                chomp;
 
-               # Check if the line contains a valid single address or network and
-               # convert it into binary format. Store the result/start and
-               # end values in a temporary array.
-               my @values = &Guardian::Base::IPOrNet2Int($_);
+               # Check for an include instruction.
+               if ($_ =~ /^Include_File = (.*)/) {
+                       my $include_file = $1;
 
-               # If the function returned any values, the line contained a valid
-               # single address or network which successfully has been converted into
-               # binary format.
-               if (@values) {
-                       # Assign the array as value to the ignorehash.
-                       $ignorehash{$_} = [@values];
-               } else {
-                       # Log invalid entry.
-                       $logger->Log("err", "IgnoreFile contains an invalid address/network: $_");
+                       # Check if the parsed include file exists and is read-able.
+                       if (-e $include_file) {
+                               # Add file to the array of files wich will be included.
+                               push(@include_files, $include_file);
 
-                       # Skip line.
-                       next;
+                               # Write out log message.
+                               $logger->Log("debug", "Addresses from $include_file will be included...");
+                       } else {
+                               # Log missing file.
+                               $logger->Log("err", "$include_file will not be included. File does not exist!");
+                       }
+               } else {
+                       # Check if the line contains a valid single address or network and
+                       # convert it into binary format. Store the result/start and
+                       # end values in a temporary array.
+                       my @values = &Guardian::Base::IPOrNet2Int($_);
+
+                       # If the function returned any values, the line contained a valid
+                       # single address or network which successfully has been converted into
+                       # binary format.
+                       if (@values) {
+                               # Assign the array as value to the ignorehash.
+                               $ignorehash{$_} = [@values];
+                       } else {
+                               # Log invalid entry.
+                               $logger->Log("err", "IgnoreFile contains an invalid address/network: $_");
+
+                               # Skip line.
+                               next;
+                       }
                }
        }
 
        # Close filehandle for the IgnoreFile.
        close (IGNORE);
+
+       # Check if any files should be included.
+       if (@include_files) {
+               # Loop through the array of files which should be included.
+               foreach my $file (@include_files) {
+                       # Open the file.
+                       open(INCLUDE, $file);
+
+                       # Read-in file line by line.
+                       while(<INCLUDE>) {
+                               # Skip any comments.
+                               next if (/\#/);
+
+                               # Skip any blank lines.
+                               next if (/^\s*$/);
+
+                               # Chomp any newlines.
+                               chomp;
+
+                               # Check if the line contains a valid single address or network and
+                               # convert it into binary format. Store the result/start and
+                               # end values in a temporary array.
+                               my @values = &Guardian::Base::IPOrNet2Int($_);
+
+                               # If the function returned any values, the line contained a valid
+                               # single address or network which successfully has been converted into
+                               # binary format.
+                               if (@values) {
+                                       # Assign the array as value to the ignorehash.
+                                       $ignorehash{$_} = [@values];
+                               } else {
+                                       # Log invalid entry.
+                                       $logger->Log("err", "$file contains an invalid address/network: $_");
+
+                                       # Skip line.
+                                       next;
+                               }
+                       }
+
+                       # Close filehandle.
+                       close(INCLUDE);
+               }
+       }
+
+       # Get amount of current elements in hash.
+       my $amount = scalar(keys(%ignorehash));
+
+       # Write out log message.
+       $logger->Log("debug", "Ignore list currently contains $amount entries.");
 }
 
 #