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