]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/gsqlite3backend/gsqlite3backend.cc
dd1bdc8ca4fc3ec181b27c4b54a4c032dc700146
[thirdparty/pdns.git] / 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 ) : GSQLBackend( mode, suffix )
43 {
44 try
45 {
46 SSQLite3 *ptr = new SSQLite3( getArg( "database" ));
47 setDB(ptr);
48 if(!getArg("pragma-synchronous").empty()) {
49 ptr->execute("PRAGMA synchronous="+getArg("pragma-synchronous"));
50 }
51 if (mustDo("pragma-foreign-keys")) {
52 ptr->execute("PRAGMA foreign_keys = 1");
53 }
54 }
55 catch( SSqlException & e )
56 {
57 L << Logger::Error << mode << ": connection failed: " << e.txtReason() << std::endl;
58 throw PDNSException( "Unable to launch " + mode + " connection: " + e.txtReason());
59 }
60
61 L << Logger::Info << mode << ": connection to '"<<getArg("database")<<"' successful" << std::endl;
62 }
63
64
65 //! Constructs a gSQLite3Backend
66 class gSQLite3Factory : public BackendFactory
67 {
68 public:
69 //! Constructor.
70 gSQLite3Factory( const std::string & mode ) : BackendFactory( mode ), d_mode( mode )
71 {
72 }
73
74 //! Declares all needed arguments.
75 void declareArguments( const std::string & suffix = "" )
76 {
77 declare(suffix, "database", "Filename of the SQLite3 database", "powerdns.sqlite");
78 declare(suffix, "pragma-synchronous", "Set this to 0 for blazing speed", "");
79 declare(suffix, "pragma-foreign-keys", "Enable foreign key constraints", "no" );
80
81 declare(suffix, "dnssec", "Enable DNSSEC processing","no");
82
83 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE";
84
85 declare(suffix, "basic-query", "Basic query", record_query+" disabled=0 and type=:qtype and name=:qname");
86 declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=0 and type=:qtype and name=:qname and domain_id=:domain_id");
87 declare(suffix, "any-query", "Any query", record_query+" disabled=0 and name=:qname");
88 declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=0 and name=:qname and domain_id=:domain_id");
89
90 declare(suffix, "list-query", "AXFR query", record_query+" (disabled=0 OR :include_disabled) and domain_id=:domain_id order by name, type");
91 declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=0 and (name=:zone OR name like :wildzone) and domain_id=:domain_id");
92
93 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");
94 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");
95
96 declare(suffix, "master-zone-query", "Data", "select master from domains where name=:domain and type='SLAVE'");
97
98 declare(suffix, "info-zone-query", "","select id,name,master,last_check,notified_serial,type,account from domains where name=:domain");
99
100 declare(suffix, "info-all-slaves-query", "","select id,name,master,last_check from domains where type='SLAVE'");
101 declare(suffix, "supermaster-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver");
102 declare(suffix, "supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account");
103
104 declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check,notified_serial) values(:type, :domain, :masters, :account, null, null)");
105
106 declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth,change_date) values (:content,:ttl,:priority,:qtype,:domain_id,:disabled,:qname,:ordername,:auth,null)");
107 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,change_date,content) values (null,:domain_id,0,:qname,:ordername,:auth,null,null,null,null)");
108
109 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");
110 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");
111 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");
112 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");
113
114 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");
115 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");
116 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");
117 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");
118
119 declare(suffix, "update-master-query", "", "update domains set master=:master where name=:domain");
120 declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain");
121 declare(suffix, "update-account-query","", "update domains set account=:account where name=:domain");
122 declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id");
123 declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id");
124 declare(suffix, "zone-lastchange-query", "", "select max(change_date) from records where domain_id=:domain_id");
125 declare(suffix, "info-all-master-query", "", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
126 declare(suffix, "delete-domain-query","", "delete from domains where name=:domain");
127 declare(suffix, "delete-zone-query", "", "delete from records where domain_id=:domain_id");
128 declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=:domain_id and name=:qname and type=:qtype");
129 declare(suffix, "delete-names-query", "", "delete from records where domain_id=:domain_id and name=:qname");
130
131 declare(suffix, "add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, :flags,:active, :content from domains where name=:domain");
132 declare(suffix, "get-last-inserted-key-id-query", "", "select last_insert_rowid()");
133 declare(suffix, "list-domain-keys-query","", "select cryptokeys.id, flags, active, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=:domain");
134 declare(suffix, "get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain");
135 declare(suffix, "get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain and domainmetadata.kind=:kind");
136 declare(suffix, "clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain) and domainmetadata.kind=:kind");
137 declare(suffix, "clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain)");
138 declare(suffix, "set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, :kind, :content from domains where name=:domain");
139 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");
140 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");
141 declare(suffix, "remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
142 declare(suffix, "clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain)");
143 declare(suffix, "get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=:key_name");
144 declare(suffix, "set-tsig-key-query","", "replace into tsigkeys (name,algorithm,secret) values(:key_name,:algorithm,:content)");
145 declare(suffix, "delete-tsig-key-query","", "delete from tsigkeys where name=:key_name");
146 declare(suffix, "get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys");
147
148 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 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");
149
150 declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=:domain_id");
151 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)");
152 declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=:domain_id AND name=:qname AND type=:qtype");
153 declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=:domain_id");
154 declare(suffix, "search-records-query", "", record_query+" name LIKE :value OR content LIKE :value2 LIMIT :limit");
155 declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE :value OR comment LIKE :value2 LIMIT :limit");
156 }
157
158 //! Constructs a new gSQLite3Backend object.
159 DNSBackend *make( const string & suffix = "" )
160 {
161 return new gSQLite3Backend( d_mode, suffix );
162 }
163
164 private:
165 const string d_mode;
166 };
167
168
169 //! Magic class that is activated when the dynamic library is loaded
170 class gSQLite3Loader
171 {
172 public:
173 //! This reports us to the main UeberBackend class
174 gSQLite3Loader()
175 {
176 BackendMakers().report( new gSQLite3Factory( "gsqlite3" ));
177 L << Logger::Info << "[gsqlite3] This is the gsqlite3 backend version " VERSION
178 #ifndef REPRODUCIBLE
179 << " (" __DATE__ " " __TIME__ ")"
180 #endif
181 << " reporting" << endl;
182 }
183 };
184
185 //! Reports the backendloader to the UeberBackend.
186 static gSQLite3Loader gsqlite3loader;
187