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

PowerDNS / pdns / 18102068545

29 Sep 2025 03:22PM UTC coverage: 66.092% (+0.01%) from 66.079%
18102068545

push

github

web-flow
Merge pull request #16153 from rgacogne/ddist-docs-eol

dnsdist: Simplify EOL page

42552 of 93124 branches covered (45.69%)

Branch coverage included in aggregate %.

129278 of 166864 relevant lines covered (77.48%)

4926623.09 hits per line

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

59.17
/modules/pipebackend/pipebackend.cc
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
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include <string>
26
#include <map>
27
#include <unistd.h>
28
#include <stdlib.h>
29
#include <sstream>
30
#include "coprocess.hh"
31

32
#include "pdns/namespaces.hh"
33

34
#include "pdns/dns.hh"
35
#include "pdns/dnsbackend.hh"
36
#include "pdns/dnspacket.hh"
37
#include "pdns/pdnsexception.hh"
38
#include "pdns/logger.hh"
39
#include "pdns/arguments.hh"
40
#include <sys/socket.h>
41
#include <netinet/in.h>
42
#include <arpa/inet.h>
43
#include "pipebackend.hh"
44

45
// The following requirement guarantees UnknownDomainID will get output as "-1"
46
// for compatibility.
47
static_assert(std::is_signed<domainid_t>::value);
48

49
static const char* kBackendId = "[PIPEBackend]";
50

51
CoWrapper::CoWrapper(const string& command, int timeout, int abiVersion)
52
{
10✔
53
  d_command = command;
10✔
54
  d_timeout = timeout;
10✔
55
  d_abiVersion = abiVersion;
10✔
56
  launch(); // let exceptions fall through - if initial launch fails, we want to die
10✔
57
  // I think
58
}
10✔
59

60
CoWrapper::~CoWrapper() = default;
2✔
61

62
void CoWrapper::launch()
63
{
54✔
64
  if (d_cp)
54✔
65
    return;
44✔
66

67
  if (d_command.empty())
10!
68
    throw ArgException("pipe-command is not specified");
×
69

70
  if (isUnixSocket(d_command)) {
10!
71
    d_cp = std::make_unique<UnixRemote>(d_command);
×
72
  }
×
73
  else {
10✔
74
    auto coprocess = std::make_unique<CoProcess>(d_command, d_timeout);
10✔
75
    coprocess->launch();
10✔
76
    d_cp = std::move(coprocess);
10✔
77
  }
10✔
78

79
  d_cp->send("HELO\t" + std::to_string(d_abiVersion));
10✔
80
  string banner;
10✔
81
  d_cp->receive(banner);
10✔
82
  g_log << Logger::Error << "Backend launched with banner: " << banner << endl;
10✔
83
}
10✔
84

85
void CoWrapper::send(const string& line)
86
{
17✔
87
  launch();
17✔
88
  try {
17✔
89
    d_cp->send(line);
17✔
90
    return;
17✔
91
  }
17✔
92
  catch (PDNSException& ae) {
17✔
93
    d_cp.reset();
×
94
    throw;
×
95
  }
×
96
}
17✔
97
void CoWrapper::receive(string& line)
98
{
27✔
99
  launch();
27✔
100
  try {
27✔
101
    d_cp->receive(line);
27✔
102
    return;
27✔
103
  }
27✔
104
  catch (PDNSException& ae) {
27✔
105
    g_log << Logger::Warning << kBackendId << " Unable to receive data from coprocess. " << ae.reason << endl;
×
106
    d_cp.reset();
×
107
    throw;
×
108
  }
×
109
}
27✔
110

111
PipeBackend::PipeBackend(const string& suffix)
112
{
10✔
113
  d_disavow = false;
10✔
114
  setArgPrefix("pipe" + suffix);
10✔
115
  try {
10✔
116
    launch();
10✔
117
  }
10✔
118
  catch (const ArgException& A) {
10✔
119
    g_log << Logger::Error << kBackendId << " Unable to launch, fatal argument error: " << A.reason << endl;
×
120
    throw;
×
121
  }
×
122
  catch (...) {
10✔
123
    throw;
×
124
  }
×
125
}
10✔
126

127
void PipeBackend::launch()
128
{
54✔
129
  if (d_coproc)
54✔
130
    return;
44✔
131

132
  try {
10✔
133
    if (!getArg("regex").empty()) {
10!
134
      d_regex = std::make_unique<Regex>(getArg("regex"));
×
135
    }
×
136
    d_regexstr = getArg("regex");
10✔
137
    d_abiVersion = getArgAsNum("abi-version");
10✔
138
    d_coproc = std::make_unique<CoWrapper>(getArg("command"), getArgAsNum("timeout"), getArgAsNum("abi-version"));
10✔
139
  }
10✔
140

141
  catch (const ArgException& A) {
10✔
142
    cleanup();
×
143
    throw;
×
144
  }
×
145
}
10✔
146

147
/*
148
 * Cleans up the co-process wrapper
149
 */
150
void PipeBackend::cleanup()
151
{
2✔
152
  d_coproc.reset(nullptr);
2✔
153
  d_regex.reset();
2✔
154
  d_regexstr = string();
2✔
155
  d_abiVersion = 0;
2✔
156
}
2✔
157

158
void PipeBackend::lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* pkt_p)
159
{
17✔
160
  try {
17✔
161
    launch();
17✔
162
    d_disavow = false;
17✔
163
    if (d_regex && !d_regex->match(qname.toStringRootDot())) {
17!
164
      if (::arg().mustDo("query-logging"))
×
165
        g_log << Logger::Error << "Query for '" << qname << "' failed regex '" << d_regexstr << "'" << endl;
×
166
      d_disavow = true; // don't pass to backend
×
167
    }
×
168
    else {
17✔
169
      ostringstream query;
17✔
170
      string localIP = "0.0.0.0";
17✔
171
      string remoteIP = "0.0.0.0";
17✔
172
      Netmask realRemote("0.0.0.0/0");
17✔
173
      if (pkt_p) {
17✔
174
        localIP = pkt_p->getLocal().toString();
8✔
175
        realRemote = pkt_p->getRealRemote();
8✔
176
        remoteIP = pkt_p->getInnerRemote().toString();
8✔
177
      }
8✔
178
      // abi-version = 1
179
      // type    qname           qclass  qtype   id      remote-ip-address
180
      query << "Q\t" << qname.toStringRootDot() << "\tIN\t" << qtype.toString() << "\t" << zoneId << "\t" << remoteIP;
17✔
181

182
      // add the local-ip-address if abi-version is set to 2
183
      if (d_abiVersion >= 2)
17✔
184
        query << "\t" << localIP;
13✔
185
      if (d_abiVersion >= 3)
17✔
186
        query << "\t" << realRemote.toString();
13✔
187

188
      if (::arg().mustDo("query-logging"))
17!
189
        g_log << Logger::Error << "Query: '" << query.str() << "'" << endl;
×
190
      d_coproc->send(query.str());
17✔
191
    }
17✔
192
  }
17✔
193
  catch (PDNSException& pe) {
17✔
194
    g_log << Logger::Error << kBackendId << " Error from coprocess: " << pe.reason << endl;
×
195
    d_disavow = true;
×
196
  }
×
197
  d_qtype = qtype;
17✔
198
  d_qname = qname;
17✔
199
}
17✔
200

201
bool PipeBackend::list(const ZoneName& target, domainid_t domain_id, bool /* include_disabled */)
202
{
×
203
  try {
×
204
    launch();
×
205
    d_disavow = false;
×
206
    ostringstream query;
×
207
    // The question format:
208

209
    // type    qname           qclass  qtype   id      ip-address
210
    if (d_abiVersion >= 4)
×
211
      query << "AXFR\t" << domain_id << "\t" << target.toStringRootDot();
×
212
    else
×
213
      query << "AXFR\t" << domain_id;
×
214

215
    d_coproc->send(query.str());
×
216
  }
×
217
  catch (PDNSException& ae) {
×
218
    g_log << Logger::Error << kBackendId << " Error from coprocess: " << ae.reason << endl;
×
219
  }
×
220
  d_qname = DNSName(std::to_string(domain_id)); // why do we store a number here??
×
221
  return true;
×
222
}
×
223

224
string PipeBackend::directBackendCmd(const string& query)
225
{
×
226
  if (d_abiVersion < 5)
×
227
    return "not supported on ABI version " + std::to_string(d_abiVersion) + " (use ABI version 5 or later)\n";
×
228

229
  try {
×
230
    launch();
×
231
    ostringstream oss;
×
232
    oss << "CMD\t" << query;
×
233
    d_coproc->send(oss.str());
×
234
  }
×
235
  catch (PDNSException& ae) {
×
236
    g_log << Logger::Error << kBackendId << " Error from coprocess: " << ae.reason << endl;
×
237
    cleanup();
×
238
  }
×
239

240
  ostringstream oss;
×
241
  while (true) {
×
242
    string line;
×
243
    d_coproc->receive(line);
×
244
    if (line == "END")
×
245
      break;
×
246
    oss << line << std::endl;
×
247
  };
×
248

249
  return oss.str();
×
250
}
×
251

252
//! For the dynamic loader
253
DNSBackend* PipeBackend::maker()
254
{
×
255
  try {
×
256
    return new PipeBackend();
×
257
  }
×
258
  catch (...) {
×
259
    g_log << Logger::Error << kBackendId << " Unable to instantiate a pipebackend!" << endl;
×
260
    return nullptr;
×
261
  }
×
262
}
×
263

264
PipeBackend::~PipeBackend()
265
{
2✔
266
  cleanup();
2✔
267
}
2✔
268

269
void PipeBackend::throwTooShortDataError(const std::string& what)
270
{
×
271
  g_log << Logger::Error << kBackendId << " Coprocess returned incomplete or empty line in data section for query for " << d_qname << endl;
×
272
  throw PDNSException("Format error communicating with coprocess in data section" + what);
×
273
}
×
274

275
bool PipeBackend::get(DNSResourceRecord& r)
276
{
27✔
277
  if (d_disavow) // this query has been blocked
27!
278
    return false;
×
279

280
  string line;
27✔
281

282
  // The answer format:
283
  // DATA    qname           qclass  qtype   ttl     id      content
284

285
  try {
27✔
286
    launch();
27✔
287
    for (;;) {
27✔
288
      d_coproc->receive(line);
27✔
289
      vector<string> parts;
27✔
290
      stringtok(parts, line, "\t");
27✔
291
      if (parts.empty()) {
27!
292
        g_log << Logger::Error << kBackendId << " Coprocess returned empty line in query for " << d_qname << endl;
×
293
        throw PDNSException("Format error communicating with coprocess");
×
294
      }
×
295
      else if (parts[0] == "FAIL") {
27!
296
        throw DBException("coprocess returned a FAIL");
×
297
      }
×
298
      else if (parts[0] == "END") {
27✔
299
        return false;
17✔
300
      }
17✔
301
      else if (parts[0] == "LOG") {
10!
302
        g_log << Logger::Error << "Coprocess: " << line.substr(4) << endl;
×
303
        continue;
×
304
      }
×
305
      else if (parts[0] == "DATA") { // yay
10!
306
        // The shortest records (ENT) require 6 fields. Other may require more
307
        // and will have a stricter check once the record type has been
308
        // computed.
309
        if (parts.size() < 6 + (d_abiVersion >= 3 ? 2 : 0)) {
10!
310
          throwTooShortDataError("");
×
311
        }
×
312

313
        if (d_abiVersion >= 3) {
10✔
314
          r.scopeMask = std::stoi(parts[1]);
8✔
315
          r.auth = (parts[2] == "1");
8✔
316
          parts.erase(parts.begin() + 1, parts.begin() + 3);
8✔
317
        }
8✔
318
        else {
2✔
319
          r.scopeMask = 0;
2✔
320
          r.auth = true;
2✔
321
        }
2✔
322
        r.qname = DNSName(parts[1]);
10✔
323
        r.qtype = parts[3];
10✔
324
        pdns::checked_stoi_into(r.ttl, parts[4]);
10✔
325
        pdns::checked_stoi_into(r.domain_id, parts[5]);
10✔
326

327
        switch (r.qtype.getCode()) {
10✔
328
        case QType::ENT:
×
329
          // No other data to process
330
          r.content.clear();
×
331
          break;
×
332
        case QType::MX:
1✔
333
        case QType::SRV:
1!
334
          if (parts.size() < 8) {
1!
335
            throwTooShortDataError("of MX/SRV record");
×
336
          }
×
337
          r.content = parts[6] + " " + parts[7];
1✔
338
          break;
1✔
339
        default:
9✔
340
          if (parts.size() < 7) {
9!
341
            throwTooShortDataError("");
×
342
          }
×
343
          r.content = parts[6];
9✔
344
          for (std::vector<std::string>::size_type pos = 7; pos < parts.size(); ++pos) {
9!
345
            r.content.append(1, ' ');
×
346
            r.content.append(parts[pos]);
×
347
          }
×
348
          break;
9✔
349
        }
10✔
350
        break;
10✔
351
      }
10✔
352
      else
×
353
        throw PDNSException("Coprocess backend sent incorrect response '" + line + "'");
×
354
    }
27✔
355
  }
27✔
356
  catch (DBException& dbe) {
27✔
357
    g_log << Logger::Error << kBackendId << " " << dbe.reason << endl;
×
358
    throw;
×
359
  }
×
360
  catch (PDNSException& pe) {
27✔
361
    g_log << Logger::Error << kBackendId << " " << pe.reason << endl;
×
362
    cleanup();
×
363
    throw;
×
364
  }
×
365
  return true;
10✔
366
}
27✔
367

368
//
369
// Magic class that is activated when the dynamic library is loaded
370
//
371

372
class PipeFactory : public BackendFactory
373
{
374
public:
375
  PipeFactory() :
376
    BackendFactory("pipe") {}
5,869✔
377

378
  void declareArguments(const string& suffix = "") override
379
  {
2✔
380
    declare(suffix, "command", "Command to execute for piping questions to", "");
2✔
381
    declare(suffix, "timeout", "Number of milliseconds to wait for an answer", "2000");
2✔
382
    declare(suffix, "regex", "Regular expression of queries to pass to coprocess", "");
2✔
383
    declare(suffix, "abi-version", "Version of the pipe backend ABI", "1");
2✔
384
  }
2✔
385

386
  DNSBackend* make(const string& suffix = "") override
387
  {
10✔
388
    return new PipeBackend(suffix);
10✔
389
  }
10✔
390
};
391

392
class PipeLoader
393
{
394
public:
395
  PipeLoader()
396
  {
5,869✔
397
    BackendMakers().report(std::make_unique<PipeFactory>());
5,869✔
398
    g_log << Logger::Info << kBackendId << " This is the pipe backend version " VERSION
5,869✔
399
#ifndef REPRODUCIBLE
5,869✔
400
          << " (" __DATE__ " " __TIME__ ")"
5,869✔
401
#endif
5,869✔
402
          << " reporting" << endl;
5,869✔
403
  }
5,869✔
404
};
405

406
static PipeLoader pipeloader;
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

© 2026 Coveralls, Inc