#!/usr/local/bin/perl # Copyright (c) 2004-2006 by Jeremy Kister # Author: Jeremy Kister # Name: Email Secretary (ESec) # Function: Challange Response system to deter spam use strict; use DBI; #%%VERSION%% #%%DBUN%% #%%DBPW%% #%%DSN%% my %pnddir; my $dbh = DBI->connect($dsn, $dbun, $dbpw,{PrintError => 1}); # expire known bouncing users after 24 hours my $time = ($^T - 172800); my $sql = "DELETE FROM known_bouncers WHERE time < $time"; my $sth = $dbh->prepare($sql); $sth->execute || warn "cannot delete from known_bouncers: $!\n"; #clean up database and remove pending messages older than 1 week 1 day # because qmail bounces messages after ~7 days, and we would rather it # nuke the message from the pnd queue than get the double bounce my $weekago = ($^T - 691200); my $sql = "SELECT filename,emailid FROM messages WHERE time < $weekago"; my $sth = $dbh->prepare($sql); $sth->execute || die "cannot execute select: $!\n"; while(my $row=$sth->fetchrow_arrayref){ my $filename=$row->[0]; my $emailid=$row->[1]; unless(exists($pnddir{$emailid})){ my $sql1 = 'SELECT email FROM accounts WHERE emailid = ' . $dbh->quote($emailid); my $sth1 = $dbh->prepare($sql1); $sth1->execute; my $row1 = $sth1->fetchrow_arrayref; my ($user,$domain) = split(/\@/, $row1->[0]); my $dir; open(A, '/var/qmail/users/assign') || die "cannot open assign: $!\n"; while(){ if(/^\+${domain}\-:[^:]+:\d+:\d+:([^:]+):/){ $dir = $1; last; } } close A; # what if mail is not delviered on the host this is running on? if(-d $dir){ if(open(V, "${dir}/vpasswd")){ while(){ if(/^${user}:[^:]+:\d+:\d+:[^:]*:([^:]+):/){ $pnddir{$emailid} = "$1/Maildir/pnd"; last; } } close V; }else{ die "cannot open ${dir}/vpasswd: $!\n"; } } } if(unlink("$pnddir{$emailid}/${filename}")){ my $sql = 'DELETE FROM messages WHERE filename = ' . $dbh->quote($filename); $sql .= ' AND emailid = ' . $dbh->quote($emailid); my $sth = $dbh->prepare($sql); $sth->execute || warn "cannot remove $filename from db: $!\n"; }else{ warn "could not unlink $pnddir{$emailid}/${filename}: $!\n"; } }