| 1 | package Mail::SpamAssassin::Plugin::Karmasphere; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use vars qw(@ISA $CONNECT_FEEDSET $CONTENT_FEEDSET $DEBUG); | ||
| 6 | use bytes; | ||
| 7 | use Carp qw(confess); | ||
| 8 | use Data::Dumper; | ||
| 9 | use Time::HiRes; | ||
| 10 | use Mail::SpamAssassin::Plugin; | ||
| 11 | use Mail::SpamAssassin::Logger; | ||
| 12 | use Mail::Karmasphere::Client qw(:all); | ||
| 13 | use Mail::Karmasphere::Query qw(:all); | ||
| 14 | |||
| 15 | @ISA = qw(Mail::SpamAssassin::Plugin); | ||
| 16 | $CONNECT_FEEDSET = 'karmasphere.email-sender'; | ||
| 17 | $CONTENT_FEEDSET = 'karmasphere.email-body'; | ||
| 18 | $DEBUG = undef; | ||
| 19 | |||
| 20 | # constructor: register the eval rule and parse any config | ||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $mailsaobject = shift; | ||
| 24 | |||
| 25 | my $self = $class->SUPER::new($mailsaobject, @_); | ||
| 26 | |||
| 27 | my $conf = $mailsaobject->{conf}; | ||
| 28 | |||
| 29 | #$self->register_eval_rule("check_against_karma_db"); | ||
| 30 | |||
| 31 | $self->set_config($mailsaobject->{conf}); | ||
| 32 | |||
| 33 | $self->register_eval_rule("check_karma_range"); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | ######################################################################## | ||
| 39 | |||
| 40 | sub set_config { | ||
| 41 | my ($self, $conf) = @_; | ||
| 42 | my @cmds = (); | ||
| 43 | |||
| 44 | push(@cmds, { | ||
| 45 | setting => 'karma_feedset', | ||
| 46 | code => sub { | ||
| 47 | my $self = shift; | ||
| 48 | my ($key, $value, $line) = @_; | ||
| 49 | if ($value =~ /^(\S+)\s+(\S+)$/) { | ||
| 50 | my ($context, $composite) = ($1, $2); | ||
| 51 | $self->{karma_feedset}->{$context} = $composite; | ||
| 52 | return undef; | ||
| 53 | } | ||
| 54 | elsif (! length $value) { | ||
| 55 | return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return $Mail::SpamAssassin::Conf::INVALID_VALUE; | ||
| 59 | } | ||
| 60 | }, | ||
| 61 | }); | ||
| 62 | |||
| 63 | push(@cmds, { | ||
| 64 | setting => 'karma_feed', | ||
| 65 | code => sub { | ||
| 66 | my $self = shift; | ||
| 67 | my ($key, $value, $line) = @_; | ||
| 68 | if ($value =~ /^(\S+)\s+(\S+)$/) { | ||
| 69 | my ($context, $feeds) = ($1, $2); | ||
| 70 | push(@{ $self->{karma_feeds}->{$context} }, | ||
| 71 | split(/\s+/, $feeds)); | ||
| 72 | return undef; | ||
| 73 | } | ||
| 74 | elsif (! length $value) { | ||
| 75 | return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | return $Mail::SpamAssassin::Conf::INVALID_VALUE; | ||
| 79 | } | ||
| 80 | return undef; | ||
| 81 | }, | ||
| 82 | }); | ||
| 83 | |||
| 84 | push (@cmds, { | ||
| 85 | setting => 'karma_range', | ||
| 86 | code => sub { | ||
| 87 | my $self = shift; | ||
| 88 | my ($key, $value, $line) = @_; | ||
| 89 | if ($value =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/) { | ||
| 90 | my ($rulename, $context, $min, $max) = | ||
| 91 | ($1, $2, 0+$3, 0+$4); | ||
| 92 | $self->{karma_rules}->{$rulename} = | ||
| 93 | [ $context, $min, $max ]; | ||
| 94 | # Does anyone remember what this line does? | ||
| 95 | $self->{parser}->add_test($rulename, | ||
| 96 | "check_karma_range('$context', '$min', '$max')", | ||
| 97 | $Mail::SpamAssassin::Conf::TYPE_FULL_EVALS); | ||
| 98 | return undef; | ||
| 99 | } | ||
| 100 | elsif (! length $value) { | ||
| 101 | return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | return $Mail::SpamAssassin::Conf::INVALID_VALUE; | ||
| 105 | } | ||
| 106 | }, | ||
| 107 | }); | ||
| 108 | |||
| 109 | push (@cmds, { | ||
| 110 | setting => 'karma_host', | ||
| 111 | default => 'query.karmasphere.com', | ||
| 112 | is_admin => 1, | ||
| 113 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING | ||
| 114 | }); | ||
| 115 | |||
| 116 | push (@cmds, { | ||
| 117 | setting => 'karma_port', | ||
| 118 | default => '8666', | ||
| 119 | is_admin => 1, | ||
| 120 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC | ||
| 121 | }); | ||
| 122 | |||
| 123 | push (@cmds, { | ||
| 124 | setting => 'karma_timeout', | ||
| 125 | default => '5', | ||
| 126 | is_admin => 1, | ||
| 127 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC | ||
| 128 | }); | ||
| 129 | |||
| 130 | push (@cmds, { | ||
| 131 | setting => 'karma_ar_host', | ||
| 132 | is_admin => 1, | ||
| 133 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING | ||
| 134 | }); | ||
| 135 | |||
| 136 | push (@cmds, { | ||
| 137 | setting => 'karma_principal', | ||
| 138 | default => undef, | ||
| 139 | is_admin => 1, | ||
| 140 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING | ||
| 141 | }); | ||
| 142 | |||
| 143 | push (@cmds, { | ||
| 144 | setting => 'karma_credentials', | ||
| 145 | default => undef, | ||
| 146 | is_admin => 1, | ||
| 147 | type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING | ||
| 148 | }); | ||
| 149 | |||
| 150 | $conf->{parser}->register_commands(\@cmds); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _karma_debug { | ||
| 154 | my ($context, @args) = @_; | ||
| 155 | { | ||
| 156 | local $Data::Dumper::Indent = 1; | ||
| 157 | local $Data::Dumper::Varname = uc($context); | ||
| 158 | dbg("karma: " . Dumper(@args)); | ||
| 159 | } | ||
| 160 | $DEBUG->(@_) if $DEBUG; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _karma_client { | ||
| 164 | my $self = shift; | ||
| 165 | my $conf = shift || $self->{main}{conf}; | ||
| 166 | unless ($self->{Client}) { | ||
| 167 | my %args = ( | ||
| 168 | PeerHost => $conf->{karma_host}, | ||
| 169 | PeerPort => $conf->{karma_port}, | ||
| 170 | Principal => $conf->{karma_principal}, | ||
| 171 | Credentials => $conf->{karma_credentials} | ||
| 172 | ); | ||
| 173 | if (would_log('dbg', 'karma')) { | ||
| 174 | $args{Debug} = \&_karma_debug; | ||
| 175 | } | ||
| 176 | |||
| 177 | $self->{Client} = new Mail::Karmasphere::Client(%args); | ||
| 178 | } | ||
| 179 | return $self->{Client}; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub add_feeds { | ||
| 183 | my ($self, $conf, $query, $context) = @_; | ||
| 184 | my $feeds = $conf->{karma_feeds}->{$context}; | ||
| 185 | if ($feeds) { | ||
| 186 | $query->feed(@$feeds); | ||
| 187 | my $flags = $query->flags || 0; | ||
| 188 | $query->flags($flags | 1); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub add_connect_authentication { | ||
| 193 | my ($self, $scanner, $query) = @_; | ||
| 194 | |||
| 195 | my $conf = $scanner->{conf}; | ||
| 196 | my $arhost = lc $conf->{karma_ar_host}; | ||
| 197 | |||
| 198 | # Authentication-Results: spf.checker.net \ | ||
| 199 | # smtp.mail=spf-sender\@that.net; spf=pass | ||
| 200 | |||
| 201 | # XXX Make sure these are not preceded by any received: line. | ||
| 202 | my $authresults = $scanner->get('Authentication-Results'); | ||
| 203 | if (defined $authresults) { | ||
| 204 | my @authresults = split(/[\r\n]+/, $authresults); | ||
| 205 | foreach my $line (@authresults) { | ||
| 206 | dbg("karma: Parsing A-R header $line"); | ||
| 207 | unless ($line =~ s/^\s*(\S+)\s*//) { | ||
| 208 | dbg("Invalid Authentication-Results header: $line"); | ||
| 209 | next; | ||
| 210 | } | ||
| 211 | my $hostname = $1; | ||
| 212 | if ($arhost) { | ||
| 213 | next if $arhost ne lc $hostname; | ||
| 214 | } | ||
| 215 | my @vals = split(/\s*;\s*/, $line); | ||
| 216 | my $identity = shift @vals; | ||
| 217 | unless ($identity =~ /^([^=]*)=(.*)/) { | ||
| 218 | dbg("Invalid Authentication-Results identity: $identity"); | ||
| 219 | next; | ||
| 220 | } | ||
| 221 | my ($artype, $iddata) = ($1, $2); | ||
| 222 | dbg("karma: A-R host=$hostname, type=$artype, id=$iddata"); | ||
| 223 | my %checks = (); | ||
| 224 | my $pass = undef; | ||
| 225 | for my $val (@vals) { | ||
| 226 | unless ($val =~ /^([^=]*)=(.*)/) { | ||
| 227 | dbg("Invalid Authentication-Results result: $val"); | ||
| 228 | next; | ||
| 229 | } | ||
| 230 | $checks{$1} = $2; | ||
| 231 | $pass++ if $2 =~ /^pass/; | ||
| 232 | } | ||
| 233 | my @tags; | ||
| 234 | my $idtype; | ||
| 235 | if ($artype eq 'smtp.mail') { | ||
| 236 | $idtype = IDT_EMAIL; | ||
| 237 | push(@tags, SMTP_ENV_MAIL_FROM); | ||
| 238 | } | ||
| 239 | elsif ($artype eq 'header.from') { | ||
| 240 | $idtype = IDT_EMAIL; | ||
| 241 | push(@tags, SMTP_HEADER_FROM_ADDRESS); | ||
| 242 | } | ||
| 243 | else { | ||
| 244 | $idtype = guess_identity_type($iddata); | ||
| 245 | } | ||
| 246 | push(@tags, AUTHENTIC) if $pass; | ||
| 247 | $query->identity($iddata, $idtype, @tags); | ||
| 248 | # print Dumper($hostname, $idtype, $iddata, \%checks); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | sub add_connect_received { | ||
| 254 | my ($self, $scanner, $query) = @_; | ||
| 255 | |||
| 256 | if ($scanner->{num_relays_untrusted} > 0) { | ||
| 257 | my $lasthop = $scanner->{relays_untrusted}->[0]; | ||
| 258 | if (!defined $lasthop) { | ||
| 259 | dbg("karma: message was delivered entirely via trusted relays, not required"); | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | dbg("karma: Last hop is " . Dumper($lasthop)); | ||
| 263 | my $ip = $lasthop->{ip}; | ||
| 264 | $query->identity($ip, IDT_IP4, SMTP_CLIENT_IP) | ||
| 265 | if $ip; | ||
| 266 | my $helo = $lasthop->{lc_helo} || $lasthop->{lc_rdns}; | ||
| 267 | $query->identity($helo, IDT_DOMAIN, SMTP_ENV_HELO) | ||
| 268 | if $helo; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | dbg("karma: No untrusted relays"); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | sub add_connect_envfrom { | ||
| 276 | my ($self, $scanner, $query) = @_; | ||
| 277 | |||
| 278 | my $envfrom = $scanner->get('EnvelopeFrom:addr'); | ||
| 279 | dbg("karma: Envelope FROM is " . | ||
| 280 | (defined($envfrom) ? $envfrom : '(undef)') | ||
| 281 | ); | ||
| 282 | $query->identity($envfrom, IDT_EMAIL, SMTP_ENV_MAIL_FROM) | ||
| 283 | if $envfrom; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub add_connect_other { | ||
| 287 | my ($self, $scanner, $query) = @_; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub add_content_urls { | ||
| 291 | my ($self, $scanner, $query) = @_; | ||
| 292 | |||
| 293 | my $uris = $scanner->get_uri_detail_list(); | ||
| 294 | dbg("karma: URI list is " . Dumper($uris)); | ||
| 295 | my %uris = (); | ||
| 296 | for my $data (values %$uris) { | ||
| 297 | my $cleaned = $data->{cleaned} or next; | ||
| 298 | my $uri = $cleaned->[0] or next; | ||
| 299 | $uris{$uri} = 1; | ||
| 300 | } | ||
| 301 | |||
| 302 | for my $uri (keys %uris) { | ||
| 303 | dbg("karma: Adding URL $uri"); | ||
| 304 | $query->identity($uri, IDT_URL); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | sub add_content_other { | ||
| 309 | my ($self, $scanner, $query) = @_; | ||
| 310 | } | ||
| 311 | |||
| 312 | # The unless() conditions in here look a bit strange. The purpose | ||
| 313 | # is to permit the user to install both this plugin and several | ||
| 314 | # subclasses thereof, while having the whole operate nicely. | ||
| 315 | # The subclasses will all co-operate so that only one instance | ||
| 316 | # of each query is performed, and the responses are tracked neatly. | ||
| 317 | # Each subclass may perform custom processing of the responses. | ||
| 318 | sub _karma_send { | ||
| 319 | my ($self, $scanner) = @_; | ||
| 320 | |||
| 321 | dbg("_karma_send: called"); | ||
| 322 | |||
| 323 | my $conf = $scanner->{conf}; | ||
| 324 | my $client = $self->_karma_client($conf); | ||
| 325 | |||
| 326 | # Now we have to navigate the twists and turns of the SpamAssassin | ||
| 327 | # API to retrieve the message metadata we want. This is largely | ||
| 328 | # inconsistent, and I hope it holds up. | ||
| 329 | |||
| 330 | # The connection-time dance | ||
| 331 | unless ($scanner->{karma}->{queries}->{connect}) { | ||
| 332 | my $query = new Mail::Karmasphere::Query(); | ||
| 333 | $query->composite($conf->{karma_feedset}->{connect} | ||
| 334 | || $CONNECT_FEEDSET); | ||
| 335 | |||
| 336 | $self->add_feeds($conf, $query, 'connect'); | ||
| 337 | $self->add_connect_authentication($scanner, $query); | ||
| 338 | $self->add_connect_received($scanner, $query); | ||
| 339 | $self->add_connect_envfrom($scanner, $query); | ||
| 340 | $self->add_connect_other($scanner, $query); | ||
| 341 | $self->_karma_send_query($scanner, $query, 'connect'); | ||
| 342 | } | ||
| 343 | |||
| 344 | # The content-filtering dance | ||
| 345 | unless ($scanner->{karma}->{queries}->{content}) { | ||
| 346 | my $query = new Mail::Karmasphere::Query(); | ||
| 347 | $query->composite($conf->{karma_feedset}->{content} | ||
| 348 | || $CONTENT_FEEDSET); | ||
| 349 | |||
| 350 | $self->add_feeds($conf, $query, 'content'); | ||
| 351 | $self->add_content_urls($scanner, $query); | ||
| 352 | $self->add_content_other($scanner, $query); | ||
| 353 | $self->_karma_send_query($scanner, $query, 'content'); | ||
| 354 | } | ||
| 355 | |||
| 356 | } | ||
| 357 | |||
| 358 | sub _karma_send_query { | ||
| 359 | my ($self, $scanner, $query, $context) = @_; | ||
| 360 | |||
| 361 | dbg("_karma_send: called"); | ||
| 362 | |||
| 363 | my $conf = $scanner->{conf}; | ||
| 364 | my $client = $self->_karma_client($conf); | ||
| 365 | |||
| 366 | $scanner->{karma}->{queries}->{$context} = $query; | ||
| 367 | |||
| 368 | if ($query->has_identities) { | ||
| 369 | $client->send($query); | ||
| 370 | } | ||
| 371 | else { | ||
| 372 | dbg("karma: No identities in $context packet"); | ||
| 373 | $scanner->{karma}->{responses}->{$context} = | ||
| 374 | new Mail::Karmasphere::Response(); | ||
| 375 | } | ||
| 376 | |||
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | sub _karma_recv { | ||
| 381 | my ($self, $scanner) = @_; | ||
| 382 | |||
| 383 | dbg("_karma_recv: called"); | ||
| 384 | |||
| 385 | my $conf = $scanner->{conf}; | ||
| 386 | my $client = $self->_karma_client($conf); | ||
| 387 | |||
| 388 | my $timeout = $conf->{karma_timeout}; | ||
| 389 | $timeout = 2 if $timeout < 2; | ||
| 390 | dbg("_karma_recv: timeout=$timeout"); | ||
| 391 | my $retries = 3; | ||
| 392 | |||
| 393 | my $queries = $scanner->{karma}->{queries}; | ||
| 394 | my $remaining = scalar keys %$queries; | ||
| 395 | my $responses = $scanner->{karma}->{responses}; | ||
| 396 | # As we come into here, we sent a first round of queries some | ||
| 397 | # time ago. That is the 'normal' SpamAssassin pattern. We also | ||
| 398 | # support resends at this stage. We hope not to have to resend | ||
| 399 | # any queries, but it might happen. | ||
| 400 | RETRY: for my $retry (1..$retries) { | ||
| 401 | my $finish = time() + $timeout; | ||
| 402 | for my $context (keys %$queries) { | ||
| 403 | my $query = $queries->{$context}; | ||
| 404 | next if exists $responses->{$context}; | ||
| 405 | my $response = $client->recv($query, $finish - time()); | ||
| 406 | if ($response) { | ||
| 407 | $responses->{$context} = $response; | ||
| 408 | $remaining--; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | last RETRY unless $remaining; | ||
| 413 | # No point doing a resend if we aren't going to do another | ||
| 414 | # receive-cycle. | ||
| 415 | last RETRY if $retry == $retries; | ||
| 416 | |||
| 417 | dbg("_karma_recv: retrying some queries"); | ||
| 418 | $timeout = $timeout * 2; | ||
| 419 | for my $context (keys %$queries) { | ||
| 420 | my $query = $queries->{$context}; | ||
| 421 | next if $responses->{$context}; | ||
| 422 | $client->send($query); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | # Indicate if any queries failed/timed out. | ||
| 427 | for my $context (keys %$queries) { | ||
| 428 | my $query = $queries->{$context}; | ||
| 429 | next if $responses->{$context}; | ||
| 430 | $responses->{$context} = undef; | ||
| 431 | } | ||
| 432 | |||
| 433 | return $responses; | ||
| 434 | } | ||
| 435 | |||
| 436 | # The two hooks | ||
| 437 | |||
| 438 | sub parsed_metadata { | ||
| 439 | my ($self, $opts) = @_; | ||
| 440 | return if $self->{main}->{local_tests_only}; | ||
| 441 | |||
| 442 | my $scanner = $opts->{permsgstatus} or confess "No scanner!"; | ||
| 443 | $self->_karma_send($scanner); | ||
| 444 | |||
| 445 | return undef; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub check_post_dnsbl { | ||
| 449 | my ($self, $opts) = @_; | ||
| 450 | return if $self->{main}->{local_tests_only}; | ||
| 451 | |||
| 452 | my $scanner = $opts->{permsgstatus} or confess "No scanner!"; | ||
| 453 | my $conf = $scanner->{conf}; | ||
| 454 | |||
| 455 | my $responses = $self->_karma_recv($scanner); | ||
| 456 | |||
| 457 | # return unless keys %$responses; | ||
| 458 | |||
| 459 | my $queries = $scanner->{karma}->{queries}; | ||
| 460 | my %queries; | ||
| 461 | while (my ($context, $query) = each(%$queries)) { | ||
| 462 | $queries{$context} = $query->as_string; | ||
| 463 | chomp $queries{$context}; | ||
| 464 | $queries{$context} =~ s/\n/ \/ /g; | ||
| 465 | } | ||
| 466 | |||
| 467 | my %responses; | ||
| 468 | my %values; | ||
| 469 | my %data; | ||
| 470 | while (my ($context, $response) = each(%$responses)) { | ||
| 471 | next unless $response; | ||
| 472 | my $composite = $conf->{karma_feedset}->{$context}; | ||
| 473 | my $value = $response->value($composite); | ||
| 474 | $values{$context} = $value; | ||
| 475 | my $data = $response->data($composite); | ||
| 476 | $data{$context} = $data; | ||
| 477 | |||
| 478 | $responses{$context} = $response->as_string; | ||
| 479 | chomp $responses{$context}; | ||
| 480 | $responses{$context} =~ s/\n/ \/ /g; | ||
| 481 | } | ||
| 482 | |||
| 483 | # Do something with closures. | ||
| 484 | $scanner->set_tag("KARMASCORE", sub { | ||
| 485 | my $context = shift; | ||
| 486 | return $values{$context} if defined $values{$context}; | ||
| 487 | return '0'; | ||
| 488 | }); | ||
| 489 | $scanner->set_tag("KARMADATA", sub { | ||
| 490 | my $context = shift; | ||
| 491 | return $data{$context} if defined $data{$context}; | ||
| 492 | return "(no data in context $context)"; | ||
| 493 | }); | ||
| 494 | |||
| 495 | $scanner->set_tag("KARMAQUERY", sub { | ||
| 496 | my $context = shift; | ||
| 497 | return $queries{$context} if defined $queries{$context}; | ||
| 498 | return "(no query in context $context)"; | ||
| 499 | }); | ||
| 500 | $scanner->set_tag("KARMARESPONSE", sub { | ||
| 501 | my $context = shift; | ||
| 502 | return $responses{$context} if defined $responses{$context}; | ||
| 503 | return "(no response in context $context)"; | ||
| 504 | }); | ||
| 505 | |||
| 506 | $scanner->set_tag("KARMAFEEDS", sub { | ||
| 507 | my $context = shift; | ||
| 508 | return 'undef' unless $queries->{$context}; | ||
| 509 | local $Data::Dumper::Indent = 0; | ||
| 510 | local $Data::Dumper::Terse = 1; | ||
| 511 | return Dumper($queries->{$context}->feeds); | ||
| 512 | }); | ||
| 513 | $scanner->set_tag("KARMAFACTS", sub { | ||
| 514 | my $context = shift; | ||
| 515 | return 'undef' unless $responses->{$context}; | ||
| 516 | local $Data::Dumper::Indent = 0; | ||
| 517 | local $Data::Dumper::Terse = 1; | ||
| 518 | return Dumper($responses->{$context}->facts); | ||
| 519 | }); | ||
| 520 | |||
| 521 | # print STDERR Dumper(\%values); | ||
| 522 | return unless $conf->{karma_rules}; | ||
| 523 | # return unless keys %values; | ||
| 524 | |||
| 525 | my %rules = %{ $conf->{karma_rules} }; | ||
| 526 | while (my ($rulename, $data) = each(%rules)) { | ||
| 527 | my ($context, $min, $max) = @$data; | ||
| 528 | # my $response = $responses->{$context}; | ||
| 529 | # next unless $response; | ||
| 530 | # my $composite = $conf->{karma_feedset}->{$context}; | ||
| 531 | # my $value = $response->value($composite); | ||
| 532 | # print STDERR Dumper('feedset', $composite, $value); | ||
| 533 | my $value = $values{$context}; | ||
| 534 | next unless defined $value; | ||
| 535 | next if $value < $min; | ||
| 536 | next if $value > $max; | ||
| 537 | $scanner->got_hit($rulename); | ||
| 538 | } | ||
| 539 | |||
| 540 | return undef; | ||
| 541 | } | ||
| 542 | |||
| 543 | # This doesn't do anything. | ||
| 544 | sub check_karma_range { | ||
| 545 | my ($self, $scanner, $message, $key, $min, $max) = @_; | ||
| 546 | return 0; | ||
| 547 | } | ||
| 548 | |||
| 549 | ######################################################################## | ||
| 550 | |||
| 551 | =head1 NAME | ||
| 552 | |||
| 553 | Mail::SpamAssassin::Plugin::Karmasphere - Query the Karmasphere reputation system | ||
| 554 | |||
| 555 | =head1 SYNOPSIS | ||
| 556 | |||
| 557 | loadplugin Mail::SpamAssassin::Plugin::Karmasphere | ||
| 558 | |||
| 559 | karma_feedset connect karmasphere.email-sender | ||
| 560 | karma_range KARMA_CONNECT_0_10 connect 0 10 | ||
| 561 | score KARMA_CONNECT_0_10 0.1 | ||
| 562 | |||
| 563 | =head1 DESCRIPTION | ||
| 564 | |||
| 565 | The Karmasphere reputation service is a real-time reputation | ||
| 566 | service for Internet identities. The aim of this plugin is | ||
| 567 | to detect identities used by spammers and phishers, and thus | ||
| 568 | detect zero-day spam runs and phishing scams. Conversely, it | ||
| 569 | can also detect identities used by reputable senders, and thus | ||
| 570 | use a whitelisting strategy to bypass further antispam checking | ||
| 571 | and (one hopes) reduce false positives. | ||
| 572 | |||
| 573 | This plugin performs lookups against the Karmasphere reputation | ||
| 574 | service. Two lookups are performed: One on the connect-time identities | ||
| 575 | (client-ip, helo-address and envelope-from) and one on any identities | ||
| 576 | found in the body of the message. Of these, the first is relatively | ||
| 577 | trustworthy, since it works (where possible) with authenticated | ||
| 578 | identities. The second works with unathenticated identities, but | ||
| 579 | should still trap URLs used by spammers and phishing sites. | ||
| 580 | |||
| 581 | =head1 USER SETTINGS | ||
| 582 | |||
| 583 | An extremely simplistic, minimal example configuration file is provided | ||
| 584 | in the eg/spamassassin/ subdirectory of this distribution. An | ||
| 585 | administrator would be expected to write a more complex | ||
| 586 | configuration file including more useful score ranges. The details | ||
| 587 | of a particular configuration file will depend on the choice of | ||
| 588 | feedsets used for the various context. | ||
| 589 | |||
| 590 | The very similar-looking words 'context', 'connect' and 'content' | ||
| 591 | are used throughout this document. Please read carefully. | ||
| 592 | |||
| 593 | Valid B<contexts> are B<connect>, for a karma query relating to | ||
| 594 | connection-time metadata, and B<content> for content-filtering data. | ||
| 595 | |||
| 596 | =over 4 | ||
| 597 | |||
| 598 | =item B<karma_range> rulename context min max | ||
| 599 | |||
| 600 | A karma score range. B<context> is either B<connect> or B<content> | ||
| 601 | |||
| 602 | =item B<karma_feedset> context feedsetname | ||
| 603 | |||
| 604 | The feedset name to query in the given context information. The | ||
| 605 | default for the B<connect> context is C<karmasphere.email-sender>. | ||
| 606 | The default for the B<content> filter context is | ||
| 607 | C<karmasphere.contentfilter>. | ||
| 608 | |||
| 609 | =back | ||
| 610 | |||
| 611 | =head1 ADMINISTRATOR SETTINGS | ||
| 612 | |||
| 613 | =over 4 | ||
| 614 | |||
| 615 | =item B<karma_host> | ||
| 616 | |||
| 617 | Hostname or IP address of the Karmasphere query server. | ||
| 618 | The default is C<query.karmasphere.com>. | ||
| 619 | |||
| 620 | =item B<karma_port> | ||
| 621 | |||
| 622 | Port number of the Karmasphere query server. | ||
| 623 | The default is C<8666>. | ||
| 624 | |||
| 625 | =item B<karma_timeout> | ||
| 626 | |||
| 627 | The timeout for receiving Karmasphere responses, in seconds. | ||
| 628 | The default is C<15>. | ||
| 629 | |||
| 630 | =item B<karma_principal> | ||
| 631 | |||
| 632 | An identifier may be used to authenticate queries from | ||
| 633 | SpamAssassin to Karmasphere. It is not required for most | ||
| 634 | cases. See | ||
| 635 | http://my.karmasphere.com/devzone/client/configuration#credentials | ||
| 636 | |||
| 637 | That web page talks in terms of "username" and "password. | ||
| 638 | |||
| 639 | Karma_principal corresponds to "username". | ||
| 640 | |||
| 641 | =item B<karma_credentials> | ||
| 642 | |||
| 643 | See karma_principal, above. | ||
| 644 | |||
| 645 | Karma_credentials corresponds to "password". | ||
| 646 | |||
| 647 | =back | ||
| 648 | |||
| 649 | =head1 TEMPLATE TAGS | ||
| 650 | |||
| 651 | This module adds two extra template tags for header rewriting: | ||
| 652 | _KARMASCORE(C<context>)_ and _KARMADATA(C<context>)_, which expand | ||
| 653 | to the numeric score, and explanatory data generated by Karmasphere | ||
| 654 | in the given context. For example, to generate a "traditional" karma | ||
| 655 | header for the connect context, use: | ||
| 656 | |||
| 657 | add_header all Karma-Connect _KARMASCORE(connect)_: _KARMADATA(connect)_ | ||
| 658 | |||
| 659 | Due to the limitations of SpamAssassin, it is impossible to generate | ||
| 660 | a header "X-Karma". The generated headers are all prefixed with | ||
| 661 | "X-Spam-". Post-filter programs designed to work with this Karma plugin | ||
| 662 | will therefore need to look for the configured header variants instead | ||
| 663 | of X-Karma. | ||
| 664 | |||
| 665 | =head1 INTERNALS | ||
| 666 | |||
| 667 | The plugin hooks two points in the SpamAssassin scanner cycle. It | ||
| 668 | sends Karmasphere queries during the parsed_metadata callback, | ||
| 669 | and it receives responses during the check_post_dnsbl callback. | ||
| 670 | |||
| 671 | Several hooks are provided for the user to alter the query packet | ||
| 672 | constructed. The routine which builds the packet by default calls | ||
| 673 | the following routines on itself to add fields to the query: | ||
| 674 | |||
| 675 | =over 4 | ||
| 676 | |||
| 677 | =item $self->add_connect_authentication($scanner, $query) | ||
| 678 | |||
| 679 | Adds any information gathered from Authentication-Results headers | ||
| 680 | to the given connection query packet. This is usually authenticated | ||
| 681 | versions of of various identities checked by SPF, DKIM or other | ||
| 682 | external mechanisms. | ||
| 683 | |||
| 684 | =item $self->add_connect_received($scanner, $query) | ||
| 685 | |||
| 686 | Adds any information gathered from Received headers to the given | ||
| 687 | connection query packet. This frequently includes the client IP, | ||
| 688 | mail from address, and so forth. | ||
| 689 | |||
| 690 | =item $self->add_connect_envfrom($scanner, $query) | ||
| 691 | |||
| 692 | Adds the information gathered from $scanner->get('EnvelopeFrom:addr') | ||
| 693 | to the given connection query packet. | ||
| 694 | |||
| 695 | =item $self->add_connect_other($scanner, $query) | ||
| 696 | |||
| 697 | By default, this does nothing. This is the recommended extension | ||
| 698 | point for custom fields in the connection query packet. | ||
| 699 | |||
| 700 | =item $self->add_content_urls($scanner, $query) | ||
| 701 | |||
| 702 | Adds URLs found in the body to the given content query packet. | ||
| 703 | |||
| 704 | =item $self->add_content_other($scanner, $query) | ||
| 705 | |||
| 706 | By default, this does nothing. This is the recommended extension | ||
| 707 | point for custom fields in the content query packet. | ||
| 708 | |||
| 709 | =back | ||
| 710 | |||
| 711 | The plugin is designed to be subclassed by modulesoverriding the | ||
| 712 | add_*_other() routines. If you do this, you must load your subclass | ||
| 713 | instead of this class in your configuration file. | ||
| 714 | |||
| 715 | If SpamAssassin's debugging for the 'karma' facility is enabled | ||
| 716 | (i.e. would_log('dbg', 'karma') returns true), then access to the | ||
| 717 | L<Mail::Karmasphere::Client> Debug mechanism is provided via a | ||
| 718 | global variable $Mail::SpamAssassin::Plugin::Karmasphere::DEBUG. If | ||
| 719 | that variable contains a subroutine reference, the referenced | ||
| 720 | subroutine will be called as the L<Mail::Karmasphere::Client> | ||
| 721 | Debug routine. | ||
| 722 | |||
| 723 | Developers needing more information about any of the above features | ||
| 724 | should dig into the source code. | ||
| 725 | |||
| 726 | =head1 BUGS | ||
| 727 | |||
| 728 | Ensure that Authentication-Results headers are only honoured when | ||
| 729 | not preceded by a Received line. | ||
| 730 | |||
| 731 | =head1 INSTALLATION | ||
| 732 | |||
| 733 | These instructions get the plugin working with SpamAssassin. | ||
| 734 | |||
| 735 | After a successful CPAN install of | ||
| 736 | Mail::Karmasphere::Client, you should find Karmasphere.pm | ||
| 737 | present alongside your other SpamAssassin plugins. | ||
| 738 | |||
| 739 | First, we need to | ||
| 740 | |||
| 741 | loadplugin Mail::SpamAssassin::Plugin::SPF | ||
| 742 | |||
| 743 | Look in /etc/mail/spamassassin or /etc/spamassassin. | ||
| 744 | |||
| 745 | If C<init.pre> exists, place the loadplugin line there. | ||
| 746 | Otherwise, insert it at the top of C<local.cf>. | ||
| 747 | |||
| 748 | Then add the following lines to the end of local.cf: | ||
| 749 | |||
| 750 | ifplugin Mail::SpamAssassin::Plugin::Karmasphere | ||
| 751 | |||
| 752 | karma_feedset connect karmasphere.email-sender | ||
| 753 | karma_feedset content karmasphere.email-body | ||
| 754 | |||
| 755 | karma_range KARMA_CONNECT_POSITIVE connect 300 1000 | ||
| 756 | karma_range KARMA_CONNECT_NEGATIVE connect -1000 -300 | ||
| 757 | |||
| 758 | karma_range KARMA_CONTENT_POSITIVE content 300 1000 | ||
| 759 | karma_range KARMA_CONTENT_NEGATIVE content -1000 -300 | ||
| 760 | |||
| 761 | score KARMA_CONNECT_POSITIVE -2.0 | ||
| 762 | score KARMA_CONNECT_NEGATIVE 2.0 | ||
| 763 | score KARMA_CONTENT_POSITIVE -1.0 | ||
| 764 | score KARMA_CONTENT_NEGATIVE 1.0 | ||
| 765 | |||
| 766 | add_header all Karma-Connect _KARMASCORE(connect)_: _KARMADATA(connect)_ | ||
| 767 | add_header all Karma-Content _KARMASCORE(content)_: _KARMADATA(content)_ | ||
| 768 | |||
| 769 | endif # Mail::SpamAssassin::Plugin::Karmasphere | ||
| 770 | |||
| 771 | |||
| 772 | =head1 TODO | ||
| 773 | |||
| 774 | Use the data fields from the Karma response to construct an | ||
| 775 | explanation message. | ||
| 776 | |||
| 777 | =head1 SEE ALSO | ||
| 778 | |||
| 779 | L<Mail::Karmasphere::Client>, | ||
| 780 | http://www.karmasphere.com/, | ||
| 781 | L<Mail::SpamAssassin>, | ||
| 782 | L<Mail::SpamAssassin::Conf>, | ||
| 783 | L<Mail::SpamAssassin::Logger>, | ||
| 784 | L<eg/spamassassin/26_karmasphere.cf> | ||
| 785 | |||
| 786 | =head1 COPYRIGHT | ||
| 787 | |||
| 788 | Copyright (c) 2005-2006 Shevek, Karmasphere. All rights reserved. | ||
| 789 | |||
| 790 | This program is free software; you can redistribute it and/or modify | ||
| 791 | it under the same terms as Perl itself. | ||
| 792 | |||
| 793 | =cut | ||
| 794 | |||
| 795 | 1; |