#-----------------------------------------------------------------------------
# /etc/rc.d/rc900 - infobox configuration
#
# Creation:     03.06.2006  
# Last Update:  11.06.2006
#-----------------------------------------------------------------------------

case $OPT_INFOBOX in
yes)
    case "$OPT_HTTPD" in 
    yes)
        begin_script INFOBOX "configuring OPT_INFOBOX ..."
        httpd-menu.sh add infobox.cgi Infobox


	BASEDIR="/srv/www/admin"
	
#----------------------------------------- savedir

		mkdir -p $INFOBOX_SAVEDIR

#----------------------------------------- dhcp
		DHCPTYPE="NONE"
		if [ "$OPT_DHCP" = "yes" ]
		then
			DHCPTYPE="DHCP"
			LEASEFILE="$DHCP_LEASES_DIR/dhcpd.leases"
		fi
		if [ "$OPT_DNSDHCP" = "yes" ]
		then
			DHCPTYPE="DNSDHCP"
			LEASEFILE="$DNSDHCP_LEASES_DIR/dnsmasq.leases"
		fi
#----------------------------------------- bootlog
		if [ "$DEBUG_STARTUP" = "yes" ] &&  [ $DEBUG_LOG != "" ]
		then
			BOOTLOG="$DEBUG_LOG"
		else
			BOOTLOG="no"
		fi
#----------------------------------------- syslog
		if [ "$OPT_SYSLOGD" = "yes" ]
		then
			SYSLOG="yes"
		else
			SYSLOG="no"
		fi
#----------------------------------------- Imond
		if [ "$START_IMOND" = "yes" ]
		then
			IMOND="$IMOND_LOGDIR/imond.log"
		else
			IMOND="no" 
		fi
#----------------------------------------- write
	{
		echo "BASEDIR=\"$BASEDIR\""
		echo "SAVEDIR=\"$INFOBOX_SAVEDIR\""
		echo "HOSTNAME=\"$HOSTNAME\""
		echo "DHCPTYPE=\"$DHCPTYPE\""
		echo "LEASEFILE=\"$LEASEFILE\""
		echo "BOOTLOG=\"$BOOTLOG\""
		echo "IMOND=\"$IMOND\""
		echo "SYSLOG=\"$SYSLOG\""
		echo "INFOBOX_PS=\"$INFOBOX_PS\""
		echo "INFOBOX_FILES=\"$INFOBOX_FILES\""
		echo "INFOBOX_HOSTS=\"$INFOBOX_HOSTS\""
		echo "INFOBOX_INPUT=\"$INFOBOX_INPUT\""
		echo "INFOBOX_FWD=\"$INFOBOX_FWD\""
		echo "INFOBOX_PORTFWD=\"no\""

	} >/var/run/infobox.conf
	
	        if [ "$INFOBOX_HOSTS_N" -gt 0 ]
			then
            idx=1
            [ 0$INFOBOX_HOSTS_N -eq 0 ] || for idx in `seq 1 $INFOBOX_HOSTS_N`
            do
                eval ip='$INFOBOX_HOSTS_'$idx'_BLOCK'
				sh /usr/local/bin/infobox_hosts.sh block $ip
            done
			fi
			
#---------------------------------------------------------- import INPUT

	if [ -f "$INFOBOX_SAVEDIR/input.conf" ]
	then
		iptables -nL INPUT --line-numbers |
		while read line
		do
			set -- $line
			num=$1; target=$2; prot=$3; source=$5; dest=$6;
			echo "$num" > /tmp/count.tmp
		done

		set +f
        idx=`cat /tmp/count.tmp`			#get end of table
        idxn=`expr $idx`
		rm /tmp/count.tmp
		
		while read line
		do
			set -- $line
			string="$2 $3 $4 $5 $6 $7 $8 $9 $10";				#cut line number	
			iptables -I INPUT $idxn $string -j ACCEPT
			echo $idxn $string >> /tmp/input.conf.tmp			#put string to tmp file whith line number
			idxn=`expr $idxn + 1`

		done < $INFOBOX_SAVEDIR/input.conf
		cp /tmp/input.conf.tmp $INFOBOX_SAVEDIR/input.conf
		rm /tmp/input.conf.tmp
		
	fi
	
#---------------------------------------------------------- import FORWARD

	if [ -f "$INFOBOX_SAVEDIR/forward.conf" ]
	then
		iptables -nL FORWARD --line-numbers |
		while read line
		do
			set -- $line
			num=$1; target=$2; prot=$3; source=$5; dest=$6;
			echo "$num" > /tmp/count.tmp
		done

		set +f
        idx=`cat /tmp/count.tmp`			#get end of table
        idxn=`expr $idx - 1`
		rm /tmp/count.tmp
		
		while read line
		do
			set -- $line
			string="$2 $3 $4 $5 $6 $7 $8 $9 $10";				#cut line number	
			iptables -I FORWARD $idxn $string -j DROP
			echo $idxn $string >> /tmp/forward.conf.tmp			#put string to tmp file whith line number
			idxn=`expr $idxn + 1`

		done < $INFOBOX_SAVEDIR/forward.conf
		cp /tmp/forward.conf.tmp $INFOBOX_SAVEDIR/forward.conf
		rm /tmp/forward.conf.tmp
		
	fi

    end_script

	;;
    esac
;;
esac