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