]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/ids-functions.pl
ids-functions.pl: Use temporary file in downloader.
[ipfire-2.x.git] / config / cfgroot / ids-functions.pl
index 2cf1ad7cea81179002f328686a033fd245714397..cf7452ef996106d38887e3dfb184a87fd2b4163e 100644 (file)
@@ -168,11 +168,46 @@ sub downloadruleset {
                return 1;
        }
 
+       # Pass the requrested url to the downloader.
+       my $request = HTTP::Request->new(HEAD => $url);
+
+       # Accept the html header.
+       $request->header('Accept' => 'text/html');
+
+       # Perform the request and fetch the html header.
+       my $response = $downloader->request($request);
+
+       # Check if there was any error.
+       unless ($response->is_success) {
+               # Obtain error.
+               my $error = $response->content;
+
+               # Log error message.
+               &_log_to_syslog("Unable to download the ruleset. \($error\)");
+
+               # Return "1" - false.
+               return 1;
+       }
+
+       # Assign the fetched header object.
+       my $header = $response->headers;
+
+       # Grab the remote file size from the object and store it in the
+       # 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);
 
-       # 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) {
@@ -186,6 +221,34 @@ sub downloadruleset {
                return 1;
        }
 
+       # Load perl stat module.
+       use File::stat;
+
+       # Perform stat on the tmpfile.
+       my $stat = stat($tmpfile);
+
+       # Grab the local filesize of the downloaded tarball.
+       my $local_filesize = $stat->size;
+
+       # Check if both file sizes match.
+       unless ($remote_filesize eq $local_filesize) {
+               # Log error message.
+               &_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;
+       }
+
+       # 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;
 }