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 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.
17 * @file identity-provider/plugin_identity_provider_sqlite.c
18 * @brief sqlite-based idp backend
19 * @author Martin Schanzenbach
23 #include "gnunet_identity_provider_service.h"
24 #include "gnunet_identity_provider_plugin.h"
25 #include "gnunet_identity_attribute_lib.h"
26 #include "gnunet_sq_lib.h"
30 * After how many ms "busy" should a DB operation fail for good? A
31 * low value makes sure that we are more responsive to requests
32 * (especially PUTs). A high value guarantees a higher success rate
33 * (SELECTs in iterate can take several seconds despite LIMIT=1).
35 * The default value of 1s should ensure that users do not experience
36 * huge latencies while at the same time allowing operations to
37 * succeed with reasonable probability.
39 #define BUSY_TIMEOUT_MS 1000
43 * Log an error message at log-level 'level' that indicates
44 * a failure of the command 'cmd' on file 'filename'
45 * with the message given by strerror(errno).
47 #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)
49 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
53 * Context for all functions in this plugin.
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Native SQLite database handle.
71 * Precompiled SQL to store ticket.
73 sqlite3_stmt *store_ticket;
76 * Precompiled SQL to delete existing ticket.
78 sqlite3_stmt *delete_ticket;
81 * Precompiled SQL to iterate tickets.
83 sqlite3_stmt *iterate_tickets;
86 * Precompiled SQL to get ticket attributes.
88 sqlite3_stmt *get_ticket_attrs;
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=? AND rnd=?",
283 &plugin->get_ticket_attrs)) ||
285 sq_prepare (plugin->dbh,
286 "SELECT identity,audience,rnd,attributes"
287 " FROM identity001tickets WHERE identity=?"
288 " ORDER BY rnd LIMIT 1 OFFSET ?",
289 &plugin->iterate_tickets)) ||
291 sq_prepare (plugin->dbh,
292 "SELECT identity,audience,rnd,attributes"
293 " FROM identity001tickets WHERE audience=?"
294 " ORDER BY rnd LIMIT 1 OFFSET ?",
295 &plugin->iterate_tickets_by_audience)) )
298 GNUNET_ERROR_TYPE_ERROR,
300 return GNUNET_SYSERR;
307 * Shutdown database connection and associate data
309 * @param plugin the plugin context (state for this module)
312 database_shutdown (struct Plugin *plugin)
317 if (NULL != plugin->store_ticket)
318 sqlite3_finalize (plugin->store_ticket);
319 if (NULL != plugin->delete_ticket)
320 sqlite3_finalize (plugin->delete_ticket);
321 if (NULL != plugin->iterate_tickets)
322 sqlite3_finalize (plugin->iterate_tickets);
323 if (NULL != plugin->iterate_tickets_by_audience)
324 sqlite3_finalize (plugin->iterate_tickets_by_audience);
325 if (NULL != plugin->get_ticket_attrs)
326 sqlite3_finalize (plugin->get_ticket_attrs);
327 result = sqlite3_close (plugin->dbh);
328 if (result == SQLITE_BUSY)
330 LOG (GNUNET_ERROR_TYPE_WARNING,
331 _("Tried to close sqlite without finalizing all prepared statements.\n"));
332 stmt = sqlite3_next_stmt (plugin->dbh,
336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
338 "Closing statement %p\n",
340 result = sqlite3_finalize (stmt);
341 if (result != SQLITE_OK)
342 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
344 "Failed to close statement %p: %d\n",
347 stmt = sqlite3_next_stmt (plugin->dbh,
350 result = sqlite3_close (plugin->dbh);
352 if (SQLITE_OK != result)
354 GNUNET_ERROR_TYPE_ERROR,
357 GNUNET_free_non_null (plugin->fn);
362 * Store a ticket in the database.
364 * @param cls closure (internal context for the plugin)
365 * @param ticket the ticket to persist
366 * @param attrs the attributes associated with the ticket
367 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
370 identity_provider_sqlite_store_ticket (void *cls,
371 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
372 const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
374 struct Plugin *plugin = cls;
380 /* First delete duplicates */
381 struct GNUNET_SQ_QueryParam dparams[] = {
382 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
383 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
384 GNUNET_SQ_query_param_end
387 GNUNET_SQ_bind (plugin->delete_ticket,
391 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
392 "sqlite3_bind_XXXX");
393 GNUNET_SQ_reset (plugin->dbh,
394 plugin->delete_ticket);
395 return GNUNET_SYSERR;
397 n = sqlite3_step (plugin->delete_ticket);
398 GNUNET_SQ_reset (plugin->dbh,
399 plugin->delete_ticket);
401 attrs_len = GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (attrs);
402 attrs_ser = GNUNET_malloc (attrs_len);
403 GNUNET_IDENTITY_ATTRIBUTE_list_serialize (attrs,
405 struct GNUNET_SQ_QueryParam sparams[] = {
406 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
407 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
408 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
409 GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
410 GNUNET_SQ_query_param_end
414 GNUNET_SQ_bind (plugin->store_ticket,
418 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
419 "sqlite3_bind_XXXX");
420 GNUNET_SQ_reset (plugin->dbh,
421 plugin->store_ticket);
422 return GNUNET_SYSERR;
424 n = sqlite3_step (plugin->store_ticket);
425 GNUNET_SQ_reset (plugin->dbh,
426 plugin->store_ticket);
427 GNUNET_free (attrs_ser);
432 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
438 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
443 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
445 return GNUNET_SYSERR;
451 * Store a ticket in the database.
453 * @param cls closure (internal context for the plugin)
454 * @param ticket the ticket to delete
455 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
458 identity_provider_sqlite_delete_ticket (void *cls,
459 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
461 struct Plugin *plugin = cls;
465 struct GNUNET_SQ_QueryParam sparams[] = {
466 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
467 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
468 GNUNET_SQ_query_param_end
472 GNUNET_SQ_bind (plugin->delete_ticket,
476 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
477 "sqlite3_bind_XXXX");
478 GNUNET_SQ_reset (plugin->dbh,
479 plugin->store_ticket);
480 return GNUNET_SYSERR;
482 n = sqlite3_step (plugin->delete_ticket);
483 GNUNET_SQ_reset (plugin->dbh,
484 plugin->delete_ticket);
489 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
495 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
500 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
502 return GNUNET_SYSERR;
508 * The given 'sqlite' statement has been prepared to be run.
509 * It will return a record which should be given to the iterator.
510 * Runs the statement and parses the returned record.
512 * @param plugin plugin context
513 * @param stmt to run (and then clean up)
514 * @param iter iterator to call with the result
515 * @param iter_cls closure for @a iter
516 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
519 get_ticket_and_call_iterator (struct Plugin *plugin,
521 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
524 struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
525 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
532 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
534 struct GNUNET_SQ_ResultSpec rs[] = {
535 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
536 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
537 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
538 GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
540 GNUNET_SQ_result_spec_end
543 ret = GNUNET_SQ_extract_result (stmt,
545 if (GNUNET_OK != ret)
552 attrs = GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (attrs_ser,
558 GNUNET_IDENTITY_ATTRIBUTE_list_destroy (attrs);
561 GNUNET_SQ_cleanup_result (rs);
565 if (SQLITE_DONE != sret)
567 GNUNET_ERROR_TYPE_ERROR,
570 GNUNET_SQ_reset (plugin->dbh,
577 * Lookup tickets in the datastore.
579 * @param cls closure (internal context for the plugin)
580 * @param ticket the ticket to retrieve attributes for
581 * @param iter function to call with the result
582 * @param iter_cls closure for @a iter
583 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
586 identity_provider_sqlite_ticket_get_attrs (void *cls,
587 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
588 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
591 struct Plugin *plugin = cls;
592 struct GNUNET_SQ_QueryParam params[] = {
593 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
594 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
595 GNUNET_SQ_query_param_end
599 GNUNET_SQ_bind (plugin->get_ticket_attrs,
602 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
603 "sqlite3_bind_XXXX");
604 GNUNET_SQ_reset (plugin->dbh,
605 plugin->get_ticket_attrs);
606 return GNUNET_SYSERR;
608 return get_ticket_and_call_iterator (plugin,
609 plugin->get_ticket_attrs,
616 * Iterate over the results for a particular key and zone in the
617 * datastore. Will return at most one result to the iterator.
619 * @param cls closure (internal context for the plugin)
620 * @param identity the issuing identity or audience (depending on audience switch)
621 * @param audience GNUNET_YES if identity is audience
622 * @param offset offset in the list of all matching records
623 * @param iter function to call with the result
624 * @param iter_cls closure for @a iter
625 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
628 identity_provider_sqlite_iterate_tickets (void *cls,
629 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
632 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
635 struct Plugin *plugin = cls;
639 if (NULL == identity)
642 return GNUNET_SYSERR;
644 struct GNUNET_SQ_QueryParam params[] = {
645 GNUNET_SQ_query_param_auto_from_type (identity),
646 GNUNET_SQ_query_param_uint64 (&offset),
647 GNUNET_SQ_query_param_end
649 if (GNUNET_YES == audience)
651 stmt = plugin->iterate_tickets_by_audience;
652 err = GNUNET_SQ_bind (stmt,
657 stmt = plugin->iterate_tickets;
658 err = GNUNET_SQ_bind (stmt,
661 if (GNUNET_OK != err)
664 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
665 "sqlite3_bind_XXXX");
666 GNUNET_SQ_reset (plugin->dbh,
668 return GNUNET_SYSERR;
670 return get_ticket_and_call_iterator (plugin,
678 * Entry point for the plugin.
680 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
681 * @return NULL on error, otherwise the plugin context
684 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
686 static struct Plugin plugin;
687 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
688 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
690 if (NULL != plugin.cfg)
691 return NULL; /* can only initialize once! */
692 memset (&plugin, 0, sizeof (struct Plugin));
694 if (GNUNET_OK != database_setup (&plugin))
696 database_shutdown (&plugin);
699 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
701 api->store_ticket = &identity_provider_sqlite_store_ticket;
702 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
703 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
704 api->get_ticket_attributes = &identity_provider_sqlite_ticket_get_attrs;
705 LOG (GNUNET_ERROR_TYPE_INFO,
706 _("Sqlite database running\n"));
712 * Exit point from the plugin.
714 * @param cls the plugin context (as returned by "init")
715 * @return always NULL
718 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
720 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
721 struct Plugin *plugin = api->cls;
723 database_shutdown (plugin);
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "sqlite plugin is finished\n");
731 /* end of plugin_identity_provider_sqlite.c */