]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ids-functions.pl: Introduce filesize check for downloader
authorStefan Schantl <stefan.schantl@ipfire.org>
Tue, 18 Dec 2018 13:16:13 +0000 (14:16 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 18 Dec 2018 13:16:13 +0000 (14:16 +0100)
The downloader now requests the html header for the rulestarball
and obtain the size of the file bevore downloading it.

After success the size of the downloaded file will be compared with
the requested one before. If they do not match, an error will be gained.

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

index 2cf1ad7cea81179002f328686a033fd245714397..1556c5b8503bb1e9a43148d79b39ed4b53aa7562 100644 (file)
@@ -168,6 +168,34 @@ 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;
+
        # Pass the requested url to the downloader.
        my $request = HTTP::Request->new(GET => $url);
 
@@ -186,6 +214,25 @@ sub downloadruleset {
                return 1;
        }
 
+       # Load perl stat module.
+       use File::stat;
+
+       # Perform stat on the rulestarball.
+       my $stat = stat($rulestarball);
+
+       # 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. ");
+
+               # Return "1" - false.
+               return 1;
+       }
+
        # If we got here, everything worked fine. Return nothing.
        return;
 }