LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_validation.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 52 68 76.5 %
Date: 2017-09-17 17:24:28 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2016, 2017 GNUnet e.V.
       4             : 
       5             :   TALER 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             :   TALER 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             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : 
      17             : /**
      18             :  * @file taler-exchange-httpd_validation.c
      19             :  * @brief helpers for calling the wire plugins to validate addresses
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <gnunet/gnunet_util_lib.h>
      24             : #include "taler-exchange-httpd.h"
      25             : #include "taler-exchange-httpd_validation.h"
      26             : #include "taler-exchange-httpd_wire.h"
      27             : #include "taler_wire_lib.h"
      28             : 
      29             : 
      30             : /**
      31             :  * Information we keep for each plugin.
      32             :  */
      33             : struct Plugin
      34             : {
      35             : 
      36             :   /**
      37             :    * We keep plugins in a DLL.
      38             :    */
      39             :   struct Plugin *next;
      40             : 
      41             :   /**
      42             :    * We keep plugins in a DLL.
      43             :    */
      44             :   struct Plugin *prev;
      45             : 
      46             :   /**
      47             :    * Type of the wireformat.
      48             :    */
      49             :   char *type;
      50             : 
      51             :   /**
      52             :    * Pointer to the plugin.
      53             :    */
      54             :   struct TALER_WIRE_Plugin *plugin;
      55             : 
      56             : };
      57             : 
      58             : /**
      59             :  * Head of DLL of wire plugins.
      60             :  */
      61             : static struct Plugin *wire_head;
      62             : 
      63             : /**
      64             :  * Tail of DLL of wire plugins.
      65             :  */
      66             : static struct Plugin *wire_tail;
      67             : 
      68             : 
      69             : /**
      70             :  * Load plugin @a name.
      71             :  *
      72             :  * @param cls pointer to `int` to set to #GNUNET_SYSERR on errors
      73             :  * @param name name of the plugin to load
      74             :  */
      75             : static void
      76           5 : load_plugin (void *cls,
      77             :              const char *name)
      78             : {
      79           5 :   int *ret = cls;
      80             :   struct Plugin *p;
      81             :   json_t *fees;
      82             : 
      83           5 :   p = GNUNET_new (struct Plugin);
      84           5 :   p->type = GNUNET_strdup (name);
      85           5 :   p->plugin = TALER_WIRE_plugin_load (cfg,
      86             :                                       name);
      87           5 :   if (NULL == p->plugin)
      88             :   {
      89           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      90             :                 "Failed to load plugin %s\n",
      91             :                 name);
      92           0 :     GNUNET_free (p->type);
      93           0 :     GNUNET_free (p);
      94           0 :     *ret = GNUNET_SYSERR;
      95           0 :     return;
      96             :   }
      97           5 :   fees = TEH_WIRE_get_fees (name);
      98           5 :   if (NULL == fees)
      99             :   {
     100           1 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     101             :                 "Disabling method `%s' as wire transfer fees are not given correctly\n",
     102             :                 name);
     103           1 :     GNUNET_free (p->type);
     104           1 :     GNUNET_free (p);
     105           1 :     *ret = GNUNET_SYSERR;
     106           1 :     return;
     107             :   }
     108           4 :   json_decref (fees);
     109           4 :   GNUNET_CONTAINER_DLL_insert (wire_head,
     110             :                                wire_tail,
     111             :                                p);
     112             : }
     113             : 
     114             : 
     115             : /**
     116             :  * Initialize validation subsystem.
     117             :  *
     118             :  * @param cfg configuration to use
     119             :  * @return #GNUNET_OK on success
     120             :  */
     121             : int
     122           3 : TEH_VALIDATION_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
     123             : {
     124             :   int ret;
     125             : 
     126           3 :   ret = GNUNET_OK;
     127           3 :   TALER_WIRE_find_enabled (cfg,
     128             :                            &load_plugin,
     129             :                            &ret);
     130           3 :   if (NULL == wire_head)
     131             :   {
     132           1 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     133             :                 "Failed to find properly configured wire transfer method\n");
     134           1 :     ret = GNUNET_SYSERR;
     135             :   }
     136           3 :   if (GNUNET_OK != ret)
     137           1 :     TEH_VALIDATION_done ();
     138           3 :   return ret;
     139             : }
     140             : 
     141             : 
     142             : /**
     143             :  * Shutdown validation subsystem.
     144             :  */
     145             : void
     146           3 : TEH_VALIDATION_done ()
     147             : {
     148             :   struct Plugin *p;
     149             : 
     150          10 :   while (NULL != (p = wire_head))
     151             :   {
     152           4 :     GNUNET_CONTAINER_DLL_remove (wire_head,
     153             :                                  wire_tail,
     154             :                                  p);
     155           4 :     TALER_WIRE_plugin_unload (p->plugin);
     156           4 :     GNUNET_free (p->type);
     157           4 :     GNUNET_free (p);
     158             :   }
     159           3 : }
     160             : 
     161             : 
     162             : /**
     163             :  * Check if the given wire format JSON object is correctly formatted as
     164             :  * a wire address.
     165             :  *
     166             :  * @param wire the JSON wire format object
     167             :  * @param ours #GNUNET_YES if the signature should match our master key
     168             :  * @param[out] emsg set to error message if we return an error code
     169             :  * @return #TALER_EC_NONE if correctly formatted; otherwise error code
     170             :  */
     171             : enum TALER_ErrorCode
     172          22 : TEH_json_validate_wireformat (const json_t *wire,
     173             :                               int ours,
     174             :                               char **emsg)
     175             : {
     176             :   const char *stype;
     177             :   json_error_t error;
     178             :   struct Plugin *p;
     179             : 
     180          22 :   *emsg = NULL;
     181          22 :   if (0 != json_unpack_ex ((json_t *) wire,
     182             :                            &error, 0,
     183             :                            "{s:s}",
     184             :                            "type", &stype))
     185             :   {
     186           0 :     GNUNET_asprintf (emsg,
     187             :                      "No `type' specified in the wire details\n");
     188           0 :     return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_TYPE_MISSING;
     189             :   }
     190          24 :   for (p=wire_head; NULL != p; p = p->next)
     191          24 :     if (0 == strcasecmp (p->type,
     192             :                          stype))
     193          22 :       return p->plugin->wire_validate (p->plugin->cls,
     194             :                                        wire,
     195             :                                        (GNUNET_YES == ours)
     196             :                                        ? &TEH_master_public_key
     197             :                                        : NULL,
     198             :                                        emsg);
     199           0 :   GNUNET_asprintf (emsg,
     200             :                    "Wire format type `%s' is not supported by this exchange\n",
     201             :                    stype);
     202           0 :   return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_TYPE_UNSUPPORTED;
     203             : }
     204             : 
     205             : 
     206             : /**
     207             :  * Obtain JSON of the supported wire methods for a given
     208             :  * account name prefix.
     209             :  *
     210             :  * @return JSON array with the supported validation methods
     211             :  */
     212             : json_t *
     213           2 : TEH_VALIDATION_get_wire_methods ()
     214             : {
     215             :   json_t *methods;
     216             :   char *account_name;
     217             :   char *emsg;
     218             :   enum TALER_ErrorCode ec;
     219             : 
     220           2 :   methods = json_object ();
     221           6 :   for (struct Plugin *p=wire_head;NULL != p;p = p->next)
     222             :   {
     223           4 :     struct TALER_WIRE_Plugin *plugin = p->plugin;
     224             :     json_t *method;
     225             :     json_t *fees;
     226             : 
     227           4 :     GNUNET_asprintf (&account_name,
     228             :                      "exchange-wire-%s",
     229             :                      p->type);
     230           4 :     method = plugin->get_wire_details (plugin->cls,
     231             :                                        cfg,
     232             :                                        account_name);
     233           4 :     if (TALER_EC_NONE !=
     234             :         (ec = TEH_json_validate_wireformat (method,
     235             :                                             GNUNET_YES,
     236             :                                             &emsg)))
     237             :     {
     238           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     239             :                   "Disabling method `%s' as details are ill-formed: %s (%d)\n",
     240             :                   p->type,
     241             :                   emsg,
     242             :                   ec);
     243           0 :       GNUNET_free (emsg);
     244           0 :       json_decref (method);
     245           0 :       method = NULL;
     246             :     }
     247           4 :     fees = TEH_WIRE_get_fees (p->type);
     248           4 :     if (NULL == fees)
     249             :     {
     250           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     251             :                   "Disabling method `%s' as wire transfer fees are not given correctly\n",
     252             :                   p->type);
     253           0 :       json_decref (method);
     254           0 :       method = NULL;
     255             :     }
     256             :     else
     257             :     {
     258           4 :       json_object_set_new (method,
     259             :                            "fees",
     260             :                            fees);
     261             :     }
     262             : 
     263           4 :     if (NULL != method)
     264           4 :       json_object_set_new (methods,
     265           4 :                            p->type,
     266             :                            method);
     267           4 :     GNUNET_free (account_name);
     268             :   }
     269           2 :   return methods;
     270             : }
     271             : 
     272             : 
     273             : /* end of taler-exchange-httpd_validation.c */

Generated by: LCOV version 1.13