(Power)DNS fun
Filed under DNS, Python on may 05, 2009
For some time now I have implemented PowerDNS authoritative and recursing nameservers on my servers. Amongst other cool features such as MySQL support, IPv6 recursion and Super Masters, you can develop your own pipe backend for PowerDNS. The idea is really simple: receive a DNS request over STDIN, and reply over STDOUT.
A basic working example:
import os
import sys
line = sys.stdin.readline().strip()
if not line.startswith('HELO'):
print 'FAIL'
sys.exit(1)
else:
print 'OK\t%s ready' % (os.path.basename(sys.argv[0]),)
while True:
line = sys.stdin.readline().strip()
if not line:
break
request = line.split('\t')
if len(request) < 6:
print 'LOG\tPowerDNS sent unparsable line'
print 'FAIL'
continue
kind, qname, qclass, qtype, qid, ip = request
if qtype in ['A', 'ANY']:
if qname == 'whoami.freecode.nl':
print 'DATA\t%s\t%s\tA\t%d\t-1\t%s' % \
(qname, qclass, TTL, ip)
Configuration in PowerDNS‘ pdns.conf:
cache-ttl=0 query-cache-ttl=0 launch=pipe:info pipe-info-command=/etc/powerdns/pipe/info.py pipe-info-timeout=500
This will respond to an A lookup on whoami.freecode.nl with the IP of the nameserver that sent the request. You can see a live demo on the whoami.freecode.nl subdomain:
wijnand@pound:~$ dig +short a whoami.freecode.nl 195.95.198.142
I have implemented a few other query types:
wijnand@pound:~$ dig +short aaaa whoami.freecode.nl 2001:7b8:34c::1 wijnand@pound:~$ dig +short txt whoami.freecode.nl | head ;; Truncated, retrying in TCP mode. "mnt-routes: PELICAN-NL-MNT" "route: 195.95.198.0/24" "The Netherlands"
Post your feedback
You can use this form to leave your feedback. Your insights are always appreciated.