-fixes
[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  * @param attrs the attributes associated with the ticket
372  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
373  */
374 static int
375 identity_provider_sqlite_store_ticket (void *cls,
376                                        const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
377                                        const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
378 {
379   struct Plugin *plugin = cls;
380   size_t attrs_len;
381   char *attrs_ser;
382   int n;
383
384   { 
385     /* First delete duplicates */
386     struct GNUNET_SQ_QueryParam dparams[] = {
387       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
388       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
389       GNUNET_SQ_query_param_end
390     };
391     if (GNUNET_OK !=
392         GNUNET_SQ_bind (plugin->delete_ticket,
393                         dparams))
394     {
395       LOG_SQLITE (plugin,
396                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
397                   "sqlite3_bind_XXXX");
398       GNUNET_SQ_reset (plugin->dbh,
399                        plugin->delete_ticket);
400       return GNUNET_SYSERR;
401     }
402     n = sqlite3_step (plugin->delete_ticket);
403     GNUNET_SQ_reset (plugin->dbh,
404                      plugin->delete_ticket);
405     
406     attrs_len = GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (attrs);
407     attrs_ser = GNUNET_malloc (attrs_len);
408     GNUNET_IDENTITY_ATTRIBUTE_list_serialize (attrs,
409                               attrs_ser);
410     struct GNUNET_SQ_QueryParam sparams[] = {
411       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
412       GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
413       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
414       GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
415       GNUNET_SQ_query_param_end
416     };
417
418     if (GNUNET_OK !=
419         GNUNET_SQ_bind (plugin->store_ticket,
420                         sparams))
421     {
422       LOG_SQLITE (plugin,
423                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
424                   "sqlite3_bind_XXXX");
425       GNUNET_SQ_reset (plugin->dbh,
426                        plugin->store_ticket);
427       return GNUNET_SYSERR;
428     }
429     n = sqlite3_step (plugin->store_ticket);
430     GNUNET_SQ_reset (plugin->dbh,
431                      plugin->store_ticket);
432     GNUNET_free (attrs_ser);
433   }
434   switch (n)
435   {
436     case SQLITE_DONE:
437       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
438                        "sqlite",
439                        "Ticket stored\n");
440       return GNUNET_OK;
441     case SQLITE_BUSY:
442       LOG_SQLITE (plugin,
443                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
444                   "sqlite3_step");
445       return GNUNET_NO;
446     default:
447       LOG_SQLITE (plugin,
448                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
449                   "sqlite3_step");
450       return GNUNET_SYSERR;
451   }
452 }
453
454
455 /**
456  * Store a ticket in the database.
457  *
458  * @param cls closure (internal context for the plugin)
459  * @param ticket the ticket to delete
460  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
461  */
462 static int
463 identity_provider_sqlite_delete_ticket (void *cls,
464                                         const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
465 {
466   struct Plugin *plugin = cls;
467   int n;
468
469   {  
470     struct GNUNET_SQ_QueryParam sparams[] = {
471       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
472       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
473       GNUNET_SQ_query_param_end
474     };
475
476     if (GNUNET_OK !=
477         GNUNET_SQ_bind (plugin->delete_ticket,
478                         sparams))
479     {
480       LOG_SQLITE (plugin,
481                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
482                   "sqlite3_bind_XXXX");
483       GNUNET_SQ_reset (plugin->dbh,
484                        plugin->store_ticket);
485       return GNUNET_SYSERR;
486     }
487     n = sqlite3_step (plugin->delete_ticket);
488     GNUNET_SQ_reset (plugin->dbh,
489                      plugin->delete_ticket);
490   }
491   switch (n)
492   {
493     case SQLITE_DONE:
494       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
495                        "sqlite",
496                        "Ticket deleted\n");
497       return GNUNET_OK;
498     case SQLITE_BUSY:
499       LOG_SQLITE (plugin,
500                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
501                   "sqlite3_step");
502       return GNUNET_NO;
503     default:
504       LOG_SQLITE (plugin,
505                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
506                   "sqlite3_step");
507       return GNUNET_SYSERR;
508   }
509 }
510
511
512 /**
513  * The given 'sqlite' statement has been prepared to be run.
514  * It will return a record which should be given to the iterator.
515  * Runs the statement and parses the returned record.
516  *
517  * @param plugin plugin context
518  * @param stmt to run (and then clean up)
519  * @param iter iterator to call with the result
520  * @param iter_cls closure for @a iter
521  * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
522  */
523 static int
524 get_ticket_and_call_iterator (struct Plugin *plugin,
525                               sqlite3_stmt *stmt,
526                               GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
527                               void *iter_cls)
528 {
529   struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
530   struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
531   int ret;
532   int sret;
533   size_t attrs_len;
534   char *attrs_ser;
535
536   ret = GNUNET_NO;
537   if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
538   {
539     struct GNUNET_SQ_ResultSpec rs[] = {
540       GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
541       GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
542       GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
543       GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
544                                            &attrs_len),
545       GNUNET_SQ_result_spec_end
546
547     };
548     ret = GNUNET_SQ_extract_result (stmt,
549                                     rs);
550     if (GNUNET_OK != ret)
551     {
552       GNUNET_break (0);
553       ret = GNUNET_SYSERR;
554     }
555     else
556     {
557       attrs = GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (attrs_ser,
558                                           attrs_len);
559       if (NULL != iter)
560         iter (iter_cls,
561               &ticket,
562               attrs);
563       GNUNET_IDENTITY_ATTRIBUTE_list_destroy (attrs);
564       ret = GNUNET_YES;
565     }
566     GNUNET_SQ_cleanup_result (rs);
567   }
568   else
569   {
570     if (SQLITE_DONE != sret)
571       LOG_SQLITE (plugin,
572                   GNUNET_ERROR_TYPE_ERROR,
573                   "sqlite_step");
574   }
575   GNUNET_SQ_reset (plugin->dbh,
576                    stmt);
577   return ret;
578 }
579
580
581 /**
582  * Lookup tickets in the datastore.
583  *
584  * @param cls closure (internal context for the plugin)
585  * @param ticket the ticket to retrieve attributes for
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 */