#!/bin/sh
# Download, update, and query local exploit database from exploit-db.com
# Local exploit-db.com exploit database tool by Deeno9 2010
# Based on Local Milw0rm.com exploit database tool by Harvie 2009 ( http://blog.harvie.cz/ )
# Modified and cleaned up by Evan Teitelman <teitelmanevan@gmail.com>

echo "$(tput bold; tput setaf 1)Note: sploitctl is better. Try it out. pacman -S sploitctl.$(tput sgr0)"

target_dir=/var/exploit-db

case "$1" in
	update)
		if [ $EUID != 0 ]; then
			echo >&2 'You must run exploit-db update as root.'
			exit 1
		fi

		echo 'Downloading and extracting exploit archive from exploit-db.com...'

		mkdir -p "$target_dir"
		cd "$target_dir"

		curl "http://www.exploit-db.com/archive.tar.bz2" | tar xjp

		# Enforce permissions.
		find . -type d -exec chmod -R 755 {} \;
		find . -type f -exec chmod -R 644 {} \;
		;;
	search)
		grep -i "$2" "$target_dir/files.csv" | \
			sed -r 's@^[0-9]+,([^,]+),"([^"]*)",([0-9-]+),.*@'"$target_dir"'/\1 \2 (\3)@'
		;;
	usearch)
		$0 update
		$0 search "$2"
		;;
	*)
		echo "Target directory: $target_dir"
		echo "usage: $0 <update|search|usearch> [regex]"
esac
