mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-28 11:43:25 +02:00
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:
@@ -460,6 +460,9 @@ sub downloadruleset ($) {
|
|||||||
|
|
||||||
#
|
#
|
||||||
## Function to extract a given ruleset.
|
## Function to extract a given ruleset.
|
||||||
|
##
|
||||||
|
## In case the ruleset provider offers a plain file, it simply will
|
||||||
|
## be copied.
|
||||||
#
|
#
|
||||||
sub extractruleset ($) {
|
sub extractruleset ($) {
|
||||||
my ($provider) = @_;
|
my ($provider) = @_;
|
||||||
@@ -470,12 +473,15 @@ sub extractruleset ($) {
|
|||||||
# Load perl module to deal with files and path.
|
# Load perl module to deal with files and path.
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
|
|
||||||
|
# Load perl module for file copying.
|
||||||
|
use File::Copy;
|
||||||
|
|
||||||
# Get full path and downloaded rulesfile for the given provider.
|
# Get full path and downloaded rulesfile for the given provider.
|
||||||
my $tarball = &_get_dl_rulesfile($provider);
|
my $tarball = &_get_dl_rulesfile($provider);
|
||||||
|
|
||||||
# Check if the file exists.
|
# Check if the file exists.
|
||||||
unless (-f $tarball) {
|
unless (-f $tarball) {
|
||||||
&_log_to_syslog("Could not extract ruleset file: $tarball");
|
&_log_to_syslog("Could not find ruleset file: $tarball");
|
||||||
|
|
||||||
# Return nothing.
|
# Return nothing.
|
||||||
return;
|
return;
|
||||||
@@ -486,69 +492,84 @@ sub extractruleset ($) {
|
|||||||
mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
|
mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
|
||||||
mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
|
mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
|
||||||
|
|
||||||
# Initialize the tar module.
|
# Omit the type (dl_type) of the stored ruleset.
|
||||||
my $tar = Archive::Tar->new($tarball);
|
my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
|
||||||
|
|
||||||
# Get the filelist inside the tarball.
|
# Handle the different ruleset types.
|
||||||
my @packed_files = $tar->list_files;
|
if ($type eq "plain") {
|
||||||
|
# Generate destination filename an full path.
|
||||||
|
my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
|
||||||
|
|
||||||
# Loop through the filelist.
|
# Copy the file into the temporary rules directory.
|
||||||
foreach my $packed_file (@packed_files) {
|
copy($tarball, $destination);
|
||||||
my $destination;
|
|
||||||
|
|
||||||
# Splitt the packed file into chunks.
|
} elsif ( $type eq "archive") {
|
||||||
my $file = fileparse($packed_file);
|
# Initialize the tar module.
|
||||||
|
my $tar = Archive::Tar->new($tarball);
|
||||||
|
|
||||||
# Handle msg-id.map file.
|
# Get the filelist inside the tarball.
|
||||||
if ("$file" eq "sid-msg.map") {
|
my @packed_files = $tar->list_files;
|
||||||
# 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;
|
|
||||||
|
|
||||||
# Splitt the filename into chunks.
|
# Loop through the filelist.
|
||||||
my @filename = split("-", $file);
|
foreach my $packed_file (@packed_files) {
|
||||||
|
my $destination;
|
||||||
|
|
||||||
# Reverse the array.
|
# Splitt the packed file into chunks.
|
||||||
@filename = reverse(@filename);
|
my $file = fileparse($packed_file);
|
||||||
|
|
||||||
# Get the amount of elements in the array.
|
# Handle msg-id.map file.
|
||||||
my $elements = @filename;
|
if ("$file" eq "sid-msg.map") {
|
||||||
|
# Set extract destination to temporary config_dir.
|
||||||
|
$destination = "$tmp_conf_directory/$provider\-sid-msg.map";
|
||||||
|
|
||||||
# Remove last element of the hash.
|
# Handle classification.conf
|
||||||
# It contains the vendor name, which will be replaced.
|
} elsif ("$file" eq "classification.config") {
|
||||||
if ($elements >= 3) {
|
# Set extract destination to temporary config_dir.
|
||||||
|
$destination = "$tmp_conf_directory/$provider\-classification.config";
|
||||||
|
|
||||||
|
# Handle rules files.
|
||||||
|
} elsif ($file =~ m/\.rules$/) {
|
||||||
|
my $rulesfilename;
|
||||||
|
|
||||||
|
# Splitt the filename into chunks.
|
||||||
|
my @filename = split("-", $file);
|
||||||
|
|
||||||
|
# Reverse the array.
|
||||||
|
@filename = reverse(@filename);
|
||||||
|
|
||||||
|
# Get the amount of elements in the array.
|
||||||
|
my $elements = @filename;
|
||||||
|
|
||||||
|
# Remove last element of the hash.
|
||||||
|
# It contains the vendor name, which will be replaced.
|
||||||
|
if ($elements >= 3) {
|
||||||
# Remove last element from hash.
|
# Remove last element from hash.
|
||||||
pop(@filename);
|
pop(@filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if the last element of the filename does not
|
||||||
|
# contain the providers name.
|
||||||
|
if ($filename[-1] ne "$provider") {
|
||||||
|
# Add provider name as last element.
|
||||||
|
push(@filename, $provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reverse the array back.
|
||||||
|
@filename = reverse(@filename);
|
||||||
|
|
||||||
|
# Generate the name for the rulesfile.
|
||||||
|
$rulesfilename = join("-", @filename);
|
||||||
|
|
||||||
|
# Set extract destination to temporaray rules_dir.
|
||||||
|
$destination = "$tmp_rules_directory/$rulesfilename";
|
||||||
|
} else {
|
||||||
|
# Skip all other files.
|
||||||
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if the last element of the filename does not
|
# Extract the file to the temporary directory.
|
||||||
# contain the providers name.
|
$tar->extract_file("$packed_file", "$destination");
|
||||||
if ($filename[-1] ne "$provider") {
|
|
||||||
# Add provider name as last element.
|
|
||||||
push(@filename, $provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Reverse the array back.
|
|
||||||
@filename = reverse(@filename);
|
|
||||||
|
|
||||||
# Generate the name for the rulesfile.
|
|
||||||
$rulesfilename = join("-", @filename);
|
|
||||||
|
|
||||||
# Set extract destination to temporaray rules_dir.
|
|
||||||
$destination = "$tmp_rules_directory/$rulesfilename";
|
|
||||||
} else {
|
|
||||||
# Skip all other files.
|
|
||||||
next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Extract the file to the temporary directory.
|
|
||||||
$tar->extract_file("$packed_file", "$destination");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,9 +578,6 @@ sub extractruleset ($) {
|
|||||||
## call the functions to merge the additional config files. (classification, sid-msg, etc.).
|
## call the functions to merge the additional config files. (classification, sid-msg, etc.).
|
||||||
#
|
#
|
||||||
sub oinkmaster () {
|
sub oinkmaster () {
|
||||||
# Load perl module for file copying.
|
|
||||||
use File::Copy;
|
|
||||||
|
|
||||||
# Check if the files in rulesdir have the correct permissions.
|
# Check if the files in rulesdir have the correct permissions.
|
||||||
&_check_rulesdir_permissions();
|
&_check_rulesdir_permissions();
|
||||||
|
|
||||||
@@ -571,26 +589,8 @@ sub oinkmaster () {
|
|||||||
|
|
||||||
# Loop through the array of enabled providers.
|
# Loop through the array of enabled providers.
|
||||||
foreach my $provider (@enabled_providers) {
|
foreach my $provider (@enabled_providers) {
|
||||||
# Omit the type (dl_type) of the stored ruleset.
|
# Call the extractruleset function.
|
||||||
my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
|
&extractruleset($provider);
|
||||||
|
|
||||||
# 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.
|
# Load perl module to talk to the kernel syslog.
|
||||||
|
|||||||
Reference in New Issue
Block a user