#!/usr/bin/perl
# 
#  	iManListPlugins
#  		Program to list the plugins installed on iManager remotely.
#
#       Version  1.0
#       Date     9th Dec 2006
#    
#       Author   Nagareshwar Y Talekar (tnagareshwar@novell.com)
#
#
##################################################################################################


use LWP::UserAgent;
use Net::Ping;
use Socket; 
use Switch;
use threads; 


use strict;

$|++;                       # force auto flush of output buffer


 my $host;
 my $disablePing = 0;
 my $port=80;
 
my @plugins = ("ldap_plugin.npm", 
               "nmas.npm",
               "sp_iman26.npm",
               "sso.npm", 
               "pcprox.npm", 
               "sw.npm",
               "secretstore.npm",
               "arkmgmt.npm",
               "BAndR.npm",
               "RWiz.npm",
               "ICEWiz.npm",
               "indexmanager.npm",
               "dsmerge.npm",
               "dsrepair.npm",
               "servicemanager.npm",
               "snmp.npm",
               "EA.npm",
               "ER.npm",
               "eDirExtended.npm",
               "iFolder3.npm",
               "iPrint for NetWare Servers.npm",
               "iPrint for Linux Servers.npm",
               "naudit.npm",
               "bm.npm",
               "vpn.npm",
               "identity_manager_plugins.npm",
               "SharedContentV1.npm",
               "kerberosPlugin.npm",
               "PS.npm",
               "pwdpolicy.npm",
               "wanman.npm",
               "zfsca.npm",
               "ZFS_PolyDistPlugins.npm",
               "CSP.npm",
               "ncsmgmt.npm",
               "pki.npm",
               "FTPADMIN.npm",
               "hmsplugin.npm",
               "DirXMLCommon.npm",
               "nlsplug.npm",
               "nsadmin.npm",
               "NFSGYAdmin.npm",
               "notfconfig.npm",
               "NTPTimeSync.npm",
               "QFSearch.npm",
               "sms.npm",
               "nssmgmt.npm",
               "WebLook.npm"
               );

my @descs = ("LDAP Management Plugin",
             "Novell Modular Authentication Service (NMAS) Plugin",
             "Support Pack 2 for iManager 2.6",
             "Novell Secure Login Administration Plugin", 
             "NSL pcProx Method Management Plugin", 
             "Novell Secure Workstation Plugin",
             "Secret Store Administration Plugin",
             "Archive Versioning Plugin",
             "eDirectory Backup and Restore Plugin",
             "eDirectory Filtered Replica Management Plugin",
             "eDirectory ICE Plugin",
             "eDirectory Indexes Plugin",
             "eDirectory Merge Plugin",
             "eDirectory Repair and Logfile Plugin",
             "eDirectory Service Management Plugin",
             "eDirectory SNMP Plugin",
             "Encrypted Attributes Plugin",
             "Encrypted Replication Plugin",
             "Extended Native Libraries, required for ICE Plugin",
             "iFolder 3 iManager Plugin",
             "iPrint Management Plugin for Netware",
             "iPrint Management Plugin for Linux",
             "Novell Audit 2.0 Configuration and Reporting Plugin",
             "Novell Border Manager Firewall Configuration Plugin",
             "Novell Border Manager VPN Configuration Plugin",
             "Novell Identity Manager 3 Plugins",
             "Novell iManager Content - needed for Universal Password",
             "Novell Kerberos Plugin",
             "Priority Sync Plugin",
             "Universal Password Management Plugin",
             "WAN Traffic Management Plugin",
             "ZENworks Certificate Authority Plugin",
             "ZENworks 7.0.1 Server Management Plugin",
             "Case Sensitive Password Plugin",
             "Cluster Services Management Plugin",
             "eDirectory Certificate Management Plugin",
             "FTP Administration Plugin",
             "Health Monitor iManager Plugin",
             "IDM - Common Utilities Plugin",
             "Licensing and Metering Plugin",
             "NetStorage Management Plugin",
             "NFS Management Plugin",
             "eMail Notification Configuration Plug-ins",
             "NTP Timesync Administration Plugin",
             "QuickFinder Server Management",
             "SMS Management Plugin",
             "Storage Management Plugin (NSS)",
             "Utililites for Installing NetWare 6.5 Plugin"
             );


#check if any command line parameters specified
if( @ARGV < 1 || @ARGV > 4)
{
	printUsage();
 	exit;
}

my $i=0;
for(; $i < @ARGV-1 ; $i++)
{
    if( $ARGV[$i] =~ /-d/i )
    {
	#print "\n Disabling the ping scan....";
	$disablePing = 1;	    
	next;    
    } 
	
    if( $ARGV[$i] =~ /-p/i )
    {
	$i++;
	if( $i < @ARGV-1 )
	{
		$port = $ARGV[$i];	    
		#print "\n port = " . $port;
	}
	else
	{
	   printUsage();
	   print "\n\n Error : Specify valid port number with -p option\n\n";
	   exit;
	}

	next;	    
    } 
    
    # Some invalid option specified
    printUsage();
    print "\n\n Error : Invalid options specified \n\n";
    exit;

}
 
 $host = $ARGV[@ARGV-1];

 #print "\n host = $host and port $port";

 
 print "\n\n iManager Plugins Lister v1.0 \n\t\tby Nagareshwar Y Talekar (tnagareshwar\@novell.com)\n";
 
  # check if the host is alive
  if( !$disablePing )
  {
      #print "\n checking if the host is alive ";
      if( !checkHost($host) )
      {
          print "\n Specified host $host is not alive\n\n" ;
          exit;   
      }
  }    
  
  #check if iManager present
  if( CheckiManager($host, $port, "nps/iManager.html") == 0)
  {
  	print "\n iManager is not running on specified host $host\n\n";
  	exit;    	
  }
  
  
  
  # Now scan for each host one by one
  print "\n List of plugins installed on $host\n";
 
  print "\n*******************************************************************************************";
  print "\n Plugin Name \t\t\t    Description ";
  print "\n*******************************************************************************************\n";
  
  my $i;
  my @threadList;
  my $thr;
  
  for($i=0; $i < @plugins ; $i++)
  {
  
  	#scanHost($host, $plugins[$i]);
  	#CheckSnapin($ip, $port, "nps/packages/pcprox.npm") 
  	$thr = threads->new( \&CheckSnapin, $host, $port, $i); 
	push(@threadList, $thr);
  }
  
  
  # waiting for all threads to terminate..
  foreach (@threadList) 
  { 
      $_->join(); 
  }
  
  
  print "\n*******************************************************************************************";
   
  print "\n\n Finished listing the plugins for the host $host\n Cleaning up, please wait...\n\n";
  
 
 
 
  exit;
 
 
 
 
 
 
 
 
 
 #****************************************************************************
  
    
  
  sub CheckiManager      
    {
      my $ip = $_[0];
      my $port = $_[1];
      my $snapin = $_[2];
      
   
  	  my $ua = new LWP::UserAgent;
  
  	  # wait for 5 seconds before getting response
  	  # This timeout does not work if the port is not open
  	  # It works only for http protocol transaction
  	  $ua->timeout(5);
  
  	  $ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5");
  	  my $url = "http://" . $ip . ":$port/" . $snapin;
  	  #print "\n url = $url";
  
  	  my $request = HTTP::Request->new(HEAD => $url);
  	  my $resp = $ua->request($request);
  
  	  if( $resp->is_info || $resp->is_success )
  	  {
    		if( $resp->server =~ /apache/i )
    		{
    		    return 1;
    		}
   	 		
  		#print "\n response " . $resp->content_type;
  		#print "\n length   " . $resp->content_length;
  		#print "\n title   " . $resp->server;
  		
  		return 0;
  	  }
  	  else
  	  {
  		return 0;
  	  }
    
     
   	  return 0;
 }
  
  
  
  #****************************************************************************
  
  sub CheckSnapin
  {
    my $ip = $_[0];
    my $port = $_[1];
    my $index = $_[2];
    
 
	  my $ua = new LWP::UserAgent;


	  # wait for 5 seconds before getting response
	  # This timeout does not work if the port is not open
	  # It works only for http protocol transaction
	  $ua->timeout(5);

	  $ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5");
	  my $url = "http://" . $ip . ":$port/nps/packages/" . $plugins[$index];
	  #print "\n url = $url";

	  my $request = HTTP::Request->new(HEAD => $url);
	  my $resp = $ua->request($request);

	  if( $resp->is_info || $resp->is_success )
	  {
	  	if( $plugins[$index] =~ /npm/i )
	  	{
	  	     if( $resp->content_type =~ /plain/ && $resp->content_length > 1000 )
	  	     {
	  	     	printf " %-35s%s\n", $plugins[$index], $descs[$index];
	  	     	#print "My Test Plugin" . "\n";
	  	     	#print $descs[$index] . "\n";
	  	     	return;
	  	     }
	  	}
	  	
	 		
		#print "\n response " . $resp->content_type;
		#print "\n length   " . $resp->content_length;
		#print "\n title   " . $resp->server;
		
	  }
   
 	  return 0;
 }
 
  
  
  
 
 #****************************************************************************
  
 # Check if the host is alive 
 sub checkHost
 {
 
   #return 1;
   my $target = $_[0];
   
   #print "\n Checking for host " . $target;
   
   my $p = Net::Ping->new("icmp", 1, 1);
 
   my $result = $p->ping($target);
   
   $p->close();
   
   return $result;
   
 }
 
 
 
 
 
 
 #****************************************************************************
 
 # print the usage  
 sub printUsage
 {
    print "\n\n iManager Plugins Lister v1.0 \n\t\tby Nagareshwar Y Talekar (tnagareshwar\@novell.com)";
    print "\n\n Usage: \n\tperl iManListPlugins.pl [-d] [-p <port>] {hostname} \n\n";
    print "\n Options: \n\t-d          Do not use ping scan to check if the host is alive \n";
    print "\t-p <port>   Use specified <port> instead of default port 80 \n";
    print "\n Example: \n\tperl iManListPlugins.pl -d ldap.myhost.edu\n\tperl iManListPlugins.pl -d -p 8080 192.168.0.100\n\n";
    
 }
 
 
  #****************************************************************************

