[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#605723: Updated route wrapper suggestion



In order to react properly on incomplete calls like

    $ route -h
    $ route -n

two cases of protection on 'shift' are needed:

    -shift
    +test $# -gt 0  &&  shift

Without these Dash aborts with an error.


Best regards,
Mats Erik Andersson, DM
#!/bin/sh
#
# /sbin/route
#
# Wrapper script around /lib/freebsd/route
# crafted to catch the routing commands issued
# by /sbin/ifup and /sbin/ifdown, translating
# them into valid FreeBSD commands, at the same
# time preserving all BSD native routing input.
#
# Authors: Axel Beckert, Mats Erik Andersson

set -e

if [ $# = 0 ] ; then
  # Issue usage info
  /lib/freebsd/route
fi

args=""

# Capture all options first. Save them for later.
# One Debian endemic addition: -A family.
# Typically: -A inet6
#
opts=""
af=""

while [ $# -gt 0 ]; do
  case "$1" in
    -*)
      # One Debian addition.
      if [ "$1" = "-A" ] ; then
        shift
        if [ -n "$1" ] ; then
          af="-$1"
          shift
          continue
        fi
        # Incorrect usage, call for help
        /lib/freebsd/route
      fi
      # A native option remains
      opts="${opts} $1"
      shift
      ;;
    *)
     # Must be the beginning of a command mode line.
     break ;;
  esac
done

cmd="$1"
test $# -gt 0  &&  shift

# Commands originating from ifupdown can only be
# stated either for "add" or for "del".
#
# The commands from "ifupdown" are acceptable
# as long as they do not contain the keyword "gw".
# In case the command does so, then the very last
# command line argument is the name of the interface.
# We ignore it for now, since an interface specification
# do not carry the same connotation in FreeBSD as it
# does in GNU/Linux.
#
# The keyword "metric" seems unknown for FreeBSD,
# so it is filtered off.

if [ "$cmd" = "add" -o "$cmd" = "del" ] ; then
  # Our simple state machine
  HAVE_SEEN_GW=""

  while [ $# -gt 0 ]; do
    if [ -n "$HAVE_SEEN_GW" ] ; then
      if [ $# -eq 1 ] ; then
        # We have reached the end. An interface name
        # appears as last argument, since we are in
        # a state caused by a call from ifupdown.
        # We simply discard the interface name.
        break
      fi
      if [ "$1" = "metric" ] ; then
        # Ignored, together with its argument
        shift
      else
        args="${args} $1"
      fi
    else
      # Ifup and ifdown use "gw" only in two forms.
      if [ "$1" = "gw" -a \( $# -eq 3 -o \( $# -eq 5 -a "$3" = "metric" \) \) ]
      then
        # Change state and discard "gw",
        # hoping that no administrator ever
        # names a host or a network as "gw".
        HAVE_SEEN_GW="YES"
      else
        # This ingredient is important. Add it.
        args="${args} $1"
      fi
    fi
    test $# -gt 0  &&  shift
  done
else
  # Must have a native command, whatever the content.
  args="$@"
fi

exec /lib/freebsd/route ${opts} ${cmd} ${af} ${args}

Attachment: signature.asc
Description: Digital signature


Reply to: