LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_management_wire_enable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 34 53 64.2 %
Date: 2021-08-30 06:43:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 Taler Systems SA
       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             :  * @file taler-exchange-httpd_management_wire_enable.c
      18             :  * @brief Handle request to add wire account.
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include <gnunet/gnunet_json_lib.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h>
      26             : #include <pthread.h>
      27             : #include "taler_json_lib.h"
      28             : #include "taler_mhd_lib.h"
      29             : #include "taler_signatures.h"
      30             : #include "taler-exchange-httpd_management.h"
      31             : #include "taler-exchange-httpd_responses.h"
      32             : #include "taler-exchange-httpd_wire.h"
      33             : 
      34             : 
      35             : /**
      36             :  * Closure for the #add_wire transaction.
      37             :  */
      38             : struct AddWireContext
      39             : {
      40             :   /**
      41             :    * Master signature affirming the WIRE ADD operation
      42             :    * (includes timestamp).
      43             :    */
      44             :   struct TALER_MasterSignatureP master_sig_add;
      45             : 
      46             :   /**
      47             :    * Master signature to share with clients affirming the
      48             :    * wire details of the bank.
      49             :    */
      50             :   struct TALER_MasterSignatureP master_sig_wire;
      51             : 
      52             :   /**
      53             :    * Payto:// URI this is about.
      54             :    */
      55             :   const char *payto_uri;
      56             : 
      57             :   /**
      58             :    * Timestamp for checking against replay attacks.
      59             :    */
      60             :   struct GNUNET_TIME_Absolute validity_start;
      61             : 
      62             : };
      63             : 
      64             : 
      65             : /**
      66             :  * Function implementing database transaction to add an wire.  Runs the
      67             :  * transaction logic; IF it returns a non-error code, the transaction logic
      68             :  * MUST NOT queue a MHD response.  IF it returns an hard error, the
      69             :  * transaction logic MUST queue a MHD response and set @a mhd_ret.  IF it
      70             :  * returns the soft error code, the function MAY be called again to retry and
      71             :  * MUST not queue a MHD response.
      72             :  *
      73             :  * @param cls closure with a `struct AddWireContext`
      74             :  * @param connection MHD request which triggered the transaction
      75             :  * @param[out] mhd_ret set to MHD response status for @a connection,
      76             :  *             if transaction failed (!)
      77             :  * @return transaction status
      78             :  */
      79             : static enum GNUNET_DB_QueryStatus
      80          10 : add_wire (void *cls,
      81             :           struct MHD_Connection *connection,
      82             :           MHD_RESULT *mhd_ret)
      83             : {
      84          10 :   struct AddWireContext *awc = cls;
      85             :   struct GNUNET_TIME_Absolute last_date;
      86             :   enum GNUNET_DB_QueryStatus qs;
      87             : 
      88          10 :   qs = TEH_plugin->lookup_wire_timestamp (TEH_plugin->cls,
      89             :                                           awc->payto_uri,
      90             :                                           &last_date);
      91          10 :   if (qs < 0)
      92             :   {
      93           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      94           0 :       return qs;
      95           0 :     GNUNET_break (0);
      96           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
      97             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
      98             :                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
      99             :                                            "lookup wire");
     100           0 :     return qs;
     101             :   }
     102          10 :   if ( (0 < qs) &&
     103           1 :        (last_date.abs_value_us > awc->validity_start.abs_value_us) )
     104             :   {
     105           0 :     *mhd_ret = TALER_MHD_reply_with_error (
     106             :       connection,
     107             :       MHD_HTTP_CONFLICT,
     108             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_MORE_RECENT_PRESENT,
     109             :       NULL);
     110           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     111             :   }
     112          10 :   if (0 == qs)
     113           9 :     qs = TEH_plugin->insert_wire (TEH_plugin->cls,
     114             :                                   awc->payto_uri,
     115             :                                   awc->validity_start,
     116           9 :                                   &awc->master_sig_wire);
     117             :   else
     118           1 :     qs = TEH_plugin->update_wire (TEH_plugin->cls,
     119             :                                   awc->payto_uri,
     120             :                                   awc->validity_start,
     121             :                                   true);
     122          10 :   if (qs < 0)
     123             :   {
     124           0 :     GNUNET_break (0);
     125           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     126           0 :       return qs;
     127           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     128             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     129             :                                            TALER_EC_GENERIC_DB_STORE_FAILED,
     130             :                                            "add wire");
     131           0 :     return qs;
     132             :   }
     133          10 :   return qs;
     134             : }
     135             : 
     136             : 
     137             : MHD_RESULT
     138          11 : TEH_handler_management_post_wire (
     139             :   struct MHD_Connection *connection,
     140             :   const json_t *root)
     141             : {
     142             :   struct AddWireContext awc;
     143             :   struct GNUNET_JSON_Specification spec[] = {
     144          11 :     GNUNET_JSON_spec_fixed_auto ("master_sig_wire",
     145             :                                  &awc.master_sig_wire),
     146          11 :     GNUNET_JSON_spec_fixed_auto ("master_sig_add",
     147             :                                  &awc.master_sig_add),
     148          11 :     GNUNET_JSON_spec_string ("payto_uri",
     149             :                              &awc.payto_uri),
     150          11 :     TALER_JSON_spec_absolute_time ("validity_start",
     151             :                                    &awc.validity_start),
     152          11 :     GNUNET_JSON_spec_end ()
     153             :   };
     154             : 
     155             :   {
     156             :     enum GNUNET_GenericReturnValue res;
     157             : 
     158          11 :     res = TALER_MHD_parse_json_data (connection,
     159             :                                      root,
     160             :                                      spec);
     161          11 :     if (GNUNET_SYSERR == res)
     162           0 :       return MHD_NO; /* hard failure */
     163          11 :     if (GNUNET_NO == res)
     164           0 :       return MHD_YES; /* failure */
     165             :   }
     166          11 :   if (GNUNET_OK !=
     167          11 :       TALER_exchange_offline_wire_add_verify (awc.payto_uri,
     168             :                                               awc.validity_start,
     169             :                                               &TEH_master_public_key,
     170             :                                               &awc.master_sig_add))
     171             :   {
     172           1 :     GNUNET_break_op (0);
     173           1 :     return TALER_MHD_reply_with_error (
     174             :       connection,
     175             :       MHD_HTTP_FORBIDDEN,
     176             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_ADD_SIGNATURE_INVALID,
     177             :       NULL);
     178             :   }
     179          10 :   if (GNUNET_OK !=
     180          10 :       TALER_exchange_wire_signature_check (awc.payto_uri,
     181             :                                            &TEH_master_public_key,
     182             :                                            &awc.master_sig_wire))
     183             :   {
     184           0 :     GNUNET_break_op (0);
     185           0 :     return TALER_MHD_reply_with_error (
     186             :       connection,
     187             :       MHD_HTTP_FORBIDDEN,
     188             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_DETAILS_SIGNATURE_INVALID,
     189             :       NULL);
     190             :   }
     191             :   {
     192             :     char *wire_method;
     193             : 
     194          10 :     wire_method = TALER_payto_get_method (awc.payto_uri);
     195          10 :     if (NULL == wire_method)
     196             :     {
     197           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                   "payto:// URI `%s' is malformed\n",
     199             :                   awc.payto_uri);
     200           0 :       return TALER_MHD_reply_with_error (
     201             :         connection,
     202             :         MHD_HTTP_BAD_REQUEST,
     203             :         TALER_EC_GENERIC_PARAMETER_MALFORMED,
     204             :         "payto_uri");
     205             :     }
     206          10 :     GNUNET_free (wire_method);
     207             :   }
     208             : 
     209             :   {
     210             :     enum GNUNET_GenericReturnValue res;
     211             :     MHD_RESULT ret;
     212             : 
     213          10 :     res = TEH_DB_run_transaction (connection,
     214             :                                   "add wire",
     215             :                                   &ret,
     216             :                                   &add_wire,
     217             :                                   &awc);
     218          10 :     if (GNUNET_SYSERR == res)
     219           0 :       return ret;
     220             :   }
     221          10 :   TEH_wire_update_state ();
     222          10 :   return TALER_MHD_reply_static (
     223             :     connection,
     224             :     MHD_HTTP_NO_CONTENT,
     225             :     NULL,
     226             :     NULL,
     227             :     0);
     228             : }
     229             : 
     230             : 
     231             : /* end of taler-exchange-httpd_management_wire.c */

Generated by: LCOV version 1.14