Skip to content
Snippets Groups Projects
cli-install.sh 2.25 KiB
Newer Older
#!/bin/bash 

function yesno_prompt()
{
	local msg=$1
	local default_answer=$2

	while true
	do
		echo -n $msg
		if [[ $default_answer == 1 ]]; then
			echo -n " [Y/n] "
		else
			echo -n " [y/N] "
		fi

		case $yn in
		[Yy] ) return 1
			;;
		[Nn] ) return 0
			;;
		"" ) return $default_answer
			;;
		* ) echo "Please answer with y or n."
		;;
		esac
	done
}

function do_afl()
{
	which afl-fuzz  > /dev/null 2>&1 
	local has_afl=$?

	echo "Configuring core pattern for AFL"
	echo core >/proc/sys/kernel/core_pattern

	if [[ $has_afl -eq 0 ]]; then
		echo "AFL is already installed, skipping install."
		return
	fi

	yesno_prompt "AFL was not detected.   Would you like me to using the package manager to attempt to install it for you? " 1 
	local install_afl=$?;

	if [[ $install_afl -eq 1 ]]; then
		which apt-get  > /dev/null 2>&1 
		local has_apt=$?
		which yum  > /dev/null 2>&1 
		local has_yum=$?
		echo "Installing AFL ..."

		if [[ $has_apt -eq 0 ]] ; then
			sudo apt-get install afl  || ! echo "Had trouble installing afl, giving up..." || exit 1
		elif [[ $has_yum -eq 0 ]] ; then
			sudo yum     install afl  || ! echo "Had trouble installing afl, giving up..." || exit 1
		else
			echo "Cannot detect package manager" 
			exit 1

		fi
	else
		echo "Skipping AFL install upon user request."
	fi
}

function install_files()
{
	declare -A files=(
		["libautozafl.so"]="/usr/local/lib 755"
		["libzafl.so"]="/usr/local/lib 755"
		["turbo-cli"]="/usr/local/bin 755"
		)

	for file in ${!files[@]}
	do
		local item="${files[$file]}"
		local path=$(echo $item |cut -d" " -f1)
		local perms=$(echo $item |cut -d" " -f2)

		if [[ -e $path/$file ]]; then
			yesno_prompt "File $path/$file already exists. Overwrite it? " 1 
			local overwrite=$?
			if [[ $overwrite -eq 0 ]]; then
				continue
			fi 
		fi
		echo "Installing $path/$file..."
Jason Hiser's avatar
Jason Hiser committed
		curl --fail -s -o $path/$file https://allzp.zephyr-software.io/turbo/$file 
		chmod $perms $path/$file
		local curl_res=$?
		if [[ $curl_res -ne 0 ]]; then
			echo "Cannot download $file, is the network up?" 
			exit 1
		fi
	done
	
}

function check_root()
{
	if [[ "$EUID" -ne 0 ]]; then 
		echo "Please run as root"
		exit 1
	fi
}

function main()
{
	check_root
	do_afl
	install_files

	echo "Install finished!"
	exit 0
}


main "$@"