#!/bin/sh

prefix=/usr/local
ETC=${prefix}/etc



CHANNEL=6
CARDTYPE=""
CONFIGFILE="$ETC/kismet.conf"
DEVICE=""
HOPPER="no"
HOPOPT=""


# Trinux modifications

CARDTYPE=`cat /etc/tux/config/kismet/cardtype` 2> /dev/null
DEVICE=`cat /etc/tux/config/kismet/interface` 2> /dev/null


while test "$#" -gt 0; do
	case "$1" in
	"-h")
		echo "kismet_monitor - places a wireless card into rf-mon mode (if possible)"
		echo "Most of these options SHOULD be set in your kismet.conf file since kismet"
		echo "won't work correctly if they aren't, but they can be overriden here."
		echo "Usage:"
		echo "  -f <file>         Use alternate Kismet config file <file>"
		echo "  -i <dev>          Use interface <dev>"
		echo "  -t <type>         Use card type <type>"
		echo "  -c <channel>      Activate on channel <channel> (default 6)"
		echo "  -H                Spawn kismet_hopper"
		echo "  -O <options>      Extra options to pass to the hopper"
		echo "  -h                What you're reading now"
		exit
		;;
	"-f")
		shift
		if test -f "$1"; then
			CONFIGFILE=$1
		else
			echo "Unable to open specified config file '$1'"
			exit
		fi
		;;
	"-i")
		shift
		if test "$1" = ""; then
			echo "No capture interface specified."
			exit
		else
			DEVICE=$1
		fi
		;;
	"-t")
		shift
		if test "$1" = ""; then
			echo "No card type specified."
			exit
		else
			CARDTYPE=$1
		fi
		;;
	"-c")
		shift
		if test "$1" = ""; then
			echo "No channel specified."
			exit
		else
			CHANNEL=$1
		fi
		;;
	"-H")
		HOPPER="yes"
		echo "Will launch kismet_hopper"
		;;
	"-O")
		shift
		if test "$1" = ""; then
			echo "No hopper options specified."
			exit
		else
			HOPOPT=$1
		fi
		;;
	*)
		echo "Unknown option $1"
		exit
		;;
	esac
shift
done

if test "$CONFIGFILE" = ""; then
	CONFIGFILE=$ETC/kismet.conf
fi

if test ! -f "$CONFIGFILE"; then
	echo "Could not find '$CONFIGFILE'.  Please make sure Kismet is"
	echo "installed correctly, or specify a configfile with -f."
	exit
fi

if test "$DEVICE" = ""; then
	echo "Setting interface from $CONFIGFILE capinterface"
	DEVICE=`grep -e "^\ *capinterface\ *=" $CONFIGFILE | cut -d= -f2 | tr -d [:blank:]`
fi

if test "$DEVICE" = ""; then
	echo "Could not determine what interface to use."
	echo "Please make sure your 'capinterface=' in '$CONFIGFILE' is correct"
	echo "or specify the device with -d."
	exit
fi

if test "$CARDTYPE" = ""; then
	echo "Setting card type from $CONFIGFILE cardtype"
	CARDTYPE=`grep -i "^\ *cardtype\ *=" $CONFIGFILE | cut -d= -f2 | tr -d [:blank:]`
fi

if test "$CARDTYPE" = ""; then
	echo "Could not determine what type of card to enable monitoring for."
	echo "Please make sure your 'card=' in '$CONFIGFILE' is correct"
	echo "or specify the card type with -t."
fi

case "$CARDTYPE" in
	"cisco")
		echo "Enabling monitor mode for a cisco card on $DEVICE"
		iwconfig $DEVICE essid off
		echo "Mode: r" > /proc/driver/aironet/$DEVICE/Config
		echo "Mode: y" > /proc/driver/aironet/$DEVICE/Config
		echo "XmitPower: 1" > /proc/driver/aironet/$DEVICE/Config
		ifconfig $DEVICE promisc up
		if test "$HOPPER" = "yes"; then
			echo "Cisco cards use an internal channel hopper, disabling kismet_hopper."
			HOPPER="no"
		fi
		;;
	"cisco_cvs")
		echo "Enabling monitor mode for a cisco card on $DEVICE"
		DEVICE2=`echo $DEVICE | sed -e 's/wifi/eth/'`
		iwconfig $DEVICE essid off
		echo "Modifying device $DEVICE2"
		echo "Mode: r" > /proc/driver/aironet/$DEVICE2/Config
		echo "Mode: y" > /proc/driver/aironet/$DEVICE2/Config
		echo "XmitPower: 1" > /proc/driver/aironet/$DEVICE2/Config
		ifconfig $DEVICE promisc up
		if test "$HOPPER" = "yes"; then
			echo "Cisco cards use an internal channel hopper, disabling kismet_hopper."
			HOPPER="no"
		fi
		;;
	"cisco_bsd")
		echo "Enabling monitor mode for a cisco card on $DEVICE"
		ancontrol -i $DEVICE -o 1
		ancontrol -i $DEVICE -p 1
		ancontrol -i $DEVICE -M 7
		if test "$HOPPER" = "yes"; then
			echo "Cisco cards use an internal channel hopper, disabling kismet_hopper."
			HOPPER="no"
		fi
		;;
	"prism2")
		echo "Enabling monitor mode for a prism2 card on $DEVICE channel $CHANNEL"
		ifconfig $DEVICE promisc up
		wlanctl-ng $DEVICE lnxreq_wlansniff channel=$CHANNEL enable=true
		;;
	"prism2_pcap")
		echo "Enabling monitor mode for a pcap prism2 card on $DEVICE channel $CHANNEL"
		ifconfig $DEVICE promisc up
		wlanctl-ng $DEVICE lnxreq_wlansniff channel=$CHANNEL enable=true prismheader=true
		;;
	"prism2_bsd")
		echo "Enabling monitor mode for a prism2 card on $DEVICE channel $CHANNEL using prism2ctl"
		prism2ctl $DEVICE -m 
		prism2ctl $DEVICE -f $CHANNEL
		;;
	"prism2_hostap")
		echo "Enabling monitor mode for a hostap prism2 card on $DEVICE channel $CHANNEL"
		iwpriv $DEVICE monitor 3
		iwconfig $DEVICE channel $CHANNEL
		ifconfig $DEVICE promisc up
		;;
	"orinoco")
		echo "Enabling monitor mode for an orinoco card on $DEVICE channel $CHANNEL"
		iwpriv $DEVICE monitor 1 $CHANNEL
		ifconfig $DEVICE promisc up
		;;
	"orinoco_bsd")
		echo "orinoco BSD - we need commands here!"
		;;
	"generic")
		echo "Generic card specified, so we do nothing."
		exit
		;;
	*)
		echo "Unknown card type '$CARDTYPE'.  Doing nothing."
		exit	
		;;
esac

if test "$HOPPER" = "yes"; then
	echo "Launching kismet_hopper in the background."
	kismet_hopper -i $DEVICE -f $CONFIGFILE -t $CARDTYPE $HOPOPT &
fi

exit
