]>
git.proxmox.com Git - pmg-api.git/blob - PMG/Utils.pm
12 use Time
::HiRes qw
(gettimeofday
);
18 my $msg = shift || '';
26 return $dbh->last_insert_id(
27 undef, undef, undef, undef, { sequence
=> $seq});
31 my ($filename, $lasttime) = @_;
33 my $st = stat($filename);
35 return 0 if !defined($st);
37 return ($lasttime >= $st->ctime);
40 sub extract_filename
{
43 if (my $value = $head->recommended_filename()) {
45 if (my $decvalue = MIME
::Words
::decode_mimewords
($value)) {
46 $decvalue =~ s/\0/ /g;
47 $decvalue = trim
($decvalue);
56 my ($entity, $add_id, $id) = @_;
60 foreach my $tag (grep {/^x-proxmox-tmp/i} $entity->head->tags) {
61 $entity->head->delete ($tag);
64 $entity->head->replace('X-Proxmox-tmp-AID', $id) if $add_id;
66 foreach my $part ($entity->parts) {
67 $id = remove_marks
($part, $add_id, $id + 1);
78 foreach my $k (keys %$dh) {
81 $body =~ s/__${k}__/$v/gs;
89 my ($entity, $sender, $targets, $xforward, $me, $nodsn) = @_;
97 my $smtp = Net
::SMTP-
>new('127.0.0.1', Port
=> 10025, Hello
=> $me) ||
98 die "unable to connect to localhost at port 10025";
100 if (defined($xforward)) {
103 foreach my $attr (keys %{$xforward}) {
104 $xfwd .= " $attr=$xforward->{$attr}";
107 if ($xfwd && $smtp->command("XFORWARD", $xfwd)->response() != CMD_OK
) {
108 syslog
('err', "xforward error - got: %s %s", $smtp->code, scalar($smtp->message));
112 if (!$smtp->mail($sender)) {
113 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
114 die "smtp from: ERROR";
117 my $dsnopts = $nodsn ?
{Notify
=> ['NEVER']} : {};
119 if (!$smtp->to (@$targets, $dsnopts)) {
120 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
121 die "smtp to: ERROR";
125 #$entity->sync_headers ();
128 my $out = PMG
::SMTPPrinter-
>new($smtp);
129 $entity->print($out);
131 # make sure we always have a newline at the end of the mail
132 # else dataend() fails
133 $smtp->datasend("\n");
135 if ($smtp->dataend()) {
136 my @msgs = $smtp->message;
137 $resmess = $msgs[$#msgs];
138 ($resid) = $resmess =~ m/Ok: queued as ([0-9A-Z]+)/;
139 $rescode = $smtp->code;
141 die sprintf("unexpected SMTP result - got: %s %s : WARNING", $smtp->code, $resmess);
144 my @msgs = $smtp->message;
145 $resmess = $msgs[$#msgs];
146 $rescode = $smtp->code;
147 die sprintf("sending data failed - got: %s %s : ERROR", $smtp->code, $resmess);
152 $smtp->quit if $smtp;
155 syslog
('err', $err);
158 return wantarray ?
($resid, $rescode, $resmess) : $resid;
161 sub analyze_virus_clam
{
162 my ($queue, $dname, $pmg_cfg) = @_;
167 my $clamdscan_opts = "--stdout";
169 my ($csec, $usec) = gettimeofday
();
175 $previous_alarm = alarm($timeout);
178 die "$queue->{logid}: Maximum time ($timeout sec) exceeded. " .
179 "virus analyze (clamav) failed: ERROR";
182 open(CMD
, "/usr/bin/clamdscan $clamdscan_opts '$dname'|") ||
183 die "$queue->{logid}: can't exec clamdscan: $! : ERROR";
189 if (m/^$dname.*:\s+([^ :]*)\s+FOUND$/) {
190 # we just use the first detected virus name
191 $vinfo = $1 if !$vinfo;
193 if (m/^Infected files:\s(\d*)$/i) {
202 alarm(0); # avoid race conditions
204 if (!defined($ifiles)) {
205 die "$queue->{logid}: got undefined output from " .
206 "virus detector: $res : ERROR";
210 syslog
('info', "$queue->{logid}: virus detected: $vinfo (clamav)");
215 alarm($previous_alarm);
217 my ($csec_end, $usec_end) = gettimeofday
();
218 $queue->{ptime_clam
} =
219 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
222 syslog
('err', $err);
224 $queue->{errors
} = 1;
227 $queue->{vinfo_clam
} = $vinfo;
229 return $vinfo ?
"$vinfo (clamav)" : undef;
233 my ($queue, $filename, $pmg_cfg, $testmode) = @_;
235 # TODO: support other virus scanners?
237 # always scan with clamav
238 return analyze_virus_clam
($queue, $filename, $pmg_cfg);