]>
Commit | Line | Data |
---|---|---|
3164123a | 1 | #!/usr/bin/perl |
ddaec666 DM |
2 | |
3 | use strict; | |
3164123a | 4 | use warnings; |
ddaec666 | 5 | use Getopt::Long; |
ddaec666 DM |
6 | use POSIX qw(errno_h signal_h); |
7 | ||
8 | use Net::Server::PreForkSimple; | |
9 | use Net::DNS::Resolver; | |
10 | use Mail::SPF; | |
11 | use Fcntl; | |
12 | use Fcntl ':flock'; | |
13 | use IO::Multiplex; | |
3164123a | 14 | use Time::HiRes qw(gettimeofday); |
ddaec666 | 15 | |
3164123a DM |
16 | use PVE::INotify; |
17 | use PVE::Tools; | |
18 | use PVE::SafeSyslog; | |
ddaec666 | 19 | |
3164123a DM |
20 | use PMG::Utils; |
21 | use PMG::RuleDB; | |
22 | use PMG::DBTools; | |
23 | use PMG::RuleCache; | |
24 | use PMG::Config; | |
25 | use PMG::ClusterConfig; | |
26 | ||
27 | use base qw(Net::Server::PreForkSimple); | |
ddaec666 DM |
28 | |
29 | my $greylist_delay = 3*60; # greylist window | |
30 | my $greylist_lifetime = 3600*24*2; # retry window | |
31 | my $greylist_awlifetime = 3600*24*36; # expire window | |
32 | ||
33 | my $opt_commandline = [$0, @ARGV]; | |
34 | my $opt_policy_port = 10022; | |
35 | my $opt_max_dequeue = 1; | |
36 | my $opt_dequeue_time = 60*2; | |
37 | ||
38 | my $opt_testmode; | |
39 | my $opt_pidfile; | |
40 | my $opt_database; | |
41 | ||
3164123a DM |
42 | if (!GetOptions ('pidfile=s' => \$opt_pidfile, |
43 | 'testmode' => \$opt_testmode, | |
ddaec666 DM |
44 | 'database=s' => \$opt_database)) { |
45 | die "usage error\n"; | |
46 | exit (-1); | |
47 | } | |
48 | ||
3164123a | 49 | $opt_pidfile = "/var/run/pmgpolicy.pid" if !$opt_pidfile; |
ddaec666 DM |
50 | $opt_max_dequeue = 0 if $opt_testmode; |
51 | ||
3164123a DM |
52 | initlog('pmgpolicy', 'mail'); |
53 | ||
54 | my $max_servers = 5; | |
ddaec666 DM |
55 | |
56 | if (!$opt_testmode) { | |
57 | ||
3164123a DM |
58 | my $pmg_cfg = PMG::Config->new (); |
59 | my $demo = $pmg_cfg->get('admin', 'demo'); | |
60 | $max_servers = $pmg_cfg->get('mail', 'max_policy'); | |
ddaec666 DM |
61 | |
62 | if ($demo) { | |
3164123a DM |
63 | syslog('info', 'demo mode detected - not starting server'); |
64 | exit(0); | |
ddaec666 DM |
65 | } |
66 | } | |
67 | ||
68 | my $daemonize = 1; | |
69 | if (defined ($ENV{BOUND_SOCKETS})) { | |
70 | $daemonize = undef; | |
71 | } | |
72 | ||
ddaec666 DM |
73 | |
74 | my $server_attr = { | |
75 | port => [ $opt_policy_port ], | |
76 | host => '127.0.0.1', | |
77 | max_servers => $max_servers, | |
78 | max_dequeue => $opt_max_dequeue, | |
79 | check_for_dequeue => $opt_dequeue_time, | |
80 | log_level => 3, | |
81 | pid_file => $opt_pidfile, | |
82 | commandline => $opt_commandline, | |
83 | no_close_by_child => 1, | |
84 | setsid => $daemonize, | |
85 | }; | |
86 | ||
87 | my $database; | |
88 | if (defined($opt_database)) { | |
89 | $database = $opt_database; | |
90 | } else { | |
91 | $database = "Proxmox_ruledb"; | |
92 | } | |
93 | ||
94 | $SIG{'__WARN__'} = sub { | |
95 | my $err = $@; | |
96 | my $t = $_[0]; | |
97 | chomp $t; | |
98 | syslog('warning', "WARNING: %s", $t); | |
99 | $@ = $err; | |
100 | }; | |
101 | ||
102 | sub run_dequeue { | |
103 | my $self = shift; | |
104 | ||
3164123a | 105 | $self->log(2, "starting greylist database maintainance"); |
ddaec666 DM |
106 | |
107 | my ($csec, $usec) = gettimeofday (); | |
108 | ||
3164123a | 109 | my $cinfo = PVE::INotify::read_file("cluster.conf"); |
ddaec666 | 110 | my $lcid = $cinfo->{local}->{cid}; |
3164123a DM |
111 | my $role = $cinfo->{local}->{type} // '-'; |
112 | ||
ddaec666 | 113 | my $dbh; |
3164123a | 114 | |
ddaec666 | 115 | eval { |
3164123a | 116 | $dbh = PMG::DBTools::open_ruledb($database); |
ddaec666 DM |
117 | }; |
118 | my $err = $@; | |
119 | ||
120 | if ($err) { | |
b902c0b8 | 121 | $self->log(0, "ERROR: $err"); |
ddaec666 DM |
122 | return; |
123 | } | |
124 | ||
3164123a | 125 | my $now = time(); |
ddaec666 DM |
126 | |
127 | my $ecount = 0; | |
128 | ||
ddaec666 DM |
129 | eval { |
130 | ||
131 | $dbh->begin_work; | |
132 | ||
133 | # we do not lock the table here to avoid delays | |
134 | # but that is OK, because we only touch expired records | |
135 | # which do not change nornmally | |
136 | ## $dbh->do ("LOCK TABLE CGreylist IN ROW EXCLUSIVE MODE"); | |
137 | ||
138 | # move expired and undelivered records from Greylist to Statistic | |
3164123a | 139 | |
ddaec666 | 140 | my $rntxt = ''; |
3164123a | 141 | if (!$lcid) { |
ddaec666 DM |
142 | $rntxt = "AND CID = 0"; |
143 | } else { | |
3164123a | 144 | if ($role eq 'master') { |
ddaec666 DM |
145 | # master is responsible for all non-cluster (deleted) nodes |
146 | foreach my $rcid (@{$cinfo->{remnodes}}) { | |
147 | $rntxt .= $rntxt ? " AND CID != $rcid" : "AND (CID != $rcid"; | |
148 | } | |
149 | $rntxt .= ")" if $rntxt; | |
150 | } else { | |
151 | $rntxt = "AND (CID = 0 OR CID = $lcid)"; | |
152 | } | |
153 | } | |
154 | ||
155 | ||
156 | my $cmds = ''; | |
157 | ||
3164123a DM |
158 | my $sth = $dbh->prepare( |
159 | "SELECT distinct instance, sender FROM CGreylist " . | |
160 | "WHERE passed = 0 AND extime < ? $rntxt"); | |
ddaec666 DM |
161 | |
162 | $sth->execute ($now); | |
163 | ||
164 | ||
165 | while (my $ref = $sth->fetchrow_hashref()) { | |
3164123a DM |
166 | my $sth2 = $dbh->prepare( |
167 | "SELECT * FROM CGreylist WHERE instance = ? AND sender = ?"); | |
ddaec666 DM |
168 | $sth2->execute ($ref->{instance}, $ref->{sender}); |
169 | my $rctime; | |
170 | my @rcvrs; | |
171 | my $bc = 0; | |
172 | ||
173 | while (my $ref2 = $sth2->fetchrow_hashref()) { | |
174 | $rctime = $ref2->{rctime} if !$rctime; | |
175 | $bc += $ref2->{blocked}; | |
176 | push @rcvrs, $ref2->{receiver}; | |
177 | } | |
178 | ||
179 | $sth2->finish(); | |
180 | ||
181 | # hack: sometimes query sth2 does not return anything - maybe a | |
182 | # postgres bug? We simply ignore (when rctime is undefined) it | |
183 | # to avoid problems. | |
184 | ||
185 | if ($rctime) { | |
3164123a DM |
186 | $cmds .= "SELECT nextval ('cstatistic_id_seq');" . |
187 | "INSERT INTO CStatistic " . | |
ddaec666 DM |
188 | "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" . |
189 | "$lcid, currval ('cstatistic_id_seq'), currval ('cstatistic_id_seq'), "; | |
190 | ||
191 | my $sl = $bc >= 100000 ? 4 : 5; | |
192 | $cmds .= $rctime . ", 0, '1', $sl, NULL, 0, "; | |
193 | $cmds .= $dbh->quote ($ref->{sender}) . ');'; | |
194 | ||
195 | foreach my $r (@rcvrs) { | |
196 | my $tmp = $dbh->quote ($r); | |
197 | $cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) ". | |
198 | "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '1'); "; | |
199 | } | |
200 | } | |
201 | ||
202 | if (length ($cmds) > 100000) { | |
203 | $dbh->do ($cmds); | |
204 | $cmds = ''; | |
205 | } | |
206 | ||
ddaec666 DM |
207 | $ecount++; |
208 | ||
209 | # this produces too much log traffic | |
210 | # my $targets = join (", ", @rcvrs); | |
211 | #my $msg = "expire mail $ref->{instance} from $ref->{sender} to $targets"; | |
b902c0b8 | 212 | #$self->log (0, $msg); |
ddaec666 DM |
213 | } |
214 | ||
215 | $dbh->do ($cmds) if $cmds; | |
216 | ||
217 | $sth->finish(); | |
218 | ||
219 | if ($ecount > 0) { | |
220 | my $msg = "found $ecount expired mails in greylisting database"; | |
221 | $self->log (0, $msg); | |
222 | } | |
223 | ||
224 | $dbh->do ("DELETE FROM CGreylist WHERE extime < $now"); | |
225 | ||
226 | $dbh->commit; | |
227 | }; | |
228 | ||
229 | $err = $@; | |
230 | ||
231 | my ($csec_end, $usec_end) = gettimeofday (); | |
232 | my $ptime = int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000); | |
233 | ||
234 | if ($err) { | |
235 | $dbh->rollback if $dbh; | |
b902c0b8 | 236 | $self->log(0, $err); |
ddaec666 | 237 | } else { |
3164123a | 238 | $self->log(2, "end greylist database maintainance ($ptime ms)"); |
ddaec666 DM |
239 | } |
240 | ||
241 | $dbh->disconnect() if $dbh; | |
242 | } | |
243 | ||
244 | sub pre_loop_hook { | |
3164123a | 245 | my $self = shift; |
ddaec666 | 246 | |
3164123a | 247 | my $prop = $self->{server}; |
ddaec666 | 248 | |
3164123a | 249 | $prop->{log_level} = 3; |
ddaec666 | 250 | |
3164123a | 251 | $self->log(0, "Policy daemon (re)started"); |
ddaec666 | 252 | |
3164123a DM |
253 | $SIG{'USR1'} = sub { |
254 | # reloading server configuration | |
255 | if (defined $prop->{children}) { | |
256 | foreach my $pid (keys %{$prop->{children}}) { | |
08f49442 | 257 | kill(10, $pid); # SIGUSR1 childs |
3164123a DM |
258 | } |
259 | } | |
260 | }; | |
ddaec666 | 261 | |
3164123a DM |
262 | my $sig_set = POSIX::SigSet->new; |
263 | $sig_set->addset (&POSIX::SIGHUP); | |
264 | $sig_set->addset (&POSIX::SIGCHLD); | |
265 | my $old_sig_set = POSIX::SigSet->new(); | |
266 | ||
267 | sigprocmask (SIG_UNBLOCK, $sig_set, $old_sig_set); | |
ddaec666 DM |
268 | } |
269 | ||
270 | sub load_config { | |
3164123a | 271 | my $self = shift; |
ddaec666 | 272 | |
3164123a | 273 | my $prop = $self->{server}; |
ddaec666 | 274 | |
3164123a DM |
275 | if ($self->{ruledb}) { |
276 | $self->log(0, "reloading configuration $database"); | |
277 | $self->{ruledb}->close(); | |
278 | } | |
ddaec666 | 279 | |
3164123a | 280 | my $pmg_cfg = PMG::Config->new (); |
3164123a DM |
281 | $self->{use_spf} = $pmg_cfg->get('mail', 'spf'); |
282 | $self->{use_greylist} = $pmg_cfg->get('mail', 'greylist'); | |
ddaec666 | 283 | |
1dc67c8f DM |
284 | if ($opt_testmode) { |
285 | $self->{use_spf} = 1; | |
286 | $self->{use_greylist} = 1; | |
287 | } | |
288 | ||
3164123a DM |
289 | my $nodename = PVE::INotify::nodename(); |
290 | my $resolv = PVE::INotify::read_file('resolvconf'); | |
291 | my $domain = $resolv->{search}; | |
292 | $self->{fqdn} = "$nodename.$domain"; | |
ddaec666 | 293 | |
3164123a DM |
294 | my $cinfo = PVE::INotify::read_file("cluster.conf"); |
295 | my $lcid = $cinfo->{local}->{cid}; | |
296 | $self->{cinfo} = $cinfo; | |
297 | $self->{lcid} = $lcid; | |
ddaec666 | 298 | |
3164123a | 299 | my $dbh; |
ddaec666 | 300 | |
3164123a DM |
301 | eval { |
302 | $dbh = PMG::DBTools::open_ruledb($database); | |
303 | $self->{ruledb} = PMG::RuleDB->new($dbh); | |
304 | $self->{rulecache} = PMG::RuleCache->new($self->{ruledb}); | |
305 | }; | |
306 | if (my $err = $@) { | |
b902c0b8 | 307 | $self->log(0, "ERROR: unable to load database : $err"); |
3164123a | 308 | } |
ddaec666 | 309 | |
3164123a DM |
310 | $self->{reload_config} = 0; |
311 | } | |
ddaec666 | 312 | |
3164123a DM |
313 | sub child_init_hook { |
314 | my $self = shift; | |
315 | ||
316 | my $prop = $self->{server}; | |
ddaec666 | 317 | |
3164123a | 318 | $0 = 'pmgpolicy child'; |
ddaec666 | 319 | |
3164123a DM |
320 | setup_fork_signal_mask(0); # unblocking signals for children |
321 | ||
322 | eval { | |
323 | $self->load_config(); | |
324 | ||
325 | $self->{mux} = IO::Multiplex->new(); | |
326 | $self->{mux}->set_callback_object($self); | |
327 | ||
328 | my %dnsargs = ( | |
329 | tcp_timeout => 3, | |
330 | udp_timeout => 3, | |
331 | retry => 1, | |
332 | retrans => 0, | |
333 | dnsrch => 0, | |
334 | defnames => 0, | |
335 | ); | |
336 | ||
337 | if ($opt_testmode) { | |
338 | # $dnsargs{nameservers} = [ qw (213.129.232.1 213.129.226.2) ]; | |
339 | } | |
ddaec666 | 340 | |
3164123a DM |
341 | $self->{dns_resolver} = Net::DNS::Resolver->new(%dnsargs); |
342 | ||
343 | $self->{spf_server} = Mail::SPF::Server->new( | |
344 | hostname => $self->{fqdn}, dns_resolver => $self->{dns_resolver}); | |
345 | }; | |
346 | if (my $err = $@) { | |
b902c0b8 | 347 | $self->log(0, $err); |
3164123a DM |
348 | $self->child_finish_hook; |
349 | exit(-1); | |
350 | } | |
351 | ||
352 | $SIG{'USR1'} = sub { | |
353 | $self->{reload_config} = 1; | |
354 | } | |
ddaec666 DM |
355 | } |
356 | ||
357 | sub child_finish_hook { | |
3164123a | 358 | my $self = shift; |
ddaec666 | 359 | |
3164123a | 360 | my $prop = $self->{server}; |
ddaec666 | 361 | |
3164123a DM |
362 | $self->{ruledb}->close() if $self->{ruledb}; |
363 | } | |
ddaec666 DM |
364 | |
365 | sub get_spf_result { | |
366 | my ($self, $instance, $ip, $helo, $sender) = @_; | |
367 | ||
368 | my $result; | |
369 | my $spf_header; | |
370 | my $local_expl; | |
371 | my $auth_expl; | |
372 | ||
373 | # we only use helo tests when we have no sender, | |
3164123a | 374 | # helo is sometimes empty, so we cant use SPF helo tests |
ddaec666 DM |
375 | # in that case - strange |
376 | if ($helo && !$sender) { | |
377 | my $query; | |
378 | ||
3164123a | 379 | if (defined ($self->{cache}->{$instance}) && |
ddaec666 DM |
380 | defined ($self->{cache}->{$instance}->{spf_helo_result})) { |
381 | ||
382 | $query = $self->{cache}->{$instance}->{spf_helo_result}; | |
383 | ||
384 | } else { | |
3164123a DM |
385 | my $request = Mail::SPF::Request->new( |
386 | scope => 'helo', identity => $helo, ip_address => $ip); | |
387 | ||
388 | $query = $self->{cache}->{$instance}->{spf_helo_result} = | |
ddaec666 DM |
389 | $self->{spf_server}->process ($request); |
390 | } | |
391 | ||
392 | $result = $query->code; | |
393 | $spf_header = $query->received_spf_header; | |
394 | $local_expl = $query->local_explanation; | |
395 | $auth_expl = $query->authority_explanation if $query->is_code('fail'); | |
396 | ||
397 | # return if we get a definitive result | |
398 | if ($result eq 'pass' || $result eq 'fail' || $result eq 'temperror') { | |
399 | return ($result, $spf_header, $local_expl, $auth_expl); | |
400 | } | |
401 | } | |
402 | ||
403 | if ($sender) { | |
404 | ||
405 | my $query; | |
406 | ||
3164123a | 407 | if (defined ($self->{cache}->{$instance}) && |
ddaec666 | 408 | defined ($self->{cache}->{$instance}->{spf_mfrom_result})) { |
3164123a | 409 | |
ddaec666 DM |
410 | $query = $self->{cache}->{$instance}->{spf_mfrom_result}; |
411 | ||
412 | } else { | |
413 | ||
3164123a DM |
414 | my $request = Mail::SPF::Request->new( |
415 | scope => 'mfrom', identity => $sender, | |
416 | ip_address => $ip, helo_identity => $helo); | |
417 | ||
ddaec666 DM |
418 | $query = $self->{cache}->{$instance}->{spf_mfrom_result} = |
419 | $self->{spf_server}->process($request); | |
420 | } | |
421 | ||
422 | $result = $query->code; | |
423 | $spf_header = $query->received_spf_header; | |
424 | $local_expl = $query->local_explanation; | |
425 | $auth_expl = $query->authority_explanation if $query->is_code('fail'); | |
426 | ||
427 | return ($result, $spf_header, $local_expl, $auth_expl); | |
428 | } | |
429 | ||
430 | return undef; | |
431 | } | |
432 | ||
433 | sub is_backup_mx { | |
434 | my ($self, $ip, $receiver) = @_; | |
435 | ||
436 | my ($rdomain) = $receiver =~ /([^@]+)$/; | |
437 | ||
438 | my $dkey = "BKMX:$rdomain"; | |
439 | ||
440 | if (defined ($self->{cache}->{$dkey}) && | |
441 | ($self->{cache}->{$dkey}->{status} == 1)) { | |
442 | return $self->{cache}->{$dkey}->{$ip}; | |
443 | } | |
444 | ||
445 | my $resolver = $self->{dns_resolver}; | |
446 | ||
3164123a | 447 | if (my $mx = $resolver->send($rdomain, 'MX')) { |
ddaec666 DM |
448 | $self->{cache}->{$dkey}->{status} = 1; |
449 | my @mxa = grep { $_->type eq 'MX' } $mx->answer; | |
450 | my @mxl = sort { $a->preference <=> $b->preference } @mxa; | |
451 | # shift @mxl; # optionaly skip primary MX ? | |
452 | foreach my $rr (@mxl) { | |
453 | my $a = $resolver->send ($rr->exchange, 'A'); | |
454 | if ($a) { | |
455 | foreach my $rra ($a->answer) { | |
456 | if ($rra->type eq 'A') { | |
457 | $self->{cache}->{$dkey}->{$rra->address} = 1; | |
458 | } | |
459 | } | |
460 | } | |
461 | } | |
462 | } else { | |
463 | $self->{cache}->{$dkey}->{status} = 0; | |
464 | } | |
465 | ||
466 | return $self->{cache}->{$dkey}->{$ip}; | |
467 | } | |
468 | ||
469 | sub greylist_value { | |
470 | my ($self, $ctime, $helo, $ip, $sender, $rcpt, $instance) = @_; | |
471 | ||
472 | my $rulecache = $self->{rulecache}; | |
473 | ||
474 | my $dbh = $self->{ruledb}->{dbh}; | |
475 | ||
476 | # try to reconnect if database connection is broken | |
477 | if (!$dbh->ping) { | |
3164123a | 478 | $self->log(0, 'Database connection broken - trying to reconnect'); |
ddaec666 DM |
479 | my $dbh; |
480 | eval { | |
3164123a | 481 | $dbh = PMG::DBTools::open_ruledb($database); |
ddaec666 DM |
482 | }; |
483 | my $err = $@; | |
484 | if ($err) { | |
b902c0b8 | 485 | $self->log(0, "unable to reconnect to database server: $err"); |
ddaec666 DM |
486 | return 'dunno'; |
487 | } | |
3164123a | 488 | $self->{ruledb} = PMG::RuleDB->new($dbh); |
ddaec666 DM |
489 | } |
490 | ||
491 | # some sender substitutions | |
492 | my ($user, $domain) = split('@', $sender, 2); | |
493 | if (defined ($user) && defined ($domain)) { | |
494 | # see http://cr.yp.to/proto/verp.txt | |
495 | $user =~ s/\+.*//; # strip extensions (mailing-list VERP) | |
496 | $user =~ s/\b\d+\b/#/g; #replace nubmers in VERP address | |
497 | $sender = "$user\@$domain"; | |
498 | } | |
499 | ||
3164123a DM |
500 | if ($self->is_backup_mx($ip, $rcpt)) { |
501 | $self->log(3, "accept mails from backup MX host - $ip"); | |
ddaec666 DM |
502 | return 'dunno'; |
503 | } | |
504 | ||
505 | # greylist exclusion (sender whitelist) | |
506 | if ($rulecache->greylist_match ($sender, $ip)) { | |
3164123a | 507 | $self->log(3, "accept mails from whitelist - $ip"); |
ddaec666 DM |
508 | return 'dunno'; |
509 | } | |
510 | ||
ddaec666 DM |
511 | # greylist exclusion (receiver whitelist) |
512 | if ($rulecache->greylist_match_receiver ($rcpt)) { | |
3164123a | 513 | $self->log(3, "accept mails to whitelist - <$rcpt>"); |
ddaec666 DM |
514 | return 'dunno'; |
515 | } | |
516 | ||
517 | my ($net, $host) = $ip =~ m/(\d+\.\d+\.\d+)\.(\d+)/; | |
518 | my $spf_header; | |
519 | ||
3164123a | 520 | if ((!$opt_testmode && $self->{use_spf}) || |
ddaec666 DM |
521 | ($opt_testmode && ($rcpt =~ m/^testspf/))) { |
522 | ||
523 | # ask SPF | |
524 | my $spf_result; | |
525 | my $local_expl, | |
526 | my $auth_expl; | |
527 | ||
528 | my $previous_alarm; | |
529 | ||
530 | my ($result, $smtp_comment, $header_comment); | |
531 | ||
532 | eval { | |
3164123a | 533 | $previous_alarm = alarm(10); |
ddaec666 | 534 | local $SIG{ALRM} = sub { die "SPF timeout\n" }; |
3164123a DM |
535 | |
536 | ($result, $spf_header, $local_expl, $auth_expl) = | |
537 | $self->get_spf_result($instance, $ip, $helo, $sender); | |
538 | ||
539 | alarm(0); # avoid race condition | |
ddaec666 | 540 | }; |
ddaec666 DM |
541 | my $err = $@; |
542 | ||
3164123a | 543 | alarm($previous_alarm) if defined($previous_alarm); |
ddaec666 DM |
544 | |
545 | if ($err) { | |
546 | $err = $err->text if UNIVERSAL::isa ($err, 'Mail::SPF::Exception'); | |
b902c0b8 | 547 | $self->log (0, $err); |
ddaec666 DM |
548 | } else { |
549 | ||
550 | if ($result && $result eq 'pass') { | |
3164123a | 551 | $self->log(3, "SPF says $result"); |
ddaec666 DM |
552 | $spf_result = $spf_header ? "prepend $spf_header" : 'dunno'; |
553 | } | |
3164123a | 554 | |
ddaec666 | 555 | if ($result && $result eq 'fail') { |
3164123a DM |
556 | $self->log(3, "SPF says $result"); |
557 | $spf_result = "reject ${auth_expl}"; | |
558 | ||
ddaec666 DM |
559 | eval { |
560 | ||
561 | $dbh->begin_work; | |
562 | ||
563 | # try to avoid locks everywhere - we use merge instead of insert | |
564 | #$dbh->do ("LOCK TABLE CGreylist IN ROW EXCLUSIVE MODE"); | |
3164123a | 565 | |
ddaec666 | 566 | # check if there is already a record in the GL database |
3164123a DM |
567 | my $sth = $dbh->prepare( |
568 | "SELECT * FROM CGreylist " . | |
569 | "where IPNet = ? AND Sender = ? AND Receiver = ?"); | |
ddaec666 | 570 | |
3164123a | 571 | $sth->execute($net, $sender, $rcpt); |
ddaec666 DM |
572 | my $ref = $sth->fetchrow_hashref(); |
573 | $sth->finish(); | |
574 | ||
575 | # else add an entry to the GL Database with short | |
3164123a | 576 | # expiration time. run_dequeue() moves those entries into the statistic |
ddaec666 DM |
577 | # table later. We set 'blocked' to 100000 to identify those entries. |
578 | ||
3164123a | 579 | if (!defined($ref->{rctime})) { |
ddaec666 | 580 | |
2e049252 | 581 | $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef, |
3164123a DM |
582 | $net, $host, $sender, $rcpt, $instance, |
583 | $ctime, $ctime + 10, 0, 100000, 0, $ctime, $self->{lcid}); | |
ddaec666 DM |
584 | } |
585 | ||
586 | $dbh->commit; | |
587 | }; | |
3164123a | 588 | if (my $err = $@) { |
ddaec666 | 589 | $dbh->rollback; |
b902c0b8 | 590 | $self->log(0, $err); |
ddaec666 DM |
591 | } |
592 | } | |
593 | } | |
594 | ||
595 | return $spf_result if $spf_result; | |
596 | } | |
597 | ||
598 | ||
599 | my $res = $spf_header ? "prepend $spf_header" : 'dunno'; | |
600 | ||
601 | return $res if !$self->{use_greylist}; | |
602 | ||
3164123a DM |
603 | my $defer_res = "defer_if_permit Service is unavailable (try later)"; |
604 | ||
ddaec666 DM |
605 | eval { |
606 | ||
607 | # we dont use alarm here, because it does not work with DBI | |
608 | ||
609 | $dbh->begin_work; | |
610 | ||
611 | # try to avoid locks everywhere - we use merge instead of insert | |
612 | #$dbh->do ("LOCK TABLE CGreylist IN ROW EXCLUSIVE MODE"); | |
613 | ||
3164123a DM |
614 | my $sth = $dbh->prepare( |
615 | "SELECT * FROM CGreylist " . | |
616 | "where IPNet = ? AND Sender = ? AND Receiver = ?"); | |
617 | ||
618 | $sth->execute($net, $sender, $rcpt); | |
ddaec666 | 619 | |
ddaec666 DM |
620 | my $ref = $sth->fetchrow_hashref(); |
621 | ||
622 | $sth->finish(); | |
623 | ||
3164123a | 624 | if (!defined($ref->{rctime})) { |
ddaec666 | 625 | |
2e049252 DM |
626 | $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef, |
627 | $net, $host, $sender, $rcpt, $instance, | |
628 | $ctime, $ctime + $greylist_lifetime, 0, 1, 0, $ctime, $self->{lcid}); | |
ddaec666 DM |
629 | |
630 | $res = $defer_res; | |
3164123a | 631 | $self->log(3, "defer greylisted mail"); |
ddaec666 | 632 | } else { |
3164123a | 633 | my $age = $ctime - $ref->{rctime}; |
ddaec666 DM |
634 | |
635 | if ($age < $greylist_delay) { | |
636 | # defer (resent within greylist_delay window) | |
637 | $res = $defer_res; | |
3164123a DM |
638 | $self->log(3, "defer greylisted mail"); |
639 | $dbh->do("UPDATE CGreylist " . | |
640 | "SET Blocked = Blocked + 1, Host = ?, MTime = ? " . | |
641 | "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef, | |
642 | $host, $ctime, $net, $sender, $rcpt); | |
ddaec666 DM |
643 | } else { |
644 | if ($ctime < $ref->{extime}) { | |
645 | # accept (not expired) | |
646 | my $lifetime = $sender eq "" ? 0 : $greylist_awlifetime; | |
647 | my $delay = $ref->{passed} ? "" : "Delay = $age, "; | |
3164123a DM |
648 | $dbh->do("UPDATE CGreylist " . |
649 | "SET Passed = Passed + 1, $delay Host = ?, ExTime = ?, MTime = ? " . | |
650 | "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef, | |
651 | $host, $ctime + $lifetime, $ctime, $net, $sender, $rcpt); | |
ddaec666 DM |
652 | } else { |
653 | # defer (record is expired) | |
654 | $res = $defer_res; | |
3164123a DM |
655 | $dbh->do("UPDATE CGreylist " . |
656 | "SET Host = ?, RCTime = ?, ExTime = ?, MTime = ?, Instance = ?, " . | |
657 | "Blocked = 1, Passed = 0 " . | |
658 | "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef, | |
659 | $host, $ctime, $ctime + $greylist_lifetime, $ctime, $instance, | |
660 | $net, $sender, $rcpt); | |
ddaec666 | 661 | } |
3164123a | 662 | } |
ddaec666 DM |
663 | } |
664 | ||
665 | $dbh->commit; | |
666 | }; | |
3164123a DM |
667 | if (my $err = $@) { |
668 | $dbh->rollback; | |
b902c0b8 | 669 | $self->log (0, $err); |
ddaec666 DM |
670 | } |
671 | ||
672 | return $res; | |
673 | } | |
674 | ||
3164123a | 675 | # shutdown connections: we need this - else file handles are |
ddaec666 DM |
676 | # not closed and we run out of handles |
677 | sub mux_eof { | |
678 | my ($self, $mux, $fh) = @_; | |
679 | ||
680 | $mux->shutdown($fh, 1); | |
681 | } | |
682 | ||
683 | sub mux_input { | |
684 | my ($self, $mux, $fh, $dataref) = @_; | |
685 | my $prop = $self->{server}; | |
3164123a | 686 | |
ddaec666 DM |
687 | my $attribute = {}; |
688 | ||
689 | eval { | |
3164123a | 690 | $self->load_config() if $self->{reload_config}; |
ddaec666 DM |
691 | |
692 | while ($$dataref =~ s/^([^\r\n]*)\r?\n//) { | |
693 | my $line = $1; | |
694 | next if !defined ($line); | |
695 | ||
696 | if ($line =~ m/([^=]+)=(.*)/) { | |
697 | $attribute->{substr($1, 0, 255)} = substr($2, 0, 255); | |
3164123a | 698 | } elsif ($line eq '') { |
ddaec666 DM |
699 | my $res = 'dunno'; |
700 | my $ctime = time; | |
701 | ||
702 | if ($opt_testmode) { | |
703 | die "undefined test time :ERROR" if !defined $attribute->{testtime}; | |
704 | $ctime = $attribute->{testtime}; | |
705 | } | |
706 | ||
3164123a | 707 | if ($attribute->{instance} && $attribute->{recipient} && |
ddaec666 DM |
708 | $attribute->{client_address} && $attribute->{request} && |
709 | $attribute->{request} eq 'smtpd_access_policy') { | |
3164123a | 710 | |
ddaec666 | 711 | eval { |
3164123a DM |
712 | |
713 | $res = $self->greylist_value( | |
714 | $ctime, | |
715 | lc ($attribute->{helo_name}), | |
716 | lc ($attribute->{client_address}), | |
717 | lc ($attribute->{sender}), | |
718 | lc ($attribute->{recipient}), | |
719 | lc ($attribute->{instance})); | |
ddaec666 | 720 | }; |
3164123a | 721 | if (my $err = $@) { |
b902c0b8 | 722 | $self->log(0, $err); |
3164123a | 723 | } |
ddaec666 DM |
724 | } |
725 | ||
726 | print $fh "action=$res\n\n"; | |
727 | ||
728 | $attribute = {}; | |
729 | } else { | |
3164123a | 730 | $self->log(0, "greylist policy protocol error - got '%s'", $line); |
ddaec666 DM |
731 | } |
732 | } | |
733 | }; | |
3164123a | 734 | my $err = $@; |
ddaec666 DM |
735 | |
736 | # remove remaining data, if any | |
737 | if ($$dataref ne '') { | |
3164123a | 738 | $self->log(0, "greylist policy protocol error - unused data '%s'", $$dataref); |
ddaec666 DM |
739 | $$dataref = ''; |
740 | } | |
741 | ||
b902c0b8 | 742 | $self->log(0, $err) if $err; |
ddaec666 DM |
743 | } |
744 | ||
745 | sub restart_close_hook { | |
3164123a | 746 | my $self = shift; |
ddaec666 | 747 | |
3164123a DM |
748 | my $sig_set = POSIX::SigSet->new; |
749 | $sig_set->addset(&POSIX::SIGHUP); | |
750 | $sig_set->addset(&POSIX::SIGCHLD); # to avoid zombies | |
751 | my $old_sig_set = POSIX::SigSet->new(); | |
ddaec666 | 752 | |
3164123a | 753 | sigprocmask(SIG_BLOCK, $sig_set, $old_sig_set); |
ddaec666 DM |
754 | } |
755 | ||
756 | sub pre_server_close_hook { | |
3164123a DM |
757 | my $self = shift; |
758 | ||
759 | my $prop = $self->{server}; | |
ddaec666 | 760 | |
3164123a DM |
761 | if (defined $prop->{_HUP}) { |
762 | undef $prop->{pid_file_unlink}; | |
763 | } | |
08f49442 DM |
764 | |
765 | if (defined $prop->{children}) { | |
766 | foreach my $pid (keys %{$prop->{children}}) { | |
767 | kill(1, $pid); # HUP childs | |
768 | } | |
769 | } | |
770 | ||
771 | # nicely shutdown childs (give them max 30 seconds to shut down) | |
772 | my $previous_alarm = alarm(30); | |
773 | eval { | |
774 | local $SIG{ALRM} = sub { die "Timed Out!\n" }; | |
775 | ||
776 | my $pid; | |
777 | 1 while ((($pid = waitpid(-1, 0)) > 0) || ($! == EINTR)); | |
778 | ||
779 | alarm(0); # avoid race | |
780 | }; | |
781 | alarm ($previous_alarm); | |
ddaec666 DM |
782 | } |
783 | ||
784 | sub setup_fork_signal_mask { | |
785 | my $block = shift; | |
786 | ||
787 | my $sig_set = POSIX::SigSet->new; | |
3164123a DM |
788 | $sig_set->addset(&POSIX::SIGINT); |
789 | $sig_set->addset(&POSIX::SIGTERM); | |
790 | $sig_set->addset(&POSIX::SIGQUIT); | |
791 | $sig_set->addset(&POSIX::SIGHUP); | |
ddaec666 DM |
792 | my $old_sig_set = POSIX::SigSet->new(); |
793 | ||
794 | if ($block) { | |
795 | sigprocmask (SIG_BLOCK, $sig_set, $old_sig_set); | |
796 | } else { | |
797 | sigprocmask (SIG_UNBLOCK, $sig_set, $old_sig_set); | |
798 | } | |
799 | } | |
800 | ||
801 | # subroutine to start up a specified number of children. | |
802 | # We need to block signals until handlers are set up correctly. | |
3164123a DM |
803 | # Else its possible that HUP occurs after fork, which triggers |
804 | # singal TERM at childs and calls server_close() instead of | |
ddaec666 DM |
805 | # simply exit the child. |
806 | # Note: on server startup signals are setup to trigger | |
807 | # asynchronously for a short period of time (in PreForkSimple]::loop, | |
3164123a | 808 | # run_n_children is called before run_parent) |
ddaec666 | 809 | # Net::Server::PreFork does not have this problem, because it is using |
3164123a | 810 | # signal HUP stop children |
ddaec666 | 811 | sub run_n_children { |
3164123a DM |
812 | my ($self, $n) = @_; |
813 | ||
ddaec666 | 814 | my $prop = $self->{server}; |
ddaec666 | 815 | |
3164123a DM |
816 | setup_fork_signal_mask(1); # block signals |
817 | ||
818 | $self->SUPER::run_n_children($n); | |
ddaec666 | 819 | |
3164123a | 820 | setup_fork_signal_mask(0); # unblocking signals for parent |
ddaec666 DM |
821 | } |
822 | ||
3164123a | 823 | # test sig_hup with: for ((;;)) ;do kill -HUP `cat /var/run/pmgpolicy.pid`; done; |
ddaec666 DM |
824 | # wrapper to avoid multiple calls to sig_hup |
825 | sub sig_hup { | |
3164123a DM |
826 | my $self = shift; |
827 | ||
828 | my $prop = $self->{server}; | |
ddaec666 | 829 | |
3164123a | 830 | return if defined($prop->{_HUP}); # do not call twice |
ddaec666 | 831 | |
3164123a | 832 | $self->SUPER::sig_hup(); |
ddaec666 DM |
833 | } |
834 | ||
835 | ### child process which will accept on the port | |
836 | sub run_child { | |
3164123a | 837 | my $self = shift; |
ddaec666 | 838 | |
3164123a | 839 | my $prop = $self->{server}; |
ddaec666 | 840 | |
3164123a | 841 | $self->log(4, "Child Preforked ($$)\n"); |
ddaec666 | 842 | |
3164123a DM |
843 | # set correct signal handlers before enabling signals again |
844 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { | |
845 | $self->child_finish_hook; | |
846 | exit; | |
847 | }; | |
ddaec666 | 848 | |
3164123a | 849 | delete $prop->{children}; |
ddaec666 | 850 | |
3164123a | 851 | $self->child_init_hook; |
ddaec666 | 852 | |
3164123a | 853 | # accept connections |
ddaec666 | 854 | |
3164123a | 855 | my $sock = $prop->{sock}->[0]; |
ddaec666 | 856 | |
3164123a DM |
857 | # make sure we got a good sock |
858 | if (!defined ($sock)){ | |
859 | $self->log(0, "ERROR: Received a bad socket"); | |
860 | exit (-1); | |
861 | } | |
862 | ||
863 | # sometimes the socket is not usable, don't know why | |
864 | my $flags = fcntl($sock, F_GETFL, 0); | |
865 | if (!$flags) { | |
b902c0b8 | 866 | $self->log(0, "socket not ready - $!"); |
3164123a DM |
867 | exit (-1); |
868 | } | |
ddaec666 | 869 | |
3164123a DM |
870 | # cache is limited, because postfix does max. 100 queries |
871 | $self->{cache} = {}; | |
ddaec666 | 872 | |
3164123a DM |
873 | eval { |
874 | my $mux = $self->{mux}; | |
875 | $mux->listen ($sock); | |
876 | $mux->loop; | |
877 | }; | |
878 | if (my $err = $@) { | |
b902c0b8 | 879 | $self->log(0, "ERROR: $err"); |
3164123a | 880 | } |
ddaec666 | 881 | |
3164123a | 882 | $self->child_finish_hook; |
ddaec666 | 883 | |
3164123a | 884 | exit; |
ddaec666 DM |
885 | } |
886 | ||
3164123a DM |
887 | my $syslog_map = { |
888 | 0 => 'err', | |
889 | 1 => 'warning', | |
890 | 2 => 'notice', | |
891 | 3 => 'info', | |
892 | 4 => 'debug' | |
893 | }; | |
894 | ||
ddaec666 DM |
895 | sub log { |
896 | my ($self, $level, $msg, @therest) = @_; | |
3164123a | 897 | |
ddaec666 DM |
898 | my $prop = $self->{server}; |
899 | ||
900 | return if $level =~ /^\d+$/ && $level > $prop->{log_level}; | |
901 | ||
3164123a | 902 | $level = $syslog_map->{$level} || $level; |
ddaec666 DM |
903 | if (@therest) { |
904 | syslog($level, $msg, @therest); | |
905 | } else { | |
906 | syslog ($level, $msg); | |
907 | } | |
908 | } | |
909 | ||
910 | my $server = bless { | |
911 | server => $server_attr, | |
912 | }; | |
913 | ||
914 | $server->sig_chld(); # avoid zombies after restart | |
915 | ||
916 | $server->run (); | |
917 | ||
918 | exit (0); | |
919 | ||
920 | __END__ | |
921 | ||
922 | =head1 NAME | |
3164123a DM |
923 | |
924 | pmgpolicy - The Proxmox policy daemon | |
ddaec666 DM |
925 | |
926 | =head1 SYNOPSIS | |
927 | ||
3164123a | 928 | pmgpolicy |
ddaec666 DM |
929 | |
930 | =head1 DESCRIPTION | |
931 | ||
932 | Documentation is available at www.proxmox.com |