;# getopts.pl - a better getopt.pl

;# Usage:
;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
;#                           #  side effect.
sub Getopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;
    local($[) = 0;

    @args = split( / */, $argumentative );
    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
	($first,$rest) = ($1,$2);
	$pos = index($argumentative,$first);
	if($pos >= $[) {
	    if($args[$pos+1] eq ':') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		eval "\$opt_$first = \$rest;";
	    }
	    else {
		eval "\$opt_$first = 1";
		if($rest eq '') {
		    shift(@ARGV);
		}
		else {
		    $ARGV[0] = "-$rest";
		}
	    }
	}
	else {
	    print STDERR "Unknown option: $first\n";
	    ++$errs;
	    if($rest ne '') {
		$ARGV[0] = "-$rest";
	    }
	    else {
		shift(@ARGV);
	    }
	}
    }
    $errs == 0;
}

1;

sub sans10 {
local($arg) = @_;
($arg,$cert) = split(/\[/,$arg);
$arg = $cert if ($cert);
$arg =~ s/\]//;
$sans_label = "";
# return 1 if $sans ne "on";
  $sans_type = "SANS TOP 20";
  $sans_url = "html/cve/Top_20_cve.html";
  $sans_file = "html/cve/cve_sara.db";
return 1 if ! -f $sans_file;
if (! @sans_vul) {
   $cnt = 0;
   open (SANS, $sans_file);
   while (<SANS>) {
     chomp;
     if (($_ !~ /^#/) || (! $_)) {
    ($cve[$cnt],$text[$cnt],$sara_vul[$cnt],$dum,$sans_vul[$cnt])=split(/\|/);
       $sara_vul[$cnt] =~ s/\.html//;
       $sara_vul[$cnt] =~ s/_/ /g;
       $cnt++;
     }
   }
}
   $i = 0;
   $sans_label = "";
   for $test (@sara_vul) {
    if ($test eq $arg) {
     if ($sans_vul[$i]) {
      if ($sans_label !~ /$sans_vul[$i]/) { 
        $sans_label .= " <A HREF=\"#$sans_vul[$i]\">$sans_vul[$i]</A>";
      }
     }
    }
     $i++;
   }
   return 1 if $sans ne "on" || $sans_label;
   return 0;
}
#
# Routine to compare to-be-reported vulnerabilities with the
# corrections file to determine if Reporter should report it.
# args are $target and $text
#
sub correction {
  local($arg) = @_;
  ($mytarget,$mykeyword) = split(/\|/,$arg);
  $mykeyword =~ s/\\//g;
  $mykeyword =~ s/\///g;
  return 0 if $correction ne "on";
  return 0 if ! -f $correction_file;
  open (CORRECTION, $correction_file);
  while (<CORRECTION>) {
   chomp;
   $_ =~ s/\\//g;
   $_ =~ s/\///g;
    if (($_ !~ /^#/) || (! $_)) {
      ($nexttarget,$nextkeyword,$nextadmin,$nextdate) = split(/\|/,$_);
      $mykeyword =~ s/[\:\/\(\)\[\]\$]//g;
      $nextkeyword =~ s/[\:\/\(\)\[\]\$]//g;
#      if( $nextadmin  && $nextdate ) {
        if ( $nexttarget =~ /$mytarget/ && $mykeyword =~ /$nextkeyword/) {
         close(CORRECTION);
         return 1;
#        }
      }
    }
  }
  close(CORRECTION);
  return 0;
}
sub add_correction {
  open (CORRECTION, ">>$correction_file");
    print CORRECTION "@_\n";
  close (CORRECTION);
}
sub drop_correction {
  my ($args) = @_;
  return 0 if ! -f $correction_file;
  my ($_target,$_string) = split(/\|/,$args);
  open (OLD, "> tmp.$$");
  open (CORRECTION, "$correction_file");
  while (<CORRECTION>) {
    chomp;
    ($target,$string,$admin,$date) = split(/\|/);
    $full_string = $string;
    $string  =~ s/[\:\/\(\)\[\]\$]//g;
    $_string =~ s/[\:\/\(\)\[\]\$]//g;
    next if(($_target =~ /$target/) && ($_string =~ /$string/));
    print OLD "$target|$full_string|$admin|$date\n";
  }
  close(OLD);
  close(CORRECTION);
  rename("tmp.$$", "$correction_file");
}
