#!/bin/sh

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

GREP_OPTIONS=""

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

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

ENABLELIST=""
ENABLECMD=0

while test "$#" -gt 0; do
	case "$1" in
	"-h")
		echo "kismet_unmonitor - takes a wireless card out of 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                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
		;;
	*)
		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 "Disabling monitor mode for a cisco card on $DEVICE"
			iwconfig $DEVICE essid ""
			echo "Mode: i" > /proc/driver/aironet/$DEVICE/Config
			echo "XmitPower: 100" > /proc/driver/aironet/$DEVICE/Config
			ifconfig $DEVICE -promisc up
			;;
		"cisco_cvs")
			echo "Disabling monitor mode for a cisco card on $DEVICE"
			DEVICE1=`echo $DEVICE | cut -f 1 -d:`;
			DEVICE2=`echo $DEVICE | cut -f 2 -d:`;
#			DEVICE2=`echo $DEVICE | sed -e 's/wifi/eth/'`
#			echo "Modifying device $DEVICE2"
			iwconfig $DEVICE1 essid ""
			echo "Mode: i" > /proc/driver/aironet/$DEVICE1/Config
			echo "XmitPower: 100" > /proc/driver/aironet/$DEVICE1/Config
			ifconfig $DEVICE1 -promisc up
			ifconfig $DEVICE2 down
			;;
		"cisco_bsd")
			echo "No unmonitor script for cisco_bsd"
			;;
		"prism2_legacy")
			echo "Disabling monitor mode for a prism2 card on $DEVICE"
			ifconfig $DEVICE -promisc up
			wlanctl-ng $DEVICE lnxreq_wlansniff channel=$CHANNEL enable=false
			;;
		"prism2")
			echo "Disabling monitor mode for a pcap prism2 card on $DEVICE"
			ifconfig $DEVICE -promisc up
			wlanctl-ng $DEVICE lnxreq_wlansniff channel=$CHANNEL enable=false
			;;
		"prism2_avs")
			echo "Disabling monitor mode for a pcap prism2 card on $DEVICE"
			ifconfig $DEVICE -promisc up
			wlanctl-ng $DEVICE lnxreq_wlansniff channel=$CHANNEL enable=false
			;;
		"prism2_bsd")
			echo "Disabling monitor mode for a prism2 card on $DEVICE under BSD."
			prism2ctl $DEVICE -h
			;;
		"prism2_hostap")
			echo "Disabling monitor mode for a hostap prism2 card on $DEVICE"
			iwconfig $DEVICE mode managed
			ifconfig $DEVICE -promisc up
			;;
		"orinoco")
			echo "Disabling monitor mode for an orinoco card on $DEVICE"
			iwpriv $DEVICE monitor 0 $CHANNEL
			ifconfig $DEVICE -promisc up
			;;
		"generic")
			echo "Generic card specified, so we do nothing."
			;;
		"drone")
			echo "Drone connection specified, so we do nothing."
			;;
		"wsp100")
			echo "Disabling wsp100 device at $DEVICE"
	        # sensor::running
	        snmpset $DEVICE public .1.3.6.1.4.1.14422.1.1.4 i 0
	        ;;
		"ar5k")
			echo "Disabling monitore mode for an ar5k 802.11a on $DEVICE (setting to managed)"
			iwconfig $DEVICE mode managed
			;;
		*)
			echo "Unknown card type '$CARDTYPE'.  Doing nothing."
			;;
	esac
done
	
echo "You will likely need to restart your PCMCIA services to reconfigure your card"
echo "for the correct channel and SSID."

exit
