LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_management_wire_disable.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 42 66.7 %
Date: 2025-07-03 11:36:01 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020-2023 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_disable.c
      18             :  * @brief Handle request to disable wire account.
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "taler/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/taler_json_lib.h"
      28             : #include "taler/taler_mhd_lib.h"
      29             : #include "taler-exchange-httpd_management.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : #include "taler-exchange-httpd_keys.h"
      32             : 
      33             : 
      34             : /**
      35             :  * Closure for the #del_wire transaction.
      36             :  */
      37             : struct DelWireContext
      38             : {
      39             :   /**
      40             :    * Master signature affirming the WIRE DEL operation
      41             :    * (includes timestamp).
      42             :    */
      43             :   struct TALER_MasterSignatureP master_sig;
      44             : 
      45             :   /**
      46             :    * Payto:// URI this is about.
      47             :    */
      48             :   struct TALER_FullPayto payto_uri;
      49             : 
      50             :   /**
      51             :    * Timestamp for checking against replay attacks.
      52             :    */
      53             :   struct GNUNET_TIME_Timestamp validity_end;
      54             : 
      55             : };
      56             : 
      57             : 
      58             : /**
      59             :  * Function implementing database transaction to del an wire.  Runs the
      60             :  * transaction logic; IF it returns a non-error code, the transaction logic
      61             :  * MUST NOT queue a MHD response.  IF it returns an hard error, the
      62             :  * transaction logic MUST queue a MHD response and set @a mhd_ret.  IF it
      63             :  * returns the soft error code, the function MAY be called again to retry and
      64             :  * MUST not queue a MHD response.
      65             :  *
      66             :  * @param cls closure with a `struct DelWireContext`
      67             :  * @param connection MHD request which triggered the transaction
      68             :  * @param[out] mhd_ret set to MHD response status for @a connection,
      69             :  *             if transaction failed (!)
      70             :  * @return transaction status
      71             :  */
      72             : static enum GNUNET_DB_QueryStatus
      73           4 : del_wire (void *cls,
      74             :           struct MHD_Connection *connection,
      75             :           MHD_RESULT *mhd_ret)
      76             : {
      77           4 :   struct DelWireContext *awc = cls;
      78             :   struct GNUNET_TIME_Timestamp last_date;
      79             :   enum GNUNET_DB_QueryStatus qs;
      80             : 
      81           4 :   qs = TEH_plugin->lookup_wire_timestamp (TEH_plugin->cls,
      82             :                                           awc->payto_uri,
      83             :                                           &last_date);
      84           4 :   if (qs < 0)
      85             :   {
      86           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      87           0 :       return qs;
      88           0 :     GNUNET_break (0);
      89           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
      90             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
      91             :                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
      92             :                                            "lookup wire");
      93           0 :     return qs;
      94             :   }
      95           4 :   if (GNUNET_TIME_timestamp_cmp (last_date,
      96             :                                  >,
      97             :                                  awc->validity_end))
      98             :   {
      99           0 :     *mhd_ret = TALER_MHD_reply_with_error (
     100             :       connection,
     101             :       MHD_HTTP_CONFLICT,
     102             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_MORE_RECENT_PRESENT,
     103             :       NULL);
     104           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     105             :   }
     106           4 :   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     107             :   {
     108           2 :     *mhd_ret = TALER_MHD_reply_with_error (
     109             :       connection,
     110             :       MHD_HTTP_NOT_FOUND,
     111             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_NOT_FOUND,
     112             :       NULL);
     113           2 :     return GNUNET_DB_STATUS_HARD_ERROR;
     114             :   }
     115           2 :   qs = TEH_plugin->update_wire (TEH_plugin->cls,
     116             :                                 awc->payto_uri,
     117             :                                 NULL,
     118             :                                 NULL,
     119             :                                 NULL,
     120             :                                 awc->validity_end,
     121             :                                 NULL,
     122             :                                 NULL,
     123             :                                 0,
     124             :                                 false);
     125           2 :   if (qs < 0)
     126             :   {
     127           0 :     GNUNET_break (0);
     128           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     129           0 :       return qs;
     130           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     131             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     132             :                                            TALER_EC_GENERIC_DB_STORE_FAILED,
     133             :                                            "del wire");
     134           0 :     return qs;
     135             :   }
     136           2 :   return qs;
     137             : }
     138             : 
     139             : 
     140             : MHD_RESULT
     141           6 : TEH_handler_management_post_wire_disable (
     142             :   struct MHD_Connection *connection,
     143             :   const json_t *root)
     144             : {
     145             :   struct DelWireContext awc;
     146             :   struct GNUNET_JSON_Specification spec[] = {
     147           6 :     GNUNET_JSON_spec_fixed_auto ("master_sig_del",
     148             :                                  &awc.master_sig),
     149           6 :     TALER_JSON_spec_full_payto_uri ("payto_uri",
     150             :                                     &awc.payto_uri),
     151           6 :     GNUNET_JSON_spec_timestamp ("validity_end",
     152             :                                 &awc.validity_end),
     153           6 :     GNUNET_JSON_spec_end ()
     154             :   };
     155             : 
     156             :   {
     157             :     enum GNUNET_GenericReturnValue res;
     158             : 
     159           6 :     res = TALER_MHD_parse_json_data (connection,
     160             :                                      root,
     161             :                                      spec);
     162           6 :     if (GNUNET_SYSERR == res)
     163           0 :       return MHD_NO; /* hard failure */
     164           6 :     if (GNUNET_NO == res)
     165           0 :       return MHD_YES; /* failure */
     166             :   }
     167           6 :   if (GNUNET_OK !=
     168           6 :       TALER_exchange_offline_wire_del_verify (
     169             :         awc.payto_uri,
     170             :         awc.validity_end,
     171             :         &TEH_master_public_key,
     172             :         &awc.master_sig))
     173             :   {
     174           2 :     GNUNET_break_op (0);
     175           2 :     return TALER_MHD_reply_with_error (
     176             :       connection,
     177             :       MHD_HTTP_FORBIDDEN,
     178             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_DEL_SIGNATURE_INVALID,
     179             :       NULL);
     180             :   }
     181             : 
     182             :   {
     183             :     enum GNUNET_GenericReturnValue res;
     184             :     MHD_RESULT ret;
     185             : 
     186           4 :     res = TEH_DB_run_transaction (connection,
     187             :                                   "del wire",
     188             :                                   TEH_MT_REQUEST_OTHER,
     189             :                                   &ret,
     190             :                                   &del_wire,
     191             :                                   &awc);
     192           4 :     if (GNUNET_SYSERR == res)
     193           2 :       return ret;
     194             :   }
     195           2 :   TEH_wire_update_state ();
     196           2 :   return TALER_MHD_reply_static (
     197             :     connection,
     198             :     MHD_HTTP_NO_CONTENT,
     199             :     NULL,
     200             :     NULL,
     201             :     0);
     202             : }
     203             : 
     204             : 
     205             : /* end of taler-exchange-httpd_management_wire_disable.c */

Generated by: LCOV version 1.16