]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | #ifdef HAVE_CONFIG_H | |
24 | #include "config.h" | |
25 | #endif | |
26 | #include <string> | |
27 | #include <map> | |
28 | #include "pdns/namespaces.hh" | |
29 | #include "pdns/dns.hh" | |
30 | #include "pdns/dnsbackend.hh" | |
31 | #include "pdns/dnspacket.hh" | |
32 | #include "pdns/pdnsexception.hh" | |
33 | #include "pdns/logger.hh" | |
34 | #include "pdns/arguments.hh" | |
35 | #include "gmysqlbackend.hh" | |
36 | #include "smysql.hh" | |
37 | #include <sstream> | |
38 | ||
39 | gMySQLBackend::gMySQLBackend(const string& mode, const string& suffix) : | |
40 | GSQLBackend(mode, suffix) | |
41 | { | |
42 | try { | |
43 | reconnect(); | |
44 | } | |
45 | ||
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()); | |
49 | } | |
50 | g_log << Logger::Info << mode << " Connection successful. Connected to database '" << getArg("dbname") << "' on '" << (getArg("host").empty() ? getArg("socket") : getArg("host")) << "'." << endl; | |
51 | } | |
52 | ||
53 | void gMySQLBackend::reconnect() | |
54 | { | |
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"), | |
64 | mustDo("thread-cleanup")))); | |
65 | allocateStatements(); | |
66 | } | |
67 | ||
68 | class gMySQLFactory : public BackendFactory | |
69 | { | |
70 | public: | |
71 | gMySQLFactory(const string& mode) : | |
72 | BackendFactory(mode), d_mode(mode) {} | |
73 | ||
74 | void declareArguments(const string& suffix = "") override | |
75 | { | |
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"); | |
89 | ||
90 | string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE"; | |
91 | ||
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=?"); | |
96 | ||
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 | ||
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"); | |
101 | declare(suffix, "list-subzone-query", "Subzone listing", record_query + " disabled=0 and (name=? OR name like ?) and domain_id=?"); | |
102 | ||
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"); | |
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"); | |
105 | ||
106 | declare(suffix, "info-zone-query", "", "select id,name,master,last_check,notified_serial,type,options,catalog,account from domains where name=?"); | |
107 | ||
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')"); | |
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 (?,?,?)"); | |
112 | declare(suffix, "autoprimary-remove", "", "delete from supermasters where ip = ? and nameserver = ?"); | |
113 | declare(suffix, "list-autoprimaries", "", "select ip,nameserver,account from supermasters"); | |
114 | ||
115 | declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check,notified_serial) values(?,?,?,?,NULL,NULL)"); | |
116 | ||
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)"); | |
119 | ||
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"); | |
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"); | |
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"); | |
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"); | |
124 | ||
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"); | |
129 | ||
130 | declare(suffix, "update-primary-query", "", "update domains set master=? where name=?"); | |
131 | declare(suffix, "update-kind-query", "", "update domains set type=? where name=?"); | |
132 | declare(suffix, "update-options-query", "", "update domains set options=? where name=?"); | |
133 | declare(suffix, "update-catalog-query", "", "update domains set catalog=? where name=?"); | |
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=?"); | |
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"); | |
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=?"); | |
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"); | |
163 | ||
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 ?"); | |
165 | ||
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=?"); | |
170 | declare(suffix, "search-records-query", "", record_query + " name LIKE ? OR content LIKE ? LIMIT ?"); | |
171 | declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE ? OR comment LIKE ? LIMIT ?"); | |
172 | } | |
173 | ||
174 | DNSBackend* make(const string& suffix = "") override | |
175 | { | |
176 | return new gMySQLBackend(d_mode, suffix); | |
177 | } | |
178 | ||
179 | private: | |
180 | const string d_mode; | |
181 | }; | |
182 | ||
183 | //! Magic class that is activated when the dynamic library is loaded | |
184 | class gMySQLLoader | |
185 | { | |
186 | public: | |
187 | //! This reports us to the main UeberBackend class | |
188 | gMySQLLoader() | |
189 | { | |
190 | BackendMakers().report(std::make_unique<gMySQLFactory>("gmysql")); | |
191 | g_log << Logger::Info << "[gmysqlbackend] This is the gmysql backend version " VERSION | |
192 | #ifndef REPRODUCIBLE | |
193 | << " (" __DATE__ " " __TIME__ ")" | |
194 | #endif | |
195 | << " reporting" << endl; | |
196 | } | |
197 | }; | |
198 | static gMySQLLoader gmysqlloader; |