#!/bin/sh
#
#   $Id: networking,v 1.19 2003/07/23 23:11:42 rvenkat Exp $
#   $Source: /home/CVS/fireballTop/fireballFS/root/etc/init.d/networking,v $
#
# start/stop networking daemons.
#
# chkconfig: S 40 0
#


if ! [ -x /sbin/ifup ]
then
    exit 0
fi

if [ -e /etc/network/spoof-protect ]
then
    . /etc/network/spoof-protect
fi

INTERFACES=/etc/sysconfig/interfaces
if ! [ -f $INTERFACES ];
then 
   echo "Networking: Using default interface configuration"
   INTERFACES=/usr/local/fireball/defaults/sysconfig/interfaces
fi

hostname_set () {
    # Set the hostname
    #
    # Note that the hostname was probably already set, perhaps in
    # /etc/init.d/fireball, based on the system name in
    # /etc/sysconfig/sysname.  Although there has been talk of making
    # the hostname adapt to what comes in from DHCP or DNS, this has not
    # been implemented.
    # Also note that the shell variable $HOSTNAME is set by bash to the
    # value obtained from "hostname", presumably when the shell running
    # this script was invoked.  So the tests below are equivalent
    # to checking the results of running the command hostname.

#    if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]
#    then
#      HOSTNAME=localhost
#    fi


#    echo "Setting hostname: ${HOSTNAME} "
#    hostname ${HOSTNAME}
#    return 0


# Determine the default system name, if needed.
# This value can be specified by the user, and its state is kept in
# /etc/syconfig/sysname.  If that file doesn't exist, or the name is
# blank, then we will create a default name of "SN1234567890", where
# the numeric part is the ten digit system serial number.  That value
# will be 'SN0000000000' if no serial number has been assigned.

SYSNAME=''

# If the config file exists, set the SYSNAME to the value contained in
# the file.
if [[ -e /etc/sysconfig/sysname ]]
then
  SYSNAME=$(</etc/sysconfig/sysname)

  # If that value was the result of a setting based on a blank serial
  # number, we hope that a new SN will have been assigned and attempt
  # to use that.  Trigger this by pretending the file didn't exist,
  # leaving SYSNAME blank.
  if [[ "$SYSNAME" = 'RS0000000000' ]]
  then
    SYSNAME=''
  fi
fi

# If the name is blank, determine the default value.
if [[ -z "$SYSNAME" ]]
then
  echo -n 'Setting to default: '
  # Get the full 12 character MAC address.
  SYSNAME=$(/usr/local/fireball/warranty -s)
  # That looks like 'Serial_number: 123456789012'
  # Drop everything but the numeric portion
  # Prefix with "RS"
  SYSNAME=RS${SYSNAME#Serial_number: }

  # Put the default into the file for other programs to use.
  echo $SYSNAME > /etc/sysconfig/sysname
fi

#echo "System name is $SYSNAME"


# Use the configfile value to set the system-wide hostname.
hostname $SYSNAME

}

spoofprotect_rp_filter () {
    # This is the best method: turn on Source Address Verification and get
    # spoof protection on all current and future interfaces.
    
    if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]; then
        for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
            echo 1 > $f
        done
        return 0
    else
        return 1
    fi
}

spoofprotect () {
    echo -n "Setting up IP spoofing protection: "
    if spoofprotect_rp_filter; then
        echo "rp_filter."
    else
        echo "FAILED"
    fi
}

ip_forward () {
    if [ -e /proc/sys/net/ipv4/ip_forward ]; then
        echo -n "Enabling packet forwarding: "
        echo 1 > /proc/sys/net/ipv4/ip_forward
        echo "done."
    fi
}

syncookies () {
    if [ -e /proc/sys/net/ipv4/tcp_syncookies ]; then
        echo -n "Enabling TCP/IP SYN cookies: "
        echo 1 > /proc/sys/net/ipv4/tcp_syncookies
        echo "done."
    fi
}

disable_tcp_ecn () {
    if [ -e /proc/sys/net/ipv4/tcp_ecn ]; then
        echo -n "Disable TCP/IP Explicit Congestion Notification: "
        echo 0 > /proc/sys/net/ipv4/tcp_ecn
        echo "done."
    fi
}

doopt () {
    optname=$1
    default=$2
    opt=`grep "^$optname=" /etc/network/options`
    if [ -z "$opt" ]; then
        opt="$optname=$default"
    fi
    optval=${opt#$optname=}
    if [ "$optval" = "yes" ]; then
        eval $optname
    fi
}

case "$1" in
    start)
        doopt spoofprotect yes
        doopt syncookies no
        doopt ip_forward no
        doopt disable_tcp_ecn yes

        if sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts | 
          grep -q "^/ nfs$"
        then
            echo "NOT configuring network interfaces: / is an NFS mount"
            echo "Setting up loopback interface"
            /sbin/ifconfig lo 127.0.0.1
            route add -host 127.0.0.1 lo
            exit 1
        fi
        echo -n "Configuring network interfaces: "
#        /sbin/ifup -a --interfaces=/etc/sysconfig/interfaces
        /sbin/ifup -a --interfaces=$INTERFACES
        doopt hostname_set yes
        echo "done."
        
        ;;
    stop)
        if sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts | 
          grep -q "^/ nfs$"
        then
            echo "NOT deconfiguring network interfaces: / is an NFS mount"
        else
            echo -n "Deconfiguring network interfaces: "
            /sbin/ifdown -a --interfaces=/etc/sysconfig/interfaces
            echo "done."
        fi
        ;;
    reload)
        ;;
    force-reload)
        $0 restart
        ;;
    restart)
        echo -n "Reconfiguring network interfaces: "
        /sbin/ifdown -i /etc/sysconfig/interfaces -a
        /sbin/ifup -i /etc/sysconfig/interfaces -a
        echo "done."
        ;;
    *)
        echo "Usage: /etc/init.d/networking {start|stop|reload|restart}"
        exit 1
        ;;
esac

exit 0

