#!/bin/sh

###
# This program written by ADE Muffett (aem@aber.ac.uk), August 1991,
# as part of the 'Crack' password cracking package.
###

###
# For Bourne shell version, CRACK home cannot be relative to ~username
# (unless you have a really weird 'sh')
###

CRACK_HOME="/aber/aem/SEC/CRACK32"              # This is an ENVIRONMENT var.
export CRACK_HOME

trash="/dev/null"
remote="FALSE"
default_bin="generic"
tmpfile="/tmp/cpwfs.$$"
net_cf="Scripts/network.conf"

###
# Check existance of a home directory
###

if [ -d $CRACK_HOME ]
then
	cd $CRACK_HOME || exit 1
else
	echo "Error: The directory $CRACK_HOME does not exist."
	echo ""
	echo "Please set the value of CRACK_HOME in the Crack script to the CRACK31"
	echo "installation directory."
	echo ""
	echo "The current working directory is" `pwd`
	exit 1
fi

###
# Announce ourselves.
###

echo "Crack 3.2a Password Cracker by ADE Muffett, 1991"

###
# Check that we have arguments
###

if [ "$1" = "" ]
then
	echo "Usage:    $0 [options] [bindir] passwdfile [...]"
	echo "Or:       $0 -network [options] passwdfile [...]"
	echo "With options:-"
	echo "	-v              - to produce verbose output (if configured)"
	echo "	-nnicevalue     - to run niced"
	echo "	-rpointfile     - to recover a crashed-out job"
	echo "	-Rpointfile     - to recover (with verify) a crashed-out job"
	exit 1
fi

###
# Parse command line
###

argl=""

while :
do
	case $1 in
		-network)
			if [ ! -f $net_cf ]
			then
				echo "$0: error: no file $net_cf"
				exit 1
			fi
			shift
			Scripts/Crack.network $*
			exit 0
			;;

		-remote)
			echo "Invoked: $0 $*"
			set remote="TRUE"
			shift
			;;
		-v*|-n*|-r*|-R*)
			argl="$argl $1"
			shift
			;;
		-*)
			echo "Crack: unknown argument $1"
			shift
			;;
		*)
			break
			;;
	esac
done

###
# Test first non-switch argument for existance
###

if [ -f "$1" ]
then
	CRACK_ARCH="$CRACK_HOME/$default_bin"
	if [ ! -d $default_bin ]
	then
		echo "Making default binary directory: $default_bin"
		mkdir $default_bin || exit 1
	fi
elif [ -d "$1" ]
then
	CRACK_ARCH="$CRACK_HOME/$1"
	shift
else
	echo "Crack: error: There is no directory or file $1."
	echo "Crack: warning: Creating directory $1"
	mkdir $1 || exit 1
	CRACK_ARCH="$CRACK_HOME/$1"
	shift
fi

export CRACK_ARCH                       # This is an ENVIRONMENT var.

###
# Double check the dictionary directory
###

if [ ! -d $CRACK_HOME/Dicts ]
then
	echo "Crack: error: There is no directory or file Dicts."
	echo "Crack: warning: Creating directory Dicts"
	mkdir Dicts || exit 1
fi

###
# Now to tidy up a bit
###

make -f Scripts/crack.mf cleanprogs >/dev/null 2>&1

echo "Using binaries in directory: $CRACK_ARCH"

cd $CRACK_ARCH || exit 1

###
# Install makefiles if necessary.
###

if [ ! -f Makefile ]
then
	cp $CRACK_HOME/Scripts/install.mf Makefile
fi

make crack-pwc || exit 1

cd $CRACK_HOME

make -f Scripts/crack.mf dictstamp || exit 1

###
# Check how we have been invoked : ergo how we get our data.
###

if [ "$remote" = "TRUE" ]
then
	cat > $tmpfile
else
	Scripts/joinfiles $* > $tmpfile || exit 1
fi

echo "Backgrounding program. Output will be written to a file in this directory."

nohup $CRACK_ARCH/crack-pwc $argl -i$tmpfile Dicts/dict.* </dev/null >$trash 2>&1 &

sleep 1

test -f nohup.out && rm nohup.out

###
# There are horrible timeraces involved in removing $tmpfile, so I dont.
###
