diff --git a/plugins/check_earlytalker b/plugins/check_earlytalker index b44192b..f21748b 100644 --- a/plugins/check_earlytalker +++ b/plugins/check_earlytalker @@ -62,12 +62,46 @@ sub register { 'defer-reject' => 0, @args, }; - $self->register_hook('connect', 'connect_handler'); + if ($qp->{conn} && $qp->{conn}->isa('Apache2::Connection')) { + require APR::Const; + APR::Const->import(qw(POLLIN SUCCESS)); + $self->register_hook('connect', 'apr_connect_handler'); + } + else { + $self->register_hook('connect', 'connect_handler'); + } $self->register_hook('mail', 'mail_handler') if $self->{_args}->{'defer-reject'}; 1; } +sub apr_connect_handler { + my ($self, $transaction) = @_; + + return DECLINED if ($self->qp->connection->notes('whitelistclient')); + my $ip = $self->qp->connection->remote_ip; + + my $c = $self->qp->{conn}; + my $socket = $c->client_socket; + my $timeout = $self->{_args}->{'wait'} * 1_000_000; + + my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN()); + if ($rc == APR::Const::SUCCESS()) { + $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]"); + if ($self->{_args}->{'defer-reject'}) { + $self->qp->connection->notes('earlytalker', 1); + } + else { + my $msg = 'Connecting host started transmitting before SMTP greeting'; + return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny'; + return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft'; + } + } + else { + $self->log(LOGINFO, "remote host said nothing spontaneous, proceeding"); + } +} + sub connect_handler { my ($self, $transaction) = @_; my $in = new IO::Select;