#!/bin/sh 
#
# CGI which process the request of the user _after_ he/she has pressed continue
#
# Rick van der Zwet <info@rickvanderzwet.nl>
#
PATH=$PATH:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
DB=/tmp/authenticated.txt

REMOTE_MAC=`arp -n $REMOTE_ADDR | awk '{print $4}'`

# Make sure database exists
touch $DB

# Place to store which MAC was authenticated against which IP address
grep -v $REMOTE_MAC $DB >> $DB.new
mv $DB.new $DB
echo `date "+%s"` $REMOTE_ADDR $REMOTE_MAC >> $DB

# Add entry to authorized list
sudo pfctl -q -t wlportal -T add $REMOTE_ADDR

# Redirect to proper page
echo 'Content-Type: text/html'
echo 'Cache-Control: no-cache, no-store, must-revalidate'
echo 'Pragma: no-cache'
echo 'Expires: 0'
echo 'Refresh: 2; url=http://www.wirelessleiden.nl'
echo ''
sed -e "s/\$REMOTE_ADDR/$REMOTE_ADDR/" \
    -e "s/\$REMOTE_MAC/$REMOTE_MAC/" \
    `dirname $0`/index.html.tmpl