| 1 | # $Id: ConnectDNSBL.pm,v 1.6 2004/04/15 18:37:56 tvierling Exp $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2002-2004 Todd Vierling <tv@pobox.com> <tv@duh.org> | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||
| 14 | # documentation and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # 3. Neither the name of the author nor the names of contributors may be used | ||
| 17 | # to endorse or promote products derived from this software without specific | ||
| 18 | # prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | |||
| 32 | package Mail::Milter::Module::ConnectDNSBL; | ||
| 33 | |||
| 34 | use 5.006; | ||
| 35 | use base Exporter; | ||
| 36 | use base Mail::Milter::Object; | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | |||
| 41 | use Carp; | ||
| 42 | use Sendmail::Milter 0.18; # get needed constants | ||
| 43 | use Socket; | ||
| 44 | use UNIVERSAL; | ||
| 45 | |||
| 46 | our $VERSION = '0.02'; | ||
| 47 | |||
| 48 | =pod | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | Mail::Milter::Module::ConnectDNSBL - milter to accept/reject connecting hosts matching DNSBLs | ||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | use Mail::Milter::Module::ConnectDNSBL; | ||
| 57 | |||
| 58 | my $milter = new Mail::Milter::Module::ConnectDNSBL('foo.spamlist.dom'); | ||
| 59 | |||
| 60 | my $milter2 = &ConnectDNSBL('foo.spamlist.dom'); # convenience | ||
| 61 | |||
| 62 | $milter2->set_message('Connections from %A disallowed'); | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This milter module rejects any connecting host whose IPv4 address matches | ||
| 67 | a given DNS Blocking List (DNSBL). It can also function as a whitelisting | ||
| 68 | Chain element; see C<accept_match()>. | ||
| 69 | |||
| 70 | The check used by this module is a simple "A" record lookup, via the | ||
| 71 | standard "gethostbyname" lookup mechanism. This method does not require | ||
| 72 | the use of Net::DNS and is thus typically very fast. | ||
| 73 | |||
| 74 | (Note: If the connecting host is not using IPv4, this module will simply | ||
| 75 | be a passthrough using SMFIS_CONTINUE.) | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | our @EXPORT = qw(&ConnectDNSBL); | ||
| 84 | |||
| 85 | sub ConnectDNSBL { | ||
| 86 | new Mail::Milter::Module::ConnectDNSBL(@_); | ||
| 87 | } | ||
| 88 | |||
| 89 | =pod | ||
| 90 | |||
| 91 | =item new(DNSBL) | ||
| 92 | |||
| 93 | =item new(DNSBL, MATCHRECORD[, ...]) | ||
| 94 | |||
| 95 | =item new(DNSBL, SUBREF) | ||
| 96 | |||
| 97 | Creates a ConnectDNSBL object. DNSBL is the root host hierarchy to use | ||
| 98 | for lookups. Three methods of matching can be used: | ||
| 99 | |||
| 100 | If no additional arguments are provided, the match succeeds if there is | ||
| 101 | any address entry present for the DNSBL lookup; the values are not | ||
| 102 | examined. | ||
| 103 | |||
| 104 | If one or more MATCHRECORD values are supplied, they are string | ||
| 105 | representations of IPv4 addresses. If any of these match record values is | ||
| 106 | the same as any address record returned by the DNSBL lookup, the match | ||
| 107 | succeeds. | ||
| 108 | |||
| 109 | If a SUBREF (reference to a subroutine; may be an anonymous inline | ||
| 110 | C<sub{}>) is supplied, it is called for each of the address records | ||
| 111 | returned by the DNSBL lookup. The subroutine should return 0 or undef to | ||
| 112 | indicate a failed match, and nonzero to indicate a successful match. The | ||
| 113 | subroutine receives a binary-encoded four byte scalar that should be | ||
| 114 | transformed as needed with C<inet_ntoa()> or C<unpack>. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub new ($$;@) { | ||
| 119 | my $this = Mail::Milter::Object::new(shift); | ||
| 120 | my $dnsbl = $this->{_dnsbl} = shift; | ||
| 121 | |||
| 122 | $this->{_accept} = 0; | ||
| 123 | $this->{_ignoretempfail} = 0; | ||
| 124 | $this->{_message} = 'Access denied to host %A (address is listed by %L)'; | ||
| 125 | |||
| 126 | if (UNIVERSAL::isa($_[0], 'CODE')) { | ||
| 127 | $this->{_matcher} = shift; | ||
| 128 | } else { | ||
| 129 | my @records; | ||
| 130 | |||
| 131 | foreach my $record (@_) { | ||
| 132 | my $addr = inet_aton($record); | ||
| 133 | |||
| 134 | croak "new ConnectDNSBL: address $record is not a valid IPv4 address" unless defined($addr); | ||
| 135 | |||
| 136 | push(@records, $addr); | ||
| 137 | } | ||
| 138 | |||
| 139 | if (scalar @records) { | ||
| 140 | $this->{_matcher} = sub { | ||
| 141 | my $addr = shift; | ||
| 142 | foreach my $record (@records) { | ||
| 143 | return 1 if ($addr eq $record); | ||
| 144 | } | ||
| 145 | undef; | ||
| 146 | }; | ||
| 147 | } else { | ||
| 148 | $this->{_matcher} = sub { 1 }; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | $this; | ||
| 153 | } | ||
| 154 | |||
| 155 | =pod | ||
| 156 | |||
| 157 | =item accept_match(FLAG) | ||
| 158 | |||
| 159 | If FLAG is 0 (the default), a matching DNSBL will cause the connection to | ||
| 160 | be rejected. | ||
| 161 | |||
| 162 | If FLAG is 1, a matching DNSBL will cause this module to return SMFIS_ACCEPT | ||
| 163 | instead. This allows a C<ConnectDNSBL> to be used inside a | ||
| 164 | C<Mail::Milter::Chain> container (in C<accept_break(1)> mode), to function | ||
| 165 | as a whitelist rather than a blacklist. | ||
| 166 | |||
| 167 | This method returns a reference to the object itself, allowing this method | ||
| 168 | call to be chained. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub accept_match ($$) { | ||
| 173 | my $this = shift; | ||
| 174 | my $flag = shift; | ||
| 175 | |||
| 176 | croak 'accept_match: flag argument is undef' unless defined($flag); | ||
| 177 | $this->{_accept} = $flag; | ||
| 178 | |||
| 179 | $this; | ||
| 180 | } | ||
| 181 | |||
| 182 | =pod | ||
| 183 | |||
| 184 | =item ignore_tempfail(FLAG) | ||
| 185 | |||
| 186 | If FLAG is 0 (the default), a DNSBL lookup which fails the underlying DNS | ||
| 187 | query will cause the milter to return a temporary failure result | ||
| 188 | (SMFIS_TEMPFAIL). | ||
| 189 | |||
| 190 | If FLAG is 1, a temporary DNS failure will be treated as if the lookup | ||
| 191 | resulted in an empty record set (SMFIS_CONTINUE). | ||
| 192 | |||
| 193 | This method returns a reference to the object itself, allowing this method | ||
| 194 | call to be chained. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub ignore_tempfail ($$) { | ||
| 199 | my $this = shift; | ||
| 200 | my $flag = shift; | ||
| 201 | |||
| 202 | croak 'ignore_tempfail: flag argument is undef' unless defined($flag); | ||
| 203 | $this->{_ignoretempfail} = $flag; | ||
| 204 | |||
| 205 | $this; | ||
| 206 | } | ||
| 207 | |||
| 208 | =pod | ||
| 209 | |||
| 210 | =item set_message(MESSAGE) | ||
| 211 | |||
| 212 | Sets the message used when rejecting connections. This string may contain | ||
| 213 | the substring C<%A>, which will be replaced by the matching IPv4 address, | ||
| 214 | or C<%L>, which will be replaced by the name of the matching DNSBL. | ||
| 215 | |||
| 216 | This method returns a reference to the object itself, allowing this method | ||
| 217 | call to be chained. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub set_message ($$) { | ||
| 222 | my $this = shift; | ||
| 223 | |||
| 224 | $this->{_message} = shift; | ||
| 225 | |||
| 226 | $this; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub connect_callback { | ||
| 230 | my $this = shift; | ||
| 231 | my $ctx = shift; | ||
| 232 | shift; # $hostname | ||
| 233 | my $pack = shift; | ||
| 234 | |||
| 235 | my $addr = eval { | ||
| 236 | my @unpack = unpack_sockaddr_in($pack); | ||
| 237 | $unpack[1]; | ||
| 238 | }; | ||
| 239 | |||
| 240 | return SMFIS_CONTINUE unless defined($addr); | ||
| 241 | |||
| 242 | my $dnsbl = $this->{_dnsbl}; | ||
| 243 | my $lookup = join('.', reverse(unpack('C4', $addr))).'.'.$dnsbl; | ||
| 244 | my @lookup_addrs; | ||
| 245 | (undef, undef, undef, undef, @lookup_addrs) = gethostbyname($lookup); | ||
| 246 | |||
| 247 | unless (scalar @lookup_addrs) { | ||
| 248 | # h_errno 1 == HOST_NOT_FOUND | ||
| 249 | return SMFIS_CONTINUE if ($? == 1 || $this->{_ignoretempfail}); | ||
| 250 | |||
| 251 | $ctx->setreply('451', '4.7.1', "Temporary failure in DNS lookup for $lookup"); | ||
| 252 | return SMFIS_TEMPFAIL; | ||
| 253 | } | ||
| 254 | |||
| 255 | foreach my $lookup_addr (@lookup_addrs) { | ||
| 256 | if (&{$this->{_matcher}}($lookup_addr)) { | ||
| 257 | return SMFIS_ACCEPT if $this->{_accept}; | ||
| 258 | |||
| 259 | my $msg = $this->{_message}; | ||
| 260 | |||
| 261 | if (defined($msg)) { | ||
| 262 | my $haddr = inet_ntoa($addr); | ||
| 263 | $msg =~ s/%A/$haddr/g; | ||
| 264 | $msg =~ s/%L/$dnsbl/g; | ||
| 265 | $ctx->setreply('554', '5.7.1', $msg); | ||
| 266 | } | ||
| 267 | |||
| 268 | return SMFIS_REJECT; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | SMFIS_CONTINUE; # don't whitelist a fallthrough | ||
| 273 | } | ||
| 274 | |||
| 275 | 1; | ||
| 276 | __END__ |