From eea7c2ea8660ecdd493f25e03664b27fddbbc11b Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Fri, 14 Apr 2023 20:24:03 +0200 Subject: [PATCH] location-functions.pl: Introduce load_location() function. This function uses the ipset-functions library to create/update a location list. Signed-off-by: Stefan Schantl --- config/cfgroot/location-functions.pl | 46 +++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/config/cfgroot/location-functions.pl b/config/cfgroot/location-functions.pl index 46e27c04a6..2f65de986e 100644 --- a/config/cfgroot/location-functions.pl +++ b/config/cfgroot/location-functions.pl @@ -23,6 +23,8 @@ package Location::Functions; use Location; +require '/var/ipfire/ipset-functions.pl'; + # Hash which contains country codes and their names which are special or not # part of ISO 3166-1. my %not_iso_3166_location = ( @@ -53,7 +55,10 @@ our $database = "$location_dir/database.db"; our $keyfile = "$location_dir/signing-key.pem"; # Directory which contains the exported databases. -our $ipset_db_directory = "$location_dir/ipset"; +our $exported_db_directory = "$location_dir/exported"; + +# File extension of the exported databases. +our $exported_extension = ".networks"; # Create libloc database handle. my $db_handle = &init(); @@ -259,6 +264,45 @@ sub get_as_name($) { return $as_name; } +# +## Function to load a given location. +## +## This function uses the ipset-functions library to +## load a given location set into the kernel. +# +sub load_location ($) { + my ($location) = @_; + + # Generate path and file where the database for + # the given location lives. + my $file = "$exported_db_directory/$location"; + + # The database files have an "v4" as part of their name. + $file = $file . "v4"; + + # Add the file extension + $file = $file . "$exported_extension"; + + # Open the exported database file for reading. + open (FILE, $file) if (-f $file); + + # Read-in the file content. + my @data = ; + + # Close the file handle. + close(FILE); + + # Return an error if the file could not be read or + # is empty. + return "No data" unless(@data); + + # Call the ipset_load_set function to load the set. + my $error = &IPSet::Functions::ipset_load_set($location, @data); + + # Return the error message if there was one. + return $error if($error); +} + # Custom END declaration which will be executed when perl # ends, to release the database handle to libloc. END { -- 2.39.5