Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use Net::SMTP;
- use Net::SSH qw(sshopen2);
- use Net::SCP;
- use DBI;
- # Global Variables
- my $whitelist = "/etc/spam/whitelist.conf";
- my $removeaccount = "/etc/spam/removeaccount.conf";
- my @toteclamail = qw(equipe.mail@tecla.com.br fabricio.fonseca@alog.com.br davidson.flores@alog.com.br);
- my @toalogmail = qw{equipe.email@alog.com.br suporterj@alog.com.br};
- my @remoterelays = qw{relay01 relay02 relay03};
- my $remotecmd = "cat /var/log/mail.log";
- my $remoteuser = "root";
- my $outtmp = "/tmp/emailout";
- my $tmapa = "/tmp/tempmap";
- my $rhosts;
- my %emailscount;
- my %keys;
- my $keys;
- my $emailsname;
- my %domain;
- my $onedomain;
- my $auth;
- my $semail;
- my $crtlemail = 0;
- my $pghost = "10.0.6.4";
- my $pgdb = "ldap";
- my $pguser = "ldap";
- my $pgpass = "babaca";
- my %emailm;
- my $domains;
- my $alog;
- my $tecla;
- my $alogmail;
- my $toalogmail;
- my $crtlhsphere = 0;
- my $crtlcomadmin = 0;
- ## Making a count.
- foreach $rhosts (@remoterelays) {
- chomp ($rhosts);
- sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
- while (<READER>) {
- if (/sasl_username=(.+),(:?.+)/i or /sasl_username=(:?.+)/i) {
- $emailscount{$1}++;
- }
- }
- close READER;
- close WRITER;
- }
- # removing some domains which cannot be block.
- open WHITE, $whitelist or die "Nao foi possivel abrir o arquivo de whitelist: $!\n";
- while (<WHITE>) {
- chomp;
- foreach $keys (keys %emailscount) {
- if ($keys =~ /\@(.+)/) {
- delete $emailscount{$keys} if $1 eq $_;
- }
- }
- }
- close WHITE;
- # removing some accounts which cannot be block.
- open ACC, $removeaccount or die "Nao foi possivel abrir o arquivo de removeaccount.conf: $!\n";
- while (<ACC>) {
- chomp;
- foreach $keys (keys %emailscount) {
- delete $emailscount{$keys} if $keys eq $_;
- }
- }
- close ACC;
- ## Checking if biglig exists, if it exists, it's gonna remove.
- if (-s $outtmp) {
- unlink $outtmp or die "Nao foi possivel remover outtmp: $!";
- }
- &criamapa;
- # Counting sent emails and make a hash for domains.
- open OUTEMAIL, ">", $outtmp;
- foreach $emailsname (sort {$emailscount{$b} <=> $emailscount{$a}} keys %emailscount) {
- if ($emailscount{$emailsname} > 3000 ) {
- $crtlemail = 1;
- $auth = $emailscount{$emailsname} - 3000;
- print OUTEMAIL "3000 e-mails enviados e $auth tentativas autenticações após o bloqueio por: $emailsname\n";
- &inseremapa($emailsname, $emailscount{$emailsname});
- if ($emailsname =~ /\@(.*)/) {
- $domain{$1}++;
- }
- }
- }
- close OUTEMAIL;
- # Sending email to postmasters and making a flag mark whether alog or tecla.
- my $dbc = DBI->connect("dbi:Pg:database=$pgdb;host=$pghost","$pguser","$pgpass") or die $DBI::errstr;
- foreach $onedomain (keys %domain) {
- if ($domain{$onedomain}) {
- my $objexec = $dbc->prepare("select painel from dominios where dominio='$onedomain'");
- $objexec->execute;
- my $dbresu = $objexec->fetchrow();
- if ($dbresu eq "HSPHERE") {
- $emailm{$onedomain} = 'HSPHERE';
- $crtlhsphere = 1;
- } else {
- $emailm{$onedomain} = 'COMADMIN';
- $crtlcomadmin = 1;
- }
- if (&emaildomain($onedomain) == 0 and $crtlemail == 1) {
- print "Foi enviado e-mail para o postmaster do dominio: $onedomain\n";
- } else {
- print "Houve algum problema no sistema de envio\n";
- }
- }
- }
- $dbc->disconnect;
- # Sending an email to alog members.
- if (-s $tmapa) {
- if (&enviamapa == 0) {
- print "Mapa enviado com sucesso\n";
- if (&fazpostmap == 0) {
- print "Postmap feito com sucesso\n";
- if ($crtlemail == 1) {
- if ($crtlhsphere == 1) {
- push (@toteclamail, ('tecla'));
- if (&alogemail(@toteclamail) == 0) {
- print "Enviado email com os dominios do Tecla.\n";
- } else {
- print "Houve problema no envio das mensagens.\n";
- }
- }
- if ($crtlcomadmin == 1) {
- push (@toalogmail, ('alog'));
- if (&alogemail(@toalogmail) == 0) {
- print "Enviado email com os dominios da Alog.\n";
- } else {
- print "Houve problema no envio das mensagens.\n";
- }
- }
- }
- } else {
- print "Houve erro na rotina do postmap\n";
- }
- } else {
- print "Houve erro na rotina de envio de mapa\n";
- }
- } else {
- print "O mapa nao foi escrito\n";
- }
- # Subroutines
- sub criamapa {
- my $mapa = "/tmp/tempmap";
- my $default = "/etc/spam/dominiospadroes.conf";
- open MAPA, ">", $mapa or die "Nao foi possivel criar o serasa.map: $!\n";
- open DEF, $default or die "Nao foi possivel abrir o arquivo de dominios padroes: $!\n";
- if (-s $mapa) {
- unlink $mapa or die "Nao foi possivel remover o mapa: $!\n";
- }
- print MAPA <DEF> if ! -s $mapa;
- close MAPA;
- close DEF;
- }
- sub inseremapa {
- my $mapa = "/tmp/tempmap";
- my $count;
- my $email;
- ($email, $count) = @_;
- my $auth = $count - 3000;
- open MAPA, ">>", $mapa or die "Nao foi possivel gravar os emails no mapa: $!\n";
- print MAPA "$email\tDISCARD\tEnvio excedeu o limite permitido - 3000 MSGS e $auth tentativas de autenticacao apos o bloqueio\n";
- close MAPA;
- }
- sub alogemail {
- my ($cliente) = pop(@_);
- my $smtphost = "relay02.email.alog.com.br";
- if (my $smtp = Net::SMTP->new($smtphost)) {
- $smtp->mail('noreply@alog.com.br');
- foreach my $toemail (@_) {
- $smtp->to($toemail);
- }
- $smtp->data();
- $smtp->datasend("FROM: Alog E-mail System <noreply\@alog.com.br>\n");
- foreach my $toemail (@_) {
- $smtp->datasend("TO: $toemail\n");
- }
- $smtp->datasend("SUBJECT: Contas Bloqueadas\n");
- $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
- $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
- $smtp->datasend("\n");
- $smtp->datasend("As contas abaixo estão bloqueadas devido a excesso de envio de mensagens:\n");
- $smtp->datasend("\n");
- foreach my $clients (keys %emailm) {
- if ($cliente eq "tecla" and $emailm{$clients} eq "HSPHERE") {
- open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
- while (<BODY>) {
- if (/.*\@+$clients/) {
- $smtp->datasend("$&\n\n");
- }
- }
- close BODY;
- }
- }
- foreach my $clients (keys %emailm) {
- if ($cliente eq "alog" and $emailm{$clients} eq "COMADMIN") {
- open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
- while (<BODY>) {
- if (/.*\@+$clients/) {
- $smtp->datasend("$&\n\n");
- }
- }
- close BODY;
- }
- }
- $smtp->dataend();
- $smtp->quit;
- return 0;
- } else {
- return 1;
- }
- }
- sub emaildomain {
- my $domain = "@_";
- my $smtphost = "relay02.email.alog.com.br";
- if (my $smtp = Net::SMTP->new($smtphost)) {;
- $smtp->mail('noreply@alog.com.br');
- $smtp->to("postmaster\@$domain");
- $smtp->data();
- $smtp->datasend("FROM: Alog E-mail System <noreply\@alog.com.br>\n");
- $smtp->datasend("TO: postmaster\@$domain\n");
- $smtp->datasend("SUBJECT: Contas Bloqueadas\n");
- $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
- $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
- $smtp->datasend("\n");
- $smtp->datasend("Prezados,\n");
- $smtp->datasend("As contas abaixo estão bloqueadas devido a excesso de envio de mensagens:\n");
- open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
- while (<BODY>) {
- /\@(.*)/;
- if ($domain eq $1) {
- $smtp->datasend("$_\n");
- }
- }
- close BODY;
- $smtp->datasend("Qualquer dúvida entrar em contato com o suporte.\n");
- $smtp->datasend("Atenciosamente,\n");
- $smtp->datasend("Equipe de E-mail.\n");
- $smtp->dataend();
- $smtp->quit;
- return 0;
- } else {
- return 1;
- }
- }
- sub enviamapa {
- my $dir = "/etc/postfix/map/";
- my $namemap = "serasa3.map";
- my $mapa = "/tmp/tempmap";
- my $hostr;
- if (-s $mapa) {
- foreach $hostr (@remoterelays) {
- if (my $scp = Net::SCP->new({ "host"=>$hostr, "user"=>$remoteuser })) {
- $scp->put("$mapa","$dir$namemap") or die $scp->{errstr};
- } else {
- print "Teve problema na entrega dos mapas na relay: $hostr\n";
- return 1;
- exit;
- }
- }
- return 0;
- } else {
- return 1;
- }
- }
- sub fazpostmap {
- my $mapa = "/etc/postfix/map/serasa3.map";
- my $postmap = "postmap";
- my $rcommand = "$postmap $mapa";
- my $rhosts;
- foreach $rhosts (@remoterelays) {
- chomp($rhosts);
- sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$rcommand") || die "ssh: $!";
- while (<READER>) {
- if (/w+/) {
- print "Houve problema para efetuar o postmap na: $rhosts\n";
- return 1;
- exit;
- }
- }
- close READER;
- close WRITER;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement