ids-functions.pl: Merge same named rulefiles during extract.

Message ID 20220305152717.5879-1-stefan.schantl@ipfire.org
State Accepted
Commit 9106bfca42a86f9720c4e2f5d0d166832cac6454
Headers
Series ids-functions.pl: Merge same named rulefiles during extract. |

Commit Message

Stefan Schantl March 5, 2022, 3:27 p.m. UTC
  In case a rulestarball contains several same-named rulefiles
they have been overwritten each time and so only contained the content
from the last extracted one.

Now the content of those files will be merged by appending the content
to the first extracted one for each time.

Fixes #12792.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/cfgroot/ids-functions.pl | 34 +++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
  

Comments

Bernhard Bitsch March 6, 2022, 3:14 p.m. UTC | #1
Reviewed-by: Bernhard Bitsch <bbitsch@ipfire.org>

Am 05.03.2022 um 16:27 schrieb Stefan Schantl:
> In case a rulestarball contains several same-named rulefiles
> they have been overwritten each time and so only contained the content
> from the last extracted one.
> 
> Now the content of those files will be merged by appending the content
> to the first extracted one for each time.
> 
> Fixes #12792.
> 
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
>   config/cfgroot/ids-functions.pl | 34 +++++++++++++++++++++++++++++++--
>   1 file changed, 32 insertions(+), 2 deletions(-)
> 
> diff --git a/config/cfgroot/ids-functions.pl b/config/cfgroot/ids-functions.pl
> index 74d55def6..53ca1bdd4 100644
> --- a/config/cfgroot/ids-functions.pl
> +++ b/config/cfgroot/ids-functions.pl
> @@ -572,8 +572,38 @@ sub extractruleset ($) {
>   				next;
>   			}
>   
> -			# Extract the file to the temporary directory.
> -			$tar->extract_file("$packed_file", "$destination");
> +			# Check if the destination file exists.
> +			unless(-e "$destination") {
> +				# Extract the file to the temporary directory.
> +				$tar->extract_file("$packed_file", "$destination");
> +			} else {
> +				# Load perl module to deal with temporary files.
> +				use File::Temp;
> +
> +				# Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
> +				my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
> +				my $tmpfile = $tmp->filename();
> +
> +				# Extract the file to the new temporary file name.
> +				$tar->extract_file("$packed_file", "$tmpfile");
> +
> +				# Open the the existing file.
> +				open(DESTFILE, ">>", "$destination") or die "Could not open $destination. $!\n";
> +				open(TMPFILE, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
> +
> +				# Loop through the content of the temporary file.
> +				while (<TMPFILE>) {
> +					# Append the content line by line to the destination file.
> +					print DESTFILE "$_";
> +				}
> +
> +				# Close the file handles.
> +				close(TMPFILE);
> +				close(DESTFILE);
> +
> +				# Remove the temporary file.
> +				unlink("$tmpfile");
> +			}
>   		}
>   	}
>   }
  

Patch

diff --git a/config/cfgroot/ids-functions.pl b/config/cfgroot/ids-functions.pl
index 74d55def6..53ca1bdd4 100644
--- a/config/cfgroot/ids-functions.pl
+++ b/config/cfgroot/ids-functions.pl
@@ -572,8 +572,38 @@  sub extractruleset ($) {
 				next;
 			}
 
-			# Extract the file to the temporary directory.
-			$tar->extract_file("$packed_file", "$destination");
+			# Check if the destination file exists.
+			unless(-e "$destination") {
+				# Extract the file to the temporary directory.
+				$tar->extract_file("$packed_file", "$destination");
+			} else {
+				# Load perl module to deal with temporary files.
+				use File::Temp;
+
+				# Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
+				my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
+				my $tmpfile = $tmp->filename();
+
+				# Extract the file to the new temporary file name.
+				$tar->extract_file("$packed_file", "$tmpfile");
+
+				# Open the the existing file.
+				open(DESTFILE, ">>", "$destination") or die "Could not open $destination. $!\n";
+				open(TMPFILE, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
+
+				# Loop through the content of the temporary file.
+				while (<TMPFILE>) {
+					# Append the content line by line to the destination file.
+					print DESTFILE "$_";
+				}
+
+				# Close the file handles.
+				close(TMPFILE);
+				close(DESTFILE);
+
+				# Remove the temporary file.
+				unlink("$tmpfile");
+			}
 		}
 	}
 }