Advertisement
Guest User

Untitled

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