]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Utils.pm
fix bug #2035 cluster sync issues with ipv6
[pmg-api.git] / PMG / Utils.pm
index 563d800e0a841e34bac916e2ddab40d856becaad..26c09218674f7627312bcb4a8c44f1743f964ac7 100644 (file)
@@ -23,6 +23,9 @@ use Socket;
 use RRDs;
 use Filesys::Df;
 use Encode;
+use utf8;
+no utf8;
+
 use HTML::Entities;
 
 use PVE::ProcFSTools;
@@ -208,16 +211,42 @@ sub reinject_mail {
            }
        }
 
-       if (!$smtp->mail($sender)) {
+       my $has_utf8_targets = 0;
+       foreach my $target (@$targets) {
+           if (utf8::is_utf8($target)) {
+               $has_utf8_targets = 1;
+               last;
+           }
+       }
+
+       my $mail_opts = " BODY=8BITMIME";
+       my $sender_addr;
+       if (utf8::is_utf8($sender)) {
+           $sender_addr = encode('UTF-8', $smtp->_addr($sender));
+           $mail_opts .= " SMTPUTF8";
+       } else {
+           $sender_addr = $smtp->_addr($sender);
+           $mail_opts .= " SMTPUTF8" if $has_utf8_targets;
+       }
+
+       if (!$smtp->_MAIL("FROM:" . $sender_addr . $mail_opts)) {
            syslog('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
            die "smtp from: ERROR";
        }
 
-       my $dsnopts = $nodsn ? {Notify => ['NEVER']} : {};
+       my $rcpt_opts = $nodsn ? " NOTIFY=NEVER" : "";
 
-       if (!$smtp->to (@$targets, $dsnopts)) {
-           syslog ('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
-           die "smtp to: ERROR";
+       foreach my $target (@$targets) {
+           my $rcpt_addr;
+           if (utf8::is_utf8($target)) {
+               $rcpt_addr = encode('UTF-8', $smtp->_addr($target));
+           } else {
+               $rcpt_addr = $smtp->_addr($target);
+           }
+           if (!$smtp->_RCPT("TO:" . $rcpt_addr . $rcpt_opts)) {
+               syslog ('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
+               die "smtp to: ERROR";
+           }
        }
 
        # Output the head:
@@ -327,13 +356,96 @@ sub analyze_virus_clam {
     return $vinfo ? "$vinfo (clamav)" : undef;
 }
 
+sub analyze_virus_avast {
+    my ($queue, $dname, $pmg_cfg) = @_;
+
+    my $timeout = 60*5;
+    my $vinfo;
+
+    my ($csec, $usec) = gettimeofday();
+
+    my $previous_alarm;
+
+    eval {
+
+       $previous_alarm = alarm($timeout);
+
+       $SIG{ALRM} = sub {
+           die "$queue->{logid}: Maximum time ($timeout sec) exceeded. " .
+               "virus analyze (avast) failed: ERROR";
+       };
+
+       open(my $cmd, '-|', '/bin/scan', $dname) ||
+           die "$queue->{logid}: can't exec avast scan: $! : ERROR";
+
+       my $response = '';
+       while (defined(my $line = <$cmd>)) {
+           if ($line =~ m/^$dname\s+(.*\S)\s*$/) {
+               # we just use the first detected virus name
+               $vinfo = $1 if !$vinfo;
+           }
+
+           $response .= $line;
+       }
+
+       close($cmd);
+
+       alarm(0); # avoid race conditions
+
+       if ($vinfo) {
+           syslog('info', "$queue->{logid}: virus detected: $vinfo (avast)");
+       }
+    };
+    my $err = $@;
+
+    alarm($previous_alarm);
+
+    my ($csec_end, $usec_end) = gettimeofday();
+    $queue->{ptime_clam} =
+       int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
+
+    if ($err) {
+       syslog ('err', $err);
+       $vinfo = undef;
+       $queue->{errors} = 1;
+    }
+
+    return undef if !$vinfo;
+
+    $queue->{vinfo_avast} = $vinfo;
+
+    return "$vinfo (avast)";
+}
+
 sub analyze_virus {
     my ($queue, $filename, $pmg_cfg, $testmode) = @_;
 
     # TODO: support other virus scanners?
 
-    # always scan with clamav
-    return analyze_virus_clam($queue, $filename, $pmg_cfg);
+    if ($testmode) {
+       my $vinfo_clam = analyze_virus_clam($queue, $filename, $pmg_cfg);
+       my $vinfo_avast = analyze_virus_avast($queue, $filename, $pmg_cfg);
+
+       return $vinfo_avast || $vinfo_clam;
+    }
+
+    my $enable_avast  = $pmg_cfg->get('admin', 'avast');
+
+    if ($enable_avast) {
+       if (my $vinfo = analyze_virus_avast($queue, $filename, $pmg_cfg)) {
+           return $vinfo;
+       }
+    }
+
+    my $enable_clamav = $pmg_cfg->get('admin', 'clamav');
+
+    if ($enable_clamav) {
+       if (my $vinfo = analyze_virus_clam($queue, $filename, $pmg_cfg)) {
+           return $vinfo;
+       }
+    }
+
+    return undef;
 }
 
 sub magic_mime_type_for_file {
@@ -471,7 +583,7 @@ __EOD__
 }
 
 sub find_local_network_for_ip {
-    my ($ip) = @_;
+    my ($ip, $noerr) = @_;
 
     my $testip = Net::IP->new($ip);
 
@@ -499,6 +611,8 @@ sub find_local_network_for_ip {
        }
     }
 
+    return undef if $noerr;
+
     die "unable to detect local network for ip '$ip'\n";
 }
 
@@ -574,6 +688,10 @@ sub service_cmd {
 
     if ($service eq 'pmgdaemon' || $service eq 'pmgproxy') {
        die "invalid service cmd '$service $cmd': ERROR" if $cmd eq 'stop';
+    } elsif ($service eq 'fetchmail') {
+       # use restart instead of start - else it does not start 'exited' unit
+       # after setting START_DAEMON=yes in /etc/default/fetchmail
+       $cmd = 'restart' if $cmd eq 'start';
     }
 
     $service = $service_aliases->{$service} // $service;
@@ -612,11 +730,13 @@ sub run_postmap {
     # make sure the file exists (else postmap fails)
     IO::File->new($filename, 'a', 0644);
 
-    my $age_src = -M $filename // 0;
-    my $age_dst = -M "$filename.db" // 10000000000;
+    my $mtime_src = (CORE::stat($filename))[9] //
+       die "unbale to read mtime of $filename\n";
+
+    my $mtime_dst = (CORE::stat("$filename.db"))[9] // 0;
 
     # if not changed, do nothing
-    return if $age_src > $age_dst;
+    return if $mtime_src <= $mtime_dst;
 
     eval {
        PVE::Tools::run_command(