LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_management_wire_disable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 28 42 66.7 %
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_disable.c
      18             :  * @brief Handle request to disable 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-exchange-httpd_management.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : #include "taler-exchange-httpd_wire.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             :   const char *payto_uri;
      49             : 
      50             :   /**
      51             :    * Timestamp for checking against replay attacks.
      52             :    */
      53             :   struct GNUNET_TIME_Absolute 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           2 : del_wire (void *cls,
      74             :           struct MHD_Connection *connection,
      75             :           MHD_RESULT *mhd_ret)
      76             : {
      77           2 :   struct DelWireContext *awc = cls;
      78             :   struct GNUNET_TIME_Absolute last_date;
      79             :   enum GNUNET_DB_QueryStatus qs;
      80             : 
      81           2 :   qs = TEH_plugin->lookup_wire_timestamp (TEH_plugin->cls,
      82             :                                           awc->payto_uri,
      83             :                                           &last_date);
      84           2 :   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           2 :   if (last_date.abs_value_us > awc->validity_end.abs_value_us)
      96             :   {
      97           0 :     *mhd_ret = TALER_MHD_reply_with_error (
      98             :       connection,
      99             :       MHD_HTTP_CONFLICT,
     100             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_MORE_RECENT_PRESENT,
     101             :       NULL);
     102           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     103             :   }
     104           2 :   if (0 == qs)
     105             :   {
     106           1 :     *mhd_ret = TALER_MHD_reply_with_error (
     107             :       connection,
     108             :       MHD_HTTP_NOT_FOUND,
     109             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_NOT_FOUND,
     110             :       NULL);
     111           1 :     return GNUNET_DB_STATUS_HARD_ERROR;
     112             :   }
     113           1 :   qs = TEH_plugin->update_wire (TEH_plugin->cls,
     114             :                                 awc->payto_uri,
     115             :                                 awc->validity_end,
     116             :                                 false);
     117           1 :   if (qs < 0)
     118             :   {
     119           0 :     GNUNET_break (0);
     120           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     121           0 :       return qs;
     122           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     123             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     124             :                                            TALER_EC_GENERIC_DB_STORE_FAILED,
     125             :                                            "del wire");
     126           0 :     return qs;
     127             :   }
     128           1 :   return qs;
     129             : }
     130             : 
     131             : 
     132             : MHD_RESULT
     133           3 : TEH_handler_management_post_wire_disable (
     134             :   struct MHD_Connection *connection,
     135             :   const json_t *root)
     136             : {
     137             :   struct DelWireContext awc;
     138             :   struct GNUNET_JSON_Specification spec[] = {
     139           3 :     GNUNET_JSON_spec_fixed_auto ("master_sig_del",
     140             :                                  &awc.master_sig),
     141           3 :     GNUNET_JSON_spec_string ("payto_uri",
     142             :                              &awc.payto_uri),
     143           3 :     TALER_JSON_spec_absolute_time ("validity_end",
     144             :                                    &awc.validity_end),
     145           3 :     GNUNET_JSON_spec_end ()
     146             :   };
     147             : 
     148             :   {
     149             :     enum GNUNET_GenericReturnValue res;
     150             : 
     151           3 :     res = TALER_MHD_parse_json_data (connection,
     152             :                                      root,
     153             :                                      spec);
     154           3 :     if (GNUNET_SYSERR == res)
     155           0 :       return MHD_NO; /* hard failure */
     156           3 :     if (GNUNET_NO == res)
     157           0 :       return MHD_YES; /* failure */
     158             :   }
     159           3 :   if (GNUNET_OK !=
     160           3 :       TALER_exchange_offline_wire_del_verify (
     161             :         awc.payto_uri,
     162             :         awc.validity_end,
     163             :         &TEH_master_public_key,
     164             :         &awc.master_sig))
     165             :   {
     166           1 :     GNUNET_break_op (0);
     167           1 :     return TALER_MHD_reply_with_error (
     168             :       connection,
     169             :       MHD_HTTP_FORBIDDEN,
     170             :       TALER_EC_EXCHANGE_MANAGEMENT_WIRE_DEL_SIGNATURE_INVALID,
     171             :       NULL);
     172             :   }
     173             : 
     174             :   {
     175             :     enum GNUNET_GenericReturnValue res;
     176             :     MHD_RESULT ret;
     177             : 
     178           2 :     res = TEH_DB_run_transaction (connection,
     179             :                                   "del wire",
     180             :                                   &ret,
     181             :                                   &del_wire,
     182             :                                   &awc);
     183           2 :     if (GNUNET_SYSERR == res)
     184           1 :       return ret;
     185             :   }
     186           1 :   TEH_wire_update_state ();
     187           1 :   return TALER_MHD_reply_static (
     188             :     connection,
     189             :     MHD_HTTP_NO_CONTENT,
     190             :     NULL,
     191             :     NULL,
     192             :     0);
     193             : }
     194             : 
     195             : 
     196             : /* end of taler-exchange-httpd_management_wire_disable.c */

Generated by: LCOV version 1.14