]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/API2/Firewall/VM.pm
move option definition to PVE::Firewall
[pve-firewall.git] / src / PVE / API2 / Firewall / VM.pm
index 732f6bf84cffb223e159f25f2a38e70ff8b3144a..644d6bb1a4a6303c7d3e1a9dadbc88c14ff4cf1d 100644 (file)
@@ -12,40 +12,7 @@ use Data::Dumper; # fixme: remove
 
 use base qw(PVE::RESTHandler);
 
-my $option_properties = {
-    enable => {
-       description => "Enable host firewall rules.",
-       type => 'boolean',
-       optional => 1,
-    },
-    macfilter => {
-       description => "Enable/disable MAC address filter.",
-       type => 'boolean',
-       optional => 1,
-    },
-    dhcp => {
-       description => "Enable DHCP.",
-       type => 'boolean',
-       optional => 1,
-    },
-    policy_in => {
-       description => "Input policy.",
-       type => 'string',
-       optional => 1,
-       enum => ['ACCEPT', 'REJECT', 'DROP'],
-    },
-    policy_out => { 
-       description => "Output policy.",
-       type => 'string',
-       optional => 1,
-       enum => ['ACCEPT', 'REJECT', 'DROP'],
-    },
-    log_level_in =>  get_standard_option('pve-fw-loglevel', {
-       description => "Log level for incoming traffic." }),
-    log_level_out =>  get_standard_option('pve-fw-loglevel', {
-       description => "Log level for outgoing traffic." }),
-
-};
+my $option_properties = $PVE::Firewall::vm_option_properties;
 
 my $add_option_properties = sub {
     my ($properties) = @_;
@@ -87,6 +54,8 @@ sub register_handlers {
            my $result = [
                { name => 'rules' },
                { name => 'aliases' },
+               { name => 'ipset' },
+               { name => 'refs' },
                { name => 'options' },
                ];
 
@@ -100,6 +69,9 @@ sub register_handlers {
        method => 'GET',
        description => "Get VM firewall options.",
        proxyto => 'node',
+       permissions => {
+           check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+       },
        parameters => {
            additionalProperties => 0,
            properties => {
@@ -128,6 +100,9 @@ sub register_handlers {
        description => "Set Firewall options.",
        protected => 1,
        proxyto => 'node',
+       permissions => {
+           check => ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
+       },
        parameters => {
            additionalProperties => 0,
            properties => &$add_option_properties({
@@ -232,6 +207,93 @@ sub register_handlers {
            
            return $lines; 
        }});
+
+
+    $class->register_method({
+       name => 'refs',
+       path => 'refs',
+       method => 'GET',
+       description => "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
+       permissions => {
+           check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+       },
+       parameters => {
+           additionalProperties => 0,
+           properties => {
+               node => get_standard_option('pve-node'),
+               vmid => get_standard_option('pve-vmid'),
+               type => {
+                   description => "Only list references of specified type.",
+                   type => 'string',
+                   enum => ['alias', 'ipset'],
+                   optional => 1,
+               },
+           },
+       },
+       returns => {
+           type => 'array',
+           items => {
+               type => "object",
+               properties => { 
+                   type => {
+                       type => 'string',
+                       enum => ['alias', 'ipset'],
+                   },
+                   name => {
+                       type => 'string',
+                   },
+                   comment => { 
+                       type => 'string',
+                       optional => 1,
+                   },
+               },
+           },
+       },
+       code => sub {
+           my ($param) = @_;
+           
+           my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
+           my $fw_conf = PVE::Firewall::load_vmfw_conf($cluster_conf, $rule_env, $param->{vmid});
+
+           my $ipsets = {};
+           my $aliases = {};
+
+           foreach my $conf (($cluster_conf, $fw_conf)) {
+               next if !$conf;
+               if (!$param->{type} || $param->{type} eq 'ipset') {
+                   foreach my $name (keys %{$conf->{ipset}}) {
+                       my $data = { 
+                           type => 'ipset',
+                           name => $name,
+                           ref => "+$name",
+                       };
+                       if (my $comment = $conf->{ipset_comments}->{$name}) {
+                           $data->{comment} = $comment;
+                       }
+                       $ipsets->{$name} = $data;
+                   }
+               }
+
+               if (!$param->{type} || $param->{type} eq 'alias') {
+                   foreach my $name (keys %{$conf->{aliases}}) {
+                       my $e = $conf->{aliases}->{$name};
+                       my $data = { 
+                           type => 'alias',
+                           name => $name,
+                           ref => $name,
+                       };
+                       $data->{comment} = $e->{comment} if $e->{comment};
+                       $aliases->{$name} = $data;
+                   }
+               }
+           }
+
+           my $res = [];
+           foreach my $e (values %$ipsets) { push @$res, $e; };
+           foreach my $e (values %$aliases) { push @$res, $e; };
+           
+           return $res; 
+       }});
 }
 
 package PVE::API2::Firewall::VM;