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 "identity_attribute.h"
31 #include "gnunet_sq_lib.h"
35 * After how many ms "busy" should a DB operation fail for good? A
36 * low value makes sure that we are more responsive to requests
37 * (especially PUTs). A high value guarantees a higher success rate
38 * (SELECTs in iterate can take several seconds despite LIMIT=1).
40 * The default value of 1s should ensure that users do not experience
41 * huge latencies while at the same time allowing operations to
42 * succeed with reasonable probability.
44 #define BUSY_TIMEOUT_MS 1000
48 * Log an error message at log-level 'level' that indicates
49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno).
52 #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)
54 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
58 * Context for all functions in this plugin.
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
71 * Native SQLite database handle.
76 * Precompiled SQL to store ticket.
78 sqlite3_stmt *store_ticket;
81 * Precompiled SQL to delete existing ticket.
83 sqlite3_stmt *delete_ticket;
86 * Precompiled SQL to iterate tickets.
88 sqlite3_stmt *iterate_tickets;
91 * Precompiled SQL to iterate tickets by audience.
93 sqlite3_stmt *iterate_tickets_by_audience;
98 * @brief Prepare a SQL statement
100 * @param dbh handle to the database
101 * @param zSql SQL statement, UTF-8 encoded
102 * @param ppStmt set to the prepared statement
103 * @return 0 on success
106 sq_prepare (sqlite3 *dbh,
108 sqlite3_stmt **ppStmt)
114 sqlite3_prepare_v2 (dbh,
118 (const char **) &dummy);
119 LOG (GNUNET_ERROR_TYPE_DEBUG,
120 "Prepared `%s' / %p: %d\n",
128 * Create our database indices.
130 * @param dbh handle to the database
133 create_indices (sqlite3 * dbh)
138 "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
139 NULL, NULL, NULL)) ||
142 "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
144 LOG (GNUNET_ERROR_TYPE_ERROR,
145 "Failed to create indices: %s\n",
146 sqlite3_errmsg (dbh));
152 #define CHECK(a) GNUNET_break(a)
156 #define ENULL_DEFINED 1
157 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
162 * Initialize the database connections and associated
163 * data structures (create tables and indices
164 * as needed as well).
166 * @param plugin the plugin context (state for this module)
167 * @return #GNUNET_OK on success
170 database_setup (struct Plugin *plugin)
179 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
180 "identity-provider-sqlite",
184 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
185 "identity-provider-sqlite",
187 return GNUNET_SYSERR;
190 GNUNET_DISK_file_test (afsdir))
193 GNUNET_DISK_directory_create_for_file (afsdir))
196 GNUNET_free (afsdir);
197 return GNUNET_SYSERR;
200 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
203 /* Open database and precompile statements */
204 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
206 LOG (GNUNET_ERROR_TYPE_ERROR,
207 _("Unable to initialize SQLite: %s.\n"),
208 sqlite3_errmsg (plugin->dbh));
209 return GNUNET_SYSERR;
212 sqlite3_exec (plugin->dbh,
213 "PRAGMA temp_store=MEMORY", NULL, NULL,
216 sqlite3_exec (plugin->dbh,
217 "PRAGMA synchronous=NORMAL", NULL, NULL,
220 sqlite3_exec (plugin->dbh,
221 "PRAGMA legacy_file_format=OFF", NULL, NULL,
224 sqlite3_exec (plugin->dbh,
225 "PRAGMA auto_vacuum=INCREMENTAL", NULL,
228 sqlite3_exec (plugin->dbh,
229 "PRAGMA encoding=\"UTF-8\"", NULL,
232 sqlite3_exec (plugin->dbh,
233 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
236 sqlite3_exec (plugin->dbh,
237 "PRAGMA page_size=4092", NULL, NULL,
241 sqlite3_busy_timeout (plugin->dbh,
247 sq_prepare (plugin->dbh,
248 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
250 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
253 "CREATE TABLE identity001tickets ("
254 " identity BLOB NOT NULL DEFAULT '',"
255 " audience BLOB NOT NULL DEFAULT '',"
256 " rnd INT8 NOT NULL DEFAULT '',"
257 " attributes BLOB NOT NULL DEFAULT ''"
259 NULL, NULL, NULL) != SQLITE_OK))
261 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
263 sqlite3_finalize (stmt);
264 return GNUNET_SYSERR;
266 sqlite3_finalize (stmt);
268 create_indices (plugin->dbh);
271 sq_prepare (plugin->dbh,
272 "INSERT INTO identity001tickets (identity, audience, rnd, attributes)"
273 " VALUES (?, ?, ?, ?)",
274 &plugin->store_ticket)) ||
276 sq_prepare (plugin->dbh,
277 "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
278 &plugin->delete_ticket)) ||
280 sq_prepare (plugin->dbh,
281 "SELECT identity,audience,rnd,attributes"
282 " FROM identity001tickets WHERE identity=?"
283 " ORDER BY rnd LIMIT 1 OFFSET ?",
284 &plugin->iterate_tickets)) ||
286 sq_prepare (plugin->dbh,
287 "SELECT identity,audience,rnd,attributes"
288 " FROM identity001tickets WHERE audience=?"
289 " ORDER BY rnd LIMIT 1 OFFSET ?",
290 &plugin->iterate_tickets_by_audience)) )
293 GNUNET_ERROR_TYPE_ERROR,
295 return GNUNET_SYSERR;
302 * Shutdown database connection and associate data
304 * @param plugin the plugin context (state for this module)
307 database_shutdown (struct Plugin *plugin)
312 if (NULL != plugin->store_ticket)
313 sqlite3_finalize (plugin->store_ticket);
314 if (NULL != plugin->delete_ticket)
315 sqlite3_finalize (plugin->delete_ticket);
316 if (NULL != plugin->iterate_tickets)
317 sqlite3_finalize (plugin->iterate_tickets);
318 if (NULL != plugin->iterate_tickets_by_audience)
319 sqlite3_finalize (plugin->iterate_tickets_by_audience);
320 result = sqlite3_close (plugin->dbh);
321 if (result == SQLITE_BUSY)
323 LOG (GNUNET_ERROR_TYPE_WARNING,
324 _("Tried to close sqlite without finalizing all prepared statements.\n"));
325 stmt = sqlite3_next_stmt (plugin->dbh,
329 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
331 "Closing statement %p\n",
333 result = sqlite3_finalize (stmt);
334 if (result != SQLITE_OK)
335 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
337 "Failed to close statement %p: %d\n",
340 stmt = sqlite3_next_stmt (plugin->dbh,
343 result = sqlite3_close (plugin->dbh);
345 if (SQLITE_OK != result)
347 GNUNET_ERROR_TYPE_ERROR,
350 GNUNET_free_non_null (plugin->fn);
355 * Store a ticket in the database.
357 * @param cls closure (internal context for the plugin)
358 * @param ticket the ticket to persist
359 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
362 identity_provider_sqlite_store_ticket (void *cls,
363 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
364 const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
366 struct Plugin *plugin = cls;
372 /* First delete duplicates */
373 struct GNUNET_SQ_QueryParam dparams[] = {
374 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
375 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
376 GNUNET_SQ_query_param_end
379 GNUNET_SQ_bind (plugin->delete_ticket,
383 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
384 "sqlite3_bind_XXXX");
385 GNUNET_SQ_reset (plugin->dbh,
386 plugin->delete_ticket);
387 return GNUNET_SYSERR;
389 n = sqlite3_step (plugin->delete_ticket);
390 GNUNET_SQ_reset (plugin->dbh,
391 plugin->delete_ticket);
393 attrs_len = attribute_list_serialize_get_size (attrs);
394 attrs_ser = GNUNET_malloc (attrs_len);
395 attribute_list_serialize (attrs,
397 struct GNUNET_SQ_QueryParam sparams[] = {
398 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
399 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
400 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
401 GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
402 GNUNET_SQ_query_param_end
406 GNUNET_SQ_bind (plugin->store_ticket,
410 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
411 "sqlite3_bind_XXXX");
412 GNUNET_SQ_reset (plugin->dbh,
413 plugin->store_ticket);
414 return GNUNET_SYSERR;
416 n = sqlite3_step (plugin->store_ticket);
417 GNUNET_SQ_reset (plugin->dbh,
418 plugin->store_ticket);
419 GNUNET_free (attrs_ser);
424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
430 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
435 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
437 return GNUNET_SYSERR;
443 * Store a ticket in the database.
445 * @param cls closure (internal context for the plugin)
446 * @param ticket the ticket to delete
447 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
450 identity_provider_sqlite_delete_ticket (void *cls,
451 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
453 struct Plugin *plugin = cls;
457 struct GNUNET_SQ_QueryParam sparams[] = {
458 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
459 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
460 GNUNET_SQ_query_param_end
464 GNUNET_SQ_bind (plugin->delete_ticket,
468 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
469 "sqlite3_bind_XXXX");
470 GNUNET_SQ_reset (plugin->dbh,
471 plugin->store_ticket);
472 return GNUNET_SYSERR;
474 n = sqlite3_step (plugin->delete_ticket);
475 GNUNET_SQ_reset (plugin->dbh,
476 plugin->delete_ticket);
481 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
487 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
492 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
494 return GNUNET_SYSERR;
500 * The given 'sqlite' statement has been prepared to be run.
501 * It will return a record which should be given to the iterator.
502 * Runs the statement and parses the returned record.
504 * @param plugin plugin context
505 * @param stmt to run (and then clean up)
506 * @param iter iterator to call with the result
507 * @param iter_cls closure for @a iter
508 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
511 get_ticket_and_call_iterator (struct Plugin *plugin,
513 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
516 struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
517 struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs;
524 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
526 struct GNUNET_SQ_ResultSpec rs[] = {
527 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
528 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
529 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
530 GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
532 GNUNET_SQ_result_spec_end
535 ret = GNUNET_SQ_extract_result (stmt,
537 if (GNUNET_OK != ret)
544 attrs = attribute_list_deserialize (attrs_ser,
552 GNUNET_SQ_cleanup_result (rs);
556 if (SQLITE_DONE != sret)
558 GNUNET_ERROR_TYPE_ERROR,
561 GNUNET_SQ_reset (plugin->dbh,
567 * Iterate over the results for a particular key and zone in the
568 * datastore. Will return at most one result to the iterator.
570 * @param cls closure (internal context for the plugin)
571 * @param identity the issuing identity or audience (depending on audience switch)
572 * @param audience GNUNET_YES if identity is audience
573 * @param offset offset in the list of all matching records
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 if there were no results, #GNUNET_SYSERR on error
579 identity_provider_sqlite_iterate_tickets (void *cls,
580 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
583 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
586 struct Plugin *plugin = cls;
590 if (NULL == identity)
593 return GNUNET_SYSERR;
595 struct GNUNET_SQ_QueryParam params[] = {
596 GNUNET_SQ_query_param_auto_from_type (identity),
597 GNUNET_SQ_query_param_uint64 (&offset),
598 GNUNET_SQ_query_param_end
600 if (GNUNET_YES == audience)
602 stmt = plugin->iterate_tickets_by_audience;
603 err = GNUNET_SQ_bind (stmt,
608 stmt = plugin->iterate_tickets;
609 err = GNUNET_SQ_bind (stmt,
612 if (GNUNET_OK != err)
615 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
616 "sqlite3_bind_XXXX");
617 GNUNET_SQ_reset (plugin->dbh,
619 return GNUNET_SYSERR;
621 return get_ticket_and_call_iterator (plugin,
629 * Entry point for the plugin.
631 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
632 * @return NULL on error, otherwise the plugin context
635 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
637 static struct Plugin plugin;
638 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
639 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
641 if (NULL != plugin.cfg)
642 return NULL; /* can only initialize once! */
643 memset (&plugin, 0, sizeof (struct Plugin));
645 if (GNUNET_OK != database_setup (&plugin))
647 database_shutdown (&plugin);
650 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
652 api->store_ticket = &identity_provider_sqlite_store_ticket;
653 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
654 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
655 LOG (GNUNET_ERROR_TYPE_INFO,
656 _("Sqlite database running\n"));
662 * Exit point from the plugin.
664 * @param cls the plugin context (as returned by "init")
665 * @return always NULL
668 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
670 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
671 struct Plugin *plugin = api->cls;
673 database_shutdown (plugin);
676 LOG (GNUNET_ERROR_TYPE_DEBUG,
677 "sqlite plugin is finished\n");
681 /* end of plugin_identity_provider_sqlite.c */