]> git.proxmox.com Git - pve-firewall.git/commitdiff
implement simple option parser
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 21 Feb 2014 09:39:13 +0000 (10:39 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 21 Feb 2014 09:39:13 +0000 (10:39 +0100)
PVE/Firewall.pm

index aaba7f930ca38e0d04c487a1d50a03503fd4bcc2..09703b086f57873dda4f17735635c25bb8a955d8 100644 (file)
@@ -908,10 +908,29 @@ sub parse_fw_rule {
     return $rules;
 }
 
+sub parse_fw_option {
+    my ($line) = @_;
+
+    my ($opt, $value);
+
+    if ($line =~ m/^enable:\s*(0|1)\s*$/i) {
+       $opt = 'enable';
+       $value = int($1);
+    } elsif ($line =~ m/^(policy-(in|out)):\s*(ACCEPT|DROP|REJECT)\s*$/i) {
+       $opt = lc($1);
+       $value = uc($3);
+     } else {
+       chomp $line;
+       die "can't parse option '$line'\n"
+    }
+
+    return ($opt, $value);
+}
+
 sub parse_vm_fw_rules {
     my ($filename, $fh) = @_;
 
-    my $res = { in => [], out => [] };
+    my $res = { in => [], out => [], options => {}};
 
     my $section;
 
@@ -922,8 +941,9 @@ sub parse_vm_fw_rules {
        my $linenr = $fh->input_line_number();
        my $prefix = "$filename (line $linenr)";
 
-       if ($line =~ m/^\[(in|out)\]\s*$/i) {
+       if ($line =~ m/^\[(\S+)\]\s*$/i) {
            $section = lc($1);
+           warn "$prefix: ignore unknown section '$section'\n" if !$res->{$section};
            next;
        }
        if (!$section) {
@@ -931,6 +951,17 @@ sub parse_vm_fw_rules {
            next;
        }
 
+       next if !$res->{$section}; # skip undefined section
+
+       if ($section eq 'options') {
+           eval { 
+               my ($opt, $value) = parse_fw_option($line); 
+               $res->{options}->{$opt} = $value;
+           };
+           warn "$prefix: $@" if $@;
+           next;
+       }
+
        my $rules;
        eval { $rules = parse_fw_rule($line, 1, 1); };
        if (my $err = $@) {