]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ssqlite3.cc
auth: Prevent temporary objects in the DNSBackend::get() overrides
[thirdparty/pdns.git] / pdns / ssqlite3.cc
1 /* SQLite backend for PowerDNS
2 * Copyright (C) 2003, Michel Stol <michel@powerdns.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * Additionally, the license of this program contains a special
9 * exception which allows to distribute the program in binary form when
10 * it is linked against OpenSSL.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string>
26 #include <sstream>
27 #include "ssqlite3.hh"
28 #include <iostream>
29 #include <fstream>
30 #include "pdns/logger.hh"
31 #include "misc.hh"
32 #include "utility.hh"
33 #include <unistd.h>
34
35 /*
36 ** Set all the parameters in the compiled SQL statement to NULL.
37 *
38 * copied from sqlite 3.3.6 // cmouse
39 */
40 int pdns_sqlite3_clear_bindings(sqlite3_stmt *pStmt){
41 int i;
42 int rc = SQLITE_OK;
43 for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
44 rc = sqlite3_bind_null(pStmt, i);
45 }
46 return rc;
47 }
48
49 static string SSQLite3ErrorString(sqlite3 *db)
50 {
51 return string(sqlite3_errmsg(db)+string(" (")+std::to_string(sqlite3_extended_errcode(db))+string(")"));
52 }
53
54 class SSQLite3Statement: public SSqlStatement
55 {
56 public:
57 SSQLite3Statement(SSQLite3 *db, bool dolog, const string& query) :
58 d_query(query),
59 d_db(db),
60 d_dolog(dolog)
61 {
62 }
63
64 int name2idx(const string& name) {
65 string zName = string(":")+name;
66 prepareStatement();
67 return sqlite3_bind_parameter_index(d_stmt, zName.c_str());
68 // XXX: support @ and $?
69 }
70
71 SSqlStatement* bind(const string& name, bool value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int(d_stmt, idx, value ? 1 : 0); }; return this; }
72 SSqlStatement* bind(const string& name, int value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int(d_stmt, idx, value); }; return this; }
73 SSqlStatement* bind(const string& name, uint32_t value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
74 SSqlStatement* bind(const string& name, long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
75 SSqlStatement* bind(const string& name, unsigned long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
76 SSqlStatement* bind(const string& name, long long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; };
77 SSqlStatement* bind(const string& name, unsigned long long value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_int64(d_stmt, idx, value); }; return this; }
78 SSqlStatement* bind(const string& name, const std::string& value) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_text(d_stmt, idx, value.c_str(), value.size(), SQLITE_TRANSIENT); }; return this; }
79 SSqlStatement* bindNull(const string& name) { int idx = name2idx(name); if (idx>0) { sqlite3_bind_null(d_stmt, idx); }; return this; }
80
81 SSqlStatement* execute() {
82 prepareStatement();
83 if (d_dolog) {
84 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": " << d_query << endl;
85 d_dtime.set();
86 }
87 int attempts = d_db->inTransaction(); // try only once
88 while(attempts < 2 && (d_rc = sqlite3_step(d_stmt)) == SQLITE_BUSY) attempts++;
89
90 if (d_rc != SQLITE_ROW && d_rc != SQLITE_DONE) {
91 // failed.
92 releaseStatement();
93 if (d_rc == SQLITE_CANTOPEN)
94 throw SSqlException(string("CANTOPEN error in sqlite3, often caused by unwritable sqlite3 db *directory*: ")+SSQLite3ErrorString(d_db->db()));
95 throw SSqlException(string("Error while retrieving SQLite query results: ")+SSQLite3ErrorString(d_db->db()));
96 }
97 if(d_dolog)
98 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": "<<d_dtime.udiffNoReset()<<" usec to execute"<<endl;
99 return this;
100 }
101 bool hasNextRow() {
102 if(d_dolog && d_rc != SQLITE_ROW) {
103 g_log<<Logger::Warning<< "Query "<<((long)(void*)this)<<": "<<d_dtime.udiffNoReset()<<" total usec to last row"<<endl;
104 }
105 return d_rc == SQLITE_ROW;
106 }
107
108 SSqlStatement* nextRow(row_t& row) {
109 row.clear();
110 int numCols = sqlite3_column_count(d_stmt);
111 row.reserve(numCols); // preallocate memory
112 // Another row received, process it.
113 for ( int i=0; i<numCols; i++)
114 {
115 if (sqlite3_column_type(d_stmt,i) == SQLITE_NULL) {
116 row.emplace_back("");
117 } else {
118 const char *pData = (const char*) sqlite3_column_text(d_stmt, i);
119 row.emplace_back(pData, sqlite3_column_bytes(d_stmt, i));
120 }
121 }
122 d_rc = sqlite3_step(d_stmt);
123 return this;
124 }
125
126 SSqlStatement* getResult(result_t& result) {
127 result.clear();
128 while(hasNextRow()) {
129 row_t row;
130 nextRow(row);
131 result.push_back(std::move(row));
132 }
133 return this;
134 }
135
136 SSqlStatement* reset() {
137 sqlite3_reset(d_stmt);
138 #if SQLITE_VERSION_NUMBER >= 3003009
139 sqlite3_clear_bindings(d_stmt);
140 #else
141 pdns_sqlite3_clear_bindings(d_stmt);
142 #endif
143 return this;
144 }
145
146 ~SSQLite3Statement() {
147 // deallocate if necessary
148 releaseStatement();
149 }
150
151 const string& getQuery() { return d_query; };
152 private:
153 string d_query;
154 DTime d_dtime;
155 sqlite3_stmt* d_stmt{nullptr};
156 SSQLite3* d_db{nullptr};
157 int d_rc{0};
158 bool d_dolog;
159 bool d_prepared{false};
160
161 void prepareStatement() {
162 const char *pTail;
163
164 if (d_prepared) return;
165 #if SQLITE_VERSION_NUMBER >= 3003009
166 if (sqlite3_prepare_v2(d_db->db(), d_query.c_str(), -1, &d_stmt, &pTail ) != SQLITE_OK)
167 #else
168 if (sqlite3_prepare(d_db->db(), d_query.c_str(), -1, &d_stmt, &pTail ) != SQLITE_OK)
169 #endif
170 {
171 releaseStatement();
172 throw SSqlException(string("Unable to compile SQLite statement : '")+d_query+"': "+SSQLite3ErrorString(d_db->db()));
173 }
174 if (pTail && strlen(pTail)>0)
175 g_log<<Logger::Warning<<"Sqlite3 command partially processed. Unprocessed part: "<<pTail<<endl;
176 d_prepared = true;
177 }
178
179 void releaseStatement() {
180 if (d_stmt)
181 sqlite3_finalize(d_stmt);
182 d_stmt = nullptr;
183 d_prepared = false;
184 }
185 };
186
187 // Constructor.
188 SSQLite3::SSQLite3( const std::string & database, const std::string & journalmode, bool creat )
189 {
190 if (access( database.c_str(), F_OK ) == -1){
191 if (!creat)
192 throw sPerrorException( "SQLite database '"+database+"' does not exist yet" );
193 } else {
194 if (creat)
195 throw sPerrorException( "SQLite database '"+database+"' already exists" );
196 }
197
198 if ( sqlite3_open( database.c_str(), &m_pDB)!=SQLITE_OK )
199 throw sPerrorException( "Could not connect to the SQLite database '" + database + "'" );
200 m_dolog = 0;
201 m_in_transaction = false;
202 sqlite3_busy_handler(m_pDB, busyHandler, 0);
203
204 if(journalmode.length())
205 execute("PRAGMA journal_mode="+journalmode);
206 }
207
208 void SSQLite3::setLog(bool state)
209 {
210 m_dolog=state;
211 }
212
213 // Destructor.
214 SSQLite3::~SSQLite3()
215 {
216 int ret;
217 for(int n = 0; n < 2 ; ++n) {
218 if((ret =sqlite3_close( m_pDB )) != SQLITE_OK) {
219 if(n || ret != SQLITE_BUSY) { // if we have SQLITE_BUSY, and a working m_Pstmt, try finalize
220 cerr<<"Unable to close down sqlite connection: "<<ret<<endl;
221 abort();
222 }
223 }
224 else
225 break;
226 }
227 }
228
229 std::unique_ptr<SSqlStatement> SSQLite3::prepare(const string& query, int nparams __attribute__((unused))) {
230 return std::unique_ptr<SSqlStatement>(new SSQLite3Statement(this, m_dolog, query));
231 }
232
233 void SSQLite3::execute(const string& query) {
234 char *errmsg;
235 int rc;
236 if (sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) == SQLITE_BUSY) {
237 if (m_in_transaction) {
238 std::string errstr(errmsg);
239 sqlite3_free(errmsg);
240 throw("Failed to execute query: " + errstr);
241 } else {
242 if ((rc = sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) != SQLITE_OK) && rc != SQLITE_DONE && rc != SQLITE_ROW) {
243 std::string errstr(errmsg);
244 sqlite3_free(errmsg);
245 throw("Failed to execute query: " + errstr);
246 }
247 }
248 }
249 }
250
251 int SSQLite3::busyHandler(void*, int)
252 {
253 Utility::usleep(1000);
254 return 1;
255 }
256
257 void SSQLite3::startTransaction() {
258 execute("begin");
259 m_in_transaction = true;
260 }
261
262 void SSQLite3::rollback() {
263 execute("rollback");
264 m_in_transaction = false;
265 }
266
267 void SSQLite3::commit() {
268 execute("commit");
269 m_in_transaction = false;
270 }
271
272 // Constructs a SSqlException object.
273 SSqlException SSQLite3::sPerrorException( const std::string & reason )
274 {
275 return SSqlException( reason );
276 }