Skip to content

Instantly share code, notes, and snippets.

@jgworks
Last active October 14, 2016 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgworks/51e7032fb0ce4dd896405f4975e6841c to your computer and use it in GitHub Desktop.
Save jgworks/51e7032fb0ce4dd896405f4975e6841c to your computer and use it in GitHub Desktop.
lua script to filter out private A records and replace internal domain with external domain on powerdns slave
-- lua script to filter out private A records and replace internal domain with external domain on powerdns slave
-- sqlite> select * from domain;
-- 1|example.com|[u'10.0.0.1']|Slave|1476367424|0|1|0
-- sqlite> insert into domainmetadata values (1, 1, 'LUA-AXFR-SCRIPT', '/var/opt/pdns/axferfilter.lua');
pdnslog("loading axferfilter.lua")
ranges={
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}
function axfrfilter(remoteip, zone, qname, qtype, ttl, prio, content)
-- filter out any record that starts with a _
if qname:match("^_") == "_" then
return 0, {}
end
-- filter out any A records that match rfc1918 IPs
if qtype == pdns.A and matchnetmask(content, ranges) then
return 0, {}
end
if qtype == pdns.NS or qtype == pdns.SOA then
content = content:gsub("ldap", "ns")
end
content = string.lower(content)
content = content:gsub("int.local", "example.com")
resp = {}
resp[1] = {qname=qname, qtype=qtype, ttl=ttl, prio=prio, content=content}
-- pdnslog("axfrfilter: remoteip: ".. tostring(remoteip).." zone: "..tostring(zone).." qname: "..tostring(qname).." qtype: "..tostring(qtype).." ttl: "..tostring(ttl).." prio: "..tostring(prio).." content:" .. tostring(content))
return 0, resp
end
function string.starts(s, start)
return s.sub(s, 1, s.len(start)) == start
end
@clay584
Copy link

clay584 commented Oct 14, 2016

Looks great. You might consider doing string.lower(content) first in case somebody put some funny upper/lower case stuff in there, causing your gsub to not to match. This will also just convert it to lowercase before responding.

content = string.lower(content)
content = content:gsub("int.local", "example.com")

@jgworks
Copy link
Author

jgworks commented Oct 14, 2016

Good idea. The gsub for the capitalized version was for _kerberos TXT records, but I've decided to filter those out.

I've updated the script with those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment