#!/bin/sh
#
#
##############################################################################
#                                                                            #
# This program is free software; you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by       #
# the Free Software Foundation; either version 2 of the License, or          #
# (at your option) any later version. http://www.gnu.org/copyleft/gpl.html   #
# A copy of this license can be obtained from the Free Software              #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
##############################################################################
#                                                                            #
# Copyright (c) 2004  Joran Kvalvaag  jk@nerdworks.org                       #
#                                                                            #
##############################################################################
#                                                                            #
#                                                                            #
# This script is made for people using Enom Inc. (www.enom.com) as their     #
# registrar. It can be used to atomatically update your IP address in Enom's #
# DNS servers for people who want to run publically available servers while  #
# using a dynamically assigned IP address. Read more about this at:          #
# http://www.enom.com/help/faq_dynamicdns.asp                                #
#                                                                            #
# If you manage to make this script work with other registrars,              #
# please let me know, and I will include their URL here.                     #
#                                                                            #
# Your external (official) IP address can be obtained from a interface       #
# ie. eth0, ppp0, or from the Internet.                                      #
# You can run this script on any internal host as long as you get the        #
# IP address from the Internet.                                              #
#                                                                            #
# If you would like to add a new hostname to your existing domain,           #
# all you have to do is to add the new hostname to the list of hostnames     #
# used by this script (etc/hosteddomains). There is no need to log into      #
# your account at www.enom.com to add the new host.                          #
#                                                                            #
# To make MX work, first set your email settings to "User" in the            #
# "domain control panel" at www.enom.com.                                    #
# Next put ie. "mail.yourdomain.tld" as address in the "@(none)" row in the  #
# "Mail Host Record Setup" section in the "host settings" window.            #
# Then in the "Host Record Setup", make a row for the "mail" host with an    #
# "A(Address)" record type, and your current IP address.                     #
# This way you only need to update the IP address for                        #
# the "mail" host to get the email routing right.                            #
#                                                                            #
# This script requires that all the hosts you want to update the IP address  #
# for are present in the file /etc/hosteddomains.                            #
# This file must not have any comments or empty lines in it,                 #
# and there must be one hostname on each line. See example below:            #
# www.yourdomain1.tld                                                        #
# ftp.yourdomain1.tld                                                        #
# mail.yourdomain1.tld                                                       #
# www.yourdomain2.tld                                                        #
# mail.yourdomain2.tld                                                       #
# www.yourdomain3.tld                                                        #
# ftp.yourdomain3.tld                                                        #
#                                                                            #
# To update the "@(none)" and "*(other)" fields in the                       #
# "host setting window" at www.enom.com, add the following lines to          #
# /etc/hosteddomains.                                                        #
# @.yourdomain.tld                                                           #
# *.yourdomain.tld                                                           #
#                                                                            #
# If you are using Apache configured for "Mass virtual hosting", you can     #
# use a simple shellscript for updating the /etc/hosteddomains file.         #
# If the VirtualDocumentRoot in Apache is configured like                    #
# /var/www/%-1/%-2/%-3/htdocs                                                #
# a shellscript like the following will update your list of hosts,           #
# including the @ and * records.                                             #
#                                                                            #
# #!/bin/sh                                                                  #
# cd /var/www/                                                               #
# find net com org -type -d maxdepth 2 -mindepth 2 | \                       #
# awk -F '/' '{ print $3"."$2"."$1 }' > /etc/hosteddomains.tmp               #
# find net com org -type -d maxdepth 1 -mindepth 1 | \                       #
# awk -F '/' '{ print @"."$2"."$1 }' >> /etc/hosteddomains.tmp               #
# find net com org -type -d maxdepth 1 -mindepth 1 | \                       #
# awk -F '/' '{ print *"."$2"."$1 }' >> /etc/hosteddomains.tmp               #
# cd /etc                                                                    #
# diff hosteddomains.tmp hosteddomains > /dev/null                           #
# if [ $? -ne 0 ]; then echo 0 > /tmp/.currenip; fi                          #
# cat hosteddomains.tmp hosteddomains                                        #
# rm hosteddomains                                                           #
# # End script                                                               #
#                                                                            #
# The "if" line will force dyniupdate to update if a new host is added.      #
#                                                                            #
# You can of course include the commands above in this script to make        #
# new hosts added and updated automatically in DNS.                          #
#                                                                            #
# You must also set a domain password for your domain(s). This script        #
# currently supports only one password for all domains. If one common        #
# password for all your domains is unacceptable, make a separate version     #
# of this script for each domain.                                            #
#                                                                            #
# Comments and suggestions are very welcome.                                 #
#                                                                            #
##############################################################################
#                                                                            #
# INSALLATION AND USAGE                                                      #
#                                                                            #
# Name this file "dynipupdate", and put it in /usr/sbin/.                    #
# Make this file executable, and readable only by root (contains the         #
# domain password) with the commands                                         #
# "chown root.root /usr/sbin/dynipupdate"                                    #
# "chmod 700 /usr/sbin/dynipupdate"                                          #
#                                                                            #
# To run this script every 5 minutes,                                        #
# add a line like the next to roots crontab.                                 #
# */5 * * * * /usr/sbin/dynipupdate                                          #
#                                                                            #
# You should also put a line like the next one in your rc.local file.        #
# /usr/sbin/dynipupdate                                                      #
#                                                                            #
# To force an update, ie. after adding a new entry to /etc/hosteddomains,    #
# run this script with the -f option.                                        #
#                                                                            #
# Wget is requierd for this script to work.                                  #
#                                                                            #
##############################################################################
#
# USER VARIABLES
#
#
# If something goes wrong, a email can be sent to the administrator.
# This requires that a working sendmail executable is present on the system.
# Leave blank if no mail should be sent.
#
#ADMINMAIL=""
#ADMINMAIL="you@yourdomain.com"
ADMINMAIL="root"

# Path to the sendmail executable
MAIL="/usr/lib/sendmail"


# From here are we going to get your external IP address?
# this script must reside on the computer with your external IP,
# if you choose a interface.
# WEB is recomended
#
#IPSOURCE=eth0
#IPSOURCE=eth1
#IPSOURCE=ppp0
IPSOURCE=WEB


# If you are using the WEB option above to obtain your external IP address, we
# need an URL to a webpage showing your external IP address. Please note that
# currently this file can contain only the IP address.
# You may set up your own external webpage for this purpose, and point the
# EXTIPURL variable to that page. This page must reside on a webserver with
# SSI enabled. This webpage should only have one line of code:
# <!--#echo var="REMOTE_ADDR" -->
# An example file can be found at http://home.no.net/aves2/ip.shtml.txt
# Rename this file to ip.shtml
#
#EXTIPURL="http://www.SomeURLNotOnYourOwnNet.tld/ip.shtml"
#EXTIPURL="http://www.nerdworks.org/ip.shtml"
EXTIPURL="http://home.no.net/aves2/ip.shtml"


# Logging
LOGFILE="/var/log/dynipupdate.log"


# Temporary file for storing current IP address
CURRFILE="/tmp/.currentip"


# Temporary file for storing results from a update
RESULT="/tmp/.updateresult"


# File containing all of your hostnames
HOSTEDFILE="/etc/hosteddomains"


# Domain password
DOMPASS="YourDomainPassword"


# UpdateURL
UU="http://dynamic.name-services.com/interface.asp"

# End of user variables
#
##############################################################################

DATE=`date +%b-%d-%T`

if [ $UID -ne 0 ]; then
    echo "You must be root to run this script!"
#   echo "The administrator has been notified about your attempt."
#   (echo "From: dynipupdate@`hostname -f`"
#   echo "Subject: ALERT Attempted illegal use of script"
#   echo "TO: $ADMINMAIL"
#   echo ""
#   echo "A user with UID $UID tried to run the dynipupdate script at $DATE."
#   ) | $MAIL -t
#   exit 1
fi


if [ -f $CURRFILE ]; then
    CURRIP=`cat $CURRFILE`
else
    touch $CURRFILE
fi

if [[ -n $1 && $1 == "-f" ]]; then
    CURRIP=0
fi

if [ $IPSOURCE != WEB ]; then
    EXTIP=`/sbin/ifconfig ppp0 |awk '/inet addr/{split($2,x,":"); print x[2]}'`
else
    EXTIP=`wget -q -O - $EXTIPURL`
fi

if [ $? -ne 0 ]; then
    (echo "From: dynipupdate@`hostname -f`"
    echo "Subject: ALERT Failed to complete IP update!"
    echo "TO: $ADMINMAIL"
    echo ""
    echo "Sorry, unable to get the external IP address."
    ) | $MAIL -t
    echo "$DATE  Unable to get the external IP address." >> $LOGFILE
    exit 1
else
    if [ $CURRIP != $EXTIP ]; then
        for HOST in `cat $HOSTEDFILE` ; do
            CMD="command=setdnshost"
            DPASS="domainpassword=$DOMPASS"
            wget -q -O $RESULT "$UU?$CMD&zone=$HOST&address=$EXTIP&$DPASS"
            if [ ! `grep 'ErrCount=0' $RESULT` ]; then
                (echo "From: dynipupdate@`hostname -f`"
                echo "Subject: ALERT Error updating DNS info!"
                echo "TO: $ADMINMAIL"
                echo ""
                echo "There was a error when updating DNS info for $HOST."
                echo "Here is the response from the registrar:"
                echo ""
                echo "`cat $RESULT`"
                echo ""
                ) | $MAIL -t
                echo "$DATE  Error when updating DNS info." >> $LOGFILE
                exit 1
            fi
        done
        echo "$DATE  IP address updated from $CURRIP to $EXTIP" >> $LOGFILE
    fi
    echo -n $EXTIP > $CURRFILE
fi

# End