#!/usr/bin/perl
# nss - Written By Narrow (nss@privacyx.com)

# Needed Files
use Socket;
use Getopt::Std;

require 'include/banner.rs';
require 'include/logger.rs';
require 'include/report.rs';
require 'include/count.rs';
require 'include/mail.rs';
require 'include/modes.rs';
require 'nss.conf';

# Main Code [-START-]
$|=1;
getopts("h:l:m:s:", \%args);
&banner;
if(!%args) {
print <<"EOT";
Usage: (perl) $0 <options> ...

Options:
-h [host file ]	-> File with hosts that will be scanned
-s [host name ] -> Scan a single host
-l [log file  ]	-> File where the results are saved (default: [hostfile].log)
-m [email addr]	-> Send the results to <email addr>

To configure NSS, edit the file "nss.conf".

EOT
exit;
}
if(!defined $args{l}) {
$logfile = "$args{h}.log";
} else {
$logfile  = $args{l};
}
$hostfilz = $args{h};
$mailto = $args{m};
$single = $args{s};
if($single) {
    $logfile = "./single/$single.log";
}

# Plugins
print "Loading Plugins ...\n";
opendir(DIR, "plugin/unix");
while($in=readdir(DIR)) {
    next if ($in=~/^[.]{1,2}/);
    next if !($in=~/\.rs$/);
    require "plugin/unix/$in";
}
closedir(DIR);

opendir(DIR, "plugin/windows");
while($in=readdir(DIR)) {
    next if ($in=~/^[.]{1,2}/);
    next if !($in=~/\.rs$/);
    require "plugin/windows/$in";
}
closedir(DIR);
if($single) { &single_scan($single); } else { &hostfile_scan($hostfilz); }
if(length($mailto) != 0) {
    &mailer($mailto, $logfile);
}

print "\nScan Done.\n";
exit(0);

sub single_scan {
$hosts = shift;
$Alive=0;
    print "\nScanning $hosts ...";
    if($chk_alive == 1) {
    if(inet_aton($hosts)) {
	$Alive=1;
	print "\t[ALIVE]\n";
    } else {
	print "\t[DEAD]\n";
    }
    } else {
	$Alive=1;
    }
    &logs("$logfile","open");
    if($scan_mode == 0) {
	if($Alive) {
	    &winscan($hosts);
	}
    }
    elsif($scan_mode == 1) {
	if($Alive) {
	    &unxscan($hosts);
	}
    }
    elsif($scan_mode == 2) {
	if($Alive) {
	    &detscan($hosts);
	}
    }
    else { print "Unknow mode. Trying to detect ...\n";
	$scan_mode = 2;
	    if($Alive) {
		&detscan($hosts);
	}
}
    print LOG "\n";
    &logs("$logfile","close");
}

sub hostfile_scan {
$hostfile = shift;
print "Loading $hostfile ... "; $count = 0;
count("$hostfile");
foreach $cnn (@cnt) { $count++; }
if($count <= 0) { print "File \"$hostfile\" Possible Empty!\n\n"; exit; }
print "$count Host(s) Found!\n\n"; $now=1;
foreach $host (@cnt) {
    chomp($host); $Alive=0;
    &report($host, $now, $count, "$hostfile.rpt");
    print "Scanning $host \($now of $count\)";

    if($chk_alive == 1) {
    if(inet_aton($host)) {
	$Alive=1; print "\t[ALIVE]\n";
    } else {
	print "\t[DEAD]\n";
    }
    } else {
	$Alive=1;
    }
    &logs("$logfile","open");

    if($scan_mode == 0) {
	if($Alive) {
	    &winscan($host);
	}
    }
    elsif($scan_mode == 1) {
	if($Alive) {
	    &unxscan($host);
	}
    }
    elsif($scan_mode == 2) {
	if($Alive) {
	    &detscan($host);
	}
    }
    else { print "Unknow mode. Trying to detect ...\n";
	$scan_mode = 2;
	    if($Alive) {
		&detscan($host);
	}
}
    &logs("$logfile","close");
    $now++;
}
return;
}

# Main Code [-END-]