2 * This file is part of GNUnet
3 * Copyright (C) 2009-2017 GNUnet e.V.
5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file namestore/plugin_namestore_sqlite.c
23 * @brief sqlite-based namestore backend
24 * @author Christian Grothoff
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_sq_lib.h"
32 #include "namestore.h"
36 * After how many ms "busy" should a DB operation fail for good? A
37 * low value makes sure that we are more responsive to requests
38 * (especially PUTs). A high value guarantees a higher success rate
39 * (SELECTs in iterate can take several seconds despite LIMIT=1).
41 * The default value of 1s should ensure that users do not experience
42 * huge latencies while at the same time allowing operations to
43 * succeed with reasonable probability.
45 #define BUSY_TIMEOUT_MS 1000
49 * Log an error message at log-level 'level' that indicates
50 * a failure of the command 'cmd' on file 'filename'
51 * with the message given by strerror(errno).
53 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \
54 "namestore-sqlite", _ ( \
55 "`%s' failed at %s:%d with error: %s\n"), \
62 #define LOG(kind, ...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
66 * Context for all functions in this plugin.
70 const struct GNUNET_CONFIGURATION_Handle *cfg;
78 * Native SQLite database handle.
83 * Precompiled SQL to store records.
85 sqlite3_stmt *store_records;
88 * Precompiled SQL to deltete existing records.
90 sqlite3_stmt *delete_records;
93 * Precompiled SQL for iterate records within a zone.
95 sqlite3_stmt *iterate_zone;
98 * Precompiled SQL for iterate all records within all zones.
100 sqlite3_stmt *iterate_all_zones;
103 * Precompiled SQL to for reverse lookup based on PKEY.
105 sqlite3_stmt *zone_to_name;
108 * Precompiled SQL to lookup records based on label.
110 sqlite3_stmt *lookup_label;
115 * Initialize the database connections and associated
116 * data structures (create tables and indices
117 * as needed as well).
119 * @param plugin the plugin context (state for this module)
120 * @return #GNUNET_OK on success
123 database_setup (struct Plugin *plugin)
125 char *sqlite_filename;
126 struct GNUNET_SQ_ExecuteStatement es[] = {
127 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
128 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
129 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
130 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
131 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
132 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
133 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
134 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
135 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
136 " uid INTEGER PRIMARY KEY,"
137 " zone_private_key BLOB NOT NULL,"
139 " rvalue INT8 NOT NULL,"
140 " record_count INT NOT NULL,"
141 " record_data BLOB NOT NULL,"
142 " label TEXT NOT NULL"
144 GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
145 "ON ns098records (zone_private_key,pkey)"),
146 GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
147 "ON ns098records (zone_private_key,uid)"),
148 GNUNET_SQ_EXECUTE_STATEMENT_END
150 struct GNUNET_SQ_PrepareStatement ps[] = {
151 GNUNET_SQ_make_prepare ("INSERT INTO ns098records "
152 "(zone_private_key,pkey,rvalue,record_count,record_data,label)"
153 " VALUES (?, ?, ?, ?, ?, ?)",
154 &plugin->store_records),
155 GNUNET_SQ_make_prepare ("DELETE FROM ns098records "
156 "WHERE zone_private_key=? AND label=?",
157 &plugin->delete_records),
158 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
160 " WHERE zone_private_key=? AND pkey=?",
161 &plugin->zone_to_name),
162 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
164 " WHERE zone_private_key=? AND uid > ?"
167 &plugin->iterate_zone),
168 GNUNET_SQ_make_prepare (
169 "SELECT uid,record_count,record_data,label,zone_private_key"
174 &plugin->iterate_all_zones),
175 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
177 " WHERE zone_private_key=? AND label=?",
178 &plugin->lookup_label),
179 GNUNET_SQ_PREPARE_END
183 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
188 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
191 return GNUNET_SYSERR;
194 GNUNET_DISK_file_test (sqlite_filename))
197 GNUNET_DISK_directory_create_for_file (sqlite_filename))
200 GNUNET_free (sqlite_filename);
201 return GNUNET_SYSERR;
204 /* sqlite_filename should be UTF-8-encoded. If it isn't, it's a bug */
205 plugin->fn = sqlite_filename;
207 /* Open database and precompile statements */
209 sqlite3_open (plugin->fn,
212 LOG (GNUNET_ERROR_TYPE_ERROR,
213 _ ("Unable to initialize SQLite: %s.\n"),
214 sqlite3_errmsg (plugin->dbh));
215 return GNUNET_SYSERR;
217 GNUNET_break (SQLITE_OK ==
218 sqlite3_busy_timeout (plugin->dbh,
221 GNUNET_SQ_exec_statements (plugin->dbh,
225 LOG (GNUNET_ERROR_TYPE_ERROR,
226 _ ("Failed to setup database at `%s'\n"),
228 return GNUNET_SYSERR;
232 GNUNET_SQ_prepare (plugin->dbh,
236 LOG (GNUNET_ERROR_TYPE_ERROR,
237 _ ("Failed to setup database at `%s'\n"),
239 return GNUNET_SYSERR;
246 * Shutdown database connection and associate data
248 * @param plugin the plugin context (state for this module)
251 database_shutdown (struct Plugin *plugin)
256 if (NULL != plugin->store_records)
257 sqlite3_finalize (plugin->store_records);
258 if (NULL != plugin->delete_records)
259 sqlite3_finalize (plugin->delete_records);
260 if (NULL != plugin->iterate_zone)
261 sqlite3_finalize (plugin->iterate_zone);
262 if (NULL != plugin->iterate_all_zones)
263 sqlite3_finalize (plugin->iterate_all_zones);
264 if (NULL != plugin->zone_to_name)
265 sqlite3_finalize (plugin->zone_to_name);
266 if (NULL != plugin->lookup_label)
267 sqlite3_finalize (plugin->lookup_label);
268 result = sqlite3_close (plugin->dbh);
269 if (result == SQLITE_BUSY)
271 LOG (GNUNET_ERROR_TYPE_WARNING,
273 "Tried to close sqlite without finalizing all prepared statements.\n"));
274 stmt = sqlite3_next_stmt (plugin->dbh,
278 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
280 "Closing statement %p\n",
282 result = sqlite3_finalize (stmt);
283 if (result != SQLITE_OK)
284 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
286 "Failed to close statement %p: %d\n",
289 stmt = sqlite3_next_stmt (plugin->dbh,
292 result = sqlite3_close (plugin->dbh);
294 if (SQLITE_OK != result)
296 GNUNET_ERROR_TYPE_ERROR,
299 GNUNET_free_non_null (plugin->fn);
304 * Store a record in the datastore. Removes any existing record in the
305 * same zone with the same name.
307 * @param cls closure (internal context for the plugin)
308 * @param zone_key private key of the zone
309 * @param label name that is being mapped (at most 255 characters long)
310 * @param rd_count number of entries in @a rd array
311 * @param rd array of records with data to store
312 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
315 namestore_sqlite_store_records (void *cls,
317 GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
319 unsigned int rd_count,
320 const struct GNUNET_GNSRECORD_Data *rd)
322 struct Plugin *plugin = cls;
324 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
331 for (unsigned int i = 0; i < rd_count; i++)
332 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
334 GNUNET_break (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) ==
336 GNUNET_memcpy (&pkey,
341 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
343 data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
348 return GNUNET_SYSERR;
350 if (data_size > 64 * 65536)
353 return GNUNET_SYSERR;
356 /* First delete 'old' records */
357 char data[data_size];
358 struct GNUNET_SQ_QueryParam dparams[] = {
359 GNUNET_SQ_query_param_auto_from_type (zone_key),
360 GNUNET_SQ_query_param_string (label),
361 GNUNET_SQ_query_param_end
365 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
373 return GNUNET_SYSERR;
376 GNUNET_SQ_bind (plugin->delete_records,
380 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
381 "sqlite3_bind_XXXX");
382 GNUNET_SQ_reset (plugin->dbh,
383 plugin->delete_records);
384 return GNUNET_SYSERR;
386 n = sqlite3_step (plugin->delete_records);
387 GNUNET_SQ_reset (plugin->dbh,
388 plugin->delete_records);
392 uint32_t rd_count32 = (uint32_t) rd_count;
393 struct GNUNET_SQ_QueryParam sparams[] = {
394 GNUNET_SQ_query_param_auto_from_type (zone_key),
395 GNUNET_SQ_query_param_auto_from_type (&pkey),
396 GNUNET_SQ_query_param_uint64 (&rvalue),
397 GNUNET_SQ_query_param_uint32 (&rd_count32),
398 GNUNET_SQ_query_param_fixed_size (data, data_size),
399 GNUNET_SQ_query_param_string (label),
400 GNUNET_SQ_query_param_end
404 GNUNET_SQ_bind (plugin->store_records,
408 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
409 "sqlite3_bind_XXXX");
410 GNUNET_SQ_reset (plugin->dbh,
411 plugin->store_records);
412 return GNUNET_SYSERR;
414 n = sqlite3_step (plugin->store_records);
415 GNUNET_SQ_reset (plugin->dbh,
416 plugin->store_records);
423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
427 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
434 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
440 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
442 return GNUNET_SYSERR;
448 * The given 'sqlite' statement has been prepared to be run.
449 * It will return a record which should be given to the iterator.
450 * Runs the statement and parses the returned record.
452 * @param plugin plugin context
453 * @param stmt to run (and then clean up)
454 * @param zone_key private key of the zone
455 * @param limit maximum number of results to fetch
456 * @param iter iterator to call with the result
457 * @param iter_cls closure for @a iter
458 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
461 get_records_and_call_iterator (struct Plugin *plugin,
464 GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
466 GNUNET_NAMESTORE_RecordIterator iter,
473 for (uint64_t i = 0; i < limit; i++)
475 sret = sqlite3_step (stmt);
477 if (SQLITE_DONE == sret)
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Iteration done (no results)\n");
484 if (SQLITE_ROW != sret)
487 GNUNET_ERROR_TYPE_ERROR,
495 uint32_t record_count;
499 struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
500 struct GNUNET_SQ_ResultSpec rs[] = {
501 GNUNET_SQ_result_spec_uint64 (&seq),
502 GNUNET_SQ_result_spec_uint32 (&record_count),
503 GNUNET_SQ_result_spec_variable_size (&data,
505 GNUNET_SQ_result_spec_string (&label),
506 GNUNET_SQ_result_spec_end
508 struct GNUNET_SQ_ResultSpec rsx[] = {
509 GNUNET_SQ_result_spec_uint64 (&seq),
510 GNUNET_SQ_result_spec_uint32 (&record_count),
511 GNUNET_SQ_result_spec_variable_size (&data,
513 GNUNET_SQ_result_spec_string (&label),
514 GNUNET_SQ_result_spec_auto_from_type (&zk),
515 GNUNET_SQ_result_spec_end
518 ret = GNUNET_SQ_extract_result (stmt,
522 if ((GNUNET_OK != ret) ||
523 (record_count > 64 * 1024))
525 /* sanity check, don't stack allocate far too much just
526 because database might contain a large value here */
533 struct GNUNET_GNSRECORD_Data rd[record_count];
535 GNUNET_assert (0 != seq);
537 GNUNET_GNSRECORD_records_deserialize (data_size,
548 if (NULL != zone_key)
559 GNUNET_SQ_cleanup_result (rs);
562 GNUNET_SQ_reset (plugin->dbh,
569 * Lookup records in the datastore for which we are the authority.
571 * @param cls closure (internal context for the plugin)
572 * @param zone private key of the zone
573 * @param label name of the record in the zone
574 * @param iter function to call with the result
575 * @param iter_cls closure for @a iter
576 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
579 namestore_sqlite_lookup_records (void *cls,
581 GNUNET_CRYPTO_EcdsaPrivateKey *zone,
583 GNUNET_NAMESTORE_RecordIterator iter,
586 struct Plugin *plugin = cls;
587 struct GNUNET_SQ_QueryParam params[] = {
588 GNUNET_SQ_query_param_auto_from_type (zone),
589 GNUNET_SQ_query_param_string (label),
590 GNUNET_SQ_query_param_end
596 return GNUNET_SYSERR;
599 GNUNET_SQ_bind (plugin->lookup_label,
602 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
603 "sqlite3_bind_XXXX");
604 GNUNET_SQ_reset (plugin->dbh,
605 plugin->lookup_label);
606 return GNUNET_SYSERR;
608 return get_records_and_call_iterator (plugin,
609 plugin->lookup_label,
618 * Iterate over the results for a particular key and zone in the
619 * datastore. Will return at most one result to the iterator.
621 * @param cls closure (internal context for the plugin)
622 * @param zone hash of public key of the zone, NULL to iterate over all zones
623 * @param serial serial number to exclude in the list of all matching records
624 * @param limit maximum number of results to return
625 * @param iter function to call with the result
626 * @param iter_cls closure for @a iter
627 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
630 namestore_sqlite_iterate_records (void *cls,
632 GNUNET_CRYPTO_EcdsaPrivateKey *zone,
635 GNUNET_NAMESTORE_RecordIterator iter,
638 struct Plugin *plugin = cls;
644 struct GNUNET_SQ_QueryParam params[] = {
645 GNUNET_SQ_query_param_uint64 (&serial),
646 GNUNET_SQ_query_param_uint64 (&limit),
647 GNUNET_SQ_query_param_end
650 stmt = plugin->iterate_all_zones;
651 err = GNUNET_SQ_bind (stmt,
656 struct GNUNET_SQ_QueryParam params[] = {
657 GNUNET_SQ_query_param_auto_from_type (zone),
658 GNUNET_SQ_query_param_uint64 (&serial),
659 GNUNET_SQ_query_param_uint64 (&limit),
660 GNUNET_SQ_query_param_end
663 stmt = plugin->iterate_zone;
664 err = GNUNET_SQ_bind (stmt,
667 if (GNUNET_OK != err)
670 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
671 "sqlite3_bind_XXXX");
672 GNUNET_SQ_reset (plugin->dbh,
674 return GNUNET_SYSERR;
676 return get_records_and_call_iterator (plugin,
686 * Look for an existing PKEY delegation record for a given public key.
687 * Returns at most one result to the iterator.
689 * @param cls closure (internal context for the plugin)
690 * @param zone private key of the zone to look up in, never NULL
691 * @param value_zone public key of the target zone (value), never NULL
692 * @param iter function to call with the result
693 * @param iter_cls closure for @a iter
694 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
697 namestore_sqlite_zone_to_name (void *cls,
698 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
700 GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
701 GNUNET_NAMESTORE_RecordIterator iter,
704 struct Plugin *plugin = cls;
705 struct GNUNET_SQ_QueryParam params[] = {
706 GNUNET_SQ_query_param_auto_from_type (zone),
707 GNUNET_SQ_query_param_auto_from_type (value_zone),
708 GNUNET_SQ_query_param_end
712 GNUNET_SQ_bind (plugin->zone_to_name,
716 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
717 "sqlite3_bind_XXXX");
718 GNUNET_SQ_reset (plugin->dbh,
719 plugin->zone_to_name);
720 return GNUNET_SYSERR;
722 LOG (GNUNET_ERROR_TYPE_DEBUG,
723 "Performing reverse lookup for `%s'\n",
724 GNUNET_GNSRECORD_z2s (value_zone));
725 return get_records_and_call_iterator (plugin,
726 plugin->zone_to_name,
735 * Entry point for the plugin.
737 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
738 * @return NULL on error, otherwise the plugin context
741 libgnunet_plugin_namestore_sqlite_init (void *cls)
743 static struct Plugin plugin;
744 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
745 struct GNUNET_NAMESTORE_PluginFunctions *api;
747 if (NULL != plugin.cfg)
748 return NULL; /* can only initialize once! */
751 sizeof(struct Plugin));
753 if (GNUNET_OK != database_setup (&plugin))
755 database_shutdown (&plugin);
758 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
760 api->store_records = &namestore_sqlite_store_records;
761 api->iterate_records = &namestore_sqlite_iterate_records;
762 api->zone_to_name = &namestore_sqlite_zone_to_name;
763 api->lookup_records = &namestore_sqlite_lookup_records;
764 LOG (GNUNET_ERROR_TYPE_INFO,
765 _ ("Sqlite database running\n"));
771 * Exit point from the plugin.
773 * @param cls the plugin context (as returned by "init")
774 * @return always NULL
777 libgnunet_plugin_namestore_sqlite_done (void *cls)
779 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
780 struct Plugin *plugin = api->cls;
782 database_shutdown (plugin);
785 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 "sqlite plugin is finished\n");
791 /* end of plugin_namestore_sqlite.c */