LCOV - code coverage report
Current view: top level - lib - auditor_api_handle.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 94 129 72.9 %
Date: 2021-08-30 06:43:37 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/auditor_api_handle.c
      19             :  * @brief Implementation of the "handle" component of the auditor's HTTP API
      20             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <microhttpd.h>
      25             : #include <gnunet/gnunet_curl_lib.h>
      26             : #include "taler_json_lib.h"
      27             : #include "taler_auditor_service.h"
      28             : #include "taler_signatures.h"
      29             : #include "auditor_api_handle.h"
      30             : #include "auditor_api_curl_defaults.h"
      31             : #include "backoff.h"
      32             : 
      33             : /**
      34             :  * Which revision of the Taler auditor protocol is implemented
      35             :  * by this library?  Used to determine compatibility.
      36             :  */
      37             : #define TALER_PROTOCOL_CURRENT 0
      38             : 
      39             : /**
      40             :  * How many revisions back are we compatible to?
      41             :  */
      42             : #define TALER_PROTOCOL_AGE 0
      43             : 
      44             : 
      45             : /**
      46             :  * Log error related to CURL operations.
      47             :  *
      48             :  * @param type log level
      49             :  * @param function which function failed to run
      50             :  * @param code what was the curl error code
      51             :  */
      52             : #define CURL_STRERROR(type, function, code)      \
      53             :   GNUNET_log (type, "Curl function `%s' has failed at `%s:%d' with error: %s", \
      54             :               function, __FILE__, __LINE__, curl_easy_strerror (code));
      55             : 
      56             : /**
      57             :  * Stages of initialization for the `struct TALER_AUDITOR_Handle`
      58             :  */
      59             : enum AuditorHandleState
      60             : {
      61             :   /**
      62             :    * Just allocated.
      63             :    */
      64             :   MHS_INIT = 0,
      65             : 
      66             :   /**
      67             :    * Obtained the auditor's versioning data and version.
      68             :    */
      69             :   MHS_VERSION = 1,
      70             : 
      71             :   /**
      72             :    * Failed to initialize (fatal).
      73             :    */
      74             :   MHS_FAILED = 2
      75             : };
      76             : 
      77             : 
      78             : /**
      79             :  * Data for the request to get the /version of a auditor.
      80             :  */
      81             : struct VersionRequest;
      82             : 
      83             : 
      84             : /**
      85             :  * Handle to the auditor
      86             :  */
      87             : struct TALER_AUDITOR_Handle
      88             : {
      89             :   /**
      90             :    * The context of this handle
      91             :    */
      92             :   struct GNUNET_CURL_Context *ctx;
      93             : 
      94             :   /**
      95             :    * The URL of the auditor (i.e. "http://auditor.taler.net/")
      96             :    */
      97             :   char *url;
      98             : 
      99             :   /**
     100             :    * Function to call with the auditor's certification data,
     101             :    * NULL if this has already been done.
     102             :    */
     103             :   TALER_AUDITOR_VersionCallback version_cb;
     104             : 
     105             :   /**
     106             :    * Closure to pass to @e version_cb.
     107             :    */
     108             :   void *version_cb_cls;
     109             : 
     110             :   /**
     111             :    * Data for the request to get the /version of a auditor,
     112             :    * NULL once we are past stage #MHS_INIT.
     113             :    */
     114             :   struct VersionRequest *vr;
     115             : 
     116             :   /**
     117             :    * Task for retrying /version request.
     118             :    */
     119             :   struct GNUNET_SCHEDULER_Task *retry_task;
     120             : 
     121             :   /**
     122             :    * /version data of the auditor, only valid if
     123             :    * @e handshake_complete is past stage #MHS_VERSION.
     124             :    */
     125             :   char *version;
     126             : 
     127             :   /**
     128             :    * Version information for the callback.
     129             :    */
     130             :   struct TALER_AUDITOR_VersionInformation vi;
     131             : 
     132             :   /**
     133             :    * Retry /version frequency.
     134             :    */
     135             :   struct GNUNET_TIME_Relative retry_delay;
     136             : 
     137             :   /**
     138             :    * Stage of the auditor's initialization routines.
     139             :    */
     140             :   enum AuditorHandleState state;
     141             : 
     142             : };
     143             : 
     144             : 
     145             : /* ***************** Internal /version fetching ************* */
     146             : 
     147             : /**
     148             :  * Data for the request to get the /version of a auditor.
     149             :  */
     150             : struct VersionRequest
     151             : {
     152             :   /**
     153             :    * The connection to auditor this request handle will use
     154             :    */
     155             :   struct TALER_AUDITOR_Handle *auditor;
     156             : 
     157             :   /**
     158             :    * The url for this handle
     159             :    */
     160             :   char *url;
     161             : 
     162             :   /**
     163             :    * Entry for this request with the `struct GNUNET_CURL_Context`.
     164             :    */
     165             :   struct GNUNET_CURL_Job *job;
     166             : 
     167             : };
     168             : 
     169             : 
     170             : /**
     171             :  * Release memory occupied by a version request.
     172             :  * Note that this does not cancel the request
     173             :  * itself.
     174             :  *
     175             :  * @param vr request to free
     176             :  */
     177             : static void
     178          14 : free_version_request (struct VersionRequest *vr)
     179             : {
     180          14 :   GNUNET_free (vr->url);
     181          14 :   GNUNET_free (vr);
     182          14 : }
     183             : 
     184             : 
     185             : /**
     186             :  * Decode the JSON in @a resp_obj from the /version response and store the data
     187             :  * in the @a key_data.
     188             :  *
     189             :  * @param[in] resp_obj JSON object to parse
     190             :  * @param[out] auditor where to store the results we decoded
     191             :  * @param[out] vc where to store version compatibility data
     192             :  * @return #TALER_EC_NONE on success
     193             :  */
     194             : static enum TALER_ErrorCode
     195           3 : decode_version_json (const json_t *resp_obj,
     196             :                      struct TALER_AUDITOR_Handle *auditor,
     197             :                      enum TALER_AUDITOR_VersionCompatibility *vc)
     198             : {
     199           3 :   struct TALER_AUDITOR_VersionInformation *vi = &auditor->vi;
     200             :   unsigned int age;
     201             :   unsigned int revision;
     202             :   unsigned int current;
     203             :   const char *ver;
     204             :   struct GNUNET_JSON_Specification spec[] = {
     205           3 :     GNUNET_JSON_spec_string ("version",
     206             :                              &ver),
     207           3 :     GNUNET_JSON_spec_fixed_auto ("auditor_public_key",
     208             :                                  &vi->auditor_pub),
     209           3 :     GNUNET_JSON_spec_end ()
     210             :   };
     211             : 
     212           3 :   if (JSON_OBJECT != json_typeof (resp_obj))
     213             :   {
     214           0 :     GNUNET_break_op (0);
     215           0 :     return TALER_EC_GENERIC_JSON_INVALID;
     216             :   }
     217             :   /* check the version */
     218           3 :   if (GNUNET_OK !=
     219           3 :       GNUNET_JSON_parse (resp_obj,
     220             :                          spec,
     221             :                          NULL, NULL))
     222             :   {
     223           0 :     GNUNET_break_op (0);
     224           0 :     return TALER_EC_GENERIC_JSON_INVALID;
     225             :   }
     226           3 :   if (3 != sscanf (ver,
     227             :                    "%u:%u:%u",
     228             :                    &current,
     229             :                    &revision,
     230             :                    &age))
     231             :   {
     232           0 :     GNUNET_break_op (0);
     233           0 :     return TALER_EC_GENERIC_VERSION_MALFORMED;
     234             :   }
     235           3 :   auditor->version = GNUNET_strdup (ver);
     236           3 :   vi->version = auditor->version;
     237           3 :   *vc = TALER_AUDITOR_VC_MATCH;
     238           3 :   if (TALER_PROTOCOL_CURRENT < current)
     239             :   {
     240           0 :     *vc |= TALER_AUDITOR_VC_NEWER;
     241           0 :     if (TALER_PROTOCOL_CURRENT < current - age)
     242           0 :       *vc |= TALER_AUDITOR_VC_INCOMPATIBLE;
     243             :   }
     244             :   if (TALER_PROTOCOL_CURRENT > current)
     245             :   {
     246             :     *vc |= TALER_AUDITOR_VC_OLDER;
     247             :     if (TALER_PROTOCOL_CURRENT - TALER_PROTOCOL_AGE > current)
     248             :       *vc |= TALER_AUDITOR_VC_INCOMPATIBLE;
     249             :   }
     250           3 :   return TALER_EC_NONE;
     251             : }
     252             : 
     253             : 
     254             : /**
     255             :  * Initiate download of /version from the auditor.
     256             :  *
     257             :  * @param cls auditor where to download /version from
     258             :  */
     259             : static void
     260             : request_version (void *cls);
     261             : 
     262             : 
     263             : /**
     264             :  * Callback used when downloading the reply to a /version request
     265             :  * is complete.
     266             :  *
     267             :  * @param cls the `struct VersionRequest`
     268             :  * @param response_code HTTP response code, 0 on error
     269             :  * @param gresp_obj parsed JSON result, NULL on error, must be a `const json_t *`
     270             :  */
     271             : static void
     272          13 : version_completed_cb (void *cls,
     273             :                       long response_code,
     274             :                       const void *gresp_obj)
     275             : {
     276          13 :   const json_t *resp_obj = gresp_obj;
     277          13 :   struct VersionRequest *vr = cls;
     278          13 :   struct TALER_AUDITOR_Handle *auditor = vr->auditor;
     279             :   enum TALER_AUDITOR_VersionCompatibility vc;
     280          13 :   struct TALER_AUDITOR_HttpResponse hr = {
     281             :     .reply = resp_obj,
     282          13 :     .http_status = (unsigned int) response_code
     283             :   };
     284             : 
     285          13 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     286             :               "Received version from URL `%s' with status %ld.\n",
     287             :               vr->url,
     288             :               response_code);
     289          13 :   vc = TALER_AUDITOR_VC_PROTOCOL_ERROR;
     290          13 :   switch (response_code)
     291             :   {
     292          10 :   case 0:
     293             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     294             :     /* NOTE: this design is debatable. We MAY want to throw this error at the
     295             :        client. We may then still additionally internally re-try. */
     296          10 :     free_version_request (vr);
     297          10 :     auditor->vr = NULL;
     298          10 :     GNUNET_assert (NULL == auditor->retry_task);
     299          10 :     auditor->retry_delay = EXCHANGE_LIB_BACKOFF (auditor->retry_delay);
     300          10 :     auditor->retry_task = GNUNET_SCHEDULER_add_delayed (auditor->retry_delay,
     301             :                                                         &request_version,
     302             :                                                         auditor);
     303          10 :     return;
     304           3 :   case MHD_HTTP_OK:
     305           3 :     if (NULL == resp_obj)
     306             :     {
     307           0 :       GNUNET_break_op (0);
     308           0 :       TALER_LOG_WARNING ("NULL body for a 200-OK /version\n");
     309           0 :       hr.http_status = 0;
     310           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     311           0 :       break;
     312             :     }
     313           3 :     hr.ec = decode_version_json (resp_obj,
     314             :                                  auditor,
     315             :                                  &vc);
     316           3 :     if (TALER_EC_NONE != hr.ec)
     317             :     {
     318           0 :       GNUNET_break_op (0);
     319           0 :       hr.http_status = 0;
     320           0 :       break;
     321             :     }
     322           3 :     auditor->retry_delay = GNUNET_TIME_UNIT_ZERO; /* restart quickly */
     323           3 :     break;
     324           0 :   default:
     325           0 :     hr.ec = TALER_JSON_get_error_code (resp_obj);
     326           0 :     hr.hint = TALER_JSON_get_error_hint (resp_obj);
     327           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     328             :                 "Unexpected response code %u/%d\n",
     329             :                 (unsigned int) response_code,
     330             :                 (int) hr.ec);
     331           0 :     break;
     332             :   }
     333           3 :   if (MHD_HTTP_OK != response_code)
     334             :   {
     335           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     336             :                 "/version failed for auditor %p: %u!\n",
     337             :                 auditor,
     338             :                 (unsigned int) response_code);
     339           0 :     auditor->vr = NULL;
     340           0 :     free_version_request (vr);
     341           0 :     auditor->state = MHS_FAILED;
     342             :     /* notify application that we failed */
     343           0 :     auditor->version_cb (auditor->version_cb_cls,
     344             :                          &hr,
     345             :                          NULL,
     346             :                          vc);
     347           0 :     return;
     348             :   }
     349             : 
     350           3 :   auditor->vr = NULL;
     351           3 :   free_version_request (vr);
     352           3 :   TALER_LOG_DEBUG ("Switching auditor state to 'version'\n");
     353           3 :   auditor->state = MHS_VERSION;
     354           3 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     355             :               "Auditor %p is now READY!\n",
     356             :               auditor);
     357             :   /* notify application about the key information */
     358           3 :   auditor->version_cb (auditor->version_cb_cls,
     359             :                        &hr,
     360           3 :                        &auditor->vi,
     361             :                        vc);
     362             : }
     363             : 
     364             : 
     365             : /* ********************* library internal API ********* */
     366             : 
     367             : 
     368             : /**
     369             :  * Get the context of a auditor.
     370             :  *
     371             :  * @param h the auditor handle to query
     372             :  * @return ctx context to execute jobs in
     373             :  */
     374             : struct GNUNET_CURL_Context *
     375           2 : TALER_AUDITOR_handle_to_context_ (struct TALER_AUDITOR_Handle *h)
     376             : {
     377           2 :   return h->ctx;
     378             : }
     379             : 
     380             : 
     381             : /**
     382             :  * Check if the handle is ready to process requests.
     383             :  *
     384             :  * @param h the auditor handle to query
     385             :  * @return #GNUNET_YES if we are ready, #GNUNET_NO if not
     386             :  */
     387             : int
     388           2 : TALER_AUDITOR_handle_is_ready_ (struct TALER_AUDITOR_Handle *h)
     389             : {
     390           2 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     391             :               "Checking if auditor %p (%s) is now ready: %s\n",
     392             :               h,
     393             :               h->url,
     394             :               (MHD_VERSION == h->state) ? "yes" : "no");
     395           2 :   return (MHS_VERSION == h->state) ? GNUNET_YES : GNUNET_NO;
     396             : }
     397             : 
     398             : 
     399             : /**
     400             :  * Obtain the URL to use for an API request.
     401             :  *
     402             :  * @param h handle for the auditor
     403             :  * @param path Taler API path (i.e. "/deposit-confirmation")
     404             :  * @return the full URL to use with cURL
     405             :  */
     406             : char *
     407          16 : TALER_AUDITOR_path_to_url_ (struct TALER_AUDITOR_Handle *h,
     408             :                             const char *path)
     409             : {
     410          16 :   GNUNET_assert ('/' == path[0]);
     411          16 :   return TALER_url_join (h->url,
     412             :                          path + 1,
     413             :                          NULL);
     414             : }
     415             : 
     416             : 
     417             : /* ********************* public API ******************* */
     418             : 
     419             : 
     420             : /**
     421             :  * Initialise a connection to the auditor. Will connect to the
     422             :  * auditor and obtain information about the auditor's master public
     423             :  * key and the auditor's auditor.  The respective information will
     424             :  * be passed to the @a version_cb once available, and all future
     425             :  * interactions with the auditor will be checked to be signed
     426             :  * (where appropriate) by the respective master key.
     427             :  *
     428             :  * @param ctx the context
     429             :  * @param url HTTP base URL for the auditor
     430             :  * @param version_cb function to call with the
     431             :  *        auditor's version information
     432             :  * @param version_cb_cls closure for @a version_cb
     433             :  * @return the auditor handle; NULL upon error
     434             :  */
     435             : struct TALER_AUDITOR_Handle *
     436           9 : TALER_AUDITOR_connect (struct GNUNET_CURL_Context *ctx,
     437             :                        const char *url,
     438             :                        TALER_AUDITOR_VersionCallback version_cb,
     439             :                        void *version_cb_cls)
     440             : {
     441             :   struct TALER_AUDITOR_Handle *auditor;
     442             : 
     443             :   /* Disable 100 continue processing */
     444           9 :   GNUNET_break (GNUNET_OK ==
     445             :                 GNUNET_CURL_append_header (ctx,
     446             :                                            "Expect:"));
     447           9 :   auditor = GNUNET_new (struct TALER_AUDITOR_Handle);
     448           9 :   auditor->retry_delay = GNUNET_TIME_UNIT_SECONDS; /* start slowly */
     449           9 :   auditor->ctx = ctx;
     450           9 :   auditor->url = GNUNET_strdup (url);
     451           9 :   auditor->version_cb = version_cb;
     452           9 :   auditor->version_cb_cls = version_cb_cls;
     453           9 :   auditor->retry_task = GNUNET_SCHEDULER_add_now (&request_version,
     454             :                                                   auditor);
     455           9 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     456             :               "Connecting to auditor at URL `%s' (%p).\n",
     457             :               url,
     458             :               auditor);
     459           9 :   return auditor;
     460             : }
     461             : 
     462             : 
     463             : /**
     464             :  * Initiate download of /version from the auditor.
     465             :  *
     466             :  * @param cls auditor where to download /version from
     467             :  */
     468             : static void
     469          14 : request_version (void *cls)
     470             : {
     471          14 :   struct TALER_AUDITOR_Handle *auditor = cls;
     472             :   struct VersionRequest *vr;
     473             :   CURL *eh;
     474             : 
     475          14 :   auditor->retry_task = NULL;
     476          14 :   GNUNET_assert (NULL == auditor->vr);
     477          14 :   vr = GNUNET_new (struct VersionRequest);
     478          14 :   vr->auditor = auditor;
     479          14 :   vr->url = TALER_AUDITOR_path_to_url_ (auditor,
     480             :                                         "/version");
     481          14 :   if (NULL == vr->url)
     482             :   {
     483           0 :     struct TALER_AUDITOR_HttpResponse hr = {
     484             :       .ec = TALER_EC_GENERIC_CONFIGURATION_INVALID
     485             :     };
     486             : 
     487           0 :     auditor->version_cb (auditor->version_cb_cls,
     488             :                          &hr,
     489             :                          NULL,
     490             :                          TALER_AUDITOR_VC_PROTOCOL_ERROR);
     491           0 :     return;
     492             :   }
     493          14 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     494             :               "Requesting auditor version with URL `%s'.\n",
     495             :               vr->url);
     496          14 :   eh = TALER_AUDITOR_curl_easy_get_ (vr->url);
     497          14 :   if (NULL == eh)
     498             :   {
     499           0 :     GNUNET_break (0);
     500           0 :     auditor->retry_delay = EXCHANGE_LIB_BACKOFF (auditor->retry_delay);
     501           0 :     auditor->retry_task = GNUNET_SCHEDULER_add_delayed (auditor->retry_delay,
     502             :                                                         &request_version,
     503             :                                                         auditor);
     504           0 :     return;
     505             :   }
     506          14 :   GNUNET_break (CURLE_OK ==
     507             :                 curl_easy_setopt (eh,
     508             :                                   CURLOPT_TIMEOUT,
     509             :                                   (long) 300));
     510          14 :   vr->job = GNUNET_CURL_job_add (auditor->ctx,
     511             :                                  eh,
     512             :                                  &version_completed_cb,
     513             :                                  vr);
     514          14 :   auditor->vr = vr;
     515             : }
     516             : 
     517             : 
     518             : /**
     519             :  * Disconnect from the auditor
     520             :  *
     521             :  * @param auditor the auditor handle
     522             :  */
     523             : void
     524           9 : TALER_AUDITOR_disconnect (struct TALER_AUDITOR_Handle *auditor)
     525             : {
     526           9 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     527             :               "Disconnecting from auditor at URL `%s' (%p).\n",
     528             :               auditor->url,
     529             :               auditor);
     530           9 :   if (NULL != auditor->vr)
     531             :   {
     532           1 :     GNUNET_CURL_job_cancel (auditor->vr->job);
     533           1 :     free_version_request (auditor->vr);
     534           1 :     auditor->vr = NULL;
     535             :   }
     536           9 :   GNUNET_free (auditor->version);
     537           9 :   if (NULL != auditor->retry_task)
     538             :   {
     539           5 :     GNUNET_SCHEDULER_cancel (auditor->retry_task);
     540           5 :     auditor->retry_task = NULL;
     541             :   }
     542           9 :   GNUNET_free (auditor->url);
     543           9 :   GNUNET_free (auditor);
     544           9 : }
     545             : 
     546             : 
     547             : /* end of auditor_api_handle.c */

Generated by: LCOV version 1.14