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