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