| 1 | # $Id: DeferToRCPT.pm,v 1.8 2004/04/23 15:54:14 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::Wrapper::DeferToRCPT; | ||
| 33 | |||
| 34 | use 5.006; | ||
| 35 | use base Exporter; | ||
| 36 | use base Mail::Milter::Wrapper; | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | |||
| 41 | use Carp; | ||
| 42 | use Mail::Milter::ContextWrapper; | ||
| 43 | use Sendmail::Milter 0.18; # get needed constants | ||
| 44 | |||
| 45 | our $VERSION = '0.03'; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | Mail::Milter::Wrapper::DeferToRCPT - milter wrapper to delay failure returns | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | use Mail::Milter::Wrapper::DeferToRCPT; | ||
| 56 | |||
| 57 | my $milter = ...; | ||
| 58 | my $wrapper = new Mail::Milter::Wrapper::DeferToRCPT($milter); | ||
| 59 | |||
| 60 | my $wrapper2 = &DeferToRCPT($milter); # convenience | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Mail::Milter::Wrapper::DeferToRCPT is a convenience milter wrapper which | ||
| 65 | defers any error return during the "connect", "helo", and/or "envfrom" | ||
| 66 | callbacks to the "envrcpt" callback. | ||
| 67 | |||
| 68 | Many broken client mailers exist in the real world and will do such things | ||
| 69 | as instantaneously reconnect when receiving an error at the MAIL FROM: | ||
| 70 | stage. This wrapper ensures that errors are never propagated back to the | ||
| 71 | MTA until at least the RCPT TO: phase. | ||
| 72 | |||
| 73 | Errors in "connect" and "helo" will apply to the entire SMTP transaction. | ||
| 74 | Errors in "envfrom" will only apply to that particular message. | ||
| 75 | |||
| 76 | This wrapper can also be used to enhance logging. Though the contained | ||
| 77 | milter may wish to reject a mail in progress, it may be useful for logging | ||
| 78 | purposes to capture the HELO string, sender, and recipient addresses of | ||
| 79 | each attempted mail. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | our @EXPORT = qw(&DeferToRCPT); | ||
| 84 | |||
| 85 | sub DeferToRCPT { | ||
| 86 | new Mail::Milter::Wrapper::DeferToRCPT(@_); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub new ($$) { | ||
| 90 | Mail::Milter::Wrapper::new(shift, shift, | ||
| 91 | \&wrapper, qw{connect envfrom envrcpt close}); | ||
| 92 | } | ||
| 93 | |||
| 94 | # internal methods | ||
| 95 | sub wrapper { | ||
| 96 | my $this = shift; | ||
| 97 | my $cbname = shift; | ||
| 98 | my $callback_sub = shift; | ||
| 99 | my $oldctx = shift; | ||
| 100 | |||
| 101 | my $newctx = $oldctx->getpriv(); | ||
| 102 | |||
| 103 | unless (defined($newctx)) { | ||
| 104 | $newctx = new Mail::Milter::ContextWrapper($oldctx, { | ||
| 105 | setreply => sub { shift->set_key(reply => [ @_ ]); }, | ||
| 106 | }); | ||
| 107 | $oldctx->setpriv($newctx); | ||
| 108 | } | ||
| 109 | |||
| 110 | # If rejection is pending, "stage" has the value: | ||
| 111 | # 0, if rejected in "connect" or "helo" for whole connection. | ||
| 112 | # 1, if rejected in "envfrom". | ||
| 113 | # 2, if rejected in "envrcpt" or later. | ||
| 114 | |||
| 115 | my $rc = $newctx->get_key('rc'); | ||
| 116 | |||
| 117 | if ($cbname eq 'connect' || $cbname eq 'close') { | ||
| 118 | # | ||
| 119 | # Always start fresh. | ||
| 120 | # | ||
| 121 | $newctx->set_key(stage => 0); | ||
| 122 | $newctx->set_key(reply => undef); | ||
| 123 | $newctx->set_key(rc => ($rc = SMFIS_CONTINUE)); | ||
| 124 | } elsif ($cbname eq 'envfrom') { | ||
| 125 | # | ||
| 126 | # If we've reached this point naturally or the last | ||
| 127 | # reject was in "envfrom", then reset state. | ||
| 128 | # | ||
| 129 | if ($rc == SMFIS_CONTINUE || $newctx->get_key('stage') >= 1) { | ||
| 130 | $newctx->set_key(stage => 1); | ||
| 131 | $newctx->set_key(reply => undef); | ||
| 132 | $newctx->set_key(rc => ($rc = SMFIS_CONTINUE)); | ||
| 133 | } | ||
| 134 | } elsif ($cbname eq 'envrcpt') { | ||
| 135 | # | ||
| 136 | # If we've reached this point naturally, then reset state. | ||
| 137 | # | ||
| 138 | if ($rc == SMFIS_CONTINUE) { | ||
| 139 | $newctx->set_key(stage => 2); | ||
| 140 | $newctx->set_key(reply => undef); | ||
| 141 | $newctx->set_key(rc => $rc); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | # Only call the callback if there is not a pending error. | ||
| 146 | $rc = &$callback_sub($newctx, @_) if ($rc == SMFIS_CONTINUE); | ||
| 147 | |||
| 148 | if (($cbname eq 'connect' || $cbname eq 'helo' || $cbname eq 'envfrom') && | ||
| 149 | ($rc == SMFIS_TEMPFAIL || $rc == SMFIS_REJECT)) { | ||
| 150 | # | ||
| 151 | # Convert error to pending, and return CONTINUE. | ||
| 152 | # | ||
| 153 | $newctx->set_key(rc => $rc); | ||
| 154 | $rc = SMFIS_CONTINUE; | ||
| 155 | } | ||
| 156 | |||
| 157 | # Propagate the replycode only if we'll be returning an error. | ||
| 158 | if ($rc == SMFIS_TEMPFAIL || $rc == SMFIS_REJECT) { | ||
| 159 | my $reply = $newctx->get_key('reply'); | ||
| 160 | |||
| 161 | if ($reply) { | ||
| 162 | $reply->[0] = 550 if ($reply->[0] == 554); | ||
| 163 | $oldctx->setreply(@$reply); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | if ($newctx->get_key('stage') >= 2) { | ||
| 168 | # | ||
| 169 | # If we weren't rejected in stages 0 or 1, then reset state. | ||
| 170 | # | ||
| 171 | $newctx->set_key(rc => SMFIS_CONTINUE); | ||
| 172 | $newctx->set_key(reply => undef); | ||
| 173 | } | ||
| 174 | |||
| 175 | $oldctx->setpriv(undef) if ($cbname eq 'close'); | ||
| 176 | |||
| 177 | $rc; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | __END__ |