From patchwork Wed Mar 9 14:12:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 5323 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4KDDg26G55z3xpF for ; Wed, 9 Mar 2022 14:12:46 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4KDDg04fjrz5SD; Wed, 9 Mar 2022 14:12:44 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4KDDg01N5Bz30HJ; Wed, 9 Mar 2022 14:12:44 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4KDDfz57Jnz2xMX for ; Wed, 9 Mar 2022 14:12:43 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4KDDfz313JzfC; Wed, 9 Mar 2022 14:12:43 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1646835163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mbq2O4kFY1Q4fCC25G4j5Fgl8Hjr47ebKpIhKLtisis=; b=QeJn5y6RHAEfMlLeFeBIrHv0bOK86tGhPPSA5u7CwXFCbPYW8+f5g50D3XKLoPgp1yxo8W B4F7/34v6QSiYoBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1646835163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mbq2O4kFY1Q4fCC25G4j5Fgl8Hjr47ebKpIhKLtisis=; b=kES24xoZaAnIFawuwRnop80vpIar3sqrgCwNOXwlD0Xgh/fuAYlmQxDJK0y0lhJt/nSb6f WrqNm5laQDu7w+bVkxTa+8bfJd856qeFPmZy1rCNz722TVZGYB4SxtsNIqNOnIML+q/S6u JL8uDA7Sbjx7li54YDOhvR+2rp0LWjeOkp60mNo2ItPGq7C7cITu3X2snY4OiXGOyvvxtK b1VaznpbYe7UXPOFJUJfBfflh2p5j26RM0Gy6R3ngsgVcd8PoLJAnqhLw7FhDDmZv8WS8c Z7UYYmL0BaMzSKdTFRp5rkAFwLFhs9ONqNjzRsbo6mHYVE8ePYCd12eoiAcMIA== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 2/4] rules.pl: Adjust code to use sets generated by libloc 0.9.11 Date: Wed, 9 Mar 2022 15:12:30 +0100 Message-Id: <20220309141232.2401848-2-stefan.schantl@ipfire.org> In-Reply-To: <20220309141232.2401848-1-stefan.schantl@ipfire.org> References: <20220309141232.2401848-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Signed-off-by: Stefan Schantl --- config/firewall/rules.pl | 44 +++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl index b8c602538..83216e78a 100644 --- a/config/firewall/rules.pl +++ b/config/firewall/rules.pl @@ -957,20 +957,44 @@ sub ipset_restore ($) { # Check if the given set name is a country code. if($set ~~ @locations) { - # Libloc adds "ipset4" as prefix to all exported IPv4 data. - my $file_prefix = "ipset4"; + # Libloc adds the IP type (v4 or v6) as part of the set and file name. + my $loc_set = "$set" . "v4"; - # Generate full path and filename for the ipset db file to restore. - $db_file = "$Location::Functions::ipset_db_directory/$set.$file_prefix"; + # The bare filename equals the set name. + my $filename = $loc_set; + + # Libloc uses "ipset" as file extension. + my $file_extension = "ipset"; + + # Generate full path and filename for the ipset db file. + my $db_file = "$Location::Functions::ipset_db_directory/$filename.$file_extension"; + + # Call function to restore/load the set. + &ipset_call_restore($db_file); + + # Check if the set is already loaded (has been used before). + if ($set ~~ @ipset_used_sets) { + # The sets contains the IP type (v4 or v6) as part of the name. + # The firewall rules matches against sets without that extension. So we safely + # can swap or rename the sets to use the new ones. + run("$IPSET swap $loc_set $set"); + } else { + # If the set is not loaded, we have to rename it to proper use it. + run("$IPSET rename $loc_set $set"); + } } - # Check if the generated file exists. - if (-f $db_file) { - # Run ipset and restore the given set. - run("$IPSET restore < $db_file"); + # Store the restored set to the hash to prevent from loading it again. + $ipset_loaded_sets{$set} = "1"; +} - # Store the restored set to the hash to prevent from loading it again. - $ipset_loaded_sets{$set} = "1"; +sub ipset_call_restore ($) { + my ($file) = @_; + + # Check if the requested file exists. + if (-f $file) { + # Run ipset and restore the given set. + run("$IPSET restore -f $file"); } }