paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / identity-provider / plugin_identity_provider_sqlite.c
1  /*
2   * This file is part of GNUnet
3   * Copyright (C) 2009-2017 GNUnet e.V.
4   *
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.
9   *
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.
14   *
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/>.
17   */
18
19 /**
20  * @file identity-provider/plugin_identity_provider_sqlite.c
21  * @brief sqlite-based idp backend
22  * @author Martin Schanzenbach
23  */
24
25 #include "platform.h"
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"
30 #include <sqlite3.h>
31
32 /**
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).
37  *
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.
41  */
42 #define BUSY_TIMEOUT_MS 1000
43
44
45 /**
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).
49  */
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)
51
52 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
53
54
55 /**
56  * Context for all functions in this plugin.
57  */
58 struct Plugin
59 {
60
61   const struct GNUNET_CONFIGURATION_Handle *cfg;
62
63   /**
64    * Database filename.
65    */
66   char *fn;
67
68   /**
69    * Native SQLite database handle.
70    */
71   sqlite3 *dbh;
72
73   /**
74    * Precompiled SQL to store ticket.
75    */
76   sqlite3_stmt *store_ticket;
77
78   /**
79    * Precompiled SQL to delete existing ticket.
80    */
81   sqlite3_stmt *delete_ticket;
82
83   /**
84    * Precompiled SQL to iterate tickets.
85    */
86   sqlite3_stmt *iterate_tickets;
87
88   /**
89    * Precompiled SQL to get ticket attributes.
90    */
91   sqlite3_stmt *get_ticket_attrs;
92   
93   /**
94    * Precompiled SQL to iterate tickets by audience.
95    */
96   sqlite3_stmt *iterate_tickets_by_audience;
97 };
98
99
100 /**
101  * @brief Prepare a SQL statement
102  *
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
107  */
108 static int
109 sq_prepare (sqlite3 *dbh,
110             const char *zSql,
111             sqlite3_stmt **ppStmt)
112 {
113   char *dummy;
114   int result;
115
116   result =
117       sqlite3_prepare_v2 (dbh,
118                           zSql,
119                           strlen (zSql),
120                           ppStmt,
121                           (const char **) &dummy);
122   LOG (GNUNET_ERROR_TYPE_DEBUG,
123        "Prepared `%s' / %p: %d\n",
124        zSql,
125        *ppStmt,
126        result);
127   return result;
128 }
129
130 /**
131  * Create our database indices.
132  *
133  * @param dbh handle to the database
134  */
135 static void
136 create_indices (sqlite3 * dbh)
137 {
138   /* create indices */
139   if ( (SQLITE_OK !=
140         sqlite3_exec (dbh,
141                       "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
142                       NULL, NULL, NULL)) ||
143        (SQLITE_OK !=
144         sqlite3_exec (dbh,
145                       "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
146                       NULL, NULL, NULL)) )
147     LOG (GNUNET_ERROR_TYPE_ERROR,
148          "Failed to create indices: %s\n",
149          sqlite3_errmsg (dbh));
150 }
151
152
153
154 #if 0
155 #define CHECK(a) GNUNET_break(a)
156 #define ENULL NULL
157 #else
158 #define ENULL &e
159 #define ENULL_DEFINED 1
160 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
161 #endif
162
163
164 /**
165  * Initialize the database connections and associated
166  * data structures (create tables and indices
167  * as needed as well).
168  *
169  * @param plugin the plugin context (state for this module)
170  * @return #GNUNET_OK on success
171  */
172 static int
173 database_setup (struct Plugin *plugin)
174 {
175   sqlite3_stmt *stmt;
176   char *afsdir;
177 #if ENULL_DEFINED
178   char *e;
179 #endif
180
181   if (GNUNET_OK !=
182       GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
183                                                "identity-provider-sqlite",
184                                                "FILENAME",
185                                                &afsdir))
186   {
187     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
188                                "identity-provider-sqlite",
189                                "FILENAME");
190     return GNUNET_SYSERR;
191   }
192   if (GNUNET_OK !=
193       GNUNET_DISK_file_test (afsdir))
194   {
195     if (GNUNET_OK !=
196         GNUNET_DISK_directory_create_for_file (afsdir))
197     {
198       GNUNET_break (0);
199       GNUNET_free (afsdir);
200       return GNUNET_SYSERR;
201     }
202   }
203   /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
204   plugin->fn = afsdir;
205
206   /* Open database and precompile statements */
207   if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
208   {
209     LOG (GNUNET_ERROR_TYPE_ERROR,
210          _("Unable to initialize SQLite: %s.\n"),
211          sqlite3_errmsg (plugin->dbh));
212     return GNUNET_SYSERR;
213   }
214   CHECK (SQLITE_OK ==
215          sqlite3_exec (plugin->dbh,
216                        "PRAGMA temp_store=MEMORY", NULL, NULL,
217                        ENULL));
218   CHECK (SQLITE_OK ==
219          sqlite3_exec (plugin->dbh,
220                        "PRAGMA synchronous=NORMAL", NULL, NULL,
221                        ENULL));
222   CHECK (SQLITE_OK ==
223          sqlite3_exec (plugin->dbh,
224                        "PRAGMA legacy_file_format=OFF", NULL, NULL,
225                        ENULL));
226   CHECK (SQLITE_OK ==
227          sqlite3_exec (plugin->dbh,
228                        "PRAGMA auto_vacuum=INCREMENTAL", NULL,
229                        NULL, ENULL));
230   CHECK (SQLITE_OK ==
231          sqlite3_exec (plugin->dbh,
232                        "PRAGMA encoding=\"UTF-8\"", NULL,
233                        NULL, ENULL));
234   CHECK (SQLITE_OK ==
235          sqlite3_exec (plugin->dbh,
236                        "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
237                        ENULL));
238   CHECK (SQLITE_OK ==
239          sqlite3_exec (plugin->dbh,
240                        "PRAGMA page_size=4092", NULL, NULL,
241                        ENULL));
242
243   CHECK (SQLITE_OK ==
244          sqlite3_busy_timeout (plugin->dbh,
245                                BUSY_TIMEOUT_MS));
246
247
248   /* Create table */
249   CHECK (SQLITE_OK ==
250          sq_prepare (plugin->dbh,
251                      "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
252                      &stmt));
253   if ((sqlite3_step (stmt) == SQLITE_DONE) &&
254       (sqlite3_exec
255        (plugin->dbh,
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 ''"
261         ")",
262         NULL, NULL, NULL) != SQLITE_OK))
263   {
264     LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
265                 "sqlite3_exec");
266     sqlite3_finalize (stmt);
267     return GNUNET_SYSERR;
268   }
269   sqlite3_finalize (stmt);
270
271   create_indices (plugin->dbh);
272
273   if ( (SQLITE_OK !=
274         sq_prepare (plugin->dbh,
275                     "INSERT INTO identity001tickets (identity, audience, rnd, attributes)"
276                     " VALUES (?, ?, ?, ?)",
277                     &plugin->store_ticket)) ||
278        (SQLITE_OK !=
279         sq_prepare (plugin->dbh,
280                     "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
281                     &plugin->delete_ticket)) ||
282        (SQLITE_OK !=
283         sq_prepare (plugin->dbh,
284                     "SELECT identity,audience,rnd,attributes"
285                     " FROM identity001tickets WHERE identity=? AND rnd=?",
286                     &plugin->get_ticket_attrs)) ||
287        (SQLITE_OK !=
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)) ||
293        (SQLITE_OK !=
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)) ) 
299   {
300     LOG_SQLITE (plugin,
301                 GNUNET_ERROR_TYPE_ERROR,
302                 "precompiling");
303     return GNUNET_SYSERR;
304   }
305   return GNUNET_OK;
306 }
307
308
309 /**
310  * Shutdown database connection and associate data
311  * structures.
312  * @param plugin the plugin context (state for this module)
313  */
314 static void
315 database_shutdown (struct Plugin *plugin)
316 {
317   int result;
318   sqlite3_stmt *stmt;
319
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)
332   {
333     LOG (GNUNET_ERROR_TYPE_WARNING,
334          _("Tried to close sqlite without finalizing all prepared statements.\n"));
335     stmt = sqlite3_next_stmt (plugin->dbh,
336                               NULL);
337     while (NULL != stmt)
338     {
339       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
340                        "sqlite",
341                        "Closing statement %p\n",
342                        stmt);
343       result = sqlite3_finalize (stmt);
344       if (result != SQLITE_OK)
345         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
346                          "sqlite",
347                          "Failed to close statement %p: %d\n",
348                          stmt,
349                          result);
350       stmt = sqlite3_next_stmt (plugin->dbh,
351                                 NULL);
352     }
353     result = sqlite3_close (plugin->dbh);
354   }
355   if (SQLITE_OK != result)
356     LOG_SQLITE (plugin,
357                 GNUNET_ERROR_TYPE_ERROR,
358                 "sqlite3_close");
359
360   GNUNET_free_non_null (plugin->fn);
361 }
362
363
364 /**
365  * Store a ticket in the database.
366  *
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
371  */
372 static int
373 identity_provider_sqlite_store_ticket (void *cls,
374                                        const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
375                                        const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
376 {
377   struct Plugin *plugin = cls;
378   size_t attrs_len;
379   char *attrs_ser;
380   int n;
381
382   { 
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
388     };
389     if (GNUNET_OK !=
390         GNUNET_SQ_bind (plugin->delete_ticket,
391                         dparams))
392     {
393       LOG_SQLITE (plugin,
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;
399     }
400     n = sqlite3_step (plugin->delete_ticket);
401     GNUNET_SQ_reset (plugin->dbh,
402                      plugin->delete_ticket);
403     
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,
407                               attrs_ser);
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
414     };
415
416     if (GNUNET_OK !=
417         GNUNET_SQ_bind (plugin->store_ticket,
418                         sparams))
419     {
420       LOG_SQLITE (plugin,
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;
426     }
427     n = sqlite3_step (plugin->store_ticket);
428     GNUNET_SQ_reset (plugin->dbh,
429                      plugin->store_ticket);
430     GNUNET_free (attrs_ser);
431   }
432   switch (n)
433   {
434     case SQLITE_DONE:
435       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
436                        "sqlite",
437                        "Ticket stored\n");
438       return GNUNET_OK;
439     case SQLITE_BUSY:
440       LOG_SQLITE (plugin,
441                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
442                   "sqlite3_step");
443       return GNUNET_NO;
444     default:
445       LOG_SQLITE (plugin,
446                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
447                   "sqlite3_step");
448       return GNUNET_SYSERR;
449   }
450 }
451
452
453 /**
454  * Store a ticket in the database.
455  *
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
459  */
460 static int
461 identity_provider_sqlite_delete_ticket (void *cls,
462                                         const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
463 {
464   struct Plugin *plugin = cls;
465   int n;
466
467   {  
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
472     };
473
474     if (GNUNET_OK !=
475         GNUNET_SQ_bind (plugin->delete_ticket,
476                         sparams))
477     {
478       LOG_SQLITE (plugin,
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;
484     }
485     n = sqlite3_step (plugin->delete_ticket);
486     GNUNET_SQ_reset (plugin->dbh,
487                      plugin->delete_ticket);
488   }
489   switch (n)
490   {
491     case SQLITE_DONE:
492       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
493                        "sqlite",
494                        "Ticket deleted\n");
495       return GNUNET_OK;
496     case SQLITE_BUSY:
497       LOG_SQLITE (plugin,
498                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
499                   "sqlite3_step");
500       return GNUNET_NO;
501     default:
502       LOG_SQLITE (plugin,
503                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
504                   "sqlite3_step");
505       return GNUNET_SYSERR;
506   }
507 }
508
509
510 /**
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.
514  *
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
520  */
521 static int
522 get_ticket_and_call_iterator (struct Plugin *plugin,
523                               sqlite3_stmt *stmt,
524                               GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
525                               void *iter_cls)
526 {
527   struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
528   struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
529   int ret;
530   int sret;
531   size_t attrs_len;
532   char *attrs_ser;
533
534   ret = GNUNET_NO;
535   if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
536   {
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,
542                                            &attrs_len),
543       GNUNET_SQ_result_spec_end
544
545     };
546     ret = GNUNET_SQ_extract_result (stmt,
547                                     rs);
548     if (GNUNET_OK != ret)
549     {
550       GNUNET_break (0);
551       ret = GNUNET_SYSERR;
552     }
553     else
554     {
555       attrs = GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (attrs_ser,
556                                           attrs_len);
557       if (NULL != iter)
558         iter (iter_cls,
559               &ticket,
560               attrs);
561       GNUNET_IDENTITY_ATTRIBUTE_list_destroy (attrs);
562       ret = GNUNET_YES;
563     }
564     GNUNET_SQ_cleanup_result (rs);
565   }
566   else
567   {
568     if (SQLITE_DONE != sret)
569       LOG_SQLITE (plugin,
570                   GNUNET_ERROR_TYPE_ERROR,
571                   "sqlite_step");
572   }
573   GNUNET_SQ_reset (plugin->dbh,
574                    stmt);
575   return ret;
576 }
577
578
579 /**
580  * Lookup tickets in the datastore.
581  *
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
587  */
588 static int
589 identity_provider_sqlite_ticket_get_attrs (void *cls,
590                                            const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
591                                            GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
592                                            void *iter_cls)
593 {
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
599   };
600
601   if (GNUNET_OK !=
602       GNUNET_SQ_bind (plugin->get_ticket_attrs,
603                       params))
604   {
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;
610   }
611   return get_ticket_and_call_iterator (plugin,
612                                        plugin->get_ticket_attrs,
613                                        iter,
614                                        iter_cls);
615 }
616
617
618 /**
619  * Iterate over the results for a particular key and zone in the
620  * datastore.  Will return at most one result to the iterator.
621  *
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
629  */
630 static int
631 identity_provider_sqlite_iterate_tickets (void *cls,
632                                           const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
633                                           int audience,
634                                           uint64_t offset,
635                                           GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
636                                           void *iter_cls)
637 {
638   struct Plugin *plugin = cls;
639   sqlite3_stmt *stmt;
640   int err;
641
642   if (NULL == identity)
643   {
644     GNUNET_break (0);
645     return GNUNET_SYSERR;
646   }
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
651   };
652   if (GNUNET_YES == audience)
653   {
654     stmt = plugin->iterate_tickets_by_audience;
655     err = GNUNET_SQ_bind (stmt,
656                           params);
657   }
658   else
659   {
660     stmt = plugin->iterate_tickets;
661     err = GNUNET_SQ_bind (stmt,
662                           params);
663   }
664   if (GNUNET_OK != err)
665   {
666     LOG_SQLITE (plugin,
667                 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
668                 "sqlite3_bind_XXXX");
669     GNUNET_SQ_reset (plugin->dbh,
670                      stmt);
671     return GNUNET_SYSERR;
672   }
673   return get_ticket_and_call_iterator (plugin,
674                                        stmt,
675                                        iter,
676                                        iter_cls);
677 }
678
679
680 /**
681  * Entry point for the plugin.
682  *
683  * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
684  * @return NULL on error, otherwise the plugin context
685  */
686 void *
687 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
688 {
689   static struct Plugin plugin;
690   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
691   struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
692
693   if (NULL != plugin.cfg)
694     return NULL;                /* can only initialize once! */
695   memset (&plugin, 0, sizeof (struct Plugin));
696   plugin.cfg = cfg;
697   if (GNUNET_OK != database_setup (&plugin))
698   {
699     database_shutdown (&plugin);
700     return NULL;
701   }
702   api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
703   api->cls = &plugin;
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"));
710   return api;
711 }
712
713
714 /**
715  * Exit point from the plugin.
716  *
717  * @param cls the plugin context (as returned by "init")
718  * @return always NULL
719  */
720 void *
721 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
722 {
723   struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
724   struct Plugin *plugin = api->cls;
725
726   database_shutdown (plugin);
727   plugin->cfg = NULL;
728   GNUNET_free (api);
729   LOG (GNUNET_ERROR_TYPE_DEBUG,
730        "sqlite plugin is finished\n");
731   return NULL;
732 }
733
734 /* end of plugin_identity_provider_sqlite.c */