TinyDNS djbdns

I know there are others, but this is mine.   I always forget how-to change the default installation path with his software, until of course it’s too late and then I get to re-do it.

Need daemontools, ucspi-tcp, and djbdns source code.

 

Create a working directory

$ mkdir ~/working  && cd ~/working

Grab src code

$ wget http://cr.yp.to/djbdns/djbdns-1.05.tar.gz

$ wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
$ wget http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz

1).  First install daemontools, now if you have never installed anything from David Bernstein, his approach, may seem a little different than what you are normally used to with ./configure; make; make install.  There might be a easy approach to moving this somewhere else but I have found that when I do..it sometimes comes back to bite me.  So for daemontools, I leave it defaulted even thought it still drives me nuts everytime..

Create a /package directory:

We use TinyDNS at work.  I will admit when I started working at this company, I didn’t much care for TinyDNS.  However, the more I read up on it, and the more I actually witness the performance of this daemon.  I am always walking away with the same thought…”Wow, the creator of TinyDNS is just brilliant”  in reference to David J Bernstein. (djbdns).

 

Ease of use: BIND versus djbdns

Problem: Set up an external cache on 1.2.3.4 for clients in the 1.2.3.* network. BIND solution:

  1. Create a localhost zone file, with an SOA record, an NS record, and an A record.
  2. Create a 0.0.127.in-addr.arpa zone file, with an SOA record, an NS record, and a PTR record.
  3. Create a root.hints zone file with the root server IP addresses.
  4. Create a named.conf file: zone "localhost" in { type master; file "localhost"; }; zone "0.0.127.in-addr.arpa" in { type master; file "0.0.127.in-addr.arpa"; }; zone "." in { type hint; file "root.hints"; }; options { interface-interval 0; listen-on { 1.2.3.4; }; allow-query { 1.2.3/24; }; };
  5. Start named.
  6. Make sure that your boot scripts start named.
djbdns solution:

  1. Create Gdnscache and Gdnslog accounts.
  2. dnscache-conf Gdnscache Gdnslog /etc/dnscache 1.2.3.4
  3. ln -s /etc/dnscache /service
  4. cd /service/dnscache
  5. touch root/ip/1.2.3
Problem: Also allow queries from clients in the 1.5.* network. BIND solution:

  1. Edit named.conf.
  2. Add 1.5/16 to the allow-query section.
  3. Send a HUP signal to named.
  4. Look for errors in your system’s logs. The DNS and BIND” book says, correctly, that you have to do this, because otherwise you might never notice” syntax errors in the configuration files.
djbdns solution:

  1. cd /service/dnscache
  2. touch root/ip/1.5
Problem: Run the cache non-root and chrooted. BIND solution:

  1. Create a Gbindin account.
  2. mkdir /etc/bindin
  3. Copy various system-dependent files, which are not thoroughly described in the BIND manual, into /etc/bindin.
  4. Kill named.
  5. Start named -u Gbindin -t /etc/bindin.
  6. Change the named line in your boot scripts accordingly.
djbdns solution: Relax. It’s already non-root and chrooted.
Problem: Arrange for the cache to be restarted if someone accidentally kills it. BIND solution: Write a script that looks for the named process, restarts named if it is not running, pauses for a second, and repeats. Start that script. djbdns solution: Relax. The cache is already monitored.
Problem: Tell the cache to consult an internal server at IP address 10.1.2.5 for information about moon.x.mil. BIND solution:

  1. Edit named.conf.
  2. Add a forwarding zone: zone "moon.x.mil" in { type forward; forward only; forwarders { 10.1.2.5; }; };
  3. Send a HUP signal to named.
  4. Look for errors in your system’s logs.
djbdns solution:

  1. cd /service/dnscache
  2. echo 10.1.2.5 > root/servers/moon.x.mil
  3. svc -t .
Problem: Tell the cache to follow a new delegation from the internal server to 10.1.2.6 for d.moon.x.mil. BIND solution:

  1. Edit named.conf.
  2. Add another forwarding zone: zone "d.moon.x.mil" in { type forward; forward only; forwarders { 10.1.2.6; }; };
  3. Send a HUP signal to named.
  4. Look for errors in your system’s logs.
djbdns solution: Relax. The cache follows delegations.
Problem: Set up a server on 1.2.3.5 to publish information about x.mil and 1.2.3.*. BIND solution:

  1. Create an x.mil zone file, with an SOA record and an NS record.
  2. Create a 3.2.1.in-addr.arpa zone file, with an SOA record and an NS record.
  3. Create a named.conf file: zone "x.mil" in { type master; file "x.mil"; }; zone "3.2.1.in-addr.arpa" in { type master; file "3.2.1.in-addr.arpa"; }; options { interface-interval 0; listen-on { 1.2.3.5; }; recursion no; fetch-glue no; allow-transfer { none; }; };
  4. Start named.
  5. Make sure that your boot scripts start named.
djbdns solution:

  1. Create Gtinydns and Gdnslog accounts.
  2. tinydns-conf Gtinydns Gdnslog /etc/tinydns 1.2.3.5
  3. ln -s /etc/tinydns /service
  4. cd /service/tinydns/root
  5. ./add-ns x.mil 1.2.3.5
  6. ./add-ns 3.2.1.in-addr.arpa 1.2.3.5
  7. make
Problem: Run the server non-root and chrooted. BIND solution:

  1. Create a Gbindout account.
  2. mkdir /etc/bindout
  3. Copy various system-dependent files, which are not thoroughly described in the BIND manual, into /etc/bindout.
  4. Kill named.
  5. Start named -u Gbindout -t /etc/bindout.
  6. Change the named line in your boot scripts accordingly.
djbdns solution: Relax. The server, like the cache, is already non-root and chrooted.
Problem: Add a host, lion.x.mil, with address 1.2.3.4. BIND solution:

  1. Edit the x.mil zone.
  2. Add lion A 1.2.3.4.
  3. Change the serial number in the SOA record.
  4. Edit the 3.2.1.in-addr.arpa zone.
  5. Add 4 PTR lion.x.mil.
  6. Change the serial number in the SOA record.
  7. Send a HUP signal to named.
  8. Look for errors in your system’s logs. As noted above: The DNS and BIND” book says, correctly, that you have to do this, because otherwise you might never notice” syntax errors in the configuration files.

Oops! Did I forget the dot on lion.x.mil? As the DNS and BIND” book says: It’s very easy to leave off trailing dots.”

djbdns solution:

  1. cd /service/tinydns/root
  2. ./add-host lion.x.mil 1.2.3.4
  3. make

You don’t have to check your system’s logs: if there are any syntax errors, disk-write errors, etc., they’ll appear on your screen in response to make. Anyway, the add-host program never introduces any syntax errors.

Problem: Avoid accidentally reusing a previous host name, or reusing a previous host IP address. BIND solution: Manually search the zone file for the new host name and the new IP address. You won’t have to look far if you sort your forward zones by name and sort your reverse zones by address. djbdns solution: Relax. add-host handles this automatically. (If you want to reuse a name or address, use add-alias.)
Problem: Avoid destroying a zone if there’s trouble saving new data: e.g., not enough disk space, or a sudden power outage. BIND solution:

  1. Copy the zone file.
  2. Edit the copy.
  3. Sync the copy to disk.
  4. Rename the copy.
djbdns solution: Relax. add-host handles this automatically.
Problem: Make sure there are no syntax errors in new data added by hand. BIND solution: Watch your system’s logs for error messages. Some messages mean that named is no longer providing information about that zone, and users will begin seeing failures in a matter of seconds; in this case, panic. djbdns solution: If there is a syntax error, make will print an error message immediately, and tinydns will continue providing the old data.
Problem: Send incoming mail for x.mil to a mail server at 1.2.3.4. BIND solution:

  1. Edit the x.mil zone.
  2. Add @ MX 0 lion.
  3. Change the serial number in the SOA record.
  4. Send a HUP signal to named.
  5. Look for errors in your system’s logs.
djbdns solution:

  1. cd /service/tinydns/root
  2. ./add-mx x.mil 1.2.3.4
  3. make
Problem: Delegate elysium.x.mil to a DNS server at 1.2.3.144. BIND solution:

  1. Edit the x.mil zone.
  2. Add elysium NS a.ns.elysium.
  3. Add a.ns.elysium A 1.2.3.144.
  4. Change the serial number in the SOA record.
  5. Send a HUP signal to named.
  6. Look for errors in your system’s logs.
djbdns solution:

  1. cd /service/tinydns/root
  2. ./add-childns elysium.x.mil 1.2.3.144
  3. make
Problem: Replicate DNS service from 1.2.3.5 to 1.2.3.6. BIND solution: On 1.2.3.5:

  1. Edit named.conf.
  2. Insert 1.2.3.6; into the allow-transfer section.
  3. Edit the x.mil zone file.
  4. Add an NS record pointing to 1.2.3.6.
  5. Edit the 3.2.1.in-addr.arpa zone file.
  6. Add an NS record pointing to 1.2.3.6.
  7. Send a HUP signal to named.
  8. Look for errors in your system’s logs.

On 1.2.3.6:

  1. Create a named.conf file: zone "x.mil" in { type slave; file "x.mil"; masters { 1.2.3.5; }; }; zone "3.2.1.in-addr.arpa" in { type slave; file "3.2.1.in-addr.arpa"; masters { 1.2.3.5; }; }; options { interface-interval 0; listen-on { 1.2.3.5; }; recursion no; fetch-glue no; allow-transfer { none; }; };
  2. Start named.
  3. Make sure that your boot scripts start named.
djbdns solution: On 1.2.3.6:

  1. Create Gtinydns and Gdnslog accounts.
  2. tinydns-conf Gtinydns Gdnslog /etc/tinydns 1.2.3.6
  3. ln -s /etc/tinydns /service

On 1.2.3.5:

  1. cd /service/tinydns/root
  2. At the top of Makefile, add a target remote: data.cdb, with command rsync -az -e ssh data.cdb 1.2.3.6:/ service/ tinydns/ root/ data.cdb. (No spaces after the slashes.)
  3. ./add-ns x.mil 1.2.3.6
  4. ./add-ns 3.2.1.in-addr.arpa 1.2.3.6
  5. make
Problem: Replicate a newly created zone. BIND solution:

  1. Edit named.conf.
  2. Add a new type slave line.
  3. Send a HUP signal to named.
  4. Look for errors in your system’s logs.
djbdns solution: Relax. New zones are automatically replicated.

# cat /etc/init.d/tinydns
#!/bin/sh
#
# tinydns Authoritative DNS server
#
# chkconfig: 345 11 89
# description: Dan Bernstein (DJB) authoritative dns server
#

# See how we were called.
case “$1” in
start)

echo -n “Starting: ”
env – PATH=”/var/qmail/bin:/usr/local/bin” IP=192.168.2.226 \
ROOT=/var/tinydns/root envuidgid tinydns softlimit -d300000 \
/usr/local/bin/tinydns 2>&1 | /usr/local/bin/multilog t /var/tinydns/log/226 &
# ROOT=/var/tinydns/root envuidgid tinydns softlimit -d300000 \
# /usr/local/bin/tinydns 2> /dev/null &
env – PATH=”/var/qmail/bin:/usr/local/bin” IP=192.168.2.227 \
ROOT=/var/tinydns/root envuidgid tinydns softlimit -d300000 \
/usr/local/bin/tinydns 2>&1 | /usr/local/bin/multilog t /var/tinydns/log/227 &
# ROOT=/var/tinydns/root envuidgid tinydns softlimit -d300000 \
# /usr/local/bin/tinydns 2> /dev/null &

echo “tinydns”
;;
stop)
killall tinydns
;;
restart)
$0 stop
$0 start
;;
*)
echo “Usage: tinydns {start|stop|restart}”
exit 1
esac

exit 0

Here is the djbdns dnscache Log File formats, copied directly from http://dqd.com/~mayoff/notes/djbdns/dnscache-log.html

dnscache Log File Format
by Rob Mayoff

These notes are incomplete.

dnscache is part of the djbdns package, written by Daniel J. Bernstein, aka djb. I couldn’t find any documentation on its log file format, other than this explanation of one field of the stats log entry. This file contains my notes on what the log entries mean. If there are any errors here, they are mine and not djb’s.

dnscache logs IP addresses as 8 digit hexadecimal strings. For example, 127.0.0.1 is logged as 7f000001.

dnscache logs UDP ports and query IDs as 4 digit hexadecimal strings.

dnscache logs all time intervals (including TTLs) as decimal strings, in units of seconds.

dnscache logs record types numerically. For a list of record types, see RFC 1700, page 79. The common ones are 1 = A, 12 = PTR, and 15 = MX.

RFC 1035 specifies the implementation of DNS.

You can find some programs for analyzing dnscache logs at http://www.fibrespeed.net/~mbabcock/code/.

Log entry types:

cached type name
cached cname name cname
cached ns control server
cached nxdomain name
drop serial error
lame serverip name control
nodata serverip ttl type name
nxdomain serverip ttl name
query serial clientip:clientport:id type name
rr serverip ttl type name data
rr serverip ttl cname name cname
rr serverip ttl mx name preference exchanger
rr serverip ttl ns name server
rr serverip ttl ptr name pname
rr serverip ttl soa server email serial refresh retry expire minimum
sent serial length
servfail name error
starting
stats query-count cache-motion udp-active tcp-active
tcpopen clientip:clientport
tcpclose clientip:clientport error
tx gluelessness type name control serverips…

Descriptions

cached type name

dnscache needs some records and found them in the cache. It may have needed the records because the client requested them, or it may have needed the addresses of a name server in order to look up some other records.

The actual cached data is not recorded with this log entry. The cached data may include several records, but dnscache makes only one log entry.

Field Meaning
type The type of records needed.
name The domain name for which records were needed.

cached cname name cname

dnscache found the answer to a client query in its cache, and the answer was a CNAME record. In this case, dnscache starts over, looking for the same record type but with the “canonical name”.

Field Meaning
name The domain name for which the client wants records.
cname The “canonical name” for name. meaning that name should be treated as an alias for cname.

cached ns control server

dnscache needed to know the authoritative nameservers for some domain, and found a set of nameservers for the domain, or some ancestor of it, in the cache. dnscache creates one log entry for each nameserver in the set. The actual name for which dnscache needed to find nameservers is on the query log entry preceding the set of cached ns log entries.

For example:

query 673 7f000001:09b6:7c48 1 www.windows.com.
cached ns com. a.root-servers.net.
cached ns com. e.gtld-servers.net.
cached ns com. f.gtld-servers.net.
cached ns com. j.gtld-servers.net.
cached ns com. k.gtld-servers.net.
cached ns com. a.gtld-servers.net.
cached ns com. m.gtld-servers.net.
cached ns com. g.gtld-servers.net.
cached ns com. c.gtld-servers.net.
cached ns com. i.gtld-servers.net.
cached ns com. b.gtld-servers.net.
cached ns com. d.gtld-servers.net.

dnscache needed to know the authoritative nameservers for www.windows.com, and the nearest set of nameservers in its cache was the set of nameservers that are authoritative for com.

Field Meaning
control The domain name for which server is authoritative.
server The name of a server that is authoritative for control.

cached nxdomain name

dnscache needed to find records for name and found a cached nxdomain entry in the cache.

Field Meaning
name The domain name for which records were requested.

drop serial error

dnscache decided not to try to respond to a client query.

Field Meaning
serial The serial number of the client request. See query for an explanation of client request serial numbers.
error The reason dnscache dropped the request:

timed out
dnscache had MAXUDP (200) active UDP queries and received another UDP query. It dropped the oldest active query.
permission denied
dnscache received an AXFR request.
permission denied
dnscache received an AXFR request.
out of memory
dnscache could not allocate memory for parsing a query packet or building a response.
XXX more errors…

lame serverip name control

dnscache found a lame delegation. This means that the server is supposed to be authoritative for some domain, but isn’t.

Field Meaning
serverip The IP address of the lame server.
name The domain name for which records were requested.
control The domain for which the server is supposed to be authoritative, but isn’t.

nodata serverip ttl type name

dnscache received a “no data” response. This means that the server has records for the requested name, but no records of the requested type.

Field Meaning
serverip The IP address of the responding server.
ttl The time-to-live of the SOA record in the response. This is how long dnscache is allowed to cache the negative response. dnscache will not cache a negative response for more than one hour in any case.
type The requested record type.
name The domain name for which records were requested.

nxdomain serverip ttl name

dnscache received a “Name Error” response. This means that the server has no records of any type for the requested name.

Field Meaning
serverip The IP address of the responding server.
ttl The time-to-live of the SOA record in the response. This is how long dnscache is allowed to cache the negative response. dnscache will not cache a negative response for more than one hour in any case.
name The domain name for which records were requested.

query serial clientip:clientport:id type name

dnscache received a packet containing a query and intends to try to answer it.

Field Meaning
serial The number of queries dnscache received prior to this query since starting, plus one. In other words, serial number 1 is assigned to the first query received, serial number 2 is assigned to the second query received, and so on. The counter is stored using 64 bits, so chances of it wrapping are unlikely.
clientip The source IP address of the packet. Presumably this is the IP address from which the packet was sent, though it could have been spoofed.
clientport The source UDP port of the packet.
id The id from the packet. The id is chosen by the client, and the server will include it in the response.
type The type of records the client wants.
name The domain name for which the client wants records.

rr XXX

sent serial length

dnscache finished constructing a response to a query. If the query came over UDP, then dnscache also sent the response. If the query came over TCP, then dnscache did not send the response before making this log entry. (Sending over TCP may block so dnscache trickles the data out as part of its main loop.)

Field Meaning
serial The serial number of the client request to which dnscache responded. See query for an explanation of client request serial numbers.
length The number of bytes in the response.

servfail name error

dnscache sent a packet with rcode 2, “Server failure”, because it encountered an error.

Some of the errors that can make dnscache do this:

failure to allocate storage for a received DNS packet
failure to create a UDP socket
failure to set the O_NONBLOCK flag on the UDP socket
failure to bind the UDP socket to a port
failure to transmit a packet to any of up to 16 nameservers and receive a response packet with an rcode of 0 (no error) or 3 (NXDOMAIN), with four attempts per nameserver
failure to create a TCP socket
failure to set the O_NONBLOCK flag on the TCP socket
failure to bind the TCP socket to a port
failure to connect the TCP socket to any of up to 16 nameservers (one attempt per nameserver), transmit a query to the nameserver, and receive a response packet with an rcode of 0 (no error) or 3 (NXDOMAIN)

There may be other ways for dnscache to log/send servfail, but these are all the ones I have found from inspecting the source code.

Field Meaning
name The domain name for which the dnscache was trying to find records.
error As of djbdns version 1.05, the error message will always be “input/output error”, because the only call to log_servfail is in doit in query.c, like this:

errno = error_io;
if (state == 1) goto HAVEPACKET;
if (state == -1) {
log_servfail(z->name[z->level]);
goto SERVFAIL;
}

starting

dnscache logs this entry when it starts up.

stats query-count cache-motion udp-active tcp-active

This entry contains statistics about dnscache’s behavior, both since startup and at the moment the entry was logged.

Field Meaning
query-count The total number of queries received by dnscache since startup.
cache-motion The total number of bytes dnscache has stored in its cache since startup. This says nothing about the maximum size of the cache or how much data has been evicted from the cache. See djb’s explanation of cache motion for more information.
udp-active The number of queries that dnscache has received via UDP but not yet responded to or dropped.
tcp-active The number of queries that dnscache has received via TCP but not yet responded to or dropped.

tx gluelessness type name control serverips…

This line indicates that dnscache transmitted a query.

Field Meaning
gluelessness The amount of gluelessness that generated this query.

Read djb’s explanation of gluelessness.

For the case of www.monty.de, the queries dnscache sent for www.monty.de have gluelessness 0. The query sent for ns.norplex.net has gluelessness 1. The query for vserver.neptun11.de has gluelessness 2. The query sent for ns.germany.net has gluelessness 3. And so on.
type The requested record type.
name The domain name for which records are being requested.
control dnscache sends a query to a server because it has been told that the server is authoritative for the domain in question, or some ancestor thereof. The control field shows the domain for which dnscache thinks the server is authoritative.

For example, suppose the cache is empty, and you ask dnscache for the A records for example.com. First, dnscache will send a query to a root server, because the root server is authoritative for the root domain (written “.”). So the tx line for the query will have “.” in the control field.

The root server will give dnscache a list of servers that are authoritative for the com domain. dnscache will ask one of the com servers for the A records for example.com, and the log entry will have com. in the control field.

Suppose the com server says that ns.example.net is authoritative for example.com. Then when dnscache asks ns.example.net for A records for example.com, the log entry will have example.com in the control field.
serverips The IP addresses of all the authoritative servers for the control domain, in random order. dnscache transmits the query to the first server in the list. If the server doesn’t respond, dnscache moves on to the next server in the list.

(The blank space above allows the tx named anchor to appear at the top of your window.)