Friday, March 17, 2017

PowerDNS PIPE backend api python example



We recently needed a pipe backend to return a CNAME record for all requests on powerdns 3, so I started reading the docs and developing the backend. So:

yum install pdns pdns-backend-pipe -y

vi /etc/pdns/pdns.conf 
 
launch=bind,pipe
pipe-command=/etc/pdns/pdns-backend.py


Here is my final script (Thanks to Jan Spike for his blog post):

vi /etc/pdns/pdns-backend.py

#!/usr/bin/python -u

from sys import stdin, stdout
data = stdin.readline()
stdout.write("OK\tMy Backend\n")
stdout.flush()

cname='cname-value.com'

while True:
    data = stdin.readline().strip()
    kind, qname, qclass, qtype, id, ip = data.split('\t')
    if kind == 'Q' and qname not in cname:
 r="DATA\t"+qname+"\t"+qclass+"\t"+"SOA\t86400\t-1\tsupport"+qname+" ns1.domain.org 2008080300 1800 3600 604800 3600\n"
        if qtype == 'ANY' or qtype == 'CNAME':
            r += "DATA\t"+qname+"\t"+qclass+"\tCNAME\t86400\t"+id+"\t"+cname+"\n"
        stdout.write(r)
    stdout.write("END\n")
    stdout.flush() 
 
 

No comments:

Post a Comment