ids-functions.pl: Move code to handle plain rules files to

extractruleset() function.

Now everithing which is extracting or moving stored ruleset files is
easily accessing via one function which takes care about.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
This commit is contained in:
Stefan Schantl
2021-04-02 20:22:15 +02:00
parent 01fc880cf3
commit 1b5aec1b7d

View File

@@ -460,6 +460,9 @@ sub downloadruleset ($) {
#
## Function to extract a given ruleset.
##
## In case the ruleset provider offers a plain file, it simply will
## be copied.
#
sub extractruleset ($) {
my ($provider) = @_;
@@ -470,12 +473,15 @@ sub extractruleset ($) {
# 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);
# Check if the file exists.
unless (-f $tarball) {
&_log_to_syslog("Could not extract ruleset file: $tarball");
&_log_to_syslog("Could not find ruleset file: $tarball");
# Return nothing.
return;
@@ -486,6 +492,18 @@ sub extractruleset ($) {
mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
# Omit the type (dl_type) of the stored ruleset.
my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
# Handle the different ruleset types.
if ($type eq "plain") {
# Generate destination filename an full path.
my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
# Copy the file into the temporary rules directory.
copy($tarball, $destination);
} elsif ( $type eq "archive") {
# Initialize the tar module.
my $tar = Archive::Tar->new($tarball);
@@ -503,10 +521,12 @@ sub extractruleset ($) {
if ("$file" eq "sid-msg.map") {
# Set extract destination to temporary config_dir.
$destination = "$tmp_conf_directory/$provider\-sid-msg.map";
# Handle classification.conf
} elsif ("$file" eq "classification.config") {
# Set extract destination to temporary config_dir.
$destination = "$tmp_conf_directory/$provider\-classification.config";
# Handle rules files.
} elsif ($file =~ m/\.rules$/) {
my $rulesfilename;
@@ -551,15 +571,13 @@ sub extractruleset ($) {
$tar->extract_file("$packed_file", "$destination");
}
}
}
#
## A wrapper function to call the oinkmaster script, setup the rules structues and
## call the functions to merge the additional config files. (classification, sid-msg, etc.).
#
sub oinkmaster () {
# Load perl module for file copying.
use File::Copy;
# Check if the files in rulesdir have the correct permissions.
&_check_rulesdir_permissions();
@@ -571,26 +589,8 @@ sub oinkmaster () {
# Loop through the array of enabled providers.
foreach my $provider (@enabled_providers) {
# Omit the type (dl_type) of the stored ruleset.
my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
# Handle the different ruleset types.
if ($type eq "archive") {
# Call the extractruleset function.
&extractruleset($provider);
} elsif ($type eq "plain") {
# Generate filename and full path for the stored rulesfile.
my $dl_rulesfile = &_get_dl_rulesfile($provider);
# Generate destination filename an full path.
my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
# Copy the file into the temporary rules directory.
copy($dl_rulesfile, $destination);
} else {
# Skip unknown type.
next;
}
}
# Load perl module to talk to the kernel syslog.