LCOV - code coverage report
Current view: top level - exchangedb - exchangedb_accounts.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 56 79 70.9 %
Date: 2021-04-12 06:08:44 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file exchangedb/exchangedb_accounts.c
      18             :  * @brief Logic to parse account information from the configuration
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_exchangedb_lib.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Head of list of wire accounts of the exchange.
      27             :  */
      28             : static struct TALER_EXCHANGEDB_WireAccount *wa_head;
      29             : 
      30             : /**
      31             :  * Tail of list of wire accounts of the exchange.
      32             :  */
      33             : static struct TALER_EXCHANGEDB_WireAccount *wa_tail;
      34             : 
      35             : 
      36             : /**
      37             :  * Closure of #check_for_account.
      38             :  */
      39             : struct FindAccountContext
      40             : {
      41             :   /**
      42             :    * Configuration we are using.
      43             :    */
      44             :   const struct GNUNET_CONFIGURATION_Handle *cfg;
      45             : 
      46             :   /**
      47             :    * Callback to invoke.
      48             :    */
      49             :   TALER_EXCHANGEDB_AccountCallback cb;
      50             : 
      51             :   /**
      52             :    * Closure for @e cb.
      53             :    */
      54             :   void *cb_cls;
      55             : 
      56             :   /**
      57             :    * Set to #GNUNET_SYSERR if the configuration is invalid.
      58             :    */
      59             :   int res;
      60             : };
      61             : 
      62             : 
      63             : /**
      64             :  * Check if @a section begins with "exchange-account-", and if so if the
      65             :  * "PAYTO_URI" is given. If not, a warning is printed, otherwise we also check
      66             :  * if "ENABLE_CREDIT" or "ENABLE_DEBIT" options are set to "YES" and then call
      67             :  * the callback in @a cls with all of the information gathered.
      68             :  *
      69             :  * @param cls our `struct FindAccountContext`
      70             :  * @param section name of a section in the configuration
      71             :  */
      72             : static void
      73         840 : check_for_account (void *cls,
      74             :                    const char *section)
      75             : {
      76         840 :   struct FindAccountContext *ctx = cls;
      77             :   char *method;
      78             :   char *payto_uri;
      79             : 
      80         840 :   if (0 != strncasecmp (section,
      81             :                         "exchange-account-",
      82             :                         strlen ("exchange-account-")))
      83         798 :     return;
      84          42 :   if (GNUNET_OK !=
      85          42 :       GNUNET_CONFIGURATION_get_value_string (ctx->cfg,
      86             :                                              section,
      87             :                                              "PAYTO_URI",
      88             :                                              &payto_uri))
      89             :   {
      90           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
      91             :                                section,
      92             :                                "PAYTO_URI");
      93           0 :     ctx->res = GNUNET_SYSERR;
      94           0 :     return;
      95             :   }
      96          42 :   method = TALER_payto_get_method (payto_uri);
      97          42 :   if (NULL == method)
      98             :   {
      99           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     100             :                 "payto URI in config ([%s]/PAYTO_URI) malformed\n",
     101             :                 section);
     102           0 :     ctx->res = GNUNET_SYSERR;
     103           0 :     GNUNET_free (payto_uri);
     104           0 :     return;
     105             :   }
     106             :   {
     107         126 :     struct TALER_EXCHANGEDB_AccountInfo ai = {
     108             :       .section_name = section,
     109             :       .method = method,
     110          42 :       .debit_enabled = (GNUNET_YES ==
     111          42 :                         GNUNET_CONFIGURATION_get_value_yesno (
     112             :                           ctx->cfg,
     113             :                           section,
     114             :                           "ENABLE_DEBIT")),
     115          42 :       .credit_enabled = (GNUNET_YES ==
     116          42 :                          GNUNET_CONFIGURATION_get_value_yesno (ctx->cfg,
     117             :                                                                section,
     118             :                                                                "ENABLE_CREDIT"))
     119             :     };
     120             : 
     121          42 :     ctx->cb (ctx->cb_cls,
     122             :              &ai);
     123             :   }
     124          42 :   GNUNET_free (payto_uri);
     125          42 :   GNUNET_free (method);
     126             : }
     127             : 
     128             : 
     129             : /**
     130             :  * Parse the configuration to find account information.
     131             :  *
     132             :  * @param cfg configuration to use
     133             :  * @param cb callback to invoke
     134             :  * @param cb_cls closure for @a cb
     135             :  * @return #GNUNET_OK if the configuration seems valid, #GNUNET_SYSERR if not
     136             :  */
     137             : int
     138          42 : TALER_EXCHANGEDB_find_accounts (const struct GNUNET_CONFIGURATION_Handle *cfg,
     139             :                                 TALER_EXCHANGEDB_AccountCallback cb,
     140             :                                 void *cb_cls)
     141             : {
     142          42 :   struct FindAccountContext ctx = {
     143             :     .cfg = cfg,
     144             :     .cb = cb,
     145             :     .cb_cls = cb_cls,
     146             :     .res = GNUNET_OK
     147             :   };
     148             : 
     149          42 :   GNUNET_CONFIGURATION_iterate_sections (cfg,
     150             :                                          &check_for_account,
     151             :                                          &ctx);
     152          42 :   return ctx.res;
     153             : }
     154             : 
     155             : 
     156             : /**
     157             :  * Find the wire plugin for the given payto:// URL
     158             :  *
     159             :  * @param method wire method we need an account for
     160             :  * @return NULL on error
     161             :  */
     162             : struct TALER_EXCHANGEDB_WireAccount *
     163          28 : TALER_EXCHANGEDB_find_account_by_method (const char *method)
     164             : {
     165          28 :   for (struct TALER_EXCHANGEDB_WireAccount *wa = wa_head; NULL != wa; wa =
     166             :          wa->next)
     167          28 :     if (0 == strcmp (method,
     168          28 :                      wa->method))
     169          28 :       return wa;
     170           0 :   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     171             :               "No wire account known for method `%s'\n",
     172             :               method);
     173           0 :   return NULL;
     174             : }
     175             : 
     176             : 
     177             : /**
     178             :  * Find the wire plugin for the given payto:// URL
     179             :  *
     180             :  * @param url wire address we need an account for
     181             :  * @return NULL on error
     182             :  */
     183             : struct TALER_EXCHANGEDB_WireAccount *
     184          16 : TALER_EXCHANGEDB_find_account_by_payto_uri (const char *url)
     185             : {
     186             :   char *method;
     187             :   struct TALER_EXCHANGEDB_WireAccount *wa;
     188             : 
     189          16 :   method = TALER_payto_get_method (url);
     190          16 :   if (NULL == method)
     191             :   {
     192           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     193             :                 "Invalid payto:// URL `%s'\n",
     194             :                 url);
     195           0 :     return NULL;
     196             :   }
     197          16 :   wa = TALER_EXCHANGEDB_find_account_by_method (method);
     198          16 :   GNUNET_free (method);
     199          16 :   return wa;
     200             : }
     201             : 
     202             : 
     203             : /**
     204             :  * Function called with information about a wire account.  Adds
     205             :  * the account to our list.
     206             :  *
     207             :  * @param cls closure, a `struct GNUNET_CONFIGURATION_Handle`
     208             :  * @param ai account information
     209             :  */
     210             : static void
     211          42 : add_account_cb (void *cls,
     212             :                 const struct TALER_EXCHANGEDB_AccountInfo *ai)
     213             : {
     214          42 :   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
     215             :   struct TALER_EXCHANGEDB_WireAccount *wa;
     216             :   char *payto_uri;
     217             : 
     218             :   (void) cls;
     219          42 :   if (GNUNET_YES != ai->debit_enabled)
     220           0 :     return; /* not enabled for us, skip */
     221          42 :   wa = GNUNET_new (struct TALER_EXCHANGEDB_WireAccount);
     222          42 :   if (GNUNET_OK !=
     223          42 :       GNUNET_CONFIGURATION_get_value_string (cfg,
     224             :                                              ai->section_name,
     225             :                                              "PAYTO_URI",
     226             :                                              &payto_uri))
     227             :   {
     228           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     229             :                                ai->section_name,
     230             :                                "PAYTO_URI");
     231           0 :     GNUNET_free (wa);
     232           0 :     return;
     233             :   }
     234          42 :   wa->method = TALER_payto_get_method (payto_uri);
     235          42 :   if (NULL == wa->method)
     236             :   {
     237           0 :     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
     238             :                                ai->section_name,
     239             :                                "PAYTO_URI",
     240             :                                "could not obtain wire method from URI");
     241           0 :     GNUNET_free (wa);
     242           0 :     return;
     243             :   }
     244          42 :   GNUNET_free (payto_uri);
     245          42 :   if (GNUNET_OK !=
     246          42 :       TALER_BANK_auth_parse_cfg (cfg,
     247             :                                  ai->section_name,
     248             :                                  &wa->auth))
     249             :   {
     250           0 :     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
     251             :                 "Failed to load exchange account `%s'\n",
     252             :                 ai->section_name);
     253           0 :     GNUNET_free (wa->method);
     254           0 :     GNUNET_free (wa);
     255           0 :     return;
     256             :   }
     257          42 :   wa->section_name = GNUNET_strdup (ai->section_name);
     258          42 :   GNUNET_CONTAINER_DLL_insert (wa_head,
     259             :                                wa_tail,
     260             :                                wa);
     261             : }
     262             : 
     263             : 
     264             : /**
     265             :  * Load account information opf the exchange from
     266             :  * @a cfg.
     267             :  *
     268             :  * @param cfg configuration to load from
     269             :  * @return #GNUNET_OK on success, #GNUNET_NO if no accounts are configured
     270             :  */
     271             : int
     272          42 : TALER_EXCHANGEDB_load_accounts (const struct GNUNET_CONFIGURATION_Handle *cfg)
     273             : {
     274          42 :   TALER_EXCHANGEDB_find_accounts (cfg,
     275             :                                   &add_account_cb,
     276             :                                   (void *) cfg);
     277          42 :   if (NULL == wa_head)
     278           0 :     return GNUNET_NO;
     279          42 :   return GNUNET_OK;
     280             : }
     281             : 
     282             : 
     283             : /**
     284             :  * Free resources allocated by
     285             :  * #TALER_EXCHANGEDB_load_accounts().
     286             :  */
     287             : void
     288          42 : TALER_EXCHANGEDB_unload_accounts (void)
     289             : {
     290             :   struct TALER_EXCHANGEDB_WireAccount *wa;
     291             : 
     292          84 :   while (NULL != (wa = wa_head))
     293             :   {
     294          42 :     GNUNET_CONTAINER_DLL_remove (wa_head,
     295             :                                  wa_tail,
     296             :                                  wa);
     297          42 :     TALER_BANK_auth_free (&wa->auth);
     298          42 :     GNUNET_free (wa->section_name);
     299          42 :     GNUNET_free (wa->method);
     300          42 :     GNUNET_free (wa);
     301             :   }
     302          42 : }
     303             : 
     304             : 
     305             : /* end of exchangedb_accounts.c */

Generated by: LCOV version 1.14