]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - config/cfgroot/ids-functions.pl
ids-functions.pl: Use If-Modified-Since header to reduce file downloads.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / ids-functions.pl
index eb276030b6265618b11c7c42f27888dd36f1f338..61aecc250bba2ce1ab806582c39c7f18ed831926 100644 (file)
@@ -29,6 +29,36 @@ require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/network-functions.pl";
 require "${General::swroot}/suricata/ruleset-sources";
 
+# Load perl module to deal with Archives.
+use Archive::Tar;
+
+# Load perl module to deal with files and path.
+use File::Basename;
+
+# Load module to move files.
+use File::Copy;
+
+# Load module to recursely remove files and a folder.
+use File::Path qw(rmtree);
+
+# Load module to get file stats.
+use File::stat;
+
+# Load module to deal with temporary files.
+use File::Temp;
+
+# Load module to deal with the date formats used by the HTTP protocol.
+use HTTP::Date;
+
+# Load the libwwwperl User Agent module.
+use LWP::UserAgent;
+
+# Load function from posix module to format time strings.
+use POSIX qw (strftime);
+
+# Load module to talk to the kernel log daemon.
+use Sys::Syslog qw(:DEFAULT setlogsock);
+
 # Location where all config and settings files are stored.
 our $settingsdir = "${General::swroot}/suricata";
 
@@ -256,6 +286,10 @@ sub downloadruleset ($) {
        # If no provider is given default to "all".
        $provider //= 'all';
 
+       # The amount of download attempts before giving up and
+       # logging an error.
+       my $max_dl_attempts = 3;
+
        # Hash to store the providers and access id's, for which rules should be downloaded.
        my %sheduled_providers = ();
 
@@ -276,9 +310,6 @@ sub downloadruleset ($) {
        my %proxysettings=();
        &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
 
-       # Load required perl module to handle the download.
-       use LWP::UserAgent;
-
        # Init the download module.
        #
        # Request SSL hostname verification and specify path
@@ -354,40 +385,86 @@ sub downloadruleset ($) {
                        return 1;
                }
 
-               # Load perl module to deal with temporary files.
-               use File::Temp;
+               # Pass the requested URL to the downloader.
+               my $request = HTTP::Request->new(GET => $url);
 
                # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
+               # The downloaded file will be stored there until some sanity checks are performed.
                my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 );
                my $tmpfile = $tmp->filename();
 
-               # Pass the requested url to the downloader.
-               my $request = HTTP::Request->new(GET => $url);
+               # Call function to get the final path and filename for the downloaded file.
+               my $dl_rulesfile = &_get_dl_rulesfile($provider);
 
-               # Perform the request and save the output into the tmpfile.
-               my $response = $downloader->request($request, $tmpfile);
+               # Check if the rulesfile already exits, because it has been downloaded in the past.
+               #
+               # In this case we are requesting the server if the remote file has been changed or not.
+               # This will be done by sending the modification time in a special HTTP header.
+               if (-f $dl_rulesfile) {
+                       # Call stat on the file.
+                       my $stat = stat($dl_rulesfile);
 
-               # Check if there was any error.
-               unless ($response->is_success) {
-                       # Obtain error.
-                       my $error = $response->content;
+                       # Omit the mtime of the existing file.
+                       my $mtime = $stat->mtime;
 
-                       # Log error message.
-                       &_log_to_syslog("Unable to download the ruleset. \($error\)");
+                       # Convert the timestamp into right format.
+                       my $http_date = time2str($mtime);
 
-                       # Return "1" - false.
-                       return 1;
+                       # Add the If-Modified-Since header to the request to ask the server if the
+                       # file has been modified.
+                       $request->header( 'If-Modified-Since' => "$http_date" );
+               }
+
+               my $dl_attempt = 1;
+               my $response;
+
+               # Download and retry on failure.
+               while ($dl_attempt <= $max_dl_attempts) {
+                       # Perform the request and save the output into the tmpfile.
+                       $response = $downloader->request($request, $tmpfile);
+
+                       # Check if the download was successfull.
+                       if($response->is_success) {
+                               # Break loop.
+                               last;
+
+                       # Check if the server responds with 304 (Not Modified).
+                       } elsif ($response->code == 304) {
+                               # Log to syslog.
+                               &_log_to_syslog("Ruleset is up-to-date, no update required.");
+
+                               # Nothing to do, the ruleset is up-to-date.
+                               return;
+
+                       # Check if we ran out of download re-tries.
+                       } elsif ($dl_attempt eq $max_dl_attempts) {
+                               # Obtain error.
+                               my $error = $response->content;
+
+                               # Log error message.
+                               &_log_to_syslog("Unable to download the ruleset. \($error\)");
+
+                               # Return "1" - false.
+                               return 1;
+                       }
+
+                       # Remove temporary file, if one exists.
+                       unlink("$tmpfile") if (-e "$tmpfile");
+
+                       # Increase download attempt counter.
+                       $dl_attempt++;
                }
 
                # Obtain the connection headers.
                my $headers = $response->headers;
 
+               # Get the timestamp from header, when the file has been modified the
+               # last time.
+               my $last_modified = $headers->last_modified;
+
                # Get the remote size of the downloaded file.
                my $remote_filesize = $headers->content_length;
 
-               # Load perl stat module.
-               use File::stat;
-
                # Perform stat on the tmpfile.
                my $stat = stat($tmpfile);
 
@@ -407,9 +484,6 @@ sub downloadruleset ($) {
                        return 1;
                }
 
-               # Genarate and assign file name and path to store the downloaded rules file.
-               my $dl_rulesfile = &_get_dl_rulesfile($provider);
-
                # Check if a file name could be obtained.
                unless ($dl_rulesfile) {
                        # Log error message.
@@ -422,12 +496,16 @@ sub downloadruleset ($) {
                        return 1;
                }
 
-               # Load file copy module, which contains the move() function.
-               use File::Copy;
-
                # Overwrite the may existing rulefile or tarball with the downloaded one.
                move("$tmpfile", "$dl_rulesfile");
 
+               # Check if we got a last-modified value from the server.
+               if ($last_modified) {
+                       # Assign the last-modified timestamp as mtime to the
+                       # rules file.
+                       utime(time(), "$last_modified", "$dl_rulesfile");
+               }
+
                # Delete temporary file.
                unlink("$tmpfile");
 
@@ -448,18 +526,9 @@ sub downloadruleset ($) {
 sub extractruleset ($) {
        my ($provider) = @_;
 
-       # Load perl module to deal with archives.
-       use Archive::Tar;
-
        # Disable chown functionality when uncompressing files.
        $Archive::Tar::CHOWN = "0";
 
-       # Load perl module to deal with files and path.
-       use File::Basename;
-
-       # Load perl module for file copying.
-       use File::Copy;
-
        # Get full path and downloaded rulesfile for the given provider.
        my $tarball = &_get_dl_rulesfile($provider);
 
@@ -565,9 +634,6 @@ sub extractruleset ($) {
                                # Extract the file to the temporary directory.
                                $tar->extract_file("$packed_file", "$destination");
                        } else {
-                               # Load perl module to deal with temporary files.
-                               use File::Temp;
-
                                # Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
                                my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
                                my $tmpfile = $tmp->filename();
@@ -616,9 +682,6 @@ sub oinkmaster () {
                &extractruleset($provider);
        }
 
-       # Load perl module to talk to the kernel syslog.
-       use Sys::Syslog qw(:DEFAULT setlogsock);
-
        # Establish the connection to the syslog service.
        openlog('oinkmaster', 'cons,pid', 'user');
 
@@ -790,9 +853,6 @@ sub merge_sid_msg (@) {
 ## the rules directory.
 #
 sub move_tmp_ruleset() {
-       # Load perl module.
-       use File::Copy;
-
        # Do a directory listing of the temporary directory.
        opendir  DH, $tmp_rules_directory;
 
@@ -810,8 +870,6 @@ sub move_tmp_ruleset() {
 ## Function to cleanup the temporary IDS directroy.
 #
 sub cleanup_tmp_directory () {
-       # Load rmtree() function from file path perl module.
-       use File::Path 'rmtree';
 
        # Delete temporary directory and all containing files.
        rmtree([ "$tmp_directory" ]);
@@ -839,9 +897,6 @@ sub log_error ($) {
 sub _log_to_syslog ($) {
        my ($message) = @_;
 
-       # Load perl module to talk to the kernel syslog.
-       use Sys::Syslog qw(:DEFAULT setlogsock);
-
        # The syslog function works best with an array based input,
        # so generate one before passing the message details to syslog.
        my @syslog = ("ERR", "<ERROR> $message");
@@ -1563,10 +1618,6 @@ sub get_ruleset_date($) {
        my $date;
        my $mtime;
 
-       # Load neccessary perl modules for file stat and to format the timestamp.
-       use File::stat;
-       use POSIX qw( strftime );
-
        # Get the stored rulesfile for this provider.
        my $stored_rulesfile = &_get_dl_rulesfile($provider);