8 use Time
::HiRes qw
(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);
23 use Mail
::SpamAssassin
;
24 use Mail
::SpamAssassin
::NetSet
;
42 use base
qw(Net::Server::PreFork);
44 my $opt_commandline = [$0, @ARGV];
45 my $opt_max_dequeue = 1;
46 my $opt_dequeue_time = 60*2;
48 my $opt_ext_port = 10024;
49 my $opt_int_port = 10023;
50 my $opt_inject_port = 10025;
57 initlog
('proxprox', 'mail');
59 if (!GetOptions
('testmode=s' => \
$opt_testmode,
60 'pidfile=s' => \
$opt_pidfile,
61 'untrusted' => \
$opt_untrusted,
62 'database=s' => \
$opt_database)) {
67 $opt_pidfile = "/var/run/proxprox.pid" if !$opt_pidfile;
71 my $min_spare_servers = 0;
72 my $max_spare_servers = 0;
77 my $pmg_cfg = PMG
::Config-
>new();
79 my $demo = $pmg_cfg->get('administration', 'demo');
82 syslog
('info', 'demo mode detected - not starting server');
86 $max_servers = $pmg_cfg->get('mail', 'max_filters') + 2;
88 $min_spare_servers = 1;
89 $max_spare_servers = 4;
93 $opt_max_dequeue = 0 if $opt_testmode;
96 if (defined ($ENV{BOUND_SOCKETS
})) {
101 port
=> [ $opt_int_port, $opt_ext_port ],
103 min_servers
=> $min_servers,
104 max_servers
=> $max_servers,
105 min_spare_servers
=> $min_spare_servers,
106 max_spare_servers
=> $max_spare_servers,
107 max_requests
=> $max_requests,
108 serialize
=> 'flock',
109 max_dequeue
=> $opt_max_dequeue,
110 check_for_dequeue
=> $opt_dequeue_time,
112 pid_file
=> $opt_pidfile,
113 no_close_by_child
=> 1,
114 no_client_stdout
=> 1,
115 commandline
=> $opt_commandline,
118 $server_attr->{setsid
} = $daemonize if !$opt_testmode;
122 if (defined($opt_database)) {
123 $database = $opt_database;
125 $database = $opt_testmode ?
"Proxmox_testdb" : "Proxmox_ruledb";
128 $SIG{'__WARN__'} = sub {
132 syslog
('warning', "WARNING: %s", $t);
137 my ($self, $queue, $entity, $msginfo, $rule, $targets, $spaminfo) = @_;
140 sa_score
=> $queue->{sa_score
},
141 sa_hits
=> $queue->{sa_hits
},
142 sa_data
=> $queue->{sa_data
},
143 sa_max
=> $queue->{sa_max
}
147 'SUBJECT' => $entity->head->get ('subject', 0) || 'No Subject',
148 'RULE' => $rule->{name
},
149 'RULE_INFO' => $msginfo->{rule_info
},
150 'SENDER' => $msginfo->{sender
},
151 'SENDER_IP' => $msginfo->{xforward
}->{addr
},
152 'TARGETS' => join (', ', @$targets),
153 'RECEIVERS' => join (', ', @{$msginfo->{targets
}}),
154 'SPAMLEVEL' => $spaminfo->{sa_score
},
155 'SPAMSTARS' => '*' x
(($spaminfo->{sa_score
} || 0) > 100 ?
100 : $spaminfo->{sa_score
} || 0),
156 'ADMIN' => $self->{pmg_cfg
}->get('administration', 'email'),
157 'HOST' => $msginfo->{hostname
},
158 'DOMAIN' => $msginfo->{domain
},
159 'FQDN' => $msginfo->{fqdn
},
160 'MSGID' => $queue->{msgid
},
161 'VERSION' => PMG
::pmgcfg
::package() . "/" . PMG
::pmgcfg
::version
() . "/" . PMG
::pmgcfg
::repoid
(),
164 $vars->{__spaminfo
} = $spaminfo;
167 if ($queue->{vinfo_avira
} || $queue->{vinfo_clam
} || $queue->{vinfo_commtouch
}) {
168 $vars->{'VIRUS_INFO'} = "Virus Info:";
169 $vars->{'VIRUS_INFO'} .= " clam: $queue->{vinfo_clam}" if $queue->{vinfo_clam
};
170 $vars->{'VIRUS_INFO'} .= " avira: $queue->{vinfo_avira}" if $queue->{vinfo_avira
};
171 $vars->{'VIRUS_INFO'} .= " commtouch: $queue->{vinfo_commtouch}" if $queue->{vinfo_commtouch
};
173 $vars->{'VIRUS_INFO'} = '';
176 if ($queue->{vinfo
}) {
177 $vars->{'VIRUS_INFO'} = "Virus Info: $queue->{vinfo}\n";
179 $vars->{'VIRUS_INFO'} = '';
183 $vars->{'SPAM_HITS'} = $spaminfo->{sa_hits
};
185 $vars->{'SPAM_INFO'} = '';
186 my $sscores = $spaminfo->{sa_data
};
188 if (defined ($sscores) && @$sscores != -1) {
191 $sa_text = "Spam detection results: 100\n";
193 $sa_text = "Spam detection results: $spaminfo->{sa_score}\n";
196 foreach my $s (@$sscores) {
198 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
},
199 1, $s->{desc
} || '-');
201 $sa_text .= sprintf ("%-22s %6s %s\n", $s->{rule
},
202 $s->{score
}, $s->{desc
} || '-');
205 $vars->{'SPAM_INFO'} = $sa_text;
209 delete ($vars->{'ADMIN'});
210 #delete ($vars->{'SPAM_INFO'});
217 my ($self, $queue, $msginfo, $entity, $ldap) = @_;
223 my $matching_rules = [];
225 my $rulecache = $self->{rulecache
};
226 my $rules = $rulecache->rules ();
227 my $dbh = $self->{ruledb
}->{dbh
};
229 # first, we remove all conditional written 'X-' header attributes
230 foreach my $rule (@$rules) {
231 next if !$rule->{active
};
232 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
233 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
235 my $actions = $rulecache->get_actions ($rule->{id
});
237 foreach my $action (@$actions) {
238 if ($action->isa ("Proxmox::RuleDB::ModField")) {
239 my $fname = $action->{field
};
240 next if $fname !~ m/^X-/i;
241 $entity->head->delete($fname);
247 foreach my $rule (@$rules) {
248 next if !$rule->{active
};
249 next if ($rule->{direction
} == 0) && $msginfo->{trusted
};
250 next if ($rule->{direction
} == 1) && !$msginfo->{trusted
};
252 # match from, when and what classes (not target dependent)
253 if (!($rulecache->from_match ($rule->{id
}, $msginfo->{sender
}, $msginfo->{xforward
}->{addr
}, $ldap) &&
254 $rulecache->when_match ($rule->{id
}, time))) {
258 $rule_marks{$rule->{id
}} =
259 $rulecache->what_match ($rule->{id
}, $queue, $entity, $msginfo, $dbh);
261 $rule_actions{$rule->{id
}} = $rulecache->get_actions ($rule->{id
});
262 my $fin = $rulecache->final ($rule->{id
});
265 foreach my $target (@{$msginfo->{targets
}}) {
266 next if $final->{$target};
267 next if !defined ($rule_marks{$rule->{id
}});
268 next if !defined ($rule_marks{$rule->{id
}}->{$target});
269 next if !defined ($rule_marks{$rule->{id
}}->{$target}->{marks
});
270 next if !$rulecache->to_match ($rule->{id
}, $target, $ldap);
272 $final->{$target} = $fin;
274 push @{$rule_targets{$rule->{id
}}}, $target;
278 # Compute rule_info (summary about matching rule)
279 # this can be used for debugging
281 foreach my $rule (@$rules) {
282 next if !$rule_targets{$rule->{id
}};
284 push @$matching_rules, $rule->{id
};
286 $rule_info .= "Rule: $rule->{name}\n";
288 foreach my $target (@{$rule_targets{$rule->{id
}}}) {
289 $rule_info .= " Receiver: $target\n";
291 foreach my $action (@{$rule_actions{$rule->{id
}}}) {
292 $rule_info .= " Action: " . $action->short_desc () . "\n";
296 $msginfo->{rule_info
} = $rule_info;
298 if ($msginfo->{testmode
}) {
299 my $vars = $self->get_prox_vars ($queue, $entity, $msginfo, undef, [], undef);
300 my $out = "__RULE_INFO__";
301 $out = PMG
::Utils
::subst_values
($out, $vars);
302 my $fh = $msginfo->{test_fh
};
308 my $mod_group = PMG
::ModGroup-
>new($entity, $msginfo->{targets
});
310 foreach my $rule (@$rules) {
311 my $targets = $rule_targets{$rule->{id
}};
315 foreach my $t (@$targets) {
316 if ($rule_marks{$rule->{id
}}->{$t} && $rule_marks{$rule->{id
}}->{$t}->{spaminfo
}) {
317 $spaminfo = $rule_marks{$rule->{id
}}->{$t}->{spaminfo
};
318 # we assume spam info is the same for all matching targets
323 my $vars = $self->get_prox_vars ($queue, $entity, $msginfo, $rule,
324 $rule_targets{$rule->{id
}}, $spaminfo);
328 sort {$a->priority <=> $b->priority} @{$rule_actions{$rule->{id
}}};
330 foreach my $action (@sorted_actions) {
331 $action->execute ($queue, $self->{ruledb
}, $mod_group,
332 $rule_targets{$rule->{id
}},
333 $msginfo, $vars, $rule_marks{$rule->{id
}}->{marks
}, $ldap);
334 last if $action->final;
338 # we deliver all mail not matched by any rule
339 # (default action = accept)
341 foreach my $target (@{$msginfo->{targets
}}) {
342 next if $final->{$target};
344 push @$unmatched, $target;
348 my $accept = Proxmox
::RuleDB
::Accept-
>new ();
349 $accept->execute ($queue, $self->{ruledb
}, $mod_group, $unmatched,
350 $msginfo, undef, undef, undef);
353 return $matching_rules;
356 # reload ruledb and pmg config
359 my $prop = $self->{server
};
361 if ($self->{ruledb
}) {
362 $self->log (0, "reloading configuration $database");
363 $self->{ruledb
}->close ();
366 $self->{pmg_cfg
} = PMG
::Config-
>new();
367 $self->{cinfo
} = PVE
::INotify
::read_file
("cluster.conf");
370 my $dbh = PMG
::DBTools
::open_ruledb
($database);
371 $self->{ruledb
} = PMG
::RuleDB-
>new ($dbh);
374 $self->{rulecache
} = PMG
::RuleCache-
>new ($self->{ruledb
});
380 sleep (10); # reduce restart rate when postgres is down
385 $self->{ldap
} = PMG
::LDAPSet-
>new_from_pmg_cfg($self->{pmg_cfg
}, 1);
387 $self->{reload_config
} = 0;
399 my ($self, $level, $msg, @therest) = @_;
400 my $prop = $self->{server
};
402 return if $level =~ /^\d+$/ && $level > $prop->{log_level
};
404 $level = $syslog_map->{$level} || $level;
406 syslog
($level, $msg, @therest);
408 syslog
($level, $msg);
414 my $prop = $self->{server
};
416 $prop->{log_level
} = 3;
418 $self->log (0, "Filter daemon (re)started (max. $max_servers processes)");
420 eval { Proxmox
::MailQueue
::cleanup_active
(); };
421 $self->log (0, "Cleanup failures: $@") if $@;
423 my $sig_set = POSIX
::SigSet-
>new;
424 $sig_set->addset (&POSIX
::SIGHUP
);
425 $sig_set->addset (&POSIX
::SIGCHLD
);
426 my $old_sig_set = POSIX
::SigSet-
>new();
428 sigprocmask
(SIG_UNBLOCK
, $sig_set, $old_sig_set);
430 my ($backup_umask) = umask;
432 my $pmg_cfg = PMG
::Config-
>new();
434 # Note: you need to restart the daemon when you change 'rbl_checks'
435 my $rbl_checks = $pmg_cfg->get('spam', 'rbl_checks');
437 $self->{sa
} = Mail
::SpamAssassin-
>new ({
439 local_tests_only
=> $opt_testmode || !$rbl_checks,
440 home_dir_for_helpers
=> '/root',
441 userstate_dir
=> '/root/.spamassassin',
442 dont_copy_prefs
=> 1,
443 stop_at_threshold
=> 0,
446 $self->{sa
}->compile_now;
448 alarm (0); # SA forgets to clear alarm in some cases
449 umask ($backup_umask);
450 initlog
('proxprox', 'mail');
453 # reloading server configuration
454 if (defined $prop->{children
}) {
455 foreach my $pid (keys %{$prop->{children
}}) {
456 kill (10, $pid); # SIGUSR1 childs
462 sub child_init_hook
{
465 $0 = 'proxprox child';
467 # $self->log (3, "init child");
470 $self->load_config ();
475 $self->child_finish_hook;
480 $self->{reload_config
} = 1;
484 sub child_finish_hook
{
487 # $self->log (3, "finish child");
488 $self->{ruledb
}->close () if $self->{ruledb
};
494 # do database maintainance here
496 $self->log (2, "starting database maintainance");
498 my ($csec, $usec) = gettimeofday
();
500 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
505 $dbh = PMG
::DBTools
::open_ruledb
($database);
510 $self->log (0, msgquote
("ERROR: $err"));
515 Proxmox
::Statistic
::update_stats
($dbh, $cinfo);
519 my ($csec_end, $usec_end) = gettimeofday
();
520 my $ptime = int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
523 $self->log (0, msgquote
($err));
525 $self->log (2, "end database maintainance ($ptime ms)");
528 $dbh->disconnect() if $dbh;
533 my ($self, $unpack, $entity, $msginfo, $queue) = @_;
538 if (($magic = $entity->{PMX_magic_ct
}) &&
539 ($path = $entity->{PMX_decoded_path
})) {
541 my $filename = basename
($path);
543 if (Proxmox
::Unpack
::is_archive
($magic)) {
544 $self->log (3, "$queue->{logid}: found archive '$filename' ($magic)");
546 my $start = [gettimeofday
];
548 $unpack->{mime
} = {};
551 $unpack->unpack_archive ($path, $magic);
554 $self->log (3, "$queue->{logid}: unpack failed - $@") if $@;
556 $entity->{PMX_content_types
} = $unpack->{mime
};
559 my $types = join (", ", sort keys (%{$entity->{PMX_content_types
}}));
560 my $fh = $msginfo->{test_fh
};
561 $filename =~ s/\d+/X/g if $filename =~ m/^msg-\d+-\d+.msg/;
562 print $fh "Types:$filename: $types\n" if $types;
565 my $elapsed = int(tv_interval
($start) * 1000);
567 $self->log (3, "$queue->{logid}: unpack archive '$filename' done ($elapsed ms)");
571 foreach my $part ($entity->parts) {
572 $self->unpack_entity ($unpack, $part, $msginfo, $queue);
578 my ($self, $smtp) = @_;
580 my ($csec, $usec) = gettimeofday
();
584 my $pmg_cfg = $self->{pmg_cfg
};
585 my $ldap = $self->{ldap
};
586 my $cinfo = $self->{cinfo
};
587 my $lcid = $cinfo->{local}->{cid
};
589 $msginfo->{test_fh
} = PMG
::AtomicFile-
>new("testresult.out", "w")
592 $msginfo->{trusted
} = $self->{trusted
};
595 # PHASE 1 - save incoming mail (already done)
598 $queue = $smtp->{queue
};
599 $queue->{sa
} = $self->{sa
};
601 $queue->{lic_valid
} = 1;
606 $msginfo->{testmode
} = $opt_testmode;
607 $msginfo->{sender
} = $smtp->{from
};
608 $msginfo->{xforward
} = $smtp->{xforward
};
609 $msginfo->{targets
} = $smtp->{to
};
611 $msginfo->{hostname
} = PVE
::INotify
::nodename
();
612 my $resolv = PVE
::INotify
::read_file
('resolvconf');
614 $msginfo->{domain
} = $resolv->{search
};
615 $msginfo->{fqdn
} = "$msginfo->{hostname}.$msginfo->{domain}";
616 $msginfo->{lcid
} = $lcid;
618 # $msginfo->{targets} is case sensitive,
619 # but pmail is always lower case!
621 foreach my $t (@{$msginfo->{targets
}}) {
623 if ($ldap && ($res = $ldap->account_info ($t))) {
624 $msginfo->{pmail
}->{$t} = $res->{pmail
};
626 $msginfo->{pmail
}->{$t} = lc ($t);
630 # PHASE 2 - parse mail
633 my $maxfiles = $pmg_cfg->get('clamav', 'archivemaxfiles');
635 my $entity = $queue->parse_mail($maxfiles);
637 $self->log (3, "$queue->{logid}: new mail message-id=%s", $queue->{msgid
});
639 # PHASE 3 - run external content analyzers
640 # (SPAM analyzer is run on demand later)
641 # on error: log error messages
644 # test for virus first
645 $queue->{vinfo
} = PMG
::Utils
::analyze_virus
(
646 $queue, $queue->{dataname
}, $pmg_cfg, $opt_testmode);
649 # only run with valid license
650 # do not run when we already detected a virus
651 # do not run for outbound mail (requires separate ctasd)
653 $queue->{ptime_commtouch
} = 0;
656 ($queue->{lic_valid
} && !$queue->{vinfo
} && !$msginfo->{trusted
})) {
657 my ($ct_refid, $ct_spam, $ct_vod, $ct_virus);
658 my ($csec, $usec) = gettimeofday
();
661 ($ct_refid, $ct_spam, $ct_vod, $ct_virus) =
662 Proxmox
::Commtouch
::ctasd_classify
($queue->{dataname
}, $queue->{from
});
666 my ($csec_end, $usec_end) = gettimeofday
();
667 $queue->{ptime_commtouch
} =
668 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
671 syslog
('err', "ctasd error: %s", $err);
672 $queue->{errors
} = 1;
675 syslog
('info', "$queue->{logid}: virus detected: $ct_virus (commtouch)");
676 $queue->{vinfo_commtouch
} = $ct_virus;
677 $queue->{vinfo
} = "$ct_virus (commtouch)";
680 if ($ct_spam && $ct_spam eq 'Confirmed') {
681 syslog
('info', "$queue->{logid}: known spam mail (confirmed by commtouch)");
684 $queue->{commtouch_spam
} = $ct_spam;
685 $queue->{commtouch_vod
} = $ct_vod;
688 $queue->{commtouch_refid
} = "FAKEK_COMTOUCH_REFID(testmode)";
690 $queue->{commtouch_refid
} = $ct_refid;
695 # always add this headers to incoming mails
696 # to enable user to report false negatives
697 if (!$msginfo->{trusted
}) {
698 if (!$queue->{lic_valid
}) {
699 $entity->head->replace('X-Proxmox-License', 'Invalid');
702 if ($queue->{vinfo
}) {
703 $entity->head->replace('X-Proxmox-VInfo', $queue->{vinfo
});
706 if ($queue->{commtouch_refid
}) {
707 $entity->head->replace('X-Proxmox-CTCH-Refid', $queue->{commtouch_refid
});
711 # we unpack after virus scanning, because this is more secure.
712 # This way virus scanners gets the whole mail files and are able
713 # to detect phishing signature for example - which would not work
714 # if we decompose first and only analyze the decomposed attachments.
715 # Disadvantage is that we need to unpack more than
716 # once (bad performance).
718 # should we scan content types inside archives
721 my $rulecache = $self->{rulecache
};
723 my $scan_archives = 0;
725 if (($rulecache->{archivefilter_in
} && !$msginfo->{trusted
}) ||
726 ($rulecache->{archivefilter_out
} && $msginfo->{trusted
})) {
730 if ($scan_archives && !$queue->{vinfo
}) {
732 # unpack all archives - determine contained content types
734 my $decdir = $queue->{dumpdir
} . "/__decoded_archives";
737 my $start = [gettimeofday
];
742 # limits: We use clamav limit for maxfiles, and scan
743 # only 4 levels, timeout of 30 seconds
745 $unpack = Proxmox
::Unpack-
>new (tmpdir
=> $decdir,
747 ctonly
=> 1, # only detect CTs
749 maxfiles
=> $maxfiles);
751 $self->unpack_entity ($unpack, $entity, $msginfo, $queue);
756 $unpack->cleanup() if $unpack;
758 my $elapsed = int(tv_interval
($start) * 1000);
761 $self->log (3, "$queue->{logid}: unpack archive failed ($elapsed ms) - $err");
765 # PHASE 4 - apply rules
766 # on error: exit (cleanup process should do the rest)
767 $msginfo->{maxspamsize
} = $pmg_cfg->get('spam', 'maxspamsize');
768 if ($msginfo->{maxspamsize
} <= 1024*64) {
769 $msginfo->{maxspamsize
} = 1024*64;
772 if ($msginfo->{trusted
}) {
773 my $hide = $pmg_cfg->get('mail', 'hide_received');
774 $entity->head->delete("Received") if $hide;
777 $matching_rules = $self->apply_rules($queue, $msginfo, $entity, $ldap);
782 $self->{errors
} = $queue->{errors
};
785 $self->{errors
} = 1 if $err;
791 my ($csec_end, $usec_end) = gettimeofday
();
793 int (($csec_end-$csec)*1000 + ($usec_end - $usec)/1000);
795 # PHASE 5 - log statistics
796 # on error: log error messages
799 my $dbh = $self->{ruledb
}->{dbh
};
801 foreach my $rid (@$matching_rules) {
802 $where .= $where ?
" OR ID = $rid" : "ID = $rid";
805 $dbh->do ("UPDATE Rule " .
806 "SET Count = Count + 1 " .
810 my $insert_cmds = "SELECT nextval ('cstatistic_id_seq');INSERT INTO CStatistic " .
811 "(CID, RID, ID, Time, Bytes, Direction, Spamlevel, VirusInfo, PTime, Sender) VALUES (" .
812 "$lcid, currval ('cstatistic_id_seq'), currval ('cstatistic_id_seq'),";
814 $insert_cmds .= $queue->{rtime
} . ',';
815 $insert_cmds .= $queue->{bytes
} . ',';
816 $insert_cmds .= $dbh->quote ($msginfo->{trusted
} ?
0 : 1) . ',';
817 $insert_cmds .= ($queue->{sa_score
} || 0) . ',';
818 $insert_cmds .= $dbh->quote ($queue->{vinfo
}) . ',';
819 $insert_cmds .= $time_total . ',';
820 $insert_cmds .= $dbh->quote ($msginfo->{sender
}) . ');';
822 foreach my $r (@{$msginfo->{targets
}}) {
823 my $tmp = $dbh->quote ($r);
824 my $blocked = $queue->{status
}->{$r} eq 'blocked' ?
1 : 0;
825 $insert_cmds .= "INSERT INTO CReceivers (CStatistic_CID, CStatistic_RID, Receiver, Blocked) " .
826 "VALUES ($lcid, currval ('cstatistic_id_seq'), $tmp, '$blocked'); ";
829 $dbh->do ($insert_cmds);
832 # save $err (because log clears $@)
835 $time_total = $time_total/1000;
837 my $ptspam = ($queue->{ptime_spam
} || 0)/1000;
838 my $ptcommtouch = ($queue->{ptime_commtouch
} || 0)/1000;
839 my $ptavira = ($queue->{ptime_avira
} || 0)/1000;
840 my $ptclam = ($queue->{ptime_clam
} || 0)/1000;
842 $self->log (3, "$queue->{logid}: processing time: ${time_total} seconds ($ptcommtouch, $ptspam, $ptclam, $ptavira)");
844 $msginfo->{test_fh
}->close if $opt_testmode;
850 sub process_request
{
852 my $prop = $self->{server
};
853 my $sock = $prop->{client
};
857 # make sure the ldap cache is up to date
858 $self->{ldap
}->update (1);
860 $self->load_config() if $self->{reload_config
};
862 $self->{trusted
} = 0;
863 if ($prop->{sockport
} == $opt_int_port && !$opt_untrusted) {
864 $self->{trusted
} = 1;
867 my $smtp = Proxmox
::SMTP-
>new ($sock);
869 my $maxcount = $max_requests - $prop->{requests
};
871 my $count = $smtp->loop (\
&handle_smtp
, $self, $maxcount);
873 $prop->{requests
} += $count - 1;
879 $self->log (0, $err) if $err;
881 kill (15, $prop->{ppid
}) if $opt_testmode;
883 my $mem = PVE
::ProcFSTools
::read_memory_usage
();
886 $self->log (0, "memory usage: $mem->{size} bytes");
888 if ($self->{errors
}) {
889 $self->log (0, "fast exit because of errors (free $mem->{size} bytes)");
891 } elsif ($mem->{size
} > (300*1024*1024)) {
892 $self->log (0, "fast exit to reduce server load (free $mem->{size} bytes)");
897 $self->done (1) if $err;
900 # test sig_hup with: for ((;;)) ;do kill -HUP `cat /var/run/proxprox.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 ($previous_alarm);
947 # initialize mime system before fork
948 xdg_mime_get_mime_type_for_file
($0);
951 server
=> $server_attr,
954 if (!$opt_testmode) {
961 my $sender ='sender@proxtest.com';
962 my $targets = ['target1@proxtest.com',
963 'target2@proxtest.com',
964 'target3@proxtest.com'];
966 my $smtp = Net
::SMTP-
>new ('127.0.0.1', Port
=> 10023);
968 die "unable to connect $!" if !$smtp;
970 # syslog ('info', "connected to " . $smtp->domain);
972 $smtp->mail ($sender);
973 $smtp->to (@$targets);
977 open (TMP
, $opt_testmode) ||
978 die "unable to upen file '$opt_testmode' - $! :ERROR";
980 $smtp->datasend ($_);
984 $smtp->datasend ("\n");
997 pmg-smtp-filter - the Proxmox mail filter
1001 pmg-smtp-filter [-u] [-t testfile]
1005 Documentation is available at www.proxmox.com