]> git.proxmox.com Git - pve-firewall.git/blobdiff - pvefw
rename ./pvefw enabletaprules -> ./pvefw enablevmfw
[pve-firewall.git] / pvefw
diff --git a/pvefw b/pvefw
index 4ac96796e7331f32bbd293fafce45692f4f93478..2d4d4503481824e6c1103d9c560771f04705386e 100755 (executable)
--- a/pvefw
+++ b/pvefw
@@ -3,15 +3,11 @@
 use strict;
 use lib qw(.);
 use PVE::Firewall;
-use File::Path;
-use IO::File;
-use Data::Dumper;
 
 use PVE::SafeSyslog;
 use PVE::Cluster;
 use PVE::INotify;
 use PVE::RPCEnvironment;
-use PVE::QemuServer;
 
 use PVE::JSONSchema qw(get_standard_option);
 
@@ -33,127 +29,112 @@ $rpcenv->init_request();
 $rpcenv->set_language($ENV{LANG});
 $rpcenv->set_user('root@pam');
 
+__PACKAGE__->register_method({
+    name => 'enablevmfw',
+    path => 'enablevmfw',
+    method => 'POST',
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            vmid => get_standard_option('pve-vmid'),
+            netid => {
+                type => 'string',
+               optional => 1
+            },
+        },
+    },
+    returns => { type => 'null' },
+    code => sub {
+        my ($param) = @_;
 
-sub parse_fw_rules {
-    my ($filename, $fh) = @_;
-
-    my $section;
-
-    my $res = { in => [], out => [] };
-
-    my $macros = PVE::Firewall::get_shorewall_macros();
+        # test if VM exists
+        my $vmid = $param->{vmid};
+        my $netid = $param->{netid};
 
-    while (defined(my $line = <$fh>)) {
-       next if $line =~ m/^#/;
-       next if $line =~ m/^\s*$/;
+       my $conf = PVE::QemuServer::load_config($vmid);
 
-       if ($line =~ m/^\[(in|out)\]\s*$/i) {
-           $section = lc($1);
-           next;
+       foreach my $opt (keys %$conf) {
+            next if $opt !~ m/^net(\d+)$/;
+            my $net = PVE::QemuServer::parse_net($conf->{$opt});
+            next if !$net;
+           next if $netid && $opt != $netid;
+           PVE::Firewall::generate_tap_rules($net, $opt, $vmid);
        }
-       next if !$section;
 
-       my ($action, $iface, $source, $dest, $proto, $dport, $sport) =
-           split(/\s+/, $line);
+        return undef;
+    }});
 
-       if (!$action) {
-           warn "skip incomplete line\n";
-           next;
-       }
+__PACKAGE__->register_method({
+    name => 'disablevmfw',
+    path => 'disablevmfw',
+    method => 'POST',
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            vmid => get_standard_option('pve-vmid'),
+            netid => {
+                type => 'string',
+               optional => 1
+            },
+
+        },
+    },
+    returns => { type => 'null' },
+    code => sub {
+        my ($param) = @_;
 
-       my $service;
-       if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
-           # OK
-       } elsif ($action =~ m/^(\S+)\((ACCEPT|DROP|REJECT)\)$/) {
-           ($service, $action) = ($1, $2);
-           if (!$macros->{$service}) {
-               warn "unknown service '$service'\n";
-               next;
-           }
-       } else {
-           warn "unknown action '$action'\n";
-           next;
-       }
+        # test if VM exists
+        my $vmid = $param->{vmid};
+        my $netid = $param->{netid};
 
-       $iface = undef if $iface && $iface eq '-';
-       if ($iface && $iface !~ m/^(net0|net1|net2|net3|net4|net5)$/) {
-           warn "unknown interface '$iface'\n";
-           next;
-       }
+       my $conf = PVE::QemuServer::load_config($vmid);
 
-       $proto = undef if $proto && $proto eq '-';
-       if ($proto && $proto !~ m/^(icmp|tcp|udp)$/) {
-           warn "unknown protokol '$proto'\n";
-           next;
+       foreach my $opt (keys %$conf) {
+            next if $opt !~ m/^net(\d+)$/;
+            my $net = PVE::QemuServer::parse_net($conf->{$opt});
+            next if !$net;
+           next if $netid && $opt != $netid;
+           PVE::Firewall::flush_tap_rules($net, $opt, $vmid);
        }
 
-       $source = undef if $source && $source eq '-';
-
-#      if ($source !~ m/^(XYZ)$/) {
-#          warn "unknown source '$source'\n";
-#          next;
-#      }
-
-       $dest = undef if $dest && $dest eq '-';
-#      if ($dest !~ m/^XYZ)$/) {
-#          warn "unknown destination '$dest'\n";
-#          next;
-#      }
-
-       $dport = undef if $dport && $dport eq '-';
-       $sport = undef if $sport && $sport eq '-';
-
-       my $rule = {
-           action => $action,
-           service => $service,
-           iface => $iface,
-           source => $source,
-           dest => $dest,
-           proto => $proto,
-           dport => $dport,
-           sport => $sport,
-       };
-
-       push @{$res->{$section}}, $rule;
-    }
-
-    return $res;
-}
-
-sub read_local_vm_config {
-
-    my $openvz = {};
+        return undef;
+    }});
 
-    my $qemu = {};
+__PACKAGE__->register_method({
+    name => 'enablehostfw',
+    path => 'enablehostfw',
+    method => 'POST',
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null' },
 
-    my $list = PVE::QemuServer::config_list();
+    code => sub {
+       my ($param) = @_;
 
-    foreach my $vmid (keys %$list) {
-       # next if $vmid ne '100';
-       my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
-       if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
-           $qemu->{$vmid} = $conf;
-       }
-    }
+       PVE::Firewall::enablehostfw();
 
-    my $vmdata = { openvz => $openvz, qemu => $qemu };
+       return undef;
+    }});
 
-    return $vmdata;
-};
+__PACKAGE__->register_method({
+    name => 'disablehostfw',
+    path => 'disablehostfw',
+    method => 'POST',
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null' },
 
-sub read_vm_firewall_rules {
-    my ($vmdata) = @_;
-    my $rules = {};
-    foreach my $vmid (keys %{$vmdata->{qemu}}, keys %{$vmdata->{openvz}}) {
-       my $filename = "/etc/pve/firewall/$vmid.fw";
-       my $fh = IO::File->new($filename, O_RDONLY);
-       next if !$fh;
+    code => sub {
+       my ($param) = @_;
 
-       $rules->{$vmid} = parse_fw_rules($filename, $fh);
-    }
+       PVE::Firewall::disablehostfw();
 
-    return $rules;
-}
+       return undef;
+    }});
 
 __PACKAGE__->register_method ({
     name => 'compile',
@@ -169,27 +150,35 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $vmdata = read_local_vm_config();
-       my $rules = read_vm_firewall_rules($vmdata);
+       PVE::Firewall::compile();
 
-       # print Dumper($vmdata);
+       return undef;
+    }});
 
-       my $swdir = '/etc/shorewall';
-       mkdir $swdir;
+__PACKAGE__->register_method ({
+    name => 'start',
+    path => 'start',
+    method => 'POST',
+    description => "Start firewall.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null' },
 
-       PVE::Firewall::compile($swdir, $vmdata, $rules);
+    code => sub {
+       my ($param) = @_;
 
-       PVE::Tools::run_command(['shorewall', 'compile']);
+       PVE::Firewall::compile_and_start();
 
        return undef;
-
     }});
 
 __PACKAGE__->register_method ({
-    name => 'start',
-    path => 'start',
+    name => 'restart',
+    path => 'restart',
     method => 'POST',
-    description => "Start firewall.",
+    description => "Restart firewall.",
     parameters => {
        additionalProperties => 0,
        properties => {},
@@ -199,7 +188,7 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       PVE::Tools::run_command(['shorewall', 'start']);
+       PVE::Firewall::compile_and_start(1);
 
        return undef;
     }});
@@ -247,8 +236,13 @@ my $nodename = PVE::INotify::nodename();
 my $cmddef = {
     compile => [ __PACKAGE__, 'compile', []],
     start => [ __PACKAGE__, 'start', []],
+    restart => [ __PACKAGE__, 'restart', []],
     stop => [ __PACKAGE__, 'stop', []],
     clear => [ __PACKAGE__, 'clear', []],
+    enablevmfw => [ __PACKAGE__, 'enablevmfw', []],
+    disablevmfw => [ __PACKAGE__, 'disablevmfw', []],
+    enablehostfw => [ __PACKAGE__, 'enablehostfw', []],
+    disablehostfw => [ __PACKAGE__, 'disablehostfw', []],
 };
 
 my $cmd = shift;