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