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