#!/bin/sh
#
#
#set -x
ACLFILE=/var/etc/lijst.txt
IPT=/sbin/iptables
DEFAULTIP1=192.168.1.1
DEFAULTIP2=192.168.1.2
DEFAULTIP3=192.168.1.3

SetACL()
{
  # flush current list
  $IPT -F

  # allow return traffic of dreambox initiated sessions (keyupdate etc)
  $IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

  # add some default ipnumber, in case acl file contains errors
  $IPT -A INPUT -s 127.0.0.1 -j ACCEPT # add localhost
  $IPT -A INPUT -s $LOCALIP -j ACCEPT # add current ipnr
  $IPT -A INPUT -s $DEFAULTIP1 -j ACCEPT # for testing
  $IPT -A INPUT -s $DEFAULTIP2 -j ACCEPT # for testing
  $IPT -A INPUT -s $DEFAULTIP3 -j ACCEPT # for testing

  # now read acl file and add these ip numbers as well
  while read IPNR
  do
  $IPT -A INPUT -s $IPNR -j ACCEPT
  done < $ACLFILE
  
  # reject everything else
  $IPT -A INPUT -j REJECT
  
  # show user current INPUT chain
  echo "New iptables INPUT chain:"
  $IPT -nL INPUT
} 

GetLocalIP() # get's local ipaddress ($localip)
{
  a=`/sbin/ifconfig eth0 | grep inet | cut -f 2 -d ":"`
  LOCALIP=${a% Bcast}
}

CheckNetpilot()
{
  if [ `ps -ef | grep netpilot | grep -v grep` ]
    then
    $netpilot=ok #dummy actie
  else
    echo "netpilot not running, starting now"
    /var/bin/netpilot 127.0.0.1 666 &
  fi
} 
 
Main()
{ 
  if [ -e $ACLFILE ]
    then
    dos2unix $ACLFILE
    GetLocalIP;
    SetACL;
  else
    echo "File $ACLFILE does not exist!"
    exit 1
  fi
}

Main;
