You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by jm...@apache.org on 2004/03/05 03:07:25 UTC

svn commit: rev 7002 - in incubator/spamassassin/trunk: lib/Mail/SpamAssassin lib/Mail/SpamAssassin/Message lib/Mail/SpamAssassin/Plugin rules t

Author: jm
Date: Thu Mar  4 18:07:24 2004
New Revision: 7002

Modified:
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Dns.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/EvalTests.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Received.pm
   incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm
   incubator/spamassassin/trunk/rules/25_uribl.cf
   incubator/spamassassin/trunk/t/ip_addrs.t
Log:
a few changes from last night I hadn't checked in yet:

- Fixed fd leak in URIBL code, which was pretty serious after a big mass-check
  run.  my bad!

- URIBL code now correctly disables itself if mass-check --rules is used and it
  is not selected; previously it did not.

- added more paranoia to envelope-from retrieval, to deal with the case where
  an intermediate relay added an X-Envelope-From header (for example) then
  relayed into a mailing list, which in turn used a new env-from.  old code
  would have picked up X-Envelope-From, even though that was no longer
  appropriate.  New code checks for Received headers before that hdr, and
  ignores it if they exist.

- SPF now working well; only FPs I now get are from ISPs who've set up over-
  permissive SPF records (*cough BlueYonder.co.uk cough*) and from a couple
  of domains with buggy SPF records.

- added another (infrequent) Received line format (I think it's TIS -- now NAI
  -- fwtk).



Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Dns.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Dns.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Dns.pm	Thu Mar  4 18:07:24 2004
@@ -295,10 +295,21 @@
 sub rbl_finish {
   my ($self) = @_;
 
+  foreach my $type (keys %{$self->{dnspending}}) {
+    foreach my $host (keys %{$self->{dnspending}->{$type}}) {
+      if (defined $self->{dnspending}->{$type}->{$host}->[BGSOCK]) {
+	eval {
+	  # ensure the sockets are closed
+	  delete $self->{dnspending}->{$type}->{$host}->[BGSOCK];
+	};
+      }
+    }
+  }
   delete $self->{rbl_launch};
   delete $self->{dnspending};
+
+  # TODO: do not remove these since they can be retained!
   delete $self->{dnscache};
-  # TODO: do not remove this since it can be retained!
   delete $self->{dnspost};
   delete $self->{dnsresult};
   delete $self->{dnsuri};

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/EvalTests.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/EvalTests.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/EvalTests.pm	Thu Mar  4 18:07:24 2004
@@ -3145,34 +3145,31 @@
   my $sender = '';
 
   if ($ishelo) {
-    dbg ("SPF: checking HELO");
-
-    # drop any hostname parts, if we can.
-    my @domparts = split (/\./, $helo);
-    my $numparts = scalar @domparts;
-
-    # for the HELO variant, drop the first bit of the HELO (ie. turn
-    # "host.dom.ain" into "dom.ain".)
-    if ($numparts > 0) {
-      my $partsreqd = 2;
-      if (Mail::SpamAssassin::Util::is_in_subdelegated_cctld ($helo)) {
-        $partsreqd = 3;
-      }
-      if ($numparts >= $partsreqd) { $helo =~ s/^[^\.]+\.//; }
-    }
+    dbg ("SPF: checking HELO (helo=$helo, ip=$ip)");
+    $helo = Mail::SpamAssassin::Util::trim_domain_to_registrar_boundary ($helo);
+    dbg ("SPF: trimmed HELO down to '$helo'");
 
   } else {
-    dbg ("SPF: checking EnvelopeFrom");
     $sender = $lasthop->{envfrom};
 
     if ($sender) {
       dbg ("SPF: found Envelope-From in last untrusted Received header");
+    }
+    else {
+      # We cannot use the env-from data, since it went through 1 or
+      # more relays since the untrusted sender and they may have
+      # rewritten it.
+      #
+      if ($self->{num_relays_trusted} > 0) {
+	dbg ("SPF: relayed through one or more trusted relays, cannot use header-based Envelope-From, skipping");
+	return;
+      }
 
-    } else {
       # we can (apparently) use whatever the current Envelope-From was,
       # from the Return-Path, X-Envelope-From, or whatever header.
       # it's better to get it from Received though, as that is updated
       # hop-by-hop.
+      #
       $sender = $self->get ("EnvelopeFrom");
     }
 
@@ -3180,6 +3177,7 @@
       dbg ("SPF: cannot get Envelope-From, cannot use SPF");
       return;
     }
+    dbg ("SPF: checking EnvelopeFrom (helo=$helo, ip=$ip, envfrom=$sender)");
   }
 
   if (!$ip || !$helo) {

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message.pm	Thu Mar  4 18:07:24 2004
@@ -43,6 +43,8 @@
 
 package Mail::SpamAssassin::Message;
 use strict;
+use bytes;
+
 use Mail::SpamAssassin;
 use Mail::SpamAssassin::Message::Node;
 use Mail::SpamAssassin::Message::Metadata;

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm	Thu Mar  4 18:07:24 2004
@@ -35,6 +35,8 @@
 
 package Mail::SpamAssassin::Message::Node;
 use strict;
+use bytes;
+
 use Mail::SpamAssassin;
 use Mail::SpamAssassin::HTML;
 use MIME::Base64;

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm	Thu Mar  4 18:07:24 2004
@@ -2156,13 +2156,38 @@
   }
 
   # procmailrc notes this, amavisd are adding it, we recommend it
-  if ($envf = $self->get ("X-Envelope-From")) { goto ok; }
+  # (although we now recommend adding to Received instead)
+  if ($envf = $self->get ("X-Envelope-From")) {
+    # heuristic: this could have been relayed via a list which then used
+    # a *new* Envelope-from.  check
+    if ($self->get ("ALL") =~ /(?:^|\n)Received:\s.*\nX-Envelope-From:\s/s) {
+      dbg ("X-Envelope-From header found after 1 or more Received lines, cannot trust envelope-from");
+    } else {
+      goto ok;
+    }
+  }
 
   # qmail, new-inject(1)
-  if ($envf = $self->get ("Envelope-Sender")) { goto ok; }
+  if ($envf = $self->get ("Envelope-Sender")) {
+    # heuristic: this could have been relayed via a list which then used
+    # a *new* Envelope-from.  check
+    if ($self->get ("ALL") =~ /(?:^|\n)Received:\s.*\nEnvelope-Sender:\s/s) {
+      dbg ("Envelope-Sender header found after 1 or more Received lines, cannot trust envelope-from");
+    } else {
+      goto ok;
+    }
+  }
 
   # Postfix, sendmail, also mentioned in RFC821
-  if ($envf = $self->get ("Return-Path")) { goto ok; }
+  if ($envf = $self->get ("Return-Path")) {
+    # heuristic: this could have been relayed via a list which then used
+    # a *new* Envelope-from.  check
+    if ($self->get ("ALL") =~ /(?:^|\n)Received:\s.*\nReturn-Path:\s/s) {
+      dbg ("Return-Path header found after 1 or more Received lines, cannot trust envelope-from");
+    } else {
+      goto ok;
+    }
+  }
 
   # give up.
   return undef;

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/URIDNSBL.pm	Thu Mar  4 18:07:24 2004
@@ -121,6 +121,10 @@
 
   # only hit DNSBLs for which score != 0
   foreach my $rulename (keys %{$scanner->{conf}->{uridnsbls}}) {
+    # trim_rules() will remove the head_evals entry for this
+    next unless ($scanner->{conf}->{head_evals}->{$rulename});
+    # set score to 0 should also block the rule
+    next unless ($scanner->{conf}->{scores}->{$rulename});
     $scanstate->{active_rules}->{$rulename} = 1;
   }
 
@@ -217,6 +221,8 @@
       $scan->got_hit ($rulename, "");
     }
   }
+
+  $self->abort_remaining_lookups ($scanstate);
 }
 
 # ---------------------------------------------------------------------------
@@ -269,7 +275,9 @@
 
 sub complete_ns_lookup {
   my ($self, $scanstate, $ent, $dom) = @_;
+
   my $packet = $self->{res}->bgread($ent->{sock});
+  $self->close_ent_socket ($ent);
   my @answer = $packet->answer;
 
   foreach my $rr (@answer) {
@@ -301,6 +309,7 @@
   my ($self, $scanstate, $ent, $hname) = @_;
 
   my $packet = $self->{res}->bgread($ent->{sock});
+  $self->close_ent_socket ($ent);
   my @answer = $packet->answer;
 
   foreach my $rr (@answer) {
@@ -348,6 +357,7 @@
   my ($self, $scanstate, $ent, $dnsblip) = @_;
 
   my $packet = $self->{res}->bgread($ent->{sock});
+  $self->close_ent_socket ($ent);
   my @answer = $packet->answer;
   foreach my $rr (@answer) {
     my $str = $rr->string;
@@ -380,6 +390,7 @@
 
 # perform a poll of our lookups, to see if any are completed; if they
 # are, the next lookup in the sequence will be kicked off.
+
 sub complete_lookups {
   my ($self, $scanstate) = @_;
   my %typecount = ();
@@ -452,6 +463,7 @@
 	$scanstate->{times_count_was_same}++;
 	if ($scanstate->{times_count_was_same} > 20) {
 	  dbg ("URIDNSBL: escaping: must have lost requests");
+	  $self->abort_remaining_lookups ($scanstate);
 	  $stillwaiting = 0;
 	}
       } else {
@@ -462,6 +474,33 @@
   }
 
   return (!$stillwaiting);
+}
+
+# ---------------------------------------------------------------------------
+
+sub abort_remaining_lookups  {
+  my ($self, $scanstate) = @_;
+
+  my $pending = $scanstate->{pending_lookups};
+  my $foundone = 0;
+  foreach my $key (keys %{$pending})
+  {
+    if (!$foundone) {
+      dbg ("URIDNSBL: aborting remaining lookups");
+      $foundone = 1;
+    }
+
+    $self->close_ent_socket ($pending->{$key});
+    delete $pending->{$key};
+  }
+}
+
+sub close_ent_socket {
+  my ($ent) = @_;
+  if ($ent->{sock}) {
+    $ent->{sock}->close();
+    delete $ent->{sock};
+  }
 }
 
 # ---------------------------------------------------------------------------

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Received.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Received.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Received.pm	Thu Mar  4 18:07:24 2004
@@ -700,6 +700,13 @@
       $rdns = $1; $ip = $2; $by = $3; goto enough;
     }
 
+    # Received: from smtp.greyware.com(208.14.208.51, HELO smtp.sff.net) by x.x.org via smap (V1.3)
+    # id xma002908; Fri, 27 Feb 04 14:16:56 -0800
+    if (/^from (\S+)\((${IP_ADDRESS}), HELO (\S+)\) by (\S+) via smap /) {
+      $mta_looked_up_dns = 1;
+      $rdns = $1; $ip = $2; $helo = $3; $by = $4; goto enough;
+    }
+
     # Received: from [192.168.0.71] by web01-nyc.clicvu.com (Post.Office MTA
     # v3.5.3 release 223 ID# 0-64039U1000L100S0V35) with SMTP id com for
     # <x...@x.org>; Tue, 25 Mar 2003 11:42:04 -0500

Modified: incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm
==============================================================================
--- incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm	(original)
+++ incubator/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm	Thu Mar  4 18:07:24 2004
@@ -751,6 +751,41 @@
 
 ###########################################################################
 
+sub trim_domain_to_registrar_boundary {
+  my ($domain) = @_;
+
+  # drop any hostname parts, if we can.
+  my @domparts = split (/\./, $domain);
+  my $numparts = scalar @domparts;
+
+  # for the HELO variant, drop the first bit of the HELO (ie. turn
+  # "host.dom.ain" into "dom.ain".)
+  if ($numparts > 0) {
+    my $partsreqd = 2;
+
+    # the "Demon case"; Demon Internet registers domains for
+    # its customers, ie. "foo.demon.co.uk".
+    if ($domain =~ /\. (?:
+		      demon\.co\.uk
+		      )$/ix)
+    {
+      $partsreqd = 4;
+    }
+    # Subdelegated CCTLD
+    elsif (is_in_subdelegated_cctld ($domain)) {
+      $partsreqd = 3;
+    }
+
+    while ($numparts > $partsreqd) {
+      $domain =~ s/^[^\.]+\.//;
+      $numparts--;
+    }
+  }
+  $domain;
+}
+
+###########################################################################
+
 sub dbg { Mail::SpamAssassin::dbg (@_); }
 
 1;

Modified: incubator/spamassassin/trunk/rules/25_uribl.cf
==============================================================================
--- incubator/spamassassin/trunk/rules/25_uribl.cf	(original)
+++ incubator/spamassassin/trunk/rules/25_uribl.cf	Thu Mar  4 18:07:24 2004
@@ -17,7 +17,8 @@
 header		URIBL_DSBL	eval:check_uridnsbl('URIBL_DSBL')
 describe	URIBL_DSBL	Contains a URL listed in the DSBL blocklist
 
-uridnsbl	URIBL_SORBS	dnsbl.sorbs.net.	A
-header		URIBL_SORBS	eval:check_uridnsbl('URIBL_SORBS')
-describe	URIBL_SORBS	Contains a URL listed in the SORBS blocklist
+# SORBS has many, many FPs in this form
+#uridnsbl	URIBL_SORBS	dnsbl.sorbs.net.	A
+#header		URIBL_SORBS	eval:check_uridnsbl('URIBL_SORBS')
+#describe	URIBL_SORBS	Contains a URL listed in the SORBS blocklist
 

Modified: incubator/spamassassin/trunk/t/ip_addrs.t
==============================================================================
--- incubator/spamassassin/trunk/t/ip_addrs.t	(original)
+++ incubator/spamassassin/trunk/t/ip_addrs.t	Thu Mar  4 18:07:24 2004
@@ -26,7 +26,7 @@
     rules_filename => "$prefix/rules",
 });
 
-plan tests => 58;
+plan tests => 66;
 
 sub tryone {
   my ($pat, $testip) = @_;
@@ -91,4 +91,25 @@
 ok (!tryone ($Mail::SpamAssassin::LOCALHOST, "3ffe:fffff:100:f101:210:a4ff:fee3:9566"));
 ok (!tryone ($Mail::SpamAssassin::LOCALHOST, "::192.168.0.1"));
 ok (!tryone ($Mail::SpamAssassin::LOCALHOST, "notlocalhost"));
+
+sub tsttrim {
+  my $dom = shift;
+  my $want = shift;
+  my $got = Mail::SpamAssassin::Util::trim_domain_to_registrar_boundary ($dom);
+  if ($got eq $want) {
+    return 1;
+  } else {
+    warn "trimmed $dom, wanted $want, got $got\n";
+    return 0;
+  }
+}
+
+ok (tsttrim ("foo.demon.co.uk", "foo.demon.co.uk"));
+ok (tsttrim ("bar.foo.demon.co.uk", "foo.demon.co.uk"));
+ok (tsttrim ("a.b.c.d.e.f.g.g.h.bar.foo.demon.co.uk", "foo.demon.co.uk"));
+ok (tsttrim ("de", "de"));
+ok (tsttrim ("jmason.org", "jmason.org"));
+ok (tsttrim ("localhost.jmason.org", "jmason.org"));
+ok (tsttrim ("localhost.jmason.edu.au", "jmason.edu.au"));
+ok (tsttrim ("localhost.jmason.edu.net", "edu.net"));