2006-12-08 20:46:18 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use lib "./lib";
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
BEGIN {
|
|
|
|
delete $ENV{ENV};
|
|
|
|
delete $ENV{BASH_ENV};
|
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/var/qmail/bin:/usr/local/bin';
|
|
|
|
}
|
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
# Profiling - requires Devel::Profiler 0.05
|
|
|
|
#BEGIN { $Devel::Profiler::NO_INIT = 1; }
|
|
|
|
#use Devel::Profiler;
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
use strict;
|
|
|
|
use vars qw($DEBUG);
|
|
|
|
use FindBin qw();
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# TODO: need to make this taint friendly
|
|
|
|
use lib "$FindBin::Bin/lib";
|
|
|
|
use Danga::Socket;
|
|
|
|
use Danga::Client;
|
|
|
|
use Qpsmtpd::PollServer;
|
|
|
|
use Qpsmtpd::ConfigServer;
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use IO::Socket;
|
|
|
|
use Carp;
|
|
|
|
use POSIX qw(WNOHANG);
|
|
|
|
use Getopt::Long;
|
2007-12-03 18:45:31 +01:00
|
|
|
use List::Util qw(shuffle);
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
$|++;
|
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
use Socket
|
|
|
|
qw(SOMAXCONN IPPROTO_TCP SO_KEEPALIVE TCP_NODELAY SOL_SOCKET AF_UNIX SOCK_STREAM PF_UNSPEC);
|
2006-12-08 20:46:18 +01:00
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
$SIG{'PIPE'} = "IGNORE"; # handled manually
|
2006-12-08 20:46:18 +01:00
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
$DEBUG = 0;
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
my $CONFIG_PORT = 20025;
|
|
|
|
my $CONFIG_LOCALADDR = '127.0.0.1';
|
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
my $PORT = 2525;
|
|
|
|
my $LOCALADDR = '0.0.0.0';
|
|
|
|
my $PROCS = 1;
|
|
|
|
my $USER = (getpwuid $>)[0]; # user to suid to
|
|
|
|
$USER = "smtpd" if $USER eq "root";
|
|
|
|
my $PAUSED = 0;
|
|
|
|
my $NUMACCEPT = 20;
|
|
|
|
my $PID_FILE = '';
|
2008-03-13 22:20:56 +01:00
|
|
|
my $ACCEPT_RSET;
|
2013-04-21 06:34:07 +02:00
|
|
|
my $DETACH; # daemonize on startup
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
# make sure we don't spend forever doing accept()
|
|
|
|
use constant ACCEPT_MAX => 1000;
|
|
|
|
|
|
|
|
sub reset_num_accept {
|
|
|
|
$NUMACCEPT = 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub help {
|
|
|
|
print <<EOT;
|
|
|
|
Usage:
|
|
|
|
qpsmtpd [OPTIONS]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-l, --listen-address addr : listen on a specific address; default 0.0.0.0
|
|
|
|
-p, --port P : listen on a specific port; default 2525
|
2008-03-13 20:50:01 +01:00
|
|
|
--config-port : config server port; default 20025
|
2006-12-08 20:46:18 +01:00
|
|
|
-u, --user U : run as a particular user; defualt 'smtpd'
|
|
|
|
-j, --procs J : spawn J processes; default 1
|
2007-10-04 17:18:57 +02:00
|
|
|
-d, --detach : detach from controlling terminal (daemonize)
|
|
|
|
--pid-file P : print main servers PID to file P
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
-h, --help : this page
|
|
|
|
--use-poll : force use of poll() instead of epoll()/kqueue()
|
|
|
|
EOT
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOptions(
|
2013-04-21 06:34:07 +02:00
|
|
|
'p|port=i' => \$PORT,
|
|
|
|
'l|listen-address=s' => \$LOCALADDR,
|
|
|
|
'j|procs=i' => \$PROCS,
|
|
|
|
'v|verbose+' => \$DEBUG,
|
|
|
|
'u|user=s' => \$USER,
|
|
|
|
'pid-file=s' => \$PID_FILE,
|
|
|
|
'd|detach' => \$DETACH,
|
|
|
|
'h|help' => \&help,
|
|
|
|
'config-port=i' => \$CONFIG_PORT,
|
|
|
|
)
|
|
|
|
|| help();
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
# detaint the commandline
|
2013-04-21 06:34:07 +02:00
|
|
|
if ($PORT =~ /^(\d+)$/) { $PORT = $1 }
|
|
|
|
else { &help }
|
|
|
|
if ($LOCALADDR =~ /^([\d\w\-.]+)$/) { $LOCALADDR = $1 }
|
|
|
|
else { &help }
|
|
|
|
if ($USER =~ /^([\w\-]+)$/) { $USER = $1 }
|
|
|
|
else { &help }
|
|
|
|
if ($PROCS =~ /^(\d+)$/) { $PROCS = $1 }
|
|
|
|
else { &help }
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
sub force_poll {
|
2013-04-21 06:34:07 +02:00
|
|
|
$Danga::Socket::HaveEpoll = 0;
|
2006-12-08 20:46:18 +01:00
|
|
|
$Danga::Socket::HaveKQueue = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
my $POLL = "with "
|
|
|
|
. (
|
|
|
|
$Danga::Socket::HaveEpoll ? "epoll()"
|
|
|
|
: $Danga::Socket::HaveKQueue ? "kqueue()"
|
|
|
|
: "poll()"
|
|
|
|
);
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
my $SERVER;
|
|
|
|
my $CONFIG_SERVER;
|
|
|
|
|
2007-12-03 22:37:45 +01:00
|
|
|
use constant READY => 1;
|
|
|
|
use constant ACCEPTING => 2;
|
|
|
|
use constant RESTARTING => 999;
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my %childstatus = ();
|
2007-12-03 18:45:31 +01:00
|
|
|
|
2007-10-04 17:18:57 +02:00
|
|
|
if ($PID_FILE && -r $PID_FILE) {
|
|
|
|
open PID, "<$PID_FILE"
|
2013-04-21 06:34:07 +02:00
|
|
|
or die "open_pidfile $PID_FILE: $!\n";
|
|
|
|
my $running_pid = <PID> || '';
|
|
|
|
chomp $running_pid;
|
2007-10-04 17:18:57 +02:00
|
|
|
if ($running_pid =~ /^(\d+)/) {
|
2013-04-21 06:34:07 +02:00
|
|
|
if (kill 0, $running_pid) {
|
|
|
|
die "Found an already running qpsmtpd with pid $running_pid.\n";
|
|
|
|
}
|
2007-10-04 17:18:57 +02:00
|
|
|
}
|
|
|
|
close(PID);
|
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
run_as_server();
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
sub _fork {
|
|
|
|
my $pid = fork;
|
|
|
|
if (!defined($pid)) { die "Cannot fork: $!" }
|
|
|
|
return $pid if $pid;
|
|
|
|
|
|
|
|
# Fixup Net::DNS randomness after fork
|
|
|
|
srand($$ ^ time);
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
local $^W;
|
|
|
|
delete $INC{'Net/DNS/Header.pm'};
|
|
|
|
require Net::DNS::Header;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# cope with different versions of Net::DNS
|
|
|
|
eval {
|
|
|
|
$Net::DNS::Resolver::global{id} = 1;
|
2013-04-21 06:34:07 +02:00
|
|
|
$Net::DNS::Resolver::global{id} =
|
|
|
|
int(rand(Net::DNS::Resolver::MAX_ID()));
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# print "Next DNS ID: $Net::DNS::Resolver::global{id}\n";
|
|
|
|
};
|
|
|
|
if ($@) {
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# print "Next DNS ID: " . Net::DNS::Header::nextid() . "\n";
|
|
|
|
}
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# Fixup lost kqueue after fork
|
|
|
|
$Danga::Socket::HaveKQueue = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub spawn_child {
|
|
|
|
my $plugin_loader = shift || Qpsmtpd::SMTP->new;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
|
|
|
socketpair(my $reader, my $writer, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
|
|
|
|
|| die "Unable to create a pipe";
|
2007-12-03 18:45:31 +01:00
|
|
|
$writer->autoflush(1);
|
2007-12-03 22:37:45 +01:00
|
|
|
$reader->autoflush(1);
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
if (my $pid = _fork) {
|
2007-12-03 22:37:45 +01:00
|
|
|
$childstatus{$pid} = $writer;
|
2006-12-08 20:46:18 +01:00
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
$SIG{CHLD} = $SIG{INT} = $SIG{TERM} = 'DEFAULT';
|
2006-12-08 20:46:18 +01:00
|
|
|
$SIG{PIPE} = 'IGNORE';
|
2007-12-03 22:37:45 +01:00
|
|
|
$SIG{HUP} = 'IGNORE';
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
close $CONFIG_SERVER;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
Qpsmtpd::PollServer->Reset;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 22:37:45 +01:00
|
|
|
Qpsmtpd::PollServer->OtherFds(
|
2013-04-21 06:34:07 +02:00
|
|
|
fileno($reader) => sub { command_handler($reader) },
|
|
|
|
fileno($SERVER) => \&accept_handler,);
|
2006-12-08 20:46:18 +01:00
|
|
|
|
2008-03-13 22:20:56 +01:00
|
|
|
$ACCEPT_RSET = Danga::Socket->AddTimer(30, \&reset_num_accept);
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
$plugin_loader->run_hooks('post-fork');
|
|
|
|
|
|
|
|
Qpsmtpd::PollServer->EventLoop();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2007-12-03 22:37:45 +01:00
|
|
|
# Note this is broken on KQueue because it requires that it handle signals itself or it breaks the event loop.
|
2007-12-03 18:45:31 +01:00
|
|
|
sub sig_hup {
|
2007-12-03 22:37:45 +01:00
|
|
|
for my $writer (values %childstatus) {
|
2007-12-03 18:45:31 +01:00
|
|
|
print $writer "hup\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
sub sig_chld {
|
|
|
|
my $spawn_count = 0;
|
2013-04-21 06:34:07 +02:00
|
|
|
while ((my $child = waitpid(-1, WNOHANG)) > 0) {
|
2006-12-08 20:46:18 +01:00
|
|
|
if (!defined $childstatus{$child}) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
last unless $child > 0;
|
|
|
|
print "SIGCHLD: child $child died\n";
|
|
|
|
delete $childstatus{$child};
|
|
|
|
$spawn_count++;
|
|
|
|
}
|
|
|
|
if ($spawn_count) {
|
2013-04-21 06:34:07 +02:00
|
|
|
for (1 .. $spawn_count) {
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# restart a new child if in poll server mode
|
|
|
|
my $pid = spawn_child();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$SIG{CHLD} = \&sig_chld;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub HUNTSMAN {
|
|
|
|
$SIG{CHLD} = 'DEFAULT';
|
|
|
|
kill 'INT' => keys %childstatus;
|
2007-10-04 17:18:57 +02:00
|
|
|
if ($PID_FILE && -e $PID_FILE) {
|
|
|
|
unlink $PID_FILE or ::log(LOGERROR, "unlink: $PID_FILE: $!");
|
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_as_server {
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# establish SERVER socket, bind and listen.
|
2013-04-21 06:34:07 +02:00
|
|
|
$SERVER = IO::Socket::INET->new(
|
|
|
|
LocalPort => $PORT,
|
2006-12-08 20:46:18 +01:00
|
|
|
LocalAddr => $LOCALADDR,
|
|
|
|
Type => SOCK_STREAM,
|
|
|
|
Proto => IPPROTO_TCP,
|
|
|
|
Blocking => 0,
|
|
|
|
Reuse => 1,
|
2013-04-21 06:34:07 +02:00
|
|
|
Listen => SOMAXCONN
|
|
|
|
)
|
|
|
|
or die "Error creating server $LOCALADDR:$PORT : $@\n";
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
IO::Handle::blocking($SERVER, 0);
|
|
|
|
binmode($SERVER, ':raw');
|
2013-04-21 06:34:07 +02:00
|
|
|
|
|
|
|
$CONFIG_SERVER =
|
|
|
|
IO::Socket::INET->new(
|
|
|
|
LocalPort => $CONFIG_PORT,
|
|
|
|
LocalAddr => $CONFIG_LOCALADDR,
|
|
|
|
Type => SOCK_STREAM,
|
|
|
|
Proto => IPPROTO_TCP,
|
|
|
|
Blocking => 0,
|
|
|
|
Reuse => 1,
|
|
|
|
Listen => 1
|
|
|
|
)
|
|
|
|
or die "Error creating server $CONFIG_LOCALADDR:$CONFIG_PORT : $@\n";
|
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
IO::Handle::blocking($CONFIG_SERVER, 0);
|
|
|
|
binmode($CONFIG_SERVER, ':raw');
|
|
|
|
|
2007-05-27 01:38:09 +02:00
|
|
|
# Drop priviledges
|
2013-04-21 06:34:07 +02:00
|
|
|
my (undef, undef, $quid, $qgid) = getpwnam $USER
|
|
|
|
or die "unable to determine uid/gid for $USER\n";
|
2008-03-13 20:56:25 +01:00
|
|
|
my $groups = "$qgid $qgid";
|
|
|
|
while (my (undef, undef, $gid, $members) = getgrent) {
|
|
|
|
my @m = split(/ /, $members);
|
|
|
|
if (grep { $_ eq $USER } @m) {
|
|
|
|
$groups .= " $gid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endgrent;
|
|
|
|
$) = $groups;
|
2013-04-21 06:34:07 +02:00
|
|
|
POSIX::setgid($qgid)
|
|
|
|
or die "unable to change gid: $!\n";
|
|
|
|
POSIX::setuid($quid)
|
|
|
|
or die "unable to change uid: $!\n";
|
2007-05-27 01:38:09 +02:00
|
|
|
$> = $quid;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# Load plugins here
|
|
|
|
my $plugin_loader = Qpsmtpd::SMTP->new();
|
|
|
|
$plugin_loader->load_plugins;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2008-09-04 13:38:54 +02:00
|
|
|
if ($DETACH) {
|
2013-04-21 06:34:07 +02:00
|
|
|
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: $!";
|
2008-09-04 13:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($PID_FILE) {
|
2013-04-21 06:34:07 +02:00
|
|
|
open PID, ">$PID_FILE" || die "$PID_FILE: $!";
|
|
|
|
print PID $$, "\n";
|
2008-09-04 13:38:54 +02:00
|
|
|
close PID;
|
|
|
|
}
|
2013-04-21 06:34:07 +02:00
|
|
|
|
|
|
|
$plugin_loader->log(LOGINFO,
|
|
|
|
'Running as user '
|
|
|
|
. (getpwuid($>) || $>)
|
|
|
|
. ', group '
|
|
|
|
. (getgrgid($)) || $))
|
|
|
|
);
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
$SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
|
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
######################
|
2013-04-21 06:34:07 +02:00
|
|
|
# more Profiling code
|
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
=pod
|
|
|
|
$plugin_loader->run_hooks('post-fork');
|
|
|
|
|
|
|
|
Devel::Profiler->set_options(
|
|
|
|
bad_subs => [qw(Danga::Socket::EventLoop)],
|
|
|
|
sub_filter => sub {
|
|
|
|
my ($pkg, $sub) = @_;
|
|
|
|
return 0 if $sub eq 'AUTOLOAD';
|
|
|
|
return 0 if $pkg =~ /ParaDNS::XS/;
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Devel::Profiler->init();
|
|
|
|
|
|
|
|
Qpsmtpd::PollServer->OtherFds(
|
|
|
|
fileno($SERVER) => \&accept_handler,
|
|
|
|
fileno($CONFIG_SERVER) => \&config_handler, );
|
|
|
|
|
|
|
|
Qpsmtpd::PollServer->EventLoop;
|
|
|
|
exit;
|
|
|
|
=cut
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2008-03-13 20:50:01 +01:00
|
|
|
#####################
|
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
for (1 .. $PROCS) {
|
2007-12-03 18:45:31 +01:00
|
|
|
my $pid = spawn_child($plugin_loader);
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
2013-04-21 06:34:07 +02:00
|
|
|
$plugin_loader->log(LOGDEBUG,
|
|
|
|
"Listening on $PORT with $PROCS children $POLL");
|
2007-12-03 18:45:31 +01:00
|
|
|
$SIG{CHLD} = \&sig_chld;
|
|
|
|
$SIG{HUP} = \&sig_hup;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
|
|
|
Qpsmtpd::PollServer->OtherFds(fileno($CONFIG_SERVER) => \&config_handler,);
|
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
Qpsmtpd::PollServer->EventLoop;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
exit;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub config_handler {
|
|
|
|
my $csock = $CONFIG_SERVER->accept();
|
|
|
|
if (!$csock) {
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# warn("accept failed on config server: $!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
binmode($csock, ':raw');
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
printf("Config server connection\n") if $DEBUG;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
IO::Handle::blocking($csock, 0);
|
|
|
|
setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
my $client = Qpsmtpd::ConfigServer->new($csock);
|
|
|
|
$client->watch_read(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
sub command_handler {
|
|
|
|
my $reader = shift;
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
chomp(my $command = <$reader>);
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
#print "Got command: $command\n";
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
my $real_command = "cmd_$command";
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
no strict 'refs';
|
2007-12-03 22:37:45 +01:00
|
|
|
$real_command->();
|
2007-12-03 18:45:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_hup {
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
# clear cache
|
2007-12-03 22:37:45 +01:00
|
|
|
print "Clearing cache\n";
|
2007-12-03 18:45:31 +01:00
|
|
|
Qpsmtpd::clear_config_cache();
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
# should also reload modules... but can't do that yet.
|
|
|
|
}
|
|
|
|
|
|
|
|
# Accept all new connections
|
2007-12-03 22:37:45 +01:00
|
|
|
sub accept_handler {
|
2006-12-08 20:46:18 +01:00
|
|
|
for (1 .. $NUMACCEPT) {
|
|
|
|
return unless _accept_handler();
|
|
|
|
}
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# got here because we have accept's left.
|
|
|
|
# So double the number we accept next time.
|
|
|
|
$NUMACCEPT *= 2;
|
|
|
|
$NUMACCEPT = ACCEPT_MAX if $NUMACCEPT > ACCEPT_MAX;
|
2008-03-13 22:20:56 +01:00
|
|
|
$ACCEPT_RSET->cancel if defined $ACCEPT_RSET;
|
2006-12-08 20:46:18 +01:00
|
|
|
$ACCEPT_RSET = Danga::Socket->AddTimer(30, \&reset_num_accept);
|
|
|
|
}
|
|
|
|
|
|
|
|
use Errno qw(EAGAIN EWOULDBLOCK);
|
|
|
|
|
|
|
|
sub _accept_handler {
|
|
|
|
my $csock = $SERVER->accept();
|
|
|
|
if (!$csock) {
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
# warn("accept() failed: $!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
binmode($csock, ':raw');
|
|
|
|
|
2013-04-21 06:34:07 +02:00
|
|
|
printf("Listen child making a Qpsmtpd::PollServer for %d.\n",
|
|
|
|
fileno($csock))
|
|
|
|
if $DEBUG;
|
2006-12-08 20:46:18 +01:00
|
|
|
|
|
|
|
IO::Handle::blocking($csock, 0);
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
#setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
|
|
|
|
|
2007-12-03 18:45:31 +01:00
|
|
|
#print "Got connection\n";
|
2006-12-08 20:46:18 +01:00
|
|
|
my $client = Qpsmtpd::PollServer->new($csock);
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2006-12-08 20:46:18 +01:00
|
|
|
if ($PAUSED) {
|
|
|
|
$client->write("451 Sorry, this server is currently paused\r\n");
|
|
|
|
$client->close;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-04-21 06:34:07 +02:00
|
|
|
|
2007-05-26 02:49:00 +02:00
|
|
|
$client->process_line("Connect\n");
|
2006-12-08 20:46:18 +01:00
|
|
|
$client->watch_read(1);
|
2008-01-02 21:37:45 +01:00
|
|
|
$client->pause_read();
|
2006-12-08 20:46:18 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
sub log {
|
2013-04-21 06:34:07 +02:00
|
|
|
my ($level, $message) = @_;
|
|
|
|
|
|
|
|
# $level not used yet. this is reimplemented from elsewhere anyway
|
|
|
|
warn("$$ fd:? $message\n");
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub pause {
|
2013-04-21 06:34:07 +02:00
|
|
|
my ($pause) = @_;
|
|
|
|
$PAUSED = $pause;
|
2006-12-08 20:46:18 +01:00
|
|
|
}
|