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

PowerDNS / pdns / 19332892815

13 Nov 2025 01:18PM UTC coverage: 73.029% (-0.04%) from 73.072%
19332892815

Pull #16389

github

web-flow
Merge bd30b08e1 into 60476c10b
Pull Request #16389: auth Lua health checks: more responsiveness

38533 of 63446 branches covered (60.73%)

Branch coverage included in aggregate %.

0 of 13 new or added lines in 1 file covered. (0.0%)

175 existing lines in 14 files now uncovered.

128003 of 164594 relevant lines covered (77.77%)

6070740.47 hits per line

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

94.55
/modules/gsqlite3backend/gsqlite3backend.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 "pdns/utility.hh"
26
#include <map>
27
#include <unistd.h>
28
#include <sstream>
29
#include <string>
30

31
#include "pdns/dns.hh"
32
#include "pdns/dnsbackend.hh"
33
#include "pdns/dnspacket.hh"
34
#include "pdns/pdnsexception.hh"
35
#include "pdns/logger.hh"
36
#include "pdns/arguments.hh"
37
#include "pdns/ssqlite3.hh"
38
#include "gsqlite3backend.hh"
39
#include <boost/algorithm/string.hpp>
40

41
// Connects to the database.
42
gSQLite3Backend::gSQLite3Backend(const std::string& mode, const std::string& suffix) :
43
  GSQLBackend(mode, suffix)
44
{
1,766✔
45
  try {
1,766✔
46
    auto ptr = std::unique_ptr<SSql>(new SSQLite3(getArg("database"), getArg("pragma-journal-mode")));
1,766✔
47
    if (!getArg("pragma-synchronous").empty()) {
1,766✔
48
      ptr->execute("PRAGMA synchronous=" + getArg("pragma-synchronous"));
159✔
49
    }
159✔
50
    if (mustDo("pragma-foreign-keys")) {
1,766!
UNCOV
51
      ptr->execute("PRAGMA foreign_keys = 1");
×
UNCOV
52
    }
×
53
    setDB(std::move(ptr));
1,766✔
54
    allocateStatements();
1,766✔
55
  }
1,766✔
56
  catch (SSqlException& e) {
1,766✔
57
    g_log << Logger::Error << mode << ": connection failed: " << e.txtReason() << std::endl;
×
58
    throw PDNSException("Unable to launch " + mode + " connection: " + e.txtReason());
×
59
  }
×
60

61
  g_log << Logger::Info << mode << ": connection to '" << getArg("database") << "' successful" << std::endl;
1,762✔
62
}
1,762✔
63

64
//! Constructs a gSQLite3Backend
65
class gSQLite3Factory : public BackendFactory
66
{
67
public:
68
  //! Constructor.
69
  gSQLite3Factory(const std::string& mode) :
70
    BackendFactory(mode), d_mode(mode)
71
  {
5,555✔
72
  }
5,555✔
73

74
  //! Declares all needed arguments.
75
  void declareArguments(const std::string& suffix = "") override
76
  {
413✔
77
    declare(suffix, "database", "Filename of the SQLite3 database", "powerdns.sqlite");
413✔
78
    declare(suffix, "pragma-synchronous", "Set this to 0 for blazing speed", "");
413✔
79
    declare(suffix, "pragma-foreign-keys", "Enable foreign key constraints", "no");
413✔
80
    declare(suffix, "pragma-journal-mode", "SQLite3 journal mode", "WAL");
413✔
81

82
    declare(suffix, "dnssec", "Enable DNSSEC processing", "no");
413✔
83

84
    string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE";
413✔
85

86
    declare(suffix, "basic-query", "Basic query", record_query + " disabled=0 and type=:qtype and name=:qname");
413✔
87
    declare(suffix, "id-query", "Basic with ID query", record_query + " disabled=0 and type=:qtype and name=:qname and domain_id=:domain_id");
413✔
88
    declare(suffix, "any-query", "Any query", record_query + " disabled=0 and name=:qname");
413✔
89
    declare(suffix, "any-id-query", "Any with ID query", record_query + " disabled=0 and name=:qname and domain_id=:domain_id");
413✔
90

91
    declare(suffix, "api-id-query", "API basic with ID query", record_query + " (disabled=0 or :include_disabled) and type=:qtype and name=:qname and domain_id=:domain_id");
413✔
92
    declare(suffix, "api-any-id-query", "API any with ID query", record_query + " (disabled=0 or :include_disabled) and name=:qname and domain_id=:domain_id");
413✔
93

94
    declare(suffix, "list-query", "AXFR query", "SELECT content,ttl,prio,type,domain_id,disabled,name,auth,ordername FROM records WHERE (disabled=0 OR :include_disabled) and domain_id=:domain_id order by name, type");
413✔
95
    declare(suffix, "list-subzone-query", "Subzone listing", record_query + " disabled=0 and (name=:zone OR name like :wildzone) and domain_id=:domain_id");
413✔
96

97
    declare(suffix, "remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=:domain_id and type is null");
413✔
98
    declare(suffix, "delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=:domain_id and name=:qname and type is null");
413✔
99

100
    declare(suffix, "info-zone-query", "", "select id,name,master,last_check,notified_serial,type,options,catalog,account from domains where name=:domain");
413✔
101

102
    declare(suffix, "info-all-secondaries-query", "", "select domains.id, domains.name, domains.type, domains.master, domains.last_check, records.content from domains LEFT JOIN records ON records.domain_id=domains.id AND records.type='SOA' AND records.name=domains.name where domains.type in ('SLAVE', 'CONSUMER')");
413✔
103
    declare(suffix, "autoprimary-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver");
413✔
104
    declare(suffix, "autoprimary-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account");
413✔
105
    declare(suffix, "autoprimary-add", "", "insert into supermasters (ip, nameserver, account) values (:ip,:nameserver,:account)");
413✔
106
    declare(suffix, "autoprimary-remove", "", "delete from supermasters where ip = :ip and nameserver = :nameserver");
413✔
107
    declare(suffix, "list-autoprimaries", "", "select ip,nameserver,account from supermasters");
413✔
108

109
    declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check,notified_serial) values(:type, :domain, :primaries, :account, null, null)");
413✔
110

111
    declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth) values (:content,:ttl,:priority,:qtype,:domain_id,:disabled,:qname,:ordername,:auth)");
413✔
112
    declare(suffix, "insert-empty-non-terminal-order-query", "insert empty non-terminal in zone", "insert into records (type,domain_id,disabled,name,ordername,auth,ttl,prio,content) values (null,:domain_id,0,:qname,:ordername,:auth,null,null,null)");
413✔
113

114
    declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, first", "select ordername from records where disabled=0 and domain_id=:domain_id and ordername is not null order by 1 asc limit 1");
413✔
115
    declare(suffix, "get-order-before-query", "DNSSEC Ordering Query, before", "select ordername, name from records where disabled=0 and ordername <= :ordername and domain_id=:domain_id and ordername is not null order by 1 desc limit 1");
413✔
116
    declare(suffix, "get-order-after-query", "DNSSEC Ordering Query, after", "select min(ordername) from records where disabled=0 and ordername > :ordername and domain_id=:domain_id and ordername is not null");
413✔
117
    declare(suffix, "get-order-last-query", "DNSSEC Ordering Query, last", "select ordername, name from records where disabled=0 and ordername != '' and domain_id=:domain_id and ordername is not null order by 1 desc limit 1");
413✔
118

119
    declare(suffix, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=:ordername,auth=:auth where domain_id=:domain_id and name=:qname and disabled=0");
413✔
120
    declare(suffix, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=:ordername,auth=:auth where domain_id=:domain_id and name=:qname and type=:qtype and disabled=0");
413✔
121
    declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=:auth where domain_id=:domain_id and name=:qname and disabled=0");
413✔
122
    declare(suffix, "nullify-ordername-and-update-auth-type-query", "DNSSEC nullify ordername and update auth for a rrset query", "update records set ordername=NULL,auth=:auth where domain_id=:domain_id and name=:qname and type=:qtype and disabled=0");
413✔
123

124
    declare(suffix, "update-primary-query", "", "update domains set master=:master where name=:domain");
413✔
125
    declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain");
413✔
126
    declare(suffix, "update-options-query", "", "update domains set options=:options where name=:domain");
413✔
127
    declare(suffix, "update-catalog-query", "", "update domains set catalog=:catalog where name=:domain");
413✔
128
    declare(suffix, "update-account-query", "", "update domains set account=:account where name=:domain");
413✔
129
    declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id");
413✔
130
    declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id");
413✔
131
    declare(suffix, "info-all-primary-query", "", "select domains.id, domains.name, domains.type, domains.notified_serial, domains.options, domains.catalog, records.content from records join domains on records.domain_id=domains.id and records.name=domains.name where records.type='SOA' and records.disabled=0 and domains.type in ('MASTER', 'PRODUCER') order by domains.id");
413✔
132
    declare(suffix, "info-producer-members-query", "", "select domains.id, domains.name, domains.options from records join domains on records.domain_id=domains.id and records.name=domains.name where domains.type='MASTER' and domains.catalog=:catalog and records.type='SOA' and records.disabled=0");
413✔
133
    declare(suffix, "info-consumer-members-query", "", "select id, name, options, master from domains where type='SLAVE' and catalog=:catalog");
413✔
134
    declare(suffix, "delete-domain-query", "", "delete from domains where name=:domain");
413✔
135
    declare(suffix, "delete-zone-query", "", "delete from records where domain_id=:domain_id");
413✔
136
    declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=:domain_id and name=:qname and type=:qtype");
413✔
137
    declare(suffix, "delete-names-query", "", "delete from records where domain_id=:domain_id and name=:qname");
413✔
138

139
    declare(suffix, "add-domain-key-query", "", "insert into cryptokeys (domain_id, flags, active, published, content) select id, :flags, :active, :published, :content from domains where name=:domain");
413✔
140
    declare(suffix, "get-last-inserted-key-id-query", "", "select last_insert_rowid()");
413✔
141
    declare(suffix, "list-domain-keys-query", "", "select cryptokeys.id, flags, active, published, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=:domain");
413✔
142
    declare(suffix, "get-all-domain-metadata-query", "", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain");
413✔
143
    declare(suffix, "get-domain-metadata-query", "", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain and domainmetadata.kind=:kind");
413✔
144
    declare(suffix, "clear-domain-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=:domain) and domainmetadata.kind=:kind");
413✔
145
    declare(suffix, "clear-domain-all-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=:domain)");
413✔
146
    declare(suffix, "set-domain-metadata-query", "", "insert into domainmetadata (domain_id, kind, content) select id, :kind, :content from domains where name=:domain");
413✔
147
    declare(suffix, "activate-domain-key-query", "", "update cryptokeys set active=1 where domain_id=(select id from domains where name=:domain) and  cryptokeys.id=:key_id");
413✔
148
    declare(suffix, "deactivate-domain-key-query", "", "update cryptokeys set active=0 where domain_id=(select id from domains where name=:domain) and  cryptokeys.id=:key_id");
413✔
149
    declare(suffix, "publish-domain-key-query", "", "update cryptokeys set published=1 where domain_id=(select id from domains where name=:domain) and  cryptokeys.id=:key_id");
413✔
150
    declare(suffix, "unpublish-domain-key-query", "", "update cryptokeys set published=0 where domain_id=(select id from domains where name=:domain) and  cryptokeys.id=:key_id");
413✔
151
    declare(suffix, "remove-domain-key-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
413✔
152
    declare(suffix, "clear-domain-all-keys-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=:domain)");
413✔
153
    declare(suffix, "get-tsig-key-query", "", "select algorithm, secret from tsigkeys where name=:key_name");
413✔
154
    declare(suffix, "set-tsig-key-query", "", "replace into tsigkeys (name,algorithm,secret) values(:key_name,:algorithm,:content)");
413✔
155
    declare(suffix, "delete-tsig-key-query", "", "delete from tsigkeys where name=:key_name");
413✔
156
    declare(suffix, "get-tsig-keys-query", "", "select name,algorithm, secret from tsigkeys");
413✔
157

158
    declare(suffix, "get-all-domains-query", "Retrieve all domains", "select domains.id, domains.name, records.content, domains.type, domains.master, domains.notified_serial, domains.last_check, domains.account, domains.catalog from domains LEFT JOIN records ON records.domain_id=domains.id AND records.type='SOA' AND records.name=domains.name WHERE records.disabled=0 OR :include_disabled");
413✔
159

160
    declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=:domain_id");
413✔
161
    declare(suffix, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES (:domain_id, :qname, :qtype, :modified_at, :account, :content)");
413✔
162
    declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=:domain_id AND name=:qname AND type=:qtype");
413✔
163
    declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=:domain_id");
413✔
164
    declare(suffix, "search-records-query", "", record_query + " name LIKE :value ESCAPE '\\' OR content LIKE :value2 ESCAPE '\\' LIMIT :limit");
413✔
165
    declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE :value ESCAPE '\\' OR comment LIKE :value2 ESCAPE '\\' LIMIT :limit");
413✔
166
  }
413✔
167

168
  //! Constructs a new gSQLite3Backend object.
169
  DNSBackend* make(const string& suffix = "") override
170
  {
1,766✔
171
    return new gSQLite3Backend(d_mode, suffix);
1,766✔
172
  }
1,766✔
173

174
private:
175
  const string d_mode;
176
};
177

178
//! Magic class that is activated when the dynamic library is loaded
179
class gSQLite3Loader
180
{
181
public:
182
  //! This reports us to the main UeberBackend class
183
  gSQLite3Loader()
184
  {
5,555✔
185
    BackendMakers().report(std::make_unique<gSQLite3Factory>("gsqlite3"));
5,555✔
186
    g_log << Logger::Info << "[gsqlite3] This is the gsqlite3 backend version " VERSION
5,555✔
187
#ifndef REPRODUCIBLE
5,555✔
188
          << " (" __DATE__ " " __TIME__ ")"
5,555✔
189
#endif
5,555✔
190
          << " reporting" << endl;
5,555✔
191
  }
5,555✔
192
};
193

194
//! Reports the backendloader to the UeberBackend.
195
static gSQLite3Loader gsqlite3loader;
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