]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/Firewall.pm
birectionnal macros cleanups
[pve-firewall.git] / src / PVE / Firewall.pm
index be5354c780799f02dfe184937481e921f1d7f6fb..dae867d8fd23fb9d4f6177b20e4bd5eb53ec2df4 100644 (file)
@@ -157,9 +157,8 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'tcp', dport => '6277' },
     ],
     'DHCPfwd' => [
-       "Forwarded DHCP traffic (bidirectional)",
+       "Forwarded DHCP traffic",
        { action => 'PARAM', proto => 'udp', dport => '67:68', sport => '67:68' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '67:68', sport => '67:68' },
     ],
     'DNS' => [
        "Domain Name System traffic (upd and tcp)",
@@ -186,9 +185,8 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'udp', dport => '1080' },
     ],
     'GRE' => [
-       "Generic Routing Encapsulation tunneling protocol (bidirectional)",
+       "Generic Routing Encapsulation tunneling protocol",
        { action => 'PARAM', proto => '47' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
     ],
     'Git' => [
        "Git distributed revision control traffic",
@@ -223,32 +221,24 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'tcp', dport => '993' },
     ],
     'IPIP' => [
-       "IPIP capsulation traffic (bidirectional)",
+       "IPIP capsulation traffic",
        { action => 'PARAM', proto => '94' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '94' },
     ],
     'IPsec' => [
-       "IPsec traffic (bidirectional)",
+       "IPsec traffic",
        { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
        { action => 'PARAM', proto => '50' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
     ],
     'IPsecah' => [
-       "IPsec authentication (AH) traffic (bidirectional)",
+       "IPsec authentication (AH) traffic",
        { action => 'PARAM', proto => 'udp', dport => '500', sport => '500' },
        { action => 'PARAM', proto => '51' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500', sport => '500' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '51' },
     ],
     'IPsecnat' => [
-       "IPsec traffic and Nat-Traversal (bidirectional)",
+       "IPsec traffic and Nat-Traversal",
        { action => 'PARAM', proto => 'udp', dport => '500' },
        { action => 'PARAM', proto => 'udp', dport => '4500' },
        { action => 'PARAM', proto => '50' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '500' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '4500' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '50' },
     ],
     'IRC' => [
        "Internet Relay Chat traffic",
@@ -261,7 +251,6 @@ my $pve_fw_macros = {
     'L2TP' => [
        "Layer 2 Tunneling Protocol traffic",
        { action => 'PARAM', proto => 'udp', dport => '1701' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1701' },
     ],
     'LDAP' => [
        "Lightweight Directory Access Protocol traffic",
@@ -305,11 +294,6 @@ my $pve_fw_macros = {
        "Network Time Protocol (ntpd)",
        { action => 'PARAM', proto => 'udp', dport => '123' },
     ],
-    'NTPbi' => [
-       "Bi-directional NTP (for NTP peers)",
-       { action => 'PARAM', proto => 'udp', dport => '123' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '123' },
-    ],
     'OSPF' => [
        "OSPF multicast traffic",
        { action => 'PARAM', proto => '89' },
@@ -334,7 +318,6 @@ my $pve_fw_macros = {
     'PPtP' => [
        "Point-to-Point Tunneling Protocol",
        { action => 'PARAM', proto => '47' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => '47' },
        { action => 'PARAM', proto => 'tcp', dport => '1723' },
     ],
     'Ping' => [
@@ -353,10 +336,9 @@ my $pve_fw_macros = {
        "Microsoft Remote Desktop Protocol traffic",
        { action => 'PARAM', proto => 'tcp', dport => '3389' },
     ],
-    'RIPbi' => [
+    'RIP' => [
        "Routing Information Protocol (bidirectional)",
        { action => 'PARAM', proto => 'udp', dport => '520' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '520' },
     ],
     'RNDC' => [
        "BIND remote management protocol",
@@ -385,17 +367,6 @@ my $pve_fw_macros = {
        { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
        { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
     ],
-    'SMBBI' => [
-       "Microsoft SMB traffic (bidirectional)",
-       { action => 'PARAM', proto => 'udp', dport => '135,445' },
-       { action => 'PARAM', proto => 'udp', dport => '137:139' },
-       { action => 'PARAM', proto => 'udp', dport => '1024:65535', sport => '137' },
-       { action => 'PARAM', proto => 'tcp', dport => '135,139,445' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '135,445' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '137:139' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'udp', dport => '1024:65535', sport => '137' },
-       { action => 'PARAM', source => 'DEST', dest => 'SOURCE', proto => 'tcp', dport => '135,139,445' },
-    ],
     'SMBswat' => [
        "Samba Web Administration Tool",
        { action => 'PARAM', proto => 'tcp', dport => '901' },
@@ -1000,7 +971,10 @@ sub verify_rule {
        raise_param_exc({ type => "unknown rule type '$type'"});
     }
 
-    # fixme: verify $rule->{iface}?
+    if ($rule->{iface}) {
+       eval { PVE::JSONSchema::pve_verify_iface($rule->{iface}); };
+       raise_param_exc({ iface => $@ }) if $@;
+    }  
 
     if ($rule->{macro}) {
        my $preferred_name = $pve_fw_preferred_macro_names->{lc($rule->{macro})};
@@ -1764,9 +1738,11 @@ for (my $i = 0; $i < $MAX_NETS; $i++)  {
 }
 
 sub parse_fw_rule {
-    my ($line, $need_iface, $allow_groups) = @_;
+    my ($line, $allow_iface, $allow_groups) = @_;
+
+    my ($type, $action, $macro, $iface, $source, $dest, $proto, $dport, $sport);
 
-    my ($type, $action, $iface, $source, $dest, $proto, $dport, $sport);
+    chomp $line;
 
     # we can add single line comments to the end of the rule
     my $comment = decode('utf8', $1) if $line =~ s/#\s*(.*?)\s*$//;
@@ -1776,22 +1752,11 @@ sub parse_fw_rule {
 
     $enable = 0 if $line =~ s/^\|//;
 
-    my @data = split(/\s+/, $line);
-    my $expected_elements = $need_iface ? 8 : 7;
-
-    die "wrong number of rule elements\n" if scalar(@data) > $expected_elements;
-
-    if ($need_iface) {
-       ($type, $action, $iface, $source, $dest, $proto, $dport, $sport) = @data
-    } else {
-       ($type, $action, $source, $dest, $proto, $dport, $sport) =  @data;
-    }
-
-    die "incomplete rule\n" if ! ($type && $action);
-
-    my $macro;
-
-    $type = lc($type);
+    $line =~ s/^(\S+)\s+(\S+)\s*// ||
+       die "unable to parse rule: $line\n";
+    
+    $type = lc($1);
+    $action = $2;
 
     if ($type eq  'in' || $type eq 'out') {
        if ($action =~ m/^(ACCEPT|DROP|REJECT)$/) {
@@ -1805,32 +1770,52 @@ sub parse_fw_rule {
            die "unknown action '$action'\n";
        }
     } elsif ($type eq 'group') {
-       die "wrong number of rule elements\n" if scalar(@data) > 3;
        die "groups disabled\n" if !$allow_groups;
-
        die "invalid characters in group name\n" if $action !~ m/^${security_group_name_pattern}$/;
     } else {
        die "unknown rule type '$type'\n";
     }
 
-    if ($need_iface) {
-       $iface = undef if $iface && $iface eq '-';
-    }
-
-    $proto = undef if $proto && $proto eq '-';
-    pve_fw_verify_protocol_spec($proto) if $proto;
+    while (length($line)) {
+       if ($line =~ s/^-i (\S+)\s*//) {
+           die "parameter -i not allowed\n" if !$allow_iface;
+           $iface = $1;
+           PVE::JSONSchema::pve_verify_iface($iface);
+           next;
+       }
 
-    $source = undef if $source && $source eq '-';
-    $dest = undef if $dest && $dest eq '-';
+       last if $type eq 'group';
 
-    $dport = undef if $dport && $dport eq '-';
-    $sport = undef if $sport && $sport eq '-';
+       if ($line =~ s/^-p (\S+)\s*//) {
+           $proto = $1;
+           pve_fw_verify_protocol_spec($proto);
+           next;
+       }
+       if ($line =~ s/^-dport (\S+)\s*//) {
+           $dport = $1;
+           parse_port_name_number_or_range($dport);
+           next;
+       }
+       if ($line =~ s/^-sport (\S+)\s*//) {
+           $sport = $1;
+           parse_port_name_number_or_range($sport);
+           next;
+       }
+       if ($line =~ s/^-source (\S+)\s*//) {
+           $source = $1;
+           parse_address_list($source);
+           next;
+       }
+       if ($line =~ s/^-dest (\S+)\s*//) {
+           $dest = $1;
+           parse_address_list($dest);
+           next;
+       }
 
-    parse_port_name_number_or_range($dport) if defined($dport);
-    parse_port_name_number_or_range($sport) if defined($sport);
+       last;
+    }
 
-    parse_address_list($source) if $source;
-    parse_address_list($dest) if $dest;
+    die "unable to parse rule parameters: $line\n" if length($line);
 
     return {
        type => $type,
@@ -2218,7 +2203,7 @@ sub load_vmfw_conf {
 }
 
 my $format_rules = sub {
-    my ($rules, $need_iface) = @_;
+    my ($rules, $allow_iface) = @_;
 
     my $raw = '';
 
@@ -2231,14 +2216,16 @@ my $format_rules = sub {
            } else {
                $raw .= " " . $rule->{action};
            }
-           $raw .= " " . ($rule->{iface} || '-') if $need_iface;
+           if ($allow_iface && $rule->{iface}) {
+               $raw .= " -i $rule->{iface}";
+           }
 
            if ($rule->{type} ne  'group')  {
-               $raw .= " " . ($rule->{source} || '-');
-               $raw .= " " . ($rule->{dest} || '-');
-               $raw .= " " . ($rule->{proto} || '-');
-               $raw .= " " . ($rule->{dport} || '-');
-               $raw .= " " . ($rule->{sport} || '-');
+               $raw .= " -source $rule->{source}" if $rule->{source};
+               $raw .= " -dest $rule->{dest}" if $rule->{dest};
+               $raw .= " -p $rule->{proto}" if $rule->{proto};
+               $raw .= " -dport $rule->{dport}" if $rule->{dport};
+               $raw .= " -sport $rule->{sport}" if $rule->{sport};
            }
 
            $raw .= " # " . encode('utf8', $rule->{comment})