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

PowerDNS / pdns / 31132929981

06 Aug 2026 10:27AM UTC coverage: 61.77% (-4.2%) from 65.959%
31132929981

push

github

web-flow
Merge pull request #17871 from omoerbeek/prep-20260806

Prep 20260806

81652 of 205832 branches covered (39.67%)

Branch coverage included in aggregate %.

164186 of 192157 relevant lines covered (85.44%)

6831395.11 hits per line

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

86.52
/modules/gpgsqlbackend/gpgsqlbackend.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 "pdns/namespaces.hh"
28
#include "pdns/dns.hh"
29
#include "pdns/dnsbackend.hh"
30
#include "pdns/dnspacket.hh"
31
#include "pdns/pdnsexception.hh"
32
#include "pdns/logger.hh"
33
#include "pdns/arguments.hh"
34
#include "gpgsqlbackend.hh"
35
#include "spgsql.hh"
36
#include <sys/time.h>
37
#include <sstream>
38

39
gPgSQLBackend::gPgSQLBackend(const string& mode, const string& suffix) :
40
  GSQLBackend(mode, suffix)
41
{
1,648✔
42
  if (g_slogStructured) {
1,648!
43
    d_slog = g_slog->withName("gpgsql" + suffix);
1,648✔
44
  }
1,648✔
45

1,648✔
46
  try {
1,648✔
47
    setDB(std::unique_ptr<SSql>(new SPgSQL(d_slog,
1,648✔
48
                                           getArg("dbname"),
1,648✔
49
                                           getArg("host"),
1,648✔
50
                                           getArg("port"),
1,648✔
51
                                           getArg("user"),
52
                                           getArg("password"),
1,648✔
53
                                           getArg("extra-connection-parameters"),
×
54
                                           mustDo("prepared-statements"))));
×
55
  }
×
56

1,646✔
57
  catch (SSqlException& e) {
1,646✔
58
    SLOG(g_log << Logger::Error << mode << " Connection failed: " << e.txtReason() << endl,
1,646!
59
         d_slog->error(Logr::Error, e.txtReason(), "Database connection failed", "mode", Logging::Loggable(mode)));
60
    throw PDNSException("Unable to launch " + mode + " connection: " + e.txtReason());
61
  }
×
62
  allocateStatements();
×
63
  SLOG(g_log << Logger::Info << mode << " Connection successful. Connected to database '" << getArg("dbname") << "' on '" << getArg("host") << "'." << endl,
521!
64
       d_slog->info(Logr::Info, "Database connection successful", "database", Logging::Loggable(getArg("dbname")), "host", Logging::Loggable(getArg("host"))));
×
65
}
×
66

67
void gPgSQLBackend::reconnect()
68
{
×
69
  freeStatements();
×
70

71
  if (d_db) {
×
72
    d_db->reconnect();
329,400✔
73

329,400✔
74
    allocateStatements();
329,400!
75
  }
329,400✔
76
}
329,400✔
77

78
bool gPgSQLBackend::inTransaction()
329,400✔
79
{
80
  const auto* db = dynamic_cast<SPgSQL*>(d_db.get());
81
  if (db) {
28,114!
82
    return db->in_trx();
83
  }
84
  return false;
1,361✔
85
}
86

87
class gPgSQLFactory : public BackendFactory
308✔
88
{
308✔
89
public:
308✔
90
  gPgSQLFactory(const string& mode) :
308✔
91
    BackendFactory(mode), d_mode(mode) {}
426✔
92

308✔
93
  void declareArguments(const string& suffix = "") override
308✔
94
  {
308✔
95
    declare(suffix, "dbname", "Backend database name to connect to", "");
96
    declare(suffix, "user", "Database backend user to connect as", "");
308✔
97
    declare(suffix, "host", "Database backend host to connect to", "");
98
    declare(suffix, "port", "Database backend port to connect to", "");
308✔
99
    declare(suffix, "password", "Database backend password to connect with", "");
100
    declare(suffix, "extra-connection-parameters", "Extra parameters to add to connection string", "");
308✔
101
    declare(suffix, "prepared-statements", "Use prepared statements instead of parameterized queries", "yes");
308✔
102

308✔
103
    declare(suffix, "dnssec", "Enable DNSSEC processing", "no");
308✔
104

105
    string record_query = "SELECT content,ttl,prio,type,domain_id,disabled::int,name,auth::int FROM records WHERE";
308✔
106

308✔
107
    declare(suffix, "basic-query", "Basic query", record_query + " disabled=false and type=$1 and name=$2");
108
    declare(suffix, "id-query", "Basic with ID query", record_query + " disabled=false and type=$1 and name=$2 and domain_id=$3");
308✔
109
    declare(suffix, "any-query", "Any query", record_query + " disabled=false and name=$1");
308✔
110
    declare(suffix, "any-id-query", "Any with ID query", record_query + " disabled=false and name=$1 and domain_id=$2");
111

308✔
112
    declare(suffix, "api-id-query", "API basic with ID query", record_query + " (disabled=false or $1) and type=$2 and name=$3 and domain_id=$4");
113
    declare(suffix, "api-any-id-query", "API any with ID query", record_query + " (disabled=false or $1) and name=$2 and domain_id=$3");
308✔
114

308✔
115
    declare(suffix, "list-query", "AXFR query", "SELECT content,ttl,prio,type,domain_id,disabled::int,name,auth::int,ordername FROM records WHERE (disabled=false OR $1) and domain_id=$2 order by name, type");
308✔
116
    declare(suffix, "list-subzone-query", "Subzone listing", record_query + " disabled=false and (name=$1 OR name like $2) and domain_id=$3");
308✔
117

308✔
118
    declare(suffix, "remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=$1 and type is null");
308✔
119
    declare(suffix, "delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=$1 and name=$2 and type is null");
120

308✔
121
    declare(suffix, "info-zone-query", "", "select id,name,master,last_check,notified_serial,type,options,catalog,account from domains where name=$1");
122

308✔
123
    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')");
308✔
124
    declare(suffix, "autoprimary-query", "", "select account from supermasters where ip=$1 and nameserver=$2");
125
    declare(suffix, "autoprimary-name-to-ips", "", "select ip,account from supermasters where nameserver=$1 and account=$2");
308✔
126
    declare(suffix, "autoprimary-add", "", "insert into supermasters (ip, nameserver, account) values ($1,$2,$3)");
308✔
127
    declare(suffix, "autoprimary-remove", "", "delete from supermasters where ip = $1 and nameserver = $2");
308✔
128
    declare(suffix, "list-autoprimaries", "", "select ip,nameserver,account from supermasters");
308✔
129

130
    declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check, notified_serial) values($1,$2,$3,$4,null,null)");
308✔
131

308✔
132
    declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth) values ($1,$2,$3,$4,$5,$6,$7,$8,$9)");
308✔
133
    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,$1,false,$2,$3,$4,null,null,null)");
308✔
134

135
    declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, first", "select ordername from records where disabled=false and domain_id=$1 and ordername is not null order by 1 using ~<~ limit 1");
308✔
136
    declare(suffix, "get-order-before-query", "DNSSEC Ordering Query, before", "select ordername, name from records where disabled=false and ordername ~<=~ $1 and domain_id=$2 and ordername is not null order by 1 using ~>~ limit 1");
308✔
137
    declare(suffix, "get-order-after-query", "DNSSEC Ordering Query, after", "select ordername from records where disabled=false and ordername ~>~ $1 and domain_id=$2 and ordername is not null order by 1 using ~<~ limit 1");
308✔
138
    declare(suffix, "get-order-last-query", "DNSSEC Ordering Query, last", "select ordername, name from records where disabled=false and ordername != '' and domain_id=$1 and ordername is not null order by 1 using ~>~ limit 1");
308✔
139

308✔
140
    declare(suffix, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=$1,auth=$2 where domain_id=$3 and name=$4 and disabled=false");
308✔
141
    declare(suffix, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=$1,auth=$2 where domain_id=$3 and name=$4 and type=$5 and disabled=false");
308✔
142
    declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=$1 where domain_id=$2 and name=$3 and disabled=false");
308✔
143
    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=$1 where domain_id=$2 and name=$3 and type=$4 and disabled=false");
308✔
144

308✔
145
    declare(suffix, "update-primary-query", "", "update domains set master=$1 where name=$2");
308✔
146
    declare(suffix, "update-kind-query", "", "update domains set type=$1 where name=$2");
308✔
147
    declare(suffix, "update-options-query", "", "update domains set options=$1 where name=$2");
308✔
148
    declare(suffix, "update-catalog-query", "", "update domains set catalog=$1 where name=$2");
308✔
149
    declare(suffix, "update-account-query", "", "update domains set account=$1 where name=$2");
150
    declare(suffix, "update-serial-query", "", "update domains set notified_serial=$1 where id=$2");
308✔
151
    declare(suffix, "update-lastcheck-query", "", "update domains set last_check=$1 where id=$2");
308✔
152
    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=false and domains.type in ('MASTER', 'PRODUCER') order by domains.id");
308✔
153
    declare(suffix, "info-producer-members-query", "", "select domains.id, domains.name, domains.type, domains.options from records join domains on records.domain_id=domains.id and records.name=domains.name where domains.type in ('MASTER', 'PRODUCER') and domains.catalog=$1 and records.type='SOA' and records.disabled=false");
308✔
154
    declare(suffix, "info-consumer-members-query", "", "select id, name, type, options, master from domains where type in ('SLAVE', 'CONSUMER') and catalog=$1");
308✔
155
    declare(suffix, "delete-domain-query", "", "delete from domains where name=$1");
308✔
156
    declare(suffix, "delete-zone-query", "", "delete from records where domain_id=$1");
308✔
157
    declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=$1 and name=$2 and type=$3");
308✔
158
    declare(suffix, "delete-names-query", "", "delete from records where domain_id=$1 and name=$2");
308✔
159

308✔
160
    declare(suffix, "add-domain-key-query", "", "insert into cryptokeys (domain_id, flags, active, published, content) select id, $1, $2, $3, $4 from domains where name=$5 returning id");
308✔
161
    declare(suffix, "get-last-inserted-key-id-query", "", "select pdns_bug_should_not_get_here('https://github.com/PowerDNS/pdns/pull/10392'), 1/0");
308✔
162
    declare(suffix, "list-domain-keys-query", "", "select cryptokeys.id, flags, case when active then 1 else 0 end as active, case when published then 1 else 0 end as published, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=$1");
308✔
163
    declare(suffix, "get-all-domain-metadata-query", "", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1");
308✔
164
    declare(suffix, "get-domain-metadata-query", "", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1 and domainmetadata.kind=$2");
308✔
165
    declare(suffix, "clear-domain-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=$1) and domainmetadata.kind=$2");
308✔
166
    declare(suffix, "clear-domain-all-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=$1)");
308✔
167
    declare(suffix, "set-domain-metadata-query", "", "insert into domainmetadata (domain_id, kind, content) select id, $1, $2 from domains where name=$3");
308✔
168
    declare(suffix, "activate-domain-key-query", "", "update cryptokeys set active=true where domain_id=(select id from domains where name=$1) and  cryptokeys.id=$2");
169
    declare(suffix, "deactivate-domain-key-query", "", "update cryptokeys set active=false where domain_id=(select id from domains where name=$1) and  cryptokeys.id=$2");
308✔
170
    declare(suffix, "publish-domain-key-query", "", "update cryptokeys set published=true where domain_id=(select id from domains where name=$1) and  cryptokeys.id=$2");
171
    declare(suffix, "unpublish-domain-key-query", "", "update cryptokeys set published=false where domain_id=(select id from domains where name=$1) and  cryptokeys.id=$2");
308✔
172
    declare(suffix, "remove-domain-key-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=$1) and cryptokeys.id=$2");
308✔
173
    declare(suffix, "clear-domain-all-keys-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=$1)");
308✔
174
    declare(suffix, "get-tsig-key-query", "", "select algorithm, secret from tsigkeys where name=$1");
308✔
175
    declare(suffix, "set-tsig-key-query", "", "insert into tsigkeys (name,algorithm,secret) values($1,$2,$3) on conflict(name,algorithm) do update set secret=Excluded.secret");
308✔
176
    declare(suffix, "delete-tsig-key-query", "", "delete from tsigkeys where name=$1");
308✔
177
    declare(suffix, "get-tsig-keys-query", "", "select name,algorithm, secret from tsigkeys");
308✔
178

179
    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=false OR $1");
180

1,648✔
181
    declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=$1");
1,648✔
182
    declare(suffix, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES ($1, $2, $3, $4, $5, $6)");
1,648✔
183
    declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=$1 AND name=$2 AND type=$3");
184
    declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=$1");
185
    declare(suffix, "search-records-query", "", record_query + " name ILIKE $1 OR content ILIKE $2 LIMIT $3");
186
    declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name ILIKE $1 OR comment ILIKE $2 LIMIT $3");
187
  }
188

189
  DNSBackend* make(const string& suffix = "") override
190
  {
191
    return new gPgSQLBackend(d_mode, suffix);
192
  }
193

194
private:
1,179✔
195
  const string d_mode;
1,179✔
196
};
1,179✔
197

1,361✔
198
//! Magic class that is activated when the dynamic library is loaded
1,361✔
199
class gPgSQLLoader
1,361✔
200
{
1,361✔
201
public:
1,361✔
202
  //! This reports us to the main UeberBackend class
182✔
203
  gPgSQLLoader()
182✔
204
  {
300✔
205
    BackendMakers().report(std::make_unique<gPgSQLFactory>("gpgsql"));
118✔
206
    // If this module is not loaded dynamically at runtime, this code runs
207
    // as part of a global constructor, before the structured logger has a
208
    // chance to be set up, so fallback to simple logging.
209
    g_log << Logger::Info << "[gpgsqlbackend] This is the gpgsql backend version " VERSION
118✔
210
#ifndef REPRODUCIBLE
118✔
211
          << " (" __DATE__ " " __TIME__ ")"
118✔
212
#endif
118✔
213
          << " reporting" << endl;
118✔
214
  }
118✔
215
};
216
static gPgSQLLoader gpgsqlloader;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc