• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 18743945403

23 Oct 2025 09:29AM UTC coverage: 65.845% (+0.02%) from 65.829%
18743945403

Pull #16356

github

web-flow
Merge 8a2027ef1 into efa3637e8
Pull Request #16356: auth 5.0: backport "pdnsutil: fix b2b-migrate to from sql to non-sql"

42073 of 92452 branches covered (45.51%)

Branch coverage included in aggregate %.

128008 of 165855 relevant lines covered (77.18%)

6379935.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/pdns/recursordist/lua-recursor4.hh
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#pragma once
23

24
#include "config.h"
25

26
#include "iputils.hh"
27
#include "dnsname.hh"
28
#include "namespaces.hh"
29
#include "filterpo.hh"
30
#include "ednsoptions.hh"
31
#include "validate.hh"
32
#include "lua-base4.hh"
33
#include "proxy-protocol.hh"
34
#include "rec-eventtrace.hh"
35

36
#include <unordered_map>
37

38
#include "lua-recursor4-ffi.hh"
39

40
// pdns_ffi_param_t is a lightuserdata
41
template <>
42
struct LuaContext::Pusher<pdns_ffi_param*>
43
{
44
  static const int minSize = 1;
45
  static const int maxSize = 1;
46

47
  static PushedObject push(lua_State* state, pdns_ffi_param* ptr) noexcept
48
  {
65✔
49
    lua_pushlightuserdata(state, ptr);
65✔
50
    return PushedObject{state, 1};
65✔
51
  }
65✔
52
};
53

54
// pdns_postresolve_ffi_handle is a lightuserdata
55
template <>
56
struct LuaContext::Pusher<pdns_postresolve_ffi_handle*>
57
{
58
  static const int minSize = 1;
59
  static const int maxSize = 1;
60

61
  static PushedObject push(lua_State* state, pdns_postresolve_ffi_handle* ptr) noexcept
62
  {
7✔
63
    lua_pushlightuserdata(state, ptr);
7✔
64
    return PushedObject{state, 1};
7✔
65
  }
7✔
66
};
67

68
class RecursorLua4 : public BaseLua4
69
{
70
public:
71
  RecursorLua4(const std::string& includePath = "") :
72
    BaseLua4(includePath)
73
  {
125✔
74
    prepareContext();
125✔
75
  };
125✔
76
  RecursorLua4(const RecursorLua4&) = delete;
77
  RecursorLua4(RecursorLua4&&) = delete;
78
  RecursorLua4& operator=(const RecursorLua4&) = delete;
79
  RecursorLua4& operator=(RecursorLua4&&) = delete;
80
  ~RecursorLua4() override; // this is so unique_ptr works with an incomplete type
81

82
  struct MetaValue
83
  {
84
    std::unordered_set<std::string> stringVal;
85
    std::unordered_set<int64_t> intVal;
86
  };
87
  struct DNSQuestion
88
  {
89
    // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
90
    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_) :
91
      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)
92
    {
3,169✔
93
    }
3,169✔
94
    const DNSName& qname;
95
    const ComboAddress& interface_local;
96
    const ComboAddress& interface_remote;
97
    const ComboAddress& local;
98
    const ComboAddress& remote;
99
    const ComboAddress* fromAuthIP{nullptr};
100
    const struct dnsheader* dh{nullptr};
101
    const std::vector<pair<uint16_t, string>>* ednsOptions{nullptr};
102
    const uint16_t* ednsFlags{nullptr};
103
    vector<DNSRecord>* currentRecords{nullptr};
104
    DNSFilterEngine::Policy* appliedPolicy{nullptr};
105
    std::unordered_set<std::string>* policyTags{nullptr};
106
    const std::vector<ProxyProtocolValue>* proxyProtocolValues{nullptr};
107
    std::unordered_map<std::string, bool>* discardedPolicies{nullptr};
108
    std::string* extendedErrorExtra{nullptr};
109
    boost::optional<uint16_t>* extendedErrorCode{nullptr};
110
    std::string requestorId;
111
    std::string deviceId;
112
    std::string deviceName;
113
    bool& variable;
114
    bool& wantsRPZ;
115
    bool& logResponse;
116
    bool& addPaddingToResponse;
117
    std::map<std::string, MetaValue> meta;
118
    struct timeval queryTime;
119

120
    vector<DNSRecord> records;
121

122
    string followupFunction;
123
    string followupPrefix;
124

125
    string udpQuery;
126
    string udpAnswer;
127
    string udpCallback;
128

129
    LuaContext::LuaObject data;
130
    DNSName followupName;
131
    ComboAddress udpQueryDest;
132

133
    unsigned int tag{0};
134
    int rcode{0};
135
    const uint16_t qtype;
136
    bool isTcp;
137
    vState validationState{vState::Indeterminate};
138

139
    void addAnswer(uint16_t type, const std::string& content, boost::optional<int> ttl, boost::optional<string> name);
140
    void addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional<int> ttl, boost::optional<string> name);
141
    [[nodiscard]] vector<pair<int, DNSRecord>> getRecords() const;
142
    [[nodiscard]] boost::optional<dnsheader> getDH() const;
143
    [[nodiscard]] vector<pair<uint16_t, string>> getEDNSOptions() const;
144
    [[nodiscard]] boost::optional<string> getEDNSOption(uint16_t code) const;
145
    [[nodiscard]] boost::optional<Netmask> getEDNSSubnet() const;
146
    [[nodiscard]] std::vector<std::pair<int, ProxyProtocolValue>> getProxyProtocolValues() const;
147
    [[nodiscard]] vector<string> getEDNSFlags() const;
148
    [[nodiscard]] bool getEDNSFlag(const string& flag) const;
149
    void setRecords(const vector<pair<int, DNSRecord>>& arg);
150
  };
151

152
  struct PolicyEvent
153
  {
154
    PolicyEvent(const ComboAddress& rem, const DNSName& name, const QType& type, bool tcp) :
155
      qname(name), qtype(type), remote(rem), isTcp(tcp)
156
    {
2✔
157
    }
2✔
158
    const DNSName& qname;
159
    const QType qtype;
160
    const ComboAddress& remote;
161
    const bool isTcp;
162
    DNSFilterEngine::Policy* appliedPolicy{nullptr};
163
    std::unordered_set<std::string>* policyTags{nullptr};
164
    std::unordered_map<std::string, bool>* discardedPolicies{nullptr};
165
  };
166

167
  struct FFIParams
168
  {
169
  public:
170
    // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
171
    FFIParams(const DNSName& qname_, uint16_t qtype_, const ComboAddress& plocal_, const ComboAddress& premote_, const ComboAddress& local_, const ComboAddress& remote_, const Netmask& ednssubnet_, LuaContext::LuaObject& data_, std::unordered_set<std::string>& policyTags_, std::vector<DNSRecord>& records_, const EDNSOptionViewMap& ednsOptions_, const std::vector<ProxyProtocolValue>& proxyProtocolValues_, std::string& requestorId_, std::string& deviceId_, std::string& deviceName_, std::string& routingTag_, boost::optional<int>& rcode_, uint32_t& ttlCap_, bool& variable_, bool tcp_, bool& logQuery_, bool& logResponse_, bool& followCNAMERecords_, boost::optional<uint16_t>& extendedErrorCode_, std::string& extendedErrorExtra_, bool& disablePadding_, std::map<std::string, MetaValue>& meta_) :
172
      data(data_), qname(qname_), interface_local(plocal_), interface_remote(premote_), local(local_), remote(remote_), ednssubnet(ednssubnet_), policyTags(policyTags_), records(records_), ednsOptions(ednsOptions_), proxyProtocolValues(proxyProtocolValues_), requestorId(requestorId_), deviceId(deviceId_), deviceName(deviceName_), routingTag(routingTag_), extendedErrorExtra(extendedErrorExtra_), rcode(rcode_), extendedErrorCode(extendedErrorCode_), ttlCap(ttlCap_), variable(variable_), logQuery(logQuery_), logResponse(logResponse_), followCNAMERecords(followCNAMERecords_), disablePadding(disablePadding_), qtype(qtype_), tcp(tcp_), meta(meta_)
173
    {
65✔
174
    }
65✔
175

176
    LuaContext::LuaObject& data;
177
    const DNSName& qname;
178
    const ComboAddress& interface_local;
179
    const ComboAddress& interface_remote;
180
    const ComboAddress& local;
181
    const ComboAddress& remote;
182
    const Netmask& ednssubnet;
183
    std::unordered_set<std::string>& policyTags;
184
    std::vector<DNSRecord>& records;
185
    const EDNSOptionViewMap& ednsOptions;
186
    const std::vector<ProxyProtocolValue>& proxyProtocolValues;
187
    std::string& requestorId;
188
    std::string& deviceId;
189
    std::string& deviceName;
190
    std::string& routingTag;
191
    std::string& extendedErrorExtra;
192
    boost::optional<int>& rcode;
193
    boost::optional<uint16_t>& extendedErrorCode;
194
    uint32_t& ttlCap;
195
    bool& variable;
196
    bool& logQuery;
197
    bool& logResponse;
198
    bool& followCNAMERecords;
199
    bool& disablePadding;
200

201
    unsigned int tag{0};
202
    uint16_t qtype;
203
    bool tcp;
204

205
    std::map<std::string, MetaValue>& meta;
206
  };
207

208
  unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::unordered_set<std::string>* policyTags, LuaContext::LuaObject& data, const EDNSOptionViewMap&, bool tcp, std::string& requestorId, std::string& deviceId, std::string& deviceName, std::string& routingTag, const std::vector<ProxyProtocolValue>& proxyProtocolValues) const;
209
  unsigned int gettag_ffi(FFIParams&) const;
210

211
  void maintenance() const;
212
  bool prerpz(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
213
  bool preresolve(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
214
  bool nxdomain(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
215
  bool nodata(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
216
  bool postresolve(DNSQuestion& dnsQuestion, int& ret, RecEventTrace&) const;
217

218
  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;
219
  bool ipfilter(const ComboAddress& remote, const ComboAddress& local, const struct dnsheader&, RecEventTrace&) const;
220

221
  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;
222

223
  void runStartStopFunction(const std::string& script, bool start, Logr::log_t log);
224

225
  [[nodiscard]] bool needDQ() const
226
  {
×
227
    return (d_prerpz || d_preresolve || d_nxdomain || d_nodata || d_postresolve);
×
228
  }
×
229

230
  struct PostResolveFFIHandle
231
  {
232
    PostResolveFFIHandle(DNSQuestion& dnsQuestion) :
233
      d_dq(dnsQuestion)
234
    {
7✔
235
    }
7✔
236
    DNSQuestion& d_dq;
237
    bool d_ret{false};
238
  };
239
  bool postresolve_ffi(PostResolveFFIHandle&) const;
240

241
  [[nodiscard]] bool hasGettagFunc() const
242
  {
461✔
243
    return static_cast<bool>(d_gettag);
461✔
244
  }
461✔
245
  [[nodiscard]] bool hasGettagFFIFunc() const
246
  {
524✔
247
    return static_cast<bool>(d_gettag_ffi);
524✔
248
  }
524✔
249
  [[nodiscard]] bool hasPostResolveFFIfunc() const
250
  {
325✔
251
    return static_cast<bool>(d_postresolve_ffi);
325✔
252
  }
325✔
253

254
protected:
255
  void postPrepareContext() override;
256
  void postLoad() override;
257
  void getFeatures(Features& features) override;
258

259
private:
260
  using gettag_t = std::function<std::tuple<unsigned int, boost::optional<std::unordered_map<int, string>>, boost::optional<LuaContext::LuaObject>, boost::optional<std::string>, boost::optional<std::string>, boost::optional<std::string>, boost::optional<string>>(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const EDNSOptionViewMap&, bool, const std::vector<std::pair<int, const ProxyProtocolValue*>>&)>;
261
  gettag_t d_gettag; // public so you can query if we have this hooked
262

263
  using gettag_ffi_t = std::function<boost::optional<LuaContext::LuaObject>(pdns_ffi_param_t*)>;
264
  gettag_ffi_t d_gettag_ffi;
265

266
  using postresolve_ffi_t = std::function<bool(pdns_postresolve_ffi_handle_t*)>;
267
  postresolve_ffi_t d_postresolve_ffi;
268

269
  using luamaintenance_t = std::function<void()>;
270
  luamaintenance_t d_maintenance;
271

272
  using luacall_t = std::function<bool(DNSQuestion*)>;
273
  luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
274
  bool genhook(const luacall_t& func, DNSQuestion& dnsQuestion, int& ret) const;
275

276
  using ipfilter_t = std::function<bool(ComboAddress, ComboAddress, struct dnsheader)>;
277
  ipfilter_t d_ipfilter;
278

279
  using policyEventFilter_t = std::function<bool(PolicyEvent&)>;
280
  policyEventFilter_t d_policyHitEventFilter;
281
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc