14 use Time
::HiRes qw
(gettimeofday
);
28 my $msg = shift || '';
36 return $dbh->last_insert_id(
37 undef, undef, undef, undef, { sequence
=> $seq});
41 my ($filename, $lasttime) = @_;
43 my $st = stat($filename);
45 return 0 if !defined($st);
47 return ($lasttime >= $st->ctime);
50 sub extract_filename
{
53 if (my $value = $head->recommended_filename()) {
55 if (my $decvalue = MIME
::Words
::decode_mimewords
($value)) {
56 $decvalue =~ s/\0/ /g;
57 $decvalue = PVE
::Tools
::trim
($decvalue);
66 my ($entity, $add_id, $id) = @_;
70 foreach my $tag (grep {/^x-proxmox-tmp/i} $entity->head->tags) {
71 $entity->head->delete ($tag);
74 $entity->head->replace('X-Proxmox-tmp-AID', $id) if $add_id;
76 foreach my $part ($entity->parts) {
77 $id = remove_marks
($part, $add_id, $id + 1);
88 foreach my $k (keys %$dh) {
91 $body =~ s/__\Q${k}\E__/$v/gs;
99 my ($entity, $sender, $targets, $xforward, $me, $nodsn) = @_;
107 my $smtp = Net
::SMTP-
>new('127.0.0.1', Port
=> 10025, Hello
=> $me) ||
108 die "unable to connect to localhost at port 10025";
110 if (defined($xforward)) {
113 foreach my $attr (keys %{$xforward}) {
114 $xfwd .= " $attr=$xforward->{$attr}";
117 if ($xfwd && $smtp->command("XFORWARD", $xfwd)->response() != CMD_OK
) {
118 syslog
('err', "xforward error - got: %s %s", $smtp->code, scalar($smtp->message));
122 if (!$smtp->mail($sender)) {
123 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
124 die "smtp from: ERROR";
127 my $dsnopts = $nodsn ?
{Notify
=> ['NEVER']} : {};
129 if (!$smtp->to (@$targets, $dsnopts)) {
130 syslog
('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
131 die "smtp to: ERROR";
135 #$entity->sync_headers ();
138 my $out = PMG
::SMTPPrinter-
>new($smtp);
139 $entity->print($out);
141 # make sure we always have a newline at the end of the mail
142 # else dataend() fails
143 $smtp->datasend("\n");
145 if ($smtp->dataend()) {
146 my @msgs = $smtp->message;
147 $resmess = $msgs[$#msgs];
148 ($resid) = $resmess =~ m/Ok: queued as ([0-9A-Z]+)/;
149 $rescode = $smtp->code;
151 die sprintf("unexpected SMTP result - got: %s %s : WARNING", $smtp->code, $resmess);
154 my @msgs = $smtp->message;
155 $resmess = $msgs[$#msgs];
156 $rescode = $smtp->code;
157 die sprintf("sending data failed - got: %s %s : ERROR", $smtp->code, $resmess);
162 $smtp->quit if $smtp;
165 syslog
('err', $err);
168 return wantarray ?
($resid, $rescode, $resmess) : $resid;
171 sub analyze_virus_clam
{
172 my ($queue, $dname, $pmg_cfg) = @_;
177 my $clamdscan_opts = "--stdout";
179 my ($csec, $usec) = gettimeofday
();
185 $previous_alarm = alarm($timeout);
188 die "$queue->{logid}: Maximum time ($timeout sec) exceeded. " .
189 "virus analyze (clamav) failed: ERROR";
192 open(CMD
, "/usr/bin/clamdscan $clamdscan_opts '$dname'|") ||
193 die "$queue->{logid}: can't exec clamdscan: $! : ERROR";
198 while (defined(my $line = <CMD
>)) {
199 if ($line =~ m/^$dname.*:\s+([^ :]*)\s+FOUND$/) {
200 # we just use the first detected virus name
201 $vinfo = $1 if !$vinfo;
202 } elsif ($line =~ m/^Infected files:\s(\d*)$/i) {
211 alarm(0); # avoid race conditions
213 if (!defined($ifiles)) {
214 die "$queue->{logid}: got undefined output from " .
215 "virus detector: $response : ERROR";
219 syslog
('info', "$queue->{logid}: virus detected: $vinfo (clamav)");
224 alarm($previous_alarm);
226 my ($csec_end, $usec_end) = gettimeofday
();
227 $queue->{ptime_clam
} =
228 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
231 syslog
('err', $err);
233 $queue->{errors
} = 1;
236 $queue->{vinfo_clam
} = $vinfo;
238 return $vinfo ?
"$vinfo (clamav)" : undef;
242 my ($queue, $filename, $pmg_cfg, $testmode) = @_;
244 # TODO: support other virus scanners?
246 # always scan with clamav
247 return analyze_virus_clam
($queue, $filename, $pmg_cfg);
250 sub magic_mime_type_for_file
{
253 # we do not use get_mime_type_for_file, because that considers
254 # filename extensions - we only want magic type detection
256 my $bufsize = Xdgmime
::xdg_mime_get_max_buffer_extents
();
257 die "got strange value for max_buffer_extents" if $bufsize > 4096*10;
259 my $ct = "application/octet-stream";
261 my $fh = IO
::File-
>new("<$filename") ||
262 die "unable to open file '$filename' - $!";
265 if (($len = $fh->read($buf, $bufsize)) > 0) {
266 $ct = xdg_mime_get_mime_type_for_data
($buf, $len);
270 die "unable to read file '$filename' - $!" if ($len < 0);
278 if (my $path = $entity->{PMX_decoded_path
}) {
280 # set a reasonable default if magic does not give a result
281 $entity->{PMX_magic_ct
} = $entity->head->mime_attr('content-type');
283 if (my $ct = magic_mime_type_for_file
($path)) {
284 if ($ct ne 'application/octet-stream' || !$entity->{PMX_magic_ct
}) {
285 $entity->{PMX_magic_ct
} = $ct;
289 my $filename = $entity->head->recommended_filename;
290 $filename = basename
($path) if !defined($filename) || $filename eq '';
292 if (my $ct = xdg_mime_get_mime_type_from_file_name
($filename)) {
293 $entity->{PMX_glob_ct
} = $ct;
297 foreach my $part ($entity->parts) {
298 add_ct_marks
($part);
302 # x509 certificate utils
304 my $proxmox_tls_cert_fn = "/etc/proxmox/proxmox-tls.pem";
306 sub gen_proxmox_tls_cert
{
307 my ($force, $company, $cn) = @_;
309 return if !$force && -f
$proxmox_tls_cert_fn;
311 my $sslconf = <<__EOD__;
312 RANDFILE = /root/.rnd
317 distinguished_name = req_distinguished_name
318 req_extensions = v3_req
320 string_mask = nombstr
322 [ req_distinguished_name ]
323 organizationalUnitName = Proxmox Mail Gateway
324 organizationName = $company
328 basicConstraints = CA:FALSE
330 keyUsage = nonRepudiation, digitalSignature, keyEncipherment
333 my $cfgfn = "/tmp/proxmoxtlsconf-$$.tmp";
334 my $fh = IO
::File-
>new ($cfgfn, "w");
339 PVE
::Tools
::run_command
(['openssl', 'req', '-batch', '-x509', '-new', '-sha256',
340 '-config', $cfgfn, '-days', 3650, '-nodes',
341 '-out', $proxmox_tls_cert_fn,
342 '-keyout', $proxmox_tls_cert_fn]);
346 unlink $proxmox_tls_cert_fn;
348 die "unable to generate proxmox certificate request:\n$err";
354 sub find_local_network_for_ip
{
357 my $testip = Net
::IP-
>new($ip);
359 my $isv6 = $testip->version == 6;
361 PVE
::ProcFSTools
::read_proc_net_ipv6_route
() :
362 PVE
::ProcFSTools
::read_proc_net_route
();
364 foreach my $entry (@$routes) {
367 $mask = $entry->{prefix
};
368 next if !$mask; # skip the default route...
370 $mask = $PVE::Network
::ipv4_mask_hash_localnet-
>{$entry->{mask
}};
371 next if !defined($mask);
373 my $cidr = "$entry->{dest}/$mask";
374 my $testnet = Net
::IP-
>new($cidr);
375 my $overlap = $testnet->overlaps($testip);
376 if ($overlap == $Net::IP
::IP_B_IN_A_OVERLAP
||
377 $overlap == $Net::IP
::IP_IDENTICAL
)
383 die "unable to detect local network for ip '$ip'\n";