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 * @param attrs attributes to persist
360 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
363 identity_provider_sqlite_store_ticket (void *cls,
364 const struct GNUNET_IDENTITY_PROVIDER_Ticket2 *ticket,
365 const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
367 struct Plugin *plugin = cls;
370 char *attrs_serialized;
372 attrs_size = attribute_list_serialize_get_size (attrs);
374 attrs_serialized = GNUNET_malloc (attrs_size);
376 attribute_list_serialize (attrs,
380 struct GNUNET_SQ_QueryParam sparams[] = {
381 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
382 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
383 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
384 GNUNET_SQ_query_param_fixed_size (attrs_serialized, attrs_size),
385 GNUNET_SQ_query_param_end
389 GNUNET_SQ_bind (plugin->store_ticket,
393 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
394 "sqlite3_bind_XXXX");
395 GNUNET_SQ_reset (plugin->dbh,
396 plugin->store_ticket);
397 return GNUNET_SYSERR;
399 n = sqlite3_step (plugin->store_ticket);
400 GNUNET_SQ_reset (plugin->dbh,
401 plugin->store_ticket);
406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
412 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
417 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
419 return GNUNET_SYSERR;
425 * Store a ticket in the database.
427 * @param cls closure (internal context for the plugin)
428 * @param ticket the ticket to delete
429 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
432 identity_provider_sqlite_delete_ticket (void *cls,
433 const struct GNUNET_IDENTITY_PROVIDER_Ticket2 *ticket)
435 struct Plugin *plugin = cls;
439 struct GNUNET_SQ_QueryParam sparams[] = {
440 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
441 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
442 GNUNET_SQ_query_param_end
446 GNUNET_SQ_bind (plugin->delete_ticket,
450 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
451 "sqlite3_bind_XXXX");
452 GNUNET_SQ_reset (plugin->dbh,
453 plugin->store_ticket);
454 return GNUNET_SYSERR;
456 n = sqlite3_step (plugin->delete_ticket);
457 GNUNET_SQ_reset (plugin->dbh,
458 plugin->delete_ticket);
463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
469 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
474 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
476 return GNUNET_SYSERR;
482 * The given 'sqlite' statement has been prepared to be run.
483 * It will return a record which should be given to the iterator.
484 * Runs the statement and parses the returned record.
486 * @param plugin plugin context
487 * @param stmt to run (and then clean up)
488 * @param iter iterator to call with the result
489 * @param iter_cls closure for @a iter
490 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
493 get_ticket_and_call_iterator (struct Plugin *plugin,
495 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
498 struct GNUNET_IDENTITY_PROVIDER_Ticket2 ticket;
500 void *attrs_serialized;
505 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
507 struct GNUNET_SQ_ResultSpec rs[] = {
508 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
509 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
510 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
511 GNUNET_SQ_result_spec_variable_size (&attrs_serialized, &attrs_size),
512 GNUNET_SQ_result_spec_end
515 ret = GNUNET_SQ_extract_result (stmt,
517 if (GNUNET_OK != ret)
524 struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs;
526 attrs = attribute_list_deserialize (attrs_serialized, attrs_size);
542 GNUNET_SQ_cleanup_result (rs);
546 if (SQLITE_DONE != sret)
548 GNUNET_ERROR_TYPE_ERROR,
551 GNUNET_SQ_reset (plugin->dbh,
557 * Iterate over the results for a particular key and zone in the
558 * datastore. Will return at most one result to the iterator.
560 * @param cls closure (internal context for the plugin)
561 * @param identity the issuing identity or audience (depending on audience switch)
562 * @param audience GNUNET_YES if identity is audience
563 * @param offset offset in the list of all matching records
564 * @param iter function to call with the result
565 * @param iter_cls closure for @a iter
566 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
569 identity_provider_sqlite_iterate_tickets (void *cls,
570 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
573 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
576 struct Plugin *plugin = cls;
580 if (NULL == identity)
583 return GNUNET_SYSERR;
585 struct GNUNET_SQ_QueryParam params[] = {
586 GNUNET_SQ_query_param_auto_from_type (identity),
587 GNUNET_SQ_query_param_uint64 (&offset),
588 GNUNET_SQ_query_param_end
590 if (GNUNET_YES == audience)
592 stmt = plugin->iterate_tickets_by_audience;
593 err = GNUNET_SQ_bind (stmt,
598 stmt = plugin->iterate_tickets;
599 err = GNUNET_SQ_bind (stmt,
602 if (GNUNET_OK != err)
605 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
606 "sqlite3_bind_XXXX");
607 GNUNET_SQ_reset (plugin->dbh,
609 return GNUNET_SYSERR;
611 return get_ticket_and_call_iterator (plugin,
619 * Entry point for the plugin.
621 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
622 * @return NULL on error, otherwise the plugin context
625 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
627 static struct Plugin plugin;
628 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
629 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
631 if (NULL != plugin.cfg)
632 return NULL; /* can only initialize once! */
633 memset (&plugin, 0, sizeof (struct Plugin));
635 if (GNUNET_OK != database_setup (&plugin))
637 database_shutdown (&plugin);
640 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
642 api->store_ticket = &identity_provider_sqlite_store_ticket;
643 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
644 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
645 LOG (GNUNET_ERROR_TYPE_INFO,
646 _("Sqlite database running\n"));
652 * Exit point from the plugin.
654 * @param cls the plugin context (as returned by "init")
655 * @return always NULL
658 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
660 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
661 struct Plugin *plugin = api->cls;
663 database_shutdown (plugin);
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "sqlite plugin is finished\n");
671 /* end of plugin_identity_provider_sqlite.c */