#!/bin/sh
#
# diald         This shell script takes care of starting 
#               diald (demand dialing daemon).
#
# Copyright (c) 1997, 1998 The Meme Factory, Inc.  http://www.meme.com
#
# This code is distributed under the GNU copyright.
#
# Read the README for a description of all the control variables and
# where they come from.
#

function error {
  echo
  echo "$1"
  echo "diald not started"
  logger -p daemon.info -t diald.init "$1"
  exit 1
}

DIALD_UMASK="177"

[ -f /usr/sbin/chat ] || error "/usr/sbin/chat: missing program" 
[ -f /etc/sysconfig/dialdcfg ] || error "/etc/sysconfig/dialdcfg: missing file"
[ -f /etc/sysconfig/network-scripts/$1 ] || error "/etc/sysconfig/network-scripts/$1: missing file"
 
# Source diald configuration.
. /etc/sysconfig/dialdcfg

# Source the diald configuration for this interface.
. /etc/sysconfig/network-scripts/$1

# Get the interface.
IFACE=$(echo "$1" | sed "s/^dialdcfg-//")

# Get configuration for the interface.
if [ -f network-functions ] ; then
  # Redhat 5.0 style sourcing.
  CONFIG="ifcfg-$IFACE"
  if [ -n "${CLONE}" ] ; then
    CONFIG="${CONFIG}-${CLONE}"
  fi
  . network-functions
  source_config
else
  if [ ! -f /etc/sysconfig/network-scripts/ifcfg-$IFACE ] ; then
    error "/etc/sysconfig/network-scripts/ifcfg-$IFACE does not exist"
  fi
  . /etc/sysconfig/network-scripts/ifcfg-$IFACE
fi

# Compatiblity support.
if [ "${CONFPROG}" = "REDHAT" ] ; then
  if [ -z "${REMOTENAME}" ] ; then
    REMOTENAME="${DEVICE}"
  fi
  if [ -z "${IPPARM}" ] ; then
    IPPARAM="${DEVICE}"
  fi
fi

# Check for errors in configuration.
if [ -z "${IPADDR}${LINK_DOWN_IPADDR}" ] ; then
  error "Either IPADDR or LINK_DOWN_IPADDR must have a value."
fi

if [ -z "${REMIP}${LINK_DOWN_REMIP}" ] ; then
  error "Either REMIP or LINK_DOWN_REMIP must have a value."
fi


# Make us a custom diald network filter.

defs="-DDEFAULT_TIMEOUT=${DEFAULT_TIMEOUT}"
defs="$defs -DIFACE=${IFACE}"
if [ -n "${STARTUP_TIMEOUT}" ] ; then
  defs="$defs -DSTARTUP_TIMEOUT=${STARTUP_TIMEOUT}"
fi

if [ -n "${HTTP_TIMEOUT}" ] ; then
  defs="$defs -DHTTP_TIMEOUT=${HTTP_TIMEOUT}"
elif [ -n "${WEB_BROWSING_TIMEOUT}" ] ; then
  defs="$defs -DHTTP_TIMEOUT=${WEB_BROWSING_TIMEOUT}"
fi

if [ -n "${NNTP_TIMEOUT}" ] ; then
  defs="$defs -DNNTP_TIMEOUT=${NNTP_TIMEOUT}"
elif [ -n "${WEB_BROWSING_TIMEOUT}" ] ; then
  defs="$defs -DNNTP_TIMEOUT=${WEB_BROWSING_TIMEOUT}"
fi

if [ -n "${FTP_TIMEOUT}" ] ; then
  defs="$defs -DFTP_TIMEOUT=${FTP_TIMEOUT}"
elif [ -n "${WEB_BROWSING_TIMEOUT}" ] ; then
  defs="$defs -DFTP_TIMEOUT=${WEB_BROWSING_TIMEOUT}"
fi

if [ -n "${SUPPORT_SCO_TELNET}" ] ; then
  defs="$defs -DSUPPORT_SCO_TELNET"
fi
if [ -n "${FETCH_MAIL_TIMEOUT}" ] ; then
  defs="$defs -DFETCH_MAIL_TIMEOUT=${FETCH_MAIL_TIMEOUT}"
fi
if [ -n "${SEND_MAIL_TIMEOUT}" ] ; then
  defs="$defs -DSEND_MAIL_TIMEOUT=${SEND_MAIL_TIMEOUT}"
fi
if [ -n "${DOMAIN_TIMEOUT}" ] ; then
  defs="$defs -DDOMAIN_TIMEOUT=${DOMAIN_TIMEOUT}"
fi
if [ -n "${NETBIOS_NS_TIMEOUT}" ] ; then
  defs="$defs -DNETBIOS_NS_TIMEOUT=${NETBIOS_NS_TIMEOUT}"
fi
if [ -n "${OTHER_TCP_TIMEOUT}" ] ; then
  defs="$defs -DOTHER_TCP_TIMEOUT=${OTHER_TCP_TIMEOUT}"
fi
if [ -n "${OTHER_UDP_TIMEOUT}" ] ; then
  defs="$defs -DOTHER_UDP_TIMEOUT=${OTHER_UDP_TIMEOUT}"
fi
if [ "${QUICK_DROP}" = "yes" ] ; then
  defs="$defs -DQUICK_DROP"
fi
if [ "${STANDARD_IGNORES}" = "yes" ] ; then
  defs="$defs -DSTANDARD_IGNORES"
fi
if [ "${IGNORE_NAMESERVER}" = "yes" ] ; then
  defs="$defs -DIGNORE_NAMESERVER"
fi

SAVE_UMASK="$(umask)"
umask ${DIALD_UMASK}
m4 $defs /usr/local/lib/diald/standard.filter.m4 > /tmp/diald-${IFACE}.filter
umask ${SAVE_UMASK}

# diald isn't too happy about taking command line arguments, so
# stash 'em in a file.  (Probably another security hole.)

# Logic snarfed from /etc/sysconfig/network-scripts/ifup-ppp.

SAVE_UMASK="$(umask)"
umask ${DIALD_UMASK}
( # All this standard out gets stashed.

echo "pidfile diald-${IFACE}.pid"
echo "connect /tmp/diald-${IFACE}.connect"
if [ -n "${FIFO}" ] ; then
  echo "fifo ${FIFO}"
fi
echo "device $MODEMPORT"
echo "speed $LINESPEED"
if [ "${HARDFLOWCTL}" = yes ] ; then
  echo "modem"
  echo "crtscts"
fi
if [ "${DEFROUTE}" = yes ] ; then
  echo "defaultroute"
fi
if [ -n "${ADDROUTE}" ] ; then
  echo "addroute ${ADDROUTE}"
fi
if [ -n "${BUFFER_TIMEOUT}" ] ; then
  echo "buffer-timeout ${BUFFER_TIMEOUT}"
fi
if [ -n "${REDIAL_TIMEOUT}" ] ; then
  echo "redial-timeout ${REDIAL_TIMEOUT}"
fi
if [ -n "${DIAL_FAIL_LIMIT}" ] ; then
  echo "dial-fail-limit ${DIAL_FAIL_LIMIT}"
fi
if [ -n "${NODEV_RETRY_TIMEOUT}" ] ; then
  echo "nodev-retry-timeout ${NODEV_RETRY_TIMEOUT}"
fi
if [ -n "${RETRY_COUNT}" ] ; then
  echo "retry-count ${RETRY_COUNT}"
fi
if [ -n "${REDIAL_BACKOFF_START}" ] ; then
  echo "redial-backoff-start ${REDIAL_BACKOFF_START}"
fi
if [ -n "${REDIAL_BACKOFF_LIMIT}" ] ; then
  echo "redial-backoff-limit ${REDIAL_BACKOFF_LIMIT}"
fi
if [ -n "${MRU}" ] ; then
  echo "mru ${MRU}"
fi
if [ -n "${IPADDR}" ] ; then
  echo "local $IPADDR"
else
  echo "local $LINK_DOWN_IPADDR"
fi
if [ -n "${REMIP}" ] ; then
  echo "remote $REMIP"
else
  echo "remote $LINK_DOWN_REMIP"
fi
if [ -z "${REMIP}" -o -z "${IPADDR}" ] ; then
  echo "dynamic"
fi

# Generate the ppp options
pppopts=""
if [ "${ESCAPECHARS}" != yes ] ; then
  pppopts="$pppopts asyncmap 00000000"
fi
if [ -n "${LCP_ECHO_FAILURE}" ] ; then
  pppopts="$pppopts lcp-echo-failure ${LCP_ECHO_FAILURE}"
fi
if [ -n "${LCP_ECHO_INTERVAL}" ] ; then
  pppopts="$pppopts lcp-echo-interval ${LCP_ECHO_INTERVAL}"
fi
if [ "${NOIPDEFAULT}" = yes ] ; then
  pppopts="$pppopts noipdefault"
fi
if [ -z "${IPADDR}" ] ; then
  pppopts="$pppopts ipcp-accept-local"
fi
if [ -z "${REMIP}" ] ; then
  pppopts="$pppopts ipcp-accept-remote"
fi
# If PAP authentication was configured by netcfg, pass the info to pppd.
if [ -n "${PAPNAME}" ] ; then
  pppopts="$pppopts name ${PAPNAME} remotename ${IFACE}"
fi
if [ -n "${REMOTENAME}" ] ; then
  pppopts="$pppopts remotename ${REMOTENAME}"
fi
# Local and remote IP addresses.
if [ -n "${IPADDR}" ] ; then
  pppopts="$pppopts ${IPADDR}"
else
  pppopts="$pppopts ${LINK_DOWN_IPADDR}"
fi
pppopts="${pppopts}:"
if [ -n "${REMIP}" ] ; then
  pppopts="${pppopts}${REMIP}"
else
  pppopts="${pppopts}${LINK_DOWN_REMIP}"
fi

if [ -n "${IPPARAM}" ] ; then
  pppopts="$pppopts ipparam ${IPPARAM}"
fi
if [ -n "${DEBUG}" ] ; then
  pppopts="$pppopts debug"
fi

if [ -n "$pppopts" ] ; then
  echo "pppd-options $pppopts"
fi

# This should go last.
echo "include /tmp/diald-${IFACE}.filter"

) > /tmp/diald-${IFACE}.conf
umask ${SAVE_UMASK}

# Create the connection script.
SAVE_UMASK="$(umask)"
umask ${DIALD_UMASK}
(
echo "#!/bin/sh"
echo "exec /etc/sysconfig/network-scripts/diald-connect ${IFACE}" 
) > /tmp/diald-${IFACE}.connect
umask ${SAVE_UMASK}
chmod u+x /tmp/diald-${IFACE}.connect

# Do the dirty deed.
diald -file /tmp/diald-${IFACE}.conf
result=$?

if [ -n "${FIFO}" ] ; then
  # Tell diald how we want it to run.

  if [ "${START_STATE}" = "up" ] ; then
    echo force > ${FIFO}
  elif [ "${START_STATE}" = "down" ] ; then
    echo block > ${FIFO}
  fi
fi

exit $result
