]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/commitdiff
ids-functions.pl: Merge same named rulefiles during extract.
authorStefan Schantl <stefan.schantl@ipfire.org>
Sat, 5 Mar 2022 15:27:17 +0000 (16:27 +0100)
committerPeter Müller <peter.mueller@ipfire.org>
Sat, 5 Mar 2022 19:28:08 +0000 (19:28 +0000)
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

index d15973a38e854679e8b82f16f8d566dffed8711f..79cc369829c7ef9be4ae853069ee284828b7e946 100644 (file)
@@ -583,8 +583,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");
+                       }
                }
        }
 }