| 1 | # Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License along | ||
| 14 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 15 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 16 | # | ||
| 17 | # This software is available without the GPL: please write if you need | ||
| 18 | # a non-GPL license. All submissions of patches must come with a | ||
| 19 | # copyright grant so that David Sharnoff remains able to change the | ||
| 20 | # license at will. | ||
| 21 | |||
| 22 | package Qpsmtpd::Plugin::Quarantine::CGI; | ||
| 23 | |||
| 24 | use CGI qw(); | ||
| 25 | use CGI::Cookie; | ||
| 26 | use Carp qw(longmess); | ||
| 27 | use OOPS; | ||
| 28 | use File::Slurp; | ||
| 29 | use Data::Dumper; | ||
| 30 | use strict; | ||
| 31 | use Template; | ||
| 32 | use Net::SMTP; | ||
| 33 | use Scalar::Util qw(refaddr); | ||
| 34 | use Digest::MD5 qw(md5_hex); | ||
| 35 | use Time::CTime; | ||
| 36 | use Time::ParseDate; | ||
| 37 | use Qpsmtpd::Plugin::Quarantine::Common; | ||
| 38 | use Qpsmtpd::Plugin::Quarantine::Sendmail; | ||
| 39 | use Mail::SendVarious; | ||
| 40 | |||
| 41 | our @ISA = qw(Exporter); | ||
| 42 | our @EXPORT = qw(main); | ||
| 43 | |||
| 44 | require Exporter; | ||
| 45 | |||
| 46 | use strict; | ||
| 47 | |||
| 48 | my $template; | ||
| 49 | |||
| 50 | my $random_token; | ||
| 51 | |||
| 52 | my $cgi = new CGI; | ||
| 53 | my $md5rx = qr/[a-f0-9]{32}/; | ||
| 54 | my $pi; | ||
| 55 | my $debug = 1; | ||
| 56 | |||
| 57 | my $filtered_domains; | ||
| 58 | |||
| 59 | my $cookie_time_fmt = "%A, %d-%b-%Y %X GMT"; | ||
| 60 | |||
| 61 | sub error { | ||
| 62 | local($Carp::CarpLevel) = 1; | ||
| 63 | print STDERR longmess(@_); | ||
| 64 | die "\n"; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub main | ||
| 68 | { | ||
| 69 | $filtered_domains = "$defaults{qpsmtpd_dir}/filter_domains"; | ||
| 70 | |||
| 71 | $cgi = new CGI; | ||
| 72 | |||
| 73 | $template = Template->new({ | ||
| 74 | INCLUDE_PATH => $defaults{templates}, | ||
| 75 | INTERPOLATE => 1, | ||
| 76 | POST_CHOMP => 0, | ||
| 77 | EVAL_PERL => 1, | ||
| 78 | RECURSION => 1, | ||
| 79 | }) || error Template->error(); | ||
| 80 | |||
| 81 | $pi = $cgi->path_info(); | ||
| 82 | |||
| 83 | transaction(sub { | ||
| 84 | my $oops = get_oops(); | ||
| 85 | $random_token = $oops->{quarantine}{random_token}; | ||
| 86 | if ($pi =~ m{^/message/($md5rx)$}) { | ||
| 87 | handle_message($oops,$1); | ||
| 88 | } elsif ($pi =~ m{^/sender/($md5rx)/(.*)$}i) { | ||
| 89 | handle_sender($oops, $2, $1); | ||
| 90 | } elsif ($pi =~ m{^/recipient/($md5rx)/(.+)$}i) { | ||
| 91 | handle_recipient($oops, $2, $1); | ||
| 92 | } elsif ($pi =~ m{^/recipient/(.*)$}i) { | ||
| 93 | handle_unauthorized_recipient($oops, $1); | ||
| 94 | } elsif ($pi =~ m{^/admin}) { | ||
| 95 | handle_admin($oops); | ||
| 96 | } else { | ||
| 97 | print $cgi->header(); | ||
| 98 | $template->process('main-menu.tt2', { cgi => $cgi, config => \%defaults}) || error $Template::ERROR; | ||
| 99 | } | ||
| 100 | }); | ||
| 101 | print STDERR "E=$@\n" if $@; | ||
| 102 | error $@ if $@; | ||
| 103 | |||
| 104 | send_postponed(); | ||
| 105 | print STDERR "Done\n"; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub handle_unauthorized_sender | ||
| 109 | { | ||
| 110 | my ($oops, $sender_encoded) = @_; | ||
| 111 | |||
| 112 | print $cgi->header(); | ||
| 113 | |||
| 114 | my $qd = $oops->{quarantine} || error; | ||
| 115 | |||
| 116 | my $sender = CGI::unescape($sender_encoded) || $cgi->param('sender'); | ||
| 117 | |||
| 118 | my $action = $cgi->param('action') || ''; | ||
| 119 | |||
| 120 | my $psender = $qd->{senders}{$sender}; | ||
| 121 | my $token = $psender && $psender->{token}; | ||
| 122 | |||
| 123 | if ($action eq $defaults{button_sender_url} && $sender =~ /^([^\@]+)\@([^\@]+)$/) { | ||
| 124 | my $buf; | ||
| 125 | # sender may not exist yet | ||
| 126 | if ($psender) { | ||
| 127 | $token = md5_hex($$ . Time::HiRes::time() . $qd->{random_token} . $sender); | ||
| 128 | $psender->{token} = $token; | ||
| 129 | print STDERR "New token for $sender: $token\n"; | ||
| 130 | } else { | ||
| 131 | $psender = new_sender($oops, $1, $sender); | ||
| 132 | $token = $psender->{token}; | ||
| 133 | print STDERR "First token for $sender: $token\n"; | ||
| 134 | } | ||
| 135 | $oops->commit(); | ||
| 136 | $template->process('sender-notification.mail', { | ||
| 137 | config => \%defaults, | ||
| 138 | sender => $sender, | ||
| 139 | request_ip => $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST}, | ||
| 140 | sender_url => $psender->url(), | ||
| 141 | now => scalar(localtime(time)), | ||
| 142 | }, \$buf) || error $Template::ERROR; | ||
| 143 | print STDERR "Message to send to $sender:\n$buf" if $debug; | ||
| 144 | sendmail_or_postpone(from => $defaults{send_from}, to => $sender, message => $buf); | ||
| 145 | $template->process('sender-access-url-sent.tt2', { | ||
| 146 | config => \%defaults, | ||
| 147 | sender => $sender, | ||
| 148 | }) || error $Template::ERROR; | ||
| 149 | } else { | ||
| 150 | $template->process('unauthorized-sender.tt2', { | ||
| 151 | config => \%defaults, | ||
| 152 | sender => $sender | ||
| 153 | }) || error $Template::ERROR; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub handle_sender | ||
| 158 | { | ||
| 159 | my ($oops, $sender_encoded, $sender_checksum) = @_; | ||
| 160 | |||
| 161 | print $cgi->header(); | ||
| 162 | |||
| 163 | my $qd = $oops->{quarantine} || error; | ||
| 164 | my $sender = CGI::unescape($sender_encoded); | ||
| 165 | my $action = $cgi->param('action') || ''; | ||
| 166 | my $psender = $qd->{senders}{$sender}; | ||
| 167 | my $docommit = 0; | ||
| 168 | |||
| 169 | unless ($psender) { | ||
| 170 | $psender = new_sender($oops, $sender, $sender); | ||
| 171 | $docommit = 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | my $correct_checksum = $psender->{token}; | ||
| 175 | |||
| 176 | if ($action eq $defaults{button_sender_url}) { | ||
| 177 | return handle_unauthorized_sender($oops, $sender_encoded); | ||
| 178 | } | ||
| 179 | |||
| 180 | if ($sender_checksum ne $correct_checksum) { | ||
| 181 | my %error = (); | ||
| 182 | %error = ( | ||
| 183 | error => 'Your URL is invalid', | ||
| 184 | error_detail => 'The URL you used does not contain the correct authentication tokens for your email address. Please ask for a new authentication URL.', | ||
| 185 | |||
| 186 | ) if $sender; | ||
| 187 | $template->process('unauthorized-sender.tt2', { | ||
| 188 | config => \%defaults, | ||
| 189 | sender => $sender, | ||
| 190 | %error, | ||
| 191 | }) || error $Template::ERROR; | ||
| 192 | print STDERR "CHECKSUM MISMATCH $sender_checksum vs $correct_checksum\n"; | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | my %extra; | ||
| 197 | my %args = ( | ||
| 198 | sender => $sender, | ||
| 199 | psender => $psender, | ||
| 200 | sender_url => $psender->url(), | ||
| 201 | config => \%defaults, | ||
| 202 | otherheaders => [ grep(! $_->{done}, values %{$psender->{headers}}) ], | ||
| 203 | ); | ||
| 204 | |||
| 205 | my ($acommit, $doform, @message) = sender_action($oops, $sender, $psender, $action, \%args, 1); | ||
| 206 | $docommit ||= $acommit; | ||
| 207 | |||
| 208 | $oops->commit() if $docommit; | ||
| 209 | |||
| 210 | $extra{message} = message('Sender', @message); | ||
| 211 | |||
| 212 | if ($doform) { | ||
| 213 | $template->process('sender-menu.tt2', { | ||
| 214 | %extra, | ||
| 215 | %args, | ||
| 216 | }) || error $Template::ERROR; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | sub sender_action | ||
| 221 | { | ||
| 222 | my ($oops, $sender, $psender, $action, $args, $canshow) = @_; | ||
| 223 | |||
| 224 | |||
| 225 | my $qd = $oops->{quarantine} || error; | ||
| 226 | my $docommit = 0; | ||
| 227 | my $doform = 1; | ||
| 228 | my @message; | ||
| 229 | if ($action eq $defaults{button_sender_update}) { | ||
| 230 | my $na = $cgi->param('new_action'); | ||
| 231 | my $bf = $cgi->param('renotify_days'); | ||
| 232 | if ($na ne ($psender->{action} || 'quarantine')) { | ||
| 233 | $psender->{action} = $na; | ||
| 234 | if ($na eq 'discard') { | ||
| 235 | push(@message, "When we think a message from you might be SPAM, we'll just drop it."); | ||
| 236 | } elsif ($na eq 'quarantine') { | ||
| 237 | push(@message, "When we think a message from you might be SPAM, we'll quarantine so that you can verify that it isn't spam."); | ||
| 238 | } elsif ($na eq 'bounce') { | ||
| 239 | push(@message, "When we think a message from you might be SPAM, we'll bounce it."); | ||
| 240 | } else { | ||
| 241 | error; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | if ($bf != ($psender->{renotify_days} || $defaults{renotify_sender_ip})) { | ||
| 245 | $psender->{renotify_days} = $bf; | ||
| 246 | push(@message, "You will only be notified every $bf days (per IP address) if we think a message might be spam"); | ||
| 247 | } | ||
| 248 | $docommit = 1; | ||
| 249 | } elsif ($action eq $defaults{button_sender_reset_timer}) { | ||
| 250 | if (%{$psender->{send_ip_used}}) { | ||
| 251 | $psender->{sender}{send_ip_used} = {}; | ||
| 252 | $docommit = 1; | ||
| 253 | push(@message, "Notification timers reset"); | ||
| 254 | } | ||
| 255 | } elsif ($action eq $defaults{button_sender_replace_token}) { | ||
| 256 | require Time::HiRes; | ||
| 257 | $psender->{token} = md5_hex($qd->{random_token} . $sender . Time::HiRes::time() . $$ . $ENV{REMOTE_HOST} . $ENV{REMOTE_ADDR}); | ||
| 258 | $oops->commit; | ||
| 259 | $docommit = 0; | ||
| 260 | |||
| 261 | my $buf; | ||
| 262 | $template->process('sender-notification.mail', { | ||
| 263 | %$args, | ||
| 264 | sender_url => $psender->url(), | ||
| 265 | request_ip => $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST}, | ||
| 266 | now => scalar(localtime(time)), | ||
| 267 | }, \$buf) || error $Template::ERROR; | ||
| 268 | |||
| 269 | sendmail_or_postpone(from => $defaults{send_from}, to => $sender, message => $buf); | ||
| 270 | |||
| 271 | if ($canshow) { | ||
| 272 | $template->process('access-url-sent.tt2', { | ||
| 273 | config => \%defaults, | ||
| 274 | recipient => $sender, | ||
| 275 | }) || error $Template::ERROR; | ||
| 276 | print STDERR "## New sender token sent to $sender\n"; | ||
| 277 | $doform = 0; | ||
| 278 | } else { | ||
| 279 | push(@message, "New sender token sent to $sender"); | ||
| 280 | } | ||
| 281 | } elsif ((undef, $docommit, @message) = handle_other_messages($oops, $psender, $action, $args, undef)) { | ||
| 282 | # nada | ||
| 283 | } elsif (! $action) { | ||
| 284 | # nada | ||
| 285 | } else { | ||
| 286 | error "action=$action"; | ||
| 287 | } | ||
| 288 | return ($docommit, $doform, @message); | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | sub handle_other_messages | ||
| 293 | { | ||
| 294 | my ($oops, $psender, $action, $templateargs, $tt2) = @_; | ||
| 295 | |||
| 296 | my $do; | ||
| 297 | my $set; | ||
| 298 | if ($action eq $defaults{button_sender_delete_checked}) { | ||
| 299 | $do = 'delete'; | ||
| 300 | $set = 'checked'; | ||
| 301 | } elsif ($action eq $defaults{button_sender_delete_all}) { | ||
| 302 | $do = 'delete'; | ||
| 303 | $set = 'all'; | ||
| 304 | } elsif ($action eq $defaults{button_sender_send_checked}) { | ||
| 305 | $do = 'send'; | ||
| 306 | $set = 'checked'; | ||
| 307 | } else { | ||
| 308 | return 0; | ||
| 309 | } | ||
| 310 | |||
| 311 | my @set; | ||
| 312 | for my $hsum (keys %{$psender->{headers}}) { | ||
| 313 | next if $psender->{headers}{$hsum}{done}; | ||
| 314 | push(@set, $psender->{headers}{$hsum}) if $set eq 'all' || $cgi->param("cb-".$hsum); | ||
| 315 | } | ||
| 316 | my @message; | ||
| 317 | if ($do eq 'delete') { | ||
| 318 | for my $h (@set) { | ||
| 319 | message_handled($oops, $h, 'deleted'); | ||
| 320 | push(@message, "Deleted message w/Subject $h->{subject}"); | ||
| 321 | } | ||
| 322 | } else { | ||
| 323 | for my $h (@set) { | ||
| 324 | message_handled($oops, $h, 'sent'); | ||
| 325 | sendmail_or_postpone(from => ($h->{sender}{address} || '<>'), to => $h->{recipients}, header => $h->{header}, body => $h->{body}{body}); | ||
| 326 | push(@message, "Sent message w/Subject $h->{subject}"); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | return (1, 1, @message) unless $tt2; | ||
| 330 | $oops->commit(); | ||
| 331 | my $message = message('Sender', @message); | ||
| 332 | $template->process($tt2, { | ||
| 333 | %$templateargs, | ||
| 334 | message => $message, | ||
| 335 | }) || error $Template::ERROR; | ||
| 336 | return 1; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub message_handled | ||
| 340 | { | ||
| 341 | my ($oops, $h, $how) = @_; | ||
| 342 | error if $h->{done}; | ||
| 343 | $h->{done} = $how; | ||
| 344 | my $qd = $oops->{quarantine} || error; | ||
| 345 | for my $r (@{$h->{recipients}}) { | ||
| 346 | my $rd = $qd->{recipients}{$r}; | ||
| 347 | if ($rd->{headers}{$h->{cksum}}) { | ||
| 348 | $rd->{mcount}--; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | sub handle_recipient | ||
| 354 | { | ||
| 355 | my ($oops, $recipient_encoded, $recipient_checksum) = @_; | ||
| 356 | |||
| 357 | print $cgi->header(); | ||
| 358 | |||
| 359 | my $qd = $oops->{quarantine} || error; | ||
| 360 | my $recipient = CGI::unescape($recipient_encoded); | ||
| 361 | my $action = $cgi->param('action') || ''; | ||
| 362 | my $rd = $qd->{recipients}{$recipient}; | ||
| 363 | |||
| 364 | $rd = new_recipient($oops, $recipient) | ||
| 365 | unless $rd; | ||
| 366 | |||
| 367 | my $correct_checksum = $rd->{token} || md5_hex($qd->{random_token} . $recipient); | ||
| 368 | |||
| 369 | if ($action eq $defaults{button_recipient_url}) { | ||
| 370 | return handle_unauthorized_recipient($oops, $recipient_encoded); | ||
| 371 | } | ||
| 372 | |||
| 373 | if ($recipient_checksum ne $correct_checksum) { | ||
| 374 | $template->process('unauthorized-recipient.tt2', { | ||
| 375 | config => \%defaults, | ||
| 376 | recipient => $recipient, | ||
| 377 | error => 'Your URL is invalid', | ||
| 378 | error_detail => 'The URL you used does not contain the correct authentication tokens for your email address. Please ask for a new authentication URL.', | ||
| 379 | }) || error $Template::ERROR; | ||
| 380 | print STDERR "CHECKSUM MISMATCH $recipient_checksum vs $correct_checksum\n"; | ||
| 381 | return; | ||
| 382 | } | ||
| 383 | |||
| 384 | my %args = ( | ||
| 385 | config => \%defaults, | ||
| 386 | recipient => $recipient, | ||
| 387 | rd => $rd, | ||
| 388 | ); | ||
| 389 | |||
| 390 | (my $showform, my @message) = recipient_action($oops, $recipient, $rd, \%args, 1, $action); | ||
| 391 | |||
| 392 | my $message = message('Sender', @message); | ||
| 393 | |||
| 394 | if ($showform) { | ||
| 395 | $template->process('recipient-menu.tt2', { | ||
| 396 | message => $message, | ||
| 397 | %args, | ||
| 398 | }) || error $Template::ERROR; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | sub recipient_action | ||
| 403 | { | ||
| 404 | my ($oops, $recipient, $rd, $args, $canshow, $action) = @_; | ||
| 405 | my $qd = $oops->{quarantine} || error; | ||
| 406 | my @message; | ||
| 407 | my $docommit; | ||
| 408 | my $showform = 1; | ||
| 409 | if ($action eq $defaults{button_recipient_update}) { | ||
| 410 | my $na = $cgi->param('new_action'); | ||
| 411 | if ($na eq 'drop') { | ||
| 412 | $rd->{action} = 'drop'; | ||
| 413 | $oops->commit(); | ||
| 414 | print STDERR "We will now drop messages for $recipient\n"; | ||
| 415 | push(@message, 'Settings changed: spammy messages for you will now be dropped'); | ||
| 416 | } elsif ($na eq 'quarantine') { | ||
| 417 | delete $rd->{action}; | ||
| 418 | $oops->commit(); | ||
| 419 | print STDERR "We will now quarantine messages for $recipient\n"; | ||
| 420 | push(@message, 'Settings changed: spammy messages for you will now be quarantined'); | ||
| 421 | } elsif ($na eq 'forward') { | ||
| 422 | require Mail::Address; | ||
| 423 | my ($new, @junk) = Mail::Address->parse($cgi->param('new_address')); | ||
| 424 | if (! $new or @junk or ! $new->host) { | ||
| 425 | push(@message, 'Please enter a simple address (user@host) for forwarding'); | ||
| 426 | } else { | ||
| 427 | if (domain_is_filtered($new->host)) { | ||
| 428 | push(@message, 'You cannot forward to @'.$new->host.' addresses because they suffer from the same problem that your current address has'); | ||
| 429 | } else { | ||
| 430 | my @tosend; | ||
| 431 | for my $hsum (keys %{$rd->{headers}}) { | ||
| 432 | my $h = $rd->{headers}{$hsum}; | ||
| 433 | next if $h->{done}; | ||
| 434 | delete $rd->{headers}{$hsum}; | ||
| 435 | push(@tosend, { | ||
| 436 | to => $recipient, | ||
| 437 | sender => $h->{sender}{address}, | ||
| 438 | header => $h->{header}, | ||
| 439 | body => $h->{body}, | ||
| 440 | }); | ||
| 441 | my @newrlist = grep( $_ ne $recipient, @{$h->{recipients}}); | ||
| 442 | message_handled($oops, $h, 'done') unless @newrlist; | ||
| 443 | $h->{recipients} = bless [ @newrlist ], 'Quarantine::RecipientList'; | ||
| 444 | } | ||
| 445 | send_queued($new->format, @tosend); | ||
| 446 | $rd->{action} = 'forward'; | ||
| 447 | $rd->{new_address} = $new->format; | ||
| 448 | $oops->commit(); | ||
| 449 | push(@message, sprintf("Settings changed: spammy messages for you will now be forwarded. Quarantined messages released: %d", scalar(@tosend))); | ||
| 450 | |||
| 451 | my $buf; | ||
| 452 | $template->process('recipient-forwarding.mail', { | ||
| 453 | %$args, | ||
| 454 | new_address => $new->format, | ||
| 455 | request_ip => $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST}, | ||
| 456 | recipient_url => $rd->url($qd), | ||
| 457 | now => scalar(localtime(time)), | ||
| 458 | }, \$buf) || error $Template::ERROR; | ||
| 459 | |||
| 460 | print STDERR "Message to send to $recipient:\n$buf" if $debug; | ||
| 461 | |||
| 462 | sendmail_or_postpone(from => $defaults{send_from}, to => $new->address, message => $buf); | ||
| 463 | |||
| 464 | } | ||
| 465 | } | ||
| 466 | } else { | ||
| 467 | error; | ||
| 468 | } | ||
| 469 | } elsif ($action eq $defaults{button_recipient_replace_token}) { | ||
| 470 | require Time::HiRes; | ||
| 471 | $rd->{token} = md5_hex($qd->{random_token} . $recipient . $rd->{new_address} . Time::HiRes::time() . $$ . $ENV{REMOTE_HOST} . $ENV{REMOTE_ADDR}); | ||
| 472 | $oops->commit; | ||
| 473 | |||
| 474 | my $buf; | ||
| 475 | $template->process('recipient-notification.mail', { | ||
| 476 | %$args, | ||
| 477 | request_ip => $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST}, | ||
| 478 | recipient_url => $rd->url($qd), | ||
| 479 | now => scalar(localtime(time)), | ||
| 480 | }, \$buf) || error $Template::ERROR; | ||
| 481 | |||
| 482 | print STDERR "Message to send to $recipient:\n$buf" if $debug; | ||
| 483 | |||
| 484 | sendmail_or_postpone(from => $defaults{send_from}, to => $recipient, message => $buf); | ||
| 485 | |||
| 486 | if ($rd->{action} eq 'forward') { | ||
| 487 | print STDERR "ALSO Message to send to $rd->{new_address}\n" if $debug; | ||
| 488 | sendmail_or_postpone(from => $defaults{send_from}, to => $rd->{new_address}, message => $buf); | ||
| 489 | } | ||
| 490 | |||
| 491 | if ($canshow) { | ||
| 492 | $template->process('access-url-sent.tt2', { | ||
| 493 | %$args, | ||
| 494 | }) || error $Template::ERROR; | ||
| 495 | $showform = 0; | ||
| 496 | } else { | ||
| 497 | push(@message, "Recipient access URL sent"); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | return($showform, @message); | ||
| 501 | } | ||
| 502 | |||
| 503 | sub handle_unauthorized_recipient | ||
| 504 | { | ||
| 505 | my ($oops, $recipient_encoded) = @_; | ||
| 506 | |||
| 507 | print $cgi->header(); | ||
| 508 | |||
| 509 | my $qd = $oops->{quarantine} || error; | ||
| 510 | |||
| 511 | my $recipient = CGI::unescape($recipient_encoded) || $cgi->param('recipient'); | ||
| 512 | |||
| 513 | my $action = $cgi->param('action') || ''; | ||
| 514 | |||
| 515 | my $rd = $qd->{recipients}{$recipient}; | ||
| 516 | my $token = ($rd && $rd->{token}) || md5_hex($qd->{random_token} . $recipient); | ||
| 517 | |||
| 518 | if ($action eq $defaults{button_recipient_url} && $recipient =~ /^[^\@]+\@([^\@]+)$/) { | ||
| 519 | my $buf; | ||
| 520 | # recipient may not exist yet | ||
| 521 | $template->process('recipient-notification.mail', { | ||
| 522 | config => \%defaults, | ||
| 523 | recipient => $recipient, | ||
| 524 | request_ip => $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST}, | ||
| 525 | recipient_url => "$defaults{baseurl}/recipient/$token/$escape{$recipient}", | ||
| 526 | now => scalar(localtime(time)), | ||
| 527 | }, \$buf) || error $Template::ERROR; | ||
| 528 | print STDERR "Message to send to $recipient:\n$buf" if $debug; | ||
| 529 | sendmail_or_postpone(from => $defaults{send_from}, to => $recipient, message => $buf); | ||
| 530 | if ($rd && ($rd->{action} eq 'forward')) { | ||
| 531 | print STDERR "ALSO Message to send to $rd->{new_address}\n" if $debug; | ||
| 532 | sendmail_or_postpone(from => $defaults{send_from}, to => $rd->{new_address}, message => $buf); | ||
| 533 | } | ||
| 534 | $template->process('access-url-sent.tt2', { | ||
| 535 | config => \%defaults, | ||
| 536 | recipient => $recipient, | ||
| 537 | }) || error $Template::ERROR; | ||
| 538 | } else { | ||
| 539 | $template->process('unauthorized-recipient.tt2', { | ||
| 540 | config => \%defaults, | ||
| 541 | recipient => $recipient | ||
| 542 | }) || error $Template::ERROR; | ||
| 543 | } | ||
| 544 | } | ||
| 545 | |||
| 546 | |||
| 547 | sub handle_message | ||
| 548 | { | ||
| 549 | my ($oops,$hdr_sum) = @_; | ||
| 550 | |||
| 551 | print $cgi->header(); | ||
| 552 | |||
| 553 | my $qd = $oops->{quarantine} || error; | ||
| 554 | |||
| 555 | my $h = $qd->{headers}{$hdr_sum}; | ||
| 556 | |||
| 557 | unless ($h) { | ||
| 558 | $template->process('error.tt2', { | ||
| 559 | config => \%defaults, | ||
| 560 | error => "Message not found", | ||
| 561 | verbose => "Your message was not found in our database. We expire messages fairly quickly so it may be that you waited too long. Please re-send your original message to start the process over again." | ||
| 562 | }) || error $Template::ERROR; | ||
| 563 | return; | ||
| 564 | }; | ||
| 565 | |||
| 566 | my $otherheaders = [ grep(refaddr($_) != refaddr($h) && ! $_->{done}, values %{$h->{sender}{headers}}) ]; | ||
| 567 | my $repeat = ($cgi->referer() =~ /\Q$defaults{baseurl}\E/); | ||
| 568 | my (%args) = ( | ||
| 569 | header => $h, | ||
| 570 | sender => $h->{sender}{canonical}, | ||
| 571 | psender => $h->{sender}, | ||
| 572 | recipients => join(', ', @{$h->{recipients}}), | ||
| 573 | otherheaders => $otherheaders, | ||
| 574 | repeat => $repeat, | ||
| 575 | config => \%defaults, | ||
| 576 | baseurl => $defaults{baseurl}, | ||
| 577 | sender_url => $h->{sender}->url(), | ||
| 578 | ); | ||
| 579 | |||
| 580 | my $action = $cgi->param('action') || ''; | ||
| 581 | print STDERR "Action = '$action'\n" if $debug; | ||
| 582 | if ($action eq $defaults{button_sender_delete}) { | ||
| 583 | message_handled($oops, $h, 'deleted'); | ||
| 584 | $oops->commit(); | ||
| 585 | $template->process('sender-action-taken.tt2', { | ||
| 586 | %args, | ||
| 587 | code => 'DELETE', | ||
| 588 | message => "Your message was deleted. Thank you.", | ||
| 589 | }) || error $Template::ERROR; | ||
| 590 | } elsif ($action eq $defaults{button_sender_send}) { | ||
| 591 | sendmail_or_postpone(from => $h->{sender}{address}, to => $h->{recipients}, header => $h->{header}, body => $h->{body}{body}); | ||
| 592 | message_handled($oops, $h, 'sent'); | ||
| 593 | $oops->commit(); | ||
| 594 | $template->process('sender-action-taken.tt2', { | ||
| 595 | %args, | ||
| 596 | code => 'SENT', | ||
| 597 | message => 'Your message was released from quarantine and is now on its way to its destination', | ||
| 598 | }) || error $Template::ERROR; | ||
| 599 | } elsif ($action eq 'Discard My Mail') { | ||
| 600 | $h->{sender}{silentely_discard} = { | ||
| 601 | host => $cgi->remote_host(), | ||
| 602 | agent => $cgi->user_agent(), | ||
| 603 | date => time, | ||
| 604 | }; | ||
| 605 | $oops->commit(); | ||
| 606 | $template->process('sender-action-taken.tt2', { | ||
| 607 | %args, | ||
| 608 | code => 'DISCARD_ALL', | ||
| 609 | message => 'Mail from you that we think is spam will be silently discarded. This is not reversable. Do not ask.', | ||
| 610 | }) || error $Template::ERROR; | ||
| 611 | } elsif ($action eq '') { | ||
| 612 | if ($h->{sender}{send_ip_used}) { | ||
| 613 | $h->{sender}{send_ip_used} = {}; | ||
| 614 | $oops->commit(); | ||
| 615 | } | ||
| 616 | my $x; | ||
| 617 | $template->process('message-menu.tt2', \%args, \$x) or error $Template::ERROR; | ||
| 618 | # print STDERR "X=$x\n"; | ||
| 619 | print $x; | ||
| 620 | } elsif (handle_other_messages($oops, $h->{sender}, $action, \%args, 'message-menu.tt2')) { | ||
| 621 | # nada | ||
| 622 | } else { | ||
| 623 | error "action=$action"; | ||
| 624 | } | ||
| 625 | } | ||
| 626 | |||
| 627 | sub handle_admin | ||
| 628 | { | ||
| 629 | my ($oops) = @_; | ||
| 630 | |||
| 631 | my $qd = $oops->{quarantine} || error; | ||
| 632 | |||
| 633 | my (%cookies) = CGI::Cookie->fetch(); | ||
| 634 | |||
| 635 | my %args = ( config => \%defaults ); | ||
| 636 | my $authorized = 0; | ||
| 637 | my $action = $cgi->param('action') || ''; | ||
| 638 | my $setcookie; | ||
| 639 | |||
| 640 | if ($action eq $defaults{button_login}) { | ||
| 641 | if (authorized_admin($cgi->param('user'), $cgi->param('pass'))) { | ||
| 642 | my $expire = strftime($cookie_time_fmt, gmtime(time + 86400*30)); | ||
| 643 | $setcookie = CGI::Cookie->new( | ||
| 644 | -name => 'admin', | ||
| 645 | -value => $cgi->param('user') | ||
| 646 | . ':' | ||
| 647 | . md5_hex($qd->{random_token} . $cgi->param('user') . $ENV{REMOTE_ADDR}), | ||
| 648 | -expires => $expire, | ||
| 649 | ); | ||
| 650 | $authorized = $cgi->param('user'); | ||
| 651 | print $cgi->header(-cookie => $setcookie); | ||
| 652 | } else { | ||
| 653 | $args{error} = "Invalid login"; | ||
| 654 | $args{message} = "We're logging your IP address"; | ||
| 655 | print STDERR "Bad admin password guess for ".$cgi->param('user')." from $ENV{REMOTE_ADDR}\n"; | ||
| 656 | } | ||
| 657 | } elsif ($action eq $defaults{button_logout}) { | ||
| 658 | print $cgi->header(-cookie => CGI::Cookie->new( | ||
| 659 | -name => 'admin', | ||
| 660 | -value => 'nope', | ||
| 661 | )); | ||
| 662 | %cookies = (); | ||
| 663 | } | ||
| 664 | |||
| 665 | unless ($setcookie) { | ||
| 666 | print $cgi->header(); | ||
| 667 | |||
| 668 | if ($cookies{admin}) { | ||
| 669 | my $v = $cookies{admin}->value(); | ||
| 670 | $v =~ m/^([^:]+):(.*)/; | ||
| 671 | my $u = $1; | ||
| 672 | my $md5 = $2; | ||
| 673 | my $verify = md5_hex($qd->{random_token} . $u . $ENV{REMOTE_ADDR}); | ||
| 674 | if ($md5 eq $verify) { | ||
| 675 | $authorized = $u; | ||
| 676 | } elsif ($v ne 'nope') { | ||
| 677 | $args{error} = "Invalid login cookie"; | ||
| 678 | $args{message} = "Please log in again"; | ||
| 679 | print STDERR "Invalid admin cookie for $u from $ENV{REMOTE_ADDR}\n"; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | } | ||
| 683 | |||
| 684 | my $email = $cgi->param('lookupemail') || CGI::unescape($cgi->param('adminemail')); | ||
| 685 | |||
| 686 | if ($authorized && $email) { | ||
| 687 | $args{email} = $email; | ||
| 688 | |||
| 689 | $args{hiddenstate} = qq{<input type="hidden" name="adminemail" value="$escape{$email}">}; | ||
| 690 | $args{adminemail} = $escape{$email}; | ||
| 691 | |||
| 692 | my $psender = $qd->{senders}{$email}; | ||
| 693 | if ($psender) { | ||
| 694 | $args{psender} = $psender; | ||
| 695 | $args{sender} = $psender; | ||
| 696 | $args{otherheaders} = [ grep(! $_->{done}, values %{$psender->{headers}}) ]; | ||
| 697 | $args{sender_url} = $psender->url; | ||
| 698 | } | ||
| 699 | |||
| 700 | my $rd = $qd->{recipients}{$email}; | ||
| 701 | if ($rd) { | ||
| 702 | $args{rd} = $rd; | ||
| 703 | $args{recipient} = $email; | ||
| 704 | $args{recipient_url} = $rd->url($qd); | ||
| 705 | } | ||
| 706 | |||
| 707 | if ($action eq $defaults{button_lookup_email}) { | ||
| 708 | # nothing | ||
| 709 | } else { | ||
| 710 | for my $button (keys %defaults) { | ||
| 711 | next unless $button =~ /^button_/; | ||
| 712 | next unless $action eq $defaults{$button}; | ||
| 713 | print STDERR "Button $button pressed\n"; | ||
| 714 | if ($button =~ /sender/) { | ||
| 715 | (my $docommit, undef, my @message) = sender_action($oops, $email, $psender, $action, \%args, 0); | ||
| 716 | $oops->commit() if $docommit; | ||
| 717 | $args{smessage} = message('Sender', @message); | ||
| 718 | last; | ||
| 719 | } elsif ($button =~ /recipient/) { | ||
| 720 | (undef, my @message) = recipient_action($oops, $email, $rd, \%args, 0, $action); | ||
| 721 | $args{rmessage} = message('Recipient', @message); | ||
| 722 | last; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | } | ||
| 726 | } | ||
| 727 | |||
| 728 | $args{authorized} = $authorized; | ||
| 729 | $template->process('admin.tt2', \%args) || error $Template::ERROR; | ||
| 730 | return; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub message | ||
| 734 | { | ||
| 735 | my ($role, @message) = @_; | ||
| 736 | return "" unless @message; | ||
| 737 | print STDERR "## $role: @message\n"; | ||
| 738 | return "<p>\n".join("\n</p><p>\n", @message)."\n</p>\n"; | ||
| 739 | } | ||
| 740 | |||
| 741 | |||
| 742 | sub send_queued | ||
| 743 | { | ||
| 744 | my ($to, @list) = @_; | ||
| 745 | for my $m (@list) { | ||
| 746 | sendmail_or_postpone( | ||
| 747 | from => ($m->{sender} || '<>'), | ||
| 748 | to => $m->{to}, | ||
| 749 | header => $m->{header}, | ||
| 750 | body => $m->{body}, | ||
| 751 | ); | ||
| 752 | } | ||
| 753 | } | ||
| 754 | |||
| 755 | sub authorized_admin | ||
| 756 | { | ||
| 757 | my ($user, $pass) = @_; | ||
| 758 | return 0 unless $user =~ /^\w/; | ||
| 759 | open(PWFILE, "<$defaults{admin_passwd_file}") || error; | ||
| 760 | while(<PWFILE>) { | ||
| 761 | next if /^$/; | ||
| 762 | next if /^#/; | ||
| 763 | chomp; | ||
| 764 | my ($u, $p) = split(':', $_); | ||
| 765 | next if $u ne $user; | ||
| 766 | return 1 if crypt($pass, $p) eq $p; | ||
| 767 | printf STDERR "Attempted login: %s '%s' ne '%s'\n", $pass, $p, crypt($pass, $p); | ||
| 768 | return 0; | ||
| 769 | } | ||
| 770 | close(PWFILE); | ||
| 771 | print STDERR "User $user not found in password file\n"; | ||
| 772 | return 0; | ||
| 773 | } | ||
| 774 | |||
| 775 | my %filter_domains; | ||
| 776 | sub domain_is_filtered | ||
| 777 | { | ||
| 778 | my ($domain) = @_; | ||
| 779 | unless (%filter_domains) { | ||
| 780 | open(DOMS, "<$filtered_domains") || error "open $filtered_domains: $!"; | ||
| 781 | while(<DOMS>) { | ||
| 782 | next if /^#/; | ||
| 783 | next if /^$/; | ||
| 784 | chomp; | ||
| 785 | $filter_domains{$_} = 1; | ||
| 786 | } | ||
| 787 | close(DOMS); | ||
| 788 | } | ||
| 789 | return match_domain($domain, \%filter_domains); | ||
| 790 | } | ||
| 791 | |||
| 792 | sub match_domain | ||
| 793 | { | ||
| 794 | my ($domain, $hashref) = @_; | ||
| 795 | while ($domain) { | ||
| 796 | if ($hashref->{$domain}) { | ||
| 797 | return 1; | ||
| 798 | } | ||
| 799 | $domain =~ s/^[^\.]+// or last; | ||
| 800 | $domain =~ s/^\.//; | ||
| 801 | } | ||
| 802 | return 0; | ||
| 803 | } | ||
| 804 | |||
| 805 | # for the benifit of the Template module... | ||
| 806 | |||
| 807 | sub Quarantine::Sender::cookie | ||
| 808 | { | ||
| 809 | my ($sender) = @_; | ||
| 810 | return "foo"; | ||
| 811 | } | ||
| 812 | |||
| 813 | 1; |