#!/bin/sh
#
# OpenAanval Console Installation Script
#

V="1.0"		# Installer Version
OLD=`pwd`
MYPID=$$

# Utility Check
utility_installed() {
	ok=""
	where=`which $1 2>&1 | grep -v "no $1"`
	test -n "$where" && {
		test -x "$where" && return 0
	}
	return 1
}


# Check to ensure all required applications are installed / available

utility_installed "tar" || {
	echo "'tar' was not found within your \$PATH."
	echo "The installation cannot continue"
	echo
	exit
}

utility_installed "snort" || {
	echo "'snort' was not found within your \$PATH."
	echo "Please install snort before you run the installation again"
	echo
	exit
}
 
utility_installed "php" || {
	echo "'php' (binary) was not found within your \$PATH."
	echo "Please install php before you run the installation again"
	echo
	exit
}

utility_installed "mysql" || {
	echo "'mysql' was not found within your \$PATH."
	echo "Please install mysql before you run the isntallation again"
	echo
	exit
}

retrieve_by_web() {
	RETR=""
	utility_installed "wget" && RETR="wget -O - "

	test -z "$RETR" && {
 		utility_installed  "lynx" && RETR="lynx -source "
	}

	test -z "$RETR" && {
	 	echo "wget or lynx are required to download OpenAanval packages"
		exit 
	}
	
	utility_installed "gzip" || {
 		echo "gzip is required to uncompress OpenAanval packages "
		exit
	}
	GZIP="gzip -cd"
 
	utility_installed "tar" || {
 		echo "tar is required to situate OpenAanval package files"
		exit
	}
	TAR=tar

        utility_installed "mysql" || {
                echo "mysql is required for the OpenAanval Console to operate"
                exit
        }
        MYSQL=mysql
 
	echo $Xn "Does this system need a proxy to access the Internet? [y/n] " $Xc
	read answer < /dev/tty
	test -z "$answer" && answer="y"
 
	test "$answer" = "y" && {
 		echo $Xn "IP address of proxy? " $Xc
		read IP < /dev/tty
		echo $Xn "Proxy Port? " $Xc
		read PORT < /dev/tty
		http_proxy="http://$IP:$PORT/"
		export http_proxy
	}
 		
	echo "Downloading openaanval-latest-stable.tar.gz"
	cd $p_path
	$RETR http://www.aanval.com/downloads/openaanval-latest-stable.tar.gz > download.tar.gz
	$GZIP < download.tar.gz > download.tar
	$TAR -xf download.tar 
	rm -f download.tar download.tar.gz

	echo
	echo "Press any key to continue"
	read n < /dev/tty
}

mysql_write() {

	if [ $dbpass ];
	then
		tmp_pass=" -p\"$dbpass\" "
	else 
		tmp_pass=""
	fi

	$MYSQL $dbname -h $dbhost -u $dbuser $tmp_pass < $path/db/openaanval_mysql_db.txt

}

clear
echo "###################################################################################"
echo "##                         OpenAanval by Remote Assessment                       ##"
echo "##                              http://www.aanval.com                            ##"
echo "##                                                                               ##"
echo "##  Installation Assistance/Support Email: administration@remoteassessment.com   ##"
echo "##                                                                               ##"
echo "##                                                                           $V ##"
echo "###################################################################################"
echo
echo "Welcome to the OpenAanval Automated Installation Script!"
echo
echo "This installation script will download, configure and install the latest version"
echo "of the OpenAanval Console."
echo
echo "Requirements:"
echo "a) You should have Snort installed and configured to use MySQL."
echo "b) You should have Apache installed and configured with PHP support."
echo
echo "Installation:"
echo "1) Execute this script; providing accurate information."
echo "2) Proceed to final installation steps within readme.txt"
echo
echo $Xn "Press any key to begin the installation of OpenAanval" $Xc 
read n < /dev/tty
clear

echo "[ OpenAanval Installation ]"
echo "---------------------------"
echo
echo "The following questions will be used to build the appropriate OpenAanval"
echo "configuration files. If you answer incorrectly, you may cancel with CTRL-C"
echo "and restart this installation script."
echo
echo "SITE INFORMATION"
echo "---"
echo "Company Name: (Example: CompanyX, Inc)"
read p_comp < /dev/tty
test -n "$p_comp" && company="$p_comp"
echo
echo "URL to OpenAanval: (Example: www.site.com/openaanval/ or open.site.com) "
read p_site < /dev/tty
test -n "$p_site" && site="$p_site"
echo
echo "PATH INFORMATION"
echo "---"
echo "Install Path: (Example: /var/www/html/)"
read p_path < /dev/tty
test -n "$p_path" && path="$p_path"
echo
echo "DATABASE INFORMATION"
echo "---"
echo "SNORT Database Name: (Example: snortdb"
read p_dbname < /dev/tty
test -n "$p_dbname" && dbname="$p_dbname"
echo
echo "SNORT Database User: (Example: snort_user)"
read p_dbuser < /dev/tty
test -n "$p_dbuser" && dbuser="$p_dbuser"
echo
echo "SNORT Database Password: (Example: secret101)"
read p_dbpass < /dev/tty
test -n "$p_dbpass" && dbpass="$p_dbpass"
echo
echo "SNORT Database Host: (Example: localhost or 127.0.0.1)"
read p_dbhost < /dev/tty
test -n "$p_dbhost" && dbhost="$p_dbhost"

echo
echo "Press [enter] to continue (CTRL-C to Cancel)"
read n < /dev/tty

clear
echo "[ OpenAanval Installation ]"
echo "---------------------------"
echo
echo "OpenAanval will now be downloaded from the Aanval website (www.aanval.com) "
echo "and installed using the parameters entered above."
echo
echo
echo "Press [enter] to continue and begin installation (CTRL-C to Cancel)"
read n < /dev/tty

retrieve_by_web "$tag"

clear
echo "[ OpenAanval Installation ]"
echo "---------------------------"
echo "Creating configuration files and background applications."
echo
echo "<?" > $path/conf.php
echo >> $path/conf.php
echo "// OpenAanval Console Configuration File" >> $path/conf.php
echo "\$aanvalcompany=\"$company\";" >> $path/conf.php
echo "\$root_path=\"$path\";" >> $path/conf.php
echo "\$aanvalsite=\"$site\";" >> $path/conf.php
echo >> $path/conf.php
echo "\$aanvaldb=\"$dbname\";" >> $path/conf.php
echo "\$aanvaldbuser=\"$dbuser\";" >> $path/conf.php
echo "\$aanvaldbpass=\"$dbpass\";" >> $path/conf.php
echo "\$aanvaldbhost=\"$dbhost\";" >> $path/conf.php
echo >> $path/conf.php
echo "\$version_checking=1;" >> $path/conf.php
echo >> $path/conf.php
echo "?>" >> $path/conf.php
echo
echo "$path/conf.php >> written"

echo "#!/usr/bin/perl -w" > $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "use strict;" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "$|++;" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "while(1) {" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "system(\"/usr/bin/php -q $path/apps/processor.php &\");" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "sleep 5;" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "}" >> $path/apps/ids_background.pl
echo >> $path/apps/ids_background.pl
echo "$path/apps/ids_background.pl >> written"

echo "#!/usr/bin/perl -w" > $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "use strict;" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "$|++;" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "while(1) {" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "system(\"/usr/bin/php -q $path/apps/hostname.php &\");" >> $path/apps/ids_misc.pl
echo "system(\"/usr/bin/php -q $path/apps/total_events.php &\");" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "sleep 60;" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "}" >> $path/apps/ids_misc.pl
echo >> $path/apps/ids_misc.pl
echo "$path/apps/ids_misc.pl >> written"
echo
echo "Press any key to continue"
read n < /dev/tty

clear
echo "[ OpenAanval Installation ]"
echo "---------------------------"
echo
echo "*** Follow the instructions in readme.txt to complete the installation ***"
echo
echo "Your default LOGIN & PASSWORD to the OpenAanval Console is:"
echo "Username: root"
echo "Password: specter"
echo
echo "Problems/Comments:"
echo "                   administration@remoteassessment.com"
echo "                   support@remoteassessment.com"
echo
echo "Press any key to exit"
read n < /dev/tty
