Milter/Module/ConnectMatchesHostname.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 101 CC 6 SCAR 27.8
CC
6
Cov
0%
connect_callback
42.0
ConnectMatchesHostname
2.0
new
2.0
CRAP 16.1
1 # $Id: ConnectMatchesHostname.pm,v 1.4 2004/04/12 15:27:00 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::ConnectMatchesHostname;
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  
45 our $VERSION = '0.01';
46  
47 =pod
48  
49 =head1 NAME
50  
51 Mail::Milter::Module::ConnectMatchesHostname - milter to accept/reject connecting hosts matching regex(es)
52  
53 =head1 SYNOPSIS
54  
55 use Mail::Milter::Module::ConnectMatchesHostname;
56  
57 my $milter = new Mail::Milter::Module::ConnectMatchesHostname;
58  
59 my $milter2 = &ConnectMatchesHostname; # convenience
60  
61 $milter2->set_message('Connecting hostname %H looks like a dynamic address');
62  
63 =head1 DESCRIPTION
64  
65 This milter module rejects any connecting host whose hostname contains one
66 of a group of built-in patterns that match the IP address of the connecting
67 host. This is normally used to detect dynamic pool addresses.
68  
69 Currently the following patterns embedded in the hostname are considered
70 matching, where 10.11.12.13 is the IPv4 address of the connecting host.
71 In the following cases, the string must be preceded by a non-digit
72 character or otherwise must be at the start of the hostname.
73  
74 010.011.012.013. (optionally without internal dots, or with - in place of .)
75 013.012.011.010. (optionally with - in place of .)
76 10.11.12.13. (optionally without internal dots, or with - in place of .)
77 13.12.11.10. (optionally with - in place of .)
78 0A0B0C0D (hexadecimal, ignoring case)
79  
80 More specific patterns are anticipated to be added in the future. Because
81 of this, if you use ConnectMatchesHostname, pay attention to this perldoc
82 manual page when updating to a newer version of Mail::Milter.
83  
84 One final note. ISPs can and do use "dynamic-looking" reverse DNS entries
85 for what they consider to be legitimate server addresses. This is not
86 ideal, and may require embedding this module in a Chain set to
87 "accept_break" with regular expressions; for example:
88  
89 my $milter = new Mail::Milter::Chain(
90 &ConnectRegex(
91 '\.fooisp\.com$',
92 )->accept_match(1);
93 &ConnectMatchesHostname,
94 )->accept_break(1);
95  
96 =head1 METHODS
97  
98 =over 4
99  
100 =cut
101  
102 our @EXPORT = qw(&ConnectMatchesHostname);
103  
104 sub ConnectMatchesHostname {
105 new Mail::Milter::Module::ConnectMatchesHostname(@_);
106 }
107  
108 =pod
109  
110 =item new()
111  
112 Creates a ConnectMatchesHostname object.
113  
114 =cut
115  
116 sub new ($) {
117 my $this = Mail::Milter::Object::new(shift);
118  
119 $this->{_message} = 'Connecting hostname %H looks like a dynamic pool address (contains the connecting address %A)';
120  
121 $this;
122 }
123  
124 =pod
125  
126 =item set_message(MESSAGE)
127  
128 Sets the message used when rejecting connections. This string may contain
129 the substring C<%H>, which will be replaced by the matching hostname, and/or
130 the substring C<%A>, which will be replaced by the matching IP address.
131  
132 This method returns a reference to the object itself, allowing this method
133 call to be chained.
134  
135 =cut
136  
137 sub set_message ($$) {
138 my $this = shift;
139  
140 $this->{_message} = shift;
141  
142 $this;
143 }
144  
145 sub connect_callback {
146 my $this = shift;
147 my $ctx = shift;
148 my $hostname = shift;
149 my $pack = shift;
150 my $addr;
151  
152 return SMFIS_ACCEPT if ($hostname =~ /^\[/);
153  
154 # We want IPv4 only (for now).
155 $addr = eval {
156 my @unpack = unpack_sockaddr_in($pack);
157 inet_ntoa($unpack[1]);
158 } unless defined($addr);
159  
160 return SMFIS_ACCEPT unless defined($addr);
161  
162 my ($i1, $i2, $i3, $i4) = split(/\./, $addr);
163 my $f1 = sprintf('%03d', $i1);
164 my $f2 = sprintf('%03d', $i2);
165 my $f3 = sprintf('%03d', $i3);
166 my $f4 = sprintf('%03d', $i4);
167 my $hex = sprintf('%08x', unpack('N', pack('C4', $i1, $i2, $i3, $i4)));
168  
169 if (
170 $hostname =~ /(?:\A|\D)$i1[\.-]$i2[\.-]$i3[\.-]$i4\D/ ||
171 $hostname =~ /(?:\A|\D)$f1[\.-]?$f2[\.-]?$f3[\.-]?$f4\D/ ||
172 $hostname =~ /(?:\A|\D)$i4[\.-]$i3[\.-]$i2[\.-]$i1\D/ ||
173 $hostname =~ /(?:\A|\D)$f4[\.-]?$f3[\.-]?$f2[\.-]?$f1\D/ ||
174 $hostname =~ /$hex/i
175 ) {
176 my $msg = $this->{_message};
177  
178 if (defined($msg)) {
179 $msg =~ s/%H/$hostname/g;
180 $msg =~ s/%A/$addr/g;
181 $ctx->setreply('554', '5.7.1', $msg);
182 }
183  
184 return SMFIS_REJECT;
185 }
186  
187 SMFIS_ACCEPT;
188 }
189  
190 1;
191 __END__