#!/bin/sh
#
#
#
 LOCALHOST=yes
 PINGS=yes
 TRACEROUTE=yes
 OUTGOING=yes
 TCP="21 22 23 80 137 138 139 443 445 513 631 1010"
 UDP="21 69 123 137 138 139 445 5353"
 NFS=no
 NFSPORTS="111 1110 2049 4045 4046"
 PASSIVE=yes
 RANGE=1026:65535
 BRF=yes
 BLACKLIST=/etc/firewall/black.list
 DDOS=no
 dos2unix /etc/firewall/config 2>/dev/null
 . /etc/firewall/config
 dos2unix $BLACKLIST 2>/dev/null
 rm -fr /usr/share/server/brf
#
#
#
 case "$1" in
 'accept')
 iptables -P INPUT ACCEPT
 iptables -P FORWARD ACCEPT
 iptables -P OUTPUT ACCEPT
 ;;
#
#
#
 'blacklist')
 if [ -e $BLACKLIST ]; then
   BLACKLIST=$(cat $BLACKLIST)
   for i in $BLACKLIST; do
     iptables -I INPUT -s $i -j DROP
   done
 fi
 ;;
#
#
#
 'bruteforce')
 if [ $BRF = yes ]; then
   . /etc/firewall/brf/brf.conf
   iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name BRF
   iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds $Y --hitcount $X --rttl --name BRF -j DROP
 fi
 ;;
#
#
#
 'ddos')
 iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
 iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
 iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
 if [ $DDOS = yes ]; then
   . /etc/firewall/ddos/ddos.conf
   iptables -I INPUT -p tcp -m connlimit --connlimit-above $N --connlimit-mask 32 -j REJECT --reject-with tcp-reset
 fi
 ;;
#
#
#
 'drop')
 iptables -P INPUT DROP
 iptables -P FORWARD DROP
 if [ $OUTGOING = yes ]; then
   iptables -P OUTPUT ACCEPT
   else
   iptables -P OUTPUT DROP
 fi
 ;;
#
#
#
 'flush')
 iptables --flush
 iptables --delete-chain
 ;;
#
#
#
 'help')
 echo "firewall start --> start 4MLinux Firewall"
 echo "firewall stop --> stop 4MLinux Firewall"
 echo "firewall restart --> restart 4MLinux Firewall"
 echo "firewall test --> test 4MLinux Firewall"
 ;;
#
#
#
 'icmp')
 if [ $PINGS = yes ]; then
   iptables -A INPUT -p ICMP --icmp-type 8 -j ACCEPT
 fi
 if [ $TRACEROUTE = yes ]; then
   iptables -A INPUT -p ICMP --icmp-type 11 -j ACCEPT
 fi
 ;;
#
#
#
 'localhost')
 if [ $LOCALHOST = yes ]; then
   iptables -A INPUT -i lo -j ACCEPT
   iptables -A OUTPUT -o lo -j ACCEPT
 fi
 ;;
#
#
#
 'restart')
 firewall stop
 sleep 0.5
 firewall start
 ;;
#
#
#
 'start')
 for i in flush drop bruteforce ddos localhost icmp tcp udp blacklist; do
   firewall $i
 done
 modprobe -r ip_conntrack_ftp 2>/dev/null
 if [ $PASSIVE = yes ]; then
   modprobe ip_conntrack_ftp
 fi
 touch /tmp/.firewall
 ;;
#
#
#
 'stop')
 firewall flush
 firewall accept
 rm /tmp/.firewall 2>/dev/null
 ;;
#
#
#
 'tcp')
 iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
 if [ $NFS = yes ]; then
   TCP="$TCP $NFSPORTS"
 fi
 for i in $TCP; do
   iptables -A INPUT -p tcp --dport $i -j ACCEPT
 done
 if [ $PASSIVE = yes ]; then
   iptables -A INPUT -p tcp --match multiport --dports $RANGE -j ACCEPT
 fi
 ;;
#
#
#
 'test')
 if [ ! -e /tmp/.firewall ]; then
   echo "Your firewall seems to be down."
   read -p "Do you wish to start it now (y/n)?: " CHOICE
   CHOICE=y$CHOICE
   if [ $CHOICE = yy ]; then
     firewall start
   fi
 fi
 clear
 echo ""
 iptables -L
 ;;
#
#
#
 'udp')
 if [ $NFS = yes ]; then
   UDP="$UDP $NFSPORTS"
 fi
 for i in $UDP; do
   iptables -A INPUT -p udp --dport $i -j ACCEPT
 done
 ;;
#
#
#
 *)
 echo "Usage:"
 firewall help
 ;;
 esac