2006-09-14 21:48:37 +02:00
|
|
|
#!/usr/bin/perl -Tw
|
2006-05-31 22:54:03 +02:00
|
|
|
# High performance pre-forking qpsmtpd daemon, Copyright (C) 2006 SoftScan
|
|
|
|
# http://www.softscan.co.uk
|
|
|
|
#
|
|
|
|
# Based on qpsmtpd-forkserver Copyright (C) 2001 Ask Bjoern Hansen
|
|
|
|
# See the LICENSE file for details.
|
|
|
|
#
|
|
|
|
# For more information see http://develooper.com/code/qpsmtpd/
|
|
|
|
|
|
|
|
# safety guards
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
# includes
|
|
|
|
use IO::Socket;
|
|
|
|
use POSIX;
|
|
|
|
use IPC::Shareable(':all');
|
|
|
|
use lib 'lib';
|
|
|
|
use Qpsmtpd::TcpServer::Prefork;
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use Getopt::Long;
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2007-05-18 00:16:27 +02:00
|
|
|
my $has_ipv6 = Qpsmtpd::TcpServer::has_ipv6;
|
|
|
|
|
|
|
|
if ($has_ipv6) {
|
|
|
|
use Socket6;
|
|
|
|
}
|
|
|
|
|
2006-05-31 22:54:03 +02:00
|
|
|
#use Time::HiRes qw(gettimeofday tv_interval);
|
|
|
|
|
|
|
|
# secure shell
|
|
|
|
$ENV{'PATH'} = '/bin:/usr/bin';
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
|
|
|
# version
|
2006-06-01 16:13:44 +02:00
|
|
|
my $VERSION = "1.0";
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2007-09-02 09:32:57 +02:00
|
|
|
# qpsmtpd instances
|
|
|
|
my ($qpsmtpd, $qpsmtpd_base);
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# cmd's needed by IPC
|
2006-05-31 22:54:03 +02:00
|
|
|
my $ipcrm = '/usr/bin/ipcrm';
|
2006-06-01 16:13:44 +02:00
|
|
|
my $ipcs = '/usr/bin/ipcs';
|
2006-05-31 22:54:03 +02:00
|
|
|
my $xargs = '/usr/bin/xargs';
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# vars we need
|
|
|
|
my $chld_shmem; # shared mem to keep track of children (and their connections)
|
2006-05-31 22:54:03 +02:00
|
|
|
my %children;
|
|
|
|
my $chld_pool;
|
|
|
|
my $chld_busy;
|
2008-09-04 13:41:13 +02:00
|
|
|
my @children_term; # terminated children, their death pending processing
|
|
|
|
# by the main loop
|
2006-06-01 16:13:44 +02:00
|
|
|
my $d; # socket
|
|
|
|
|
|
|
|
# default settings
|
2008-05-15 19:07:33 +02:00
|
|
|
my $pid_file;
|
2006-06-01 16:13:44 +02:00
|
|
|
my $d_port = 25;
|
2007-05-18 00:16:27 +02:00
|
|
|
my $d_addr;
|
|
|
|
if ($has_ipv6) {
|
|
|
|
$d_addr = "[::]";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$d_addr = "0.0.0.0";
|
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
my $debug = 0;
|
|
|
|
my $max_children = 15; # max number of child processes to spawn
|
|
|
|
my $idle_children = 5; # number of idle child processes to spawn
|
|
|
|
my $maxconnip = 10;
|
|
|
|
my $child_lifetime = 100; # number of times a child may be reused
|
|
|
|
my $loop_sleep = 30; # seconds main_loop sleeps before checking children
|
|
|
|
my $re_nice = 5; # substracted from parents current nice level
|
|
|
|
my $d_start = 0;
|
|
|
|
my $quiet = 0;
|
|
|
|
my $status = 0;
|
|
|
|
my $signal = '';
|
2006-11-22 17:30:37 +01:00
|
|
|
my $pretty = 0;
|
2008-05-14 21:09:02 +02:00
|
|
|
my $detach = 0;
|
2006-05-31 23:06:40 +02:00
|
|
|
my $user;
|
2006-05-31 22:54:03 +02:00
|
|
|
|
|
|
|
# help text
|
2006-05-31 23:06:40 +02:00
|
|
|
sub usage {
|
2006-06-01 16:13:44 +02:00
|
|
|
print <<"EOT";
|
|
|
|
Usage: qpsmtpd-prefork [ options ]
|
2006-05-31 22:54:03 +02:00
|
|
|
--quiet : Be quiet (even errors are suppressed)
|
2007-05-18 00:16:27 +02:00
|
|
|
--version : Show version information
|
2006-05-31 22:54:03 +02:00
|
|
|
--debug : Enable debug output
|
|
|
|
--interface addr : Interface daemon should listen on (default: $d_addr)
|
|
|
|
--port int : TCP port daemon should listen on (default: $d_port)
|
|
|
|
--max-from-ip int : Limit number of connections from single IP (default: $maxconnip, 0 to disable)
|
|
|
|
--children int : Max number of children that can be spawned (default: $max_children)
|
|
|
|
--idle-children int : Number of idle children to spawn (default: $idle_children, 0 to disable)
|
2006-11-22 17:30:37 +01:00
|
|
|
--pretty-child : Change child process name (default: 0)
|
2006-05-31 23:06:40 +02:00
|
|
|
--user username : User the daemon should run as
|
2006-05-31 22:54:03 +02:00
|
|
|
--pid-file path : Path to pid file
|
|
|
|
--renice-parent int : Subtract value from parent process nice level (default: $re_nice)
|
2008-05-14 21:09:02 +02:00
|
|
|
--detach : detach from controlling terminal (daemonize)
|
2006-05-31 22:54:03 +02:00
|
|
|
--help : This message
|
|
|
|
EOT
|
2006-06-01 16:13:44 +02:00
|
|
|
exit 0;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# get arguments
|
|
|
|
GetOptions(
|
2006-06-01 16:13:44 +02:00
|
|
|
'quiet' => \$quiet,
|
|
|
|
'version' => sub { print "Qpsmtpd Daemon - version $VERSION\n"; exit 0; },
|
|
|
|
'debug' => \$debug,
|
|
|
|
'interface=s' => \$d_addr,
|
|
|
|
'port=i' => \$d_port,
|
|
|
|
'max-from-ip=i' => \$maxconnip,
|
|
|
|
'children=i' => \$max_children,
|
|
|
|
'idle-children=i' => \$idle_children,
|
2006-11-22 17:30:37 +01:00
|
|
|
'pretty-child' => \$pretty,
|
2006-06-01 16:13:44 +02:00
|
|
|
'user=s' => \$user,
|
|
|
|
'renice-parent=i' => \$re_nice,
|
2008-05-14 21:09:02 +02:00
|
|
|
'detach' => \$detach,
|
2008-05-15 19:07:33 +02:00
|
|
|
'pid-file=s' => \$pid_file,
|
2006-06-01 16:13:44 +02:00
|
|
|
'help' => \&usage,
|
|
|
|
) || &usage;
|
|
|
|
|
2008-05-14 21:09:02 +02:00
|
|
|
if ($user =~ /^([\w\-]+)$/) { $user = $1 } else { &usage }
|
2006-09-14 21:48:37 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# set max from ip to max number of children if option is set to disabled
|
|
|
|
$maxconnip = $max_children if ($maxconnip == 0);
|
|
|
|
|
|
|
|
#to fix limit counter error in plugin <hosts_allow>
|
|
|
|
$maxconnip++;
|
|
|
|
|
|
|
|
#ensure that idle_children matches value given to max_children
|
2006-05-31 23:06:40 +02:00
|
|
|
$idle_children = $max_children
|
2006-06-01 16:13:44 +02:00
|
|
|
if (!$idle_children || $idle_children > $max_children || $idle_children < -1);
|
2006-05-31 22:54:03 +02:00
|
|
|
$chld_pool = $idle_children;
|
|
|
|
|
2008-05-15 19:07:33 +02:00
|
|
|
if ($pid_file) {
|
|
|
|
if ($pid_file =~ m#^(/[\w\d/\-.]+)$#) { $pid_file = $1 } else { &usage }
|
|
|
|
if (-e $pid_file) {
|
|
|
|
open PID, "+<$pid_file"
|
|
|
|
or die "open pid_file: $!\n";
|
|
|
|
my $running_pid = <PID> || ''; chomp $running_pid;
|
|
|
|
if ($running_pid =~ /(\d+)/) {
|
|
|
|
$running_pid = $1;
|
|
|
|
die "Found an already running qpsmtpd with pid $running_pid.\n"
|
|
|
|
if (kill 0, $running_pid);
|
|
|
|
}
|
|
|
|
seek PID, 0, 0
|
|
|
|
or die "Could not seek back to beginning of $pid_file: $!\n";
|
|
|
|
truncate PID, 0
|
|
|
|
or die "Could not truncate $pid_file at 0: $!";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
open PID, ">$pid_file"
|
|
|
|
or die "open pid_file: $!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 23:06:40 +02:00
|
|
|
run();
|
2006-05-31 22:54:03 +02:00
|
|
|
|
|
|
|
#start daemon
|
2006-05-31 23:06:40 +02:00
|
|
|
sub run {
|
2006-05-31 22:54:03 +02:00
|
|
|
# get UUID/GUID
|
2008-05-14 21:09:02 +02:00
|
|
|
my ($quid, $qgid, $groups);
|
2006-05-31 23:06:40 +02:00
|
|
|
if ($user) {
|
2008-05-14 21:09:02 +02:00
|
|
|
(undef, undef, $quid, $qgid) = getpwnam $user
|
|
|
|
or die "unable to determine uid/gid for $user\n";
|
|
|
|
$groups = "$qgid $qgid";
|
|
|
|
while (my ($name,$passwd,$gid,$members) = getgrent()) {
|
|
|
|
my @m = split(/ /, $members);
|
|
|
|
if (grep {$_ eq $user} @m) {
|
|
|
|
$groups .= " $gid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endgrent;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2007-05-18 00:16:27 +02:00
|
|
|
my @Socket_opts = (
|
|
|
|
LocalPort => $d_port,
|
|
|
|
LocalAddr => $d_addr,
|
|
|
|
Proto => 'tcp',
|
|
|
|
Listen => SOMAXCONN,
|
|
|
|
Reuse => 1,
|
|
|
|
);
|
2006-05-31 22:54:03 +02:00
|
|
|
# create new socket (used by clients to communicate with daemon)
|
2007-05-18 00:16:27 +02:00
|
|
|
if ($has_ipv6) {
|
|
|
|
$d = IO::Socket::INET6->new(@Socket_opts);
|
|
|
|
}
|
|
|
|
else {
|
2007-07-30 19:44:58 +02:00
|
|
|
$d = IO::Socket::INET->new(@Socket_opts);
|
2007-05-18 00:16:27 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
die "FATAL: Failed to start daemon.\nReason: $!\n(It may be nessesary to "
|
|
|
|
. "wait 20 secs before starting daemon again)\n"
|
|
|
|
unless $d;
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
info("qpsmtpd-prefork daemon, version: $VERSION, staring on host: " .
|
|
|
|
"$d_addr, port: $d_port (user: $user [$<])");
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# reset priority
|
2006-05-31 22:54:03 +02:00
|
|
|
my $old_nice = getpriority(0, 0);
|
|
|
|
my $new_nice = $old_nice - $re_nice;
|
|
|
|
if ($new_nice < 20 && $new_nice > -20) {
|
2006-06-01 16:13:44 +02:00
|
|
|
setpriority(0, 0, $1) if ($new_nice =~ /(\-?\d+)/);
|
|
|
|
info("parent daemon nice level: $1");
|
2006-05-31 23:06:40 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-06-01 16:13:44 +02:00
|
|
|
die "FATAL: new nice level: $new_nice is not between -19 and 19 "
|
|
|
|
. "(old level = $old_nice, renice value = $re_nice)";
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
|
2006-05-31 23:06:40 +02:00
|
|
|
if ($user) {
|
2006-06-01 16:13:44 +02:00
|
|
|
# change UUID/UGID
|
2008-05-14 21:09:02 +02:00
|
|
|
$) = $groups;
|
|
|
|
POSIX::setgid($qgid) or die "unable to change gid: $!\n";
|
|
|
|
POSIX::setuid($quid) or die "unable to change uid: $!\n";
|
|
|
|
$> = $quid;
|
|
|
|
die "FATAL: failed to setuid to user: $user, uid: $quid\n"
|
|
|
|
if ($> != $quid and $> != ($quid - 2**32));
|
2006-05-31 23:06:40 +02:00
|
|
|
}
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# setup shared memory
|
2007-09-03 18:28:34 +02:00
|
|
|
$chld_shmem = shmem($d_port."qpsmtpd", 1);
|
2006-05-31 22:54:03 +02:00
|
|
|
untie $chld_shmem;
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# Interrupt handler
|
2006-05-31 22:54:03 +02:00
|
|
|
$SIG{INT} = $SIG{TERM} = sub {
|
|
|
|
# terminate daemon (and children)
|
|
|
|
my $sig = shift;
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# prevent another signal and disable reaper
|
|
|
|
$SIG{$sig} = $SIG{CHLD} = $SIG{HUP} = 'IGNORE';
|
2008-05-15 19:07:33 +02:00
|
|
|
unlink($pid_file) if $pid_file;
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# close socket
|
|
|
|
$d->close();
|
2006-05-31 22:54:03 +02:00
|
|
|
my $cnt = kill 'INT' => keys %children;
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# cleanup shared memory
|
|
|
|
IPC::Shareable->clean_up;
|
2006-05-31 22:54:03 +02:00
|
|
|
info("shutdown of daemon (and $cnt children)");
|
|
|
|
exit;
|
|
|
|
};
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# Hup handler
|
2006-05-31 22:54:03 +02:00
|
|
|
$SIG{HUP} = sub {
|
2006-06-01 16:13:44 +02:00
|
|
|
# reload qpmstpd plugins
|
2008-06-15 11:28:02 +02:00
|
|
|
$qpsmtpd = $qpsmtpd_base = qpsmtpd_instance('restart' => 1); # reload plugins...
|
2006-06-01 16:13:44 +02:00
|
|
|
$qpsmtpd->load_plugins;
|
|
|
|
kill 'HUP' => keys %children;
|
|
|
|
info("reload daemon requested");
|
2006-05-31 22:54:03 +02:00
|
|
|
};
|
|
|
|
|
2007-09-02 09:32:57 +02:00
|
|
|
# setup qpsmtpd_instance(s), _base is for resetting to a known state
|
|
|
|
# after each connection
|
|
|
|
$qpsmtpd = $qpsmtpd_base = qpsmtpd_instance();
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2008-09-04 13:38:54 +02:00
|
|
|
if ($detach) {
|
|
|
|
open STDIN, '/dev/null' or die "/dev/null: $!";
|
|
|
|
open STDOUT, '>/dev/null' or die "/dev/null: $!";
|
|
|
|
open STDERR, '>&STDOUT' or die "open(stderr): $!";
|
|
|
|
defined (my $pid = fork) or die "fork: $!";
|
|
|
|
exit 0 if $pid;
|
|
|
|
POSIX::setsid or die "setsid: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pid_file) {
|
|
|
|
print PID $$,"\n";
|
|
|
|
close PID;
|
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# child reaper
|
2006-05-31 23:06:40 +02:00
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
spawn_children();
|
|
|
|
main_loop();
|
|
|
|
exit;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# initialize children (only done at daemon startup)
|
|
|
|
sub spawn_children {
|
2006-06-01 16:13:44 +02:00
|
|
|
# block signals while new children are being spawned
|
2006-05-31 23:06:40 +02:00
|
|
|
my $sigset = block_signal(SIGCHLD);
|
2006-06-01 16:13:44 +02:00
|
|
|
for (1 .. $chld_pool) {
|
2006-05-31 23:06:40 +02:00
|
|
|
new_child();
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# reset block signals
|
2006-05-31 23:06:40 +02:00
|
|
|
unblock_signal($sigset);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# cleanup after child dies
|
|
|
|
sub reaper {
|
|
|
|
my $stiff;
|
2006-06-01 16:13:44 +02:00
|
|
|
while (($stiff = waitpid(-1, &WNOHANG)) > 0) {
|
2006-05-31 22:54:03 +02:00
|
|
|
my $res = WEXITSTATUS($?);
|
|
|
|
info("child terminated, pid: $stiff (status $?, res: $res)");
|
2006-06-01 16:13:44 +02:00
|
|
|
delete $children{$stiff}; # delete pid from children
|
|
|
|
# add pid to array so it later can be removed from shared memory
|
2008-09-04 13:41:13 +02:00
|
|
|
push @children_term, $stiff;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2006-05-31 22:54:03 +02:00
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
}
|
|
|
|
|
2008-09-04 13:41:13 +02:00
|
|
|
#main_loop: main loop. Either processes children that have exited or
|
|
|
|
# periodically scans the shared memory for children that are not longer
|
|
|
|
# alive. Spawns new children when necessary.
|
2006-05-31 22:54:03 +02:00
|
|
|
#arg0: void
|
|
|
|
#ret0: void
|
|
|
|
sub main_loop {
|
|
|
|
while (1) {
|
2008-09-04 13:41:13 +02:00
|
|
|
# if there is no child death to process, then sleep EXPR seconds
|
|
|
|
# or until signal (i.e. child death) is received
|
|
|
|
sleep $loop_sleep unless @children_term;
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2008-09-04 13:41:13 +02:00
|
|
|
# block CHLD signals to avoid race
|
2006-05-31 23:06:40 +02:00
|
|
|
my $sigset = block_signal(SIGCHLD);
|
2006-06-01 16:13:44 +02:00
|
|
|
|
2008-09-04 13:41:13 +02:00
|
|
|
# get number of busy children
|
|
|
|
if (@children_term) {
|
|
|
|
# remove dead children info from shared memory
|
|
|
|
$chld_busy = shmem_opt(undef, \@children_term, undef, undef);
|
|
|
|
@children_term = ();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# just check the shared memory
|
|
|
|
$chld_busy = shmem_opt(undef, undef, undef, undef, 1);
|
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# calculate children in pool (if valid busy children number)
|
2006-05-31 22:54:03 +02:00
|
|
|
if (defined($chld_busy)) {
|
2006-06-01 16:13:44 +02:00
|
|
|
info("busy children: $chld_busy");
|
|
|
|
$chld_pool = $chld_busy + $idle_children;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# ensure pool limit is max_children
|
|
|
|
$chld_pool = $max_children if ($chld_pool > $max_children);
|
|
|
|
|
|
|
|
# spawn children
|
|
|
|
for (my $i = scalar(keys %children) ; $i < $chld_pool ; $i++) {
|
|
|
|
new_child(); # add to the child pool
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
info( "children pool: $chld_pool (currently spawned: "
|
|
|
|
. scalar(keys %children)
|
|
|
|
. ")");
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# unblock signals
|
2006-05-31 23:06:40 +02:00
|
|
|
unblock_signal($sigset);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# block_signal: block signals
|
|
|
|
# arg0..n: int with signal(s) to block
|
|
|
|
# ret0: ref str with sigset (used to later unblock signal)
|
2006-05-31 22:54:03 +02:00
|
|
|
sub block_signal {
|
2006-06-01 16:13:44 +02:00
|
|
|
my @signal = @_; #arg0..n
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
my ($sigset, $blockset);
|
|
|
|
|
|
|
|
$sigset = POSIX::SigSet->new();
|
|
|
|
$blockset = POSIX::SigSet->new(@signal);
|
|
|
|
sigprocmask(SIG_BLOCK, $blockset, $sigset)
|
|
|
|
or die "Could not block @signal signals: $!\n";
|
|
|
|
|
|
|
|
return ($sigset);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# unblock_signal: unblock/reset and receive pending signals
|
|
|
|
# arg0: ref str with sigset
|
|
|
|
# ret0: void
|
2006-05-31 22:54:03 +02:00
|
|
|
sub unblock_signal {
|
2006-06-01 16:13:44 +02:00
|
|
|
my $sigset = shift; # arg0
|
|
|
|
sigprocmask(SIG_SETMASK, $sigset)
|
|
|
|
or die "Could not restore signals: $!\n";
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# new_child: initialize new child
|
|
|
|
# arg0: void
|
|
|
|
# ret0: void
|
2006-05-31 22:54:03 +02:00
|
|
|
sub new_child {
|
|
|
|
# daemonize away from the parent process
|
|
|
|
my $pid;
|
2006-06-01 16:13:44 +02:00
|
|
|
die "Cannot fork child: $!\n" unless defined($pid = fork);
|
2006-05-31 22:54:03 +02:00
|
|
|
if ($pid) {
|
2006-06-01 16:13:44 +02:00
|
|
|
# in parent
|
|
|
|
$children{$pid} = 1;
|
|
|
|
info("new child, pid: $pid");
|
|
|
|
return;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
|
2006-05-31 22:54:03 +02:00
|
|
|
# in child
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# reset priority
|
|
|
|
setpriority 0, 0, getpriority(0, 0) + $re_nice;
|
2006-05-31 22:54:03 +02:00
|
|
|
|
|
|
|
# reset signals
|
2006-06-01 16:13:44 +02:00
|
|
|
my $sigset = POSIX::SigSet->new();
|
2006-05-31 22:54:03 +02:00
|
|
|
my $blockset = POSIX::SigSet->new(SIGCHLD);
|
2006-06-01 16:13:44 +02:00
|
|
|
sigprocmask(SIG_UNBLOCK, $blockset, $sigset)
|
|
|
|
or die "Could not unblock SIGHUP signal: $!\n";
|
2006-05-31 22:54:03 +02:00
|
|
|
$SIG{CHLD} = $SIG{INT} = $SIG{TERM} = $SIG{ALRM} = 'DEFAULT';
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# child should exit if it receives HUP signal (note: blocked while child
|
|
|
|
# is busy, but restored once done)
|
2006-05-31 23:06:40 +02:00
|
|
|
$SIG{HUP} = sub {
|
|
|
|
info("signal HUP received, going to exit");
|
2006-06-01 16:13:44 +02:00
|
|
|
exit 1;
|
|
|
|
};
|
|
|
|
|
2006-05-31 22:54:03 +02:00
|
|
|
# continue to accept connections until "old age" is reached
|
2006-06-01 16:13:44 +02:00
|
|
|
for (my $i = 0 ; $i < $child_lifetime ; $i++) {
|
|
|
|
# accept a connection
|
2006-11-22 17:30:37 +01:00
|
|
|
if ( $pretty ) {
|
|
|
|
$ENV{PROCESS} = $0 if not defined $ENV{PROCESS}; # 1st time only
|
|
|
|
$0 = 'qpsmtpd child'; # set pretty child name in process listing
|
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
my ($client, $iinfo) = $d->accept()
|
|
|
|
or die
|
|
|
|
"failed to create new object - $!"; # wait here until client connects
|
2006-06-01 16:13:44 +02:00
|
|
|
info("connect from: " . $client->peerhost . ":" . $client->peerport);
|
2007-09-02 09:32:57 +02:00
|
|
|
|
|
|
|
# clear a previously running instance by cloning the base:
|
|
|
|
$qpsmtpd = $qpsmtpd_base;
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# set STDIN/STDOUT and autoflush
|
2007-09-25 08:10:23 +02:00
|
|
|
# ... no longer use POSIX::dup2: it failes after a few
|
|
|
|
# million connections
|
|
|
|
close(STDIN);
|
|
|
|
open(STDIN, "+<&".fileno($client))
|
|
|
|
or die "unable to duplicate filehandle to STDIN - $!";
|
|
|
|
|
|
|
|
close(STDOUT);
|
|
|
|
open(STDOUT, "+>&".fileno($client))
|
|
|
|
or die "unable to duplicate filehandle to STDOUT - $!";
|
|
|
|
select(STDOUT);
|
2006-06-01 16:13:44 +02:00
|
|
|
$| = 1;
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# connection recieved, block signals
|
2006-05-31 23:06:40 +02:00
|
|
|
my $sigset = block_signal(SIGHUP);
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# start a session if connection looks valid
|
2007-05-18 00:16:27 +02:00
|
|
|
qpsmtpd_session($client, $iinfo, $qpsmtpd) if ($iinfo);
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# close connection and cleanup
|
|
|
|
$client->shutdown(2);
|
|
|
|
|
|
|
|
# unset block and receive pending signals
|
|
|
|
unblock_signal($sigset);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
exit; # this child has reached its end-of-life
|
|
|
|
}
|
|
|
|
|
|
|
|
# respond to client
|
2006-06-01 16:13:44 +02:00
|
|
|
# arg0: ref to socket object (client)
|
2006-05-31 22:54:03 +02:00
|
|
|
# arg1: int with SMTP reply code
|
|
|
|
# arg2: arr with message
|
|
|
|
# ret0: int 0|1 (0 = failure, 1 = success)
|
|
|
|
sub respond_client {
|
2006-06-01 16:13:44 +02:00
|
|
|
my ($client, $code, @message) = @_;
|
|
|
|
$client->autoflush(1);
|
|
|
|
while (my $msg = shift @message) {
|
|
|
|
my $line = $code . (@message ? "-" : " ") . $msg;
|
|
|
|
info("reply to client: <$line>");
|
|
|
|
print $client "$line\r\n"
|
2006-05-31 23:06:40 +02:00
|
|
|
or (info("Could not print [$line]: $!"), return 0);
|
2006-06-01 16:13:44 +02:00
|
|
|
}
|
|
|
|
return 1;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# qpsmtpd_instance: setup qpsmtpd instance
|
|
|
|
# arg0: void
|
|
|
|
# ret0: ref to qpsmtpd_instance
|
2007-09-02 09:32:57 +02:00
|
|
|
sub qpsmtpd_instance {
|
2008-06-15 11:28:02 +02:00
|
|
|
my %args = @_;
|
|
|
|
my $qpsmtpd = Qpsmtpd::TcpServer::Prefork->new(%args);
|
2006-05-31 22:54:03 +02:00
|
|
|
$qpsmtpd->load_plugins;
|
|
|
|
$qpsmtpd->spool_dir;
|
|
|
|
$qpsmtpd->size_threshold;
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
return ($qpsmtpd);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# shmem: tie to shared memory hash
|
|
|
|
# arg0: str with glue
|
|
|
|
# arg1: int 0|1 (0 = don't create shmem, 1 = create shmem)
|
|
|
|
# ret0: ref to shared hash
|
2006-05-31 22:54:03 +02:00
|
|
|
sub shmem {
|
2006-06-01 16:13:44 +02:00
|
|
|
my $glue = shift; #arg0
|
|
|
|
my $create = shift || 0; #arg1
|
|
|
|
|
|
|
|
my %options = (
|
|
|
|
create => $create,
|
|
|
|
exclusive => 0,
|
|
|
|
mode => 0640,
|
|
|
|
destroy => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
my %shmem_hash;
|
|
|
|
eval {
|
|
|
|
tie %shmem_hash, 'IPC::Shareable', $glue, {%options}
|
|
|
|
|| die "unable to tie to shared memory - $!";
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
info("$@");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (\%shmem_hash);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# shmem_opt: connect to shared memory and perform options
|
|
|
|
# arg0: ref to hash where shared memory should be copied to
|
|
|
|
# arg1: ref to arr with pid(s) to delete
|
|
|
|
# arg2: int with pid to add (key)
|
|
|
|
# arg3: str with packed iaddr to add (value)
|
|
|
|
# arg4: int 0|1 check and cleanup shared memory (0 = no, 1 = yes - default 0)
|
|
|
|
# ret0: int with number of busy children (undef if error)
|
2006-05-31 22:54:03 +02:00
|
|
|
sub shmem_opt {
|
2006-06-01 16:13:44 +02:00
|
|
|
my $ref_shmem = shift; #arg0
|
|
|
|
my $ref_pid_del = shift; #arg1
|
|
|
|
my $pid_add_key = shift; #arg2
|
|
|
|
my $pid_add_value = shift; #arg3
|
|
|
|
my $check = shift || 0; #arg4
|
|
|
|
|
|
|
|
# check arguments
|
|
|
|
if ( (defined($pid_add_key) && !defined($pid_add_value))
|
|
|
|
|| (!defined($pid_add_key) && defined($pid_add_value)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($chld_shmem, $chld_busy);
|
|
|
|
eval {
|
2007-09-03 18:28:34 +02:00
|
|
|
$chld_shmem = &shmem($d_port."qpsmtpd", 0); #connect to shared memory hash
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
if (tied %{$chld_shmem}) {
|
|
|
|
# perform options
|
|
|
|
(tied %{$chld_shmem})->shlock(LOCK_EX);
|
|
|
|
|
|
|
|
# delete
|
|
|
|
if ($ref_pid_del) {
|
|
|
|
foreach my $pid_del (@{$ref_pid_del}) {
|
|
|
|
delete $$chld_shmem{$pid_del};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# add
|
|
|
|
$$chld_shmem{$pid_add_key} = $pid_add_value if ($pid_add_key);
|
|
|
|
# copy
|
|
|
|
%{$ref_shmem} = %{$chld_shmem} if ($ref_shmem);
|
|
|
|
if ($check) {
|
|
|
|
# loop through pid list and delete orphaned processes
|
|
|
|
foreach my $pid (keys %{$chld_shmem}) {
|
|
|
|
if (!kill 0, $pid) {
|
|
|
|
delete $$chld_shmem{$pid};
|
|
|
|
warn("orphaned child, pid: $pid removed from memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# count number of busy children
|
|
|
|
$chld_busy = scalar(keys %{$chld_shmem});
|
|
|
|
(tied %{$chld_shmem})->shunlock;
|
|
|
|
|
|
|
|
# untie from shared memory
|
|
|
|
untie $chld_shmem || die "unable to untie from shared memory";
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# check for error
|
|
|
|
if ($@) {
|
|
|
|
undef($chld_busy);
|
|
|
|
warn("$@");
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
return ($chld_busy);
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# info: write info
|
|
|
|
# arg0: str with debug text
|
|
|
|
sub info {
|
2006-06-01 16:13:44 +02:00
|
|
|
my $text = shift; #arg0
|
|
|
|
return if (!$debug);
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
|
2006-05-31 22:54:03 +02:00
|
|
|
my $nowtime = sprintf "%02d/%02d/%02d %02d:%02d:%02d", $mday, $mon + 1,
|
|
|
|
$year + 1900, $hour, $min, $sec;
|
|
|
|
|
|
|
|
chomp($text);
|
2006-05-31 23:06:40 +02:00
|
|
|
print STDERR "$nowtime:$$: $text\n";
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# start qpmstpd session
|
2006-05-31 22:54:03 +02:00
|
|
|
# arg0: ref to socket object
|
2007-05-18 00:16:27 +02:00
|
|
|
# arg1: ref to socket object
|
|
|
|
# arg2: ref to qpsmtpd instance
|
2006-05-31 22:54:03 +02:00
|
|
|
# ret0: void
|
|
|
|
sub qpsmtpd_session {
|
2006-06-01 16:13:44 +02:00
|
|
|
my $client = shift; #arg0
|
2007-05-18 00:16:27 +02:00
|
|
|
my $iinfo = shift; #arg1
|
|
|
|
my $qpsmtpd = shift; #arg2
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# get local/remote hostname, port and ip address
|
2007-05-18 00:16:27 +02:00
|
|
|
my ($port, $iaddr, $lport, $laddr, $nto_iaddr, $nto_laddr) = Qpsmtpd::TcpServer::lrpip($d, $client, $iinfo);
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# get current connected ip addresses (from shared memory)
|
2006-05-31 22:54:03 +02:00
|
|
|
my %children;
|
2006-05-31 23:06:40 +02:00
|
|
|
shmem_opt(\%children, undef, $$, $iaddr);
|
2006-06-01 16:13:44 +02:00
|
|
|
|
2006-05-31 23:06:40 +02:00
|
|
|
my ($rc, @msg) =
|
|
|
|
$qpsmtpd->run_hooks(
|
|
|
|
"pre-connection",
|
2007-05-18 00:16:27 +02:00
|
|
|
remote_ip => $nto_iaddr,
|
2006-06-01 16:13:44 +02:00
|
|
|
remote_port => $port,
|
2007-05-18 00:16:27 +02:00
|
|
|
local_ip => $nto_laddr,
|
2006-06-01 16:13:44 +02:00
|
|
|
local_port => $lport,
|
|
|
|
max_conn_ip => $maxconnip,
|
|
|
|
child_addrs => [values %children],
|
|
|
|
);
|
2006-05-31 23:06:40 +02:00
|
|
|
if ( $rc == DENYSOFT
|
|
|
|
|| $rc == DENYSOFT_DISCONNECT
|
|
|
|
|| $rc == DENY
|
|
|
|
|| $rc == DENY_DISCONNECT)
|
|
|
|
{
|
2006-06-01 16:13:44 +02:00
|
|
|
#smtp return code to reply client with (seed with soft deny)
|
|
|
|
my $rc_reply = 451;
|
|
|
|
unless ($msg[0]) {
|
|
|
|
if ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) {
|
|
|
|
@msg = ("Sorry, try again later");
|
2006-05-31 23:06:40 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-06-01 16:13:44 +02:00
|
|
|
@msg = ("Sorry, service not available to you");
|
|
|
|
$rc_reply = 550;
|
|
|
|
}
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-05-31 23:06:40 +02:00
|
|
|
respond_client($client, $rc_reply, @msg);
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# remove pid from shared memory
|
|
|
|
shmem_opt(undef, [$$], undef, undef);
|
|
|
|
|
|
|
|
# retur so child can be reused
|
|
|
|
return;
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
2006-06-01 16:13:44 +02:00
|
|
|
|
2006-05-31 22:54:03 +02:00
|
|
|
# all children should have different seeds, to prevent conflicts
|
2006-06-01 16:13:44 +02:00
|
|
|
srand(time ^ ($$ + ($$ << 15)));
|
|
|
|
|
|
|
|
# ALRM handler
|
|
|
|
$SIG{ALRM} = sub {
|
|
|
|
print $client "421 Connection Timed Out\n";
|
2006-05-31 23:06:40 +02:00
|
|
|
info("Connection Timed Out");
|
2006-06-01 16:13:44 +02:00
|
|
|
|
|
|
|
# kill the child
|
|
|
|
exit 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
# set enviroment variables
|
2007-05-18 00:16:27 +02:00
|
|
|
($ENV{TCPLOCALIP}, $ENV{TCPREMOTEIP}, $ENV{TCPREMOTEHOST}) = Qpsmtpd::TcpServer::tcpenv($nto_laddr, $nto_iaddr);
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# run qpmsptd functions
|
2006-05-31 22:54:03 +02:00
|
|
|
$SIG{__DIE__} = 'DEFAULT';
|
|
|
|
eval {
|
2006-06-01 16:13:44 +02:00
|
|
|
$qpsmtpd->start_connection(
|
|
|
|
local_ip => $ENV{TCPLOCALIP},
|
|
|
|
local_port => $lport,
|
|
|
|
remote_ip => $ENV{TCPREMOTEIP},
|
|
|
|
remote_port => $client->peerport,
|
|
|
|
);
|
|
|
|
$qpsmtpd->run();
|
|
|
|
$qpsmtpd->run_hooks("post-connection");
|
2008-05-09 19:40:31 +02:00
|
|
|
$qpsmtpd->connection->reset;
|
2006-05-31 22:54:03 +02:00
|
|
|
};
|
2006-06-01 16:13:44 +02:00
|
|
|
if ($@ !~ /^(disconnect_tcpserver|died while reading from STDIN)/) {
|
|
|
|
warn("$@");
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
# child is now idle again so remove it's pid from shared mem
|
|
|
|
shmem_opt(undef, [$$], undef, undef);
|
2006-05-31 22:54:03 +02:00
|
|
|
|
2006-06-01 16:13:44 +02:00
|
|
|
info("remote host: $ENV{TCPREMOTEIP} left...");
|
2006-05-31 22:54:03 +02:00
|
|
|
}
|