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