frosix

Multiparty signature service (experimental)
Log | Files | Refs | README | LICENSE

frosix_authorization_plugin.c (5267B)


      1 /*
      2   This file is part of Frosix
      3   Copyright (C) 2015, 2016, 2021 Anastasis SARL
      4 
      5   Frosix is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Frosix is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   Frosix; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file frosix_authorization_plugin.c
     18  * @brief Logic to load authorization plugin
     19  * @author Christian Grothoff
     20  * @author Dominik Meister
     21  */
     22 #include "platform.h"
     23 #include "frosix_authorization_lib.h"
     24 #include <ltdl.h>
     25 
     26 
     27 /**
     28  * Head of linked list for all loaded plugins
     29  */
     30 static struct AuthPlugin *ap_head;
     31 
     32 /**
     33  * Tail ofinked list for all loaded plugins
     34  */
     35 static struct AuthPlugin *ap_tail;
     36 
     37 
     38 /**
     39  * Authentication plugin which is used to verify code based authentication
     40  * like SMS, E-Mail.
     41  */
     42 struct AuthPlugin
     43 {
     44   /**
     45    * Kept in a DLL.
     46    */
     47   struct AuthPlugin *next;
     48 
     49   /**
     50    * Kept in a DLL.
     51    */
     52   struct AuthPlugin *prev;
     53 
     54   /**
     55    * Actual plugin handle.
     56    */
     57   struct FROSIX_AuthorizationPlugin *authorization;
     58 
     59   /**
     60    * I.e. "sms", "phone".
     61    */
     62   char *name;
     63 
     64   /**
     65    * Name of the shared object providing the plugin logic.
     66    */
     67   char *lib_name;
     68 
     69   /**
     70    * Authorization context passed to the plugin.
     71    */
     72   struct FROSIX_AuthorizationContext ac;
     73 
     74 };
     75 
     76 
     77 struct FROSIX_AuthorizationPlugin *
     78 FROSIX_authorization_plugin_load (
     79   const char *method,
     80   struct FROSIX_DatabasePlugin *db,
     81   const struct GNUNET_CONFIGURATION_Handle *FH_cfg)
     82 {
     83   struct FROSIX_AuthorizationPlugin *authorization;
     84   char *lib_name;
     85   char *sec_name;
     86   struct AuthPlugin *ap;
     87   char *currency;
     88   struct TALER_Amount cost;
     89 
     90   for (ap = ap_head; NULL != ap; ap = ap->next)
     91     if (0 == strcmp (method,
     92                      ap->name))
     93       return ap->authorization;
     94   if (GNUNET_OK !=
     95       TALER_config_get_currency (FH_cfg,
     96                                  &currency))
     97     return NULL;
     98   ap = GNUNET_new (struct AuthPlugin);
     99   ap->ac.db = db;
    100   ap->ac.cfg = FH_cfg;
    101   GNUNET_asprintf (&sec_name,
    102                    "authorization-%s",
    103                    method);
    104   if (GNUNET_OK !=
    105       TALER_config_get_amount (FH_cfg,
    106                                sec_name,
    107                                "COST",
    108                                &cost))
    109   {
    110     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
    111                                sec_name,
    112                                "COST");
    113     GNUNET_free (sec_name);
    114     GNUNET_free (currency);
    115     GNUNET_free (ap);
    116     return NULL;
    117   }
    118 
    119   GNUNET_free (currency);
    120   GNUNET_free (sec_name);
    121   GNUNET_asprintf (&lib_name,
    122                    "libfrosix_plugin_authorization_%s",
    123                    method);
    124   authorization = GNUNET_PLUGIN_load (lib_name,
    125                                       &ap->ac);
    126   if (NULL == authorization)
    127   {
    128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                 "Authentication method `%s' not supported\n",
    130                 method);
    131     GNUNET_free (lib_name);
    132     GNUNET_free (ap);
    133     return NULL;
    134   }
    135   authorization->cost = cost;
    136   ap->name = GNUNET_strdup (method);
    137   ap->lib_name = lib_name;
    138   ap->authorization = authorization;
    139   GNUNET_CONTAINER_DLL_insert (ap_head,
    140                                ap_tail,
    141                                ap);
    142   return authorization;
    143 }
    144 
    145 
    146 void
    147 FROSIX_authorization_plugin_shutdown (void)
    148 {
    149   struct AuthPlugin *ap;
    150 
    151   while (NULL != (ap = ap_head))
    152   {
    153     GNUNET_CONTAINER_DLL_remove (ap_head,
    154                                  ap_tail,
    155                                  ap);
    156     GNUNET_PLUGIN_unload (ap->lib_name,
    157                           ap->authorization);
    158     GNUNET_free (ap->lib_name);
    159     GNUNET_free (ap->name);
    160     GNUNET_free (ap);
    161   }
    162 }
    163 
    164 
    165 /**
    166  * Libtool search path before we started.
    167  */
    168 static char *old_dlsearchpath;
    169 
    170 
    171 /**
    172  * Setup libtool paths.
    173  */
    174 void __attribute__ ((constructor))
    175 frosix_authorization_plugin_init (void)
    176 {
    177   int err;
    178   const char *opath;
    179   char *path;
    180   char *cpath;
    181 
    182   err = lt_dlinit ();
    183   if (err > 0)
    184   {
    185     fprintf (stderr,
    186              ("Initialization of plugin mechanism failed: %s!\n"),
    187              lt_dlerror ());
    188     return;
    189   }
    190   opath = lt_dlgetsearchpath ();
    191   if (NULL != opath)
    192     old_dlsearchpath = GNUNET_strdup (opath);
    193   path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
    194   if (NULL != path)
    195   {
    196     if (NULL != opath)
    197     {
    198       GNUNET_asprintf (&cpath, "%s:%s", opath, path);
    199       lt_dlsetsearchpath (cpath);
    200       GNUNET_free (path);
    201       GNUNET_free (cpath);
    202     }
    203     else
    204     {
    205       lt_dlsetsearchpath (path);
    206       GNUNET_free (path);
    207     }
    208   }
    209 }
    210 
    211 
    212 /**
    213  * Shutdown libtool.
    214  */
    215 void __attribute__ ((destructor))
    216 frosix_authorization_plugin_fini (void)
    217 {
    218   lt_dlsetsearchpath (old_dlsearchpath);
    219   if (NULL != old_dlsearchpath)
    220   {
    221     GNUNET_free (old_dlsearchpath);
    222   }
    223   lt_dlexit ();
    224 }
    225 
    226 
    227 /* end of frosix_authorization_plugin.c */