| 1 | |||
| 2 | # Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com> | ||
| 3 | # | ||
| 4 | # This program is free software; you can redistribute it and/or modify | ||
| 5 | # it under the terms of the GNU General Public License as published by | ||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||
| 7 | # (at your option) any later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 17 | # | ||
| 18 | # This software is available without the GPL: please write if you need | ||
| 19 | # a non-GPL license. All submissions of patches must come with a | ||
| 20 | # copyright grant so that David Sharnoff remains able to change the | ||
| 21 | # license at will. | ||
| 22 | |||
| 23 | package Qpsmtpd::Plugin::Quarantine; | ||
| 24 | |||
| 25 | use Qpsmtpd::Constants; | ||
| 26 | use Qpsmtpd::DSN; | ||
| 27 | use OOPS; | ||
| 28 | use Digest::MD5 qw(md5_hex); | ||
| 29 | use File::Slurp; | ||
| 30 | use Time::HiRes qw(); | ||
| 31 | use Net::Netmask; | ||
| 32 | use CGI; | ||
| 33 | use Template; | ||
| 34 | use Sys::Hostname; | ||
| 35 | use Mail::SPF::Query; | ||
| 36 | use Mail::Field; | ||
| 37 | use Net::SMTP; | ||
| 38 | use Mail::Address; | ||
| 39 | use DB_File; | ||
| 40 | use Qpsmtpd::Plugin::Quarantine::Spam; | ||
| 41 | use Qpsmtpd::Plugin::Quarantine::Common; | ||
| 42 | use Qpsmtpd::Plugin::Quarantine::Sendmail; | ||
| 43 | require Mail::Field::Received; | ||
| 44 | require Exporter; | ||
| 45 | |||
| 46 | @ISA = qw(Exporter); | ||
| 47 | @EXPORT = qw(hook_data_post register); | ||
| 48 | @EXPORT_OK = qw($hostname); | ||
| 49 | |||
| 50 | use strict; | ||
| 51 | use warnings; | ||
| 52 | |||
| 53 | our $VERSION = 1.02; | ||
| 54 | my $debug = 1; | ||
| 55 | |||
| 56 | our $myhostname = hostname(); | ||
| 57 | |||
| 58 | my $fmes1 = "Your message is quarantined because we think it is probably spam, if it is not spam, click"; | ||
| 59 | my $fmes2 = "to release your message from quarantine or to choose to have the spam you send silently deleted instead of bounced"; | ||
| 60 | my $fmes3 = "None of the recipients of your email wish to receive mail that is likely to be spam"; | ||
| 61 | |||
| 62 | # ----------------------------------------------------------------------- | ||
| 63 | |||
| 64 | my $qa = {}; # state hash | ||
| 65 | |||
| 66 | my $fmes1rx = $defaults{senderbounce1}; | ||
| 67 | $fmes1rx =~ s/ /\\s+/g; | ||
| 68 | $fmes1rx = qr/$fmes1rx/; | ||
| 69 | my $fmes2rx = $defaults{senderbounce2}; | ||
| 70 | $fmes2rx =~ s/ /\\s+/g; | ||
| 71 | $fmes2rx = qr/$fmes2rx/; | ||
| 72 | my $fmes3rx = $defaults{senderbounce3}; | ||
| 73 | $fmes3rx =~ s/ /\\s+/g; | ||
| 74 | $fmes3rx = qr/$fmes3rx/; | ||
| 75 | |||
| 76 | # ----------------------------------------------------------------------- | ||
| 77 | |||
| 78 | sub register | ||
| 79 | { | ||
| 80 | my ($qp, undef, @args) = @_; | ||
| 81 | my %args; | ||
| 82 | if (@args % 2 == 0) { | ||
| 83 | %args = @args; | ||
| 84 | } else { | ||
| 85 | warn "Malformed arguments to syslog plugin"; | ||
| 86 | return DECLINED; | ||
| 87 | } | ||
| 88 | $qa->{justkidding} = "no templates directory" | ||
| 89 | unless -d $defaults{templates}; | ||
| 90 | $qa->{start_time} = Time::HiRes::time(); | ||
| 91 | $qa->{our_domains} = { map { $_ => 1 } $qp->qp->config('our_domains') }; | ||
| 92 | $qa->{filter_domains} = { map { $_ => 1 } $qp->qp->config('filter_domains') }; | ||
| 93 | |||
| 94 | recompute_defaults; | ||
| 95 | |||
| 96 | if (0 && $debug) { | ||
| 97 | for my $i (sort keys %{$qa->{our_domains}}) { | ||
| 98 | $qp->log(LOGDEBUG, "Our domain: $i"); | ||
| 99 | } | ||
| 100 | for my $i (sort keys %{$qa->{filter_domains}}) { | ||
| 101 | $qp->log(LOGDEBUG, "Filter domain: $i"); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | $qa->{our_networks} = {}; | ||
| 106 | for my $net ($qp->qp->config('our_networks')) { | ||
| 107 | my $block = new2 Net::Netmask $net; | ||
| 108 | $qp->log(LOGDEBUG, "Our network: $net"); | ||
| 109 | if ($block) { | ||
| 110 | $block->storeNetblock($qa->{our_networks}); | ||
| 111 | } else { | ||
| 112 | warn "Cannot parse network block '$net': $Net::Netmask::error" | ||
| 113 | } | ||
| 114 | } | ||
| 115 | $qa->{ignore_networks} = {}; | ||
| 116 | for my $net ($qp->qp->config('ignore_networks'), @{$defaults{ignore_networks}}) { | ||
| 117 | my $block = new2 Net::Netmask $net; | ||
| 118 | $qp->log(LOGDEBUG, "Ingore network: $net"); | ||
| 119 | if ($block) { | ||
| 120 | $block->storeNetblock($qa->{ignore_networks}); | ||
| 121 | } else { | ||
| 122 | warn "Cannot parse network block '$net': $Net::Netmask::error" | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | $qa->{template} = Template->new({ | ||
| 127 | INCLUDE_PATH => $args{templates}, | ||
| 128 | INTERPOLATE => 1, | ||
| 129 | POST_CHOMP => 0, | ||
| 130 | EVAL_PERL => 1, | ||
| 131 | RECURSION => 1, | ||
| 132 | }) || die Template->error(); | ||
| 133 | srand(time ^ ($$ << 5)); | ||
| 134 | |||
| 135 | if ($defaults{notify_recipient_only} && -e $defaults{notify_recipient_only}) { | ||
| 136 | my $db = tie my %h, 'DB_File', $defaults{notify_recipient_only} | ||
| 137 | or die "dbopen $defaults{notify_recipient_only}: $!"; | ||
| 138 | $db->filter_fetch_key ( sub { s/\0$// } ) ; | ||
| 139 | $db->filter_store_key ( sub { $_ .= "\0" } ) ; | ||
| 140 | $db->filter_fetch_value( sub { s/\0$// } ) ; | ||
| 141 | $db->filter_store_value( sub { $_ .= "\0" } ) ; | ||
| 142 | |||
| 143 | $qa->{notify_recipient_only} = \%h; | ||
| 144 | } else { | ||
| 145 | $qa->{notify_recipient_only} = {}; | ||
| 146 | } | ||
| 147 | |||
| 148 | if ($defaults{special_sender_db} && -e $defaults{special_sender_db}) { | ||
| 149 | my $db = tie my %h, 'DB_File', $defaults{special_sender_db} | ||
| 150 | or die "dbmopen $defaults{special_sender_db}: $!"; | ||
| 151 | $db->filter_fetch_key ( sub { s/\0$// } ) ; | ||
| 152 | $db->filter_store_key ( sub { $_ .= "\0" } ) ; | ||
| 153 | $db->filter_fetch_value( sub { s/\0$// } ) ; | ||
| 154 | $db->filter_store_value( sub { $_ .= "\0" } ) ; | ||
| 155 | |||
| 156 | $qa->{special_sender_db} = \%h; | ||
| 157 | } else { | ||
| 158 | $qa->{sepcial_sender_db} = {}; | ||
| 159 | } | ||
| 160 | |||
| 161 | spam_init(); | ||
| 162 | |||
| 163 | return OK; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub hook_data_post | ||
| 167 | { | ||
| 168 | my ($qp, $transaction) = @_; | ||
| 169 | |||
| 170 | $qp->log(LOGDEBUG, "----------------------------------------------------------------------------------"); | ||
| 171 | |||
| 172 | get_message_info($qp, $transaction); | ||
| 173 | |||
| 174 | if ($transaction->notes('filtered_recipient_count')) { | ||
| 175 | $qp->log(LOGDEBUG, "Checking message to a filtered destination"); | ||
| 176 | } elsif ($defaults{check_from_our_ip} && $transaction->notes('address_type') eq 'internal') { | ||
| 177 | $qp->log(LOGDEBUG, "Checking message from our IP addresses"); | ||
| 178 | } elsif ($defaults{check_not_from_our_ip} && $transaction->notes('address_type') eq 'external') { | ||
| 179 | $qp->log(LOGDEBUG, "Checking message not from our IP addresses"); | ||
| 180 | } elsif ($defaults{check_from_our_domain} && $transaction->notes('from_our_domain')) { | ||
| 181 | $qp->log(LOGDEBUG, "Checking message from our domain"); | ||
| 182 | } elsif ($defaults{check_not_our_domain} && ! $transaction->notes('from_our_domain')) { | ||
| 183 | $qp->log(LOGDEBUG, "Checking message not from our domain"); | ||
| 184 | } elsif ($defaults{check_all_recipients}) { | ||
| 185 | $qp->log(LOGDEBUG, "Checking message because we check 'em all"); | ||
| 186 | } elsif ($defaults{special_sender_db} && $qa->{special_sender_db}{$transaction->sender->address()}) { | ||
| 187 | $qp->log(LOGDEBUG, "Checking message from a special sender"); | ||
| 188 | } elsif ($defaults{randomly_check_messages} && rand(100) < $defaults{randomly_check_messages}) { | ||
| 189 | $qp->log(LOGDEBUG, "Checking message randomly"); | ||
| 190 | } else { | ||
| 191 | $qp->log(LOGDEBUG, "QRESULT: Not checking for spam"); | ||
| 192 | return DECLINED; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $spammy = check_message_for_spam($qp, $transaction); | ||
| 196 | |||
| 197 | unless ($spammy) { | ||
| 198 | $qp->log(LOGDEBUG, "QRESULT: Not spam"); | ||
| 199 | return DECLINED; | ||
| 200 | } | ||
| 201 | |||
| 202 | return filterit($qp, $transaction, $spammy); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub filterit | ||
| 206 | { | ||
| 207 | my ($qp, $transaction, $spam_reason) = @_; | ||
| 208 | my @retcode = DECLINED; | ||
| 209 | die unless $qa->{our_networks}; | ||
| 210 | my $qt = $transaction->notes('quarantine'); | ||
| 211 | if ($qa->{justkidding}) { | ||
| 212 | $qp->log(LOGDEBUG, "skipping... $qa->{justkidding}"); | ||
| 213 | return DECLINED; | ||
| 214 | } | ||
| 215 | if ($qt->{already_filtered}++) { | ||
| 216 | $qp->log(LOGDEBUG, "skipping... already filtered"); | ||
| 217 | return DECLINED; | ||
| 218 | } | ||
| 219 | |||
| 220 | my $body = $transaction->body_as_string(); | ||
| 221 | my $body_checksum = md5_hex($body); | ||
| 222 | my $headers = $transaction->header(); | ||
| 223 | my $header = $headers->as_string(); | ||
| 224 | my $suser = $transaction->sender->user(); | ||
| 225 | my $sdomain = $transaction->sender->host(); | ||
| 226 | my $sender_address = "$suser\@$sdomain"; | ||
| 227 | my @recipients = map { $_->address() } $transaction->recipients(); | ||
| 228 | |||
| 229 | my $ip = $transaction->notes('external_ip') || $transaction->notes('internal_ip'); | ||
| 230 | |||
| 231 | |||
| 232 | transaction(sub { | ||
| 233 | my $oops = get_oops(); | ||
| 234 | my $time = time; | ||
| 235 | my $qd = $oops->{quarantine} || initialize($qp, $oops, $transaction); | ||
| 236 | my $psender = $qd->{senders}{$sender_address}; | ||
| 237 | my $sender_token = $psender | ||
| 238 | ? $psender->{token} | ||
| 239 | : md5_hex($$ . Time::HiRes::time() . $sender_address); | ||
| 240 | my $header_checksum = md5_hex($header . $qd->{random_token} . $sender_token); | ||
| 241 | |||
| 242 | # | ||
| 243 | # Let our own bounces through (up to a point) | ||
| 244 | # | ||
| 245 | |||
| 246 | $qp->log(LOGDEBUG, "RX: $fmes1rx\\s+\Q$defaults{baseurl}\E/message/(\\S+)\\s+$fmes2rx"); | ||
| 247 | if ($body =~ m{$fmes1rx\s+\Q$defaults{baseurl}\E/message/(\S+)\s+$fmes2rx}s && exists $qd->{headers}{$1}) { | ||
| 248 | |||
| 249 | # This is an email about something we have held in quarantine. | ||
| 250 | my $pheader = $qd->{headers}{$1}; | ||
| 251 | if ($pheader->{bounce_seen}++ < $defaults{max_bounces_per_header}) { | ||
| 252 | $oops->commit(); | ||
| 253 | $qp->log(LOGINFO, "QRESULT: Message with our own bounce forwarded"); | ||
| 254 | @retcode = DECLINED; | ||
| 255 | return; | ||
| 256 | } else { | ||
| 257 | $qp->log(LOGINFO, "No free pass for our own bounce, this is number $pheader->{bounce_seen} for this message"); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | $qp->log(LOGDEBUG, "RX MATCHED ($1)") if $1; | ||
| 261 | |||
| 262 | # | ||
| 263 | # Check to see if any of the recipients have set options to control | ||
| 264 | # their mail. | ||
| 265 | # | ||
| 266 | |||
| 267 | my $recipients_modified = 0; | ||
| 268 | my (@nr); | ||
| 269 | my $counter = 0; | ||
| 270 | my %remap_from; | ||
| 271 | for my $r (@recipients) { | ||
| 272 | my $rd; | ||
| 273 | if (($rd = $qd->{recipients}{$r}) && $rd->{action}) { | ||
| 274 | if ($rd->{action} eq 'drop') { | ||
| 275 | $qp->log(LOGDEBUG, "Recipient '$r': drop"); | ||
| 276 | # skip this one... | ||
| 277 | } elsif ($rd->{action} eq 'forward' && $counter < 10) { | ||
| 278 | my ($nap) = Mail::Address->parse($rd->{new_address}); | ||
| 279 | if ($nap && $nap->address) { | ||
| 280 | $qp->log(LOGDEBUG, "Recipient '$r': redirect to ".$nap->address); | ||
| 281 | $remap_from{$nap->address} = $r; | ||
| 282 | $r = $nap->address; | ||
| 283 | $counter++; | ||
| 284 | redo; | ||
| 285 | } else { | ||
| 286 | $qp->log(LOGWARN, "Could not parse forward address for $r: '$rd->{new_address}'"); | ||
| 287 | push(@nr, $r); | ||
| 288 | } | ||
| 289 | } else { | ||
| 290 | $qp->log(LOGWARN, "Bogus action for $r: '$rd->{action}'"); | ||
| 291 | push(@nr, $r); | ||
| 292 | } | ||
| 293 | } else { | ||
| 294 | $qp->log(LOGDEBUG, "No special action for recipient '$r'"); | ||
| 295 | push(@nr, $r); | ||
| 296 | } | ||
| 297 | $counter = 0; | ||
| 298 | } | ||
| 299 | @recipients = @nr; | ||
| 300 | |||
| 301 | # | ||
| 302 | # Let's figure out which recipients get notified, which get protected, | ||
| 303 | # and which get a message. | ||
| 304 | # | ||
| 305 | |||
| 306 | my @passthrough_recipients; # not quarantined | ||
| 307 | my @quarantine_recipients; # quarantined and they know it | ||
| 308 | my @filter_recipients; # quarantined but they don't know it | ||
| 309 | my @queued_messages; | ||
| 310 | for my $r (@recipients) { | ||
| 311 | my $rd = $qd->{recipients}{$r}; | ||
| 312 | my ($ra) = Mail::Address->parse($r); | ||
| 313 | die unless ref($ra); | ||
| 314 | my $rdomain = $ra->host; | ||
| 315 | if (! $rd && ! match_domain($qp, $rdomain, $qa->{filter_domains}, 'filter_domains')) { | ||
| 316 | push(@passthrough_recipients, $r); | ||
| 317 | next; | ||
| 318 | } | ||
| 319 | $rd = new_recipient($oops, $r) | ||
| 320 | unless $rd; | ||
| 321 | $rd->{mcount} += 1; | ||
| 322 | $rd->{total_count} += 1; | ||
| 323 | |||
| 324 | my $do_qr = 0; | ||
| 325 | if ($rd->{total_count} >= $defaults{notify_recipients}) { | ||
| 326 | $do_qr = "Recipient $r has is over the threshold ($rd->{total_count}): let the recipient choose"; | ||
| 327 | } else { | ||
| 328 | $qp->log(LOGDEBUG, "Recipient $r is under the threshold ($rd->{total_count})"); | ||
| 329 | } | ||
| 330 | if (! $do_qr and $qa->{notify_recipient_only}{lc($r)}) { | ||
| 331 | $do_qr = "Recipient is in the special list: let the recipient choose"; | ||
| 332 | } else { | ||
| 333 | $qp->log(LOGDEBUG, "Lookup of notify_recipient_only{$r} = nada"); | ||
| 334 | } | ||
| 335 | if ($do_qr) { | ||
| 336 | $qp->log(LOGDEBUG, $do_qr); | ||
| 337 | if ($time - $rd->{last_timestamp} > 86400 * $defaults{renotify_recipient_days}) { | ||
| 338 | push(@queued_messages, send_recipient_notification($qp, $transaction, $r, $rd, $qd)) | ||
| 339 | } else { | ||
| 340 | $qp->log(LOGDEBUG, "Not yet time to bug recipient $r"); | ||
| 341 | } | ||
| 342 | push(@quarantine_recipients, $r); | ||
| 343 | } else { | ||
| 344 | push(@filter_recipients, $r); | ||
| 345 | } | ||
| 346 | $rd->{last_timestamp} = $time; | ||
| 347 | } | ||
| 348 | |||
| 349 | if (@passthrough_recipients == 1 && @quarantine_recipients == 0 && @filter_recipients == 0 && $remap_from{$passthrough_recipients[0]}) { | ||
| 350 | $headers->replace('X-Mail-Redirected-From', "$remap_from{$passthrough_recipients[0]} on $myhostname"); | ||
| 351 | $transaction->header($headers); | ||
| 352 | } | ||
| 353 | |||
| 354 | my $notify_recipients_only = @quarantine_recipients && ! @filter_recipients; | ||
| 355 | |||
| 356 | # | ||
| 357 | # Basic sender tracking | ||
| 358 | # | ||
| 359 | |||
| 360 | $psender = new_sender($oops, $sender_address, $transaction->sender->address(), $sender_token) | ||
| 361 | unless $psender; | ||
| 362 | my $sender_ip_last_used = $psender->{send_ip_used}{$ip} || 0; | ||
| 363 | |||
| 364 | # | ||
| 365 | # What are we doing? | ||
| 366 | # | ||
| 367 | |||
| 368 | my $reply = ''; | ||
| 369 | my $noteOK = ''; | ||
| 370 | my $noteDENY = ''; | ||
| 371 | |||
| 372 | my $sender_okay = | ||
| 373 | ( | ||
| 374 | spf($qp, $transaction) eq 'pass' | ||
| 375 | && | ||
| 376 | ($noteOK = 'sender-SPF-passed') | ||
| 377 | ) | ||
| 378 | || | ||
| 379 | ( | ||
| 380 | ( | ||
| 381 | ( | ||
| 382 | $transaction->notes('origin_type') eq 'internal' | ||
| 383 | && | ||
| 384 | ($noteOK = 'from-one-of-our-ip-addresses') | ||
| 385 | ) | ||
| 386 | || | ||
| 387 | ( | ||
| 388 | $transaction->notes('from_our_domain') | ||
| 389 | && | ||
| 390 | ($noteOK = 'from-one-of-our-domains') | ||
| 391 | ) | ||
| 392 | ) | ||
| 393 | && | ||
| 394 | ! ( | ||
| 395 | spf($qp, $transaction) eq 'fail' | ||
| 396 | && | ||
| 397 | ($noteDENY = 'sender-SPF-failed') | ||
| 398 | ) | ||
| 399 | ) | ||
| 400 | || | ||
| 401 | ( | ||
| 402 | ( | ||
| 403 | $defaults{notify_other_senders} | ||
| 404 | && | ||
| 405 | ($noteOK = 'notify-external-senders') | ||
| 406 | ) | ||
| 407 | && | ||
| 408 | ! ( | ||
| 409 | spf($qp, $transaction) eq 'fail' | ||
| 410 | && | ||
| 411 | ($noteDENY = 'sender-SPF-failed') | ||
| 412 | ) | ||
| 413 | ) | ||
| 414 | ; | ||
| 415 | |||
| 416 | my $no_recipient_bounce_body = | ||
| 417 | ! @recipients | ||
| 418 | && | ||
| 419 | $body =~ m{$fmes3rx} | ||
| 420 | && | ||
| 421 | ($reply = 'message will be discarded as useless bounce') | ||
| 422 | ; | ||
| 423 | |||
| 424 | my $sender_bounce = $sender_okay; | ||
| 425 | $sender_bounce = 0 | ||
| 426 | if | ||
| 427 | ( | ||
| 428 | $transaction->sender() eq "<>" | ||
| 429 | && | ||
| 430 | ($reply = 'no bounces for MAILER-DAEMON') | ||
| 431 | ) | ||
| 432 | || | ||
| 433 | ( | ||
| 434 | $psender->{action} | ||
| 435 | && | ||
| 436 | $psender->{action} eq 'discard' | ||
| 437 | && | ||
| 438 | ($reply = "sender doesn't want to know") | ||
| 439 | ) | ||
| 440 | || | ||
| 441 | ( | ||
| 442 | $notify_recipients_only | ||
| 443 | && | ||
| 444 | ($reply = 'recipients will be notified instead') | ||
| 445 | ) | ||
| 446 | || | ||
| 447 | $no_recipient_bounce_body | ||
| 448 | ; | ||
| 449 | |||
| 450 | my $sender_quarantine = $sender_bounce; | ||
| 451 | $sender_quarantine = 0 | ||
| 452 | if | ||
| 453 | ( | ||
| 454 | $psender->{action} | ||
| 455 | && | ||
| 456 | $psender->{action} eq 'bounce' | ||
| 457 | && | ||
| 458 | ($reply = "Bounced due to your request at $defaults{baseurl}") | ||
| 459 | ) | ||
| 460 | || | ||
| 461 | ( | ||
| 462 | ! @recipients | ||
| 463 | && | ||
| 464 | ($reply = 'No Recipients Want Spammy Messages') | ||
| 465 | ) | ||
| 466 | ; | ||
| 467 | |||
| 468 | |||
| 469 | my $do_quarantine = | ||
| 470 | ( | ||
| 471 | ( | ||
| 472 | $sender_quarantine | ||
| 473 | && | ||
| 474 | @filter_recipients | ||
| 475 | ) | ||
| 476 | || | ||
| 477 | @quarantine_recipients | ||
| 478 | ) | ||
| 479 | ; | ||
| 480 | |||
| 481 | $sender_bounce = 0 | ||
| 482 | if | ||
| 483 | ( | ||
| 484 | $sender_bounce | ||
| 485 | && | ||
| 486 | $time - $sender_ip_last_used < 86400 * ($psender->{renotify_days} || $defaults{renotify_sender_ip}) | ||
| 487 | && | ||
| 488 | ($reply = 'not yet time for another bounce') | ||
| 489 | ) | ||
| 490 | ; | ||
| 491 | |||
| 492 | # | ||
| 493 | # Sender tracking. Track how many spams are sent and save one every now and then. | ||
| 494 | # | ||
| 495 | |||
| 496 | if ($sender_bounce) { | ||
| 497 | $psender->{send_ip_used}{$ip} = $time; | ||
| 498 | } | ||
| 499 | my $today = $time / 86400; | ||
| 500 | my $spams_sent = 0; | ||
| 501 | $psender->{spams_sent_perday}{$today} += 1; | ||
| 502 | for my $spamday ($psender->{spams_sent_perday}) { | ||
| 503 | next if $today - $spamday > $defaults{sender_history_to_keep}; | ||
| 504 | $spams_sent += $psender->{spams_sent_perday}; | ||
| 505 | } | ||
| 506 | if ($defaults{keep_every_nth_message} && ($spams_sent % $defaults{keep_every_nth_message}) == 0) { | ||
| 507 | $do_quarantine = 1; | ||
| 508 | } | ||
| 509 | $psender->{last_message} = $time; | ||
| 510 | |||
| 511 | # | ||
| 512 | # We only need to save the message if it's interesting. | ||
| 513 | # | ||
| 514 | if ($do_quarantine) { | ||
| 515 | my $pbody = $qd->{bodies}{$body_checksum}; | ||
| 516 | unless ($pbody) { | ||
| 517 | $pbody = $qd->{bodies}{$body_checksum} = bless { | ||
| 518 | body => $body, | ||
| 519 | cksum => $body_checksum, | ||
| 520 | size => length($body), | ||
| 521 | }, 'Quarantine::Body'; | ||
| 522 | $qd->{diskused}{$$ % $defaults{size_storage_array_size}} | ||
| 523 | += length($body) + $defaults{message_size_overhead}; | ||
| 524 | } | ||
| 525 | my @recip = $sender_quarantine | ||
| 526 | ? @recipients | ||
| 527 | : (@filter_recipients, @quarantine_recipients); | ||
| 528 | |||
| 529 | my $pheader = $qd->{headers}{$header_checksum} = bless { | ||
| 530 | from => $headers->get('From'), | ||
| 531 | to => $headers->get('To'), | ||
| 532 | subject => $headers->get('Subject'), | ||
| 533 | date => $headers->get('Date'), | ||
| 534 | time => $time, | ||
| 535 | sender => $psender, | ||
| 536 | recipients => (bless [ @recip ], 'Quarantine::RecipientList'), | ||
| 537 | header => $header, | ||
| 538 | body => $pbody, | ||
| 539 | cksum => $header_checksum, | ||
| 540 | }, 'Quarantine::Header'; | ||
| 541 | $pbody->{last_reference} = $pheader; | ||
| 542 | $psender->{headers}{$header_checksum} = $pheader; | ||
| 543 | unless ($qd->{buckets3}) { | ||
| 544 | $qd->{buckets3} = bless {}, 'Quarantine::Buckets'; | ||
| 545 | } | ||
| 546 | $qd->{buckets3}{int($time / 86400)}{int(($time % 86400) / 3600)}{$header_checksum} = $pheader; | ||
| 547 | $oops->virtual_object($qd->{buckets3}{int($time / 86400)}, 1); | ||
| 548 | $oops->virtual_object($qd->{buckets3}{int($time / 86400)}{int(($time % 86400) / 3600)}, 1); | ||
| 549 | |||
| 550 | for my $r (@quarantine_recipients, @filter_recipients) { | ||
| 551 | my $rd = $qd->{recipients}{$r}; | ||
| 552 | $rd->{headers}{$header_checksum} = $pheader; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | # | ||
| 557 | # Some debugging | ||
| 558 | # | ||
| 559 | |||
| 560 | $qp->log(LOGDEBUG, "recipients = @recipients"); | ||
| 561 | $qp->log(LOGDEBUG, "filter_recipients = @filter_recipients"); | ||
| 562 | $qp->log(LOGDEBUG, "quarantine_recipients = @quarantine_recipients"); | ||
| 563 | $qp->log(LOGDEBUG, "passthrough_recipients = @passthrough_recipients"); | ||
| 564 | $qp->log(LOGDEBUG, "notify_recipients_only = $notify_recipients_only"); | ||
| 565 | $qp->log(LOGDEBUG, "reply = $reply"); | ||
| 566 | $qp->log(LOGDEBUG, "noteOK = $noteOK"); | ||
| 567 | $qp->log(LOGDEBUG, "noteDENY = $noteDENY"); | ||
| 568 | $qp->log(LOGDEBUG, "sender_okay = $sender_okay"); | ||
| 569 | $qp->log(LOGDEBUG, "no_recipient_bounce_body = $no_recipient_bounce_body"); | ||
| 570 | $qp->log(LOGDEBUG, "sender_bounce = $sender_bounce"); | ||
| 571 | $qp->log(LOGDEBUG, "sender_quarantine = $sender_quarantine"); | ||
| 572 | $qp->log(LOGDEBUG, "do_quarantine = $do_quarantine"); | ||
| 573 | |||
| 574 | # | ||
| 575 | # Do it. | ||
| 576 | # | ||
| 577 | |||
| 578 | $oops->commit(); | ||
| 579 | |||
| 580 | my $messageid = $headers->get('Message-ID'); | ||
| 581 | $messageid =~ s/[\r\n]+\z//; | ||
| 582 | |||
| 583 | my (@new_recip); | ||
| 584 | for my $r (@passthrough_recipients) { | ||
| 585 | push(@new_recip, Qpsmtpd::Address->new($r)); | ||
| 586 | } | ||
| 587 | @new_recip = Qpsmtpd::Address->new($defaults{nobody_address}) | ||
| 588 | unless @new_recip; | ||
| 589 | |||
| 590 | if ($do_quarantine && $sender_quarantine) { | ||
| 591 | my $and_recipients = @queued_messages ? ", Recipients Notified" : ""; | ||
| 592 | $qp->log(LOGINFO, "QRESULT: Message quarantined, sender notified$and_recipients - $noteOK - $messageid"); | ||
| 593 | @retcode = Qpsmtpd::DSN->mbox_disabled(DENY, "$fmes1 $defaults{baseurl}/message/$header_checksum $fmes2"); | ||
| 594 | } elsif ($sender_bounce && ! @recipients) { | ||
| 595 | $qp->log(LOGINFO, "QRESULT: Message bounced - no recipients - $noteOK - $messageid"); | ||
| 596 | @retcode = Qpsmtpd::DSN->mbox_disabled(DENY, $defaults{senderbounce3}); | ||
| 597 | } elsif ($do_quarantine && @quarantine_recipients && @passthrough_recipients) { | ||
| 598 | $qp->log(LOGINFO, "QRESULT: Message quarantined silently; some recipients notified, some passthrough - $noteOK $noteDENY - $messageid"); | ||
| 599 | $transaction->recipients(@new_recip); | ||
| 600 | @retcode = DECLINED; | ||
| 601 | } elsif ($do_quarantine && @quarantine_recipients) { | ||
| 602 | $qp->log(LOGINFO, "QRESULT: Message quarantined silently, recipients notified - $noteOK $noteDENY - $messageid"); | ||
| 603 | $transaction->recipients(@new_recip); | ||
| 604 | @retcode = DECLINED; | ||
| 605 | } elsif ($do_quarantine && @passthrough_recipients) { | ||
| 606 | # is this possible? | ||
| 607 | $qp->log(LOGINFO, "QRESULT: Message quarantined silently, some recipients passthrough $noteOK $noteDENY - $messageid"); | ||
| 608 | $transaction->recipients(@new_recip); | ||
| 609 | @retcode = DECLINED; | ||
| 610 | } elsif ($do_quarantine) { | ||
| 611 | # is this possible? | ||
| 612 | $qp->log(LOGINFO, "QRESULT: Message quarantined silently $noteOK $noteDENY - $messageid"); | ||
| 613 | $transaction->recipients(@new_recip); | ||
| 614 | @retcode = DECLINED; | ||
| 615 | } elsif (@passthrough_recipients && ! @filter_recipients && ! @quarantine_recipients) { | ||
| 616 | $qp->log(LOGINFO, "QRESULT: Messages allowed $noteOK $noteDENY - $messageid"); | ||
| 617 | $transaction->recipients(@new_recip); | ||
| 618 | @retcode = DECLINED; | ||
| 619 | } elsif ($sender_bounce) { | ||
| 620 | # | ||
| 621 | # it doesn't matter if there were some passthrough recipients, we're | ||
| 622 | # bouncing it back to the sender 'cause it looked spammy. | ||
| 623 | # | ||
| 624 | $qp->log(LOGINFO, "QRESULT: Message bounced $reply - $noteOK - $messageid"); | ||
| 625 | @retcode = Qpsmtpd::DSN->mbox_disabled(DENY, $reply || "Spammy message rejected. See $defaults{baseurl} for options"); | ||
| 626 | } elsif (@passthrough_recipients) { | ||
| 627 | $qp->log(LOGINFO, "QRESULT: Message passed through $noteOK $noteDENY - $messageid"); | ||
| 628 | $transaction->recipients(@new_recip); | ||
| 629 | @retcode = DECLINED; | ||
| 630 | } else { | ||
| 631 | $qp->log(LOGINFO, "QRESULT: Message discarded silently $reply $noteOK $noteDENY - $messageid"); | ||
| 632 | $transaction->recipients(Qpsmtpd::Address->new($defaults{nobody_address})); | ||
| 633 | @retcode = DECLINED; | ||
| 634 | } | ||
| 635 | |||
| 636 | if ($do_quarantine) { | ||
| 637 | for my $qm (@queued_messages) { | ||
| 638 | $qp->log(LOGDEBUG, "Sending message to $qm->{recipient}:\n$qm->{message}"); | ||
| 639 | sendmail_or_queue( | ||
| 640 | from => $defaults{send_from}, | ||
| 641 | to => $qm->{recipient}, | ||
| 642 | message => $qm->{message}, | ||
| 643 | debuglogger => sub { $qp->log(LOGDEBUG, @_) }, | ||
| 644 | errorlogger => sub { $qp->log(LOGINFO, @_) }, | ||
| 645 | ); | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | return 1; | ||
| 650 | }) or return DECLINED; | ||
| 651 | $qp->log(LOGDEBUG, "quarantine returning: @retcode"); | ||
| 652 | return @retcode; | ||
| 653 | } | ||
| 654 | |||
| 655 | sub initialize | ||
| 656 | { | ||
| 657 | my ($qp, $oops, $transaction) = @_; | ||
| 658 | require Data::Dumper; | ||
| 659 | # my $qa = $qp->{_quarantine}; | ||
| 660 | die unless $qa->{our_networks}; | ||
| 661 | my $qd = $oops->{quarantine} = bless { | ||
| 662 | senders => (bless {}, 'Quarantine::Senders'), | ||
| 663 | headers => (bless {}, 'Quarantine::Headers'), | ||
| 664 | bodies => (bless {}, 'Quarantine::Bodies'), | ||
| 665 | buckets3 => (bless {}, 'Quarantine::Buckets'), | ||
| 666 | recipients => (bless {}, 'Quarantine::Recipients'), | ||
| 667 | mqueue => (bless {}, 'Quarantine::MailQueue'), | ||
| 668 | diskused => (bless {}, 'Quarantine::DiskUsage'), | ||
| 669 | version => $VERSION, | ||
| 670 | }, 'Quarantine::Top'; | ||
| 671 | $oops->virtual_object($qd->{headers}, 1); | ||
| 672 | $oops->virtual_object($qd->{bodies}, 1); | ||
| 673 | $oops->virtual_object($qd->{senders}, 1); | ||
| 674 | $oops->virtual_object($qd->{recipients}, 1); | ||
| 675 | $oops->virtual_object($qd->{mqueue}, 1); | ||
| 676 | $oops->virtual_object($qd->{diskused}, 1); | ||
| 677 | |||
| 678 | # make a pseudo-random token | ||
| 679 | my $header_checksum = md5_hex($transaction->header()->as_string()); | ||
| 680 | my $time = Time::HiRes::time(); | ||
| 681 | my $stuff1 = "$$.$header_checksum.$time."; | ||
| 682 | my $stuff2 = join('?',values(%$qa)); | ||
| 683 | |||
| 684 | |||
| 685 | my $stuff3; | ||
| 686 | open(RANDOM, "/dev/random") || next; | ||
| 687 | read(RANDOM, $stuff3, 32, 0); | ||
| 688 | close(RANDOM); | ||
| 689 | |||
| 690 | $qd->{random_token} = md5_hex($stuff1).md5_hex($stuff2).md5_hex($stuff3); | ||
| 691 | |||
| 692 | $qp->log(LOGWARN, "Quarantine data structures initialized"); | ||
| 693 | |||
| 694 | return $qd; | ||
| 695 | } | ||
| 696 | |||
| 697 | sub send_recipient_notification | ||
| 698 | { | ||
| 699 | my ($qp, $transaction, $r, $rd, $qd) = @_; | ||
| 700 | my $headers = $transaction->header(); | ||
| 701 | my $buf; | ||
| 702 | my (undef, $domain) = split('@', $r); | ||
| 703 | $qa->{template}->process('recipient-notification.mail', { | ||
| 704 | config => \%defaults, | ||
| 705 | recipient => $r, | ||
| 706 | mcount => $rd->{mcount}, | ||
| 707 | domain => $domain, | ||
| 708 | headers => $headers, | ||
| 709 | sender => $transaction->sender(), | ||
| 710 | recipient_url => $rd->url($qd), | ||
| 711 | now => scalar(localtime(time)), | ||
| 712 | }, \$buf); | ||
| 713 | $buf =~ s/\A\s*//s; | ||
| 714 | return { | ||
| 715 | recipient => $r, | ||
| 716 | message => $buf, | ||
| 717 | }; | ||
| 718 | } | ||
| 719 | |||
| 720 | sub get_message_info | ||
| 721 | { | ||
| 722 | my ($qp, $transaction) = @_; | ||
| 723 | |||
| 724 | # my $qa = $qp->{_quarantine} = {}; | ||
| 725 | die unless $qa->{our_networks}; | ||
| 726 | my $headers = $transaction->header(); | ||
| 727 | |||
| 728 | if (0 && $debug) { | ||
| 729 | $qp->log(LOGDEBUG, "Our stuff..."); | ||
| 730 | for my $i (sort keys %{$qa->{our_domains}}) { | ||
| 731 | $qp->log(LOGDEBUG, "Our domain: $i"); | ||
| 732 | } | ||
| 733 | for my $i (sort keys %{$qa->{filter_domains}}) { | ||
| 734 | $qp->log(LOGDEBUG, "Filter domain: $i"); | ||
| 735 | } | ||
| 736 | for my $i (dumpNetworkTable($qa->{our_networks})) { | ||
| 737 | $qp->log(LOGDEBUG, "Our netblock: $i"); | ||
| 738 | } | ||
| 739 | for my $i (dumpNetworkTable($qa->{ignore_networks})) { | ||
| 740 | $qp->log(LOGDEBUG, "Ignore netblock: $i"); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | |||
| 744 | unless ($transaction->notes('origin_type')) { | ||
| 745 | my $external_ip; | ||
| 746 | my $internal_ip; | ||
| 747 | for my $received_line ($headers->get('Received')) { | ||
| 748 | # $qp->log(LOGDEBUG, "Processing Received line: $received_line"); | ||
| 749 | my $received = Mail::Field->new('Received', $received_line); | ||
| 750 | if ($received->parsed_ok()) { | ||
| 751 | my $pt = $received->parse_tree(); | ||
| 752 | my $ip = $pt->{from}{address}; | ||
| 753 | my $our_block = findNetblock($ip, $qa->{our_networks}); | ||
| 754 | if ($our_block) { | ||
| 755 | $internal_ip = $ip; | ||
| 756 | $qp->log(LOGDEBUG, "Found an internal address: $ip, will see if there's more..."); | ||
| 757 | next; | ||
| 758 | } | ||
| 759 | my $ignore_block = findNetblock($ip, $qa->{ignore_networks}); | ||
| 760 | if ($ignore_block) { | ||
| 761 | $qp->log(LOGDEBUG, "IP address should be ignored ($ip)"); | ||
| 762 | next; | ||
| 763 | } | ||
| 764 | unless ($ip) { | ||
| 765 | $qp->log(LOGDEBUG, "No IP address found in: $received_line"); | ||
| 766 | next; | ||
| 767 | } | ||
| 768 | $qp->log(LOGDEBUG, "Looked for $ip, but isn't one of ours"); | ||
| 769 | $external_ip = $ip; | ||
| 770 | # $qp->log(LOGDEBUG, "sender host:", $transaction->sender->host()); | ||
| 771 | # $qp->log(LOGDEBUG, "sender user:", $transaction->sender->user()); | ||
| 772 | $qp->log(LOGDEBUG, "sender address:", $transaction->sender->address()); | ||
| 773 | |||
| 774 | $transaction->notes( | ||
| 775 | entry_ip => $ip, | ||
| 776 | entry_helo => $pt->{from}{HELO}, | ||
| 777 | entry_domain => $pt->{by}{domain}, | ||
| 778 | ); | ||
| 779 | |||
| 780 | last; | ||
| 781 | } else { | ||
| 782 | $qp->log(LOGWARN, "Mail::Field::Received failed: ", $received->diagnostics()); | ||
| 783 | last; | ||
| 784 | } | ||
| 785 | } | ||
| 786 | |||
| 787 | if ($external_ip) { | ||
| 788 | $transaction->notes(external_ip => $external_ip); | ||
| 789 | $transaction->notes(origin_type => 'external'); | ||
| 790 | $qp->log(LOGDEBUG, "External IP origin: $external_ip"); | ||
| 791 | } else { | ||
| 792 | $transaction->notes(origin_type => 'internal'); | ||
| 793 | $qp->log(LOGDEBUG, "Internal IP origin: $internal_ip"); | ||
| 794 | } | ||
| 795 | } | ||
| 796 | |||
| 797 | unless (defined $transaction->notes('from_our_domain')) { | ||
| 798 | my $sdomain = $transaction->sender->host(); | ||
| 799 | my $from_our_domain = match_domain($qp, $sdomain, $qa->{our_domains}, 'our_domains'); | ||
| 800 | $transaction->notes(from_our_domain => $from_our_domain); | ||
| 801 | $qp->log(LOGDEBUG, "From our domain: $from_our_domain ($sdomain)"); | ||
| 802 | } | ||
| 803 | |||
| 804 | unless (defined $transaction->notes('filtered_recipient_count')) { | ||
| 805 | my @rdomains = map { $_->host() } $transaction->recipients(); | ||
| 806 | my $filter_count = 0; | ||
| 807 | for my $rd (@rdomains) { | ||
| 808 | $filter_count++ if match_domain($qp, $rd, $qa->{filter_domains}, 'filter_domains'); | ||
| 809 | } | ||
| 810 | $transaction->notes(filtered_recipient_count => $filter_count); | ||
| 811 | $qp->log(LOGDEBUG, "Filtered Recipient Count: $filter_count"); | ||
| 812 | } | ||
| 813 | } | ||
| 814 | |||
| 815 | |||
| 816 | sub spf | ||
| 817 | { | ||
| 818 | my ($qp, $transaction) = @_; | ||
| 819 | |||
| 820 | my $done = $transaction->notes('spf_result'); | ||
| 821 | return $done if $done; | ||
| 822 | |||
| 823 | get_message_info($qp, $transaction) | ||
| 824 | unless $transaction->notes('origin_type'); | ||
| 825 | |||
| 826 | return '' | ||
| 827 | unless $transaction->notes('origin_type') eq 'external'; | ||
| 828 | |||
| 829 | my $ip = $transaction->notes('entry_ip'); | ||
| 830 | my $sender = $transaction->sender->address(); | ||
| 831 | my $entry_helo = $transaction->notes('entry_helo'); | ||
| 832 | $qp->log(LOGDEBUG, "SPF Query with ip=$ip, sender=$sender, helo='$entry_helo'"); | ||
| 833 | my $spf = new Mail::SPF::Query ( | ||
| 834 | ip => $ip, | ||
| 835 | sender => $sender, | ||
| 836 | helo => $entry_helo, | ||
| 837 | myhostname => ($transaction->notes('entry_domain') || $myhostname), | ||
| 838 | debug => 0, | ||
| 839 | debuglog => sub { $qp->log(LOGDEBUG, @_) }, | ||
| 840 | trusted => 1, | ||
| 841 | guess => 1, | ||
| 842 | sanitize => 0, | ||
| 843 | ); | ||
| 844 | my $result = ($spf->result())[0]; | ||
| 845 | $qp->log(LOGDEBUG, "SPF results: $result"); | ||
| 846 | $transaction->notes(spf_result => $result); | ||
| 847 | return $result; | ||
| 848 | } | ||
| 849 | |||
| 850 | sub match_domain | ||
| 851 | { | ||
| 852 | my ($qp, $domain, $hashref, $what) = @_; | ||
| 853 | while ($domain) { | ||
| 854 | $qp->log(LOGDEBUG, "Trying to match $domain for $what"); | ||
| 855 | if ($hashref->{$domain}) { | ||
| 856 | return 1; | ||
| 857 | } | ||
| 858 | $domain =~ s/^[^\.]+// or last; | ||
| 859 | $domain =~ s/^\.//; | ||
| 860 | } | ||
| 861 | return 0; | ||
| 862 | } | ||
| 863 |