Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pdns/recursordist/docs/lua-scripting/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ Interception Functions

:attr:`dq.isTCP <DNSQuestion.isTcp>` can be set to force TCP or UDP

.. versionchanged:: 5.4.0

:attr:`remoteaddr <DNSQuestion.remoteaddr>` can be set to override the remote server

This hook is not called in response to a client packet, but fires when the Recursor wants to talk to an authoritative server.

When this hook sets the special result code ``-3``, the whole DNS client query causing this outgoing query gets a ``ServFail``.
Expand All @@ -197,7 +201,7 @@ Interception Functions

In the case of :func:`preoutquery`, only a few attributes if the :class:`dq <DNSQuestion>` object are filled in:

- :attr:`dq.remoteaddr <DNSQuestion.remoteaddr>` containing the target nameserver address
- :attr:`dq.remoteaddr <DNSQuestion.remoteaddr>` containing the target nameserver address; since version 5.4.0 this attribute may be overridden by this hook
- :attr:`dq.localaddr <DNSQuestion.localaddr>`
- :attr:`dq.qname <DNSQuestion.qname>`
- :attr:`dq.qtype <DNSQuestion.qtype>`
Expand Down
8 changes: 5 additions & 3 deletions pdns/recursordist/lua-recursor4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void RecursorLua4::postPrepareContext() // NOLINT(readability-function-cognitive
d_lw->registerMember<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion& dnsQuestion) -> uint16_t { return dnsQuestion.qtype; }, [](DNSQuestion& /* dnsQuestion */, uint16_t newType) { (void) newType; });
d_lw->registerMember<bool (DNSQuestion::*)>("isTcp", [](const DNSQuestion& dnsQuestion) -> bool { return dnsQuestion.isTcp; }, [](DNSQuestion& dnsQuestion, bool newTcp) { dnsQuestion.isTcp = newTcp; });
d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion& dnsQuestion) -> const ComboAddress& { return dnsQuestion.local; }, [](DNSQuestion& /* dnsQuestion */, const ComboAddress& newLocal) { (void) newLocal; });
d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion& dnsQuestion) -> const ComboAddress& { return dnsQuestion.remote; }, [](DNSQuestion& /* dnsQuestion */, const ComboAddress& newRemote) { (void) newRemote; });
d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion& dnsQuestion) -> const ComboAddress& { return dnsQuestion.remote; }, [](DNSQuestion& dnsQuestion, const ComboAddress& newRemote) { dnsQuestion.remote = newRemote; });
d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("interface_localaddr", [](const DNSQuestion& dnsQuestion) -> const ComboAddress& { return dnsQuestion.interface_local; }, [](DNSQuestion& /* dnsQuestion */, const ComboAddress& newLocal) { (void) newLocal; });
d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("interface_remoteaddr", [](const DNSQuestion& dnsQuestion) -> const ComboAddress& { return dnsQuestion.interface_remote; }, [](DNSQuestion& /* dnsQuestion */, const ComboAddress& newRemote) { (void) newRemote; });
d_lw->registerMember<uint8_t (DNSQuestion::*)>("validationState", [](const DNSQuestion& dnsQuestion) -> uint8_t { return (vStateIsBogus(dnsQuestion.validationState) ? /* in order not to break older scripts */ static_cast<uint8_t>(255) : static_cast<uint8_t>(dnsQuestion.validationState)); }, [](DNSQuestion& /* dnsQuestion */, uint8_t newState) { (void) newState; });
Expand Down Expand Up @@ -659,7 +659,7 @@ bool RecursorLua4::postresolve(DNSQuestion& dnsQuestion, int& ret, RecEventTrace
return isOK;
}

bool RecursorLua4::preoutquery(const ComboAddress& nameserver, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool& isTcp, vector<DNSRecord>& res, int& ret, RecEventTrace& eventTrace, const struct timeval& theTime) const
bool RecursorLua4::preoutquery(ComboAddress& nameserver, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool& isTcp, vector<DNSRecord>& res, int& ret, RecEventTrace& eventTrace, const struct timeval& theTime) const
{
if (!d_preoutquery) {
return false;
Expand All @@ -668,13 +668,15 @@ bool RecursorLua4::preoutquery(const ComboAddress& nameserver, const ComboAddres
bool wantsRPZ = false;
bool logQuery = false;
bool addPaddingToResponse = false;
RecursorLua4::DNSQuestion dnsQuestion(nameserver, requestor, nameserver, requestor, query, qtype.getCode(), isTcp, variableAnswer, wantsRPZ, logQuery, addPaddingToResponse, theTime);
ComboAddress remote = nameserver;
RecursorLua4::DNSQuestion dnsQuestion(nameserver, requestor, remote, requestor, query, qtype.getCode(), isTcp, variableAnswer, wantsRPZ, logQuery, addPaddingToResponse, theTime);
dnsQuestion.currentRecords = &res;
auto match = eventTrace.add(RecEventTrace::LuaPreOutQuery);
bool isOK = genhook(d_preoutquery, dnsQuestion, ret);
eventTrace.add(RecEventTrace::LuaPreOutQuery, isOK, false, match);
warnDrop(dnsQuestion);

nameserver = remote;
isTcp = dnsQuestion.isTcp;

return isOK;
Expand Down
6 changes: 3 additions & 3 deletions pdns/recursordist/lua-recursor4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public:
struct DNSQuestion
{
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
DNSQuestion(const ComboAddress& prem, const ComboAddress& ploc, const ComboAddress& rem, const ComboAddress& loc, const DNSName& query, uint16_t type, bool tcp, bool& variable_, bool& wantsRPZ_, bool& logResponse_, bool& addPaddingToResponse_, const struct timeval& queryTime_) :
DNSQuestion(const ComboAddress& prem, const ComboAddress& ploc, ComboAddress& rem, const ComboAddress& loc, const DNSName& query, uint16_t type, bool tcp, bool& variable_, bool& wantsRPZ_, bool& logResponse_, bool& addPaddingToResponse_, const struct timeval& queryTime_) :
qname(query), interface_local(ploc), interface_remote(prem), local(loc), remote(rem), variable(variable_), wantsRPZ(wantsRPZ_), logResponse(logResponse_), addPaddingToResponse(addPaddingToResponse_), queryTime(queryTime_), qtype(type), isTcp(tcp)
{
}
const DNSName& qname;
const ComboAddress& interface_local;
const ComboAddress& interface_remote;
const ComboAddress& local;
const ComboAddress& remote;
ComboAddress& remote;
const ComboAddress* fromAuthIP{nullptr};
const struct dnsheader* dh{nullptr};
const std::vector<pair<uint16_t, string>>* ednsOptions{nullptr};
Expand Down Expand Up @@ -215,7 +215,7 @@ public:
bool nodata(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
bool postresolve(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;

bool preoutquery(const ComboAddress& nameserver, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool& isTcp, vector<DNSRecord>& res, int& ret, RecEventTrace& eventTrace, const struct timeval& theTime) const;
bool preoutquery(ComboAddress& nameserver, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool& isTcp, vector<DNSRecord>& res, int& ret, RecEventTrace& eventTrace, const struct timeval& theTime) const;
bool ipfilter(const ComboAddress& remote, const ComboAddress& local, const struct dnsheader&, RecEventTrace&) const;

bool policyHitEventFilter(const ComboAddress& remote, const DNSName& qname, const QType& qtype, bool tcp, DNSFilterEngine::Policy& policy, std::unordered_set<std::string>& tags, std::unordered_map<std::string, bool>& discardedPolicies) const;
Expand Down
5 changes: 4 additions & 1 deletion pdns/recursordist/syncres.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5436,7 +5436,7 @@ void SyncRes::checkTotalTime(const DNSName& qname, QType qtype, boost::optional<
}
}

bool SyncRes::doResolveAtThisIP(const std::string& prefix, const DNSName& qname, const QType qtype, LWResult& lwr, boost::optional<Netmask>& ednsmask, const DNSName& auth, bool const sendRDQuery, const bool wasForwarded, const DNSName& nsName, const ComboAddress& remoteIP, bool doTCP, bool doDoT, bool& truncated, bool& spoofed, boost::optional<EDNSExtendedError>& extendedError, bool dontThrottle)
bool SyncRes::doResolveAtThisIP(const std::string& prefix, const DNSName& qname, const QType qtype, LWResult& lwr, boost::optional<Netmask>& ednsmask, const DNSName& auth, bool const sendRDQuery, const bool wasForwarded, const DNSName& nsName, ComboAddress& remoteIP, bool doTCP, bool doDoT, bool& truncated, bool& spoofed, boost::optional<EDNSExtendedError>& extendedError, bool dontThrottle)
{
checkTotalTime(qname, qtype, extendedError);

Expand All @@ -5448,6 +5448,9 @@ bool SyncRes::doResolveAtThisIP(const std::string& prefix, const DNSName& qname,
LOG(prefix << qname << ": Query handled by Lua" << endl);
}
else {
if (doTCP && SyncRes::s_dot_to_port_853 && remoteIP.getPort() == 853) {
doDoT = true;
}
ednsmask = getEDNSSubnetMask(qname, remoteIP);
if (ednsmask) {
LOG(prefix << qname << ": Adding EDNS Client Subnet Mask " << ednsmask->toString() << " to query" << endl);
Expand Down
2 changes: 1 addition & 1 deletion pdns/recursordist/syncres.hh
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ private:
void ednsStats(boost::optional<Netmask>& ednsmask, const DNSName& qname, const string& prefix);
void incTimeoutStats(const ComboAddress& remoteIP);
void checkTotalTime(const DNSName& qname, QType qtype, boost::optional<EDNSExtendedError>& extendedError) const;
bool doResolveAtThisIP(const std::string& prefix, const DNSName& qname, QType qtype, LWResult& lwr, boost::optional<Netmask>& ednsmask, const DNSName& auth, bool sendRDQuery, bool wasForwarded, const DNSName& nsName, const ComboAddress& remoteIP, bool doTCP, bool doDoT, bool& truncated, bool& spoofed, boost::optional<EDNSExtendedError>& extendedError, bool dontThrottle = false);
bool doResolveAtThisIP(const std::string& prefix, const DNSName& qname, QType qtype, LWResult& lwr, boost::optional<Netmask>& ednsmask, const DNSName& auth, bool sendRDQuery, bool wasForwarded, const DNSName& nsName, ComboAddress& remoteIP, bool doTCP, bool doDoT, bool& truncated, bool& spoofed, boost::optional<EDNSExtendedError>& extendedError, bool dontThrottle = false);
bool processAnswer(unsigned int depth, const string& prefix, LWResult& lwr, const DNSName& qname, QType qtype, DNSName& auth, bool wasForwarded, const boost::optional<Netmask>& ednsmask, bool sendRDQuery, NsSet& nameservers, std::vector<DNSRecord>& ret, const DNSFilterEngine& dfe, bool* gotNewServers, int* rcode, vState& state, const ComboAddress& remoteIP);

int doResolve(const DNSName& qname, QType qtype, vector<DNSRecord>& ret, unsigned int depth, set<GetBestNSAnswer>& beenthere, Context& context);
Expand Down
2 changes: 1 addition & 1 deletion pdns/recursordist/test-syncres_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void BaseLua4::getFeatures(Features& /* features */)
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool RecursorLua4::preoutquery(const ComboAddress& /* ns */, const ComboAddress& /* requestor */, const DNSName& /* query */, const QType& /* qtype */, bool& /* isTcp */, vector<DNSRecord>& /* res */, int& /* ret */, RecEventTrace& /* et */, const struct timeval& /* tv */) const
bool RecursorLua4::preoutquery(ComboAddress& /* ns */, const ComboAddress& /* requestor */, const DNSName& /* query */, const QType& /* qtype */, bool& /* isTcp */, vector<DNSRecord>& /* res */, int& /* ret */, RecEventTrace& /* et */, const struct timeval& /* tv */) const
{
return false;
}
Expand Down
Loading