| 1 | # $Id: HeaderValidateMIME.pm,v 1.1 2006/03/21 20:27:25 tvierling Exp $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2006 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::HeaderValidateMIME; | ||
| 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 Sendmail::Milter 0.18; # get needed constants | ||
| 42 | |||
| 43 | our $VERSION = '0.01'; | ||
| 44 | |||
| 45 | =pod | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | Mail::Milter::Module::HeaderValidateMIME - enforce MIME header conformance | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | use Mail::Milter::Module::HeaderValidateMIME; | ||
| 54 | |||
| 55 | my $milter = new Mail::Milter::Module::HeaderValidateMIME(); | ||
| 56 | |||
| 57 | my $milter2 = &HeaderValidateMIME; # convenience | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | This milter module rejects any message at the DATA stage that has one of | ||
| 62 | the Content-Type: or MIME-Version: headers, but not the other. In the | ||
| 63 | future, this module may enforce stricter MIME checks. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | our @EXPORT = qw(&HeaderValidateMIME); | ||
| 68 | |||
| 69 | sub HeaderValidateMIME { | ||
| 70 | new Mail::Milter::Module::HeaderValidateMIME(@_); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub envfrom_callback { | ||
| 74 | shift; # $this | ||
| 75 | my $ctx = shift; | ||
| 76 | |||
| 77 | $ctx->setpriv({}); | ||
| 78 | |||
| 79 | SMFIS_CONTINUE; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub header_callback { | ||
| 83 | shift; # $this | ||
| 84 | my $ctx = shift; | ||
| 85 | my $hname = lc(shift); | ||
| 86 | my $priv = $ctx->getpriv(); | ||
| 87 | |||
| 88 | if ($hname eq 'content-type' || $hname eq 'mime-version') { | ||
| 89 | $priv->{$hname} = 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | SMFIS_CONTINUE; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub eoh_callback { | ||
| 96 | shift; # $this | ||
| 97 | my $ctx = shift; | ||
| 98 | my $priv = $ctx->getpriv(); | ||
| 99 | |||
| 100 | if (defined($priv->{'content-type'}) xor defined($priv->{'mime-version'})) { | ||
| 101 | $ctx->setreply("554", "5.6.0", "Message is corrupt -- RFC2047: MIME-Version and Content-Type must be used together, or not at all"); | ||
| 102 | return SMFIS_REJECT; | ||
| 103 | } | ||
| 104 | |||
| 105 | SMFIS_ACCEPT; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | __END__ |