#!/bin/sh

prefix=/
ETC=${prefix}/etc/kismet

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

# Tempfile of interfaces
IFFILE=`mktemp /tmp/kismonitor.XXXXXX`
USEFILE=0

ENABLELIST=""
ENABLECMD=0

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 "  -s <source>       Use alternate capture source (engine,interface,type,name)"
		echo "  -C <enable>       Use an alternate list of sources to enable"
		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
		;;
	"-s")
		shift
		if test "$1" = ""; then
			echo "No capture interface specified."
			exit
		else
			echo "$1" >> $IFFILE
			USEFILE=1
		fi
		;;
	"-C")
		shift
		if test "$1" = ""; then
			echo "No enable list specified."
			exit
		else
			ENABLELIST="$1"
			ENABLECMD=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 "$USEFILE" -eq 0; then
	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

	echo "Using $CONFIGFILE sources..."
	`grep -e "^\ *source\ *=" $CONFIGFILE | cut -d= -f2 | tr -d " \t" > $IFFILE`

	if test "$ENABLECMD" -eq 0; then
		ENABLELIST=`grep -e "^\ *enablesources\ *=" $CONFIGFILE | cut -d= -f2 | tr -d " \t"`
	fi
fi

# Pad the enable list
if test "$ENABLELIST" != ""; then
	ENABLELIST=","$ENABLELIST","
fi

for line in `cat $IFFILE`; do 
	CARDTYPE=`echo $line | cut -f 1 -d,`
	DEVICE=`echo $line | cut -f 2 -d,`
	NAME=`echo $line | cut -f 3 -d,`

	if test "$ENABLELIST" != ""; then
		if test "`echo $ENABLELIST | grep \",\"$NAME\",\"`" == ""; then
			continue;
		fi
	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"
			prism2_param $DEVICE monitor_type 1
			iwconfig $DEVICE mode monitor
			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
			;;
		"wsp100")
			echo "Enabling a wsp100 at $DEVICE for channel $CHANNEL"
			if test "$HOSTIP" == ""; then
				HOSTIP=`hostname -i`
				echo "'hostname -i' thinks our IP is $HOSTIP.  Set HOSTIP manually if this is wrong."
				echo "     ie, HOSTIP=1.2.3.4 kismet_monitor"
			fi
	
			WSPDEVICE=`echo $DEVICE | cut -f 1 -d:`;
			WSPPORT=`echo $DEVICE | cut -f 2 -d:`;
	
			# sensor::loghostaddress
			snmpset -c public $WSPDEVICE .1.3.6.1.4.1.14422.1.1.5 a $HOSTIP
			# sensor::channel
			snmpset -c public $WSPDEVICE .1.3.6.1.4.1.14422.1.3.1 i $CHANNEL
			# sensor::serverport
			snmpset -c public $WSPDEVICE .1.3.6.1.4.1.14422.1.4.1 i $WSPPORT
			# sensor::running
			snmpset -c public $WSPDEVICE .1.3.6.1.4.1.14422.1.1.4 i 1
			;;
		*)
			echo "Unknown card type '$CARDTYPE'.  Doing nothing."
			exit	
			;;
	esac
done

if test "$HOPPER" = "yes"; then
	echo "Launching kismet_hopper in the background."

	if test "$usefile" -eq 1; then
		for x in `cat $IFFILE`; do
			srcs="$srcs -s $x"
		done
	fi

	kismet_hopper $srcs $HOPOPT &
fi

rm $IFFILE

exit
