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

PowerDNS / pdns / 17089486438

20 Aug 2025 05:31AM UTC coverage: 64.683% (-1.2%) from 65.866%
17089486438

push

github

web-flow
Merge pull request #15991 from omoerbeek/boost-fix-system-lib-dep

Fix Boost system lib req: it is no longer a lib for boost >= 1.89

40936 of 91910 branches covered (44.54%)

Branch coverage included in aggregate %.

124729 of 164210 relevant lines covered (75.96%)

4764249.21 hits per line

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

2.75
/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
{
×
53
  d_command = command;
×
54
  d_timeout = timeout;
×
55
  d_abiVersion = abiVersion;
×
56
  launch(); // let exceptions fall through - if initial launch fails, we want to die
×
57
  // I think
58
}
×
59

60
CoWrapper::~CoWrapper() = default;
×
61

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

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

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

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

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

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

128
void PipeBackend::launch()
129
{
×
130
  if (d_coproc)
×
131
    return;
×
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

265
PipeBackend::~PipeBackend()
266
{
×
267
  cleanup();
×
268
}
×
269

270
bool PipeBackend::get(DNSResourceRecord& r)
271
{
×
272
  if (d_disavow) // this query has been blocked
×
273
    return false;
×
274

275
  string line;
×
276

277
  // The answer format:
278
  // DATA    qname           qclass  qtype   ttl     id      content
279
  unsigned int extraFields = 0;
×
280
  if (d_abiVersion >= 3)
×
281
    extraFields = 2;
×
282

283
  try {
×
284
    launch();
×
285
    for (;;) {
×
286
      d_coproc->receive(line);
×
287
      vector<string> parts;
×
288
      stringtok(parts, line, "\t");
×
289
      if (parts.empty()) {
×
290
        g_log << Logger::Error << kBackendId << " Coprocess returned empty line in query for " << d_qname << endl;
×
291
        throw PDNSException("Format error communicating with coprocess");
×
292
      }
×
293
      else if (parts[0] == "FAIL") {
×
294
        throw DBException("coprocess returned a FAIL");
×
295
      }
×
296
      else if (parts[0] == "END") {
×
297
        return false;
×
298
      }
×
299
      else if (parts[0] == "LOG") {
×
300
        g_log << Logger::Error << "Coprocess: " << line.substr(4) << endl;
×
301
        continue;
×
302
      }
×
303
      else if (parts[0] == "DATA") { // yay
×
304
        if (parts.size() < 7 + extraFields) {
×
305
          g_log << Logger::Error << kBackendId << " Coprocess returned incomplete or empty line in data section for query for " << d_qname << endl;
×
306
          throw PDNSException("Format error communicating with coprocess in data section");
×
307
          // now what?
308
        }
×
309

310
        if (d_abiVersion >= 3) {
×
311
          r.scopeMask = std::stoi(parts[1]);
×
312
          r.auth = (parts[2] == "1");
×
313
        }
×
314
        else {
×
315
          r.scopeMask = 0;
×
316
          r.auth = true;
×
317
        }
×
318
        r.qname = DNSName(parts[1 + extraFields]);
×
319
        r.qtype = parts[3 + extraFields];
×
320
        pdns::checked_stoi_into(r.ttl, parts[4 + extraFields]);
×
321
        pdns::checked_stoi_into(r.domain_id, parts[5 + extraFields]);
×
322

323
        if (r.qtype.getCode() != QType::MX && r.qtype.getCode() != QType::SRV) {
×
324
          r.content.clear();
×
325
          for (unsigned int n = 6 + extraFields; n < parts.size(); ++n) {
×
326
            if (n != 6 + extraFields)
×
327
              r.content.append(1, ' ');
×
328
            r.content.append(parts[n]);
×
329
          }
×
330
        }
×
331
        else {
×
332
          if (parts.size() < 8 + extraFields) {
×
333
            g_log << Logger::Error << kBackendId << " Coprocess returned incomplete MX/SRV line in data section for query for " << d_qname << endl;
×
334
            throw PDNSException("Format error communicating with coprocess in data section of MX/SRV record");
×
335
          }
×
336

337
          r.content = parts[6 + extraFields] + " " + parts[7 + extraFields];
×
338
        }
×
339
        break;
×
340
      }
×
341
      else
×
342
        throw PDNSException("Coprocess backend sent incorrect response '" + line + "'");
×
343
    }
×
344
  }
×
345
  catch (DBException& dbe) {
×
346
    g_log << Logger::Error << kBackendId << " " << dbe.reason << endl;
×
347
    throw;
×
348
  }
×
349
  catch (PDNSException& pe) {
×
350
    g_log << Logger::Error << kBackendId << " " << pe.reason << endl;
×
351
    cleanup();
×
352
    throw;
×
353
  }
×
354
  return true;
×
355
}
×
356

357
//
358
// Magic class that is activated when the dynamic library is loaded
359
//
360

361
class PipeFactory : public BackendFactory
362
{
363
public:
364
  PipeFactory() :
365
    BackendFactory("pipe") {}
5,336✔
366

367
  void declareArguments(const string& suffix = "") override
368
  {
×
369
    declare(suffix, "command", "Command to execute for piping questions to", "");
×
370
    declare(suffix, "timeout", "Number of milliseconds to wait for an answer", "2000");
×
371
    declare(suffix, "regex", "Regular expression of queries to pass to coprocess", "");
×
372
    declare(suffix, "abi-version", "Version of the pipe backend ABI", "1");
×
373
  }
×
374

375
  DNSBackend* make(const string& suffix = "") override
376
  {
×
377
    return new PipeBackend(suffix);
×
378
  }
×
379
};
380

381
class PipeLoader
382
{
383
public:
384
  PipeLoader()
385
  {
5,336✔
386
    BackendMakers().report(std::make_unique<PipeFactory>());
5,336✔
387
    g_log << Logger::Info << kBackendId << " This is the pipe backend version " VERSION
5,336✔
388
#ifndef REPRODUCIBLE
5,336✔
389
          << " (" __DATE__ " " __TIME__ ")"
5,336✔
390
#endif
5,336✔
391
          << " reporting" << endl;
5,336✔
392
  }
5,336✔
393
};
394

395
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