From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id AA34B741D4 for ; Tue, 31 May 2022 13:52:55 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9C2C88AB4 for ; Tue, 31 May 2022 13:52:55 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 012E18AA4 for ; Tue, 31 May 2022 13:52:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 951994397B for ; Tue, 31 May 2022 13:52:54 +0200 (CEST) From: Dominik Csapak To: pmg-devel@lists.proxmox.com Date: Tue, 31 May 2022 13:52:53 +0200 Message-Id: <20220531115253.3398902-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.112 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pmg-devel] [PATCH pmg-api] config: add ips/nets uniquely to the template variables X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 May 2022 11:52:55 -0000 otherwise a config with many entries such as: domain1: ip1 domain2: ip1 domain3: ip1 etc. unnecessarily adds 'ip1' multiple times to the 'mynetworks' variable to keep the output sorted (so it's stable) move the sort to the 'join' Signed-off-by: Dominik Csapak --- src/PMG/Config.pm | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/PMG/Config.pm b/src/PMG/Config.pm index 31f9c6f..9ba5c76 100755 --- a/src/PMG/Config.pm +++ b/src/PMG/Config.pm @@ -1297,63 +1297,65 @@ sub get_template_vars { my $int_ip = PMG::Cluster::remote_node_ip($dnsinfo->{hostname}); $vars->{ipconfig}->{int_ip} = $int_ip; - my $transportnets = []; + my $transportnets = {}; + my $mynetworks = { + '127.0.0.0/8' => 1, + '[::1]/128', => 1, + }; if (my $tmap = PVE::INotify::read_file('transport')) { - foreach my $domain (sort keys %$tmap) { + foreach my $domain (keys %$tmap) { my $data = $tmap->{$domain}; my $host = $data->{host}; if ($host =~ m/^$IPV4RE$/) { - push @$transportnets, "$host/32"; + $transportnets->{"$host/32"} = 1; + $mynetworks->{"$host/32"} = 1; } elsif ($host =~ m/^(?:ipv6:)?($IPV6RE)$/i) { - push @$transportnets, "[$1]/128"; + $transportnets->{"[$1]/128"} = 1; + $mynetworks->{"[$1]/128"} = 1; } } } - $vars->{postfix}->{transportnets} = join(' ', @$transportnets); - - my $mynetworks = [ '127.0.0.0/8', '[::1]/128' ]; + $vars->{postfix}->{transportnets} = join(' ', sort keys %$transportnets); if (defined($int_ip)) { # we cannot really do anything and the loopback nets are already added if (my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip, 1)) { if ($int_net_cidr =~ m/^($IPV6RE)\/(\d+)$/) { - push @$mynetworks, "[$1]/$2"; + $mynetworks->{"[$1]/$2"} = 1; } else { - push @$mynetworks, $int_net_cidr; + $mynetworks->{$int_net_cidr} = 1; } } else { if ($int_ip =~ m/^$IPV6RE$/) { - push @$mynetworks, "[$int_ip]/128"; + $mynetworks->{"[$int_ip]/128"} = 1; } else { - push @$mynetworks, "$int_ip/32"; + $mynetworks->{"$int_ip/32"} = 1; } } } my $netlist = PVE::INotify::read_file('mynetworks'); - foreach my $cidr (sort keys %$netlist) { + foreach my $cidr (keys %$netlist) { if ($cidr =~ m/^($IPV6RE)\/(\d+)$/) { - push @$mynetworks, "[$1]/$2"; + $mynetworks->{"[$1]/$2"} = 1; } else { - push @$mynetworks, $cidr; + $mynetworks->{$cidr} = 1; } } - push @$mynetworks, @$transportnets; - # add default relay to mynetworks if (my $relay = $self->get('mail', 'relay')) { if ($relay =~ m/^$IPV4RE$/) { - push @$mynetworks, "$relay/32"; + $mynetworks->{"$relay/32"} = 1; } elsif ($relay =~ m/^$IPV6RE$/) { - push @$mynetworks, "[$relay]/128"; + $mynetworks->{"[$relay]/128"} = 1; } else { # DNS name - do nothing ? } } - $vars->{postfix}->{mynetworks} = join(' ', @$mynetworks); + $vars->{postfix}->{mynetworks} = join(' ', sort keys %$mynetworks); # normalize dnsbl_sites my @dnsbl_sites = PVE::Tools::split_list($vars->{pmg}->{mail}->{dnsbl_sites}); -- 2.30.2