2004-06-28 05:05:03 +02:00
|
|
|
#!/usr/bin/perl -Tw
|
2004-03-15 09:59:02 +01:00
|
|
|
# Copyright (c) 2001 Ask Bjoern Hansen. See the LICENSE file for details.
|
|
|
|
# The "command dispatch" system is taken from colobus - http://trainedmonkey.com/colobus/
|
|
|
|
#
|
|
|
|
# For more information see http://develooper.com/code/qpsmtpd/
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
use lib 'lib';
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use IO::Socket;
|
2005-07-05 17:16:36 +02:00
|
|
|
use IO::Select;
|
2005-07-11 21:10:49 +02:00
|
|
|
use Qpsmtpd::PollServer;
|
2004-03-15 09:59:02 +01:00
|
|
|
use Socket;
|
2004-06-28 05:05:03 +02:00
|
|
|
use Getopt::Long;
|
2004-03-19 00:02:43 +01:00
|
|
|
use POSIX qw(:sys_wait_h :errno_h :signal_h);
|
2004-03-15 09:59:02 +01:00
|
|
|
use strict;
|
|
|
|
$| = 1;
|
|
|
|
|
2004-04-15 04:19:01 +02:00
|
|
|
# Configuration
|
2005-07-06 09:53:41 +02:00
|
|
|
my $MAXCONN = 15; # max simultaneous connections
|
|
|
|
my $PORT = 2525; # port number
|
|
|
|
my @LOCALADDR; # ip address(es) to bind to
|
|
|
|
my $USER = 'smtpd'; # user to suid to
|
|
|
|
my $MAXCONNIP = 5; # max simultaneous connections from one IP
|
2005-07-10 13:38:40 +02:00
|
|
|
my $PID_FILE = ''; # file to which server PID will be written
|
2005-07-31 10:42:43 +02:00
|
|
|
my $DETACH; # daemonize on startup
|
2005-07-11 21:10:49 +02:00
|
|
|
our $DEBUG = 0;
|
2004-04-15 04:19:01 +02:00
|
|
|
|
2004-06-28 05:05:03 +02:00
|
|
|
sub usage {
|
|
|
|
print <<"EOT";
|
|
|
|
usage: qpsmtpd-forkserver [ options ]
|
2005-07-06 09:50:00 +02:00
|
|
|
-l, --listen-address addr : listen on specific address(es); can be specified
|
|
|
|
multiple times for multiple bindings. Default is
|
|
|
|
0.0.0.0 (all interfaces).
|
2004-11-29 04:37:38 +01:00
|
|
|
-p, --port P : listen on a specific port; default 2525
|
2004-06-28 05:05:03 +02:00
|
|
|
-c, --limit-connections N : limit concurrent connections to N; default 15
|
2005-06-29 11:37:10 +02:00
|
|
|
-u, --user U : run as a particular user (default 'smtpd')
|
2004-11-29 04:37:38 +01:00
|
|
|
-m, --max-from-ip M : limit connections from a single IP; default 5
|
2005-07-04 16:44:51 +02:00
|
|
|
--pid-file P : print main servers PID to file P
|
2005-12-30 20:42:57 +01:00
|
|
|
-h, --help : this page
|
|
|
|
--use-poll : force use of poll() instead of epoll()/kqueue()
|
2005-12-22 22:30:53 +01:00
|
|
|
-d, --detach : detach from controlling terminal (daemonize)
|
2004-06-28 05:05:03 +02:00
|
|
|
EOT
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOptions('h|help' => \&usage,
|
2005-12-30 20:42:57 +01:00
|
|
|
'l|listen-address=s' => \@LOCALADDR,
|
|
|
|
'c|limit-connections=i' => \$MAXCONN,
|
|
|
|
'm|max-from-ip=i' => \$MAXCONNIP,
|
|
|
|
'p|port=i' => \$PORT,
|
|
|
|
'u|user=s' => \$USER,
|
|
|
|
'pid-file=s' => \$PID_FILE,
|
|
|
|
'debug+' => \$DEBUG,
|
|
|
|
'use-poll' => \&force_poll,
|
|
|
|
'h|help' => \&usage,
|
|
|
|
'd|detach' => \$DETACH,
|
2005-07-04 16:44:51 +02:00
|
|
|
) || &usage;
|
2004-06-28 05:05:03 +02:00
|
|
|
|
2005-12-30 20:42:57 +01:00
|
|
|
sub force_poll {
|
|
|
|
$Danga::Socket::HaveEpoll = 0;
|
|
|
|
$Danga::Socket::HaveKQueue = 0;
|
|
|
|
}
|
|
|
|
|
2004-06-28 05:05:03 +02:00
|
|
|
# detaint the commandline
|
|
|
|
if ($PORT =~ /^(\d+)$/) { $PORT = $1 } else { &usage }
|
2005-07-06 09:50:00 +02:00
|
|
|
@LOCALADDR = ( '0.0.0.0' ) if !@LOCALADDR;
|
|
|
|
for (0..$#LOCALADDR) {
|
|
|
|
if ($LOCALADDR[$_] =~ /^([\d\w\-.]+)$/) {
|
|
|
|
$LOCALADDR[$_] = $1;
|
|
|
|
} else {
|
|
|
|
&usage;
|
|
|
|
}
|
|
|
|
}
|
2004-06-28 05:05:03 +02:00
|
|
|
if ($USER =~ /^([\w\-]+)$/) { $USER = $1 } else { &usage }
|
|
|
|
if ($MAXCONN =~ /^(\d+)$/) { $MAXCONN = $1 } else { &usage }
|
|
|
|
|
2005-08-26 11:51:57 +02:00
|
|
|
delete @ENV{'ENV','CDPATH','IFS','BASH_ENV'};
|
2004-03-15 09:59:02 +01:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/var/qmail/bin';
|
|
|
|
|
2004-04-15 04:19:01 +02:00
|
|
|
my %childstatus = ();
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
sub REAPER {
|
2004-04-15 04:19:01 +02:00
|
|
|
while ( defined(my $chld = waitpid(-1, WNOHANG)) ){
|
|
|
|
last unless $chld > 0;
|
2005-03-24 22:16:35 +01:00
|
|
|
::log(LOGINFO,"cleaning up after $chld");
|
2004-04-15 04:19:01 +02:00
|
|
|
delete $childstatus{$chld};
|
|
|
|
}
|
2004-03-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2004-06-16 22:27:51 +02:00
|
|
|
sub HUNTSMAN {
|
|
|
|
$SIG{CHLD} = 'DEFAULT';
|
|
|
|
kill 'INT' => keys %childstatus;
|
2005-07-10 13:38:40 +02:00
|
|
|
if ($PID_FILE && -e $PID_FILE) {
|
|
|
|
unlink $PID_FILE or ::log(LOGERROR, "unlink: $PID_FILE: $!");
|
|
|
|
}
|
2004-06-16 22:27:51 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$SIG{INT} = \&HUNTSMAN;
|
|
|
|
$SIG{TERM} = \&HUNTSMAN;
|
2004-03-15 09:59:02 +01:00
|
|
|
|
2005-07-06 09:50:00 +02:00
|
|
|
my $select = new IO::Select;
|
|
|
|
|
|
|
|
# establish SERVER socket(s), bind and listen.
|
|
|
|
for my $listen_addr (@LOCALADDR) {
|
|
|
|
my $server = IO::Socket::INET->new(LocalPort => $PORT,
|
|
|
|
LocalAddr => $listen_addr,
|
|
|
|
Proto => 'tcp',
|
|
|
|
Reuse => 1,
|
|
|
|
Blocking => 0,
|
|
|
|
Listen => SOMAXCONN )
|
|
|
|
or die "Creating TCP socket $listen_addr:$PORT: $!\n";
|
|
|
|
IO::Handle::blocking($server, 0);
|
|
|
|
$select->add($server);
|
|
|
|
}
|
2004-03-15 09:59:02 +01:00
|
|
|
|
2005-07-05 17:16:36 +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";
|
2005-07-31 11:02:42 +02:00
|
|
|
my $running_pid = <PID> || ''; chomp $running_pid;
|
2005-07-05 17:16:36 +02:00
|
|
|
if ($running_pid =~ /(\d+)/) {
|
|
|
|
$running_pid = $1;
|
|
|
|
if (kill 0, $running_pid) {
|
|
|
|
die "Found an already running qpsmtpd with pid $running_pid.\n";
|
|
|
|
}
|
2005-07-04 16:44:51 +02:00
|
|
|
}
|
2005-07-05 17:16:36 +02:00
|
|
|
seek PID, 0, 0
|
|
|
|
or die "Could not seek back to beginning of $PID_FILE: $!\n";
|
2005-07-10 13:46:15 +02:00
|
|
|
truncate PID, 0
|
|
|
|
or die "Could not truncate $PID_FILE at 0: $!";
|
2005-07-05 17:16:36 +02:00
|
|
|
} else {
|
|
|
|
open PID, ">$PID_FILE"
|
|
|
|
or die "open pid_file: $!\n";
|
2005-07-04 16:44:51 +02:00
|
|
|
}
|
|
|
|
}
|
2005-07-05 17:16:36 +02:00
|
|
|
|
|
|
|
# Load plugins here
|
2005-12-30 20:42:57 +01:00
|
|
|
my $qpsmtpd = Qpsmtpd::PollServer->new();
|
2005-07-04 16:44:51 +02:00
|
|
|
|
|
|
|
# Drop privileges
|
2004-04-15 04:19:01 +02:00
|
|
|
my (undef, undef, $quid, $qgid) = getpwnam $USER or
|
|
|
|
die "unable to determine uid/gid for $USER\n";
|
2005-07-04 16:44:51 +02:00
|
|
|
my $groups = "$qgid $qgid";
|
|
|
|
while (my ($name,$passwd,$gid,$members) = getgrent()) {
|
|
|
|
my @m = split(/ /, $members);
|
|
|
|
if (grep {$_ eq $USER} @m) {
|
|
|
|
$groups .= " $gid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$) = $groups;
|
2004-03-15 09:59:02 +01:00
|
|
|
POSIX::setgid($qgid) or
|
|
|
|
die "unable to change gid: $!\n";
|
|
|
|
POSIX::setuid($quid) or
|
|
|
|
die "unable to change uid: $!\n";
|
|
|
|
$> = $quid;
|
|
|
|
|
2005-12-11 10:14:20 +01:00
|
|
|
$qpsmtpd->load_plugins;
|
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
::log(LOGINFO,"Listening on port $PORT");
|
|
|
|
::log(LOGINFO, 'Running as user '.
|
|
|
|
(getpwuid($>) || $>) .
|
|
|
|
', group '.
|
|
|
|
(getgrgid($)) || $)));
|
2004-03-15 09:59:02 +01:00
|
|
|
|
2005-07-31 10:42:43 +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;
|
|
|
|
}
|
|
|
|
|
2005-12-22 22:30:53 +01:00
|
|
|
# Populate class cached variables
|
|
|
|
$qpsmtpd->spool_dir;
|
|
|
|
$qpsmtpd->size_threshold;
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
while (1) {
|
2005-07-05 17:16:36 +02:00
|
|
|
REAPER();
|
2004-04-15 04:19:01 +02:00
|
|
|
my $running = scalar keys %childstatus;
|
2005-07-05 17:25:54 +02:00
|
|
|
if ($running >= $MAXCONN) {
|
2004-04-15 04:19:01 +02:00
|
|
|
::log(LOGINFO,"Too many connections: $running >= $MAXCONN. Waiting one second.");
|
2005-07-05 17:20:40 +02:00
|
|
|
sleep(1);
|
|
|
|
next;
|
2005-07-05 17:16:36 +02:00
|
|
|
}
|
2005-07-06 09:50:00 +02:00
|
|
|
my @ready = $select->can_read(1);
|
|
|
|
next if !@ready;
|
2005-07-06 14:13:53 +02:00
|
|
|
while (my $server = shift @ready) {
|
|
|
|
my ($client, $hisaddr) = $server->accept;
|
|
|
|
|
|
|
|
if (!$hisaddr) {
|
|
|
|
# possible something condition...
|
|
|
|
next;
|
2004-07-05 21:20:15 +02:00
|
|
|
}
|
2005-07-13 19:10:38 +02:00
|
|
|
# Make this client blocking while we figure out if we actually want to
|
|
|
|
# do something with it.
|
2005-07-06 14:13:53 +02:00
|
|
|
IO::Handle::blocking($client, 1);
|
|
|
|
my ($port, $iaddr) = sockaddr_in($hisaddr);
|
|
|
|
if ($MAXCONNIP) {
|
|
|
|
my $num_conn = 1; # seed with current value
|
|
|
|
|
|
|
|
foreach my $rip (values %childstatus) {
|
|
|
|
++$num_conn if (defined $rip && $rip eq $iaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($num_conn > $MAXCONNIP) {
|
|
|
|
my $rem_ip = inet_ntoa($iaddr);
|
|
|
|
::log(LOGINFO,"Too many connections from $rem_ip: "
|
|
|
|
."$num_conn > $MAXCONNIP. Denying connection.");
|
|
|
|
$client->autoflush(1);
|
|
|
|
print $client "451 Sorry, too many connections from $rem_ip, try again later\r\n";
|
|
|
|
close $client;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my $pid = safe_fork();
|
|
|
|
if ($pid) {
|
|
|
|
# parent
|
|
|
|
$childstatus{$pid} = $iaddr; # add to table
|
|
|
|
# $childstatus{$pid} = 1; # add to table
|
|
|
|
$running++;
|
|
|
|
close($client);
|
2005-07-06 09:53:41 +02:00
|
|
|
next;
|
2004-03-19 00:02:43 +01:00
|
|
|
}
|
2005-07-06 14:13:53 +02:00
|
|
|
# otherwise child
|
|
|
|
|
|
|
|
# all children should have different seeds, to prevent conflicts
|
|
|
|
srand( time ^ ($$ + ($$ << 15)) );
|
2005-07-06 09:53:41 +02:00
|
|
|
|
2005-07-06 14:13:53 +02:00
|
|
|
close($server);
|
2005-07-06 09:53:41 +02:00
|
|
|
|
2005-07-06 14:13:53 +02:00
|
|
|
$SIG{$_} = 'DEFAULT' for keys %SIG;
|
|
|
|
$SIG{ALRM} = sub {
|
|
|
|
print $client "421 Connection Timed Out\n";
|
|
|
|
::log(LOGINFO, "Connection Timed Out");
|
|
|
|
exit; };
|
2005-07-06 09:53:41 +02:00
|
|
|
|
2005-12-22 22:30:53 +01:00
|
|
|
my $localsockaddr = getsockname($client);
|
|
|
|
my ($lport, $laddr) = sockaddr_in($localsockaddr);
|
|
|
|
$ENV{TCPLOCALIP} = inet_ntoa($laddr);
|
|
|
|
# my ($port, $iaddr) = sockaddr_in($hisaddr);
|
|
|
|
$ENV{TCPREMOTEIP} = inet_ntoa($iaddr);
|
|
|
|
$ENV{TCPREMOTEHOST} = gethostbyaddr($iaddr, AF_INET) || "Unknown";
|
|
|
|
|
|
|
|
# don't do this!
|
|
|
|
#$0 = "qpsmtpd-forkserver: $ENV{TCPREMOTEIP} / $ENV{TCPREMOTEHOST}";
|
|
|
|
|
|
|
|
::log(LOGINFO, "Accepted connection $running/$MAXCONN from $ENV{TCPREMOTEIP} / $ENV{TCPREMOTEHOST}");
|
2005-07-06 14:13:53 +02:00
|
|
|
|
2005-07-11 21:10:49 +02:00
|
|
|
$::LineMode = 1;
|
2005-07-06 14:13:53 +02:00
|
|
|
|
2005-07-13 19:10:38 +02:00
|
|
|
# Make this client non-blocking so it works with the Danga framework
|
|
|
|
IO::Handle::blocking($client, 0);
|
2005-07-11 21:10:49 +02:00
|
|
|
my $qp = Qpsmtpd::PollServer->new($client);
|
|
|
|
$qp->load_plugins;
|
|
|
|
$qp->init_logger;
|
|
|
|
$qp->push_back_read("Connect\n");
|
|
|
|
Qpsmtpd::PollServer->AddTimer(0.1, sub { });
|
|
|
|
while (1) {
|
2005-12-22 22:30:53 +01:00
|
|
|
$qp->enable_read;
|
|
|
|
my $line = $qp->get_line;
|
|
|
|
last if !defined($line);
|
|
|
|
my $output = $qp->process_line($line);
|
|
|
|
$qp->write($output) if $output;
|
2005-07-11 21:10:49 +02:00
|
|
|
}
|
2005-07-06 14:13:53 +02:00
|
|
|
|
|
|
|
exit; # child leaves
|
|
|
|
}
|
2004-03-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2004-04-15 04:19:01 +02:00
|
|
|
sub log {
|
|
|
|
my ($level,$message) = @_;
|
2005-05-25 22:07:58 +02:00
|
|
|
$qpsmtpd->log($level,$message);
|
2004-04-15 04:19:01 +02:00
|
|
|
}
|
|
|
|
|
2005-07-05 17:16:36 +02:00
|
|
|
### routine to protect process during fork
|
|
|
|
sub safe_fork {
|
|
|
|
|
|
|
|
### block signal for fork
|
|
|
|
my $sigset = POSIX::SigSet->new(SIGINT);
|
|
|
|
POSIX::sigprocmask(SIG_BLOCK, $sigset)
|
|
|
|
or die "Can't block SIGINT for fork: [$!]\n";
|
|
|
|
|
|
|
|
### fork off a child
|
|
|
|
my $pid = fork;
|
|
|
|
unless( defined $pid ){
|
|
|
|
die "Couldn't fork: [$!]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
### make SIGINT kill us as it did before
|
|
|
|
$SIG{INT} = 'DEFAULT';
|
|
|
|
|
|
|
|
### put back to normal
|
|
|
|
POSIX::sigprocmask(SIG_UNBLOCK, $sigset)
|
|
|
|
or die "Can't unblock SIGINT for fork: [$!]\n";
|
|
|
|
|
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
__END__
|
|
|
|
|
|
|
|
1;
|