| 1 | package Mail::Milter::Authentication::Handler::UserDB::Hash; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use base 'Mail::Milter::Authentication::Handler::UserDB'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our $VERSION = '2.20180318'; # VERSION | ||
| 6 | # ABSTRACT: Hash table based userdb provider | ||
| 7 | |||
| 8 | use DB_File; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ( $class, $file ) = @_; | ||
| 12 | |||
| 13 | my $self = { | ||
| 14 | 'file' => $file . '.db', | ||
| 15 | 'checked_time' => 0, | ||
| 16 | 'table' => undef, | ||
| 17 | 'table_stamp' => 0, | ||
| 18 | }; | ||
| 19 | |||
| 20 | bless $self, $class; | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub preload { | ||
| 25 | my ( $self ) = @_; | ||
| 26 | $self->{'checked_time'} = time; | ||
| 27 | my $null = $self->get_table(); | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub check_reload { | ||
| 32 | my ( $self ) = @_; | ||
| 33 | my $now = time; | ||
| 34 | my $check_time = 60*10; # Check no more often than every 10 minutes | ||
| 35 | if ( $now > $self->{'checked_time'} + $check_time ) { | ||
| 36 | $self->{'checked_time'} = $now; | ||
| 37 | return $self->check_table(); | ||
| 38 | } | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub get_user_from_address { | ||
| 43 | my ( $self, $address ) = @_; | ||
| 44 | $address =~ s/\+.*@/@/; | ||
| 45 | my $table = $self->get_table(); | ||
| 46 | if ( exists $table->{ lc $address } ) { | ||
| 47 | return $table->{ lc $address }; | ||
| 48 | } | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub get_table { | ||
| 53 | my ( $self ) = @_; | ||
| 54 | return $self->{'table'} if $self->{'table'}; | ||
| 55 | my $file = $self->{'file'}; | ||
| 56 | return if not $file; | ||
| 57 | if ( ! -e $file ) { | ||
| 58 | warn "UserDB File $file does not exist"; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | $self->{'table_stamp'} = ( stat( $file ) )[9]; | ||
| 62 | my %h; | ||
| 63 | tie %h, 'DB_File', $file, O_RDONLY, undef, $DB_HASH; | ||
| 64 | my $table = {}; | ||
| 65 | foreach my $k ( keys %h ) { | ||
| 66 | my $user= $h{$k}; | ||
| 67 | if ( !( $user =~ /\@/ )) { | ||
| 68 | $user =~ s/\x00//g; | ||
| 69 | $k =~ s/\x00//g; | ||
| 70 | $table->{ lc $k } = $user; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $self->{'table'} = $table; | ||
| 74 | return $table; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub check_table { | ||
| 78 | my ( $self ) = @_; | ||
| 79 | my $file = $self->{'file'}; | ||
| 80 | return if not $file; | ||
| 81 | my $new_table_stamp = ( stat( $file ) )[9]; | ||
| 82 | if ( $new_table_stamp != $self->{'table_stamp'} ) { | ||
| 83 | delete $self->{'table'}; | ||
| 84 | my $null = $self->get_table(); | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =encoding UTF-8 | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | Mail::Milter::Authentication::Handler::UserDB::Hash - Hash table based userdb provider | ||
| 102 | |||
| 103 | =head1 VERSION | ||
| 104 | |||
| 105 | version 2.20180318 | ||
| 106 | |||
| 107 | =head1 AUTHOR | ||
| 108 | |||
| 109 | Marc Bradshaw <marc@marcbradshaw.net> | ||
| 110 | |||
| 111 | =head1 COPYRIGHT AND LICENSE | ||
| 112 | |||
| 113 | This software is copyright (c) 2018 by Marc Bradshaw. | ||
| 114 | |||
| 115 | This is free software; you can redistribute it and/or modify it under | ||
| 116 | the same terms as the Perl 5 programming language system itself. | ||
| 117 | |||
| 118 | =cut | ||