#!/usr/bin/perl5.8.4
##################################################################
# login.sara: version 3.0, 22 Mar 01, 23:30:00, last mod by todd #
# 
# Program upgraded to use Telnet.pm providing faster tests.  Still
# supports EZsetup signature and other functional characteristics
# of shell besed login.sara
#######################################


# Added so that Telnet.pm  module code be used from perl/Net
use lib 'perl/Modules';
use Net::Telnet ();
require 'config/paths.pl';
require 'perl/misc.pl';

require 'perllib/getopts.pl';

$passwd = "";
$pass   ="";
$username = "";
$debug = "";
$timeout = 15;
$banner = "authorized";

$usage = "Usage: login.sara  -u <login> -p <passwd> port target \n";
&Getopts("u:p:v") || die $usage;
$username = $opt_u if defined($opt_u);
$passwd = $opt_p if defined($opt_p);
$debug    = "true" if defined($opt_v);

if ($#ARGV < 1) {
   die $usage;
}
$target = "$ARGV[1]";
$port ="$ARGV[0]";
if($ARGV[0] =~ /\:/) {
   $port =~ s/\:TCP//;
} else {
   open(SERVICES, $SERVICES) or die "$0: cannot open $SERVICES: $!\n";
   while(<SERVICES>) {
      $port_name{$1} = $2 if /(\S+)\s+([0-9]+)\/tcp/;
   }
   $port = $port_name{$ARGV[0]} if $port_name{$ARGV[0]};
}

if (!$port) {
   $port = getservbyname($ARGV[0], "tcp");
}
$service = $ARGV[0];

 $status = "a";
 $severity = "g";
 $trustee  = "";
 $trusted  = "";
 $service_output = "";
 $text = "offers $service";
#
# Perform raw login to handle all login situations including EZsetup
#
$t = new Net::Telnet (Timeout => $timeout);
$t->errmode("return");
$t->open(Host => $target, Port => $port);
#Look for login/username banner
(@banner,@line) = $t->waitfor( Match => '/ogin:/', Match => '/sername:/');
&telnet_ok() if $err = $t->errmsg;
print "@banner@line" if $debug;
$printed = 1;
for (@banner) {
   if (/$banner/i) {
      $printed = 0;
      last;
   }
}
if ($printed == 1) {
   $sid = 'MISC-003';
   ($severity,$service_output,$text, $cve) = &get_sid($sid);
   $status  = "a";
   $text =~ s/\;/$service/;
   $status = "a";
   $trustee  = "ANY\@$target";
   $trusted  = "ANY\@$target";
   &sara_print();
}
sleep 2;
$t->print($username);
# Look for Password or prompt character
(@line,@dummy) = $t->waitfor(Match => '/assword:/', Match => '/[\$%#&gt;] $/');
print "@dummy @line" if $debug;
#If we get neither, we may be in EZsetup mode: try to break out
if ($t->errmsg) {
  $t->print("\003");
  @line = $t->waitfor('/[\$%#&gt;] $/');
  print @line if $debug;
  &telnet_ok if $err = $t->errmsg;
} else {
# We get a response to Password/prompt waitfor
 ($dummy, $data) = @line;
# We get a password; so we submit one if provided
  if ($data =~ /assword/) {
      if ($passwd) {
         $t->print($passwd);
         @line=$t->waitfor(Match=>'/login\: $/', Match=>'/[\$%#&gt;] $/');
	 if ($t->errmsg) {
            $t->print("\003");
            @line = $t->waitfor('/[\$%#&gt;] $/');
	    &telnet_ok() if $t->errmsg;
         } else {
           ($dummy, $data) =@line;
	   &telnet_ok if ($data =~ /^login:/);
         }  
        $pass = "with password\n";
       } else {
          &telnet_ok();
       }
  } 
$t->close();
}
$status = "a";
$sid    = 'MISC-002';
($severity,$service_output,$text, $cve) = &get_sid($sid);
$trustee = "ANY@$target";
$trusted = "ANY@$target";
$text =~ s/\;/$username/;
$text =~ s/\;/$pass/;
$text =~ s/\;/$service/;
&sara_print();
exit 0;
sub telnet_ok {
  &sara_print();
  exit 0;
}
