]> git.ipfire.org Git - thirdparty/pdns.git/blame - modules/gmysqlbackend/gmysqlbackend.cc
Merge pull request #15825 from miodvallat/fewer_mistakes
[thirdparty/pdns.git] / modules / gmysqlbackend / gmysqlbackend.cc
CommitLineData
a3814809 1/*
12471842
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
a3814809 4 *
12471842
PL
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.
a3814809 8 *
12471842
PL
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.
a3814809 12 *
12471842
PL
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.
a3814809 17 *
12471842
PL
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.
a3814809
PL
21 */
22
870a0fe4
AT
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
2ffc06cb
BH
26#include <string>
27#include <map>
10f4eea8 28#include "pdns/namespaces.hh"
907a3e04
BH
29#include "pdns/dns.hh"
30#include "pdns/dnsbackend.hh"
907a3e04 31#include "pdns/dnspacket.hh"
5c409fa2 32#include "pdns/pdnsexception.hh"
907a3e04
BH
33#include "pdns/logger.hh"
34#include "pdns/arguments.hh"
c9245417 35#include "gmysqlbackend.hh"
2ffc06cb 36#include "smysql.hh"
2ffc06cb
BH
37#include <sstream>
38
ff05c7e1
O
39gMySQLBackend::gMySQLBackend(const string& mode, const string& suffix) :
40 GSQLBackend(mode, suffix)
2ffc06cb 41{
2ffc06cb 42 try {
45d36933 43 reconnect();
2ffc06cb 44 }
f2343e91 45
ff05c7e1
O
46 catch (SSqlException& e) {
47 g_log << Logger::Error << mode << " Connection failed: " << e.txtReason() << endl;
48 throw PDNSException("Unable to launch " + mode + " connection: " + e.txtReason());
2ffc06cb 49 }
ff05c7e1 50 g_log << Logger::Info << mode << " Connection successful. Connected to database '" << getArg("dbname") << "' on '" << (getArg("host").empty() ? getArg("socket") : getArg("host")) << "'." << endl;
2ffc06cb
BH
51}
52
45d36933
RG
53void gMySQLBackend::reconnect()
54{
22a9aef7
RG
55 setDB(std::unique_ptr<SSql>(new SMySQL(getArg("dbname"),
56 getArg("host"),
57 getArgAsNum("port"),
58 getArg("socket"),
59 getArg("user"),
60 getArg("password"),
61 getArg("group"),
62 mustDo("innodb-read-committed"),
63 getArgAsNum("timeout"),
95a7f809 64 mustDo("thread-cleanup"))));
8e740765 65 allocateStatements();
45d36933
RG
66}
67
2ffc06cb
BH
68class gMySQLFactory : public BackendFactory
69{
70public:
ff05c7e1
O
71 gMySQLFactory(const string& mode) :
72 BackendFactory(mode), d_mode(mode) {}
c9245417 73
ff05c7e1 74 void declareArguments(const string& suffix = "") override
2ffc06cb 75 {
ff05c7e1
O
76 declare(suffix, "dbname", "Database name to connect to", "powerdns");
77 declare(suffix, "user", "Database backend user to connect as", "powerdns");
78 declare(suffix, "host", "Database backend host to connect to", "");
79 declare(suffix, "port", "Database backend port to connect to", "3306");
80 declare(suffix, "socket", "Database backend socket to connect to", "");
81 declare(suffix, "password", "Database backend password to connect with", "");
82 declare(suffix, "group", "Database backend MySQL 'group' to connect as", "client");
83 declare(suffix, "innodb-read-committed", "Use InnoDB READ-COMMITTED transaction isolation level", "yes");
84 declare(suffix, "timeout", "The timeout in seconds for each attempt to read/write to the server", "10");
85 declare(suffix, "thread-cleanup", "Explicitly call mysql_thread_end() when threads end", "no");
86 declare(suffix, "ssl", "Send the SSL capability flag to the server", "no");
87
88 declare(suffix, "dnssec", "Enable DNSSEC processing", "no");
c9245417 89
7166723e 90 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE";
2ffc06cb 91
ff05c7e1
O
92 declare(suffix, "basic-query", "Basic query", record_query + " disabled=0 and type=? and name=?");
93 declare(suffix, "id-query", "Basic with ID query", record_query + " disabled=0 and type=? and name=? and domain_id=?");
94 declare(suffix, "any-query", "Any query", record_query + " disabled=0 and name=?");
95 declare(suffix, "any-id-query", "Any with ID query", record_query + " disabled=0 and name=? and domain_id=?");
3d766f44 96
d1f8b2e7
MV
97 declare(suffix, "api-id-query", "API basic with ID query", record_query + " (disabled=0 or ?) and type=? and name=? and domain_id=?");
98 declare(suffix, "api-any-id-query", "API any with ID query", record_query + " (disabled=0 or ?) and name=? and domain_id=?");
99
fc5245c3 100 declare(suffix, "list-query", "AXFR query", "SELECT content,ttl,prio,type,domain_id,disabled,name,auth,ordername FROM records WHERE (disabled=0 OR ?) and domain_id=? order by name, type");
ff05c7e1 101 declare(suffix, "list-subzone-query", "Subzone listing", record_query + " disabled=0 and (name=? OR name like ?) and domain_id=?");
ece45ffb 102
0f310932 103 declare(suffix, "remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=? and type is null");
0f310932 104 declare(suffix, "delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=? and name=? and type is null");
c9245417 105
4bc3a9e0 106 declare(suffix, "info-zone-query", "", "select id,name,master,last_check,notified_serial,type,options,catalog,account from domains where name=?");
5443b5a7 107
c02c999b 108 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')");
d525b58b
KM
109 declare(suffix, "autoprimary-query", "", "select account from supermasters where ip=? and nameserver=?");
110 declare(suffix, "autoprimary-name-to-ips", "", "select ip,account from supermasters where nameserver=? and account=?");
111 declare(suffix, "autoprimary-add", "", "insert into supermasters (ip, nameserver, account) values (?,?,?)");
782e9b24
AT
112 declare(suffix, "autoprimary-remove", "", "delete from supermasters where ip = ? and nameserver = ?");
113 declare(suffix, "list-autoprimaries", "", "select ip,nameserver,account from supermasters");
0f310932 114
ff05c7e1 115 declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check,notified_serial) values(?,?,?,?,NULL,NULL)");
0f310932 116
76e1255a
KM
117 declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth) values (?,?,?,?,?,?,?,?,?)");
118 declare(suffix, "insert-empty-non-terminal-order-query", "insert empty non-terminal in zone", "insert into records (type,domain_id,disabled,name,ordername,auth,content,ttl,prio) values (null,?,0,?,?,?,NULL,NULL,NULL)");
0f310932 119
ff740555 120 declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, first", "select ordername from records where domain_id=? and disabled=0 and ordername is not null order by 1 asc limit 1");
0f310932 121 declare(suffix, "get-order-before-query", "DNSSEC Ordering Query, before", "select ordername, name from records where ordername <= ? and domain_id=? and disabled=0 and ordername is not null order by 1 desc limit 1");
4cdb15d2 122 declare(suffix, "get-order-after-query", "DNSSEC Ordering Query, after", "select ordername from records where ordername > ? and domain_id=? and disabled=0 and ordername is not null order by 1 asc limit 1");
0f310932 123 declare(suffix, "get-order-last-query", "DNSSEC Ordering Query, last", "select ordername, name from records where ordername != '' and domain_id=? and disabled=0 and ordername is not null order by 1 desc limit 1");
0f310932 124
79de0a80
KM
125 declare(suffix, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=?,auth=? where domain_id=? and name=? and disabled=0");
126 declare(suffix, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=?,auth=? where domain_id=? and name=? and type=? and disabled=0");
127 declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=? where domain_id=? and name=? and disabled=0");
128 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=? where domain_id=? and name=? and type=? and disabled=0");
0f310932 129
d525b58b 130 declare(suffix, "update-primary-query", "", "update domains set master=? where name=?");
ff05c7e1 131 declare(suffix, "update-kind-query", "", "update domains set type=? where name=?");
9da0c596 132 declare(suffix, "update-options-query", "", "update domains set options=? where name=?");
4bc3a9e0 133 declare(suffix, "update-catalog-query", "", "update domains set catalog=? where name=?");
ff05c7e1
O
134 declare(suffix, "update-account-query", "", "update domains set account=? where name=?");
135 declare(suffix, "update-serial-query", "", "update domains set notified_serial=? where id=?");
136 declare(suffix, "update-lastcheck-query", "", "update domains set last_check=? where id=?");
7a8ca8c1 137 declare(suffix, "info-all-primary-query", "", "select d.id, d.name, d.type, d.notified_serial,d.options, d.catalog,r.content from records r join domains d on r.domain_id=d.id and r.name=d.name where r.type='SOA' and r.disabled=0 and d.type in ('MASTER', 'PRODUCER') order by d.id");
73f9bdd8
KM
138 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=? and records.type='SOA' and records.disabled=0");
139 declare(suffix, "info-consumer-members-query", "", "select id, name, options, master from domains where type='SLAVE' and catalog=?");
ff05c7e1
O
140 declare(suffix, "delete-domain-query", "", "delete from domains where name=?");
141 declare(suffix, "delete-zone-query", "", "delete from records where domain_id=?");
142 declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=? and name=? and type=?");
143 declare(suffix, "delete-names-query", "", "delete from records where domain_id=? and name=?");
144
145 declare(suffix, "add-domain-key-query", "", "insert into cryptokeys (domain_id, flags, active, published, content) select id, ?, ?, ?, ? from domains where name=?");
146 declare(suffix, "get-last-inserted-key-id-query", "", "select LAST_INSERT_ID()");
147 declare(suffix, "list-domain-keys-query", "", "select cryptokeys.id, flags, active, published, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=?");
148 declare(suffix, "get-all-domain-metadata-query", "", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=?");
149 declare(suffix, "get-domain-metadata-query", "", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=? and domainmetadata.kind=?");
150 declare(suffix, "clear-domain-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=?) and domainmetadata.kind=?");
151 declare(suffix, "clear-domain-all-metadata-query", "", "delete from domainmetadata where domain_id=(select id from domains where name=?)");
152 declare(suffix, "set-domain-metadata-query", "", "insert into domainmetadata (domain_id, kind, content) select id, ?, ? from domains where name=?");
153 declare(suffix, "activate-domain-key-query", "", "update cryptokeys set active=1 where domain_id=(select id from domains where name=?) and cryptokeys.id=?");
154 declare(suffix, "deactivate-domain-key-query", "", "update cryptokeys set active=0 where domain_id=(select id from domains where name=?) and cryptokeys.id=?");
155 declare(suffix, "publish-domain-key-query", "", "update cryptokeys set published=1 where domain_id=(select id from domains where name=?) and cryptokeys.id=?");
156 declare(suffix, "unpublish-domain-key-query", "", "update cryptokeys set published=0 where domain_id=(select id from domains where name=?) and cryptokeys.id=?");
157 declare(suffix, "remove-domain-key-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=?) and cryptokeys.id=?");
158 declare(suffix, "clear-domain-all-keys-query", "", "delete from cryptokeys where domain_id=(select id from domains where name=?)");
159 declare(suffix, "get-tsig-key-query", "", "select algorithm, secret from tsigkeys where name=?");
160 declare(suffix, "set-tsig-key-query", "", "replace into tsigkeys (name,algorithm,secret) values(?,?,?)");
161 declare(suffix, "delete-tsig-key-query", "", "delete from tsigkeys where name=?");
162 declare(suffix, "get-tsig-keys-query", "", "select name,algorithm, secret from tsigkeys");
1325e8a2 163
f0a3c1f0 164 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 ?");
6cc98ddf 165
0f310932
AT
166 declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=?");
167 declare(suffix, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES (?, ?, ?, ?, ?, ?)");
168 declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=? AND name=? AND type=?");
169 declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=?");
ff05c7e1 170 declare(suffix, "search-records-query", "", record_query + " name LIKE ? OR content LIKE ? LIMIT ?");
cee850e7 171 declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE ? OR comment LIKE ? LIMIT ?");
2ffc06cb 172 }
c9245417 173
ff05c7e1 174 DNSBackend* make(const string& suffix = "") override
2ffc06cb 175 {
ff05c7e1 176 return new gMySQLBackend(d_mode, suffix);
2ffc06cb 177 }
ff05c7e1 178
2ffc06cb
BH
179private:
180 const string d_mode;
181};
182
2ffc06cb
BH
183//! Magic class that is activated when the dynamic library is loaded
184class gMySQLLoader
185{
186public:
187 //! This reports us to the main UeberBackend class
188 gMySQLLoader()
189 {
5144dfa8 190 BackendMakers().report(std::make_unique<gMySQLFactory>("gmysql"));
e6a9dde5 191 g_log << Logger::Info << "[gmysqlbackend] This is the gmysql backend version " VERSION
5e6a3d93 192#ifndef REPRODUCIBLE
ff05c7e1 193 << " (" __DATE__ " " __TIME__ ")"
5e6a3d93 194#endif
ff05c7e1 195 << " reporting" << endl;
2ffc06cb
BH
196 }
197};
198static gMySQLLoader gmysqlloader;