#!/usr/bin/perl
$|=1;
# Create a set of demo devices
require "./preferences.pl";
die "DEMO mode not enabled in the Settings Area." unless $DEMO;

my($basedir)      = $Basedir;
my($servicesfile) = $Servicesfile;
my($infofile)     = $Infofile;
my($configfile)   = $Configfile;
my($currentfile)  = $Currentfile;

use strict;

my(@iplist, $sip, $filename, $ip, $t, $output);
my($ipmask) = "192.168.10.1-254";
my(@portlist) = (21,23,25,80,98,111,113,139,515,707,3306,6000);
my(%Site) = ();
my(%Services) = ();
local(*F, *H, *I);
my($counter) = 0;

my($cmd) = "rm -fr ".$basedir."/192/168/10";
system($cmd);

my $time = time;
my $tendays = 10*24*60*60;
my @result = ();

print "Creating ";

open(F, "<$servicesfile") || die "Unable to open the services file: $servicesfile";
  while (<F>) {
  $Services{$2} = $1 if m/\s*([^:\s]+)\s*:\s*(.*)$/;
}
$Services{'0'} = "alive";
close(F);

foreach $ip (&GenerateSiteList($ipmask)) {
  $sip = &sip($ip);
  $Site{$sip}{'ip'} = $ip;
  foreach (0..int(rand(12))) {
    $Site{$sip}{'portlist'} .= $portlist[$_].",";
  }
  chop $Site{$sip}{'portlist'};
  ($Site{$sip}{'os'}, $Site{$sip}{'icon'}) = &GenOSandIcon();
}

open(F, ">$configfile") || die $!;
open(I, ">$infofile") || die $!;

foreach $sip (sort keys %Site) {
  $filename = $basedir . '/' . join('/', split(/\./, $Site{$sip}{'ip'})) . ".hist";
  print "." if ($counter++ % 10 == 0);

  print F $Site{$sip}{'ip'}," ",$Site{$sip}{'portlist'},"\n";

  print I "ip => \"",$Site{$sip}{'ip'},"\", ",
          "portlist => \"",$Site{$sip}{'portlist'},"\", ",
          "icon => \"",$Site{$sip}{'icon'},"\", ",
          "os => \"",$Site{$sip}{'os'},"\"\n";

  &CreateDir($filename) unless -f $filename;
  open(H, ">>$filename") || die $!;

  for ($t = ($time-$tendays); $t <= $time; $t += 7200) {
    $output = &DemoNmap($sip,int(rand(100)));
    print H $t,"\t",$output,"\n";
  }

  close(H);
  push(@result, $output);
}

close(I);
close(F);

open(F, ">$currentfile") || die $!;
print F join("\n", @result),"\n";
close(F);

print "\n";
exit;

sub DemoNmap {
    use strict;
    my($sip)    = shift;
    my($stat)   = shift;
    my($output) = "";

    if ($stat > 95) {
      $output = "Host: ".$Site{$sip}{'ip'}." ()\tStatus: Down";
    } else {
      &GenerateName($sip) unless $Site{$sip}{'name'};
      $output = "Host: ".$Site{$sip}{'ip'}." (".$Site{$sip}{'name'}.")\tPorts: ";
      my $p = "";
      foreach (split(/,/, $Site{$sip}{'portlist'})) {
        $p .= $_."/open/tcp//".$Services{$_}."///, " unless (($stat > 85) and (int(rand(100)) > 95));
      }
      chop $p; chop $p;
      $output .= $p;
    }

    return $output;
}

sub GenerateName {
    use strict;
    my($sip) = shift;
    my($n) = "";
    unless ( $Site{$sip}{'name'} ) {
      foreach (1..6) {
        $n .= chr((int(rand(26))+97));
      }
      $Site{$sip}{'name'} = $n . ".localdomain";
    }
    return $Site{$sip}{'name'};
}

sub GenOSandIcon {
    my($os, $icon);
    $os = ('Linux 2.1.122 - 2.2.14',
           'IRIX 6.2 - 6.5',
           'Windows NT4 / Win95 / Win98',
           'MacOS 8.1',
           'HP LaserJet Printer',
           'Tektronix Phaser 360 Extended',
           'HP Procurve Routing Switch')[int(rand(7))];

    if ($os =~ /Mac/) {
      $icon = "mac01";
    } elsif ($os =~ /print|phaser|laser|inkjet/i) {
      $icon = "printr01";
    } elsif ($os =~ /Linux|Solaris|Sun|SCO|Unix|Open|Net|IRIX|MVS|UX|BSD|UNIX|OSF/) {
      $icon = "server01";
    } elsif ($os =~ /Windows/) {
      $icon = "pc02";
    } elsif ($os =~ /SNMP|SMTP|Rout|router|Cisco|RAS|switch|Switch|Gate|Bridge/) {
      $icon = "router01";
    } else {
      $icon = "unk";
    }
    return $os,$icon;
}

sub GenerateSiteList {
    use strict;
    my(@sites) = ();
    my($ip1, $ip2, $ip3, $ip4, $i, @ips);

    my($ipmask) = shift || return;
    @ips = split(/\./, $ipmask);
    return unless $#ips == 3;
    foreach $i (0..3) {
      $ips[$i] =~ s/\-/\.\./g;
      $ips[$i] =~ s/\*/0\.\.255/g;
      $ips[$i] =~ s/^00?(\d)/$1/g;
      $ips[$i] =~ s/(\D)00?(\d)/$1$2/g;
    }
    foreach $ip1 (eval($ips[0])) {
      next unless $ip1 =~ /^\d+$/;
      foreach $ip2 (eval($ips[1])) {
        next unless $ip2 =~ /^\d+$/;
        foreach $ip3 (eval($ips[2])) {
          next unless $ip3 =~ /^\d+$/;
          foreach $ip4 (eval($ips[3])) {
            next unless $ip4 =~ /^\d+$/;
            push(@sites, $ip1.'.'.$ip2.'.'.$ip3.'.'.$ip4);
          }
        }
      }
    }
            
    return @sites;
}

sub CreateDir {
    use strict;
	my $filename = shift || &Error("Unable to create a null directory.");
    my @dirs = split(/\//, $filename);
    pop @dirs;     #Filename part
    unshift @dirs; #Root part
    my $dir = "";
    foreach (@dirs) {
      $dir .= "/" . $_;
      mkdir($dir,0775) unless -d $dir;
      &Error("Unable to create directory: $dir") unless -d $dir;
    }
    return 1;
}

sub sint {
    my($val) = shift;
    my($w) = shift || 3;
    my($c) = sprintf("%${w}d",$val);
    $c =~ s/ /0/g;
    return $c;
}

sub sip {
    my(@ips) = split(/\./, shift);
    my($ip) = &sint($ips[0]).'.'.&sint($ips[1]).'.'.&sint($ips[2]).'.'.&sint($ips[3]);
    return $ip;
}
