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');
74 'testmode=s' => \
$opt_testmode,
75 'pidfile=s' => \
$opt_pidfile,
76 'untrusted' => \
$opt_untrusted,
77 'database=s' => \
$opt_database
83 $opt_pidfile = "/run/${prog_name}.pid" if !$opt_pidfile;
87 my $min_spare_servers = 0;
88 my $max_spare_servers = 0;
92 my $pmg_cfg = PMG
::Config-
>new();
94 my $demo = $pmg_cfg->get('admin', 'demo');
97 syslog
('info', 'demo mode detected - not starting server');
101 my $memory = PMG
::Config
::Mail
::physical_memory
();
102 if ($memory < 3840) {
103 warn "total memory below 4 GiB, consider setting 'max_filters' manually to avoid OOM-kills\n"
104 if !defined($pmg_cfg->get('mail', 'max_filters', 1));
107 $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2;
109 $min_spare_servers = 1;
110 $max_spare_servers = 4;
114 print "using pre-fork workers with min=$min_servers, max=$max_servers, min_spare=$min_spare_servers"
115 .", max_spare=$max_spare_servers, max_requests=$max_requests\n";
117 $opt_max_dequeue = 0 if $opt_testmode;
120 if (defined ($ENV{BOUND_SOCKETS
})) {
125 port
=> [ $opt_int_port, $opt_ext_port ],
127 min_servers
=> $min_servers,
128 max_servers
=> $max_servers,
129 min_spare_servers
=> $min_spare_servers,
130 max_spare_servers
=> $max_spare_servers,
131 max_requests
=> $max_requests,
132 serialize
=> 'flock',
133 max_dequeue
=> $opt_max_dequeue,
134 check_for_dequeue
=> $opt_dequeue_time,
136 pid_file
=> $opt_pidfile,
137 no_close_by_child
=> 1,
138 no_client_stdout
=> 1,
139 commandline
=> $opt_commandline,
142 $server_attr->{setsid
} = $daemonize if !$opt_testmode;
146 if (defined($opt_database)) {
147 $database = $opt_database;
149 $database = $opt_testmode ?
"Proxmox_testdb" : "Proxmox_ruledb";
152 $SIG{'__WARN__'} = sub {
156 syslog
('warning', "WARNING: %s", $t);
161 my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_;
164 sa_score
=> $queue->{sa_score
},
165 sa_hits
=> $queue->{sa_hits
},
166 sa_data
=> $queue->{sa_data
},
167 sa_max
=> $queue->{sa_max
}
171 'SUBJECT' => PMG
::Utils
::decode_rfc1522
($entity->head->get ('subject', 0) || 'No Subject'),
172 'RULE' => $rule->{name
},
173 'RULE_INFO' => $msginfo->{rule_info
},
174 'SENDER' => $msginfo->{sender
},
175 'SENDER_IP' => $msginfo->{xforward
}->{addr
},
176 'TARGETS' => join (', ', @$targets),
177 'RECEIVERS' => join (', ', @{$msginfo->{targets
}}),
178 'SPAMLEVEL' => $spaminfo->{sa_score
},
179 'SPAMSTARS' => '*' x
(($spaminfo->{sa_score
} || 0) > 100 ?
100 : $spaminfo->{sa_score
} || 0),
180 'ADMIN' => $self->{pmg_cfg
}->get('admin', 'email'),
181 'HOST' => $msginfo->{hostname
},
182 'DOMAIN' => $msginfo->{domain
},
183 'FQDN' => $msginfo->{fqdn
},
184 'MSGID' => $queue->{msgid
},
185 'VERSION' => PMG
::pmgcfg
::package() . "/" . PMG
::pmgcfg
::release
() . "/" . PMG
::pmgcfg
::repoid
(),
188 $vars->{__spaminfo
} = $spaminfo;
191 if ($queue->{vinfo_clam
} || $queue->{vinfo_avast
} || $queue->{vinfo_custom
}) {
192 $vars->{'VIRUS_INFO'} = "Virus Info:";
193 $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam
};
194 $vars->{'VIRUS_INFO'} .= " avast: $queue->{vinfo_avast}" if $queue->{vinfo_avast
};
195 $vars->{'VIRUS_INFO'} .= " custom: $queue->{vinfo_custom}" if $queue->{vinfo_custom
};
197 $vars->{'VIRUS_INFO'} = '';
200 if ($queue->{vinfo
}) {
201 $vars->{'VIRUS_INFO'} = "Virus Info: $queue->{vinfo}\n";
203 $vars->{'VIRUS_INFO'} = '';
207 $vars->{'SPAM_HITS'} = $spaminfo->{sa_hits
};
209 $vars->{'SPAM_INFO'} = '';
210 my $sscores = $spaminfo->{sa_data
};
212 if (defined ($sscores) && @$sscores != -1) {
215 $sa_text = "Spam detection results: 100\n";
217 $sa_text = "Spam detection results: $spaminfo->{sa_score}\n";
220 foreach my $s (@$sscores) {
222 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
}, 1, $s->{desc
} || '-');
224 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
}, $s->{score
}, $s->{desc
} || '-');
227 $vars->{'SPAM_INFO'} = $sa_text;
231 delete ($vars->{'ADMIN'});
232 #delete ($vars->{'SPAM_INFO'});
239 my ($self, $queue, $msginfo, $entity, $ldap) = @_;
246 my $matching_rules = [];
248 my $rulecache = $self->{rulecache
};
249 my $rules = $rulecache->rules ();
250 my $dbh = $self->{ruledb
}->{dbh
};
252 # first, we remove all conditional written 'X-' header attributes
253 foreach my $rule (@$rules) {
254 next if !$rule->{active
};
255 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
256 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
258 my $actions = $rulecache->get_actions ($rule->{id
});
260 foreach my $action (@$actions) {
261 if ($action->isa ("PMG::RuleDB::ModField")) {
262 my $fname = $action->{field
};
263 next if $fname !~ m/^X-/i;
264 $entity->head->delete($fname);
270 foreach my $rule (@$rules) {
271 next if !$rule->{active
};
272 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
273 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
275 # match from, when and what classes (not target dependent)
276 if (!($rulecache->from_match ($rule->{id
}, $msginfo->{sender
}, $msginfo->{xforward
}->{addr
}, $ldap) &&
277 $rulecache->when_match ($rule->{id
}, time))) {
281 my ($marks, $spaminfo) =
282 $rulecache->what_match ($rule->{id
}, $queue, $entity, $msginfo, $dbh);
284 $rule_marks{$rule->{id
}} = $marks;
285 $rule_spaminfo{$rule->{id
}} = $spaminfo;
287 $rule_actions{$rule->{id
}} = $rulecache->get_actions ($rule->{id
});
288 my $fin = $rulecache->final ($rule->{id
});
291 foreach my $target (@{$msginfo->{targets
}}) {
292 next if $final->{$target};
293 next if !defined ($rule_marks{$rule->{id
}});
294 next if !defined ($rule_marks{$rule->{id
}}->{$target});
295 next if !$rulecache->to_match ($rule->{id
}, $target, $ldap);
297 $final->{$target} = $fin;
299 push @{$rule_targets{$rule->{id
}}}, $target;
303 # Compute rule_info (summary about matching rule)
304 # this can be used for debugging
306 foreach my $rule (@$rules) {
307 next if !$rule_targets{$rule->{id
}};
309 push @$matching_rules, $rule->{id
};
311 $rule_info .= "Rule: $rule->{name}\n";
313 foreach my $target (@{$rule_targets{$rule->{id
}}}) {
314 $rule_info .= " Receiver: $target\n";
316 foreach my $action (@{$rule_actions{$rule->{id
}}}) {
317 $rule_info .= " Action: " . $action->short_desc () . "\n";
321 $msginfo->{rule_info
} = $rule_info;
323 if ($msginfo->{testmode
}) {
324 my $vars = $self->get_prox_vars($queue, $entity, $msginfo, undef, [], undef);
325 my $fh = $msginfo->{test_fh
};
326 print $fh $rule_info;
331 my $mod_group = PMG
::ModGroup-
>new($entity, $msginfo->{targets
});
333 foreach my $rule (@$rules) {
334 my $targets = $rule_targets{$rule->{id
}};
337 my $vars = $self->get_prox_vars (
338 $queue, $entity, $msginfo, $rule, $rule_targets{$rule->{id
}}, $rule_spaminfo{$rule->{id
}});
340 my @sorted_actions = sort {$a->priority <=> $b->priority} @{$rule_actions{$rule->{id
}}};
342 foreach my $action (@sorted_actions) {
344 $queue, $self->{ruledb
}, $mod_group, $rule_targets{$rule->{id
}}, $msginfo, $vars,
345 $rule_marks{$rule->{id
}}, $ldap
347 last if $action->final;
351 # we deliver all mail not matched by any rule (default action = accept)
353 foreach my $target (@{$msginfo->{targets
}}) {
354 next if $final->{$target};
356 push @$unmatched, $target;
360 my $accept = PMG
::RuleDB
::Accept-
>new ();
361 $accept->execute ($queue, $self->{ruledb
}, $mod_group, $unmatched,
362 $msginfo, { RULE
=> 'default-accept' }, undef);
365 return $matching_rules;
368 # reload ruledb and pmg config
371 my $prop = $self->{server
};
373 if ($self->{ruledb
}) {
374 $self->log ('info', "reloading configuration $database");
375 $self->{ruledb
}->close ();
378 $self->{pmg_cfg
} = PMG
::Config-
>new();
379 $self->{cinfo
} = PVE
::INotify
::read_file
("cluster.conf");
381 # create spool directories
382 PMG
::MailQueue
::create_spooldirs
($self->{cinfo
}->{local}->{cid
});
385 my $dbh = PMG
::DBTools
::open_ruledb
($database);
386 $self->{ruledb
} = PMG
::RuleDB-
>new ($dbh);
389 $self->{rulecache
} = PMG
::RuleCache-
>new ($self->{ruledb
});
395 sleep (10); # reduce restart rate when postgres is down
400 my $ldapconf = PVE
::INotify
::read_file
('pmg-ldap.conf');
401 $self->{ldap
} = PMG
::LDAPSet-
>new_from_ldap_cfg($ldapconf, 1);
403 $self->{reload_config
} = 0;
415 my ($self, $level, $msg, @therest) = @_;
416 my $prop = $self->{server
};
418 return if $level =~ /^\d+$/ && $level > $prop->{log_level
};
420 $level = $syslog_map->{$level} || $level;
422 syslog
($level, $msg, @therest);
424 syslog
($level, $msg);
430 my $prop = $self->{server
};
432 $prop->{log_level
} = 3;
434 $self->log ('info', "Filter daemon (re)started (max. $max_servers processes)");
436 eval { PMG
::MailQueue
::cleanup_active
(); };
437 $self->log (0, "Cleanup failures: $@") if $@;
439 my $sig_set = POSIX
::SigSet-
>new;
440 $sig_set->addset (&POSIX
::SIGHUP
);
441 $sig_set->addset (&POSIX
::SIGCHLD
);
442 my $old_sig_set = POSIX
::SigSet-
>new();
444 sigprocmask
(SIG_UNBLOCK
, $sig_set, $old_sig_set);
446 my ($backup_umask) = umask;
448 my $pmg_cfg = PMG
::Config-
>new();
449 $pmg_cfg->write_smtp_filter_config();
451 # Note: you need to restart the daemon when you change 'rbl_checks'
452 my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');
454 $self->{sa
} = Mail
::SpamAssassin-
>new ({
456 local_tests_only
=> $opt_testmode || !$rbl_checks,
457 home_dir_for_helpers
=> '/root',
458 userstate_dir
=> '/root/.spamassassin',
459 dont_copy_prefs
=> 1,
460 stop_at_threshold
=> 0,
463 $self->{sa
}->compile_now;
465 alarm (0); # SA forgets to clear alarm in some cases
466 umask ($backup_umask);
467 initlog
($prog_name, 'mail');
470 # reloading server configuration
471 if (defined $prop->{children
}) {
472 foreach my $pid (keys %{$prop->{children
}}) {
473 kill (10, $pid); # SIGUSR1 children
479 sub child_init_hook
{
482 $0 = "$prog_name child";
484 # $self->log (3, "init child");
487 $self->load_config ();
492 $self->child_finish_hook;
497 $self->{reload_config
} = 1;
501 sub child_finish_hook
{
504 # $self->log (3, "finish child");
505 $self->{ruledb
}->close () if $self->{ruledb
};
508 my $last_dequeue_time = 0;
515 # do database maintenance here, this is called every 30 secends
517 eval { PMG
::Utils
::update_node_status_rrd
() };
519 $self->log(0, "ERROR: $err");
524 my $tdiff = $ctime - $last_dequeue_time;
526 # return if tdiff less than 2 minutes
527 return if $tdiff < 2*60;
528 $last_dequeue_time = $ctime;
530 $self->log (2, "starting database maintenance");
532 my ($csec, $usec) = gettimeofday
();
534 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
536 my $dbh = eval { PMG
::DBTools
::open_ruledb
($database) };
538 $self->log (0, "ERROR: $err");
542 eval { PMG
::Statistic
::update_stats
($dbh, $cinfo) };
546 $self->log (0, $err);
548 my ($csec_end, $usec_end) = gettimeofday
();
549 my $ptime = int (($csec_end - $csec) * 1000 + ($usec_end - $usec) / 1000);
550 $self->log (2, "end database maintenance ($ptime ms)");
553 $dbh->disconnect() if $dbh;
558 my ($self, $unpack, $entity, $msginfo, $queue) = @_;
560 my ($magic, $path) = $entity->@{'PMX_magic_ct', 'PMX_decoded_path'};
562 if ($magic && $path) {
563 my $filename = basename
($path);
565 if (PMG
::Unpack
::is_archive
($magic)) {
566 $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)");
568 my $start = [gettimeofday
];
570 $unpack->{mime
} = {};
572 eval { $unpack->unpack_archive ($path, $magic) };
573 $self->log (3, "$queue->{logid}: unpack failed - $@") if $@;
575 $entity->{PMX_content_types
} = $unpack->{mime
};
576 $entity->{PMX_filenames
} = $unpack->{filenames
};
579 my $types = join (", ", sort keys (%{$entity->{PMX_content_types
}}));
580 my $fh = $msginfo->{test_fh
};
581 $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/;
582 print $fh "Types:$filename: $types\n" if $types;
585 my $elapsed = int(tv_interval
($start) * 1000);
586 $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)");
590 for my $part ($entity->parts) {
591 $self->unpack_entity ($unpack, $part, $msginfo, $queue);
597 my ($self, $smtp) = @_;
599 my ($csec, $usec) = gettimeofday
();
603 my $pmg_cfg = $self->{pmg_cfg
};
604 my $ldap = $self->{ldap
};
605 my $cinfo = $self->{cinfo
};
606 my $lcid = $cinfo->{local}->{cid
};
608 $msginfo->{test_fh
} = PMG
::AtomicFile-
>new("testresult.out", "w") if $opt_testmode;
610 $msginfo->{trusted
} = $self->{trusted
};
613 # PHASE 1 - save incoming mail (already done)
616 $queue = $smtp->{queue
};
617 $queue->{sa
} = $self->{sa
};
618 $queue->{clamav_heuristic_score
} =
619 $opt_testmode ?
100 : $pmg_cfg->get('spam', 'clamav_heuristic_score');
621 $queue->{lic_valid
} = 1;
626 $msginfo->{testmode
} = $opt_testmode;
627 $msginfo->{sender
} = $smtp->{from
};
628 $msginfo->{xforward
} = $smtp->{xforward
};
629 $msginfo->{targets
} = $smtp->{to
};
630 $msginfo->{param
} = $smtp->{param
};
632 my $dkim_sign = $msginfo->{trusted
} && $pmg_cfg->get('admin', 'dkim_sign');
634 $msginfo->{dkim
}->{sign
} = $dkim_sign;
635 $msginfo->{dkim
}->{sign_all
} = $pmg_cfg->get('admin', 'dkim_sign_all_mail');
636 $msginfo->{dkim
}->{selector
} = $pmg_cfg->get('admin', 'dkim_selector');
639 $msginfo->{hostname
} = PVE
::INotify
::nodename
();
640 my $resolv = PVE
::INotify
::read_file
('resolvconf');
642 $msginfo->{domain
} = $resolv->{search
};
643 $msginfo->{fqdn
} = $msginfo->{hostname
};
644 $msginfo->{fqdn
} .= ".$msginfo->{domain}" if $msginfo->{domain
};
645 $msginfo->{lcid
} = $lcid;
647 # $msginfo->{targets} is case sensitive,
648 # but pmail is always lower case!
650 foreach my $t (@{$msginfo->{targets
}}) {
652 if ($ldap && ($res = $ldap->account_info ($t))) {
653 $msginfo->{pmail
}->{$t} = $res->{pmail
};
655 $msginfo->{pmail
}->{$t} = lc ($t);
659 # PHASE 2 - parse mail
662 my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles');
664 my ($entity, $max_aid) = $queue->parse_mail($maxfiles);
665 $msginfo->{max_aid
} = $max_aid;
667 $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid
});
669 # PHASE 3 - run external content analyzers
670 # (SPAM analyzer is run on demand later)
671 # on error: log error messages
673 # run custom script first
674 my ($vinfo, $custom_spaminfo) = PMG
::Utils
::analyze_custom_check
(
675 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
677 # test for virus if none found
678 if (!defined($vinfo)) {
679 $vinfo = PMG
::Utils
::analyze_virus
(
680 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
682 if ($vinfo && $vinfo =~ m/^Heuristics\.(.+)$/) {
684 $queue->{clamav_heuristic
} = $hit;
688 $queue->{vinfo
} = $vinfo;
690 # always add this headers to incoming mails
691 # to enable user to report false negatives
692 if (!$msginfo->{trusted
}) {
693 if ($queue->{vinfo
}) {
694 $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo
});
698 # we unpack after virus scanning, because this is more secure.
699 # This way virus scanners gets the whole mail files and are able
700 # to detect phishing signature for example - which would not work
701 # if we decompose first and only analyze the decomposed attachments.
702 # Disadvantage is that we need to unpack more than
703 # once (bad performance).
705 # should we scan content types inside archives
707 my $rulecache = $self->{rulecache
};
709 my $scan_archives = 0;
711 if (($rulecache->{archivefilter_in
} && !$msginfo->{trusted
}) ||
712 ($rulecache->{archivefilter_out
} && $msginfo->{trusted
})) {
716 if ($scan_archives && !$queue->{vinfo
}) {
718 # unpack all archives - determine contained content types
720 my $decdir = $queue->{dumpdir
} . "/__decoded_archives";
723 my $start = [gettimeofday
];
728 # limits: We use clamav limit for maxfiles, and scan
729 # only 4 levels, timeout of 30 seconds
731 $unpack = PMG
::Unpack-
>new (
734 ctonly
=> 1, # only detect CTs
736 maxfiles
=> $maxfiles);
738 $self->unpack_entity ($unpack, $entity, $msginfo, $queue);
743 $unpack->cleanup() if $unpack;
745 my $elapsed = int(tv_interval
($start) * 1000);
748 $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err");
752 # PHASE 4 - apply rules
753 # on error: exit (cleanup process should do the rest)
754 $msginfo->{maxspamsize
} = $pmg_cfg->get('spam', 'maxspamsize');
755 if ($msginfo->{maxspamsize
} <= 1024*64) {
756 $msginfo->{maxspamsize
} = 1024*64;
759 if ($msginfo->{trusted
}) {
760 my $hide = $pmg_cfg->get('mail', 'hide_received');
761 $entity->head->delete("Received") if $hide;
764 $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap);
769 $self->{errors
} = $queue->{errors
};
772 $self->{errors
} = 1 if $err;
778 my ($csec_end, $usec_end) = gettimeofday
();
780 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
782 # PHASE 5 - log statistics
783 # on error: log error messages
786 my $dbh = $self->{ruledb
}->{dbh
};
789 foreach my $rid (@$matching_rules) {
790 $where .= $where ?
" OR ID = $rid" : "ID = $rid";
793 $dbh->do ("UPDATE Rule SET Count = Count + 1 WHERE $where") if $where;
795 my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " .
796 "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" .
797 "$lcid, currval('cstatistic_id_seq'), currval('cstatistic_id_seq'),";
799 $insert_cmds .= $queue->{rtime
} . ',';
800 $insert_cmds .= $queue->{bytes
} . ',';
801 $insert_cmds .= $dbh->quote($msginfo->{trusted
} ?
0 : 1) . ',';
802 $insert_cmds .= ($queue->{sa_score
} || 0) . ',';
803 $insert_cmds .= $dbh->quote($queue->{vinfo
}) . ',';
804 $insert_cmds .= $time_total . ',';
805 $insert_cmds .= $dbh->quote(encode
('UTF-8', $msginfo->{sender
})) . ');';
807 foreach my $r (@{$msginfo->{targets
}}) {
808 my $tmp = $dbh->quote(encode
('UTF-8',$r));
809 my $blocked = $queue->{status
}->{$r} eq 'blocked' ?
1 : 0;
810 $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " .
811 "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); ";
814 $dbh->do($insert_cmds);
816 # save $err (because log clears $@)
819 $time_total = $time_total/1000;
821 my $ptspam = ($queue->{ptime_spam
} || 0)/1000;
822 my $ptclam = ($queue->{ptime_clam
} || 0)/1000;
823 my $ptcustom = ($queue->{ptime_custom
} || 0)/1000;
825 $self->log(3, "$queue->{logid}: processing time: ${time_total} seconds ($ptspam, $ptclam, $ptcustom)");
827 $msginfo->{test_fh
}->close if $opt_testmode;
832 my $initial_memory_usage;
834 sub process_request
{
836 my $prop = $self->{server
};
837 my $sock = $prop->{client
};
841 # make sure the ldap cache is up to date
842 $self->{ldap
}->update (1);
844 $self->load_config() if $self->{reload_config
};
846 $self->{trusted
} = 0;
847 if ($prop->{sockport
} == $opt_int_port && !$opt_untrusted) {
848 $self->{trusted
} = 1;
851 my $smtp = PMG
::SMTP-
>new ($sock);
853 my $maxcount = $max_requests - $prop->{requests
};
855 my $count = $smtp->loop (\
&handle_smtp
, $self, $maxcount);
857 $prop->{requests
} += $count - 1;
863 $self->log (0, $err) if $err;
865 kill(15, $prop->{ppid
}) if $opt_testmode;
867 my $mem = PVE
::ProcFSTools
::read_memory_usage
();
868 if (!defined($initial_memory_usage) || ($prop->{requests
} < 10)) {
869 $initial_memory_usage = $mem->{resident
};
873 $self->log (0, "memory usage: $mem->{size} bytes");
875 if ($self->{errors
}) {
876 $self->log (0, "fast exit because of errors (free $mem->{size} bytes)");
879 my $diff = $mem->{resident
} - $initial_memory_usage;
880 if ($diff > 5*1024*1024) {
881 $self->log (0, "fast exit to reduce server load (free $diff bytes)");
887 $self->done (1) if $err;
890 # test sig_hup with: for ((;;)) ;do kill -HUP `cat /run/${prog_name}.pid`; done;
891 # wrapper to avoid multiple calls to sig_hup
894 my $prop = $self->{server
};
896 return if defined ($prop->{_HUP
}); # do not call twice
898 $self->SUPER::sig_hup
();
901 sub restart_close_hook
{
904 my $sig_set = POSIX
::SigSet-
>new;
905 $sig_set->addset (&POSIX
::SIGHUP
);
906 $sig_set->addset (&POSIX
::SIGCHLD
); # to avoid zombies
907 my $old_sig_set = POSIX
::SigSet-
>new();
909 sigprocmask
(SIG_BLOCK
, $sig_set, $old_sig_set);
912 sub pre_server_close_hook
{
914 my $prop = $self->{server
};
916 if (defined $prop->{_HUP
}) {
917 undef $prop->{pid_file_unlink
};
920 if (defined $prop->{children
}) {
921 foreach my $pid (keys %{$prop->{children
}}) {
922 kill (1, $pid); # HUP children
926 # nicely shutdown children (give them max 30 seconds to shut down)
927 my $previous_alarm = alarm(30);
929 local $SIG{ALRM
} = sub { die "Timed Out!\n" };
932 1 while ((($pid = waitpid (-1, 0)) > 0) || ($! == EINTR
));
934 alarm(0); # avoid race
936 alarm ($previous_alarm);
939 # initialize mime system before fork
940 xdg_mime_get_mime_type_for_file
($0);
943 server
=> $server_attr,
946 if (!$opt_testmode) {
953 my $sender ='sender@pmg.example';
955 'target1@pmg.example',
956 'target2@pmg.example',
957 'target3@pmg.example',
962 $smtp = Net
::SMTP-
>new ('127.0.0.1', Port
=> 10023);
967 # syslog ('info', "connected to " . $smtp->domain);
969 $smtp->mail ($sender);
970 $smtp->to (@$targets);
974 open (TMP
, $opt_testmode) ||
975 die "unable to upen file '$opt_testmode' - $! :ERROR";
977 $smtp->datasend ($_);
981 $smtp->datasend ("\n");
994 pmg-smtp-filter - the Proxmox mail filter
998 pmg-smtp-filter [-u] [-t testfile]
1002 Documentation is available at www.proxmox.com