| 1 | # $Id: DecodeSRS.pm,v 1.2 2004/12/15 17:47:07 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::DecodeSRS; | ||
| 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.02'; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | Mail::Milter::Wrapper::DecodeSRS - milter wrapper to decode SRS-encoded return path | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | use Mail::Milter::Wrapper::DecodeSRS; | ||
| 56 | |||
| 57 | my $milter = ...; | ||
| 58 | my $wrapper = new Mail::Milter::Wrapper::DecodeSRS($milter); | ||
| 59 | |||
| 60 | my $wrapper2 = &DecodeSRS($milter); # convenience | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Mail::Milter::Wrapper::DecodeSRS is a convenience milter wrapper which | ||
| 65 | decodes MAIL FROM: return paths which have been encoded by the Sender | ||
| 66 | Rewrite Scheme, SRS. (More information: http://www.libsrs2.org/) | ||
| 67 | This wrapper internally understands both the SRS0 and SRS1 encoding | ||
| 68 | schemes documented by the Mail::SRS author. | ||
| 69 | |||
| 70 | The decoded address is made available to the contained milter via the | ||
| 71 | C<envfrom> callback, in the same way that a raw address would. | ||
| 72 | |||
| 73 | NOTE: If the address is not SRS encoded, the contained milter is NOT | ||
| 74 | called for the duration of the message; instead, SMFIS_ACCEPT is returned. | ||
| 75 | This is because the milter writer is expected to use this wrapper in a | ||
| 76 | chain that also includes the contained milter without wrapping, in order | ||
| 77 | to prevent a malicious sender from using SRS to bypass access checks. | ||
| 78 | |||
| 79 | For instance, the following is a proper usage of this wrapper in a chain: | ||
| 80 | |||
| 81 | my $envfrommilter = ...; | ||
| 82 | |||
| 83 | my $combinedmilter = new Mail::Milter::Chain( | ||
| 84 | new Mail::Milter::Wrapper::UnwrapSRS($envfrommilter), | ||
| 85 | $envfrommilter | ||
| 86 | ); | ||
| 87 | |||
| 88 | This behavior can also be used if, e.g., the MTA already does one form of | ||
| 89 | MAIL FROM: check, and the contained milter repeats that same database | ||
| 90 | check against SRS rewritten addresses. (A good example would be a milter | ||
| 91 | emulating Sendmail's access_db map.) | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | our @EXPORT = qw(&DecodeSRS); | ||
| 96 | |||
| 97 | sub DecodeSRS { | ||
| 98 | new Mail::Milter::Wrapper::DecodeSRS(@_); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub new ($$) { | ||
| 102 | my $this = Mail::Milter::Wrapper::new(shift, shift, | ||
| 103 | \&wrapper, qw{connect close}); | ||
| 104 | |||
| 105 | $this; | ||
| 106 | } | ||
| 107 | |||
| 108 | # internal methods | ||
| 109 | sub wrapper { | ||
| 110 | my $this = shift; | ||
| 111 | my $cbname = shift; | ||
| 112 | my $callback_sub = shift; | ||
| 113 | |||
| 114 | if ($cbname eq 'envfrom') { | ||
| 115 | my $addr = $_[1]; | ||
| 116 | |||
| 117 | # Mail::SRS::Guarded SRS1: strip to Mail::SRS::Guarded SRS0 | ||
| 118 | $addr =~ s/^<SRS1[=\+-](?:[^=]+=)?[^=]+\.[^=]+=[=\+-]/SRS0=/; | ||
| 119 | |||
| 120 | if ($addr =~ /^<SRS0[=\+-][^=]+=(?:[^=]+=)?([^=]+\.[^=]+)=(.+)\@/) { | ||
| 121 | # Mail::SRS::Guarded SRS0 | ||
| 122 | $_[1] = "<$2\@$1>"; | ||
| 123 | } else { | ||
| 124 | return SMFIS_ACCEPT; # skip this message | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | &$callback_sub(@_); | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | __END__ |