| 1 | package Mail::Milter::Authentication::Handler::UserDB; | ||
| 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 DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Mail::Milter::Authentication::Handler::UserDB::Hash; | ||
| 6 | use Sys::Syslog qw{:standard :macros}; | ||
| 7 | use Mail::AuthenticationResults::Header::Entry; | ||
| 8 | use Mail::AuthenticationResults::Header::SubEntry; | ||
| 9 | use Mail::AuthenticationResults::Header::Comment; | ||
| 10 | use base 'Mail::Milter::Authentication::Handler'; | ||
| 11 | our $VERSION = '2.20180510'; # VERSION | ||
| 12 | # ABSTRACT: Check inbound mail against a user list | ||
| 13 | |||
| 14 | my $CHECKED_TIME; | ||
| 15 | |||
| 16 | sub default_config { | ||
| 17 | return { | ||
| 18 | 'add_header' => 1, | ||
| 19 | 'lookup' => [ 'hash:/etc/postfix/virtusertable' ], | ||
| 20 | }; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub grafana_rows { | ||
| 24 | my ( $self ) = @_; | ||
| 25 | my @rows; | ||
| 26 | push @rows, $self->get_json( 'UserDB_metrics' ); | ||
| 27 | return \@rows; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub register_metrics { | ||
| 31 | return { | ||
| 32 | 'userdb_total' => 'The number of emails processed for UserDB', | ||
| 33 | }; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub setup_callback { | ||
| 37 | my ( $self ) = @_; | ||
| 38 | delete $self->{'local_user'}; | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub envrcpt_callback { | ||
| 43 | my ( $self, $env_to ) = @_; | ||
| 44 | my $address = $self->get_address_from( $env_to ); | ||
| 45 | my $user = $self->get_user_from_address( $address ); | ||
| 46 | $self->{'local_user'} = $user if $user; | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub eoh_callback { | ||
| 51 | my ( $self ) = @_; | ||
| 52 | my $config = $self->handler_config(); | ||
| 53 | if ( $self->{'local_user'} ) { | ||
| 54 | $self->metric_count( 'userdb_total', { 'result' => 'pass' } ); | ||
| 55 | if ( $config->{'add_header'} ) { | ||
| 56 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-local-user' )->safe_set_value( 'pass' ); | ||
| 57 | $self->add_auth_header( $header ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $self->metric_count( 'userdb_total', { 'result' => 'fail' } ); | ||
| 62 | } | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub close_callback { | ||
| 67 | my ( $self ) = @_; | ||
| 68 | delete $self->{'local_user'}; | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | { | ||
| 73 | my $lookers_cache; | ||
| 74 | |||
| 75 | sub get_lookers { | ||
| 76 | my ( $self ) = @_; | ||
| 77 | |||
| 78 | if ( $lookers_cache ) { | ||
| 79 | my $reloaded = 0; | ||
| 80 | foreach my $looker ( @{$lookers_cache} ) { | ||
| 81 | $reloaded = $reloaded + $looker->check_reload(); | ||
| 82 | } | ||
| 83 | if ( $reloaded ) { | ||
| 84 | $self->dbgout( 'UserDb', 'Re-loading User DB', LOG_INFO ); | ||
| 85 | } | ||
| 86 | return $lookers_cache; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->dbgout( 'UserDb', 'Loading User DB', LOG_DEBUG ); | ||
| 90 | |||
| 91 | my @lookers; | ||
| 92 | my $config = $self->handler_config(); | ||
| 93 | my $lookups = $config->{'lookup'}; | ||
| 94 | foreach my $lookup ( @$lookups ) { | ||
| 95 | my ( $type, $data ) = split ':', $lookup, 2; | ||
| 96 | if ( $type eq 'hash' ) { | ||
| 97 | my $looker = Mail::Milter::Authentication::Handler::UserDB::Hash->new( $data ); | ||
| 98 | push @lookers, $looker; | ||
| 99 | $looker->preload(); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | die "Unknown UserDB lookup type $type"; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | $lookers_cache = \@lookers; | ||
| 106 | return $lookers_cache; | ||
| 107 | } | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | sub get_user_from_address { | ||
| 112 | my ( $self, $address ) = @_; | ||
| 113 | $self->dbgout( 'UserDb Lookup', $address, LOG_DEBUG ); | ||
| 114 | my $lookers = $self->get_lookers(); | ||
| 115 | foreach my $looker ( @{$lookers} ) { | ||
| 116 | my $user = $looker->get_user_from_address( $address ); | ||
| 117 | $self->dbgout( 'UserDb Found', $user, LOG_DEBUG ) if $user; | ||
| 118 | return $user if $user; | ||
| 119 | } | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ | ||
| 126 | |||
| 127 | =pod | ||
| 128 | |||
| 129 | =encoding UTF-8 | ||
| 130 | |||
| 131 | =head1 NAME | ||
| 132 | |||
| 133 | Mail::Milter::Authentication::Handler::UserDB - Check inbound mail against a user list | ||
| 134 | |||
| 135 | =head1 VERSION | ||
| 136 | |||
| 137 | version 2.20180510 | ||
| 138 | |||
| 139 | =head1 DESCRIPTION | ||
| 140 | |||
| 141 | Check if email has a local recipient account. | ||
| 142 | |||
| 143 | =head1 CONFIGURATION | ||
| 144 | |||
| 145 | "UserDB" : { | ||
| 146 | "add_header" : 1, | ||
| 147 | "lookup" : [ "hash:/etc/postfix/virtusertable" ] | ||
| 148 | }, | ||
| 149 | |||
| 150 | =head2 CONFIG | ||
| 151 | |||
| 152 | Add a block to the handlers section of your config as follows. | ||
| 153 | |||
| 154 | "UserDB" : { | ||
| 155 | "add_header" : 1, | ||
| 156 | "lookup" : [ "hash:/etc/postfix/virtusertable" ] | ||
| 157 | }, | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | Marc Bradshaw <marc@marcbradshaw.net> | ||
| 162 | |||
| 163 | =head1 COPYRIGHT AND LICENSE | ||
| 164 | |||
| 165 | This software is copyright (c) 2018 by Marc Bradshaw. | ||
| 166 | |||
| 167 | This is free software; you can redistribute it and/or modify it under | ||
| 168 | the same terms as the Perl 5 programming language system itself. | ||
| 169 | |||
| 170 | =cut | ||