]> git.proxmox.com Git - pve-firewall.git/blobdiff - PVE/Firewall.pm
Clarify zone names
[pve-firewall.git] / PVE / Firewall.pm
index 3f0adf0cb531d0314a97135f5b3fceef79d7cf79..6a3f225dbc69e7ee523c17833cd33bdb337186ff 100644 (file)
@@ -7,6 +7,8 @@ use PVE::Tools;
 use PVE::QemuServer;
 use File::Path;
 use IO::File;
+use Net::IP;
+
 use Data::Dumper;
 
 my $macros;
@@ -22,6 +24,99 @@ sub get_shorewall_macros {
     return $macros;
 }
 
+my $etc_services;
+
+sub get_etc_services {
+
+    return $etc_services if $etc_services;
+
+    my $filename = "/etc/services";
+
+    my $fh = IO::File->new($filename, O_RDONLY);
+    if (!$fh) {
+       warn "unable to read '$filename' - $!\n";
+       return {};
+    }
+
+    my $services = {};
+
+    while (my $line = <$fh>) {
+       chomp ($line);
+       next if $line =~m/^#/;
+       next if ($line =~m/^\s*$/);
+
+       if ($line =~ m!^(\S+)\s+(\S+)/(tcp|udp).*$!) {
+           $services->{byid}->{$2}->{name} = $1;
+           $services->{byid}->{$2}->{$3} = 1;
+           $services->{byname}->{$1} = $services->{byid}->{$2};
+       }
+    }
+
+    close($fh);
+
+    $etc_services = $services;    
+    
+    return $etc_services;
+}
+
+my $etc_protocols;
+
+sub get_etc_protocols {
+    return $etc_protocols if $etc_protocols;
+
+    my $filename = "/etc/protocols";
+
+    my $fh = IO::File->new($filename, O_RDONLY);
+    if (!$fh) {
+       warn "unable to read '$filename' - $!\n";
+       return {};
+    }
+
+    my $protocols = {};
+
+    while (my $line = <$fh>) {
+       chomp ($line);
+       next if $line =~m/^#/;
+       next if ($line =~m/^\s*$/);
+
+       if ($line =~ m!^(\S+)\s+(\d+)\s+.*$!) {
+           $protocols->{byid}->{$2}->{name} = $1;
+           $protocols->{byname}->{$1} = $protocols->{byid}->{$2};
+       }
+    }
+
+    close($fh);
+
+    $etc_protocols = $protocols;
+
+    return $etc_protocols;
+}
+
+sub parse_address_list {
+    my ($str) = @_;
+
+    foreach my $aor (split(/,/, $str)) {
+       if (!Net::IP->new($aor)) {
+           my $err = Net::IP::Error();
+           die "invalid IP address: $err\n";
+       }
+    }
+}
+
+sub parse_port_name_number_or_range {
+    my ($str) = @_;
+
+    my $services = PVE::Firewall::get_etc_services();
+
+    foreach my $item (split(/,/, $str)) {
+       foreach my $pon (split(':', $item, 2)) {
+           next if $pon =~ m/^\d+$/ && $pon > 0 && $pon < 65536;
+           next if defined($services->{byname}->{$pon});
+           die "invalid port '$pon'\n";
+       }
+    }
+
+}
 
 my $rule_format = "%-15s %-30s %-30s %-15s %-15s %-15s\n";
 
@@ -41,25 +136,36 @@ my $generate_input_rule = sub {
     my $action = $rule->{service} ? 
        "$rule->{service}($rule->{action})" : $rule->{action};
 
-    my $source;
+    my $sources = [];
 
-    if ($zoneinfo->{$zone}->{type} eq 'bport') {
+    if (!$rule->{source}) {
+       push @$sources, 'all'; 
+    } elsif ($zoneinfo->{$zone}->{type} eq 'bport') {
        my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
-       my $bridge_ext_zone = $zoneinfo->{$bridge_zone}->{bridge_ext_zone} || die "internal error";
-       my $zoneref = $zoneinfo->{$bridge_ext_zone}->{zoneref} || die "internal error";
-       if (!$rule->{source}) {
-           # $source = "${zoneref}";
-           $source = 'all';
-       } else {
-           # 'all' does not work
-           $source = "${zoneref}:$rule->{source}";
+       my $zoneref = $zoneinfo->{$bridge_zone}->{zoneref} || die "internal error";
+
+       # using 'all' does not work, so we create one rule for
+       # each related zone on the same bridge
+       push @$sources, "${zoneref}:$rule->{source}";
+       foreach my $z (keys %$zoneinfo) {
+           next if $z eq $zone;
+           next if !$zoneinfo->{$z}->{bridge_zone};
+           next if $zoneinfo->{$z}->{bridge_zone} ne $bridge_zone;
+           $zoneref = $zoneinfo->{$z}->{zoneref} || die "internal error";
+           push @$sources, "${zoneref}:$rule->{source}";
        }
     } else {
-       $source = "all:$rule->{source}";
+       push @$sources, "all:$rule->{source}";
     }
 
-    return sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-', 
-                  $rule->{dport} || '-', $rule->{sport} || '-');
+    my $out = '';
+
+    foreach my $source (@$sources) {
+       $out .= sprintf($rule_format, $action, $source, $dest, $rule->{proto} || '-', 
+                       $rule->{dport} || '-', $rule->{sport} || '-');
+    }
+
+    return $out;
 };
 
 my $generate_output_rule = sub {
@@ -108,11 +214,11 @@ my $compile_shorewall = sub {
     $register_bridge = sub {
        my ($bridge, $vlan) = @_;
 
-       my $zone =  'z' . $bridge;
+       my $zone =  $bridge;
 
        return $zone if $zoneinfo->{$zone};
 
-       my $ext_zone = "z${bridge}ext";
+       my $ext_zone = "${bridge}_ext";
 
        $zoneinfo->{$zone} = {
            type => 'bridge',
@@ -143,7 +249,7 @@ my $compile_shorewall = sub {
        my ($bridge, $vlan, $vmzone, $tap) = @_;
 
        my $bridge_zone = &$register_bridge($bridge, $vlan);
-       my $zone = $bridge_zone . $vmzone;
+       my $zone = $bridge_zone . '_' . $vmzone;
 
        if (!$zoneinfo->{$zone}) {
            $zoneinfo->{$zone} = {
@@ -333,8 +439,9 @@ sub parse_fw_rules {
 
     my $res = { in => [], out => [] };
 
-    my $macros = PVE::Firewall::get_shorewall_macros();
-
+    my $macros = get_shorewall_macros();
+    my $protocols = get_etc_protocols();
+    
     while (defined(my $line = <$fh>)) {
        next if $line =~ m/^#/;
        next if $line =~ m/^\s*$/;
@@ -374,27 +481,31 @@ sub parse_fw_rules {
        }
 
        $proto = undef if $proto && $proto eq '-';
-       if ($proto && $proto !~ m/^(icmp|tcp|udp)$/) {
+       if ($proto && !(defined($protocols->{byname}->{$proto}) ||
+                       defined($protocols->{byid}->{$proto}))) {
            warn "unknown protokol '$proto'\n";
            next;
        }
 
        $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 '-';
 
+       eval {
+           parse_address_list($source) if $source;
+           parse_address_list($dest) if $dest;
+           parse_port_name_number_or_range($dport) if $dport;
+           parse_port_name_number_or_range($sport) if $sport;
+       };
+       if (my $err = $@) {
+           warn $err;
+           next;
+
+       }
+
+
        my $rule = {
            action => $action,
            service => $service,
@@ -421,7 +532,7 @@ sub read_local_vm_config {
     my $list = PVE::QemuServer::config_list();
 
     foreach my $vmid (keys %$list) {
-       # next if $vmid ne '100';
+       #next if !($vmid eq '100' || $vmid eq '102');
        my $cfspath = PVE::QemuServer::cfs_config_path($vmid);
        if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
            $qemu->{$vmid} = $conf;