]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
ids-functions.pl: Use temporary file in downloader.
authorStefan Schantl <stefan.schantl@ipfire.org>
Tue, 18 Dec 2018 14:14:08 +0000 (15:14 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 18 Dec 2018 14:14:08 +0000 (15:14 +0100)
Download the requested rules tarball into a temporay file
and if every thing is fine, replace the old by the
downloaded one.

In addition with the previously implemented file size check,
we are saved now from a corrupt rules tarball on disk.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/ids-functions.pl

index 1556c5b8503bb1e9a43148d79b39ed4b53aa7562..cf7452ef996106d38887e3dfb184a87fd2b4163e 100644 (file)
@@ -196,11 +196,18 @@ sub downloadruleset {
        # variable.
        my $remote_filesize = $header->content_length;
 
        # variable.
        my $remote_filesize = $header->content_length;
 
+       # Load perl module to deal with temporary files.
+       use File::Temp;
+
+       # Generate temporay file name, located in "/var/tmp" and with a suffix of ".tar.gz".
+       my $tmp = File::Temp->new( SUFFIX => ".tar.gz", DIR => "/var/tmp/", UNLINK => 0 );
+       my $tmpfile = $tmp->filename();
+
        # Pass the requested url to the downloader.
        my $request = HTTP::Request->new(GET => $url);
 
        # Pass the requested url to the downloader.
        my $request = HTTP::Request->new(GET => $url);
 
-       # Perform the request and save the output into the "$rulestarball" file.
-       my $response = $downloader->request($request, $rulestarball);
+       # Perform the request and save the output into the tmpfile.
+       my $response = $downloader->request($request, $tmpfile);
 
        # Check if there was any error.
        unless ($response->is_success) {
 
        # Check if there was any error.
        unless ($response->is_success) {
@@ -217,8 +224,8 @@ sub downloadruleset {
        # Load perl stat module.
        use File::stat;
 
        # Load perl stat module.
        use File::stat;
 
-       # Perform stat on the rulestarball.
-       my $stat = stat($rulestarball);
+       # Perform stat on the tmpfile.
+       my $stat = stat($tmpfile);
 
        # Grab the local filesize of the downloaded tarball.
        my $local_filesize = $stat->size;
 
        # Grab the local filesize of the downloaded tarball.
        my $local_filesize = $stat->size;
@@ -229,10 +236,19 @@ sub downloadruleset {
                &_log_to_syslog("Unable to completely download the ruleset. ");
                &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
 
                &_log_to_syslog("Unable to completely download the ruleset. ");
                &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. ");
 
+               # Delete temporary file.
+               unlink("$tmpfile");
+
                # Return "1" - false.
                return 1;
        }
 
                # Return "1" - false.
                return 1;
        }
 
+       # Load file copy module, which contains the move() function.
+       use File::Copy;
+
+       # Overwrite existing rules tarball with the new downloaded one.
+       move("$tmpfile", "$rulestarball");
+
        # If we got here, everything worked fine. Return nothing.
        return;
 }
        # If we got here, everything worked fine. Return nothing.
        return;
 }