#!/bin/sh
#
# Update the nameserver cache information file once per month.
# This is run automatically by a cron entry.
#
# Original by Al Longyear
# Updated for BIND 8 by Nicolai Langfeldt  MODIFIEE POUR BIND 9 sous DEBIAN Squeeze/Wheezy
# Miscelanious error-conditions reported by David A. Ranch
# Ping test suggested by Martin Foster
# named up-test suggested by Erik Bryer.
#
(
 echo "To: root+ns0@julienth37.tk"
 echo "From: bind9-ns0@julienth37.tk"

 # Is named up? Check the status of named.
 case `rndc status 2>&1` in
    *refused*)
        echo "named is DOWN. db.root was NOT updated"
        echo
        exit 0
        ;;
 esac

 PATH=/sbin:/usr/sbin:/bin:/usr/bin:
 export PATH
 # NOTE: /etc/bind must be writable only by trusted users or this script
 # will cause root compromise/denial of service opportunities.
 cd /etc/bind 2>/dev/null || {
    echo "Subject: Cannot cd to /etc/bind, error $?"
    echo
    echo "The subject says it all"
    exit 1
 }

 # Are we online?  Ping a server at your ISP
 case `ping -qnc 1 www.fdn.fr 2>&1` in
   *'100% packet loss'*)
        echo "Subject: db.root NOT updated.  The network is DOWN."
        echo
        echo "The subject says it all"
        exit 1
        ;;
 esac

 dig @f.root-servers.net . ns >db.root.new 2> errors

 case `cat db.root.new` in
   *NOERROR*)
        # It worked
        :;;
   *)
        echo "Subject: The db.root file update has FAILED."
        echo
        echo "The db.root update has failed"
        echo "This is the dig output reported:"
        echo
        cat db.root.new errors
        exit 1
        ;;
 esac

 echo "Subject: The db.root file has been updated"
 echo
 echo "The db.root file has been updated to contain the following information:"
 echo
 cat db.root.new

 chown root.root db.root.new
 chmod 444 db.root.new
 rm -f db.root.old errors
 mv db.root db.root.old
 mv db.root.new db.root
 rndc restart
 echo
 echo "The nameserver has been restarted to ensure that the update is complete."
 echo "The previous db.root file is now called /etc/bind/db.root.old."
) 2>&1 | /usr/lib/sendmail -t
exit 0
