I want to use lua in pdns-recursor to log the responses/answers

450 views Asked by At

I want to log the response to DNS queries at my pdns recursor to have resolved values at that time (for DynamicDNS etc). So I wrote a little LUA script that logs the answers and it works so far, but it would be nice to translate the type of the DNSRecord object as the string and not as the integer value. But as I am new to lua I have no idea how to do this.

My lua script is:

function postresolve(dq)
  pdnslog("postresolve called for "..dq.qname:toString())
  local records = dq:getRecords()
  for k,v in pairs(records) do
    pdnslog(k.." "..v.name:toString().." "..v.type.." "..v:getContent())
  end
  -- default, do not rewrite this response
  return false
end

And results look like:

Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 postresolve called for login.microsoftonline.com.
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 1 login.microsoftonline.com. 5 login.mso.msidentity.com.
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 2 login.mso.msidentity.com. 5 ak.privatelink.msidentity.com.
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 3 ak.privatelink.msidentity.com. 5 www.tm.ak.prd.aadg.akadns.net.
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 4 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.68
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 5 www.tm.ak.prd.aadg.akadns.net. 1 40.126.31.67
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 6 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.73
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 7 www.tm.ak.prd.aadg.akadns.net. 1 40.126.31.71
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 8 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.75
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 9 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.0
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 10 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.71
Jan 13 10:31:08 resolv-dns-2 pdns_recursor[12973]: Jan 13 09:31:08 11 www.tm.ak.prd.aadg.akadns.net. 1 20.190.159.4

It would be nice to have CNAME instead of 5 and A instead of 1 in the log. I am sure there is a function that translate it, but I cannot find it.

Thank you!

1

There are 1 answers

0
AliHZ On

why don't use recordTypeStrings? if there is no function for that you can define your Record Types in below dictionary and use that for generate logs.

local recordTypeStrings = {
    [1] = "A",
    [5] = "CNAME",
}

function postresolve(dq)
  pdnslog("postresolve called for "..dq.qname:toString())
  local records = dq:getRecords()
  for k,v in pairs(records) do
    local recordTypeString = recordTypeStrings[v.type] or tostring(v.type)
    pdnslog(k.." "..v.name:toString().." "..recordTypeString.." "..v:getContent())
  end
  -- default, do not rewrite this response
  return false
end