]> git.proxmox.com Git - pmg-api.git/blame - bin/pmg-smtp-filter
pmgconfig: new CLI helper
[pmg-api.git] / bin / pmg-smtp-filter
CommitLineData
b429250e 1#!/usr/bin/perl
6f4aeba1
DM
2
3use strict;
f62194b2
DM
4use warnings;
5
6f4aeba1
DM
6use Carp;
7use Getopt::Long;
21853580 8use Time::HiRes qw (usleep gettimeofday tv_interval);
6f4aeba1
DM
9use POSIX qw(:sys_wait_h errno_h signal_h);
10
11use MIME::Parser;
12use File::Path;
13use Net::Server::PreFork;
14use Net::Server::SIG qw(register_sig check_sigs);
97662001
DM
15use Net::SMTP;
16
6f4aeba1
DM
17use Fcntl ':flock';
18use File::Basename;
6f4aeba1 19use Xdgmime;
f62194b2
DM
20
21use PVE::SafeSyslog;
22use PVE::ProcFSTools;
23use PVE::INotify;
6f4aeba1
DM
24
25use Mail::SpamAssassin;
26use Mail::SpamAssassin::NetSet;
6f4aeba1 27
f62194b2
DM
28use PMG::pmgcfg;
29use PMG::Utils;
30use PMG::Cluster;
6f4aeba1 31
f62194b2
DM
32use PMG::DBTools;
33use PMG::RuleDB;
34use PMG::RuleCache;
35use PMG::ModGroup;
36use PMG::AtomicFile;
37use PMG::LDAPSet;
38use PMG::Config;
a43bcf61 39use PMG::MailQueue;
91a5af02 40use PMG::Unpack;
521dca53 41use PMG::SMTP;
6f4aeba1 42
521dca53
DM
43use PMG::Unpack;
44use PMG::Statistic;
f62194b2
DM
45
46use base qw(Net::Server::PreFork);
6f4aeba1
DM
47
48my $opt_commandline = [$0, @ARGV];
49my $opt_max_dequeue = 1;
50my $opt_dequeue_time = 60*2;
51
52my $opt_ext_port = 10024;
53my $opt_int_port = 10023;
54my $opt_inject_port = 10025;
55
56my $opt_testmode;
57my $opt_untrusted;
58my $opt_pidfile;
59my $opt_database;
60
e646f7ca
DM
61my $prog_name = 'pmg-smtp-filter';
62
63initlog($prog_name, 'mail');
6f4aeba1
DM
64
65if (!GetOptions ('testmode=s' => \$opt_testmode,
f62194b2
DM
66 'pidfile=s' => \$opt_pidfile,
67 'untrusted' => \$opt_untrusted,
6f4aeba1
DM
68 'database=s' => \$opt_database)) {
69 die "usage error\n";
70 exit (-1);
71}
72
e646f7ca 73$opt_pidfile = "/var/run/${prog_name}.pid" if !$opt_pidfile;
6f4aeba1
DM
74
75my $max_servers = 1;
76my $min_servers = 1;
77my $min_spare_servers = 0;
78my $max_spare_servers = 0;
79my $max_requests = 1;
80
ca49df5f
DM
81# create spool directories
82PMG::MailQueue::create_sppoldirs();
83
6f4aeba1
DM
84if (!$opt_testmode) {
85
f62194b2
DM
86 my $pmg_cfg = PMG::Config->new();
87
ac5d1312 88 my $demo = $pmg_cfg->get('admin', 'demo');
6f4aeba1
DM
89
90 if ($demo) {
91 syslog ('info', 'demo mode detected - not starting server');
92 exit (0);
93 }
94
f62194b2 95 $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2;
6f4aeba1
DM
96 $min_servers = 2;
97 $min_spare_servers = 1;
98 $max_spare_servers = 4;
99 $max_requests = 20;
100}
101
102$opt_max_dequeue = 0 if $opt_testmode;
103
104my $daemonize = 1;
105if (defined ($ENV{BOUND_SOCKETS})) {
106 $daemonize = undef;
107}
108
109my $server_attr = {
110 port => [ $opt_int_port, $opt_ext_port ],
111 host => '127.0.0.1',
112 min_servers => $min_servers,
113 max_servers => $max_servers,
114 min_spare_servers => $min_spare_servers,
115 max_spare_servers => $max_spare_servers,
116 max_requests => $max_requests,
117 serialize => 'flock',
118 max_dequeue => $opt_max_dequeue,
119 check_for_dequeue => $opt_dequeue_time,
120 log_level => 3,
121 pid_file => $opt_pidfile,
122 no_close_by_child => 1,
123 no_client_stdout => 1,
124 commandline => $opt_commandline,
125};
126
127$server_attr->{setsid} = $daemonize if !$opt_testmode;
128
129my $database;
130
131if (defined($opt_database)) {
132 $database = $opt_database;
133} else {
134 $database = $opt_testmode ? "Proxmox_testdb" : "Proxmox_ruledb";
135}
136
137$SIG{'__WARN__'} = sub {
138 my $err = $@;
139 my $t = $_[0];
140 chomp $t;
141 syslog('warning', "WARNING: %s", $t);
142 $@ = $err;
143};
144
145sub get_prox_vars {
146 my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_;
147
148 $spaminfo = {
149 sa_score => $queue->{sa_score},
150 sa_hits => $queue->{sa_hits},
151 sa_data => $queue->{sa_data},
152 sa_max => $queue->{sa_max}
153 } if !$spaminfo;
154
155 my $vars = {
156 'SUBJECT' => $entity->head->get ('subject', 0) || 'No Subject',
157 'RULE' => $rule->{name},
158 'RULE_INFO' => $msginfo->{rule_info},
159 'SENDER' => $msginfo->{sender},
160 'SENDER_IP' => $msginfo->{xforward}->{addr},
161 'TARGETS' => join (', ', @$targets),
162 'RECEIVERS' => join (', ', @{$msginfo->{targets}}),
163 'SPAMLEVEL' => $spaminfo->{sa_score},
164 'SPAMSTARS' => '*' x (($spaminfo->{sa_score} || 0) > 100 ? 100 : $spaminfo->{sa_score} || 0),
ac5d1312 165 'ADMIN' => $self->{pmg_cfg}->get('admin', 'email'),
6f4aeba1
DM
166 'HOST' => $msginfo->{hostname},
167 'DOMAIN' => $msginfo->{domain},
168 'FQDN' => $msginfo->{fqdn},
169 'MSGID' => $queue->{msgid},
f62194b2 170 'VERSION' => PMG::pmgcfg::package() . "/" . PMG::pmgcfg::version() . "/" . PMG::pmgcfg::repoid(),
6f4aeba1
DM
171 };
172
173 $vars->{__spaminfo} = $spaminfo;
174
175 if ($opt_testmode) {
521dca53 176 if ($queue->{vinfo_clam}) {
6f4aeba1
DM
177 $vars->{'VIRUS_INFO'} = "Virus Info:";
178 $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam};
6f4aeba1
DM
179 } else {
180 $vars->{'VIRUS_INFO'} = '';
181 }
182 } else {
183 if ($queue->{vinfo}) {
184 $vars->{'VIRUS_INFO'} = "Virus Info: $queue->{vinfo}\n";
185 } else {
186 $vars->{'VIRUS_INFO'} = '';
187 }
188 }
189
190 $vars->{'SPAM_HITS'} = $spaminfo->{sa_hits};
191
192 $vars->{'SPAM_INFO'} = '';
193 my $sscores = $spaminfo->{sa_data};
194
195 if (defined ($sscores) && @$sscores != -1) {
196 my $sa_text;
197 if ($opt_testmode) {
198 $sa_text = "Spam detection results: 100\n";
199 } else {
200 $sa_text = "Spam detection results: $spaminfo->{sa_score}\n";
201 }
202
203 foreach my $s (@$sscores) {
204 if ($opt_testmode) {
f62194b2 205 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule},
6f4aeba1
DM
206 1, $s->{desc} || '-');
207 } else {
f62194b2 208 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule},
6f4aeba1
DM
209 $s->{score}, $s->{desc} || '-');
210 }
211 }
212 $vars->{'SPAM_INFO'} = $sa_text;
213 }
214
215 if ($opt_testmode) {
216 delete ($vars->{'ADMIN'});
217 #delete ($vars->{'SPAM_INFO'});
218 }
219
220 return $vars;
221}
222
223sub apply_rules {
224 my ($self, $queue, $msginfo, $entity, $ldap) = @_;
225
226 my $final;
227 my %rule_targets;
228 my %rule_actions;
229 my %rule_marks;
230 my $matching_rules = [];
231
232 my $rulecache = $self->{rulecache};
233 my $rules = $rulecache->rules ();
234 my $dbh = $self->{ruledb}->{dbh};
f62194b2
DM
235
236 # first, we remove all conditional written 'X-' header attributes
6f4aeba1
DM
237 foreach my $rule (@$rules) {
238 next if !$rule->{active};
239 next if ($rule->{direction} == 0) && $msginfo->{trusted};
240 next if ($rule->{direction} == 1) && !$msginfo->{trusted};
241
242 my $actions = $rulecache->get_actions ($rule->{id});
243 if ($actions) {
244 foreach my $action (@$actions) {
521dca53 245 if ($action->isa ("PMG::RuleDB::ModField")) {
6f4aeba1
DM
246 my $fname = $action->{field};
247 next if $fname !~ m/^X-/i;
248 $entity->head->delete($fname);
249 }
250 }
251 }
252 }
253
254 foreach my $rule (@$rules) {
255 next if !$rule->{active};
256 next if ($rule->{direction} == 0) && $msginfo->{trusted};
257 next if ($rule->{direction} == 1) && !$msginfo->{trusted};
258
259 # match from, when and what classes (not target dependent)
260 if (!($rulecache->from_match ($rule->{id}, $msginfo->{sender}, $msginfo->{xforward}->{addr}, $ldap) &&
261 $rulecache->when_match ($rule->{id}, time))) {
262 next;
263 }
264
f62194b2 265 $rule_marks{$rule->{id}} =
6f4aeba1
DM
266 $rulecache->what_match ($rule->{id}, $queue, $entity, $msginfo, $dbh);
267
268 $rule_actions{$rule->{id}} = $rulecache->get_actions ($rule->{id});
269 my $fin = $rulecache->final ($rule->{id});
270
271 # match targets
272 foreach my $target (@{$msginfo->{targets}}) {
273 next if $final->{$target};
274 next if !defined ($rule_marks{$rule->{id}});
275 next if !defined ($rule_marks{$rule->{id}}->{$target});
276 next if !defined ($rule_marks{$rule->{id}}->{$target}->{marks});
277 next if !$rulecache->to_match ($rule->{id}, $target, $ldap);
278
279 $final->{$target} = $fin;
280
281 push @{$rule_targets{$rule->{id}}}, $target;
f62194b2 282 }
6f4aeba1
DM
283 }
284
285 # Compute rule_info (summary about matching rule)
f62194b2 286 # this can be used for debugging
6f4aeba1
DM
287 my $rule_info = "";
288 foreach my $rule (@$rules) {
289 next if !$rule_targets{$rule->{id}};
290
291 push @$matching_rules, $rule->{id};
292
293 $rule_info .= "Rule: $rule->{name}\n";
294
295 foreach my $target (@{$rule_targets{$rule->{id}}}) {
296 $rule_info .= " Receiver: $target\n";
f62194b2 297 }
6f4aeba1
DM
298 foreach my $action (@{$rule_actions{$rule->{id}}}) {
299 $rule_info .= " Action: " . $action->short_desc () . "\n";
300 }
301
302 }
303 $msginfo->{rule_info} = $rule_info;
304
305 if ($msginfo->{testmode}) {
2d611b3b 306 my $vars = $self->get_prox_vars($queue, $entity, $msginfo, undef, [], undef);
6f4aeba1 307 my $fh = $msginfo->{test_fh};
2d611b3b 308 print $fh $rule_info;
6f4aeba1
DM
309 }
310
311 # apply actions
312
f62194b2 313 my $mod_group = PMG::ModGroup->new($entity, $msginfo->{targets});
6f4aeba1
DM
314
315 foreach my $rule (@$rules) {
316 my $targets = $rule_targets{$rule->{id}};
317 next if !$targets;
f62194b2 318
6f4aeba1
DM
319 my $spaminfo;
320 foreach my $t (@$targets) {
321 if ($rule_marks{$rule->{id}}->{$t} && $rule_marks{$rule->{id}}->{$t}->{spaminfo}) {
322 $spaminfo = $rule_marks{$rule->{id}}->{$t}->{spaminfo};
323 # we assume spam info is the same for all matching targets
324 last;
325 }
326 }
327
328 my $vars = $self->get_prox_vars ($queue, $entity, $msginfo, $rule,
329 $rule_targets{$rule->{id}}, $spaminfo);
330
331
f62194b2 332 my @sorted_actions =
6f4aeba1 333 sort {$a->priority <=> $b->priority} @{$rule_actions{$rule->{id}}};
f62194b2 334
6f4aeba1 335 foreach my $action (@sorted_actions) {
f62194b2 336 $action->execute ($queue, $self->{ruledb}, $mod_group,
6f4aeba1
DM
337 $rule_targets{$rule->{id}},
338 $msginfo, $vars, $rule_marks{$rule->{id}}->{marks}, $ldap);
339 last if $action->final;
340 }
341 }
342
343 # we deliver all mail not matched by any rule
344 # (default action = accept)
345 my $unmatched;
346 foreach my $target (@{$msginfo->{targets}}) {
347 next if $final->{$target};
348
349 push @$unmatched, $target;
350 }
351
352 if ($unmatched) {
521dca53 353 my $accept = PMG::RuleDB::Accept->new ();
f62194b2 354 $accept->execute ($queue, $self->{ruledb}, $mod_group, $unmatched,
6f4aeba1
DM
355 $msginfo, undef, undef, undef);
356 }
357
358 return $matching_rules;
359}
360
f62194b2 361# reload ruledb and pmg config
6f4aeba1
DM
362sub load_config {
363 my $self = shift;
364 my $prop = $self->{server};
365
366 if ($self->{ruledb}) {
367 $self->log (0, "reloading configuration $database");
368 $self->{ruledb}->close ();
369 }
370
f62194b2
DM
371 $self->{pmg_cfg} = PMG::Config->new();
372 $self->{cinfo} = PVE::INotify::read_file("cluster.conf");
6f4aeba1
DM
373
374 eval {
f62194b2
DM
375 my $dbh = PMG::DBTools::open_ruledb ($database);
376 $self->{ruledb} = PMG::RuleDB->new ($dbh);
6f4aeba1
DM
377
378 # load rulecache
f62194b2 379 $self->{rulecache} = PMG::RuleCache->new ($self->{ruledb});
6f4aeba1
DM
380 };
381
382 my $err = $@;
383
384 if ($err) {
385 sleep (10); # reduce restart rate when postgres is down
386 die $err;
387 }
388
f62194b2
DM
389 # create LDAP object
390 $self->{ldap} = PMG::LDAPSet->new_from_pmg_cfg($self->{pmg_cfg}, 1);
6f4aeba1 391
f62194b2 392 $self->{reload_config} = 0;
6f4aeba1
DM
393}
394
f62194b2
DM
395my $syslog_map = {
396 0 => 'err',
397 1 => 'warning',
398 2 => 'notice',
399 3 => 'info',
400 4 => 'debug'
401};
402
6f4aeba1
DM
403sub log {
404 my ($self, $level, $msg, @therest) = @_;
405 my $prop = $self->{server};
406
407 return if $level =~ /^\d+$/ && $level > $prop->{log_level};
408
f62194b2 409 $level = $syslog_map->{$level} || $level;
6f4aeba1
DM
410 if (@therest) {
411 syslog($level, $msg, @therest);
412 } else {
413 syslog ($level, $msg);
414 }
415}
416
417sub pre_loop_hook {
418 my $self = shift;
419 my $prop = $self->{server};
420
421 $prop->{log_level} = 3;
422
423 $self->log (0, "Filter daemon (re)started (max. $max_servers processes)");
424
521dca53 425 eval { PMG::MailQueue::cleanup_active(); };
6f4aeba1
DM
426 $self->log (0, "Cleanup failures: $@") if $@;
427
428 my $sig_set = POSIX::SigSet->new;
f62194b2
DM
429 $sig_set->addset (&POSIX::SIGHUP);
430 $sig_set->addset (&POSIX::SIGCHLD);
6f4aeba1
DM
431 my $old_sig_set = POSIX::SigSet->new();
432
433 sigprocmask (SIG_UNBLOCK, $sig_set, $old_sig_set);
434
435 my ($backup_umask) = umask;
436
f62194b2
DM
437 my $pmg_cfg = PMG::Config->new();
438
439 # Note: you need to restart the daemon when you change 'rbl_checks'
440 my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');
6f4aeba1
DM
441
442 $self->{sa} = Mail::SpamAssassin->new ({
443 debug => 0,
f62194b2 444 local_tests_only => $opt_testmode || !$rbl_checks,
6f4aeba1
DM
445 home_dir_for_helpers => '/root',
446 userstate_dir => '/root/.spamassassin',
447 dont_copy_prefs => 1,
448 stop_at_threshold => 0,
449 });
450
451 $self->{sa}->compile_now;
452
453 alarm (0); # SA forgets to clear alarm in some cases
454 umask ($backup_umask);
e646f7ca 455 initlog ($prog_name, 'mail');
6f4aeba1
DM
456
457 $SIG{'USR1'} = sub {
458 # reloading server configuration
459 if (defined $prop->{children}) {
460 foreach my $pid (keys %{$prop->{children}}) {
461 kill (10, $pid); # SIGUSR1 childs
462 }
463 }
464 }
465}
466
467sub child_init_hook {
468 my $self = shift;
469
e646f7ca 470 $0 = "$prog_name child";
6f4aeba1
DM
471
472 # $self->log (3, "init child");
473
474 eval {
475 $self->load_config ();
476 };
477
478 if ($@) {
479 $self->log (0, $@);
480 $self->child_finish_hook;
481 exit;
482 }
483
484 $SIG{'USR1'} = sub {
f62194b2 485 $self->{reload_config} = 1;
6f4aeba1
DM
486 }
487}
488
489sub child_finish_hook {
490 my $self = shift;
f62194b2 491
6f4aeba1
DM
492 # $self->log (3, "finish child");
493 $self->{ruledb}->close () if $self->{ruledb};
494}
495
496sub run_dequeue {
497 my $self = shift;
498
499 # do database maintainance here
500
501 $self->log (2, "starting database maintainance");
502
503 my ($csec, $usec) = gettimeofday ();
504
f62194b2
DM
505 my $cinfo = PVE::INotify::read_file("cluster.conf");
506
6f4aeba1 507 my $dbh;
f62194b2 508
6f4aeba1 509 eval {
f62194b2 510 $dbh = PMG::DBTools::open_ruledb($database);
6f4aeba1 511 };
6f4aeba1
DM
512 my $err = $@;
513
514 if ($err) {
f62194b2 515 $self->log (0, msgquote("ERROR: $err"));
6f4aeba1
DM
516 return;
517 }
518
519 eval {
521dca53 520 PMG::Statistic::update_stats($dbh, $cinfo);
6f4aeba1 521 };
6f4aeba1
DM
522 $err = $@;
523
524 my ($csec_end, $usec_end) = gettimeofday ();
525 my $ptime = int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
526
527 if ($err) {
f62194b2 528 $self->log (0, msgquote($err));
6f4aeba1
DM
529 } else {
530 $self->log (2, "end database maintainance ($ptime ms)");
531 }
532
533 $dbh->disconnect() if $dbh;
534}
535
536
537sub unpack_entity {
538 my ($self, $unpack, $entity, $msginfo, $queue) = @_;
539
540 my $magic;
541 my $path;
542
f62194b2 543 if (($magic = $entity->{PMX_magic_ct}) &&
6f4aeba1
DM
544 ($path = $entity->{PMX_decoded_path})) {
545
546 my $filename = basename ($path);
547
521dca53 548 if (PMG::Unpack::is_archive ($magic)) {
6f4aeba1
DM
549 $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)");
550
551 my $start = [gettimeofday];
552
553 $unpack->{mime} = {};
554
f62194b2
DM
555 eval {
556 $unpack->unpack_archive ($path, $magic);
6f4aeba1
DM
557 };
558
559 $self->log (3, "$queue->{logid}: unpack failed - $@") if $@;
560
561 $entity->{PMX_content_types} = $unpack->{mime};
f62194b2 562
6f4aeba1
DM
563 if ($opt_testmode) {
564 my $types = join (", ", sort keys (%{$entity->{PMX_content_types}}));
565 my $fh = $msginfo->{test_fh};
566 $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/;
567 print $fh "Types:$filename: $types\n" if $types;
568 }
569
570 my $elapsed = int(tv_interval ($start) * 1000);
571
572 $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)");
573 }
574 }
575
576 foreach my $part ($entity->parts) {
577 $self->unpack_entity ($unpack, $part, $msginfo, $queue);
578 }
579
580}
581
582sub handle_smtp {
583 my ($self, $smtp) = @_;
584
585 my ($csec, $usec) = gettimeofday ();
586
587 my $queue;
588 my $msginfo = {};
f62194b2 589 my $pmg_cfg = $self->{pmg_cfg};
6f4aeba1
DM
590 my $ldap = $self->{ldap};
591 my $cinfo = $self->{cinfo};
592 my $lcid = $cinfo->{local}->{cid};
f62194b2
DM
593
594 $msginfo->{test_fh} = PMG::AtomicFile->new("testresult.out", "w")
6f4aeba1
DM
595 if $opt_testmode;
596
597 $msginfo->{trusted} = $self->{trusted};
598
599
600# PHASE 1 - save incoming mail (already done)
601# on error: exit
602
603 $queue = $smtp->{queue};
604 $queue->{sa} = $self->{sa};
605
f62194b2 606 $queue->{lic_valid} = 1;
6f4aeba1
DM
607
608 my $matching_rules;
609
610 eval {
611 $msginfo->{testmode} = $opt_testmode;
612 $msginfo->{sender} = $smtp->{from};
613 $msginfo->{xforward} = $smtp->{xforward};
614 $msginfo->{targets} = $smtp->{to};
615
f62194b2
DM
616 $msginfo->{hostname} = PVE::INotify::nodename();
617 my $resolv = PVE::INotify::read_file('resolvconf');
618
619 $msginfo->{domain} = $resolv->{search};
6f4aeba1
DM
620 $msginfo->{fqdn} = "$msginfo->{hostname}.$msginfo->{domain}";
621 $msginfo->{lcid} = $lcid;
f62194b2 622
6f4aeba1
DM
623 # $msginfo->{targets} is case sensitive,
624 # but pmail is always lower case!
625
626 foreach my $t (@{$msginfo->{targets}}) {
627 my $res;
628 if ($ldap && ($res = $ldap->account_info ($t))) {
629 $msginfo->{pmail}->{$t} = $res->{pmail};
630 } else {
631 $msginfo->{pmail}->{$t} = lc ($t);
632 }
633 }
634
635# PHASE 2 - parse mail
636# on error: exit
6f4aeba1 637
f62194b2
DM
638 my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles');
639
640 my $entity = $queue->parse_mail($maxfiles);
6f4aeba1
DM
641
642 $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid});
643
644# PHASE 3 - run external content analyzers
645# (SPAM analyzer is run on demand later)
646# on error: log error messages
647
648
649 # test for virus first
f62194b2
DM
650 $queue->{vinfo} = PMG::Utils::analyze_virus(
651 $queue, $queue->{dataname}, $pmg_cfg, $opt_testmode);
6f4aeba1 652
6f4aeba1
DM
653 # always add this headers to incoming mails
654 # to enable user to report false negatives
655 if (!$msginfo->{trusted}) {
6f4aeba1
DM
656 if ($queue->{vinfo}) {
657 $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo});
658 }
6f4aeba1
DM
659 }
660
661 # we unpack after virus scanning, because this is more secure.
662 # This way virus scanners gets the whole mail files and are able
663 # to detect phishing signature for example - which would not work
664 # if we decompose first and only analyze the decomposed attachments.
f62194b2 665 # Disadvantage is that we need to unpack more than
6f4aeba1 666 # once (bad performance).
f62194b2 667
6f4aeba1
DM
668 # should we scan content types inside archives
669
6f4aeba1
DM
670 my $rulecache = $self->{rulecache};
671
672 my $scan_archives = 0;
673
674 if (($rulecache->{archivefilter_in} && !$msginfo->{trusted}) ||
675 ($rulecache->{archivefilter_out} && $msginfo->{trusted})) {
676 $scan_archives = 1;
677 }
678
679 if ($scan_archives && !$queue->{vinfo}) {
680
681 # unpack all archives - determine contained content types
682
683 my $decdir = $queue->{dumpdir} . "/__decoded_archives";
684 mkdir $decdir;
685
686 my $start = [gettimeofday];
f62194b2 687
6f4aeba1
DM
688 my $unpack;
689 eval {
690
691 # limits: We use clamav limit for maxfiles, and scan
692 # only 4 levels, timeout of 30 seconds
693
521dca53
DM
694 $unpack = PMG::Unpack->new (
695 tmpdir => $decdir,
696 timeout => 30,
697 ctonly => 1, # only detect CTs
698 maxrec => -4,
699 maxfiles => $maxfiles);
6f4aeba1 700
f62194b2 701 $self->unpack_entity ($unpack, $entity, $msginfo, $queue);
6f4aeba1
DM
702 };
703
704 my $err = $@;
705
706 $unpack->cleanup() if $unpack;
707
708 my $elapsed = int(tv_interval ($start) * 1000);
709
710 if ($err) {
711 $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err");
712 }
713 }
714
715# PHASE 4 - apply rules
716# on error: exit (cleanup process should do the rest)
f62194b2 717 $msginfo->{maxspamsize} = $pmg_cfg->get('spam', 'maxspamsize');
6f4aeba1
DM
718 if ($msginfo->{maxspamsize} <= 1024*64) {
719 $msginfo->{maxspamsize} = 1024*64;
720 }
721
722 if ($msginfo->{trusted}) {
f62194b2 723 my $hide = $pmg_cfg->get('mail', 'hide_received');
6f4aeba1
DM
724 $entity->head->delete("Received") if $hide;
725 }
726
f62194b2 727 $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap);
6f4aeba1 728 };
f62194b2 729
6f4aeba1
DM
730 my $err = $@;
731
732 $self->{errors} = $queue->{errors};
733
734 # restart on error
735 $self->{errors} = 1 if $err;
736
737 $queue->close ();
738
739 die $err if $err;
740
741 my ($csec_end, $usec_end) = gettimeofday ();
f62194b2 742 my $time_total =
6f4aeba1
DM
743 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
744
745# PHASE 5 - log statistics
746# on error: log error messages
747
748 eval {
749 my $dbh = $self->{ruledb}->{dbh};
750 my $where = "";
751 foreach my $rid (@$matching_rules) {
752 $where .= $where ? " OR ID = $rid" : "ID = $rid";
753 }
754 if ($where) {
755 $dbh->do ("UPDATE Rule " .
756 "SET Count = Count + 1 " .
757 "WHERE $where");
758 }
759
760 my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " .
761 "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" .
762 "$lcid, currval ('cstatistic_id_seq'), currval ('cstatistic_id_seq'),";
763
764 $insert_cmds .= $queue->{rtime} . ',';
765 $insert_cmds .= $queue->{bytes} . ',';
766 $insert_cmds .= $dbh->quote ($msginfo->{trusted} ? 0 : 1) . ',';
767 $insert_cmds .= ($queue->{sa_score} || 0) . ',';
768 $insert_cmds .= $dbh->quote ($queue->{vinfo}) . ',';
769 $insert_cmds .= $time_total . ',';
770 $insert_cmds .= $dbh->quote ($msginfo->{sender}) . ');';
771
772 foreach my $r (@{$msginfo->{targets}}) {
773 my $tmp = $dbh->quote ($r);
774 my $blocked = $queue->{status}->{$r} eq 'blocked' ? 1 : 0;
775 $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " .
776 "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); ";
777 }
778
779 $dbh->do ($insert_cmds);
780 };
f62194b2 781
6f4aeba1
DM
782 # save $err (because log clears $@)
783 $err = $@;
784
785 $time_total = $time_total/1000;
786
787 my $ptspam = ($queue->{ptime_spam} || 0)/1000;
6f4aeba1
DM
788 my $ptclam = ($queue->{ptime_clam} || 0)/1000;
789
521dca53 790 $self->log (3, "$queue->{logid}: processing time: ${time_total} seconds ($ptspam, $ptclam)");
6f4aeba1
DM
791
792 $msginfo->{test_fh}->close if $opt_testmode;
793
f62194b2 794 die $err if ($err);
6f4aeba1
DM
795}
796
797
798sub process_request {
799 my $self = shift;
800 my $prop = $self->{server};
801 my $sock = $prop->{client};
802
803 eval {
804
805 # make sure the ldap cache is up to date
806 $self->{ldap}->update (1);
807
f62194b2 808 $self->load_config() if $self->{reload_config};
6f4aeba1
DM
809
810 $self->{trusted} = 0;
811 if ($prop->{sockport} == $opt_int_port && !$opt_untrusted) {
812 $self->{trusted} = 1;
813 }
814
521dca53 815 my $smtp = PMG::SMTP->new ($sock);
6f4aeba1
DM
816
817 my $maxcount = $max_requests - $prop->{requests};
818
819 my $count = $smtp->loop (\&handle_smtp, $self, $maxcount);
820 if ($count > 1) {
821 $prop->{requests} += $count - 1;
822 }
823 };
f62194b2 824
6f4aeba1
DM
825 my $err = $@;
826
827 $self->log (0, $err) if $err;
828
24244199 829 kill(15, $prop->{ppid}) if $opt_testmode;
6f4aeba1 830
f62194b2 831 my $mem = PVE::ProcFSTools::read_memory_usage();
6f4aeba1
DM
832
833 if ($opt_testmode) {
834 $self->log (0, "memory usage: $mem->{size} bytes");
835 } else {
836 if ($self->{errors}) {
837 $self->log (0, "fast exit because of errors (free $mem->{size} bytes)");
838 $self->done (1);
839 } elsif ($mem->{size} > (300*1024*1024)) {
840 $self->log (0, "fast exit to reduce server load (free $mem->{size} bytes)");
841 $self->done (1);
842 }
843 }
844
845 $self->done (1) if $err;
846}
847
e646f7ca 848# test sig_hup with: for ((;;)) ;do kill -HUP `cat /var/run/${prog_name}.pid`; done;
6f4aeba1
DM
849# wrapper to avoid multiple calls to sig_hup
850sub sig_hup {
851 my $self = shift;
852 my $prop = $self->{server};
853
854 return if defined ($prop->{_HUP}); # do not call twice
855
856 $self->SUPER::sig_hup();
857}
858
859sub restart_close_hook {
860 my $self = shift;
861
862 my $sig_set = POSIX::SigSet->new;
f62194b2 863 $sig_set->addset (&POSIX::SIGHUP);
6f4aeba1
DM
864 $sig_set->addset (&POSIX::SIGCHLD); # to avoid zombies
865 my $old_sig_set = POSIX::SigSet->new();
866
867 sigprocmask (SIG_BLOCK, $sig_set, $old_sig_set);
868}
869
870sub pre_server_close_hook {
871 my $self = shift;
872 my $prop = $self->{server};
873
874 if (defined $prop->{_HUP}) {
f62194b2 875 undef $prop->{pid_file_unlink};
6f4aeba1
DM
876 }
877
878 if (defined $prop->{children}) {
879 foreach my $pid (keys %{$prop->{children}}) {
880 kill (1, $pid); # HUP childs
881 }
882 }
883
884 # nicely shutdown childs (give them max 30 seconds to shut down)
885 my $previous_alarm = alarm (30);
886 eval {
887 local $SIG{ALRM} = sub { die "Timed Out!\n" };
f62194b2 888
6f4aeba1
DM
889 my $pid;
890 1 while ((($pid = waitpid (-1, 0)) > 0) || ($! == EINTR));
891 };
892 alarm ($previous_alarm);
893}
894
895# initialize mime system before fork
896xdg_mime_get_mime_type_for_file ($0);
897
898my $server = bless {
899 server => $server_attr,
900};
901
902if (!$opt_testmode) {
903 $server->run ();
904} else {
905 if (fork) {
21853580 906 $server->run();
6f4aeba1 907 } else {
21853580 908
f62194b2
DM
909 my $sender ='sender@proxtest.com';
910 my $targets = ['target1@proxtest.com',
6f4aeba1
DM
911 'target2@proxtest.com',
912 'target3@proxtest.com'];
913
21853580
DM
914 my $smtp;
915 while (!$smtp) {
916 $smtp = Net::SMTP->new ('127.0.0.1', Port => 10023);
917 last if $smtp;
918 usleep(10);
919 }
6f4aeba1
DM
920
921 # syslog ('info', "connected to " . $smtp->domain);
922
923 $smtp->mail ($sender);
924 $smtp->to (@$targets);
925
926 $smtp->data();
927
f62194b2
DM
928 open (TMP, $opt_testmode) ||
929 die "unable to upen file '$opt_testmode' - $! :ERROR";
6f4aeba1
DM
930 while (<TMP>) {
931 $smtp->datasend ($_);
932 }
933 close TMP;
934
935 $smtp->datasend ("\n");
936 $smtp->dataend ();
937
938 $smtp->quit;
939 }
940}
941
942exit (0);
943
944__END__
945
946=head1 NAME
f62194b2
DM
947
948pmg-smtp-filter - the Proxmox mail filter
6f4aeba1
DM
949
950=head1 SYNOPSIS
951
f62194b2 952pmg-smtp-filter [-u] [-t testfile]
6f4aeba1
DM
953
954=head1 DESCRIPTION
955
956Documentation is available at www.proxmox.com