8 use Time
::HiRes qw
(usleep gettimeofday tv_interval
);
9 use POSIX
qw(:sys_wait_h errno_h signal_h);
13 use Net
::Server
::PreFork
;
14 use Net
::Server
::SIG
qw(register_sig check_sigs);
25 use Mail
::SpamAssassin
;
26 use Mail
::SpamAssassin
::NetSet
;
31 use PMG
::ClusterConfig
;
48 use base
qw(Net::Server::PreFork);
50 my $opt_commandline = [$0, @ARGV];
51 my $opt_max_dequeue = 1;
52 my $opt_dequeue_time = 30;
54 my $opt_ext_port = 10024;
55 my $opt_int_port = 10023;
56 my $opt_inject_port = 10025;
63 my $prog_name = 'pmg-smtp-filter';
65 initlog
($prog_name, 'mail');
67 if (!GetOptions
('testmode=s' => \
$opt_testmode,
68 'pidfile=s' => \
$opt_pidfile,
69 'untrusted' => \
$opt_untrusted,
70 'database=s' => \
$opt_database)) {
75 $opt_pidfile = "/run/${prog_name}.pid" if !$opt_pidfile;
79 my $min_spare_servers = 0;
80 my $max_spare_servers = 0;
85 my $pmg_cfg = PMG
::Config-
>new();
87 my $demo = $pmg_cfg->get('admin', 'demo');
90 syslog
('info', 'demo mode detected - not starting server');
94 $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2;
96 $min_spare_servers = 1;
97 $max_spare_servers = 4;
101 $opt_max_dequeue = 0 if $opt_testmode;
104 if (defined ($ENV{BOUND_SOCKETS
})) {
109 port
=> [ $opt_int_port, $opt_ext_port ],
111 min_servers
=> $min_servers,
112 max_servers
=> $max_servers,
113 min_spare_servers
=> $min_spare_servers,
114 max_spare_servers
=> $max_spare_servers,
115 max_requests
=> $max_requests,
116 serialize
=> 'flock',
117 max_dequeue
=> $opt_max_dequeue,
118 check_for_dequeue
=> $opt_dequeue_time,
120 pid_file
=> $opt_pidfile,
121 no_close_by_child
=> 1,
122 no_client_stdout
=> 1,
123 commandline
=> $opt_commandline,
126 $server_attr->{setsid
} = $daemonize if !$opt_testmode;
130 if (defined($opt_database)) {
131 $database = $opt_database;
133 $database = $opt_testmode ?
"Proxmox_testdb" : "Proxmox_ruledb";
136 $SIG{'__WARN__'} = sub {
140 syslog
('warning', "WARNING: %s", $t);
145 my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_;
148 sa_score
=> $queue->{sa_score
},
149 sa_hits
=> $queue->{sa_hits
},
150 sa_data
=> $queue->{sa_data
},
151 sa_max
=> $queue->{sa_max
}
155 'SUBJECT' => $entity->head->get ('subject', 0) || 'No Subject',
156 'RULE' => $rule->{name
},
157 'RULE_INFO' => $msginfo->{rule_info
},
158 'SENDER' => $msginfo->{sender
},
159 'SENDER_IP' => $msginfo->{xforward
}->{addr
},
160 'TARGETS' => join (', ', @$targets),
161 'RECEIVERS' => join (', ', @{$msginfo->{targets
}}),
162 'SPAMLEVEL' => $spaminfo->{sa_score
},
163 'SPAMSTARS' => '*' x
(($spaminfo->{sa_score
} || 0) > 100 ?
100 : $spaminfo->{sa_score
} || 0),
164 'ADMIN' => $self->{pmg_cfg
}->get('admin', 'email'),
165 'HOST' => $msginfo->{hostname
},
166 'DOMAIN' => $msginfo->{domain
},
167 'FQDN' => $msginfo->{fqdn
},
168 'MSGID' => $queue->{msgid
},
169 'VERSION' => PMG
::pmgcfg
::package() . "/" . PMG
::pmgcfg
::release
() . "/" . PMG
::pmgcfg
::repoid
(),
172 $vars->{__spaminfo
} = $spaminfo;
175 if ($queue->{vinfo_clam
} || $queue->{vinfo_avast
} || $queue->{vinfo_custom
}) {
176 $vars->{'VIRUS_INFO'} = "Virus Info:";
177 $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam
};
178 $vars->{'VIRUS_INFO'} .= " avast: $queue->{vinfo_avast}" if $queue->{vinfo_avast
};
179 $vars->{'VIRUS_INFO'} .= " custom: $queue->{vinfo_custom}" if $queue->{vinfo_custom
};
181 $vars->{'VIRUS_INFO'} = '';
184 if ($queue->{vinfo
}) {
185 $vars->{'VIRUS_INFO'} = "Virus Info: $queue->{vinfo}\n";
187 $vars->{'VIRUS_INFO'} = '';
191 $vars->{'SPAM_HITS'} = $spaminfo->{sa_hits
};
193 $vars->{'SPAM_INFO'} = '';
194 my $sscores = $spaminfo->{sa_data
};
196 if (defined ($sscores) && @$sscores != -1) {
199 $sa_text = "Spam detection results: 100\n";
201 $sa_text = "Spam detection results: $spaminfo->{sa_score}\n";
204 foreach my $s (@$sscores) {
206 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
},
207 1, $s->{desc
} || '-');
209 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
},
210 $s->{score
}, $s->{desc
} || '-');
213 $vars->{'SPAM_INFO'} = $sa_text;
217 delete ($vars->{'ADMIN'});
218 #delete ($vars->{'SPAM_INFO'});
225 my ($self, $queue, $msginfo, $entity, $ldap) = @_;
231 my $matching_rules = [];
233 my $rulecache = $self->{rulecache
};
234 my $rules = $rulecache->rules ();
235 my $dbh = $self->{ruledb
}->{dbh
};
237 # first, we remove all conditional written 'X-' header attributes
238 foreach my $rule (@$rules) {
239 next if !$rule->{active
};
240 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
241 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
243 my $actions = $rulecache->get_actions ($rule->{id
});
245 foreach my $action (@$actions) {
246 if ($action->isa ("PMG::RuleDB::ModField")) {
247 my $fname = $action->{field
};
248 next if $fname !~ m/^X-/i;
249 $entity->head->delete($fname);
255 foreach my $rule (@$rules) {
256 next if !$rule->{active
};
257 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
258 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
260 # match from, when and what classes (not target dependent)
261 if (!($rulecache->from_match ($rule->{id
}, $msginfo->{sender
}, $msginfo->{xforward
}->{addr
}, $ldap) &&
262 $rulecache->when_match ($rule->{id
}, time))) {
266 $rule_marks{$rule->{id
}} =
267 $rulecache->what_match ($rule->{id
}, $queue, $entity, $msginfo, $dbh);
269 $rule_actions{$rule->{id
}} = $rulecache->get_actions ($rule->{id
});
270 my $fin = $rulecache->final ($rule->{id
});
273 foreach my $target (@{$msginfo->{targets
}}) {
274 next if $final->{$target};
275 next if !defined ($rule_marks{$rule->{id
}});
276 next if !defined ($rule_marks{$rule->{id
}}->{$target});
277 next if !defined ($rule_marks{$rule->{id
}}->{$target}->{marks
});
278 next if !$rulecache->to_match ($rule->{id
}, $target, $ldap);
280 $final->{$target} = $fin;
282 push @{$rule_targets{$rule->{id
}}}, $target;
286 # Compute rule_info (summary about matching rule)
287 # this can be used for debugging
289 foreach my $rule (@$rules) {
290 next if !$rule_targets{$rule->{id
}};
292 push @$matching_rules, $rule->{id
};
294 $rule_info .= "Rule: $rule->{name}\n";
296 foreach my $target (@{$rule_targets{$rule->{id
}}}) {
297 $rule_info .= " Receiver: $target\n";
299 foreach my $action (@{$rule_actions{$rule->{id
}}}) {
300 $rule_info .= " Action: " . $action->short_desc () . "\n";
304 $msginfo->{rule_info
} = $rule_info;
306 if ($msginfo->{testmode
}) {
307 my $vars = $self->get_prox_vars($queue, $entity, $msginfo, undef, [], undef);
308 my $fh = $msginfo->{test_fh
};
309 print $fh $rule_info;
314 my $mod_group = PMG
::ModGroup-
>new($entity, $msginfo->{targets
});
316 foreach my $rule (@$rules) {
317 my $targets = $rule_targets{$rule->{id
}};
321 foreach my $t (@$targets) {
322 if ($rule_marks{$rule->{id
}}->{$t} && $rule_marks{$rule->{id
}}->{$t}->{spaminfo
}) {
323 $spaminfo = $rule_marks{$rule->{id
}}->{$t}->{spaminfo
};
324 # we assume spam info is the same for all matching targets
329 my $vars = $self->get_prox_vars ($queue, $entity, $msginfo, $rule,
330 $rule_targets{$rule->{id
}}, $spaminfo);
334 sort {$a->priority <=> $b->priority} @{$rule_actions{$rule->{id
}}};
336 foreach my $action (@sorted_actions) {
337 $action->execute ($queue, $self->{ruledb
}, $mod_group,
338 $rule_targets{$rule->{id
}},
339 $msginfo, $vars, $rule_marks{$rule->{id
}}->{marks
}, $ldap);
340 last if $action->final;
344 # we deliver all mail not matched by any rule
345 # (default action = accept)
347 foreach my $target (@{$msginfo->{targets
}}) {
348 next if $final->{$target};
350 push @$unmatched, $target;
354 my $accept = PMG
::RuleDB
::Accept-
>new ();
355 $accept->execute ($queue, $self->{ruledb
}, $mod_group, $unmatched,
356 $msginfo, { RULE
=> 'default-accept' }, undef);
359 return $matching_rules;
362 # reload ruledb and pmg config
365 my $prop = $self->{server
};
367 if ($self->{ruledb
}) {
368 $self->log (0, "reloading configuration $database");
369 $self->{ruledb
}->close ();
372 $self->{pmg_cfg
} = PMG
::Config-
>new();
373 $self->{cinfo
} = PVE
::INotify
::read_file
("cluster.conf");
375 # create spool directories
376 PMG
::MailQueue
::create_spooldirs
($self->{cinfo
}->{local}->{cid
});
379 my $dbh = PMG
::DBTools
::open_ruledb
($database);
380 $self->{ruledb
} = PMG
::RuleDB-
>new ($dbh);
383 $self->{rulecache
} = PMG
::RuleCache-
>new ($self->{ruledb
});
389 sleep (10); # reduce restart rate when postgres is down
394 my $ldapconf = PVE
::INotify
::read_file
('pmg-ldap.conf');
395 $self->{ldap
} = PMG
::LDAPSet-
>new_from_ldap_cfg($ldapconf, 1);
397 $self->{reload_config
} = 0;
409 my ($self, $level, $msg, @therest) = @_;
410 my $prop = $self->{server
};
412 return if $level =~ /^\d+$/ && $level > $prop->{log_level
};
414 $level = $syslog_map->{$level} || $level;
416 syslog
($level, $msg, @therest);
418 syslog
($level, $msg);
424 my $prop = $self->{server
};
426 $prop->{log_level
} = 3;
428 $self->log (0, "Filter daemon (re)started (max. $max_servers processes)");
430 eval { PMG
::MailQueue
::cleanup_active
(); };
431 $self->log (0, "Cleanup failures: $@") if $@;
433 my $sig_set = POSIX
::SigSet-
>new;
434 $sig_set->addset (&POSIX
::SIGHUP
);
435 $sig_set->addset (&POSIX
::SIGCHLD
);
436 my $old_sig_set = POSIX
::SigSet-
>new();
438 sigprocmask
(SIG_UNBLOCK
, $sig_set, $old_sig_set);
440 my ($backup_umask) = umask;
442 my $pmg_cfg = PMG
::Config-
>new();
443 $pmg_cfg->write_smtp_filter_config();
445 # Note: you need to restart the daemon when you change 'rbl_checks'
446 my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');
448 $self->{sa
} = Mail
::SpamAssassin-
>new ({
450 local_tests_only
=> $opt_testmode || !$rbl_checks,
451 home_dir_for_helpers
=> '/root',
452 userstate_dir
=> '/root/.spamassassin',
453 dont_copy_prefs
=> 1,
454 stop_at_threshold
=> 0,
457 $self->{sa
}->compile_now;
459 alarm (0); # SA forgets to clear alarm in some cases
460 umask ($backup_umask);
461 initlog
($prog_name, 'mail');
464 # reloading server configuration
465 if (defined $prop->{children
}) {
466 foreach my $pid (keys %{$prop->{children
}}) {
467 kill (10, $pid); # SIGUSR1 childs
473 sub child_init_hook
{
476 $0 = "$prog_name child";
478 # $self->log (3, "init child");
481 $self->load_config ();
486 $self->child_finish_hook;
491 $self->{reload_config
} = 1;
495 sub child_finish_hook
{
498 # $self->log (3, "finish child");
499 $self->{ruledb
}->close () if $self->{ruledb
};
502 my $last_dequeue_time = 0;
509 # do database maintainance here
511 # this is called every 30 secends
513 PMG
::Utils
::update_node_status_rrd
();
516 $self->log(0, "ERROR: $err");
521 my $tdiff = $ctime - $last_dequeue_time;
523 # return if tdiff less than 2 minutes
524 return if $tdiff < 2*60;
526 $last_dequeue_time = $ctime;
528 $self->log (2, "starting database maintainance");
530 my ($csec, $usec) = gettimeofday
();
532 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
537 $dbh = PMG
::DBTools
::open_ruledb
($database);
540 $self->log (0, "ERROR: $err");
545 PMG
::Statistic
::update_stats
($dbh, $cinfo);
549 my ($csec_end, $usec_end) = gettimeofday
();
550 my $ptime = int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
553 $self->log (0, $err);
555 $self->log (2, "end database maintainance ($ptime ms)");
558 $dbh->disconnect() if $dbh;
563 my ($self, $unpack, $entity, $msginfo, $queue) = @_;
568 if (($magic = $entity->{PMX_magic_ct
}) &&
569 ($path = $entity->{PMX_decoded_path
})) {
571 my $filename = basename
($path);
573 if (PMG
::Unpack
::is_archive
($magic)) {
574 $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)");
576 my $start = [gettimeofday
];
578 $unpack->{mime
} = {};
581 $unpack->unpack_archive ($path, $magic);
584 $self->log (3, "$queue->{logid}: unpack failed - $@") if $@;
586 $entity->{PMX_content_types
} = $unpack->{mime
};
589 my $types = join (", ", sort keys (%{$entity->{PMX_content_types
}}));
590 my $fh = $msginfo->{test_fh
};
591 $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/;
592 print $fh "Types:$filename: $types\n" if $types;
595 my $elapsed = int(tv_interval
($start) * 1000);
597 $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)");
601 foreach my $part ($entity->parts) {
602 $self->unpack_entity ($unpack, $part, $msginfo, $queue);
608 my ($self, $smtp) = @_;
610 my ($csec, $usec) = gettimeofday
();
614 my $pmg_cfg = $self->{pmg_cfg
};
615 my $ldap = $self->{ldap
};
616 my $cinfo = $self->{cinfo
};
617 my $lcid = $cinfo->{local}->{cid
};
619 $msginfo->{test_fh
} = PMG
::AtomicFile-
>new("testresult.out", "w")
622 $msginfo->{trusted
} = $self->{trusted
};
625 # PHASE 1 - save incoming mail (already done)
628 $queue = $smtp->{queue
};
629 $queue->{sa
} = $self->{sa
};
630 $queue->{clamav_heuristic_score
} =
631 $opt_testmode ?
100 : $pmg_cfg->get('spam', 'clamav_heuristic_score');
633 $queue->{lic_valid
} = 1;
638 $msginfo->{testmode
} = $opt_testmode;
639 $msginfo->{sender
} = $smtp->{from
};
640 $msginfo->{xforward
} = $smtp->{xforward
};
641 $msginfo->{targets
} = $smtp->{to
};
643 my $dkim_sign = $msginfo->{trusted
} && $pmg_cfg->get('admin', 'dkim_sign');
645 $msginfo->{dkim
}->{sign
} = $dkim_sign;
646 $msginfo->{dkim
}->{sign_all
} = $pmg_cfg->get('admin', 'dkim_sign_all_mail');
647 $msginfo->{dkim
}->{selector
} = $pmg_cfg->get('admin', 'dkim_selector');
650 $msginfo->{hostname
} = PVE
::INotify
::nodename
();
651 my $resolv = PVE
::INotify
::read_file
('resolvconf');
653 $msginfo->{domain
} = $resolv->{search
};
654 $msginfo->{fqdn
} = $msginfo->{hostname
};
655 $msginfo->{fqdn
} .= ".$msginfo->{domain}" if $msginfo->{domain
};
656 $msginfo->{lcid
} = $lcid;
658 # $msginfo->{targets} is case sensitive,
659 # but pmail is always lower case!
661 foreach my $t (@{$msginfo->{targets
}}) {
663 if ($ldap && ($res = $ldap->account_info ($t))) {
664 $msginfo->{pmail
}->{$t} = $res->{pmail
};
666 $msginfo->{pmail
}->{$t} = lc ($t);
670 # PHASE 2 - parse mail
673 my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles');
675 my $entity = $queue->parse_mail($maxfiles);
677 $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid
});
679 # PHASE 3 - run external content analyzers
680 # (SPAM analyzer is run on demand later)
681 # on error: log error messages
683 # run custom script first
684 my ($vinfo, $custom_spaminfo) = PMG
::Utils
::analyze_custom_check
(
685 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
687 # test for virus if none found
688 if (!defined($vinfo)) {
689 $vinfo = PMG
::Utils
::analyze_virus
(
690 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
692 if ($vinfo && $vinfo =~ m/^Heuristics\.(.+)$/) {
694 $queue->{clamav_heuristic
} = $hit;
698 $queue->{vinfo
} = $vinfo;
700 # always add this headers to incoming mails
701 # to enable user to report false negatives
702 if (!$msginfo->{trusted
}) {
703 if ($queue->{vinfo
}) {
704 $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo
});
708 # we unpack after virus scanning, because this is more secure.
709 # This way virus scanners gets the whole mail files and are able
710 # to detect phishing signature for example - which would not work
711 # if we decompose first and only analyze the decomposed attachments.
712 # Disadvantage is that we need to unpack more than
713 # once (bad performance).
715 # should we scan content types inside archives
717 my $rulecache = $self->{rulecache
};
719 my $scan_archives = 0;
721 if (($rulecache->{archivefilter_in
} && !$msginfo->{trusted
}) ||
722 ($rulecache->{archivefilter_out
} && $msginfo->{trusted
})) {
726 if ($scan_archives && !$queue->{vinfo
}) {
728 # unpack all archives - determine contained content types
730 my $decdir = $queue->{dumpdir
} . "/__decoded_archives";
733 my $start = [gettimeofday
];
738 # limits: We use clamav limit for maxfiles, and scan
739 # only 4 levels, timeout of 30 seconds
741 $unpack = PMG
::Unpack-
>new (
744 ctonly
=> 1, # only detect CTs
746 maxfiles
=> $maxfiles);
748 $self->unpack_entity ($unpack, $entity, $msginfo, $queue);
753 $unpack->cleanup() if $unpack;
755 my $elapsed = int(tv_interval
($start) * 1000);
758 $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err");
762 # PHASE 4 - apply rules
763 # on error: exit (cleanup process should do the rest)
764 $msginfo->{maxspamsize
} = $pmg_cfg->get('spam', 'maxspamsize');
765 if ($msginfo->{maxspamsize
} <= 1024*64) {
766 $msginfo->{maxspamsize
} = 1024*64;
769 if ($msginfo->{trusted
}) {
770 my $hide = $pmg_cfg->get('mail', 'hide_received');
771 $entity->head->delete("Received") if $hide;
774 $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap);
779 $self->{errors
} = $queue->{errors
};
782 $self->{errors
} = 1 if $err;
788 my ($csec_end, $usec_end) = gettimeofday
();
790 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
792 # PHASE 5 - log statistics
793 # on error: log error messages
796 my $dbh = $self->{ruledb
}->{dbh
};
799 foreach my $rid (@$matching_rules) {
800 $where .= $where ?
" OR ID = $rid" : "ID = $rid";
803 $dbh->do ("UPDATE Rule SET Count = Count + 1 WHERE $where") if $where;
805 my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " .
806 "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" .
807 "$lcid, currval('cstatistic_id_seq'), currval('cstatistic_id_seq'),";
809 $insert_cmds .= $queue->{rtime
} . ',';
810 $insert_cmds .= $queue->{bytes
} . ',';
811 $insert_cmds .= $dbh->quote($msginfo->{trusted
} ?
0 : 1) . ',';
812 $insert_cmds .= ($queue->{sa_score
} || 0) . ',';
813 $insert_cmds .= $dbh->quote($queue->{vinfo
}) . ',';
814 $insert_cmds .= $time_total . ',';
815 $insert_cmds .= $dbh->quote($msginfo->{sender
}) . ');';
817 foreach my $r (@{$msginfo->{targets
}}) {
818 my $tmp = $dbh->quote($r);
819 my $blocked = $queue->{status
}->{$r} eq 'blocked' ?
1 : 0;
820 $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " .
821 "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); ";
824 $dbh->do($insert_cmds);
826 # save $err (because log clears $@)
829 $time_total = $time_total/1000;
831 my $ptspam = ($queue->{ptime_spam
} || 0)/1000;
832 my $ptclam = ($queue->{ptime_clam
} || 0)/1000;
833 my $ptcustom = ($queue->{ptime_custom
} || 0)/1000;
835 $self->log(3, "$queue->{logid}: processing time: ${time_total} seconds ($ptspam, $ptclam, $ptcustom)");
837 $msginfo->{test_fh
}->close if $opt_testmode;
842 my $initial_memory_usage;
844 sub process_request
{
846 my $prop = $self->{server
};
847 my $sock = $prop->{client
};
851 # make sure the ldap cache is up to date
852 $self->{ldap
}->update (1);
854 $self->load_config() if $self->{reload_config
};
856 $self->{trusted
} = 0;
857 if ($prop->{sockport
} == $opt_int_port && !$opt_untrusted) {
858 $self->{trusted
} = 1;
861 my $smtp = PMG
::SMTP-
>new ($sock);
863 my $maxcount = $max_requests - $prop->{requests
};
865 my $count = $smtp->loop (\
&handle_smtp
, $self, $maxcount);
867 $prop->{requests
} += $count - 1;
873 $self->log (0, $err) if $err;
875 kill(15, $prop->{ppid
}) if $opt_testmode;
877 my $mem = PVE
::ProcFSTools
::read_memory_usage
();
878 if (!defined($initial_memory_usage) || ($prop->{requests
} < 10)) {
879 $initial_memory_usage = $mem->{resident
};
883 $self->log (0, "memory usage: $mem->{size} bytes");
885 if ($self->{errors
}) {
886 $self->log (0, "fast exit because of errors (free $mem->{size} bytes)");
889 my $diff = $mem->{resident
} - $initial_memory_usage;
890 if ($diff > 5*1024*1024) {
891 $self->log (0, "fast exit to reduce server load (free $diff bytes)");
897 $self->done (1) if $err;
900 # test sig_hup with: for ((;;)) ;do kill -HUP `cat /run/${prog_name}.pid`; done;
901 # wrapper to avoid multiple calls to sig_hup
904 my $prop = $self->{server
};
906 return if defined ($prop->{_HUP
}); # do not call twice
908 $self->SUPER::sig_hup
();
911 sub restart_close_hook
{
914 my $sig_set = POSIX
::SigSet-
>new;
915 $sig_set->addset (&POSIX
::SIGHUP
);
916 $sig_set->addset (&POSIX
::SIGCHLD
); # to avoid zombies
917 my $old_sig_set = POSIX
::SigSet-
>new();
919 sigprocmask
(SIG_BLOCK
, $sig_set, $old_sig_set);
922 sub pre_server_close_hook
{
924 my $prop = $self->{server
};
926 if (defined $prop->{_HUP
}) {
927 undef $prop->{pid_file_unlink
};
930 if (defined $prop->{children
}) {
931 foreach my $pid (keys %{$prop->{children
}}) {
932 kill (1, $pid); # HUP childs
936 # nicely shutdown childs (give them max 30 seconds to shut down)
937 my $previous_alarm = alarm(30);
939 local $SIG{ALRM
} = sub { die "Timed Out!\n" };
942 1 while ((($pid = waitpid (-1, 0)) > 0) || ($! == EINTR
));
944 alarm(0); # avoid race
946 alarm ($previous_alarm);
949 # initialize mime system before fork
950 xdg_mime_get_mime_type_for_file
($0);
953 server
=> $server_attr,
956 if (!$opt_testmode) {
963 my $sender ='sender@proxtest.com';
964 my $targets = ['target1@proxtest.com',
965 'target2@proxtest.com',
966 'target3@proxtest.com'];
970 $smtp = Net
::SMTP-
>new ('127.0.0.1', Port
=> 10023);
975 # syslog ('info', "connected to " . $smtp->domain);
977 $smtp->mail ($sender);
978 $smtp->to (@$targets);
982 open (TMP
, $opt_testmode) ||
983 die "unable to upen file '$opt_testmode' - $! :ERROR";
985 $smtp->datasend ($_);
989 $smtp->datasend ("\n");
1002 pmg-smtp-filter - the Proxmox mail filter
1006 pmg-smtp-filter [-u] [-t testfile]
1010 Documentation is available at www.proxmox.com