exim: update exim_surbl.pl to version 2.3
[mirror/dsa-puppet.git] / modules / exim / files / common / exim_surbl.pl
index dd406a5..40c6106 100644 (file)
@@ -1,9 +1,9 @@
 #
 ## THIS FILE IS UNDER PUPPET CONTROL. DON'T EDIT IT HERE.
 ## USE: git clone git+ssh://$USER@puppet.debian.org/srv/puppet.debian.org/git/dsa-puppet.git
-##
-#
-# Copyright (c) 2006-2007 Erik Mugele.  All rights reserved.
+
+-#
+# Copyright (c) 2006-2012 Erik Mugele.  All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-# NOTES
-# -----
-#
-# 1. This script makes use of the Country Code Top Level 
-# Domains (ccTLD) provided by the SURBL group at
-# http://spamcheck.freeapp.net/two-level-tlds  
-# THE VARIABLE $cctld_file MUST BE SET TO THE FULL PATH AND 
-# NAME OF THE FILE CONTAINING THE CCTLD LIST!  (see below)
-#
-# 2. This script makes use of whitelisting of popular domains.  The 
-# source of the list can be found here: 
-# http://spamassassin.apache.org/full/3.1.x/dist/rules/25_uribl.cf
-# These are domains that are whitelisted by the SURBL group so it
-# doesn't make sense to waste resources doing lookups on them.
-# THE VARIABLE $whitelist_file MUST BE SET TO THE FULL PATH AND
-# NAME OF THE FILE CONTAINING THE WHITE LIST!  (see below)          
-# 
-# 3. Per the guidelines at http://www.surbl.org, if your site processes
-# more than 100,000 messages per day, you should NOT be using the 
-# public SURBL name servers but should be rsync-ing from them and 
-# running your own.  See http://www3.surbl.org/rsync-signup.html
-#
+
 sub surblspamcheck
 {
-# Designed and written by Erik Mugele, 2004-2006
-# http://www.teuton.org/~ejm
-# Version 2.0
+
+# Designed and written by Erik Mugele, 2004-2010,1http://www.teuton.org/~ejm
+# Version 2.3-beta
+#
+# Please see the following website for details on usage of
+# this script:  http://www.teuton.org/~ejm/exim_surbl
 
     # The following variable is the full path to the file containing the 
-    # list of Country Code Top Level Domains (ccTLD).
+    # two-level top level domains (TLD).
     # ---------------------------------------------------------------------
     # THIS VARIABLE MUST BE SET TO THE FULL PATH AND NAME OF THE FILE 
-    # CONTAINING THE CCTLD LIST!
+    # CONTAINING THE TWO LEVEL TLD!
     # ---------------------------------------------------------------------
-    my $cctld_file = "/etc/exim4/ccTLD.txt";    
-    
+    my $twotld_file = "/etc/exim4/two-level-tlds";    
+
+    # The following variable is the full path to the file containing the 
+    # three-level top level domains (TLD).
+    # ---------------------------------------------------------------------
+    # THIS VARIABLE MUST BE SET TO THE FULL PATH AND NAME OF THE FILE 
+    # CONTAINING THE THREE LEVEL TLD!
+    # ---------------------------------------------------------------------
+    my $threetld_file = "/etc/exim4/three-level-tlds";
+
     # The following variable is the full path to the file containing
     # whitelist entries.  
     # ---------------------------------------------------------------------
@@ -74,11 +63,12 @@ sub surblspamcheck
     # keep the load down on the server.  Size is in bytes.
     my $max_file_size = 50000;
     
-    # The following two variables enable or disable the SURBL and URIBL
+    # The following variables enable or disable the SURBL, URIBL and DBL
     # lookups.  Set to 1 to enable and 0 to disable.
     my $surbl_enable = 1;
     my $uribl_enable = 1;
-    
+    my $dbl_enable = 1;
+
     # Check to see if a decode MIME attachment is being checked or 
     # just a plain old text message with no attachments
     my $exim_body = "";
@@ -139,29 +129,58 @@ sub surblspamcheck
         # This subroutine does the actual DNS lookup and builds and returns
         # the return message for the URIBL check.
         my @params = @_;
-        my $surbldomain = ".black.uribl.com";
-        @dnsbladdr=gethostbyname($params[0].$surbldomain);
+        my $uribldomain = ".black.uribl.com";
+        @dnsbladdr=gethostbyname($params[0].$uribldomain);
         # If gethostbyname() returned anything, build a return message.
         $return_string = "";
         if (scalar(@dnsbladdr) != 0) {
             $return_string = "Blacklisted URL in message. (".$params[0].") in";
-            @surblipaddr = unpack('C4',($dnsbladdr[4])[0]);
-            if ($surblipaddr[3] & 8) {
+            @ipaddr = unpack('C4',($dnsbladdr[4])[0]);
+            if ($ipaddr[3] & 8) {
                 $return_string .= " [red]";
             }
-            if ($surblipaddr[3] & 4) {
+            if ($ipaddr[3] & 4) {
                 $return_string .= " [grey]";
             }
-            if ($surblipaddr[3] & 2) {
+            if ($ipaddr[3] & 2) {
                 $return_string .= " [black]";
             }
             $return_string .= ". See http://lookup.uribl.com.";
         }
         return $return_string;
     }
+
+    sub dbllookup {
+        # This subroutine does the actual DNS lookup and builds and returns
+        # the return message for the Spamhaus DBL check.
+        my @params = @_;
+        my $dbldomain = ".dbl.spamhaus.org";
+        @dnsbladdr=gethostbyname($params[0].$dbldomain);
+        # If gethostbyname() returned anything, build a return message.
+        $return_string = "";
+        if (scalar(@dnsbladdr) != 0) {
+            $return_string = "Blacklisted URL in message: ".$params[0];
+            $return_string .= ". See http://www.spamhaus.org/lookup.lasso?dnsbl=domain.";
+        }
+        return $return_string;
+    }
+
+    sub mkaddress {
+        # This subroutine takes a list of domain parts
+        # (e.g. ["www","example","com"]) and a number (e.g. 2) and returns a 
+        # the address of the given number of parts (e.g. example.com).
+        my $numparts = @_[-1];
+        pop(@_);
+        my @domain = @_;
+        my $address = $domain[-1];
+        for (my $i=2; $i<=$numparts; $i++) {
+            $address = $domain[-$i].".".$address;
+        }
+        return $address;
+    }
     
     sub converthex {
-        # This subroutin converts two hex characters to an ASCII character.
+        # This subroutine converts two hex characters to an ASCII character.
         # It is called when ASCII obfuscation or Printed-Quatable characters
         # are found (i.e. %AE or =AE).
         # It should return a converted/plain address after splitting off
@@ -182,15 +201,22 @@ sub surblspamcheck
 
     if ($exim_body) {
         # Find all the URLs in the message by finding the HTTP string
-        @parts = split /[hH][tT][tT][pP]:\/\//,$exim_body;
+        @parts = split(/[hH][tT][tT][pP](:|=3[aA])(\/|=2[Ff])(\/|=2[Ff])/,$exim_body);
         if (scalar(@parts) > 1) {
-            # Read the entries from the ccTLD file.
-            open (cctld_handle,$cctld_file) or die "Can't open $cctld_file.\n";
-            while (<cctld_handle>) {
+            # Read the entries from the two-level TLD file.
+            open (twotld_handle,$twotld_file) or die "Can't open $twotld_file.\n";
+            while (<twotld_handle>) {
                 next if (/^#/ || /^$/ || /^\s$/);
-                push(@cctlds,$_);
+                push(@twotlds,$_);
             }
-            close (cctld_handle) or die "Close: $!\n";
+            close (twotld_handle) or die "Close: $!\n";
+            # Read the entries from the three-level TLD file.
+            open (threetld_handle,$threetld_file) or die "Can't open $threetld_file.\n";
+            while (<threetld_handle>) {
+                next if (/^#/ || /^$/ || /^\s$/);
+                push(@threetlds,$_);
+            }
+            close (threetld_handle) or die "Close: $!\n";
             # Read the entries from the whitelist file.
             open (whitelist_handle,$whitelist_file) or die "Can't open $whitelist_file.\n";
             while (<whitelist_handle>) {
@@ -198,122 +224,379 @@ sub surblspamcheck
                 push(@whitelist,$_);
             }
             close (whitelist_handle) or die "Close: $!\n";
+
+            @surbl_list = ();
+            @uribl_list = ();
+            @dbl_list = ();
+
             # Go through each of the HTTP parts that were found in the message
             for ($i=1; $i < scalar(@parts); $i++) {
                 # Special case of Quoted Printable EOL marker
                 $parts[$i] =~ s/=\n//g;
-                    # Split the parts and find the address portion of the URL.
+
+                # Split the parts and find the address portion of the URL.
                 # Address SHOULD be either a FQDN, IP address, or encoded address.
                 $address = (split(/[^A-Za-z0-9\._\-%=]/,$parts[$i]))[0];
+
                 # Check for an =.  If it exists, we assume the URL is doing 
-                # Quoted-Printable.  Decode it and redine $address
+                # Quoted-Printable.  Decode it and redefine $address
                 if ($address =~ /=/) {
-                    @ob_parts = split /=/,$address;
+                    @ob_parts = split(/=/,$address);
                     $address = converthex(@ob_parts);
                 }
+
                 # Check for a %.  If it exists the URL is using % ASCII
                 # obfuscation.  Decode it and redefine $address.
                 if ($address =~ /%/) {
-                    @ob_parts = split /%/,$address;
+                    @ob_parts = split(/%/,$address);
                     $address = converthex(@ob_parts);
                 }
+
+                # Convert the address to lower case.
+                $address = lc($address);
+
                 # Split the the address into the elements separated by periods.
-                @domain = split /\./,$address;
+                @domain = split(/\./,$address);
+
                 # Check the length of the domain name.  If less then two elements
                 # at this point it is probably bogus or there is a bug in one of 
                 # the decoding/converting routines above.
-                if (scalar(@domain) >= 2) {
-                    $return_result="";
-                    # By default, assume that the domain check is on a 
-                    # "standard" two level domain
-                    $spamcheckdomain=$domain[-2].".".$domain[-1];
-                    # Check for a two level domain
-                    if (((scalar(@domain) == 2) || (scalar(@domain) >= 5))  && 
-                        (grep(/^$spamcheckdomain$/i,@cctlds))) {
-                        $return_result="cctld";
-                    }
-                    # Check for a three level domain
-                    if (scalar(@domain) == 3) {
-                        if (grep(/^$spamcheckdomain$/i,@cctlds)) {
-                            $spamcheckdomain=$domain[-3].".".$spamcheckdomain;
-                            if (grep(/^$spamcheckdomain$/,@cctlds)) {
-                                $return_result="cctld";
+                if (scalar(@domain) >=2) {
+                    $spamcheckdomain = "";
+
+                    # DEBUG statement.
+                    #warn ("FOUND DOMAIN: ".mkaddress(@domain,scalar(@domain))."\n");
+
+                    # Domain has two or more than four elements.
+                    if ((scalar(@domain) == 2) || (scalar(@domain) >=5)) {
+                        # Add two elements of the domain to the list(s).
+                        $spamcheckdomain=mkaddress(@domain,2);
+                        # Check if $spamcheckdomain is not in the whitelist.
+                        if (! grep(/^$spamcheckdomain$/i,@whitelist)) {
+                            # If SURBL checks are enabled and the domain is
+                            # not in the SURBL list, add it.
+                            if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                ($surbl_enable == 1)) {
+                                push(@surbl_list,$spamcheckdomain);
+                            }
+                            # If URIBL checks are enabled and the domain is
+                            # not in the URIBL list, add it.
+                            if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                ($uribl_enable == 1)) {
+                                push(@uribl_list,$spamcheckdomain);
+                            }
+                            # If DBL checks are enabled and the domain is
+                            # not in the DBL list, add it.
+                            if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                ($dbl_enable == 1)) {
+                                push(@dbl_list,$spamcheckdomain);
                             }
                         }
                     }
-                    # Check for a four level domain
-                    if (scalar(@domain) == 4) {
-                        # Check to see if the domain is an IP address
-                        if ($domain[-1] =~ /[a-zA-Z]/) {
-                            if (grep(/^$spamcheckdomain$/i,@cctlds)) {
-                                $spamcheckdomain=$domain[-3].".".$spamcheckdomain;
-                                if (grep(/^$spamcheckdomain$/i,@cctlds)) {
-                                    $spamcheckdomain=$domain[-4].".".$spamcheckdomain;
+
+                    # Domain has three elements.
+                    if (scalar(@domain) == 3) {
+                        # Set $spamcheckdomain to two elements.
+                        $spamcheckdomain = mkaddress(@domain,2);
+                        $two_checkdomain = $spamcheckdomain;
+                        if (grep(/^$spamcheckdomain$/i,@twotlds)) {
+                            # $spamcheckdomain is in the two-level TLD list.
+                            # Reset $spamcheckdomain to three elements.
+                            $spamcheckdomain = mkaddress(@domain,3);
+                           # Check if $spamcheckdomain is not in the whitelist.
+                            if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                (! grep(/^two_checkdomain$/i,@whitelist))) {
+                               # If SURBL checks are enabled and the domain is
+                               # not in the SURBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                    ($surbl_enable == 1)) {
+                                    push(@surbl_list,$spamcheckdomain);
+                                }
+                               # If URIBL checks are enabled and the domain is
+                               # not in the URIBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                    ($uribl_enable == 1)) {
+                                    push(@uribl_list,$spamcheckdomain);
+                                }
+                               # If DBL checks are enabled and the domain is
+                               # not in the DBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                    ($dbl_enable == 1)) {
+                                    push(@dbl_list,$spamcheckdomain);
                                 }
                             }
-                        } else {
+                       } else {
+                            # $spamcheckdomain is not in the two-level TLD list.
+                            # $spamcheckdomain is still two elements.
+                           # Check if $spamcheckdomain is not in the whitelist.
+                            if (! grep(/^$spamcheckdomain$/i,@whitelist)) {
+                               # If SURBL checks are enabled and the domain is
+                               # not in the SURBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                    ($surbl_enable == 1)) {
+                                    push(@surbl_list,$spamcheckdomain);
+                                }
+                               # If URIBL checks are enabled and the domain is
+                               # not in the URIBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                    ($uribl_enable == 1)) {
+                                    push(@uribl_list,$spamcheckdomain);
+                                }
+                               # If DBL checks are enabled and the domain is
+                               # not in the DBL list, add it.
+                                if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                    ($dbl_enable == 1)) {
+                                    push(@dbl_list,$spamcheckdomain);
+                                }
+                            }
+                           # Reset $spamcheckdomain to three elements.
+                           $spamcheckdomain = mkaddress(@domain,3);
+                           # Check if $spamcheckdomain is not in the whitelist.
+                           if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                (! grep(/^$two_checkdomain$/i,@whitelist))) {
+                               # If URIBL checks are enabled and the domain is
+                               # not in the URIBL list, add it.
+                               if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                   ($uribl_enable == 1)) {
+                                   push(@uribl_list,$spamcheckdomain);
+                               }
+                               # If DBL checks are enabled and the domain is
+                               # not in the DBL list, add it.
+                               if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                   ($dbl_enable == 1)) {
+                                   push(@dbl_list,$spamcheckdomain);
+                               }
+                           }
+                       }
+                   }
+
+                    # Domain has four elements.
+                    if (scalar(@domain) == 4) {
+                        if ($domain[-1] =~ /^(\d){1,3}$/) {
                             # Domain is an IP address
-                            $spamcheckdomain=$domain[3].".".$domain[2].
+                           # Set $spamcheckdomain to the IP address in reverse.
+                            $spamcheckdomain = $domain[3].".".$domain[2].
                                 ".".$domain[1].".".$domain[0];
-                        }
-                    }
-                    # DEBUG statement
-                    #warn ("FOUND DOMAIN ($mime_filename): $spamcheckdomain\n");
-                    # If whitelisting is enabled check domain against the 
-                    # whitelist.
-                    if ($whitelist_file ne "") {
-                        foreach $whitelist_entry (@whitelist) {
-                            chomp($whitelist_entry);
-                            if ($spamcheckdomain =~ m/^$whitelist_entry$/i) {
-                                $return_result="whitelisted";
-                                last;
-                            }
-                        }
-                    }
-                    # If the domain is whitelisted or in the cctld skip adding
-                    # it to the lookup list.
-                    if ($return_result eq "") {
-                        if (scalar(@lookupdomains) > 0) {
-                            # Check so see if the domain already is in the list.
-                            if (not grep(/^$spamcheckdomain$/i,@lookupdomains)) {
-                                    push(@lookupdomains,$spamcheckdomain);
-                            }
-                        } else {
-                            push(@lookupdomains,$spamcheckdomain);
-                        }
-                    }
-                }
-            }
-            # If there are items in the lookupdomains list then
-            # perform lookups on them.  If there are not, something is wrong
-            # and just return false.  There should always be something in the list.
-            if (scalar(@lookupdomains) > 0) {
-                foreach $i (@lookupdomains) {
-                    # DEBUG statement.
-                    #warn ("CHECKING DOMAIN ($mime_filename): $i\n");
-                    # If SURBL lookups are enabled do an SURBL lookup
-                    if ($surbl_enable == 1) {
-                        $return_result = surbllookup($i);
+
+                           # Do NOT check IP addresses against the Spamhaus DBL list.
+
+                           # If SURBL checks are enabled and the IP is
+                           # not in the SURBL list, add it.
+                           if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                               ($surbl_enable == 1)) {
+                               push(@surbl_list,$spamcheckdomain);
+                           }
+                           # If URIBL checks are enabled and the IP is 
+                           # not in the URIBL list, add it.
+                           if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                               ($uribl_enable == 1)) {
+                               push(@uribl_list,$spamcheckdomain);
+                           }
+                       } else {
+                            # Domain is not an IP address.
+                           # Check if the last three elements of the domain are
+                           # in the three-level TLD list.
+                            $three_checkdomain = mkaddress(@domain,3);
+                            $two_checkdomain = mkaddress(@domain,2);
+                           if (grep(/^$three_checkdomain$/i,@threetlds)) {
+                               # Set $spamcheckdomain to four elements.
+                                $spamcheckdomain = mkaddress(@domain,4);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                                if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                    (! grep(/^three_checkdomain$/i,@whitelist)) &&
+                                    (! grep(/^two_checkdomain$/i,@whitelist))) {
+                                   # If SURBL checks are enabled and the domain is
+                                   # not in the SURBL list, add it.
+                                    if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                        ($surbl_enable == 1)) {
+                                        push(@surbl_list,$spamcheckdomain);
+                                    }
+                                   # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                    if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                        ($uribl_enable == 1)) {
+                                        push(@uribl_list,$spamcheckdomain);
+                                    }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                    if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                        ($dbl_enable == 1)) {
+                                        push(@dbl_list,$spamcheckdomain);
+                                    }
+                                }
+                           }
+
+                           # Check if the last two elements of the domain are
+                           # in the two-level TLD list.
+                           elsif (grep(/^$two_checkdomain$/i,@twotlds)) {
+                               # Reset $spamcheckdomain to three elements.
+                               $spamcheckdomain = mkaddress(@domain,3);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                               if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                    (! grep(/^two_checkdomain$/i,@whitelist))) {
+                                   # If SURBL checks are enabled and the domain is
+                                   # not in the SURBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                       ($surbl_enable == 1)) {
+                                       push(@surbl_list,$spamcheckdomain);
+                                   }
+                                    # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                       ($uribl_enable == 1)) {
+                                       push(@uribl_list,$spamcheckdomain);
+                                   }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                       ($dbl_enable == 1)) {
+                                       push(@dbl_list,$spamcheckdomain);
+                                   }
+                               }
+
+                               # Reset $spamcheckdomain to four elements.
+                               $spamcheckdomain = mkaddress(@domain,4);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                               if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                    (! grep(/^three_checkdomain$/i,@whitelist)) &&
+                                    (! grep(/^two_checkdomain$/i,@whitelist))) {
+                                   # If SURBL checks are enabled and the domain is
+                                   # not in the SURBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                       ($surbl_enable == 1)) {
+                                       push(@surbl_list,$spamcheckdomain);
+                                   }
+                                   # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                       ($uribl_enable == 1)) {
+                                       push(@uribl_list,$spamcheckdomain);
+                                   }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                       ($dbl_enable == 1)) {
+                                       push(@dbl_list,$spamcheckdomain);
+                                   }
+                               }
+
+                           } else {
+                               # Set $spamcheckdomain to two elements
+                               $spamcheckdomain = mkaddress(@domain,2);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                               if (! grep(/^$spamcheckdomain$/i,@whitelist)) {
+                                   # If SURBL checks are enabled and the domain is
+                                   # not in the SURBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@surbl_list) &&
+                                       ($surbl_enable == 1)) {
+                                       push(@surbl_list,$spamcheckdomain);
+                                   }
+                                   # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                       ($uribl_enable == 1)) {
+                                       push(@uribl_list,$spamcheckdomain);
+                                   }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                       ($dbl_enable == 1)) {
+                                       push(@dbl_list,$spamcheckdomain);
+                                   }
+                               }
+                               # Reset $spamcheckdomain to three elements
+                               $spamcheckdomain = mkaddress(@domain,3);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                               if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                    (! grep(/^$two_checkdomain$/i,@whitelist))) {
+                                   # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                       ($uribl_enable == 1)) {
+                                       push(@uribl_list,$spamcheckdomain);
+                                   }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                       ($dbl_enable == 1)) {
+                                       push(@dbl_list,$spamcheckdomain);
+                                   }
+                               }
+                               # Set $spamcheckdomain to four elements
+                               $spamcheckdomain = mkaddress(@domain,4);
+                               # Check if $spamcheckdomain is not in the whitelist.
+                               if ((! grep(/^$spamcheckdomain$/i,@whitelist)) &&
+                                    (! grep(/^$three_checkdomain$/i,@whitelist)) &&
+                                    (! grep(/^$two_checkdomain$/i,@whitelist))) {
+                                   # If URIBL checks are enabled and the domain is
+                                   # not in the URIBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@uribl_list) &&
+                                       ($uribl_enable == 1)) {
+                                       push(@uribl_list,$spamcheckdomain);
+                                   }
+                                   # If DBL checks are enabled and the domain is
+                                   # not in the DBL list, add it.
+                                   if (! grep(/^$spamcheckdomain$/i,@dbl_list) &&
+                                       ($dbl_enable == 1)) {
+                                       push(@dbl_list,$spamcheckdomain);
+                                   }
+                               }
+                           }
+                        } # End: if ($domain[-1] =~ /^(\d){1,3}$/)
+                   } # End: if (scalar(@domain) == 4)
+                } # End: if (scalar(@domain) >=2)
+            } # End: for ($i=1; $i < scalar(@parts); $i++)
+
+           # If there are items in the SURBL list and the SURBL check
+           # is enabled then perform lookups on them.
+           if ((scalar(@surbl_list) > 0) && 
+               ($surbl_enable == 1)) {
+               foreach $i (@surbl_list) {
+                   # DEBUG statement.
+                    #warn ("CHECKING DOMAIN ($mime_filename): $i in SURBL list.\n");
+                   $return_result = surbllookup($i);
+                    if ($return_result ne "") {
+                        return $return_result;
                     }
-                    # If URIBL lookups are enabled and the SURBL lookup failed
-                    # do a URIBL lookup
-                    if (($uribl_enable == 1) && ($return_result eq "")) {
-                        $return_result = uribllookup($i);
+               }
+           }
+
+           # If there are items in the URIBL list and the URIBL check
+           # is enabled and the previous lookup did not return a result
+           # then perform lookups on them.
+           if ((scalar(@uribl_list) > 0) && 
+               ($uribl_enable == 1) &&
+               ($return_result eq "")) {
+               foreach $i (@uribl_list) {
+                   # DEBUG statement.
+                    #warn ("CHECKING DOMAIN ($mime_filename): $i in URIBL list.\n");
+                   $return_result = uribllookup($i);
+                    if ($return_result ne "") {
+                        return $return_result;
                     }
-                    # If we got a hit return the result to Exim
+               }
+           }
+
+           # If there are items in the DBL list and the DBL check
+           # is enabled and the previous lookups did not return a result
+           # then perform lookups on them.
+           if ((scalar(@dbl_list) > 0) && 
+               ($dbl_enable == 1) &&
+               ($return_result eq "")) {
+               foreach $i (@dbl_list) {
+                   # DEBUG statement.
+                    #warn ("CHECKING DOMAIN ($mime_filename): $i in DBL list.\n");
+                   $return_result = dbllookup($i);
                     if ($return_result ne "") {
-                        undef @cctlds;
-                        undef @whitelist;
                         return $return_result;
                     }
-                }
-            }
-        }
-    }
-    # We didn't find any URLs or the URLs we did find were not
-    # listed so return false.
-    undef @cctlds;
-    undef @whitelist;
+               }
+           }
+        } # End: if (scalar(@parts) > 1)
+    } # End: if ($exim_body)
+
+    # No URLs were found or the URLs that were found were not
+    # listed in any list so return false.
     return false;
-}
 
+} # End Main: - sub surblspamcheck