2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2006-01-11 18:03:45 +01:00
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
=head1 NAME
|
2006-01-11 18:03:45 +01:00
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
hosts_allow - decide if a host is allowed to connect
|
2006-01-11 18:03:45 +01:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
The B<hosts_allow> module decides before the SMTP-Greeting if a host is
|
|
|
|
allowed to connect. It checks for too many (running) connections from one
|
2012-05-11 07:50:03 +02:00
|
|
|
host (see -m/--max-from-ip options in qpsmtpd-forkserver) and the config
|
2006-01-11 18:03:45 +01:00
|
|
|
file I<hosts_allow>.
|
2012-05-11 07:50:03 +02:00
|
|
|
|
|
|
|
The plugin takes no config/plugin arguments.
|
|
|
|
|
|
|
|
This plugin only works with the forkserver and prefork deployment models. It
|
|
|
|
does not work with the tcpserver deployment model. See SEE ALSO below.
|
2006-01-11 18:03:45 +01:00
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
The I<hosts_allow> config file contains lines with two or three items. The
|
|
|
|
first is an IP address or a network/mask pair. The second is a (valid) return
|
|
|
|
code from Qpsmtpd::Constants. The last is a comment which will be returned to
|
|
|
|
the connecting client if the return code is DENY or DENYSOFT (and of course
|
|
|
|
DENY_DISCONNECT and DENYSOFT_DISCONNECT).
|
|
|
|
|
2006-01-11 18:03:45 +01:00
|
|
|
Example:
|
|
|
|
|
|
|
|
192.168.3.4 DECLINED
|
|
|
|
192.168.3.0/24 DENY Sorry, known spam only source
|
|
|
|
|
|
|
|
This would exclude 192.168.3.4 from the DENY of 192.168.3.0/24.
|
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
To get similar functionality for the tcpserver deployment model, use
|
|
|
|
tcpserver's -x feature. Create a tcp.smtp file with entries like this:
|
|
|
|
|
|
|
|
70.65.227.235:deny
|
|
|
|
183.7.90.207:deny
|
|
|
|
:allow
|
|
|
|
|
|
|
|
compile the tcp.smtp file like this:
|
|
|
|
|
|
|
|
/usr/local/bin/tcprules tcp.smtp.cdb tcp.smtp.tmp < tcp.smtp
|
|
|
|
|
|
|
|
and add the file to the chain of arguments to tcpserver in your run file.
|
|
|
|
|
|
|
|
See also: http://cr.yp.to/ucspi-tcp.html
|
|
|
|
|
2006-01-11 18:03:45 +01:00
|
|
|
=cut
|
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2006-01-11 18:03:45 +01:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use Socket;
|
|
|
|
|
|
|
|
sub hook_pre_connection {
|
|
|
|
my ($self,$transaction,%args) = @_;
|
|
|
|
|
|
|
|
# remote_ip => inet_ntoa($iaddr),
|
|
|
|
# remote_port => $port,
|
|
|
|
# local_ip => inet_ntoa($laddr),
|
|
|
|
# local_port => $lport,
|
|
|
|
# max_conn_ip => $MAXCONNIP,
|
2012-05-11 07:50:03 +02:00
|
|
|
# child_addrs => [values %childstatus],
|
2006-01-11 18:03:45 +01:00
|
|
|
|
|
|
|
my $remote = $args{remote_ip};
|
2012-05-11 07:50:03 +02:00
|
|
|
my $max = $args{max_conn_ip};
|
2013-03-13 08:19:48 +01:00
|
|
|
my $karma = $self->connection->notes('karma_history');
|
2006-01-11 18:03:45 +01:00
|
|
|
|
2012-05-11 07:50:03 +02:00
|
|
|
if ( $max ) {
|
2006-01-11 18:03:45 +01:00
|
|
|
my $num_conn = 1; # seed with current value
|
|
|
|
my $raddr = inet_aton($remote);
|
|
|
|
foreach my $rip (@{$args{child_addrs}}) {
|
|
|
|
++$num_conn if (defined $rip && $rip eq $raddr);
|
|
|
|
}
|
2013-03-13 08:19:48 +01:00
|
|
|
$max = $self->karma_bump( $karma, $max ) if defined $karma;
|
2012-05-11 07:50:03 +02:00
|
|
|
if ($num_conn > $max ) {
|
|
|
|
my $err_mess = "too many connections from $remote";
|
|
|
|
$self->log(LOGINFO, "fail: $err_mess ($num_conn > $max)");
|
2013-03-23 06:47:34 +01:00
|
|
|
return (DENYSOFT, "$err_mess, try again later");
|
2006-01-11 18:03:45 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-11 07:50:03 +02:00
|
|
|
|
2012-06-23 06:45:18 +02:00
|
|
|
my @r = $self->in_hosts_allow( $remote );
|
|
|
|
return @r if scalar @r;
|
|
|
|
|
2013-03-23 06:47:34 +01:00
|
|
|
$self->log(LOGDEBUG, "pass" );
|
2012-06-23 06:45:18 +02:00
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub in_hosts_allow {
|
|
|
|
my $self = shift;
|
|
|
|
my $remote = shift;
|
|
|
|
|
|
|
|
foreach ( $self->qp->config('hosts_allow') ) {
|
|
|
|
s/^\s*//; # trim leading whitespace
|
2006-01-11 18:03:45 +01:00
|
|
|
my ($ipmask, $const, $message) = split /\s+/, $_, 3;
|
|
|
|
next unless defined $const;
|
|
|
|
|
2012-11-20 07:40:57 +01:00
|
|
|
my ($net,$mask) = split /\//, $ipmask, 2;
|
2012-06-23 06:45:18 +02:00
|
|
|
$mask = 32 if ! defined $mask;
|
2006-01-11 18:03:45 +01:00
|
|
|
$mask = pack "B32", "1"x($mask)."0"x(32-$mask);
|
2012-06-23 06:45:18 +02:00
|
|
|
if (join('.', unpack('C4', inet_aton($remote) & $mask)) eq $net) {
|
2006-01-11 18:03:45 +01:00
|
|
|
$const = Qpsmtpd::Constants::return_code($const) || DECLINED;
|
2012-05-11 07:50:03 +02:00
|
|
|
if ( $const =~ /deny/i ) {
|
2012-06-23 06:45:18 +02:00
|
|
|
$self->log( LOGINFO, "fail, $message" );
|
2012-05-11 07:50:03 +02:00
|
|
|
};
|
2012-06-23 06:45:18 +02:00
|
|
|
$self->log( LOGDEBUG, "pass, $const, $message" );
|
2006-01-11 18:03:45 +01:00
|
|
|
return($const, $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-23 06:45:18 +02:00
|
|
|
return;
|
|
|
|
};
|
2013-03-13 08:19:48 +01:00
|
|
|
|
|
|
|
sub karma_bump {
|
|
|
|
my ($self, $karma, $max) = @_;
|
2013-03-23 06:47:34 +01:00
|
|
|
|
|
|
|
if ( $karma > 5 ) {
|
2013-04-21 05:48:24 +02:00
|
|
|
$self->log(LOGDEBUG, "connect limit +3 for positive karma");
|
2013-03-23 06:47:34 +01:00
|
|
|
return $max + 3;
|
|
|
|
};
|
2013-03-13 08:19:48 +01:00
|
|
|
if ( $karma <= 0 ) {
|
2013-04-21 05:48:24 +02:00
|
|
|
$self->log(LOGINFO, "connect limit 1, karma $karma");
|
2013-03-13 08:19:48 +01:00
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
return $max;
|
|
|
|
};
|