]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/API2/Firewall/IPSet.pm
bump version to 5.0.3
[pve-firewall.git] / src / PVE / API2 / Firewall / IPSet.pm
index 6c7ab5e6c681fdf76aa34122e5e5bf4b40dd674f..ed92d877ee0685726f69fc5f807c3a316a925dbe 100644 (file)
@@ -9,10 +9,10 @@ use PVE::Firewall;
 
 use base qw(PVE::RESTHandler);
 
-my $api_properties = { 
+my $api_properties = {
     cidr => {
        description => "Network/IP specification in CIDR format.",
-       type => 'string', format => 'IPv4orCIDRorAlias',
+       type => 'string', format => 'IPorCIDRorAlias',
     },
     name => get_standard_option('ipset-name'),
     comment => {
@@ -25,6 +25,12 @@ my $api_properties = {
     },
 };
 
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    die "implement this in subclass";
+}
+
 sub load_config {
     my ($class, $param) = @_;
 
@@ -39,6 +45,12 @@ sub save_config {
     die "implement this in subclass";
 }
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    die "implement this in subclass";
+}
+
 sub save_ipset {
     my ($class, $param, $fw_conf, $ipset) = @_;
 
@@ -79,6 +91,7 @@ sub register_get_ipset {
        path => '',
        method => 'GET',
        description => "List IPSet content",
+       permissions => PVE::Firewall::rules_audit_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -99,7 +112,7 @@ sub register_get_ipset {
                        type => 'boolean',
                        optional => 1,
                    },
-                   digest => get_standard_option('pve-config-digest', { optional => 0} ),      
+                   digest => get_standard_option('pve-config-digest', { optional => 0} ),
                },
            },
            links => [ { rel => 'child', href => "{cidr}" } ],
@@ -119,6 +132,11 @@ sub register_delete_ipset {
     my $properties = $class->additional_parameters();
 
     $properties->{name} = get_standard_option('ipset-name');
+    $properties->{force} = {
+       type => 'boolean',
+       optional => 1,
+       description => 'Delete all members of the IPSet, if there are any.',
+    };
 
     $class->register_method({
        name => 'delete_ipset',
@@ -126,6 +144,7 @@ sub register_delete_ipset {
        method => 'DELETE',
        description => "Delete IPSet",
        protected => 1,
+       permissions => PVE::Firewall::rules_modify_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -133,29 +152,23 @@ sub register_delete_ipset {
        returns => { type => 'null' },
        code => sub {
            my ($param) = @_;
-           
-           my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
 
-           die "IPSet '$param->{name}' is not empty\n" 
-               if scalar(@$ipset);
+           $class->lock_config($param, sub {
+               my ($param) = @_;
+
+               my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
 
-           $class->save_ipset($param, $fw_conf, undef);
+               die "IPSet '$param->{name}' is not empty\n"
+                   if scalar(@$ipset) && !$param->{force};
+
+               $class->save_ipset($param, $fw_conf, undef);
+
+           });
 
            return undef;
        }});
 }
 
-my $verify_alias_exists = sub {
-    my ($cluster_conf, $fw_conf, $cidr) = @_;
-
-    if ($cidr !~ m/^\d/) {
-       my $alias = lc($cidr);
-       die "no such alias '$cidr'\n"
-           if !(($cluster_conf && $cluster_conf->{aliases}->{$alias}) || 
-                ($fw_conf && $fw_conf->{aliases}->{$alias}));
-    }
-};
-
 sub register_create_ip {
     my ($class) = @_;
 
@@ -172,6 +185,7 @@ sub register_create_ip {
        method => 'POST',
        description => "Add IP or Network to IPSet.",
        protected => 1,
+       permissions => PVE::Firewall::rules_modify_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -180,25 +194,42 @@ sub register_create_ip {
        code => sub {
            my ($param) = @_;
 
-           my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
+           $class->lock_config($param, sub {
+               my ($param) = @_;
+
+               my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
+
+               my $cidr = $param->{cidr};
+               if ($cidr =~ m@^(dc/|guest/)?(${PVE::Firewall::ip_alias_pattern})$@) {
+                   my $scope = $1 // "";
+                   my $alias = $2;
+                   # make sure alias exists (if $cidr is an alias)
+                   PVE::Firewall::resolve_alias($cluster_conf, $fw_conf, $alias, $scope);
+               } else {
+                   $cidr = PVE::Firewall::clean_cidr($cidr);
+                   # normalize like config parser, otherwise duplicates might slip through
+                   $cidr = PVE::Firewall::parse_ip_or_cidr($cidr);
+               }
 
-           my $cidr = $param->{cidr};
-           
-           foreach my $entry (@$ipset) {
-               raise_param_exc({ cidr => "address '$cidr' already exists" }) 
-                   if $entry->{cidr} eq $cidr;
-           }
+               foreach my $entry (@$ipset) {
+                   raise_param_exc({ cidr => "address '$cidr' already exists" })
+                       if $entry->{cidr} eq $cidr;
+               }
+
+               raise_param_exc({ cidr => "a zero prefix is not allowed in ipset entries" })
+                   if $cidr =~ m!/0+$!;
 
-           &$verify_alias_exists($cluster_conf, $fw_conf, $cidr);
 
-           my $data = { cidr => $cidr };
+               my $data = { cidr => $cidr };
 
-           $data->{nomatch} = 1 if $param->{nomatch};
-           $data->{comment} = $param->{comment} if $param->{comment};
+               $data->{nomatch} = 1 if $param->{nomatch};
+               $data->{comment} = $param->{comment} if $param->{comment};
 
-           unshift @$ipset, $data;
+               unshift @$ipset, $data;
 
-           $class->save_ipset($param, $fw_conf, $ipset);
+               $class->save_ipset($param, $fw_conf, $ipset);
+
+           });
 
            return undef;
        }});
@@ -211,12 +242,13 @@ sub register_read_ip {
 
     $properties->{name} = $api_properties->{name};
     $properties->{cidr} = $api_properties->{cidr};
-    
+
     $class->register_method({
        name => 'read_ip',
        path => '{cidr}',
        method => 'GET',
        description => "Read IP or Network settings from IPSet.",
+       permissions => PVE::Firewall::rules_audit_permissions($class->rule_env()),
        protected => 1,
        parameters => {
            additionalProperties => 0,
@@ -257,6 +289,7 @@ sub register_update_ip {
        method => 'PUT',
        description => "Update IP or Network settings",
        protected => 1,
+       permissions => PVE::Firewall::rules_modify_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -265,19 +298,27 @@ sub register_update_ip {
        code => sub {
            my ($param) = @_;
 
-           my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
+           my $found = $class->lock_config($param, sub {
+               my ($param) = @_;
 
-           my (undef, $digest) = PVE::Firewall::copy_list_with_digest($ipset);
-           PVE::Tools::assert_if_modified($digest, $param->{digest});
+               my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
 
-           foreach my $entry (@$ipset) {
-               if($entry->{cidr} eq $param->{cidr}) {
-                   $entry->{nomatch} = $param->{nomatch};
-                   $entry->{comment} = $param->{comment};
-                   $class->save_ipset($param, $fw_conf, $ipset);
-                   return;
+               my (undef, $digest) = PVE::Firewall::copy_list_with_digest($ipset);
+               PVE::Tools::assert_if_modified($digest, $param->{digest});
+
+               foreach my $entry (@$ipset) {
+                   if($entry->{cidr} eq $param->{cidr}) {
+                       $entry->{nomatch} = $param->{nomatch};
+                       $entry->{comment} = $param->{comment};
+                       $class->save_ipset($param, $fw_conf, $ipset);
+                       return 1;
+                   }
                }
-           }
+
+               return 0;
+           });
+
+           return if $found;
 
            raise_param_exc({ cidr => "no such IP/Network" });
        }});
@@ -298,6 +339,7 @@ sub register_delete_ip {
        method => 'DELETE',
        description => "Remove IP or Network from IPSet.",
        protected => 1,
+       permissions => PVE::Firewall::rules_modify_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -306,19 +348,23 @@ sub register_delete_ip {
        code => sub {
            my ($param) = @_;
 
-           my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
+           $class->lock_config($param, sub {
+               my ($param) = @_;
 
-           my (undef, $digest) = PVE::Firewall::copy_list_with_digest($ipset);
-           PVE::Tools::assert_if_modified($digest, $param->{digest});
+               my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param);
 
-           my $new = [];
-   
-           foreach my $entry (@$ipset) {
-               push @$new, $entry if $entry->{cidr} ne $param->{cidr};
-           }
+               my (undef, $digest) = PVE::Firewall::copy_list_with_digest($ipset);
+               PVE::Tools::assert_if_modified($digest, $param->{digest});
+
+               my $new = [];
+
+               foreach my $entry (@$ipset) {
+                   push @$new, $entry if $entry->{cidr} ne $param->{cidr};
+               }
+
+               $class->save_ipset($param, $fw_conf, $new);
+           });
 
-           $class->save_ipset($param, $fw_conf, $new);
-           
            return undef;
        }});
 }
@@ -341,6 +387,18 @@ use warnings;
 
 use base qw(PVE::API2::Firewall::IPSetBase);
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'cluster';
+}
+
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_clusterfw_conf(10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
 
@@ -367,11 +425,23 @@ use PVE::JSONSchema qw(get_standard_option);
 
 use base qw(PVE::API2::Firewall::IPSetBase);
 
-__PACKAGE__->additional_parameters({ 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'vm';
+}
+
+__PACKAGE__->additional_parameters({
     node => get_standard_option('pve-node'),
-    vmid => get_standard_option('pve-vmid'),                              
+    vmid => get_standard_option('pve-vmid'),
 });
 
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_vmfw_conf($param->{vmid}, 10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
 
@@ -399,11 +469,23 @@ use PVE::JSONSchema qw(get_standard_option);
 
 use base qw(PVE::API2::Firewall::IPSetBase);
 
-__PACKAGE__->additional_parameters({ 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'ct';
+}
+
+__PACKAGE__->additional_parameters({
     node => get_standard_option('pve-node'),
-    vmid => get_standard_option('pve-vmid'),                              
+    vmid => get_standard_option('pve-vmid'),
 });
 
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_vmfw_conf($param->{vmid}, 10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
 
@@ -433,9 +515,15 @@ use PVE::Firewall;
 
 use base qw(PVE::RESTHandler);
 
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    die "implement this in subclass";
+}
+
 sub load_config {
     my ($class, $param) = @_;
+
     die "implement this in subclass";
 
     #return ($cluster_conf, $fw_conf);
@@ -447,6 +535,12 @@ sub save_config {
     die "implement this in subclass";
 }
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    die "implement this in subclass";
+}
+
 my $additional_param_hash_list = {};
 
 sub additional_parameters {
@@ -467,8 +561,8 @@ my $get_ipset_list = sub {
     my ($fw_conf) = @_;
 
     my $res = [];
-    foreach my $name (keys %{$fw_conf->{ipset}}) {
-       my $data = { 
+    foreach my $name (sort keys %{$fw_conf->{ipset}}) {
+       my $data = {
            name => $name,
        };
        if (my $comment = $fw_conf->{ipset_comments}->{$name}) {
@@ -492,6 +586,7 @@ sub register_index {
        path => '',
        method => 'GET',
        description => "List IPSets",
+       permissions => PVE::Firewall::rules_audit_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -500,10 +595,10 @@ sub register_index {
            type => 'array',
            items => {
                type => "object",
-               properties => { 
+               properties => {
                    name => get_standard_option('ipset-name'),
                    digest => get_standard_option('pve-config-digest', { optional => 0} ),
-                   comment => { 
+                   comment => {
                        type => 'string',
                        optional => 1,
                    }
@@ -513,10 +608,10 @@ sub register_index {
        },
        code => sub {
            my ($param) = @_;
-           
+
            my ($cluster_conf, $fw_conf) = $class->load_config($param);
 
-           return &$get_ipset_list($fw_conf); 
+           return &$get_ipset_list($fw_conf);
        }});
 }
 
@@ -541,6 +636,7 @@ sub register_create {
        method => 'POST',
        description => "Create new IPSet",
        protected => 1,
+       permissions => PVE::Firewall::rules_modify_permissions($class->rule_env()),
        parameters => {
            additionalProperties => 0,
            properties => $properties,
@@ -548,33 +644,42 @@ sub register_create {
        returns => { type => 'null' },
        code => sub {
            my ($param) = @_;
-           
-           my ($cluster_conf, $fw_conf) = $class->load_config($param);
 
-           if ($param->{rename}) {
-               my (undef, $digest) = &$get_ipset_list($fw_conf);
-               PVE::Tools::assert_if_modified($digest, $param->{digest});
+           $class->lock_config($param, sub {
+               my ($param) = @_;
 
-               raise_param_exc({ name => "IPSet '$param->{rename}' does not exists" }) 
-                   if !$fw_conf->{ipset}->{$param->{rename}};
+               my ($cluster_conf, $fw_conf) = $class->load_config($param);
 
-               my $data = delete $fw_conf->{ipset}->{$param->{rename}};
-               $fw_conf->{ipset}->{$param->{name}} = $data;
-               if (my $comment = delete $fw_conf->{ipset_comments}->{$param->{rename}}) {
-                   $fw_conf->{ipset_comments}->{$param->{name}} = $comment;
-               }
-               $fw_conf->{ipset_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
-           } else { 
-               foreach my $name (keys %{$fw_conf->{ipset}}) {
-                   raise_param_exc({ name => "IPSet '$name' already exists" }) 
-                       if $name eq $param->{name};
-               }
+               if ($param->{rename}) {
+                   my (undef, $digest) = &$get_ipset_list($fw_conf);
+                   PVE::Tools::assert_if_modified($digest, $param->{digest});
 
-               $fw_conf->{ipset}->{$param->{name}} = [];
-               $fw_conf->{ipset_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
-           }
+                   raise_param_exc({ name => "IPSet '$param->{rename}' does not exist" })
+                       if !$fw_conf->{ipset}->{$param->{rename}};
 
-           $class->save_config($param, $fw_conf);
+                   # prevent overwriting existing ipset
+                   raise_param_exc({ name => "IPSet '$param->{name}' does already exist"})
+                       if $fw_conf->{ipset}->{$param->{name}} &&
+                       $param->{name} ne $param->{rename};
+
+                   my $data = delete $fw_conf->{ipset}->{$param->{rename}};
+                   $fw_conf->{ipset}->{$param->{name}} = $data;
+                   if (my $comment = delete $fw_conf->{ipset_comments}->{$param->{rename}}) {
+                       $fw_conf->{ipset_comments}->{$param->{name}} = $comment;
+                   }
+                   $fw_conf->{ipset_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
+               } else {
+                   foreach my $name (keys %{$fw_conf->{ipset}}) {
+                       raise_param_exc({ name => "IPSet '$name' already exists" })
+                           if $name eq $param->{name};
+                   }
+
+                   $fw_conf->{ipset}->{$param->{name}} = [];
+                   $fw_conf->{ipset_comments}->{$param->{name}} = $param->{comment} if defined($param->{comment});
+               }
+
+               $class->save_config($param, $fw_conf);
+           });
 
            return undef;
        }});
@@ -595,9 +700,21 @@ use PVE::Firewall;
 
 use base qw(PVE::API2::Firewall::BaseIPSetList);
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'cluster';
+}
+
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_clusterfw_conf(10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
+
     my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
     return (undef, $cluster_conf);
 }
@@ -611,10 +728,10 @@ sub save_config {
 __PACKAGE__->register_handlers();
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Firewall::ClusterIPset",  
+    subclass => "PVE::API2::Firewall::ClusterIPset",
     path => '{name}',
-    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/' 
-    fragmentDelimiter => '', 
+    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/'
+    fragmentDelimiter => '',
 });
 
 package PVE::API2::Firewall::VMIPSetList;
@@ -626,14 +743,26 @@ use PVE::Firewall;
 
 use base qw(PVE::API2::Firewall::BaseIPSetList);
 
-__PACKAGE__->additional_parameters({ 
+__PACKAGE__->additional_parameters({
     node => get_standard_option('pve-node'),
-    vmid => get_standard_option('pve-vmid'),                              
+    vmid => get_standard_option('pve-vmid'),
 });
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'vm';
+}
+
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_vmfw_conf($param->{vmid}, 10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
+
     my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
     my $fw_conf = PVE::Firewall::load_vmfw_conf($cluster_conf, 'vm', $param->{vmid});
     return ($cluster_conf, $fw_conf);
@@ -648,10 +777,10 @@ sub save_config {
 __PACKAGE__->register_handlers();
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Firewall::VMIPset",  
+    subclass => "PVE::API2::Firewall::VMIPset",
     path => '{name}',
-    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/' 
-    fragmentDelimiter => '', 
+    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/'
+    fragmentDelimiter => '',
 });
 
 package PVE::API2::Firewall::CTIPSetList;
@@ -663,14 +792,26 @@ use PVE::Firewall;
 
 use base qw(PVE::API2::Firewall::BaseIPSetList);
 
-__PACKAGE__->additional_parameters({ 
+__PACKAGE__->additional_parameters({
     node => get_standard_option('pve-node'),
-    vmid => get_standard_option('pve-vmid'),                              
+    vmid => get_standard_option('pve-vmid'),
 });
 
+sub rule_env {
+    my ($class, $param) = @_;
+
+    return 'ct';
+}
+
+sub lock_config {
+    my ($class, $param, $code) = @_;
+
+    PVE::Firewall::lock_vmfw_conf($param->{vmid}, 10, $code, $param);
+}
+
 sub load_config {
     my ($class, $param) = @_;
+
     my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
     my $fw_conf = PVE::Firewall::load_vmfw_conf($cluster_conf, 'ct', $param->{vmid});
     return ($cluster_conf, $fw_conf);
@@ -685,10 +826,10 @@ sub save_config {
 __PACKAGE__->register_handlers();
 
 __PACKAGE__->register_method ({
-    subclass => "PVE::API2::Firewall::CTIPset",  
+    subclass => "PVE::API2::Firewall::CTIPset",
     path => '{name}',
-    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/' 
-    fragmentDelimiter => '', 
+    # set fragment delimiter (no subdirs) - we need that, because CIDR address contain a slash '/'
+    fragmentDelimiter => '',
 });
 
 1;