LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_link.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 32 50 64.0 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2019 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_link.c
      18             :  * @brief Handle /coins/$COIN_PUB/link requests
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h>
      27             : #include "taler_mhd_lib.h"
      28             : #include "taler-exchange-httpd_mhd.h"
      29             : #include "taler-exchange-httpd_link.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : 
      32             : 
      33             : /**
      34             :  * Closure for #handle_link_data().
      35             :  */
      36             : struct HTD_Context
      37             : {
      38             : 
      39             :   /**
      40             :    * Public key of the coin for which we are running link.
      41             :    */
      42             :   struct TALER_CoinSpendPublicKeyP coin_pub;
      43             : 
      44             :   /**
      45             :    * Json array with transfer data we collect.
      46             :    */
      47             :   json_t *mlist;
      48             : 
      49             :   /**
      50             :    * Taler error code.
      51             :    */
      52             :   enum TALER_ErrorCode ec;
      53             : };
      54             : 
      55             : 
      56             : /**
      57             :  * Function called with the session hashes and transfer secret
      58             :  * information for a given coin.  Gets the linkage data and
      59             :  * builds the reply for the client.
      60             :  *
      61             :  * @param cls closure, a `struct HTD_Context`
      62             :  * @param transfer_pub public transfer key for the session
      63             :  * @param ldl link data related to @a transfer_pub
      64             :  */
      65             : static void
      66           1 : handle_link_data (void *cls,
      67             :                   const struct TALER_TransferPublicKeyP *transfer_pub,
      68             :                   const struct TALER_EXCHANGEDB_LinkList *ldl)
      69             : {
      70           1 :   struct HTD_Context *ctx = cls;
      71             :   json_t *list;
      72             : 
      73           1 :   if (NULL == ctx->mlist)
      74           0 :     return; /* we failed earlier */
      75           1 :   if (NULL == (list = json_array ()))
      76           0 :     goto fail;
      77             : 
      78           5 :   for (const struct TALER_EXCHANGEDB_LinkList *pos = ldl;
      79             :        NULL != pos;
      80           4 :        pos = pos->next)
      81             :   {
      82             :     json_t *obj;
      83             : 
      84           4 :     obj = GNUNET_JSON_PACK (
      85             :       GNUNET_JSON_pack_rsa_public_key ("denom_pub",
      86             :                                        pos->denom_pub.rsa_public_key),
      87             :       GNUNET_JSON_pack_rsa_signature ("ev_sig",
      88             :                                       pos->ev_sig.rsa_signature),
      89             :       GNUNET_JSON_pack_data_auto ("link_sig",
      90             :                                   &pos->orig_coin_link_sig));
      91           8 :     if ( (NULL == obj) ||
      92             :          (0 !=
      93           4 :           json_array_append_new (list,
      94             :                                  obj)) )
      95             :     {
      96           0 :       json_decref (list);
      97           0 :       goto fail;
      98             :     }
      99             :   }
     100             :   {
     101             :     json_t *root;
     102             : 
     103           1 :     root = GNUNET_JSON_PACK (
     104             :       GNUNET_JSON_pack_array_steal ("new_coins",
     105             :                                     list),
     106             :       GNUNET_JSON_pack_data_auto ("transfer_pub",
     107             :                                   transfer_pub));
     108           2 :     if ( (NULL == root) ||
     109             :          (0 !=
     110           1 :           json_array_append_new (ctx->mlist,
     111             :                                  root)) )
     112           0 :       goto fail;
     113             :   }
     114           1 :   return;
     115           0 : fail:
     116           0 :   ctx->ec = TALER_EC_GENERIC_JSON_ALLOCATION_FAILURE;
     117           0 :   json_decref (ctx->mlist);
     118           0 :   ctx->mlist = NULL;
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Execute a link operation.  Returns the linkage information that will allow
     124             :  * the owner of a coin to follow the trail to the refreshed coin.
     125             :  *
     126             :  * If it returns a non-error code, the transaction logic MUST NOT queue a MHD
     127             :  * response.  IF it returns an hard error, the transaction logic MUST queue a
     128             :  * MHD response and set @a mhd_ret.  IF it returns the soft error code, the
     129             :  * function MAY be called again to retry and MUST not queue a MHD response.
     130             :  *
     131             :  * @param cls closure
     132             :  * @param connection MHD request which triggered the transaction
     133             :  * @param[out] mhd_ret set to MHD response status for @a connection,
     134             :  *             if transaction failed (!)
     135             :  * @return transaction status
     136             :  */
     137             : static enum GNUNET_DB_QueryStatus
     138           1 : link_transaction (void *cls,
     139             :                   struct MHD_Connection *connection,
     140             :                   MHD_RESULT *mhd_ret)
     141             : {
     142           1 :   struct HTD_Context *ctx = cls;
     143             :   enum GNUNET_DB_QueryStatus qs;
     144             : 
     145           1 :   qs = TEH_plugin->get_link_data (TEH_plugin->cls,
     146           1 :                                   &ctx->coin_pub,
     147             :                                   &handle_link_data,
     148             :                                   ctx);
     149           1 :   if (NULL == ctx->mlist)
     150             :   {
     151           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     152             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     153             :                                            ctx->ec,
     154             :                                            NULL);
     155           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     156             :   }
     157           1 :   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     158             :   {
     159           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     160             :                                            MHD_HTTP_NOT_FOUND,
     161             :                                            TALER_EC_EXCHANGE_LINK_COIN_UNKNOWN,
     162             :                                            NULL);
     163           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     164             :   }
     165           1 :   return qs;
     166             : }
     167             : 
     168             : 
     169             : MHD_RESULT
     170           1 : TEH_handler_link (struct TEH_RequestContext *rc,
     171             :                   const char *const args[2])
     172             : {
     173             :   struct HTD_Context ctx;
     174             :   MHD_RESULT mhd_ret;
     175             : 
     176           1 :   memset (&ctx,
     177             :           0,
     178             :           sizeof (ctx));
     179           1 :   if (GNUNET_OK !=
     180           1 :       GNUNET_STRINGS_string_to_data (args[0],
     181             :                                      strlen (args[0]),
     182             :                                      &ctx.coin_pub,
     183             :                                      sizeof (ctx.coin_pub)))
     184             :   {
     185           0 :     GNUNET_break_op (0);
     186           0 :     return TALER_MHD_reply_with_error (rc->connection,
     187             :                                        MHD_HTTP_BAD_REQUEST,
     188             :                                        TALER_EC_EXCHANGE_GENERIC_COINS_INVALID_COIN_PUB,
     189             :                                        args[0]);
     190             :   }
     191           1 :   ctx.mlist = json_array ();
     192           1 :   GNUNET_assert (NULL != ctx.mlist);
     193           1 :   if (GNUNET_OK !=
     194           1 :       TEH_DB_run_transaction (rc->connection,
     195             :                               "run link",
     196             :                               &mhd_ret,
     197             :                               &link_transaction,
     198             :                               &ctx))
     199             :   {
     200           0 :     if (NULL != ctx.mlist)
     201           0 :       json_decref (ctx.mlist);
     202           0 :     return mhd_ret;
     203             :   }
     204           2 :   mhd_ret = TALER_MHD_reply_json (rc->connection,
     205           1 :                                   ctx.mlist,
     206             :                                   MHD_HTTP_OK);
     207           1 :   json_decref (ctx.mlist);
     208           1 :   return mhd_ret;
     209             : }
     210             : 
     211             : 
     212             : /* end of taler-exchange-httpd_link.c */

Generated by: LCOV version 1.14