| 1 | package Mail::SRS::DB; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use vars qw(@ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use MLDBM qw(DB_File Storable); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Mail::SRS qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | @ISA = qw(Mail::SRS); | |||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | Mail::SRS::DB - A MLDBM based Sender Rewriting Scheme | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | use Mail::SRS::DB; | ||||||||||
| 20 | my $srs = new Mail::SRS::DB( | ||||||||||
| 21 | Database => '/var/run/srs.db', | ||||||||||
| 22 | ... | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | See Mail::SRS for details of the standard SRS subclass interface. | ||||||||||
| 28 | This module provides the methods compile() and parse(). | ||||||||||
| 29 | |||||||||||
| 30 | This module requires one extra parameter to the constructor, a filename | ||||||||||
| 31 | for a Berkeley DB_File database. | ||||||||||
| 32 | |||||||||||
| 33 | =head1 BUGS | ||||||||||
| 34 | |||||||||||
| 35 | This code relies on not getting collisions in the cryptographic | ||||||||||
| 36 | hash. This can and should be fixed. | ||||||||||
| 37 | |||||||||||
| 38 | The database is not garbage collected. | ||||||||||
| 39 | |||||||||||
| 40 | =head1 SEE ALSO | ||||||||||
| 41 | |||||||||||
| 42 | L<Mail::SRS> | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||
| 47 | 1 | my $class = shift; | |||||||||
| 48 | 1 | my $self = $class->SUPER::new(@_); | |||||||||
| 49 | die "No database specified for Mail::SRS::DB" | ||||||||||
| 50 | 1 | ▶ | unless $self->{Database}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 1 | my %data; | |||||||||
| 52 | my $dbm = tie %data, 'MLDBM', | ||||||||||
| 53 | 1 | ▶ | $self->{Database}, O_CREAT|O_RDWR, 0640 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | or die "Cannot open $self->{Database}: $!"; | ||||||||||
| 55 | 0 | $self->{Data} = \%data; | |||||||||
| 56 | 0 | return $self; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub compile { | ||||||||
|
Subroutine
compile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 0 | my ($self, $sendhost, $senduser) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $time = time(); | |||||||||
| 63 | |||||||||||
| 64 | 0 | my $data = { | |||||||||
| 65 | Time => $time, | ||||||||||
| 66 | SendHost => $sendhost, | ||||||||||
| 67 | SendUser => $senduser, | ||||||||||
| 68 | }; | ||||||||||
| 69 | |||||||||||
| 70 | # We rely on not getting collisions in this hash. | ||||||||||
| 71 | 0 | my $hash = $self->hash_create($sendhost, $senduser); | |||||||||
| 72 | |||||||||||
| 73 | 0 | $self->{Data}->{$hash} = $data; | |||||||||
| 74 | |||||||||||
| 75 | # Note that there are 4 fields here and that sendhost may | ||||||||||
| 76 | # not contain a + sign. Therefore, we do not need to escape | ||||||||||
| 77 | # + signs anywhere in order to reverse this transformation. | ||||||||||
| 78 | 0 | return $SRS0TAG . $self->separator . $hash; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 82 | 0 | my ($self, $user) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | unless ($user =~ s/$SRS0RE//oi) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | die "Reverse address does not match $SRS0RE."; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | my $hash = $user; | |||||||||
| 89 | 0 | my $data; | |||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | unless ($data = $self->{Data}->{$hash}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | die "No data found"; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 0 | my $sendhost = $data->{SendHost}; | |||||||||
| 96 | 0 | my $senduser = $data->{SendUser}; | |||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | unless ($self->hash_verify($hash, $sendhost, $senduser)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | die "Invalid hash"; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | unless ($self->time_check($data->{Time})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | die "Invalid timestamp"; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | return ($sendhost, $senduser); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 2 | 1; | |||||||||