#!/usr/bin/perl
##
# This is mailrep.pl v1.1
# By - Marcelo Araujo (araujo@bsdmail.org) - http://experience.bsdnetwork.org
#
# Add 16 Setember 2006:
# Scheduling Crontab support
# By - araujo
use strict;
my @conf;
my @setting;
my $set;
my $fluxo;
my $mem = "1";
my $count = "1";
my @count;
my $abuseat;
my $spamhaus;
my $ordb;
my $blitzed;
my $dsbl;
my $sorbs;
my $result_blocks;
my $host_not_found;
my $address_not_listed;
my $content_reject;
my $domain_not_found;
my $helo_rejected;
my $softbounce;
my $sender_access_denied;
my $relay_local;
my $user_unknown;
my $host_not_found;
my $address_not_listed;
my $content_reject;
my $domain_not_found;
my $helo_rejected;
my $softbounce;
my $sender_access_denied;
my $mail_to_ok;
my $mail_to_nok;
my $from_mail;
my @email;
my @cron_storage;
my @crontab;
my $cron_fluxo;
my $date = localtime time;
my $sendmail = "/usr/sbin/sendmail -t";
my @stg = ("");
sub conf()
{
open(CONF, "</usr/local/etc/mailrep/mailrep.conf") || die "/usr/local/etc/mailrep/mailrep.conf Not Found\n";
while(<CONF>)
{
push(@stg,$_);
}
close(CONF);
for(@stg)
{
if (/1/)
{
for($fluxo=1;$fluxo < 2; $fluxo++)
{
$mem++;
$conf[$mem] = "$_";
$_ =~ s/=(..)|\n|#| |\t|[a-z]|:(..)|\|(..)//g;
push(@setting, $_);
}
}
if (/MAILADDR/)
{
$_ =~ s/=(..)|\n|#| |\t||:(..)|\|(..)|\"|MAILADDR//g;
@email = $_;
}
}
}
sub header()
{
print ("+=================================================================+\n");
print ("+ MailRep V1.0b - $date +\n");
print ("+=================================================================+\n");
}
sub license()
{
print("\t\t\t\tCopyright (c) 2006, Marcelo Araujo\n");
print("\t\t\t\t\tAll rights reserved.\n");
print("\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n");
print("\t* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n");
print("\t* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n");
print("\t* Neither the name of the Marcelo Araujo and EXPERIENCE.BSDNETWORK.ORG nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n\n");
print("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 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.\n");
}
sub rbl_spamrel()
{
&conf();
open(MAILLOG, "</var/log/maillog") || die "/var/log/maillog not reader\n";
while (<MAILLOG>)
{
push(@count,$_);
}
close(MAILLOG);
for($fluxo=0; $fluxo < scalar(@setting); $fluxo++)
{
if($setting[$fluxo] eq "ABUSEAT")
{
$count=0;
for(@count)
{
if(/cbl.abuseat.org/)
{
$count++;
}
$abuseat = $count;
}
printf ("%-40s: $count\n", " Blocked ABUSEAT");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "SPAMHAUS")
{
$count=0;
for(@count)
{
if(/spamhaus.org/)
{
$count++;
}
$spamhaus = $count;
}
printf ("%-40s: $count\n", " Blocked SPAMHAUS");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "ORDB")
{
$count=0;
for(@count)
{
if(/relays.ordb.org/)
{
$count++;
}
$ordb = $count;
}
printf ("%-40s: $count\n", " Blocked ORDB");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "BLITZED")
{
$count=0;
for(@count)
{
if(/opm.blitzed.org/)
{
$count++;
}
$blitzed = $count;
}
printf ("%-40s: $count\n", " Blocked BLITZED");
print ("------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "DSBL")
{
$count=0;
for(@count)
{
if(/dsbl.org/)
{
$count++;
}
$dsbl = $count;
}
printf ("%-40s: $count\n", " Blocked DSBL");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "SORBS")
{
$count=0;
for(@count)
{
if(/dul.dnsbl.sorbs.net/)
{
$count++;
}
$sorbs = $count;
}
printf ("%-40s: $count\n", " Blocked SORBS");
print ("-------------------------------------------------------------------\n");
}
}
$result_blocks = $abuseat+$spamhaus+$ordb+$blitzed+$dsbl+$sorbs;
if($result_blocks != 0)
{
print "\t\t\t\t\tTotal of blockades: ", $result_blocks, "\n";
print "\t\t\t\t\t===========================\n";
} else {
print " Without blockades.\n";
print " - I doubt.\n\n";
}
}
sub errors_spamrel()
{
&conf();
open(MAILLOG, "</var/log/maillog") || die "Impossivel ler maillog\n";
while (<MAILLOG>)
{
push(@count,$_);
}
close(MAILLOG);
for($fluxo=0; $fluxo < scalar(@setting); $fluxo++)
{
if($setting[$fluxo] eq "USER_UNKNOWN")
{
$count=0;
for(@count)
{
if(/User unknown in local recipient table/)
{
$count++;
}
$user_unknown = $count;
}
printf ("%-40s: $count\n", " User unknown");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "HOST_NOT_FOUND")
{
$count=0;
for(@count)
{
if(/verification failed: Host not found/)
{
$count++;
}
$host_not_found = $count;
}
printf ("%-40s: $count\n", " Host not found");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "ADDRESS_NOT_LISTED")
{
$count=0;
for(@count)
{
if(/address not listed for hostname/)
{
$count++;
}
$address_not_listed = $count;
}
printf ("%-40s: $count\n", " Address not listed");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "CONTENT_REJECT")
{
$count=0;
for(@count)
{
if(/Message content rejected/)
{
$count++;
}
$content_reject = $count;
}
printf ("%-40s: $count\n", " Content Reject");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "DOMAIN_NOT_FOUND")
{
$count=0;
for(@count)
{
if(/Sender address rejected: Domain not found/)
{
$count++;
}
$domain_not_found = $count;
}
printf ("%-40s: $count\n", " Sender Domain not found");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "HELO_REJECTED")
{
$count=0;
for(@count)
{
if(/Helo command rejected: Invalid name/)
{
$count++;
}
$helo_rejected = $count;
}
printf ("%-40s: $count\n", " Helo Rejected");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "SOFTBOUNCE")
{
$count=0;
for(@count)
{
if(/SOFTBOUNCE/)
{
$count++;
}
$softbounce = $count;
}
printf ("%-40s: $count\n", " SoftBounced");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "SENDER_ACCESS_DENIED")
{
$count=0;
for(@count)
{
if(/Sender address rejected: Access denied/)
{
$count++;
}
$sender_access_denied = $count;
}
printf ("%-40s: $count\n", " Access Denied");
print ("-------------------------------------------------------------------\n");
}
if($setting[$fluxo] eq "RELAY_LOCAL")
{
$count=0;
for(@count)
{
if(/relay=local/ || /status=sent/)
{
$count++;
}
$relay_local = $count;
}
printf ("%-40s: $count\n", " Sent Local");
print ("-------------------------------------------------------------------\n");
}
}
$result_blocks = $user_unknown+$host_not_found+$address_not_listed+$content_reject+$domain_not_found+$helo_rejected+$softbounce+$sender_access_denied;
if($result_blocks != 0)
{
print "\t\t\tTotal of emails you do not deliver: ", $result_blocks, "\n";
print "\t\t\tTotal of emails delivered : ", $relay_local, "\n";
print "\t\t\t===========================================\n";
} else {
print " Without blockades.\n";
print " - I doubt.\n\n";
}
}
sub mail_to()
{
if ($ARGV[0] eq "--mail-to" & $ARGV[1] eq "--with")
{
if($ARGV[2] eq "")
{
print "It uses: mailrep.pl --mail-to --with user\@domain\n";
} elsif($ARGV[2] =~ "\@")
{
open(MAILLOG, "</var/log/maillog") || die "Impossivel ler maillog\n";
while (<MAILLOG>)
{
push(@count, $_);
}
close(MAILLOG);
for($fluxo=0; $fluxo < 2; $fluxo++)
{
$mail_to_ok=0;
$mail_to_nok=0;
for(@count)
{
if (/to=<$ARGV[2]>/ & /to=<$ARGV[2]>/ & /status=sent/)
{
if ($_ ne "^$ARGV[2]")
{
$mail_to_ok++;
}
}
if(/smtpd/ & /$ARGV[2]/)
{
if($_ ne "^$ARGV[2]")
{
$mail_to_nok++;
}
}
}
}
&header();
printf ("%-40s: $mail_to_ok\n", " Envoy for [$ARGV[2]]");
$mail_to_nok = $mail_to_nok - $mail_to_ok;
print ("-------------------------------------------------------------------\n");
if ( $mail_to_nok > 0 )
{
printf ("%-40s: $mail_to_nok\n", " Error for [$ARGV[2]]");
print ("-------------------------------------------------------------------\n");
}
} elsif($ARGV[2] eq "")
{
print "Erro na funcao\n";
}
} else {
print "It uses: mailrep.pl --mail-to --with user\@domain\n";
}
}
sub mail_from()
{
if ($ARGV[0] eq "--mail-from" & $ARGV[1] eq "--with")
{
if($ARGV[2] eq "")
{
print "It uses: mailrep.pl --mail-from --with user\@domain\n";
} elsif($ARGV[2] =~ "\@")
{
open(MAILLOG, "</var/log/maillog") || die "Impossivel ler maillog\n";
while (<MAILLOG>)
{
push(@count, $_);
}
close(MAILLOG);
}
for($fluxo=0; $fluxo < 2; $fluxo++)
{
$from_mail=0;
for(@count)
{
if (/from/ & /$ARGV[2]/)
{
$from_mail++
}
}
}
} else {
print "It uses: mailrep.pl --mail-from --with user\@domain\n";
}
if($from_mail > 0)
{
&header();
print (" [$ARGV[2]]\n");
printf ("%-40s: $from_mail\n", " Emails of who sends");
print ("-------------------------------------------------------------------\n");
} elsif($ARGV[2] =~ "\@" & $from_mail == 0) {
&header();
print (" Nothing.....\n");
print ("-------------------------------------------------------------------\n");
}
}
sub cron_mailrep()
{
&conf();
for($fluxo=0; $fluxo < scalar(@setting); $fluxo++)
{
if($setting[$fluxo] eq "CRON_SET")
{
##print ("@email\n");
@cron_storage = `mailrep.pl --rbl && mailrep.pl --errors`;
open(CRON, "</etc/crontab") || die "No crontab scheduling conf\n";
while(<CRON>)
{
push(@crontab,$_);
}
close(CRON);
for(@crontab)
{
if(/mailrep.pl/)
{
if(/-cron/)
{
$cron_fluxo = 0;
} else { $cron_fluxo = 1; }
} else { $cron_fluxo = 1; }
}
}
}
if($cron_fluxo == 1)
{
print("NO SCHEDULING\n");
print("Please: ./mailrep.pl --cronsetting <MINUTE> <HOUR>\n");
}
elsif($cron_fluxo == 0)
{
my $subject = "Subject: MailReport Daily Scheduling";
my $reply_to = "@email";
my $to = "@email";
open(LOG,">/tmp/log.mailrep");
print LOG @cron_storage;
close(LOG);
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL "From: $reply_to\n";
print SENDMAIL "To: $to\n";
print SENDMAIL "$subject\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL @cron_storage;
close(SENDMAIL);
system("rm -f /tmp/log.mailrep");
}
}
sub cron_remove()
{
open(CRON, "/etc/crontab") || die "No crontab scheduling conf\n";
while(<CRON>)
{
unless($_ =~ s/mailrep.pl -cron//g)
{
push(@crontab,$_);
}
}
close(CRON);
open(CRON, ">/etc/crontab");
print CRON @crontab;
close(CRON);
}
sub cron_setting()
{
if ($ARGV[1] eq "" & $ARGV[2] eq "")
{
print("Usage: ./mailrep.pl --cronsetting <MINUTE> <HOUR>\n");
print("Example: ./mailrep.pl --cronsetting 15 22\n");
}
elsif ($ARGV[2] eq "")
{
print("Usage: ./mailrep.pl --cronsetting $ARGV[1] <HOUR>\n");
} else {
open(CRON,">>/etc/crontab");
print ("$ARGV[1] \t$ARGV[2] \t* \t* \t* \tmailrep.pl -cron\n");
print CRON "$ARGV[1] \t$ARGV[2] \t* \t* \t* \troot \t/usr/local/sbin/mailrep.pl -cron\n";
close(CRON);
}
}
sub help_spamrel()
{
if ($ARGV[0] eq "")
{
print ("\nmailrep --help\n\n");
}
elsif ($ARGV[0] ne "--help" & $ARGV[0] ne "--conf" & $ARGV[0] ne "--rbl" & $ARGV[0] ne "--errors" & $ARGV[0] ne "--license" & $ARGV[0] ne "--mail-to" & $ARGV[0] ne "--mail-from" & $ARGV[0] ne "-cron" & $ARGV[0] ne "--cronsetting")
{
print("\nmailrep --help\n\n");
}
if ($ARGV[0] eq "--help")
{
print("\nmailrep --license [To see the license BSD]\n");
print("mailrep --conf [To see the configuration options]\n");
print("mailrep --mail-to --with user\@domain [Number of emails sent for determined account]\n");
print("mailrep --mail-from --with user\@domain [Number of email of who external sends\n");
print("mailrep --rbl [To report the number of blockades in the RBLS]\n");
print("mailrep --errors [Errors in agreement the configuration archive]\n");
print("mailrep --cronsetting [Setting Minute and Hour for Scheduling in crontab]\n\n");
}
if ($ARGV[0] eq "--conf")
{
&conf();
for(@conf)
{
if($_ eq '')
{
print "";
} else {
# Aqui pega todas as variaveis dentro do CONF
print $_,"";
}
}
}
if ($ARGV[0] eq "--rbl")
{
&header();
&rbl_spamrel();
}
if ($ARGV[0] eq "--errors")
{
&header();
&errors_spamrel();
}
if ($ARGV[0] eq "--license")
{
&license();
}
if ($ARGV[0] eq "--mail-to")
{
&mail_to();
}
if ($ARGV[0] eq "--mail-from")
{
&mail_from();
}
if ($ARGV[0] eq "-cron")
{
&conf();
for($fluxo=0; $fluxo < scalar(@setting); $fluxo++)
{
if($setting[$fluxo] eq "CRON_SET")
{
&cron_mailrep();
$cron_fluxo = 3;
} else {
$cron_fluxo = 2;
}
}
if($cron_fluxo == 2)
{
&cron_remove();
print("Please set CRON_SET = 1 in /usr/local/etc/mailrep/mailrep.conf\n");
}
}
if ($ARGV[0] eq "--cronsetting")
{
&conf();
for($fluxo=0; $fluxo < scalar(@setting); $fluxo++)
{
if($setting[$fluxo] eq "CRON_SET")
{
&cron_setting();
$cron_fluxo = 3;
} else {
$cron_fluxo = 2;
}
}
if($cron_fluxo == 2)
{
&cron_remove();
print("Please set CRON_SET = 1 in /usr/local/etc/mailrep/mailrep.conf\n");
}
}
}
&help_spamrel();
syntax highlighted by Code2HTML, v. 0.9.1