| 1 | package Mail::Milter::Authentication::Handler::RSpamD; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Mail::Milter::Authentication::Handler'; | ||
| 5 | our $VERSION = '2.20180318'; # VERSION | ||
| 6 | # #ABSTRACT: RSpamD scanning for Authentication Milter | ||
| 7 | |||
| 8 | use English qw{ -no_match_vars }; | ||
| 9 | use Sys::Syslog qw{:standard :macros}; | ||
| 10 | use HTTP::Tiny; | ||
| 11 | use JSON; | ||
| 12 | use Mail::AuthenticationResults::Header::Entry; | ||
| 13 | use Mail::AuthenticationResults::Header::SubEntry; | ||
| 14 | use Mail::AuthenticationResults::Header::Comment; | ||
| 15 | |||
| 16 | use Data::Dumper; | ||
| 17 | |||
| 18 | sub default_config { | ||
| 19 | return { | ||
| 20 | 'default_user' => 'nobody', | ||
| 21 | 'rs_host' => 'localhost', | ||
| 22 | 'rs_port' => '11333', | ||
| 23 | 'hard_reject' => 1, | ||
| 24 | 'remove_headers' => 'yes', | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | sub grafana_rows { | ||
| 29 | my ( $self ) = @_; | ||
| 30 | my @rows; | ||
| 31 | push @rows, $self->get_json( 'RSpamD_metrics' ); | ||
| 32 | return \@rows; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub register_metrics { | ||
| 36 | return { | ||
| 37 | 'rspamd_total' => 'The number of emails processed for RSpamD', | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub get_user { | ||
| 42 | my ( $self ) = @_; | ||
| 43 | my $user_handler = $self->get_handler('UserDB'); | ||
| 44 | my $user = $user_handler->{'local_user'}; | ||
| 45 | return $user if $user; | ||
| 46 | my $config = $self->handler_config(); | ||
| 47 | return $config->{'default_user'}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub remove_header { | ||
| 51 | my ( $self, $key, $value ) = @_; | ||
| 52 | if ( !exists( $self->{'remove_headers'} ) ) { | ||
| 53 | $self->{'remove_headers'} = {}; | ||
| 54 | } | ||
| 55 | if ( !exists( $self->{'remove_headers'}->{ lc $key } ) ) { | ||
| 56 | $self->{'remove_headers'}->{ $key } = []; | ||
| 57 | } | ||
| 58 | push @{ $self->{'remove_headers'}->{ lc $key } }, $value; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub helo_callback { | ||
| 63 | my ( $self, $helo_host ) = @_; | ||
| 64 | $self->{'helo_name'} = $helo_host; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub envfrom_callback { | ||
| 69 | my ($self, $from) = @_; | ||
| 70 | $self->{'lines'} = []; | ||
| 71 | $self->{'rcpt_to'} = []; | ||
| 72 | $self->{'mail_from'} = $from; | ||
| 73 | delete $self->{'header_index'}; | ||
| 74 | delete $self->{'remove_headers'}; | ||
| 75 | $self->{'metrics_data'} = {}; | ||
| 76 | $self->{ 'metrics_data' }->{ 'header_removed' } = 'no'; | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub envrcpt_callback { | ||
| 81 | my ( $self, $env_to ) = @_; | ||
| 82 | push @{ $self->{'rcpt_to'} }, $env_to; | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub header_callback { | ||
| 87 | my ( $self, $header, $value ) = @_; | ||
| 88 | push @{$self->{'lines'}} ,$header . ': ' . $value . "\r\n"; | ||
| 89 | my $config = $self->handler_config(); | ||
| 90 | |||
| 91 | return if ( $self->is_trusted_ip_address() ); | ||
| 92 | return if ( lc $config->{'remove_headers'} eq 'no' ); | ||
| 93 | |||
| 94 | foreach my $header_type ( qw{ X-Spam-score X-Spam-Status X-Spam-Action } ) { | ||
| 95 | if ( lc $header eq lc $header_type ) { | ||
| 96 | if ( !exists $self->{'header_index'} ) { | ||
| 97 | $self->{'header_index'} = {}; | ||
| 98 | } | ||
| 99 | if ( !exists $self->{'header_index'}->{ lc $header_type } ) { | ||
| 100 | $self->{'header_index'}->{ lc $header_type } = 0; | ||
| 101 | } | ||
| 102 | $self->{'header_index'}->{ lc $header_type } = | ||
| 103 | $self->{'header_index'}->{ lc $header_type } + 1; | ||
| 104 | $self->remove_header( $header_type, $self->{'header_index'}->{ lc $header_type } ); | ||
| 105 | $self->{ 'metrics_data' }->{ 'header_removed' } = 'yes'; | ||
| 106 | if ( lc $config->{'remove_headers'} ne 'silent' ) { | ||
| 107 | my $forged_header = | ||
| 108 | '(Received ' . $header_type . ' header removed by ' | ||
| 109 | . $self->get_my_hostname() | ||
| 110 | . ')' . "\n" | ||
| 111 | . ' ' | ||
| 112 | . $value; | ||
| 113 | $self->append_header( 'X-Received-' . $header_type, | ||
| 114 | $forged_header ); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub eoh_callback { | ||
| 123 | my ( $self ) = @_; | ||
| 124 | push @{$self->{'lines'}} , "\r\n"; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub body_callback { | ||
| 129 | my ( $self, $chunk ) = @_; | ||
| 130 | push @{$self->{'lines'}} , $chunk; | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub get_auth_name { | ||
| 135 | my ($self) = @_; | ||
| 136 | my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'}; | ||
| 137 | if ( $protocol ne 'milter' ) { | ||
| 138 | return 'unauthorized'; | ||
| 139 | } | ||
| 140 | my $name = $self->get_symbol('{auth_authen}') || 'unauthorized'; | ||
| 141 | return $name; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub eom_callback { | ||
| 145 | my ($self) = @_; | ||
| 146 | |||
| 147 | my $config = $self->handler_config(); | ||
| 148 | |||
| 149 | my $host = $config->{'rs_host'} || 'localhost'; | ||
| 150 | my $port = $config->{'rs_port'} || 11333; | ||
| 151 | my $user = $self->get_user(); | ||
| 152 | |||
| 153 | $self->dbgout( 'RSpamDUser', $user, LOG_INFO ); | ||
| 154 | |||
| 155 | my $queue_id = $self->get_symbol('i') || q{--}; | ||
| 156 | |||
| 157 | my $message = join( q{} , @{$self->{'lines'} } ); | ||
| 158 | my $headers = { | ||
| 159 | 'Deliver-To' => $user, | ||
| 160 | 'IP' => $self->ip_address(), | ||
| 161 | 'Helo' => $self->{'helo_name'}, | ||
| 162 | 'From' => $self->{'mail_from'}, | ||
| 163 | 'Queue-Id' => $queue_id, | ||
| 164 | 'Rcpt' => $self->{'rcpt_to'}, | ||
| 165 | 'Pass' => 'all', # all to check all filters | ||
| 166 | 'User' => $self->get_auth_name(), | ||
| 167 | }; | ||
| 168 | |||
| 169 | my $http = HTTP::Tiny->new( | ||
| 170 | 'keep_alive' => 0, | ||
| 171 | ); | ||
| 172 | my $response = $http->post( "http://$host:$port/check", { 'headers' => $headers, 'content' => $message } ); | ||
| 173 | if ( ! $response->{'success'} ) { | ||
| 174 | $self->log_error( 'RSpamD could not connect to server - ' . $response->{'status'} . ' - ' . $response->{'reason'} . ' - ' . $response->{'content'} ); | ||
| 175 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-rspam' )->safe_set_value( 'temperror' ); | ||
| 176 | $self->add_auth_header( $header ); | ||
| 177 | $self->{ 'metrics_data' }->{ 'result' } = 'servererror'; | ||
| 178 | $self->metric_count( 'rspamd_total', $self->{ 'metrics_data' } ); | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | my $j = JSON->new(); | ||
| 183 | my $rspamd_data = eval{ $j->decode( $response->{'content'} ); }; | ||
| 184 | if ( ! exists( $rspamd_data->{'default'} ) ) { | ||
| 185 | $self->log_error( 'RSpamD bad data from server' ); | ||
| 186 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-rspam' )->safe_set_value( 'temperror' ); | ||
| 187 | $self->add_auth_header( $header ); | ||
| 188 | $self->{ 'metrics_data' }->{ 'result' } = 'serverdataerror'; | ||
| 189 | $self->metric_count( 'rspamd_total', $self->{ 'metrics_data' } ); | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | my $spam = $rspamd_data->{ 'default' }; | ||
| 193 | |||
| 194 | my $status = join( q{}, | ||
| 195 | ( $spam->{'is_spam'} eq 0 ? 'No, ' : 'Yes, ' ), | ||
| 196 | 'score=', sprintf( '%.02f', $spam->{'score'} ), | ||
| 197 | ' ', | ||
| 198 | 'required=', sprintf( '%.02f', $spam->{'required_score'} ), | ||
| 199 | ); | ||
| 200 | |||
| 201 | my $action = $spam->{'action'}; | ||
| 202 | |||
| 203 | if ( $action eq 'rewrite subject' ) { | ||
| 204 | $action .= ' - ' . $spam->{'subject'}; | ||
| 205 | ## ToDo - Rewrite the subject | ||
| 206 | } | ||
| 207 | |||
| 208 | if ( $action eq 'reject' ) { | ||
| 209 | if ( $config->{'hard_reject'} ) { | ||
| 210 | if ( ( ! $self->is_local_ip_address() ) && ( ! $self->is_trusted_ip_address() ) ) { | ||
| 211 | $self->reject_mail( '550 5.7.0 SPAM policy violation' ); | ||
| 212 | $self->dbgout( 'RSpamDReject', "Policy reject", LOG_INFO ); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->prepend_header( 'X-Spam-score', sprintf( '%.02f', $spam->{'score'} ) ); | ||
| 218 | $self->prepend_header( 'X-Spam-Status', $status ); | ||
| 219 | $self->prepend_header( 'X-Spam-Action', $action ); | ||
| 220 | |||
| 221 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-rspam' )->safe_set_value( ( $spam->{'is_spam'} eq 0 ? 'pass' : 'fail' ) ); | ||
| 222 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'score' )->safe_set_value( sprintf ( '%.02f', $spam->{'score'} ) ) ); | ||
| 223 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'required' )->safe_set_value( sprintf ( '%.02f', $spam->{'required_score'} ) ) ); | ||
| 224 | $self->add_auth_header($header); | ||
| 225 | |||
| 226 | $self->{ 'metrics_data' }->{ 'result' } = ( $spam->{'is_spam'} eq 0 ? 'pass' : 'fail' ); | ||
| 227 | |||
| 228 | $self->metric_count( 'rspamd_total', $self->{ 'metrics_data' } ); | ||
| 229 | return if ( lc $config->{'remove_headers'} eq 'no' ); | ||
| 230 | |||
| 231 | foreach my $header_type ( qw{ X-Spam-score X-Spam-Status X-Spam-Action } ) { | ||
| 232 | if ( exists( $self->{'remove_headers'}->{ lc $header_type } ) ) { | ||
| 233 | foreach my $header ( reverse @{ $self->{'remove_headers'}->{ lc $header_type } } ) { | ||
| 234 | $self->dbgout( 'RemoveSpamHeader', $header_type . ', ' . $header, LOG_DEBUG ); | ||
| 235 | $self->change_header( lc $header_type, $header, q{} ); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | return; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub close_callback { | ||
| 244 | my ( $self ) = @_; | ||
| 245 | |||
| 246 | delete $self->{'lines'}; | ||
| 247 | delete $self->{'mail_from'}; | ||
| 248 | delete $self->{'helo_name'}; | ||
| 249 | delete $self->{'rcpt_to'}; | ||
| 250 | delete $self->{'remove_headers'}; | ||
| 251 | delete $self->{'header_index'}; | ||
| 252 | delete $self->{'metrics_data'}; | ||
| 253 | return; | ||
| 254 | } | ||
| 255 | |||
| 256 | 1; | ||
| 257 | |||
| 258 | __END__ | ||
| 259 | |||
| 260 | =pod | ||
| 261 | |||
| 262 | =encoding UTF-8 | ||
| 263 | |||
| 264 | =head1 NAME | ||
| 265 | |||
| 266 | Mail::Milter::Authentication::Handler::RSpamD | ||
| 267 | |||
| 268 | =head1 VERSION | ||
| 269 | |||
| 270 | version 2.20180318 | ||
| 271 | |||
| 272 | =head1 DESCRIPTION | ||
| 273 | |||
| 274 | Check email for spam using rspamd. | ||
| 275 | |||
| 276 | =head1 CONFIGURATION | ||
| 277 | |||
| 278 | "RSpamD" : { | ||
| 279 | "default_user" : "nobody", | ||
| 280 | "rs_host" : "localhost", | ||
| 281 | "rs_port" : "11333", | ||
| 282 | "hard_reject" : "1", | ||
| 283 | "remove_headers" : "yes" | ||
| 284 | }, | ||
| 285 | |||
| 286 | =head2 CONFIG | ||
| 287 | |||
| 288 | Add a block to the handlers section of your config as follows. | ||
| 289 | |||
| 290 | "RSpamD" : { | ||
| 291 | "default_user" : "nobody", | ||
| 292 | "rs_host" : "localhost", | ||
| 293 | "rs_port" : "11333", | ||
| 294 | "hard_reject" : "1", | ||
| 295 | "remove_headers" : "yes" | ||
| 296 | }, | ||
| 297 | |||
| 298 | =head1 AUTHOR | ||
| 299 | |||
| 300 | Marc Bradshaw <marc@marcbradshaw.net> | ||
| 301 | |||
| 302 | =head1 COPYRIGHT AND LICENSE | ||
| 303 | |||
| 304 | This software is copyright (c) 2018 by Marc Bradshaw. | ||
| 305 | |||
| 306 | This is free software; you can redistribute it and/or modify it under | ||
| 307 | the same terms as the Perl 5 programming language system itself. | ||
| 308 | |||
| 309 | =cut |