#!/bin/sh
#
# GetNiktoDb
#
# Copyright 2012, Weberhofer GmbH, Austria
# 
# This file is part of w3af, w3af.sourceforge.net .
# 
# w3af 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.
# 
# w3af 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 w3af; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

BASEURL='http://cirt.net/nikto/UPDATES'
VERSION='1.36'
FILE='scan_database.db'
FILECHECKSUM='80d127e13a5f66a8f1df5cd2307c7f76'
CURDIR=`dirname $0`
TARGETPATH=`readlink -e "$CURDIR/../plugins/discovery/pykto"`
DOWNLOAD=0
EXIT=0

pushd "$TARGETPATH" > /dev/null
if [ $? -ne 0 ] ; then
	exit 1
fi
if [ -e "$FILE" ] ; then
	if [ `md5sum "$FILE" | awk '{ print $1}' ` == "$FILECHECKSUM" ] ; then
		echo "Database $FILE is ok."
	else
		DOWNLOAD=1
	fi
else
	DOWNLOAD=1
fi
		
if [ $DOWNLOAD -eq 1 ] ; then
	echo "Downloading $FILE"
	curl --connect-timeout 10 -s "http://cirt.net/nikto/UPDATES/$VERSION/$FILE" > "$FILE"
	if [ $? -ne 0 ] ; then
		echo "Could not download file $BASEURL/$VERSION/$FILE" 1>&2
		EXIT=1
	fi
	sed -i "$FILE" -e \
		's/databases on the server. Disable/databases on the server. \\xcaDisable/ ; 
		 s/Open Market Inc.ContentServer/Open Market Inc.\\xcaContentServer/ ; 
		 s/Shopping Cart 1.5 http/Shopping Cart 1.5 \\xcahttp/'
	if [ `md5sum "$FILE" | awk '{ print $1}'` != "$FILECHECKSUM" ] ; then
		echo "Checksum for $FILE is invalid" 1>&2
		EXIT=2
	fi
fi
popd > /dev/null
exit $EXIT
