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