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

Diff for scripts/shell/firewall/fw-universal.sh between version 2.91 and 2.119

version 2.91, 2012/02/14 22:52:12 version 2.119, 2019/02/14 07:41:47
Line 4 
Line 4 
 # Provides:          firewall  # Provides:          firewall
 # Required-Start:    $network  # Required-Start:    $network
 # Required-Stop:     $remote_fs  # Required-Stop:     $remote_fs
 # Default-Start:     S  # Default-Start:     2 3 4 5
 # Default-Stop:      0 6  # Default-Stop:      0 6
 # Short-Description: Starts firewall  # Short-Description: Starts firewall
 # Description:       Handle universal firewall script by Platon Group  # Description:       Handle universal firewall script by Platon Group
 #                    http://platon.sk/cvs/cvs.php/scripts/shell/firewall/  #                    http://platon.sk/cvs/cvs.php/scripts/shell/firewall/
 # Author:            Lubomir Host <rajo@platon.sk>  # Author:            Lubomir Host <rajo@platon.sk>
 # Copyright:         (c) 2003-2011 Platon Group  # Copyright:         (c) 2003-2018 Platon Group
 ### END INIT INFO  ### END INIT INFO
   
 #  #
Line 18 
Line 18 
 # Can be started by init or by hand.  # Can be started by init or by hand.
 #  #
 # Developed by Lubomir Host 'rajo' <rajo AT platon.sk>  # Developed by Lubomir Host 'rajo' <rajo AT platon.sk>
 # Copyright (c) 2003-2011 Platon Group, http://platon.sk/  # Copyright (c) 2003-2018 Platon Group, http://platon.sk/
 # 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.90 2012-02-11 19:38:51 rajo Exp $  # $Platon: scripts/shell/firewall/fw-universal.sh,v 2.118 2018/12/10 11:46:12 nepto Exp $
 #  #
 # Changelog:  # Changelog:
 # 2003-10-24 - created  # 2003-10-24 - created
 # 2011-07-20 - implemented XEN_MODE  # 2011-07-20 - implemented XEN_MODE
   # 2018-03-01 - fixed Default-Start for SystemD on Stretch (nepto)
 #  #
   
   
Line 39  DEFAULT_FIREWALL_CONFIG="${DEFAULT_FIREW
Line 40  DEFAULT_FIREWALL_CONFIG="${DEFAULT_FIREW
 DEFAULT_FIREWALL_CONFIG_DIR="${DEFAULT_FIREWALL_CONFIG_DIR:=/etc/default/firewall.d}"  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}"
   
   DIST_FIREWALL_CONFIG_DIR="${DIST_FIREWALL_CONFIG_DIR:=/etc/firewall/firewall.d}"
   
 # quiet output? {{{  # quiet output? {{{
 if [ "x$1" = "xblock" ] || [ "x$QUIET" = "xyes" ]; then  if [ "x$QUIET" = "xyes" ]; then
         print_info()          print_info()
         {          {
                 echo -n ""                  echo -n ""
Line 53  else
Line 56  else
 fi  fi
 # }}}  # }}}
   
   # Define function which can be used in config file
   # Usage:
   #   load_subnets eth0_ACCEPT_INPUT_TCP Slovakia.txt 22
   load_subnets()
   { # {{{
           cfgvar="$1";
           cfgfile="$2";
           port="$3";
   
           print_info "LOAD_SUBNETS: $*";
   
           if [ -f "$DEFAULT_FIREWALL_CONFIG_DIR/subnets/$cfgfile" ]; then
                   cfgfound="$DEFAULT_FIREWALL_CONFIG_DIR/subnets/$cfgfile";
           else if [ -f "$DIST_FIREWALL_CONFIG_DIR/subnets/$cfgfile" ]; then
                   cfgfound="$DIST_FIREWALL_CONFIG_DIR/subnets/$cfgfile";
           else
                   print_info "LOAD_SUBNETS: config file not found: $cfgfile";
                   return 1
           fi fi
           LOADED_CONFIG_FILES="$LOADED_CONFIG_FILES $cfgfound";
   
           print_info "LOAD_SUBNETS: found $cfgfile: $cfgfound";
           print_info "LOAD_SUBNETS: mapping $cfgfile to $cfgvar, port $port"
   
           lines=0;
           while read subnet ; do
                   case "$subnet" in
                           ""|\#*)
                                   continue
                                   ;;
                   esac
                   eval "$cfgvar=\"\$$cfgvar $subnet:$port\"";
                   lines=$(($lines + 1));
           done < $cfgfound
           print_info "LOAD_SUBNETS: $lines subnets loaded from $cfgfile"
   } # }}}
   
 if [ -f "$DEFAULT_FIREWALL_CONFIG" ]; then  if [ -f "$DEFAULT_FIREWALL_CONFIG" ]; then
         print_info "Reading config file $DEFAULT_FIREWALL_CONFIG"          print_info "Reading config file $DEFAULT_FIREWALL_CONFIG"
         . $DEFAULT_FIREWALL_CONFIG          . $DEFAULT_FIREWALL_CONFIG
Line 177  load_cache()
Line 217  load_cache()
   
         config="";          config="";
         if [ -r "$DEFAULT_FIREWALL_CONFIG" ]; then          if [ -r "$DEFAULT_FIREWALL_CONFIG" ]; then
                 config="$config ` cat \"$DEFAULT_FIREWALL_CONFIG\" `";                  config="$config ` md5sum \"$DEFAULT_FIREWALL_CONFIG\" `";
         fi          fi
         if [ -r "$0" ]; then          if [ -r "$0" ]; then
                 config="$config ` cat \"$0\" `";                  config="$config ` md5sum \"$0\" `";
         fi          fi
         if [ -r "$DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list" ]; then          if [ -r "$DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list" ]; then
                 config="$config ` cat \"$DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list\" `";                  config="$config ` md5sum \"$DEFAULT_FIREWALL_CONFIG_DIR/deploy-servers.list\" `";
         fi          fi
         if [ -r "$DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf" ]; then          if [ -r "$DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf" ]; then
                 config="$config ` cat \"$DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf\" `";                  config="$config ` md5sum \"$DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf\" `";
         fi          fi
           for lc in $LOADED_CONFIG_FILES; do
                   echo "CHECKSUM $lc";
                   config="$config ` md5sum \"$lc\" `";
           done
         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 350  invalid_packet_filter()
Line 394  invalid_packet_filter()
                         print_info -ne " XEN_MODE ";                          print_info -ne " XEN_MODE ";
                         continue;                          continue;
                 fi                  fi
                 $IPTABLES_LOG   -A $chain -m state --state INVALID $LOG_LIMIT "INVALID $chain: "                  $IPTABLES_LOG   -A $chain -m conntrack --ctstate INVALID $LOG_LIMIT "INVALID $chain: "
                 print_info -en "."                  print_info -en "."
                 $IPTABLES               -A $chain -m state --state INVALID -j DROP                  $IPTABLES               -A $chain -m conntrack --ctstate INVALID -j DROP
                 print_info -en "."                  print_info -en "."
         done          done
   
Line 370  syn_flood()
Line 414  syn_flood()
                 $IPTABLES -A INPUT -i $riface -p TCP --syn -j syn-flood                  $IPTABLES -A INPUT -i $riface -p TCP --syn -j syn-flood
   
                 # packet is marked az NEW, but doesn't have SYN flag - drop it                  # packet is marked az NEW, but doesn't have SYN flag - drop it
                 $IPTABLES -A INPUT -i $riface -p TCP ! --syn -m state --state NEW -j DROP                  $IPTABLES -A INPUT -i $riface -p TCP ! --syn -m conntrack --ctstate NEW -j DROP
         done          done
   
   
Line 461  mangle_output()
Line 505  mangle_output()
   
 masquerade()  masquerade()
 { # {{{  { # {{{
           if [ -z "$NAT_LAN_IFACE" ]; then
                   return;
           fi
   
           print_info -en "NAT: Masquerading local subnet: $NAT_SUBNET_IFACE --> $NAT_LAN_IFACE"
   
         if [ "X$XEN_MODE" = "Xon" ]; then          if [ "X$XEN_MODE" = "Xon" ]; then
                 print_info "XEN_MODE enabled: masquerade is not supported in this mode";                  if [ -n "$NAT_SUBNET_SRC" ]; then
                           NAT_SUBNET_SRC="-s $NAT_SUBNET_SRC";
                   fi
                   $IPTABLES -t nat -A POSTROUTING -o $NAT_LAN_IFACE -j MASQUERADE $NAT_SUBNET_SRC
                   print_info " done."
                   print_info "XEN_MODE enabled: masquerade is limited to basic functionality only";
                 return;                  return;
         fi          fi
         if [ ! -z "$NAT_LAN_IFACE" ]; then  
         print_info -en "NAT: Enabling packet forwarding..."  
         echo 1 > /proc/sys/net/ipv4/ip_forward  
         print_info " done."  
                 print_info -en "NAT: Masquerading local subnet: $NAT_SUBNET_IFACE --> $NAT_LAN_IFACE"  
   
                 ip="`get_first_ip_addr IP_$NAT_SUBNET_IFACE`"  
                 netmask="Mask_$NAT_SUBNET_IFACE"  
                 localnet="$ip/${!netmask}"  
   
                 lan_ip="`get_first_ip_addr IP_$NAT_LAN_IFACE`"          ip="`get_first_ip_addr IP_$NAT_SUBNET_IFACE`"
           netmask="Mask_$NAT_SUBNET_IFACE"
                 # alow packets from private subnet          localnet="$ip/${!netmask}"
                 $IPTABLES -A FORWARD -s ! $localnet -i $NAT_SUBNET_IFACE -j DROP  
                 for client_ip in $NAT_CLIENT_DROP; do  
                         print_info -en " !$client_ip";  
                         $IPTABLES -A FORWARD -s $client_ip -i $NAT_SUBNET_IFACE -j DROP  
                 done  
   
                 for redirect in $NAT_TCP_PORT_REDIRECT; do          lan_ip="`get_first_ip_addr IP_$NAT_LAN_IFACE`"
                         #eval `echo $redirect | $AWK -v FS=: '{ printf "remote_port=%s; local_port=%s;", $1, $2; }'`  
                         eval `echo $redirect | \          # alow packets from private subnet
                                 $AWK -v FS=: '  (NF == 2) { remote_ip = "$lan_ip"; remote_port = $1; local_port = $2; } \          $IPTABLES -A FORWARD -s ! $localnet -i $NAT_SUBNET_IFACE -j DROP
                                                                 (NF == 3) { remote_ip = $2;        remote_port = $1; local_port = $3; } \          for client_ip in $NAT_CLIENT_DROP; do
                                                                 END { printf "remote_ip=%s; remote_port=%s; local_port=%s;", remote_ip, remote_port, local_port; }'`                  print_info -en " !$client_ip";
                         print_info -en " $remote_port>>$remote_ip:$local_port(tcp)"                  $IPTABLES -A FORWARD -s $client_ip -i $NAT_SUBNET_IFACE -j DROP
                         $IPTABLES -t nat -A PREROUTING -p TCP \          done
   
           for redirect in $NAT_TCP_PORT_REDIRECT; do
                   #eval `echo $redirect | $AWK -v FS=: '{ printf "remote_port=%s; local_port=%s;", $1, $2; }'`
                   eval `echo $redirect | \
                           $AWK -v FS=: '  (NF == 2) { remote_ip = "$lan_ip"; remote_port = $1; local_port = $2; } \
                                                           (NF == 3) { remote_ip = $2;        remote_port = $1; local_port = $3; } \
                                                           END { printf "remote_ip=%s; remote_port=%s; local_port=%s;", remote_ip, remote_port, local_port; }'`
                   print_info -en " $remote_port>>$remote_ip:$local_port(tcp)"
                   $IPTABLES -t nat -A PREROUTING -p TCP \
                           -i $NAT_SUBNET_IFACE \
                           --dport $remote_port -j REDIRECT --to-port $local_port
           done
           for redirect in $NAT_UDP_PORT_REDIRECT; do
                   #eval `echo $redirect | $AWK -v FS=: '{ printf "remote_port=%s; local_port=%s;", $1, $2; }'`
                   eval `echo $redirect | \
                           $AWK -v FS=: '  (NF == 2) { dnat = "no"  ; remote_ip = "X"; remote_port = $1; local_port = $2; } \
                                                           (NF == 3) { dnat = "yes" ; remote_ip = $2;  remote_port = $1; local_port = $3; } \
                                                           END { printf "dnat=%s; remote_ip=%s; remote_port=%s; local_port=%s;", dnat, remote_ip, remote_port, local_port; }'`
                   print_info -en " $remote_port>>$remote_ip:$local_port(udp)"
                   if [ "x$dnat" = "xyes" ]; then
                           $IPTABLES -t nat -A PREROUTING -p UDP -i $NAT_SUBNET_IFACE -d ! $ip \
                           --dport $local_port -j DNAT --to $remote_ip:$remote_port
                           $IPTABLES -A FORWARD -p UDP -i $NAT_SUBNET_IFACE -d ! $ip --dport $local_port -j ACCEPT
                   else
                           $IPTABLES -t nat -A PREROUTING -p UDP \
                                 -i ! $NAT_LAN_IFACE -d ! $lan_ip \                                  -i ! $NAT_LAN_IFACE -d ! $lan_ip \
                                 --dport $remote_port -j REDIRECT --to-port $local_port                                  --dport $remote_port -j REDIRECT --to-port $local_port
                 done                  fi
                 for redirect in $NAT_UDP_PORT_REDIRECT; do          done
                         #eval `echo $redirect | $AWK -v FS=: '{ printf "remote_port=%s; local_port=%s;", $1, $2; }'`  
                         eval `echo $redirect | \  
                                 $AWK -v FS=: '  (NF == 2) { dnat = "no"  ; remote_ip = "X"; remote_port = $1; local_port = $2; } \  
                                                                 (NF == 3) { dnat = "yes" ; remote_ip = $2;  remote_port = $1; local_port = $3; } \  
                                                                 END { printf "dnat=%s; remote_ip=%s; remote_port=%s; local_port=%s;", dnat, remote_ip, remote_port, local_port; }'`  
                         print_info -en " $remote_port>>$remote_ip:$local_port(udp)"  
                         if [ "x$dnat" = "xyes" ]; then  
                                 $IPTABLES -t nat -A PREROUTING -p UDP -i $NAT_SUBNET_IFACE -d ! $ip \  
                                 --dport $local_port -j DNAT --to $remote_ip:$remote_port  
                                 $IPTABLES -A FORWARD -p UDP -i $NAT_SUBNET_IFACE -d ! $ip --dport $local_port -j ACCEPT  
                         else  
                                 $IPTABLES -t nat -A PREROUTING -p UDP \  
                                         -i ! $NAT_LAN_IFACE -d ! $lan_ip \  
                                         --dport $remote_port -j REDIRECT --to-port $local_port  
                         fi  
                 done  
   
                 #$IPTABLES -t nat -A POSTROUTING -s $localnet -o $NAT_LAN_IFACE -j MASQUERADE  
                 $IPTABLES -t nat -A POSTROUTING -o $NAT_LAN_IFACE -j MASQUERADE  
   
                 print_info " done."  
   
                 # don't forward Miscrosoft protocols - NOT RFC compliant packets          if [ -n "$NAT_SUBNET_SRC" ]; then
                 if [ ! -z "$NAT_FORWARD_MICROSOFT" ]; then                  NAT_SUBNET_SRC="-s $NAT_SUBNET_SRC";
                         if [ "x$NAT_FORWARD_MICROSOFT" = "xno" ]; then          fi
                                 $IPTABLES -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP          $IPTABLES -t nat -A POSTROUTING -o $NAT_LAN_IFACE -j MASQUERADE $NAT_SUBNET_SRC
   
                                 for port in 67 68 69 135 445 1434 6667; do          print_info " done."
                                         $IPTABLES -A FORWARD -p TCP --dport $port -j DROP  
                                         $IPTABLES -A FORWARD -p UDP --dport $port -j DROP  
                                 done  
                         fi  
                 fi  
   
                 if [ ! -z "$NAT_FORWARD_TCP_PORTS" ]; then          # don't forward Miscrosoft protocols - NOT RFC compliant packets
                         print_info -en "\tAccepting FORWARD TCP ports:"          if [ ! -z "$NAT_FORWARD_MICROSOFT" ]; then
                         for port in $NAT_FORWARD_TCP_PORTS; do                  if [ "x$NAT_FORWARD_MICROSOFT" = "xno" ]; then
                                 print_info -en " $port"                          $IPTABLES -A FORWARD -p TCP ! --syn -m conntrack --ctstate NEW -j DROP
                                 $IPTABLES -A FORWARD -p TCP --dport $port -m state --state NEW -j ACCEPT  
                         done  
                         print_info " done."  
                 fi  
   
                 if [ ! -z "$NAT_FORWARD_UDP_PORTS" ]; then                          for port in 67 68 69 135 445 1434 6667; do
                         print_info -en "\tAccepting FORWARD UDP ports:"                                  $IPTABLES -A FORWARD -p TCP --dport $port -j DROP
                         for port in $NAT_FORWARD_UDP_PORTS; do                                  $IPTABLES -A FORWARD -p UDP --dport $port -j DROP
                                 print_info -en " $port"  
                                 $IPTABLES -A FORWARD -p UDP --dport $port -m state --state NEW -j ACCEPT  
                         done                          done
                         print_info " done."  
                 fi                  fi
           fi
   
                 # NAT_FORWARD_TCP_HOSTS {{{          if [ ! -z "$NAT_FORWARD_TCP_PORTS" ]; then
                 if [ ! -z "$NAT_FORWARD_TCP_HOSTS" ]; then                  print_info -en "\tAccepting FORWARD TCP ports:"
                         print_info -en "\tAccepting FORWARD TCP hosts:"                  for port in $NAT_FORWARD_TCP_PORTS; do
                         for host in $NAT_FORWARD_TCP_HOSTS; do                          print_info -en " $port"
                                 print_info -en " $host"                          $IPTABLES -A FORWARD -p TCP --dport $port -m conntrack --ctstate NEW -j ACCEPT
                                 $IPTABLES -A FORWARD -p TCP -d $host -m state --state NEW -j ACCEPT                  done
                         done                  print_info " done."
                         print_info " done."          fi
                 fi  
                 # }}}  
   
                 # NAT_FORWARD_UDP_HOSTS {{{          if [ ! -z "$NAT_FORWARD_UDP_PORTS" ]; then
                 if [ ! -z "$NAT_FORWARD_UDP_HOSTS" ]; then                  print_info -en "\tAccepting FORWARD UDP ports:"
                         print_info -en "\tAccepting FORWARD UDP hosts:"                  for port in $NAT_FORWARD_UDP_PORTS; do
                         for host in $NAT_FORWARD_UDP_HOSTS; do                          print_info -en " $port"
                                 print_info -en " $host"                          $IPTABLES -A FORWARD -p UDP --dport $port -m conntrack --ctstate NEW -j ACCEPT
                                 $IPTABLES -A FORWARD -p UDP -d $host -m state --state NEW -j ACCEPT                  done
                         done                  print_info " done."
                         print_info " done."          fi
                 fi  
                 # }}}  
   
                 # NAT_FORWARD_TCP_CLIENTS {{{          # NAT_FORWARD_TCP_HOSTS {{{
                 if [ ! -z "$NAT_FORWARD_TCP_CLIENTS" ]; then          if [ ! -z "$NAT_FORWARD_TCP_HOSTS" ]; then
                         print_info -en "\tAccepting FORWARD TCP clients:"                  print_info -en "\tAccepting FORWARD TCP hosts:"
                         for client in $NAT_FORWARD_TCP_CLIENTS; do                  for host in $NAT_FORWARD_TCP_HOSTS; do
                                 print_info -en " $client"                          print_info -en " $host"
                                 $IPTABLES -A FORWARD -p TCP -s $client -m state --state NEW -j ACCEPT                          $IPTABLES -A FORWARD -p TCP -d $host -m conntrack --ctstate NEW -j ACCEPT
                         done                  done
                         print_info " done."                  print_info " done."
                 fi          fi
                 # }}}          # }}}
   
                 # NAT_FORWARD_UDP_CLIENTS {{{          # NAT_FORWARD_UDP_HOSTS {{{
                 if [ ! -z "$NAT_FORWARD_UDP_CLIENTS" ]; then          if [ ! -z "$NAT_FORWARD_UDP_HOSTS" ]; then
                         print_info -en "\tAccepting FORWARD UDP clients:"                  print_info -en "\tAccepting FORWARD UDP hosts:"
                         for client in $NAT_FORWARD_UDP_CLIENTS; do                  for host in $NAT_FORWARD_UDP_HOSTS; do
                                 print_info -en " $client"                          print_info -en " $host"
                                 $IPTABLES -A FORWARD -p UDP -s $client -m state --state NEW -j ACCEPT                          $IPTABLES -A FORWARD -p UDP -d $host -m conntrack --ctstate NEW -j ACCEPT
                         done                  done
                         print_info " done."                  print_info " done."
                 fi          fi
                 # }}}          # }}}
   
                 print_info -en "\tAccepting ICMP packets:"          # NAT_FORWARD_TCP_CLIENTS {{{
                 for type in $ACCEPT_ICMP_PACKETS; do          if [ ! -z "$NAT_FORWARD_TCP_CLIENTS" ]; then
                         print_info -en " $type"                  print_info -en "\tAccepting FORWARD TCP clients:"
                         $IPTABLES -A FORWARD -p ICMP --icmp-type $type -j ACCEPT                  for client in $NAT_FORWARD_TCP_CLIENTS; do
                 done                          print_info -en " $client"
                 #$IPTABLES_LOG -A FORWARD -p ICMP -j LOG --log-prefix "FWD ICMP: "                          $IPTABLES -A FORWARD -p TCP -s $client -m conntrack --ctstate NEW -j ACCEPT
                   done
                 print_info " done."                  print_info " done."
           fi
           # }}}
   
                 # Port forwarding to local machines          # NAT_FORWARD_UDP_CLIENTS {{{
                 if [ ! -z "$NAT_TCP_PORT_FORWARD" ]; then          if [ ! -z "$NAT_FORWARD_UDP_CLIENTS" ]; then
                         print_info -en "\tForwarding TCP ports to local machines:"                  print_info -en "\tAccepting FORWARD UDP clients:"
                         for redirect in $NAT_TCP_PORT_FORWARD; do                  for client in $NAT_FORWARD_UDP_CLIENTS; do
                                 #eval `echo $redirect | $AWK -v FS=: '{ printf "src_port=%s; local_machine=%s; dest_port=%s;", $1, $2, $3; }'`                          print_info -en " $client"
                                 eval `echo $redirect | \                          $IPTABLES -A FORWARD -p UDP -s $client -m conntrack --ctstate NEW -j ACCEPT
                                         $AWK -v FS=: '  (NF == 3) { src_ip = "$lan_ip" ; src_port = $1; local_machine = $2; dest_port = $3; } \                  done
                                                                         (NF == 4) { src_ip = $1 ; src_port = $2; local_machine = $3; dest_port = $4; } \                  print_info " done."
                                                                         END { printf "src_ip=%s; src_port=%s; local_machine=%s; dest_port=%s;", src_ip, src_port, local_machine, dest_port; }'`          fi
                                 print_info -en " $src_ip:$src_port -> $local_machine:$dest_port"          # }}}
                                 $IPTABLES -t nat -A PREROUTING -p TCP -i $NAT_LAN_IFACE -d $src_ip \  
                                 --dport $src_port -j DNAT --to $local_machine:$dest_port  
                                 $IPTABLES -A FORWARD -p TCP -i $NAT_LAN_IFACE -d $local_machine --dport $dest_port -j ACCEPT  
                         done  
                         print_info " done."  
                 fi  
                 if [ ! -z "$NAT_UDP_PORT_FORWARD" ]; then  
                         print_info -en "\tForwarding UDP ports to local machines:"  
                         for redirect in $NAT_UDP_PORT_FORWARD; do  
                                 #eval `echo $redirect | $AWK -v FS=: '{ printf "src_port=%s; local_machine=%s; dest_port=%s;", $1, $2, $3; }'`  
                                 eval `echo $redirect | \  
                                         $AWK -v FS=: '  (NF == 3) { src_ip = "$lan_ip" ; src_port = $1; local_machine = $2; dest_port = $3; } \  
                                                                         (NF == 4) { src_ip = $1 ; src_port = $2; local_machine = $3; dest_port = $4; } \  
                                                                         END { printf "src_ip=%s; src_port=%s; local_machine=%s; dest_port=%s;", src_ip, src_port, local_machine, dest_port; }'`  
                                 print_info -en " $src_port -> $local_machine:$dest_port"  
                                 $IPTABLES -t nat -A PREROUTING -p UDP -i $NAT_LAN_IFACE -d $lan_ip \  
                                 --dport $src_port -j DNAT --to $local_machine:$dest_port  
                                 $IPTABLES -A FORWARD -p UDP -i $NAT_LAN_IFACE -d $local_machine --dport $dest_port -j ACCEPT  
                         done  
                         print_info " done."  
                 fi  
   
                 # Keep state of connections from private subnets          print_info -en "\tAccepting ICMP packets:"
                 $IPTABLES -A OUTPUT  -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT          for type in $ACCEPT_ICMP_PACKETS; do
                 #$IPTABLES -A FORWARD -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT                  print_info -en " $type"
                 $IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT                  $IPTABLES -A FORWARD -p ICMP --icmp-type $type -j ACCEPT
           done
                 # hide NAT clients behind firewall: - set TTL          #$IPTABLES_LOG -A FORWARD -p ICMP -j LOG --log-prefix "FWD ICMP: "
                 # XXX: warning: this breaks traceroute !!!          print_info " done."
                 if [ ! "a$NAT_SET_TTL" = "ano" ]; then  
                         print_info "NAT: clients hidden behind firewall - setting TTL to $NAT_SET_TTL"  
                         $IPTABLES -t mangle -A POSTROUTING -o $NAT_LAN_IFACE -j TTL --ttl-set $NAT_SET_TTL  
                 fi  
   
           # Port forwarding to local machines
           if [ ! -z "$NAT_TCP_PORT_FORWARD" ]; then
                   print_info -en "\tForwarding TCP ports to local machines:"
                   for redirect in $NAT_TCP_PORT_FORWARD; do
                           #eval `echo $redirect | $AWK -v FS=: '{ printf "src_port=%s; local_machine=%s; dest_port=%s;", $1, $2, $3; }'`
                           eval `echo $redirect | \
                                   $AWK -v FS=: '  (NF == 3) { src_ip = "$lan_ip" ; src_port = $1; local_machine = $2; dest_port = $3; } \
                                                                   (NF == 4) { src_ip = $1 ; src_port = $2; local_machine = $3; dest_port = $4; } \
                                                                   END { printf "src_ip=%s; src_port=%s; local_machine=%s; dest_port=%s;", src_ip, src_port, local_machine, dest_port; }'`
                           print_info -en " $src_ip:$src_port -> $local_machine:$dest_port"
                           $IPTABLES -t nat -A PREROUTING -p TCP -i $NAT_LAN_IFACE -d $src_ip \
                           --dport $src_port -j DNAT --to $local_machine:$dest_port
                           $IPTABLES -A FORWARD -p TCP -i $NAT_LAN_IFACE -d $local_machine --dport $dest_port -j ACCEPT
                   done
                   print_info " done."
           fi
           if [ ! -z "$NAT_UDP_PORT_FORWARD" ]; then
                   print_info -en "\tForwarding UDP ports to local machines:"
                   for redirect in $NAT_UDP_PORT_FORWARD; do
                           #eval `echo $redirect | $AWK -v FS=: '{ printf "src_port=%s; local_machine=%s; dest_port=%s;", $1, $2, $3; }'`
                           eval `echo $redirect | \
                                   $AWK -v FS=: '  (NF == 3) { src_ip = "$lan_ip" ; src_port = $1; local_machine = $2; dest_port = $3; } \
                                                                   (NF == 4) { src_ip = $1 ; src_port = $2; local_machine = $3; dest_port = $4; } \
                                                                   END { printf "src_ip=%s; src_port=%s; local_machine=%s; dest_port=%s;", src_ip, src_port, local_machine, dest_port; }'`
                           print_info -en " $src_port -> $local_machine:$dest_port"
                           $IPTABLES -t nat -A PREROUTING -p UDP -i $NAT_LAN_IFACE -d $lan_ip \
                           --dport $src_port -j DNAT --to $local_machine:$dest_port
                           $IPTABLES -A FORWARD -p UDP -i $NAT_LAN_IFACE -d $local_machine --dport $dest_port -j ACCEPT
                   done
                   print_info " done."
           fi
   
           # Keep state of connections from private subnets
           $IPTABLES -A OUTPUT  -m conntrack --ctstate NEW -o $NAT_LAN_IFACE -j ACCEPT
           #$IPTABLES -A FORWARD -m conntrack --ctstate NEW -o $NAT_LAN_IFACE -j ACCEPT
           $IPTABLES -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
   
           # hide NAT clients behind firewall: - set TTL
           # XXX: warning: this breaks traceroute !!!
           if [ ! "a$NAT_SET_TTL" = "ano" ]; then
                   print_info "NAT: clients hidden behind firewall - setting TTL to $NAT_SET_TTL"
                   $IPTABLES -t mangle -A POSTROUTING -o $NAT_LAN_IFACE -j TTL --ttl-set $NAT_SET_TTL
         fi          fi
 } # }}}  } # }}}
   
Line 657  log_new_connections()
Line 706  log_new_connections()
                         fi                          fi
                         print_info -en "Logging new connections $NAT_LOG_NEW_CONNECTIONS:"                          print_info -en "Logging new connections $NAT_LOG_NEW_CONNECTIONS:"
                         for proto in $NAT_LOG_NEW_CONNECTIONS; do                          for proto in $NAT_LOG_NEW_CONNECTIONS; do
                                 $IPTABLES_LOG -A INPUT   -m state --state NEW -p $proto -j LOG --log-prefix "IN  connection: "                                  $IPTABLES_LOG -A INPUT   -m conntrack --ctstate NEW -p $proto -j LOG --log-prefix "IN  connection: "
                                 $IPTABLES_LOG -A OUTPUT  -m state --state NEW -p $proto -j LOG --log-prefix "OUT connection: "                                  $IPTABLES_LOG -A OUTPUT  -m conntrack --ctstate NEW -p $proto -j LOG --log-prefix "OUT connection: "
                                 $IPTABLES_LOG -A FORWARD -m state --state NEW -p $proto -j LOG --log-prefix "FWD connection: "                                  $IPTABLES_LOG -A FORWARD -m conntrack --ctstate NEW -p $proto -j LOG --log-prefix "FWD connection: "
                         done                          done
                         print_info " done."                          print_info " done."
                 fi                  fi
Line 705  drop_output()
Line 754  drop_output()
                         print_info " done."                          print_info " done."
                 fi                  fi
         done          done
   } # }}}
   
   do_ban_single_ip()
   { # {{{
           if [ -z "$1" ]; then
                   print_info "do_ban_single_ip(): empty banned_ip";
                   return;
           fi
           for banned_ip in $*; do
                   # This does immediate connection termination, but it must be inserted
                   # and thus not appended into chain, otherwise connection will still
                   # remain alive. Former forward chain rule was removed as unneccessary.
                   #   -- Nepto [2018-08-23]
                   #   -- Plantroon [2018-12-10]
                   $IPTABLES -I INPUT -s $banned_ip -j DROP;
           done
 } # }}}  } # }}}
   
 bann_ip_adresses()  bann_ip_adresses()
Line 727  bann_ip_adresses()
Line 791  bann_ip_adresses()
         if [ ! -z "$BANNED_IP" ]; then          if [ ! -z "$BANNED_IP" ]; then
                 print_info -en "Dropping ALL packets from IP:"                  print_info -en "Dropping ALL packets from IP:"
                 for banned_ip in $BANNED_IP; do                  for banned_ip in $BANNED_IP; do
                         print_info -en " $banned_ip"                          print_info -en " $banned_ip";
                         $IPTABLES -A INPUT              -s $banned_ip -j DROP                          do_ban_single_ip "$banned_ip";
   
                         if [ "X$XEN_MODE" = "Xon" ]; then  
                                         print_info -ne " XEN_MODE ";  
                         else  
                                 $IPTABLES -A FORWARD    -s $banned_ip -j DROP  
                         fi  
                 done                  done
                 print_info " done."                  print_info " done."
         fi          fi
Line 758  allow_accept_all()
Line 816  allow_accept_all()
         fi          fi
 } # }}}  } # }}}
   
   allow_accept_vrrp()
   { # {{{
           if [ ! -z "$IFACE_ACCEPT_VRRP" ]; then
                   print_info -en "Accepting VRRP packets on interfaces:"
                   for iface in $IFACE_ACCEPT_VRRP; do
                           print_info -en " $iface"
                           $IPTABLES -A INPUT  -i $iface -d 224.0.0.18/32 -p vrrp -j ACCEPT;
                           $IPTABLES -A OUTPUT -i $iface -d 224.0.0.18/32 -p vrrp -j ACCEPT;
                   done
                   print_info " done."
           fi
   } # }}}
   
 drop_input()  drop_input()
 { # {{{  { # {{{
         if [ ! -z "$NAT_LAN_IFACE" ]; then          if [ ! -z "$NAT_LAN_IFACE" ]; then
Line 905  reject_input()
Line 976  reject_input()
 allow_input()  allow_input()
 { # {{{  { # {{{
         if [ ! -z "$ALL_ACCEPT_INPUT_TCP" ]; then          if [ ! -z "$ALL_ACCEPT_INPUT_TCP" ]; then
                 print_info -en "Accepting ALL INPUT TCP connections on ports:"                  print_info -e "Accepting ALL INPUT TCP connections on ports:"
                 for port in $ALL_ACCEPT_INPUT_TCP; do                  for port in $ALL_ACCEPT_INPUT_TCP; do
                         src_ip=""                          src_ip=""
                         eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`                          eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
Line 918  allow_input()
Line 989  allow_input()
                         fi                          fi
                         for iface in $INTERFACES; do                          for iface in $INTERFACES; do
                                 riface="IFname_$iface";                                  riface="IFname_$iface";
                                   if [ "$last_port" != "$port" -a -n "$src_ip" ]; then counter=0; print_info ""; fi
                                 print_info -en " $port($iface)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`                                  print_info -en " $port($iface)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`
                                   if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;
                                   last_port="$port";
                                 IPS="IP_$iface";                                  IPS="IP_$iface";
                                 for ip in ${!IPS}; do                                  for ip in ${!IPS}; do
                                         if [ -z "$src_ip" ]; then                                          if [ -z "$src_ip" ]; then
Line 932  allow_input()
Line 1006  allow_input()
                 print_info " done."                  print_info " done."
         fi          fi
         if [ ! -z "$ALL_ACCEPT_INPUT_UDP" ]; then          if [ ! -z "$ALL_ACCEPT_INPUT_UDP" ]; then
                 print_info -en "Accepting ALL INPUT UDP connections on ports:"                  print_info -e "Accepting ALL INPUT UDP connections on ports:"
                 for port in $ALL_ACCEPT_INPUT_UDP; do                  for port in $ALL_ACCEPT_INPUT_UDP; do
                         src_ip=""                          src_ip=""
                         eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`                          eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
Line 945  allow_input()
Line 1019  allow_input()
                         fi                          fi
                         for iface in $INTERFACES; do                          for iface in $INTERFACES; do
                                 riface="IFname_$iface";                                  riface="IFname_$iface";
                                   if [ "$last_port" != "$port" -a -n "$src_ip" ]; then counter=0; print_info ""; fi
                                 print_info -en " $port($iface)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`                                  print_info -en " $port($iface)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`
                                   if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;
                                   last_port="$port";
                                 IPS="IP_$iface";                                  IPS="IP_$iface";
                                 if [ "x$port" = "x67" ]; then # DHCP requests doesn't have destination IP specified                                  if [ "x$port" = "x67" ]; then # DHCP requests doesn't have destination IP specified
                                         $IPTABLES -A INPUT -i ${!riface} -p UDP --dport $port -j ACCEPT                                          $IPTABLES -A INPUT -i ${!riface} -p UDP --dport $port -j ACCEPT
Line 964  allow_input()
Line 1041  allow_input()
         fi          fi
   
         if [ ! -z "$REAL_ACCEPT_INPUT_TCP" ]; then          if [ ! -z "$REAL_ACCEPT_INPUT_TCP" ]; then
                 print_info -en "Accepting REAL all INPUT TCP connections for ALL interfaces on ports:"                  print_info -e "Accepting REAL all INPUT TCP connections for ALL interfaces on ports:"
                 for port in $REAL_ACCEPT_INPUT_TCP; do                  for port in $REAL_ACCEPT_INPUT_TCP; do
                         src_ip=""                          src_ip=""
                         eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`                          eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                           if [ "$last_port" != "$port" -a -n "$src_ip" ]; then counter=0; print_info ""; fi
                         print_info -en " $port(ALL)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`                          print_info -en " $port(ALL)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`
                           if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;
                           last_port="$port";
                         echo $port | grep -q ,                          echo $port | grep -q ,
                         multiport="$?";                          multiport="$?";
                         if [ "$multiport" -eq 0 ]; then                          if [ "$multiport" -eq 0 ]; then
Line 985  allow_input()
Line 1065  allow_input()
                 print_info " done."                  print_info " done."
         fi          fi
         if [ ! -z "$REAL_ACCEPT_INPUT_UDP" ]; then          if [ ! -z "$REAL_ACCEPT_INPUT_UDP" ]; then
                 print_info -en "Accepting REAL all INPUT UDP connections for ALL interfaces on ports:"                  print_info -e "Accepting REAL all INPUT UDP connections for ALL interfaces on ports:"
                 for port in $REAL_ACCEPT_INPUT_UDP; do                  for port in $REAL_ACCEPT_INPUT_UDP; do
                         src_ip=""                          src_ip=""
                         eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`                          eval `echo $port | awk -v FS=: '/:/ { printf "src_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                           if [ "$last_port" != "$port" -a -n "$src_ip" ]; then counter=0; print_info ""; fi
                         print_info -en " $port(ALL)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`                          print_info -en " $port(ALL)"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`
                           if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;
                           last_port="$port";
                         echo $port | grep -q ,                          echo $port | grep -q ,
                         multiport="$?";                          multiport="$?";
                         if [ "$multiport" -eq 0 ]; then                          if [ "$multiport" -eq 0 ]; then
Line 1006  allow_input()
Line 1089  allow_input()
                 print_info " done."                  print_info " done."
         fi          fi
   
         for iface in $INTERFACES; do          # We are using INTERFACES + lo instead of INTERFACES here, because we want
           # to do redirects for "lo" interface as well. However for "lo" it is done
           # quite differently. See http://ix.sk/0WY2j for more information on this.
           #   -- Nepto [2015-10-19]
           for iface in lo $INTERFACES; do
                 riface="IFname_$iface";                  riface="IFname_$iface";
                 IPS="IP_$iface";                  IPS="IP_$iface";
   
Line 1034  allow_input()
Line 1121  allow_input()
                                                                         (NF == 3) { remote_ip = $1;          from_port = $2; to_port = $3; } \                                                                          (NF == 3) { remote_ip = $1;          from_port = $2; to_port = $3; } \
                                                                         END { printf "remote_ip=%s; from_port=%s; to_port=%s;", remote_ip, from_port, to_port; }'`                                                                          END { printf "remote_ip=%s; from_port=%s; to_port=%s;", remote_ip, from_port, to_port; }'`
                                 print_info -en " $remote_ip:$from_port->$to_port"                                  print_info -en " $remote_ip:$from_port->$to_port"
                                 $IPTABLES -t nat -A PREROUTING -p TCP -i ${!riface} -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port                                  if [ "X$iface" = "Xlo" ]; then
                                           $IPTABLES -t nat -A OUTPUT -p TCP -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port
                                   else
                                           $IPTABLES -t nat -A PREROUTING -p TCP -i ${!riface} -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port
                                   fi
                         done                          done
                         print_info " done."                          print_info " done."
                 fi                  fi
Line 1048  allow_input()
Line 1139  allow_input()
                                                                         (NF == 3) { remote_ip = $1;          from_port = $2; to_port = $3; } \                                                                          (NF == 3) { remote_ip = $1;          from_port = $2; to_port = $3; } \
                                                                         END { printf "remote_ip=%s; from_port=%s; to_port=%s;", remote_ip, from_port, to_port; }'`                                                                          END { printf "remote_ip=%s; from_port=%s; to_port=%s;", remote_ip, from_port, to_port; }'`
                                 print_info -en " $remote_ip:$from_port->$to_port"                                  print_info -en " $remote_ip:$from_port->$to_port"
                                 $IPTABLES -t nat -A PREROUTING -p UDP -i ${!riface} -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port                                  if [ "X$iface" = "Xlo" ]; then
                                           $IPTABLES -t nat -A OUTPUT -p UDP -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port
                                   else
                                           $IPTABLES -t nat -A PREROUTING -p UDP -i ${!riface} -s $remote_ip -d $ip --dport $from_port -j REDIRECT --to-port $to_port
                                   fi
                         done                          done
                         print_info " done."                          print_info " done."
                 fi                  fi
Line 1105  allow_input()
Line 1200  allow_input()
   
                 # ACCEPT {{{                  # ACCEPT {{{
                 if [ ! -z "$ACCEPT_INPUT_TCP" ]; then                  if [ ! -z "$ACCEPT_INPUT_TCP" ]; then
                         print_info -en "$iface: Accepting INPUT TCP connections on ports:"                          print_info -e "$iface: Accepting INPUT TCP connections on ports:"
                         counter=0;                          counter=0;
                         for port in $ACCEPT_INPUT_TCP; do                          for port in $ACCEPT_INPUT_TCP; do
                                 src_ip=""                                  src_ip=""
Line 1113  allow_input()
Line 1208  allow_input()
                                 if [ -n "$src_ip" -a "$port" = "0" ]; then                                  if [ -n "$src_ip" -a "$port" = "0" ]; then
                                         port="ALL";                                          port="ALL";
                                 fi                                  fi
                                   if [ "$last_port" != "$port" -a -n "$src_ip" ]; then counter=0; print_info ""; fi
                                 print_info -en " $port"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`                                  print_info -en " $port"`[ ! -z "$src_ip" ] && echo "[$src_ip]"`
                                 if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;                                  if [ $(( ++counter )) -ge 5 ]; then counter=0; print_info ""; fi;
                                   last_port="$port";
                                 echo $port | grep -q ,                                  echo $port | grep -q ,
                                 multiport="$?";                                  multiport="$?";
                                 if [ "$multiport" -eq 0 ]; then                                  if [ "$multiport" -eq 0 ]; then
Line 1199  allow_input()
Line 1296  allow_input()
   
 } # }}}  } # }}}
   
 # ACCEPT all packets from our IP address  # ACCEPT selected IPs/ports if defined for interface
   # if not defined ACCEPT all packets from our IP addresses
 allow_output()  allow_output()
 { # {{{  { # {{{
           output_tcp_str="";
           output_udp_str="";
           output_icmp_str="";
   
         # Povolíme odchozí pakety, které mají naše IP adresy  
         print_info -en "Accepting OUTPUT packets from"  
         for iface in $INTERFACES; do          for iface in $INTERFACES; do
                   gateway="Gateway_$iface";
                 riface="IFname_$iface";                  riface="IFname_$iface";
                 IPS="IP_$iface";                  IPS="IP_$iface";
                 for ip in ${!IPS}; do  
                         print_info -en " $ip($iface)"  
                         $IPTABLES -A OUTPUT -o ${!riface} -s $ip -j ACCEPT  
                 done  
         done;  
         print_info " done.";  
   
                   accept_output_tcp="${iface}_ACCEPT_OUTPUT_TCP"
                   ACCEPT_OUTPUT_TCP="${!accept_output_tcp}"
                   accept_output_udp="${iface}_ACCEPT_OUTPUT_UDP"
                   ACCEPT_OUTPUT_UDP="${!accept_output_udp}"
   
                   # UDP *must* go before TCP
                   #
                   # Reason: we need to have working DNS resolving, which works over
                   # port 53/UDP. Resolving is required for those rules, which use
                   # hostname instead of IP address, for example cvs.platon.sk:2401.
   
                   # UDP
                   if [ -z "$ACCEPT_OUTPUT_UDP" ]; then
                           if [ -n "${!gateway}" ]; then
                                   for ip in ${!IPS}; do
                                           output_udp_str="$output_udp_str $ip:${!riface}:${!gateway}";
                                           $IPTABLES -A OUTPUT -p UDP -o ${!riface} -s $ip -j ACCEPT
                                   done
                           fi
                   else
                           print_info -en "$iface: Accepting OUTPUT UDP connections to ports:"
                           for port in $ACCEPT_OUTPUT_UDP; do
                                   dest_ip=""
                                   eval `echo $port | awk -v FS=: '/:/ { printf "dest_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                                   if [ -n "$dest_ip" -a "$port" = "0" ]; then
                                           port="ALL";
                                   fi
                                   print_info -en " $port"`[ ! -z "$dest_ip" ] && echo "[$dest_ip]"`
                                   if [ -z "$dest_ip" ]; then
                                           $IPTABLES -A OUTPUT -o ${!riface} -p UDP --dport $port -j ACCEPT
                                   else
                                           if [ "$port" = "ALL" ]; then
                                                   $IPTABLES -A OUTPUT -o ${!riface} -d $dest_ip -p UDP -j ACCEPT
                                           else
                                                   $IPTABLES -A OUTPUT -o ${!riface} -d $dest_ip -p UDP --dport $port -j ACCEPT
                                           fi
                                   fi
                           done
                           print_info " done."
                   fi
   
                   # TCP
                   if [ -z "$ACCEPT_OUTPUT_TCP" ]; then
                           if [ -n "${!gateway}" ]; then
                                   for ip in ${!IPS}; do
                                           output_tcp_str="$output_tcp_str $ip:${!riface}:${!gateway}";
                                           $IPTABLES -A OUTPUT -p TCP -o ${!riface} -s $ip -j ACCEPT
                                   done
                           fi
                   else
                           print_info -en "$iface: Accepting OUTPUT TCP connections to ports:"
                           for port in $ACCEPT_OUTPUT_TCP; do
                                   dest_ip=""
                                   eval `echo $port | awk -v FS=: '/:/ { printf "dest_ip=\"%s\"; port=\"%s\";", $1, $2; }'`
                                   if [ -n "$dest_ip" -a "$port" = "0" ]; then
                                           port="ALL";
                                   fi
                                   print_info -en " $port"`[ ! -z "$dest_ip" ] && echo "[$dest_ip]"`
                                   if [ -z "$dest_ip" ]; then
                                           $IPTABLES -A OUTPUT -o ${!riface} -p TCP --dport $port -j ACCEPT
                                   else
                                           if [ "$port" = "ALL" ]; then
                                                   $IPTABLES -A OUTPUT -o ${!riface} -d $dest_ip -p TCP -j ACCEPT
                                           else
                                                   $IPTABLES -A OUTPUT -o ${!riface} -d $dest_ip -p TCP --dport $port -j ACCEPT
                                           fi
                                   fi
                           done
                           print_info " done."
                   fi
   
                   # ICMP
                   if [ -n "${!gateway}" ]; then
                           for ip in ${!IPS}; do
                                   output_icmp_str="$output_icmp_str $ip:${!riface}:${!gateway}";
                                   $IPTABLES -A OUTPUT -p ICMP -o ${!riface} -s $ip -j ACCEPT
                           done
                   fi
           done
   
           if [ -n "$output_tcp_str" ]; then
                   print_info "Accepting OUTPUT TCP packets through $output_tcp_str done."
           fi
           if [ -n "$output_udp_str" ]; then
                   print_info "Accepting OUTPUT UDP packets through $output_udp_str done."
           fi
           if [ -n "$output_icmp_str" ]; then
                   print_info "Accepting OUTPUT ICMP packets through $output_icmp_str done."
           fi
 } # }}}  } # }}}
   
 allow_icmp()  allow_icmp()
Line 1368  accept_related()
Line 1551  accept_related()
 { # {{{  { # {{{
   
         print_info -en "Accepting ESTABLISHED, RELATED packets ..."          print_info -en "Accepting ESTABLISHED, RELATED packets ..."
         $IPTABLES -A INPUT      -m state --state ESTABLISHED,RELATED -j ACCEPT          $IPTABLES -A INPUT      -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
         $IPTABLES -A OUTPUT     -m state --state ESTABLISHED,RELATED -j ACCEPT          $IPTABLES -A OUTPUT     -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
         print_info " done."          print_info " done."
   
 } # }}}  } # }}}
Line 1455  shaping_status()
Line 1638  shaping_status()
   
 # }}}  # }}}
   
   check_banned_ip()
   { # {{{
           output="`grep \"$1\" $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf`";
           if [ "$?" -eq 0 -a -n "$output" ]; then
                   return 0;
           fi
           return 1;
   } # }}}
   
 add_banned_ip()  add_banned_ip()
 { # {{{  { # {{{
         echo "# `date '+%Y-%m-%d %X' `" >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf          echo "# `date '+%F %T'`" >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf
         TMPFILE=`mktemp -t fw-universal.sh-XXXXXX` || exit 1          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          trap 'rm -f $TMPFILE' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
         if [ -z "$*" ]; then          if [ -z "$*" ]; then
Line 1470  add_banned_ip()
Line 1662  add_banned_ip()
         fi          fi
         read_config_ips $TMPFILE >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf          read_config_ips $TMPFILE >> $DEFAULT_FIREWALL_CONFIG_DIR/BANNED_IP.conf
         rm -f $TMPFILE          rm -f $TMPFILE
         # start with new firewalling rules  
         $0 start  
 } # }}}  } # }}}
   
 deploy_block()  deploy_block()
Line 1572  while (my $line = <STDIN>) {
Line 1762  while (my $line = <STDIN>) {
                 $iface  = $1;                  $iface  = $1;
                 my $iface_hwaddr = $2;                  my $iface_hwaddr = $2;
                 my $x_iface     = $iface;                  my $x_iface     = $iface;
                 $iface  =~ s/:/_/; # convert "eth0:0" --> "eth0_0"                  $iface  =~ s/:$//g;
                   $iface  =~ s/:/_/g; # convert "eth0:0" --> "eth0_0"
                 $x_iface                = [ $x_iface =~ m/^([a-z0-9]+)/i ]->[0]; # convert "eth0:0" --> "eth0"                  $x_iface                = [ $x_iface =~ m/^([a-z0-9]+)/i ]->[0]; # convert "eth0:0" --> "eth0"
                 $ifname{$iface} = $x_iface;                  $ifname{$iface} = $x_iface;
                 $ipcount{$iface}++;                  $ipcount{$iface}++;
Line 1592  while (my $line = <STDIN>) {
Line 1783  while (my $line = <STDIN>) {
                 push @{$ip6{$iface}}, $fields[3];                  push @{$ip6{$iface}}, $fields[3];
                 $scope6{$iface} = [ $fields[4] =~ m/Scope:(.*)$/i ]->[0];                  $scope6{$iface} = [ $fields[4] =~ m/Scope:(.*)$/i ]->[0];
         }          }
           elsif ($line =~ m/^[ \t]+inet\s/) { # Linux IP address
                   die unless defined $iface;
                   my @fields = split(/[\s:]+/, $line);
                   push @{$ip{$iface}}, $fields[2];
                   $bcast{$iface} = (defined($fields[5]) and $fields[5] eq "broadcast") ? $fields[6] : "";
                   $mask{$iface} = $fields[4];
           }
   
 }  }
   
Line 1605  map { printf "IPcount_%s=\"%s\";    export
Line 1803  map { printf "IPcount_%s=\"%s\";    export
 map { printf "IFname_%s=\"%s\";         export IFname_%s;\n",   $_, $ifname{$_},        $_; } keys %ifname;  map { printf "IFname_%s=\"%s\";         export IFname_%s;\n",   $_, $ifname{$_},        $_; } keys %ifname;
 printf "interfaces=\"%s\";      export interfaces;\n", join(" ", sort keys %ip);  printf "interfaces=\"%s\";      export interfaces;\n", join(" ", sort keys %ip);
         '`          '`
         eval "$parsed_interfaces";  
         #echo "$parsed_interfaces";          #echo "$parsed_interfaces";
           eval "$parsed_interfaces";
   
         parsed_routes=`$PERL -e '          parsed_routes=`$PERL -e '
         $\ = "\n";          $\ = "\n";
Line 1655  printf "interfaces=\"%s\";     export inter
Line 1853  printf "interfaces=\"%s\";     export inter
   
         } # }}}          } # }}}
         '`          '`
           #echo $parsed_routes
         eval "$parsed_routes";          eval "$parsed_routes";
   
         # Now we have defined variables like this:          # Now we have defined variables like this:
Line 1717  done
Line 1916  done
 REAL_INTERFACES="`echo $x_REAL_INTERFACES | awk -v RS=' ' '{ print; }' | sort -u`"  REAL_INTERFACES="`echo $x_REAL_INTERFACES | awk -v RS=' ' '{ print; }' | sort -u`"
 INTERFACES_ACCEPT_ALL="$IFACE_ACCEPT_ALL"  INTERFACES_ACCEPT_ALL="$IFACE_ACCEPT_ALL"
   
   retcode=0;
 case "$1" in  case "$1" in
         start)          start)
                 print_info -n "Starting $DESC: "                  print_info -n "Starting $DESC: "
Line 1732  case "$1" in
Line 1931  case "$1" in
                 #                  #
                 bann_ip_adresses                  bann_ip_adresses
                 allow_accept_all                  allow_accept_all
                   allow_accept_vrrp
                 nmap_scan_filter                  nmap_scan_filter
                 invalid_packet_filter                  invalid_packet_filter
                 anti_spoof_filter                  anti_spoof_filter
Line 1739  case "$1" in
Line 1939  case "$1" in
                 mangle_prerouting                  mangle_prerouting
                 mangle_output                  mangle_output
                 accept_related                  accept_related
                   accept_loopback
                 log_new_connections                  log_new_connections
                 drop_output                  drop_output
                   allow_output
                   allow_icmp
                   print_info "----[ INCOMMING TRAFFIC ]------------------------------------------------"
                 drop_input                  drop_input
                 reject_input                  reject_input
                 allow_input                  allow_input
                 allow_output  
                 allow_icmp  
                 accept_loopback  
                 masquerade                  masquerade
                   forward_on
                 log_input_drop                  log_input_drop
                 log_output_drop                  log_output_drop
                 log_forward_drop                  log_forward_drop
                 forward_on  
                 do_ip_accounting                  do_ip_accounting
                 shaping_off                  shaping_off
                 shaping_on                  shaping_on
Line 1787  case "$1" in
Line 1988  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)          block)
                 shift;                  shift;
                 add_banned_ip $*;                  for banned_ip in $*; do
                 # start the some script twice to refresh rules (new blocked IP's)                          check_banned_ip "$banned_ip";
                 QUIET=yes $0 start;                          if [ "$?" -eq 0 ]; then
                                   print_info "Already blocked IP address: $banned_ip";
                                   retcode=1;
                           else
                                   print_info "Blocking IP address: $banned_ip";
                                   add_banned_ip "$banned_ip";
                                   do_ban_single_ip "$banned_ip";
                           fi
                   done
                 ;;                  ;;
         update)          update)
                 update;                  update;
Line 1812  case "$1" in
Line 2022  case "$1" in
                 ;;                  ;;
 esac  esac
   
 exit 0  exit "$retcode";
   
 # vim600: fdm=marker fdl=0 fdc=3  # vim600: fdm=marker fdl=0 fdc=3
   

Legend:
Removed from v.2.91  
changed lines
  Added in v.2.119

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