]> git.proxmox.com Git - pmg-api.git/blame - src/bin/pmgpolicy
split source and packaging, and clean latter a bit up
[pmg-api.git] / src / 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
8a35c2e4
DM
548 my ($net, $host) = $ip =~ m/(\d+\.\d+\.\d+)\.(\d+)/; # IPv4 for now
549 return 'dunno' if !defined($net);
550
ddaec666
DM
551 my $spf_header;
552
3164123a 553 if ((!$opt_testmode && $self->{use_spf}) ||
ddaec666
DM
554 ($opt_testmode && ($rcpt =~ m/^testspf/))) {
555
556 # ask SPF
557 my $spf_result;
558 my $local_expl,
559 my $auth_expl;
560
561 my $previous_alarm;
562
563 my ($result, $smtp_comment, $header_comment);
564
565 eval {
3164123a 566 $previous_alarm = alarm(10);
ddaec666 567 local $SIG{ALRM} = sub { die "SPF timeout\n" };
3164123a
DM
568
569 ($result, $spf_header, $local_expl, $auth_expl) =
570 $self->get_spf_result($instance, $ip, $helo, $sender);
571
572 alarm(0); # avoid race condition
ddaec666 573 };
ddaec666
DM
574 my $err = $@;
575
3164123a 576 alarm($previous_alarm) if defined($previous_alarm);
ddaec666
DM
577
578 if ($err) {
579 $err = $err->text if UNIVERSAL::isa ($err, 'Mail::SPF::Exception');
b902c0b8 580 $self->log (0, $err);
ddaec666
DM
581 } else {
582
583 if ($result && $result eq 'pass') {
3164123a 584 $self->log(3, "SPF says $result");
ddaec666
DM
585 $spf_result = $spf_header ? "prepend $spf_header" : 'dunno';
586 }
3164123a 587
ddaec666 588 if ($result && $result eq 'fail') {
3164123a
DM
589 $self->log(3, "SPF says $result");
590 $spf_result = "reject ${auth_expl}";
591
ddaec666
DM
592 eval {
593
594 $dbh->begin_work;
595
596 # try to avoid locks everywhere - we use merge instead of insert
597 #$dbh->do ("LOCK TABLE CGreylist IN ROW EXCLUSIVE MODE");
3164123a 598
ddaec666 599 # check if there is already a record in the GL database
3164123a
DM
600 my $sth = $dbh->prepare(
601 "SELECT * FROM CGreylist " .
602 "where IPNet = ? AND Sender = ? AND Receiver = ?");
ddaec666 603
3164123a 604 $sth->execute($net, $sender, $rcpt);
ddaec666
DM
605 my $ref = $sth->fetchrow_hashref();
606 $sth->finish();
607
608 # else add an entry to the GL Database with short
3164123a 609 # expiration time. run_dequeue() moves those entries into the statistic
ddaec666
DM
610 # table later. We set 'blocked' to 100000 to identify those entries.
611
3164123a 612 if (!defined($ref->{rctime})) {
ddaec666 613
2e049252 614 $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef,
3164123a
DM
615 $net, $host, $sender, $rcpt, $instance,
616 $ctime, $ctime + 10, 0, 100000, 0, $ctime, $self->{lcid});
ddaec666
DM
617 }
618
619 $dbh->commit;
620 };
3164123a 621 if (my $err = $@) {
ddaec666 622 $dbh->rollback;
b902c0b8 623 $self->log(0, $err);
ddaec666
DM
624 }
625 }
626 }
627
628 return $spf_result if $spf_result;
629 }
630
631
fd218150
DM
632 my $res = 'dunno';
633
634 # add spf_header once - SA can re-use this information
635 if (!defined($self->{cache}->{$instance}) ||
636 !$self->{cache}->{$instance}->{spf_header_added}) {
637 $res = "prepend $spf_header" if $spf_header;
638 $self->{cache}->{$instance}->{spf_header_added} = 1;
639 }
ddaec666
DM
640
641 return $res if !$self->{use_greylist};
642
3164123a
DM
643 my $defer_res = "defer_if_permit Service is unavailable (try later)";
644
ddaec666
DM
645 eval {
646
647 # we dont use alarm here, because it does not work with DBI
648
649 $dbh->begin_work;
650
651 # try to avoid locks everywhere - we use merge instead of insert
652 #$dbh->do ("LOCK TABLE CGreylist IN ROW EXCLUSIVE MODE");
653
3164123a
DM
654 my $sth = $dbh->prepare(
655 "SELECT * FROM CGreylist " .
656 "where IPNet = ? AND Sender = ? AND Receiver = ?");
657
658 $sth->execute($net, $sender, $rcpt);
ddaec666 659
ddaec666
DM
660 my $ref = $sth->fetchrow_hashref();
661
662 $sth->finish();
663
3164123a 664 if (!defined($ref->{rctime})) {
ddaec666 665
2e049252
DM
666 $dbh->do($PMG::DBTools::cgreylist_merge_sql, undef,
667 $net, $host, $sender, $rcpt, $instance,
668 $ctime, $ctime + $greylist_lifetime, 0, 1, 0, $ctime, $self->{lcid});
ddaec666
DM
669
670 $res = $defer_res;
3164123a 671 $self->log(3, "defer greylisted mail");
ddaec666 672 } else {
3164123a 673 my $age = $ctime - $ref->{rctime};
ddaec666
DM
674
675 if ($age < $greylist_delay) {
676 # defer (resent within greylist_delay window)
677 $res = $defer_res;
3164123a
DM
678 $self->log(3, "defer greylisted mail");
679 $dbh->do("UPDATE CGreylist " .
680 "SET Blocked = Blocked + 1, Host = ?, MTime = ? " .
681 "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef,
682 $host, $ctime, $net, $sender, $rcpt);
ddaec666
DM
683 } else {
684 if ($ctime < $ref->{extime}) {
685 # accept (not expired)
686 my $lifetime = $sender eq "" ? 0 : $greylist_awlifetime;
687 my $delay = $ref->{passed} ? "" : "Delay = $age, ";
3164123a
DM
688 $dbh->do("UPDATE CGreylist " .
689 "SET Passed = Passed + 1, $delay Host = ?, ExTime = ?, MTime = ? " .
690 "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef,
691 $host, $ctime + $lifetime, $ctime, $net, $sender, $rcpt);
ddaec666
DM
692 } else {
693 # defer (record is expired)
694 $res = $defer_res;
3164123a
DM
695 $dbh->do("UPDATE CGreylist " .
696 "SET Host = ?, RCTime = ?, ExTime = ?, MTime = ?, Instance = ?, " .
697 "Blocked = 1, Passed = 0 " .
698 "WHERE IPNet = ? AND Sender = ? AND Receiver = ?", undef,
699 $host, $ctime, $ctime + $greylist_lifetime, $ctime, $instance,
700 $net, $sender, $rcpt);
ddaec666 701 }
3164123a 702 }
ddaec666
DM
703 }
704
705 $dbh->commit;
706 };
3164123a
DM
707 if (my $err = $@) {
708 $dbh->rollback;
b902c0b8 709 $self->log (0, $err);
ddaec666
DM
710 }
711
712 return $res;
713}
714
3164123a 715# shutdown connections: we need this - else file handles are
ddaec666
DM
716# not closed and we run out of handles
717sub mux_eof {
718 my ($self, $mux, $fh) = @_;
719
720 $mux->shutdown($fh, 1);
721}
722
c196a54a
DM
723
724my $last_reload_test = 0;
725my $last_confid_version;
726my (undef, $pmgconffilename) = PVE::INotify::ccache_info('pmg.conf');
727sub test_config_version {
728
729 my $ctime = time();
730
731 if (($ctime - $last_reload_test) < 5) { return 0; }
732
733 $last_reload_test = $ctime;
734
735 my $version = PVE::INotify::poll_changes($pmgconffilename);
736
737 if (!defined($last_confid_version) ||
738 $last_confid_version != $version) {
739 $last_confid_version = $version;
740 return 1;
741 }
742
743 return 0;
744}
745
ddaec666
DM
746sub mux_input {
747 my ($self, $mux, $fh, $dataref) = @_;
748 my $prop = $self->{server};
3164123a 749
ddaec666
DM
750 my $attribute = {};
751
752 eval {
c196a54a 753 $self->{reload_config} = 1 if test_config_version();
3164123a 754 $self->load_config() if $self->{reload_config};
ddaec666
DM
755
756 while ($$dataref =~ s/^([^\r\n]*)\r?\n//) {
757 my $line = $1;
758 next if !defined ($line);
759
760 if ($line =~ m/([^=]+)=(.*)/) {
761 $attribute->{substr($1, 0, 255)} = substr($2, 0, 255);
3164123a 762 } elsif ($line eq '') {
ddaec666
DM
763 my $res = 'dunno';
764 my $ctime = time;
765
766 if ($opt_testmode) {
767 die "undefined test time :ERROR" if !defined $attribute->{testtime};
768 $ctime = $attribute->{testtime};
769 }
770
3164123a 771 if ($attribute->{instance} && $attribute->{recipient} &&
ddaec666
DM
772 $attribute->{client_address} && $attribute->{request} &&
773 $attribute->{request} eq 'smtpd_access_policy') {
3164123a 774
ddaec666 775 eval {
3164123a
DM
776
777 $res = $self->greylist_value(
778 $ctime,
779 lc ($attribute->{helo_name}),
780 lc ($attribute->{client_address}),
781 lc ($attribute->{sender}),
782 lc ($attribute->{recipient}),
783 lc ($attribute->{instance}));
ddaec666 784 };
3164123a 785 if (my $err = $@) {
b902c0b8 786 $self->log(0, $err);
3164123a 787 }
ddaec666
DM
788 }
789
790 print $fh "action=$res\n\n";
791
792 $attribute = {};
793 } else {
3164123a 794 $self->log(0, "greylist policy protocol error - got '%s'", $line);
ddaec666
DM
795 }
796 }
797 };
3164123a 798 my $err = $@;
ddaec666
DM
799
800 # remove remaining data, if any
801 if ($$dataref ne '') {
3164123a 802 $self->log(0, "greylist policy protocol error - unused data '%s'", $$dataref);
ddaec666
DM
803 $$dataref = '';
804 }
805
b902c0b8 806 $self->log(0, $err) if $err;
ddaec666
DM
807}
808
809sub restart_close_hook {
3164123a 810 my $self = shift;
ddaec666 811
3164123a
DM
812 my $sig_set = POSIX::SigSet->new;
813 $sig_set->addset(&POSIX::SIGHUP);
814 $sig_set->addset(&POSIX::SIGCHLD); # to avoid zombies
815 my $old_sig_set = POSIX::SigSet->new();
ddaec666 816
3164123a 817 sigprocmask(SIG_BLOCK, $sig_set, $old_sig_set);
ddaec666
DM
818}
819
820sub pre_server_close_hook {
3164123a
DM
821 my $self = shift;
822
823 my $prop = $self->{server};
ddaec666 824
3164123a
DM
825 if (defined $prop->{_HUP}) {
826 undef $prop->{pid_file_unlink};
827 }
08f49442
DM
828
829 if (defined $prop->{children}) {
830 foreach my $pid (keys %{$prop->{children}}) {
831 kill(1, $pid); # HUP childs
832 }
833 }
834
835 # nicely shutdown childs (give them max 30 seconds to shut down)
836 my $previous_alarm = alarm(30);
837 eval {
838 local $SIG{ALRM} = sub { die "Timed Out!\n" };
839
840 my $pid;
841 1 while ((($pid = waitpid(-1, 0)) > 0) || ($! == EINTR));
842
843 alarm(0); # avoid race
844 };
845 alarm ($previous_alarm);
ddaec666
DM
846}
847
848sub setup_fork_signal_mask {
849 my $block = shift;
850
851 my $sig_set = POSIX::SigSet->new;
3164123a
DM
852 $sig_set->addset(&POSIX::SIGINT);
853 $sig_set->addset(&POSIX::SIGTERM);
854 $sig_set->addset(&POSIX::SIGQUIT);
855 $sig_set->addset(&POSIX::SIGHUP);
ddaec666
DM
856 my $old_sig_set = POSIX::SigSet->new();
857
858 if ($block) {
859 sigprocmask (SIG_BLOCK, $sig_set, $old_sig_set);
860 } else {
861 sigprocmask (SIG_UNBLOCK, $sig_set, $old_sig_set);
862 }
863}
864
865# subroutine to start up a specified number of children.
866# We need to block signals until handlers are set up correctly.
3164123a
DM
867# Else its possible that HUP occurs after fork, which triggers
868# singal TERM at childs and calls server_close() instead of
ddaec666
DM
869# simply exit the child.
870# Note: on server startup signals are setup to trigger
871# asynchronously for a short period of time (in PreForkSimple]::loop,
3164123a 872# run_n_children is called before run_parent)
ddaec666 873# Net::Server::PreFork does not have this problem, because it is using
3164123a 874# signal HUP stop children
ddaec666 875sub run_n_children {
3164123a
DM
876 my ($self, $n) = @_;
877
ddaec666 878 my $prop = $self->{server};
ddaec666 879
3164123a
DM
880 setup_fork_signal_mask(1); # block signals
881
882 $self->SUPER::run_n_children($n);
ddaec666 883
3164123a 884 setup_fork_signal_mask(0); # unblocking signals for parent
ddaec666
DM
885}
886
3164123a 887# test sig_hup with: for ((;;)) ;do kill -HUP `cat /var/run/pmgpolicy.pid`; done;
ddaec666
DM
888# wrapper to avoid multiple calls to sig_hup
889sub sig_hup {
3164123a
DM
890 my $self = shift;
891
892 my $prop = $self->{server};
ddaec666 893
3164123a 894 return if defined($prop->{_HUP}); # do not call twice
ddaec666 895
3164123a 896 $self->SUPER::sig_hup();
ddaec666
DM
897}
898
899### child process which will accept on the port
900sub run_child {
3164123a 901 my $self = shift;
ddaec666 902
3164123a 903 my $prop = $self->{server};
ddaec666 904
3164123a 905 $self->log(4, "Child Preforked ($$)\n");
ddaec666 906
3164123a
DM
907 # set correct signal handlers before enabling signals again
908 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub {
909 $self->child_finish_hook;
910 exit;
911 };
ddaec666 912
3164123a 913 delete $prop->{children};
ddaec666 914
3164123a 915 $self->child_init_hook;
ddaec666 916
3164123a 917 # accept connections
ddaec666 918
3164123a 919 my $sock = $prop->{sock}->[0];
ddaec666 920
3164123a
DM
921 # make sure we got a good sock
922 if (!defined ($sock)){
923 $self->log(0, "ERROR: Received a bad socket");
924 exit (-1);
925 }
926
927 # sometimes the socket is not usable, don't know why
928 my $flags = fcntl($sock, F_GETFL, 0);
929 if (!$flags) {
b902c0b8 930 $self->log(0, "socket not ready - $!");
3164123a
DM
931 exit (-1);
932 }
ddaec666 933
3164123a
DM
934 # cache is limited, because postfix does max. 100 queries
935 $self->{cache} = {};
ddaec666 936
3164123a
DM
937 eval {
938 my $mux = $self->{mux};
939 $mux->listen ($sock);
940 $mux->loop;
941 };
942 if (my $err = $@) {
b902c0b8 943 $self->log(0, "ERROR: $err");
3164123a 944 }
ddaec666 945
3164123a 946 $self->child_finish_hook;
ddaec666 947
3164123a 948 exit;
ddaec666
DM
949}
950
3164123a
DM
951my $syslog_map = {
952 0 => 'err',
953 1 => 'warning',
954 2 => 'notice',
955 3 => 'info',
956 4 => 'debug'
957};
958
ddaec666
DM
959sub log {
960 my ($self, $level, $msg, @therest) = @_;
3164123a 961
ddaec666
DM
962 my $prop = $self->{server};
963
964 return if $level =~ /^\d+$/ && $level > $prop->{log_level};
965
3164123a 966 $level = $syslog_map->{$level} || $level;
ddaec666
DM
967 if (@therest) {
968 syslog($level, $msg, @therest);
969 } else {
970 syslog ($level, $msg);
971 }
972}
973
974my $server = bless {
975 server => $server_attr,
976};
977
978$server->sig_chld(); # avoid zombies after restart
979
980$server->run ();
981
982exit (0);
983
984__END__
985
986=head1 NAME
3164123a
DM
987
988pmgpolicy - The Proxmox policy daemon
ddaec666
DM
989
990=head1 SYNOPSIS
991
3164123a 992pmgpolicy
ddaec666
DM
993
994=head1 DESCRIPTION
995
996Documentation is available at www.proxmox.com