#!/bin/bash
# dhsup v1.1 by Jehsom <jehsom@usa.net>
# dnsup v1.2 - v1.7 by Simon Budig  <simon@budig.de>
#
# This script updates dynamic home.unix-ag.org hosts. Put the script in your 
#  crontab to run every 15 or so minutes, as in the following example:
#
#    */15 * * * *   $HOME/bin/dnsup
#
# Of course, replace the executable name and path to match your own.
#  the good thing about this script is that it can be run as any user.
#  However, you must have some downloader (snarf, lynx, wget), dig and
#  ifconfig installed and in your path.
#
# Changes:
#
# 1.7 - More descriptive error handling.
#
# 1.6 - Use http POST if using lynx. This keeps passwords out of logfiles.
#     - use a proxy if HTTP_PROXY has been configured.
#
# 1.5 - Log the stuff to a logfile via logger
#
# 1.4 - test if the IP delivered by the responsible DNS-Server is
#       correct. Do nothing if this is the case.
#
# 1.3 - changed to script to use the private service of
#       the Unix-AG Siegen.
#
# 1.2 - Made the ifconfig independant of the current locale.
#     - reorganized the detection which command is available
#     - added snarf as the preferred tool.
#     - make lynx dump the source of the page.
#     - determine the interface from the defaultroute if unconfigured.
#
# 1.1 - Changed script to use lynx first if available, since it functions
#       more reliably for some reason.
#     - Checked webpage output to ensure that it was in fact updating.
#
#################
# CONFIGURATION #
#################
# 
# Stuff in this file will override the values defined in this script

CONFIGFILE="/etc/dnsconf"

#
# Set your DNS login and dynamic hostname settings below. Alternatively
# write these lines to $CONFIGFILE .

DNS_USER=""    # Your login name on the DNS members page
DNS_PASS=""    # Password for the $DNS_USER account
DNS_HOST=""    # dynamic hostname
DNS_DOMAIN=""  # dynamic domainname
DNS_SERVER=""  # responsible DNS Server.

#
# Set your linux network device that gets you onto the internet
# if unset we try to use some magic to determine it.

NET_DEVICE=""

# if you need to specify a proxy:

HTTP_PROXY=""

# Source a configuration file.
if [ -r "$CONFIGFILE" ] ; then
  source "$CONFIGFILE"
fi

#
# Nothing below here needs to be modified.
#
###############################################################

LOGGER=/usr/bin/logger

# make sure that we have all necessary information for DNS:

if [ -z "$DNS_USER" -o -z "$DNS_PASS" -o -z "$DNS_HOST" -o -z "$DNS_DOMAIN" -o -z "$DNS_SERVER" ] ; then
  echo "$0: Missing configuration. Edit the script or $CONFIGFILE ." 1>&2
  exit 1
fi

# determine the interface using the default route.

if [ -z "$NET_DEVICE" ] ; then
  NET_DEVICE=`netstat -rn | sed -n "s/^0\.0\.0\.0.* //p"`
  if [ -z "$NET_DEVICE" ] ; then
    echo "$0: Sorry, unable to determine default interface." 1>&2
    exit 1
  fi
fi

CURRIP=$(LANG=C /sbin/ifconfig "$NET_DEVICE" 2>/dev/null | 
	sed -n '/inet addr/ {s/^[^:]*:\([0-9.]*\) .*$/\1/; p;}' | head -1)

if [ -z "$CURRIP" ] ; then
  echo "$0: IP address for interface $NET_DEVICE could not be obtained." 1>&2
  exit 1
fi

if [ "$CURRIP" = `dig +short @$DNS_SERVER $DNS_HOST.$DNS_DOMAIN` ] ; then
  echo "$0: IP already valid"
  exit 0
fi

# we have all necessary information to create the URL

URL="http://www.home.unix-ag.org/arne/cgi/bin/dyndns"
DATA="user=$DNS_USER&password=$DNS_PASS&host=$DNS_HOST&ip=$CURRIP"

if [ -n "$HTTP_PROXY" ] ; then
  export http_proxy="$HTTP_PROXY"
fi

# determine, which program to run. "which" searches for an executable in
# the path, head ensures, that only the first program is in $COMMAND

COMMAND=`which lynx snarf wget | head -1`
if [ -z "$COMMAND" ] ; then
  echo "$0: Neither snarf nor lynx nor wget are available for updating." 1>&2
  exit 1
fi

$LOGGER -p daemon.info -t dnsup "interface=$NET_DEVICE, host=$DNS_HOST, ip=$CURRIP"

error=`
  case "$COMMAND" in
    *lynx)
      # $COMMAND -source -auth="$DNS_USER:$DNS_PASS" "$URL"
      echo "$DATA" | $COMMAND -post-data -source "$URL"
      ;;
    *snarf)
      # $COMMAND -q "http://$DNS_USER:$DNS_PASS@${URL##http://}" -
      $COMMAND -q "${URL}?${DATA}" -
      ;; 
    *wget)
      # $COMMAND -q --http-user="$DNS_USER" --http-passwd="$DNS_PASS" "$URL" -O-
      $COMMAND -q "${URL}?${DATA}" -O-
      ;;
    *)
      echo "Sorry, don't know how to handle $COMMAND." 1>&2
  esac | grep -i "^Error:"
`

if [ -z "$error" ] ; then
  echo "$0: successfully set the new IP"
  $LOGGER -p daemon.info -t dnsup "Updating to a new IP was accepted"
  exit 0
else
  echo "$0: $error" 1>&2
  $LOGGER -p daemon.info -t dnsup "Updating to a new IP failed: $error"
  exit 1
fi

