]> git.proxmox.com Git - pve-firewall.git/commitdiff
start host API
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Mar 2014 10:30:53 +0000 (11:30 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Mar 2014 10:30:53 +0000 (11:30 +0100)
src/PVE/API2/Firewall/Host.pm [new file with mode: 0644]
src/PVE/API2/Firewall/Makefile
src/PVE/Firewall.pm

diff --git a/src/PVE/API2/Firewall/Host.pm b/src/PVE/API2/Firewall/Host.pm
new file mode 100644 (file)
index 0000000..e82604e
--- /dev/null
@@ -0,0 +1,113 @@
+package PVE::API2::Firewall::Host;
+
+use strict;
+use warnings;
+use PVE::JSONSchema qw(get_standard_option);
+
+use PVE::Firewall;
+
+
+use Data::Dumper; # fixme: remove
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    permissions => { user => 'all' },
+    description => "Directory index.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {},
+       },
+       links => [ { rel => 'child', href => "{name}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $result = [
+           { name => 'rules' },
+           { name => 'options' },
+           ];
+
+       return $result;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'get_rules',
+    path => 'rules',
+    method => 'GET',
+    description => "List host firewall rules.",
+    proxyto => 'node',
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {},
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $hostfw_conf = PVE::Firewall::load_hostfw_conf();
+
+       my $rules = $hostfw_conf->{rules} || [];
+
+       my $digest = $hostfw_conf->{digest};
+
+       my $res = [];
+
+       my $ind = 0;
+       foreach my $rule (@$rules) {
+           push @$res, PVE::Firewall::cleanup_fw_rule($rule, $digest, $ind++);
+       }
+
+       return $res;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'get_options',
+    path => 'options',
+    method => 'GET',
+    description => "Get host firewall options.",
+    proxyto => 'node',
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+       },
+    },
+    returns => {
+       type => "object",
+       properties => {},
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $hostfw_conf = PVE::Firewall::load_hostfw_conf();
+
+       my $options = $hostfw_conf->{options} || {};
+
+       my $digest = $hostfw_conf->{digest};
+
+       $options->{digest} = $digest;
+
+       return $options;
+    }});
+
+1;
index bb57ab9bcd39a077d91bb885458c584077bb4254..062f31f6f8b98cde99e052f1f7b183ba59f43860 100644 (file)
@@ -1,4 +1,5 @@
 LIB_SOURCES=                   \
+       Host.pm                 \
        Groups.pm
 
 all:
index 7e3daada50b171af51aa9c0a1f6b5f385459563f..3c8d687bf008f4f43afcb53a8476ab8d14146832 100644 (file)
@@ -1448,7 +1448,11 @@ sub parse_host_fw_rules {
 
     my $section;
 
+    my $digest = Digest::SHA->new('sha1');
+
     while (defined(my $line = <$fh>)) {
+       $digest->add($line);
+
        next if $line =~ m/^#/;
        next if $line =~ m/^\s*$/;
 
@@ -1486,6 +1490,8 @@ sub parse_host_fw_rules {
        push @{$res->{$section}}, @$rules;
     }
 
+    $res->{digest} = $digest->b64digest;
+
     return $res;
 }
 
@@ -1706,6 +1712,16 @@ sub load_security_groups {
     return $groups_conf;
 }
 
+sub load_hostfw_conf {
+
+    my $hostfw_conf = {};
+    my $filename = "/etc/pve/local/host.fw";
+    if (my $fh = IO::File->new($filename, O_RDONLY)) {
+       $hostfw_conf = parse_host_fw_rules($filename, $fh);
+    }
+    return $hostfw_conf;
+}
+
 sub compile {
     my $vmdata = read_local_vm_config();
     my $vmfw_configs = read_vm_firewall_configs($vmdata);
@@ -1721,14 +1737,8 @@ sub compile {
 
     ruleset_create_chain($ruleset, "PVEFW-FORWARD");
 
-    my $hostfw_options = {};
-    my $hostfw_conf = {};
-
-    my $filename = "/etc/pve/local/host.fw";
-    if (my $fh = IO::File->new($filename, O_RDONLY)) {
-       $hostfw_conf = parse_host_fw_rules($filename, $fh);
-       $hostfw_options = $hostfw_conf->{options};
-    }
+    my $hostfw_conf = load_hostfw_conf();
+    my $hostfw_options = $hostfw_conf->{options} || {};
 
     generate_std_chains($ruleset, $hostfw_options);