random walk through fields of musings

Wednesday, October 9, 2013

auto-generating IPv6 PTR DNS records with PowerDNS recursor in Lua

Pre-generating PTR records for dynamically-assigned SLAAC IPv6 is fairly unreasonable and silly because most of the time there won't be a match. However, if you do want to do that, and you runPowerDNS (which you should pick because it has working Lua scripting support to do this), you can generate them when the query hits quite easily:
function preresolve ( remoteip, domain, qtype )
  -- for a v6 only connected site, don't resolve A records
  if matchnetmask( remoteip, "2000::/3") then
    -- only rewrite PTR requests
    if (qtype == pdns.PTR) then
      -- and then only in our block(s)
      qv6 = string.find(domain, "d.f.8.6.8.a.8.3.0.0.2.ip6.arpa") 
      if not qv6 then return -1, {} end
      ret={}
      revdom="reverse-ip.mylab.tld.";
      rev= string.reverse( string.gsub( string.gsub(domain, "%.ip6%.arpa", ""), "%.", "") ) .. "." .. revdom;
      --  pdnslog("returning " .. rev .. " for " .. domain);
      ret[1]= {qtype=pdns.PTR, content=rev, ttl=86400}
      return 0, ret
    end
  end
end