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/>.
20 * @file identity-provider/plugin_identity_provider_sqlite.c
21 * @brief sqlite-based idp backend
22 * @author Martin Schanzenbach
26 #include "gnunet_identity_provider_service.h"
27 #include "gnunet_identity_provider_plugin.h"
28 #include "gnunet_identity_attribute_lib.h"
29 #include "gnunet_sq_lib.h"
33 * After how many ms "busy" should a DB operation fail for good? A
34 * low value makes sure that we are more responsive to requests
35 * (especially PUTs). A high value guarantees a higher success rate
36 * (SELECTs in iterate can take several seconds despite LIMIT=1).
38 * The default value of 1s should ensure that users do not experience
39 * huge latencies while at the same time allowing operations to
40 * succeed with reasonable probability.
42 #define BUSY_TIMEOUT_MS 1000
46 * Log an error message at log-level 'level' that indicates
47 * a failure of the command 'cmd' on file 'filename'
48 * with the message given by strerror(errno).
50 #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)
52 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
56 * Context for all functions in this plugin.
61 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Native SQLite database handle.
74 * Precompiled SQL to store ticket.
76 sqlite3_stmt *store_ticket;
79 * Precompiled SQL to delete existing ticket.
81 sqlite3_stmt *delete_ticket;
84 * Precompiled SQL to iterate tickets.
86 sqlite3_stmt *iterate_tickets;
89 * Precompiled SQL to get ticket attributes.
91 sqlite3_stmt *get_ticket_attrs;
94 * Precompiled SQL to iterate tickets by audience.
96 sqlite3_stmt *iterate_tickets_by_audience;
101 * @brief Prepare a SQL statement
103 * @param dbh handle to the database
104 * @param zSql SQL statement, UTF-8 encoded
105 * @param ppStmt set to the prepared statement
106 * @return 0 on success
109 sq_prepare (sqlite3 *dbh,
111 sqlite3_stmt **ppStmt)
117 sqlite3_prepare_v2 (dbh,
121 (const char **) &dummy);
122 LOG (GNUNET_ERROR_TYPE_DEBUG,
123 "Prepared `%s' / %p: %d\n",
131 * Create our database indices.
133 * @param dbh handle to the database
136 create_indices (sqlite3 * dbh)
141 "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
142 NULL, NULL, NULL)) ||
145 "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
147 LOG (GNUNET_ERROR_TYPE_ERROR,
148 "Failed to create indices: %s\n",
149 sqlite3_errmsg (dbh));
155 #define CHECK(a) GNUNET_break(a)
159 #define ENULL_DEFINED 1
160 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
165 * Initialize the database connections and associated
166 * data structures (create tables and indices
167 * as needed as well).
169 * @param plugin the plugin context (state for this module)
170 * @return #GNUNET_OK on success
173 database_setup (struct Plugin *plugin)
182 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
183 "identity-provider-sqlite",
187 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
188 "identity-provider-sqlite",
190 return GNUNET_SYSERR;
193 GNUNET_DISK_file_test (afsdir))
196 GNUNET_DISK_directory_create_for_file (afsdir))
199 GNUNET_free (afsdir);
200 return GNUNET_SYSERR;
203 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
206 /* Open database and precompile statements */
207 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
209 LOG (GNUNET_ERROR_TYPE_ERROR,
210 _("Unable to initialize SQLite: %s.\n"),
211 sqlite3_errmsg (plugin->dbh));
212 return GNUNET_SYSERR;
215 sqlite3_exec (plugin->dbh,
216 "PRAGMA temp_store=MEMORY", NULL, NULL,
219 sqlite3_exec (plugin->dbh,
220 "PRAGMA synchronous=NORMAL", NULL, NULL,
223 sqlite3_exec (plugin->dbh,
224 "PRAGMA legacy_file_format=OFF", NULL, NULL,
227 sqlite3_exec (plugin->dbh,
228 "PRAGMA auto_vacuum=INCREMENTAL", NULL,
231 sqlite3_exec (plugin->dbh,
232 "PRAGMA encoding=\"UTF-8\"", NULL,
235 sqlite3_exec (plugin->dbh,
236 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
239 sqlite3_exec (plugin->dbh,
240 "PRAGMA page_size=4092", NULL, NULL,
244 sqlite3_busy_timeout (plugin->dbh,
250 sq_prepare (plugin->dbh,
251 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
253 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
256 "CREATE TABLE identity001tickets ("
257 " identity BLOB NOT NULL DEFAULT '',"
258 " audience BLOB NOT NULL DEFAULT '',"
259 " rnd INT8 NOT NULL DEFAULT '',"
260 " attributes BLOB NOT NULL DEFAULT ''"
262 NULL, NULL, NULL) != SQLITE_OK))
264 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
266 sqlite3_finalize (stmt);
267 return GNUNET_SYSERR;
269 sqlite3_finalize (stmt);
271 create_indices (plugin->dbh);
274 sq_prepare (plugin->dbh,
275 "INSERT INTO identity001tickets (identity, audience, rnd, attributes)"
276 " VALUES (?, ?, ?, ?)",
277 &plugin->store_ticket)) ||
279 sq_prepare (plugin->dbh,
280 "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
281 &plugin->delete_ticket)) ||
283 sq_prepare (plugin->dbh,
284 "SELECT identity,audience,rnd,attributes"
285 " FROM identity001tickets WHERE identity=? AND rnd=?",
286 &plugin->get_ticket_attrs)) ||
288 sq_prepare (plugin->dbh,
289 "SELECT identity,audience,rnd,attributes"
290 " FROM identity001tickets WHERE identity=?"
291 " ORDER BY rnd LIMIT 1 OFFSET ?",
292 &plugin->iterate_tickets)) ||
294 sq_prepare (plugin->dbh,
295 "SELECT identity,audience,rnd,attributes"
296 " FROM identity001tickets WHERE audience=?"
297 " ORDER BY rnd LIMIT 1 OFFSET ?",
298 &plugin->iterate_tickets_by_audience)) )
301 GNUNET_ERROR_TYPE_ERROR,
303 return GNUNET_SYSERR;
310 * Shutdown database connection and associate data
312 * @param plugin the plugin context (state for this module)
315 database_shutdown (struct Plugin *plugin)
320 if (NULL != plugin->store_ticket)
321 sqlite3_finalize (plugin->store_ticket);
322 if (NULL != plugin->delete_ticket)
323 sqlite3_finalize (plugin->delete_ticket);
324 if (NULL != plugin->iterate_tickets)
325 sqlite3_finalize (plugin->iterate_tickets);
326 if (NULL != plugin->iterate_tickets_by_audience)
327 sqlite3_finalize (plugin->iterate_tickets_by_audience);
328 if (NULL != plugin->get_ticket_attrs)
329 sqlite3_finalize (plugin->get_ticket_attrs);
330 result = sqlite3_close (plugin->dbh);
331 if (result == SQLITE_BUSY)
333 LOG (GNUNET_ERROR_TYPE_WARNING,
334 _("Tried to close sqlite without finalizing all prepared statements.\n"));
335 stmt = sqlite3_next_stmt (plugin->dbh,
339 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
341 "Closing statement %p\n",
343 result = sqlite3_finalize (stmt);
344 if (result != SQLITE_OK)
345 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
347 "Failed to close statement %p: %d\n",
350 stmt = sqlite3_next_stmt (plugin->dbh,
353 result = sqlite3_close (plugin->dbh);
355 if (SQLITE_OK != result)
357 GNUNET_ERROR_TYPE_ERROR,
360 GNUNET_free_non_null (plugin->fn);
365 * Store a ticket in the database.
367 * @param cls closure (internal context for the plugin)
368 * @param ticket the ticket to persist
369 * @param attrs the attributes associated with the ticket
370 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
373 identity_provider_sqlite_store_ticket (void *cls,
374 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
375 const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
377 struct Plugin *plugin = cls;
383 /* First delete duplicates */
384 struct GNUNET_SQ_QueryParam dparams[] = {
385 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
386 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
387 GNUNET_SQ_query_param_end
390 GNUNET_SQ_bind (plugin->delete_ticket,
394 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
395 "sqlite3_bind_XXXX");
396 GNUNET_SQ_reset (plugin->dbh,
397 plugin->delete_ticket);
398 return GNUNET_SYSERR;
400 n = sqlite3_step (plugin->delete_ticket);
401 GNUNET_SQ_reset (plugin->dbh,
402 plugin->delete_ticket);
404 attrs_len = GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (attrs);
405 attrs_ser = GNUNET_malloc (attrs_len);
406 GNUNET_IDENTITY_ATTRIBUTE_list_serialize (attrs,
408 struct GNUNET_SQ_QueryParam sparams[] = {
409 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
410 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
411 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
412 GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
413 GNUNET_SQ_query_param_end
417 GNUNET_SQ_bind (plugin->store_ticket,
421 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
422 "sqlite3_bind_XXXX");
423 GNUNET_SQ_reset (plugin->dbh,
424 plugin->store_ticket);
425 return GNUNET_SYSERR;
427 n = sqlite3_step (plugin->store_ticket);
428 GNUNET_SQ_reset (plugin->dbh,
429 plugin->store_ticket);
430 GNUNET_free (attrs_ser);
435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
441 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
446 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
448 return GNUNET_SYSERR;
454 * Store a ticket in the database.
456 * @param cls closure (internal context for the plugin)
457 * @param ticket the ticket to delete
458 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
461 identity_provider_sqlite_delete_ticket (void *cls,
462 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
464 struct Plugin *plugin = cls;
468 struct GNUNET_SQ_QueryParam sparams[] = {
469 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
470 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
471 GNUNET_SQ_query_param_end
475 GNUNET_SQ_bind (plugin->delete_ticket,
479 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
480 "sqlite3_bind_XXXX");
481 GNUNET_SQ_reset (plugin->dbh,
482 plugin->store_ticket);
483 return GNUNET_SYSERR;
485 n = sqlite3_step (plugin->delete_ticket);
486 GNUNET_SQ_reset (plugin->dbh,
487 plugin->delete_ticket);
492 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
498 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
503 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
505 return GNUNET_SYSERR;
511 * The given 'sqlite' statement has been prepared to be run.
512 * It will return a record which should be given to the iterator.
513 * Runs the statement and parses the returned record.
515 * @param plugin plugin context
516 * @param stmt to run (and then clean up)
517 * @param iter iterator to call with the result
518 * @param iter_cls closure for @a iter
519 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
522 get_ticket_and_call_iterator (struct Plugin *plugin,
524 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
527 struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
528 struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
535 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
537 struct GNUNET_SQ_ResultSpec rs[] = {
538 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
539 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
540 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
541 GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
543 GNUNET_SQ_result_spec_end
546 ret = GNUNET_SQ_extract_result (stmt,
548 if (GNUNET_OK != ret)
555 attrs = GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (attrs_ser,
561 GNUNET_IDENTITY_ATTRIBUTE_list_destroy (attrs);
564 GNUNET_SQ_cleanup_result (rs);
568 if (SQLITE_DONE != sret)
570 GNUNET_ERROR_TYPE_ERROR,
573 GNUNET_SQ_reset (plugin->dbh,
580 * Lookup tickets in the datastore.
582 * @param cls closure (internal context for the plugin)
583 * @param ticket the ticket to retrieve attributes for
584 * @param iter function to call with the result
585 * @param iter_cls closure for @a iter
586 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
589 identity_provider_sqlite_ticket_get_attrs (void *cls,
590 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
591 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
594 struct Plugin *plugin = cls;
595 struct GNUNET_SQ_QueryParam params[] = {
596 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
597 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
598 GNUNET_SQ_query_param_end
602 GNUNET_SQ_bind (plugin->get_ticket_attrs,
605 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
606 "sqlite3_bind_XXXX");
607 GNUNET_SQ_reset (plugin->dbh,
608 plugin->get_ticket_attrs);
609 return GNUNET_SYSERR;
611 return get_ticket_and_call_iterator (plugin,
612 plugin->get_ticket_attrs,
619 * Iterate over the results for a particular key and zone in the
620 * datastore. Will return at most one result to the iterator.
622 * @param cls closure (internal context for the plugin)
623 * @param identity the issuing identity or audience (depending on audience switch)
624 * @param audience GNUNET_YES if identity is audience
625 * @param offset offset in the list of all matching records
626 * @param iter function to call with the result
627 * @param iter_cls closure for @a iter
628 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
631 identity_provider_sqlite_iterate_tickets (void *cls,
632 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
635 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
638 struct Plugin *plugin = cls;
642 if (NULL == identity)
645 return GNUNET_SYSERR;
647 struct GNUNET_SQ_QueryParam params[] = {
648 GNUNET_SQ_query_param_auto_from_type (identity),
649 GNUNET_SQ_query_param_uint64 (&offset),
650 GNUNET_SQ_query_param_end
652 if (GNUNET_YES == audience)
654 stmt = plugin->iterate_tickets_by_audience;
655 err = GNUNET_SQ_bind (stmt,
660 stmt = plugin->iterate_tickets;
661 err = GNUNET_SQ_bind (stmt,
664 if (GNUNET_OK != err)
667 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
668 "sqlite3_bind_XXXX");
669 GNUNET_SQ_reset (plugin->dbh,
671 return GNUNET_SYSERR;
673 return get_ticket_and_call_iterator (plugin,
681 * Entry point for the plugin.
683 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
684 * @return NULL on error, otherwise the plugin context
687 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
689 static struct Plugin plugin;
690 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
691 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
693 if (NULL != plugin.cfg)
694 return NULL; /* can only initialize once! */
695 memset (&plugin, 0, sizeof (struct Plugin));
697 if (GNUNET_OK != database_setup (&plugin))
699 database_shutdown (&plugin);
702 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
704 api->store_ticket = &identity_provider_sqlite_store_ticket;
705 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
706 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
707 api->get_ticket_attributes = &identity_provider_sqlite_ticket_get_attrs;
708 LOG (GNUNET_ERROR_TYPE_INFO,
709 _("Sqlite database running\n"));
715 * Exit point from the plugin.
717 * @param cls the plugin context (as returned by "init")
718 * @return always NULL
721 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
723 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
724 struct Plugin *plugin = api->cls;
726 database_shutdown (plugin);
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "sqlite plugin is finished\n");
734 /* end of plugin_identity_provider_sqlite.c */