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