]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/Firewall.pm
really save options
[pve-firewall.git] / src / PVE / Firewall.pm
index c03d91a449810d8d034b0e43d206239dd778e464..6707e669e3065c6539b120f928a170c7603d0c5f 100644 (file)
@@ -16,6 +16,7 @@ use Net::IP;
 use PVE::Tools qw(run_command lock_file);
 use Encode;
 
+my $hostfw_conf_filename = "/etc/pve/local/host.fw";
 my $clusterfw_conf_filename = "/etc/pve/firewall/cluster.fw";
 
 # dynamically include PVE::QemuServer and PVE::OpenVZ 
@@ -800,7 +801,7 @@ sub iptables_restore_cmdlist {
 sub ipset_restore_cmdlist {
     my ($cmdlist) = @_;
 
-    run_command(" /usr/sbin/ipset restore", input => $cmdlist);
+    run_command("/usr/sbin/ipset restore", input => $cmdlist);
 }
 
 sub iptables_get_chains {
@@ -892,8 +893,10 @@ sub ipset_get_chains {
 
        return if $line =~ m/^#/;
        return if $line =~ m/^\s*$/;
-       if ($line =~ m/^(\S+)\s(\S+)\s(\S+)/) {
-           push @{$chains->{$2}}, $line;
+       if ($line =~ m/^(?:\S+)\s(\S+)\s(?:\S+).*/) {
+           my $chain = $1;
+           $line =~ s/\s+$//; # delete trailing white space
+           push @{$chains->{$chain}}, $line;
        } else {
            # simply ignore the rest
            return;
@@ -1903,6 +1906,66 @@ sub load_vmfw_conf {
     return $vmfw_conf;
 }
 
+my $format_rules = sub {
+    my ($rules, $need_iface) = @_;
+
+    my $raw = '';
+
+    foreach my $rule (@$rules) {
+       if ($rule->{type} eq  'in' || $rule->{type} eq 'out') {
+           $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
+           $raw .= uc($rule->{type});
+           $raw .= " " . $rule->{action};
+           $raw .= " " . ($rule->{iface} || '-') if $need_iface;
+           $raw .= " " . ($rule->{source} || '-');
+           $raw .= " " . ($rule->{dest} || '-');
+           $raw .= " " . ($rule->{proto} || '-');
+           $raw .= " " . ($rule->{dport} || '-');
+           $raw .= " " . ($rule->{sport} || '-');
+           $raw .= " # " . encode('utf8', $rule->{comment}) 
+               if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
+           $raw .= "\n";
+       } else {
+           die "implement me '$rule->{type}'";
+       }
+    }
+
+    return $raw;
+};
+
+my $format_options = sub {
+    my ($options) = @_;
+
+    my $raw = '';
+
+    $raw .= "[OPTIONS]\n\n";
+    foreach my $opt (keys %$options) {
+       $raw .= "$opt: $options->{$opt}\n";
+    }
+    $raw .= "\n";
+
+    return $raw;
+};
+
+sub save_vmfw_conf {
+    my ($vmid, $vmfw_conf) = @_;
+
+    my $raw = '';
+
+    my $options = $vmfw_conf->{options};
+    $raw .= &$format_options($options) if scalar(keys %$options);
+    
+    my $rules = $vmfw_conf->{rules};
+    if (scalar(@$rules)) {
+       $raw .= "[RULES]\n\n";
+       $raw .= &$format_rules($rules, 1);
+       $raw .= "\n";
+    }
+
+    my $filename = "/etc/pve/firewall/$vmid.fw";
+    PVE::Tools::file_set_contents($filename, $raw);
+}
+
 sub read_vm_firewall_configs {
     my ($vmdata) = @_;
     my $vmfw_configs = {};
@@ -1964,40 +2027,38 @@ sub generate_std_chains {
 }
 
 sub generate_ipset_chains {
-    my ($ipset_ruleset, $options) = @_;
+    my ($ipset_ruleset, $fw_conf) = @_;
 
-    foreach my $ipset (keys %{$options->{ipset}}) {
-       generate_ipset($ipset_ruleset, $ipset, $options->{ipset}->{$ipset});
+    foreach my $ipset (keys %{$fw_conf->{ipset}}) {
+       generate_ipset($ipset_ruleset, $ipset, $fw_conf->{ipset}->{$ipset});
     }
 }
 
 sub generate_ipset {
     my ($ipset_ruleset, $name, $options) = @_;
 
-    my $hashsize = @{$options};
-    if ($hashsize <= 64){
+    my $hashsize = scalar(@$options);
+    if ($hashsize <= 64) {
        $hashsize = 64;
-    }else{
+    } else {
        $hashsize = round_powerof2($hashsize);
     }
 
-    push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize ";
+    push @{$ipset_ruleset->{$name}}, "create $name hash:net family inet hashsize $hashsize maxelem $hashsize";
 
-    foreach my $ip (@{$options}) {
+    foreach my $ip (@$options) {
        push @{$ipset_ruleset->{$name}}, "add $name $ip";
     }
-
 }
 
 sub round_powerof2 {
-   my ($int) = @_;
+    my ($int) = @_;
 
-   $int--;
-   $int |= $int >> $_ foreach (1,2,4,8,16);
-   return ++$int;
+    $int--;
+    $int |= $int >> $_ foreach (1,2,4,8,16);
+    return ++$int;
 }
 
-
 sub save_pvefw_status {
     my ($status) = @_;
 
@@ -2060,60 +2121,27 @@ sub load_clusterfw_conf {
     return $cluster_conf;
 }
 
-my $rules_to_conf = sub {
-    my ($rules, $need_iface) = @_;
-
-    my $raw = '';
-
-    foreach my $rule (@$rules) {
-       if ($rule->{type} eq  'in' || $rule->{type} eq 'out') {
-           $raw .= '|' if defined($rule->{enable}) && !$rule->{enable};
-           $raw .= uc($rule->{type});
-           $raw .= " " . $rule->{action};
-           $raw .= " " . ($rule->{iface} || '-') if $need_iface;
-           $raw .= " " . ($rule->{source} || '-');
-           $raw .= " " . ($rule->{dest} || '-');
-           $raw .= " " . ($rule->{proto} || '-');
-           $raw .= " " . ($rule->{dport} || '-');
-           $raw .= " " . ($rule->{sport} || '-');
-           $raw .= " # " . encode('utf8', $rule->{comment}) 
-               if $rule->{comment} && $rule->{comment} !~ m/^\s*$/;
-           $raw .= "\n";
-       } else {
-           die "implement me '$rule->{type}'";
-       }
-    }
-
-    return $raw;
-};
-
 sub save_clusterfw_conf {
     my ($cluster_conf) = @_;
 
     my $raw = '';
 
     my $options = $cluster_conf->{options};
-    if (scalar(keys %$options)) {
-       $raw .= "[OPTIONS]\n\n";
-       foreach my $opt (keys %$options) {
-           $raw .= "$opt: $options->{$opt}\n";
-       }
-       $raw .= "\n";
-    }
+    $raw .= &$format_options($options) if scalar(keys %$options);
 
     # fixme: save ipset
 
     my $rules = $cluster_conf->{rules};
     if (scalar(@$rules)) {
        $raw .= "[RULES]\n\n";
-       $raw .= &$rules_to_conf($rules, 1);
+       $raw .= &$format_rules($rules, 1);
        $raw .= "\n";
     }
 
     foreach my $group (sort keys %{$cluster_conf->{groups}}) {
        my $rules = $cluster_conf->{groups}->{$group};
        $raw .= "[group $group]\n\n";
-       $raw .= &$rules_to_conf($rules, 0);
+       $raw .= &$format_rules($rules, 0);
        $raw .= "\n";
     }
 
@@ -2123,13 +2151,30 @@ sub save_clusterfw_conf {
 sub load_hostfw_conf {
 
     my $hostfw_conf = {};
-    my $filename = "/etc/pve/local/host.fw";
-    if (my $fh = IO::File->new($filename, O_RDONLY)) {
-       $hostfw_conf = parse_host_fw_rules($filename, $fh);
+    if (my $fh = IO::File->new($hostfw_conf_filename, O_RDONLY)) {
+       $hostfw_conf = parse_host_fw_rules($hostfw_conf_filename, $fh);
     }
     return $hostfw_conf;
 }
 
+sub save_hostfw_conf {
+    my ($hostfw_conf) = @_;
+
+    my $raw = '';
+
+    my $options = $hostfw_conf->{options};
+    $raw .= &$format_options($options) if scalar(keys %$options);
+    
+    my $rules = $hostfw_conf->{rules};
+    if (scalar(@$rules)) {
+       $raw .= "[RULES]\n\n";
+       $raw .= &$format_rules($rules, 1);
+       $raw .= "\n";
+    }
+
+    PVE::Tools::file_set_contents($hostfw_conf_filename, $raw);
+}
+
 sub compile {
     my $vmdata = read_local_vm_config();
     my $vmfw_configs = read_vm_firewall_configs($vmdata);
@@ -2354,52 +2399,49 @@ sub get_ruleset_cmdlist {
 }
 
 sub get_ipset_cmdlist {
-    my ($ruleset, $delete, $verbose) = @_;
+    my ($ruleset, $verbose) = @_;
 
     my $cmdlist = "";
 
+    my $delete_cmdlist = "";
+
     my $active_chains = ipset_get_chains();
     my $statushash = get_ruleset_status($ruleset, $active_chains, \&ipset_chain_digest, $verbose);
 
-    if(!$delete){
-
-       foreach my $chain (sort keys %$ruleset) {
-           my $stat = $statushash->{$chain};
-           die "internal error" if !$stat;
+    foreach my $chain (sort keys %$ruleset) {
+       my $stat = $statushash->{$chain};
+       die "internal error" if !$stat;
 
-           if ($stat->{action} eq 'create') {
-               foreach my $cmd (@{$ruleset->{$chain}}) {
+       if ($stat->{action} eq 'create') {
+           foreach my $cmd (@{$ruleset->{$chain}}) {
                $cmdlist .= "$cmd\n";
-               }
            }
+       }
 
-           if ($stat->{action} eq 'update') {
-               my $chain_swap = $chain."_swap";
-
-               foreach my $cmd (@{$ruleset->{$chain}}) {
-                   $cmd =~ s/$chain/$chain_swap/;
-                   $cmdlist .= "$cmd\n";
-               }
-               $cmdlist .= "swap $chain_swap $chain\n";
-               $cmdlist .= "flush $chain_swap\n";
-               $cmdlist .= "destroy $chain_swap\n";
+       if ($stat->{action} eq 'update') {
+           my $chain_swap = $chain."_swap";
+           
+           foreach my $cmd (@{$ruleset->{$chain}}) {
+               $cmd =~ s/$chain/$chain_swap/;
+               $cmdlist .= "$cmd\n";
            }
-
+           $cmdlist .= "swap $chain_swap $chain\n";
+           $cmdlist .= "flush $chain_swap\n";
+           $cmdlist .= "destroy $chain_swap\n";
        }
 
-    }else{
+    }
 
-       foreach my $chain (keys %$statushash) {
-           next if $statushash->{$chain}->{action} ne 'delete';
+    foreach my $chain (keys %$statushash) {
+       next if $statushash->{$chain}->{action} ne 'delete';
 
-           $cmdlist .= "flush $chain\n";
-           $cmdlist .= "destroy $chain\n";
-       }
+       $delete_cmdlist .= "flush $chain\n";
+       $delete_cmdlist .= "destroy $chain\n";
     }
 
-    my $changes = $cmdlist ? 1 : 0;
-
-    return wantarray ? ($cmdlist, $changes) : $cmdlist;
+    my $changes = ($cmdlist || $delete_cmdlist) ? 1 : 0;
+    
+    return ($cmdlist, $delete_cmdlist, $changes);
 }
 
 sub apply_ruleset {
@@ -2409,27 +2451,33 @@ sub apply_ruleset {
 
     update_nf_conntrack_max($hostfw_conf);
 
-    my $ipset_create_cmdlist = get_ipset_cmdlist($ipset_ruleset, undef, $verbose);
-
-    my $ipset_delete_cmdlist = get_ipset_cmdlist($ipset_ruleset, 1, $verbose);
+    my ($ipset_create_cmdlist, $ipset_delete_cmdlist, $ipset_changes) = 
+       get_ipset_cmdlist($ipset_ruleset, undef, $verbose);
 
-    my $cmdlist = get_ruleset_cmdlist($ruleset, $verbose);
+    my ($cmdlist, $changes) = get_ruleset_cmdlist($ruleset, $verbose);
 
-    print $ipset_create_cmdlist if $verbose;
-
-    print $ipset_delete_cmdlist if $verbose;
+    if ($verbose) {
+       if ($ipset_changes) {
+           print "ipset changes:\n";
+           print $ipset_create_cmdlist if $ipset_create_cmdlist;
+           print $ipset_delete_cmdlist if $ipset_delete_cmdlist;
+       }
 
-    print $cmdlist if $verbose;
+       if ($changes) {
+           print "iptables changes:\n";
+           print $cmdlist;
+       }
+    }
 
     ipset_restore_cmdlist($ipset_create_cmdlist);
 
     iptables_restore_cmdlist($cmdlist);
 
-    ipset_restore_cmdlist($ipset_delete_cmdlist);
+    ipset_restore_cmdlist($ipset_delete_cmdlist) if $ipset_delete_cmdlist;
 
     # test: re-read status and check if everything is up to date
     my $active_chains = iptables_get_chains();
-    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, $verbose);
+    my $statushash = get_ruleset_status($ruleset, $active_chains, \&iptables_chain_digest, 0);
 
     my $errors;
     foreach my $chain (sort keys %$ruleset) {