| 1 | # $Id: MailDomainDotMX.pm,v 1.2 2004/12/15 22:07:59 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::MailDomainDotMX; | ||
| 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; | ||
| 43 | use Sendmail::Milter 0.18; # get needed constants | ||
| 44 | use Socket; | ||
| 45 | use UNIVERSAL; | ||
| 46 | |||
| 47 | our $VERSION = '0.01'; | ||
| 48 | |||
| 49 | =pod | ||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | Mail::Milter::Module::MailDomainDotMX - milter to reject mail whose sender domain publishes a null MX record | ||
| 54 | |||
| 55 | =head1 SYNOPSIS | ||
| 56 | |||
| 57 | use Mail::Milter::Module::MailDomainDotMX; | ||
| 58 | |||
| 59 | my $milter = new Mail::Milter::Module::MailDomainDotMX; | ||
| 60 | |||
| 61 | my $milter2 = &MailDomainDotMX; # convenience | ||
| 62 | |||
| 63 | $milter2->set_message('Mail from %M domain invalid (has dot-MX record)'); | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | This milter module rejects any mail from a sender's domain (in the MAIL FROM | ||
| 68 | part of the SMTP transaction, not in the From: header) if that domain | ||
| 69 | publishes a "null", or "dot" MX record. Such a record looks like the | ||
| 70 | following in DNS: | ||
| 71 | |||
| 72 | example.com. IN MX 0 . | ||
| 73 | |||
| 74 | This lookup requires the Net::DNS module to be installed in order to fetch | ||
| 75 | the MX record. | ||
| 76 | |||
| 77 | An extra check as to whether the MX is valid is not (yet) done here. It is | ||
| 78 | currently assumed that the MTA does rudimentary checking for the presence of | ||
| 79 | a valid MX or A record on the sending domain. | ||
| 80 | |||
| 81 | =head1 METHODS | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | our @EXPORT = qw(&MailDomainDotMX); | ||
| 88 | |||
| 89 | sub MailDomainDotMX { | ||
| 90 | new Mail::Milter::Module::MailDomainDotMX(@_); | ||
| 91 | } | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =item new() | ||
| 96 | |||
| 97 | Creates a MailDomainDotMX object. There are no arguments to configure this | ||
| 98 | module, as it is a fixed check. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub new ($) { | ||
| 103 | my $this = Mail::Milter::Object::new(shift); | ||
| 104 | |||
| 105 | $this->{_ignoretempfail} = 0; | ||
| 106 | $this->{_message} = 'Access denied to sender address %M (domain publishes a deliberately invalid MX record)'; | ||
| 107 | |||
| 108 | $this; | ||
| 109 | } | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | =item ignore_tempfail(FLAG) | ||
| 114 | |||
| 115 | If FLAG is 0 (the default), a DNS lookup which fails the underlying DNS | ||
| 116 | query will cause the milter to return a temporary failure result | ||
| 117 | (SMFIS_TEMPFAIL). | ||
| 118 | |||
| 119 | If FLAG is 1, a temporary DNS failure will be treated as if the lookup | ||
| 120 | resulted in an empty record set (SMFIS_CONTINUE). | ||
| 121 | |||
| 122 | This method returns a reference to the object itself, allowing this method | ||
| 123 | call to be chained. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub ignore_tempfail ($$) { | ||
| 128 | my $this = shift; | ||
| 129 | my $flag = shift; | ||
| 130 | |||
| 131 | croak 'ignore_tempfail: flag argument is undef' unless defined($flag); | ||
| 132 | $this->{_ignoretempfail} = $flag; | ||
| 133 | |||
| 134 | $this; | ||
| 135 | } | ||
| 136 | |||
| 137 | =pod | ||
| 138 | |||
| 139 | =item set_message(MESSAGE) | ||
| 140 | |||
| 141 | Sets the message used when rejecting messages. This string may contain | ||
| 142 | the substring C<%M>, which will be replaced by the matching e-mail | ||
| 143 | address. | ||
| 144 | |||
| 145 | This method returns a reference to the object itself, allowing this method | ||
| 146 | call to be chained. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub set_message ($$) { | ||
| 151 | my $this = shift; | ||
| 152 | |||
| 153 | $this->{_message} = shift; | ||
| 154 | |||
| 155 | $this; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub envfrom_callback { | ||
| 159 | my $this = shift; | ||
| 160 | my $ctx = shift; | ||
| 161 | my $from = lc(shift); | ||
| 162 | |||
| 163 | $from =~ s/^<//; | ||
| 164 | $from =~ s/>$//; | ||
| 165 | |||
| 166 | return SMFIS_CONTINUE if ($from eq ''); # null <> sender | ||
| 167 | |||
| 168 | my $fromdomain = $from; | ||
| 169 | |||
| 170 | $fromdomain =~ s/^[^\@]+\@//; | ||
| 171 | |||
| 172 | my $res = new Net::DNS::Resolver; | ||
| 173 | my $query = $res->query($fromdomain, 'MX'); | ||
| 174 | |||
| 175 | if (!defined($query)) { | ||
| 176 | return SMFIS_CONTINUE if $this->{_ignoretempfail}; | ||
| 177 | $ctx->setreply('451', '4.7.1', "Temporary failure in DNS lookup for $fromdomain"); | ||
| 178 | return SMFIS_TEMPFAIL; | ||
| 179 | } | ||
| 180 | |||
| 181 | foreach my $rr (grep { $_->type eq 'MX' } $query->answer) { | ||
| 182 | if ($rr->exchange eq '') { | ||
| 183 | my $msg = $this->{_message}; | ||
| 184 | |||
| 185 | if (defined($msg)) { | ||
| 186 | $msg =~ s/%M/$from/g; | ||
| 187 | $ctx->setreply('554', '5.7.1', $msg); | ||
| 188 | } | ||
| 189 | |||
| 190 | return SMFIS_REJECT; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | SMFIS_CONTINUE; # don't whitelist a fallthrough | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | __END__ |