]> git.proxmox.com Git - pmg-api.git/blobdiff - bin/pmgpolicy
pmgpolicy: use PMG::ClusterConfig->new()
[pmg-api.git] / bin / pmgpolicy
index 94e2b1cb3f1929fb75ce86ca1775a76fde3226e0..d17b800eb5c7a6bdd93979a5bf301bab62f5866f 100755 (executable)
@@ -106,7 +106,7 @@ sub run_dequeue {
 
     my ($csec, $usec) = gettimeofday ();
 
-    my $cinfo = PVE::INotify::read_file("cluster.conf");
+    my $cinfo = PMG::ClusterConfig->new();
     my $lcid = $cinfo->{local}->{cid};
     my $role = $cinfo->{local}->{type} // '-';
 
@@ -118,7 +118,7 @@ sub run_dequeue {
     my $err = $@;
 
     if ($err) {
-       $self->log(0, PMG::Utils::msgquote("ERROR: $err"));
+       $self->log(0, "ERROR: $err");
        return;
     }
 
@@ -209,7 +209,7 @@ sub run_dequeue {
            # this produces too much log traffic
            # my $targets = join (", ", @rcvrs);
            #my $msg = "expire mail $ref->{instance} from $ref->{sender} to $targets";
-           #$self->log (0, PMG::Utils::msgquote ($msg));
+           #$self->log (0, $msg);
        }
 
        $dbh->do ($cmds) if $cmds;
@@ -233,7 +233,7 @@ sub run_dequeue {
 
     if ($err) {
        $dbh->rollback if $dbh;
-       $self->log(0, PMG::Utils::msgquote($err));
+       $self->log(0, $err);
     } else {
        $self->log(2, "end greylist database maintainance ($ptime ms)");
     }
@@ -254,7 +254,7 @@ sub pre_loop_hook {
        # reloading server configuration
        if (defined $prop->{children}) {
            foreach my $pid (keys %{$prop->{children}}) {
-               kill (10, $pid); # SIGUSR1 childs
+               kill(10, $pid); # SIGUSR1 childs
            }
        }
     };
@@ -278,16 +278,18 @@ sub load_config {
     }
 
     my $pmg_cfg = PMG::Config->new ();
-    $self->{use_rbl} = $pmg_cfg->get('mail', 'use_rbl');
     $self->{use_spf} = $pmg_cfg->get('mail', 'spf');
     $self->{use_greylist} = $pmg_cfg->get('mail', 'greylist');
 
+    if ($opt_testmode) {
+       $self->{use_spf} = 1;
+       $self->{use_greylist} = 1;
+    }
+
     my $nodename = PVE::INotify::nodename();
-    my $resolv = PVE::INotify::read_file('resolvconf');
-    my $domain = $resolv->{search};
-    $self->{fqdn} = "$nodename.$domain";
+    $self->{fqdn} = PVE::Tools::get_fqdn($nodename);
 
-    my $cinfo = PVE::INotify::read_file("cluster.conf");
+    my $cinfo = PMG::ClusterConfig->new();
     my $lcid = $cinfo->{local}->{cid};
     $self->{cinfo} = $cinfo;
     $self->{lcid} = $lcid;
@@ -300,7 +302,7 @@ sub load_config {
        $self->{rulecache} = PMG::RuleCache->new($self->{ruledb});
     };
     if (my $err = $@) {
-       $self->log(0, PMG::Utils::msgquote("ERROR: unable to load database : $err"));
+       $self->log(0, "ERROR: unable to load database : $err");
     }
 
     $self->{reload_config} = 0;
@@ -340,7 +342,7 @@ sub child_init_hook {
            hostname => $self->{fqdn}, dns_resolver => $self->{dns_resolver});
     };
     if (my $err = $@) {
-       $self->log(0, PMG::Utils::msgquote($err));
+       $self->log(0, $err);
        $self->child_finish_hook;
        exit(-1);
     }
@@ -478,7 +480,7 @@ sub greylist_value {
        };
        my $err = $@;
        if ($err) {
-           $self->log(0, PMG::Utils::msgquote("unable to reconnect to database server: $err"));
+           $self->log(0, "unable to reconnect to database server: $err");
            return 'dunno';
        }
        $self->{ruledb} = PMG::RuleDB->new($dbh);
@@ -540,7 +542,7 @@ sub greylist_value {
 
        if ($err) {
             $err = $err->text if UNIVERSAL::isa ($err, 'Mail::SPF::Exception');
-           $self->log (0, PMG::Utils::msgquote($err));
+           $self->log (0, $err);
        } else {
 
            if ($result && $result eq 'pass') {
@@ -574,7 +576,7 @@ sub greylist_value {
 
                    if (!defined($ref->{rctime})) {
 
-                       $dbh->do("SELECT merge_greylist(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
+                       $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef,
                                 $net, $host, $sender, $rcpt, $instance,
                                 $ctime, $ctime + 10, 0, 100000, 0, $ctime, $self->{lcid});
                    }
@@ -583,7 +585,7 @@ sub greylist_value {
                };
                if (my $err = $@) {
                    $dbh->rollback;
-                   $self->log(0, PMG::Utils::msgquote($err));
+                   $self->log(0, $err);
                }
            }
        }
@@ -619,9 +621,9 @@ sub greylist_value {
 
        if (!defined($ref->{rctime})) {
 
-           $dbh->do ("SELECT merge_greylist(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
-                     $net, $host, $sender, $rcpt, $instance,
-                     $ctime, $ctime + $greylist_lifetime, 0, 1, 0, $ctime, $self->{lcid});
+           $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef,
+                    $net, $host, $sender, $rcpt, $instance,
+                    $ctime, $ctime + $greylist_lifetime, 0, 1, 0, $ctime, $self->{lcid});
 
            $res = $defer_res;
            $self->log(3, "defer greylisted mail");
@@ -662,7 +664,7 @@ sub greylist_value {
     };
     if (my $err = $@) {
        $dbh->rollback;
-       $self->log (0, PMG::Utils::msgquote($err));
+       $self->log (0, $err);
     }
 
     return $res;
@@ -715,7 +717,7 @@ sub mux_input {
                            lc ($attribute->{instance}));
                    };
                    if (my $err = $@) {
-                       $self->log(0, PMG::Utils::msgquote($err));
+                       $self->log(0, $err);
                    }
                }
 
@@ -735,7 +737,7 @@ sub mux_input {
        $$dataref = '';
     }
 
-    $self->log(0, PMG::Utils::msgquote($err)) if $err;
+    $self->log(0, $err) if $err;
 }
 
 sub restart_close_hook {
@@ -757,6 +759,24 @@ sub pre_server_close_hook {
     if (defined $prop->{_HUP}) {
        undef $prop->{pid_file_unlink};
     }
+
+    if (defined $prop->{children}) {
+       foreach my $pid (keys %{$prop->{children}}) {
+           kill(1, $pid); # HUP childs
+       }
+    }
+
+    # nicely shutdown childs (give them max 30 seconds to shut down)
+    my $previous_alarm = alarm(30);
+    eval {
+       local $SIG{ALRM} = sub { die "Timed Out!\n" };
+
+       my $pid;
+       1 while ((($pid = waitpid(-1, 0)) > 0) || ($! == EINTR));
+
+       alarm(0); # avoid race
+    };
+    alarm ($previous_alarm);
 }
 
 sub setup_fork_signal_mask {
@@ -841,7 +861,7 @@ sub run_child {
     # sometimes the socket is not usable, don't know why
     my $flags = fcntl($sock, F_GETFL, 0);
     if (!$flags) {
-       $self->log(0, PMG::Utils::msgquote("socket not ready - $!"));
+       $self->log(0, "socket not ready - $!");
        exit (-1);
     }
 
@@ -854,7 +874,7 @@ sub run_child {
        $mux->loop;
     };
     if (my $err = $@) {
-       $self->log(0, PMG::Utils::msgquote("ERROR: $err"));
+       $self->log(0, "ERROR: $err");
     }
 
     $self->child_finish_hook;