use PVE::QemuServer;
use File::Path;
use IO::File;
+use Net::IP;
+
use Data::Dumper;
my $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";
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 {
$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',
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} = {
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*$/;
}
$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,
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;