14 use Time
::HiRes qw
(gettimeofday
);
24 my $msg = shift || '';
32 return $dbh->last_insert_id(
33 undef, undef, undef, undef, { sequence
=> $seq});
37 my ($filename, $lasttime) = @_;
39 my $st = stat($filename);
41 return 0 if !defined($st);
43 return ($lasttime >= $st->ctime);
46 sub extract_filename
{
49 if (my $value = $head->recommended_filename()) {
51 if (my $decvalue = MIME
::Words
::decode_mimewords
($value)) {
52 $decvalue =~ s/\0/ /g;
53 $decvalue = PVE
::Tools
::trim
($decvalue);
62 my ($entity, $add_id, $id) = @_;
66 foreach my $tag (grep {/^x-proxmox-tmp/i} $entity->head->tags) {
67 $entity->head->delete ($tag);
70 $entity->head->replace('X-Proxmox-tmp-AID', $id) if $add_id;
72 foreach my $part ($entity->parts) {
73 $id = remove_marks
($part, $add_id, $id + 1);
84 foreach my $k (keys %$dh) {
87 $body =~ s/__\Q${k}\E__/$v/gs;
95 my ($entity, $sender, $targets, $xforward, $me, $nodsn) = @_;
103 my $smtp = Net
::SMTP-
>new('127.0.0.1', Port
=> 10025, Hello
=> $me) ||
104 die "unable to connect to localhost at port 10025";
106 if (defined($xforward)) {
109 foreach my $attr (keys %{$xforward}) {
110 $xfwd .= " $attr=$xforward->{$attr}";
113 if ($xfwd && $smtp->command("XFORWARD", $xfwd)->response() != CMD_OK
) {
114 syslog
('err', "xforward error - got: %s %s", $smtp->code, scalar($smtp->message));
118 if (!$smtp->mail($sender)) {
119 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
120 die "smtp from: ERROR";
123 my $dsnopts = $nodsn ?
{Notify
=> ['NEVER']} : {};
125 if (!$smtp->to (@$targets, $dsnopts)) {
126 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
127 die "smtp to: ERROR";
131 #$entity->sync_headers ();
134 my $out = PMG
::SMTPPrinter-
>new($smtp);
135 $entity->print($out);
137 # make sure we always have a newline at the end of the mail
138 # else dataend() fails
139 $smtp->datasend("\n");
141 if ($smtp->dataend()) {
142 my @msgs = $smtp->message;
143 $resmess = $msgs[$#msgs];
144 ($resid) = $resmess =~ m/Ok: queued as ([0-9A-Z]+)/;
145 $rescode = $smtp->code;
147 die sprintf("unexpected SMTP result - got: %s %s : WARNING", $smtp->code, $resmess);
150 my @msgs = $smtp->message;
151 $resmess = $msgs[$#msgs];
152 $rescode = $smtp->code;
153 die sprintf("sending data failed - got: %s %s : ERROR", $smtp->code, $resmess);
158 $smtp->quit if $smtp;
161 syslog
('err', $err);
164 return wantarray ?
($resid, $rescode, $resmess) : $resid;
167 sub analyze_virus_clam
{
168 my ($queue, $dname, $pmg_cfg) = @_;
173 my $clamdscan_opts = "--stdout";
175 my ($csec, $usec) = gettimeofday
();
181 $previous_alarm = alarm($timeout);
184 die "$queue->{logid}: Maximum time ($timeout sec) exceeded. " .
185 "virus analyze (clamav) failed: ERROR";
188 open(CMD
, "/usr/bin/clamdscan $clamdscan_opts '$dname'|") ||
189 die "$queue->{logid}: can't exec clamdscan: $! : ERROR";
194 while (defined(my $line = <CMD
>)) {
195 if ($line =~ m/^$dname.*:\s+([^ :]*)\s+FOUND$/) {
196 # we just use the first detected virus name
197 $vinfo = $1 if !$vinfo;
198 } elsif ($line =~ m/^Infected files:\s(\d*)$/i) {
207 alarm(0); # avoid race conditions
209 if (!defined($ifiles)) {
210 die "$queue->{logid}: got undefined output from " .
211 "virus detector: $response : ERROR";
215 syslog
('info', "$queue->{logid}: virus detected: $vinfo (clamav)");
220 alarm($previous_alarm);
222 my ($csec_end, $usec_end) = gettimeofday
();
223 $queue->{ptime_clam
} =
224 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
227 syslog
('err', $err);
229 $queue->{errors
} = 1;
232 $queue->{vinfo_clam
} = $vinfo;
234 return $vinfo ?
"$vinfo (clamav)" : undef;
238 my ($queue, $filename, $pmg_cfg, $testmode) = @_;
240 # TODO: support other virus scanners?
242 # always scan with clamav
243 return analyze_virus_clam
($queue, $filename, $pmg_cfg);
246 sub magic_mime_type_for_file
{
249 # we do not use get_mime_type_for_file, because that considers
250 # filename extensions - we only want magic type detection
252 my $bufsize = Xdgmime
::xdg_mime_get_max_buffer_extents
();
253 die "got strange value for max_buffer_extents" if $bufsize > 4096*10;
255 my $ct = "application/octet-stream";
257 my $fh = IO
::File-
>new("<$filename") ||
258 die "unable to open file '$filename' - $!";
261 if (($len = $fh->read($buf, $bufsize)) > 0) {
262 $ct = xdg_mime_get_mime_type_for_data
($buf, $len);
266 die "unable to read file '$filename' - $!" if ($len < 0);
274 if (my $path = $entity->{PMX_decoded_path
}) {
276 # set a reasonable default if magic does not give a result
277 $entity->{PMX_magic_ct
} = $entity->head->mime_attr('content-type');
279 if (my $ct = magic_mime_type_for_file
($path)) {
280 if ($ct ne 'application/octet-stream' || !$entity->{PMX_magic_ct
}) {
281 $entity->{PMX_magic_ct
} = $ct;
285 my $filename = $entity->head->recommended_filename;
286 $filename = basename
($path) if !defined($filename) || $filename eq '';
288 if (my $ct = xdg_mime_get_mime_type_from_file_name
($filename)) {
289 $entity->{PMX_glob_ct
} = $ct;
293 foreach my $part ($entity->parts) {
294 add_ct_marks
($part);