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
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file identity-provider/plugin_identity_provider_sqlite.c
23 * @brief sqlite-based idp backend
24 * @author Martin Schanzenbach
28 #include "gnunet_identity_provider_service.h"
29 #include "gnunet_identity_provider_plugin.h"
30 #include "gnunet_sq_lib.h"
34 * After how many ms "busy" should a DB operation fail for good? A
35 * low value makes sure that we are more responsive to requests
36 * (especially PUTs). A high value guarantees a higher success rate
37 * (SELECTs in iterate can take several seconds despite LIMIT=1).
39 * The default value of 1s should ensure that users do not experience
40 * huge latencies while at the same time allowing operations to
41 * succeed with reasonable probability.
43 #define BUSY_TIMEOUT_MS 1000
47 * Log an error message at log-level 'level' that indicates
48 * a failure of the command 'cmd' on file 'filename'
49 * with the message given by strerror(errno).
51 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "identity-provider", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
53 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
57 * Context for all functions in this plugin.
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * Native SQLite database handle.
75 * Precompiled SQL to store ticket.
77 sqlite3_stmt *store_ticket;
80 * Precompiled SQL to delete existing ticket.
82 sqlite3_stmt *delete_ticket;
85 * Precompiled SQL to iterate tickets.
87 sqlite3_stmt *iterate_tickets;
90 * Precompiled SQL to iterate tickets by audience.
92 sqlite3_stmt *iterate_tickets_by_audience;
97 * @brief Prepare a SQL statement
99 * @param dbh handle to the database
100 * @param zSql SQL statement, UTF-8 encoded
101 * @param ppStmt set to the prepared statement
102 * @return 0 on success
105 sq_prepare (sqlite3 *dbh,
107 sqlite3_stmt **ppStmt)
113 sqlite3_prepare_v2 (dbh,
117 (const char **) &dummy);
118 LOG (GNUNET_ERROR_TYPE_DEBUG,
119 "Prepared `%s' / %p: %d\n",
127 * Create our database indices.
129 * @param dbh handle to the database
132 create_indices (sqlite3 * dbh)
137 "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
138 NULL, NULL, NULL)) ||
141 "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
143 LOG (GNUNET_ERROR_TYPE_ERROR,
144 "Failed to create indices: %s\n",
145 sqlite3_errmsg (dbh));
151 #define CHECK(a) GNUNET_break(a)
155 #define ENULL_DEFINED 1
156 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
161 * Initialize the database connections and associated
162 * data structures (create tables and indices
163 * as needed as well).
165 * @param plugin the plugin context (state for this module)
166 * @return #GNUNET_OK on success
169 database_setup (struct Plugin *plugin)
178 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
179 "identity-provider-sqlite",
183 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
184 "identity-provider-sqlite",
186 return GNUNET_SYSERR;
189 GNUNET_DISK_file_test (afsdir))
192 GNUNET_DISK_directory_create_for_file (afsdir))
195 GNUNET_free (afsdir);
196 return GNUNET_SYSERR;
199 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
202 /* Open database and precompile statements */
203 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
205 LOG (GNUNET_ERROR_TYPE_ERROR,
206 _("Unable to initialize SQLite: %s.\n"),
207 sqlite3_errmsg (plugin->dbh));
208 return GNUNET_SYSERR;
211 sqlite3_exec (plugin->dbh,
212 "PRAGMA temp_store=MEMORY", NULL, NULL,
215 sqlite3_exec (plugin->dbh,
216 "PRAGMA synchronous=NORMAL", NULL, NULL,
219 sqlite3_exec (plugin->dbh,
220 "PRAGMA legacy_file_format=OFF", NULL, NULL,
223 sqlite3_exec (plugin->dbh,
224 "PRAGMA auto_vacuum=INCREMENTAL", NULL,
227 sqlite3_exec (plugin->dbh,
228 "PRAGMA encoding=\"UTF-8\"", NULL,
231 sqlite3_exec (plugin->dbh,
232 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
235 sqlite3_exec (plugin->dbh,
236 "PRAGMA page_size=4092", NULL, NULL,
240 sqlite3_busy_timeout (plugin->dbh,
246 sq_prepare (plugin->dbh,
247 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
249 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
252 "CREATE TABLE identity001tickets ("
253 " identity BLOB NOT NULL DEFAULT '',"
254 " audience BLOB NOT NULL DEFAULT '',"
255 " rnd INT8 NOT NULL DEFAULT ''"
257 NULL, NULL, NULL) != SQLITE_OK))
259 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
261 sqlite3_finalize (stmt);
262 return GNUNET_SYSERR;
264 sqlite3_finalize (stmt);
266 create_indices (plugin->dbh);
269 sq_prepare (plugin->dbh,
270 "INSERT INTO identity001tickets (identity, audience, rnd)"
272 &plugin->store_ticket)) ||
274 sq_prepare (plugin->dbh,
275 "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
276 &plugin->delete_ticket)) ||
278 sq_prepare (plugin->dbh,
279 "SELECT identity,audience,rnd"
280 " FROM identity001tickets WHERE identity=?"
281 " ORDER BY rnd LIMIT 1 OFFSET ?",
282 &plugin->iterate_tickets)) ||
284 sq_prepare (plugin->dbh,
285 "SELECT identity,audience,rnd"
286 " FROM identity001tickets WHERE audience=?"
287 " ORDER BY rnd LIMIT 1 OFFSET ?",
288 &plugin->iterate_tickets_by_audience)) )
291 GNUNET_ERROR_TYPE_ERROR,
293 return GNUNET_SYSERR;
300 * Shutdown database connection and associate data
302 * @param plugin the plugin context (state for this module)
305 database_shutdown (struct Plugin *plugin)
310 if (NULL != plugin->store_ticket)
311 sqlite3_finalize (plugin->store_ticket);
312 if (NULL != plugin->delete_ticket)
313 sqlite3_finalize (plugin->delete_ticket);
314 if (NULL != plugin->iterate_tickets)
315 sqlite3_finalize (plugin->iterate_tickets);
316 if (NULL != plugin->iterate_tickets_by_audience)
317 sqlite3_finalize (plugin->iterate_tickets_by_audience);
318 result = sqlite3_close (plugin->dbh);
319 if (result == SQLITE_BUSY)
321 LOG (GNUNET_ERROR_TYPE_WARNING,
322 _("Tried to close sqlite without finalizing all prepared statements.\n"));
323 stmt = sqlite3_next_stmt (plugin->dbh,
327 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
329 "Closing statement %p\n",
331 result = sqlite3_finalize (stmt);
332 if (result != SQLITE_OK)
333 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
335 "Failed to close statement %p: %d\n",
338 stmt = sqlite3_next_stmt (plugin->dbh,
341 result = sqlite3_close (plugin->dbh);
343 if (SQLITE_OK != result)
345 GNUNET_ERROR_TYPE_ERROR,
348 GNUNET_free_non_null (plugin->fn);
353 * Store a ticket in the database.
355 * @param cls closure (internal context for the plugin)
356 * @param ticket the ticket to persist
357 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
360 identity_provider_sqlite_store_ticket (void *cls,
361 const struct GNUNET_IDENTITY_PROVIDER_Ticket2 *ticket)
363 struct Plugin *plugin = cls;
367 /* First delete duplicates */
368 struct GNUNET_SQ_QueryParam dparams[] = {
369 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
370 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
371 GNUNET_SQ_query_param_end
374 GNUNET_SQ_bind (plugin->delete_ticket,
378 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
379 "sqlite3_bind_XXXX");
380 GNUNET_SQ_reset (plugin->dbh,
381 plugin->delete_ticket);
382 return GNUNET_SYSERR;
384 n = sqlite3_step (plugin->delete_ticket);
385 GNUNET_SQ_reset (plugin->dbh,
386 plugin->delete_ticket);
388 struct GNUNET_SQ_QueryParam sparams[] = {
389 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
390 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
391 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
392 GNUNET_SQ_query_param_end
396 GNUNET_SQ_bind (plugin->store_ticket,
400 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
401 "sqlite3_bind_XXXX");
402 GNUNET_SQ_reset (plugin->dbh,
403 plugin->store_ticket);
404 return GNUNET_SYSERR;
406 n = sqlite3_step (plugin->store_ticket);
407 GNUNET_SQ_reset (plugin->dbh,
408 plugin->store_ticket);
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
419 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
424 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
426 return GNUNET_SYSERR;
432 * Store a ticket in the database.
434 * @param cls closure (internal context for the plugin)
435 * @param ticket the ticket to delete
436 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
439 identity_provider_sqlite_delete_ticket (void *cls,
440 const struct GNUNET_IDENTITY_PROVIDER_Ticket2 *ticket)
442 struct Plugin *plugin = cls;
446 struct GNUNET_SQ_QueryParam sparams[] = {
447 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
448 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
449 GNUNET_SQ_query_param_end
453 GNUNET_SQ_bind (plugin->delete_ticket,
457 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
458 "sqlite3_bind_XXXX");
459 GNUNET_SQ_reset (plugin->dbh,
460 plugin->store_ticket);
461 return GNUNET_SYSERR;
463 n = sqlite3_step (plugin->delete_ticket);
464 GNUNET_SQ_reset (plugin->dbh,
465 plugin->delete_ticket);
470 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
476 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
481 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
483 return GNUNET_SYSERR;
489 * The given 'sqlite' statement has been prepared to be run.
490 * It will return a record which should be given to the iterator.
491 * Runs the statement and parses the returned record.
493 * @param plugin plugin context
494 * @param stmt to run (and then clean up)
495 * @param iter iterator to call with the result
496 * @param iter_cls closure for @a iter
497 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
500 get_ticket_and_call_iterator (struct Plugin *plugin,
502 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
505 struct GNUNET_IDENTITY_PROVIDER_Ticket2 ticket;
510 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
512 struct GNUNET_SQ_ResultSpec rs[] = {
513 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
514 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
515 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
516 GNUNET_SQ_result_spec_end
519 ret = GNUNET_SQ_extract_result (stmt,
521 if (GNUNET_OK != ret)
533 GNUNET_SQ_cleanup_result (rs);
537 if (SQLITE_DONE != sret)
539 GNUNET_ERROR_TYPE_ERROR,
542 GNUNET_SQ_reset (plugin->dbh,
548 * Iterate over the results for a particular key and zone in the
549 * datastore. Will return at most one result to the iterator.
551 * @param cls closure (internal context for the plugin)
552 * @param identity the issuing identity or audience (depending on audience switch)
553 * @param audience GNUNET_YES if identity is audience
554 * @param offset offset in the list of all matching records
555 * @param iter function to call with the result
556 * @param iter_cls closure for @a iter
557 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
560 identity_provider_sqlite_iterate_tickets (void *cls,
561 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
564 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
567 struct Plugin *plugin = cls;
571 if (NULL == identity)
574 return GNUNET_SYSERR;
576 struct GNUNET_SQ_QueryParam params[] = {
577 GNUNET_SQ_query_param_auto_from_type (identity),
578 GNUNET_SQ_query_param_uint64 (&offset),
579 GNUNET_SQ_query_param_end
581 if (GNUNET_YES == audience)
583 stmt = plugin->iterate_tickets_by_audience;
584 err = GNUNET_SQ_bind (stmt,
589 stmt = plugin->iterate_tickets;
590 err = GNUNET_SQ_bind (stmt,
593 if (GNUNET_OK != err)
596 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
597 "sqlite3_bind_XXXX");
598 GNUNET_SQ_reset (plugin->dbh,
600 return GNUNET_SYSERR;
602 return get_ticket_and_call_iterator (plugin,
610 * Entry point for the plugin.
612 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
613 * @return NULL on error, otherwise the plugin context
616 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
618 static struct Plugin plugin;
619 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
620 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
622 if (NULL != plugin.cfg)
623 return NULL; /* can only initialize once! */
624 memset (&plugin, 0, sizeof (struct Plugin));
626 if (GNUNET_OK != database_setup (&plugin))
628 database_shutdown (&plugin);
631 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
633 api->store_ticket = &identity_provider_sqlite_store_ticket;
634 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
635 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
636 LOG (GNUNET_ERROR_TYPE_INFO,
637 _("Sqlite database running\n"));
643 * Exit point from the plugin.
645 * @param cls the plugin context (as returned by "init")
646 * @return always NULL
649 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
651 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
652 struct Plugin *plugin = api->cls;
654 database_shutdown (plugin);
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "sqlite plugin is finished\n");
662 /* end of plugin_identity_provider_sqlite.c */