#!/usr/bin/perl5.6.1
use lib 'perl/Modules';
use Net::Telnet ();
require 'perl/misc.pl';
$USR_FILE = "rules/auth-data/users";
$PWD_FILE = "rules/auth-data/passwords";
$target = $ARGV[0];
if (!$target) {
  print "Usage: bin/pop0.sara <hostname>\n";
  exit;
}
$debug = 0;
$port   = "110";
@users = "";
$usr_cnt = 0;
($hostname,$dummy) = split(/\./, $target);
open (USR, "$USR_FILE");
while (<USR>) {
  chomp;
  if (/\%HOST/) {
    $_ = $hostname if ($hostname =~ /[azAZ]/);
  }
  $users[$usr_cnt++] = $_;
}
@pwds    = ("", "\%USER", "pa\$\$w0rd");
$pwd_cnt = 3;
if ($use_passwords == 1) {
  @pwds = "";
  $pwd_cnt = 0;
  open (PWD, "$PWD_FILE");
  while (<PWD>) {
    chomp;
    next if (/^\#/);
    next if (/NETBIOS/);
    $pwds[$pwd_cnt++] = $_;
  }
}
for $user (@users) {
  for $pwd (@pwds) {
    $pass = $pwd;
    if ($pwd =~ /\%USER1/) {
      $pass  = $user;
      $pass .= "1";
    } elsif ($pwd =~ /\%USER/) {
      $pass  = $user;
    }
    $command0 = "USER $user";
    $command1 = "PASS $pass";
    $t = new Net::Telnet (Telnetmode => 0);
    $t->open(Host => $target, Port => $port);
    $line = $t->getline;
    print "$line" if $debug != 0;
    next unless $line =~ /^\+OK/;
    print "Logging on with $user\/$pass\n" if $debug != 0;;
    $t->print($command0);
    $line = $t->getline;
    print "$line\n" if $debug != 0;
    if ($line !~ /^\+OK/) {
      $t->print("quit");
      $line = $t->getline;
      next;
    }
    $t->print($command1);
    $line = $t->getline;
    print "$line\n" if $debug != 0;
    if ($line !~ /^\+OK/) {
      $t->print("quit");
      $line = $t->getline;
      next;
    }

    $status  = "a";
    $service = "pop3";
    $severity= "us";
    $trustee = "ANY\@ANY";
    $trusted = "ANY\@ANY";
    $service_output="remote service access";
    $text="user $user with passwd ($pass) can access pop3";
    &sara_print();
    $t->print("quit");
    $line = $t->getline;
  }
}

