]>
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; | |
21853580 | 8 | use Time::HiRes qw (usleep 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; | |
9f67f5b3 | 31 | use PMG::ClusterConfig; |
6f4aeba1 | 32 | |
f62194b2 DM |
33 | use PMG::DBTools; |
34 | use PMG::RuleDB; | |
35 | use PMG::RuleCache; | |
36 | use PMG::ModGroup; | |
37 | use PMG::AtomicFile; | |
7b3c53bd | 38 | use PMG::LDAPConfig; |
f62194b2 DM |
39 | use PMG::LDAPSet; |
40 | use PMG::Config; | |
a43bcf61 | 41 | use PMG::MailQueue; |
91a5af02 | 42 | use PMG::Unpack; |
521dca53 | 43 | use PMG::SMTP; |
6f4aeba1 | 44 | |
521dca53 DM |
45 | use PMG::Unpack; |
46 | use PMG::Statistic; | |
f62194b2 DM |
47 | |
48 | use base qw(Net::Server::PreFork); | |
6f4aeba1 DM |
49 | |
50 | my $opt_commandline = [$0, @ARGV]; | |
51 | my $opt_max_dequeue = 1; | |
dff363d9 | 52 | my $opt_dequeue_time = 30; |
6f4aeba1 DM |
53 | |
54 | my $opt_ext_port = 10024; | |
55 | my $opt_int_port = 10023; | |
56 | my $opt_inject_port = 10025; | |
57 | ||
58 | my $opt_testmode; | |
59 | my $opt_untrusted; | |
60 | my $opt_pidfile; | |
61 | my $opt_database; | |
62 | ||
e646f7ca DM |
63 | my $prog_name = 'pmg-smtp-filter'; |
64 | ||
65 | initlog($prog_name, 'mail'); | |
6f4aeba1 DM |
66 | |
67 | if (!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 | ||
83e9f427 | 75 | $opt_pidfile = "/run/${prog_name}.pid" if !$opt_pidfile; |
6f4aeba1 DM |
76 | |
77 | my $max_servers = 1; | |
78 | my $min_servers = 1; | |
79 | my $min_spare_servers = 0; | |
80 | my $max_spare_servers = 0; | |
81 | my $max_requests = 1; | |
82 | ||
83 | if (!$opt_testmode) { | |
84 | ||
f62194b2 DM |
85 | my $pmg_cfg = PMG::Config->new(); |
86 | ||
ac5d1312 | 87 | my $demo = $pmg_cfg->get('admin', 'demo'); |
6f4aeba1 DM |
88 | |
89 | if ($demo) { | |
90 | syslog ('info', 'demo mode detected - not starting server'); | |
91 | exit (0); | |
92 | } | |
93 | ||
f62194b2 | 94 | $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2; |
6f4aeba1 DM |
95 | $min_servers = 2; |
96 | $min_spare_servers = 1; | |
97 | $max_spare_servers = 4; | |
98 | $max_requests = 20; | |
99 | } | |
100 | ||
101 | $opt_max_dequeue = 0 if $opt_testmode; | |
102 | ||
103 | my $daemonize = 1; | |
104 | if (defined ($ENV{BOUND_SOCKETS})) { | |
105 | $daemonize = undef; | |
106 | } | |
107 | ||
108 | my $server_attr = { | |
109 | port => [ $opt_int_port, $opt_ext_port ], | |
110 | host => '127.0.0.1', | |
111 | min_servers => $min_servers, | |
112 | max_servers => $max_servers, | |
113 | min_spare_servers => $min_spare_servers, | |
114 | max_spare_servers => $max_spare_servers, | |
115 | max_requests => $max_requests, | |
116 | serialize => 'flock', | |
117 | max_dequeue => $opt_max_dequeue, | |
118 | check_for_dequeue => $opt_dequeue_time, | |
119 | log_level => 3, | |
120 | pid_file => $opt_pidfile, | |
121 | no_close_by_child => 1, | |
122 | no_client_stdout => 1, | |
123 | commandline => $opt_commandline, | |
124 | }; | |
125 | ||
126 | $server_attr->{setsid} = $daemonize if !$opt_testmode; | |
127 | ||
128 | my $database; | |
129 | ||
130 | if (defined($opt_database)) { | |
131 | $database = $opt_database; | |
132 | } else { | |
133 | $database = $opt_testmode ? "Proxmox_testdb" : "Proxmox_ruledb"; | |
134 | } | |
135 | ||
136 | $SIG{'__WARN__'} = sub { | |
137 | my $err = $@; | |
138 | my $t = $_[0]; | |
139 | chomp $t; | |
140 | syslog('warning', "WARNING: %s", $t); | |
141 | $@ = $err; | |
142 | }; | |
143 | ||
144 | sub get_prox_vars { | |
145 | my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_; | |
146 | ||
147 | $spaminfo = { | |
148 | sa_score => $queue->{sa_score}, | |
149 | sa_hits => $queue->{sa_hits}, | |
150 | sa_data => $queue->{sa_data}, | |
151 | sa_max => $queue->{sa_max} | |
152 | } if !$spaminfo; | |
153 | ||
154 | my $vars = { | |
155 | 'SUBJECT' => $entity->head->get ('subject', 0) || 'No Subject', | |
156 | 'RULE' => $rule->{name}, | |
157 | 'RULE_INFO' => $msginfo->{rule_info}, | |
158 | 'SENDER' => $msginfo->{sender}, | |
159 | 'SENDER_IP' => $msginfo->{xforward}->{addr}, | |
160 | 'TARGETS' => join (', ', @$targets), | |
161 | 'RECEIVERS' => join (', ', @{$msginfo->{targets}}), | |
162 | 'SPAMLEVEL' => $spaminfo->{sa_score}, | |
163 | 'SPAMSTARS' => '*' x (($spaminfo->{sa_score} || 0) > 100 ? 100 : $spaminfo->{sa_score} || 0), | |
ac5d1312 | 164 | 'ADMIN' => $self->{pmg_cfg}->get('admin', 'email'), |
6f4aeba1 DM |
165 | 'HOST' => $msginfo->{hostname}, |
166 | 'DOMAIN' => $msginfo->{domain}, | |
167 | 'FQDN' => $msginfo->{fqdn}, | |
168 | 'MSGID' => $queue->{msgid}, | |
1c15ce0f | 169 | 'VERSION' => PMG::pmgcfg::package() . "/" . PMG::pmgcfg::release() . "/" . PMG::pmgcfg::repoid(), |
6f4aeba1 DM |
170 | }; |
171 | ||
172 | $vars->{__spaminfo} = $spaminfo; | |
173 | ||
174 | if ($opt_testmode) { | |
4c4fec6b | 175 | if ($queue->{vinfo_clam} || $queue->{vinfo_avast} || $queue->{vinfo_custom}) { |
6f4aeba1 DM |
176 | $vars->{'VIRUS_INFO'} = "Virus Info:"; |
177 | $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam}; | |
6ccbc37f | 178 | $vars->{'VIRUS_INFO'} .= " avast: $queue->{vinfo_avast}" if $queue->{vinfo_avast}; |
4c4fec6b | 179 | $vars->{'VIRUS_INFO'} .= " custom: $queue->{vinfo_custom}" if $queue->{vinfo_custom}; |
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 | ||
224 | sub 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, |
ecf08b56 | 356 | $msginfo, { RULE => 'default-accept' }, undef); |
6f4aeba1 DM |
357 | } |
358 | ||
359 | return $matching_rules; | |
360 | } | |
361 | ||
f62194b2 | 362 | # reload ruledb and pmg config |
6f4aeba1 DM |
363 | sub 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 | 374 | |
ab6aa17b DM |
375 | # create spool directories |
376 | PMG::MailQueue::create_spooldirs($self->{cinfo}->{local}->{cid}); | |
377 | ||
6f4aeba1 | 378 | eval { |
f62194b2 DM |
379 | my $dbh = PMG::DBTools::open_ruledb ($database); |
380 | $self->{ruledb} = PMG::RuleDB->new ($dbh); | |
6f4aeba1 DM |
381 | |
382 | # load rulecache | |
f62194b2 | 383 | $self->{rulecache} = PMG::RuleCache->new ($self->{ruledb}); |
6f4aeba1 DM |
384 | }; |
385 | ||
386 | my $err = $@; | |
387 | ||
388 | if ($err) { | |
389 | sleep (10); # reduce restart rate when postgres is down | |
390 | die $err; | |
391 | } | |
392 | ||
f62194b2 | 393 | # create LDAP object |
7b3c53bd DM |
394 | my $ldapconf = PVE::INotify::read_file('pmg-ldap.conf'); |
395 | $self->{ldap} = PMG::LDAPSet->new_from_ldap_cfg($ldapconf, 1); | |
6f4aeba1 | 396 | |
f62194b2 | 397 | $self->{reload_config} = 0; |
6f4aeba1 DM |
398 | } |
399 | ||
f62194b2 DM |
400 | my $syslog_map = { |
401 | 0 => 'err', | |
402 | 1 => 'warning', | |
403 | 2 => 'notice', | |
404 | 3 => 'info', | |
405 | 4 => 'debug' | |
406 | }; | |
407 | ||
6f4aeba1 DM |
408 | sub log { |
409 | my ($self, $level, $msg, @therest) = @_; | |
410 | my $prop = $self->{server}; | |
411 | ||
412 | return if $level =~ /^\d+$/ && $level > $prop->{log_level}; | |
413 | ||
f62194b2 | 414 | $level = $syslog_map->{$level} || $level; |
6f4aeba1 DM |
415 | if (@therest) { |
416 | syslog($level, $msg, @therest); | |
417 | } else { | |
418 | syslog ($level, $msg); | |
419 | } | |
420 | } | |
421 | ||
422 | sub pre_loop_hook { | |
423 | my $self = shift; | |
424 | my $prop = $self->{server}; | |
425 | ||
426 | $prop->{log_level} = 3; | |
427 | ||
428 | $self->log (0, "Filter daemon (re)started (max. $max_servers processes)"); | |
429 | ||
521dca53 | 430 | eval { PMG::MailQueue::cleanup_active(); }; |
6f4aeba1 DM |
431 | $self->log (0, "Cleanup failures: $@") if $@; |
432 | ||
433 | my $sig_set = POSIX::SigSet->new; | |
f62194b2 DM |
434 | $sig_set->addset (&POSIX::SIGHUP); |
435 | $sig_set->addset (&POSIX::SIGCHLD); | |
6f4aeba1 DM |
436 | my $old_sig_set = POSIX::SigSet->new(); |
437 | ||
438 | sigprocmask (SIG_UNBLOCK, $sig_set, $old_sig_set); | |
439 | ||
440 | my ($backup_umask) = umask; | |
441 | ||
f62194b2 | 442 | my $pmg_cfg = PMG::Config->new(); |
e5b31a61 | 443 | $pmg_cfg->write_smtp_filter_config(); |
f62194b2 DM |
444 | |
445 | # Note: you need to restart the daemon when you change 'rbl_checks' | |
446 | my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks'); | |
6f4aeba1 DM |
447 | |
448 | $self->{sa} = Mail::SpamAssassin->new ({ | |
449 | debug => 0, | |
f62194b2 | 450 | local_tests_only => $opt_testmode || !$rbl_checks, |
6f4aeba1 DM |
451 | home_dir_for_helpers => '/root', |
452 | userstate_dir => '/root/.spamassassin', | |
453 | dont_copy_prefs => 1, | |
454 | stop_at_threshold => 0, | |
455 | }); | |
456 | ||
457 | $self->{sa}->compile_now; | |
458 | ||
459 | alarm (0); # SA forgets to clear alarm in some cases | |
460 | umask ($backup_umask); | |
e646f7ca | 461 | initlog ($prog_name, 'mail'); |
6f4aeba1 DM |
462 | |
463 | $SIG{'USR1'} = sub { | |
464 | # reloading server configuration | |
465 | if (defined $prop->{children}) { | |
466 | foreach my $pid (keys %{$prop->{children}}) { | |
467 | kill (10, $pid); # SIGUSR1 childs | |
468 | } | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | sub child_init_hook { | |
474 | my $self = shift; | |
475 | ||
e646f7ca | 476 | $0 = "$prog_name child"; |
6f4aeba1 DM |
477 | |
478 | # $self->log (3, "init child"); | |
479 | ||
480 | eval { | |
481 | $self->load_config (); | |
482 | }; | |
483 | ||
484 | if ($@) { | |
485 | $self->log (0, $@); | |
486 | $self->child_finish_hook; | |
487 | exit; | |
488 | } | |
489 | ||
490 | $SIG{'USR1'} = sub { | |
f62194b2 | 491 | $self->{reload_config} = 1; |
6f4aeba1 DM |
492 | } |
493 | } | |
494 | ||
495 | sub child_finish_hook { | |
496 | my $self = shift; | |
f62194b2 | 497 | |
6f4aeba1 DM |
498 | # $self->log (3, "finish child"); |
499 | $self->{ruledb}->close () if $self->{ruledb}; | |
500 | } | |
501 | ||
dff363d9 DM |
502 | my $last_dequeue_time = 0; |
503 | ||
6f4aeba1 DM |
504 | sub run_dequeue { |
505 | my $self = shift; | |
506 | ||
dff363d9 DM |
507 | my $err; |
508 | ||
6f4aeba1 DM |
509 | # do database maintainance here |
510 | ||
de199535 | 511 | # this is called every 30 secends |
dff363d9 DM |
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 | ||
de199535 DM |
520 | my $ctime = time(); |
521 | my $tdiff = $ctime - $last_dequeue_time; | |
522 | ||
523 | # return if tdiff less than 2 minutes | |
dff363d9 DM |
524 | return if $tdiff < 2*60; |
525 | ||
de199535 DM |
526 | $last_dequeue_time = $ctime; |
527 | ||
6f4aeba1 DM |
528 | $self->log (2, "starting database maintainance"); |
529 | ||
530 | my ($csec, $usec) = gettimeofday (); | |
531 | ||
f62194b2 DM |
532 | my $cinfo = PVE::INotify::read_file("cluster.conf"); |
533 | ||
6f4aeba1 | 534 | my $dbh; |
f62194b2 | 535 | |
6f4aeba1 | 536 | eval { |
f62194b2 | 537 | $dbh = PMG::DBTools::open_ruledb($database); |
6f4aeba1 | 538 | }; |
dff363d9 | 539 | if ($err = $@) { |
b902c0b8 | 540 | $self->log (0, "ERROR: $err"); |
6f4aeba1 DM |
541 | return; |
542 | } | |
543 | ||
544 | eval { | |
521dca53 | 545 | PMG::Statistic::update_stats($dbh, $cinfo); |
6f4aeba1 | 546 | }; |
6f4aeba1 DM |
547 | $err = $@; |
548 | ||
549 | my ($csec_end, $usec_end) = gettimeofday (); | |
550 | my $ptime = int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000); | |
551 | ||
552 | if ($err) { | |
b902c0b8 | 553 | $self->log (0, $err); |
6f4aeba1 DM |
554 | } else { |
555 | $self->log (2, "end database maintainance ($ptime ms)"); | |
556 | } | |
557 | ||
558 | $dbh->disconnect() if $dbh; | |
559 | } | |
560 | ||
561 | ||
562 | sub unpack_entity { | |
563 | my ($self, $unpack, $entity, $msginfo, $queue) = @_; | |
564 | ||
565 | my $magic; | |
566 | my $path; | |
567 | ||
f62194b2 | 568 | if (($magic = $entity->{PMX_magic_ct}) && |
6f4aeba1 DM |
569 | ($path = $entity->{PMX_decoded_path})) { |
570 | ||
571 | my $filename = basename ($path); | |
572 | ||
521dca53 | 573 | if (PMG::Unpack::is_archive ($magic)) { |
6f4aeba1 DM |
574 | $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)"); |
575 | ||
576 | my $start = [gettimeofday]; | |
577 | ||
578 | $unpack->{mime} = {}; | |
579 | ||
f62194b2 DM |
580 | eval { |
581 | $unpack->unpack_archive ($path, $magic); | |
6f4aeba1 DM |
582 | }; |
583 | ||
584 | $self->log (3, "$queue->{logid}: unpack failed - $@") if $@; | |
585 | ||
586 | $entity->{PMX_content_types} = $unpack->{mime}; | |
f62194b2 | 587 | |
6f4aeba1 DM |
588 | if ($opt_testmode) { |
589 | my $types = join (", ", sort keys (%{$entity->{PMX_content_types}})); | |
590 | my $fh = $msginfo->{test_fh}; | |
591 | $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/; | |
592 | print $fh "Types:$filename: $types\n" if $types; | |
593 | } | |
594 | ||
595 | my $elapsed = int(tv_interval ($start) * 1000); | |
596 | ||
597 | $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)"); | |
598 | } | |
599 | } | |
600 | ||
601 | foreach my $part ($entity->parts) { | |
602 | $self->unpack_entity ($unpack, $part, $msginfo, $queue); | |
603 | } | |
604 | ||
605 | } | |
606 | ||
607 | sub handle_smtp { | |
608 | my ($self, $smtp) = @_; | |
609 | ||
610 | my ($csec, $usec) = gettimeofday (); | |
611 | ||
612 | my $queue; | |
613 | my $msginfo = {}; | |
f62194b2 | 614 | my $pmg_cfg = $self->{pmg_cfg}; |
6f4aeba1 DM |
615 | my $ldap = $self->{ldap}; |
616 | my $cinfo = $self->{cinfo}; | |
617 | my $lcid = $cinfo->{local}->{cid}; | |
f62194b2 DM |
618 | |
619 | $msginfo->{test_fh} = PMG::AtomicFile->new("testresult.out", "w") | |
6f4aeba1 DM |
620 | if $opt_testmode; |
621 | ||
622 | $msginfo->{trusted} = $self->{trusted}; | |
623 | ||
624 | ||
625 | # PHASE 1 - save incoming mail (already done) | |
626 | # on error: exit | |
627 | ||
628 | $queue = $smtp->{queue}; | |
629 | $queue->{sa} = $self->{sa}; | |
cda67dee DM |
630 | $queue->{clamav_heuristic_score} = |
631 | $opt_testmode ? 100 : $pmg_cfg->get('spam', 'clamav_heuristic_score'); | |
6f4aeba1 | 632 | |
f62194b2 | 633 | $queue->{lic_valid} = 1; |
6f4aeba1 DM |
634 | |
635 | my $matching_rules; | |
636 | ||
637 | eval { | |
638 | $msginfo->{testmode} = $opt_testmode; | |
639 | $msginfo->{sender} = $smtp->{from}; | |
640 | $msginfo->{xforward} = $smtp->{xforward}; | |
641 | $msginfo->{targets} = $smtp->{to}; | |
642 | ||
f62194b2 DM |
643 | $msginfo->{hostname} = PVE::INotify::nodename(); |
644 | my $resolv = PVE::INotify::read_file('resolvconf'); | |
645 | ||
646 | $msginfo->{domain} = $resolv->{search}; | |
da14f4e1 ML |
647 | $msginfo->{fqdn} = $msginfo->{hostname}; |
648 | $msginfo->{fqdn} .= ".$msginfo->{domain}" if $msginfo->{domain}; | |
6f4aeba1 | 649 | $msginfo->{lcid} = $lcid; |
f62194b2 | 650 | |
6f4aeba1 DM |
651 | # $msginfo->{targets} is case sensitive, |
652 | # but pmail is always lower case! | |
653 | ||
654 | foreach my $t (@{$msginfo->{targets}}) { | |
655 | my $res; | |
656 | if ($ldap && ($res = $ldap->account_info ($t))) { | |
657 | $msginfo->{pmail}->{$t} = $res->{pmail}; | |
658 | } else { | |
659 | $msginfo->{pmail}->{$t} = lc ($t); | |
660 | } | |
661 | } | |
662 | ||
663 | # PHASE 2 - parse mail | |
664 | # on error: exit | |
6f4aeba1 | 665 | |
f62194b2 DM |
666 | my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles'); |
667 | ||
668 | my $entity = $queue->parse_mail($maxfiles); | |
6f4aeba1 DM |
669 | |
670 | $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid}); | |
671 | ||
672 | # PHASE 3 - run external content analyzers | |
673 | # (SPAM analyzer is run on demand later) | |
674 | # on error: log error messages | |
675 | ||
4c4fec6b SI |
676 | # run custom script first |
677 | my ($vinfo, $custom_spaminfo) = PMG::Utils::analyze_custom_check( | |
f62194b2 | 678 | $queue, $queue->{dataname}, $pmg_cfg, $opt_testmode); |
6f4aeba1 | 679 | |
4c4fec6b SI |
680 | # test for virus if none found |
681 | if (!defined($vinfo)) { | |
682 | $vinfo = PMG::Utils::analyze_virus( | |
683 | $queue, $queue->{dataname}, $pmg_cfg, $opt_testmode); | |
0d5209bf | 684 | |
4c4fec6b SI |
685 | if ($vinfo && $vinfo =~ m/^Heuristics\.(.+)$/) { |
686 | my $hit = $1; | |
687 | $queue->{clamav_heuristic} = $hit; | |
688 | $vinfo = undef; | |
689 | } | |
690 | } | |
0d5209bf DM |
691 | $queue->{vinfo} = $vinfo; |
692 | ||
6f4aeba1 DM |
693 | # always add this headers to incoming mails |
694 | # to enable user to report false negatives | |
695 | if (!$msginfo->{trusted}) { | |
6f4aeba1 DM |
696 | if ($queue->{vinfo}) { |
697 | $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo}); | |
698 | } | |
6f4aeba1 DM |
699 | } |
700 | ||
701 | # we unpack after virus scanning, because this is more secure. | |
702 | # This way virus scanners gets the whole mail files and are able | |
703 | # to detect phishing signature for example - which would not work | |
704 | # if we decompose first and only analyze the decomposed attachments. | |
f62194b2 | 705 | # Disadvantage is that we need to unpack more than |
6f4aeba1 | 706 | # once (bad performance). |
f62194b2 | 707 | |
6f4aeba1 DM |
708 | # should we scan content types inside archives |
709 | ||
6f4aeba1 DM |
710 | my $rulecache = $self->{rulecache}; |
711 | ||
712 | my $scan_archives = 0; | |
713 | ||
714 | if (($rulecache->{archivefilter_in} && !$msginfo->{trusted}) || | |
715 | ($rulecache->{archivefilter_out} && $msginfo->{trusted})) { | |
716 | $scan_archives = 1; | |
717 | } | |
718 | ||
719 | if ($scan_archives && !$queue->{vinfo}) { | |
720 | ||
721 | # unpack all archives - determine contained content types | |
722 | ||
723 | my $decdir = $queue->{dumpdir} . "/__decoded_archives"; | |
724 | mkdir $decdir; | |
725 | ||
726 | my $start = [gettimeofday]; | |
f62194b2 | 727 | |
6f4aeba1 DM |
728 | my $unpack; |
729 | eval { | |
730 | ||
731 | # limits: We use clamav limit for maxfiles, and scan | |
732 | # only 4 levels, timeout of 30 seconds | |
733 | ||
521dca53 DM |
734 | $unpack = PMG::Unpack->new ( |
735 | tmpdir => $decdir, | |
736 | timeout => 30, | |
737 | ctonly => 1, # only detect CTs | |
738 | maxrec => -4, | |
739 | maxfiles => $maxfiles); | |
6f4aeba1 | 740 | |
f62194b2 | 741 | $self->unpack_entity ($unpack, $entity, $msginfo, $queue); |
6f4aeba1 DM |
742 | }; |
743 | ||
744 | my $err = $@; | |
745 | ||
746 | $unpack->cleanup() if $unpack; | |
747 | ||
748 | my $elapsed = int(tv_interval ($start) * 1000); | |
749 | ||
750 | if ($err) { | |
751 | $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err"); | |
752 | } | |
753 | } | |
754 | ||
755 | # PHASE 4 - apply rules | |
756 | # on error: exit (cleanup process should do the rest) | |
f62194b2 | 757 | $msginfo->{maxspamsize} = $pmg_cfg->get('spam', 'maxspamsize'); |
6f4aeba1 DM |
758 | if ($msginfo->{maxspamsize} <= 1024*64) { |
759 | $msginfo->{maxspamsize} = 1024*64; | |
760 | } | |
761 | ||
762 | if ($msginfo->{trusted}) { | |
f62194b2 | 763 | my $hide = $pmg_cfg->get('mail', 'hide_received'); |
6f4aeba1 DM |
764 | $entity->head->delete("Received") if $hide; |
765 | } | |
766 | ||
f62194b2 | 767 | $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap); |
6f4aeba1 | 768 | }; |
f62194b2 | 769 | |
6f4aeba1 DM |
770 | my $err = $@; |
771 | ||
772 | $self->{errors} = $queue->{errors}; | |
773 | ||
774 | # restart on error | |
775 | $self->{errors} = 1 if $err; | |
776 | ||
777 | $queue->close (); | |
778 | ||
779 | die $err if $err; | |
780 | ||
781 | my ($csec_end, $usec_end) = gettimeofday (); | |
f62194b2 | 782 | my $time_total = |
6f4aeba1 DM |
783 | int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000); |
784 | ||
785 | # PHASE 5 - log statistics | |
786 | # on error: log error messages | |
787 | ||
788 | eval { | |
789 | my $dbh = $self->{ruledb}->{dbh}; | |
2579281e | 790 | |
6f4aeba1 DM |
791 | my $where = ""; |
792 | foreach my $rid (@$matching_rules) { | |
793 | $where .= $where ? " OR ID = $rid" : "ID = $rid"; | |
794 | } | |
2579281e DM |
795 | |
796 | $dbh->do ("UPDATE Rule SET Count = Count + 1 WHERE $where") if $where; | |
6f4aeba1 DM |
797 | |
798 | my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " . | |
799 | "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" . | |
2579281e | 800 | "$lcid, currval('cstatistic_id_seq'), currval('cstatistic_id_seq'),"; |
6f4aeba1 DM |
801 | |
802 | $insert_cmds .= $queue->{rtime} . ','; | |
803 | $insert_cmds .= $queue->{bytes} . ','; | |
2579281e | 804 | $insert_cmds .= $dbh->quote($msginfo->{trusted} ? 0 : 1) . ','; |
6f4aeba1 | 805 | $insert_cmds .= ($queue->{sa_score} || 0) . ','; |
2579281e | 806 | $insert_cmds .= $dbh->quote($queue->{vinfo}) . ','; |
6f4aeba1 | 807 | $insert_cmds .= $time_total . ','; |
2579281e | 808 | $insert_cmds .= $dbh->quote($msginfo->{sender}) . ');'; |
6f4aeba1 DM |
809 | |
810 | foreach my $r (@{$msginfo->{targets}}) { | |
2579281e | 811 | my $tmp = $dbh->quote($r); |
6f4aeba1 DM |
812 | my $blocked = $queue->{status}->{$r} eq 'blocked' ? 1 : 0; |
813 | $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " . | |
814 | "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); "; | |
815 | } | |
816 | ||
2579281e | 817 | $dbh->do($insert_cmds); |
6f4aeba1 | 818 | }; |
6f4aeba1 DM |
819 | # save $err (because log clears $@) |
820 | $err = $@; | |
821 | ||
822 | $time_total = $time_total/1000; | |
823 | ||
824 | my $ptspam = ($queue->{ptime_spam} || 0)/1000; | |
6f4aeba1 | 825 | my $ptclam = ($queue->{ptime_clam} || 0)/1000; |
4c4fec6b | 826 | my $ptcustom = ($queue->{ptime_custom} || 0)/1000; |
6f4aeba1 | 827 | |
4c4fec6b | 828 | $self->log(3, "$queue->{logid}: processing time: ${time_total} seconds ($ptspam, $ptclam, $ptcustom)"); |
6f4aeba1 DM |
829 | |
830 | $msginfo->{test_fh}->close if $opt_testmode; | |
831 | ||
2579281e | 832 | die $err if $err; |
6f4aeba1 DM |
833 | } |
834 | ||
ab6aa17b | 835 | my $initial_memory_usage; |
6f4aeba1 DM |
836 | |
837 | sub process_request { | |
838 | my $self = shift; | |
839 | my $prop = $self->{server}; | |
840 | my $sock = $prop->{client}; | |
841 | ||
842 | eval { | |
843 | ||
844 | # make sure the ldap cache is up to date | |
845 | $self->{ldap}->update (1); | |
846 | ||
f62194b2 | 847 | $self->load_config() if $self->{reload_config}; |
6f4aeba1 DM |
848 | |
849 | $self->{trusted} = 0; | |
850 | if ($prop->{sockport} == $opt_int_port && !$opt_untrusted) { | |
851 | $self->{trusted} = 1; | |
852 | } | |
853 | ||
521dca53 | 854 | my $smtp = PMG::SMTP->new ($sock); |
6f4aeba1 DM |
855 | |
856 | my $maxcount = $max_requests - $prop->{requests}; | |
857 | ||
858 | my $count = $smtp->loop (\&handle_smtp, $self, $maxcount); | |
859 | if ($count > 1) { | |
860 | $prop->{requests} += $count - 1; | |
861 | } | |
862 | }; | |
f62194b2 | 863 | |
6f4aeba1 DM |
864 | my $err = $@; |
865 | ||
866 | $self->log (0, $err) if $err; | |
867 | ||
24244199 | 868 | kill(15, $prop->{ppid}) if $opt_testmode; |
6f4aeba1 | 869 | |
f62194b2 | 870 | my $mem = PVE::ProcFSTools::read_memory_usage(); |
ab6aa17b DM |
871 | if (!defined($initial_memory_usage) || ($prop->{requests} < 10)) { |
872 | $initial_memory_usage = $mem->{resident}; | |
873 | } | |
6f4aeba1 DM |
874 | |
875 | if ($opt_testmode) { | |
876 | $self->log (0, "memory usage: $mem->{size} bytes"); | |
877 | } else { | |
878 | if ($self->{errors}) { | |
879 | $self->log (0, "fast exit because of errors (free $mem->{size} bytes)"); | |
880 | $self->done (1); | |
ab6aa17b DM |
881 | } else { |
882 | my $diff = $mem->{resident} - $initial_memory_usage; | |
883 | if ($diff > 5*1024*1024) { | |
884 | $self->log (0, "fast exit to reduce server load (free $diff bytes)"); | |
885 | $self->done (1); | |
886 | } | |
887 | } | |
6f4aeba1 DM |
888 | } |
889 | ||
890 | $self->done (1) if $err; | |
891 | } | |
892 | ||
83e9f427 | 893 | # test sig_hup with: for ((;;)) ;do kill -HUP `cat /run/${prog_name}.pid`; done; |
6f4aeba1 DM |
894 | # wrapper to avoid multiple calls to sig_hup |
895 | sub sig_hup { | |
896 | my $self = shift; | |
897 | my $prop = $self->{server}; | |
898 | ||
899 | return if defined ($prop->{_HUP}); # do not call twice | |
900 | ||
901 | $self->SUPER::sig_hup(); | |
902 | } | |
903 | ||
904 | sub restart_close_hook { | |
905 | my $self = shift; | |
906 | ||
907 | my $sig_set = POSIX::SigSet->new; | |
f62194b2 | 908 | $sig_set->addset (&POSIX::SIGHUP); |
6f4aeba1 DM |
909 | $sig_set->addset (&POSIX::SIGCHLD); # to avoid zombies |
910 | my $old_sig_set = POSIX::SigSet->new(); | |
911 | ||
912 | sigprocmask (SIG_BLOCK, $sig_set, $old_sig_set); | |
913 | } | |
914 | ||
915 | sub pre_server_close_hook { | |
916 | my $self = shift; | |
917 | my $prop = $self->{server}; | |
918 | ||
919 | if (defined $prop->{_HUP}) { | |
f62194b2 | 920 | undef $prop->{pid_file_unlink}; |
6f4aeba1 DM |
921 | } |
922 | ||
923 | if (defined $prop->{children}) { | |
924 | foreach my $pid (keys %{$prop->{children}}) { | |
925 | kill (1, $pid); # HUP childs | |
926 | } | |
927 | } | |
928 | ||
929 | # nicely shutdown childs (give them max 30 seconds to shut down) | |
08f49442 | 930 | my $previous_alarm = alarm(30); |
6f4aeba1 DM |
931 | eval { |
932 | local $SIG{ALRM} = sub { die "Timed Out!\n" }; | |
f62194b2 | 933 | |
6f4aeba1 DM |
934 | my $pid; |
935 | 1 while ((($pid = waitpid (-1, 0)) > 0) || ($! == EINTR)); | |
08f49442 DM |
936 | |
937 | alarm(0); # avoid race | |
6f4aeba1 DM |
938 | }; |
939 | alarm ($previous_alarm); | |
940 | } | |
941 | ||
942 | # initialize mime system before fork | |
943 | xdg_mime_get_mime_type_for_file ($0); | |
944 | ||
945 | my $server = bless { | |
946 | server => $server_attr, | |
947 | }; | |
948 | ||
949 | if (!$opt_testmode) { | |
950 | $server->run (); | |
951 | } else { | |
952 | if (fork) { | |
21853580 | 953 | $server->run(); |
6f4aeba1 | 954 | } else { |
21853580 | 955 | |
f62194b2 DM |
956 | my $sender ='sender@proxtest.com'; |
957 | my $targets = ['target1@proxtest.com', | |
6f4aeba1 DM |
958 | 'target2@proxtest.com', |
959 | 'target3@proxtest.com']; | |
960 | ||
21853580 DM |
961 | my $smtp; |
962 | while (!$smtp) { | |
963 | $smtp = Net::SMTP->new ('127.0.0.1', Port => 10023); | |
964 | last if $smtp; | |
965 | usleep(10); | |
966 | } | |
6f4aeba1 DM |
967 | |
968 | # syslog ('info', "connected to " . $smtp->domain); | |
969 | ||
970 | $smtp->mail ($sender); | |
971 | $smtp->to (@$targets); | |
972 | ||
973 | $smtp->data(); | |
974 | ||
f62194b2 DM |
975 | open (TMP, $opt_testmode) || |
976 | die "unable to upen file '$opt_testmode' - $! :ERROR"; | |
6f4aeba1 DM |
977 | while (<TMP>) { |
978 | $smtp->datasend ($_); | |
979 | } | |
980 | close TMP; | |
981 | ||
982 | $smtp->datasend ("\n"); | |
983 | $smtp->dataend (); | |
984 | ||
985 | $smtp->quit; | |
986 | } | |
987 | } | |
988 | ||
989 | exit (0); | |
990 | ||
991 | __END__ | |
992 | ||
993 | =head1 NAME | |
f62194b2 DM |
994 | |
995 | pmg-smtp-filter - the Proxmox mail filter | |
6f4aeba1 DM |
996 | |
997 | =head1 SYNOPSIS | |
998 | ||
f62194b2 | 999 | pmg-smtp-filter [-u] [-t testfile] |
6f4aeba1 DM |
1000 | |
1001 | =head1 DESCRIPTION | |
1002 | ||
1003 | Documentation is available at www.proxmox.com |