]> git.ipfire.org Git - thirdparty/pdns.git/blame - modules/gsqlite3backend/gsqlite3backend.cc
sqlite3: make journal mode configurable; default to WAL
[thirdparty/pdns.git] / modules / gsqlite3backend / gsqlite3backend.cc
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
fc3c07b4 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.
fc3c07b4 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.
fc3c07b4 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.
fc3c07b4 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.
fc3c07b4 21 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
0b67a76d
BH
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"
5c409fa2 34#include "pdns/pdnsexception.hh"
0b67a76d
BH
35#include "pdns/logger.hh"
36#include "pdns/arguments.hh"
32869e14 37#include "pdns/ssqlite3.hh"
0b67a76d 38#include "gsqlite3backend.hh"
bdaab6b8 39#include <boost/algorithm/string.hpp>
0b67a76d
BH
40
41// Connects to the database.
42gSQLite3Backend::gSQLite3Backend( const std::string & mode, const std::string & suffix ) : GSQLBackend( mode, suffix )
43{
8415acc9 44 try
0b67a76d 45 {
367f9b40 46 SSQLite3 *ptr = new SSQLite3( getArg( "database" ), getArg( "pragma-journal-mode") );
0f310932 47 setDB(ptr);
6edbdc95 48 if(!getArg("pragma-synchronous").empty()) {
0f310932 49 ptr->execute("PRAGMA synchronous="+getArg("pragma-synchronous"));
6edbdc95 50 }
f4373b2a
AT
51 if (mustDo("pragma-foreign-keys")) {
52 ptr->execute("PRAGMA foreign_keys = 1");
53 }
0f310932
AT
54 }
55 catch( SSqlException & e )
0b67a76d 56 {
e6a9dde5 57 g_log << Logger::Error << mode << ": connection failed: " << e.txtReason() << std::endl;
3f81d239 58 throw PDNSException( "Unable to launch " + mode + " connection: " + e.txtReason());
0b67a76d
BH
59 }
60
e6a9dde5 61 g_log << Logger::Info << mode << ": connection to '"<<getArg("database")<<"' successful" << std::endl;
0b67a76d
BH
62}
63
64
65//! Constructs a gSQLite3Backend
66class gSQLite3Factory : public BackendFactory
67{
68public:
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 {
0f310932
AT
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" );
367f9b40 80 declare(suffix, "pragma-journal-mode", "SQLite3 journal mode", "WAL");
0b67a76d 81
fe8e6b44
KM
82 declare(suffix, "dnssec", "Enable DNSSEC processing","no");
83
84 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE";
3d766f44 85
0f310932
AT
86 declare(suffix, "basic-query", "Basic query", record_query+" disabled=0 and type=:qtype and name=:qname");
87 declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=0 and type=:qtype and name=:qname and domain_id=:domain_id");
88 declare(suffix, "any-query", "Any query", record_query+" disabled=0 and name=:qname");
89 declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=0 and name=:qname and domain_id=:domain_id");
0b67a76d 90
0f310932
AT
91 declare(suffix, "list-query", "AXFR query", record_query+" (disabled=0 OR :include_disabled) and domain_id=:domain_id order by name, type");
92 declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=0 and (name=:zone OR name like :wildzone) and domain_id=:domain_id");
ece45ffb 93
0f310932 94 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");
0f310932 95 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");
0f310932 96
8ffb7a9b 97 declare(suffix, "info-zone-query", "","select id,name,master,last_check,notified_serial,type,account from domains where name=:domain");
0f310932 98
b42b90d4 99 declare(suffix, "info-all-slaves-query", "","select id,name,master,last_check from domains where type='SLAVE'");
0f310932
AT
100 declare(suffix, "supermaster-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver");
101 declare(suffix, "supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account");
102
b90323a9 103 declare(suffix, "insert-zone-query", "", "insert into domains (type,name,master,account,last_check,notified_serial) values(:type, :domain, :masters, :account, null, null)");
0f310932 104
76e1255a
KM
105 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)");
106 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)");
0f310932 107
b42b90d4 108 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");
0f310932
AT
109 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");
110 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");
111 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");
0f310932 112
79de0a80
KM
113 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");
114 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");
115 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");
116 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");
0f310932
AT
117
118 declare(suffix, "update-master-query", "", "update domains set master=:master where name=:domain");
119 declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain");
79532aa7 120 declare(suffix, "update-account-query","", "update domains set account=:account where name=:domain");
0f310932
AT
121 declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id");
122 declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id");
bd78c872 123 declare(suffix, "info-all-master-query", "", "select domains.id, domains.name, domains.notified_serial, records.content from records join domains on records.name=domains.name where records.type='SOA' and records.disabled=0 and domains.type='MASTER'");
0f310932
AT
124 declare(suffix, "delete-domain-query","", "delete from domains where name=:domain");
125 declare(suffix, "delete-zone-query", "", "delete from records where domain_id=:domain_id");
126 declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=:domain_id and name=:qname and type=:qtype");
127 declare(suffix, "delete-names-query", "", "delete from records where domain_id=:domain_id and name=:qname");
128
129 declare(suffix, "add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, :flags,:active, :content from domains where name=:domain");
031a1857 130 declare(suffix, "get-last-inserted-key-id-query", "", "select last_insert_rowid()");
0f310932
AT
131 declare(suffix, "list-domain-keys-query","", "select cryptokeys.id, flags, active, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=:domain");
132 declare(suffix, "get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain");
133 declare(suffix, "get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain and domainmetadata.kind=:kind");
134 declare(suffix, "clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain) and domainmetadata.kind=:kind");
135 declare(suffix, "clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain)");
136 declare(suffix, "set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, :kind, :content from domains where name=:domain");
137 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");
138 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");
139 declare(suffix, "remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
140 declare(suffix, "clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain)");
141 declare(suffix, "get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=:key_name");
142 declare(suffix, "set-tsig-key-query","", "replace into tsigkeys (name,algorithm,secret) values(:key_name,:algorithm,:content)");
143 declare(suffix, "delete-tsig-key-query","", "delete from tsigkeys where name=:key_name");
144 declare(suffix, "get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys");
145
8ffb7a9b 146 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");
0f310932
AT
147
148 declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=:domain_id");
149 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)");
150 declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=:domain_id AND name=:qname AND type=:qtype");
151 declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=:domain_id");
075d77ab
AT
152 declare(suffix, "search-records-query", "", record_query+" name LIKE :value OR content LIKE :value2 LIMIT :limit");
153 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");
0b67a76d 154 }
fe8e6b44 155
0b67a76d
BH
156 //! Constructs a new gSQLite3Backend object.
157 DNSBackend *make( const string & suffix = "" )
158 {
159 return new gSQLite3Backend( d_mode, suffix );
160 }
161
162private:
163 const string d_mode;
164};
165
166
167//! Magic class that is activated when the dynamic library is loaded
168class gSQLite3Loader
169{
170public:
171 //! This reports us to the main UeberBackend class
172 gSQLite3Loader()
173 {
174 BackendMakers().report( new gSQLite3Factory( "gsqlite3" ));
e6a9dde5 175 g_log << Logger::Info << "[gsqlite3] This is the gsqlite3 backend version " VERSION
5e6a3d93
PL
176#ifndef REPRODUCIBLE
177 << " (" __DATE__ " " __TIME__ ")"
178#endif
179 << " reporting" << endl;
0b67a76d
BH
180 }
181};
182
0b67a76d
BH
183//! Reports the backendloader to the UeberBackend.
184static gSQLite3Loader gsqlite3loader;
185