| 1 |
#!perl -w |
| 2 |
|
| 3 |
use Qpsmtpd::Plugin::Async::DNSBLBase; |
| 4 |
|
| 5 |
sub init { |
| 6 |
my ($self, $qp, $denial) = @_; |
| 7 |
my $class = ref $self; |
| 8 |
|
| 9 |
{ |
| 10 |
no strict 'refs'; |
| 11 |
push @{"${class}::ISA"}, 'Qpsmtpd::Plugin::Async::DNSBLBase'; |
| 12 |
} |
| 13 |
|
| 14 |
if (defined $denial and $denial =~ /^disconnect$/i) { |
| 15 |
$self->{_dnsbl}->{DENY} = DENY_DISCONNECT; |
| 16 |
} |
| 17 |
else { |
| 18 |
$self->{_dnsbl}->{DENY} = DENY; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
sub hook_connect { |
| 23 |
my ($self, $transaction) = @_; |
| 24 |
my $class = ref $self; |
| 25 |
|
| 26 |
my $remote_ip = $self->connection->remote_ip; |
| 27 |
|
| 28 |
my $allow = |
| 29 |
grep { s/\.?$/./; $_ eq substr($remote_ip . '.', 0, length $_) } |
| 30 |
$self->qp->config('dnsbl_allow'); |
| 31 |
return DECLINED if $allow; |
| 32 |
|
| 33 |
my %dnsbl_zones = |
| 34 |
map { (split /:/, $_, 2)[0, 1] } $self->qp->config('dnsbl_zones'); |
| 35 |
return DECLINED unless %dnsbl_zones; |
| 36 |
|
| 37 |
my $reversed_ip = join(".", reverse(split(/\./, $remote_ip))); |
| 38 |
|
| 39 |
my @A_zones = grep { defined($dnsbl_zones{$_}) } keys %dnsbl_zones; |
| 40 |
my @TXT_zones = grep { !defined($dnsbl_zones{$_}) } keys %dnsbl_zones; |
| 41 |
|
| 42 |
if (@A_zones) { |
| 43 |
|
| 44 |
# message templates for responding to the client |
| 45 |
$self->connection->notes( |
| 46 |
dnsbl_templates => { |
| 47 |
map { |
| 48 |
+"$reversed_ip.$_" => $dnsbl_zones{$_} |
| 49 |
} @A_zones |
| 50 |
} |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return DECLINED |
| 55 |
unless $class->lookup($self->qp, |
| 56 |
[map { "$reversed_ip.$_" } @A_zones], |
| 57 |
[map { "$reversed_ip.$_" } @TXT_zones], |
| 58 |
); |
| 59 |
|
| 60 |
return YIELD; |
| 61 |
} |
| 62 |
|
| 63 |
sub process_a_result { |
| 64 |
my ($class, $qp, $result, $query) = @_; |
| 65 |
|
| 66 |
my $conn = $qp->connection; |
| 67 |
return if $class->connection->notes('dnsbl'); |
| 68 |
|
| 69 |
my $templates = $class->connection->notes('dnsbl_templates'); |
| 70 |
my $ip = $conn->remote_ip; |
| 71 |
|
| 72 |
my $template = $templates->{$query}; |
| 73 |
$template =~ s/%IP%/$ip/g; |
| 74 |
|
| 75 |
$class->connection->notes('dnsbl', $template); |
| 76 |
} |
| 77 |
|
| 78 |
sub process_txt_result { |
| 79 |
my ($class, $qp, $result, $query) = @_; |
| 80 |
|
| 81 |
my $conn = $class->connection; |
| 82 |
$conn->notes('dnsbl', $result) unless $conn->notes('dnsbl'); |
| 83 |
} |
| 84 |
|
| 85 |
sub hook_rcpt { |
| 86 |
my ($self, $transaction, $rcpt) = @_; |
| 87 |
my $connection = $self->qp->connection; |
| 88 |
|
| 89 |
# RBLSMTPD being non-empty means it contains the failure message to return |
| 90 |
if (defined($ENV{'RBLSMTPD'}) && $ENV{'RBLSMTPD'} ne '') { |
| 91 |
my $result = $ENV{'RBLSMTPD'}; |
| 92 |
my $remote_ip = $self->connection->remote_ip; |
| 93 |
$result =~ s/%IP%/$remote_ip/g; |
| 94 |
return (DENY, join(" ", $self->qp->config('dnsbl_rejectmsg'), $result)); |
| 95 |
} |
| 96 |
|
| 97 |
my $note = $self->connection->notes('dnsbl'); |
| 98 |
return (DENY, $note) if $note; |
| 99 |
return DECLINED; |
| 100 |
} |
| 101 |
|
| 102 |
=head1 NAME |
| 103 |
|
| 104 |
dnsbl - handle DNS BlackList lookups |
| 105 |
|
| 106 |
=head1 DESCRIPTION |
| 107 |
|
| 108 |
Plugin that checks the IP address of the incoming connection against |
| 109 |
a configurable set of RBL services. |
| 110 |
|
| 111 |
=head1 Configuration files |
| 112 |
|
| 113 |
This plugin uses the following configuration files. All of these are optional. |
| 114 |
However, not specifying dnsbl_zones is like not using the plugin at all. |
| 115 |
|
| 116 |
=over 4 |
| 117 |
|
| 118 |
=item dnsbl_zones |
| 119 |
|
| 120 |
Normal ip based dns blocking lists ("RBLs") which contain TXT records are |
| 121 |
specified simply as: |
| 122 |
|
| 123 |
relays.ordb.org |
| 124 |
spamsources.fabel.dk |
| 125 |
|
| 126 |
To configure RBL services which do not contain TXT records in the DNS, |
| 127 |
but only A records (e.g. the RBL+ at http://www.mail-abuse.org), specify your |
| 128 |
own error message to return in the SMTP conversation after a colon e.g. |
| 129 |
|
| 130 |
rbl-plus.mail-abuse.org:You are listed at - http://http://www.mail-abuse.org/cgi-bin/lookup?%IP% |
| 131 |
|
| 132 |
The string %IP% will be replaced with the IP address of incoming connection. |
| 133 |
Thus a fully specified file could be: |
| 134 |
|
| 135 |
sbl-xbl.spamhaus.org |
| 136 |
list.dsbl.org |
| 137 |
rbl-plus.mail-abuse.ja.net:Listed by rbl-plus.mail-abuse.ja.net - see <URL:http://www.mail-abuse.org/cgi-bin/lookup?%IP%> |
| 138 |
relays.ordb.org |
| 139 |
|
| 140 |
=item dnsbl_allow |
| 141 |
|
| 142 |
List of allowed ip addresses that bypass RBL checking. Format is one entry per line, |
| 143 |
with either a full IP address or a truncated IP address with a period at the end. |
| 144 |
For example: |
| 145 |
|
| 146 |
192.168.1.1 |
| 147 |
172.16.33. |
| 148 |
|
| 149 |
NB the environment variable RBLSMTPD is considered before this file is |
| 150 |
referenced. See below. |
| 151 |
|
| 152 |
=item dnsbl_rejectmsg |
| 153 |
|
| 154 |
A textual message that is sent to the sender on an RBL failure. The TXT record |
| 155 |
from the RBL list is also sent, but this file can be used to indicate what |
| 156 |
action the sender should take. |
| 157 |
|
| 158 |
For example: |
| 159 |
|
| 160 |
If you think you have been blocked in error, then please forward |
| 161 |
this entire error message to your ISP so that they can fix their problems. |
| 162 |
The next line often contains a URL that can be visited for more information. |
| 163 |
|
| 164 |
=back |
| 165 |
|
| 166 |
=head1 Environment Variables |
| 167 |
|
| 168 |
=head2 RBLSMTPD |
| 169 |
|
| 170 |
The environment variable RBLSMTPD is supported and mimics the behaviour of |
| 171 |
Dan Bernstein's rblsmtpd. The exception to this is the '-' char at the |
| 172 |
start of RBLSMTPD which is used to force a hard error in Dan's rblsmtpd. |
| 173 |
NB I don't really see the benefit |
| 174 |
of using a soft error for a site in an RBL list. This just complicates |
| 175 |
things as it takes 7 days (or whatever default period) before a user |
| 176 |
gets an error email back. In the meantime they are complaining that their |
| 177 |
emails are being "lost" :( |
| 178 |
|
| 179 |
=over 4 |
| 180 |
|
| 181 |
=item RBLSMTPD is set and non-empty |
| 182 |
|
| 183 |
The contents are used as the SMTP conversation error. |
| 184 |
Use this for forcibly blocking sites you don't like |
| 185 |
|
| 186 |
=item RBLSMTPD is set, but empty |
| 187 |
|
| 188 |
In this case no RBL checks are made. |
| 189 |
This can be used for local addresses. |
| 190 |
|
| 191 |
=item RBLSMTPD is not set |
| 192 |
|
| 193 |
All RBL checks will be made. |
| 194 |
This is the setting for remote sites that you want to check against RBL. |
| 195 |
|
| 196 |
=back |
| 197 |
|
| 198 |
=head1 Revisions |
| 199 |
|
| 200 |
See: http://cvs.perl.org/viewcvs/qpsmtpd/plugins/dnsbl |
| 201 |
|
| 202 |
=cut |