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

Diff for scripts/shell/firewall/fw-universal.sh between version 2.13 and 2.20

version 2.13, 2005/01/16 11:06:10 version 2.20, 2005/03/04 23:53:14
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.12 2005/01/16 10:55:39 rajo Exp $  # $Platon: scripts/shell/firewall/fw-universal.sh,v 2.19 2005/03/01 23:17:11 rajo Exp $
 #  #
 # Changelog:  # Changelog:
 # 2003-10-24 - created  # 2003-10-24 - created
Line 48  AWK="${AWK:=/usr/bin/awk}"
Line 48  AWK="${AWK:=/usr/bin/awk}"
 LO_IFACE="${LO_IFACE:=lo}"  LO_IFACE="${LO_IFACE:=lo}"
 LO_IP="IP_$LO_IFACE"  LO_IP="IP_$LO_IFACE"
   
 # Which ports will be allowed on INPUT (TCP connections)  #
 ALL_ACCEPT_INPUT_TCP="${ALL_ACCEPT_INPUT_TCP:=}"  # CONSTANTS - Do not edit
 # interface eth0  #
 eth0_ACCEPT_INPUT_TCP="${eth0_ACCEPT_INPUT_TCP:=}"  ANYWHERE="0.0.0.0/0"                            # Match any IP address
 # interface ppp0  BROADCAST_SRC="0.0.0.0"                         # Broadcast Source Address
 ppp0_ACCEPT_INPUT_TCP="${ppp0_ACCEPT_INPUT_TCP:=}"  BROADCAST_DEST="255.255.255.255"        # Broadcast Destination Address
   CLASS_A="10.0.0.0/8"                            # Class-A Private (RFC-1918) Networks
 # Which ports will be allowed on INPUT (UDP connections)  CLASS_B="172.16.0.0/12"                         # Class-B Private (RFC-1918) Networks
 # interface eth0  CLASS_C="192.168.0.0/16"                        # Class-C Private (RFC-1918) Networks
 eth0_ACCEPT_INPUT_UDP="${eth0_ACCEPT_INPUT_UDP:=}"  CLASS_D_MULTICAST="224.0.0.0/4"         # Class-D Multicast Addresses
 # interface ppp0  CLASS_E_RESERVED_NET="240.0.0.0/5"      # Class-E Reserved Addresses
 ppp0_ACCEPT_INPUT_UDP="${ppp0_ACCEPT_INPUT_UDP:=}"  PRIVPORTS="0:1023"                                      # Well-Known, Privileged Port Range
   UNPRIVPORTS="1024:65535"                        # Unprivileged Port Range
   TRACEROUTE_SRC_PORTS="32769:65535"      # Traceroute Source Ports
   TRACEROUTE_DEST_PORTS="33434:33523"     # Traceroute Destination Ports
   
   
 # allow some ICMP packets - needed for ping etc.  # allow some ICMP packets - needed for ping etc.
 ACCEPT_ICMP_PACKETS="${ACCEPT_ICMP_PACKETS:=echo-reply destination-unreachable echo-request time-exceeded}"  ACCEPT_ICMP_PACKETS="${ACCEPT_ICMP_PACKETS:=echo-reply destination-unreachable echo-request time-exceeded}"
   
   
 # load necessary modules from $MODULES variable  # load necessary modules from $MODULES variable
 load_modules()  load_modules()
 { # {{{  { # {{{
Line 90  unload_modules()
Line 95  unload_modules()
 print_iface_status()  print_iface_status()
 { # {{{  { # {{{
         # Print interfaces:          # Print interfaces:
         echo "# iface   | IP addr       | broadcast     | netmask       | HW addr"          echo "# iface   | IP addr       | Gateway       | broadcast     | netmask       | HW addr"
         for iface in $interfaces; do          for iface in $interfaces; do
                 IP="IP_$iface"; Bcast="Bcast_$iface"; Mask="Mask_$iface"; HWaddr="HWaddr_$iface";                  IP="IP_$iface"; Gateway="Gateway_$iface"; Bcast="Bcast_$iface"; Mask="Mask_$iface"; HWaddr="HWaddr_$iface";
                 echo "$iface    | ${!IP}        | ${!Bcast}     | ${!Mask}      | ${!HWaddr}"                  echo "$iface    | ${!IP}        | ${!Gateway}   | ${!Bcast}     | ${!Mask}      | ${!HWaddr}"
         done          done
 } # }}}  } # }}}
   
Line 120  remove_chains()
Line 125  remove_chains()
         for table in filter nat mangle; do          for table in filter nat mangle; do
                 $IPTABLES -t $table -F # clear all chains                  $IPTABLES -t $table -F # clear all chains
                 $IPTABLES -t $table -X # remove all chains                  $IPTABLES -t $table -X # remove all chains
                   $IPTABLES -t $table -Z # zero counts
         done          done
   
 } # }}}  } # }}}
   
 # all packets on loopback are accpted  
 set_loopback()  
 { # {{{  
         $IPTABLES -A INPUT  -j ACCEPT -i $LO_IFACE  
         $IPTABLES -A OUTPUT -j ACCEPT -o $LO_IFACE  
 } # }}}  
   
 # DROP packages from nmap(1)  # DROP packages from nmap(1)
 nmap_scan_filter()  nmap_scan_filter()
 { # {{{  { # {{{
Line 286  masquerade()
Line 285  masquerade()
   
                 # alow packets from private subnet                  # alow packets from private subnet
                 $IPTABLES -A FORWARD -s ! $localnet -i $NAT_SUBNET_IFACE -j DROP                  $IPTABLES -A FORWARD -s ! $localnet -i $NAT_SUBNET_IFACE -j DROP
                   for client_ip in $NAT_CLIENT_DROP; do
                           echo -en " !$client_ip";
                           $IPTABLES -A FORWARD -s $client_ip -i $NAT_SUBNET_IFACE -j DROP
                   done
   
                 for redirect in $NAT_TCP_PORT_REDIRECT; do                  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=: '{ printf "remote_port=%s; local_port=%s;", $1, $2; }'`
Line 338  masquerade()
Line 341  masquerade()
                 #$IPTABLES -A FORWARD -p ICMP -j LOG --log-prefix "FWD ICMP: "                  #$IPTABLES -A FORWARD -p ICMP -j LOG --log-prefix "FWD ICMP: "
                 echo " done."                  echo " done."
   
                   # Port forwarding to local machines
                   if [ ! -z "$NAT_TCP_PORT_FORWARD" ]; then
                           echo -en "\tForwarding 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; }'`
                                   echo -en " $src_port -> $local_machine:$dest_port"
                                   $IPTABLES -t nat -A PREROUTING -p TCP -i $NAT_LAN_IFACE -d ${!lan_ip} \
                                   --dport $src_port -j DNAT --to $local_machine:$dest_port
                                   $IPTABLES -A FORWARD -p TCP -i eth0 -d $local_machine --dport $dest_port -j ACCEPT
                           done
                           echo " done."
                   fi
   
                 # Keep state of connections from private subnets                  # Keep state of connections from private subnets
                 $IPTABLES -A OUTPUT  -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT                  $IPTABLES -A OUTPUT  -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT
                 #$IPTABLES -A FORWARD -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT                  #$IPTABLES -A FORWARD -m state --state NEW -o $NAT_LAN_IFACE -j ACCEPT
Line 392  drop_output()
Line 408  drop_output()
   
 } # }}}  } # }}}
   
 allow_input()  allow_accept_all()
 { # {{{  { # {{{
   
         if [ ! -z "$IFACE_ACCEPT_ALL" ]; then          if [ ! -z "$IFACE_ACCEPT_ALL" ]; then
                 echo -en "Accepting ALL packets on interfaces:"                  echo -en "Accepting ALL packets on interfaces:"
                 for iface in $IFACE_ACCEPT_ALL; do                  for iface in $IFACE_ACCEPT_ALL; do
Line 405  allow_input()
Line 420  allow_input()
                 done                  done
                 echo " done."                  echo " done."
         fi          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:"
Line 457  allow_input()
Line 476  allow_input()
                 fi                  fi
         done          done
   
           # Enable outgoing TRACEROUTE requests (required e.g. by Skype, http://www.skype.com)
           if [ ! -z "$TRACEROUTE_IFACE" ]; then
                   ip="IP_$ANTISPOOF_IFACE";
                   echo -en "Accepting traceroute:"
   
                   $IPTABLES -A OUTPUT -o $ANTISPOOF_IFACE -p UDP \
                           --sport $TRACEROUTE_SRC_PORTS --dport $TRACEROUTE_DEST_PORTS \
                           -s ${!ip} -d $ANYWHERE -j ACCEPT
   
                   for iface in $TRACEROUTE_IFACE; do
                           $IPTABLES -A FORWARD -p UDP -i $iface --sport $TRACEROUTE_SRC_PORTS \
                                   --dport $TRACEROUTE_DEST_PORTS -j ACCEPT
                   done
                   echo " done."
           fi
   
 } # }}}  } # }}}
   
 # ACCEPT all packets from our IP address  # ACCEPT all packets from our IP address
Line 583  parse_ifconfig()
Line 618  parse_ifconfig()
                         END { printf "\ninterfaces=\"%s\";      export interfaces;\n", interfaces; }                          END { printf "\ninterfaces=\"%s\";      export interfaces;\n", interfaces; }
         '`          '`
   
           eval `perl -e '
           $\ = "\n";
           open(FILE, "/proc/net/route") or die "Can not open /proc/net/route: $!";
           my @columns = split(/\s+/, <FILE>);
           while (my $line = <FILE>) {
                   my $iface;
                   my @vals = split(/\s+/, $line);
                   foreach my $key (@columns) {
                           $iface->{$key} = shift @vals;
                   }
   
                   foreach my $key (qw( Gateway Destination )) {
                           print "${key}_$iface->{Iface}=",
                                   qw("), hex2ip($iface->{$key}), qw("),
                                   "; export ${key}_$iface->{Iface};";
                   }
                   foreach my $key (qw( Flags MTU Metric Window IRTT )) {
                           print "${key}_$iface->{Iface}=",
                                   qw("), $iface->{$key}, qw("),
                                   "; export ${key}_$iface->{Iface};";
                   }
           }
           close(FILE);
   
   
           sub hex2ip
           { # {{{
                   my ($str) = @_;
                   my @block;
   
                   my $hex = uc($str);
   
                   while (length($hex)) {
                           my $x = ord(substr($hex, 0, 1));
                           my $y = ord(substr($hex, 1, 1));
   
                           $x = $x > 64 ? $x - 55 : $x - 48;
                           $y = $y > 64 ? $y - 55 : $y - 48;
   
                           push @block, 16 * $x + $y;
                           $hex = substr($hex, 2);
                   }
   
                   return join(".", reverse @block);
   
           } # }}}
           '`
         # Now we have defined variables like this:          # Now we have defined variables like this:
         # IFACE_eth0 HWaddr_eth0 IP_eth0 Bcast_eth0 Mask_eth0          # IFACE_eth0 HWaddr_eth0 IP_eth0 Bcast_eth0 Mask_eth0
         # IFACE_lo   HWaddr_lo   IP_lo   Bcast_lo   Mask_lo          # IFACE_lo   HWaddr_lo   IP_lo   Bcast_lo   Mask_lo
Line 594  parse_ifconfig()
Line 676  parse_ifconfig()
 parse_ifconfig  parse_ifconfig
 print_iface_status  print_iface_status
   
 # $interfaces - all interfaces  #
   # Split interfaces into 2 groups:
   #
   # $INTERFACES_ACCEPT_ALL - interfaces withouth restrictions
   #
 # $INTERFACES - all interfaces withouth loopback  # $INTERFACES - all interfaces withouth loopback
   #               and devices without restrictions (e.g. tun0 tun1 tap0 ...)
   #
   # list of all interfaces is in $interfaces variable
   #
 INTERFACES=""  INTERFACES=""
   INTERFACES_ACCEPT_ALL=""
   regexp='^\('`echo $IFACE_ACCEPT_ALL | sed 's/ /\\\|/g; s/+/.*/g;'`'\)$'
 for iface in $interfaces; do  for iface in $interfaces; do
         if [ "o$iface" = "olo" ]; then continue; fi          #if [ "o$iface" = "olo" ]; then continue; fi
         INTERFACES="$INTERFACES $iface";          echo $iface | grep -q -e "$regexp"
           if [ $? = 0 ] || [ "o$iface" = "olo" ]; then # lo interface is always here
                   INTERFACES_ACCEPT_ALL="$INTERFACES_ACCEPT_ALL $iface";
           else
                   INTERFACES="$INTERFACES $iface";
           fi
 done  done
   
   
Line 614  case "$1" in
Line 711  case "$1" in
                 #                  #
                 # (un)commnet next lines as needed                  # (un)commnet next lines as needed
                 #                  #
                 set_loopback                  allow_accept_all
                 nmap_scan_filter                  nmap_scan_filter
                 invalid_packet_filter                  invalid_packet_filter
                 anti_spoof_filter                  anti_spoof_filter

Legend:
Removed from v.2.13  
changed lines
  Added in v.2.20

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