+use File::Path;
+use IO::File;
+use Net::IP;
+
+use Data::Dumper;
+
+my $macros;
+sub get_shorewall_macros {
+
+ return $macros if $macros;
+
+ foreach my $path (</usr/share/shorewall/macro.*>) {
+ if ($path =~ m|/macro\.(\S+)$|) {
+ $macros->{$1} = 1;
+ }
+ }
+ 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 $generate_input_rule = sub {
+ my ($zoneinfo, $rule, $net, $netid) = @_;
+
+ my $zone = $net->{zone} || die "internal error";
+ my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
+ my $tap = $net->{tap} || die "internal error";
+
+ my $dest = "$zid:$tap";
+
+ if ($rule->{dest}) {
+ $dest .= ":$rule->{dest}";
+ }
+
+ my $action = $rule->{service} ?
+ "$rule->{service}($rule->{action})" : $rule->{action};
+
+ my $sources = [];
+
+ if (!$rule->{source}) {
+ push @$sources, 'all';
+ } elsif ($zoneinfo->{$zone}->{type} eq 'bport') {
+ my $bridge_zone = $zoneinfo->{$zone}->{bridge_zone} || die "internal error";
+ 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 {
+ push @$sources, "all:$rule->{source}";
+ }
+
+ 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 {
+ my ($zoneinfo, $rule, $net, $netid) = @_;
+
+ my $zone = $net->{zone} || die "internal error";
+ my $zid = $zoneinfo->{$zone}->{zoneref} || die "internal error";
+ my $tap = $net->{tap} || die "internal error";
+
+ my $action = $rule->{service} ?
+ "$rule->{service}($rule->{action})" : $rule->{action};
+
+ my $dest;
+
+ if (!$rule->{dest}) {
+ $dest = 'all';
+ } else {
+ $dest = "all:$rule->{dest}";
+ }
+
+ return sprintf($rule_format, $action, "$zid:$tap", $dest,
+ $rule->{proto} || '-', $rule->{dport} || '-', $rule->{sport} || '-');
+};