refactor start_connection

This commit is contained in:
Matt Simerson 2015-01-28 22:54:04 -08:00
parent d81b2d4331
commit 98d148a0bf

View File

@ -6,6 +6,7 @@ use Socket;
use lib 'lib'; use lib 'lib';
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
use parent 'Qpsmtpd::Base';
use parent 'Qpsmtpd::SMTP'; use parent 'Qpsmtpd::SMTP';
my $has_ipv6 = 0; my $has_ipv6 = 0;
@ -24,62 +25,71 @@ sub has_ipv6 {
my $first_0; my $first_0;
sub start_connection { sub conn_info_tcpserver {
my $self = shift;
my (
$remote_host, $remote_info, $remote_ip, $remote_port,
$local_ip, $local_port, $local_host
);
if ($ENV{TCPREMOTEIP}) {
# started from tcpserver (or some other superserver which # started from tcpserver (or some other superserver which
# exports the TCPREMOTE* variables. # exports the TCPREMOTE* variables.
$remote_ip = $ENV{TCPREMOTEIP};
$remote_host = $ENV{TCPREMOTEHOST} || "[$remote_ip]"; my $r_host = $ENV{TCPREMOTEHOST} || '[' . $ENV{TCPREMOTEIP} . ']';
$remote_info = return (
$ENV{TCPREMOTEINFO} local_ip => $ENV{TCPLOCALIP},
? "$ENV{TCPREMOTEINFO}\@$remote_host" local_host => $ENV{TCPLOCALHOST},
: $remote_host; local_port => $ENV{TCPLOCALPORT},
$remote_port = $ENV{TCPREMOTEPORT}; remote_ip => $ENV{TCPREMOTEIP},
$local_ip = $ENV{TCPLOCALIP}; remote_host => $r_host,
$local_port = $ENV{TCPLOCALPORT}; remote_info => $ENV{TCPREMOTEINFO} ? "$ENV{TCPREMOTEINFO}\@$r_host" : $r_host,
$local_host = $ENV{TCPLOCALHOST}; remote_port => $ENV{TCPREMOTEPORT},
} )
else { }
sub conn_info_inetd {
my $self = shift;
# Started from inetd or similar. # Started from inetd or similar.
# get info on the remote host from the socket. # get info on the remote host from the socket.
# ignore ident/tap/... # ignore ident/tap/...
my $hersockaddr = getpeername(STDIN)
or die my $hersockaddr = getpeername(STDIN) or die "getpeername failed:" .
"getpeername failed: $0 must be called from tcpserver, (x)inetd or a similar program which passes a socket to stdin"; " $0 must be called from tcpserver, (x)inetd or" .
my ($port, $iaddr) = sockaddr_in($hersockaddr); " a similar program which passes a socket to stdin";
$remote_ip = inet_ntoa($iaddr);
$remote_host = gethostbyaddr($iaddr, AF_INET) || "[$remote_ip]"; my ($r_port, $iaddr) = sockaddr_in($hersockaddr);
$remote_info = $remote_host; my $r_ip = inet_ntoa($iaddr);
my ($r_host) = $self->resolve_ptr($r_ip) || "[$r_ip]";
return (
local_ip => '',
local_host => '',
local_port => '',
remote_ip => $r_ip,
remote_host => $r_host,
remote_info => $r_host,
remote_port => $r_port,
)
}
sub start_connection {
my $self = shift;
my %info;
if ($ENV{TCPREMOTEIP}) {
%info = $self->conn_info_tcpserver();
} }
$self->log(LOGNOTICE, "Connection from $remote_info [$remote_ip]"); else {
%info = $self->conn_info_inetd();
}
$self->log(LOGNOTICE, "Connection from $info{remote_info} [$info{remote_ip}]");
# if the local dns resolver doesn't filter it out we might get # if the local dns resolver doesn't filter it out we might get
# ansi escape characters that could make a ps axw do "funny" # ansi escape characters that could make a ps axw do "funny"
# things. So to be safe, cut them out. # things. So to be safe, cut them out.
$remote_host =~ tr/a-zA-Z\.\-0-9\[\]//cd; $info{remote_host} =~ tr/a-zA-Z\.\-0-9\[\]//cd;
$first_0 = $0 unless $first_0; $first_0 = $0 unless $first_0;
my $now = POSIX::strftime("%H:%M:%S %Y-%m-%d", localtime); my $now = POSIX::strftime("%H:%M:%S %Y-%m-%d", localtime);
$0 = "$first_0 [$remote_ip : $remote_host : $now]"; $0 = "$first_0 [$info{remote_ip} : $info{remote_host} : $now]";
$self->SUPER::connection->start( $self->SUPER::connection->start(%info, @_);
remote_info => $remote_info,
remote_ip => $remote_ip,
remote_host => $remote_host,
remote_port => $remote_port,
local_ip => $local_ip,
local_port => $local_port,
local_host => $local_host,
@_
);
} }
sub run { sub run {