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