2004-03-15 09:59:02 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
# 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::TcpServer;
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use IO::Socket;
|
|
|
|
use Socket;
|
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
|
|
|
|
my $MAXCONN = 15; # max simultaneous connections
|
|
|
|
my $PORT = 25; # port number
|
|
|
|
my $LOCALADDR = '0.0.0.0'; # ip address to bind to
|
|
|
|
my $USER = 'smtpd'; # user to suid to
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
delete $ENV{ENV};
|
|
|
|
$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-06-16 22:27:51 +02:00
|
|
|
$SIG{CHLD} = \&REAPER;
|
2004-04-15 04:19:01 +02:00
|
|
|
while ( defined(my $chld = waitpid(-1, WNOHANG)) ){
|
|
|
|
last unless $chld > 0;
|
|
|
|
warn("$$ cleaning up after $chld\n");
|
|
|
|
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;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
$SIG{CHLD} = \&REAPER;
|
2004-06-16 22:27:51 +02:00
|
|
|
$SIG{INT} = \&HUNTSMAN;
|
|
|
|
$SIG{TERM} = \&HUNTSMAN;
|
2004-03-15 09:59:02 +01:00
|
|
|
|
|
|
|
# establish SERVER socket, bind and listen.
|
2004-04-15 04:19:01 +02:00
|
|
|
my $server = IO::Socket::INET->new(LocalPort => $PORT,
|
|
|
|
LocalAddr => $LOCALADDR,
|
2004-03-15 09:59:02 +01:00
|
|
|
Proto => 'tcp',
|
|
|
|
Reuse => 1,
|
|
|
|
Listen => SOMAXCONN )
|
|
|
|
or die "making socket: $@\n";
|
|
|
|
|
|
|
|
# Drop priviledges
|
2004-04-15 04:19:01 +02:00
|
|
|
my $user = 'mailfw';
|
|
|
|
my (undef, undef, $quid, $qgid) = getpwnam $USER or
|
|
|
|
die "unable to determine uid/gid for $USER\n";
|
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;
|
|
|
|
|
|
|
|
# Load plugins here
|
|
|
|
my $plugin_loader = Qpsmtpd::TcpServer->new();
|
|
|
|
$plugin_loader->load_plugins;
|
|
|
|
|
2004-04-15 04:19:01 +02:00
|
|
|
::log(LOGINFO,"Listening on port $PORT\n");
|
2004-03-15 09:59:02 +01:00
|
|
|
|
|
|
|
while (1) {
|
2004-04-15 04:19:01 +02:00
|
|
|
my $running = scalar keys %childstatus;
|
|
|
|
while ($running >= $MAXCONN) {
|
|
|
|
::log(LOGINFO,"Too many connections: $running >= $MAXCONN. Waiting one second.");
|
|
|
|
sleep(1) ;
|
|
|
|
$running = scalar keys %childstatus;
|
|
|
|
}
|
2004-03-19 00:02:43 +01:00
|
|
|
my $hisaddr = accept(my $client, $server);
|
2004-03-15 09:59:02 +01:00
|
|
|
if (!$hisaddr) {
|
|
|
|
# possible something condition...
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
my $pid = fork;
|
2004-03-19 00:02:43 +01:00
|
|
|
if ($pid) {
|
2004-04-15 04:19:01 +02:00
|
|
|
# parent
|
|
|
|
$childstatus{$pid} = 1; # add to table
|
|
|
|
$running++;
|
2004-03-19 00:02:43 +01:00
|
|
|
close($client);
|
|
|
|
next;
|
|
|
|
}
|
2004-03-15 09:59:02 +01:00
|
|
|
die "fork: $!" unless defined $pid; # failure
|
|
|
|
# otherwise child
|
|
|
|
|
2004-03-19 00:02:43 +01:00
|
|
|
close($server);
|
|
|
|
|
2004-06-16 22:27:51 +02:00
|
|
|
$SIG{$_} = 'DEFAULT' for keys %SIG;
|
2004-03-15 09:59:02 +01:00
|
|
|
|
2004-03-19 00:02:43 +01:00
|
|
|
my $localsockaddr = getsockname($client);
|
|
|
|
my ($lport, $laddr) = sockaddr_in($localsockaddr);
|
|
|
|
$ENV{TCPLOCALIP} = inet_ntoa($laddr);
|
2004-03-15 09:59:02 +01:00
|
|
|
my ($port, $iaddr) = sockaddr_in($hisaddr);
|
|
|
|
$ENV{TCPREMOTEIP} = inet_ntoa($iaddr);
|
|
|
|
$ENV{TCPREMOTEHOST} = gethostbyaddr($iaddr, AF_INET) || "Unknown";
|
2004-04-15 04:19:01 +02:00
|
|
|
|
|
|
|
# don't do this!
|
|
|
|
#$0 = "qpsmtpd-forkserver: $ENV{TCPREMOTEIP} / $ENV{TCPREMOTEHOST}";
|
|
|
|
|
|
|
|
::log(LOGINFO, "Accepted connection $running/$MAXCONN from $ENV{TCPREMOTEIP} / $ENV{TCPREMOTEHOST}");
|
2004-03-15 09:59:02 +01:00
|
|
|
|
|
|
|
# dup to STDIN/STDOUT
|
|
|
|
POSIX::dup2(fileno($client), 0);
|
|
|
|
POSIX::dup2(fileno($client), 1);
|
|
|
|
|
|
|
|
my $qpsmtpd = Qpsmtpd::TcpServer->new();
|
|
|
|
$qpsmtpd->start_connection();
|
|
|
|
$qpsmtpd->run();
|
|
|
|
|
|
|
|
exit; # child leaves
|
|
|
|
}
|
|
|
|
|
2004-04-15 04:19:01 +02:00
|
|
|
sub log {
|
|
|
|
my ($level,$message) = @_;
|
|
|
|
# $level not used yet. this is reimplemented from elsewhere anyway
|
|
|
|
warn("$$ $message\n");
|
|
|
|
}
|
|
|
|
|
2004-03-15 09:59:02 +01:00
|
|
|
__END__
|
|
|
|
|
|
|
|
1;
|