| 1 | # | ||
| 2 | # Courier::Filter::Module::DNSBL class | ||
| 3 | # | ||
| 4 | # (C) 2003-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: DNSBL.pm 210 2008-03-21 19:30:31Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::DNSBL - DNS black-list filter module for the | ||
| 12 | Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::DNSBL; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use IO::File; | ||
| 24 | use Net::RBLClient; | ||
| 25 | |||
| 26 | use Courier::Filter::Util qw( | ||
| 27 | ipv4_address_pattern | ||
| 28 | loopback_address_pattern | ||
| 29 | ); | ||
| 30 | |||
| 31 | use constant TRUE => (0 == 0); | ||
| 32 | use constant FALSE => not TRUE; | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use Courier::Filter::Module::DNSBL; | ||
| 37 | |||
| 38 | my $module = Courier::Filter::Module::DNSBL->new( | ||
| 39 | zones => \@dns_zones, | ||
| 40 | |||
| 41 | logger => $logger, | ||
| 42 | inverse => 0, | ||
| 43 | trusting => 0, | ||
| 44 | testing => 0, | ||
| 45 | debugging => 0 | ||
| 46 | ); | ||
| 47 | |||
| 48 | my $filter = Courier::Filter->new( | ||
| 49 | ... | ||
| 50 | modules => [ $module ], | ||
| 51 | ... | ||
| 52 | ); | ||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | This class is a filter module class for use with Courier::Filter. It matches a | ||
| 57 | message if the sending machine's IP address (currently IPv4 only) is listed by | ||
| 58 | one of the configured DNS black-lists. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | # Implementation: | ||
| 63 | ############################################################################### | ||
| 64 | |||
| 65 | =head2 Constructor | ||
| 66 | |||
| 67 | The following constructor is provided: | ||
| 68 | |||
| 69 | =over | ||
| 70 | |||
| 71 | =item B<new(%options)>: returns I<Courier::Filter::Module::DNSBL> | ||
| 72 | |||
| 73 | Creates a new B<DNSBL> filter module. | ||
| 74 | |||
| 75 | %options is a list of key/value pairs representing any of the following | ||
| 76 | options: | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item B<zones> | ||
| 81 | |||
| 82 | I<Required>. A reference to an array containing the DNS zone names of the | ||
| 83 | black-lists to be used. | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | All options of the B<Courier::Filter::Module> constructor are also supported. | ||
| 88 | Please see L<Courier::Filter::Module/"new()"> for their descriptions. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub new { | ||
| 93 | my ($class, %options) = @_; | ||
| 94 | |||
| 95 | my $dnsbl_client = Net::RBLClient->new( | ||
| 96 | lists => $options{zones}, | ||
| 97 | query_txt => TRUE, | ||
| 98 | max_time => 10 | ||
| 99 | ); | ||
| 100 | |||
| 101 | return $class->SUPER::new( | ||
| 102 | %options, | ||
| 103 | dnsbl_client => $dnsbl_client | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =head2 Instance methods | ||
| 110 | |||
| 111 | See L<Courier::Filter::Module/"Instance methods"> for a description of the | ||
| 112 | provided instance methods. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub match { | ||
| 117 | my ($self, $message) = @_; | ||
| 118 | |||
| 119 | return undef | ||
| 120 | if $message->remote_host !~ / ^ (?: ::ffff: )? ( ${\ipv4_address_pattern} ) $ /ix; | ||
| 121 | # Ignore IPv6 senders for now, as Net::RBLClient doesn't support it. | ||
| 122 | |||
| 123 | my $remote_host_ipv4 = $1; | ||
| 124 | |||
| 125 | return undef | ||
| 126 | if $message->remote_host =~ / ^ ${\loopback_address_pattern} $ /x; | ||
| 127 | # Exempt IPv4/IPv6 loopback addresses, i.e., self submissions. | ||
| 128 | |||
| 129 | my $dnsbl_client = $self->{dnsbl_client}; | ||
| 130 | |||
| 131 | $dnsbl_client->lookup($remote_host_ipv4); | ||
| 132 | |||
| 133 | my $result; | ||
| 134 | my $results = $dnsbl_client->txt_hash(); | ||
| 135 | if (keys(%$results)) { | ||
| 136 | $result = join( | ||
| 137 | "\n", | ||
| 138 | map( | ||
| 139 | sprintf("DNSBL/%s: %s", $_, $results->{$_}), | ||
| 140 | keys(%$results) | ||
| 141 | ) | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | return $result; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>. | ||
| 151 | |||
| 152 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 153 | L<Courier::Filter::Overview>. | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | Julian Mehnle <julian@mehnle.net> | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | TRUE; |