public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Carsten Haerle <carsten.haerle@straightec.de>
To: pmg-devel@lists.proxmox.com
Cc: Carsten Haerle <carsten.haerle@straightec.de>
Subject: [PATCH pmg-api 2/6] config: factor out automatically trusted networks
Date: Mon, 26 Oct 2026 10:58:47 +0100	[thread overview]
Message-ID: <20261026095852.3243241-3-carsten.haerle@straightec.de> (raw)
In-Reply-To: <20261026095852.3243241-1-carsten.haerle@straightec.de>

Move the computation of the networks that are added to mynetworks
automatically (local network, default relay, transport hosts) from
get_template_vars() into get_automatic_mynetworks(), which also records
where each entry comes from. This allows the API to show these entries,
so that the GUI can display what postfix actually trusts.

No functional change intended.

Signed-off-by: Carsten Haerle <carsten.haerle@straightec.de>
---
 src/PMG/Config.pm | 95 +++++++++++++++++++++++++++--------------------
 1 file changed, 54 insertions(+), 41 deletions(-)

diff --git a/src/PMG/Config.pm b/src/PMG/Config.pm
index 655a433..e01d4d7 100644
--- a/src/PMG/Config.pm
+++ b/src/PMG/Config.pm
@@ -1555,6 +1555,55 @@ sub get_host_dns_info {
     return $dnsinfo;
 }
 
+# Networks trusted automatically, in addition to loopback and the configured
+# networks. Returns a hash: cidr => { local => 1, relay => 1, transport => [domains] }
+sub get_automatic_mynetworks {
+    my ($self, $int_ip) = @_;
+
+    my $res = {};
+
+    if (my $tmap = PVE::INotify::read_file('transport')) {
+        foreach my $domain (sort keys %$tmap) {
+            my $host = $tmap->{$domain}->{host};
+            my $cidr;
+            if ($host =~ m/^$IPV4RE$/) {
+                $cidr = "$host/32";
+            } elsif ($host =~ m/^(?:ipv6:)?($IPV6RE)$/i) {
+                $cidr = "[$1]/128";
+            }
+            push $res->{$cidr}->{transport}->@*, $domain if defined($cidr);
+        }
+    }
+
+    # without an IP we cannot really do anything and the loopback nets are always trusted
+    if (defined($int_ip) && $self->get('mail', 'trust_local_network')) {
+        my $cidr;
+        if (my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip, 1)) {
+            if ($int_net_cidr =~ m/^($IPV6RE)\/(\d+)$/) {
+                $cidr = "[$1]/$2";
+            } else {
+                $cidr = $int_net_cidr;
+            }
+        } elsif ($int_ip =~ m/^$IPV6RE$/) {
+            $cidr = "[$int_ip]/128";
+        } else {
+            $cidr = "$int_ip/32";
+        }
+        $res->{$cidr}->{local} = 1;
+    }
+
+    # host names are not resolved, they are not trusted automatically
+    if (my $relay = $self->get('mail', 'relay')) {
+        if ($relay =~ m/^$IPV4RE$/) {
+            $res->{"$relay/32"}->{relay} = 1;
+        } elsif ($relay =~ m/^$IPV6RE$/) {
+            $res->{"[$relay]/128"}->{relay} = 1;
+        }
+    }
+
+    return $res;
+}
+
 sub get_template_vars {
     my ($self) = @_;
 
@@ -1565,45 +1614,20 @@ sub get_template_vars {
     my $int_ip = PMG::Cluster::remote_node_ip($dnsinfo->{hostname});
     $vars->{ipconfig}->{int_ip} = $int_ip;
 
+    my $automatic = $self->get_automatic_mynetworks($int_ip);
+
     my $transportnets = {};
     my $mynetworks = {
         '127.0.0.0/8' => 1,
         '[::1]/128', => 1,
     };
-
-    if (my $tmap = PVE::INotify::read_file('transport')) {
-        foreach my $domain (keys %$tmap) {
-            my $data = $tmap->{$domain};
-            my $host = $data->{host};
-            if ($host =~ m/^$IPV4RE$/) {
-                $transportnets->{"$host/32"} = 1;
-                $mynetworks->{"$host/32"} = 1;
-            } elsif ($host =~ m/^(?:ipv6:)?($IPV6RE)$/i) {
-                $transportnets->{"[$1]/128"} = 1;
-                $mynetworks->{"[$1]/128"} = 1;
-            }
-        }
+    foreach my $cidr (keys %$automatic) {
+        $mynetworks->{$cidr} = 1;
+        $transportnets->{$cidr} = 1 if $automatic->{$cidr}->{transport};
     }
 
     $vars->{postfix}->{transportnets} = join(' ', sort keys %$transportnets);
 
-    # without an IP we cannot really do anything and the loopback nets are already added
-    if (defined($int_ip) && $self->get('mail', 'trust_local_network')) {
-        if (my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip, 1)) {
-            if ($int_net_cidr =~ m/^($IPV6RE)\/(\d+)$/) {
-                $mynetworks->{"[$1]/$2"} = 1;
-            } else {
-                $mynetworks->{$int_net_cidr} = 1;
-            }
-        } else {
-            if ($int_ip =~ m/^$IPV6RE$/) {
-                $mynetworks->{"[$int_ip]/128"} = 1;
-            } else {
-                $mynetworks->{"$int_ip/32"} = 1;
-            }
-        }
-    }
-
     my $netlist = PVE::INotify::read_file('mynetworks');
     foreach my $cidr (keys %$netlist) {
         my $ip = PVE::Network::IP_from_cidr($cidr);
@@ -1618,17 +1642,6 @@ sub get_template_vars {
         }
     }
 
-    # add default relay to mynetworks
-    if (my $relay = $self->get('mail', 'relay')) {
-        if ($relay =~ m/^$IPV4RE$/) {
-            $mynetworks->{"$relay/32"} = 1;
-        } elsif ($relay =~ m/^$IPV6RE$/) {
-            $mynetworks->{"[$relay]/128"} = 1;
-        } else {
-            # DNS name - do nothing ?
-        }
-    }
-
     $vars->{postfix}->{mynetworks} = join(' ', sort keys %$mynetworks);
 
     # normalize dnsbl_sites
-- 
2.53.0








  parent reply	other threads:[~2026-10-27  9:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26  9:58 [PATCH pmg-api/pmg-gui/pmg-docs 0/6] fix #8231: mail proxy: make automatically trusted networks visible and the local network optional Carsten Haerle
2026-10-26  9:58 ` [PATCH pmg-api 1/6] fix #8231: config: add option to not trust the local network automatically Carsten Haerle
2026-10-26  9:58 ` Carsten Haerle [this message]
2026-10-26  9:58 ` [PATCH pmg-api 3/6] fix #8231: api: mynetworks: optionally list automatically trusted networks Carsten Haerle
2026-10-26  9:58 ` [PATCH pmg-api 4/6] fix #8231: config: resolve relay and transport host names for mynetworks Carsten Haerle
2026-10-26  9:58 ` [PATCH pmg-gui 5/6] fix #8231: mail proxy: add trust local network option, show automatic networks Carsten Haerle
2026-10-26  9:58 ` [PATCH pmg-docs 6/6] mail proxy: networks: document automatically trusted networks Carsten Haerle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026095852.3243241-3-carsten.haerle@straightec.de \
    --to=carsten.haerle@straightec.de \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal