2002-07-03 15:10:44 +02:00
|
|
|
package Qpsmtpd::TcpServer;
|
|
|
|
use strict;
|
|
|
|
|
2004-06-05 12:06:44 +02:00
|
|
|
use POSIX ();
|
2014-11-11 01:26:46 +01:00
|
|
|
use Socket;
|
|
|
|
|
|
|
|
use lib 'lib';
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use parent 'Qpsmtpd::SMTP';
|
2004-06-05 12:06:44 +02:00
|
|
|
|
2012-06-24 05:04:50 +02:00
|
|
|
my $has_ipv6 = 0;
|
2007-05-18 00:16:27 +02:00
|
|
|
if (
|
2014-11-11 01:26:46 +01:00
|
|
|
eval { require Socket6; } &&
|
|
|
|
eval { require IO::Socket::INET6; IO::Socket::INET6->VERSION('2.51'); }
|
2013-04-21 06:08:43 +02:00
|
|
|
)
|
|
|
|
{
|
2014-11-11 01:26:46 +01:00
|
|
|
Socket6->import('inet_ntop');
|
2013-04-21 06:08:43 +02:00
|
|
|
$has_ipv6 = 1;
|
2007-05-18 00:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub has_ipv6 {
|
|
|
|
return $has_ipv6;
|
|
|
|
}
|
|
|
|
|
2012-05-04 20:25:27 +02:00
|
|
|
my $first_0;
|
2004-06-05 12:06:44 +02:00
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
sub start_connection {
|
|
|
|
my $self = shift;
|
|
|
|
|
2010-05-11 06:55:53 +02:00
|
|
|
my (
|
|
|
|
$remote_host, $remote_info, $remote_ip, $remote_port,
|
|
|
|
$local_ip, $local_port, $local_host
|
|
|
|
);
|
2002-08-06 14:34:03 +02:00
|
|
|
|
2006-12-16 10:30:32 +01:00
|
|
|
if ($ENV{TCPREMOTEIP}) {
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# started from tcpserver (or some other superserver which
|
|
|
|
# exports the TCPREMOTE* variables.
|
|
|
|
$remote_ip = $ENV{TCPREMOTEIP};
|
|
|
|
$remote_host = $ENV{TCPREMOTEHOST} || "[$remote_ip]";
|
|
|
|
$remote_info =
|
|
|
|
$ENV{TCPREMOTEINFO}
|
|
|
|
? "$ENV{TCPREMOTEINFO}\@$remote_host"
|
|
|
|
: $remote_host;
|
2010-05-11 06:55:53 +02:00
|
|
|
$remote_port = $ENV{TCPREMOTEPORT};
|
|
|
|
$local_ip = $ENV{TCPLOCALIP};
|
|
|
|
$local_port = $ENV{TCPLOCALPORT};
|
|
|
|
$local_host = $ENV{TCPLOCALHOST};
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
# Started from inetd or similar.
|
|
|
|
# get info on the remote host from the socket.
|
|
|
|
# ignore ident/tap/...
|
|
|
|
my $hersockaddr = getpeername(STDIN)
|
|
|
|
or die
|
|
|
|
"getpeername failed: $0 must be called from tcpserver, (x)inetd or a similar program which passes a socket to stdin";
|
|
|
|
my ($port, $iaddr) = sockaddr_in($hersockaddr);
|
|
|
|
$remote_ip = inet_ntoa($iaddr);
|
|
|
|
$remote_host = gethostbyaddr($iaddr, AF_INET) || "[$remote_ip]";
|
|
|
|
$remote_info = $remote_host;
|
2006-12-16 10:30:32 +01:00
|
|
|
}
|
2004-08-01 08:56:33 +02:00
|
|
|
$self->log(LOGNOTICE, "Connection from $remote_info [$remote_ip]");
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2003-04-21 10:28:12 +02:00
|
|
|
# if the local dns resolver doesn't filter it out we might get
|
|
|
|
# ansi escape characters that could make a ps axw do "funny"
|
2012-05-04 20:25:27 +02:00
|
|
|
# things. So to be safe, cut them out.
|
2008-11-16 09:28:29 +01:00
|
|
|
$remote_host =~ tr/a-zA-Z\.\-0-9\[\]//cd;
|
2003-04-21 10:28:12 +02:00
|
|
|
|
2004-06-05 12:06:44 +02:00
|
|
|
$first_0 = $0 unless $first_0;
|
|
|
|
my $now = POSIX::strftime("%H:%M:%S %Y-%m-%d", localtime);
|
|
|
|
$0 = "$first_0 [$remote_ip : $remote_host : $now]";
|
2003-04-16 18:35:14 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->SUPER::connection->start(
|
|
|
|
remote_info => $remote_info,
|
2010-05-11 06:55:53 +02:00
|
|
|
remote_ip => $remote_ip,
|
|
|
|
remote_host => $remote_host,
|
|
|
|
remote_port => $remote_port,
|
|
|
|
local_ip => $local_ip,
|
|
|
|
local_port => $local_port,
|
|
|
|
local_host => $local_host,
|
2013-04-21 06:08:43 +02:00
|
|
|
@_
|
|
|
|
);
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub run {
|
2008-09-26 19:40:04 +02:00
|
|
|
my ($self, $client) = @_;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# Set local client_socket to passed client object for testing socket state on writes
|
2008-09-26 19:40:04 +02:00
|
|
|
$self->{__client_socket} = $client;
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2014-11-11 01:26:46 +01:00
|
|
|
$self->load_plugins if !$self->{hooks};
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2003-04-15 19:01:43 +02:00
|
|
|
my $rc = $self->start_conversation;
|
|
|
|
return if $rc != DONE;
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# this should really be the loop and read_input should just get one line; I think
|
2002-07-03 15:10:44 +02:00
|
|
|
$self->read_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub read_input {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $timeout = $self->config('timeoutsmtpd') # qmail smtpd control file
|
|
|
|
|| $self->config('timeout') # qpsmtpd control file
|
|
|
|
|| 1200; # default value
|
|
|
|
|
2002-07-06 09:16:23 +02:00
|
|
|
alarm $timeout;
|
2013-04-21 06:08:43 +02:00
|
|
|
while (<STDIN>) {
|
|
|
|
alarm 0;
|
|
|
|
$_ =~ s/\r?\n$//s; # advanced chomp
|
|
|
|
$self->log(LOGINFO, "dispatching $_");
|
|
|
|
$self->connection->notes('original_string', $_);
|
|
|
|
defined $self->dispatch(split / +/, $_, 2)
|
|
|
|
or $self->respond(502, "command unrecognized: '$_'");
|
|
|
|
alarm $timeout;
|
|
|
|
}
|
|
|
|
alarm(0);
|
|
|
|
return if $self->connection->notes('disconnected');
|
|
|
|
$self->reset_transaction;
|
|
|
|
$self->run_hooks('disconnect');
|
|
|
|
$self->connection->notes(disconnected => 1);
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub respond {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $code, @messages) = @_;
|
|
|
|
my $buf = '';
|
|
|
|
|
|
|
|
if (!$self->check_socket()) {
|
|
|
|
$self->log(LOGERROR,
|
|
|
|
"Lost connection to client, cannot send response.");
|
2014-09-18 03:28:51 +02:00
|
|
|
return 0;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (my $msg = shift @messages) {
|
|
|
|
my $line = $code . (@messages ? "-" : " ") . $msg;
|
|
|
|
$self->log(LOGINFO, $line);
|
|
|
|
$buf .= "$line\r\n";
|
|
|
|
}
|
|
|
|
print $buf
|
|
|
|
or ($self->log(LOGERROR, "Could not print [$buf]: $!"), return 0);
|
|
|
|
return 1;
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
2002-07-15 13:49:49 +02:00
|
|
|
sub disconnect {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->log(LOGINFO, "click, disconnecting");
|
|
|
|
$self->SUPER::disconnect(@_);
|
|
|
|
$self->run_hooks("post-connection");
|
|
|
|
$self->connection->reset;
|
|
|
|
exit;
|
2002-07-15 13:49:49 +02:00
|
|
|
}
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2007-05-18 00:16:27 +02:00
|
|
|
# local/remote port and ip address
|
|
|
|
sub lrpip {
|
2014-11-11 01:26:46 +01:00
|
|
|
my ($self, $server, $client, $hisaddr) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
my $localsockaddr = getsockname($client);
|
2014-11-11 01:26:46 +01:00
|
|
|
my ($port, $iaddr, $lport, $laddr, $nto_iaddr, $nto_laddr);
|
|
|
|
|
|
|
|
if ($server->sockdomain == AF_INET6) { # IPv6
|
|
|
|
($port, $iaddr) = sockaddr_in6($hisaddr);
|
|
|
|
($lport, $laddr) = sockaddr_in6($localsockaddr);
|
|
|
|
$nto_iaddr = inet_ntop(AF_INET6(), $iaddr);
|
|
|
|
$nto_laddr = inet_ntop(AF_INET6(), $laddr);
|
|
|
|
}
|
|
|
|
else { # IPv4
|
|
|
|
($port, $iaddr) = sockaddr_in($hisaddr);
|
|
|
|
($lport, $laddr) = sockaddr_in($localsockaddr);
|
|
|
|
$nto_iaddr = inet_ntoa($iaddr);
|
|
|
|
$nto_laddr = inet_ntoa($laddr);
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$nto_iaddr =~ s/::ffff://;
|
|
|
|
$nto_laddr =~ s/::ffff://;
|
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return $port, $iaddr, $lport, $laddr, $nto_iaddr, $nto_laddr;
|
2007-05-18 00:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub tcpenv {
|
2014-11-11 01:26:46 +01:00
|
|
|
my ($self, $TCPLOCALIP, $TCPREMOTEIP, $no_rdns) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
if ($no_rdns) {
|
2014-09-18 03:28:51 +02:00
|
|
|
return $TCPLOCALIP, $TCPREMOTEIP,
|
2014-11-11 01:26:46 +01:00
|
|
|
$TCPREMOTEIP ? "[$ENV{TCPREMOTEIP}]" : "[noip!]";
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2014-11-11 01:26:46 +01:00
|
|
|
my ($TCPREMOTEHOST) = $self->resolve_ptr($TCPREMOTEIP) || 'Unknown';
|
|
|
|
return $TCPLOCALIP, $TCPREMOTEIP, $TCPREMOTEHOST;
|
2007-05-18 00:16:27 +02:00
|
|
|
}
|
|
|
|
|
2008-09-26 19:40:04 +02:00
|
|
|
sub check_socket() {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2008-09-26 19:40:04 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return 1 if ($self->{__client_socket}->connected);
|
2012-05-04 20:25:27 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return 0;
|
2008-09-26 19:40:04 +02:00
|
|
|
}
|
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
1;
|