Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 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.email@alog.com.br};
  12. my @toalogmail = qw{equipe.email@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.  
  39. ## Making a count.
  40. foreach $rhosts (@remoterelays) {
  41. chomp ($rhosts);
  42. sshopen2("$remoteuser\@$rhosts", *READER, *WRITER, "$remotecmd") || die "ssh: $!";
  43. while (<READER>) {
  44. if (/sasl_username=(.+),(:?.+)/i or /sasl_username=(:?.+)/i) {
  45. $emailscount{$1}++;
  46. }
  47. }
  48. close READER;
  49. close WRITER;
  50. }
  51. # removing some domains which cannot be block.
  52. open WHITE, $whitelist or die "Nao foi possivel abrir o arquivo de whitelist: $!\n";
  53. while (<WHITE>) {
  54. chomp;
  55. foreach $keys (keys %emailscount) {
  56. if ($keys =~ /\@(.+)/) {
  57. delete $emailscount{$keys} if $1 eq $_;
  58. }
  59. }
  60. }
  61. close WHITE;
  62.  
  63. # removing some accounts which cannot be block.
  64. open ACC, $removeaccount or die "Nao foi possivel abrir o arquivo de removeaccount.conf: $!\n";
  65. while (<ACC>) {
  66. chomp;
  67. foreach $keys (keys %emailscount) {
  68. delete $emailscount{$keys} if $keys eq $_;
  69. }
  70. }
  71. close ACC;
  72.  
  73. ## Checking if biglig exists, if it exists, it's gonna remove.
  74. if (-s $outtmp) {
  75. unlink $outtmp or die "Nao foi possivel remover outtmp: $!";
  76. }
  77.  
  78. &criamapa;
  79. # Counting sent emails and make a hash for domains.
  80. open OUTEMAIL, ">", $outtmp;
  81. foreach $emailsname (sort {$emailscount{$b} <=> $emailscount{$a}} keys %emailscount) {
  82. if ($emailscount{$emailsname} > 3000 ) {
  83. $crtlemail = 0;
  84. $auth = $emailscount{$emailsname} - 3000;
  85. print OUTEMAIL "3000 e-mails enviados e $auth tentativas autenticações após o bloqueio por: $emailsname\n";
  86. #&inseremapa($emailsname, $emailscount{$emailsname});
  87. if ($emailsname =~ /\@(.*)/) {
  88. $domain{$1}++;
  89. }
  90. }
  91. }
  92. close OUTEMAIL;
  93.  
  94. # Sending email to postmasters and making flag mark whether alog or tecla.
  95. my $dbc = DBI->connect("dbi:Pg:database=$pgdb;host=$pghost","$pguser","$pgpass") or die $DBI::errstr;
  96. foreach $onedomain (keys %domain) {
  97. if ($domain{$onedomain}) {
  98. my $objexec = $dbc->prepare("select painel from dominios where dominio='$onedomain'");
  99. $objexec->execute;
  100. my $dbresu = $objexec->fetchrow();
  101. if ($dbresu eq "HSPHERE") {
  102. $emailm{$onedomain} = 'HSPHERE';
  103. } else {
  104. $emailm{$onedomain} = 'COMADMIN';
  105. }
  106. if (&emaildomain($onedomain) == 0 and $crtlemail == 1) {
  107. print "Foi enviado e-mails para os postmaster's dos dominios: $onedomain\n";
  108. } else {
  109. print "Ouve algum problema no sistema de envio\n";
  110. }
  111.  
  112. }
  113. }
  114. $dbc->disconnect;
  115.  
  116. # Sending an email to alog members.
  117. if (-s $tmapa) {
  118. if (&enviamapa == 0) {
  119. print "Mapa enviado com sucesso\n";
  120. if (&fazpostmap == 0) {
  121. print "Postmap feito com sucesso\n";
  122. if ($crtlemail == 1) {
  123. foreach $domains (keys %emailm) {
  124. if ($emailm{$domains} eq "HSPHERE") {
  125. foreach $tecla (@toteclamail) {
  126. if (&alogemail($tecla,"tecla") == 0) {
  127. print "Enviado email com os dominios do tecla\n";
  128. } else {
  129. print "Houve problema no envio das mensagens\n";
  130. }
  131. }
  132. }
  133. last;
  134. }
  135. foreach $domains (keys %emailm) {
  136. if ($emailm{$domains} eq "COMADMIN") {
  137. foreach $toalogmail (@toalogmail) {
  138. if (&alogemail($alogmail,"alog") == 0) {
  139. print "Enviado email com os dominios da Alog\n";
  140. } else {
  141. print "Houve problema no envio das mensagens\n";
  142. }
  143. }
  144. }
  145. last;
  146. }
  147.  
  148. }
  149. } else {
  150. print "Ouve erro na rotina do postmap\n";
  151. }
  152. } else {
  153. print "Ouve erro na rotina de envio de mapa\n";
  154. }
  155. } else {
  156. print "O mapa nao foi escrito\n";
  157. }
  158. # Subroutines
  159. sub criamapa {
  160. my $mapa = "/tmp/tempmap";
  161. my $default = "/etc/spam/dominiospadroes.conf";
  162. open MAPA, ">", $mapa or die "Nao foi possivel criar o serasa.map: $!\n";
  163. open DEF, $default or die "Nao foi possivel abrir o arquivo de dominios padroes: $!\n";
  164. if (-s $mapa) {
  165. unlink $mapa or die "Nao foi possivel remover o mapa: $!\n";
  166. }
  167. print MAPA <DEF> if ! -s $mapa;
  168. close MAPA;
  169. close DEF;
  170. }
  171. sub inseremapa {
  172. my $mapa = "/tmp/tempmap";
  173. my $count;
  174. my $email;
  175. ($email, $count) = @_;
  176. my $auth = $count - 3000;
  177. open MAPA, ">>", $mapa or die "Nao foi possivel gravar os emails no mapa: $!\n";
  178. print MAPA "$email\tDISCARD\tEnvio excedeu o limite permitido - 3000 MSGS e $auth tentativas de autenticacao apos o bloqueio\n";
  179. close MAPA;
  180. }
  181. sub alogemail {
  182. my $smtphost = "relay02.email.alog.com.br";
  183. my ($toemail,$cliente) = "@_";
  184. my $smtp;
  185. if ($smtp = Net::SMTP->new($smtphost)) {
  186. $smtp->mail('noreply@alog.com.br');
  187. $smtp->to($toemail);
  188. $smtp->data();
  189. $smtp->datasend("FROM: Alog E-mail System <noreply\@alog.com.br>\n");
  190. $smtp->datasend("TO: $toemail\n");
  191. $smtp->datasend("SUBJECT: Contas Bloqueadas\n");
  192. $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
  193. $smtp->datasend("Content-Transfer-Encoding: quoted-printable\n");
  194. $smtp->datasend("\n");
  195. $smtp->datasend("As contas abaixo estão bloqueadas devido a excesso de envio de mensagens:\n");
  196. $smtp->datasend("\n");
  197. foreach $clients (keys %emailm) {
  198. if ($cliente eq "tecla" and $emailm{$clients} eq "HSPHERE") {
  199. open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
  200. while (<BODY>) {
  201. /($clients+)/
  202. $smtp->datasend("$1\n");
  203. close BODY;
  204. }
  205. }
  206. }
  207. foreach $clients (keys %emailm) {
  208. if ($cliente eq "alog" and $emailm{$clients} eq "COMADMIN") {
  209. open BODY, $outtmp or die "Nao foi possivel abrir o outtmp: $!\n";
  210. while (<BODY>) {
  211. /($clients+)/
  212. $smtp->datasend("$1\n");
  213. close BODY;
  214. }
  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