| 1 | # | ||
| 2 | # Courier::Filter::Module::FakeDate class | ||
| 3 | # | ||
| 4 | # (C) 2006-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: FakeDate.pm 211 2008-03-23 01:25:20Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::FakeDate - Fake "Date:" message header filter module | ||
| 12 | for the Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::FakeDate; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use Error ':try'; | ||
| 24 | |||
| 25 | use DateTime::Duration; | ||
| 26 | use DateTime::Format::Mail; | ||
| 27 | |||
| 28 | use constant TRUE => (0 == 0); | ||
| 29 | use constant FALSE => not TRUE; | ||
| 30 | |||
| 31 | use constant forward_tolerance_default => { hours => 2 }; | ||
| 32 | use constant backward_tolerance_default => { days => 5 }; | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use Courier::Filter::Module::FakeDate; | ||
| 37 | |||
| 38 | my $module = Courier::Filter::Module::Header->new( | ||
| 39 | forward_tolerance => { | ||
| 40 | # years, months, weeks, days, hours, minutes, seconds | ||
| 41 | hours => 2 | ||
| 42 | }, | ||
| 43 | backward_tolerance => { | ||
| 44 | # years, months, weeks, days, hours, minutes, seconds | ||
| 45 | days => 5 | ||
| 46 | }, | ||
| 47 | |||
| 48 | ignore_unparseable => 0, | ||
| 49 | |||
| 50 | logger => $logger, | ||
| 51 | inverse => 0, | ||
| 52 | trusting => 0, | ||
| 53 | testing => 0, | ||
| 54 | debugging => 0 | ||
| 55 | ); | ||
| 56 | |||
| 57 | my $filter = Courier::Filter->new( | ||
| 58 | ... | ||
| 59 | modules => [ $module ], | ||
| 60 | ... | ||
| 61 | ); | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | This class is a filter module class for use with Courier::Filter. It matches a | ||
| 66 | message if it has a C<Date> header field that lies too far in the future or the | ||
| 67 | past, relative to the local system's time. If the message has a C<Resent-Date> | ||
| 68 | header field (see RFC 2822, 3.6.6), that one is examined instead, because the | ||
| 69 | message could simply be an old one that has recently been re-sent, which is | ||
| 70 | perfectly legitimate behavior. | ||
| 71 | |||
| 72 | In the case of a match, the response tells the sender that their C<Date> header | ||
| 73 | is implausible and that they should check their clock. | ||
| 74 | |||
| 75 | I<Note>: Times in different time zones are compared correctly. | ||
| 76 | |||
| 77 | I<Note>: When using this filter module, it is essential that the local system's | ||
| 78 | own clock is set correctly, or there will be an increased risk of legitimate | ||
| 79 | messages getting rejected. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | # Implementation: | ||
| 84 | ############################################################################### | ||
| 85 | |||
| 86 | =head2 Constructor | ||
| 87 | |||
| 88 | The following constructor is provided: | ||
| 89 | |||
| 90 | =over | ||
| 91 | |||
| 92 | =item B<new(%options)>: returns I<Courier::Filter::Module::FakeDate> | ||
| 93 | |||
| 94 | Creates a new B<FakeDate> filter module. | ||
| 95 | |||
| 96 | %options is a list of key/value pairs representing any of the following | ||
| 97 | options: | ||
| 98 | |||
| 99 | =over | ||
| 100 | |||
| 101 | =item B<forward_tolerance> | ||
| 102 | |||
| 103 | =item B<backward_tolerance> | ||
| 104 | |||
| 105 | The maximum durations by which a message's C<Date> or C<Resent-Date> header may | ||
| 106 | diverge into the future and the past, respectively, from the local system's | ||
| 107 | time. Each duration must be specified as a hash-ref containing one or more | ||
| 108 | time units and their respective quantity/ies, just as specified by | ||
| 109 | L<DateTime::Duration>. C<forward_tolerance> defaults to I<2 hours>. | ||
| 110 | C<backward_tolerance> defaults to I<5 days> to account for transmission | ||
| 111 | delays. | ||
| 112 | |||
| 113 | For example: | ||
| 114 | |||
| 115 | forward_tolerance => { hours => 4 }, | ||
| 116 | backward_tolerance => { days => 1, hours => 12 } | ||
| 117 | |||
| 118 | =item B<ignore_unparseable> | ||
| 119 | |||
| 120 | A boolean value controlling whether messages whose C<Date> or C<Resent-Date> | ||
| 121 | header does not loosely conform to RFCs 822 or 2822 should be ignored (B<true>) | ||
| 122 | or matched (B<false>). Defaults to B<false>. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | All options of the B<Courier::Filter::Module> constructor are also supported. | ||
| 127 | Please see L<Courier::Filter::Module/new> for their descriptions. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub new { | ||
| 132 | my ($class, %options) = @_; | ||
| 133 | |||
| 134 | my $forward_tolerance = DateTime::Duration->new( | ||
| 135 | %{ $options{forward_tolerance} || $class->forward_tolerance_default } | ||
| 136 | ); | ||
| 137 | my $backward_tolerance = DateTime::Duration->new( | ||
| 138 | %{ $options{backward_tolerance} || $class->backward_tolerance_default } | ||
| 139 | ); | ||
| 140 | |||
| 141 | my $date_parser = DateTime::Format::Mail->new( loose => TRUE ); | ||
| 142 | |||
| 143 | my $self = $class->SUPER::new( | ||
| 144 | %options, | ||
| 145 | forward_tolerance => $forward_tolerance, | ||
| 146 | backward_tolerance => $backward_tolerance, | ||
| 147 | date_parser => $date_parser | ||
| 148 | ); | ||
| 149 | |||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | =back | ||
| 154 | |||
| 155 | =head2 Instance methods | ||
| 156 | |||
| 157 | See L<Courier::Filter::Module/"Instance methods"> for a description of the | ||
| 158 | provided instance methods. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub match { | ||
| 163 | my ($self, $message) = @_; | ||
| 164 | |||
| 165 | my $date_header_field; | ||
| 166 | my $date_text; | ||
| 167 | foreach ('Resent-Date', 'Date') { | ||
| 168 | $date_text = $message->header($_); | ||
| 169 | $date_header_field = $_, last | ||
| 170 | if defined($date_text); | ||
| 171 | } | ||
| 172 | |||
| 173 | return undef | ||
| 174 | if not defined($date_text); | ||
| 175 | # Do not match if there is neither a "Date" nor a "Resent-Date" header field. | ||
| 176 | |||
| 177 | my $date = eval { $self->{date_parser}->parse_datetime($date_text) }; | ||
| 178 | |||
| 179 | if (not defined($date)) { | ||
| 180 | $self->warn(sprintf('FakeDate: Unparseable "%s" header detected: "%s". Fix your software!', $date_header_field, $date_text)); | ||
| 181 | return | ||
| 182 | $self->{ignore_unparseable} ? | ||
| 183 | undef | ||
| 184 | : sprintf( | ||
| 185 | 'FakeDate: Unparseable "%s" header detected: "%s". Fix your software!', | ||
| 186 | $date_header_field, $date_text | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | $date->set_time_zone('UTC'); | ||
| 191 | |||
| 192 | my $now = DateTime->now; | ||
| 193 | my $max_date = $now + $self->{forward_tolerance}; | ||
| 194 | my $min_date = $now - $self->{backward_tolerance}; | ||
| 195 | |||
| 196 | return sprintf('FakeDate: Implausible "%s" header detected. Check your clock!', $date_header_field) | ||
| 197 | if $date < $min_date or $date > $max_date; | ||
| 198 | |||
| 199 | return undef; | ||
| 200 | } | ||
| 201 | |||
| 202 | =head1 SEE ALSO | ||
| 203 | |||
| 204 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>. | ||
| 205 | |||
| 206 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 207 | L<Courier::Filter::Overview>. | ||
| 208 | |||
| 209 | =head1 AUTHOR | ||
| 210 | |||
| 211 | Julian Mehnle <julian@mehnle.net> | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | TRUE; |