[pmg-devel] [PATCH pmg-api v2] fix #4410: Remove non-null host-bits from CIDR when reading `mynetworks`
Christoph Heiss
c.heiss at proxmox.com
Tue Dec 27 13:29:15 CET 2022
This will simply drop non-null host bits when reading the config file,
thus preserving backwards-compatibility.
When creating new entries, invalid CIDRs are now rejected to prevent
creation of such entries in the future.
In the GUI, the entries are displayed as the user entered them (as
suggested my Stoiko). This is done by considering /etc/pmg/mynetworks
as the "source of truth" - all entries are saved there verbatim, but
when writing the postfix config the right ones are picked.
Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
src/PMG/API2/MyNetworks.pm | 10 ++++++++--
src/PMG/Config.pm | 15 ++++++++-------
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/PMG/API2/MyNetworks.pm b/src/PMG/API2/MyNetworks.pm
index 975ca2e..325e59b 100644
--- a/src/PMG/API2/MyNetworks.pm
+++ b/src/PMG/API2/MyNetworks.pm
@@ -3,6 +3,7 @@ package PMG::API2::MyNetworks;
use strict;
use warnings;
use Data::Dumper;
+use Net::IP;
use PVE::SafeSyslog;
use PVE::Tools qw(extract_param);
@@ -10,6 +11,7 @@ use HTTP::Status qw(:constants);
use PVE::JSONSchema qw(get_standard_option);
use PVE::RESTHandler;
use PVE::INotify;
+use PVE::Network;
use PMG::Config;
@@ -77,13 +79,17 @@ __PACKAGE__->register_method ({
my ($param) = @_;
my $code = sub {
+ die "invalid network adress '$param->{cidr}', host-bits must be null\n"
+ if !Net::IP::ip_normalize($param->{cidr});
my $mynetworks = PVE::INotify::read_file('mynetworks');
+ my $ip = PVE::Network::IP_from_cidr($param->{cidr});
die "trusted network '$param->{cidr}' already exists\n"
- if $mynetworks->{$param->{cidr}};
+ if $mynetworks->{$ip->prefix()};
- $mynetworks->{$param->{cidr}} = {
+ $mynetworks->{$ip->prefix()} = {
+ cidr => $param->{cidr},
comment => $param->{comment} // '',
};
diff --git a/src/PMG/Config.pm b/src/PMG/Config.pm
index 9ba5c76..a29060b 100755
--- a/src/PMG/Config.pm
+++ b/src/PMG/Config.pm
@@ -730,6 +730,7 @@ use PVE::SafeSyslog;
use PVE::Tools qw($IPV4RE $IPV6RE);
use PVE::INotify;
use PVE::JSONSchema;
+use PVE::Network;
use PMG::Cluster;
use PMG::Utils;
@@ -1008,13 +1009,13 @@ sub read_pmg_mynetworks {
while (defined(my $line = <$fh>)) {
chomp $line;
next if $line =~ m/^\s*$/;
- if ($line =~ m!^((?:$IPV4RE|$IPV6RE))/(\d+)\s*(?:#(.*)\s*)?$!) {
- my ($network, $prefix_size, $comment) = ($1, $2, $3);
- my $cidr = "$network/${prefix_size}";
- $mynetworks->{$cidr} = {
+ if ($line =~ m!^((?:$IPV4RE|$IPV6RE)/\d+)\s*(?:#(.*)\s*)?$!) {
+ my ($cidr, $comment) = ($1, $2);
+ my $ip = PVE::Network::IP_from_cidr($cidr);
+ $mynetworks->{$ip->prefix()} = {
cidr => $cidr,
- network_address => $network,
- prefix_size => $prefix_size,
+ network_address => $ip->short(),
+ prefix_size => $ip->prefixlen(),
comment => $comment // '',
};
} else {
@@ -1032,7 +1033,7 @@ sub write_pmg_mynetworks {
foreach my $cidr (sort keys %$mynetworks) {
my $data = $mynetworks->{$cidr};
my $comment = $data->{comment} // '*';
- PVE::Tools::safe_print($filename, $fh, "$cidr #$comment\n");
+ PVE::Tools::safe_print($filename, $fh, "$data->{cidr} #$comment\n");
}
}
--
2.30.2
More information about the pmg-devel
mailing list