LCOV - code coverage report
Current view: top level - auditor - taler-auditor-exchange.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 39 68 57.4 %
Date: 2021-08-30 06:43:37 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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 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 <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-auditor-exchange.c
      18             :  * @brief Tool used by the auditor to add or remove the exchange's master key
      19             :  *        to its database.
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include <platform.h>
      23             : #include "taler_exchangedb_lib.h"
      24             : #include "taler_auditordb_lib.h"
      25             : 
      26             : 
      27             : /**
      28             :  * URL of the exchange.
      29             :  */
      30             : static char *exchange_url;
      31             : 
      32             : /**
      33             :  * Master public key of the exchange.
      34             :  */
      35             : static struct TALER_MasterPublicKeyP master_public_key;
      36             : 
      37             : /**
      38             :  * Our configuration.
      39             :  */
      40             : static struct GNUNET_CONFIGURATION_Handle *cfg;
      41             : 
      42             : /**
      43             :  * Handle to access the auditor's database.
      44             :  */
      45             : static struct TALER_AUDITORDB_Plugin *adb;
      46             : 
      47             : /**
      48             :  * -r option given.
      49             :  */
      50             : static int remove_flag;
      51             : 
      52             : 
      53             : /**
      54             :  * The main function of the taler-auditor-exchange tool.  This tool is used
      55             :  * to add (or remove) an exchange's master key and base URL to the auditor's
      56             :  * database.
      57             :  *
      58             :  * @param argc number of arguments from the command line
      59             :  * @param argv command line arguments
      60             :  * @return 0 ok, non-zero on error
      61             :  */
      62             : int
      63          11 : main (int argc,
      64             :       char *const *argv)
      65             : {
      66          11 :   char *cfgfile = NULL;
      67          11 :   const struct GNUNET_GETOPT_CommandLineOption options[] = {
      68          11 :     GNUNET_GETOPT_option_cfgfile (&cfgfile),
      69          11 :     GNUNET_GETOPT_option_help (
      70             :       "Add or remove exchange to list of audited exchanges"),
      71          11 :     GNUNET_GETOPT_option_mandatory
      72             :       (GNUNET_GETOPT_option_base32_auto ('m',
      73             :                                          "exchange-key",
      74             :                                          "KEY",
      75             :                                          "public key of the exchange (Crockford base32 encoded)",
      76             :                                          &master_public_key)),
      77          11 :     GNUNET_GETOPT_option_string ('u',
      78             :                                  "exchange-url",
      79             :                                  "URL",
      80             :                                  "base URL of the exchange",
      81             :                                  &exchange_url),
      82          11 :     GNUNET_GETOPT_option_flag ('r',
      83             :                                "remove",
      84             :                                "remove the exchange's key (default is to add)",
      85             :                                &remove_flag),
      86          11 :     GNUNET_GETOPT_option_version (VERSION "-" VCS_VERSION),
      87             :     GNUNET_GETOPT_OPTION_END
      88             :   };
      89             : 
      90          11 :   TALER_gcrypt_init (); /* must trigger initialization manually at this point! */
      91          11 :   GNUNET_assert (GNUNET_OK ==
      92             :                  GNUNET_log_setup ("taler-auditor-exchange",
      93             :                                    "WARNING",
      94             :                                    NULL));
      95             :   {
      96             :     int ret;
      97             : 
      98          11 :     ret = GNUNET_GETOPT_run ("taler-auditor-exchange",
      99             :                              options,
     100             :                              argc, argv);
     101          11 :     if (GNUNET_NO == ret)
     102           0 :       return EXIT_SUCCESS;
     103          11 :     if (GNUNET_SYSERR == ret)
     104           0 :       return EXIT_INVALIDARGUMENT;
     105             :   }
     106          11 :   if (NULL == cfgfile)
     107           0 :     cfgfile = GNUNET_CONFIGURATION_default_filename ();
     108          11 :   if (NULL == cfgfile)
     109             :   {
     110           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     111             :                 "Can't find default configuration file.\n");
     112           0 :     return EXIT_NOTCONFIGURED;
     113             :   }
     114          11 :   cfg = GNUNET_CONFIGURATION_create ();
     115             : 
     116          11 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     117             :               "Loading config file: %s\n",
     118             :               cfgfile);
     119             : 
     120          11 :   if (GNUNET_SYSERR ==
     121          11 :       GNUNET_CONFIGURATION_load (cfg,
     122             :                                  cfgfile))
     123             :   {
     124           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     125             :                 "Malformed configuration file `%s', exit ...\n",
     126             :                 cfgfile);
     127           0 :     GNUNET_free (cfgfile);
     128           0 :     return EXIT_NOTCONFIGURED;
     129             :   }
     130          11 :   GNUNET_free (cfgfile);
     131             : 
     132          11 :   if (! remove_flag)
     133             :   {
     134          11 :     if (NULL == exchange_url)
     135             :     {
     136           0 :       fprintf (stderr,
     137             :                _ ("Missing either `%s' or `%s'.\n"),
     138             :                "-u URL",
     139             :                "--remove");
     140           0 :       return EXIT_INVALIDARGUMENT;
     141             :     }
     142          11 :     if ( (0 == strlen (exchange_url)) ||
     143          11 :          ( (0 != strncasecmp ("http://",
     144             :                               exchange_url,
     145           0 :                               strlen ("http://"))) &&
     146           0 :            (0 != strncasecmp ("https://",
     147             :                               exchange_url,
     148          11 :                               strlen ("https://"))) ) ||
     149          11 :          ('/' != exchange_url[strlen (exchange_url) - 1]) )
     150             :     {
     151           0 :       fprintf (stderr,
     152             :                "Exchange URL must begin with `http://` or `https://` and end with `/'\n");
     153           0 :       return EXIT_INVALIDARGUMENT;
     154             :     }
     155             :   }
     156             : 
     157             : 
     158          11 :   if (NULL ==
     159          11 :       (adb = TALER_AUDITORDB_plugin_load (cfg)))
     160             :   {
     161           0 :     fprintf (stderr,
     162             :              "Failed to initialize auditor database plugin.\n");
     163           0 :     return EXIT_NOTINSTALLED;
     164             :   }
     165             : 
     166             :   /* Create required tables */
     167          11 :   if (GNUNET_OK !=
     168          11 :       adb->create_tables (adb->cls))
     169             :   {
     170           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     171             :                 "Failed to create tables in auditor's database\n");
     172           0 :     TALER_AUDITORDB_plugin_unload (adb);
     173           0 :     return EXIT_NOPERMISSION;
     174             :   }
     175             : 
     176             :   /* Update DB */
     177             :   {
     178             :     enum GNUNET_DB_QueryStatus qs;
     179             : 
     180          11 :     if (GNUNET_SYSERR ==
     181          11 :         adb->preflight (adb->cls))
     182             :     {
     183           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     184             :                   "Failed to initialize database connection\n");
     185           0 :       TALER_AUDITORDB_plugin_unload (adb);
     186           0 :       return EXIT_FAILURE;
     187             :     }
     188             : 
     189          11 :     if (remove_flag)
     190             :     {
     191           0 :       qs = adb->delete_exchange (adb->cls,
     192             :                                  &master_public_key);
     193             :     }
     194             :     else
     195             :     {
     196          11 :       qs = adb->insert_exchange (adb->cls,
     197             :                                  &master_public_key,
     198             :                                  exchange_url);
     199             :     }
     200          11 :     if (0 > qs)
     201             :     {
     202           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     203             :                   "Failed to update auditor database (status code: %d)\n",
     204             :                   qs);
     205           0 :       TALER_AUDITORDB_plugin_unload (adb);
     206           0 :       return EXIT_FAILURE;
     207             :     }
     208          11 :     if (0 == qs)
     209             :     {
     210           0 :       GNUNET_log (
     211             :         GNUNET_ERROR_TYPE_WARNING,
     212             :         (remove_flag)
     213             :         ? "Could not remove exchange from database: entry already absent\n"
     214             :         : "Could not add exchange to database: entry already exists\n");
     215           0 :       TALER_AUDITORDB_plugin_unload (adb);
     216           0 :       return EXIT_FAILURE;
     217             :     }
     218             :   }
     219          11 :   TALER_AUDITORDB_plugin_unload (adb);
     220          11 :   return EXIT_SUCCESS;
     221             : }
     222             : 
     223             : 
     224             : /* end of taler-auditor-exchange.c */

Generated by: LCOV version 1.14