9 use Time
::HiRes qw
(usleep gettimeofday tv_interval
);
10 use POSIX
qw(:sys_wait_h errno_h signal_h);
13 use MIME
::WordDecoder
qw(mime_to_perl_string);
15 use Net
::Server
::PreFork
;
16 use Net
::Server
::SIG
qw(register_sig check_sigs);
27 use Mail
::SpamAssassin
;
28 use Mail
::SpamAssassin
::NetSet
;
33 use PMG
::ClusterConfig
;
50 use base
qw(Net::Server::PreFork);
52 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
54 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
56 my $opt_commandline = [$0, @ARGV];
57 my $opt_max_dequeue = 1;
58 my $opt_dequeue_time = 30;
60 my $opt_ext_port = 10024;
61 my $opt_int_port = 10023;
62 my $opt_inject_port = 10025;
69 my $prog_name = 'pmg-smtp-filter';
71 initlog
($prog_name, 'mail');
73 if (!GetOptions
('testmode=s' => \
$opt_testmode,
74 'pidfile=s' => \
$opt_pidfile,
75 'untrusted' => \
$opt_untrusted,
76 'database=s' => \
$opt_database)) {
81 $opt_pidfile = "/run/${prog_name}.pid" if !$opt_pidfile;
85 my $min_spare_servers = 0;
86 my $max_spare_servers = 0;
90 my $pmg_cfg = PMG
::Config-
>new();
92 my $demo = $pmg_cfg->get('admin', 'demo');
95 syslog
('info', 'demo mode detected - not starting server');
99 $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2;
101 $min_spare_servers = 1;
102 $max_spare_servers = 4;
106 $opt_max_dequeue = 0 if $opt_testmode;
109 if (defined ($ENV{BOUND_SOCKETS
})) {
114 port
=> [ $opt_int_port, $opt_ext_port ],
116 min_servers
=> $min_servers,
117 max_servers
=> $max_servers,
118 min_spare_servers
=> $min_spare_servers,
119 max_spare_servers
=> $max_spare_servers,
120 max_requests
=> $max_requests,
121 serialize
=> 'flock',
122 max_dequeue
=> $opt_max_dequeue,
123 check_for_dequeue
=> $opt_dequeue_time,
125 pid_file
=> $opt_pidfile,
126 no_close_by_child
=> 1,
127 no_client_stdout
=> 1,
128 commandline
=> $opt_commandline,
131 $server_attr->{setsid
} = $daemonize if !$opt_testmode;
135 if (defined($opt_database)) {
136 $database = $opt_database;
138 $database = $opt_testmode ?
"Proxmox_testdb" : "Proxmox_ruledb";
141 $SIG{'__WARN__'} = sub {
145 syslog
('warning', "WARNING: %s", $t);
150 my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_;
153 sa_score
=> $queue->{sa_score
},
154 sa_hits
=> $queue->{sa_hits
},
155 sa_data
=> $queue->{sa_data
},
156 sa_max
=> $queue->{sa_max
}
160 'SUBJECT' => PMG
::Utils
::decode_rfc1522
($entity->head->get ('subject', 0) || 'No Subject'),
161 'RULE' => $rule->{name
},
162 'RULE_INFO' => $msginfo->{rule_info
},
163 'SENDER' => $msginfo->{sender
},
164 'SENDER_IP' => $msginfo->{xforward
}->{addr
},
165 'TARGETS' => join (', ', @$targets),
166 'RECEIVERS' => join (', ', @{$msginfo->{targets
}}),
167 'SPAMLEVEL' => $spaminfo->{sa_score
},
168 'SPAMSTARS' => '*' x
(($spaminfo->{sa_score
} || 0) > 100 ?
100 : $spaminfo->{sa_score
} || 0),
169 'ADMIN' => $self->{pmg_cfg
}->get('admin', 'email'),
170 'HOST' => $msginfo->{hostname
},
171 'DOMAIN' => $msginfo->{domain
},
172 'FQDN' => $msginfo->{fqdn
},
173 'MSGID' => $queue->{msgid
},
174 'VERSION' => PMG
::pmgcfg
::package() . "/" . PMG
::pmgcfg
::release
() . "/" . PMG
::pmgcfg
::repoid
(),
177 $vars->{__spaminfo
} = $spaminfo;
180 if ($queue->{vinfo_clam
} || $queue->{vinfo_avast
} || $queue->{vinfo_custom
}) {
181 $vars->{'VIRUS_INFO'} = "Virus Info:";
182 $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam
};
183 $vars->{'VIRUS_INFO'} .= " avast: $queue->{vinfo_avast}" if $queue->{vinfo_avast
};
184 $vars->{'VIRUS_INFO'} .= " custom: $queue->{vinfo_custom}" if $queue->{vinfo_custom
};
186 $vars->{'VIRUS_INFO'} = '';
189 if ($queue->{vinfo
}) {
190 $vars->{'VIRUS_INFO'} = "Virus Info: $queue->{vinfo}\n";
192 $vars->{'VIRUS_INFO'} = '';
196 $vars->{'SPAM_HITS'} = $spaminfo->{sa_hits
};
198 $vars->{'SPAM_INFO'} = '';
199 my $sscores = $spaminfo->{sa_data
};
201 if (defined ($sscores) && @$sscores != -1) {
204 $sa_text = "Spam detection results: 100\n";
206 $sa_text = "Spam detection results: $spaminfo->{sa_score}\n";
209 foreach my $s (@$sscores) {
211 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
}, 1, $s->{desc
} || '-');
213 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
}, $s->{score
}, $s->{desc
} || '-');
216 $vars->{'SPAM_INFO'} = $sa_text;
220 delete ($vars->{'ADMIN'});
221 #delete ($vars->{'SPAM_INFO'});
228 my ($self, $queue, $msginfo, $entity, $ldap) = @_;
234 my $matching_rules = [];
236 my $rulecache = $self->{rulecache
};
237 my $rules = $rulecache->rules ();
238 my $dbh = $self->{ruledb
}->{dbh
};
240 # first, we remove all conditional written 'X-' header attributes
241 foreach my $rule (@$rules) {
242 next if !$rule->{active
};
243 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
244 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
246 my $actions = $rulecache->get_actions ($rule->{id
});
248 foreach my $action (@$actions) {
249 if ($action->isa ("PMG::RuleDB::ModField")) {
250 my $fname = $action->{field
};
251 next if $fname !~ m/^X-/i;
252 $entity->head->delete($fname);
258 foreach my $rule (@$rules) {
259 next if !$rule->{active
};
260 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
261 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
263 # match from, when and what classes (not target dependent)
264 if (!($rulecache->from_match ($rule->{id
}, $msginfo->{sender
}, $msginfo->{xforward
}->{addr
}, $ldap) &&
265 $rulecache->when_match ($rule->{id
}, time))) {
269 $rule_marks{$rule->{id
}} =
270 $rulecache->what_match ($rule->{id
}, $queue, $entity, $msginfo, $dbh);
272 $rule_actions{$rule->{id
}} = $rulecache->get_actions ($rule->{id
});
273 my $fin = $rulecache->final ($rule->{id
});
276 foreach my $target (@{$msginfo->{targets
}}) {
277 next if $final->{$target};
278 next if !defined ($rule_marks{$rule->{id
}});
279 next if !defined ($rule_marks{$rule->{id
}}->{$target});
280 next if !defined ($rule_marks{$rule->{id
}}->{$target}->{marks
});
281 next if !$rulecache->to_match ($rule->{id
}, $target, $ldap);
283 $final->{$target} = $fin;
285 push @{$rule_targets{$rule->{id
}}}, $target;
289 # Compute rule_info (summary about matching rule)
290 # this can be used for debugging
292 foreach my $rule (@$rules) {
293 next if !$rule_targets{$rule->{id
}};
295 push @$matching_rules, $rule->{id
};
297 $rule_info .= "Rule: $rule->{name}\n";
299 foreach my $target (@{$rule_targets{$rule->{id
}}}) {
300 $rule_info .= " Receiver: $target\n";
302 foreach my $action (@{$rule_actions{$rule->{id
}}}) {
303 $rule_info .= " Action: " . $action->short_desc () . "\n";
307 $msginfo->{rule_info
} = $rule_info;
309 if ($msginfo->{testmode
}) {
310 my $vars = $self->get_prox_vars($queue, $entity, $msginfo, undef, [], undef);
311 my $fh = $msginfo->{test_fh
};
312 print $fh $rule_info;
317 my $mod_group = PMG
::ModGroup-
>new($entity, $msginfo->{targets
});
319 foreach my $rule (@$rules) {
320 my $targets = $rule_targets{$rule->{id
}};
324 foreach my $t (@$targets) {
325 if ($rule_marks{$rule->{id
}}->{$t} && $rule_marks{$rule->{id
}}->{$t}->{spaminfo
}) {
326 $spaminfo = $rule_marks{$rule->{id
}}->{$t}->{spaminfo
};
327 # we assume spam info is the same for all matching targets
332 my $vars = $self->get_prox_vars (
333 $queue, $entity, $msginfo, $rule, $rule_targets{$rule->{id
}}, $spaminfo);
335 my @sorted_actions = sort {$a->priority <=> $b->priority} @{$rule_actions{$rule->{id
}}};
337 foreach my $action (@sorted_actions) {
339 $queue, $self->{ruledb
}, $mod_group, $rule_targets{$rule->{id
}}, $msginfo, $vars,
340 $rule_marks{$rule->{id
}}->{marks
}, $ldap
342 last if $action->final;
346 # we deliver all mail not matched by any rule (default action = accept)
348 foreach my $target (@{$msginfo->{targets
}}) {
349 next if $final->{$target};
351 push @$unmatched, $target;
355 my $accept = PMG
::RuleDB
::Accept-
>new ();
356 $accept->execute ($queue, $self->{ruledb
}, $mod_group, $unmatched,
357 $msginfo, { RULE
=> 'default-accept' }, undef);
360 return $matching_rules;
363 # reload ruledb and pmg config
366 my $prop = $self->{server
};
368 if ($self->{ruledb
}) {
369 $self->log (0, "reloading configuration $database");
370 $self->{ruledb
}->close ();
373 $self->{pmg_cfg
} = PMG
::Config-
>new();
374 $self->{cinfo
} = PVE
::INotify
::read_file
("cluster.conf");
376 # create spool directories
377 PMG
::MailQueue
::create_spooldirs
($self->{cinfo
}->{local}->{cid
});
380 my $dbh = PMG
::DBTools
::open_ruledb
($database);
381 $self->{ruledb
} = PMG
::RuleDB-
>new ($dbh);
384 $self->{rulecache
} = PMG
::RuleCache-
>new ($self->{ruledb
});
390 sleep (10); # reduce restart rate when postgres is down
395 my $ldapconf = PVE
::INotify
::read_file
('pmg-ldap.conf');
396 $self->{ldap
} = PMG
::LDAPSet-
>new_from_ldap_cfg($ldapconf, 1);
398 $self->{reload_config
} = 0;
410 my ($self, $level, $msg, @therest) = @_;
411 my $prop = $self->{server
};
413 return if $level =~ /^\d+$/ && $level > $prop->{log_level
};
415 $level = $syslog_map->{$level} || $level;
417 syslog
($level, $msg, @therest);
419 syslog
($level, $msg);
425 my $prop = $self->{server
};
427 $prop->{log_level
} = 3;
429 $self->log (0, "Filter daemon (re)started (max. $max_servers processes)");
431 eval { PMG
::MailQueue
::cleanup_active
(); };
432 $self->log (0, "Cleanup failures: $@") if $@;
434 my $sig_set = POSIX
::SigSet-
>new;
435 $sig_set->addset (&POSIX
::SIGHUP
);
436 $sig_set->addset (&POSIX
::SIGCHLD
);
437 my $old_sig_set = POSIX
::SigSet-
>new();
439 sigprocmask
(SIG_UNBLOCK
, $sig_set, $old_sig_set);
441 my ($backup_umask) = umask;
443 my $pmg_cfg = PMG
::Config-
>new();
444 $pmg_cfg->write_smtp_filter_config();
446 # Note: you need to restart the daemon when you change 'rbl_checks'
447 my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');
449 $self->{sa
} = Mail
::SpamAssassin-
>new ({
451 local_tests_only
=> $opt_testmode || !$rbl_checks,
452 home_dir_for_helpers
=> '/root',
453 userstate_dir
=> '/root/.spamassassin',
454 dont_copy_prefs
=> 1,
455 stop_at_threshold
=> 0,
458 $self->{sa
}->compile_now;
460 alarm (0); # SA forgets to clear alarm in some cases
461 umask ($backup_umask);
462 initlog
($prog_name, 'mail');
465 # reloading server configuration
466 if (defined $prop->{children
}) {
467 foreach my $pid (keys %{$prop->{children
}}) {
468 kill (10, $pid); # SIGUSR1 children
474 sub child_init_hook
{
477 $0 = "$prog_name child";
479 # $self->log (3, "init child");
482 $self->load_config ();
487 $self->child_finish_hook;
492 $self->{reload_config
} = 1;
496 sub child_finish_hook
{
499 # $self->log (3, "finish child");
500 $self->{ruledb
}->close () if $self->{ruledb
};
503 my $last_dequeue_time = 0;
510 # do database maintenance here, this is called every 30 secends
512 eval { PMG
::Utils
::update_node_status_rrd
() };
514 $self->log(0, "ERROR: $err");
519 my $tdiff = $ctime - $last_dequeue_time;
521 # return if tdiff less than 2 minutes
522 return if $tdiff < 2*60;
523 $last_dequeue_time = $ctime;
525 $self->log (2, "starting database maintenance");
527 my ($csec, $usec) = gettimeofday
();
529 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
531 my $dbh = eval { PMG
::DBTools
::open_ruledb
($database) };
533 $self->log (0, "ERROR: $err");
537 eval { PMG
::Statistic
::update_stats
($dbh, $cinfo) };
541 $self->log (0, $err);
543 my ($csec_end, $usec_end) = gettimeofday
();
544 my $ptime = int (($csec_end - $csec) * 1000 + ($usec_end - $usec) / 1000);
545 $self->log (2, "end database maintenance ($ptime ms)");
548 $dbh->disconnect() if $dbh;
553 my ($self, $unpack, $entity, $msginfo, $queue) = @_;
555 my ($magic, $path) = $entity->@{'PMX_magic_ct', 'PMX_decoded_path'};
557 if ($magic && $path) {
558 my $filename = basename
($path);
560 if (PMG
::Unpack
::is_archive
($magic)) {
561 $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)");
563 my $start = [gettimeofday
];
565 $unpack->{mime
} = {};
567 eval { $unpack->unpack_archive ($path, $magic) };
568 $self->log (3, "$queue->{logid}: unpack failed - $@") if $@;
570 $entity->{PMX_content_types
} = $unpack->{mime
};
571 $entity->{PMX_filenames
} = $unpack->{filenames
};
574 my $types = join (", ", sort keys (%{$entity->{PMX_content_types
}}));
575 my $fh = $msginfo->{test_fh
};
576 $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/;
577 print $fh "Types:$filename: $types\n" if $types;
580 my $elapsed = int(tv_interval
($start) * 1000);
581 $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)");
585 for my $part ($entity->parts) {
586 $self->unpack_entity ($unpack, $part, $msginfo, $queue);
592 my ($self, $smtp) = @_;
594 my ($csec, $usec) = gettimeofday
();
598 my $pmg_cfg = $self->{pmg_cfg
};
599 my $ldap = $self->{ldap
};
600 my $cinfo = $self->{cinfo
};
601 my $lcid = $cinfo->{local}->{cid
};
603 $msginfo->{test_fh
} = PMG
::AtomicFile-
>new("testresult.out", "w") if $opt_testmode;
605 $msginfo->{trusted
} = $self->{trusted
};
608 # PHASE 1 - save incoming mail (already done)
611 $queue = $smtp->{queue
};
612 $queue->{sa
} = $self->{sa
};
613 $queue->{clamav_heuristic_score
} =
614 $opt_testmode ?
100 : $pmg_cfg->get('spam', 'clamav_heuristic_score');
616 $queue->{lic_valid
} = 1;
621 $msginfo->{testmode
} = $opt_testmode;
622 $msginfo->{sender
} = $smtp->{from
};
623 $msginfo->{xforward
} = $smtp->{xforward
};
624 $msginfo->{targets
} = $smtp->{to
};
625 $msginfo->{param
} = $smtp->{param
};
627 my $dkim_sign = $msginfo->{trusted
} && $pmg_cfg->get('admin', 'dkim_sign');
629 $msginfo->{dkim
}->{sign
} = $dkim_sign;
630 $msginfo->{dkim
}->{sign_all
} = $pmg_cfg->get('admin', 'dkim_sign_all_mail');
631 $msginfo->{dkim
}->{selector
} = $pmg_cfg->get('admin', 'dkim_selector');
634 $msginfo->{hostname
} = PVE
::INotify
::nodename
();
635 my $resolv = PVE
::INotify
::read_file
('resolvconf');
637 $msginfo->{domain
} = $resolv->{search
};
638 $msginfo->{fqdn
} = $msginfo->{hostname
};
639 $msginfo->{fqdn
} .= ".$msginfo->{domain}" if $msginfo->{domain
};
640 $msginfo->{lcid
} = $lcid;
642 # $msginfo->{targets} is case sensitive,
643 # but pmail is always lower case!
645 foreach my $t (@{$msginfo->{targets
}}) {
647 if ($ldap && ($res = $ldap->account_info ($t))) {
648 $msginfo->{pmail
}->{$t} = $res->{pmail
};
650 $msginfo->{pmail
}->{$t} = lc ($t);
654 # PHASE 2 - parse mail
657 my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles');
659 my $entity = $queue->parse_mail($maxfiles);
661 $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid
});
663 # PHASE 3 - run external content analyzers
664 # (SPAM analyzer is run on demand later)
665 # on error: log error messages
667 # run custom script first
668 my ($vinfo, $custom_spaminfo) = PMG
::Utils
::analyze_custom_check
(
669 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
671 # test for virus if none found
672 if (!defined($vinfo)) {
673 $vinfo = PMG
::Utils
::analyze_virus
(
674 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
676 if ($vinfo && $vinfo =~ m/^Heuristics\.(.+)$/) {
678 $queue->{clamav_heuristic
} = $hit;
682 $queue->{vinfo
} = $vinfo;
684 # always add this headers to incoming mails
685 # to enable user to report false negatives
686 if (!$msginfo->{trusted
}) {
687 if ($queue->{vinfo
}) {
688 $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo
});
692 # we unpack after virus scanning, because this is more secure.
693 # This way virus scanners gets the whole mail files and are able
694 # to detect phishing signature for example - which would not work
695 # if we decompose first and only analyze the decomposed attachments.
696 # Disadvantage is that we need to unpack more than
697 # once (bad performance).
699 # should we scan content types inside archives
701 my $rulecache = $self->{rulecache
};
703 my $scan_archives = 0;
705 if (($rulecache->{archivefilter_in
} && !$msginfo->{trusted
}) ||
706 ($rulecache->{archivefilter_out
} && $msginfo->{trusted
})) {
710 if ($scan_archives && !$queue->{vinfo
}) {
712 # unpack all archives - determine contained content types
714 my $decdir = $queue->{dumpdir
} . "/__decoded_archives";
717 my $start = [gettimeofday
];
722 # limits: We use clamav limit for maxfiles, and scan
723 # only 4 levels, timeout of 30 seconds
725 $unpack = PMG
::Unpack-
>new (
728 ctonly
=> 1, # only detect CTs
730 maxfiles
=> $maxfiles);
732 $self->unpack_entity ($unpack, $entity, $msginfo, $queue);
737 $unpack->cleanup() if $unpack;
739 my $elapsed = int(tv_interval
($start) * 1000);
742 $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err");
746 # PHASE 4 - apply rules
747 # on error: exit (cleanup process should do the rest)
748 $msginfo->{maxspamsize
} = $pmg_cfg->get('spam', 'maxspamsize');
749 if ($msginfo->{maxspamsize
} <= 1024*64) {
750 $msginfo->{maxspamsize
} = 1024*64;
753 if ($msginfo->{trusted
}) {
754 my $hide = $pmg_cfg->get('mail', 'hide_received');
755 $entity->head->delete("Received") if $hide;
758 $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap);
763 $self->{errors
} = $queue->{errors
};
766 $self->{errors
} = 1 if $err;
772 my ($csec_end, $usec_end) = gettimeofday
();
774 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
776 # PHASE 5 - log statistics
777 # on error: log error messages
780 my $dbh = $self->{ruledb
}->{dbh
};
783 foreach my $rid (@$matching_rules) {
784 $where .= $where ?
" OR ID = $rid" : "ID = $rid";
787 $dbh->do ("UPDATE Rule SET Count = Count + 1 WHERE $where") if $where;
789 my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " .
790 "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" .
791 "$lcid, currval('cstatistic_id_seq'), currval('cstatistic_id_seq'),";
793 $insert_cmds .= $queue->{rtime
} . ',';
794 $insert_cmds .= $queue->{bytes
} . ',';
795 $insert_cmds .= $dbh->quote($msginfo->{trusted
} ?
0 : 1) . ',';
796 $insert_cmds .= ($queue->{sa_score
} || 0) . ',';
797 $insert_cmds .= $dbh->quote($queue->{vinfo
}) . ',';
798 $insert_cmds .= $time_total . ',';
799 $insert_cmds .= $dbh->quote(encode
('UTF-8', $msginfo->{sender
})) . ');';
801 foreach my $r (@{$msginfo->{targets
}}) {
802 my $tmp = $dbh->quote(encode
('UTF-8',$r));
803 my $blocked = $queue->{status
}->{$r} eq 'blocked' ?
1 : 0;
804 $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " .
805 "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); ";
808 $dbh->do($insert_cmds);
810 # save $err (because log clears $@)
813 $time_total = $time_total/1000;
815 my $ptspam = ($queue->{ptime_spam
} || 0)/1000;
816 my $ptclam = ($queue->{ptime_clam
} || 0)/1000;
817 my $ptcustom = ($queue->{ptime_custom
} || 0)/1000;
819 $self->log(3, "$queue->{logid}: processing time: ${time_total} seconds ($ptspam, $ptclam, $ptcustom)");
821 $msginfo->{test_fh
}->close if $opt_testmode;
826 my $initial_memory_usage;
828 sub process_request
{
830 my $prop = $self->{server
};
831 my $sock = $prop->{client
};
835 # make sure the ldap cache is up to date
836 $self->{ldap
}->update (1);
838 $self->load_config() if $self->{reload_config
};
840 $self->{trusted
} = 0;
841 if ($prop->{sockport
} == $opt_int_port && !$opt_untrusted) {
842 $self->{trusted
} = 1;
845 my $smtp = PMG
::SMTP-
>new ($sock);
847 my $maxcount = $max_requests - $prop->{requests
};
849 my $count = $smtp->loop (\
&handle_smtp
, $self, $maxcount);
851 $prop->{requests
} += $count - 1;
857 $self->log (0, $err) if $err;
859 kill(15, $prop->{ppid
}) if $opt_testmode;
861 my $mem = PVE
::ProcFSTools
::read_memory_usage
();
862 if (!defined($initial_memory_usage) || ($prop->{requests
} < 10)) {
863 $initial_memory_usage = $mem->{resident
};
867 $self->log (0, "memory usage: $mem->{size} bytes");
869 if ($self->{errors
}) {
870 $self->log (0, "fast exit because of errors (free $mem->{size} bytes)");
873 my $diff = $mem->{resident
} - $initial_memory_usage;
874 if ($diff > 5*1024*1024) {
875 $self->log (0, "fast exit to reduce server load (free $diff bytes)");
881 $self->done (1) if $err;
884 # test sig_hup with: for ((;;)) ;do kill -HUP `cat /run/${prog_name}.pid`; done;
885 # wrapper to avoid multiple calls to sig_hup
888 my $prop = $self->{server
};
890 return if defined ($prop->{_HUP
}); # do not call twice
892 $self->SUPER::sig_hup
();
895 sub restart_close_hook
{
898 my $sig_set = POSIX
::SigSet-
>new;
899 $sig_set->addset (&POSIX
::SIGHUP
);
900 $sig_set->addset (&POSIX
::SIGCHLD
); # to avoid zombies
901 my $old_sig_set = POSIX
::SigSet-
>new();
903 sigprocmask
(SIG_BLOCK
, $sig_set, $old_sig_set);
906 sub pre_server_close_hook
{
908 my $prop = $self->{server
};
910 if (defined $prop->{_HUP
}) {
911 undef $prop->{pid_file_unlink
};
914 if (defined $prop->{children
}) {
915 foreach my $pid (keys %{$prop->{children
}}) {
916 kill (1, $pid); # HUP children
920 # nicely shutdown children (give them max 30 seconds to shut down)
921 my $previous_alarm = alarm(30);
923 local $SIG{ALRM
} = sub { die "Timed Out!\n" };
926 1 while ((($pid = waitpid (-1, 0)) > 0) || ($! == EINTR
));
928 alarm(0); # avoid race
930 alarm ($previous_alarm);
933 # initialize mime system before fork
934 xdg_mime_get_mime_type_for_file
($0);
937 server
=> $server_attr,
940 if (!$opt_testmode) {
947 my $sender ='sender@proxtest.com';
948 my $targets = ['target1@proxtest.com',
949 'target2@proxtest.com',
950 'target3@proxtest.com'];
954 $smtp = Net
::SMTP-
>new ('127.0.0.1', Port
=> 10023);
959 # syslog ('info', "connected to " . $smtp->domain);
961 $smtp->mail ($sender);
962 $smtp->to (@$targets);
966 open (TMP
, $opt_testmode) ||
967 die "unable to upen file '$opt_testmode' - $! :ERROR";
969 $smtp->datasend ($_);
973 $smtp->datasend ("\n");
986 pmg-smtp-filter - the Proxmox mail filter
990 pmg-smtp-filter [-u] [-t testfile]
994 Documentation is available at www.proxmox.com