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 get ticket attributes.
93 sqlite3_stmt *get_ticket_attrs;
96 * Precompiled SQL to iterate tickets by audience.
98 sqlite3_stmt *iterate_tickets_by_audience;
103 * @brief Prepare a SQL statement
105 * @param dbh handle to the database
106 * @param zSql SQL statement, UTF-8 encoded
107 * @param ppStmt set to the prepared statement
108 * @return 0 on success
111 sq_prepare (sqlite3 *dbh,
113 sqlite3_stmt **ppStmt)
119 sqlite3_prepare_v2 (dbh,
123 (const char **) &dummy);
124 LOG (GNUNET_ERROR_TYPE_DEBUG,
125 "Prepared `%s' / %p: %d\n",
133 * Create our database indices.
135 * @param dbh handle to the database
138 create_indices (sqlite3 * dbh)
143 "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
144 NULL, NULL, NULL)) ||
147 "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
149 LOG (GNUNET_ERROR_TYPE_ERROR,
150 "Failed to create indices: %s\n",
151 sqlite3_errmsg (dbh));
157 #define CHECK(a) GNUNET_break(a)
161 #define ENULL_DEFINED 1
162 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
167 * Initialize the database connections and associated
168 * data structures (create tables and indices
169 * as needed as well).
171 * @param plugin the plugin context (state for this module)
172 * @return #GNUNET_OK on success
175 database_setup (struct Plugin *plugin)
184 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
185 "identity-provider-sqlite",
189 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
190 "identity-provider-sqlite",
192 return GNUNET_SYSERR;
195 GNUNET_DISK_file_test (afsdir))
198 GNUNET_DISK_directory_create_for_file (afsdir))
201 GNUNET_free (afsdir);
202 return GNUNET_SYSERR;
205 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
208 /* Open database and precompile statements */
209 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
211 LOG (GNUNET_ERROR_TYPE_ERROR,
212 _("Unable to initialize SQLite: %s.\n"),
213 sqlite3_errmsg (plugin->dbh));
214 return GNUNET_SYSERR;
217 sqlite3_exec (plugin->dbh,
218 "PRAGMA temp_store=MEMORY", NULL, NULL,
221 sqlite3_exec (plugin->dbh,
222 "PRAGMA synchronous=NORMAL", NULL, NULL,
225 sqlite3_exec (plugin->dbh,
226 "PRAGMA legacy_file_format=OFF", NULL, NULL,
229 sqlite3_exec (plugin->dbh,
230 "PRAGMA auto_vacuum=INCREMENTAL", NULL,
233 sqlite3_exec (plugin->dbh,
234 "PRAGMA encoding=\"UTF-8\"", NULL,
237 sqlite3_exec (plugin->dbh,
238 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
241 sqlite3_exec (plugin->dbh,
242 "PRAGMA page_size=4092", NULL, NULL,
246 sqlite3_busy_timeout (plugin->dbh,
252 sq_prepare (plugin->dbh,
253 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
255 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
258 "CREATE TABLE identity001tickets ("
259 " identity BLOB NOT NULL DEFAULT '',"
260 " audience BLOB NOT NULL DEFAULT '',"
261 " rnd INT8 NOT NULL DEFAULT '',"
262 " attributes BLOB NOT NULL DEFAULT ''"
264 NULL, NULL, NULL) != SQLITE_OK))
266 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
268 sqlite3_finalize (stmt);
269 return GNUNET_SYSERR;
271 sqlite3_finalize (stmt);
273 create_indices (plugin->dbh);
276 sq_prepare (plugin->dbh,
277 "INSERT INTO identity001tickets (identity, audience, rnd, attributes)"
278 " VALUES (?, ?, ?, ?)",
279 &plugin->store_ticket)) ||
281 sq_prepare (plugin->dbh,
282 "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
283 &plugin->delete_ticket)) ||
285 sq_prepare (plugin->dbh,
286 "SELECT identity,audience,rnd,attributes"
287 " FROM identity001tickets WHERE identity=? AND rnd=?",
288 &plugin->get_ticket_attrs)) ||
290 sq_prepare (plugin->dbh,
291 "SELECT identity,audience,rnd,attributes"
292 " FROM identity001tickets WHERE identity=?"
293 " ORDER BY rnd LIMIT 1 OFFSET ?",
294 &plugin->iterate_tickets)) ||
296 sq_prepare (plugin->dbh,
297 "SELECT identity,audience,rnd,attributes"
298 " FROM identity001tickets WHERE audience=?"
299 " ORDER BY rnd LIMIT 1 OFFSET ?",
300 &plugin->iterate_tickets_by_audience)) )
303 GNUNET_ERROR_TYPE_ERROR,
305 return GNUNET_SYSERR;
312 * Shutdown database connection and associate data
314 * @param plugin the plugin context (state for this module)
317 database_shutdown (struct Plugin *plugin)
322 if (NULL != plugin->store_ticket)
323 sqlite3_finalize (plugin->store_ticket);
324 if (NULL != plugin->delete_ticket)
325 sqlite3_finalize (plugin->delete_ticket);
326 if (NULL != plugin->iterate_tickets)
327 sqlite3_finalize (plugin->iterate_tickets);
328 if (NULL != plugin->iterate_tickets_by_audience)
329 sqlite3_finalize (plugin->iterate_tickets_by_audience);
330 if (NULL != plugin->get_ticket_attrs)
331 sqlite3_finalize (plugin->get_ticket_attrs);
332 result = sqlite3_close (plugin->dbh);
333 if (result == SQLITE_BUSY)
335 LOG (GNUNET_ERROR_TYPE_WARNING,
336 _("Tried to close sqlite without finalizing all prepared statements.\n"));
337 stmt = sqlite3_next_stmt (plugin->dbh,
341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
343 "Closing statement %p\n",
345 result = sqlite3_finalize (stmt);
346 if (result != SQLITE_OK)
347 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
349 "Failed to close statement %p: %d\n",
352 stmt = sqlite3_next_stmt (plugin->dbh,
355 result = sqlite3_close (plugin->dbh);
357 if (SQLITE_OK != result)
359 GNUNET_ERROR_TYPE_ERROR,
362 GNUNET_free_non_null (plugin->fn);
367 * Store a ticket in the database.
369 * @param cls closure (internal context for the plugin)
370 * @param ticket the ticket to persist
371 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
374 identity_provider_sqlite_store_ticket (void *cls,
375 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
376 const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
378 struct Plugin *plugin = cls;
384 /* First delete duplicates */
385 struct GNUNET_SQ_QueryParam dparams[] = {
386 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
387 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
388 GNUNET_SQ_query_param_end
391 GNUNET_SQ_bind (plugin->delete_ticket,
395 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
396 "sqlite3_bind_XXXX");
397 GNUNET_SQ_reset (plugin->dbh,
398 plugin->delete_ticket);
399 return GNUNET_SYSERR;
401 n = sqlite3_step (plugin->delete_ticket);
402 GNUNET_SQ_reset (plugin->dbh,
403 plugin->delete_ticket);
405 attrs_len = attribute_list_serialize_get_size (attrs);
406 attrs_ser = GNUNET_malloc (attrs_len);
407 attribute_list_serialize (attrs,
409 struct GNUNET_SQ_QueryParam sparams[] = {
410 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
411 GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
412 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
413 GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
414 GNUNET_SQ_query_param_end
418 GNUNET_SQ_bind (plugin->store_ticket,
422 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
423 "sqlite3_bind_XXXX");
424 GNUNET_SQ_reset (plugin->dbh,
425 plugin->store_ticket);
426 return GNUNET_SYSERR;
428 n = sqlite3_step (plugin->store_ticket);
429 GNUNET_SQ_reset (plugin->dbh,
430 plugin->store_ticket);
431 GNUNET_free (attrs_ser);
436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
442 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
447 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
449 return GNUNET_SYSERR;
455 * Store a ticket in the database.
457 * @param cls closure (internal context for the plugin)
458 * @param ticket the ticket to delete
459 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
462 identity_provider_sqlite_delete_ticket (void *cls,
463 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
465 struct Plugin *plugin = cls;
469 struct GNUNET_SQ_QueryParam sparams[] = {
470 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
471 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
472 GNUNET_SQ_query_param_end
476 GNUNET_SQ_bind (plugin->delete_ticket,
480 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
481 "sqlite3_bind_XXXX");
482 GNUNET_SQ_reset (plugin->dbh,
483 plugin->store_ticket);
484 return GNUNET_SYSERR;
486 n = sqlite3_step (plugin->delete_ticket);
487 GNUNET_SQ_reset (plugin->dbh,
488 plugin->delete_ticket);
493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
499 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
504 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
506 return GNUNET_SYSERR;
512 * The given 'sqlite' statement has been prepared to be run.
513 * It will return a record which should be given to the iterator.
514 * Runs the statement and parses the returned record.
516 * @param plugin plugin context
517 * @param stmt to run (and then clean up)
518 * @param iter iterator to call with the result
519 * @param iter_cls closure for @a iter
520 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
523 get_ticket_and_call_iterator (struct Plugin *plugin,
525 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
528 struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
529 struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs;
536 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
538 struct GNUNET_SQ_ResultSpec rs[] = {
539 GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
540 GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
541 GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
542 GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
544 GNUNET_SQ_result_spec_end
547 ret = GNUNET_SQ_extract_result (stmt,
549 if (GNUNET_OK != ret)
556 attrs = attribute_list_deserialize (attrs_ser,
562 attribute_list_destroy (attrs);
565 GNUNET_SQ_cleanup_result (rs);
569 if (SQLITE_DONE != sret)
571 GNUNET_ERROR_TYPE_ERROR,
574 GNUNET_SQ_reset (plugin->dbh,
581 * Lookup tickets in the datastore.
583 * @param cls closure (internal context for the plugin)
584 * @param zone private key of the zone
585 * @param label name of the record in the zone
586 * @param iter function to call with the result
587 * @param iter_cls closure for @a iter
588 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
591 identity_provider_sqlite_ticket_get_attrs (void *cls,
592 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
593 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
596 struct Plugin *plugin = cls;
597 struct GNUNET_SQ_QueryParam params[] = {
598 GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
599 GNUNET_SQ_query_param_uint64 (&ticket->rnd),
600 GNUNET_SQ_query_param_end
604 GNUNET_SQ_bind (plugin->get_ticket_attrs,
607 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
608 "sqlite3_bind_XXXX");
609 GNUNET_SQ_reset (plugin->dbh,
610 plugin->get_ticket_attrs);
611 return GNUNET_SYSERR;
613 return get_ticket_and_call_iterator (plugin,
614 plugin->get_ticket_attrs,
621 * Iterate over the results for a particular key and zone in the
622 * datastore. Will return at most one result to the iterator.
624 * @param cls closure (internal context for the plugin)
625 * @param identity the issuing identity or audience (depending on audience switch)
626 * @param audience GNUNET_YES if identity is audience
627 * @param offset offset in the list of all matching records
628 * @param iter function to call with the result
629 * @param iter_cls closure for @a iter
630 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
633 identity_provider_sqlite_iterate_tickets (void *cls,
634 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
637 GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
640 struct Plugin *plugin = cls;
644 if (NULL == identity)
647 return GNUNET_SYSERR;
649 struct GNUNET_SQ_QueryParam params[] = {
650 GNUNET_SQ_query_param_auto_from_type (identity),
651 GNUNET_SQ_query_param_uint64 (&offset),
652 GNUNET_SQ_query_param_end
654 if (GNUNET_YES == audience)
656 stmt = plugin->iterate_tickets_by_audience;
657 err = GNUNET_SQ_bind (stmt,
662 stmt = plugin->iterate_tickets;
663 err = GNUNET_SQ_bind (stmt,
666 if (GNUNET_OK != err)
669 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
670 "sqlite3_bind_XXXX");
671 GNUNET_SQ_reset (plugin->dbh,
673 return GNUNET_SYSERR;
675 return get_ticket_and_call_iterator (plugin,
683 * Entry point for the plugin.
685 * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
686 * @return NULL on error, otherwise the plugin context
689 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
691 static struct Plugin plugin;
692 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
693 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
695 if (NULL != plugin.cfg)
696 return NULL; /* can only initialize once! */
697 memset (&plugin, 0, sizeof (struct Plugin));
699 if (GNUNET_OK != database_setup (&plugin))
701 database_shutdown (&plugin);
704 api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
706 api->store_ticket = &identity_provider_sqlite_store_ticket;
707 api->delete_ticket = &identity_provider_sqlite_delete_ticket;
708 api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
709 api->get_ticket_attributes = &identity_provider_sqlite_ticket_get_attrs;
710 LOG (GNUNET_ERROR_TYPE_INFO,
711 _("Sqlite database running\n"));
717 * Exit point from the plugin.
719 * @param cls the plugin context (as returned by "init")
720 * @return always NULL
723 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
725 struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
726 struct Plugin *plugin = api->cls;
728 database_shutdown (plugin);
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "sqlite plugin is finished\n");
736 /* end of plugin_identity_provider_sqlite.c */