Milter/Module/AccessDB.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 367 CC 50 SCAR 39.9
CC
50
Cov
0%
envfrom_callback
132.0
TranslateValue
90.0
LookUpDomain
72.0
CRAP 54.2
1 # $Id: AccessDB.pm,v 1.3 2004/12/29 04:39:35 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::AccessDB;
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 Net::DNS; # XXX should be optional (needed for MX: tagged check)
43 use Sendmail::Milter 0.18; # get needed constants
44 use Socket;
45 use UNIVERSAL;
46  
47 our $VERSION = '0.01';
48 our $DEBUGLEVEL = 0;
49  
50 =pod
51  
52 =head1 NAME
53  
54 Mail::Milter::Module::AccessDB - emulator for Sendmail "access_db" in a milter
55  
56 =head1 SYNOPSIS
57  
58 use Mail::Milter::Module::AccessDB;
59  
60 my $milter = new Mail::Milter::Module::AccessDB(\%hashref);
61  
62 my $milter2 = &AccessDB(\%hashref); # convenience
63  
64 =head1 DESCRIPTION
65  
66 Sendmail's "access_db" is a powerful access restriction database tool, but
67 it is limited only to data explicitly available through the SMTP session.
68 This milter module allows rewriting to take place (such as through
69 Mail::Milter::Wrapper::DecodeSRS) before applying the access rules.
70  
71 Not all access_db functionality is duplicated here; some is unimplemented,
72 while some is Sendmail-internal only. See DATABASE FORMAT, below, for a
73 list of supported tags and result codes in this module.
74  
75 NOTE: As of this version, this module might not be thread-safe. A future
76 version of this module will share the hashref between threads and lock it
77 properly.
78  
79 ESPECIALLY NOTE: This module is highly experimental, does not support all
80 accessdb data types yet, and is not guaranteed to work at all. Feel free
81 to try it out and to send comments to the author, but it is not yet
82 recommended to use this module in a production setup.
83  
84 =head1 DATABASE FORMAT
85  
86 [XXX: TBD]
87  
88 =head1 METHODS
89  
90 =over 4
91  
92 =cut
93  
94 our @EXPORT = qw(&AccessDB);
95  
96 sub AccessDB {
97 new Mail::Milter::Module::AccessDB(@_);
98 }
99  
100 =pod
101  
102 =item new(HASHREF)
103  
104 Create this milter using a provided hash reference. This may be a tied
105 hash, such as to an already opened Sendmail-style database.
106  
107 Currently there is no support for automatically reopening databases, hence
108 this one-shot constructor. (Sendmail does not support automatic reopening
109 either, for that matter.)
110  
111 =cut
112  
113 sub new ($$) {
114 my $this = Mail::Milter::Object::new(shift);
115 my $hashref = shift;
116  
117 $this->{_ignoretempfail} = 0;
118 $this->{_message} = '[%0] Access denied';
119 croak 'new AccessDB: no hashref supplied' unless UNIVERSAL::isa($hashref, 'HASH');
120 $this->{_db} = $hashref;
121  
122 $this;
123 }
124  
125 =pod
126  
127 =item ignore_tempfail(FLAG)
128  
129 If FLAG is 0 (the default), a DNS lookup which fails the underlying DNS
130 query will cause the milter to return a temporary failure result
131 (SMFIS_TEMPFAIL).
132  
133 If FLAG is 1, a temporary DNS failure will be treated as if the lookup
134 resulted in an empty record set (SMFIS_CONTINUE).
135  
136 This method returns a reference to the object itself, allowing this method
137 call to be chained.
138  
139 =cut
140  
141 sub ignore_tempfail ($$) {
142 my $this = shift;
143 my $flag = shift;
144  
145 croak 'ignore_tempfail: flag argument is undef' unless defined($flag);
146 $this->{_ignoretempfail} = $flag;
147  
148 $this;
149 }
150  
151 =pod
152  
153 =item set_message(MESSAGE)
154  
155 Sets the message used when rejecting messages. This string may contain
156 the substring C<%0>, which will be replaced by the matching lookup key
157 (not including type tag).
158  
159 This method returns a reference to the object itself, allowing this method
160 call to be chained.
161  
162 =cut
163  
164 sub set_message ($$) {
165 my $this = shift;
166  
167 $this->{_message} = shift;
168  
169 $this;
170 }
171  
172 sub LookUpAddress ($$$) {
173 my $this = shift;
174 my $key = shift;
175 my $tag = shift;
176 my $db = $this->{_db};
177 my $rv;
178  
179 print STDERR "$$: LookUpAddress: $tag:$key\n" if ($DEBUGLEVEL > 1);
180  
181 # lookup with tag
182 $rv = $db->{"$tag:$key"};
183  
184 # lookup without tag
185 $rv = $db->{$key} unless defined($rv);
186  
187 # found SKIP: return <default>
188 return undef if (defined($rv) && $rv eq 'SKIP');
189  
190 # no match: remove last part
191 $rv = $this->LookUpAddress($1, $tag)
192 if (!defined($rv) && ($key =~ /^(.+)[:\.]+[^:\.]+$/));
193  
194 # match: return result
195 $rv;
196 }
197  
198 sub LookUpDomain ($$$) {
199 my $this = shift;
200 my $key = shift;
201 my $tag = shift;
202 my $db = $this->{_db};
203 my $rv;
204  
205 print STDERR "$$: LookUpDomain: $tag:$key\n" if ($DEBUGLEVEL > 1);
206  
207 # lookup with tag (in front, no delimiter here)
208 $rv = $db->{"$tag:$key"};
209  
210 # lookup without tag?
211 $rv = $db->{$key} unless defined($rv);
212  
213 # LOOKUPDOTDOMAIN
214 # XXX apply this also to IP addresses?
215 # currently it works the wrong way round for [1.2.3.4]
216 if (!defined($rv) && ($key =~ /^[^\.]+(\..+)$/)) {
217 $rv = $db->{"$tag:$1"};
218 $rv = $db->{$1} unless defined($rv);
219 }
220  
221 # found SKIP: return <default>
222 return undef if (defined($rv) && $rv eq 'SKIP');
223  
224 # not found: net
225 return $this->LookUpDomain("[$1]", $tag)
226 if (!defined($rv) && ($key =~ /^\[(.+)[:\.]+[^:\.]+\]$/));
227  
228 # not found, but subdomain: try again
229 return $this->LookUpDomain($1, $tag)
230 if (!defined($rv) && ($key =~ /^[^\.]+\.(.+)$/));
231  
232 # return <result of lookup>
233 $rv;
234 }
235  
236 sub LookUpExact ($$$) {
237 my $this = shift;
238 my $key = shift;
239 my $tag = shift;
240 my $db = $this->{_db};
241 my $rv;
242  
243 print STDERR "$$: LookUpExact: $tag:$key\n" if ($DEBUGLEVEL > 1);
244  
245 $rv = $db->{"$tag:$key"};
246 $rv = $db->{$key} unless defined($rv);
247  
248 $rv;
249 }
250  
251 sub LookUpFull ($$$) {
252 my $this = shift;
253 my $key = shift;
254 my $tag = shift;
255 my $db = $this->{_db};
256 my $rv;
257  
258 print STDERR "$$: LookUpFull: $tag:$key\n" if ($DEBUGLEVEL > 1);
259  
260 $rv = $db->{"$tag:$key"};
261 $rv = $db->{$key} unless defined($rv);
262  
263 if (!defined($rv) && ($key =~ /^(.+)\+[^\+]*\@(.+)$/)) {
264 $rv = $db->{"$tag:$1+*\@$2"};
265 $rv = $db->{"$1+*\@$2"} unless defined($rv);
266 $rv = $db->{"$tag:$1\@$2"} unless defined($rv);
267 $rv = $db->{"$1\@$2"} unless defined($rv);
268 }
269  
270 $rv;
271 }
272  
273 sub LookUpUser ($$$) {
274 my $this = shift;
275 my $key = shift; # must end in @ just like in sendmail ruleset
276 my $tag = shift;
277 my $db = $this->{_db};
278 my $rv;
279  
280 print STDERR "$$: LookUpUser: $tag:$key\n" if ($DEBUGLEVEL > 1);
281  
282 $rv = $db->{"$tag:$key"};
283 $rv = $db->{$key} unless defined($rv);
284  
285 if (!defined($rv) && ($key =~ /^(.+)\+[^\+]*\@$/)) {
286 $rv = $db->{"$tag:$1+*\@"};
287 $rv = $db->{"$1+*\@"} unless defined($rv);
288 $rv = $db->{"$tag:$1\@"} unless defined($rv);
289 $rv = $db->{"$1\@"} unless defined($rv);
290 }
291  
292 $rv;
293 }
294  
295 sub TranslateValue ($$$$) {
296 my $this = shift;
297 my $ctx = shift;
298 my $key = shift;
299 my $value = shift;
300  
301 $value = "ERROR:\"554 $this->{_message}\""
302 if ($value =~ /^REJECT\s*$/);
303  
304 $value =~ s/\s+$//;
305 $value =~ s/\%0/$key/g;
306  
307 print STDERR "accessdb: $key: $value\n" if ($DEBUGLEVEL > 0);
308  
309 if ($value eq 'OK' || $value eq 'RELAY') {
310  
311 return SMFIS_CONTINUE;
312  
313 } elsif ($value =~ /^QUARANTINE:/) {
314  
315 # XXX not yet supported
316 return SMFIS_CONTINUE;
317  
318 } elsif ($value =~ /^ERROR:([45]\.\d\.\d):"(([45])\d\d) (.*)"$/) {
319  
320 $ctx->setreply($2, $1, $4);
321 return ($3 eq '5' ? SMFIS_REJECT : SMFIS_TEMPFAIL);
322  
323 } elsif ($value =~ /^(?:ERROR:)?"(([45])\d\d) (.*)"$/) {
324  
325 $ctx->setreply($1, substr($1, 0, 1).'.7.0', $3);
326 return ($2 eq '5' ? SMFIS_REJECT : SMFIS_TEMPFAIL);
327  
328 } else {
329 print STDERR "AccessDB: $key: unparseable result: $value\n";
330 }
331  
332 SMFIS_TEMPFAIL;
333 }
334  
335 sub connect_callback {
336 my $this = shift;
337 my $ctx = shift;
338 my $hostname = lc(shift);
339 my $pack = shift;
340 my $value;
341  
342 return $this->TranslateValue($ctx, $hostname, $value)
343 if defined($value = $this->LookUpDomain($hostname, 'connect'));
344  
345 # First try IPv4 unpacking.
346 my $addr = eval {
347 my @unpack = unpack_sockaddr_in($pack);
348 inet_ntoa($unpack[1]);
349 };
350  
351 $addr = eval {
352 require Socket6;
353 my @unpack = Socket6::unpack_sockaddr_in6($pack);
354 Socket6::inet_ntop(&Socket6::AF_INET6, $unpack[1]);
355 } unless defined($addr);
356  
357 if (defined($addr)) {
358 return $this->TranslateValue($ctx, $addr, $value)
359 if defined($value = $this->LookUpAddress($addr, 'connect'));
360 }
361  
362 SMFIS_CONTINUE;
363 }
364  
365 sub helo_callback {
366 # XXX need something here
367 SMFIS_CONTINUE;
368 }
369  
370 sub envfrom_callback {
371 my $this = shift;
372 my $ctx = shift;
373 my $from = lc(shift);
374 my $value;
375  
376 print STDERR "$$: envfrom: $from\n" if ($DEBUGLEVEL > 1);
377  
378 $from =~ s/^<(.*)>$/$1/; # remove angle brackets
379  
380 return $this->TranslateValue($ctx, $from, $value)
381 if defined($value = $this->LookUpFull($from, 'from'));
382  
383 $from =~ /^([^\@]+)(?:|\@([^\@]+))$/;
384 my $user = $1;
385 my $domain = $2;
386  
387 return $this->TranslateValue($ctx, $from, $value)
388 if defined($value = $this->LookUpUser("$user\@", 'from'));
389  
390 return $this->TranslateValue($ctx, $from, $value)
391 if (defined($domain) && defined($value = $this->LookUpDomain($domain, 'from')));
392  
393 # OK, no direct match. Get the MX record(s) for the domain and try those.
394 if (defined($domain)) {
395 my $res = new Net::DNS::Resolver;
396 my $query = $res->query($domain, 'MX');
397  
398 if (defined($query)) {
399 foreach my $rr (grep { $_->type eq 'MX' } $query->answer) {
400 my $mx = $rr->exchange;
401 next if ($mx eq ''); # want to reject? Use MailDomainDotMX.
402  
403 print STDERR "$$: envfrom: MX:$mx\n" if ($DEBUGLEVEL > 2);
404  
405 return $this->TranslateValue($ctx, $from, $value)
406 if (defined($domain) && defined($value = $this->LookUpDomain($mx, 'mx')));
407 }
408 } elsif (!$this->{_ignoretempfail}) {
409 $ctx->setreply('451', '4.7.1', "Temporary failure in DNS lookup for $domain");
410 return SMFIS_TEMPFAIL;
411 }
412 }
413  
414 SMFIS_CONTINUE;
415 }
416  
417 sub envrcpt_callback {
418 # XXX need something here
419 SMFIS_CONTINUE;
420 }
421  
422 1;
423 __END__