Milter/Module/HeloRegex.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 67 CC 4 SCAR 15.5
CC
4
Cov
0%
helo_callback
12.0
new
6.0
HeloRegex
2.0
CRAP 4.7
1 # $Id: HeloRegex.pm,v 1.1 2004/04/12 14:24:08 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::HeloRegex;
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  
44 our $VERSION = '0.01';
45  
46 =pod
47  
48 =head1 NAME
49  
50 Mail::Milter::Module::HeloRegex - milter to accept/reject connections with certain HELO values
51  
52 =head1 SYNOPSIS
53  
54 use Mail::Milter::Module::HeloRegex;
55  
56 my $milter = new Mail::Milter::Module::HeloRegex('^foo\.com$');
57  
58 my $milter2 = &HeloRegex('^foo\.com$'); # convenience
59  
60 =head1 DESCRIPTION
61  
62 This milter module rejects entire SMTP connections if the connecting client
63 issues a HELO command matching user-supplied regular expressions. Note that
64 only the initial word of the HELO string is tested; any EHLO parameters are
65 not checked by the regexes.
66  
67 =head1 METHODS
68  
69 =over 4
70  
71 =cut
72  
73 our @EXPORT = qw(&HeloRegex);
74  
75 sub HeloRegex {
76 new Mail::Milter::Module::HeloRegex(@_);
77 }
78  
79 =pod
80  
81 =item new(REGEX[, ...])
82  
83 Accepts one or more regular expressions, as strings or qr// precompiled
84 regexes. They are tested in sequence, and the first match terminates
85 checking.
86  
87 =cut
88  
89 sub new ($$;@) {
90 my $this = Mail::Milter::Object::new(shift);
91  
92 $this->{_message} = 'HELO parameter "%H" not permitted at this site';
93  
94 croak 'new HeloRegex: no regexes supplied' unless scalar @_;
95 $this->{_regexes} = [ map qr/$_/i, @_ ];
96  
97 $this;
98 }
99  
100 =pod
101  
102 =item set_message(MESSAGE)
103  
104 Sets the message used when rejecting messages. This string may contain the
105 substring C<%H>, which will be replaced by the matching HELO parameter.
106  
107 This method returns a reference to the object itself, allowing this method
108 call to be chained.
109  
110 =cut
111  
112 sub set_message ($$) {
113 my $this = shift;
114  
115 $this->{_message} = shift;
116  
117 $this;
118 }
119  
120 sub helo_callback {
121 my $this = shift;
122 my $ctx = shift;
123 my $helo = shift;
124 # ignore additional parameters
125  
126 foreach my $rx (@{$this->{_regexes}}) {
127 if ($helo =~ $rx) {
128 my $msg = $this->{_message};
129  
130 if (defined($msg)) {
131 $msg =~ s/%H/$helo/g;
132 $ctx->setreply('554', '5.7.1', $msg);
133 }
134  
135 return SMFIS_REJECT;
136 }
137 }
138  
139 SMFIS_CONTINUE;
140 }
141  
142 1;
143 __END__