"Fossies" - the Fresh Open Source Software Archive

Member "fake-1.1.11/fake" (8 Jul 2009, 10739 Bytes) of package /linux/misc/old/fake-1.1.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash -u
    2 ############################################################
    3 # Fake               v 1.1.8                   February 2001
    4 # Horms                                   horms@verge.net.au
    5 #
    6 # Fake
    7 # Script to spoof an ip
    8 # Designed to create redundant servers
    9 # Copyright (C) 1998  Horms <horms@verge.net.au>
   10 # 
   11 # This program is free software; you can redistribute it
   12 # and/or modify it under the terms of the GNU General Public
   13 # License as published by the Free Software Foundation;
   14 # either version 2 of the License, or (at your option) any
   15 # later version.
   16 # 
   17 # This program is distributed in the hope that it will be
   18 # useful, but WITHOUT ANY WARRANTY; without even the implied
   19 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
   20 # PURPOSE.  See the GNU General Public License for more
   21 # details.
   22 # 
   23 # You should have received a copy of the GNU General Public
   24 # License along with this program; if not, write to the Free
   25 # Software Foundation, Inc., 59 Temple Place, Suite 330,
   26 # Boston, MA 02111-1307 USA
   27 ############################################################
   28 
   29 ############################################################
   30 # Unset Locale
   31 ############################################################
   32 
   33 unset LC_ALL
   34 unset LANG
   35 
   36 ############################################################
   37 # Where the default config lies
   38 ############################################################
   39 
   40 ETC_DIR="/etc/fake"
   41 
   42 ############################################################
   43 # Check Real UserID is root
   44 ############################################################
   45 
   46 function check_root {
   47   if [ "$(id -ru)" != "0" ]; then
   48     die Must be run by the super user
   49   fi
   50 }
   51 
   52 
   53 ############################################################
   54 # Source rc file
   55 ############################################################
   56 
   57 function source_rc {
   58   local RC_FILENAME="$1"
   59   shift
   60   local RC_DIRS="$1"
   61   shift
   62   local VARIABLES="$*"
   63 
   64   local RC_FOUND="FALSE"
   65 
   66   unset $VARIABLES
   67 
   68   for i in $RC_DIRS; do
   69     local RC_FILE="${i}/${RC_FILENAME}"
   70     if [ -f "$RC_FILE" ]; then
   71       log "Sourcing $RC_FILE";
   72       RC_FOUND="TRUE"
   73       source "$RC_FILE";
   74     fi
   75   done
   76   
   77   if [ "$RC_FOUND" = "FALSE" ]; then
   78     die "No rc file \"$RC_FILENAME\" found in; $RC_DIRS"
   79   fi
   80 
   81   check_set $VARIABLES
   82 }
   83 
   84 
   85 ############################################################
   86 # Check if variables are set
   87 ############################################################
   88 
   89 function check_set {
   90   local TMP
   91 
   92   for i in $*; do
   93     TMP=$(eval echo \${$i})
   94     if [ "${TMP:-__NULL__}" = "__NULL__" ]; then
   95       die "$i is not set in config file"
   96     fi
   97   done
   98 }
   99 
  100 ############################################################
  101 # Log signal death
  102 ############################################################
  103 
  104 function bail {
  105   trap - EXIT
  106   log "Signal received bailing..."
  107   exit 1
  108 }
  109 
  110 
  111 ############################################################
  112 # Log dying
  113 ############################################################
  114 
  115 function die {
  116   log "Fatal Error: $*"
  117   exit 1
  118 }
  119 
  120 
  121 ############################################################
  122 # Warn about something
  123 ############################################################
  124 
  125 function warn {
  126   log "Warning: $*"
  127 }
  128 
  129 
  130 ############################################################
  131 # ignore signal
  132 ############################################################
  133 
  134 function ignore {
  135   log "Signal received ignoring..."
  136 }
  137 
  138 
  139 ############################################################
  140 # shutdown_fake cleanly on signal
  141 ############################################################
  142 
  143 function shutdown_fake {
  144   trap - EXIT
  145   log "Signal received shutting down..."
  146   fake_off 1
  147   exit
  148 }
  149 
  150 
  151 ############################################################
  152 # Log messages with a timestamp
  153 ############################################################
  154 
  155 function log {
  156   #echo $(date) fake[$$] $*
  157   logger -p local0.notice -t fake[$$] -- $*
  158 }
  159 
  160 
  161 ############################################################
  162 # Ohh my thats a nice usage function
  163 ############################################################
  164 
  165 function usage {
  166 cat<<__EOF__
  167 Usage: fake [remove] ip_addr
  168            ip_addr: The ip address to spoof
  169 __EOF__
  170 exit 1
  171 } 1>&2
  172 
  173 
  174 ############################################################
  175 # Parse arguments
  176 ############################################################
  177 
  178 function parse_arguments {
  179   REMOVE="FALSE"
  180 
  181   local VARIABLES="IFCONFIG SPOOF_IP TARGET_INTERFACE"
  182   local INTERFACE_VARIABLES="SPOOF_NETMASK SPOOF_BROADCAST"
  183   local FOREIGN_VARIABLES="FOREIGN_ARP"
  184   local FOREIGN_STATIC_VARIABLES="FOREIGN_MACADDR"
  185   local FOREIGN_DYNAMIC_VARIABLES="FAKE_RSH FOREIGN_INTERFACE"
  186   if [ $# -lt 1 ];then 
  187     usage 
  188   fi
  189   local GIVEN_IP="$1"
  190   shift
  191 
  192   if [ "$GIVEN_IP" = "remove" ]; then
  193     REMOVE="TRUE"
  194     VARIABLES="SPOOF_IP TARGET_INTERFACE"
  195     if [ $# -lt 1 ];then 
  196       usage 
  197     fi
  198     GIVEN_IP="$1"
  199   fi
  200 
  201   INSTANCE_CONFIG_FILE="${GIVEN_IP}.cfg"
  202 
  203   source_rc "$INSTANCE_CONFIG_FILE" "$INSTANCE_CONFIG_DIR" "$VARIABLES"
  204 
  205   if [ "${IFCONFIG}" = "TRUE" ]; then
  206     check_set $INTERFACE_VARIABLES
  207   fi
  208   
  209   if [ "${FOREIGN_INTERFACE:-NULL}" != "NULL" ]; then
  210     check_set $FOREIGN_VARIABLES
  211     if [ "${FOREIGN_INTERFACE}" = "STATIC" ]; then
  212     check_set $FOREIGN_STATIC_VARIABLES
  213     else
  214     check_set $FOREIGN_DYNAMIC_VARIABLES
  215     fi
  216   fi
  217 
  218   if [ "$GIVEN_IP" != "$SPOOF_IP" ]; then
  219     die "IP address \"$GIVEN_IP\" given as an argument does not match \$SPOOF_IP \"$SPOOF_IP\" in config file"
  220   fi
  221 
  222   PID_FILE="${PID_DIR}/fake.${SPOOF_IP}.pid"
  223 }
  224 
  225 ############################################################
  226 # set_MACADDR
  227 # Get the mac address to use
  228 # A bit clumsy, but nevermind
  229 # usage: set_MACADDR interface
  230 # usage: set_MACADDR rsh_programme foreign_host interface
  231 #   sets MACADDR to the maccaddress of the interface
  232 ############################################################
  233 
  234 function set_MACADDR(){
  235   local INTERFACE RSH HOST
  236 
  237   if [ $# -lt 2 ]; then
  238     RSH=
  239     HOST=
  240     INTERFACE=$1
  241   else
  242     RSH=$1
  243     HOST=$2
  244     INTERFACE=$3
  245   fi
  246 
  247   MACADDR=$($RSH $HOST /sbin/ifconfig $INTERFACE  | \
  248     fgrep $INTERFACE | sed \
  249     's/^.*HWaddr \(..\):\(..\):\(..\):\(..\):\(..\):\(..\).*$/\1\2\3\4\5\6/')
  250 
  251   if [ "${MACADDR:=NULL}" = "NULL" ]; then
  252     log "Could not locate obtain hardware address for $TARGET_INTERFACE"
  253   fi
  254 }
  255 
  256 
  257 ############################################################
  258 # Turn fake on
  259 ############################################################
  260 
  261 function fake_on(){
  262   log "Turning Fake on."
  263 
  264   # Tell the user what what is going on
  265   log "$(hostname) being $SPOOF_IP"
  266   
  267   # Create pid file
  268   PID_FILE="${PID_DIR}/fake.${SPOOF_IP}.pid"
  269   if [ -f $PID_FILE ]; then
  270     log "fake appears to be running: $PID_FILE was found: Killing."
  271     kill -USR1 $(cat $PID_FILE)
  272   fi
  273 
  274   echo $$ > $PID_FILE || die "Could not write to pid file $PID_FILE"
  275   
  276   
  277   if [ "${IFCONFIG}" = "TRUE" ]; then
  278     # Setup the target interface, route  and send gratuitous arp
  279     /sbin/ifconfig "$TARGET_INTERFACE" $SPOOF_IP netmask $SPOOF_NETMASK \
  280       broadcast $SPOOF_BROADCAST \
  281       || die "Could not bring up interface"
  282     /sbin/route add -host $SPOOF_IP "$TARGET_INTERFACE" \
  283       || warn "Could not add local route"
  284   fi
  285  
  286   # Get the mac address to use
  287   set_MACADDR $TARGET_INTERFACE
  288  
  289   #Send gratutious arp
  290   log "Sending endless Gratuitous Arp."
  291   while [ 1 ]; do
  292     /usr/sbin/send_arp \
  293         ${SPOOF_IP} ${MACADDR} \
  294         ${SPOOF_IP} ${MACADDR} \
  295         ${TARGET_INTERFACE} ${MACADDR} FF:FF:FF:FF:FF:FF reply \
  296        || die "Could not send gratuitous arp"
  297     sleep $ARP_DELAY
  298     /usr/sbin/send_arp \
  299             ${SPOOF_IP} ${MACADDR} \
  300         ${SPOOF_IP} 00:00:00:00:00:00 \
  301         ${TARGET_INTERFACE} ${MACADDR} FF:FF:FF:FF:FF:FF request \
  302        || die "Could not send gratuitous arp"
  303     sleep $ARP_DELAY
  304   done
  305 }  
  306 
  307 
  308 ############################################################
  309 # Turn fake off
  310 ############################################################
  311 
  312 function fake_off(){
  313   local LAYER2_SRC_HW LAYER3_SNDR_HW
  314 
  315   log "Turning Fake off."
  316 
  317   #Our Spoofed ip
  318   if [ "${IFCONFIG}" = "TRUE" ]; then
  319     /sbin/ifconfig $TARGET_INTERFACE down
  320   fi
  321 
  322   #Kill existing fake
  323   if [ -f $PID_FILE -a "$(cat $PID_FILE)" != $$ ]; then
  324     log "fake appears to be running: $PID_FILE was found: Killing."
  325     kill -USR1 $(cat $PID_FILE)
  326     rm $PID_FILE || warn "Could not remove $PID_FILE"
  327   fi
  328 
  329   if [ "${FOREIGN_INTERFACE:-NULL}" != "NULL" ]; then
  330     log "sending gratuitous arp of original mac address."
  331     if [ "${FOREIGN_INTERFACE}" != "STATIC" ]; then
  332         set_MACADDR "$FAKE_RSH" "$SPOOF_IP" "$FOREIGN_INTERFACE"
  333     else
  334         MACADDR=${FOREIGN_MACADDR}
  335     fi
  336 
  337     LAYER3_SNDR_HW=${MACADDR}
  338     set_MACADDR $TARGET_INTERFACE
  339     LAYER2_SRC_HW=${MACADDR}
  340 
  341     while [ $FOREIGN_ARP -gt 0 ]; do
  342       FOREIGN_ARP=$(($FOREIGN_ARP - 1))
  343       /usr/sbin/send_arp \
  344             ${SPOOF_IP} ${LAYER3_SNDR_HW} \
  345                 ${SPOOF_IP} ${LAYER3_SNDR_HW} \
  346         ${TARGET_INTERFACE} \
  347         ${LAYER2_SRC_HW} FF:FF:FF:FF:FF:FF reply \
  348         || die "Could not send gratuitous arp"
  349       sleep $ARP_DELAY
  350       /usr/sbin/send_arp \
  351             ${SPOOF_IP} ${LAYER3_SNDR_HW} \
  352                 ${SPOOF_IP} 00:00:00:00:00:00 \
  353         ${TARGET_INTERFACE} \
  354         ${LAYER2_SRC_HW} FF:FF:FF:FF:FF:FF request \
  355         || die "Could not send gratuitous arp"
  356       sleep $ARP_DELAY
  357     done
  358   fi
  359  
  360 }
  361 
  362 
  363 ######################################################################
  364 # Some routers may need their arp cache cleared
  365 # Works with Cisco routers with rsh access
  366 ######################################################################
  367 
  368 function clear_remote_arp_cache {
  369   #See if the data file exists
  370   if [ ! -f "$CLEAR_ROUTERS_FILE" ]; then
  371     warn "could not open clear routers file $CLEAR_ROUTERS_FILE"
  372     return
  373   fi
  374 
  375   #read in the routers
  376   ROUTERS=$(<"$CLEAR_ROUTERS_FILE" sed 's/\#.*//' | tr '\012' ' '; echo)
  377 
  378   #reset the routers in the background so it doesn't block
  379   for ROUTER in $ROUTERS; do
  380     CMD="rsh $ROUTER clear arp-cache"
  381     log "$CMD"
  382     { $CMD >& /dev/null || log "Error executing $CMD" ; } &
  383   done
  384 }
  385 
  386 
  387 ######################################################################
  388 # The main game
  389 ######################################################################
  390 
  391 log "Starting with arguments: $@"
  392 
  393 #set some traps
  394 trap bail EXIT
  395 trap bail USR1
  396 trap shutdown_fake TERM
  397 trap shutdown_fake HUP
  398 
  399 #make sure we are root
  400 check_root
  401 
  402 #read rc file
  403 source_rc .fakerc "${ETC_DIR} ${HOME}" \
  404   ARP_DELAY CLEAR_ROUTERS_FILE PID_DIR INSTANCE_CONFIG_DIR
  405 
  406 #Check arguments
  407 parse_arguments $@
  408 
  409 #Looks ok so far so start by clearing the arp cache on some routers
  410 clear_remote_arp_cache
  411 
  412 #Do the deed
  413 if [ "$REMOVE" = "TRUE" ]; then
  414   fake_off 0
  415 else
  416   fake_on
  417 fi
  418