#!/usr/bin/perl # by PJ # # Version: 1.2 7-09-01 # Version: 1.1 2-15-01 # Version: 1.0 2-13-01 # # Purpose: To check incoming emails for same to address as from address. # This should help reduce spam to your mail box. # Place the file location in your .qmail file before any other options. # ie |/var/qmail/bin/check_to_from.pl # ./Maildir/ # # Bugs: E-mail (dysan at dysan dot net) with any bug information or ideas. # (replace words with symbols for email) #--Variables--# $log = "1"; #- 0 eq off, 1 eq on. $log_file = "spam/spam.log"; #- your log file name and location. $email_file = "spam/email.log"; #- saved email. $rcpt = "/var/qmail/control/rcpthosts"; #-qmail's rcpthosts file $message = "Sorry this e-mail address does not exist. (#5.1.1)\nPlease remove this e-mail address from your list."; #-return error message $debug = "0"; #- 0 = off; 1 = on. #- (( Do Not edit the below info )) -# #-Get the date for logging. ($minute,$hour,$day,$month,$year) = (localtime) [1,2,3,4,5]; $month += "1"; $year += "1900"; if($month <= "9") { $month = "0" . "$month"; } if($minute <= "9") { $minute = "0" . "$minute"; } $date = "$month/$day/$year - $hour:$minute"; #-print exit codes. if($debug == "1") { print "Exit code 0 = success.\nExit code 99 = success and abort .qmail.\nExit code 100 = bounce(return) email.\n"; } #-Read in the email message and go through each line. while(<>) { push (@HEADER, $_); #- find the to and from addresses. chomp(); $_ =~ tr/[A-Z]/[a-z]/; #-look for the to email address. if(!$to) { if(/^to: .*\b(\S+\@[\w\.]+)\b/) { $to = $1; lc($to); if($debug == "1") { print "To address: $to\n"; } } } #-look for the from email address. if(!$from) { if(/^from: .*\b(\S+\@[\w\.]+)\b/) { $from = $1; lc($from); if($debug == "1") { print "From address: $from\n"; } } } } #-check against specific e-mail addresses. if ($to =~ "daemon" || $from =~ "daemon") { if($debug == "1") { print "Email for mailer-daemon and exited with code 99.\n"; } exit 99; } #-if from a trusted host in rcpthosts file then except. open(RC,"$rcpt") || die "Can not open $rcpt: $!"; while () { chomp; if ($from =~ $_) { if($debug == "1") { print "Domain found in $rcpt file and exited with code 0.\n"; } exit 0; } } close(RC); #-error if no from or to address are in email. &logger("$message", "No 'from' e-mail address found.", "<$from>") unless $from; &logger("$message", "No 'to' e-mail address found.", "<$from>") unless $to; #-error if to and from addresses match. if ($to eq $from) { if($debug == "1") { print "To and From addresses matched.\n"; } &logger("$message", "To & From e-mail addresses matched.", "<$from>"); } #-print debug if finished script. if($debug == "1") { print "Good email!\n"; } sub logger($) { #- log email address. #-log event. if($log == "1") { open(LOG,">>$log_file") || die "Can not open $log_file: $!"; print LOG "$date||From\:$_[2]||$_[1]\n"; close(LOG); if($debug == "1") { print "Wrote to $log_file file.\n"; } } #-save copy of email. if($log == "1") { open(EM,">>$email_file") || die "Can not open $email_file: $!"; print EM "\n-----[Start of E-mail $date]---------------------------------\n"; foreach $ln (@HEADER) { chomp($ln); print EM "$ln\n"; } print EM "-----[End of E-mail]------------------------------------------\n"; close(EM); if($debug == "1") { print "Wrote to $email_file file.\n"; } } #-send error message back to sender. warn "$_[0]\n"; if($debug == "1") { print "Bounced email and exited with code 100.\n"; } exit 100; }