]> git.proxmox.com Git - pmg-api.git/commitdiff
fix #2371: reload pmg-smtp-filter on config change
authorStoiko Ivanov <s.ivanov@proxmox.com>
Mon, 21 Oct 2019 17:23:26 +0000 (19:23 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 23 Oct 2019 07:50:34 +0000 (09:50 +0200)
the external services (postfix, clamav,...) are restarted if their configfile
changes (which Template::Toolkit tells us).

By writing a current-config to '/run/pmg-smtp-filter.cfg' we can use the same
logic to reload it on a config-change affecting it - currently hide_received

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
src/PMG/Config.pm
src/bin/pmg-smtp-filter

index 0c907a2e3f692c3eca7a25619a7817f567ee24a8..a0acd4ac498be2f973d689744a2f711c851332ed 100755 (executable)
@@ -1548,6 +1548,65 @@ sub rewrite_config_postfix {
     return $changes;
 }
 
+#parameters affecting services w/o config-file (pmgpolicy, pmg-smtp-filter)
+my $pmg_service_params = {
+    mail => { hide_received => 1 },
+};
+
+my $smtp_filter_cfg = '/run/pmg-smtp-filter.cfg';
+my $smtp_filter_cfg_lock = '/run/pmg-smtp-filter.cfg.lck';
+
+sub dump_smtp_filter_config {
+    my ($self) = @_;
+
+    my $conf = '';
+    my $val;
+    foreach my $sec (sort keys %$pmg_service_params) {
+       my $conf_sec = $self->{ids}->{$sec} // {};
+       foreach my $key (sort keys %{$pmg_service_params->{$sec}}) {
+           $val = $conf_sec->{$key};
+           $conf .= "$sec.$key:$val\n" if defined($val);
+       }
+    }
+
+    return $conf;
+}
+
+sub compare_smtp_filter_config {
+    my ($self) = @_;
+
+    my $ret = 0;
+    my $old;
+    eval {
+       $old = PVE::Tools::file_get_contents($smtp_filter_cfg);
+    };
+
+    if (my $err = $@) {
+       syslog ('warning', "reloading pmg-smtp-filter: $err");
+       $ret = 1;
+    } else {
+       my $new = $self->dump_smtp_filter_config();
+       $ret = 1 if $old ne $new;
+    }
+
+    $self->write_smtp_filter_config() if $ret;
+
+    return $ret;
+}
+
+# writes the parameters relevant for pmg-smtp-filter to /run/ for comparison
+# on config change
+sub write_smtp_filter_config {
+    my ($self) = @_;
+
+    PVE::Tools::lock_file($smtp_filter_cfg_lock, undef, sub {
+       PVE::Tools::file_set_contents($smtp_filter_cfg,
+           $self->dump_smtp_filter_config());
+    });
+
+    die $@ if $@;
+}
+
 sub rewrite_config {
     my ($self, $rulecache, $restart_services, $force_restart) = @_;
 
@@ -1589,6 +1648,12 @@ sub rewrite_config {
        $log_restart->('clamav-freshclam');
        PMG::Utils::service_cmd('clamav-freshclam', 'restart');
     }
+
+    if (($self->compare_smtp_filter_config() && $restart_services) ||
+       $force_restart->{spam}) {
+       syslog ('info', "scheduled reload for pmg-smtp-filter");
+       PMG::Utils::reload_smtp_filter();
+    }
 }
 
 1;
index 61eaf92e945b706384ed0925e656dd771ed2a4b6..62ce9ab96fe847fd4e7faeeca9243a4a2ecc3de8 100755 (executable)
@@ -440,6 +440,7 @@ sub pre_loop_hook {
     my ($backup_umask) = umask;
 
     my $pmg_cfg = PMG::Config->new();
+    $pmg_cfg->write_smtp_filter_config();
 
     # Note: you need to restart the daemon when you change 'rbl_checks'
     my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');