|
|
#!/usr/bin/perl -w
use Net::SMTP;
use strict;
# written for use with procmail...
#
# Written by @ (Aaron Thompson) 2002,2003
# added the ability to block email addresses.
#
# Making more full script that allows for easy implementation.
#
# Documentation
#
# When mail-deny is run the first time it will create
# the following:
# ~/.mail-deny
# ~/.mail-deny/blockip
# ~/.mail-deny/blockaddr
# ~/.mail-deny/log
#
# Description of files and usage...
#
# ~/.mail-deny/log
# This file contains the logs for all email
# processed. This only has information if
# $enableLog is set in this script.
#
# ~/.mail-deny/blockip
# This file is used to block email received from a
# specific IP address. Careful with this if the IP
# is near right after a line beginning with "Received"
# the message will be blocked. One IP per line.
#
# ~/.mail-deny/blockaddr
# This file is used to block email received from a
# specific email address. One email per line.
#
# Individual email responses.
# To send a response to an email you are blocking
# create a file in ~/.mail-deny/ with the same
# name as the email you it will be sent to..
#
# For example if you want to send a message to
# foo@somewhere.com create the file
# ~/.mail-deny/foo@somewhere.com
# With the text message that will be sent to
# the person.
#
# NOTES:
# ** Emails are sent from postmaster@$domain -
# make sure the messages are appropriate.
#
# ** The message in the file will NOT be send
# unless the email address of the same name
# is in ~/.mail-deny/blockaddr.
#
# Procmail configuration.
#
# The following lines need to be added to ~/.procmailrc
# put them BEFORE any other filtering or spam detection.
#
# #Used for mail-deny
# :0fw
# | /usr/local/bin/mail-deny
#
# :0:
# * ^X-mail-deny: YES
# $HOME/mail/denied
#
# the $HOME/mail/denied could be replaced
# with /dev/null
#
#Configure me
my $enableLog = "1"; # 0 == no; 1 == yes
#Read pipe and get username from email.
my @msg; my @tmp;
my $username;
while(<>){push(@msg,$_);}#elihw
@tmp = @msg;
$username="0";
foreach(@tmp){
if (/^To: .*<(.*)@/){$username=$1;}#fi
elsif (/^To: (.*)@/){$username=$1;}#fi
}#hcaerof
# if (! $username){$username = `/usr/bin/id -un`; chomp($username);}#fi
#Configuration.
my $home; $home = "/home/$username";
my $configdir; $configdir = "$home/.mail-deny";
my $badIPFile = "$configdir/blockip"; #one IP per line.
my $badAddrFile = "$configdir/blockaddr"; #one email addr per line.
my $logfile = "$configdir/log"; # logfile name - will create.
#Start workin...
my %badIP; my $ip;
my %badAddr; my $addr;
my $block;
&checkConfig;
&getIPlist;
&getEmailList;
$block = 0;
foreach(@tmp){
chomp;
if (($_ =~ m/^Received/) && ($_ =~ m/(\d+\.\d+\.\d+\.\d+)/)) {
$ip = $1;
if(exists($badIP{$ip})){
if($enableLog){writeLog("$ip blocked.");}#fi
$block++;
}
else{
if($enableLog){writeLog("$ip allowed.");}#fi
}#fi
}#fi
if (($_ =~ m/^From:/i) && ($_ =~ m/(\w{1,}[\-\_\.]{0,}\w{0,}[\@]{1}\w{1,}[\-\_\.]{0,}\w{0,}[\-\_\.]{0,}\w{0,}[\-\_\.]{0,}\w{0,}[\-\_\.]{0,}\w{0,})/)){
$addr = $1;
if(exists($badAddr{$addr})){
if($enableLog){writeLog("$addr blocked.");}#fi
if( -e "$configdir/$addr"){sendResponse($addr);}#fi
$block++;
}
else{
if($enableLog){writeLog("$addr allowed.");}#fi
}#fi
}#fi
}#hcaerof
if ($block > 0){&addHeader;}#fi
print @msg;
##########################################################################
sub addHeader{
my $header; $header = "X-mail-deny: YES\n";
my $flag; my $line;
while(@tmp){pop(@tmp);}
foreach $line (@msg){
if($line =~ /^To:/){$line .= $header;}#fi
push(@tmp,$line);
}#hcaerof
@msg = @tmp;
}#redaeHdda
##########################################################################
sub sendResponse{
# $_[0] == emailaddr to send response to..
#$smtp info found:
# http://faqchest.dynhost.com/prgm/perlu-l/perl-00/perl-0010/perl-001005/perl00103111_16616.html
# creditted to the Perl Cookbook by O'Reilly
my $server; $server = "localhost"; #default smtp server
my $domain; $domain = "localdomain";
my $mailTo; $mailTo = $_[0]; #default email reciever
my $mailFrom; $mailFrom = "postmaster\@$domain"; #default email sender
my $subject; $subject = "Message to $username\@$domain not delivered";
my $body; $body = "";
my $message; $message = "$configdir/$mailTo";
if (-e $message){
open (IN, "$message") or die "$message could not be opened.\n";
while(<IN>){$body .= $_;}#elihw
close(IN);
my $smtp = Net::SMTP->new($server);
$smtp->mail("$ENV{USER}\@$domain");
$smtp->to($mailTo);
$smtp->data();
$smtp->datasend("To: $mailTo\n");
$smtp->datasend("From: $mailFrom\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
$smtp->datasend($body);
$smtp->dataend();
$smtp->quit;
if($enableLog){writeLog("sent message $message.");}#fi
}else{
if($enableLog){writeLog("$message not found.");}#fi
}#fi
}#esnopseRdnes
##########################################################################
sub checkConfig{
if (! -e $configdir){
system("/bin/mkdir $configdir");
system("/usr/bin/touch $badIPFile");
system("/usr/bin/touch $badAddrFile");
if ($enableLog){
if (! -e $logfile){system("/usr/bin/touch $logfile");}#fi
}#fi
}#fi
}#gifnoCkcehc
##########################################################################
sub getIPlist{
if ( -e $badIPFile){
open(BH, "< $badIPFile");
while(<BH>) { chomp; $badIP{$_}++; }
close(BH);
}
else{
if($enableLog){writeLog("$badIPFile not found skipping...");}#fi
}#fi
}#tsilPIteg
##########################################################################
sub getEmailList{
if( -e $badAddrFile){
open(BA, "< $badAddrFile");
while(<BA>) { chomp; $badAddr{$_}++; }
close(BA);
}
else{
if($enableLog){writeLog("$badAddrFile not found skipping...");}#fi
}#fi
}#tsiLliamEteg
##########################################################################
sub writeLog{
my $date; $date = `/bin/date`; chomp($date);
open (LOGFILE, ">>$logfile") or die "Could not open $logfile for append.";
print LOGFILE "$date -- $_[0]\n";
close (LOGFILE);
}#goLetirw
syntax highlighted by Code2HTML, v. 0.9.1
Return to mail-deny page
|