Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, March 29, 2024

Diff for scripts/shell/firewall/fw-universal.sh between version 2.50 and 2.52

version 2.50, 2007/08/29 14:43:55 version 2.52, 2008/01/16 23:45:08
Line 9 
Line 9 
 # Licensed under terms of GNU General Public License.  # Licensed under terms of GNU General Public License.
 # All rights reserved.  # All rights reserved.
 #  #
 # $Platon: scripts/shell/firewall/fw-universal.sh,v 2.49 2006-10-04 09:23:25 rajo Exp $  # $Platon: scripts/shell/firewall/fw-universal.sh,v 2.51 2007-12-12 23:30:10 rajo Exp $
 #  #
 # Changelog:  # Changelog:
 # 2003-10-24 - created  # 2003-10-24 - created
 #  #
   
   umask 077 # security
   
 DESC="firewall"  DESC="firewall"
 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
   
 DEFAULT_FIREWALL_CONFIG="${DEFAULT_FIREWALL_CONFIG:=/etc/default/firewall}"  DEFAULT_FIREWALL_CONFIG="${DEFAULT_FIREWALL_CONFIG:=/etc/default/firewall}"
   DEFAULT_FIREWALL_CONFIG_DIR="${DEFAULT_FIREWALL_CONFIG_DIR:=/etc/default/firewall.d}"
 DEFAULT_CACHE_DIR="${DEFAULT_CACHE_DIR:=/var/cache/firewall}"  DEFAULT_CACHE_DIR="${DEFAULT_CACHE_DIR:=/var/cache/firewall}"
   
 if [ -f "$DEFAULT_FIREWALL_CONFIG" ]; then  if [ -f "$DEFAULT_FIREWALL_CONFIG" ]; then
Line 92  get_first_ip_addr()
Line 95  get_first_ip_addr()
         print_first `echo ${!varname} | sort -g`          print_first `echo ${!varname} | sort -g`
 } # }}}  } # }}}
   
   read_config_ips()
   { # {{{
           PARSE_CONFIG=$1 perl -ne 'if (m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(\d+)$/g) { print; } elsif ($_ !~ m/^\s*#/ && $_ !~ m/^\s*$/ ) { print STDERR "ERROR: $ENV{PARSE_CONFIG}:$.: ignored string $_\n"; }' $1
   } # }}}
   
 # load necessary modules from $MODULES variable  # load necessary modules from $MODULES variable
 load_modules()  load_modules()
 { # {{{  { # {{{
Line 111  load_cache()
Line 119  load_cache()
                 mkdir -p "$DEFAULT_CACHE_DIR";                  mkdir -p "$DEFAULT_CACHE_DIR";
         fi          fi
   
         config=`cat $DEFAULT_FIREWALL_CONFIG $0`; # config file and firewalling script          config=`cat $DEFAULT_FIREWALL_CONFIG $0 $DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf `; # config file and firewalling script
         md5key=`echo "config = '$config' parsed_interfaces ='$parsed_interfaces' parsed_routes='$parsed_routes'" | md5sum | $AWK '{ print $1; }'`;          md5key=`echo "config = '$config' parsed_interfaces ='$parsed_interfaces' parsed_routes='$parsed_routes'" | md5sum | $AWK '{ print $1; }'`;
         CACHE_FILE="$DEFAULT_CACHE_DIR/$md5key"          CACHE_FILE="$DEFAULT_CACHE_DIR/$md5key"
   
Line 536  bann_ip_adresses()
Line 544  bann_ip_adresses()
         #IP address is: 146.48.97.11 146.48.97.13          #IP address is: 146.48.97.11 146.48.97.13
         # User Agent: "UbiCrawler/v0.4beta (http://ubi.iit.cnr.it/projects/ubicrawler/)"          # User Agent: "UbiCrawler/v0.4beta (http://ubi.iit.cnr.it/projects/ubicrawler/)"
         #          #
           cf="$DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf"
           if [ -f $cf ]; then
                   BANNED_IP="$BANNED_IP `read_config_ips $cf`"
           fi
         if [ ! -z "$BANNED_IP" ]; then          if [ ! -z "$BANNED_IP" ]; then
                 echo -en "Dropping ALL packets from IP:"                  echo -en "Dropping ALL packets from IP:"
                 for banned_ip in $BANNED_IP; do                  for banned_ip in $BANNED_IP; do
Line 561  allow_accept_all()
Line 573  allow_accept_all()
         fi          fi
 } # }}}  } # }}}
   
 allow_input()  drop_input()
 { # {{{  { # {{{
   
         if [ ! -z "$NAT_LAN_IFACE" ]; then          if [ ! -z "$NAT_LAN_IFACE" ]; then
                 for client_ip in $NAT_CLIENT_DROP; do                  for client_ip in $NAT_CLIENT_DROP; do
                         echo -en " !$client_ip";                          echo -en " !$client_ip";
Line 590  allow_input()
Line 601  allow_input()
                 done                  done
                 echo " done."                  echo " done."
         fi          fi
   } # }}}
   
   reject_input()
   { # {{{
           if [ ! -z "$ALL_REJECT_INPUT_TCP" ]; then
                   echo -en "Reject ALL INPUT TCP connections on ports:"
                   for port in $ALL_REJECT_INPUT_TCP; do
                           for iface in $INTERFACES; do
                                   echo -en " $port($iface)"
                                   $IPTABLES -A INPUT -i $iface -p TCP --dport $port -j REJECT --reject-with icmp-port-unreachable
                           done
                   done
                   echo " done."
           fi
           if [ ! -z "$ALL_REJECT_INPUT_UDP" ]; then
                   echo -en "Reject ALL INPUT UDP connections on ports:"
                   for port in $ALL_REJECT_INPUT_UDP; do
                           for iface in $INTERFACES; do
                                   echo -en " $port($iface)"
                                   $IPTABLES -A INPUT -i $iface -p UDP --dport $port -j REJECT --reject-with icmp-port-unreachable
                           done
                   done
                   echo " done."
           fi
   } # }}}
   
   allow_input()
   { # {{{
   
         if [ ! -z "$ALL_ACCEPT_INPUT_TCP" ]; then          if [ ! -z "$ALL_ACCEPT_INPUT_TCP" ]; then
                 echo -en "Accepting ALL INPUT TCP connections on ports:"                  echo -en "Accepting ALL INPUT TCP connections on ports:"
                 for port in $ALL_ACCEPT_INPUT_TCP; do                  for port in $ALL_ACCEPT_INPUT_TCP; do
Line 637  allow_input()
Line 677  allow_input()
                 redirect_udp="${iface}_REDIRECT_UDP"                  redirect_udp="${iface}_REDIRECT_UDP"
                 REDIRECT_UDP="${!redirect_udp}"                  REDIRECT_UDP="${!redirect_udp}"
   
                   reject_input_tcp="${iface}_REJECT_INPUT_TCP"
                   REJECT_INPUT_TCP="${!reject_input_tcp}"
                   reject_input_udp="${iface}_REJECT_INPUT_UDP"
                   REJECT_INPUT_UDP="${!reject_input_udp}"
   
                 accept_input_tcp="${iface}_ACCEPT_INPUT_TCP"                  accept_input_tcp="${iface}_ACCEPT_INPUT_TCP"
                 ACCEPT_INPUT_TCP="${!accept_input_tcp}"                  ACCEPT_INPUT_TCP="${!accept_input_tcp}"
                 accept_input_udp="${iface}_ACCEPT_INPUT_UDP"                  accept_input_udp="${iface}_ACCEPT_INPUT_UDP"
Line 670  allow_input()
Line 715  allow_input()
                         echo " done."                          echo " done."
                 fi                  fi
   
                   # REJECT {{{
                   if [ ! -z "$REJECT_INPUT_TCP" ]; then
                           echo -en "$iface: Rejecting INPUT TCP connections on ports:"
                           for port in $REJECT_INPUT_TCP; do
                                   src_ip=""
                                   eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                                   echo -en " $port"`[ ! -z $src_ip ] && echo "[$src_ip]"`
                                   for ip in ${!IPS}; do
                                           if [ -z $src_ip ]; then
                                                   $IPTABLES -A INPUT -i $iface -d $ip -p TCP --dport $port -j REJECT --reject-with icmp-port-unreachable
                                           else
                                                   $IPTABLES -A INPUT -i $iface -s $src_ip -d $ip -p TCP --dport $port -j REJECT --reject-with icmp-port-unreachable
                                           fi
                                   done
                           done
                           echo " done."
                   fi
   
                   if [ ! -z "$REJECT_INPUT_UDP" ]; then
                           echo -en "$iface: Rejecting INPUT UDP connections on ports:"
                           for port in $REJECT_INPUT_UDP; do
                                   src_ip=""
                                   eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                                   echo -en " $port"`[ ! -z $src_ip ] && echo "[$src_ip]"`
                                   for ip in ${!IPS}; do
                                           if [ -z $src_ip ]; then
                                                   $IPTABLES -A INPUT -i $iface -d $ip -p UDP --dport $port -j REJECT --reject-with icmp-port-unreachable
                                           else
                                                   $IPTABLES -A INPUT -i $iface -s $src_ip -d $ip -p UDP --dport $port -j REJECT --reject-with icmp-port-unreachable
                                           fi
                                   done
                           done
                           echo " done."
                   fi
                   # }}}
   
                   # ACCEPT {{{
                 if [ ! -z "$ACCEPT_INPUT_TCP" ]; then                  if [ ! -z "$ACCEPT_INPUT_TCP" ]; then
                         echo -en "$iface: Accepting INPUT TCP connections on ports:"                          echo -en "$iface: Accepting INPUT TCP connections on ports:"
                         for port in $ACCEPT_INPUT_TCP; do                          for port in $ACCEPT_INPUT_TCP; do
Line 705  allow_input()
Line 787  allow_input()
                         done                          done
                         echo " done."                          echo " done."
                 fi                  fi
                   # }}}
         done          done
   
         # Enable outgoing TRACEROUTE requests (required e.g. by Skype, http://www.skype.com)          # Enable outgoing TRACEROUTE requests (required e.g. by Skype, http://www.skype.com)
Line 888  accept_loopback()
Line 971  accept_loopback()
   
 } # }}}  } # }}}
   
   add_banned_ip()
   { # {{{
           echo "# `date '+%Y-%m-%d %X' ` - ${SSH_CLIENT:=local}" >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf
           TMPFILE=`mktemp -t fw-universal.sh-XXXXXX` || exit 1
           trap 'rm -f $TMPFILE' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
           if [ -z "$*" ]; then
                   echo "Reading banned IP's from STDIN:"
                   cat >> $TMPFILE
           else
                   for IP in $*; do
                           echo $i >> $TMPFILE;
                   done
           fi
           read_config_ips $TMPFILE >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf
           rm -f $TMPFILE
   } # }}}
   
   deploy_block()
   { # {{{
           if [ -z "$*" ]; then
                   echo "Usage: $0 deploy-block IP1/netmask1 IP2/netmask2 ..."
                   exit 1;
           fi
           echo "Deploying to local rules ..."
           add_banned_ip $*
           # start the some script twice to refresh rules (new blocked IP's)
           $0 start
           TMPFILE=`mktemp -t fw-universal.sh-XXXXXX` || exit 1
           trap 'rm -f $TMPFILE' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
           echo TMPFILE=$TMPFILE
           for i in $*; do
                   echo $i >> $TMPFILE;
           done
           while read conn keyfile
           do
              case "$conn" in
                      ""|\#*)
                              continue
                              ;;
              esac
              echo "Deploying to $conn ...";
              cat $TMPFILE | ssh -i $keyfile $conn $0 block
           done < $DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list
           rm -f $TMPFILE
   } # }}}
   
 # Parse output from ifconfig: - tested on Linux and FreeBSD  # Parse output from ifconfig: - tested on Linux and FreeBSD
 # http://platon.sk/cvs/cvs.php/scripts/shell/firewall/ifconfig-parse.sh  # http://platon.sk/cvs/cvs.php/scripts/shell/firewall/ifconfig-parse.sh
 parse_ifconfig()  parse_ifconfig()
Line 1044  case "$1" in
Line 1173  case "$1" in
                 accept_related                  accept_related
                 log_new_connections                  log_new_connections
                 drop_output                  drop_output
                   drop_input
                   reject_input
                 allow_input                  allow_input
                 allow_output                  allow_output
                 allow_icmp                  allow_icmp
Line 1083  case "$1" in
Line 1214  case "$1" in
         purge)          purge)
                 find $DEFAULT_CACHE_DIR -type f -ls -exec rm -f {} \;                  find $DEFAULT_CACHE_DIR -type f -ls -exec rm -f {} \;
                 ;;                  ;;
           block)
                   shift;
                   add_banned_ip $*;
                   # start the some script twice to refresh rules (new blocked IP's)
                   $0 start;
                   ;;
           deploy-block)
                   shift;
                   deploy_block $*;
                   ;;
         *)          *)
                 echo "Usage: $0 {start|stop|really-off|status|purge}" >&2                  echo "Usage: $0 {start|stop|really-off|status|purge|block|deploy-block}" >&2
                 exit 1                  exit 1
                 ;;                  ;;
 esac  esac

Legend:
Removed from v.2.50  
changed lines
  Added in v.2.52

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top