line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::Milter::Authentication::Handler::UserDB; |
2
|
1
|
|
|
1
|
|
771
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
4
|
1
|
|
|
1
|
|
196
|
use DB_File; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Mail::Milter::Authentication::Handler::UserDB::Hash; |
6
|
|
|
|
|
|
|
use Sys::Syslog qw{:standard :macros}; |
7
|
|
|
|
|
|
|
use base 'Mail::Milter::Authentication::Handler'; |
8
|
|
|
|
|
|
|
use version; our $VERSION = version->declare('v1.1.1'); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $CHECKED_TIME; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub default_config { |
13
|
|
|
|
|
|
|
return { |
14
|
|
|
|
|
|
|
'add_header' => 1, |
15
|
|
|
|
|
|
|
'lookup' => [ 'hash:/etc/postfix/virtusertable' ], |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register_metrics { |
20
|
|
|
|
|
|
|
return { |
21
|
|
|
|
|
|
|
'userdb_total' => 'The number of emails processed for UserDB', |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub setup_callback { |
26
|
|
|
|
|
|
|
my ( $self ) = @_; |
27
|
|
|
|
|
|
|
delete $self->{'local_user'}; |
28
|
|
|
|
|
|
|
return; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub envrcpt_callback { |
32
|
|
|
|
|
|
|
my ( $self, $env_to ) = @_; |
33
|
|
|
|
|
|
|
my $address = $self->get_address_from( $env_to ); |
34
|
|
|
|
|
|
|
my $user = $self->get_user_from_address( $address ); |
35
|
|
|
|
|
|
|
$self->{'local_user'} = $user if $user; |
36
|
|
|
|
|
|
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub eoh_callback { |
40
|
|
|
|
|
|
|
my ( $self ) = @_; |
41
|
|
|
|
|
|
|
my $config = $self->handler_config(); |
42
|
|
|
|
|
|
|
return if ! $config->{'add_header'}; |
43
|
|
|
|
|
|
|
if ( $self->{'local_user'} ) { |
44
|
|
|
|
|
|
|
$self->add_auth_header('x-local-user=pass'); |
45
|
|
|
|
|
|
|
$self->metric_count( 'userdb_total', { 'result' => 'pass' } ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
|
|
|
|
|
|
$self->metric_count( 'userdb_total', { 'result' => 'fail' } ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
return; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub close_callback { |
54
|
|
|
|
|
|
|
my ( $self ) = @_; |
55
|
|
|
|
|
|
|
delete $self->{'local_user'}; |
56
|
|
|
|
|
|
|
return; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
my $lookers_cache; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub get_lookers { |
63
|
|
|
|
|
|
|
my ( $self ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
if ( $lookers_cache ) { |
66
|
|
|
|
|
|
|
my $reloaded = 0; |
67
|
|
|
|
|
|
|
foreach my $looker ( @{$lookers_cache} ) { |
68
|
|
|
|
|
|
|
$reloaded = $reloaded + $looker->check_reload(); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
if ( $reloaded ) { |
71
|
|
|
|
|
|
|
$self->dbgout( 'UserDb', 'Re-loading User DB', LOG_INFO ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
return $lookers_cache; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$self->dbgout( 'UserDb', 'Loading User DB', LOG_DEBUG ); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my @lookers; |
79
|
|
|
|
|
|
|
my $config = $self->handler_config(); |
80
|
|
|
|
|
|
|
my $lookups = $config->{'lookup'}; |
81
|
|
|
|
|
|
|
foreach my $lookup ( @$lookups ) { |
82
|
|
|
|
|
|
|
my ( $type, $data ) = split ':', $lookup, 2; |
83
|
|
|
|
|
|
|
if ( $type eq 'hash' ) { |
84
|
|
|
|
|
|
|
my $looker = Mail::Milter::Authentication::Handler::UserDB::Hash->new( $data ); |
85
|
|
|
|
|
|
|
push @lookers, $looker; |
86
|
|
|
|
|
|
|
$looker->preload(); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
die "Unknown UserDB lookup type $type"; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
$lookers_cache = \@lookers; |
93
|
|
|
|
|
|
|
return $lookers_cache; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_user_from_address { |
99
|
|
|
|
|
|
|
my ( $self, $address ) = @_; |
100
|
|
|
|
|
|
|
$self->dbgout( 'UserDb Lookup', $address, LOG_DEBUG ); |
101
|
|
|
|
|
|
|
my $lookers = $self->get_lookers(); |
102
|
|
|
|
|
|
|
foreach my $looker ( @{$lookers} ) { |
103
|
|
|
|
|
|
|
my $user = $looker->get_user_from_address( $address ); |
104
|
|
|
|
|
|
|
$self->dbgout( 'UserDb Found', $user, LOG_DEBUG ) if $user; |
105
|
|
|
|
|
|
|
return $user if $user; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
return; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 NAME |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Authentication Milter - UserDB Module |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Check if email has a local recipient account. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 CONFIGURATION |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
"UserDB" : { |
125
|
|
|
|
|
|
|
"add_header" : 1, |
126
|
|
|
|
|
|
|
"lookup" : [ "hash:/etc/postfix/virtusertable" ] |
127
|
|
|
|
|
|
|
}, |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SYNOPSIS |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 CONFIG |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Add a block to the handlers section of your config as follows. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
"UserDB" : { |
136
|
|
|
|
|
|
|
"add_header" : 1, |
137
|
|
|
|
|
|
|
"lookup" : [ "hash:/etc/postfix/virtusertable" ] |
138
|
|
|
|
|
|
|
}, |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 AUTHORS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Marc Bradshaw E<lt>marc@marcbradshaw.netE<gt> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 COPYRIGHT |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Copyright 2015 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This library is free software; you may redistribute it and/or |
150
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|