#!/usr/bin/perl -U

use Getopt::Std;
use Time::Local;

############## DEFAULT VARIABLES #############################################
$START_HOUR = "8";
$START_MIN = "00";
$END_HOUR = "17";
$END_MIN = "30";
############## DEFAULT VARIABLES #############################################

$usage = "usage: $0 [options] [-a/-b/-c network...] [DataBase Directory]

-f       firewall option
-s:      Start Time ( 0-23: 0-59 )
-e:      End Time ( 0-23: 0-59 )

-a:      Class A Network (xxx)
-b:      Class B Network (xxx.xxx)
-c:      Class C Network (xxx.xxx.xxx)
";


 #Setup command line options..
 getopts('fs:e:a:b:c:');
 $FIREWALL = "-f" if (defined($opt_f));

 #Setup Time..Default 9am-4:30pm
 $START_TIME = $opt_s if (defined($opt_s));
 $END_TIME = $opt_e if (defined($opt_e));

 #Setup Start Time
 if ($opt_s) {
   @start_time = split(/:/, $START_TIME);
   $START_HOUR = @start_time[0];
   $START_MIN = @start_time[1];
 }

 #Setup End Time
 if ($opt_e) {
   @end_time = split(/:/, $end_TIME);
   $END_HOUR = @end_time[0];
   $END_MIN = @end_time[1];
 }

 #Get network stuff
 $NETWORK = $opt_a if (defined($opt_a));
 $NETWORK = $opt_b if (defined($opt_b));
 $NETWORK = $opt_c if (defined($opt_c));

 if (!$opt_a && !$opt_b && !$opt_c) {
  print $usage;
  die;
 }

 #DataBase Directory & Log
 $DB_DIR =  "$ARGV[0]";
 $LOG_FILE = "$DB_DIR.log";

 #Start command....
 $SAINT_CMD .= "./saint -a2 -l0 -u -v";

 #Does it have the firewall option
 if ($opt_f) {
   $SAINT_CMD .= " $FIREWALL";
 }

 #Perform Subnet scan 
 #$SAINT_CMD .= " -s";
 $SAINT_CMD .= " -d $DB_DIR";

 if ($opt_a) {
  while (!$done) {
   for ($i=1; $i<255; $i++) {
    for ($j=1; $j<255; $j++) {
     for ($k=1; $k<255; $k++) {
       &scan("$NETWORK.$i.$j.$k");
     }
    }
   }

   $done = 1;
  }
 }
 elsif ($opt_b) {
  while (!$done) {
   for ($i=1; $i<255; $i++) {
    for ($j=1; $j<255; $j++) {
     &scan("$NETWORK.$i.$j");
    }
   }

   $done = 1;
  }
 }
 elsif ($opt_c) {
  while (!$done) {
   for ($i=1; $i<255; $i++) {
    &scan("$NETWORK.$i");
   }
  
   $done = 1;
  }
 }
 else {
  print "Nothing set to scan\n"
 }


#Pass the Network to scan
sub scan {

   $net = @_[0];

   $thishour = (0..23)[(localtime)[2]];
   $thismin = (0..59)[(localtime)[1]];

   if (($thishour > $START_HOUR) && ($thishour < $END_HOUR)) {
     $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
     system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
   }
   elsif ($thishour == $START_HOUR) {
    if ($thismin >= $START_MIN) { 
     $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
     system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
    }
    else {
     $sec_time = (($START_MIN - $thismin)*60); 
     sleep(($START_MIN - $thismin)*60);
     $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
     system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
    }
   }
   elsif ($thishour == $END_HOUR) {
    if ($thismin <= $END_MIN) {
     $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
     system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
    }
    else {
     sleep ((timelocal(0, 59,  23, (0..30)[(localtime)[3]],
             (0..11)[(localtime)[4]], (0..99)[(localtime)[5]]) - time) + 60 +
           (($START_HOUR*60*60)+($START_MIN*60)));

     $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
     system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
    }
   }
   elsif ($thishour < $START_HOUR) {
    sleep (timelocal(0, $START_MIN,  $START_HOUR, (0..30)[(localtime)[3]],
             (0..11)[(localtime)[4]], (0..99)[(localtime)[5]]) - time);
    $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
    system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
   }
   elsif ($thishour > $END_HOUR) {
    sleep ((timelocal(0, 59,  23, (0..30)[(localtime)[3]],
             (0..11)[(localtime)[4]], (0..99)[(localtime)[5]]) - time) + 60 +
           (($START_HOUR*60*60)+($START_MIN*60)));
    $GO_CMD =  "$SAINT_CMD $net >> $LOG_FILE";
    system("echo '$GO_CMD'") == 0 or die "Can't execute $GO_CMD\n";
   }
   else {
    print "Error occured!!!!\n";
    die;
   }
}
