| 1 |
package Qpsmtpd::TcpServer; |
| 2 |
use Qpsmtpd::SMTP; |
| 3 |
use Qpsmtpd::Constants; |
| 4 |
use Socket; |
| 5 |
|
| 6 |
@ISA = qw(Qpsmtpd::SMTP); |
| 7 |
use strict; |
| 8 |
|
| 9 |
use POSIX (); |
| 10 |
|
| 11 |
my $has_ipv6 = 0; |
| 12 |
if ( |
| 13 |
eval { require Socket6; } |
| 14 |
&& |
| 15 |
|
| 16 |
# INET6 prior to 2.01 will not work; sorry. |
| 17 |
eval { require IO::Socket::INET6; IO::Socket::INET6->VERSION("2.00"); } |
| 18 |
) |
| 19 |
{ |
| 20 |
Socket6->import(qw(inet_ntop)); |
| 21 |
$has_ipv6 = 1; |
| 22 |
} |
| 23 |
|
| 24 |
sub has_ipv6 { |
| 25 |
return $has_ipv6; |
| 26 |
} |
| 27 |
|
| 28 |
my $first_0; |
| 29 |
|
| 30 |
sub start_connection { |
| 31 |
my $self = shift; |
| 32 |
|
| 33 |
my ( |
| 34 |
$remote_host, $remote_info, $remote_ip, $remote_port, |
| 35 |
$local_ip, $local_port, $local_host |
| 36 |
); |
| 37 |
|
| 38 |
if ($ENV{TCPREMOTEIP}) { |
| 39 |
|
| 40 |
# started from tcpserver (or some other superserver which |
| 41 |
# exports the TCPREMOTE* variables. |
| 42 |
$remote_ip = $ENV{TCPREMOTEIP}; |
| 43 |
$remote_host = $ENV{TCPREMOTEHOST} || "[$remote_ip]"; |
| 44 |
$remote_info = |
| 45 |
$ENV{TCPREMOTEINFO} |
| 46 |
? "$ENV{TCPREMOTEINFO}\@$remote_host" |
| 47 |
: $remote_host; |
| 48 |
$remote_port = $ENV{TCPREMOTEPORT}; |
| 49 |
$local_ip = $ENV{TCPLOCALIP}; |
| 50 |
$local_port = $ENV{TCPLOCALPORT}; |
| 51 |
$local_host = $ENV{TCPLOCALHOST}; |
| 52 |
} |
| 53 |
else { |
| 54 |
# Started from inetd or similar. |
| 55 |
# get info on the remote host from the socket. |
| 56 |
# ignore ident/tap/... |
| 57 |
my $hersockaddr = getpeername(STDIN) |
| 58 |
or die |
| 59 |
"getpeername failed: $0 must be called from tcpserver, (x)inetd or a similar program which passes a socket to stdin"; |
| 60 |
my ($port, $iaddr) = sockaddr_in($hersockaddr); |
| 61 |
$remote_ip = inet_ntoa($iaddr); |
| 62 |
$remote_host = gethostbyaddr($iaddr, AF_INET) || "[$remote_ip]"; |
| 63 |
$remote_info = $remote_host; |
| 64 |
} |
| 65 |
$self->log(LOGNOTICE, "Connection from $remote_info [$remote_ip]"); |
| 66 |
|
| 67 |
# if the local dns resolver doesn't filter it out we might get |
| 68 |
# ansi escape characters that could make a ps axw do "funny" |
| 69 |
# things. So to be safe, cut them out. |
| 70 |
$remote_host =~ tr/a-zA-Z\.\-0-9\[\]//cd; |
| 71 |
|
| 72 |
$first_0 = $0 unless $first_0; |
| 73 |
my $now = POSIX::strftime("%H:%M:%S %Y-%m-%d", localtime); |
| 74 |
$0 = "$first_0 [$remote_ip : $remote_host : $now]"; |
| 75 |
|
| 76 |
$self->SUPER::connection->start( |
| 77 |
remote_info => $remote_info, |
| 78 |
remote_ip => $remote_ip, |
| 79 |
remote_host => $remote_host, |
| 80 |
remote_port => $remote_port, |
| 81 |
local_ip => $local_ip, |
| 82 |
local_port => $local_port, |
| 83 |
local_host => $local_host, |
| 84 |
@_ |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
sub run { |
| 89 |
my ($self, $client) = @_; |
| 90 |
|
| 91 |
# Set local client_socket to passed client object for testing socket state on writes |
| 92 |
$self->{__client_socket} = $client; |
| 93 |
|
| 94 |
$self->load_plugins unless $self->{hooks}; |
| 95 |
|
| 96 |
my $rc = $self->start_conversation; |
| 97 |
return if $rc != DONE; |
| 98 |
|
| 99 |
# this should really be the loop and read_input should just get one line; I think |
| 100 |
$self->read_input; |
| 101 |
} |
| 102 |
|
| 103 |
sub read_input { |
| 104 |
my $self = shift; |
| 105 |
|
| 106 |
my $timeout = $self->config('timeoutsmtpd') # qmail smtpd control file |
| 107 |
|| $self->config('timeout') # qpsmtpd control file |
| 108 |
|| 1200; # default value |
| 109 |
|
| 110 |
alarm $timeout; |
| 111 |
while (<STDIN>) { |
| 112 |
alarm 0; |
| 113 |
$_ =~ s/\r?\n$//s; # advanced chomp |
| 114 |
$self->log(LOGINFO, "dispatching $_"); |
| 115 |
$self->connection->notes('original_string', $_); |
| 116 |
defined $self->dispatch(split / +/, $_, 2) |
| 117 |
or $self->respond(502, "command unrecognized: '$_'"); |
| 118 |
alarm $timeout; |
| 119 |
} |
| 120 |
alarm(0); |
| 121 |
return if $self->connection->notes('disconnected'); |
| 122 |
$self->reset_transaction; |
| 123 |
$self->run_hooks('disconnect'); |
| 124 |
$self->connection->notes(disconnected => 1); |
| 125 |
} |
| 126 |
|
| 127 |
sub respond { |
| 128 |
my ($self, $code, @messages) = @_; |
| 129 |
my $buf = ''; |
| 130 |
|
| 131 |
if (!$self->check_socket()) { |
| 132 |
$self->log(LOGERROR, |
| 133 |
"Lost connection to client, cannot send response."); |
| 134 |
return (0); |
| 135 |
} |
| 136 |
|
| 137 |
while (my $msg = shift @messages) { |
| 138 |
my $line = $code . (@messages ? "-" : " ") . $msg; |
| 139 |
$self->log(LOGINFO, $line); |
| 140 |
$buf .= "$line\r\n"; |
| 141 |
} |
| 142 |
print $buf |
| 143 |
or ($self->log(LOGERROR, "Could not print [$buf]: $!"), return 0); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
sub disconnect { |
| 148 |
my $self = shift; |
| 149 |
$self->log(LOGINFO, "click, disconnecting"); |
| 150 |
$self->SUPER::disconnect(@_); |
| 151 |
$self->run_hooks("post-connection"); |
| 152 |
$self->connection->reset; |
| 153 |
exit; |
| 154 |
} |
| 155 |
|
| 156 |
# local/remote port and ip address |
| 157 |
sub lrpip { |
| 158 |
my ($server, $client, $hisaddr) = @_; |
| 159 |
|
| 160 |
my ($port, $iaddr) = |
| 161 |
($server->sockdomain == AF_INET) |
| 162 |
? (sockaddr_in($hisaddr)) |
| 163 |
: (sockaddr_in6($hisaddr)); |
| 164 |
my $localsockaddr = getsockname($client); |
| 165 |
my ($lport, $laddr) = |
| 166 |
($server->sockdomain == AF_INET) |
| 167 |
? (sockaddr_in($localsockaddr)) |
| 168 |
: (sockaddr_in6($localsockaddr)); |
| 169 |
|
| 170 |
my $nto_iaddr = |
| 171 |
($server->sockdomain == AF_INET) |
| 172 |
? (inet_ntoa($iaddr)) |
| 173 |
: (inet_ntop(AF_INET6(), $iaddr)); |
| 174 |
my $nto_laddr = |
| 175 |
($server->sockdomain == AF_INET) |
| 176 |
? (inet_ntoa($laddr)) |
| 177 |
: (inet_ntop(AF_INET6(), $laddr)); |
| 178 |
$nto_iaddr =~ s/::ffff://; |
| 179 |
$nto_laddr =~ s/::ffff://; |
| 180 |
|
| 181 |
return ($port, $iaddr, $lport, $laddr, $nto_iaddr, $nto_laddr); |
| 182 |
} |
| 183 |
|
| 184 |
sub tcpenv { |
| 185 |
my ($nto_laddr, $nto_iaddr, $no_rdns) = @_; |
| 186 |
|
| 187 |
my $TCPLOCALIP = $nto_laddr; |
| 188 |
my $TCPREMOTEIP = $nto_iaddr; |
| 189 |
|
| 190 |
if ($no_rdns) { |
| 191 |
return ($TCPLOCALIP, $TCPREMOTEIP, |
| 192 |
$TCPREMOTEIP ? "[$ENV{TCPREMOTEIP}]" : "[noip!]"); |
| 193 |
} |
| 194 |
my $res = Net::DNS::Resolver->new( dnsrch => 0 ); |
| 195 |
$res->tcp_timeout(3); |
| 196 |
$res->udp_timeout(3); |
| 197 |
my $query = $res->query($nto_iaddr, 'PTR'); |
| 198 |
my $TCPREMOTEHOST; |
| 199 |
if ($query) { |
| 200 |
foreach my $rr ($query->answer) { |
| 201 |
next if $rr->type ne 'PTR'; |
| 202 |
$TCPREMOTEHOST = $rr->ptrdname; |
| 203 |
} |
| 204 |
} |
| 205 |
return ($TCPLOCALIP, $TCPREMOTEIP, $TCPREMOTEHOST || 'Unknown'); |
| 206 |
} |
| 207 |
|
| 208 |
sub check_socket() { |
| 209 |
my $self = shift; |
| 210 |
|
| 211 |
return 1 if ($self->{__client_socket}->connected); |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
1; |