LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_management_auditors.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 32 47 68.1 %
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, 2021 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_auditors.c
      18             :  * @brief Handle request to add auditor.
      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_keys.h"
      32             : 
      33             : 
      34             : /**
      35             :  * Closure for the #add_auditor transaction.
      36             :  */
      37             : struct AddAuditorContext
      38             : {
      39             :   /**
      40             :    * Master signature to store.
      41             :    */
      42             :   struct TALER_MasterSignatureP master_sig;
      43             : 
      44             :   /**
      45             :    * Auditor public key this is about.
      46             :    */
      47             :   struct TALER_AuditorPublicKeyP auditor_pub;
      48             : 
      49             :   /**
      50             :    * Auditor URL this is about.
      51             :    */
      52             :   const char *auditor_url;
      53             : 
      54             :   /**
      55             :    * Human readable name of the auditor.
      56             :    */
      57             :   const char *auditor_name;
      58             : 
      59             :   /**
      60             :    * Timestamp for checking against replay attacks.
      61             :    */
      62             :   struct GNUNET_TIME_Absolute validity_start;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function implementing database transaction to add an auditor.  Runs the
      69             :  * transaction logic; IF it returns a non-error code, the transaction logic
      70             :  * MUST NOT queue a MHD response.  IF it returns an hard error, the
      71             :  * transaction logic MUST queue a MHD response and set @a mhd_ret.  IF it
      72             :  * returns the soft error code, the function MAY be called again to retry and
      73             :  * MUST not queue a MHD response.
      74             :  *
      75             :  * @param cls closure with a `struct AddAuditorContext`
      76             :  * @param connection MHD request which triggered the transaction
      77             :  * @param[out] mhd_ret set to MHD response status for @a connection,
      78             :  *             if transaction failed (!)
      79             :  * @return transaction status
      80             :  */
      81             : static enum GNUNET_DB_QueryStatus
      82           8 : add_auditor (void *cls,
      83             :              struct MHD_Connection *connection,
      84             :              MHD_RESULT *mhd_ret)
      85             : {
      86           8 :   struct AddAuditorContext *aac = cls;
      87             :   struct GNUNET_TIME_Absolute last_date;
      88             :   enum GNUNET_DB_QueryStatus qs;
      89             : 
      90           8 :   qs = TEH_plugin->lookup_auditor_timestamp (TEH_plugin->cls,
      91           8 :                                              &aac->auditor_pub,
      92             :                                              &last_date);
      93           8 :   if (qs < 0)
      94             :   {
      95           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      96           0 :       return qs;
      97           0 :     GNUNET_break (0);
      98           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
      99             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     100             :                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
     101             :                                            "lookup auditor");
     102           0 :     return qs;
     103             :   }
     104           8 :   if ( (0 < qs) &&
     105           1 :        (last_date.abs_value_us > aac->validity_start.abs_value_us) )
     106             :   {
     107           0 :     *mhd_ret = TALER_MHD_reply_with_error (
     108             :       connection,
     109             :       MHD_HTTP_CONFLICT,
     110             :       TALER_EC_EXCHANGE_MANAGEMENT_AUDITOR_MORE_RECENT_PRESENT,
     111             :       NULL);
     112           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     113             :   }
     114           8 :   if (0 == qs)
     115           7 :     qs = TEH_plugin->insert_auditor (TEH_plugin->cls,
     116           7 :                                      &aac->auditor_pub,
     117             :                                      aac->auditor_url,
     118             :                                      aac->auditor_name,
     119             :                                      aac->validity_start);
     120             :   else
     121           1 :     qs = TEH_plugin->update_auditor (TEH_plugin->cls,
     122           1 :                                      &aac->auditor_pub,
     123             :                                      aac->auditor_url,
     124             :                                      aac->auditor_name,
     125             :                                      aac->validity_start,
     126             :                                      true);
     127           8 :   if (qs < 0)
     128             :   {
     129           0 :     GNUNET_break (0);
     130           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     131           0 :       return qs;
     132           0 :     *mhd_ret = TALER_MHD_reply_with_error (connection,
     133             :                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
     134             :                                            TALER_EC_GENERIC_DB_STORE_FAILED,
     135             :                                            "add auditor");
     136           0 :     return qs;
     137             :   }
     138           8 :   TEH_keys_update_states ();
     139           8 :   return qs;
     140             : }
     141             : 
     142             : 
     143             : MHD_RESULT
     144           9 : TEH_handler_management_auditors (
     145             :   struct MHD_Connection *connection,
     146             :   const json_t *root)
     147             : {
     148             :   struct AddAuditorContext aac;
     149             :   struct GNUNET_JSON_Specification spec[] = {
     150           9 :     GNUNET_JSON_spec_fixed_auto ("master_sig",
     151             :                                  &aac.master_sig),
     152           9 :     GNUNET_JSON_spec_fixed_auto ("auditor_pub",
     153             :                                  &aac.auditor_pub),
     154           9 :     GNUNET_JSON_spec_string ("auditor_url",
     155             :                              &aac.auditor_url),
     156           9 :     GNUNET_JSON_spec_string ("auditor_name",
     157             :                              &aac.auditor_name),
     158           9 :     TALER_JSON_spec_absolute_time ("validity_start",
     159             :                                    &aac.validity_start),
     160           9 :     GNUNET_JSON_spec_end ()
     161             :   };
     162             :   MHD_RESULT res;
     163             :   enum GNUNET_GenericReturnValue ret;
     164             : 
     165           9 :   ret = TALER_MHD_parse_json_data (connection,
     166             :                                    root,
     167             :                                    spec);
     168           9 :   if (GNUNET_SYSERR == ret)
     169           0 :     return MHD_NO;   /* hard failure */
     170           9 :   if (GNUNET_NO == ret)
     171           0 :     return MHD_YES;   /* failure */
     172           9 :   if (GNUNET_OK !=
     173           9 :       TALER_exchange_offline_auditor_add_verify (
     174             :         &aac.auditor_pub,
     175             :         aac.auditor_url,
     176             :         aac.validity_start,
     177             :         &TEH_master_public_key,
     178             :         &aac.master_sig))
     179             :   {
     180           1 :     GNUNET_break_op (0);
     181           1 :     return TALER_MHD_reply_with_error (
     182             :       connection,
     183             :       MHD_HTTP_FORBIDDEN,
     184             :       TALER_EC_EXCHANGE_MANAGEMENT_AUDITOR_ADD_SIGNATURE_INVALID,
     185             :       NULL);
     186             :   }
     187             : 
     188           8 :   ret = TEH_DB_run_transaction (connection,
     189             :                                 "add auditor",
     190             :                                 &res,
     191             :                                 &add_auditor,
     192             :                                 &aac);
     193           8 :   if (GNUNET_SYSERR == ret)
     194           0 :     return res;
     195           8 :   return TALER_MHD_reply_static (
     196             :     connection,
     197             :     MHD_HTTP_NO_CONTENT,
     198             :     NULL,
     199             :     NULL,
     200             :     0);
     201             : }
     202             : 
     203             : 
     204             : /* end of taler-exchange-httpd_management_auditors.c */

Generated by: LCOV version 1.14