#!/bin/sh
# Filename:      grml-sniff
# Purpose:       script for configuring a network sniffing setup
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

CONFIG_FILE=/etc/grml/routersetup
. /etc/grml/lsb-functions
. /etc/grml/script-functions

usage_info()
{
   einfo "$0 - script for configuring a network sniffing setup"
   einfo "Configure via $CONFIG_FILE - see man 8 grml-sniff" ; eend 0
}

if ! [ -r "$CONFIG_FILE" ] ; then
  eerror "$CONFIG_FILE could not be read."
  exit 1
fi

. "$CONFIG_FILE"

if [ -z "$BRIDGE_DEVICES" ] ; then
   eerror "Bridge devices (\$BRIDGE_DEVICES) not set in $CONFIG_FILE"
   exit 1
fi

[ -n "$BRCTL" ]          || BRCTL='brctl'
[ -n "$BRIDGE_NAME" ]    || BRIDGE_NAME='br0'
[ -n "$BRIDGE_DEVICES" ] || BRIDGE_DEVICES='eth0 eth1'

check4progs $BRCTL || exit 1

case "$1" in
    start)
        check4root || exit 1
        einfo "Starting sniffing setup"
        eindent
            einfo "Creating bridge device"
            brctl addbr "$BRIDGE_NAME"
            eend $?

            einfo "Bringing network device in promiscuous mode up:"
            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i"
                   ifconfig "$i" -arp promisc 0.0.0.0 up ; eend $?
               done
            eoutdent

            einfo "Adding network devices to $BRIDGE_NAME:"
            eindent
            for i in $BRIDGE_DEVICES ; do
                einfo "$i"
                brctl addif "$BRIDGE_NAME" $i  ; eend $?
            done
            eoutdent

            einfo "Bringing bridge $BRIDGE_NAME in promiscuous up"
            ip link set "$BRIDGE_NAME" promisc on up ; eend $?
        eoutdent
   ;;

   stop)
        check4root || exit 1
        einfo "Stopping sniffing setup"
        eindent
            einfo "Removing network devices from $BRIDGE_NAME: "

            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i "
                   brctl delif "$BRIDGE_NAME" $i  ; eend $?
               done
            eoutdent

            einfo "Disabling promiscuous mode on: "
            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i "
                   ip link set "$i" promisc off ; eend $?
               done
            eoutdent

            einfo "Bringing bridge $BRIDGE_NAME down"
            ip link set "$BRIDGE_NAME" down; eend $?

            einfo "Removing bridge device $BRIDGE_NAME"
            ifconfig "$BRIDGE_NAME" down || /bin/true
            brctl delbr "$BRIDGE_NAME"
            eend $?
        eoutdent
   ;;

   restart)
        check4root || exit 1
        $0 stop
        sleep 1
        $0 start
   ;;

   info|-h|--help)
        usage_info
   ;;

   status)
        check4root || exit 1
        einfo "$0 - status:"
        $BRCTL show ; eend $?
   ;;

   *)
        echo "Usage: $0 {start|stop|restart|status|info}"
        exit 1
   ;;
esac

## END OF FILE #################################################################
# vim: ft=sh expandtab ai
