| 1 | # $Id: ConnectRegex.pm,v 1.10 2004/02/26 19:24:52 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::ConnectRegex; | ||
| 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.03'; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | Mail::Milter::Module::ConnectRegex - milter to accept/reject connecting hosts matching regex(es) | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | use Mail::Milter::Module::ConnectRegex; | ||
| 56 | |||
| 57 | my $milter = new Mail::Milter::Module::ConnectRegex('^foo$'); | ||
| 58 | |||
| 59 | my $milter2 = &ConnectRegex(qw{^foo$ ^bar$}); # convenience | ||
| 60 | |||
| 61 | $milter2->set_message('Connections from %H disallowed'); | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | This milter module rejects any connecting host whose hostname or IP address | ||
| 66 | matches user-supplied regular expressions. It can also function as a | ||
| 67 | whitelisting Chain element; see C<accept_match()>. | ||
| 68 | |||
| 69 | =head1 METHODS | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | our @EXPORT = qw(&ConnectRegex); | ||
| 76 | |||
| 77 | sub ConnectRegex { | ||
| 78 | new Mail::Milter::Module::ConnectRegex(@_); | ||
| 79 | } | ||
| 80 | |||
| 81 | =pod | ||
| 82 | |||
| 83 | =item new(REGEX[, ...]) | ||
| 84 | |||
| 85 | Accepts one or more regular expressions, as strings or qr// precompiled | ||
| 86 | regexes. They are tested in sequence, and the first match terminates | ||
| 87 | checking. Note that all IP address literals will be enclosed in [square | ||
| 88 | brackets]; so to test an IP address rather than a hostname, ensure those | ||
| 89 | brackets exist: | ||
| 90 | |||
| 91 | ^\[ADDRESS\]$ | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub new ($$;@) { | ||
| 96 | my $this = Mail::Milter::Object::new(shift); | ||
| 97 | |||
| 98 | $this->{_accept} = 0; | ||
| 99 | $this->{_message} = 'Not accepting connections from %H'; | ||
| 100 | |||
| 101 | croak 'new ConnectRegex: no regexes supplied' unless scalar @_; | ||
| 102 | $this->{_regexes} = [ map qr/$_/i, @_ ]; | ||
| 103 | |||
| 104 | $this; | ||
| 105 | } | ||
| 106 | |||
| 107 | =pod | ||
| 108 | |||
| 109 | =item accept_match(FLAG) | ||
| 110 | |||
| 111 | If FLAG is 0 (the default), a matching regex will cause the connection to | ||
| 112 | be rejected. | ||
| 113 | |||
| 114 | If FLAG is 1, a matching regex will cause this module to return SMFIS_ACCEPT | ||
| 115 | instead. This allows a C<ConnectRegex> to be used inside a | ||
| 116 | C<Mail::Milter::Chain> container (in C<accept_break(1)> mode), to function | ||
| 117 | as a whitelist rather than a blacklist. | ||
| 118 | |||
| 119 | This method returns a reference to the object itself, allowing this method | ||
| 120 | call to be chained. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub accept_match ($$) { | ||
| 125 | my $this = shift; | ||
| 126 | my $flag = shift; | ||
| 127 | |||
| 128 | croak 'accept_match: flag argument is undef' unless defined($flag); | ||
| 129 | $this->{_accept} = $flag; | ||
| 130 | |||
| 131 | $this; | ||
| 132 | } | ||
| 133 | |||
| 134 | =pod | ||
| 135 | |||
| 136 | =item set_message(MESSAGE) | ||
| 137 | |||
| 138 | Sets the message used when rejecting connections. This string may contain | ||
| 139 | the substring C<%H>, which will be replaced by the matching hostname or IP | ||
| 140 | address. | ||
| 141 | |||
| 142 | This method returns a reference to the object itself, allowing this method | ||
| 143 | call to be chained. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub set_message ($$) { | ||
| 148 | my $this = shift; | ||
| 149 | |||
| 150 | $this->{_message} = shift; | ||
| 151 | |||
| 152 | $this; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub connect_callback { | ||
| 156 | my $this = shift; | ||
| 157 | my $ctx = shift; | ||
| 158 | my $hostname = shift; | ||
| 159 | my $pack = shift; | ||
| 160 | my $addr; | ||
| 161 | |||
| 162 | if ($hostname =~ /^\[/) { | ||
| 163 | $addr = $hostname; | ||
| 164 | undef $hostname; | ||
| 165 | } | ||
| 166 | |||
| 167 | # First try IPv4 unpacking. | ||
| 168 | $addr = eval { | ||
| 169 | my @unpack = unpack_sockaddr_in($pack); | ||
| 170 | '['.inet_ntoa($unpack[1]).']'; | ||
| 171 | } unless defined($addr); | ||
| 172 | |||
| 173 | $addr = eval { | ||
| 174 | require Socket6; | ||
| 175 | my @unpack = Socket6::unpack_sockaddr_in6($pack); | ||
| 176 | '['.Socket6::inet_ntop(&Socket6::AF_INET6, $unpack[1]).']'; | ||
| 177 | } unless defined($addr); | ||
| 178 | |||
| 179 | foreach my $rx (@{$this->{_regexes}}) { | ||
| 180 | my $match; | ||
| 181 | |||
| 182 | if (defined($hostname) && $hostname =~ $rx) { | ||
| 183 | $match = $hostname; | ||
| 184 | } elsif (defined($addr) && $addr =~ $rx) { | ||
| 185 | $match = $addr; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (defined($match)) { | ||
| 189 | my $msg = $this->{_message}; | ||
| 190 | |||
| 191 | return SMFIS_ACCEPT if $this->{_accept}; | ||
| 192 | |||
| 193 | if (defined($msg)) { | ||
| 194 | $msg =~ s/%H/$match/g; | ||
| 195 | $ctx->setreply('554', '5.7.1', $msg); | ||
| 196 | } | ||
| 197 | |||
| 198 | return SMFIS_REJECT; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | SMFIS_CONTINUE; # don't whitelist a fallthrough | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | __END__ |