[Mimedefang] temp failing - got code?

Minica, Nelson (EDS) Nelson.Minica at RailAmerica.com
Wed Jan 5 15:31:01 EST 2005


Here is some code I've used on a small domain with MySQL.  If they retry
after 10 minutes and before 24 hours they are permanently let through
the greylist filter as long as they send from same classc.  Hopefully
this will help you.

sub check_greylist(){   # return 0 is tempfail, 1 is permit
  if($TrustedRelay==1 || $Sender eq "<>" || $sender =~
/(MAILER-DAEMON|postmaster)/i) { return 1; }
  $dbh = DBI->connect("DBI:mysql:dbname","dbuser",'dbpass');
  $blackperiod=10*60;   #10 minutes
  $greyperiod=24*60*60; #24 hours
  $from = lc($Sender);
  $from =~ s/[<>]//g;
  ( $username, $domain ) = split(/\@/,$from);
  @classc=split(/\./,$RelayAddr);
  foreach my $mailto (@Recipients) {
    $mailto =~ s/[<>]//g;
    $sth = $dbh->prepare("SELECT greystatus,inittime FROM greylist WHERE
domain='$domain' AND ip LIKE '$classc[0].$classc[1].$classc[2].%' AND
(greystatus=1 OR mailto='$mailto') ORDER BY greystatus DESC LIMIT 1");
    $sth->execute;
    if (($greystatus,$inittime)=$sth->fetchrow() ) {
      $timediff = time() - $inittime;
      if ($greystatus == 1) {
        md_syslog('info', "GREYLIST: $domain
$classc[0].$classc[1].$classc[2] Already white");
        $rc=1;
        }
      elsif ($timediff > $blackperiod && $timediff < $greyperiod) {
        $dbh->do("UPDATE greylist SET greystatus=1 WHERE
domain='$domain' AND ip LIKE '$classc[0].$classc[1].$classc[2].%'");
        md_syslog('info', "GREYLIST: $domain
$classc[0].$classc[1].$classc[2] Whitelisted now");
        $rc=1;
        }
      elsif ($timediff > $blackperiod && $timediff > $greyperiod) {
        $dbh->do("UPDATE greylist SET inittime='".time()."' WHERE
domain='$domain' AND ip LIKE '$classc[0].$classc[1].$classc[2].%'");
        md_syslog('info', "GREYLIST: $domain
$classc[0].$classc[1].$classc[2] Greylisted again");
        $rc=0;
        }
      else {
        md_syslog('info', "GREYLIST: $domain
$classc[0].$classc[1].$classc[2] Black still");
        $rc=0;
        }
      }
    else {
      $dbh->do("INSERT INTO greylist
(greystatus,inittime,user,domain,ip,mailto) VALUES
(0,'".time()."','$username','$domain','$RelayAddr','$mailto')");
      md_syslog('info', "GREYLIST: $domain
$classc[0].$classc[1].$classc[2] Greylisted now");
      $rc=0;
      }
    $sth->finish;
    }
  $dbh->disconnect();
  return $rc;
  }

CREATE TABLE greylist (
  greystatus int(11) NOT NULL default '0',
  inittime varchar(20) NOT NULL default '',
  user varchar(200) NOT NULL default '',
  domain varchar(200) NOT NULL default '',
  ip varchar(15) NOT NULL default '',
  mailto varchar(200) NOT NULL default '',
  PRIMARY KEY  (domain,ip,mailto),
  KEY status (greystatus)
)




More information about the MIMEDefang mailing list