first batch of license fixes (boring)
[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 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
16 /**
17  * @file identity-provider/plugin_identity_provider_sqlite.c
18  * @brief sqlite-based idp backend
19  * @author Martin Schanzenbach
20  */
21
22 #include "platform.h"
23 #include "gnunet_identity_provider_service.h"
24 #include "gnunet_identity_provider_plugin.h"
25 #include "gnunet_identity_attribute_lib.h"
26 #include "gnunet_sq_lib.h"
27 #include <sqlite3.h>
28
29 /**
30  * After how many ms "busy" should a DB operation fail for good?  A
31  * low value makes sure that we are more responsive to requests
32  * (especially PUTs).  A high value guarantees a higher success rate
33  * (SELECTs in iterate can take several seconds despite LIMIT=1).
34  *
35  * The default value of 1s should ensure that users do not experience
36  * huge latencies while at the same time allowing operations to
37  * succeed with reasonable probability.
38  */
39 #define BUSY_TIMEOUT_MS 1000
40
41
42 /**
43  * Log an error message at log-level 'level' that indicates
44  * a failure of the command 'cmd' on file 'filename'
45  * with the message given by strerror(errno).
46  */
47 #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)
48
49 #define LOG(kind,...) GNUNET_log_from (kind, "identity-provider-sqlite", __VA_ARGS__)
50
51
52 /**
53  * Context for all functions in this plugin.
54  */
55 struct Plugin
56 {
57
58   const struct GNUNET_CONFIGURATION_Handle *cfg;
59
60   /**
61    * Database filename.
62    */
63   char *fn;
64
65   /**
66    * Native SQLite database handle.
67    */
68   sqlite3 *dbh;
69
70   /**
71    * Precompiled SQL to store ticket.
72    */
73   sqlite3_stmt *store_ticket;
74
75   /**
76    * Precompiled SQL to delete existing ticket.
77    */
78   sqlite3_stmt *delete_ticket;
79
80   /**
81    * Precompiled SQL to iterate tickets.
82    */
83   sqlite3_stmt *iterate_tickets;
84
85   /**
86    * Precompiled SQL to get ticket attributes.
87    */
88   sqlite3_stmt *get_ticket_attrs;
89   
90   /**
91    * Precompiled SQL to iterate tickets by audience.
92    */
93   sqlite3_stmt *iterate_tickets_by_audience;
94 };
95
96
97 /**
98  * @brief Prepare a SQL statement
99  *
100  * @param dbh handle to the database
101  * @param zSql SQL statement, UTF-8 encoded
102  * @param ppStmt set to the prepared statement
103  * @return 0 on success
104  */
105 static int
106 sq_prepare (sqlite3 *dbh,
107             const char *zSql,
108             sqlite3_stmt **ppStmt)
109 {
110   char *dummy;
111   int result;
112
113   result =
114       sqlite3_prepare_v2 (dbh,
115                           zSql,
116                           strlen (zSql),
117                           ppStmt,
118                           (const char **) &dummy);
119   LOG (GNUNET_ERROR_TYPE_DEBUG,
120        "Prepared `%s' / %p: %d\n",
121        zSql,
122        *ppStmt,
123        result);
124   return result;
125 }
126
127 /**
128  * Create our database indices.
129  *
130  * @param dbh handle to the database
131  */
132 static void
133 create_indices (sqlite3 * dbh)
134 {
135   /* create indices */
136   if ( (SQLITE_OK !=
137         sqlite3_exec (dbh,
138                       "CREATE INDEX IF NOT EXISTS identity_reverse ON identity001tickets (identity,audience)",
139                       NULL, NULL, NULL)) ||
140        (SQLITE_OK !=
141         sqlite3_exec (dbh,
142                       "CREATE INDEX IF NOT EXISTS it_iter ON identity001tickets (rnd)",
143                       NULL, NULL, NULL)) )
144     LOG (GNUNET_ERROR_TYPE_ERROR,
145          "Failed to create indices: %s\n",
146          sqlite3_errmsg (dbh));
147 }
148
149
150
151 #if 0
152 #define CHECK(a) GNUNET_break(a)
153 #define ENULL NULL
154 #else
155 #define ENULL &e
156 #define ENULL_DEFINED 1
157 #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
158 #endif
159
160
161 /**
162  * Initialize the database connections and associated
163  * data structures (create tables and indices
164  * as needed as well).
165  *
166  * @param plugin the plugin context (state for this module)
167  * @return #GNUNET_OK on success
168  */
169 static int
170 database_setup (struct Plugin *plugin)
171 {
172   sqlite3_stmt *stmt;
173   char *afsdir;
174 #if ENULL_DEFINED
175   char *e;
176 #endif
177
178   if (GNUNET_OK !=
179       GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
180                                                "identity-provider-sqlite",
181                                                "FILENAME",
182                                                &afsdir))
183   {
184     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
185                                "identity-provider-sqlite",
186                                "FILENAME");
187     return GNUNET_SYSERR;
188   }
189   if (GNUNET_OK !=
190       GNUNET_DISK_file_test (afsdir))
191   {
192     if (GNUNET_OK !=
193         GNUNET_DISK_directory_create_for_file (afsdir))
194     {
195       GNUNET_break (0);
196       GNUNET_free (afsdir);
197       return GNUNET_SYSERR;
198     }
199   }
200   /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
201   plugin->fn = afsdir;
202
203   /* Open database and precompile statements */
204   if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
205   {
206     LOG (GNUNET_ERROR_TYPE_ERROR,
207          _("Unable to initialize SQLite: %s.\n"),
208          sqlite3_errmsg (plugin->dbh));
209     return GNUNET_SYSERR;
210   }
211   CHECK (SQLITE_OK ==
212          sqlite3_exec (plugin->dbh,
213                        "PRAGMA temp_store=MEMORY", NULL, NULL,
214                        ENULL));
215   CHECK (SQLITE_OK ==
216          sqlite3_exec (plugin->dbh,
217                        "PRAGMA synchronous=NORMAL", NULL, NULL,
218                        ENULL));
219   CHECK (SQLITE_OK ==
220          sqlite3_exec (plugin->dbh,
221                        "PRAGMA legacy_file_format=OFF", NULL, NULL,
222                        ENULL));
223   CHECK (SQLITE_OK ==
224          sqlite3_exec (plugin->dbh,
225                        "PRAGMA auto_vacuum=INCREMENTAL", NULL,
226                        NULL, ENULL));
227   CHECK (SQLITE_OK ==
228          sqlite3_exec (plugin->dbh,
229                        "PRAGMA encoding=\"UTF-8\"", NULL,
230                        NULL, ENULL));
231   CHECK (SQLITE_OK ==
232          sqlite3_exec (plugin->dbh,
233                        "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
234                        ENULL));
235   CHECK (SQLITE_OK ==
236          sqlite3_exec (plugin->dbh,
237                        "PRAGMA page_size=4092", NULL, NULL,
238                        ENULL));
239
240   CHECK (SQLITE_OK ==
241          sqlite3_busy_timeout (plugin->dbh,
242                                BUSY_TIMEOUT_MS));
243
244
245   /* Create table */
246   CHECK (SQLITE_OK ==
247          sq_prepare (plugin->dbh,
248                      "SELECT 1 FROM sqlite_master WHERE tbl_name = 'identity001tickets'",
249                      &stmt));
250   if ((sqlite3_step (stmt) == SQLITE_DONE) &&
251       (sqlite3_exec
252        (plugin->dbh,
253         "CREATE TABLE identity001tickets ("
254         " identity BLOB NOT NULL DEFAULT '',"
255         " audience BLOB NOT NULL DEFAULT '',"
256               " rnd INT8 NOT NULL DEFAULT '',"
257         " attributes BLOB NOT NULL DEFAULT ''"
258         ")",
259         NULL, NULL, NULL) != SQLITE_OK))
260   {
261     LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
262                 "sqlite3_exec");
263     sqlite3_finalize (stmt);
264     return GNUNET_SYSERR;
265   }
266   sqlite3_finalize (stmt);
267
268   create_indices (plugin->dbh);
269
270   if ( (SQLITE_OK !=
271         sq_prepare (plugin->dbh,
272                     "INSERT INTO identity001tickets (identity, audience, rnd, attributes)"
273                     " VALUES (?, ?, ?, ?)",
274                     &plugin->store_ticket)) ||
275        (SQLITE_OK !=
276         sq_prepare (plugin->dbh,
277                     "DELETE FROM identity001tickets WHERE identity=? AND rnd=?",
278                     &plugin->delete_ticket)) ||
279        (SQLITE_OK !=
280         sq_prepare (plugin->dbh,
281                     "SELECT identity,audience,rnd,attributes"
282                     " FROM identity001tickets WHERE identity=? AND rnd=?",
283                     &plugin->get_ticket_attrs)) ||
284        (SQLITE_OK !=
285         sq_prepare (plugin->dbh,
286                     "SELECT identity,audience,rnd,attributes"
287                     " FROM identity001tickets WHERE identity=?"
288                     " ORDER BY rnd LIMIT 1 OFFSET ?",
289                     &plugin->iterate_tickets)) ||
290        (SQLITE_OK !=
291         sq_prepare (plugin->dbh,
292                     "SELECT identity,audience,rnd,attributes"
293                     " FROM identity001tickets WHERE audience=?"
294                     " ORDER BY rnd LIMIT 1 OFFSET ?",
295                     &plugin->iterate_tickets_by_audience)) ) 
296   {
297     LOG_SQLITE (plugin,
298                 GNUNET_ERROR_TYPE_ERROR,
299                 "precompiling");
300     return GNUNET_SYSERR;
301   }
302   return GNUNET_OK;
303 }
304
305
306 /**
307  * Shutdown database connection and associate data
308  * structures.
309  * @param plugin the plugin context (state for this module)
310  */
311 static void
312 database_shutdown (struct Plugin *plugin)
313 {
314   int result;
315   sqlite3_stmt *stmt;
316
317   if (NULL != plugin->store_ticket)
318     sqlite3_finalize (plugin->store_ticket);
319   if (NULL != plugin->delete_ticket)
320     sqlite3_finalize (plugin->delete_ticket);
321   if (NULL != plugin->iterate_tickets)
322     sqlite3_finalize (plugin->iterate_tickets);
323   if (NULL != plugin->iterate_tickets_by_audience)
324     sqlite3_finalize (plugin->iterate_tickets_by_audience);
325   if (NULL != plugin->get_ticket_attrs)
326     sqlite3_finalize (plugin->get_ticket_attrs);
327   result = sqlite3_close (plugin->dbh);
328   if (result == SQLITE_BUSY)
329   {
330     LOG (GNUNET_ERROR_TYPE_WARNING,
331          _("Tried to close sqlite without finalizing all prepared statements.\n"));
332     stmt = sqlite3_next_stmt (plugin->dbh,
333                               NULL);
334     while (NULL != stmt)
335     {
336       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
337                        "sqlite",
338                        "Closing statement %p\n",
339                        stmt);
340       result = sqlite3_finalize (stmt);
341       if (result != SQLITE_OK)
342         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
343                          "sqlite",
344                          "Failed to close statement %p: %d\n",
345                          stmt,
346                          result);
347       stmt = sqlite3_next_stmt (plugin->dbh,
348                                 NULL);
349     }
350     result = sqlite3_close (plugin->dbh);
351   }
352   if (SQLITE_OK != result)
353     LOG_SQLITE (plugin,
354                 GNUNET_ERROR_TYPE_ERROR,
355                 "sqlite3_close");
356
357   GNUNET_free_non_null (plugin->fn);
358 }
359
360
361 /**
362  * Store a ticket in the database.
363  *
364  * @param cls closure (internal context for the plugin)
365  * @param ticket the ticket to persist
366  * @param attrs the attributes associated with the ticket
367  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
368  */
369 static int
370 identity_provider_sqlite_store_ticket (void *cls,
371                                        const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
372                                        const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs)
373 {
374   struct Plugin *plugin = cls;
375   size_t attrs_len;
376   char *attrs_ser;
377   int n;
378
379   { 
380     /* First delete duplicates */
381     struct GNUNET_SQ_QueryParam dparams[] = {
382       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
383       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
384       GNUNET_SQ_query_param_end
385     };
386     if (GNUNET_OK !=
387         GNUNET_SQ_bind (plugin->delete_ticket,
388                         dparams))
389     {
390       LOG_SQLITE (plugin,
391                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
392                   "sqlite3_bind_XXXX");
393       GNUNET_SQ_reset (plugin->dbh,
394                        plugin->delete_ticket);
395       return GNUNET_SYSERR;
396     }
397     n = sqlite3_step (plugin->delete_ticket);
398     GNUNET_SQ_reset (plugin->dbh,
399                      plugin->delete_ticket);
400     
401     attrs_len = GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (attrs);
402     attrs_ser = GNUNET_malloc (attrs_len);
403     GNUNET_IDENTITY_ATTRIBUTE_list_serialize (attrs,
404                               attrs_ser);
405     struct GNUNET_SQ_QueryParam sparams[] = {
406       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
407       GNUNET_SQ_query_param_auto_from_type (&ticket->audience),
408       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
409       GNUNET_SQ_query_param_fixed_size (attrs_ser, attrs_len),
410       GNUNET_SQ_query_param_end
411     };
412
413     if (GNUNET_OK !=
414         GNUNET_SQ_bind (plugin->store_ticket,
415                         sparams))
416     {
417       LOG_SQLITE (plugin,
418                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
419                   "sqlite3_bind_XXXX");
420       GNUNET_SQ_reset (plugin->dbh,
421                        plugin->store_ticket);
422       return GNUNET_SYSERR;
423     }
424     n = sqlite3_step (plugin->store_ticket);
425     GNUNET_SQ_reset (plugin->dbh,
426                      plugin->store_ticket);
427     GNUNET_free (attrs_ser);
428   }
429   switch (n)
430   {
431     case SQLITE_DONE:
432       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
433                        "sqlite",
434                        "Ticket stored\n");
435       return GNUNET_OK;
436     case SQLITE_BUSY:
437       LOG_SQLITE (plugin,
438                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
439                   "sqlite3_step");
440       return GNUNET_NO;
441     default:
442       LOG_SQLITE (plugin,
443                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
444                   "sqlite3_step");
445       return GNUNET_SYSERR;
446   }
447 }
448
449
450 /**
451  * Store a ticket in the database.
452  *
453  * @param cls closure (internal context for the plugin)
454  * @param ticket the ticket to delete
455  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
456  */
457 static int
458 identity_provider_sqlite_delete_ticket (void *cls,
459                                         const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
460 {
461   struct Plugin *plugin = cls;
462   int n;
463
464   {  
465     struct GNUNET_SQ_QueryParam sparams[] = {
466       GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
467       GNUNET_SQ_query_param_uint64 (&ticket->rnd),
468       GNUNET_SQ_query_param_end
469     };
470
471     if (GNUNET_OK !=
472         GNUNET_SQ_bind (plugin->delete_ticket,
473                         sparams))
474     {
475       LOG_SQLITE (plugin,
476                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
477                   "sqlite3_bind_XXXX");
478       GNUNET_SQ_reset (plugin->dbh,
479                        plugin->store_ticket);
480       return GNUNET_SYSERR;
481     }
482     n = sqlite3_step (plugin->delete_ticket);
483     GNUNET_SQ_reset (plugin->dbh,
484                      plugin->delete_ticket);
485   }
486   switch (n)
487   {
488     case SQLITE_DONE:
489       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
490                        "sqlite",
491                        "Ticket deleted\n");
492       return GNUNET_OK;
493     case SQLITE_BUSY:
494       LOG_SQLITE (plugin,
495                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
496                   "sqlite3_step");
497       return GNUNET_NO;
498     default:
499       LOG_SQLITE (plugin,
500                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
501                   "sqlite3_step");
502       return GNUNET_SYSERR;
503   }
504 }
505
506
507 /**
508  * The given 'sqlite' statement has been prepared to be run.
509  * It will return a record which should be given to the iterator.
510  * Runs the statement and parses the returned record.
511  *
512  * @param plugin plugin context
513  * @param stmt to run (and then clean up)
514  * @param iter iterator to call with the result
515  * @param iter_cls closure for @a iter
516  * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
517  */
518 static int
519 get_ticket_and_call_iterator (struct Plugin *plugin,
520                               sqlite3_stmt *stmt,
521                               GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
522                               void *iter_cls)
523 {
524   struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
525   struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs;
526   int ret;
527   int sret;
528   size_t attrs_len;
529   char *attrs_ser;
530
531   ret = GNUNET_NO;
532   if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
533   {
534     struct GNUNET_SQ_ResultSpec rs[] = {
535       GNUNET_SQ_result_spec_auto_from_type (&ticket.identity),
536       GNUNET_SQ_result_spec_auto_from_type (&ticket.audience),
537       GNUNET_SQ_result_spec_uint64 (&ticket.rnd),
538       GNUNET_SQ_result_spec_variable_size ((void**)&attrs_ser,
539                                            &attrs_len),
540       GNUNET_SQ_result_spec_end
541
542     };
543     ret = GNUNET_SQ_extract_result (stmt,
544                                     rs);
545     if (GNUNET_OK != ret)
546     {
547       GNUNET_break (0);
548       ret = GNUNET_SYSERR;
549     }
550     else
551     {
552       attrs = GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (attrs_ser,
553                                           attrs_len);
554       if (NULL != iter)
555         iter (iter_cls,
556               &ticket,
557               attrs);
558       GNUNET_IDENTITY_ATTRIBUTE_list_destroy (attrs);
559       ret = GNUNET_YES;
560     }
561     GNUNET_SQ_cleanup_result (rs);
562   }
563   else
564   {
565     if (SQLITE_DONE != sret)
566       LOG_SQLITE (plugin,
567                   GNUNET_ERROR_TYPE_ERROR,
568                   "sqlite_step");
569   }
570   GNUNET_SQ_reset (plugin->dbh,
571                    stmt);
572   return ret;
573 }
574
575
576 /**
577  * Lookup tickets in the datastore.
578  *
579  * @param cls closure (internal context for the plugin)
580  * @param ticket the ticket to retrieve attributes for
581  * @param iter function to call with the result
582  * @param iter_cls closure for @a iter
583  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
584  */
585 static int
586 identity_provider_sqlite_ticket_get_attrs (void *cls,
587                                            const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
588                                            GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
589                                            void *iter_cls)
590 {
591   struct Plugin *plugin = cls;
592   struct GNUNET_SQ_QueryParam params[] = {
593     GNUNET_SQ_query_param_auto_from_type (&ticket->identity),
594     GNUNET_SQ_query_param_uint64 (&ticket->rnd),
595     GNUNET_SQ_query_param_end
596   };
597
598   if (GNUNET_OK !=
599       GNUNET_SQ_bind (plugin->get_ticket_attrs,
600                       params))
601   {
602     LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
603                 "sqlite3_bind_XXXX");
604     GNUNET_SQ_reset (plugin->dbh,
605                      plugin->get_ticket_attrs);
606     return GNUNET_SYSERR;
607   }
608   return get_ticket_and_call_iterator (plugin,
609                                        plugin->get_ticket_attrs,
610                                        iter,
611                                        iter_cls);
612 }
613
614
615 /**
616  * Iterate over the results for a particular key and zone in the
617  * datastore.  Will return at most one result to the iterator.
618  *
619  * @param cls closure (internal context for the plugin)
620  * @param identity the issuing identity or audience (depending on audience switch)
621  * @param audience GNUNET_YES if identity is audience
622  * @param offset offset in the list of all matching records
623  * @param iter function to call with the result
624  * @param iter_cls closure for @a iter
625  * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
626  */
627 static int
628 identity_provider_sqlite_iterate_tickets (void *cls,
629                                           const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
630                                           int audience,
631                                           uint64_t offset,
632                                           GNUNET_IDENTITY_PROVIDER_TicketIterator iter,
633                                           void *iter_cls)
634 {
635   struct Plugin *plugin = cls;
636   sqlite3_stmt *stmt;
637   int err;
638
639   if (NULL == identity)
640   {
641     GNUNET_break (0);
642     return GNUNET_SYSERR;
643   }
644   struct GNUNET_SQ_QueryParam params[] = {
645     GNUNET_SQ_query_param_auto_from_type (identity),
646     GNUNET_SQ_query_param_uint64 (&offset),
647     GNUNET_SQ_query_param_end
648   };
649   if (GNUNET_YES == audience)
650   {
651     stmt = plugin->iterate_tickets_by_audience;
652     err = GNUNET_SQ_bind (stmt,
653                           params);
654   }
655   else
656   {
657     stmt = plugin->iterate_tickets;
658     err = GNUNET_SQ_bind (stmt,
659                           params);
660   }
661   if (GNUNET_OK != err)
662   {
663     LOG_SQLITE (plugin,
664                 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
665                 "sqlite3_bind_XXXX");
666     GNUNET_SQ_reset (plugin->dbh,
667                      stmt);
668     return GNUNET_SYSERR;
669   }
670   return get_ticket_and_call_iterator (plugin,
671                                        stmt,
672                                        iter,
673                                        iter_cls);
674 }
675
676
677 /**
678  * Entry point for the plugin.
679  *
680  * @param cls the "struct GNUNET_IDENTITY_PROVIDER_PluginEnvironment*"
681  * @return NULL on error, otherwise the plugin context
682  */
683 void *
684 libgnunet_plugin_identity_provider_sqlite_init (void *cls)
685 {
686   static struct Plugin plugin;
687   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
688   struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api;
689
690   if (NULL != plugin.cfg)
691     return NULL;                /* can only initialize once! */
692   memset (&plugin, 0, sizeof (struct Plugin));
693   plugin.cfg = cfg;
694   if (GNUNET_OK != database_setup (&plugin))
695   {
696     database_shutdown (&plugin);
697     return NULL;
698   }
699   api = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_PluginFunctions);
700   api->cls = &plugin;
701   api->store_ticket = &identity_provider_sqlite_store_ticket;
702   api->delete_ticket = &identity_provider_sqlite_delete_ticket;
703   api->iterate_tickets = &identity_provider_sqlite_iterate_tickets;
704   api->get_ticket_attributes = &identity_provider_sqlite_ticket_get_attrs;
705   LOG (GNUNET_ERROR_TYPE_INFO,
706        _("Sqlite database running\n"));
707   return api;
708 }
709
710
711 /**
712  * Exit point from the plugin.
713  *
714  * @param cls the plugin context (as returned by "init")
715  * @return always NULL
716  */
717 void *
718 libgnunet_plugin_identity_provider_sqlite_done (void *cls)
719 {
720   struct GNUNET_IDENTITY_PROVIDER_PluginFunctions *api = cls;
721   struct Plugin *plugin = api->cls;
722
723   database_shutdown (plugin);
724   plugin->cfg = NULL;
725   GNUNET_free (api);
726   LOG (GNUNET_ERROR_TYPE_DEBUG,
727        "sqlite plugin is finished\n");
728   return NULL;
729 }
730
731 /* end of plugin_identity_provider_sqlite.c */