#!/usr/bin/perl5.8.4
require 'perllib/getopts.pl';
require 'perl/misc.pl';
&Getopts("p:v");
if ($#ARGV < 0) {
   die "Usage: bin/oracletns.sara [-v] [-p port] hostname\n";
}

$target = $ARGV[0];

$tnscmd = "perl/contrib/tnscmd.pl";
@tnsbuffer = "";
$printed   = 0;
$user      = "";
$port      = 1521;
$port      = $opt_p if $opt_p;
$service   = "oracle-tns";

open(TNS, "$tnscmd version  -h $target -p $port|");
while (<TNS>) {
  (@tnsbuffer) = split(/\.\.+/);

}
close(TNS);
for (@tnsbuffer) {
  print "$_\n" if $opt_v;
  if (/TNSLSNR/) {
    $text = $_;
    $status = "a";
    $trustee = "";
    $trusted = "";
    $cve     = "";
   &sara_print();
   $printed = 1;
   if (/Version [0-7]\./ || /Version 8\.0\.[0-6] / || /Version 8\.1\.[0-7] / || /Version 9\.0\.[0-1] / || /Version 9\.2\.0\.[0-2]/) {

     $sid = "ORA-005";
     ($severity,$service_output,$text,$cve) = &get_sid($sid);
     $status = "a";
     $trustee = "ANY\@$target";
     $trusted = "ANY\@$target";
     &sara_print();
   }
   if (/Version 8\.0\.6\.3/ || /Version 8\.1\.7\.3/ || /Version 8\.1\.7\.4/ || /Version 9\.0\.1\.[4-5]/ || /Version 9\.2\.0\.[5-6]/ || /Version 10\.1\.0\.[2-4]/) {
     $sid = "ORA-006";
     ($severity,$service_output,$text,$cve) = &get_sid($sid);
     $status = "a";
     $trustee = "ANY\@$target";
     $trusted = "ANY\@$target";
     &sara_print();
   }
				       
  }
}
#
# Check for packet leakage
#
if ($printed == 1) {
  open(TNS, "$tnscmd ' ' -h $target -p $port --cmdsize 200|");
  while (<TNS>) {
    print if $opt_v;
    if (/USER/) {
      @users = split(/USER/);
      for (@users) {
        if(/^=/) {
          ($_user, $dum) = split(/\)/);
          $_user =~ s/^=//; 
          $_user =~ s/,/;/g;
          $user .= "$_user," if $_user;
        }
      }
    }
  }
  close(TNS);
  if ($user) {
    $user =~ s/,$//;
    $sid = "ORA-001";
    ($severity,$service_output,$texti,$cve) = &get_sid($sid);
    $status = "a";
    $trustee = "ANY\@$target";
    $trusted = "ANY\@$target";
    $text .= " ($user)";
    &sara_print();
  }
  open(TNS, "$tnscmd status -h $target -p $port |");
  while (<TNS>) {
    print if $opt_v;
    if(/LOGFILE/) {
      ($dum, $logfile) = split(/\(LOGFILE/);
      ($logfile, $dum) = split(/\)/,$logfile);
    }
    if (/SECURITY=OFF/) {
      $sid = "ORA-002";
      ($severity,$service_output,$text,$cve) = &get_sid($sid);
      $status = "a";
      $trustee = "ANY\@$target";
      $trusted = "ANY\@$target";
      &sara_print();
      last;
    }
  }
  close(TNS);
#
# We will only do this test if logfile is standard name
#
  if($logfile =~ /listener.log/ && $logfile) {
     open(TNS, "$tnscmd --rawcmd \"(DESCRIPTION=(CONNECT_DATA=(CID=(PROGRAM=)(HOST=)(USER=))(COMMAND=log_file)(ARGUMENTS=4)(SERVICE =LISTENER)(VERSION=1)(VALUE=listener.log)))\" -h $target -p $port|");
     while(<TNS>) {
       print if $opt_v;
       if(/ERR=0/) {
         $sid = "ORA-003";
         ($severity,$service_output,$text,$cve) = &get_sid($sid);
         $status = "a";
         $trustee = "ANY\@$target";
         $trusted = "ANY\@$target";
         &sara_print();
         last;
       }
     }
  } elsif($logfile) {
     $sid = "ORA-004";
     ($severity,$service_output,$text,$cve) = &get_sid($sid);
     $status = "a";
     $trustee = "ANY\@$target";
     $trusted = "ANY\@$target";
     &sara_print();
     last;
  }
}
