#!/bin/sh

# configdns.sh Version 1.1
# Philip Brown
# http://www.bolthole.com/solaris/
#
# This is technically for solaris, but 
# it will probably work for OTHER OS's as well!!
# (no guarantees though ;-)
#
# run with  "sh configdns.sh", or chmod 0755 and run directly
#
# It will then set up a DNS server runing on your local UNIX box. 
# What this does is make your own box do DNS queries directly to the
# nameservers out there. It means never again being dependant on your
# ISP's nameservers, because you have your own.

echo This script will automatically configure your machine to run a
echo DNS server. It will completely destroy /etc/resolv.conf, and
echo either /etc/named.boot, or /etc/named.conf, as appropriate
echo It may also modify /etc/nsswitch.conf if neccessary.
echo "Continue? (y/n)"

read ans

if [ "$ans" != "y" -a "$ans" != "Y" ] ; then
	echo Quitting
	exit 0
fi

OSREV=`uname -r`

case OSREV in
     5.3|5.4|5.5|5.6)
     	NAMEDCF=/etc/named.boot
	;;
     *)
     	NAMEDCF=/etc/named.conf
	;;
esac
	

if [ -f /etc/resolv.conf ] ; then
	cp /etc/resolv.conf /etc/resolv.conf.pre-config
	echo copied /etc/resolv.conf to /etc/resolv.conf.pre-config
	DOMAIN=`grep domain /etc/resolv.conf | awk '{print $2}'`
fi
if [ "$DOMAIN" = "" ] ; then
	DOMAIN=`domainname`
fi


if [ "$DOMAIN" != "" ] ; then
	echo domain $DOMAIN >/etc/resolv.conf
else
	echo >/etc/resolv.conf
fi
echo nameserver 127.0.0.1 >>/etc/resolv.conf

echo Made /etc/resolv.conf

############################################################
#dump the old-style config file to stdout.
# It belongs in /etc/named.boot
############################################################
oldnamedcf(){
cat <<EOF
; This is the config file for an old BIND4.9.1 name demon
directory       /etc/named
primary         0.0.127.in-addr.arpa    named.local
cache           .                       named.cache
EOF
}

############################################################
#dump the new-style config file to stdout.
# It belongs in /etc/named.conf
############################################################
namedcf()
{
cat <<EOF
options {
		directory "/etc/named";
# use 127.0.0.1 if you are an isolated machine. Otherwise, either
# comment out the "listen-on" line entirely, or supply the appropriate
# internal or external address
		listen-on { 127.0.0.1; };
	};
	zone "." in {
                type hint;
                file "named.cache";
	};

	zone "0.0.127.in-addr.arpa" in {
		type master;
		file "named.local";
	};

EOF
}

############################################################
# This is the list of all "root" namesservers.
# Or in this case, enough of them for you to be okay
############################################################
dumpcache()
{
cat <<EOF
;The latest version of this file can always be found at
; ftp://FTP.RS.INTERNIC.NET/domain/named.root
;
.                        3600000  IN  NS    A.ROOT-SERVERS.NET.
A.ROOT-SERVERS.NET.      3600000      A     198.41.0.4
.                        3600000      NS    B.ROOT-SERVERS.NET.
B.ROOT-SERVERS.NET.      3600000      A     128.9.0.107
.                        3600000      NS    C.ROOT-SERVERS.NET.
C.ROOT-SERVERS.NET.      3600000      A     192.33.4.12
.                        3600000      NS    D.ROOT-SERVERS.NET.
D.ROOT-SERVERS.NET.      3600000      A     128.8.10.90
.                        3600000      NS    E.ROOT-SERVERS.NET.
E.ROOT-SERVERS.NET.      3600000      A     192.203.230.10
.                        3600000      NS    F.ROOT-SERVERS.NET.
F.ROOT-SERVERS.NET.      3600000      A     192.5.5.241

EOF
}

############################################################
# This just makes the reverse-lookup for "localhost" work
# Put in named.local
############################################################
dumplocal()
{
cat <<EOF
;
;       Loopback to convert 127.0.0.1 to localhost
;
@                       IN      SOA     localhost. root.localhost. (
                                        94112201    ; serial (yymmddxx)
                                        86400       ; refresh every day
                                        3600        ; retry every 1 hour
                                        3600000     ; expire after 1000 hours
                                        3600000     ; default ttl is 1000 hours
                                        )
;
;       Name Server
;
                        IN      NS      localhost
1			IN      PTR     localhost.
localhost.		IN      A       127.0.0.1

EOF
}

# Your /etc/nsswitch.conf may need tweaking. This routine takes
# care of the details
modswitch(){
	hosts=`grep '^hosts:' /etc/nsswitch.conf`
	hasdns=`echo $hosts | grep 'hosts:.*dns'`
	notfoundcheck=`echo $hasdns | grep NOTFOUND`
	hasnis=`echo $hosts | grep nisplus`
	if [ "$hasnis" != "" ] ; then
		echo ""
		echo "****WARNING****: your nsswitch.conf has nisplus for hosts"
		echo "please add 'dns' by hand to the hosts line"
		echo "In my opinion, best placement is"
		echo "hosts: files nisplus dns"
		echo ""
		return
	fi
	if [ "$hasdns" != "" -a "$notfoundcheck" = "" ] ; then
		echo nsswitch.conf already okay
		return
	fi
	sed 's/\(hosts:.*\)/hosts: files dns/' /etc/nsswitch.conf >/tmp/ns.$$
	mv /tmp/ns.$$ /etc/nsswitch.conf
	echo /etc/nswitch.conf updated
}

mkdir /etc/named
dumplocal >/etc/named/named.local
dumpcache >/etc/named/named.cache

if [ -f $NAMEDCF -a ! -f $NAMEDCF.bak ] ; then
	cp $NAMEDCF $NAMEDCF.bak
	echo copied $NAMEDCF to $NAMEDCF.bak
fi

if [ "$NAMEDCF" = "/etc/named.boot" ] ; then
	oldnamedcf >$NAMEDCF
else
	namedcf >$NAMEDCF
fi

modswitch

echo Restarting namedemon

kill `cat /etc/named.pid` 2>/dev/null
sleep 1

/usr/sbin/in.named

echo 'As soon as your internet link is up, you should be set to go!'
if [ "$NAMEDCF" = "/etc/named.conf" ] ; then
 echo 'Please note: This script assumes you are setting up a single isolated'
 echo 'machine. If this machine needs to allow others to query DNS through it,'
 echo 'please read the comments in /etc/named.conf'
fi
echo ''
echo "Philip Brown phil@bolthole.com"
echo "http://www.bolthole.com/solaris/"

