home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20110502.etc.tar.gz / bradford.20110502.etc.tar / etc / sysconfig / scripts / SuSEfirewall2-autointerface.sh next >
Linux/UNIX/POSIX Shell Script  |  2006-04-22  |  3KB  |  121 lines

  1. #!/bin/bash
  2. #
  3. # SuSEfirewall2-autointerface.sh - helper script for SuSEfirewall2
  4. # Copyright (C) 2004 SUSE Linux AG
  5. #
  6. # Author: Ludwig Nussel
  7. # Please send feedback via http://www.suse.de/feedback
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # version 2 as published by the Free Software Foundation.
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. # determine which network devices are internal and which are external
  21. #
  22. # The external device is always the one where the default route points at. If
  23. # there is no default route, then there is also no external device.
  24. #
  25. # Active devices except the external one are considered as candidates for
  26. # internal. Devices that are configured for pppoe in
  27. # /etc/sysconfig/network/ifcfg-dsl* are removed from the active list. If only
  28. # one device is left after that filter, it's considered as internal.
  29. #
  30. # => only one external and one internal possible
  31. # => if you only have one device with no default route it's internal
  32. #
  33. # All packets that arrive on devices that are neither internal nor external
  34. # will be dropped by the firewall
  35.  
  36. # print the device where the default route points at
  37. get_default_route_dev()
  38. {
  39.     while read line; do
  40.         set -- $line
  41.         [ "$1" != default ] && continue;
  42.         # interface name comes after a "dev" token
  43.         while [ "$1" != dev -a $# -gt 0 ]; do shift; done
  44.         if [ "$1" = dev ]; then
  45.             echo $2
  46.             break;
  47.         fi
  48.     done < <(ip route show)
  49. }
  50.  
  51. # print active interfaces except lo
  52. get_active_interfaces()
  53. {
  54.     while read line; do
  55.         set -- $line
  56.         case "$3" in
  57.             *UP*)
  58.                 dev=${2%%:}
  59.                 [ "$dev" != "lo" ] && echo $dev
  60.             ;;
  61.         esac
  62.     done < <(ip -o link show)
  63. }
  64.  
  65. # first parameter is device to filter from rest of arguments
  66. filter_one_dev()
  67. {
  68.     filter="$1"
  69.     shift
  70.     if [ -z "$filter" ]; then
  71.         echo "$@"
  72.         return;
  73.     fi
  74.     for i in "$@"; do
  75.         [ "$filter" = "$i" ] && continue
  76.         echo $i
  77.     done
  78. }
  79.  
  80. # filter devices for which a pppoe link is configured. exit with status 1 if
  81. # more than one device is left
  82. filter_pppoe_devs()
  83. {
  84.     for i in /etc/sysconfig/network/ifcfg-dsl*; do
  85.         . $i
  86.         [ -z "$DEVICE" -o $PPPMODE != pppoe ] && continue
  87.         if [ -x "/sbin/getcfg-interface" ] && ! ip link show dev "$DEVICE" > /dev/null 2>&1; then
  88.             DEVICE=`/sbin/getcfg-interface "$DEVICE"` || continue
  89.         fi
  90.         set -- `filter_one_dev "$DEVICE" "$@"`
  91.     done
  92.     echo "$@"
  93.     [ "$#" -gt 1 ] && return 1
  94.     return 0
  95. }
  96.  
  97. shopt -s nullglob
  98.  
  99. internal=
  100. external=`get_default_route_dev`
  101.  
  102. # all active devices
  103. active=`get_active_interfaces`
  104.  
  105. # active devices except the default route device
  106. filtered=`filter_one_dev "$external" $active`
  107.  
  108. # active devices minus pppoe devices
  109. filtered2=`filter_pppoe_devs $filtered`
  110. [ "$?" = 0 ] && internal=$filtered2
  111.  
  112. echo "External: $external"
  113. echo "Internal: $internal"
  114.  
  115. #echo "Active: $active"
  116. #echo "Filtered: $filtered"
  117. #echo "Filtered2: $filtered2"
  118.