From: Otto Moerbeek Date: Wed, 2 Mar 2022 08:57:30 +0000 (+0100) Subject: Backport of 11300 to rec-4.5.x: Use the Lua context stored in SyncRes when calling... X-Git-Tag: rec-4.5.9~5^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F11384%2Fhead;p=thirdparty%2Fpdns.git Backport of 11300 to rec-4.5.x: Use the Lua context stored in SyncRes when calling hooks --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 9925295f0a..d002cd1534 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -278,11 +278,11 @@ GlobalStateHolder g_dontThrottleNetmasks; //! used to send information to a newborn mthread struct DNSComboWriter { - DNSComboWriter(const std::string& query, const struct timeval& now): d_mdp(true, query), d_now(now), d_query(query) + DNSComboWriter(const std::string& query, const struct timeval& now, shared_ptr luaContext): d_mdp(true, query), d_now(now), d_query(query), d_luaContext(luaContext) { } - DNSComboWriter(const std::string& query, const struct timeval& now, std::unordered_set&& policyTags, LuaContext::LuaObject&& data, std::vector&& records): d_mdp(true, query), d_now(now), d_query(query), d_policyTags(std::move(policyTags)), d_records(std::move(records)), d_data(std::move(data)) + DNSComboWriter(const std::string& query, const struct timeval& now, std::unordered_set&& policyTags, shared_ptr luaContext, LuaContext::LuaObject&& data, std::vector&& records): d_mdp(true, query), d_now(now), d_query(query), d_policyTags(std::move(policyTags)), d_records(std::move(records)), d_luaContext(luaContext), d_data(std::move(data)) { } @@ -343,7 +343,11 @@ struct DNSComboWriter { std::unordered_set d_policyTags; std::string d_routingTag; std::vector d_records; + + // d_data is tied to this LuaContext so we need to keep it alive and use it, not a newer one, as long as d_data exists + shared_ptr d_luaContext; LuaContext::LuaObject d_data; + EDNSSubnetOpts d_ednssubnet; shared_ptr d_tcpConnection; boost::optional d_extendedErrorCode{boost::none}; @@ -1601,8 +1605,8 @@ static void startDoResolve(void *p) sr.setId(MT->getTid()); bool DNSSECOK=false; - if(t_pdl) { - sr.setLuaEngine(t_pdl); + if(dc->d_luaContext) { + sr.setLuaEngine(dc->d_luaContext); } if(g_dnssecmode != DNSSECMode::Off) { sr.setDoDNSSEC(true); @@ -1694,8 +1698,8 @@ static void startDoResolve(void *p) sr.setCacheOnly(); } - if (t_pdl) { - t_pdl->prerpz(dq, res); + if (dc->d_luaContext) { + dc->d_luaContext->prerpz(dq, res); } // Check if the client has a policy attached to it @@ -1742,7 +1746,7 @@ static void startDoResolve(void *p) } // if there is a RecursorLua active, and it 'took' the query in preResolve, we don't launch beginResolve - if (!t_pdl || !t_pdl->preresolve(dq, res)) { + if (!dc->d_luaContext || !dc->d_luaContext->preresolve(dq, res)) { if (!g_dns64PrefixReverse.empty() && dq.qtype == QType::PTR && dq.qname.isPartOf(g_dns64PrefixReverse)) { res = getFakePTRRecords(dq.qname, ret); @@ -1753,7 +1757,7 @@ static void startDoResolve(void *p) if (wantsRPZ && appliedPolicy.d_kind != DNSFilterEngine::PolicyKind::NoAction) { - if (t_pdl && t_pdl->policyHitEventFilter(dc->d_source, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_tcp, appliedPolicy, dc->d_policyTags, sr.d_discardedPolicies)) { + if (dc->d_luaContext && dc->d_luaContext->policyHitEventFilter(dc->d_source, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_tcp, appliedPolicy, dc->d_policyTags, sr.d_discardedPolicies)) { /* reset to no match */ appliedPolicy = DNSFilterEngine::Policy(); } @@ -1827,10 +1831,10 @@ static void startDoResolve(void *p) } } - if (t_pdl || (g_dns64Prefix && dq.qtype == QType::AAAA && !vStateIsBogus(dq.validationState))) { + if (dc->d_luaContext || (g_dns64Prefix && dq.qtype == QType::AAAA && !vStateIsBogus(dq.validationState))) { if (res == RCode::NoError) { if (answerIsNOData(dc->d_mdp.d_qtype, res, ret)) { - if (t_pdl && t_pdl->nodata(dq, res)) { + if (dc->d_luaContext && dc->d_luaContext->nodata(dq, res)) { shouldNotValidate = true; } else if (g_dns64Prefix && dq.qtype == QType::AAAA && !vStateIsBogus(dq.validationState)) { @@ -1839,11 +1843,11 @@ static void startDoResolve(void *p) } } } - else if (res == RCode::NXDomain && t_pdl && t_pdl->nxdomain(dq, res)) { + else if (res == RCode::NXDomain && dc->d_luaContext && dc->d_luaContext->nxdomain(dq, res)) { shouldNotValidate = true; } - if (t_pdl && t_pdl->postresolve(dq, res)) { + if (dc->d_luaContext && dc->d_luaContext->postresolve(dq, res)) { shouldNotValidate = true; auto policyResult = handlePolicyHit(appliedPolicy, dc, sr, res, ret, pw); // haveAnswer case redundant @@ -2576,7 +2580,7 @@ static void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var) conn->state = TCPConnection::BYTE0; std::unique_ptr dc; try { - dc=std::unique_ptr(new DNSComboWriter(conn->data, g_now)); + dc=std::unique_ptr(new DNSComboWriter(conn->data, g_now, t_pdl)); } catch(const MOADNSException &mde) { g_stats.clientParseError++; @@ -3019,7 +3023,7 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr return 0; } - auto dc = std::unique_ptr(new DNSComboWriter(question, g_now, std::move(policyTags), std::move(data), std::move(records))); + auto dc = std::unique_ptr(new DNSComboWriter(question, g_now, std::move(policyTags), t_pdl, std::move(data), std::move(records))); dc->setSocket(fd); dc->d_tag=ctag; dc->d_qhash=qhash;