Milter/Module/MailDomainDNSBL.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 166 CC 15 SCAR 35.1
CC
15
Cov
0%
envfrom_callback
90.0
new
30.0
accept_match
6.0
CRAP 33.6
1 # $Id: MailDomainDNSBL.pm,v 1.9 2006/03/22 15:48:23 tvierling Exp $
2 #
3 # Copyright (c) 2002-2006 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::MailDomainDNSBL;
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.04';
47  
48 =pod
49  
50 =head1 NAME
51  
52 Mail::Milter::Module::MailDomainDNSBL - milter to accept/reject mail whose sender domain matches a DNSBL
53  
54 =head1 SYNOPSIS
55  
56 use Mail::Milter::Module::MailDomainDNSBL;
57  
58 my $milter = new Mail::Milter::Module::MailDomainDNSBL('foo.spamlist.dom');
59  
60 my $milter2 = &MailDomainDNSBL('foo.spamlist.dom'); # convenience
61  
62 $milter2->set_message('Mail from %M disallowed');
63  
64 =head1 DESCRIPTION
65  
66 This milter module rejects any mail from a sender's domain (in the MAIL
67 FROM part of the SMTP transaction, not in the From: header) matching a
68 given DNS Blocking List (DNSBL). It can also function as a whitelisting
69 Chain element; see C<accept_match()>. (This is known as a "RHSBL" check
70 in some anti-spam lingo.)
71  
72 The check used by this module is a simple "A" record lookup, via the
73 standard "gethostbyname" lookup mechanism. This method does not require
74 the use of Net::DNS and is thus typically very fast.
75  
76 =head1 METHODS
77  
78 =over 4
79  
80 =cut
81  
82 our @EXPORT = qw(&MailDomainDNSBL);
83  
84 sub MailDomainDNSBL {
85 new Mail::Milter::Module::MailDomainDNSBL(@_);
86 }
87  
88 =pod
89  
90 =item new(DNSBL)
91  
92 =item new(DNSBL, MATCHRECORD[, ...])
93  
94 =item new(DNSBL, SUBREF)
95  
96 Creates a MailDomainDNSBL object. DNSBL is the root host hierarchy to use
97 for lookups. Three methods of matching can be used:
98  
99 If no additional arguments are provided, the match succeeds if there is
100 any address entry present for the DNSBL lookup; the values are not
101 examined.
102  
103 If one or more MATCHRECORD values are supplied, they are string
104 representations of IPv4 addresses. If any of these match record values is
105 the same as any address record returned by the DNSBL lookup, the match
106 succeeds.
107  
108 If a SUBREF (reference to a subroutine; may be an anonymous inline
109 C<sub{}>) is supplied, it is called for each of the address records
110 returned by the DNSBL lookup. The subroutine should return 0 or undef to
111 indicate a failed match, and nonzero to indicate a successful match. The
112 subroutine receives two arguments: a binary-encoded four byte scalar that
113 should be transformed as needed with C<inet_ntoa()> or C<unpack>, and the
114 domain name being checked by the DNSBL.
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->{_checksupers} = 0;
124 $this->{_ignoretempfail} = 0;
125 $this->{_message} = 'Access denied to sender address %M (domain is listed by %L)';
126  
127 if (UNIVERSAL::isa($_[0], 'CODE')) {
128 $this->{_matcher} = shift;
129 } else {
130 my @records;
131  
132 foreach my $record (@_) {
133 my $addr = inet_aton($record);
134  
135 croak "new MailDomainDNSBL: address $record is not a valid IPv4 address" unless defined($addr);
136  
137 push(@records, $addr);
138 }
139  
140 if (scalar @records) {
141 $this->{_matcher} = sub {
142 my $addr = shift;
143 foreach my $record (@records) {
144 return 1 if ($addr eq $record);
145 }
146 undef;
147 };
148 } else {
149 $this->{_matcher} = sub { 1 };
150 }
151 }
152  
153 $this;
154 }
155  
156 =pod
157  
158 =item accept_match(FLAG)
159  
160 If FLAG is 0 (the default), a matching DNSBL will cause the mail to be
161 rejected.
162  
163 If FLAG is 1, a matching DNSBL will cause this module to return
164 SMFIS_ACCEPT instead. This allows a C<MailDomainDNSBL> to be used inside
165 a C<Mail::Milter::Chain> container (in C<accept_break(1)> mode), to
166 function as a whitelist rather than a blacklist.
167  
168 This method returns a reference to the object itself, allowing this method
169 call to be chained.
170  
171 =cut
172  
173 sub accept_match ($$) {
174 my $this = shift;
175 my $flag = shift;
176  
177 croak 'accept_match: flag argument is undef' unless defined($flag);
178 $this->{_accept} = $flag;
179  
180 $this;
181 }
182  
183 =pod
184  
185 =item ignore_tempfail(FLAG)
186  
187 If FLAG is 0 (the default), a DNSBL lookup which fails the underlying DNS
188 query will cause the milter to return a temporary failure result
189 (SMFIS_TEMPFAIL).
190  
191 If FLAG is 1, a temporary DNS failure will be treated as if the lookup
192 resulted in an empty record set (SMFIS_CONTINUE).
193  
194 This method returns a reference to the object itself, allowing this method
195 call to be chained.
196  
197 =cut
198  
199 sub ignore_tempfail ($$) {
200 my $this = shift;
201 my $flag = shift;
202  
203 croak 'ignore_tempfail: flag argument is undef' unless defined($flag);
204 $this->{_ignoretempfail} = $flag;
205  
206 $this;
207 }
208  
209 =pod
210  
211 =item check_superdomains(NUM)
212  
213 If no match is returned by checking the domain name verbatim, recurse
214 one level upward at a time and attempt the check again. If NUM is
215 positive, the recursion will stop after NUM recursions; if negative,
216 the recursion will stop when abs(NUM) domain levels have been reached.
217 The default is 0, meaning that no recursion will be done.
218  
219 For example, when checking the domain name FOO.BAR.BAZ.COM, NUM=1 will
220 also check BAR.BAZ.COM; NUM=-1 will check BAR.BAZ.COM, BAZ.COM, and COM.
221  
222 This method returns a reference to the object itself, allowing this method
223 call to be chained.
224  
225 =cut
226  
227 sub check_superdomains ($$) {
228 my $this = shift;
229 my $flag = shift;
230  
231 $this->{_checksupers} = $flag;
232  
233 $this;
234 }
235  
236 =pod
237  
238 =item set_message(MESSAGE)
239  
240 Sets the message used when rejecting messages. This string may contain
241 the substring C<%M>, which will be replaced by the matching e-mail
242 address, or C<%L>, which will be replaced by the name of the matching
243 DNSBL.
244  
245 This method returns a reference to the object itself, allowing this method
246 call to be chained.
247  
248 =cut
249  
250 sub set_message ($$) {
251 my $this = shift;
252  
253 $this->{_message} = shift;
254  
255 $this;
256 }
257  
258 sub envfrom_callback {
259 my $this = shift;
260 my $ctx = shift;
261 my $from = lc(shift);
262  
263 $from =~ s/^<//;
264 $from =~ s/>$//;
265  
266 return SMFIS_CONTINUE if ($from eq ''); # null <> sender
267  
268 my $dnsbl = $this->{_dnsbl};
269 my $fdomain = $from;
270  
271 $fdomain =~ s/^[^\@]+\@//;
272  
273 my @domainparts = split(/\./, $fdomain);
274 my $startmax = $this->{_checksupers};
275  
276 $startmax += scalar(@domainparts) if ($startmax < 0);
277 $startmax = 0 if ($startmax < 0);
278  
279 for (my $i = 0; $i <= $#domainparts && $i <= $startmax; ++$i) {
280 my $fromdomain = join('.', @domainparts[$i..$#domainparts]);
281 my $lookup = $fromdomain.'.'.$dnsbl;
282 my @lookup_addrs;
283 (undef, undef, undef, undef, @lookup_addrs) = gethostbyname($lookup);
284  
285 unless (scalar @lookup_addrs) {
286 # h_errno 1 == HOST_NOT_FOUND
287 next if ($? == 1 || $this->{_ignoretempfail});
288  
289 $ctx->setreply('451', '4.7.1', "Temporary failure in DNS lookup for $lookup");
290 return SMFIS_TEMPFAIL;
291 }
292  
293 foreach my $lookup_addr (@lookup_addrs) {
294 if (&{$this->{_matcher}}($lookup_addr, $fromdomain)) {
295 return SMFIS_ACCEPT if $this->{_accept};
296  
297 my $msg = $this->{_message};
298  
299 if (defined($msg)) {
300 $msg =~ s/%M/$from/g;
301 $msg =~ s/%L/$dnsbl/g;
302 $ctx->setreply('550', '5.7.1', $msg);
303 }
304  
305 return SMFIS_REJECT;
306 }
307 }
308 }
309  
310 SMFIS_CONTINUE; # don't whitelist a fallthrough
311 }
312  
313 1;
314 __END__