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

PowerDNS / pdns / 18776176249

24 Oct 2025 09:51AM UTC coverage: 73.028% (+15.6%) from 57.383%
18776176249

Pull #16370

github

web-flow
Merge 0f309894e into 39f7f1b27
Pull Request #16370: auth: xfr churning

38273 of 63104 branches covered (60.65%)

Branch coverage included in aggregate %.

186 of 264 new or added lines in 1 file covered. (70.45%)

14471 existing lines in 217 files now uncovered.

127487 of 163878 relevant lines covered (77.79%)

5632363.83 hits per line

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

97.27
/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,959✔
45
  try {
1,959✔
46
    auto ptr = std::unique_ptr<SSql>(new SSQLite3(getArg("database"), getArg("pragma-journal-mode")));
1,959✔
47
    if (!getArg("pragma-synchronous").empty()) {
1,959✔
48
      ptr->execute("PRAGMA synchronous=" + getArg("pragma-synchronous"));
160✔
49
    }
160✔
50
    if (mustDo("pragma-foreign-keys")) {
1,959✔
51
      ptr->execute("PRAGMA foreign_keys = 1");
184✔
UNCOV
52
    }
184✔
53
    setDB(std::move(ptr));
1,959✔
54
    allocateStatements();
1,959✔
55
  }
1,959✔
56
  catch (SSqlException& e) {
1,959✔
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,959✔
62
}
1,959✔
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,869✔
72
  }
5,869✔
73

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

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

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

86
    declare(suffix, "basic-query", "Basic query", record_query + " disabled=0 and type=:qtype and name=:qname");
593✔
87
    declare(suffix, "id-query", "Basic with ID query", record_query + " disabled=0 and type=:qtype and name=:qname and domain_id=:domain_id");
593✔
88
    declare(suffix, "any-query", "Any query", record_query + " disabled=0 and name=:qname");
593✔
89
    declare(suffix, "any-id-query", "Any with ID query", record_query + " disabled=0 and name=:qname and domain_id=:domain_id");
593✔
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");
593✔
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");
593✔
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");
593✔
95
    declare(suffix, "list-subzone-query", "Subzone listing", record_query + " disabled=0 and (name=:zone OR name like :wildzone) and domain_id=:domain_id");
593✔
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");
593✔
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");
593✔
99

100
    declare(suffix, "info-zone-query", "", "select id,name,master,last_check,notified_serial,type,options,catalog,account from domains where name=:domain");
593✔
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')");
593✔
103
    declare(suffix, "autoprimary-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver");
593✔
104
    declare(suffix, "autoprimary-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account");
593✔
105
    declare(suffix, "autoprimary-add", "", "insert into supermasters (ip, nameserver, account) values (:ip,:nameserver,:account)");
593✔
106
    declare(suffix, "autoprimary-remove", "", "delete from supermasters where ip = :ip and nameserver = :nameserver");
593✔
107
    declare(suffix, "list-autoprimaries", "", "select ip,nameserver,account from supermasters");
593✔
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)");
593✔
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)");
593✔
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)");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
123

124
    declare(suffix, "update-primary-query", "", "update domains set master=:master where name=:domain");
593✔
125
    declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain");
593✔
126
    declare(suffix, "update-options-query", "", "update domains set options=:options where name=:domain");
593✔
127
    declare(suffix, "update-catalog-query", "", "update domains set catalog=:catalog where name=:domain");
593✔
128
    declare(suffix, "update-account-query", "", "update domains set account=:account where name=:domain");
593✔
129
    declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id");
593✔
130
    declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id");
593✔
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");
593✔
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");
593✔
133
    declare(suffix, "info-consumer-members-query", "", "select id, name, options, master from domains where type='SLAVE' and catalog=:catalog");
593✔
134
    declare(suffix, "delete-domain-query", "", "delete from domains where name=:domain");
593✔
135
    declare(suffix, "delete-zone-query", "", "delete from records where domain_id=:domain_id");
593✔
136
    declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=:domain_id and name=:qname and type=:qtype");
593✔
137
    declare(suffix, "delete-names-query", "", "delete from records where domain_id=:domain_id and name=:qname");
593✔
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");
593✔
140
    declare(suffix, "get-last-inserted-key-id-query", "", "select last_insert_rowid()");
593✔
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");
593✔
142
    declare(suffix, "get-all-domain-metadata-query", "", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain");
593✔
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");
593✔
144
    declare(suffix, "clear-domain-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=:domain) and domainmetadata.kind=:kind");
593✔
145
    declare(suffix, "clear-domain-all-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=:domain)");
593✔
146
    declare(suffix, "set-domain-metadata-query", "", "insert into domainmetadata (domain_id, kind, content) select id, :kind, :content from domains where name=:domain");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
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");
593✔
152
    declare(suffix, "clear-domain-all-keys-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=:domain)");
593✔
153
    declare(suffix, "get-tsig-key-query", "", "select algorithm, secret from tsigkeys where name=:key_name");
593✔
154
    declare(suffix, "set-tsig-key-query", "", "replace into tsigkeys (name,algorithm,secret) values(:key_name,:algorithm,:content)");
593✔
155
    declare(suffix, "delete-tsig-key-query", "", "delete from tsigkeys where name=:key_name");
593✔
156
    declare(suffix, "get-tsig-keys-query", "", "select name,algorithm, secret from tsigkeys");
593✔
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");
593✔
159

160
    declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=:domain_id");
593✔
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)");
593✔
162
    declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=:domain_id AND name=:qname AND type=:qtype");
593✔
163
    declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=:domain_id");
593✔
164
    declare(suffix, "search-records-query", "", record_query + " name LIKE :value ESCAPE '\\' OR content LIKE :value2 ESCAPE '\\' LIMIT :limit");
593✔
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");
593✔
166
  }
593✔
167

168
  //! Constructs a new gSQLite3Backend object.
169
  DNSBackend* make(const string& suffix = "") override
170
  {
1,959✔
171
    return new gSQLite3Backend(d_mode, suffix);
1,959✔
172
  }
1,959✔
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,869✔
185
    BackendMakers().report(std::make_unique<gSQLite3Factory>("gsqlite3"));
5,869✔
186
    g_log << Logger::Info << "[gsqlite3] This is the gsqlite3 backend version " VERSION
5,869✔
187
#ifndef REPRODUCIBLE
5,869✔
188
          << " (" __DATE__ " " __TIME__ ")"
5,869✔
189
#endif
5,869✔
190
          << " reporting" << endl;
5,869✔
191
  }
5,869✔
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