Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Net::SMTP;
  4. use Net::SSH qw(sshopen2);
  5. use Net::SCP;
  6. use DBI;
  7.  
  8. # Global Variables
  9. my $whitelist = "/etc/spam/whitelist.conf";
  10. my $removeaccount = "/etc/spam/removeaccount.conf";
  11. my @toteclamail = qw(equipe.mail@tecla.com.br fabricio.fonseca@alog.com.br davidson.flores@alog.com.br);
  12. my @toalogmail = qw{equipe.email@alog.com.br suporterj@alog.com.br};
  13. my @remoterelays = qw{relay01 relay02 relay03};
  14. my $remotecmd = "cat /var/log/mail.log";
  15. my $remoteuser = "root";
  16. my $outtmp = "/tmp/emailout";
  17. my $tmapa = "/tmp/tempmap";
  18. my $rhosts;
  19. my %emailscount;
  20. my %keys;
  21. my $keys;
  22. my $emailsname;
  23. my %domain;
  24. my $onedomain;
  25. my $auth;
  26. my $semail;
  27. my $crtlemail = 0;
  28. my $pghost = "10.0.6.4";
  29. my $pgdb = "ldap";
  30. my $pguser = "ldap";
  31. my $pgpass = "babaca";
  32. my %emailm;
  33. my $domains;
  34. my $alog;
  35. my $tecla;
  36. my $alogmail;
  37. my $toalogmail;
  38. my $crtlhsphere = 0;
  39. my $crtlcomadmin = 0;
  40.  
  41. ## Making a count.
  42. foreach $rhosts (@remoterelays) {
  43. chomp ($rhosts);
  44. sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
  45. while (<READER>) {
  46. if (/sasl_username=(.+),(:?.+)/i or /sasl_username=(:?.+)/i) {
  47. $emailscount{$1}++;
  48. }
  49. }
  50. close READER;
  51. close WRITER;
  52. }
  53. # removing some domains which cannot be block.
  54. open WHITE, $whitelist or die "Nao foi possivel abrir o arquivo de whitelist: $!\n";
  55. while (<WHITE>) {
  56. chomp;
  57. foreach $keys (keys %emailscount) {
  58. if ($keys =~ /\@(.+)/) {
  59. delete $emailscount{$keys} if $1 eq $_;
  60. }
  61. }
  62. }
  63. close WHITE;
  64.  
  65. # removing some accounts which cannot be block.
  66. open ACC, $removeaccount or die "Nao foi possivel abrir o arquivo de removeaccount.conf: $!\n";
  67. while (<ACC>) {
  68. chomp;
  69. foreach $keys (keys %emailscount) {
  70. delete $emailscount{$keys} if $keys eq $_;
  71. }
  72. }
  73. close ACC;
  74.  
  75. ## Checking if biglig exists, if it exists, it's gonna remove.
  76. if (-s $outtmp) {
  77. unlink $outtmp or die "Nao foi possivel remover outtmp: $!";
  78. }
  79.  
  80. &criamapa;
  81. # Counting sent emails and make a hash for domains.
  82. open OUTEMAIL, ">", $outtmp;
  83. foreach $emailsname (sort {$emailscount{$b} <=> $emailscount{$a}} keys %emailscount) {
  84. if ($emailscount{$emailsname} > 3000 ) {
  85. $crtlemail = 1;
  86. $auth = $emailscount{$emailsname} - 3000;
  87. print OUTEMAIL "3000 e-mails enviados e $auth tentativas autenticações após o bloqueio por: $emailsname\n";
  88. &inseremapa($emailsname, $emailscount{$emailsname});
  89. if ($emailsname =~ /\@(.*)/) {
  90. $domain{$1}++;
  91. }
  92. }
  93. }
  94. close OUTEMAIL;
  95.  
  96. # Sending email to postmasters and making a flag mark whether alog or tecla.
  97. my $dbc = DBI->connect("dbi:Pg:database=$pgdb;host=$pghost","$pguser","$pgpass") or die $DBI::errstr;
  98. foreach $onedomain (keys %domain) {
  99. if ($domain{$onedomain}) {
  100. my $objexec = $dbc->prepare("select painel from dominios where dominio='$onedomain'");
  101. $objexec->execute;
  102. my $dbresu = $objexec->fetchrow();
  103. if ($dbresu eq "HSPHERE") {
  104. $emailm{$onedomain} = 'HSPHERE';
  105. $crtlhsphere = 1;
  106. } else {
  107. $emailm{$onedomain} = 'COMADMIN';
  108. $crtlcomadmin = 1;
  109. }
  110. if (&emaildomain($onedomain) == 0 and $crtlemail == 1) {
  111. print "Foi enviado e-mail para o postmaster do dominio: $onedomain\n";
  112. } else {
  113. print "Houve algum problema no sistema de envio\n";
  114. }
  115.  
  116. }
  117. }
  118. $dbc->disconnect;
  119.  
  120. # Sending an email to alog members.
  121. if (-s $tmapa) {
  122. if (&enviamapa == 0) {
  123. print "Mapa enviado com sucesso\n";
  124. if (&fazpostmap == 0) {
  125. print "Postmap feito com sucesso\n";
  126. if ($crtlemail == 1) {
  127. if ($crtlhsphere == 1) {
  128. push (@toteclamail, ('tecla'));
  129. if (&alogemail(@toteclamail) == 0) {
  130. print "Enviado email com os dominios do Tecla.\n";
  131. } else {
  132. print "Houve problema no envio das mensagens.\n";
  133. }
  134. }
  135. if ($crtlcomadmin == 1) {
  136. push (@toalogmail, ('alog'));
  137. if (&alogemail(@toalogmail) == 0) {
  138. print "Enviado email com os dominios da Alog.\n";
  139. } else {
  140. print "Houve problema no envio das mensagens.\n";
  141. }
  142. }
  143. }
  144. } else {
  145. print "Houve erro na rotina do postmap\n";
  146. }
  147. } else {
  148. print "Houve erro na rotina de envio de mapa\n";
  149. }
  150. } else {
  151. print "O mapa nao foi escrito\n";
  152. }
  153. # Subroutines
  154. sub criamapa {
  155. my $mapa = "/tmp/tempmap";
  156. my $default = "/etc/spam/dominiospadroes.conf";
  157. open MAPA, ">", $mapa or die "Nao foi possivel criar o serasa.map: $!\n";
  158. open DEF, $default or die "Nao foi possivel abrir o arquivo de dominios padroes: $!\n";
  159. if (-s $mapa) {
  160. unlink $mapa or die "Nao foi possivel remover o mapa: $!\n";
  161. }
  162. print MAPA <DEF> if ! -s $mapa;
  163. close MAPA;
  164. close DEF;
  165. }
  166. sub inseremapa {
  167. my $mapa = "/tmp/tempmap";
  168. my $count;
  169. my $email;
  170. ($email, $count) = @_;
  171. my $auth = $count - 3000;
  172. open MAPA, ">>", $mapa or die "Nao foi possivel gravar os emails no mapa: $!\n";
  173. print MAPA "$email\tDISCARD\tEnvio excedeu o limite permitido - 3000 MSGS e $auth tentativas de autenticacao apos o bloqueio\n";
  174. close MAPA;
  175. }
  176. sub alogemail {
  177. my ($cliente) = pop(@_);
  178. my $smtphost = "relay02.email.alog.com.br";
  179. if (my $smtp = Net::SMTP->new($smtphost)) {
  180. $smtp->mail('noreply@alog.com.br');
  181. foreach my $toemail (@_) {
  182. $smtp->to($toemail);
  183. }
  184. $smtp->data();
  185. $smtp->datasend("FROM: Alog E-mail System <noreply\@alog.com.br>\n");
  186. foreach my $toemail (@_) {
  187. $smtp->datasend("TO: $toemail\n");
  188. }
  189. $smtp->datasend("SUBJECT: Contas Bloqueadas\n");
  190. $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
  191. $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
  192. $smtp->datasend("\n");
  193. $smtp->datasend("As contas abaixo estão bloqueadas devido a excesso de envio de mensagens:\n");
  194. $smtp->datasend("\n");
  195. foreach my $clients (keys %emailm) {
  196. if ($cliente eq "tecla" and $emailm{$clients} eq "HSPHERE") {
  197. open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
  198. while (<BODY>) {
  199. if (/.*\@+$clients/) {
  200. $smtp->datasend("$&\n\n");
  201. }
  202. }
  203. close BODY;
  204. }
  205. }
  206. foreach my $clients (keys %emailm) {
  207. if ($cliente eq "alog" and $emailm{$clients} eq "COMADMIN") {
  208. open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
  209. while (<BODY>) {
  210. if (/.*\@+$clients/) {
  211. $smtp->datasend("$&\n\n");
  212. }
  213. }
  214. close BODY;
  215. }
  216. }
  217. $smtp->dataend();
  218. $smtp->quit;
  219. return 0;
  220. } else {
  221. return 1;
  222. }
  223. }
  224. sub emaildomain {
  225. my $domain = "@_";
  226. my $smtphost = "relay02.email.alog.com.br";
  227. if (my $smtp = Net::SMTP->new($smtphost)) {;
  228. $smtp->mail('noreply@alog.com.br');
  229. $smtp->to("postmaster\@$domain");
  230. $smtp->data();
  231. $smtp->datasend("FROM: Alog E-mail System <noreply\@alog.com.br>\n");
  232. $smtp->datasend("TO: postmaster\@$domain\n");
  233. $smtp->datasend("SUBJECT: Contas Bloqueadas\n");
  234. $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
  235. $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
  236. $smtp->datasend("\n");
  237. $smtp->datasend("Prezados,\n");
  238. $smtp->datasend("As contas abaixo estão bloqueadas devido a excesso de envio de mensagens:\n");
  239. open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
  240. while (<BODY>) {
  241. /\@(.*)/;
  242. if ($domain eq $1) {
  243. $smtp->datasend("$_\n");
  244. }
  245. }
  246. close BODY;
  247. $smtp->datasend("Qualquer dúvida entrar em contato com o suporte.\n");
  248. $smtp->datasend("Atenciosamente,\n");
  249. $smtp->datasend("Equipe de E-mail.\n");
  250. $smtp->dataend();
  251. $smtp->quit;
  252. return 0;
  253. } else {
  254. return 1;
  255. }
  256. }
  257. sub enviamapa {
  258. my $dir = "/etc/postfix/map/";
  259. my $namemap = "serasa3.map";
  260. my $mapa = "/tmp/tempmap";
  261. my $hostr;
  262. if (-s $mapa) {
  263. foreach $hostr (@remoterelays) {
  264. if (my $scp = Net::SCP->new({ "host"=>$hostr, "user"=>$remoteuser })) {
  265. $scp->put("$mapa","$dir$namemap") or die $scp->{errstr};
  266. } else {
  267. print "Teve problema na entrega dos mapas na relay: $hostr\n";
  268. return 1;
  269. exit;
  270. }
  271. }
  272. return 0;
  273. } else {
  274. return 1;
  275. }
  276. }
  277. sub fazpostmap {
  278. my $mapa = "/etc/postfix/map/serasa3.map";
  279. my $postmap = "postmap";
  280. my $rcommand = "$postmap $mapa";
  281. my $rhosts;
  282. foreach $rhosts (@remoterelays) {
  283. chomp($rhosts);
  284. sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$rcommand") || die "ssh: $!";
  285. while (<READER>) {
  286. if (/w+/) {
  287. print "Houve problema para efetuar o postmap na: $rhosts\n";
  288. return 1;
  289. exit;
  290. }
  291. }
  292. close READER;
  293. close WRITER;
  294. }
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement