#VERSION,1.00
###############################################################################
#  Copyright (C) 2006 Chris Sullo
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; version 2
#  of the License only.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to 
#  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# PURPOSE:
# Perform Drupal specific tests
###############################################################################
sub nikto_drupal_init {
    my $id = { name        => "drupal",
               full_name   => "Drupal Specific Tests",
               author      => "Tautology",
               description => "Performs a selection of drupal specific tests",
               hooks       => { scan => { method => \&nikto_drupal, }, 
                                start => { method => \&nikto_drupal_load, },
                              },
               options     => {
								enum-modules => "Flag to tell plugin to enumerate modules",
								path => "Basic path for modules (can usually be found in page source).",
							  },
               copyright   => "2014 Chris Sullo"
               };
    use vars qw/$DRUPALMODULESDB/;
    return $id;
}

sub nikto_drupal_load {
    # load up the database here so we don't redo it each time
    $DRUPALMODULESDB = init_db("db_drupal");
}

sub nikto_drupal {
	# load mark and parameters from plugin caller
    my ($mark, $parameters) = @_;
	
	# quit if user is terminating - this is a catch all and we should never execute it
	return if $mark->{'terminate'};
	
	# Check whether we should enumerate the modules
	return if !$parameters->{'enum-modules'};

	# Default module path
	my $modulepath="/sites/default/modules";
	
	# override it if it is passed as a parameter
	if (defined $parameters->{'path'}) {
		$modulepath=$parameters->{'path'};
	}
	
	# Now the main meat: we shall enumerated through the database and attempt to look for the
	# licence file in each module which is at modulepath/module/LICENSE.txt
    foreach my $item (@$DRUPALMODULESDB) {
		# $item will contain the entry in db_drupal, made into a hash pointer
		
		# Check if the user is terminating the current mark
        return if $mark->{'terminate'};
		
		# Set up URI
		my $uri = "$modulepath/$item->{'module'}/LICENSE.txt";
		
		# Fetch the URI, we use nfetch to ensure that auth, headers etc are taken into account
        my ($res, $content, $request, $response) = nfetch($mark, $uri, "GET", "", "", "", "drupal detection");
		
		# We're looking for a 200 response
        if ($res eq "200") {
			# We could assume we've got a validate result here, but we may as well check the
			# data for the text LICENSE [sic] to ensure that we have a real match
			
			if ($content =~ /LICENSE/) {
				# Looks like a match - raise this up to the front end
				add_vulnerability(
					$mark, 			 		 # mark structure to identify target
					"Drupal module $item->{'module'} found", # message
					$item->{'nikto_id'},			 # tid
					0, 					 # OSVDB reference
					$uri,					 # URI
					$request,				 # Request structure for full output
					$response);				 # Response structure for full output
            }
        }
    }
}

1;
